aboutsummaryrefslogtreecommitdiffstats
path: root/net/socket.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/socket.c')
-rw-r--r--net/socket.c1028
1 files changed, 586 insertions, 442 deletions
diff --git a/net/socket.c b/net/socket.c
index 6d261bf206fc..1bc4167e0da8 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,17 +53,17 @@
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 */
60 60
61#include <linux/mm.h> 61#include <linux/mm.h>
62#include <linux/smp_lock.h>
63#include <linux/socket.h> 62#include <linux/socket.h>
64#include <linux/file.h> 63#include <linux/file.h>
65#include <linux/net.h> 64#include <linux/net.h>
66#include <linux/interrupt.h> 65#include <linux/interrupt.h>
66#include <linux/rcupdate.h>
67#include <linux/netdevice.h> 67#include <linux/netdevice.h>
68#include <linux/proc_fs.h> 68#include <linux/proc_fs.h>
69#include <linux/seq_file.h> 69#include <linux/seq_file.h>
@@ -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
@@ -147,52 +146,8 @@ static struct file_operations socket_file_ops = {
147 * The protocol list. Each protocol is registered in here. 146 * The protocol list. Each protocol is registered in here.
148 */ 147 */
149 148
150static struct net_proto_family *net_families[NPROTO];
151
152#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
153static atomic_t net_family_lockct = ATOMIC_INIT(0);
154static DEFINE_SPINLOCK(net_family_lock); 149static DEFINE_SPINLOCK(net_family_lock);
155 150static const struct net_proto_family *net_families[NPROTO] __read_mostly;
156/* The strategy is: modifications net_family vector are short, do not
157 sleep and veeery rare, but read access should be free of any exclusive
158 locks.
159 */
160
161static void net_family_write_lock(void)
162{
163 spin_lock(&net_family_lock);
164 while (atomic_read(&net_family_lockct) != 0) {
165 spin_unlock(&net_family_lock);
166
167 yield();
168
169 spin_lock(&net_family_lock);
170 }
171}
172
173static __inline__ void net_family_write_unlock(void)
174{
175 spin_unlock(&net_family_lock);
176}
177
178static __inline__ void net_family_read_lock(void)
179{
180 atomic_inc(&net_family_lockct);
181 spin_unlock_wait(&net_family_lock);
182}
183
184static __inline__ void net_family_read_unlock(void)
185{
186 atomic_dec(&net_family_lockct);
187}
188
189#else
190#define net_family_write_lock() do { } while(0)
191#define net_family_write_unlock() do { } while(0)
192#define net_family_read_lock() do { } while(0)
193#define net_family_read_unlock() do { } while(0)
194#endif
195
196 151
197/* 152/*
198 * Statistics counters of the socket lists 153 * Statistics counters of the socket lists
@@ -201,19 +156,20 @@ static __inline__ void net_family_read_unlock(void)
201static DEFINE_PER_CPU(int, sockets_in_use) = 0; 156static DEFINE_PER_CPU(int, sockets_in_use) = 0;
202 157
203/* 158/*
204 * Support routines. Move socket addresses back and forth across the kernel/user 159 * Support routines.
205 * divide and look after the messy bits. 160 * Move socket addresses back and forth across the kernel/user
161 * divide and look after the messy bits.
206 */ 162 */
207 163
208#define MAX_SOCK_ADDR 128 /* 108 for Unix domain - 164#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
209 16 for IP, 16 for IPX, 165 16 for IP, 16 for IPX,
210 24 for IPv6, 166 24 for IPv6,
211 about 80 for AX.25 167 about 80 for AX.25
212 must be at least one bigger than 168 must be at least one bigger than
213 the AF_UNIX size (see net/unix/af_unix.c 169 the AF_UNIX size (see net/unix/af_unix.c
214 :unix_mkname()). 170 :unix_mkname()).
215 */ 171 */
216 172
217/** 173/**
218 * move_addr_to_kernel - copy a socket address into kernel space 174 * move_addr_to_kernel - copy a socket address into kernel space
219 * @uaddr: Address in user space 175 * @uaddr: Address in user space
@@ -227,11 +183,11 @@ static DEFINE_PER_CPU(int, sockets_in_use) = 0;
227 183
228int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr) 184int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
229{ 185{
230 if(ulen<0||ulen>MAX_SOCK_ADDR) 186 if (ulen < 0 || ulen > MAX_SOCK_ADDR)
231 return -EINVAL; 187 return -EINVAL;
232 if(ulen==0) 188 if (ulen == 0)
233 return 0; 189 return 0;
234 if(copy_from_user(kaddr,uaddr,ulen)) 190 if (copy_from_user(kaddr, uaddr, ulen))
235 return -EFAULT; 191 return -EFAULT;
236 return audit_sockaddr(ulen, kaddr); 192 return audit_sockaddr(ulen, kaddr);
237} 193}
@@ -252,51 +208,52 @@ 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 208 * length of the data is written over the length limit the user
253 * specified. Zero is returned for a success. 209 * specified. Zero is returned for a success.
254 */ 210 */
255 211
256int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen) 212int move_addr_to_user(void *kaddr, int klen, void __user *uaddr,
213 int __user *ulen)
257{ 214{
258 int err; 215 int err;
259 int len; 216 int len;
260 217
261 if((err=get_user(len, ulen))) 218 err = get_user(len, ulen);
219 if (err)
262 return err; 220 return err;
263 if(len>klen) 221 if (len > klen)
264 len=klen; 222 len = klen;
265 if(len<0 || len> MAX_SOCK_ADDR) 223 if (len < 0 || len > MAX_SOCK_ADDR)
266 return -EINVAL; 224 return -EINVAL;
267 if(len) 225 if (len) {
268 {
269 if (audit_sockaddr(klen, kaddr)) 226 if (audit_sockaddr(klen, kaddr))
270 return -ENOMEM; 227 return -ENOMEM;
271 if(copy_to_user(uaddr,kaddr,len)) 228 if (copy_to_user(uaddr, kaddr, len))
272 return -EFAULT; 229 return -EFAULT;
273 } 230 }
274 /* 231 /*
275 * "fromlen shall refer to the value before truncation.." 232 * "fromlen shall refer to the value before truncation.."
276 * 1003.1g 233 * 1003.1g
277 */ 234 */
278 return __put_user(klen, ulen); 235 return __put_user(klen, ulen);
279} 236}
280 237
281#define SOCKFS_MAGIC 0x534F434B 238#define SOCKFS_MAGIC 0x534F434B
282 239
283static kmem_cache_t * sock_inode_cachep __read_mostly; 240static kmem_cache_t *sock_inode_cachep __read_mostly;
284 241
285static struct inode *sock_alloc_inode(struct super_block *sb) 242static struct inode *sock_alloc_inode(struct super_block *sb)
286{ 243{
287 struct socket_alloc *ei; 244 struct socket_alloc *ei;
288 ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL); 245
246 ei = kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
289 if (!ei) 247 if (!ei)
290 return NULL; 248 return NULL;
291 init_waitqueue_head(&ei->socket.wait); 249 init_waitqueue_head(&ei->socket.wait);
292 250
293 ei->socket.fasync_list = NULL; 251 ei->socket.fasync_list = NULL;
294 ei->socket.state = SS_UNCONNECTED; 252 ei->socket.state = SS_UNCONNECTED;
295 ei->socket.flags = 0; 253 ei->socket.flags = 0;
296 ei->socket.ops = NULL; 254 ei->socket.ops = NULL;
297 ei->socket.sk = NULL; 255 ei->socket.sk = NULL;
298 ei->socket.file = NULL; 256 ei->socket.file = NULL;
299 ei->socket.flags = 0;
300 257
301 return &ei->vfs_inode; 258 return &ei->vfs_inode;
302} 259}
@@ -307,22 +264,25 @@ static void sock_destroy_inode(struct inode *inode)
307 container_of(inode, struct socket_alloc, vfs_inode)); 264 container_of(inode, struct socket_alloc, vfs_inode));
308} 265}
309 266
310static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) 267static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
311{ 268{
312 struct socket_alloc *ei = (struct socket_alloc *) foo; 269 struct socket_alloc *ei = (struct socket_alloc *)foo;
313 270
314 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == 271 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR))
315 SLAB_CTOR_CONSTRUCTOR) 272 == SLAB_CTOR_CONSTRUCTOR)
316 inode_init_once(&ei->vfs_inode); 273 inode_init_once(&ei->vfs_inode);
317} 274}
318 275
319static int init_inodecache(void) 276static int init_inodecache(void)
320{ 277{
321 sock_inode_cachep = kmem_cache_create("sock_inode_cache", 278 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
322 sizeof(struct socket_alloc), 279 sizeof(struct socket_alloc),
323 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 280 0,
324 SLAB_MEM_SPREAD), 281 (SLAB_HWCACHE_ALIGN |
325 init_once, NULL); 282 SLAB_RECLAIM_ACCOUNT |
283 SLAB_MEM_SPREAD),
284 init_once,
285 NULL);
326 if (sock_inode_cachep == NULL) 286 if (sock_inode_cachep == NULL)
327 return -ENOMEM; 287 return -ENOMEM;
328 return 0; 288 return 0;
@@ -335,7 +295,8 @@ static struct super_operations sockfs_ops = {
335}; 295};
336 296
337static int sockfs_get_sb(struct file_system_type *fs_type, 297static int sockfs_get_sb(struct file_system_type *fs_type,
338 int flags, const char *dev_name, void *data, struct vfsmount *mnt) 298 int flags, const char *dev_name, void *data,
299 struct vfsmount *mnt)
339{ 300{
340 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC, 301 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
341 mnt); 302 mnt);
@@ -348,12 +309,13 @@ static struct file_system_type sock_fs_type = {
348 .get_sb = sockfs_get_sb, 309 .get_sb = sockfs_get_sb,
349 .kill_sb = kill_anon_super, 310 .kill_sb = kill_anon_super,
350}; 311};
312
351static int sockfs_delete_dentry(struct dentry *dentry) 313static int sockfs_delete_dentry(struct dentry *dentry)
352{ 314{
353 return 1; 315 return 1;
354} 316}
355static struct dentry_operations sockfs_dentry_operations = { 317static struct dentry_operations sockfs_dentry_operations = {
356 .d_delete = sockfs_delete_dentry, 318 .d_delete = sockfs_delete_dentry,
357}; 319};
358 320
359/* 321/*
@@ -477,10 +439,12 @@ struct socket *sockfd_lookup(int fd, int *err)
477 struct file *file; 439 struct file *file;
478 struct socket *sock; 440 struct socket *sock;
479 441
480 if (!(file = fget(fd))) { 442 file = fget(fd);
443 if (!file) {
481 *err = -EBADF; 444 *err = -EBADF;
482 return NULL; 445 return NULL;
483 } 446 }
447
484 sock = sock_from_file(file, err); 448 sock = sock_from_file(file, err);
485 if (!sock) 449 if (!sock)
486 fput(file); 450 fput(file);
@@ -505,7 +469,7 @@ static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
505 469
506/** 470/**
507 * sock_alloc - allocate a socket 471 * sock_alloc - allocate a socket
508 * 472 *
509 * Allocate a new inode and socket object. The two are bound together 473 * 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 474 * and initialised. The socket is then returned. If we are out of inodes
511 * NULL is returned. 475 * NULL is returned.
@@ -513,8 +477,8 @@ static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
513 477
514static struct socket *sock_alloc(void) 478static struct socket *sock_alloc(void)
515{ 479{
516 struct inode * inode; 480 struct inode *inode;
517 struct socket * sock; 481 struct socket *sock;
518 482
519 inode = new_inode(sock_mnt->mnt_sb); 483 inode = new_inode(sock_mnt->mnt_sb);
520 if (!inode) 484 if (!inode)
@@ -522,7 +486,7 @@ static struct socket *sock_alloc(void)
522 486
523 sock = SOCKET_I(inode); 487 sock = SOCKET_I(inode);
524 488
525 inode->i_mode = S_IFSOCK|S_IRWXUGO; 489 inode->i_mode = S_IFSOCK | S_IRWXUGO;
526 inode->i_uid = current->fsuid; 490 inode->i_uid = current->fsuid;
527 inode->i_gid = current->fsgid; 491 inode->i_gid = current->fsgid;
528 492
@@ -536,7 +500,7 @@ static struct socket *sock_alloc(void)
536 * a back door. Remember to keep it shut otherwise you'll let the 500 * a back door. Remember to keep it shut otherwise you'll let the
537 * creepy crawlies in. 501 * creepy crawlies in.
538 */ 502 */
539 503
540static int sock_no_open(struct inode *irrelevant, struct file *dontcare) 504static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
541{ 505{
542 return -ENXIO; 506 return -ENXIO;
@@ -553,9 +517,9 @@ const struct file_operations bad_sock_fops = {
553 * 517 *
554 * The socket is released from the protocol stack if it has a release 518 * 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 519 * callback, and the inode is then released if the socket is bound to
556 * an inode not a file. 520 * an inode not a file.
557 */ 521 */
558 522
559void sock_release(struct socket *sock) 523void sock_release(struct socket *sock)
560{ 524{
561 if (sock->ops) { 525 if (sock->ops) {
@@ -575,10 +539,10 @@ void sock_release(struct socket *sock)
575 iput(SOCK_INODE(sock)); 539 iput(SOCK_INODE(sock));
576 return; 540 return;
577 } 541 }
578 sock->file=NULL; 542 sock->file = NULL;
579} 543}
580 544
581static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock, 545static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
582 struct msghdr *msg, size_t size) 546 struct msghdr *msg, size_t size)
583{ 547{
584 struct sock_iocb *si = kiocb_to_siocb(iocb); 548 struct sock_iocb *si = kiocb_to_siocb(iocb);
@@ -621,14 +585,14 @@ int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
621 * the following is safe, since for compiler definitions of kvec and 585 * the following is safe, since for compiler definitions of kvec and
622 * iovec are identical, yielding the same in-core layout and alignment 586 * iovec are identical, yielding the same in-core layout and alignment
623 */ 587 */
624 msg->msg_iov = (struct iovec *)vec, 588 msg->msg_iov = (struct iovec *)vec;
625 msg->msg_iovlen = num; 589 msg->msg_iovlen = num;
626 result = sock_sendmsg(sock, msg, size); 590 result = sock_sendmsg(sock, msg, size);
627 set_fs(oldfs); 591 set_fs(oldfs);
628 return result; 592 return result;
629} 593}
630 594
631static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, 595static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
632 struct msghdr *msg, size_t size, int flags) 596 struct msghdr *msg, size_t size, int flags)
633{ 597{
634 int err; 598 int err;
@@ -647,14 +611,14 @@ static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
647 return sock->ops->recvmsg(iocb, sock, msg, size, flags); 611 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
648} 612}
649 613
650int sock_recvmsg(struct socket *sock, struct msghdr *msg, 614int sock_recvmsg(struct socket *sock, struct msghdr *msg,
651 size_t size, int flags) 615 size_t size, int flags)
652{ 616{
653 struct kiocb iocb; 617 struct kiocb iocb;
654 struct sock_iocb siocb; 618 struct sock_iocb siocb;
655 int ret; 619 int ret;
656 620
657 init_sync_kiocb(&iocb, NULL); 621 init_sync_kiocb(&iocb, NULL);
658 iocb.private = &siocb; 622 iocb.private = &siocb;
659 ret = __sock_recvmsg(&iocb, sock, msg, size, flags); 623 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
660 if (-EIOCBQUEUED == ret) 624 if (-EIOCBQUEUED == ret)
@@ -662,9 +626,8 @@ int sock_recvmsg(struct socket *sock, struct msghdr *msg,
662 return ret; 626 return ret;
663} 627}
664 628
665int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 629int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
666 struct kvec *vec, size_t num, 630 struct kvec *vec, size_t num, size_t size, int flags)
667 size_t size, int flags)
668{ 631{
669 mm_segment_t oldfs = get_fs(); 632 mm_segment_t oldfs = get_fs();
670 int result; 633 int result;
@@ -674,8 +637,7 @@ int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
674 * the following is safe, since for compiler definitions of kvec and 637 * the following is safe, since for compiler definitions of kvec and
675 * iovec are identical, yielding the same in-core layout and alignment 638 * iovec are identical, yielding the same in-core layout and alignment
676 */ 639 */
677 msg->msg_iov = (struct iovec *)vec, 640 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
678 msg->msg_iovlen = num;
679 result = sock_recvmsg(sock, msg, size, flags); 641 result = sock_recvmsg(sock, msg, size, flags);
680 set_fs(oldfs); 642 set_fs(oldfs);
681 return result; 643 return result;
@@ -702,7 +664,8 @@ static ssize_t sock_sendpage(struct file *file, struct page *page,
702} 664}
703 665
704static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb, 666static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
705 char __user *ubuf, size_t size, struct sock_iocb *siocb) 667 char __user *ubuf, size_t size,
668 struct sock_iocb *siocb)
706{ 669{
707 if (!is_sync_kiocb(iocb)) { 670 if (!is_sync_kiocb(iocb)) {
708 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL); 671 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
@@ -720,20 +683,21 @@ static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
720} 683}
721 684
722static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb, 685static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
723 struct file *file, struct iovec *iov, unsigned long nr_segs) 686 struct file *file, struct iovec *iov,
687 unsigned long nr_segs)
724{ 688{
725 struct socket *sock = file->private_data; 689 struct socket *sock = file->private_data;
726 size_t size = 0; 690 size_t size = 0;
727 int i; 691 int i;
728 692
729 for (i = 0 ; i < nr_segs ; i++) 693 for (i = 0; i < nr_segs; i++)
730 size += iov[i].iov_len; 694 size += iov[i].iov_len;
731 695
732 msg->msg_name = NULL; 696 msg->msg_name = NULL;
733 msg->msg_namelen = 0; 697 msg->msg_namelen = 0;
734 msg->msg_control = NULL; 698 msg->msg_control = NULL;
735 msg->msg_controllen = 0; 699 msg->msg_controllen = 0;
736 msg->msg_iov = (struct iovec *) iov; 700 msg->msg_iov = (struct iovec *)iov;
737 msg->msg_iovlen = nr_segs; 701 msg->msg_iovlen = nr_segs;
738 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 702 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
739 703
@@ -748,7 +712,7 @@ static ssize_t sock_readv(struct file *file, const struct iovec *iov,
748 struct msghdr msg; 712 struct msghdr msg;
749 int ret; 713 int ret;
750 714
751 init_sync_kiocb(&iocb, NULL); 715 init_sync_kiocb(&iocb, NULL);
752 iocb.private = &siocb; 716 iocb.private = &siocb;
753 717
754 ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs); 718 ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
@@ -758,7 +722,7 @@ static ssize_t sock_readv(struct file *file, const struct iovec *iov,
758} 722}
759 723
760static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf, 724static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
761 size_t count, loff_t pos) 725 size_t count, loff_t pos)
762{ 726{
763 struct sock_iocb siocb, *x; 727 struct sock_iocb siocb, *x;
764 728
@@ -771,24 +735,25 @@ static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
771 if (!x) 735 if (!x)
772 return -ENOMEM; 736 return -ENOMEM;
773 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, 737 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
774 &x->async_iov, 1); 738 &x->async_iov, 1);
775} 739}
776 740
777static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb, 741static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
778 struct file *file, struct iovec *iov, unsigned long nr_segs) 742 struct file *file, struct iovec *iov,
743 unsigned long nr_segs)
779{ 744{
780 struct socket *sock = file->private_data; 745 struct socket *sock = file->private_data;
781 size_t size = 0; 746 size_t size = 0;
782 int i; 747 int i;
783 748
784 for (i = 0 ; i < nr_segs ; i++) 749 for (i = 0; i < nr_segs; i++)
785 size += iov[i].iov_len; 750 size += iov[i].iov_len;
786 751
787 msg->msg_name = NULL; 752 msg->msg_name = NULL;
788 msg->msg_namelen = 0; 753 msg->msg_namelen = 0;
789 msg->msg_control = NULL; 754 msg->msg_control = NULL;
790 msg->msg_controllen = 0; 755 msg->msg_controllen = 0;
791 msg->msg_iov = (struct iovec *) iov; 756 msg->msg_iov = (struct iovec *)iov;
792 msg->msg_iovlen = nr_segs; 757 msg->msg_iovlen = nr_segs;
793 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 758 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
794 if (sock->type == SOCK_SEQPACKET) 759 if (sock->type == SOCK_SEQPACKET)
@@ -815,7 +780,7 @@ static ssize_t sock_writev(struct file *file, const struct iovec *iov,
815} 780}
816 781
817static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf, 782static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
818 size_t count, loff_t pos) 783 size_t count, loff_t pos)
819{ 784{
820 struct sock_iocb siocb, *x; 785 struct sock_iocb siocb, *x;
821 786
@@ -829,46 +794,48 @@ static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
829 return -ENOMEM; 794 return -ENOMEM;
830 795
831 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, 796 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
832 &x->async_iov, 1); 797 &x->async_iov, 1);
833} 798}
834 799
835
836/* 800/*
837 * Atomic setting of ioctl hooks to avoid race 801 * Atomic setting of ioctl hooks to avoid race
838 * with module unload. 802 * with module unload.
839 */ 803 */
840 804
841static DEFINE_MUTEX(br_ioctl_mutex); 805static DEFINE_MUTEX(br_ioctl_mutex);
842static int (*br_ioctl_hook)(unsigned int cmd, void __user *arg) = NULL; 806static int (*br_ioctl_hook) (unsigned int cmd, void __user *arg) = NULL;
843 807
844void brioctl_set(int (*hook)(unsigned int, void __user *)) 808void brioctl_set(int (*hook) (unsigned int, void __user *))
845{ 809{
846 mutex_lock(&br_ioctl_mutex); 810 mutex_lock(&br_ioctl_mutex);
847 br_ioctl_hook = hook; 811 br_ioctl_hook = hook;
848 mutex_unlock(&br_ioctl_mutex); 812 mutex_unlock(&br_ioctl_mutex);
849} 813}
814
850EXPORT_SYMBOL(brioctl_set); 815EXPORT_SYMBOL(brioctl_set);
851 816
852static DEFINE_MUTEX(vlan_ioctl_mutex); 817static DEFINE_MUTEX(vlan_ioctl_mutex);
853static int (*vlan_ioctl_hook)(void __user *arg); 818static int (*vlan_ioctl_hook) (void __user *arg);
854 819
855void vlan_ioctl_set(int (*hook)(void __user *)) 820void vlan_ioctl_set(int (*hook) (void __user *))
856{ 821{
857 mutex_lock(&vlan_ioctl_mutex); 822 mutex_lock(&vlan_ioctl_mutex);
858 vlan_ioctl_hook = hook; 823 vlan_ioctl_hook = hook;
859 mutex_unlock(&vlan_ioctl_mutex); 824 mutex_unlock(&vlan_ioctl_mutex);
860} 825}
826
861EXPORT_SYMBOL(vlan_ioctl_set); 827EXPORT_SYMBOL(vlan_ioctl_set);
862 828
863static DEFINE_MUTEX(dlci_ioctl_mutex); 829static DEFINE_MUTEX(dlci_ioctl_mutex);
864static int (*dlci_ioctl_hook)(unsigned int, void __user *); 830static int (*dlci_ioctl_hook) (unsigned int, void __user *);
865 831
866void dlci_ioctl_set(int (*hook)(unsigned int, void __user *)) 832void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
867{ 833{
868 mutex_lock(&dlci_ioctl_mutex); 834 mutex_lock(&dlci_ioctl_mutex);
869 dlci_ioctl_hook = hook; 835 dlci_ioctl_hook = hook;
870 mutex_unlock(&dlci_ioctl_mutex); 836 mutex_unlock(&dlci_ioctl_mutex);
871} 837}
838
872EXPORT_SYMBOL(dlci_ioctl_set); 839EXPORT_SYMBOL(dlci_ioctl_set);
873 840
874/* 841/*
@@ -890,8 +857,8 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
890 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) { 857 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
891 err = dev_ioctl(cmd, argp); 858 err = dev_ioctl(cmd, argp);
892 } else 859 } else
893#endif /* CONFIG_WIRELESS_EXT */ 860#endif /* CONFIG_WIRELESS_EXT */
894 switch (cmd) { 861 switch (cmd) {
895 case FIOSETOWN: 862 case FIOSETOWN:
896 case SIOCSPGRP: 863 case SIOCSPGRP:
897 err = -EFAULT; 864 err = -EFAULT;
@@ -901,7 +868,8 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
901 break; 868 break;
902 case FIOGETOWN: 869 case FIOGETOWN:
903 case SIOCGPGRP: 870 case SIOCGPGRP:
904 err = put_user(sock->file->f_owner.pid, (int __user *)argp); 871 err = put_user(sock->file->f_owner.pid,
872 (int __user *)argp);
905 break; 873 break;
906 case SIOCGIFBR: 874 case SIOCGIFBR:
907 case SIOCSIFBR: 875 case SIOCSIFBR:
@@ -912,7 +880,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
912 request_module("bridge"); 880 request_module("bridge");
913 881
914 mutex_lock(&br_ioctl_mutex); 882 mutex_lock(&br_ioctl_mutex);
915 if (br_ioctl_hook) 883 if (br_ioctl_hook)
916 err = br_ioctl_hook(cmd, argp); 884 err = br_ioctl_hook(cmd, argp);
917 mutex_unlock(&br_ioctl_mutex); 885 mutex_unlock(&br_ioctl_mutex);
918 break; 886 break;
@@ -929,7 +897,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
929 break; 897 break;
930 case SIOCGIFDIVERT: 898 case SIOCGIFDIVERT:
931 case SIOCSIFDIVERT: 899 case SIOCSIFDIVERT:
932 /* Convert this to call through a hook */ 900 /* Convert this to call through a hook */
933 err = divert_ioctl(cmd, argp); 901 err = divert_ioctl(cmd, argp);
934 break; 902 break;
935 case SIOCADDDLCI: 903 case SIOCADDDLCI:
@@ -954,7 +922,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
954 if (err == -ENOIOCTLCMD) 922 if (err == -ENOIOCTLCMD)
955 err = dev_ioctl(cmd, argp); 923 err = dev_ioctl(cmd, argp);
956 break; 924 break;
957 } 925 }
958 return err; 926 return err;
959} 927}
960 928
@@ -962,7 +930,7 @@ int sock_create_lite(int family, int type, int protocol, struct socket **res)
962{ 930{
963 int err; 931 int err;
964 struct socket *sock = NULL; 932 struct socket *sock = NULL;
965 933
966 err = security_socket_create(family, type, protocol, 1); 934 err = security_socket_create(family, type, protocol, 1);
967 if (err) 935 if (err)
968 goto out; 936 goto out;
@@ -973,26 +941,33 @@ int sock_create_lite(int family, int type, int protocol, struct socket **res)
973 goto out; 941 goto out;
974 } 942 }
975 943
976 security_socket_post_create(sock, family, type, protocol, 1);
977 sock->type = type; 944 sock->type = type;
945 err = security_socket_post_create(sock, family, type, protocol, 1);
946 if (err)
947 goto out_release;
948
978out: 949out:
979 *res = sock; 950 *res = sock;
980 return err; 951 return err;
952out_release:
953 sock_release(sock);
954 sock = NULL;
955 goto out;
981} 956}
982 957
983/* No kernel lock held - perfect */ 958/* No kernel lock held - perfect */
984static unsigned int sock_poll(struct file *file, poll_table * wait) 959static unsigned int sock_poll(struct file *file, poll_table *wait)
985{ 960{
986 struct socket *sock; 961 struct socket *sock;
987 962
988 /* 963 /*
989 * We can't return errors to poll, so it's either yes or no. 964 * We can't return errors to poll, so it's either yes or no.
990 */ 965 */
991 sock = file->private_data; 966 sock = file->private_data;
992 return sock->ops->poll(file, sock, wait); 967 return sock->ops->poll(file, sock, wait);
993} 968}
994 969
995static int sock_mmap(struct file * file, struct vm_area_struct * vma) 970static int sock_mmap(struct file *file, struct vm_area_struct *vma)
996{ 971{
997 struct socket *sock = file->private_data; 972 struct socket *sock = file->private_data;
998 973
@@ -1002,12 +977,11 @@ static int sock_mmap(struct file * file, struct vm_area_struct * vma)
1002static int sock_close(struct inode *inode, struct file *filp) 977static int sock_close(struct inode *inode, struct file *filp)
1003{ 978{
1004 /* 979 /*
1005 * It was possible the inode is NULL we were 980 * It was possible the inode is NULL we were
1006 * closing an unfinished socket. 981 * closing an unfinished socket.
1007 */ 982 */
1008 983
1009 if (!inode) 984 if (!inode) {
1010 {
1011 printk(KERN_DEBUG "sock_close: NULL inode\n"); 985 printk(KERN_DEBUG "sock_close: NULL inode\n");
1012 return 0; 986 return 0;
1013 } 987 }
@@ -1033,57 +1007,52 @@ static int sock_close(struct inode *inode, struct file *filp)
1033 1007
1034static int sock_fasync(int fd, struct file *filp, int on) 1008static int sock_fasync(int fd, struct file *filp, int on)
1035{ 1009{
1036 struct fasync_struct *fa, *fna=NULL, **prev; 1010 struct fasync_struct *fa, *fna = NULL, **prev;
1037 struct socket *sock; 1011 struct socket *sock;
1038 struct sock *sk; 1012 struct sock *sk;
1039 1013
1040 if (on) 1014 if (on) {
1041 {
1042 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL); 1015 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1043 if(fna==NULL) 1016 if (fna == NULL)
1044 return -ENOMEM; 1017 return -ENOMEM;
1045 } 1018 }
1046 1019
1047 sock = filp->private_data; 1020 sock = filp->private_data;
1048 1021
1049 if ((sk=sock->sk) == NULL) { 1022 sk = sock->sk;
1023 if (sk == NULL) {
1050 kfree(fna); 1024 kfree(fna);
1051 return -EINVAL; 1025 return -EINVAL;
1052 } 1026 }
1053 1027
1054 lock_sock(sk); 1028 lock_sock(sk);
1055 1029
1056 prev=&(sock->fasync_list); 1030 prev = &(sock->fasync_list);
1057 1031
1058 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev) 1032 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1059 if (fa->fa_file==filp) 1033 if (fa->fa_file == filp)
1060 break; 1034 break;
1061 1035
1062 if(on) 1036 if (on) {
1063 { 1037 if (fa != NULL) {
1064 if(fa!=NULL)
1065 {
1066 write_lock_bh(&sk->sk_callback_lock); 1038 write_lock_bh(&sk->sk_callback_lock);
1067 fa->fa_fd=fd; 1039 fa->fa_fd = fd;
1068 write_unlock_bh(&sk->sk_callback_lock); 1040 write_unlock_bh(&sk->sk_callback_lock);
1069 1041
1070 kfree(fna); 1042 kfree(fna);
1071 goto out; 1043 goto out;
1072 } 1044 }
1073 fna->fa_file=filp; 1045 fna->fa_file = filp;
1074 fna->fa_fd=fd; 1046 fna->fa_fd = fd;
1075 fna->magic=FASYNC_MAGIC; 1047 fna->magic = FASYNC_MAGIC;
1076 fna->fa_next=sock->fasync_list; 1048 fna->fa_next = sock->fasync_list;
1077 write_lock_bh(&sk->sk_callback_lock); 1049 write_lock_bh(&sk->sk_callback_lock);
1078 sock->fasync_list=fna; 1050 sock->fasync_list = fna;
1079 write_unlock_bh(&sk->sk_callback_lock); 1051 write_unlock_bh(&sk->sk_callback_lock);
1080 } 1052 } else {
1081 else 1053 if (fa != NULL) {
1082 {
1083 if (fa!=NULL)
1084 {
1085 write_lock_bh(&sk->sk_callback_lock); 1054 write_lock_bh(&sk->sk_callback_lock);
1086 *prev=fa->fa_next; 1055 *prev = fa->fa_next;
1087 write_unlock_bh(&sk->sk_callback_lock); 1056 write_unlock_bh(&sk->sk_callback_lock);
1088 kfree(fa); 1057 kfree(fa);
1089 } 1058 }
@@ -1100,10 +1069,9 @@ int sock_wake_async(struct socket *sock, int how, int band)
1100{ 1069{
1101 if (!sock || !sock->fasync_list) 1070 if (!sock || !sock->fasync_list)
1102 return -1; 1071 return -1;
1103 switch (how) 1072 switch (how) {
1104 {
1105 case 1: 1073 case 1:
1106 1074
1107 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags)) 1075 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1108 break; 1076 break;
1109 goto call_kill; 1077 goto call_kill;
@@ -1112,7 +1080,7 @@ int sock_wake_async(struct socket *sock, int how, int band)
1112 break; 1080 break;
1113 /* fall through */ 1081 /* fall through */
1114 case 0: 1082 case 0:
1115 call_kill: 1083call_kill:
1116 __kill_fasync(sock->fasync_list, SIGIO, band); 1084 __kill_fasync(sock->fasync_list, SIGIO, band);
1117 break; 1085 break;
1118 case 3: 1086 case 3:
@@ -1121,13 +1089,15 @@ int sock_wake_async(struct socket *sock, int how, int band)
1121 return 0; 1089 return 0;
1122} 1090}
1123 1091
1124static int __sock_create(int family, int type, int protocol, struct socket **res, int kern) 1092static int __sock_create(int family, int type, int protocol,
1093 struct socket **res, int kern)
1125{ 1094{
1126 int err; 1095 int err;
1127 struct socket *sock; 1096 struct socket *sock;
1097 const struct net_proto_family *pf;
1128 1098
1129 /* 1099 /*
1130 * Check protocol is in range 1100 * Check protocol is in range
1131 */ 1101 */
1132 if (family < 0 || family >= NPROTO) 1102 if (family < 0 || family >= NPROTO)
1133 return -EAFNOSUPPORT; 1103 return -EAFNOSUPPORT;
@@ -1140,10 +1110,11 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
1140 deadlock in module load. 1110 deadlock in module load.
1141 */ 1111 */
1142 if (family == PF_INET && type == SOCK_PACKET) { 1112 if (family == PF_INET && type == SOCK_PACKET) {
1143 static int warned; 1113 static int warned;
1144 if (!warned) { 1114 if (!warned) {
1145 warned = 1; 1115 warned = 1;
1146 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm); 1116 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1117 current->comm);
1147 } 1118 }
1148 family = PF_PACKET; 1119 family = PF_PACKET;
1149 } 1120 }
@@ -1151,79 +1122,84 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
1151 err = security_socket_create(family, type, protocol, kern); 1122 err = security_socket_create(family, type, protocol, kern);
1152 if (err) 1123 if (err)
1153 return err; 1124 return err;
1154 1125
1126 /*
1127 * Allocate the socket and allow the family to set things up. if
1128 * the protocol is 0, the family is instructed to select an appropriate
1129 * default.
1130 */
1131 sock = sock_alloc();
1132 if (!sock) {
1133 if (net_ratelimit())
1134 printk(KERN_WARNING "socket: no more sockets\n");
1135 return -ENFILE; /* Not exactly a match, but its the
1136 closest posix thing */
1137 }
1138
1139 sock->type = type;
1140
1155#if defined(CONFIG_KMOD) 1141#if defined(CONFIG_KMOD)
1156 /* Attempt to load a protocol module if the find failed. 1142 /* Attempt to load a protocol module if the find failed.
1157 * 1143 *
1158 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user 1144 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1159 * requested real, full-featured networking support upon configuration. 1145 * requested real, full-featured networking support upon configuration.
1160 * Otherwise module support will break! 1146 * Otherwise module support will break!
1161 */ 1147 */
1162 if (net_families[family]==NULL) 1148 if (net_families[family] == NULL)
1163 { 1149 request_module("net-pf-%d", family);
1164 request_module("net-pf-%d",family);
1165 }
1166#endif 1150#endif
1167 1151
1168 net_family_read_lock(); 1152 rcu_read_lock();
1169 if (net_families[family] == NULL) { 1153 pf = rcu_dereference(net_families[family]);
1170 err = -EAFNOSUPPORT; 1154 err = -EAFNOSUPPORT;
1171 goto out; 1155 if (!pf)
1172 } 1156 goto out_release;
1173
1174/*
1175 * Allocate the socket and allow the family to set things up. if
1176 * the protocol is 0, the family is instructed to select an appropriate
1177 * default.
1178 */
1179
1180 if (!(sock = sock_alloc())) {
1181 if (net_ratelimit())
1182 printk(KERN_WARNING "socket: no more sockets\n");
1183 err = -ENFILE; /* Not exactly a match, but its the
1184 closest posix thing */
1185 goto out;
1186 }
1187
1188 sock->type = type;
1189 1157
1190 /* 1158 /*
1191 * We will call the ->create function, that possibly is in a loadable 1159 * We will call the ->create function, that possibly is in a loadable
1192 * module, so we have to bump that loadable module refcnt first. 1160 * module, so we have to bump that loadable module refcnt first.
1193 */ 1161 */
1194 err = -EAFNOSUPPORT; 1162 if (!try_module_get(pf->owner))
1195 if (!try_module_get(net_families[family]->owner))
1196 goto out_release; 1163 goto out_release;
1197 1164
1198 if ((err = net_families[family]->create(sock, protocol)) < 0) { 1165 /* Now protected by module ref count */
1199 sock->ops = NULL; 1166 rcu_read_unlock();
1167
1168 err = pf->create(sock, protocol);
1169 if (err < 0)
1200 goto out_module_put; 1170 goto out_module_put;
1201 }
1202 1171
1203 /* 1172 /*
1204 * Now to bump the refcnt of the [loadable] module that owns this 1173 * Now to bump the refcnt of the [loadable] module that owns this
1205 * socket at sock_release time we decrement its refcnt. 1174 * socket at sock_release time we decrement its refcnt.
1206 */ 1175 */
1207 if (!try_module_get(sock->ops->owner)) { 1176 if (!try_module_get(sock->ops->owner))
1208 sock->ops = NULL; 1177 goto out_module_busy;
1209 goto out_module_put; 1178
1210 }
1211 /* 1179 /*
1212 * Now that we're done with the ->create function, the [loadable] 1180 * Now that we're done with the ->create function, the [loadable]
1213 * module can have its refcnt decremented 1181 * module can have its refcnt decremented
1214 */ 1182 */
1215 module_put(net_families[family]->owner); 1183 module_put(pf->owner);
1184 err = security_socket_post_create(sock, family, type, protocol, kern);
1185 if (err)
1186 goto out_release;
1216 *res = sock; 1187 *res = sock;
1217 security_socket_post_create(sock, family, type, protocol, kern);
1218 1188
1219out: 1189 return 0;
1220 net_family_read_unlock(); 1190
1221 return err; 1191out_module_busy:
1192 err = -EAFNOSUPPORT;
1222out_module_put: 1193out_module_put:
1223 module_put(net_families[family]->owner); 1194 sock->ops = NULL;
1224out_release: 1195 module_put(pf->owner);
1196out_sock_release:
1225 sock_release(sock); 1197 sock_release(sock);
1226 goto out; 1198 return err;
1199
1200out_release:
1201 rcu_read_unlock();
1202 goto out_sock_release;
1227} 1203}
1228 1204
1229int sock_create(int family, int type, int protocol, struct socket **res) 1205int sock_create(int family, int type, int protocol, struct socket **res)
@@ -1262,7 +1238,8 @@ out_release:
1262 * Create a pair of connected sockets. 1238 * Create a pair of connected sockets.
1263 */ 1239 */
1264 1240
1265asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec) 1241asmlinkage long sys_socketpair(int family, int type, int protocol,
1242 int __user *usockvec)
1266{ 1243{
1267 struct socket *sock1, *sock2; 1244 struct socket *sock1, *sock2;
1268 int fd1, fd2, err; 1245 int fd1, fd2, err;
@@ -1281,7 +1258,7 @@ asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *u
1281 goto out_release_1; 1258 goto out_release_1;
1282 1259
1283 err = sock1->ops->socketpair(sock1, sock2); 1260 err = sock1->ops->socketpair(sock1, sock2);
1284 if (err < 0) 1261 if (err < 0)
1285 goto out_release_both; 1262 goto out_release_both;
1286 1263
1287 fd1 = fd2 = -1; 1264 fd1 = fd2 = -1;
@@ -1300,7 +1277,7 @@ asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *u
1300 * Not kernel problem. 1277 * Not kernel problem.
1301 */ 1278 */
1302 1279
1303 err = put_user(fd1, &usockvec[0]); 1280 err = put_user(fd1, &usockvec[0]);
1304 if (!err) 1281 if (!err)
1305 err = put_user(fd2, &usockvec[1]); 1282 err = put_user(fd2, &usockvec[1]);
1306 if (!err) 1283 if (!err)
@@ -1311,19 +1288,18 @@ asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *u
1311 return err; 1288 return err;
1312 1289
1313out_close_1: 1290out_close_1:
1314 sock_release(sock2); 1291 sock_release(sock2);
1315 sys_close(fd1); 1292 sys_close(fd1);
1316 return err; 1293 return err;
1317 1294
1318out_release_both: 1295out_release_both:
1319 sock_release(sock2); 1296 sock_release(sock2);
1320out_release_1: 1297out_release_1:
1321 sock_release(sock1); 1298 sock_release(sock1);
1322out: 1299out:
1323 return err; 1300 return err;
1324} 1301}
1325 1302
1326
1327/* 1303/*
1328 * Bind a name to a socket. Nothing much to do here since it's 1304 * Bind a name to a socket. Nothing much to do here since it's
1329 * the protocol's responsibility to handle the local address. 1305 * the protocol's responsibility to handle the local address.
@@ -1338,35 +1314,39 @@ asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1338 char address[MAX_SOCK_ADDR]; 1314 char address[MAX_SOCK_ADDR];
1339 int err, fput_needed; 1315 int err, fput_needed;
1340 1316
1341 if((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL) 1317 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1342 { 1318 if(sock) {
1343 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) { 1319 err = move_addr_to_kernel(umyaddr, addrlen, address);
1344 err = security_socket_bind(sock, (struct sockaddr *)address, addrlen); 1320 if (err >= 0) {
1321 err = security_socket_bind(sock,
1322 (struct sockaddr *)address,
1323 addrlen);
1345 if (!err) 1324 if (!err)
1346 err = sock->ops->bind(sock, 1325 err = sock->ops->bind(sock,
1347 (struct sockaddr *)address, addrlen); 1326 (struct sockaddr *)
1327 address, addrlen);
1348 } 1328 }
1349 fput_light(sock->file, fput_needed); 1329 fput_light(sock->file, fput_needed);
1350 } 1330 }
1351 return err; 1331 return err;
1352} 1332}
1353 1333
1354
1355/* 1334/*
1356 * Perform a listen. Basically, we allow the protocol to do anything 1335 * Perform a listen. Basically, we allow the protocol to do anything
1357 * necessary for a listen, and if that works, we mark the socket as 1336 * necessary for a listen, and if that works, we mark the socket as
1358 * ready for listening. 1337 * ready for listening.
1359 */ 1338 */
1360 1339
1361int sysctl_somaxconn = SOMAXCONN; 1340int sysctl_somaxconn __read_mostly = SOMAXCONN;
1362 1341
1363asmlinkage long sys_listen(int fd, int backlog) 1342asmlinkage long sys_listen(int fd, int backlog)
1364{ 1343{
1365 struct socket *sock; 1344 struct socket *sock;
1366 int err, fput_needed; 1345 int err, fput_needed;
1367 1346
1368 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) { 1347 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1369 if ((unsigned) backlog > sysctl_somaxconn) 1348 if (sock) {
1349 if ((unsigned)backlog > sysctl_somaxconn)
1370 backlog = sysctl_somaxconn; 1350 backlog = sysctl_somaxconn;
1371 1351
1372 err = security_socket_listen(sock, backlog); 1352 err = security_socket_listen(sock, backlog);
@@ -1378,7 +1358,6 @@ asmlinkage long sys_listen(int fd, int backlog)
1378 return err; 1358 return err;
1379} 1359}
1380 1360
1381
1382/* 1361/*
1383 * For accept, we attempt to create a new socket, set up the link 1362 * For accept, we attempt to create a new socket, set up the link
1384 * with the client, wake up the client, then return the new 1363 * with the client, wake up the client, then return the new
@@ -1391,7 +1370,8 @@ asmlinkage long sys_listen(int fd, int backlog)
1391 * clean when we restucture accept also. 1370 * clean when we restucture accept also.
1392 */ 1371 */
1393 1372
1394asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen) 1373asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
1374 int __user *upeer_addrlen)
1395{ 1375{
1396 struct socket *sock, *newsock; 1376 struct socket *sock, *newsock;
1397 struct file *newfile; 1377 struct file *newfile;
@@ -1403,7 +1383,7 @@ asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int _
1403 goto out; 1383 goto out;
1404 1384
1405 err = -ENFILE; 1385 err = -ENFILE;
1406 if (!(newsock = sock_alloc())) 1386 if (!(newsock = sock_alloc()))
1407 goto out_put; 1387 goto out_put;
1408 1388
1409 newsock->type = sock->type; 1389 newsock->type = sock->type;
@@ -1435,11 +1415,13 @@ asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int _
1435 goto out_fd; 1415 goto out_fd;
1436 1416
1437 if (upeer_sockaddr) { 1417 if (upeer_sockaddr) {
1438 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) { 1418 if (newsock->ops->getname(newsock, (struct sockaddr *)address,
1419 &len, 2) < 0) {
1439 err = -ECONNABORTED; 1420 err = -ECONNABORTED;
1440 goto out_fd; 1421 goto out_fd;
1441 } 1422 }
1442 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen); 1423 err = move_addr_to_user(address, len, upeer_sockaddr,
1424 upeer_addrlen);
1443 if (err < 0) 1425 if (err < 0)
1444 goto out_fd; 1426 goto out_fd;
1445 } 1427 }
@@ -1461,7 +1443,6 @@ out_fd:
1461 goto out_put; 1443 goto out_put;
1462} 1444}
1463 1445
1464
1465/* 1446/*
1466 * Attempt to connect to a socket with the server address. The address 1447 * Attempt to connect to a socket with the server address. The address
1467 * is in user space so we verify it is OK and move it to kernel space. 1448 * is in user space so we verify it is OK and move it to kernel space.
@@ -1474,7 +1455,8 @@ out_fd:
1474 * include the -EINPROGRESS status for such sockets. 1455 * include the -EINPROGRESS status for such sockets.
1475 */ 1456 */
1476 1457
1477asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen) 1458asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
1459 int addrlen)
1478{ 1460{
1479 struct socket *sock; 1461 struct socket *sock;
1480 char address[MAX_SOCK_ADDR]; 1462 char address[MAX_SOCK_ADDR];
@@ -1487,11 +1469,12 @@ asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrl
1487 if (err < 0) 1469 if (err < 0)
1488 goto out_put; 1470 goto out_put;
1489 1471
1490 err = security_socket_connect(sock, (struct sockaddr *)address, addrlen); 1472 err =
1473 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1491 if (err) 1474 if (err)
1492 goto out_put; 1475 goto out_put;
1493 1476
1494 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen, 1477 err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
1495 sock->file->f_flags); 1478 sock->file->f_flags);
1496out_put: 1479out_put:
1497 fput_light(sock->file, fput_needed); 1480 fput_light(sock->file, fput_needed);
@@ -1504,12 +1487,13 @@ out:
1504 * name to user space. 1487 * name to user space.
1505 */ 1488 */
1506 1489
1507asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len) 1490asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1491 int __user *usockaddr_len)
1508{ 1492{
1509 struct socket *sock; 1493 struct socket *sock;
1510 char address[MAX_SOCK_ADDR]; 1494 char address[MAX_SOCK_ADDR];
1511 int len, err, fput_needed; 1495 int len, err, fput_needed;
1512 1496
1513 sock = sockfd_lookup_light(fd, &err, &fput_needed); 1497 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1514 if (!sock) 1498 if (!sock)
1515 goto out; 1499 goto out;
@@ -1534,22 +1518,27 @@ out:
1534 * name to user space. 1518 * name to user space.
1535 */ 1519 */
1536 1520
1537asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len) 1521asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1522 int __user *usockaddr_len)
1538{ 1523{
1539 struct socket *sock; 1524 struct socket *sock;
1540 char address[MAX_SOCK_ADDR]; 1525 char address[MAX_SOCK_ADDR];
1541 int len, err, fput_needed; 1526 int len, err, fput_needed;
1542 1527
1543 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) { 1528 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1529 if (sock != NULL) {
1544 err = security_socket_getpeername(sock); 1530 err = security_socket_getpeername(sock);
1545 if (err) { 1531 if (err) {
1546 fput_light(sock->file, fput_needed); 1532 fput_light(sock->file, fput_needed);
1547 return err; 1533 return err;
1548 } 1534 }
1549 1535
1550 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1); 1536 err =
1537 sock->ops->getname(sock, (struct sockaddr *)address, &len,
1538 1);
1551 if (!err) 1539 if (!err)
1552 err=move_addr_to_user(address,len, usockaddr, usockaddr_len); 1540 err = move_addr_to_user(address, len, usockaddr,
1541 usockaddr_len);
1553 fput_light(sock->file, fput_needed); 1542 fput_light(sock->file, fput_needed);
1554 } 1543 }
1555 return err; 1544 return err;
@@ -1561,8 +1550,9 @@ asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int _
1561 * the protocol. 1550 * the protocol.
1562 */ 1551 */
1563 1552
1564asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags, 1553asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
1565 struct sockaddr __user *addr, int addr_len) 1554 unsigned flags, struct sockaddr __user *addr,
1555 int addr_len)
1566{ 1556{
1567 struct socket *sock; 1557 struct socket *sock;
1568 char address[MAX_SOCK_ADDR]; 1558 char address[MAX_SOCK_ADDR];
@@ -1579,54 +1569,55 @@ asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flag
1579 sock = sock_from_file(sock_file, &err); 1569 sock = sock_from_file(sock_file, &err);
1580 if (!sock) 1570 if (!sock)
1581 goto out_put; 1571 goto out_put;
1582 iov.iov_base=buff; 1572 iov.iov_base = buff;
1583 iov.iov_len=len; 1573 iov.iov_len = len;
1584 msg.msg_name=NULL; 1574 msg.msg_name = NULL;
1585 msg.msg_iov=&iov; 1575 msg.msg_iov = &iov;
1586 msg.msg_iovlen=1; 1576 msg.msg_iovlen = 1;
1587 msg.msg_control=NULL; 1577 msg.msg_control = NULL;
1588 msg.msg_controllen=0; 1578 msg.msg_controllen = 0;
1589 msg.msg_namelen=0; 1579 msg.msg_namelen = 0;
1590 if (addr) { 1580 if (addr) {
1591 err = move_addr_to_kernel(addr, addr_len, address); 1581 err = move_addr_to_kernel(addr, addr_len, address);
1592 if (err < 0) 1582 if (err < 0)
1593 goto out_put; 1583 goto out_put;
1594 msg.msg_name=address; 1584 msg.msg_name = address;
1595 msg.msg_namelen=addr_len; 1585 msg.msg_namelen = addr_len;
1596 } 1586 }
1597 if (sock->file->f_flags & O_NONBLOCK) 1587 if (sock->file->f_flags & O_NONBLOCK)
1598 flags |= MSG_DONTWAIT; 1588 flags |= MSG_DONTWAIT;
1599 msg.msg_flags = flags; 1589 msg.msg_flags = flags;
1600 err = sock_sendmsg(sock, &msg, len); 1590 err = sock_sendmsg(sock, &msg, len);
1601 1591
1602out_put: 1592out_put:
1603 fput_light(sock_file, fput_needed); 1593 fput_light(sock_file, fput_needed);
1604 return err; 1594 return err;
1605} 1595}
1606 1596
1607/* 1597/*
1608 * Send a datagram down a socket. 1598 * Send a datagram down a socket.
1609 */ 1599 */
1610 1600
1611asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags) 1601asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
1612{ 1602{
1613 return sys_sendto(fd, buff, len, flags, NULL, 0); 1603 return sys_sendto(fd, buff, len, flags, NULL, 0);
1614} 1604}
1615 1605
1616/* 1606/*
1617 * Receive a frame from the socket and optionally record the address of the 1607 * Receive a frame from the socket and optionally record the address of the
1618 * sender. We verify the buffers are writable and if needed move the 1608 * sender. We verify the buffers are writable and if needed move the
1619 * sender address from kernel to user space. 1609 * sender address from kernel to user space.
1620 */ 1610 */
1621 1611
1622asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags, 1612asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
1623 struct sockaddr __user *addr, int __user *addr_len) 1613 unsigned flags, struct sockaddr __user *addr,
1614 int __user *addr_len)
1624{ 1615{
1625 struct socket *sock; 1616 struct socket *sock;
1626 struct iovec iov; 1617 struct iovec iov;
1627 struct msghdr msg; 1618 struct msghdr msg;
1628 char address[MAX_SOCK_ADDR]; 1619 char address[MAX_SOCK_ADDR];
1629 int err,err2; 1620 int err, err2;
1630 struct file *sock_file; 1621 struct file *sock_file;
1631 int fput_needed; 1622 int fput_needed;
1632 1623
@@ -1638,23 +1629,22 @@ asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned f
1638 if (!sock) 1629 if (!sock)
1639 goto out; 1630 goto out;
1640 1631
1641 msg.msg_control=NULL; 1632 msg.msg_control = NULL;
1642 msg.msg_controllen=0; 1633 msg.msg_controllen = 0;
1643 msg.msg_iovlen=1; 1634 msg.msg_iovlen = 1;
1644 msg.msg_iov=&iov; 1635 msg.msg_iov = &iov;
1645 iov.iov_len=size; 1636 iov.iov_len = size;
1646 iov.iov_base=ubuf; 1637 iov.iov_base = ubuf;
1647 msg.msg_name=address; 1638 msg.msg_name = address;
1648 msg.msg_namelen=MAX_SOCK_ADDR; 1639 msg.msg_namelen = MAX_SOCK_ADDR;
1649 if (sock->file->f_flags & O_NONBLOCK) 1640 if (sock->file->f_flags & O_NONBLOCK)
1650 flags |= MSG_DONTWAIT; 1641 flags |= MSG_DONTWAIT;
1651 err=sock_recvmsg(sock, &msg, size, flags); 1642 err = sock_recvmsg(sock, &msg, size, flags);
1652 1643
1653 if(err >= 0 && addr != NULL) 1644 if (err >= 0 && addr != NULL) {
1654 { 1645 err2 = move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1655 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len); 1646 if (err2 < 0)
1656 if(err2<0) 1647 err = err2;
1657 err=err2;
1658 } 1648 }
1659out: 1649out:
1660 fput_light(sock_file, fput_needed); 1650 fput_light(sock_file, fput_needed);
@@ -1662,10 +1652,11 @@ out:
1662} 1652}
1663 1653
1664/* 1654/*
1665 * Receive a datagram from a socket. 1655 * Receive a datagram from a socket.
1666 */ 1656 */
1667 1657
1668asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags) 1658asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1659 unsigned flags)
1669{ 1660{
1670 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL); 1661 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1671} 1662}
@@ -1675,24 +1666,29 @@ asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags
1675 * to pass the user mode parameter for the protocols to sort out. 1666 * to pass the user mode parameter for the protocols to sort out.
1676 */ 1667 */
1677 1668
1678asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen) 1669asmlinkage long sys_setsockopt(int fd, int level, int optname,
1670 char __user *optval, int optlen)
1679{ 1671{
1680 int err, fput_needed; 1672 int err, fput_needed;
1681 struct socket *sock; 1673 struct socket *sock;
1682 1674
1683 if (optlen < 0) 1675 if (optlen < 0)
1684 return -EINVAL; 1676 return -EINVAL;
1685 1677
1686 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) 1678 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1687 { 1679 if (sock != NULL) {
1688 err = security_socket_setsockopt(sock,level,optname); 1680 err = security_socket_setsockopt(sock, level, optname);
1689 if (err) 1681 if (err)
1690 goto out_put; 1682 goto out_put;
1691 1683
1692 if (level == SOL_SOCKET) 1684 if (level == SOL_SOCKET)
1693 err=sock_setsockopt(sock,level,optname,optval,optlen); 1685 err =
1686 sock_setsockopt(sock, level, optname, optval,
1687 optlen);
1694 else 1688 else
1695 err=sock->ops->setsockopt(sock, level, optname, optval, optlen); 1689 err =
1690 sock->ops->setsockopt(sock, level, optname, optval,
1691 optlen);
1696out_put: 1692out_put:
1697 fput_light(sock->file, fput_needed); 1693 fput_light(sock->file, fput_needed);
1698 } 1694 }
@@ -1704,27 +1700,32 @@ out_put:
1704 * to pass a user mode parameter for the protocols to sort out. 1700 * to pass a user mode parameter for the protocols to sort out.
1705 */ 1701 */
1706 1702
1707asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen) 1703asmlinkage long sys_getsockopt(int fd, int level, int optname,
1704 char __user *optval, int __user *optlen)
1708{ 1705{
1709 int err, fput_needed; 1706 int err, fput_needed;
1710 struct socket *sock; 1707 struct socket *sock;
1711 1708
1712 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) { 1709 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1710 if (sock != NULL) {
1713 err = security_socket_getsockopt(sock, level, optname); 1711 err = security_socket_getsockopt(sock, level, optname);
1714 if (err) 1712 if (err)
1715 goto out_put; 1713 goto out_put;
1716 1714
1717 if (level == SOL_SOCKET) 1715 if (level == SOL_SOCKET)
1718 err=sock_getsockopt(sock,level,optname,optval,optlen); 1716 err =
1717 sock_getsockopt(sock, level, optname, optval,
1718 optlen);
1719 else 1719 else
1720 err=sock->ops->getsockopt(sock, level, optname, optval, optlen); 1720 err =
1721 sock->ops->getsockopt(sock, level, optname, optval,
1722 optlen);
1721out_put: 1723out_put:
1722 fput_light(sock->file, fput_needed); 1724 fput_light(sock->file, fput_needed);
1723 } 1725 }
1724 return err; 1726 return err;
1725} 1727}
1726 1728
1727
1728/* 1729/*
1729 * Shutdown a socket. 1730 * Shutdown a socket.
1730 */ 1731 */
@@ -1734,8 +1735,8 @@ asmlinkage long sys_shutdown(int fd, int how)
1734 int err, fput_needed; 1735 int err, fput_needed;
1735 struct socket *sock; 1736 struct socket *sock;
1736 1737
1737 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL) 1738 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1738 { 1739 if (sock != NULL) {
1739 err = security_socket_shutdown(sock, how); 1740 err = security_socket_shutdown(sock, how);
1740 if (!err) 1741 if (!err)
1741 err = sock->ops->shutdown(sock, how); 1742 err = sock->ops->shutdown(sock, how);
@@ -1744,41 +1745,42 @@ asmlinkage long sys_shutdown(int fd, int how)
1744 return err; 1745 return err;
1745} 1746}
1746 1747
1747/* A couple of helpful macros for getting the address of the 32/64 bit 1748/* A couple of helpful macros for getting the address of the 32/64 bit
1748 * fields which are the same type (int / unsigned) on our platforms. 1749 * fields which are the same type (int / unsigned) on our platforms.
1749 */ 1750 */
1750#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member) 1751#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1751#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen) 1752#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1752#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags) 1753#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1753 1754
1754
1755/* 1755/*
1756 * BSD sendmsg interface 1756 * BSD sendmsg interface
1757 */ 1757 */
1758 1758
1759asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags) 1759asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1760{ 1760{
1761 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg; 1761 struct compat_msghdr __user *msg_compat =
1762 (struct compat_msghdr __user *)msg;
1762 struct socket *sock; 1763 struct socket *sock;
1763 char address[MAX_SOCK_ADDR]; 1764 char address[MAX_SOCK_ADDR];
1764 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1765 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1765 unsigned char ctl[sizeof(struct cmsghdr) + 20] 1766 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1766 __attribute__ ((aligned (sizeof(__kernel_size_t)))); 1767 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1767 /* 20 is size of ipv6_pktinfo */ 1768 /* 20 is size of ipv6_pktinfo */
1768 unsigned char *ctl_buf = ctl; 1769 unsigned char *ctl_buf = ctl;
1769 struct msghdr msg_sys; 1770 struct msghdr msg_sys;
1770 int err, ctl_len, iov_size, total_len; 1771 int err, ctl_len, iov_size, total_len;
1771 int fput_needed; 1772 int fput_needed;
1772 1773
1773 err = -EFAULT; 1774 err = -EFAULT;
1774 if (MSG_CMSG_COMPAT & flags) { 1775 if (MSG_CMSG_COMPAT & flags) {
1775 if (get_compat_msghdr(&msg_sys, msg_compat)) 1776 if (get_compat_msghdr(&msg_sys, msg_compat))
1776 return -EFAULT; 1777 return -EFAULT;
1777 } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr))) 1778 }
1779 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1778 return -EFAULT; 1780 return -EFAULT;
1779 1781
1780 sock = sockfd_lookup_light(fd, &err, &fput_needed); 1782 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1781 if (!sock) 1783 if (!sock)
1782 goto out; 1784 goto out;
1783 1785
1784 /* do not move before msg_sys is valid */ 1786 /* do not move before msg_sys is valid */
@@ -1786,7 +1788,7 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1786 if (msg_sys.msg_iovlen > UIO_MAXIOV) 1788 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1787 goto out_put; 1789 goto out_put;
1788 1790
1789 /* Check whether to allocate the iovec area*/ 1791 /* Check whether to allocate the iovec area */
1790 err = -ENOMEM; 1792 err = -ENOMEM;
1791 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); 1793 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1792 if (msg_sys.msg_iovlen > UIO_FASTIOV) { 1794 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
@@ -1800,7 +1802,7 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1800 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ); 1802 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1801 } else 1803 } else
1802 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ); 1804 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1803 if (err < 0) 1805 if (err < 0)
1804 goto out_freeiov; 1806 goto out_freeiov;
1805 total_len = err; 1807 total_len = err;
1806 1808
@@ -1808,18 +1810,19 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1808 1810
1809 if (msg_sys.msg_controllen > INT_MAX) 1811 if (msg_sys.msg_controllen > INT_MAX)
1810 goto out_freeiov; 1812 goto out_freeiov;
1811 ctl_len = msg_sys.msg_controllen; 1813 ctl_len = msg_sys.msg_controllen;
1812 if ((MSG_CMSG_COMPAT & flags) && ctl_len) { 1814 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1813 err = cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, sizeof(ctl)); 1815 err =
1816 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1817 sizeof(ctl));
1814 if (err) 1818 if (err)
1815 goto out_freeiov; 1819 goto out_freeiov;
1816 ctl_buf = msg_sys.msg_control; 1820 ctl_buf = msg_sys.msg_control;
1817 ctl_len = msg_sys.msg_controllen; 1821 ctl_len = msg_sys.msg_controllen;
1818 } else if (ctl_len) { 1822 } else if (ctl_len) {
1819 if (ctl_len > sizeof(ctl)) 1823 if (ctl_len > sizeof(ctl)) {
1820 {
1821 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL); 1824 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1822 if (ctl_buf == NULL) 1825 if (ctl_buf == NULL)
1823 goto out_freeiov; 1826 goto out_freeiov;
1824 } 1827 }
1825 err = -EFAULT; 1828 err = -EFAULT;
@@ -1828,7 +1831,8 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1828 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted 1831 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1829 * checking falls down on this. 1832 * checking falls down on this.
1830 */ 1833 */
1831 if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len)) 1834 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1835 ctl_len))
1832 goto out_freectl; 1836 goto out_freectl;
1833 msg_sys.msg_control = ctl_buf; 1837 msg_sys.msg_control = ctl_buf;
1834 } 1838 }
@@ -1839,14 +1843,14 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1839 err = sock_sendmsg(sock, &msg_sys, total_len); 1843 err = sock_sendmsg(sock, &msg_sys, total_len);
1840 1844
1841out_freectl: 1845out_freectl:
1842 if (ctl_buf != ctl) 1846 if (ctl_buf != ctl)
1843 sock_kfree_s(sock->sk, ctl_buf, ctl_len); 1847 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1844out_freeiov: 1848out_freeiov:
1845 if (iov != iovstack) 1849 if (iov != iovstack)
1846 sock_kfree_s(sock->sk, iov, iov_size); 1850 sock_kfree_s(sock->sk, iov, iov_size);
1847out_put: 1851out_put:
1848 fput_light(sock->file, fput_needed); 1852 fput_light(sock->file, fput_needed);
1849out: 1853out:
1850 return err; 1854 return err;
1851} 1855}
1852 1856
@@ -1854,12 +1858,14 @@ out:
1854 * BSD recvmsg interface 1858 * BSD recvmsg interface
1855 */ 1859 */
1856 1860
1857asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags) 1861asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
1862 unsigned int flags)
1858{ 1863{
1859 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg; 1864 struct compat_msghdr __user *msg_compat =
1865 (struct compat_msghdr __user *)msg;
1860 struct socket *sock; 1866 struct socket *sock;
1861 struct iovec iovstack[UIO_FASTIOV]; 1867 struct iovec iovstack[UIO_FASTIOV];
1862 struct iovec *iov=iovstack; 1868 struct iovec *iov = iovstack;
1863 struct msghdr msg_sys; 1869 struct msghdr msg_sys;
1864 unsigned long cmsg_ptr; 1870 unsigned long cmsg_ptr;
1865 int err, iov_size, total_len, len; 1871 int err, iov_size, total_len, len;
@@ -1871,13 +1877,13 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1871 /* user mode address pointers */ 1877 /* user mode address pointers */
1872 struct sockaddr __user *uaddr; 1878 struct sockaddr __user *uaddr;
1873 int __user *uaddr_len; 1879 int __user *uaddr_len;
1874 1880
1875 if (MSG_CMSG_COMPAT & flags) { 1881 if (MSG_CMSG_COMPAT & flags) {
1876 if (get_compat_msghdr(&msg_sys, msg_compat)) 1882 if (get_compat_msghdr(&msg_sys, msg_compat))
1877 return -EFAULT; 1883 return -EFAULT;
1878 } else 1884 }
1879 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr))) 1885 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1880 return -EFAULT; 1886 return -EFAULT;
1881 1887
1882 sock = sockfd_lookup_light(fd, &err, &fput_needed); 1888 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1883 if (!sock) 1889 if (!sock)
@@ -1886,8 +1892,8 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1886 err = -EMSGSIZE; 1892 err = -EMSGSIZE;
1887 if (msg_sys.msg_iovlen > UIO_MAXIOV) 1893 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1888 goto out_put; 1894 goto out_put;
1889 1895
1890 /* Check whether to allocate the iovec area*/ 1896 /* Check whether to allocate the iovec area */
1891 err = -ENOMEM; 1897 err = -ENOMEM;
1892 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); 1898 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1893 if (msg_sys.msg_iovlen > UIO_FASTIOV) { 1899 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
@@ -1897,11 +1903,11 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1897 } 1903 }
1898 1904
1899 /* 1905 /*
1900 * Save the user-mode address (verify_iovec will change the 1906 * Save the user-mode address (verify_iovec will change the
1901 * kernel msghdr to use the kernel address space) 1907 * kernel msghdr to use the kernel address space)
1902 */ 1908 */
1903 1909
1904 uaddr = (void __user *) msg_sys.msg_name; 1910 uaddr = (void __user *)msg_sys.msg_name;
1905 uaddr_len = COMPAT_NAMELEN(msg); 1911 uaddr_len = COMPAT_NAMELEN(msg);
1906 if (MSG_CMSG_COMPAT & flags) { 1912 if (MSG_CMSG_COMPAT & flags) {
1907 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE); 1913 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
@@ -1909,13 +1915,13 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1909 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE); 1915 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1910 if (err < 0) 1916 if (err < 0)
1911 goto out_freeiov; 1917 goto out_freeiov;
1912 total_len=err; 1918 total_len = err;
1913 1919
1914 cmsg_ptr = (unsigned long)msg_sys.msg_control; 1920 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1915 msg_sys.msg_flags = 0; 1921 msg_sys.msg_flags = 0;
1916 if (MSG_CMSG_COMPAT & flags) 1922 if (MSG_CMSG_COMPAT & flags)
1917 msg_sys.msg_flags = MSG_CMSG_COMPAT; 1923 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1918 1924
1919 if (sock->file->f_flags & O_NONBLOCK) 1925 if (sock->file->f_flags & O_NONBLOCK)
1920 flags |= MSG_DONTWAIT; 1926 flags |= MSG_DONTWAIT;
1921 err = sock_recvmsg(sock, &msg_sys, total_len, flags); 1927 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
@@ -1924,7 +1930,8 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1924 len = err; 1930 len = err;
1925 1931
1926 if (uaddr != NULL) { 1932 if (uaddr != NULL) {
1927 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len); 1933 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr,
1934 uaddr_len);
1928 if (err < 0) 1935 if (err < 0)
1929 goto out_freeiov; 1936 goto out_freeiov;
1930 } 1937 }
@@ -1933,10 +1940,10 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1933 if (err) 1940 if (err)
1934 goto out_freeiov; 1941 goto out_freeiov;
1935 if (MSG_CMSG_COMPAT & flags) 1942 if (MSG_CMSG_COMPAT & flags)
1936 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 1943 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
1937 &msg_compat->msg_controllen); 1944 &msg_compat->msg_controllen);
1938 else 1945 else
1939 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 1946 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
1940 &msg->msg_controllen); 1947 &msg->msg_controllen);
1941 if (err) 1948 if (err)
1942 goto out_freeiov; 1949 goto out_freeiov;
@@ -1955,163 +1962,187 @@ out:
1955 1962
1956/* Argument list sizes for sys_socketcall */ 1963/* Argument list sizes for sys_socketcall */
1957#define AL(x) ((x) * sizeof(unsigned long)) 1964#define AL(x) ((x) * sizeof(unsigned long))
1958static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3), 1965static const unsigned char nargs[18]={
1959 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6), 1966 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1960 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)}; 1967 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1968 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)
1969};
1970
1961#undef AL 1971#undef AL
1962 1972
1963/* 1973/*
1964 * System call vectors. 1974 * System call vectors.
1965 * 1975 *
1966 * Argument checking cleaned up. Saved 20% in size. 1976 * Argument checking cleaned up. Saved 20% in size.
1967 * This function doesn't need to set the kernel lock because 1977 * This function doesn't need to set the kernel lock because
1968 * it is set by the callees. 1978 * it is set by the callees.
1969 */ 1979 */
1970 1980
1971asmlinkage long sys_socketcall(int call, unsigned long __user *args) 1981asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1972{ 1982{
1973 unsigned long a[6]; 1983 unsigned long a[6];
1974 unsigned long a0,a1; 1984 unsigned long a0, a1;
1975 int err; 1985 int err;
1976 1986
1977 if(call<1||call>SYS_RECVMSG) 1987 if (call < 1 || call > SYS_RECVMSG)
1978 return -EINVAL; 1988 return -EINVAL;
1979 1989
1980 /* copy_from_user should be SMP safe. */ 1990 /* copy_from_user should be SMP safe. */
1981 if (copy_from_user(a, args, nargs[call])) 1991 if (copy_from_user(a, args, nargs[call]))
1982 return -EFAULT; 1992 return -EFAULT;
1983 1993
1984 err = audit_socketcall(nargs[call]/sizeof(unsigned long), a); 1994 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
1985 if (err) 1995 if (err)
1986 return err; 1996 return err;
1987 1997
1988 a0=a[0]; 1998 a0 = a[0];
1989 a1=a[1]; 1999 a1 = a[1];
1990 2000
1991 switch(call) 2001 switch (call) {
1992 { 2002 case SYS_SOCKET:
1993 case SYS_SOCKET: 2003 err = sys_socket(a0, a1, a[2]);
1994 err = sys_socket(a0,a1,a[2]); 2004 break;
1995 break; 2005 case SYS_BIND:
1996 case SYS_BIND: 2006 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
1997 err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]); 2007 break;
1998 break; 2008 case SYS_CONNECT:
1999 case SYS_CONNECT: 2009 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2000 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]); 2010 break;
2001 break; 2011 case SYS_LISTEN:
2002 case SYS_LISTEN: 2012 err = sys_listen(a0, a1);
2003 err = sys_listen(a0,a1); 2013 break;
2004 break; 2014 case SYS_ACCEPT:
2005 case SYS_ACCEPT: 2015 err =
2006 err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]); 2016 sys_accept(a0, (struct sockaddr __user *)a1,
2007 break; 2017 (int __user *)a[2]);
2008 case SYS_GETSOCKNAME: 2018 break;
2009 err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]); 2019 case SYS_GETSOCKNAME:
2010 break; 2020 err =
2011 case SYS_GETPEERNAME: 2021 sys_getsockname(a0, (struct sockaddr __user *)a1,
2012 err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]); 2022 (int __user *)a[2]);
2013 break; 2023 break;
2014 case SYS_SOCKETPAIR: 2024 case SYS_GETPEERNAME:
2015 err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]); 2025 err =
2016 break; 2026 sys_getpeername(a0, (struct sockaddr __user *)a1,
2017 case SYS_SEND: 2027 (int __user *)a[2]);
2018 err = sys_send(a0, (void __user *)a1, a[2], a[3]); 2028 break;
2019 break; 2029 case SYS_SOCKETPAIR:
2020 case SYS_SENDTO: 2030 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2021 err = sys_sendto(a0,(void __user *)a1, a[2], a[3], 2031 break;
2022 (struct sockaddr __user *)a[4], a[5]); 2032 case SYS_SEND:
2023 break; 2033 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2024 case SYS_RECV: 2034 break;
2025 err = sys_recv(a0, (void __user *)a1, a[2], a[3]); 2035 case SYS_SENDTO:
2026 break; 2036 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2027 case SYS_RECVFROM: 2037 (struct sockaddr __user *)a[4], a[5]);
2028 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 2038 break;
2029 (struct sockaddr __user *)a[4], (int __user *)a[5]); 2039 case SYS_RECV:
2030 break; 2040 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2031 case SYS_SHUTDOWN: 2041 break;
2032 err = sys_shutdown(a0,a1); 2042 case SYS_RECVFROM:
2033 break; 2043 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2034 case SYS_SETSOCKOPT: 2044 (struct sockaddr __user *)a[4],
2035 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]); 2045 (int __user *)a[5]);
2036 break; 2046 break;
2037 case SYS_GETSOCKOPT: 2047 case SYS_SHUTDOWN:
2038 err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]); 2048 err = sys_shutdown(a0, a1);
2039 break; 2049 break;
2040 case SYS_SENDMSG: 2050 case SYS_SETSOCKOPT:
2041 err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]); 2051 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2042 break; 2052 break;
2043 case SYS_RECVMSG: 2053 case SYS_GETSOCKOPT:
2044 err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]); 2054 err =
2045 break; 2055 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2046 default: 2056 (int __user *)a[4]);
2047 err = -EINVAL; 2057 break;
2048 break; 2058 case SYS_SENDMSG:
2059 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2060 break;
2061 case SYS_RECVMSG:
2062 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2063 break;
2064 default:
2065 err = -EINVAL;
2066 break;
2049 } 2067 }
2050 return err; 2068 return err;
2051} 2069}
2052 2070
2053#endif /* __ARCH_WANT_SYS_SOCKETCALL */ 2071#endif /* __ARCH_WANT_SYS_SOCKETCALL */
2054 2072
2055/* 2073/**
2074 * sock_register - add a socket protocol handler
2075 * @ops: description of protocol
2076 *
2056 * This function is called by a protocol handler that wants to 2077 * This function is called by a protocol handler that wants to
2057 * advertise its address family, and have it linked into the 2078 * advertise its address family, and have it linked into the
2058 * SOCKET module. 2079 * socket interface. The value ops->family coresponds to the
2080 * socket system call protocol family.
2059 */ 2081 */
2060 2082int sock_register(const struct net_proto_family *ops)
2061int sock_register(struct net_proto_family *ops)
2062{ 2083{
2063 int err; 2084 int err;
2064 2085
2065 if (ops->family >= NPROTO) { 2086 if (ops->family >= NPROTO) {
2066 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO); 2087 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2088 NPROTO);
2067 return -ENOBUFS; 2089 return -ENOBUFS;
2068 } 2090 }
2069 net_family_write_lock(); 2091
2070 err = -EEXIST; 2092 spin_lock(&net_family_lock);
2071 if (net_families[ops->family] == NULL) { 2093 if (net_families[ops->family])
2072 net_families[ops->family]=ops; 2094 err = -EEXIST;
2095 else {
2096 net_families[ops->family] = ops;
2073 err = 0; 2097 err = 0;
2074 } 2098 }
2075 net_family_write_unlock(); 2099 spin_unlock(&net_family_lock);
2076 printk(KERN_INFO "NET: Registered protocol family %d\n", 2100
2077 ops->family); 2101 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2078 return err; 2102 return err;
2079} 2103}
2080 2104
2081/* 2105/**
2106 * sock_unregister - remove a protocol handler
2107 * @family: protocol family to remove
2108 *
2082 * This function is called by a protocol handler that wants to 2109 * This function is called by a protocol handler that wants to
2083 * remove its address family, and have it unlinked from the 2110 * remove its address family, and have it unlinked from the
2084 * SOCKET module. 2111 * new socket creation.
2112 *
2113 * If protocol handler is a module, then it can use module reference
2114 * counts to protect against new references. If protocol handler is not
2115 * a module then it needs to provide its own protection in
2116 * the ops->create routine.
2085 */ 2117 */
2086 2118void sock_unregister(int family)
2087int sock_unregister(int family)
2088{ 2119{
2089 if (family < 0 || family >= NPROTO) 2120 BUG_ON(family < 0 || family >= NPROTO);
2090 return -1;
2091 2121
2092 net_family_write_lock(); 2122 spin_lock(&net_family_lock);
2093 net_families[family]=NULL; 2123 net_families[family] = NULL;
2094 net_family_write_unlock(); 2124 spin_unlock(&net_family_lock);
2095 printk(KERN_INFO "NET: Unregistered protocol family %d\n", 2125
2096 family); 2126 synchronize_rcu();
2097 return 0; 2127
2128 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2098} 2129}
2099 2130
2100static int __init sock_init(void) 2131static int __init sock_init(void)
2101{ 2132{
2102 /* 2133 /*
2103 * Initialize sock SLAB cache. 2134 * Initialize sock SLAB cache.
2104 */ 2135 */
2105 2136
2106 sk_init(); 2137 sk_init();
2107 2138
2108 /* 2139 /*
2109 * Initialize skbuff SLAB cache 2140 * Initialize skbuff SLAB cache
2110 */ 2141 */
2111 skb_init(); 2142 skb_init();
2112 2143
2113 /* 2144 /*
2114 * Initialize the protocols module. 2145 * Initialize the protocols module.
2115 */ 2146 */
2116 2147
2117 init_inodecache(); 2148 init_inodecache();
@@ -2137,7 +2168,7 @@ void socket_seq_show(struct seq_file *seq)
2137 int counter = 0; 2168 int counter = 0;
2138 2169
2139 for_each_possible_cpu(cpu) 2170 for_each_possible_cpu(cpu)
2140 counter += per_cpu(sockets_in_use, cpu); 2171 counter += per_cpu(sockets_in_use, cpu);
2141 2172
2142 /* It can be negative, by the way. 8) */ 2173 /* It can be negative, by the way. 8) */
2143 if (counter < 0) 2174 if (counter < 0)
@@ -2145,11 +2176,11 @@ void socket_seq_show(struct seq_file *seq)
2145 2176
2146 seq_printf(seq, "sockets: used %d\n", counter); 2177 seq_printf(seq, "sockets: used %d\n", counter);
2147} 2178}
2148#endif /* CONFIG_PROC_FS */ 2179#endif /* CONFIG_PROC_FS */
2149 2180
2150#ifdef CONFIG_COMPAT 2181#ifdef CONFIG_COMPAT
2151static long compat_sock_ioctl(struct file *file, unsigned cmd, 2182static long compat_sock_ioctl(struct file *file, unsigned cmd,
2152 unsigned long arg) 2183 unsigned long arg)
2153{ 2184{
2154 struct socket *sock = file->private_data; 2185 struct socket *sock = file->private_data;
2155 int ret = -ENOIOCTLCMD; 2186 int ret = -ENOIOCTLCMD;
@@ -2161,6 +2192,109 @@ static long compat_sock_ioctl(struct file *file, unsigned cmd,
2161} 2192}
2162#endif 2193#endif
2163 2194
2195int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2196{
2197 return sock->ops->bind(sock, addr, addrlen);
2198}
2199
2200int kernel_listen(struct socket *sock, int backlog)
2201{
2202 return sock->ops->listen(sock, backlog);
2203}
2204
2205int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2206{
2207 struct sock *sk = sock->sk;
2208 int err;
2209
2210 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
2211 newsock);
2212 if (err < 0)
2213 goto done;
2214
2215 err = sock->ops->accept(sock, *newsock, flags);
2216 if (err < 0) {
2217 sock_release(*newsock);
2218 goto done;
2219 }
2220
2221 (*newsock)->ops = sock->ops;
2222
2223done:
2224 return err;
2225}
2226
2227int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
2228 int flags)
2229{
2230 return sock->ops->connect(sock, addr, addrlen, flags);
2231}
2232
2233int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
2234 int *addrlen)
2235{
2236 return sock->ops->getname(sock, addr, addrlen, 0);
2237}
2238
2239int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
2240 int *addrlen)
2241{
2242 return sock->ops->getname(sock, addr, addrlen, 1);
2243}
2244
2245int kernel_getsockopt(struct socket *sock, int level, int optname,
2246 char *optval, int *optlen)
2247{
2248 mm_segment_t oldfs = get_fs();
2249 int err;
2250
2251 set_fs(KERNEL_DS);
2252 if (level == SOL_SOCKET)
2253 err = sock_getsockopt(sock, level, optname, optval, optlen);
2254 else
2255 err = sock->ops->getsockopt(sock, level, optname, optval,
2256 optlen);
2257 set_fs(oldfs);
2258 return err;
2259}
2260
2261int kernel_setsockopt(struct socket *sock, int level, int optname,
2262 char *optval, int optlen)
2263{
2264 mm_segment_t oldfs = get_fs();
2265 int err;
2266
2267 set_fs(KERNEL_DS);
2268 if (level == SOL_SOCKET)
2269 err = sock_setsockopt(sock, level, optname, optval, optlen);
2270 else
2271 err = sock->ops->setsockopt(sock, level, optname, optval,
2272 optlen);
2273 set_fs(oldfs);
2274 return err;
2275}
2276
2277int kernel_sendpage(struct socket *sock, struct page *page, int offset,
2278 size_t size, int flags)
2279{
2280 if (sock->ops->sendpage)
2281 return sock->ops->sendpage(sock, page, offset, size, flags);
2282
2283 return sock_no_sendpage(sock, page, offset, size, flags);
2284}
2285
2286int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
2287{
2288 mm_segment_t oldfs = get_fs();
2289 int err;
2290
2291 set_fs(KERNEL_DS);
2292 err = sock->ops->ioctl(sock, cmd, arg);
2293 set_fs(oldfs);
2294
2295 return err;
2296}
2297
2164/* ABI emulation layers need these two */ 2298/* ABI emulation layers need these two */
2165EXPORT_SYMBOL(move_addr_to_kernel); 2299EXPORT_SYMBOL(move_addr_to_kernel);
2166EXPORT_SYMBOL(move_addr_to_user); 2300EXPORT_SYMBOL(move_addr_to_user);
@@ -2177,3 +2311,13 @@ EXPORT_SYMBOL(sock_wake_async);
2177EXPORT_SYMBOL(sockfd_lookup); 2311EXPORT_SYMBOL(sockfd_lookup);
2178EXPORT_SYMBOL(kernel_sendmsg); 2312EXPORT_SYMBOL(kernel_sendmsg);
2179EXPORT_SYMBOL(kernel_recvmsg); 2313EXPORT_SYMBOL(kernel_recvmsg);
2314EXPORT_SYMBOL(kernel_bind);
2315EXPORT_SYMBOL(kernel_listen);
2316EXPORT_SYMBOL(kernel_accept);
2317EXPORT_SYMBOL(kernel_connect);
2318EXPORT_SYMBOL(kernel_getsockname);
2319EXPORT_SYMBOL(kernel_getpeername);
2320EXPORT_SYMBOL(kernel_getsockopt);
2321EXPORT_SYMBOL(kernel_setsockopt);
2322EXPORT_SYMBOL(kernel_sendpage);
2323EXPORT_SYMBOL(kernel_sock_ioctl);