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 565f5e8d1191..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,18 +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/config.h>
62#include <linux/mm.h> 61#include <linux/mm.h>
63#include <linux/smp_lock.h>
64#include <linux/socket.h> 62#include <linux/socket.h>
65#include <linux/file.h> 63#include <linux/file.h>
66#include <linux/net.h> 64#include <linux/net.h>
67#include <linux/interrupt.h> 65#include <linux/interrupt.h>
66#include <linux/rcupdate.h>
68#include <linux/netdevice.h> 67#include <linux/netdevice.h>
69#include <linux/proc_fs.h> 68#include <linux/proc_fs.h>
70#include <linux/seq_file.h> 69#include <linux/seq_file.h>
@@ -97,25 +96,24 @@
97 96
98static int sock_no_open(struct inode *irrelevant, struct file *dontcare); 97static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
99static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf, 98static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
100 size_t size, loff_t pos); 99 size_t size, loff_t pos);
101static 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,
102 size_t size, loff_t pos); 101 size_t size, loff_t pos);
103static int sock_mmap(struct file *file, struct vm_area_struct * vma); 102static int sock_mmap(struct file *file, struct vm_area_struct *vma);
104 103
105static int sock_close(struct inode *inode, struct file *file); 104static int sock_close(struct inode *inode, struct file *file);
106static unsigned int sock_poll(struct file *file, 105static unsigned int sock_poll(struct file *file,
107 struct poll_table_struct *wait); 106 struct poll_table_struct *wait);
108static long sock_ioctl(struct file *file, 107static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
109 unsigned int cmd, unsigned long arg);
110#ifdef CONFIG_COMPAT 108#ifdef CONFIG_COMPAT
111static long compat_sock_ioctl(struct file *file, 109static long compat_sock_ioctl(struct file *file,
112 unsigned int cmd, unsigned long arg); 110 unsigned int cmd, unsigned long arg);
113#endif 111#endif
114static int sock_fasync(int fd, struct file *filp, int on); 112static int sock_fasync(int fd, struct file *filp, int on);
115static ssize_t sock_readv(struct file *file, const struct iovec *vector, 113static ssize_t sock_readv(struct file *file, const struct iovec *vector,
116 unsigned long count, loff_t *ppos); 114 unsigned long count, loff_t *ppos);
117static ssize_t sock_writev(struct file *file, const struct iovec *vector, 115static ssize_t sock_writev(struct file *file, const struct iovec *vector,
118 unsigned long count, loff_t *ppos); 116 unsigned long count, loff_t *ppos);
119static ssize_t sock_sendpage(struct file *file, struct page *page, 117static ssize_t sock_sendpage(struct file *file, struct page *page,
120 int offset, size_t size, loff_t *ppos, int more); 118 int offset, size_t size, loff_t *ppos, int more);
121 119
@@ -148,52 +146,8 @@ static struct file_operations socket_file_ops = {
148 * The protocol list. Each protocol is registered in here. 146 * The protocol list. Each protocol is registered in here.
149 */ 147 */
150 148
151static struct net_proto_family *net_families[NPROTO];
152
153#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
154static atomic_t net_family_lockct = ATOMIC_INIT(0);
155static DEFINE_SPINLOCK(net_family_lock); 149static DEFINE_SPINLOCK(net_family_lock);
156 150static const struct net_proto_family *net_families[NPROTO] __read_mostly;
157/* The strategy is: modifications net_family vector are short, do not
158 sleep and veeery rare, but read access should be free of any exclusive
159 locks.
160 */
161
162static void net_family_write_lock(void)
163{
164 spin_lock(&net_family_lock);
165 while (atomic_read(&net_family_lockct) != 0) {
166 spin_unlock(&net_family_lock);
167
168 yield();
169
170 spin_lock(&net_family_lock);
171 }
172}
173
174static __inline__ void net_family_write_unlock(void)
175{
176 spin_unlock(&net_family_lock);
177}
178
179static __inline__ void net_family_read_lock(void)
180{
181 atomic_inc(&net_family_lockct);
182 spin_unlock_wait(&net_family_lock);
183}
184
185static __inline__ void net_family_read_unlock(void)
186{
187 atomic_dec(&net_family_lockct);
188}
189
190#else
191#define net_family_write_lock() do { } while(0)
192#define net_family_write_unlock() do { } while(0)
193#define net_family_read_lock() do { } while(0)
194#define net_family_read_unlock() do { } while(0)
195#endif
196
197 151
198/* 152/*
199 * Statistics counters of the socket lists 153 * Statistics counters of the socket lists
@@ -202,19 +156,20 @@ static __inline__ void net_family_read_unlock(void)
202static DEFINE_PER_CPU(int, sockets_in_use) = 0; 156static DEFINE_PER_CPU(int, sockets_in_use) = 0;
203 157
204/* 158/*
205 * Support routines. Move socket addresses back and forth across the kernel/user 159 * Support routines.
206 * 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.
207 */ 162 */
208 163
209#define MAX_SOCK_ADDR 128 /* 108 for Unix domain - 164#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
210 16 for IP, 16 for IPX, 165 16 for IP, 16 for IPX,
211 24 for IPv6, 166 24 for IPv6,
212 about 80 for AX.25 167 about 80 for AX.25
213 must be at least one bigger than 168 must be at least one bigger than
214 the AF_UNIX size (see net/unix/af_unix.c 169 the AF_UNIX size (see net/unix/af_unix.c
215 :unix_mkname()). 170 :unix_mkname()).
216 */ 171 */
217 172
218/** 173/**
219 * move_addr_to_kernel - copy a socket address into kernel space 174 * move_addr_to_kernel - copy a socket address into kernel space
220 * @uaddr: Address in user space 175 * @uaddr: Address in user space
@@ -228,11 +183,11 @@ static DEFINE_PER_CPU(int, sockets_in_use) = 0;
228 183
229int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr) 184int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
230{ 185{
231 if(ulen<0||ulen>MAX_SOCK_ADDR) 186 if (ulen < 0 || ulen > MAX_SOCK_ADDR)
232 return -EINVAL; 187 return -EINVAL;
233 if(ulen==0) 188 if (ulen == 0)
234 return 0; 189 return 0;
235 if(copy_from_user(kaddr,uaddr,ulen)) 190 if (copy_from_user(kaddr, uaddr, ulen))
236 return -EFAULT; 191 return -EFAULT;
237 return audit_sockaddr(ulen, kaddr); 192 return audit_sockaddr(ulen, kaddr);
238} 193}
@@ -253,51 +208,52 @@ int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
253 * 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
254 * specified. Zero is returned for a success. 209 * specified. Zero is returned for a success.
255 */ 210 */
256 211
257int 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)
258{ 214{
259 int err; 215 int err;
260 int len; 216 int len;
261 217
262 if((err=get_user(len, ulen))) 218 err = get_user(len, ulen);
219 if (err)
263 return err; 220 return err;
264 if(len>klen) 221 if (len > klen)
265 len=klen; 222 len = klen;
266 if(len<0 || len> MAX_SOCK_ADDR) 223 if (len < 0 || len > MAX_SOCK_ADDR)
267 return -EINVAL; 224 return -EINVAL;
268 if(len) 225 if (len) {
269 {
270 if (audit_sockaddr(klen, kaddr)) 226 if (audit_sockaddr(klen, kaddr))
271 return -ENOMEM; 227 return -ENOMEM;
272 if(copy_to_user(uaddr,kaddr,len)) 228 if (copy_to_user(uaddr, kaddr, len))
273 return -EFAULT; 229 return -EFAULT;
274 } 230 }
275 /* 231 /*
276 * "fromlen shall refer to the value before truncation.." 232 * "fromlen shall refer to the value before truncation.."
277 * 1003.1g 233 * 1003.1g
278 */ 234 */
279 return __put_user(klen, ulen); 235 return __put_user(klen, ulen);
280} 236}
281 237
282#define SOCKFS_MAGIC 0x534F434B 238#define SOCKFS_MAGIC 0x534F434B
283 239
284static kmem_cache_t * sock_inode_cachep __read_mostly; 240static kmem_cache_t *sock_inode_cachep __read_mostly;
285 241
286static struct inode *sock_alloc_inode(struct super_block *sb) 242static struct inode *sock_alloc_inode(struct super_block *sb)
287{ 243{
288 struct socket_alloc *ei; 244 struct socket_alloc *ei;
289 ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL); 245
246 ei = kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
290 if (!ei) 247 if (!ei)
291 return NULL; 248 return NULL;
292 init_waitqueue_head(&ei->socket.wait); 249 init_waitqueue_head(&ei->socket.wait);
293 250
294 ei->socket.fasync_list = NULL; 251 ei->socket.fasync_list = NULL;
295 ei->socket.state = SS_UNCONNECTED; 252 ei->socket.state = SS_UNCONNECTED;
296 ei->socket.flags = 0; 253 ei->socket.flags = 0;
297 ei->socket.ops = NULL; 254 ei->socket.ops = NULL;
298 ei->socket.sk = NULL; 255 ei->socket.sk = NULL;
299 ei->socket.file = NULL; 256 ei->socket.file = NULL;
300 ei->socket.flags = 0;
301 257
302 return &ei->vfs_inode; 258 return &ei->vfs_inode;
303} 259}
@@ -308,22 +264,25 @@ static void sock_destroy_inode(struct inode *inode)
308 container_of(inode, struct socket_alloc, vfs_inode)); 264 container_of(inode, struct socket_alloc, vfs_inode));
309} 265}
310 266
311static 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)
312{ 268{
313 struct socket_alloc *ei = (struct socket_alloc *) foo; 269 struct socket_alloc *ei = (struct socket_alloc *)foo;
314 270
315 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == 271 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR))
316 SLAB_CTOR_CONSTRUCTOR) 272 == SLAB_CTOR_CONSTRUCTOR)
317 inode_init_once(&ei->vfs_inode); 273 inode_init_once(&ei->vfs_inode);
318} 274}
319 275
320static int init_inodecache(void) 276static int init_inodecache(void)
321{ 277{
322 sock_inode_cachep = kmem_cache_create("sock_inode_cache", 278 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
323 sizeof(struct socket_alloc), 279 sizeof(struct socket_alloc),
324 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 280 0,
325 SLAB_MEM_SPREAD), 281 (SLAB_HWCACHE_ALIGN |
326 init_once, NULL); 282 SLAB_RECLAIM_ACCOUNT |
283 SLAB_MEM_SPREAD),
284 init_once,
285 NULL);
327 if (sock_inode_cachep == NULL) 286 if (sock_inode_cachep == NULL)
328 return -ENOMEM; 287 return -ENOMEM;
329 return 0; 288 return 0;
@@ -336,7 +295,8 @@ static struct super_operations sockfs_ops = {
336}; 295};
337 296
338static int sockfs_get_sb(struct file_system_type *fs_type, 297static int sockfs_get_sb(struct file_system_type *fs_type,
339 int flags, const char *dev_name, void *data, struct vfsmount *mnt) 298 int flags, const char *dev_name, void *data,
299 struct vfsmount *mnt)
340{ 300{
341 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC, 301 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
342 mnt); 302 mnt);
@@ -349,12 +309,13 @@ static struct file_system_type sock_fs_type = {
349 .get_sb = sockfs_get_sb, 309 .get_sb = sockfs_get_sb,
350 .kill_sb = kill_anon_super, 310 .kill_sb = kill_anon_super,
351}; 311};
312
352static int sockfs_delete_dentry(struct dentry *dentry) 313static int sockfs_delete_dentry(struct dentry *dentry)
353{ 314{
354 return 1; 315 return 1;
355} 316}
356static struct dentry_operations sockfs_dentry_operations = { 317static struct dentry_operations sockfs_dentry_operations = {
357 .d_delete = sockfs_delete_dentry, 318 .d_delete = sockfs_delete_dentry,
358}; 319};
359 320
360/* 321/*
@@ -478,10 +439,12 @@ struct socket *sockfd_lookup(int fd, int *err)
478 struct file *file; 439 struct file *file;
479 struct socket *sock; 440 struct socket *sock;
480 441
481 if (!(file = fget(fd))) { 442 file = fget(fd);
443 if (!file) {
482 *err = -EBADF; 444 *err = -EBADF;
483 return NULL; 445 return NULL;
484 } 446 }
447
485 sock = sock_from_file(file, err); 448 sock = sock_from_file(file, err);
486 if (!sock) 449 if (!sock)
487 fput(file); 450 fput(file);
@@ -506,7 +469,7 @@ static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
506 469
507/** 470/**
508 * sock_alloc - allocate a socket 471 * sock_alloc - allocate a socket
509 * 472 *
510 * 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
511 * 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
512 * NULL is returned. 475 * NULL is returned.
@@ -514,8 +477,8 @@ static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
514 477
515static struct socket *sock_alloc(void) 478static struct socket *sock_alloc(void)
516{ 479{
517 struct inode * inode; 480 struct inode *inode;
518 struct socket * sock; 481 struct socket *sock;
519 482
520 inode = new_inode(sock_mnt->mnt_sb); 483 inode = new_inode(sock_mnt->mnt_sb);
521 if (!inode) 484 if (!inode)
@@ -523,7 +486,7 @@ static struct socket *sock_alloc(void)
523 486
524 sock = SOCKET_I(inode); 487 sock = SOCKET_I(inode);
525 488
526 inode->i_mode = S_IFSOCK|S_IRWXUGO; 489 inode->i_mode = S_IFSOCK | S_IRWXUGO;
527 inode->i_uid = current->fsuid; 490 inode->i_uid = current->fsuid;
528 inode->i_gid = current->fsgid; 491 inode->i_gid = current->fsgid;
529 492
@@ -537,7 +500,7 @@ static struct socket *sock_alloc(void)
537 * 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
538 * creepy crawlies in. 501 * creepy crawlies in.
539 */ 502 */
540 503
541static int sock_no_open(struct inode *irrelevant, struct file *dontcare) 504static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
542{ 505{
543 return -ENXIO; 506 return -ENXIO;
@@ -554,9 +517,9 @@ const struct file_operations bad_sock_fops = {
554 * 517 *
555 * 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
556 * 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
557 * an inode not a file. 520 * an inode not a file.
558 */ 521 */
559 522
560void sock_release(struct socket *sock) 523void sock_release(struct socket *sock)
561{ 524{
562 if (sock->ops) { 525 if (sock->ops) {
@@ -576,10 +539,10 @@ void sock_release(struct socket *sock)
576 iput(SOCK_INODE(sock)); 539 iput(SOCK_INODE(sock));
577 return; 540 return;
578 } 541 }
579 sock->file=NULL; 542 sock->file = NULL;
580} 543}
581 544
582static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock, 545static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
583 struct msghdr *msg, size_t size) 546 struct msghdr *msg, size_t size)
584{ 547{
585 struct sock_iocb *si = kiocb_to_siocb(iocb); 548 struct sock_iocb *si = kiocb_to_siocb(iocb);
@@ -622,14 +585,14 @@ int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
622 * the following is safe, since for compiler definitions of kvec and 585 * the following is safe, since for compiler definitions of kvec and
623 * iovec are identical, yielding the same in-core layout and alignment 586 * iovec are identical, yielding the same in-core layout and alignment
624 */ 587 */
625 msg->msg_iov = (struct iovec *)vec, 588 msg->msg_iov = (struct iovec *)vec;
626 msg->msg_iovlen = num; 589 msg->msg_iovlen = num;
627 result = sock_sendmsg(sock, msg, size); 590 result = sock_sendmsg(sock, msg, size);
628 set_fs(oldfs); 591 set_fs(oldfs);
629 return result; 592 return result;
630} 593}
631 594
632static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, 595static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
633 struct msghdr *msg, size_t size, int flags) 596 struct msghdr *msg, size_t size, int flags)
634{ 597{
635 int err; 598 int err;
@@ -648,14 +611,14 @@ static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
648 return sock->ops->recvmsg(iocb, sock, msg, size, flags); 611 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
649} 612}
650 613
651int sock_recvmsg(struct socket *sock, struct msghdr *msg, 614int sock_recvmsg(struct socket *sock, struct msghdr *msg,
652 size_t size, int flags) 615 size_t size, int flags)
653{ 616{
654 struct kiocb iocb; 617 struct kiocb iocb;
655 struct sock_iocb siocb; 618 struct sock_iocb siocb;
656 int ret; 619 int ret;
657 620
658 init_sync_kiocb(&iocb, NULL); 621 init_sync_kiocb(&iocb, NULL);
659 iocb.private = &siocb; 622 iocb.private = &siocb;
660 ret = __sock_recvmsg(&iocb, sock, msg, size, flags); 623 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
661 if (-EIOCBQUEUED == ret) 624 if (-EIOCBQUEUED == ret)
@@ -663,9 +626,8 @@ int sock_recvmsg(struct socket *sock, struct msghdr *msg,
663 return ret; 626 return ret;
664} 627}
665 628
666int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 629int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
667 struct kvec *vec, size_t num, 630 struct kvec *vec, size_t num, size_t size, int flags)
668 size_t size, int flags)
669{ 631{
670 mm_segment_t oldfs = get_fs(); 632 mm_segment_t oldfs = get_fs();
671 int result; 633 int result;
@@ -675,8 +637,7 @@ int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
675 * the following is safe, since for compiler definitions of kvec and 637 * the following is safe, since for compiler definitions of kvec and
676 * iovec are identical, yielding the same in-core layout and alignment 638 * iovec are identical, yielding the same in-core layout and alignment
677 */ 639 */
678 msg->msg_iov = (struct iovec *)vec, 640 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
679 msg->msg_iovlen = num;
680 result = sock_recvmsg(sock, msg, size, flags); 641 result = sock_recvmsg(sock, msg, size, flags);
681 set_fs(oldfs); 642 set_fs(oldfs);
682 return result; 643 return result;
@@ -703,7 +664,8 @@ static ssize_t sock_sendpage(struct file *file, struct page *page,
703} 664}
704 665
705static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb, 666static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
706 char __user *ubuf, size_t size, struct sock_iocb *siocb) 667 char __user *ubuf, size_t size,
668 struct sock_iocb *siocb)
707{ 669{
708 if (!is_sync_kiocb(iocb)) { 670 if (!is_sync_kiocb(iocb)) {
709 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL); 671 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
@@ -721,20 +683,21 @@ static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
721} 683}
722 684
723static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb, 685static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
724 struct file *file, struct iovec *iov, unsigned long nr_segs) 686 struct file *file, struct iovec *iov,
687 unsigned long nr_segs)
725{ 688{
726 struct socket *sock = file->private_data; 689 struct socket *sock = file->private_data;
727 size_t size = 0; 690 size_t size = 0;
728 int i; 691 int i;
729 692
730 for (i = 0 ; i < nr_segs ; i++) 693 for (i = 0; i < nr_segs; i++)
731 size += iov[i].iov_len; 694 size += iov[i].iov_len;
732 695
733 msg->msg_name = NULL; 696 msg->msg_name = NULL;
734 msg->msg_namelen = 0; 697 msg->msg_namelen = 0;
735 msg->msg_control = NULL; 698 msg->msg_control = NULL;
736 msg->msg_controllen = 0; 699 msg->msg_controllen = 0;
737 msg->msg_iov = (struct iovec *) iov; 700 msg->msg_iov = (struct iovec *)iov;
738 msg->msg_iovlen = nr_segs; 701 msg->msg_iovlen = nr_segs;
739 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 702 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
740 703
@@ -749,7 +712,7 @@ static ssize_t sock_readv(struct file *file, const struct iovec *iov,
749 struct msghdr msg; 712 struct msghdr msg;
750 int ret; 713 int ret;
751 714
752 init_sync_kiocb(&iocb, NULL); 715 init_sync_kiocb(&iocb, NULL);
753 iocb.private = &siocb; 716 iocb.private = &siocb;
754 717
755 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);
@@ -759,7 +722,7 @@ static ssize_t sock_readv(struct file *file, const struct iovec *iov,
759} 722}
760 723
761static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf, 724static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
762 size_t count, loff_t pos) 725 size_t count, loff_t pos)
763{ 726{
764 struct sock_iocb siocb, *x; 727 struct sock_iocb siocb, *x;
765 728
@@ -772,24 +735,25 @@ static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
772 if (!x) 735 if (!x)
773 return -ENOMEM; 736 return -ENOMEM;
774 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, 737 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
775 &x->async_iov, 1); 738 &x->async_iov, 1);
776} 739}
777 740
778static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb, 741static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
779 struct file *file, struct iovec *iov, unsigned long nr_segs) 742 struct file *file, struct iovec *iov,
743 unsigned long nr_segs)
780{ 744{
781 struct socket *sock = file->private_data; 745 struct socket *sock = file->private_data;
782 size_t size = 0; 746 size_t size = 0;
783 int i; 747 int i;
784 748
785 for (i = 0 ; i < nr_segs ; i++) 749 for (i = 0; i < nr_segs; i++)
786 size += iov[i].iov_len; 750 size += iov[i].iov_len;
787 751
788 msg->msg_name = NULL; 752 msg->msg_name = NULL;
789 msg->msg_namelen = 0; 753 msg->msg_namelen = 0;
790 msg->msg_control = NULL; 754 msg->msg_control = NULL;
791 msg->msg_controllen = 0; 755 msg->msg_controllen = 0;
792 msg->msg_iov = (struct iovec *) iov; 756 msg->msg_iov = (struct iovec *)iov;
793 msg->msg_iovlen = nr_segs; 757 msg->msg_iovlen = nr_segs;
794 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 758 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
795 if (sock->type == SOCK_SEQPACKET) 759 if (sock->type == SOCK_SEQPACKET)
@@ -816,7 +780,7 @@ static ssize_t sock_writev(struct file *file, const struct iovec *iov,
816} 780}
817 781
818static 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,
819 size_t count, loff_t pos) 783 size_t count, loff_t pos)
820{ 784{
821 struct sock_iocb siocb, *x; 785 struct sock_iocb siocb, *x;
822 786
@@ -830,46 +794,48 @@ static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
830 return -ENOMEM; 794 return -ENOMEM;
831 795
832 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, 796 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
833 &x->async_iov, 1); 797 &x->async_iov, 1);
834} 798}
835 799
836
837/* 800/*
838 * Atomic setting of ioctl hooks to avoid race 801 * Atomic setting of ioctl hooks to avoid race
839 * with module unload. 802 * with module unload.
840 */ 803 */
841 804
842static DEFINE_MUTEX(br_ioctl_mutex); 805static DEFINE_MUTEX(br_ioctl_mutex);
843static int (*br_ioctl_hook)(unsigned int cmd, void __user *arg) = NULL; 806static int (*br_ioctl_hook) (unsigned int cmd, void __user *arg) = NULL;
844 807
845void brioctl_set(int (*hook)(unsigned int, void __user *)) 808void brioctl_set(int (*hook) (unsigned int, void __user *))
846{ 809{
847 mutex_lock(&br_ioctl_mutex); 810 mutex_lock(&br_ioctl_mutex);
848 br_ioctl_hook = hook; 811 br_ioctl_hook = hook;
849 mutex_unlock(&br_ioctl_mutex); 812 mutex_unlock(&br_ioctl_mutex);
850} 813}
814
851EXPORT_SYMBOL(brioctl_set); 815EXPORT_SYMBOL(brioctl_set);
852 816
853static DEFINE_MUTEX(vlan_ioctl_mutex); 817static DEFINE_MUTEX(vlan_ioctl_mutex);
854static int (*vlan_ioctl_hook)(void __user *arg); 818static int (*vlan_ioctl_hook) (void __user *arg);
855 819
856void vlan_ioctl_set(int (*hook)(void __user *)) 820void vlan_ioctl_set(int (*hook) (void __user *))
857{ 821{
858 mutex_lock(&vlan_ioctl_mutex); 822 mutex_lock(&vlan_ioctl_mutex);
859 vlan_ioctl_hook = hook; 823 vlan_ioctl_hook = hook;
860 mutex_unlock(&vlan_ioctl_mutex); 824 mutex_unlock(&vlan_ioctl_mutex);
861} 825}
826
862EXPORT_SYMBOL(vlan_ioctl_set); 827EXPORT_SYMBOL(vlan_ioctl_set);
863 828
864static DEFINE_MUTEX(dlci_ioctl_mutex); 829static DEFINE_MUTEX(dlci_ioctl_mutex);
865static int (*dlci_ioctl_hook)(unsigned int, void __user *); 830static int (*dlci_ioctl_hook) (unsigned int, void __user *);
866 831
867void dlci_ioctl_set(int (*hook)(unsigned int, void __user *)) 832void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
868{ 833{
869 mutex_lock(&dlci_ioctl_mutex); 834 mutex_lock(&dlci_ioctl_mutex);
870 dlci_ioctl_hook = hook; 835 dlci_ioctl_hook = hook;
871 mutex_unlock(&dlci_ioctl_mutex); 836 mutex_unlock(&dlci_ioctl_mutex);
872} 837}
838
873EXPORT_SYMBOL(dlci_ioctl_set); 839EXPORT_SYMBOL(dlci_ioctl_set);
874 840
875/* 841/*
@@ -891,8 +857,8 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
891 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) { 857 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
892 err = dev_ioctl(cmd, argp); 858 err = dev_ioctl(cmd, argp);
893 } else 859 } else
894#endif /* CONFIG_WIRELESS_EXT */ 860#endif /* CONFIG_WIRELESS_EXT */
895 switch (cmd) { 861 switch (cmd) {
896 case FIOSETOWN: 862 case FIOSETOWN:
897 case SIOCSPGRP: 863 case SIOCSPGRP:
898 err = -EFAULT; 864 err = -EFAULT;
@@ -902,7 +868,8 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
902 break; 868 break;
903 case FIOGETOWN: 869 case FIOGETOWN:
904 case SIOCGPGRP: 870 case SIOCGPGRP:
905 err = put_user(sock->file->f_owner.pid, (int __user *)argp); 871 err = put_user(sock->file->f_owner.pid,
872 (int __user *)argp);
906 break; 873 break;
907 case SIOCGIFBR: 874 case SIOCGIFBR:
908 case SIOCSIFBR: 875 case SIOCSIFBR:
@@ -913,7 +880,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
913 request_module("bridge"); 880 request_module("bridge");
914 881
915 mutex_lock(&br_ioctl_mutex); 882 mutex_lock(&br_ioctl_mutex);
916 if (br_ioctl_hook) 883 if (br_ioctl_hook)
917 err = br_ioctl_hook(cmd, argp); 884 err = br_ioctl_hook(cmd, argp);
918 mutex_unlock(&br_ioctl_mutex); 885 mutex_unlock(&br_ioctl_mutex);
919 break; 886 break;
@@ -930,7 +897,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
930 break; 897 break;
931 case SIOCGIFDIVERT: 898 case SIOCGIFDIVERT:
932 case SIOCSIFDIVERT: 899 case SIOCSIFDIVERT:
933 /* Convert this to call through a hook */ 900 /* Convert this to call through a hook */
934 err = divert_ioctl(cmd, argp); 901 err = divert_ioctl(cmd, argp);
935 break; 902 break;
936 case SIOCADDDLCI: 903 case SIOCADDDLCI:
@@ -955,7 +922,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
955 if (err == -ENOIOCTLCMD) 922 if (err == -ENOIOCTLCMD)
956 err = dev_ioctl(cmd, argp); 923 err = dev_ioctl(cmd, argp);
957 break; 924 break;
958 } 925 }
959 return err; 926 return err;
960} 927}
961 928
@@ -963,7 +930,7 @@ int sock_create_lite(int family, int type, int protocol, struct socket **res)
963{ 930{
964 int err; 931 int err;
965 struct socket *sock = NULL; 932 struct socket *sock = NULL;
966 933
967 err = security_socket_create(family, type, protocol, 1); 934 err = security_socket_create(family, type, protocol, 1);
968 if (err) 935 if (err)
969 goto out; 936 goto out;
@@ -974,26 +941,33 @@ int sock_create_lite(int family, int type, int protocol, struct socket **res)
974 goto out; 941 goto out;
975 } 942 }
976 943
977 security_socket_post_create(sock, family, type, protocol, 1);
978 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
979out: 949out:
980 *res = sock; 950 *res = sock;
981 return err; 951 return err;
952out_release:
953 sock_release(sock);
954 sock = NULL;
955 goto out;
982} 956}
983 957
984/* No kernel lock held - perfect */ 958/* No kernel lock held - perfect */
985static unsigned int sock_poll(struct file *file, poll_table * wait) 959static unsigned int sock_poll(struct file *file, poll_table *wait)
986{ 960{
987 struct socket *sock; 961 struct socket *sock;
988 962
989 /* 963 /*
990 * 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.
991 */ 965 */
992 sock = file->private_data; 966 sock = file->private_data;
993 return sock->ops->poll(file, sock, wait); 967 return sock->ops->poll(file, sock, wait);
994} 968}
995 969
996static int sock_mmap(struct file * file, struct vm_area_struct * vma) 970static int sock_mmap(struct file *file, struct vm_area_struct *vma)
997{ 971{
998 struct socket *sock = file->private_data; 972 struct socket *sock = file->private_data;
999 973
@@ -1003,12 +977,11 @@ static int sock_mmap(struct file * file, struct vm_area_struct * vma)
1003static int sock_close(struct inode *inode, struct file *filp) 977static int sock_close(struct inode *inode, struct file *filp)
1004{ 978{
1005 /* 979 /*
1006 * It was possible the inode is NULL we were 980 * It was possible the inode is NULL we were
1007 * closing an unfinished socket. 981 * closing an unfinished socket.
1008 */ 982 */
1009 983
1010 if (!inode) 984 if (!inode) {
1011 {
1012 printk(KERN_DEBUG "sock_close: NULL inode\n"); 985 printk(KERN_DEBUG "sock_close: NULL inode\n");
1013 return 0; 986 return 0;
1014 } 987 }
@@ -1034,57 +1007,52 @@ static int sock_close(struct inode *inode, struct file *filp)
1034 1007
1035static int sock_fasync(int fd, struct file *filp, int on) 1008static int sock_fasync(int fd, struct file *filp, int on)
1036{ 1009{
1037 struct fasync_struct *fa, *fna=NULL, **prev; 1010 struct fasync_struct *fa, *fna = NULL, **prev;
1038 struct socket *sock; 1011 struct socket *sock;
1039 struct sock *sk; 1012 struct sock *sk;
1040 1013
1041 if (on) 1014 if (on) {
1042 {
1043 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL); 1015 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1044 if(fna==NULL) 1016 if (fna == NULL)
1045 return -ENOMEM; 1017 return -ENOMEM;
1046 } 1018 }
1047 1019
1048 sock = filp->private_data; 1020 sock = filp->private_data;
1049 1021
1050 if ((sk=sock->sk) == NULL) { 1022 sk = sock->sk;
1023 if (sk == NULL) {
1051 kfree(fna); 1024 kfree(fna);
1052 return -EINVAL; 1025 return -EINVAL;
1053 } 1026 }
1054 1027
1055 lock_sock(sk); 1028 lock_sock(sk);
1056 1029
1057 prev=&(sock->fasync_list); 1030 prev = &(sock->fasync_list);
1058 1031
1059 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev) 1032 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1060 if (fa->fa_file==filp) 1033 if (fa->fa_file == filp)
1061 break; 1034 break;
1062 1035
1063 if(on) 1036 if (on) {
1064 { 1037 if (fa != NULL) {
1065 if(fa!=NULL)
1066 {
1067 write_lock_bh(&sk->sk_callback_lock); 1038 write_lock_bh(&sk->sk_callback_lock);
1068 fa->fa_fd=fd; 1039 fa->fa_fd = fd;
1069 write_unlock_bh(&sk->sk_callback_lock); 1040 write_unlock_bh(&sk->sk_callback_lock);
1070 1041
1071 kfree(fna); 1042 kfree(fna);
1072 goto out; 1043 goto out;
1073 } 1044 }
1074 fna->fa_file=filp; 1045 fna->fa_file = filp;
1075 fna->fa_fd=fd; 1046 fna->fa_fd = fd;
1076 fna->magic=FASYNC_MAGIC; 1047 fna->magic = FASYNC_MAGIC;
1077 fna->fa_next=sock->fasync_list; 1048 fna->fa_next = sock->fasync_list;
1078 write_lock_bh(&sk->sk_callback_lock); 1049 write_lock_bh(&sk->sk_callback_lock);
1079 sock->fasync_list=fna; 1050 sock->fasync_list = fna;
1080 write_unlock_bh(&sk->sk_callback_lock); 1051 write_unlock_bh(&sk->sk_callback_lock);
1081 } 1052 } else {
1082 else 1053 if (fa != NULL) {
1083 {
1084 if (fa!=NULL)
1085 {
1086 write_lock_bh(&sk->sk_callback_lock); 1054 write_lock_bh(&sk->sk_callback_lock);
1087 *prev=fa->fa_next; 1055 *prev = fa->fa_next;
1088 write_unlock_bh(&sk->sk_callback_lock); 1056 write_unlock_bh(&sk->sk_callback_lock);
1089 kfree(fa); 1057 kfree(fa);
1090 } 1058 }
@@ -1101,10 +1069,9 @@ int sock_wake_async(struct socket *sock, int how, int band)
1101{ 1069{
1102 if (!sock || !sock->fasync_list) 1070 if (!sock || !sock->fasync_list)
1103 return -1; 1071 return -1;
1104 switch (how) 1072 switch (how) {
1105 {
1106 case 1: 1073 case 1:
1107 1074
1108 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags)) 1075 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1109 break; 1076 break;
1110 goto call_kill; 1077 goto call_kill;
@@ -1113,7 +1080,7 @@ int sock_wake_async(struct socket *sock, int how, int band)
1113 break; 1080 break;
1114 /* fall through */ 1081 /* fall through */
1115 case 0: 1082 case 0:
1116 call_kill: 1083call_kill:
1117 __kill_fasync(sock->fasync_list, SIGIO, band); 1084 __kill_fasync(sock->fasync_list, SIGIO, band);
1118 break; 1085 break;
1119 case 3: 1086 case 3:
@@ -1122,13 +1089,15 @@ int sock_wake_async(struct socket *sock, int how, int band)
1122 return 0; 1089 return 0;
1123} 1090}
1124 1091
1125static 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)
1126{ 1094{
1127 int err; 1095 int err;
1128 struct socket *sock; 1096 struct socket *sock;
1097 const struct net_proto_family *pf;
1129 1098
1130 /* 1099 /*
1131 * Check protocol is in range 1100 * Check protocol is in range
1132 */ 1101 */
1133 if (family < 0 || family >= NPROTO) 1102 if (family < 0 || family >= NPROTO)
1134 return -EAFNOSUPPORT; 1103 return -EAFNOSUPPORT;
@@ -1141,10 +1110,11 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
1141 deadlock in module load. 1110 deadlock in module load.
1142 */ 1111 */
1143 if (family == PF_INET && type == SOCK_PACKET) { 1112 if (family == PF_INET && type == SOCK_PACKET) {
1144 static int warned; 1113 static int warned;
1145 if (!warned) { 1114 if (!warned) {
1146 warned = 1; 1115 warned = 1;
1147 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);
1148 } 1118 }
1149 family = PF_PACKET; 1119 family = PF_PACKET;
1150 } 1120 }
@@ -1152,78 +1122,84 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
1152 err = security_socket_create(family, type, protocol, kern); 1122 err = security_socket_create(family, type, protocol, kern);
1153 if (err) 1123 if (err)
1154 return err; 1124 return err;
1155 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
1156#if defined(CONFIG_KMOD) 1141#if defined(CONFIG_KMOD)
1157 /* Attempt to load a protocol module if the find failed. 1142 /* Attempt to load a protocol module if the find failed.
1158 * 1143 *
1159 * 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
1160 * requested real, full-featured networking support upon configuration. 1145 * requested real, full-featured networking support upon configuration.
1161 * Otherwise module support will break! 1146 * Otherwise module support will break!
1162 */ 1147 */
1163 if (net_families[family]==NULL) 1148 if (net_families[family] == NULL)
1164 { 1149 request_module("net-pf-%d", family);
1165 request_module("net-pf-%d",family);
1166 }
1167#endif 1150#endif
1168 1151
1169 net_family_read_lock(); 1152 rcu_read_lock();
1170 if (net_families[family] == NULL) { 1153 pf = rcu_dereference(net_families[family]);
1171 err = -EAFNOSUPPORT; 1154 err = -EAFNOSUPPORT;
1172 goto out; 1155 if (!pf)
1173 } 1156 goto out_release;
1174
1175/*
1176 * Allocate the socket and allow the family to set things up. if
1177 * the protocol is 0, the family is instructed to select an appropriate
1178 * default.
1179 */
1180
1181 if (!(sock = sock_alloc())) {
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);