Merge with 2.4.0-test3-pre7.
[linux-2.6/linux-mips.git] / net / socket.c
blob4cd725731db2b87fdc5b356f1d33bea32f57b8ff
1 /*
2 * NET An implementation of the SOCKET network access protocol.
4 * Version: @(#)socket.c 1.1.93 18/02/95
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
7 * Ross Biro, <bir7@leland.Stanford.Edu>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
55 * This module is effectively the top level interface to the BSD socket
56 * paradigm.
60 #include <linux/config.h>
61 #include <linux/mm.h>
62 #include <linux/smp_lock.h>
63 #include <linux/socket.h>
64 #include <linux/file.h>
65 #include <linux/net.h>
66 #include <linux/interrupt.h>
67 #include <linux/netdevice.h>
68 #include <linux/proc_fs.h>
69 #include <linux/wanrouter.h>
70 #include <linux/init.h>
71 #include <linux/poll.h>
72 #include <linux/cache.h>
73 #include <linux/module.h>
75 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
76 #include <linux/kmod.h>
77 #endif
79 #include <asm/uaccess.h>
81 #include <linux/inet.h>
82 #include <net/ip.h>
83 #include <net/sock.h>
84 #include <net/tcp.h>
85 #include <net/udp.h>
86 #include <net/scm.h>
87 #include <linux/netfilter.h>
89 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
90 static loff_t sock_lseek(struct file *file, loff_t offset, int whence);
91 static ssize_t sock_read(struct file *file, char *buf,
92 size_t size, loff_t *ppos);
93 static ssize_t sock_write(struct file *file, const char *buf,
94 size_t size, loff_t *ppos);
95 static int sock_mmap(struct file *file, struct vm_area_struct * vma);
97 static int sock_close(struct inode *inode, struct file *file);
98 static unsigned int sock_poll(struct file *file,
99 struct poll_table_struct *wait);
100 static int sock_ioctl(struct inode *inode, struct file *file,
101 unsigned int cmd, unsigned long arg);
102 static int sock_fasync(int fd, struct file *filp, int on);
103 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
104 unsigned long count, loff_t *ppos);
105 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
106 unsigned long count, loff_t *ppos);
110 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
111 * in the operation structures but are done directly via the socketcall() multiplexor.
114 static struct file_operations socket_file_ops = {
115 llseek: sock_lseek,
116 read: sock_read,
117 write: sock_write,
118 poll: sock_poll,
119 ioctl: sock_ioctl,
120 mmap: sock_mmap,
121 open: sock_no_open, /* special open code to disallow open via /proc */
122 release: sock_close,
123 fasync: sock_fasync,
124 readv: sock_readv,
125 writev: sock_writev
129 * The protocol list. Each protocol is registered in here.
132 static struct net_proto_family *net_families[NPROTO];
134 #ifdef CONFIG_SMP
135 static atomic_t net_family_lockct = ATOMIC_INIT(0);
136 static spinlock_t net_family_lock = SPIN_LOCK_UNLOCKED;
138 /* The strategy is: modifications net_family vector are short, do not
139 sleep and veeery rare, but read access should be free of any exclusive
140 locks.
143 static void net_family_write_lock(void)
145 spin_lock(&net_family_lock);
146 while (atomic_read(&net_family_lockct) != 0) {
147 spin_unlock(&net_family_lock);
149 current->policy |= SCHED_YIELD;
150 schedule();
152 spin_lock(&net_family_lock);
156 static __inline__ void net_family_write_unlock(void)
158 spin_unlock(&net_family_lock);
161 static __inline__ void net_family_read_lock(void)
163 atomic_inc(&net_family_lockct);
164 spin_unlock_wait(&net_family_lock);
167 static __inline__ void net_family_read_unlock(void)
169 atomic_dec(&net_family_lockct);
172 #else
173 #define net_family_write_lock() do { } while(0)
174 #define net_family_write_unlock() do { } while(0)
175 #define net_family_read_lock() do { } while(0)
176 #define net_family_read_unlock() do { } while(0)
177 #endif
181 * Statistics counters of the socket lists
184 static union {
185 int counter;
186 char __pad[SMP_CACHE_BYTES];
187 } sockets_in_use[NR_CPUS] __cacheline_aligned = {{0}};
190 * Support routines. Move socket addresses back and forth across the kernel/user
191 * divide and look after the messy bits.
194 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
195 16 for IP, 16 for IPX,
196 24 for IPv6,
197 about 80 for AX.25
198 must be at least one bigger than
199 the AF_UNIX size (see net/unix/af_unix.c
200 :unix_mkname()).
204 * move_addr_to_kernel - copy a socket address into kernel space
205 * @uaddr: Address in user space
206 * @kaddr: Address in kernel space
207 * @ulen: Length in user space
209 * The address is copied into kernel space. If the provided address is
210 * too long an error code of -EINVAL is returned. If the copy gives
211 * invalid addresses -EFAULT is returned. On a success 0 is returned.
214 int move_addr_to_kernel(void *uaddr, int ulen, void *kaddr)
216 if(ulen<0||ulen>MAX_SOCK_ADDR)
217 return -EINVAL;
218 if(ulen==0)
219 return 0;
220 if(copy_from_user(kaddr,uaddr,ulen))
221 return -EFAULT;
222 return 0;
226 * move_addr_to_user - copy an address to user space
227 * @kaddr: kernel space address
228 * @klen: length of address in kernel
229 * @uaddr: user space address
230 * @ulen: pointer to user length field
232 * The value pointed to by ulen on entry is the buffer length available.
233 * This is overwritten with the buffer space used. -EINVAL is returned
234 * if an overlong buffer is specified or a negative buffer size. -EFAULT
235 * is returned if either the buffer or the length field are not
236 * accessible.
237 * After copying the data up to the limit the user specifies, the true
238 * length of the data is written over the length limit the user
239 * specified. Zero is returned for a success.
242 int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen)
244 int err;
245 int len;
247 if((err=get_user(len, ulen)))
248 return err;
249 if(len>klen)
250 len=klen;
251 if(len<0 || len> MAX_SOCK_ADDR)
252 return -EINVAL;
253 if(len)
255 if(copy_to_user(uaddr,kaddr,len))
256 return -EFAULT;
259 * "fromlen shall refer to the value before truncation.."
260 * 1003.1g
262 return __put_user(klen, ulen);
265 #define SOCKFS_MAGIC 0x534F434B
266 static int sockfs_statfs(struct super_block *sb, struct statfs *buf)
268 buf->f_type = SOCKFS_MAGIC;
269 buf->f_bsize = 1024;
270 buf->f_namelen = 255;
271 return 0;
274 static struct super_operations sockfs_ops = {
275 statfs: sockfs_statfs,
278 static struct super_block * sockfs_read_super(struct super_block *sb, void *data, int silent)
280 struct inode *root = get_empty_inode();
281 if (!root)
282 return NULL;
283 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
284 root->i_uid = root->i_gid = 0;
285 root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
286 root->i_sb = sb;
287 root->i_dev = sb->s_dev;
288 sb->s_blocksize = 1024;
289 sb->s_blocksize_bits = 10;
290 sb->s_magic = SOCKFS_MAGIC;
291 sb->s_op = &sockfs_ops;
292 sb->s_root = d_alloc(NULL, &(const struct qstr) { "socket:", 7, 0 });
293 if (!sb->s_root) {
294 iput(root);
295 return NULL;
297 sb->s_root->d_sb = sb;
298 sb->s_root->d_parent = sb->s_root;
299 d_instantiate(sb->s_root, root);
300 return sb;
303 static struct vfsmount *sock_mnt;
304 static DECLARE_FSTYPE(sock_fs_type, "sockfs", sockfs_read_super,
305 FS_NOMOUNT|FS_SINGLE);
306 static int sockfs_delete_dentry(struct dentry *dentry)
308 return 1;
310 static struct dentry_operations sockfs_dentry_operations = {
311 d_delete: sockfs_delete_dentry,
315 * Obtains the first available file descriptor and sets it up for use.
317 * This functions creates file structure and maps it to fd space
318 * of current process. On success it returns file descriptor
319 * and file struct implicitly stored in sock->file.
320 * Note that another thread may close file descriptor before we return
321 * from this function. We use the fact that now we do not refer
322 * to socket after mapping. If one day we will need it, this
323 * function will inincrement ref. count on file by 1.
325 * In any case returned fd MAY BE not valid!
326 * This race condition is inavoidable
327 * with shared fd spaces, we cannot solve is inside kernel,
328 * but we take care of internal coherence yet.
331 static int sock_map_fd(struct socket *sock)
333 int fd;
334 struct qstr this;
335 char name[32];
338 * Find a file descriptor suitable for return to the user.
341 fd = get_unused_fd();
342 if (fd >= 0) {
343 struct file *file = get_empty_filp();
345 if (!file) {
346 put_unused_fd(fd);
347 fd = -ENFILE;
348 goto out;
351 sprintf(name, "[%lu]", sock->inode->i_ino);
352 this.name = name;
353 this.len = strlen(name);
354 this.hash = sock->inode->i_ino;
356 file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
357 if (!file->f_dentry) {
358 put_filp(file);
359 put_unused_fd(fd);
360 fd = -ENOMEM;
361 goto out;
363 file->f_dentry->d_op = &sockfs_dentry_operations;
364 d_add(file->f_dentry, sock->inode);
365 file->f_vfsmnt = mntget(sock_mnt);
367 sock->file = file;
368 file->f_op = sock->inode->i_fop = &socket_file_ops;
369 file->f_mode = 3;
370 file->f_flags = O_RDWR;
371 file->f_pos = 0;
372 fd_install(fd, file);
375 out:
376 return fd;
379 extern __inline__ struct socket *socki_lookup(struct inode *inode)
381 return &inode->u.socket_i;
385 * sockfd_lookup - Go from a file number to its socket slot
386 * @fd: file handle
387 * @err: pointer to an error code return
389 * The file handle passed in is locked and the socket it is bound
390 * too is returned. If an error occurs the err pointer is overwritten
391 * with a negative errno code and NULL is returned. The function checks
392 * for both invalid handles and passing a handle which is not a socket.
394 * On a success the socket object pointer is returned.
397 struct socket *sockfd_lookup(int fd, int *err)
399 struct file *file;
400 struct inode *inode;
401 struct socket *sock;
403 if (!(file = fget(fd)))
405 *err = -EBADF;
406 return NULL;
409 inode = file->f_dentry->d_inode;
410 if (!inode || !inode->i_sock || !(sock = socki_lookup(inode)))
412 *err = -ENOTSOCK;
413 fput(file);
414 return NULL;
417 if (sock->file != file) {
418 printk(KERN_ERR "socki_lookup: socket file changed!\n");
419 sock->file = file;
421 return sock;
424 extern __inline__ void sockfd_put(struct socket *sock)
426 fput(sock->file);
430 * sock_alloc - allocate a socket
432 * Allocate a new inode and socket object. The two are bound together
433 * and initialised. The socket is then returned. If we are out of inodes
434 * NULL is returned.
437 struct socket *sock_alloc(void)
439 struct inode * inode;
440 struct socket * sock;
442 inode = get_empty_inode();
443 if (!inode)
444 return NULL;
446 sock = socki_lookup(inode);
448 inode->i_mode = S_IFSOCK|S_IRWXUGO;
449 inode->i_sock = 1;
450 inode->i_uid = current->fsuid;
451 inode->i_gid = current->fsgid;
453 sock->inode = inode;
454 init_waitqueue_head(&sock->wait);
455 sock->fasync_list = NULL;
456 sock->state = SS_UNCONNECTED;
457 sock->flags = 0;
458 sock->ops = NULL;
459 sock->sk = NULL;
460 sock->file = NULL;
462 sockets_in_use[smp_processor_id()].counter++;
463 return sock;
467 * In theory you can't get an open on this inode, but /proc provides
468 * a back door. Remember to keep it shut otherwise you'll let the
469 * creepy crawlies in.
472 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
474 return -ENXIO;
478 * sock_release - close a socket
479 * @sock: socket to close
481 * The socket is released from the protocol stack if it has a release
482 * callback, and the inode is then released if the socket is bound to
483 * an inode not a file.
486 void sock_release(struct socket *sock)
488 if (sock->ops)
489 sock->ops->release(sock);
491 if (sock->fasync_list)
492 printk(KERN_ERR "sock_release: fasync list not empty!\n");
494 sockets_in_use[smp_processor_id()].counter--;
495 if (!sock->file) {
496 iput(sock->inode);
497 return;
499 sock->file=NULL;
502 int sock_sendmsg(struct socket *sock, struct msghdr *msg, int size)
504 int err;
505 struct scm_cookie scm;
507 err = scm_send(sock, msg, &scm);
508 if (err >= 0) {
509 err = sock->ops->sendmsg(sock, msg, size, &scm);
510 scm_destroy(&scm);
512 return err;
515 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags)
517 struct scm_cookie scm;
519 memset(&scm, 0, sizeof(scm));
521 size = sock->ops->recvmsg(sock, msg, size, flags, &scm);
522 if (size >= 0)
523 scm_recv(sock, msg, &scm, flags);
525 return size;
530 * Sockets are not seekable.
533 static loff_t sock_lseek(struct file *file, loff_t offset, int whence)
535 return -ESPIPE;
539 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
540 * area ubuf...ubuf+size-1 is writable before asking the protocol.
543 static ssize_t sock_read(struct file *file, char *ubuf,
544 size_t size, loff_t *ppos)
546 struct socket *sock;
547 struct iovec iov;
548 struct msghdr msg;
549 int flags;
551 if (ppos != &file->f_pos)
552 return -ESPIPE;
553 if (size==0) /* Match SYS5 behaviour */
554 return 0;
556 sock = socki_lookup(file->f_dentry->d_inode);
558 msg.msg_name=NULL;
559 msg.msg_namelen=0;
560 msg.msg_iov=&iov;
561 msg.msg_iovlen=1;
562 msg.msg_control=NULL;
563 msg.msg_controllen=0;
564 iov.iov_base=ubuf;
565 iov.iov_len=size;
566 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
568 return sock_recvmsg(sock, &msg, size, flags);
573 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
574 * is readable by the user process.
577 static ssize_t sock_write(struct file *file, const char *ubuf,
578 size_t size, loff_t *ppos)
580 struct socket *sock;
581 struct msghdr msg;
582 struct iovec iov;
584 if (ppos != &file->f_pos)
585 return -ESPIPE;
586 if(size==0) /* Match SYS5 behaviour */
587 return 0;
589 sock = socki_lookup(file->f_dentry->d_inode);
591 msg.msg_name=NULL;
592 msg.msg_namelen=0;
593 msg.msg_iov=&iov;
594 msg.msg_iovlen=1;
595 msg.msg_control=NULL;
596 msg.msg_controllen=0;
597 msg.msg_flags=!(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
598 if (sock->type == SOCK_SEQPACKET)
599 msg.msg_flags |= MSG_EOR;
600 iov.iov_base=(void *)ubuf;
601 iov.iov_len=size;
603 return sock_sendmsg(sock, &msg, size);
606 int sock_readv_writev(int type, struct inode * inode, struct file * file,
607 const struct iovec * iov, long count, long size)
609 struct msghdr msg;
610 struct socket *sock;
612 sock = socki_lookup(inode);
614 msg.msg_name = NULL;
615 msg.msg_namelen = 0;
616 msg.msg_control = NULL;
617 msg.msg_controllen = 0;
618 msg.msg_iov = (struct iovec *) iov;
619 msg.msg_iovlen = count;
620 msg.msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
622 /* read() does a VERIFY_WRITE */
623 if (type == VERIFY_WRITE)
624 return sock_recvmsg(sock, &msg, size, msg.msg_flags);
626 if (sock->type == SOCK_SEQPACKET)
627 msg.msg_flags |= MSG_EOR;
629 return sock_sendmsg(sock, &msg, size);
632 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
633 unsigned long count, loff_t *ppos)
635 size_t tot_len = 0;
636 int i;
637 for (i = 0 ; i < count ; i++)
638 tot_len += vector[i].iov_len;
639 return sock_readv_writev(VERIFY_WRITE, file->f_dentry->d_inode,
640 file, vector, count, tot_len);
643 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
644 unsigned long count, loff_t *ppos)
646 size_t tot_len = 0;
647 int i;
648 for (i = 0 ; i < count ; i++)
649 tot_len += vector[i].iov_len;
650 return sock_readv_writev(VERIFY_READ, file->f_dentry->d_inode,
651 file, vector, count, tot_len);
655 * With an ioctl arg may well be a user mode pointer, but we don't know what to do
656 * with it - that's up to the protocol still.
659 int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
660 unsigned long arg)
662 struct socket *sock;
663 int err;
665 unlock_kernel();
666 sock = socki_lookup(inode);
667 err = sock->ops->ioctl(sock, cmd, arg);
668 lock_kernel();
670 return err;
674 /* No kernel lock held - perfect */
675 static unsigned int sock_poll(struct file *file, poll_table * wait)
677 struct socket *sock;
680 * We can't return errors to poll, so it's either yes or no.
682 sock = socki_lookup(file->f_dentry->d_inode);
683 return sock->ops->poll(file, sock, wait);
686 static int sock_mmap(struct file * file, struct vm_area_struct * vma)
688 struct socket *sock = socki_lookup(file->f_dentry->d_inode);
690 return sock->ops->mmap(file, sock, vma);
693 int sock_close(struct inode *inode, struct file *filp)
696 * It was possible the inode is NULL we were
697 * closing an unfinished socket.
700 if (!inode)
702 printk(KERN_DEBUG "sock_close: NULL inode\n");
703 return 0;
705 unlock_kernel();
706 sock_fasync(-1, filp, 0);
707 sock_release(socki_lookup(inode));
708 lock_kernel();
709 return 0;
713 * Update the socket async list
715 * Fasync_list locking strategy.
717 * 1. fasync_list is modified only under process context socket lock
718 * i.e. under semaphore.
719 * 2. fasync_list is used under read_lock(&sk->callback_lock)
720 * or under socket lock.
721 * 3. fasync_list can be used from softirq context, so that
722 * modification under socket lock have to be enhanced with
723 * write_lock_bh(&sk->callback_lock).
724 * --ANK (990710)
727 static int sock_fasync(int fd, struct file *filp, int on)
729 struct fasync_struct *fa, *fna=NULL, **prev;
730 struct socket *sock;
731 struct sock *sk;
733 if (on)
735 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
736 if(fna==NULL)
737 return -ENOMEM;
741 sock = socki_lookup(filp->f_dentry->d_inode);
743 if ((sk=sock->sk) == NULL)
744 return -EINVAL;
746 lock_sock(sk);
748 prev=&(sock->fasync_list);
750 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
751 if (fa->fa_file==filp)
752 break;
754 if(on)
756 if(fa!=NULL)
758 write_lock_bh(&sk->callback_lock);
759 fa->fa_fd=fd;
760 write_unlock_bh(&sk->callback_lock);
762 kfree_s(fna,sizeof(struct fasync_struct));
763 goto out;
765 fna->fa_file=filp;
766 fna->fa_fd=fd;
767 fna->magic=FASYNC_MAGIC;
768 fna->fa_next=sock->fasync_list;
769 write_lock_bh(&sk->callback_lock);
770 sock->fasync_list=fna;
771 write_unlock_bh(&sk->callback_lock);
773 else
775 if (fa!=NULL)
777 write_lock_bh(&sk->callback_lock);
778 *prev=fa->fa_next;
779 write_unlock_bh(&sk->callback_lock);
780 kfree_s(fa,sizeof(struct fasync_struct));
784 out:
785 release_sock(sock->sk);
786 return 0;
789 /* This function may be called only under socket lock or callback_lock */
791 int sock_wake_async(struct socket *sock, int how, int band)
793 if (!sock || !sock->fasync_list)
794 return -1;
795 switch (how)
797 case 1:
799 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
800 break;
801 goto call_kill;
802 case 2:
803 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
804 break;
805 /* fall through */
806 case 0:
807 call_kill:
808 __kill_fasync(sock->fasync_list, SIGIO, band);
809 break;
810 case 3:
811 __kill_fasync(sock->fasync_list, SIGURG, band);
813 return 0;
817 int sock_create(int family, int type, int protocol, struct socket **res)
819 int i;
820 struct socket *sock;
823 * Check protocol is in range
825 if(family<0 || family>=NPROTO)
826 return -EINVAL;
828 /* Compatibility.
830 This uglymoron is moved from INET layer to here to avoid
831 deadlock in module load.
833 if (family == PF_INET && type == SOCK_PACKET) {
834 static int warned;
835 if (!warned) {
836 warned = 1;
837 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
839 family = PF_PACKET;
842 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
843 /* Attempt to load a protocol module if the find failed.
845 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
846 * requested real, full-featured networking support upon configuration.
847 * Otherwise module support will break!
849 if (net_families[family]==NULL)
851 char module_name[30];
852 sprintf(module_name,"net-pf-%d",family);
853 request_module(module_name);
855 #endif
857 net_family_read_lock();
858 if (net_families[family] == NULL) {
859 i = -EINVAL;
860 goto out;
864 * Allocate the socket and allow the family to set things up. if
865 * the protocol is 0, the family is instructed to select an appropriate
866 * default.
869 if (!(sock = sock_alloc()))
871 printk(KERN_WARNING "socket: no more sockets\n");
872 i = -ENFILE; /* Not exactly a match, but its the
873 closest posix thing */
874 goto out;
877 sock->type = type;
879 if ((i = net_families[family]->create(sock, protocol)) < 0)
881 sock_release(sock);
882 goto out;
885 *res = sock;
887 out:
888 net_family_read_unlock();
889 return i;
892 asmlinkage long sys_socket(int family, int type, int protocol)
894 int retval;
895 struct socket *sock;
897 retval = sock_create(family, type, protocol, &sock);
898 if (retval < 0)
899 goto out;
901 retval = sock_map_fd(sock);
902 if (retval < 0)
903 goto out_release;
905 out:
906 /* It may be already another descriptor 8) Not kernel problem. */
907 return retval;
909 out_release:
910 sock_release(sock);
911 return retval;
915 * Create a pair of connected sockets.
918 asmlinkage long sys_socketpair(int family, int type, int protocol, int usockvec[2])
920 struct socket *sock1, *sock2;
921 int fd1, fd2, err;
924 * Obtain the first socket and check if the underlying protocol
925 * supports the socketpair call.
928 err = sock_create(family, type, protocol, &sock1);
929 if (err < 0)
930 goto out;
932 err = sock_create(family, type, protocol, &sock2);
933 if (err < 0)
934 goto out_release_1;
936 err = sock1->ops->socketpair(sock1, sock2);
937 if (err < 0)
938 goto out_release_both;
940 fd1 = fd2 = -1;
942 err = sock_map_fd(sock1);
943 if (err < 0)
944 goto out_release_both;
945 fd1 = err;
947 err = sock_map_fd(sock2);
948 if (err < 0)
949 goto out_close_1;
950 fd2 = err;
952 /* fd1 and fd2 may be already another descriptors.
953 * Not kernel problem.
956 err = put_user(fd1, &usockvec[0]);
957 if (!err)
958 err = put_user(fd2, &usockvec[1]);
959 if (!err)
960 return 0;
962 sys_close(fd2);
963 sys_close(fd1);
964 return err;
966 out_close_1:
967 sock_release(sock2);
968 sys_close(fd1);
969 return err;
971 out_release_both:
972 sock_release(sock2);
973 out_release_1:
974 sock_release(sock1);
975 out:
976 return err;
981 * Bind a name to a socket. Nothing much to do here since it's
982 * the protocol's responsibility to handle the local address.
984 * We move the socket address to kernel space before we call
985 * the protocol layer (having also checked the address is ok).
988 asmlinkage long sys_bind(int fd, struct sockaddr *umyaddr, int addrlen)
990 struct socket *sock;
991 char address[MAX_SOCK_ADDR];
992 int err;
994 if((sock = sockfd_lookup(fd,&err))!=NULL)
996 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0)
997 err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
998 sockfd_put(sock);
1000 return err;
1005 * Perform a listen. Basically, we allow the protocol to do anything
1006 * necessary for a listen, and if that works, we mark the socket as
1007 * ready for listening.
1010 asmlinkage long sys_listen(int fd, int backlog)
1012 struct socket *sock;
1013 int err;
1015 if ((sock = sockfd_lookup(fd, &err)) != NULL) {
1016 if ((unsigned) backlog > SOMAXCONN)
1017 backlog = SOMAXCONN;
1018 err=sock->ops->listen(sock, backlog);
1019 sockfd_put(sock);
1021 return err;
1026 * For accept, we attempt to create a new socket, set up the link
1027 * with the client, wake up the client, then return the new
1028 * connected fd. We collect the address of the connector in kernel
1029 * space and move it to user at the very end. This is unclean because
1030 * we open the socket then return an error.
1032 * 1003.1g adds the ability to recvmsg() to query connection pending
1033 * status to recvmsg. We need to add that support in a way thats
1034 * clean when we restucture accept also.
1037 asmlinkage long sys_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
1039 struct socket *sock, *newsock;
1040 int err, len;
1041 char address[MAX_SOCK_ADDR];
1043 sock = sockfd_lookup(fd, &err);
1044 if (!sock)
1045 goto out;
1047 err = -EMFILE;
1048 if (!(newsock = sock_alloc()))
1049 goto out_put;
1051 newsock->type = sock->type;
1052 newsock->ops = sock->ops;
1054 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1055 if (err < 0)
1056 goto out_release;
1058 if (upeer_sockaddr) {
1059 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1060 err = -ECONNABORTED;
1061 goto out_release;
1063 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1064 if (err < 0)
1065 goto out_release;
1068 /* File flags are not inherited via accept() unlike another OSes. */
1070 if ((err = sock_map_fd(newsock)) < 0)
1071 goto out_release;
1073 out_put:
1074 sockfd_put(sock);
1075 out:
1076 return err;
1078 out_release:
1079 sock_release(newsock);
1080 goto out_put;
1085 * Attempt to connect to a socket with the server address. The address
1086 * is in user space so we verify it is OK and move it to kernel space.
1088 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1089 * break bindings
1091 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1092 * other SEQPACKET protocols that take time to connect() as it doesn't
1093 * include the -EINPROGRESS status for such sockets.
1096 asmlinkage long sys_connect(int fd, struct sockaddr *uservaddr, int addrlen)
1098 struct socket *sock;
1099 char address[MAX_SOCK_ADDR];
1100 int err;
1102 sock = sockfd_lookup(fd, &err);
1103 if (!sock)
1104 goto out;
1105 err = move_addr_to_kernel(uservaddr, addrlen, address);
1106 if (err < 0)
1107 goto out_put;
1108 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1109 sock->file->f_flags);
1110 out_put:
1111 sockfd_put(sock);
1112 out:
1113 return err;
1117 * Get the local address ('name') of a socket object. Move the obtained
1118 * name to user space.
1121 asmlinkage long sys_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
1123 struct socket *sock;
1124 char address[MAX_SOCK_ADDR];
1125 int len, err;
1127 sock = sockfd_lookup(fd, &err);
1128 if (!sock)
1129 goto out;
1130 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1131 if (err)
1132 goto out_put;
1133 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1135 out_put:
1136 sockfd_put(sock);
1137 out:
1138 return err;
1142 * Get the remote address ('name') of a socket object. Move the obtained
1143 * name to user space.
1146 asmlinkage long sys_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
1148 struct socket *sock;
1149 char address[MAX_SOCK_ADDR];
1150 int len, err;
1152 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1154 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1155 if (!err)
1156 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1157 sockfd_put(sock);
1159 return err;
1163 * Send a datagram to a given address. We move the address into kernel
1164 * space and check the user space data area is readable before invoking
1165 * the protocol.
1168 asmlinkage long sys_sendto(int fd, void * buff, size_t len, unsigned flags,
1169 struct sockaddr *addr, int addr_len)
1171 struct socket *sock;
1172 char address[MAX_SOCK_ADDR];
1173 int err;
1174 struct msghdr msg;
1175 struct iovec iov;
1177 sock = sockfd_lookup(fd, &err);
1178 if (!sock)
1179 goto out;
1180 iov.iov_base=buff;
1181 iov.iov_len=len;
1182 msg.msg_name=NULL;
1183 msg.msg_iov=&iov;
1184 msg.msg_iovlen=1;
1185 msg.msg_control=NULL;
1186 msg.msg_controllen=0;
1187 msg.msg_namelen=addr_len;
1188 if(addr)
1190 err = move_addr_to_kernel(addr, addr_len, address);
1191 if (err < 0)
1192 goto out_put;
1193 msg.msg_name=address;
1195 if (sock->file->f_flags & O_NONBLOCK)
1196 flags |= MSG_DONTWAIT;
1197 msg.msg_flags = flags;
1198 err = sock_sendmsg(sock, &msg, len);
1200 out_put:
1201 sockfd_put(sock);
1202 out:
1203 return err;
1207 * Send a datagram down a socket.
1210 asmlinkage long sys_send(int fd, void * buff, size_t len, unsigned flags)
1212 return sys_sendto(fd, buff, len, flags, NULL, 0);
1216 * Receive a frame from the socket and optionally record the address of the
1217 * sender. We verify the buffers are writable and if needed move the
1218 * sender address from kernel to user space.
1221 asmlinkage long sys_recvfrom(int fd, void * ubuf, size_t size, unsigned flags,
1222 struct sockaddr *addr, int *addr_len)
1224 struct socket *sock;
1225 struct iovec iov;
1226 struct msghdr msg;
1227 char address[MAX_SOCK_ADDR];
1228 int err,err2;
1230 sock = sockfd_lookup(fd, &err);
1231 if (!sock)
1232 goto out;
1234 msg.msg_control=NULL;
1235 msg.msg_controllen=0;
1236 msg.msg_iovlen=1;
1237 msg.msg_iov=&iov;
1238 iov.iov_len=size;
1239 iov.iov_base=ubuf;
1240 msg.msg_name=address;
1241 msg.msg_namelen=MAX_SOCK_ADDR;
1242 if (sock->file->f_flags & O_NONBLOCK)
1243 flags |= MSG_DONTWAIT;
1244 err=sock_recvmsg(sock, &msg, size, flags);
1246 if(err >= 0 && addr != NULL && msg.msg_namelen)
1248 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1249 if(err2<0)
1250 err=err2;
1252 sockfd_put(sock);
1253 out:
1254 return err;
1258 * Receive a datagram from a socket.
1261 asmlinkage long sys_recv(int fd, void * ubuf, size_t size, unsigned flags)
1263 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1267 * Set a socket option. Because we don't know the option lengths we have
1268 * to pass the user mode parameter for the protocols to sort out.
1271 asmlinkage long sys_setsockopt(int fd, int level, int optname, char *optval, int optlen)
1273 int err;
1274 struct socket *sock;
1276 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1278 if (level == SOL_SOCKET)
1279 err=sock_setsockopt(sock,level,optname,optval,optlen);
1280 else
1281 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1282 sockfd_put(sock);
1284 return err;
1288 * Get a socket option. Because we don't know the option lengths we have
1289 * to pass a user mode parameter for the protocols to sort out.
1292 asmlinkage long sys_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
1294 int err;
1295 struct socket *sock;
1297 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1299 if (level == SOL_SOCKET)
1300 err=sock_getsockopt(sock,level,optname,optval,optlen);
1301 else
1302 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1303 sockfd_put(sock);
1305 return err;
1310 * Shutdown a socket.
1313 asmlinkage long sys_shutdown(int fd, int how)
1315 int err;
1316 struct socket *sock;
1318 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1320 err=sock->ops->shutdown(sock, how);
1321 sockfd_put(sock);
1323 return err;
1327 * BSD sendmsg interface
1330 asmlinkage long sys_sendmsg(int fd, struct msghdr *msg, unsigned flags)
1332 struct socket *sock;
1333 char address[MAX_SOCK_ADDR];
1334 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1335 unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
1336 unsigned char *ctl_buf = ctl;
1337 struct msghdr msg_sys;
1338 int err, ctl_len, iov_size, total_len;
1340 err = -EFAULT;
1341 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1342 goto out;
1344 sock = sockfd_lookup(fd, &err);
1345 if (!sock)
1346 goto out;
1348 /* do not move before msg_sys is valid */
1349 err = -EINVAL;
1350 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1351 goto out_put;
1353 /* Check whether to allocate the iovec area*/
1354 err = -ENOMEM;
1355 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1356 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1357 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1358 if (!iov)
1359 goto out_put;
1362 /* This will also move the address data into kernel space */
1363 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1364 if (err < 0)
1365 goto out_freeiov;
1366 total_len = err;
1368 err = -ENOBUFS;
1370 if (msg_sys.msg_controllen > INT_MAX)
1371 goto out_freeiov;
1372 ctl_len = msg_sys.msg_controllen;
1373 if (ctl_len)
1375 if (ctl_len > sizeof(ctl))
1377 err = -ENOBUFS;
1378 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1379 if (ctl_buf == NULL)
1380 goto out_freeiov;
1382 err = -EFAULT;
1383 if (copy_from_user(ctl_buf, msg_sys.msg_control, ctl_len))
1384 goto out_freectl;
1385 msg_sys.msg_control = ctl_buf;
1387 msg_sys.msg_flags = flags;
1389 if (sock->file->f_flags & O_NONBLOCK)
1390 msg_sys.msg_flags |= MSG_DONTWAIT;
1391 err = sock_sendmsg(sock, &msg_sys, total_len);
1393 out_freectl:
1394 if (ctl_buf != ctl)
1395 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1396 out_freeiov:
1397 if (iov != iovstack)
1398 sock_kfree_s(sock->sk, iov, iov_size);
1399 out_put:
1400 sockfd_put(sock);
1401 out:
1402 return err;
1406 * BSD recvmsg interface
1409 asmlinkage long sys_recvmsg(int fd, struct msghdr *msg, unsigned int flags)
1411 struct socket *sock;
1412 struct iovec iovstack[UIO_FASTIOV];
1413 struct iovec *iov=iovstack;
1414 struct msghdr msg_sys;
1415 unsigned long cmsg_ptr;
1416 int err, iov_size, total_len, len;
1418 /* kernel mode address */
1419 char addr[MAX_SOCK_ADDR];
1421 /* user mode address pointers */
1422 struct sockaddr *uaddr;
1423 int *uaddr_len;
1425 err=-EFAULT;
1426 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1427 goto out;
1429 sock = sockfd_lookup(fd, &err);
1430 if (!sock)
1431 goto out;
1433 err = -EINVAL;
1434 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1435 goto out_put;
1437 /* Check whether to allocate the iovec area*/
1438 err = -ENOMEM;
1439 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1440 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1441 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1442 if (!iov)
1443 goto out_put;
1447 * Save the user-mode address (verify_iovec will change the
1448 * kernel msghdr to use the kernel address space)
1451 uaddr = msg_sys.msg_name;
1452 uaddr_len = &msg->msg_namelen;
1453 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1454 if (err < 0)
1455 goto out_freeiov;
1456 total_len=err;
1458 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1459 msg_sys.msg_flags = 0;
1461 if (sock->file->f_flags & O_NONBLOCK)
1462 flags |= MSG_DONTWAIT;
1463 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1464 if (err < 0)
1465 goto out_freeiov;
1466 len = err;
1468 if (uaddr != NULL && msg_sys.msg_namelen) {
1469 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1470 if (err < 0)
1471 goto out_freeiov;
1473 err = __put_user(msg_sys.msg_flags, &msg->msg_flags);
1474 if (err)
1475 goto out_freeiov;
1476 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1477 &msg->msg_controllen);
1478 if (err)
1479 goto out_freeiov;
1480 err = len;
1482 out_freeiov:
1483 if (iov != iovstack)
1484 sock_kfree_s(sock->sk, iov, iov_size);
1485 out_put:
1486 sockfd_put(sock);
1487 out:
1488 return err;
1493 * Perform a file control on a socket file descriptor.
1495 * Doesn't aquire a fd lock, because no network fcntl
1496 * function sleeps currently.
1499 int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
1501 struct socket *sock;
1503 sock = socki_lookup (filp->f_dentry->d_inode);
1504 if (sock && sock->ops)
1505 return sock_no_fcntl(sock, cmd, arg);
1506 return(-EINVAL);
1509 /* Argument list sizes for sys_socketcall */
1510 #define AL(x) ((x) * sizeof(unsigned long))
1511 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1512 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1513 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1514 #undef AL
1517 * System call vectors.
1519 * Argument checking cleaned up. Saved 20% in size.
1520 * This function doesn't need to set the kernel lock because
1521 * it is set by the callees.
1524 asmlinkage long sys_socketcall(int call, unsigned long *args)
1526 unsigned long a[6];
1527 unsigned long a0,a1;
1528 int err;
1530 if(call<1||call>SYS_RECVMSG)
1531 return -EINVAL;
1533 /* copy_from_user should be SMP safe. */
1534 if (copy_from_user(a, args, nargs[call]))
1535 return -EFAULT;
1537 a0=a[0];
1538 a1=a[1];
1540 switch(call)
1542 case SYS_SOCKET:
1543 err = sys_socket(a0,a1,a[2]);
1544 break;
1545 case SYS_BIND:
1546 err = sys_bind(a0,(struct sockaddr *)a1, a[2]);
1547 break;
1548 case SYS_CONNECT:
1549 err = sys_connect(a0, (struct sockaddr *)a1, a[2]);
1550 break;
1551 case SYS_LISTEN:
1552 err = sys_listen(a0,a1);
1553 break;
1554 case SYS_ACCEPT:
1555 err = sys_accept(a0,(struct sockaddr *)a1, (int *)a[2]);
1556 break;
1557 case SYS_GETSOCKNAME:
1558 err = sys_getsockname(a0,(struct sockaddr *)a1, (int *)a[2]);
1559 break;
1560 case SYS_GETPEERNAME:
1561 err = sys_getpeername(a0, (struct sockaddr *)a1, (int *)a[2]);
1562 break;
1563 case SYS_SOCKETPAIR:
1564 err = sys_socketpair(a0,a1, a[2], (int *)a[3]);
1565 break;
1566 case SYS_SEND:
1567 err = sys_send(a0, (void *)a1, a[2], a[3]);
1568 break;
1569 case SYS_SENDTO:
1570 err = sys_sendto(a0,(void *)a1, a[2], a[3],
1571 (struct sockaddr *)a[4], a[5]);
1572 break;
1573 case SYS_RECV:
1574 err = sys_recv(a0, (void *)a1, a[2], a[3]);
1575 break;
1576 case SYS_RECVFROM:
1577 err = sys_recvfrom(a0, (void *)a1, a[2], a[3],
1578 (struct sockaddr *)a[4], (int *)a[5]);
1579 break;
1580 case SYS_SHUTDOWN:
1581 err = sys_shutdown(a0,a1);
1582 break;
1583 case SYS_SETSOCKOPT:
1584 err = sys_setsockopt(a0, a1, a[2], (char *)a[3], a[4]);
1585 break;
1586 case SYS_GETSOCKOPT:
1587 err = sys_getsockopt(a0, a1, a[2], (char *)a[3], (int *)a[4]);
1588 break;
1589 case SYS_SENDMSG:
1590 err = sys_sendmsg(a0, (struct msghdr *) a1, a[2]);
1591 break;
1592 case SYS_RECVMSG:
1593 err = sys_recvmsg(a0, (struct msghdr *) a1, a[2]);
1594 break;
1595 default:
1596 err = -EINVAL;
1597 break;
1599 return err;
1603 * This function is called by a protocol handler that wants to
1604 * advertise its address family, and have it linked into the
1605 * SOCKET module.
1608 int sock_register(struct net_proto_family *ops)
1610 int err;
1612 if (ops->family >= NPROTO) {
1613 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
1614 return -ENOBUFS;
1616 net_family_write_lock();
1617 err = -EEXIST;
1618 if (net_families[ops->family] == NULL) {
1619 net_families[ops->family]=ops;
1620 err = 0;
1622 net_family_write_unlock();
1623 return err;
1627 * This function is called by a protocol handler that wants to
1628 * remove its address family, and have it unlinked from the
1629 * SOCKET module.
1632 int sock_unregister(int family)
1634 if (family < 0 || family >= NPROTO)
1635 return -1;
1637 net_family_write_lock();
1638 net_families[family]=NULL;
1639 net_family_write_unlock();
1640 return 0;
1643 void __init proto_init(void)
1645 extern struct net_proto protocols[]; /* Network protocols */
1646 struct net_proto *pro;
1648 /* Kick all configured protocols. */
1649 pro = protocols;
1650 while (pro->name != NULL)
1652 (*pro->init_func)(pro);
1653 pro++;
1655 /* We're all done... */
1658 extern void sk_init(void);
1660 #ifdef CONFIG_BRIDGE
1661 extern int br_init(void);
1662 #endif
1664 #ifdef CONFIG_WAN_ROUTER
1665 extern void wanrouter_init(void);
1666 #endif
1668 void __init sock_init(void)
1670 int i;
1672 printk(KERN_INFO "Linux NET4.0 for Linux 2.3\n");
1673 printk(KERN_INFO "Based upon Swansea University Computer Society NET3.039\n");
1676 * Initialize all address (protocol) families.
1679 for (i = 0; i < NPROTO; i++)
1680 net_families[i] = NULL;
1683 * Initialize sock SLAB cache.
1686 sk_init();
1688 #ifdef SLAB_SKB
1690 * Initialize skbuff SLAB cache
1692 skb_init();
1693 #endif
1696 * Ethernet bridge layer.
1699 #ifdef CONFIG_BRIDGE
1700 br_init();
1701 #endif
1704 * Wan router layer.
1707 #ifdef CONFIG_WAN_ROUTER
1708 wanrouter_init();
1709 #endif
1712 * Initialize the protocols module.
1715 proto_init();
1718 * The netlink device handler may be needed early.
1721 #ifdef CONFIG_RTNETLINK
1722 rtnetlink_init();
1723 #endif
1724 #ifdef CONFIG_NETLINK_DEV
1725 init_netlink();
1726 #endif
1727 #ifdef CONFIG_NETFILTER
1728 netfilter_init();
1729 #endif
1730 register_filesystem(&sock_fs_type);
1731 sock_mnt = kern_mount(&sock_fs_type);
1734 int socket_get_info(char *buffer, char **start, off_t offset, int length)
1736 int len, cpu;
1737 int counter = 0;
1739 for (cpu=0; cpu<smp_num_cpus; cpu++)
1740 counter += sockets_in_use[cpu_logical_map(cpu)].counter;
1742 /* It can be negative, by the way. 8) */
1743 if (counter < 0)
1744 counter = 0;
1746 len = sprintf(buffer, "sockets: used %d\n", counter);
1747 if (offset >= len)
1749 *start = buffer;
1750 return 0;
1752 *start = buffer + offset;
1753 len -= offset;
1754 if (len > length)
1755 len = length;
1756 if (len < 0)
1757 len = 0;
1758 return len;