2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Generic socket support routines. Memory allocators, socket lock/release
7 * handler for protocols to use and generic option handler.
10 * Version: $Id: sock.c,v 1.1.1.1 2005/04/11 02:51:12 jack Exp $
12 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
13 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
14 * Florian La Roche, <flla@stud.uni-sb.de>
15 * Alan Cox, <A.Cox@swansea.ac.uk>
18 * Alan Cox : Numerous verify_area() problems
19 * Alan Cox : Connecting on a connecting socket
20 * now returns an error for tcp.
21 * Alan Cox : sock->protocol is set correctly.
22 * and is not sometimes left as 0.
23 * Alan Cox : connect handles icmp errors on a
24 * connect properly. Unfortunately there
25 * is a restart syscall nasty there. I
26 * can't match BSD without hacking the C
27 * library. Ideas urgently sought!
28 * Alan Cox : Disallow bind() to addresses that are
29 * not ours - especially broadcast ones!!
30 * Alan Cox : Socket 1024 _IS_ ok for users. (fencepost)
31 * Alan Cox : sock_wfree/sock_rfree don't destroy sockets,
32 * instead they leave that for the DESTROY timer.
33 * Alan Cox : Clean up error flag in accept
34 * Alan Cox : TCP ack handling is buggy, the DESTROY timer
35 * was buggy. Put a remove_sock() in the handler
36 * for memory when we hit 0. Also altered the timer
37 * code. The ACK stuff can wait and needs major
39 * Alan Cox : Fixed TCP ack bug, removed remove sock
40 * and fixed timer/inet_bh race.
41 * Alan Cox : Added zapped flag for TCP
42 * Alan Cox : Move kfree_skb into skbuff.c and tidied up surplus code
43 * Alan Cox : for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
44 * Alan Cox : kfree_s calls now are kfree_skbmem so we can track skb resources
45 * Alan Cox : Supports socket option broadcast now as does udp. Packet and raw need fixing.
46 * Alan Cox : Added RCVBUF,SNDBUF size setting. It suddenly occurred to me how easy it was so...
47 * Rick Sladkey : Relaxed UDP rules for matching packets.
48 * C.E.Hawkins : IFF_PROMISC/SIOCGHWADDR support
49 * Pauline Middelink : identd support
50 * Alan Cox : Fixed connect() taking signals I think.
51 * Alan Cox : SO_LINGER supported
52 * Alan Cox : Error reporting fixes
53 * Anonymous : inet_create tidied up (sk->reuse setting)
54 * Alan Cox : inet sockets don't set sk->type!
55 * Alan Cox : Split socket option code
56 * Alan Cox : Callbacks
57 * Alan Cox : Nagle flag for Charles & Johannes stuff
58 * Alex : Removed restriction on inet fioctl
59 * Alan Cox : Splitting INET from NET core
60 * Alan Cox : Fixed bogus SO_TYPE handling in getsockopt()
61 * Adam Caldwell : Missing return in SO_DONTROUTE/SO_DEBUG code
62 * Alan Cox : Split IP from generic code
63 * Alan Cox : New kfree_skbmem()
64 * Alan Cox : Make SO_DEBUG superuser only.
65 * Alan Cox : Allow anyone to clear SO_DEBUG
67 * Alan Cox : Added optimistic memory grabbing for AF_UNIX throughput.
68 * Alan Cox : Allocator for a socket is settable.
69 * Alan Cox : SO_ERROR includes soft errors.
70 * Alan Cox : Allow NULL arguments on some SO_ opts
71 * Alan Cox : Generic socket allocation to make hooks
72 * easier (suggested by Craig Metz).
73 * Michael Pall : SO_ERROR returns positive errno again
74 * Steve Whitehouse: Added default destructor to free
75 * protocol private data.
76 * Steve Whitehouse: Added various other default routines
77 * common to several socket families.
78 * Chris Evans : Call suser() check last on F_SETOWN
79 * Jay Schulist : Added SO_ATTACH_FILTER and SO_DETACH_FILTER.
80 * Andi Kleen : Add sock_kmalloc()/sock_kfree_s()
81 * Andi Kleen : Fix write_space callback
82 * Chris Evans : Security fixes - signedness again
83 * Arnaldo C. Melo : cleanups, use skb_queue_purge
88 * This program is free software; you can redistribute it and/or
89 * modify it under the terms of the GNU General Public License
90 * as published by the Free Software Foundation; either version
91 * 2 of the License, or (at your option) any later version.
94 #include <linux/config.h>
95 #include <linux/errno.h>
96 #include <linux/types.h>
97 #include <linux/socket.h>
99 #include <linux/kernel.h>
100 #include <linux/major.h>
101 #include <linux/sched.h>
102 #include <linux/timer.h>
103 #include <linux/string.h>
104 #include <linux/sockios.h>
105 #include <linux/net.h>
106 #include <linux/fcntl.h>
107 #include <linux/mm.h>
108 #include <linux/slab.h>
109 #include <linux/interrupt.h>
110 #include <linux/poll.h>
111 #include <linux/tcp.h>
112 #include <linux/init.h>
114 #include <asm/uaccess.h>
115 #include <asm/system.h>
117 #include <linux/netdevice.h>
118 #include <net/protocol.h>
119 #include <linux/skbuff.h>
120 #include <net/sock.h>
121 #include <linux/ipsec.h>
124 #include <linux/filter.h>
131 /* Run time adjustable parameters. */
132 __u32 sysctl_wmem_max = SK_WMEM_MAX;
133 __u32 sysctl_rmem_max = SK_RMEM_MAX;
134 __u32 sysctl_wmem_default = SK_WMEM_MAX;
135 __u32 sysctl_rmem_default = SK_RMEM_MAX;
137 /* Maximal space eaten by iovec or ancilliary data plus some space */
138 int sysctl_optmem_max = sizeof(unsigned long)*(2*UIO_MAXIOV + 512);
140 static int sock_set_timeout(long *timeo_p, char *optval, int optlen)
144 if (optlen < sizeof(tv))
146 if (copy_from_user(&tv, optval, sizeof(tv)))
149 *timeo_p = MAX_SCHEDULE_TIMEOUT;
150 if (tv.tv_sec == 0 && tv.tv_usec == 0)
152 if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
153 *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
158 * This is meant for all protocols to use and covers goings on
159 * at the socket level. Everything here is generic.
162 int sock_setsockopt(struct socket *sock, int level, int optname,
163 char *optval, int optlen)
165 struct sock *sk=sock->sk;
167 struct sk_filter *filter;
175 * Options without arguments
178 #ifdef SO_DONTLINGER /* Compatibility item... */
187 if(optlen<sizeof(int))
190 if (get_user(val, (int *)optval))
200 if(val && !capable(CAP_NET_ADMIN))
215 sk->localroute=valbool;
218 sk->broadcast=valbool;
221 /* Don't error on this BSD doesn't and if you think
222 about it this is right. Otherwise apps have to
223 play 'guess the biggest size' games. RCVBUF/SNDBUF
224 are treated in BSD as hints */
226 if (val > sysctl_wmem_max)
227 val = sysctl_wmem_max;
229 sk->userlocks |= SOCK_SNDBUF_LOCK;
230 if ((val * 2) < SOCK_MIN_SNDBUF)
231 sk->sndbuf = SOCK_MIN_SNDBUF;
233 sk->sndbuf = (val * 2);
236 * Wake up sending tasks if we
243 /* Don't error on this BSD doesn't and if you think
244 about it this is right. Otherwise apps have to
245 play 'guess the biggest size' games. RCVBUF/SNDBUF
246 are treated in BSD as hints */
248 if (val > sysctl_rmem_max)
249 val = sysctl_rmem_max;
251 sk->userlocks |= SOCK_RCVBUF_LOCK;
252 /* FIXME: is this lower bound the right one? */
253 if ((val * 2) < SOCK_MIN_RCVBUF)
254 sk->rcvbuf = SOCK_MIN_RCVBUF;
256 sk->rcvbuf = (val * 2);
261 if (sk->protocol == IPPROTO_TCP)
263 tcp_set_keepalive(sk, valbool);
266 sk->keepopen = valbool;
270 sk->urginline = valbool;
274 sk->no_check = valbool;
278 if ((val >= 0 && val <= 6) || capable(CAP_NET_ADMIN))
285 if(optlen<sizeof(ling)) {
286 ret = -EINVAL; /* 1003.1g */
289 if (copy_from_user(&ling,optval,sizeof(ling))) {
293 if(ling.l_onoff==0) {
296 #if (BITS_PER_LONG == 32)
297 if (ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
298 sk->lingertime=MAX_SCHEDULE_TIMEOUT;
301 sk->lingertime=ling.l_linger*HZ;
307 sk->bsdism = valbool;
311 sock->passcred = valbool;
315 sk->rcvtstamp = valbool;
321 sk->rcvlowat = val ? : 1;
325 ret = sock_set_timeout(&sk->rcvtimeo, optval, optlen);
329 ret = sock_set_timeout(&sk->sndtimeo, optval, optlen);
332 #ifdef CONFIG_NETDEVICES
333 case SO_BINDTODEVICE:
335 char devname[IFNAMSIZ];
338 if (!capable(CAP_NET_RAW)) {
343 /* Bind this socket to a particular device like "eth0",
344 * as specified in the passed interface name. If the
345 * name is "" or the option length is zero the socket
350 sk->bound_dev_if = 0;
352 if (optlen > IFNAMSIZ)
354 if (copy_from_user(devname, optval, optlen)) {
359 /* Remove any cached route for this socket. */
362 if (devname[0] == '\0') {
363 sk->bound_dev_if = 0;
365 struct net_device *dev = dev_get_by_name(devname);
370 sk->bound_dev_if = dev->ifindex;
380 case SO_ATTACH_FILTER:
382 if (optlen == sizeof(struct sock_fprog)) {
383 struct sock_fprog fprog;
386 if (copy_from_user(&fprog, optval, sizeof(fprog)))
389 ret = sk_attach_filter(&fprog, sk);
393 case SO_DETACH_FILTER:
394 spin_lock_bh(&sk->lock.slock);
398 spin_unlock_bh(&sk->lock.slock);
399 sk_filter_release(sk, filter);
402 spin_unlock_bh(&sk->lock.slock);
406 /* We implement the SO_SNDLOWAT etc to
407 not be settable (1003.1g 5.3) */
417 int sock_getsockopt(struct socket *sock, int level, int optname,
418 char *optval, int *optlen)
420 struct sock *sk = sock->sk;
429 unsigned int lv=sizeof(int),len;
431 if(get_user(len,optlen))
443 v.val = sk->localroute;
447 v.val= sk->broadcast;
463 v.val = sk->keepopen;
471 v.val = -sock_error(sk);
473 v.val=xchg(&sk->err_soft,0);
477 v.val = sk->urginline;
481 v.val = sk->no_check;
485 v.val = sk->priority;
490 v.ling.l_onoff=sk->linger;
491 v.ling.l_linger=sk->lingertime/HZ;
499 v.val = sk->rcvtstamp;
503 lv=sizeof(struct timeval);
504 if (sk->rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
508 v.tm.tv_sec = sk->rcvtimeo/HZ;
509 v.tm.tv_usec = ((sk->rcvtimeo%HZ)*1000)/HZ;
514 lv=sizeof(struct timeval);
515 if (sk->sndtimeo == MAX_SCHEDULE_TIMEOUT) {
519 v.tm.tv_sec = sk->sndtimeo/HZ;
520 v.tm.tv_usec = ((sk->sndtimeo%HZ)*1000)/HZ;
525 v.val = sk->rcvlowat;
533 v.val = sock->passcred;
537 if (len > sizeof(sk->peercred))
538 len = sizeof(sk->peercred);
539 if (copy_to_user(optval, &sk->peercred, len))
547 if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
551 if(copy_to_user((void*)optval, address, len))
556 /* Dubious BSD thing... Probably nobody even uses it, but
557 * the UNIX standard wants it for whatever reason... -DaveM
560 v.val = (sk->state == TCP_LISTEN);
564 return(-ENOPROTOOPT);
568 if (copy_to_user(optval, &v, len))
571 if (put_user(len, optlen))
576 static kmem_cache_t *sk_cachep;
579 * All socket objects are allocated here. This is for future
583 struct sock *sk_alloc(int family, int priority, int zero_it)
585 struct sock *sk = kmem_cache_alloc(sk_cachep, priority);
588 memset(sk, 0, sizeof(struct sock));
596 void sk_free(struct sock *sk)
599 struct sk_filter *filter;
608 sk_filter_release(sk, filter);
613 if (atomic_read(&sk->omem_alloc))
614 printk(KERN_DEBUG "sk_free: optmem leakage (%d bytes) detected.\n", atomic_read(&sk->omem_alloc));
616 kmem_cache_free(sk_cachep, sk);
619 void __init sk_init(void)
621 sk_cachep = kmem_cache_create("sock", sizeof(struct sock), 0,
622 SLAB_HWCACHE_ALIGN, 0, 0);
624 printk(KERN_CRIT "sk_init: Cannot create sock SLAB cache!");
626 if (num_physpages <= 4096) {
627 sysctl_wmem_max = 32767;
628 sysctl_rmem_max = 32767;
629 sysctl_wmem_default = 32767;
630 sysctl_rmem_default = 32767;
631 } else if (num_physpages >= 131072) {
632 sysctl_wmem_max = 131071;
633 sysctl_rmem_max = 131071;
638 * Simple resource managers for sockets.
643 * Write buffer destructor automatically called from kfree_skb.
645 void sock_wfree(struct sk_buff *skb)
647 struct sock *sk = skb->sk;
649 /* In case it might be waiting for more memory. */
650 atomic_sub(skb->truesize, &sk->wmem_alloc);
651 if (!sk->use_write_queue)
657 * Read buffer destructor automatically called from kfree_skb.
659 void sock_rfree(struct sk_buff *skb)
661 struct sock *sk = skb->sk;
663 atomic_sub(skb->truesize, &sk->rmem_alloc);
667 * Allocate a skb from the socket's send buffer.
669 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, int priority)
671 if (force || atomic_read(&sk->wmem_alloc) < sk->sndbuf) {
672 struct sk_buff * skb = alloc_skb(size, priority);
674 skb_set_owner_w(skb, sk);
682 * Allocate a skb from the socket's receive buffer.
684 struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
686 if (force || atomic_read(&sk->rmem_alloc) < sk->rcvbuf) {
687 struct sk_buff *skb = alloc_skb(size, priority);
689 skb_set_owner_r(skb, sk);
697 * Allocate a memory block from the socket's option memory buffer.
699 void *sock_kmalloc(struct sock *sk, int size, int priority)
701 if ((unsigned)size <= sysctl_optmem_max &&
702 atomic_read(&sk->omem_alloc)+size < sysctl_optmem_max) {
704 /* First do the add, to avoid the race if kmalloc
707 atomic_add(size, &sk->omem_alloc);
708 mem = kmalloc(size, priority);
711 atomic_sub(size, &sk->omem_alloc);
717 * Free an option memory block.
719 void sock_kfree_s(struct sock *sk, void *mem, int size)
722 atomic_sub(size, &sk->omem_alloc);
725 /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
726 I think, these locks should be removed for datagram sockets.
728 static long sock_wait_for_wmem(struct sock * sk, long timeo)
730 DECLARE_WAITQUEUE(wait, current);
732 clear_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags);
733 add_wait_queue(sk->sleep, &wait);
737 if (signal_pending(current))
739 set_bit(SOCK_NOSPACE, &sk->socket->flags);
740 set_current_state(TASK_INTERRUPTIBLE);
741 if (atomic_read(&sk->wmem_alloc) < sk->sndbuf)
743 if (sk->shutdown & SEND_SHUTDOWN)
747 timeo = schedule_timeout(timeo);
749 __set_current_state(TASK_RUNNING);
750 remove_wait_queue(sk->sleep, &wait);
756 * Generic send/receive buffer handlers
759 struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
760 unsigned long data_len, int noblock, int *errcode)
766 timeo = sock_sndtimeo(sk, noblock);
768 err = sock_error(sk);
773 if (sk->shutdown & SEND_SHUTDOWN)
776 if (atomic_read(&sk->wmem_alloc) < sk->sndbuf) {
777 skb = alloc_skb(header_len, sk->allocation);
782 /* No pages, we're done... */
786 npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
787 skb->truesize += data_len;
788 skb_shinfo(skb)->nr_frags = npages;
789 for (i = 0; i < npages; i++) {
793 page = alloc_pages(sk->allocation, 0);
800 frag = &skb_shinfo(skb)->frags[i];
802 frag->page_offset = 0;
803 frag->size = (data_len >= PAGE_SIZE ?
806 data_len -= PAGE_SIZE;
809 /* Full success... */
815 set_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags);
816 set_bit(SOCK_NOSPACE, &sk->socket->flags);
820 if (signal_pending(current))
822 timeo = sock_wait_for_wmem(sk, timeo);
825 skb_set_owner_w(skb, sk);
829 err = sock_intr_errno(timeo);
835 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
836 int noblock, int *errcode)
838 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode);
841 void __lock_sock(struct sock *sk)
843 DECLARE_WAITQUEUE(wait, current);
845 add_wait_queue_exclusive(&sk->lock.wq, &wait);
847 current->state = TASK_UNINTERRUPTIBLE;
848 spin_unlock_bh(&sk->lock.slock);
850 spin_lock_bh(&sk->lock.slock);
854 current->state = TASK_RUNNING;
855 remove_wait_queue(&sk->lock.wq, &wait);
858 void __release_sock(struct sock *sk)
860 struct sk_buff *skb = sk->backlog.head;
863 sk->backlog.head = sk->backlog.tail = NULL;
867 struct sk_buff *next = skb->next;
870 sk->backlog_rcv(sk, skb);
872 } while (skb != NULL);
875 } while((skb = sk->backlog.head) != NULL);
879 * Generic socket manager library. Most simpler socket families
880 * use this to manage their socket lists. At some point we should
881 * hash these. By making this generic we get the lot hashed for free.
883 * It is broken by design. All the protocols using it must be fixed. --ANK
886 rwlock_t net_big_sklist_lock = RW_LOCK_UNLOCKED;
888 void sklist_remove_socket(struct sock **list, struct sock *sk)
892 write_lock_bh(&net_big_sklist_lock);
894 while ((s = *list) != NULL) {
902 write_unlock_bh(&net_big_sklist_lock);
907 void sklist_insert_socket(struct sock **list, struct sock *sk)
909 write_lock_bh(&net_big_sklist_lock);
913 write_unlock_bh(&net_big_sklist_lock);
917 * This is only called from user mode. Thus it protects itself against
918 * interrupt users but doesn't worry about being called during work.
919 * Once it is removed from the queue no interrupt or bottom half will
920 * touch it and we are (fairly 8-) ) safe.
923 void sklist_destroy_socket(struct sock **list, struct sock *sk);
926 * Handler for deferred kills.
929 static void sklist_destroy_timer(unsigned long data)
931 struct sock *sk=(struct sock *)data;
932 sklist_destroy_socket(NULL,sk);
936 * Destroy a socket. We pass NULL for a list if we know the
937 * socket is not on a list.
940 void sklist_destroy_socket(struct sock **list,struct sock *sk)
943 sklist_remove_socket(list, sk);
945 skb_queue_purge(&sk->receive_queue);
947 if(atomic_read(&sk->wmem_alloc) == 0 &&
948 atomic_read(&sk->rmem_alloc) == 0 &&
956 * Someone is using our buffers still.. defer
958 init_timer(&sk->timer);
959 sk->timer.expires=jiffies+SOCK_DESTROY_TIME;
960 sk->timer.function=sklist_destroy_timer;
961 sk->timer.data = (unsigned long)sk;
962 add_timer(&sk->timer);
967 * Set of default routines for initialising struct proto_ops when
968 * the protocol does not support a particular function. In certain
969 * cases where it makes no sense for a protocol to have a "do nothing"
970 * function, some default processing is provided.
973 int sock_no_release(struct socket *sock)
978 int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
983 int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
989 int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
994 int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
999 int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
1005 unsigned int sock_no_poll(struct file * file, struct socket *sock, poll_table *pt)
1010 int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1015 int sock_no_listen(struct socket *sock, int backlog)
1020 int sock_no_shutdown(struct socket *sock, int how)
1025 int sock_no_setsockopt(struct socket *sock, int level, int optname,
1026 char *optval, int optlen)
1031 int sock_no_getsockopt(struct socket *sock, int level, int optname,
1032 char *optval, int *optlen)
1038 * Note: if you add something that sleeps here then change sock_fcntl()
1039 * to do proper fd locking.
1041 int sock_no_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
1043 struct sock *sk = sock->sk;
1049 * This is a little restrictive, but it's the only
1050 * way to make sure that you can't send a sigurg to
1053 if (current->pgrp != -arg &&
1054 current->pid != arg &&
1055 !capable(CAP_KILL)) return(-EPERM);
1065 int sock_no_sendmsg(struct socket *sock, struct msghdr *m, int flags,
1066 struct scm_cookie *scm)
1071 int sock_no_recvmsg(struct socket *sock, struct msghdr *m, int len, int flags,
1072 struct scm_cookie *scm)
1077 int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1079 /* Mirror missing mmap method error code */
1083 ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
1088 mm_segment_t old_fs;
1093 msg.msg_name = NULL;
1094 msg.msg_namelen = 0;
1097 msg.msg_control = NULL;
1098 msg.msg_controllen = 0;
1099 msg.msg_flags = flags;
1101 iov.iov_base = kaddr + offset;
1106 res = sock_sendmsg(sock, &msg, size);
1114 * Default Socket Callbacks
1117 void sock_def_wakeup(struct sock *sk)
1119 read_lock(&sk->callback_lock);
1120 if (sk->sleep && waitqueue_active(sk->sleep))
1121 wake_up_interruptible_all(sk->sleep);
1122 read_unlock(&sk->callback_lock);
1125 void sock_def_error_report(struct sock *sk)
1127 read_lock(&sk->callback_lock);
1128 if (sk->sleep && waitqueue_active(sk->sleep))
1129 wake_up_interruptible(sk->sleep);
1130 sk_wake_async(sk,0,POLL_ERR);
1131 read_unlock(&sk->callback_lock);
1134 void sock_def_readable(struct sock *sk, int len)
1136 read_lock(&sk->callback_lock);
1137 if (sk->sleep && waitqueue_active(sk->sleep))
1138 wake_up_interruptible(sk->sleep);
1139 sk_wake_async(sk,1,POLL_IN);
1140 read_unlock(&sk->callback_lock);
1143 void sock_def_write_space(struct sock *sk)
1145 read_lock(&sk->callback_lock);
1147 /* Do not wake up a writer until he can make "significant"
1150 if((atomic_read(&sk->wmem_alloc) << 1) <= sk->sndbuf) {
1151 if (sk->sleep && waitqueue_active(sk->sleep))
1152 wake_up_interruptible(sk->sleep);
1154 /* Should agree with poll, otherwise some programs break */
1155 if (sock_writeable(sk))
1156 sk_wake_async(sk, 2, POLL_OUT);
1159 read_unlock(&sk->callback_lock);
1162 void sock_def_destruct(struct sock *sk)
1164 if (sk->protinfo.destruct_hook)
1165 kfree(sk->protinfo.destruct_hook);
1168 void sock_init_data(struct socket *sock, struct sock *sk)
1170 skb_queue_head_init(&sk->receive_queue);
1171 skb_queue_head_init(&sk->write_queue);
1172 skb_queue_head_init(&sk->error_queue);
1174 init_timer(&sk->timer);
1176 sk->allocation = GFP_KERNEL;
1177 sk->rcvbuf = sysctl_rmem_default;
1178 sk->sndbuf = sysctl_wmem_default;
1179 sk->state = TCP_CLOSE;
1185 sk->type = sock->type;
1186 sk->sleep = &sock->wait;
1191 sk->dst_lock = RW_LOCK_UNLOCKED;
1192 sk->callback_lock = RW_LOCK_UNLOCKED;
1194 sk->state_change = sock_def_wakeup;
1195 sk->data_ready = sock_def_readable;
1196 sk->write_space = sock_def_write_space;
1197 sk->error_report = sock_def_error_report;
1198 sk->destruct = sock_def_destruct;
1200 sk->peercred.pid = 0;
1201 sk->peercred.uid = -1;
1202 sk->peercred.gid = -1;
1204 sk->rcvtimeo = MAX_SCHEDULE_TIMEOUT;
1205 sk->sndtimeo = MAX_SCHEDULE_TIMEOUT;
1207 atomic_set(&sk->refcnt, 1);