This commit was manufactured by cvs2svn to create tag 'after-xenU'.
[linux-2.6.git] / net / core / sock.c
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.
5  *
6  *              Generic socket support routines. Memory allocators, socket lock/release
7  *              handler for protocols to use and generic option handler.
8  *
9  *
10  * Version:     $Id: sock.c,v 1.117 2002/02/01 22:01:03 davem Exp $
11  *
12  * Authors:     Ross Biro
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>
16  *
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
82  *              Chris Evans     :       Security fixes - signedness again
83  *              Arnaldo C. Melo :       cleanups, use skb_queue_purge
84  *
85  * To Fix:
86  *
87  *
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.
92  */
93
94 #include <linux/config.h>
95 #include <linux/errno.h>
96 #include <linux/types.h>
97 #include <linux/socket.h>
98 #include <linux/in.h>
99 #include <linux/kernel.h>
100 #include <linux/module.h>
101 #include <linux/proc_fs.h>
102 #include <linux/seq_file.h>
103 #include <linux/sched.h>
104 #include <linux/timer.h>
105 #include <linux/string.h>
106 #include <linux/sockios.h>
107 #include <linux/net.h>
108 #include <linux/mm.h>
109 #include <linux/slab.h>
110 #include <linux/interrupt.h>
111 #include <linux/poll.h>
112 #include <linux/tcp.h>
113 #include <linux/init.h>
114
115 #include <asm/uaccess.h>
116 #include <asm/system.h>
117
118 #include <linux/netdevice.h>
119 #include <net/protocol.h>
120 #include <linux/skbuff.h>
121 #include <net/sock.h>
122 #include <net/xfrm.h>
123 #include <linux/ipsec.h>
124
125 #include <linux/filter.h>
126 #include <linux/vs_socket.h>
127 #include <linux/vs_limit.h>
128 #include <linux/vs_context.h>
129
130 #ifdef CONFIG_INET
131 #include <net/tcp.h>
132 #endif
133
134 /* Take into consideration the size of the struct sk_buff overhead in the
135  * determination of these values, since that is non-constant across
136  * platforms.  This makes socket queueing behavior and performance
137  * not depend upon such differences.
138  */
139 #define _SK_MEM_PACKETS         256
140 #define _SK_MEM_OVERHEAD        (sizeof(struct sk_buff) + 256)
141 #define SK_WMEM_MAX             (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
142 #define SK_RMEM_MAX             (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
143
144 /* Run time adjustable parameters. */
145 __u32 sysctl_wmem_max = SK_WMEM_MAX;
146 __u32 sysctl_rmem_max = SK_RMEM_MAX;
147 __u32 sysctl_wmem_default = SK_WMEM_MAX;
148 __u32 sysctl_rmem_default = SK_RMEM_MAX;
149
150 /* Maximal space eaten by iovec or ancilliary data plus some space */
151 int sysctl_optmem_max = sizeof(unsigned long)*(2*UIO_MAXIOV + 512);
152
153 static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
154 {
155         struct timeval tv;
156
157         if (optlen < sizeof(tv))
158                 return -EINVAL;
159         if (copy_from_user(&tv, optval, sizeof(tv)))
160                 return -EFAULT;
161
162         *timeo_p = MAX_SCHEDULE_TIMEOUT;
163         if (tv.tv_sec == 0 && tv.tv_usec == 0)
164                 return 0;
165         if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
166                 *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
167         return 0;
168 }
169
170 static void sock_warn_obsolete_bsdism(const char *name)
171 {
172         static int warned;
173         static char warncomm[TASK_COMM_LEN];
174         if (strcmp(warncomm, current->comm) && warned < 5) { 
175                 strcpy(warncomm,  current->comm); 
176                 printk(KERN_WARNING "process `%s' is using obsolete "
177                        "%s SO_BSDCOMPAT\n", warncomm, name);
178                 warned++;
179         }
180 }
181
182 static void sock_disable_timestamp(struct sock *sk)
183 {       
184         if (sock_flag(sk, SOCK_TIMESTAMP)) { 
185                 sock_reset_flag(sk, SOCK_TIMESTAMP);
186                 net_disable_timestamp();
187         }
188 }
189
190
191 /*
192  *      This is meant for all protocols to use and covers goings on
193  *      at the socket level. Everything here is generic.
194  */
195
196 int sock_setsockopt(struct socket *sock, int level, int optname,
197                     char __user *optval, int optlen)
198 {
199         struct sock *sk=sock->sk;
200         struct sk_filter *filter;
201         int val;
202         int valbool;
203         struct linger ling;
204         int ret = 0;
205         
206         /*
207          *      Options without arguments
208          */
209
210 #ifdef SO_DONTLINGER            /* Compatibility item... */
211         switch (optname) {
212                 case SO_DONTLINGER:
213                         sock_reset_flag(sk, SOCK_LINGER);
214                         return 0;
215         }
216 #endif  
217                 
218         if(optlen<sizeof(int))
219                 return(-EINVAL);
220         
221         if (get_user(val, (int __user *)optval))
222                 return -EFAULT;
223         
224         valbool = val?1:0;
225
226         lock_sock(sk);
227
228         switch(optname) 
229         {
230                 case SO_DEBUG:  
231                         if(val && !capable(CAP_NET_ADMIN))
232                         {
233                                 ret = -EACCES;
234                         }
235                         else if (valbool)
236                                 sock_set_flag(sk, SOCK_DBG);
237                         else
238                                 sock_reset_flag(sk, SOCK_DBG);
239                         break;
240                 case SO_REUSEADDR:
241                         sk->sk_reuse = valbool;
242                         break;
243                 case SO_TYPE:
244                 case SO_ERROR:
245                         ret = -ENOPROTOOPT;
246                         break;
247                 case SO_DONTROUTE:
248                         if (valbool)
249                                 sock_set_flag(sk, SOCK_LOCALROUTE);
250                         else
251                                 sock_reset_flag(sk, SOCK_LOCALROUTE);
252                         break;
253                 case SO_BROADCAST:
254                         sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
255                         break;
256                 case SO_SNDBUF:
257                         /* Don't error on this BSD doesn't and if you think
258                            about it this is right. Otherwise apps have to
259                            play 'guess the biggest size' games. RCVBUF/SNDBUF
260                            are treated in BSD as hints */
261                            
262                         if (val > sysctl_wmem_max)
263                                 val = sysctl_wmem_max;
264
265                         sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
266                         if ((val * 2) < SOCK_MIN_SNDBUF)
267                                 sk->sk_sndbuf = SOCK_MIN_SNDBUF;
268                         else
269                                 sk->sk_sndbuf = val * 2;
270
271                         /*
272                          *      Wake up sending tasks if we
273                          *      upped the value.
274                          */
275                         sk->sk_write_space(sk);
276                         break;
277
278                 case SO_RCVBUF:
279                         /* Don't error on this BSD doesn't and if you think
280                            about it this is right. Otherwise apps have to
281                            play 'guess the biggest size' games. RCVBUF/SNDBUF
282                            are treated in BSD as hints */
283                           
284                         if (val > sysctl_rmem_max)
285                                 val = sysctl_rmem_max;
286
287                         sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
288                         /* FIXME: is this lower bound the right one? */
289                         if ((val * 2) < SOCK_MIN_RCVBUF)
290                                 sk->sk_rcvbuf = SOCK_MIN_RCVBUF;
291                         else
292                                 sk->sk_rcvbuf = val * 2;
293                         break;
294
295                 case SO_KEEPALIVE:
296 #ifdef CONFIG_INET
297                         if (sk->sk_protocol == IPPROTO_TCP)
298                                 tcp_set_keepalive(sk, valbool);
299 #endif
300                         sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
301                         break;
302
303                 case SO_OOBINLINE:
304                         sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
305                         break;
306
307                 case SO_NO_CHECK:
308                         sk->sk_no_check = valbool;
309                         break;
310
311                 case SO_PRIORITY:
312                         if ((val >= 0 && val <= 6) || capable(CAP_NET_ADMIN)) 
313                                 sk->sk_priority = val;
314                         else
315                                 ret = -EPERM;
316                         break;
317
318                 case SO_LINGER:
319                         if(optlen<sizeof(ling)) {
320                                 ret = -EINVAL;  /* 1003.1g */
321                                 break;
322                         }
323                         if (copy_from_user(&ling,optval,sizeof(ling))) {
324                                 ret = -EFAULT;
325                                 break;
326                         }
327                         if (!ling.l_onoff)
328                                 sock_reset_flag(sk, SOCK_LINGER);
329                         else {
330 #if (BITS_PER_LONG == 32)
331                                 if (ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
332                                         sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
333                                 else
334 #endif
335                                         sk->sk_lingertime = ling.l_linger * HZ;
336                                 sock_set_flag(sk, SOCK_LINGER);
337                         }
338                         break;
339
340                 case SO_BSDCOMPAT:
341                         sock_warn_obsolete_bsdism("setsockopt");
342                         break;
343
344                 case SO_PASSCRED:
345                         if (valbool)
346                                 set_bit(SOCK_PASSCRED, &sock->flags);
347                         else
348                                 clear_bit(SOCK_PASSCRED, &sock->flags);
349                         break;
350
351 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
352                 case SO_SETXID:
353                         if (current->xid) {
354                                 ret = -EPERM;
355                                 break;
356                         }
357                         if (val < 0 || val > MAX_S_CONTEXT) {
358                                 ret = -EINVAL;
359                                 break;
360                         }
361                         sk->sk_xid = val;
362                         break;
363 #endif
364
365                 case SO_TIMESTAMP:
366                         if (valbool)  {
367                                 sock_set_flag(sk, SOCK_RCVTSTAMP);
368                                 sock_enable_timestamp(sk);
369                         } else
370                                 sock_reset_flag(sk, SOCK_RCVTSTAMP);
371                         break;
372
373                 case SO_RCVLOWAT:
374                         if (val < 0)
375                                 val = INT_MAX;
376                         sk->sk_rcvlowat = val ? : 1;
377                         break;
378
379                 case SO_RCVTIMEO:
380                         ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
381                         break;
382
383                 case SO_SNDTIMEO:
384                         ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
385                         break;
386
387 #ifdef CONFIG_NETDEVICES
388                 case SO_BINDTODEVICE:
389                 {
390                         char devname[IFNAMSIZ]; 
391
392                         /* Sorry... */ 
393                         if (!capable(CAP_NET_RAW)) {
394                                 ret = -EPERM;
395                                 break;
396                         }
397
398                         /* Bind this socket to a particular device like "eth0",
399                          * as specified in the passed interface name. If the
400                          * name is "" or the option length is zero the socket 
401                          * is not bound. 
402                          */ 
403
404                         if (!valbool) {
405                                 sk->sk_bound_dev_if = 0;
406                         } else {
407                                 if (optlen > IFNAMSIZ) 
408                                         optlen = IFNAMSIZ; 
409                                 if (copy_from_user(devname, optval, optlen)) {
410                                         ret = -EFAULT;
411                                         break;
412                                 }
413
414                                 /* Remove any cached route for this socket. */
415                                 sk_dst_reset(sk);
416
417                                 if (devname[0] == '\0') {
418                                         sk->sk_bound_dev_if = 0;
419                                 } else {
420                                         struct net_device *dev = dev_get_by_name(devname);
421                                         if (!dev) {
422                                                 ret = -ENODEV;
423                                                 break;
424                                         }
425                                         sk->sk_bound_dev_if = dev->ifindex;
426                                         dev_put(dev);
427                                 }
428                         }
429                         break;
430                 }
431 #endif
432
433
434                 case SO_ATTACH_FILTER:
435                         ret = -EINVAL;
436                         if (optlen == sizeof(struct sock_fprog)) {
437                                 struct sock_fprog fprog;
438
439                                 ret = -EFAULT;
440                                 if (copy_from_user(&fprog, optval, sizeof(fprog)))
441                                         break;
442
443                                 ret = sk_attach_filter(&fprog, sk);
444                         }
445                         break;
446
447                 case SO_DETACH_FILTER:
448                         spin_lock_bh(&sk->sk_lock.slock);
449                         filter = sk->sk_filter;
450                         if (filter) {
451                                 sk->sk_filter = NULL;
452                                 spin_unlock_bh(&sk->sk_lock.slock);
453                                 sk_filter_release(sk, filter);
454                                 break;
455                         }
456                         spin_unlock_bh(&sk->sk_lock.slock);
457                         ret = -ENONET;
458                         break;
459
460                 /* We implement the SO_SNDLOWAT etc to
461                    not be settable (1003.1g 5.3) */
462                 default:
463                         ret = -ENOPROTOOPT;
464                         break;
465         }
466         release_sock(sk);
467         return ret;
468 }
469
470
471 int sock_getsockopt(struct socket *sock, int level, int optname,
472                     char __user *optval, int __user *optlen)
473 {
474         struct sock *sk = sock->sk;
475         
476         union
477         {
478                 int val;
479                 struct linger ling;
480                 struct timeval tm;
481         } v;
482         
483         unsigned int lv = sizeof(int);
484         int len;
485         
486         if(get_user(len,optlen))
487                 return -EFAULT;
488         if(len < 0)
489                 return -EINVAL;
490                 
491         switch(optname) 
492         {
493                 case SO_DEBUG:          
494                         v.val = sock_flag(sk, SOCK_DBG);
495                         break;
496                 
497                 case SO_DONTROUTE:
498                         v.val = sock_flag(sk, SOCK_LOCALROUTE);
499                         break;
500                 
501                 case SO_BROADCAST:
502                         v.val = !!sock_flag(sk, SOCK_BROADCAST);
503                         break;
504
505                 case SO_SNDBUF:
506                         v.val = sk->sk_sndbuf;
507                         break;
508                 
509                 case SO_RCVBUF:
510                         v.val = sk->sk_rcvbuf;
511                         break;
512
513                 case SO_REUSEADDR:
514                         v.val = sk->sk_reuse;
515                         break;
516
517                 case SO_KEEPALIVE:
518                         v.val = !!sock_flag(sk, SOCK_KEEPOPEN);
519                         break;
520
521                 case SO_TYPE:
522                         v.val = sk->sk_type;                            
523                         break;
524
525                 case SO_ERROR:
526                         v.val = -sock_error(sk);
527                         if(v.val==0)
528                                 v.val = xchg(&sk->sk_err_soft, 0);
529                         break;
530
531                 case SO_OOBINLINE:
532                         v.val = !!sock_flag(sk, SOCK_URGINLINE);
533                         break;
534         
535                 case SO_NO_CHECK:
536                         v.val = sk->sk_no_check;
537                         break;
538
539                 case SO_PRIORITY:
540                         v.val = sk->sk_priority;
541                         break;
542                 
543                 case SO_LINGER: 
544                         lv              = sizeof(v.ling);
545                         v.ling.l_onoff  = !!sock_flag(sk, SOCK_LINGER);
546                         v.ling.l_linger = sk->sk_lingertime / HZ;
547                         break;
548                                         
549                 case SO_BSDCOMPAT:
550                         sock_warn_obsolete_bsdism("getsockopt");
551                         break;
552
553                 case SO_TIMESTAMP:
554                         v.val = sock_flag(sk, SOCK_RCVTSTAMP);
555                         break;
556
557                 case SO_RCVTIMEO:
558                         lv=sizeof(struct timeval);
559                         if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
560                                 v.tm.tv_sec = 0;
561                                 v.tm.tv_usec = 0;
562                         } else {
563                                 v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
564                                 v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * 1000000) / HZ;
565                         }
566                         break;
567
568                 case SO_SNDTIMEO:
569                         lv=sizeof(struct timeval);
570                         if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
571                                 v.tm.tv_sec = 0;
572                                 v.tm.tv_usec = 0;
573                         } else {
574                                 v.tm.tv_sec = sk->sk_sndtimeo / HZ;
575                                 v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * 1000000) / HZ;
576                         }
577                         break;
578
579                 case SO_RCVLOWAT:
580                         v.val = sk->sk_rcvlowat;
581                         break;
582
583                 case SO_SNDLOWAT:
584                         v.val=1;
585                         break; 
586
587                 case SO_PASSCRED:
588                         v.val = test_bit(SOCK_PASSCRED, &sock->flags) ? 1 : 0;
589                         break;
590
591                 case SO_PEERCRED:
592                         if (len > sizeof(sk->sk_peercred))
593                                 len = sizeof(sk->sk_peercred);
594                         if (copy_to_user(optval, &sk->sk_peercred, len))
595                                 return -EFAULT;
596                         goto lenout;
597
598                 case SO_PEERNAME:
599                 {
600                         char address[128];
601
602                         if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
603                                 return -ENOTCONN;
604                         if (lv < len)
605                                 return -EINVAL;
606                         if (copy_to_user(optval, address, len))
607                                 return -EFAULT;
608                         goto lenout;
609                 }
610
611                 /* Dubious BSD thing... Probably nobody even uses it, but
612                  * the UNIX standard wants it for whatever reason... -DaveM
613                  */
614                 case SO_ACCEPTCONN:
615                         v.val = sk->sk_state == TCP_LISTEN;
616                         break;
617
618                 case SO_PEERSEC:
619                         return security_socket_getpeersec(sock, optval, optlen, len);
620
621                 default:
622                         return(-ENOPROTOOPT);
623         }
624         if (len > lv)
625                 len = lv;
626         if (copy_to_user(optval, &v, len))
627                 return -EFAULT;
628 lenout:
629         if (put_user(len, optlen))
630                 return -EFAULT;
631         return 0;
632 }
633
634 /**
635  *      sk_alloc - All socket objects are allocated here
636  *      @family: protocol family
637  *      @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
638  *      @prot: struct proto associated with this new sock instance
639  *      @zero_it: if we should zero the newly allocated sock
640  */
641 struct sock *sk_alloc(int family, int priority, struct proto *prot, int zero_it)
642 {
643         struct sock *sk = NULL;
644         kmem_cache_t *slab = prot->slab;
645
646         if (slab != NULL)
647                 sk = kmem_cache_alloc(slab, priority);
648         else
649                 sk = kmalloc(prot->obj_size, priority);
650
651         if (sk) {
652                 if (zero_it) {
653                         memset(sk, 0, prot->obj_size);
654                         sk->sk_family = family;
655                         /*
656                          * See comment in struct sock definition to understand
657                          * why we need sk_prot_creator -acme
658                          */
659                         sk->sk_prot = sk->sk_prot_creator = prot;
660                         sock_lock_init(sk);
661                 }
662                 sock_vx_init(sk);
663                 sock_nx_init(sk);
664                 
665                 if (security_sk_alloc(sk, family, priority)) {
666                         if (slab != NULL)
667                                 kmem_cache_free(slab, sk);
668                         else
669                                 kfree(sk);
670                         sk = NULL;
671                 } else
672                         __module_get(prot->owner);
673         }
674         return sk;
675 }
676
677 void sk_free(struct sock *sk)
678 {
679         struct sk_filter *filter;
680         struct module *owner = sk->sk_prot_creator->owner;
681
682         if (sk->sk_destruct)
683                 sk->sk_destruct(sk);
684
685         filter = sk->sk_filter;
686         if (filter) {
687                 sk_filter_release(sk, filter);
688                 sk->sk_filter = NULL;
689         }
690
691         sock_disable_timestamp(sk);
692
693         if (atomic_read(&sk->sk_omem_alloc))
694                 printk(KERN_DEBUG "%s: optmem leakage (%d bytes) detected.\n",
695                        __FUNCTION__, atomic_read(&sk->sk_omem_alloc));
696
697         security_sk_free(sk);
698         vx_sock_dec(sk);
699         clr_vx_info(&sk->sk_vx_info);
700         sk->sk_xid = -1;
701         clr_nx_info(&sk->sk_nx_info);
702         sk->sk_nid = -1;
703         if (sk->sk_prot_creator->slab != NULL)
704                 kmem_cache_free(sk->sk_prot_creator->slab, sk);
705         else
706                 kfree(sk);
707         module_put(owner);
708 }
709
710 void __init sk_init(void)
711 {
712         if (num_physpages <= 4096) {
713                 sysctl_wmem_max = 32767;
714                 sysctl_rmem_max = 32767;
715                 sysctl_wmem_default = 32767;
716                 sysctl_rmem_default = 32767;
717         } else if (num_physpages >= 131072) {
718                 sysctl_wmem_max = 131071;
719                 sysctl_rmem_max = 131071;
720         }
721 }
722
723 /*
724  *      Simple resource managers for sockets.
725  */
726
727
728 /* 
729  * Write buffer destructor automatically called from kfree_skb. 
730  */
731 void sock_wfree(struct sk_buff *skb)
732 {
733         struct sock *sk = skb->sk;
734
735         /* In case it might be waiting for more memory. */
736         atomic_sub(skb->truesize, &sk->sk_wmem_alloc);
737         if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE))
738                 sk->sk_write_space(sk);
739         sock_put(sk);
740 }
741
742 /* 
743  * Read buffer destructor automatically called from kfree_skb. 
744  */
745 void sock_rfree(struct sk_buff *skb)
746 {
747         struct sock *sk = skb->sk;
748
749         atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
750 }
751
752
753 int sock_i_uid(struct sock *sk)
754 {
755         int uid;
756
757         read_lock(&sk->sk_callback_lock);
758         uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : 0;
759         read_unlock(&sk->sk_callback_lock);
760         return uid;
761 }
762
763 unsigned long sock_i_ino(struct sock *sk)
764 {
765         unsigned long ino;
766
767         read_lock(&sk->sk_callback_lock);
768         ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
769         read_unlock(&sk->sk_callback_lock);
770         return ino;
771 }
772
773 /*
774  * Allocate a skb from the socket's send buffer.
775  */
776 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, int priority)
777 {
778         if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
779                 struct sk_buff * skb = alloc_skb(size, priority);
780                 if (skb) {
781                         skb_set_owner_w(skb, sk);
782                         return skb;
783                 }
784         }
785         return NULL;
786 }
787
788 /*
789  * Allocate a skb from the socket's receive buffer.
790  */ 
791 struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
792 {
793         if (force || atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
794                 struct sk_buff *skb = alloc_skb(size, priority);
795                 if (skb) {
796                         skb_set_owner_r(skb, sk);
797                         return skb;
798                 }
799         }
800         return NULL;
801 }
802
803 /* 
804  * Allocate a memory block from the socket's option memory buffer.
805  */ 
806 void *sock_kmalloc(struct sock *sk, int size, int priority)
807 {
808         if ((unsigned)size <= sysctl_optmem_max &&
809             atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
810                 void *mem;
811                 /* First do the add, to avoid the race if kmalloc
812                  * might sleep.
813                  */
814                 atomic_add(size, &sk->sk_omem_alloc);
815                 mem = kmalloc(size, priority);
816                 if (mem)
817                         return mem;
818                 atomic_sub(size, &sk->sk_omem_alloc);
819         }
820         return NULL;
821 }
822
823 /*
824  * Free an option memory block.
825  */
826 void sock_kfree_s(struct sock *sk, void *mem, int size)
827 {
828         kfree(mem);
829         atomic_sub(size, &sk->sk_omem_alloc);
830 }
831
832 /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
833    I think, these locks should be removed for datagram sockets.
834  */
835 static long sock_wait_for_wmem(struct sock * sk, long timeo)
836 {
837         DEFINE_WAIT(wait);
838
839         clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
840         for (;;) {
841                 if (!timeo)
842                         break;
843                 if (signal_pending(current))
844                         break;
845                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
846                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
847                 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
848                         break;
849                 if (sk->sk_shutdown & SEND_SHUTDOWN)
850                         break;
851                 if (sk->sk_err)
852                         break;
853                 timeo = schedule_timeout(timeo);
854         }
855         finish_wait(sk->sk_sleep, &wait);
856         return timeo;
857 }
858
859
860 /*
861  *      Generic send/receive buffer handlers
862  */
863
864 static struct sk_buff *sock_alloc_send_pskb(struct sock *sk,
865                                             unsigned long header_len,
866                                             unsigned long data_len,
867                                             int noblock, int *errcode)
868 {
869         struct sk_buff *skb;
870         unsigned int gfp_mask;
871         long timeo;
872         int err;
873
874         gfp_mask = sk->sk_allocation;
875         if (gfp_mask & __GFP_WAIT)
876                 gfp_mask |= __GFP_REPEAT;
877
878         timeo = sock_sndtimeo(sk, noblock);
879         while (1) {
880                 err = sock_error(sk);
881                 if (err != 0)
882                         goto failure;
883
884                 err = -EPIPE;
885                 if (sk->sk_shutdown & SEND_SHUTDOWN)
886                         goto failure;
887
888                 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
889                         skb = alloc_skb(header_len, sk->sk_allocation);
890                         if (skb) {
891                                 int npages;
892                                 int i;
893
894                                 /* No pages, we're done... */
895                                 if (!data_len)
896                                         break;
897
898                                 npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
899                                 skb->truesize += data_len;
900                                 skb_shinfo(skb)->nr_frags = npages;
901                                 for (i = 0; i < npages; i++) {
902                                         struct page *page;
903                                         skb_frag_t *frag;
904
905                                         page = alloc_pages(sk->sk_allocation, 0);
906                                         if (!page) {
907                                                 err = -ENOBUFS;
908                                                 skb_shinfo(skb)->nr_frags = i;
909                                                 kfree_skb(skb);
910                                                 goto failure;
911                                         }
912
913                                         frag = &skb_shinfo(skb)->frags[i];
914                                         frag->page = page;
915                                         frag->page_offset = 0;
916                                         frag->size = (data_len >= PAGE_SIZE ?
917                                                       PAGE_SIZE :
918                                                       data_len);
919                                         data_len -= PAGE_SIZE;
920                                 }
921
922                                 /* Full success... */
923                                 break;
924                         }
925                         err = -ENOBUFS;
926                         goto failure;
927                 }
928                 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
929                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
930                 err = -EAGAIN;
931                 if (!timeo)
932                         goto failure;
933                 if (signal_pending(current))
934                         goto interrupted;
935                 timeo = sock_wait_for_wmem(sk, timeo);
936         }
937
938         skb_set_owner_w(skb, sk);
939         return skb;
940
941 interrupted:
942         err = sock_intr_errno(timeo);
943 failure:
944         *errcode = err;
945         return NULL;
946 }
947
948 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size, 
949                                     int noblock, int *errcode)
950 {
951         return sock_alloc_send_pskb(sk, size, 0, noblock, errcode);
952 }
953
954 static void __lock_sock(struct sock *sk)
955 {
956         DEFINE_WAIT(wait);
957
958         for(;;) {
959                 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
960                                         TASK_UNINTERRUPTIBLE);
961                 spin_unlock_bh(&sk->sk_lock.slock);
962                 schedule();
963                 spin_lock_bh(&sk->sk_lock.slock);
964                 if(!sock_owned_by_user(sk))
965                         break;
966         }
967         finish_wait(&sk->sk_lock.wq, &wait);
968 }
969
970 static void __release_sock(struct sock *sk)
971 {
972         struct sk_buff *skb = sk->sk_backlog.head;
973
974         do {
975                 sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
976                 bh_unlock_sock(sk);
977
978                 do {
979                         struct sk_buff *next = skb->next;
980
981                         skb->next = NULL;
982                         sk->sk_backlog_rcv(sk, skb);
983
984                         /*
985                          * We are in process context here with softirqs
986                          * disabled, use cond_resched_softirq() to preempt.
987                          * This is safe to do because we've taken the backlog
988                          * queue private:
989                          */
990                         cond_resched_softirq();
991
992                         skb = next;
993                 } while (skb != NULL);
994
995                 bh_lock_sock(sk);
996         } while((skb = sk->sk_backlog.head) != NULL);
997 }
998
999 /**
1000  * sk_wait_data - wait for data to arrive at sk_receive_queue
1001  * @sk:    sock to wait on
1002  * @timeo: for how long
1003  *
1004  * Now socket state including sk->sk_err is changed only under lock,
1005  * hence we may omit checks after joining wait queue.
1006  * We check receive queue before schedule() only as optimization;
1007  * it is very likely that release_sock() added new data.
1008  */
1009 int sk_wait_data(struct sock *sk, long *timeo)
1010 {
1011         int rc;
1012         DEFINE_WAIT(wait);
1013
1014         prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1015         set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1016         rc = sk_wait_event(sk, timeo, !skb_queue_empty(&sk->sk_receive_queue));
1017         clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1018         finish_wait(sk->sk_sleep, &wait);
1019         return rc;
1020 }
1021
1022 EXPORT_SYMBOL(sk_wait_data);
1023
1024 /*
1025  * Set of default routines for initialising struct proto_ops when
1026  * the protocol does not support a particular function. In certain
1027  * cases where it makes no sense for a protocol to have a "do nothing"
1028  * function, some default processing is provided.
1029  */
1030
1031 int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
1032 {
1033         return -EOPNOTSUPP;
1034 }
1035
1036 int sock_no_connect(struct socket *sock, struct sockaddr *saddr, 
1037                     int len, int flags)
1038 {
1039         return -EOPNOTSUPP;
1040 }
1041
1042 int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
1043 {
1044         return -EOPNOTSUPP;
1045 }
1046
1047 int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
1048 {
1049         return -EOPNOTSUPP;
1050 }
1051
1052 int sock_no_getname(struct socket *sock, struct sockaddr *saddr, 
1053                     int *len, int peer)
1054 {
1055         return -EOPNOTSUPP;
1056 }
1057
1058 unsigned int sock_no_poll(struct file * file, struct socket *sock, poll_table *pt)
1059 {
1060         return 0;
1061 }
1062
1063 int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1064 {
1065         return -EOPNOTSUPP;
1066 }
1067
1068 int sock_no_listen(struct socket *sock, int backlog)
1069 {
1070         return -EOPNOTSUPP;
1071 }
1072
1073 int sock_no_shutdown(struct socket *sock, int how)
1074 {
1075         return -EOPNOTSUPP;
1076 }
1077
1078 int sock_no_setsockopt(struct socket *sock, int level, int optname,
1079                     char __user *optval, int optlen)
1080 {
1081         return -EOPNOTSUPP;
1082 }
1083
1084 int sock_no_getsockopt(struct socket *sock, int level, int optname,
1085                     char __user *optval, int __user *optlen)
1086 {
1087         return -EOPNOTSUPP;
1088 }
1089
1090 int sock_no_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
1091                     size_t len)
1092 {
1093         return -EOPNOTSUPP;
1094 }
1095
1096 int sock_no_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
1097                     size_t len, int flags)
1098 {
1099         return -EOPNOTSUPP;
1100 }
1101
1102 int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1103 {
1104         /* Mirror missing mmap method error code */
1105         return -ENODEV;
1106 }
1107
1108 ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
1109 {
1110         ssize_t res;
1111         struct msghdr msg = {.msg_flags = flags};
1112         struct kvec iov;
1113         char *kaddr = kmap(page);
1114         iov.iov_base = kaddr + offset;
1115         iov.iov_len = size;
1116         res = kernel_sendmsg(sock, &msg, &iov, 1, size);
1117         kunmap(page);
1118         return res;
1119 }
1120
1121 /*
1122  *      Default Socket Callbacks
1123  */
1124
1125 static void sock_def_wakeup(struct sock *sk)
1126 {
1127         read_lock(&sk->sk_callback_lock);
1128         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1129                 wake_up_interruptible_all(sk->sk_sleep);
1130         read_unlock(&sk->sk_callback_lock);
1131 }
1132
1133 static void sock_def_error_report(struct sock *sk)
1134 {
1135         read_lock(&sk->sk_callback_lock);
1136         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1137                 wake_up_interruptible(sk->sk_sleep);
1138         sk_wake_async(sk,0,POLL_ERR); 
1139         read_unlock(&sk->sk_callback_lock);
1140 }
1141
1142 static void sock_def_readable(struct sock *sk, int len)
1143 {
1144         read_lock(&sk->sk_callback_lock);
1145         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1146                 wake_up_interruptible(sk->sk_sleep);
1147         sk_wake_async(sk,1,POLL_IN);
1148         read_unlock(&sk->sk_callback_lock);
1149 }
1150
1151 static void sock_def_write_space(struct sock *sk)
1152 {
1153         read_lock(&sk->sk_callback_lock);
1154
1155         /* Do not wake up a writer until he can make "significant"
1156          * progress.  --DaveM
1157          */
1158         if((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
1159                 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1160                         wake_up_interruptible(sk->sk_sleep);
1161
1162                 /* Should agree with poll, otherwise some programs break */
1163                 if (sock_writeable(sk))
1164                         sk_wake_async(sk, 2, POLL_OUT);
1165         }
1166
1167         read_unlock(&sk->sk_callback_lock);
1168 }
1169
1170 static void sock_def_destruct(struct sock *sk)
1171 {
1172         if (sk->sk_protinfo)
1173                 kfree(sk->sk_protinfo);
1174 }
1175
1176 void sk_send_sigurg(struct sock *sk)
1177 {
1178         if (sk->sk_socket && sk->sk_socket->file)
1179                 if (send_sigurg(&sk->sk_socket->file->f_owner))
1180                         sk_wake_async(sk, 3, POLL_PRI);
1181 }
1182
1183 void sk_reset_timer(struct sock *sk, struct timer_list* timer,
1184                     unsigned long expires)
1185 {
1186         if (!mod_timer(timer, expires))
1187                 sock_hold(sk);
1188 }
1189
1190 EXPORT_SYMBOL(sk_reset_timer);
1191
1192 void sk_stop_timer(struct sock *sk, struct timer_list* timer)
1193 {
1194         if (timer_pending(timer) && del_timer(timer))
1195                 __sock_put(sk);
1196 }
1197
1198 EXPORT_SYMBOL(sk_stop_timer);
1199
1200 void sock_init_data(struct socket *sock, struct sock *sk)
1201 {
1202         skb_queue_head_init(&sk->sk_receive_queue);
1203         skb_queue_head_init(&sk->sk_write_queue);
1204         skb_queue_head_init(&sk->sk_error_queue);
1205
1206         sk->sk_send_head        =       NULL;
1207
1208         init_timer(&sk->sk_timer);
1209         
1210         sk->sk_allocation       =       GFP_KERNEL;
1211         sk->sk_rcvbuf           =       sysctl_rmem_default;
1212         sk->sk_sndbuf           =       sysctl_wmem_default;
1213         sk->sk_state            =       TCP_CLOSE;
1214         sk->sk_socket           =       sock;
1215
1216         sock_set_flag(sk, SOCK_ZAPPED);
1217
1218         if(sock)
1219         {
1220                 sk->sk_type     =       sock->type;
1221                 sk->sk_sleep    =       &sock->wait;
1222                 sock->sk        =       sk;
1223         } else
1224                 sk->sk_sleep    =       NULL;
1225
1226         rwlock_init(&sk->sk_dst_lock);
1227         rwlock_init(&sk->sk_callback_lock);
1228
1229         sk->sk_state_change     =       sock_def_wakeup;
1230         sk->sk_data_ready       =       sock_def_readable;
1231         sk->sk_write_space      =       sock_def_write_space;
1232         sk->sk_error_report     =       sock_def_error_report;
1233         sk->sk_destruct         =       sock_def_destruct;
1234
1235         sk->sk_sndmsg_page      =       NULL;
1236         sk->sk_sndmsg_off       =       0;
1237
1238         sk->sk_peercred.pid     =       0;
1239         sk->sk_peercred.uid     =       -1;
1240         sk->sk_peercred.gid     =       -1;
1241         sk->sk_write_pending    =       0;
1242         sk->sk_rcvlowat         =       1;
1243         sk->sk_rcvtimeo         =       MAX_SCHEDULE_TIMEOUT;
1244         sk->sk_sndtimeo         =       MAX_SCHEDULE_TIMEOUT;
1245
1246         sk->sk_stamp.tv_sec     = -1L;
1247         sk->sk_stamp.tv_usec    = -1L;
1248
1249         set_vx_info(&sk->sk_vx_info, current->vx_info);
1250         sk->sk_xid = vx_current_xid();
1251         vx_sock_inc(sk);
1252         set_nx_info(&sk->sk_nx_info, current->nx_info);
1253         sk->sk_nid = nx_current_nid();
1254         atomic_set(&sk->sk_refcnt, 1);
1255 }
1256
1257 void fastcall lock_sock(struct sock *sk)
1258 {
1259         might_sleep();
1260         spin_lock_bh(&(sk->sk_lock.slock));
1261         if (sk->sk_lock.owner)
1262                 __lock_sock(sk);
1263         sk->sk_lock.owner = (void *)1;
1264         spin_unlock_bh(&(sk->sk_lock.slock));
1265 }
1266
1267 EXPORT_SYMBOL(lock_sock);
1268
1269 void fastcall release_sock(struct sock *sk)
1270 {
1271         spin_lock_bh(&(sk->sk_lock.slock));
1272         if (sk->sk_backlog.tail)
1273                 __release_sock(sk);
1274         sk->sk_lock.owner = NULL;
1275         if (waitqueue_active(&(sk->sk_lock.wq)))
1276                 wake_up(&(sk->sk_lock.wq));
1277         spin_unlock_bh(&(sk->sk_lock.slock));
1278 }
1279 EXPORT_SYMBOL(release_sock);
1280
1281 int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
1282
1283         if (!sock_flag(sk, SOCK_TIMESTAMP))
1284                 sock_enable_timestamp(sk);
1285         if (sk->sk_stamp.tv_sec == -1) 
1286                 return -ENOENT;
1287         if (sk->sk_stamp.tv_sec == 0)
1288                 do_gettimeofday(&sk->sk_stamp);
1289         return copy_to_user(userstamp, &sk->sk_stamp, sizeof(struct timeval)) ?
1290                 -EFAULT : 0; 
1291
1292 EXPORT_SYMBOL(sock_get_timestamp);
1293
1294 void sock_enable_timestamp(struct sock *sk)
1295 {       
1296         if (!sock_flag(sk, SOCK_TIMESTAMP)) { 
1297                 sock_set_flag(sk, SOCK_TIMESTAMP);
1298                 net_enable_timestamp();
1299         }
1300 }
1301 EXPORT_SYMBOL(sock_enable_timestamp); 
1302
1303 /*
1304  *      Get a socket option on an socket.
1305  *
1306  *      FIX: POSIX 1003.1g is very ambiguous here. It states that
1307  *      asynchronous errors should be reported by getsockopt. We assume
1308  *      this means if you specify SO_ERROR (otherwise whats the point of it).
1309  */
1310 int sock_common_getsockopt(struct socket *sock, int level, int optname,
1311                            char __user *optval, int __user *optlen)
1312 {
1313         struct sock *sk = sock->sk;
1314
1315         return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
1316 }
1317
1318 EXPORT_SYMBOL(sock_common_getsockopt);
1319
1320 int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
1321                         struct msghdr *msg, size_t size, int flags)
1322 {
1323         struct sock *sk = sock->sk;
1324         int addr_len = 0;
1325         int err;
1326
1327         err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
1328                                    flags & ~MSG_DONTWAIT, &addr_len);
1329         if (err >= 0)
1330                 msg->msg_namelen = addr_len;
1331         return err;
1332 }
1333
1334 EXPORT_SYMBOL(sock_common_recvmsg);
1335
1336 /*
1337  *      Set socket options on an inet socket.
1338  */
1339 int sock_common_setsockopt(struct socket *sock, int level, int optname,
1340                            char __user *optval, int optlen)
1341 {
1342         struct sock *sk = sock->sk;
1343
1344         return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
1345 }
1346
1347 EXPORT_SYMBOL(sock_common_setsockopt);
1348
1349 void sk_common_release(struct sock *sk)
1350 {
1351         if (sk->sk_prot->destroy)
1352                 sk->sk_prot->destroy(sk);
1353
1354         /*
1355          * Observation: when sock_common_release is called, processes have
1356          * no access to socket. But net still has.
1357          * Step one, detach it from networking:
1358          *
1359          * A. Remove from hash tables.
1360          */
1361
1362         sk->sk_prot->unhash(sk);
1363
1364         /*
1365          * In this point socket cannot receive new packets, but it is possible
1366          * that some packets are in flight because some CPU runs receiver and
1367          * did hash table lookup before we unhashed socket. They will achieve
1368          * receive queue and will be purged by socket destructor.
1369          *
1370          * Also we still have packets pending on receive queue and probably,
1371          * our own packets waiting in device queues. sock_destroy will drain
1372          * receive queue, but transmitted packets will delay socket destruction
1373          * until the last reference will be released.
1374          */
1375
1376         sock_orphan(sk);
1377
1378         xfrm_sk_free_policy(sk);
1379
1380 #ifdef INET_REFCNT_DEBUG
1381         if (atomic_read(&sk->sk_refcnt) != 1)
1382                 printk(KERN_DEBUG "Destruction of the socket %p delayed, c=%d\n",
1383                        sk, atomic_read(&sk->sk_refcnt));
1384 #endif
1385         sock_put(sk);
1386 }
1387
1388 EXPORT_SYMBOL(sk_common_release);
1389
1390 static DEFINE_RWLOCK(proto_list_lock);
1391 static LIST_HEAD(proto_list);
1392
1393 int proto_register(struct proto *prot, int alloc_slab)
1394 {
1395         int rc = -ENOBUFS;
1396
1397         if (alloc_slab) {
1398                 prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
1399                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
1400
1401                 if (prot->slab == NULL) {
1402                         printk(KERN_CRIT "%s: Can't create sock SLAB cache!\n",
1403                                prot->name);
1404                         goto out;
1405                 }
1406         }
1407
1408         write_lock(&proto_list_lock);
1409         list_add(&prot->node, &proto_list);
1410         write_unlock(&proto_list_lock);
1411         rc = 0;
1412 out:
1413         return rc;
1414 }
1415
1416 EXPORT_SYMBOL(proto_register);
1417
1418 void proto_unregister(struct proto *prot)
1419 {
1420         write_lock(&proto_list_lock);
1421
1422         if (prot->slab != NULL) {
1423                 kmem_cache_destroy(prot->slab);
1424                 prot->slab = NULL;
1425         }
1426
1427         list_del(&prot->node);
1428         write_unlock(&proto_list_lock);
1429 }
1430
1431 EXPORT_SYMBOL(proto_unregister);
1432
1433 #ifdef CONFIG_PROC_FS
1434 static inline struct proto *__proto_head(void)
1435 {
1436         return list_entry(proto_list.next, struct proto, node);
1437 }
1438
1439 static inline struct proto *proto_head(void)
1440 {
1441         return list_empty(&proto_list) ? NULL : __proto_head();
1442 }
1443
1444 static inline struct proto *proto_next(struct proto *proto)
1445 {
1446         return proto->node.next == &proto_list ? NULL :
1447                 list_entry(proto->node.next, struct proto, node);
1448 }
1449
1450 static inline struct proto *proto_get_idx(loff_t pos)
1451 {
1452         struct proto *proto;
1453         loff_t i = 0;
1454
1455         list_for_each_entry(proto, &proto_list, node)
1456                 if (i++ == pos)
1457                         goto out;
1458
1459         proto = NULL;
1460 out:
1461         return proto;
1462 }
1463
1464 static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
1465 {
1466         read_lock(&proto_list_lock);
1467         return *pos ? proto_get_idx(*pos - 1) : SEQ_START_TOKEN;
1468 }
1469
1470 static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1471 {
1472         ++*pos;
1473         return v == SEQ_START_TOKEN ? proto_head() : proto_next(v);
1474 }
1475
1476 static void proto_seq_stop(struct seq_file *seq, void *v)
1477 {
1478         read_unlock(&proto_list_lock);
1479 }
1480
1481 static char proto_method_implemented(const void *method)
1482 {
1483         return method == NULL ? 'n' : 'y';
1484 }
1485
1486 static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
1487 {
1488         seq_printf(seq, "%-9s %4u %6d  %6d   %-3s %6u   %-3s  %-10s "
1489                         "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
1490                    proto->name,
1491                    proto->obj_size,
1492                    proto->sockets_allocated != NULL ? atomic_read(proto->sockets_allocated) : -1,
1493                    proto->memory_allocated != NULL ? atomic_read(proto->memory_allocated) : -1,
1494                    proto->memory_pressure != NULL ? *proto->memory_pressure ? "yes" : "no" : "NI",
1495                    proto->max_header,
1496                    proto->slab == NULL ? "no" : "yes",
1497                    module_name(proto->owner),
1498                    proto_method_implemented(proto->close),
1499                    proto_method_implemented(proto->connect),
1500                    proto_method_implemented(proto->disconnect),
1501                    proto_method_implemented(proto->accept),
1502                    proto_method_implemented(proto->ioctl),
1503                    proto_method_implemented(proto->init),
1504                    proto_method_implemented(proto->destroy),
1505                    proto_method_implemented(proto->shutdown),
1506                    proto_method_implemented(proto->setsockopt),
1507                    proto_method_implemented(proto->getsockopt),
1508                    proto_method_implemented(proto->sendmsg),
1509                    proto_method_implemented(proto->recvmsg),
1510                    proto_method_implemented(proto->sendpage),
1511                    proto_method_implemented(proto->bind),
1512                    proto_method_implemented(proto->backlog_rcv),
1513                    proto_method_implemented(proto->hash),
1514                    proto_method_implemented(proto->unhash),
1515                    proto_method_implemented(proto->get_port),
1516                    proto_method_implemented(proto->enter_memory_pressure));
1517 }
1518
1519 static int proto_seq_show(struct seq_file *seq, void *v)
1520 {
1521         if (v == SEQ_START_TOKEN)
1522                 seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
1523                            "protocol",
1524                            "size",
1525                            "sockets",
1526                            "memory",
1527                            "press",
1528                            "maxhdr",
1529                            "slab",
1530                            "module",
1531                            "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
1532         else
1533                 proto_seq_printf(seq, v);
1534         return 0;
1535 }
1536
1537 static struct seq_operations proto_seq_ops = {
1538         .start  = proto_seq_start,
1539         .next   = proto_seq_next,
1540         .stop   = proto_seq_stop,
1541         .show   = proto_seq_show,
1542 };
1543
1544 static int proto_seq_open(struct inode *inode, struct file *file)
1545 {
1546         return seq_open(file, &proto_seq_ops);
1547 }
1548
1549 static struct file_operations proto_seq_fops = {
1550         .owner          = THIS_MODULE,
1551         .open           = proto_seq_open,
1552         .read           = seq_read,
1553         .llseek         = seq_lseek,
1554         .release        = seq_release,
1555 };
1556
1557 static int __init proto_init(void)
1558 {
1559         /* register /proc/net/protocols */
1560         return proc_net_fops_create("protocols", S_IRUGO, &proto_seq_fops) == NULL ? -ENOBUFS : 0;
1561 }
1562
1563 subsys_initcall(proto_init);
1564
1565 #endif /* PROC_FS */
1566 EXPORT_SYMBOL(sk_alloc);
1567 EXPORT_SYMBOL(sk_free);
1568 EXPORT_SYMBOL(sk_send_sigurg);
1569 EXPORT_SYMBOL(sock_alloc_send_skb);
1570 EXPORT_SYMBOL(sock_init_data);
1571 EXPORT_SYMBOL(sock_kfree_s);
1572 EXPORT_SYMBOL(sock_kmalloc);
1573 EXPORT_SYMBOL(sock_no_accept);
1574 EXPORT_SYMBOL(sock_no_bind);
1575 EXPORT_SYMBOL(sock_no_connect);
1576 EXPORT_SYMBOL(sock_no_getname);
1577 EXPORT_SYMBOL(sock_no_getsockopt);
1578 EXPORT_SYMBOL(sock_no_ioctl);
1579 EXPORT_SYMBOL(sock_no_listen);
1580 EXPORT_SYMBOL(sock_no_mmap);
1581 EXPORT_SYMBOL(sock_no_poll);
1582 EXPORT_SYMBOL(sock_no_recvmsg);
1583 EXPORT_SYMBOL(sock_no_sendmsg);
1584 EXPORT_SYMBOL(sock_no_sendpage);
1585 EXPORT_SYMBOL(sock_no_setsockopt);
1586 EXPORT_SYMBOL(sock_no_shutdown);
1587 EXPORT_SYMBOL(sock_no_socketpair);
1588 EXPORT_SYMBOL(sock_rfree);
1589 EXPORT_SYMBOL(sock_setsockopt);
1590 EXPORT_SYMBOL(sock_wfree);
1591 EXPORT_SYMBOL(sock_wmalloc);
1592 EXPORT_SYMBOL(sock_i_uid);
1593 EXPORT_SYMBOL(sock_i_ino);
1594 #ifdef CONFIG_SYSCTL
1595 EXPORT_SYMBOL(sysctl_optmem_max);
1596 EXPORT_SYMBOL(sysctl_rmem_max);
1597 EXPORT_SYMBOL(sysctl_wmem_max);
1598 #endif