Import 2.3.18pre1
[davej-history.git] / net / core / sock.c
blob2b0018ec93bea6771da5124a357de05fe2bbefc4
1 /*
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.86 1999/09/01 08:11:49 davem 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>
17 * Fixes:
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
38 * TCP layer surgery.
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
66 * (compatibility fix)
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
83 * To Fix:
86 * This program is free software; you can redistribute it and/or
87 * modify it under the terms of the GNU General Public License
88 * as published by the Free Software Foundation; either version
89 * 2 of the License, or (at your option) any later version.
92 #include <linux/config.h>
93 #include <linux/errno.h>
94 #include <linux/types.h>
95 #include <linux/socket.h>
96 #include <linux/in.h>
97 #include <linux/kernel.h>
98 #include <linux/major.h>
99 #include <linux/sched.h>
100 #include <linux/timer.h>
101 #include <linux/string.h>
102 #include <linux/sockios.h>
103 #include <linux/net.h>
104 #include <linux/fcntl.h>
105 #include <linux/mm.h>
106 #include <linux/slab.h>
107 #include <linux/interrupt.h>
108 #include <linux/poll.h>
109 #include <linux/init.h>
111 #include <asm/uaccess.h>
112 #include <asm/system.h>
114 #include <linux/inet.h>
115 #include <linux/netdevice.h>
116 #include <net/ip.h>
117 #include <net/protocol.h>
118 #include <net/arp.h>
119 #include <net/route.h>
120 #include <net/tcp.h>
121 #include <net/udp.h>
122 #include <linux/skbuff.h>
123 #include <net/sock.h>
124 #include <net/raw.h>
125 #include <net/icmp.h>
126 #include <linux/ipsec.h>
128 #ifdef CONFIG_FILTER
129 #include <linux/filter.h>
130 #endif
132 #define min(a,b) ((a)<(b)?(a):(b))
134 /* Run time adjustable parameters. */
135 __u32 sysctl_wmem_max = SK_WMEM_MAX;
136 __u32 sysctl_rmem_max = SK_RMEM_MAX;
137 __u32 sysctl_wmem_default = SK_WMEM_MAX;
138 __u32 sysctl_rmem_default = SK_RMEM_MAX;
140 /* Maximal space eaten by iovec or ancilliary data plus some space */
141 int sysctl_optmem_max = sizeof(unsigned long)*(2*UIO_MAXIOV + 512);
144 * This is meant for all protocols to use and covers goings on
145 * at the socket level. Everything here is generic.
148 int sock_setsockopt(struct socket *sock, int level, int optname,
149 char *optval, int optlen)
151 struct sock *sk=sock->sk;
152 #ifdef CONFIG_FILTER
153 struct sk_filter *filter;
154 #endif
155 int val;
156 int valbool;
157 int err;
158 struct linger ling;
159 int ret = 0;
162 * Options without arguments
165 #ifdef SO_DONTLINGER /* Compatibility item... */
166 switch(optname)
168 case SO_DONTLINGER:
169 sk->linger=0;
170 return 0;
172 #endif
174 if(optlen<sizeof(int))
175 return(-EINVAL);
177 err = get_user(val, (int *)optval);
178 if (err)
179 return err;
181 valbool = val?1:0;
183 lock_sock(sk);
185 switch(optname)
187 case SO_DEBUG:
188 if(val && !capable(CAP_NET_ADMIN))
190 ret = -EACCES;
192 else
193 sk->debug=valbool;
194 break;
195 case SO_REUSEADDR:
196 sk->reuse = valbool;
197 break;
198 case SO_TYPE:
199 case SO_ERROR:
200 ret = -ENOPROTOOPT;
201 break;
202 case SO_DONTROUTE:
203 sk->localroute=valbool;
204 break;
205 case SO_BROADCAST:
206 sk->broadcast=valbool;
207 break;
208 case SO_SNDBUF:
209 /* Don't error on this BSD doesn't and if you think
210 about it this is right. Otherwise apps have to
211 play 'guess the biggest size' games. RCVBUF/SNDBUF
212 are treated in BSD as hints */
214 if (val > sysctl_wmem_max)
215 val = sysctl_wmem_max;
217 sk->sndbuf = max(val*2,2048);
220 * Wake up sending tasks if we
221 * upped the value.
223 sk->write_space(sk);
224 break;
226 case SO_RCVBUF:
227 /* Don't error on this BSD doesn't and if you think
228 about it this is right. Otherwise apps have to
229 play 'guess the biggest size' games. RCVBUF/SNDBUF
230 are treated in BSD as hints */
232 if (val > sysctl_rmem_max)
233 val = sysctl_rmem_max;
235 /* FIXME: is this lower bound the right one? */
236 sk->rcvbuf = max(val*2,256);
237 break;
239 case SO_KEEPALIVE:
240 #ifdef CONFIG_INET
241 if (sk->protocol == IPPROTO_TCP)
243 tcp_set_keepalive(sk, valbool);
245 #endif
246 sk->keepopen = valbool;
247 break;
249 case SO_OOBINLINE:
250 sk->urginline = valbool;
251 break;
253 case SO_NO_CHECK:
254 sk->no_check = valbool;
255 break;
257 case SO_PRIORITY:
258 if ((val >= 0 && val <= 6) || capable(CAP_NET_ADMIN))
259 sk->priority = val;
260 else
261 ret = -EPERM;
262 break;
264 case SO_LINGER:
265 if(optlen<sizeof(ling)) {
266 ret = -EINVAL; /* 1003.1g */
267 break;
269 if (copy_from_user(&ling,optval,sizeof(ling)))
271 ret = -EFAULT;
272 break;
274 if(ling.l_onoff==0)
275 sk->linger=0;
276 else
278 sk->lingertime=ling.l_linger;
279 sk->linger=1;
281 break;
283 case SO_BSDCOMPAT:
284 sk->bsdism = valbool;
285 break;
287 case SO_PASSCRED:
288 sock->passcred = valbool;
289 break;
292 #ifdef CONFIG_NETDEVICES
293 case SO_BINDTODEVICE:
295 char devname[IFNAMSIZ];
297 /* Sorry... */
298 if (!capable(CAP_NET_RAW)) {
299 ret = -EPERM;
300 break;
303 /* Bind this socket to a particular device like "eth0",
304 * as specified in the passed interface name. If the
305 * name is "" or the option length is zero the socket
306 * is not bound.
309 if (!valbool) {
310 sk->bound_dev_if = 0;
311 } else {
312 if (optlen > IFNAMSIZ)
313 optlen = IFNAMSIZ;
314 if (copy_from_user(devname, optval, optlen)) {
315 ret = -EFAULT;
316 break;
319 /* Remove any cached route for this socket. */
320 sk_dst_reset(sk);
322 if (devname[0] == '\0') {
323 sk->bound_dev_if = 0;
324 } else {
325 struct net_device *dev = dev_get_by_name(devname);
326 if (!dev) {
327 ret = -ENODEV;
328 break;
330 sk->bound_dev_if = dev->ifindex;
331 dev_put(dev);
334 break;
336 #endif
339 #ifdef CONFIG_FILTER
340 case SO_ATTACH_FILTER:
341 ret = -EINVAL;
342 if (optlen == sizeof(struct sock_fprog)) {
343 struct sock_fprog fprog;
345 ret = -EFAULT;
346 if (copy_from_user(&fprog, optval, sizeof(fprog)))
347 break;
349 ret = sk_attach_filter(&fprog, sk);
351 break;
353 case SO_DETACH_FILTER:
354 spin_lock_bh(&sk->lock.slock);
355 filter = sk->filter;
356 if (filter) {
357 sk->filter = NULL;
358 spin_unlock_bh(&sk->lock.slock);
359 sk_filter_release(sk, filter);
360 break;
362 spin_unlock_bh(&sk->lock.slock);
363 ret = -ENONET;
364 break;
365 #endif
366 /* We implement the SO_SNDLOWAT etc to
367 not be settable (1003.1g 5.3) */
368 default:
369 ret = -ENOPROTOOPT;
370 break;
372 release_sock(sk);
373 return ret;
377 int sock_getsockopt(struct socket *sock, int level, int optname,
378 char *optval, int *optlen)
380 struct sock *sk = sock->sk;
382 union
384 int val;
385 struct linger ling;
386 struct timeval tm;
387 } v;
389 int lv=sizeof(int),len;
391 if(get_user(len,optlen))
392 return -EFAULT;
394 switch(optname)
396 case SO_DEBUG:
397 v.val = sk->debug;
398 break;
400 case SO_DONTROUTE:
401 v.val = sk->localroute;
402 break;
404 case SO_BROADCAST:
405 v.val= sk->broadcast;
406 break;
408 case SO_SNDBUF:
409 v.val=sk->sndbuf;
410 break;
412 case SO_RCVBUF:
413 v.val =sk->rcvbuf;
414 break;
416 case SO_REUSEADDR:
417 v.val = sk->reuse;
418 break;
420 case SO_KEEPALIVE:
421 v.val = sk->keepopen;
422 break;
424 case SO_TYPE:
425 v.val = sk->type;
426 break;
428 case SO_ERROR:
429 v.val = -sock_error(sk);
430 if(v.val==0)
431 v.val=xchg(&sk->err_soft,0);
432 break;
434 case SO_OOBINLINE:
435 v.val = sk->urginline;
436 break;
438 case SO_NO_CHECK:
439 v.val = sk->no_check;
440 break;
442 case SO_PRIORITY:
443 v.val = sk->priority;
444 break;
446 case SO_LINGER:
447 lv=sizeof(v.ling);
448 v.ling.l_onoff=sk->linger;
449 v.ling.l_linger=sk->lingertime;
450 break;
452 case SO_BSDCOMPAT:
453 v.val = sk->bsdism;
454 break;
456 case SO_RCVTIMEO:
457 case SO_SNDTIMEO:
458 lv=sizeof(struct timeval);
459 v.tm.tv_sec=0;
460 v.tm.tv_usec=0;
461 break;
463 case SO_RCVLOWAT:
464 case SO_SNDLOWAT:
465 v.val=1;
466 break;
468 case SO_PASSCRED:
469 v.val = sock->passcred;
470 break;
472 case SO_PEERCRED:
473 lv=sizeof(sk->peercred);
474 len=min(len, lv);
475 if(copy_to_user((void*)optval, &sk->peercred, len))
476 return -EFAULT;
477 goto lenout;
479 default:
480 return(-ENOPROTOOPT);
482 len=min(len,lv);
483 if(copy_to_user(optval,&v,len))
484 return -EFAULT;
485 lenout:
486 if(put_user(len, optlen))
487 return -EFAULT;
488 return 0;
491 static kmem_cache_t *sk_cachep;
494 * All socket objects are allocated here. This is for future
495 * usage.
498 struct sock *sk_alloc(int family, int priority, int zero_it)
500 struct sock *sk = kmem_cache_alloc(sk_cachep, priority);
502 if(sk && zero_it) {
503 memset(sk, 0, sizeof(struct sock));
504 sk->family = family;
505 sock_lock_init(sk);
508 return sk;
511 void sk_free(struct sock *sk)
513 #ifdef CONFIG_FILTER
514 struct sk_filter *filter;
515 #endif
517 if (sk->destruct)
518 sk->destruct(sk);
520 #ifdef CONFIG_FILTER
521 filter = sk->filter;
522 if (filter) {
523 sk_filter_release(sk, filter);
524 sk->filter = NULL;
526 #endif
528 if (atomic_read(&sk->omem_alloc))
529 printk(KERN_DEBUG "sk_free: optmem leakage (%d bytes) detected.\n", atomic_read(&sk->omem_alloc));
531 kmem_cache_free(sk_cachep, sk);
534 void __init sk_init(void)
536 sk_cachep = kmem_cache_create("sock", sizeof(struct sock), 0,
537 SLAB_HWCACHE_ALIGN, 0, 0);
542 * Simple resource managers for sockets.
547 * Write buffer destructor automatically called from kfree_skb.
549 void sock_wfree(struct sk_buff *skb)
551 struct sock *sk = skb->sk;
553 /* In case it might be waiting for more memory. */
554 atomic_sub(skb->truesize, &sk->wmem_alloc);
555 sk->write_space(sk);
556 sock_put(sk);
560 * Read buffer destructor automatically called from kfree_skb.
562 void sock_rfree(struct sk_buff *skb)
564 struct sock *sk = skb->sk;
566 atomic_sub(skb->truesize, &sk->rmem_alloc);
569 void sock_cfree(struct sk_buff *skb)
571 sock_put(skb->sk);
575 * Allocate a skb from the socket's send buffer.
577 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, int priority)
579 if (force || atomic_read(&sk->wmem_alloc) < sk->sndbuf) {
580 struct sk_buff * skb = alloc_skb(size, priority);
581 if (skb) {
582 skb_set_owner_w(skb, sk);
583 return skb;
586 return NULL;
590 * Allocate a skb from the socket's receive buffer.
592 struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
594 if (force || atomic_read(&sk->rmem_alloc) < sk->rcvbuf) {
595 struct sk_buff *skb = alloc_skb(size, priority);
596 if (skb) {
597 skb_set_owner_r(skb, sk);
598 return skb;
601 return NULL;
605 * Allocate a memory block from the socket's option memory buffer.
607 void *sock_kmalloc(struct sock *sk, int size, int priority)
609 if ((unsigned)size <= sysctl_optmem_max &&
610 atomic_read(&sk->omem_alloc)+size < sysctl_optmem_max) {
611 void *mem;
612 /* First do the add, to avoid the race if kmalloc
613 * might sleep.
615 atomic_add(size, &sk->omem_alloc);
616 mem = kmalloc(size, priority);
617 if (mem)
618 return mem;
619 atomic_sub(size, &sk->omem_alloc);
621 return NULL;
625 * Free an option memory block.
627 void sock_kfree_s(struct sock *sk, void *mem, int size)
629 kfree_s(mem, size);
630 atomic_sub(size, &sk->omem_alloc);
633 /* FIXME: this is insane. We are trying suppose to be controlling how
634 * how much space we have for data bytes, not packet headers.
635 * This really points out that we need a better system for doing the
636 * receive buffer. -- erics
637 * WARNING: This is currently ONLY used in tcp. If you need it else where
638 * this will probably not be what you want. Possibly these two routines
639 * should move over to the ipv4 directory.
641 unsigned long sock_rspace(struct sock *sk)
643 int amt = 0;
645 if (sk != NULL) {
646 /* This used to have some bizarre complications that
647 * to attempt to reserve some amount of space. This doesn't
648 * make sense, since the number returned here does not
649 * actually reflect allocated space, but rather the amount
650 * of space we committed to. We gamble that we won't
651 * run out of memory, and returning a smaller number does
652 * not change the gamble. If we lose the gamble tcp still
653 * works, it may just slow down for retransmissions.
655 amt = sk->rcvbuf - atomic_read(&sk->rmem_alloc);
656 if (amt < 0)
657 amt = 0;
659 return amt;
663 /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
664 I think, these locks should be removed for datagram sockets.
666 static void sock_wait_for_wmem(struct sock * sk)
668 DECLARE_WAITQUEUE(wait, current);
670 sk->socket->flags &= ~SO_NOSPACE;
671 add_wait_queue(sk->sleep, &wait);
672 for (;;) {
673 if (signal_pending(current))
674 break;
675 set_current_state(TASK_INTERRUPTIBLE);
676 if (atomic_read(&sk->wmem_alloc) < sk->sndbuf)
677 break;
678 if (sk->shutdown & SEND_SHUTDOWN)
679 break;
680 if (sk->err)
681 break;
682 schedule();
684 __set_current_state(TASK_RUNNING);
685 remove_wait_queue(sk->sleep, &wait);
690 * Generic send/receive buffer handlers
693 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
694 unsigned long fallback, int noblock, int *errcode)
696 int err;
697 struct sk_buff *skb;
699 while (1) {
700 unsigned long try_size = size;
702 err = sock_error(sk);
703 if (err != 0)
704 goto failure;
707 * We should send SIGPIPE in these cases according to
708 * 1003.1g draft 6.4. If we (the user) did a shutdown()
709 * call however we should not.
711 * Note: This routine isnt just used for datagrams and
712 * anyway some datagram protocols have a notion of
713 * close down.
716 err = -EPIPE;
717 if (sk->shutdown&SEND_SHUTDOWN)
718 goto failure;
720 if (fallback) {
721 /* The buffer get won't block, or use the atomic queue.
722 * It does produce annoying no free page messages still.
724 skb = sock_wmalloc(sk, size, 0, GFP_BUFFER);
725 if (skb)
726 break;
727 try_size = fallback;
729 skb = sock_wmalloc(sk, try_size, 0, sk->allocation);
730 if (skb)
731 break;
734 * This means we have too many buffers for this socket already.
737 sk->socket->flags |= SO_NOSPACE;
738 err = -EAGAIN;
739 if (noblock)
740 goto failure;
741 err = -ERESTARTSYS;
742 if (signal_pending(current))
743 goto failure;
744 sock_wait_for_wmem(sk);
747 return skb;
749 failure:
750 *errcode = err;
751 return NULL;
754 void __lock_sock(struct sock *sk)
756 DECLARE_WAITQUEUE(wait, current);
758 add_wait_queue_exclusive(&sk->lock.wq, &wait);
759 for(;;) {
760 current->state = TASK_EXCLUSIVE | TASK_UNINTERRUPTIBLE;
761 spin_unlock_bh(&sk->lock.slock);
762 schedule();
763 spin_lock_bh(&sk->lock.slock);
764 if(!sk->lock.users)
765 break;
767 current->state = TASK_RUNNING;
768 remove_wait_queue(&sk->lock.wq, &wait);
771 void __release_sock(struct sock *sk)
773 struct sk_buff *skb = sk->backlog.head;
774 do {
775 struct sk_buff *next = skb->next;
776 skb->next = NULL;
777 sk->backlog_rcv(sk, skb);
778 skb = next;
779 } while(skb != NULL);
780 sk->backlog.head = sk->backlog.tail = NULL;
784 * Generic socket manager library. Most simpler socket families
785 * use this to manage their socket lists. At some point we should
786 * hash these. By making this generic we get the lot hashed for free.
788 * It is broken by design. All the protocols using it must be fixed. --ANK
791 rwlock_t net_big_sklist_lock = RW_LOCK_UNLOCKED;
793 void sklist_remove_socket(struct sock **list, struct sock *sk)
795 struct sock *s;
797 write_lock_bh(&net_big_sklist_lock);
799 s= *list;
800 if(s==sk)
802 *list = s->next;
803 write_unlock_bh(&net_big_sklist_lock);
804 sock_put(sk);
805 return;
807 while(s && s->next)
809 if(s->next==sk)
811 s->next=sk->next;
812 break;
814 s=s->next;
816 write_unlock_bh(&net_big_sklist_lock);
819 void sklist_insert_socket(struct sock **list, struct sock *sk)
821 write_lock_bh(&net_big_sklist_lock);
822 sk->next= *list;
823 *list=sk;
824 sock_hold(sk);
825 write_unlock_bh(&net_big_sklist_lock);
829 * This is only called from user mode. Thus it protects itself against
830 * interrupt users but doesn't worry about being called during work.
831 * Once it is removed from the queue no interrupt or bottom half will
832 * touch it and we are (fairly 8-) ) safe.
835 void sklist_destroy_socket(struct sock **list, struct sock *sk);
838 * Handler for deferred kills.
841 static void sklist_destroy_timer(unsigned long data)
843 struct sock *sk=(struct sock *)data;
844 sklist_destroy_socket(NULL,sk);
848 * Destroy a socket. We pass NULL for a list if we know the
849 * socket is not on a list.
852 void sklist_destroy_socket(struct sock **list,struct sock *sk)
854 struct sk_buff *skb;
855 if(list)
856 sklist_remove_socket(list, sk);
858 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
860 kfree_skb(skb);
863 if(atomic_read(&sk->wmem_alloc) == 0 &&
864 atomic_read(&sk->rmem_alloc) == 0 &&
865 sk->dead)
867 sock_put(sk);
869 else
872 * Someone is using our buffers still.. defer
874 init_timer(&sk->timer);
875 sk->timer.expires=jiffies+SOCK_DESTROY_TIME;
876 sk->timer.function=sklist_destroy_timer;
877 sk->timer.data = (unsigned long)sk;
878 add_timer(&sk->timer);
883 * Set of default routines for initialising struct proto_ops when
884 * the protocol does not support a particular function. In certain
885 * cases where it makes no sense for a protocol to have a "do nothing"
886 * function, some default processing is provided.
889 int sock_no_release(struct socket *sock)
891 return 0;
894 int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
896 return -EOPNOTSUPP;
899 int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
900 int len, int flags)
902 return -EOPNOTSUPP;
905 int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
907 return -EOPNOTSUPP;
910 int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
912 return -EOPNOTSUPP;
915 int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
916 int *len, int peer)
918 return -EOPNOTSUPP;
921 unsigned int sock_no_poll(struct file * file, struct socket *sock, poll_table *pt)
923 return 0;
926 int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
928 return -EOPNOTSUPP;
931 int sock_no_listen(struct socket *sock, int backlog)
933 return -EOPNOTSUPP;
936 int sock_no_shutdown(struct socket *sock, int how)
938 return -EOPNOTSUPP;
941 int sock_no_setsockopt(struct socket *sock, int level, int optname,
942 char *optval, int optlen)
944 return -EOPNOTSUPP;
947 int sock_no_getsockopt(struct socket *sock, int level, int optname,
948 char *optval, int *optlen)
950 return -EOPNOTSUPP;
954 * Note: if you add something that sleeps here then change sock_fcntl()
955 * to do proper fd locking.
957 int sock_no_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
959 struct sock *sk = sock->sk;
961 switch(cmd)
963 case F_SETOWN:
965 * This is a little restrictive, but it's the only
966 * way to make sure that you can't send a sigurg to
967 * another process.
969 if (current->pgrp != -arg &&
970 current->pid != arg &&
971 !capable(CAP_KILL)) return(-EPERM);
972 sk->proc = arg;
973 return(0);
974 case F_GETOWN:
975 return(sk->proc);
976 default:
977 return(-EINVAL);
981 int sock_no_sendmsg(struct socket *sock, struct msghdr *m, int flags,
982 struct scm_cookie *scm)
984 return -EOPNOTSUPP;
987 int sock_no_recvmsg(struct socket *sock, struct msghdr *m, int flags,
988 struct scm_cookie *scm)
990 return -EOPNOTSUPP;
993 int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
995 /* Mirror missing mmap method error code */
996 return -ENODEV;
1000 * Default Socket Callbacks
1003 void sock_def_wakeup(struct sock *sk)
1005 read_lock(&sk->callback_lock);
1006 if(!sk->dead)
1007 wake_up_interruptible(sk->sleep);
1008 read_unlock(&sk->callback_lock);
1011 void sock_def_error_report(struct sock *sk)
1013 read_lock(&sk->callback_lock);
1014 if (!sk->dead) {
1015 wake_up_interruptible(sk->sleep);
1016 sock_wake_async(sk->socket,0);
1018 read_unlock(&sk->callback_lock);
1021 void sock_def_readable(struct sock *sk, int len)
1023 read_lock(&sk->callback_lock);
1024 if(!sk->dead) {
1025 wake_up_interruptible(sk->sleep);
1026 sock_wake_async(sk->socket,1);
1028 read_unlock(&sk->callback_lock);
1031 void sock_def_write_space(struct sock *sk)
1033 read_lock(&sk->callback_lock);
1035 /* Do not wake up a writer until he can make "significant"
1036 * progress. --DaveM
1038 if(!sk->dead &&
1039 ((atomic_read(&sk->wmem_alloc) << 1) <= sk->sndbuf)) {
1040 wake_up_interruptible(sk->sleep);
1042 /* Should agree with poll, otherwise some programs break */
1043 if (sock_writeable(sk))
1044 sock_wake_async(sk->socket, 2);
1046 read_unlock(&sk->callback_lock);
1049 void sock_def_destruct(struct sock *sk)
1051 if (sk->protinfo.destruct_hook)
1052 kfree(sk->protinfo.destruct_hook);
1055 void sock_init_data(struct socket *sock, struct sock *sk)
1057 skb_queue_head_init(&sk->receive_queue);
1058 skb_queue_head_init(&sk->write_queue);
1059 skb_queue_head_init(&sk->error_queue);
1061 spin_lock_init(&sk->timer_lock);
1062 init_timer(&sk->timer);
1064 sk->allocation = GFP_KERNEL;
1065 sk->rcvbuf = sysctl_rmem_default;
1066 sk->sndbuf = sysctl_wmem_default;
1067 sk->state = TCP_CLOSE;
1068 sk->zapped = 1;
1069 sk->socket = sock;
1071 if(sock)
1073 sk->type = sock->type;
1074 sk->sleep = &sock->wait;
1075 sock->sk = sk;
1076 } else
1077 sk->sleep = NULL;
1079 sk->callback_lock = RW_LOCK_UNLOCKED;
1081 sk->state_change = sock_def_wakeup;
1082 sk->data_ready = sock_def_readable;
1083 sk->write_space = sock_def_write_space;
1084 sk->error_report = sock_def_error_report;
1085 sk->destruct = sock_def_destruct;
1087 sk->peercred.pid = 0;
1088 sk->peercred.uid = -1;
1089 sk->peercred.gid = -1;
1091 atomic_set(&sk->refcnt, 1);