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.117 2002/02/01 22:01:03 davem Exp $
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/capability.h>
95 #include <linux/errno.h>
96 #include <linux/types.h>
97 #include <linux/socket.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>
115 #include <asm/uaccess.h>
116 #include <asm/system.h>
118 #include <linux/netdevice.h>
119 #include <net/protocol.h>
120 #include <linux/skbuff.h>
121 #include <net/request_sock.h>
122 #include <net/sock.h>
123 #include <net/xfrm.h>
124 #include <linux/ipsec.h>
126 #include <linux/filter.h>
132 /* Take into consideration the size of the struct sk_buff overhead in the
133 * determination of these values, since that is non-constant across
134 * platforms. This makes socket queueing behavior and performance
135 * not depend upon such differences.
137 #define _SK_MEM_PACKETS 256
138 #define _SK_MEM_OVERHEAD (sizeof(struct sk_buff) + 256)
139 #define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
140 #define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
142 /* Run time adjustable parameters. */
143 __u32 sysctl_wmem_max
= SK_WMEM_MAX
;
144 __u32 sysctl_rmem_max
= SK_RMEM_MAX
;
145 __u32 sysctl_wmem_default
= SK_WMEM_MAX
;
146 __u32 sysctl_rmem_default
= SK_RMEM_MAX
;
148 /* Maximal space eaten by iovec or ancilliary data plus some space */
149 int sysctl_optmem_max
= sizeof(unsigned long)*(2*UIO_MAXIOV
+ 512);
151 static int sock_set_timeout(long *timeo_p
, char __user
*optval
, int optlen
)
155 if (optlen
< sizeof(tv
))
157 if (copy_from_user(&tv
, optval
, sizeof(tv
)))
160 *timeo_p
= MAX_SCHEDULE_TIMEOUT
;
161 if (tv
.tv_sec
== 0 && tv
.tv_usec
== 0)
163 if (tv
.tv_sec
< (MAX_SCHEDULE_TIMEOUT
/HZ
- 1))
164 *timeo_p
= tv
.tv_sec
*HZ
+ (tv
.tv_usec
+(1000000/HZ
-1))/(1000000/HZ
);
168 static void sock_warn_obsolete_bsdism(const char *name
)
171 static char warncomm
[TASK_COMM_LEN
];
172 if (strcmp(warncomm
, current
->comm
) && warned
< 5) {
173 strcpy(warncomm
, current
->comm
);
174 printk(KERN_WARNING
"process `%s' is using obsolete "
175 "%s SO_BSDCOMPAT\n", warncomm
, name
);
180 static void sock_disable_timestamp(struct sock
*sk
)
182 if (sock_flag(sk
, SOCK_TIMESTAMP
)) {
183 sock_reset_flag(sk
, SOCK_TIMESTAMP
);
184 net_disable_timestamp();
189 int sock_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
194 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
195 number of warnings when compiling with -W --ANK
197 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
198 (unsigned)sk
->sk_rcvbuf
) {
203 /* It would be deadlock, if sock_queue_rcv_skb is used
204 with socket lock! We assume that users of this
205 function are lock free.
207 err
= sk_filter(sk
, skb
, 1);
212 skb_set_owner_r(skb
, sk
);
214 /* Cache the SKB length before we tack it onto the receive
215 * queue. Once it is added it no longer belongs to us and
216 * may be freed by other threads of control pulling packets
221 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
223 if (!sock_flag(sk
, SOCK_DEAD
))
224 sk
->sk_data_ready(sk
, skb_len
);
228 EXPORT_SYMBOL(sock_queue_rcv_skb
);
230 int sk_receive_skb(struct sock
*sk
, struct sk_buff
*skb
)
232 int rc
= NET_RX_SUCCESS
;
234 if (sk_filter(sk
, skb
, 0))
235 goto discard_and_relse
;
240 if (!sock_owned_by_user(sk
))
241 rc
= sk
->sk_backlog_rcv(sk
, skb
);
243 sk_add_backlog(sk
, skb
);
252 EXPORT_SYMBOL(sk_receive_skb
);
254 struct dst_entry
*__sk_dst_check(struct sock
*sk
, u32 cookie
)
256 struct dst_entry
*dst
= sk
->sk_dst_cache
;
258 if (dst
&& dst
->obsolete
&& dst
->ops
->check(dst
, cookie
) == NULL
) {
259 sk
->sk_dst_cache
= NULL
;
266 EXPORT_SYMBOL(__sk_dst_check
);
268 struct dst_entry
*sk_dst_check(struct sock
*sk
, u32 cookie
)
270 struct dst_entry
*dst
= sk_dst_get(sk
);
272 if (dst
&& dst
->obsolete
&& dst
->ops
->check(dst
, cookie
) == NULL
) {
280 EXPORT_SYMBOL(sk_dst_check
);
283 * This is meant for all protocols to use and covers goings on
284 * at the socket level. Everything here is generic.
287 int sock_setsockopt(struct socket
*sock
, int level
, int optname
,
288 char __user
*optval
, int optlen
)
290 struct sock
*sk
=sock
->sk
;
291 struct sk_filter
*filter
;
298 * Options without arguments
301 #ifdef SO_DONTLINGER /* Compatibility item... */
302 if (optname
== SO_DONTLINGER
) {
304 sock_reset_flag(sk
, SOCK_LINGER
);
310 if(optlen
<sizeof(int))
313 if (get_user(val
, (int __user
*)optval
))
323 if(val
&& !capable(CAP_NET_ADMIN
))
328 sock_set_flag(sk
, SOCK_DBG
);
330 sock_reset_flag(sk
, SOCK_DBG
);
333 sk
->sk_reuse
= valbool
;
341 sock_set_flag(sk
, SOCK_LOCALROUTE
);
343 sock_reset_flag(sk
, SOCK_LOCALROUTE
);
346 sock_valbool_flag(sk
, SOCK_BROADCAST
, valbool
);
349 /* Don't error on this BSD doesn't and if you think
350 about it this is right. Otherwise apps have to
351 play 'guess the biggest size' games. RCVBUF/SNDBUF
352 are treated in BSD as hints */
354 if (val
> sysctl_wmem_max
)
355 val
= sysctl_wmem_max
;
357 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
358 if ((val
* 2) < SOCK_MIN_SNDBUF
)
359 sk
->sk_sndbuf
= SOCK_MIN_SNDBUF
;
361 sk
->sk_sndbuf
= val
* 2;
364 * Wake up sending tasks if we
367 sk
->sk_write_space(sk
);
371 if (!capable(CAP_NET_ADMIN
)) {
378 /* Don't error on this BSD doesn't and if you think
379 about it this is right. Otherwise apps have to
380 play 'guess the biggest size' games. RCVBUF/SNDBUF
381 are treated in BSD as hints */
383 if (val
> sysctl_rmem_max
)
384 val
= sysctl_rmem_max
;
386 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
388 * We double it on the way in to account for
389 * "struct sk_buff" etc. overhead. Applications
390 * assume that the SO_RCVBUF setting they make will
391 * allow that much actual data to be received on that
394 * Applications are unaware that "struct sk_buff" and
395 * other overheads allocate from the receive buffer
396 * during socket buffer allocation.
398 * And after considering the possible alternatives,
399 * returning the value we actually used in getsockopt
400 * is the most desirable behavior.
402 if ((val
* 2) < SOCK_MIN_RCVBUF
)
403 sk
->sk_rcvbuf
= SOCK_MIN_RCVBUF
;
405 sk
->sk_rcvbuf
= val
* 2;
409 if (!capable(CAP_NET_ADMIN
)) {
417 if (sk
->sk_protocol
== IPPROTO_TCP
)
418 tcp_set_keepalive(sk
, valbool
);
420 sock_valbool_flag(sk
, SOCK_KEEPOPEN
, valbool
);
424 sock_valbool_flag(sk
, SOCK_URGINLINE
, valbool
);
428 sk
->sk_no_check
= valbool
;
432 if ((val
>= 0 && val
<= 6) || capable(CAP_NET_ADMIN
))
433 sk
->sk_priority
= val
;
439 if(optlen
<sizeof(ling
)) {
440 ret
= -EINVAL
; /* 1003.1g */
443 if (copy_from_user(&ling
,optval
,sizeof(ling
))) {
448 sock_reset_flag(sk
, SOCK_LINGER
);
450 #if (BITS_PER_LONG == 32)
451 if ((unsigned int)ling
.l_linger
>= MAX_SCHEDULE_TIMEOUT
/HZ
)
452 sk
->sk_lingertime
= MAX_SCHEDULE_TIMEOUT
;
455 sk
->sk_lingertime
= (unsigned int)ling
.l_linger
* HZ
;
456 sock_set_flag(sk
, SOCK_LINGER
);
461 sock_warn_obsolete_bsdism("setsockopt");
466 set_bit(SOCK_PASSCRED
, &sock
->flags
);
468 clear_bit(SOCK_PASSCRED
, &sock
->flags
);
473 sock_set_flag(sk
, SOCK_RCVTSTAMP
);
474 sock_enable_timestamp(sk
);
476 sock_reset_flag(sk
, SOCK_RCVTSTAMP
);
482 sk
->sk_rcvlowat
= val
? : 1;
486 ret
= sock_set_timeout(&sk
->sk_rcvtimeo
, optval
, optlen
);
490 ret
= sock_set_timeout(&sk
->sk_sndtimeo
, optval
, optlen
);
493 #ifdef CONFIG_NETDEVICES
494 case SO_BINDTODEVICE
:
496 char devname
[IFNAMSIZ
];
499 if (!capable(CAP_NET_RAW
)) {
504 /* Bind this socket to a particular device like "eth0",
505 * as specified in the passed interface name. If the
506 * name is "" or the option length is zero the socket
511 sk
->sk_bound_dev_if
= 0;
513 if (optlen
> IFNAMSIZ
- 1)
514 optlen
= IFNAMSIZ
- 1;
515 memset(devname
, 0, sizeof(devname
));
516 if (copy_from_user(devname
, optval
, optlen
)) {
521 /* Remove any cached route for this socket. */
524 if (devname
[0] == '\0') {
525 sk
->sk_bound_dev_if
= 0;
527 struct net_device
*dev
= dev_get_by_name(devname
);
532 sk
->sk_bound_dev_if
= dev
->ifindex
;
541 case SO_ATTACH_FILTER
:
543 if (optlen
== sizeof(struct sock_fprog
)) {
544 struct sock_fprog fprog
;
547 if (copy_from_user(&fprog
, optval
, sizeof(fprog
)))
550 ret
= sk_attach_filter(&fprog
, sk
);
554 case SO_DETACH_FILTER
:
555 spin_lock_bh(&sk
->sk_lock
.slock
);
556 filter
= sk
->sk_filter
;
558 sk
->sk_filter
= NULL
;
559 spin_unlock_bh(&sk
->sk_lock
.slock
);
560 sk_filter_release(sk
, filter
);
563 spin_unlock_bh(&sk
->sk_lock
.slock
);
569 set_bit(SOCK_PASSSEC
, &sock
->flags
);
571 clear_bit(SOCK_PASSSEC
, &sock
->flags
);
574 /* We implement the SO_SNDLOWAT etc to
575 not be settable (1003.1g 5.3) */
585 int sock_getsockopt(struct socket
*sock
, int level
, int optname
,
586 char __user
*optval
, int __user
*optlen
)
588 struct sock
*sk
= sock
->sk
;
597 unsigned int lv
= sizeof(int);
600 if(get_user(len
,optlen
))
608 v
.val
= sock_flag(sk
, SOCK_DBG
);
612 v
.val
= sock_flag(sk
, SOCK_LOCALROUTE
);
616 v
.val
= !!sock_flag(sk
, SOCK_BROADCAST
);
620 v
.val
= sk
->sk_sndbuf
;
624 v
.val
= sk
->sk_rcvbuf
;
628 v
.val
= sk
->sk_reuse
;
632 v
.val
= !!sock_flag(sk
, SOCK_KEEPOPEN
);
640 v
.val
= -sock_error(sk
);
642 v
.val
= xchg(&sk
->sk_err_soft
, 0);
646 v
.val
= !!sock_flag(sk
, SOCK_URGINLINE
);
650 v
.val
= sk
->sk_no_check
;
654 v
.val
= sk
->sk_priority
;
659 v
.ling
.l_onoff
= !!sock_flag(sk
, SOCK_LINGER
);
660 v
.ling
.l_linger
= sk
->sk_lingertime
/ HZ
;
664 sock_warn_obsolete_bsdism("getsockopt");
668 v
.val
= sock_flag(sk
, SOCK_RCVTSTAMP
);
672 lv
=sizeof(struct timeval
);
673 if (sk
->sk_rcvtimeo
== MAX_SCHEDULE_TIMEOUT
) {
677 v
.tm
.tv_sec
= sk
->sk_rcvtimeo
/ HZ
;
678 v
.tm
.tv_usec
= ((sk
->sk_rcvtimeo
% HZ
) * 1000000) / HZ
;
683 lv
=sizeof(struct timeval
);
684 if (sk
->sk_sndtimeo
== MAX_SCHEDULE_TIMEOUT
) {
688 v
.tm
.tv_sec
= sk
->sk_sndtimeo
/ HZ
;
689 v
.tm
.tv_usec
= ((sk
->sk_sndtimeo
% HZ
) * 1000000) / HZ
;
694 v
.val
= sk
->sk_rcvlowat
;
702 v
.val
= test_bit(SOCK_PASSCRED
, &sock
->flags
) ? 1 : 0;
706 if (len
> sizeof(sk
->sk_peercred
))
707 len
= sizeof(sk
->sk_peercred
);
708 if (copy_to_user(optval
, &sk
->sk_peercred
, len
))
716 if (sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &lv
, 2))
720 if (copy_to_user(optval
, address
, len
))
725 /* Dubious BSD thing... Probably nobody even uses it, but
726 * the UNIX standard wants it for whatever reason... -DaveM
729 v
.val
= sk
->sk_state
== TCP_LISTEN
;
733 v
.val
= test_bit(SOCK_PASSSEC
, &sock
->flags
) ? 1 : 0;
737 return security_socket_getpeersec_stream(sock
, optval
, optlen
, len
);
740 return(-ENOPROTOOPT
);
744 if (copy_to_user(optval
, &v
, len
))
747 if (put_user(len
, optlen
))
753 * sk_alloc - All socket objects are allocated here
754 * @family: protocol family
755 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
756 * @prot: struct proto associated with this new sock instance
757 * @zero_it: if we should zero the newly allocated sock
759 struct sock
*sk_alloc(int family
, gfp_t priority
,
760 struct proto
*prot
, int zero_it
)
762 struct sock
*sk
= NULL
;
763 kmem_cache_t
*slab
= prot
->slab
;
766 sk
= kmem_cache_alloc(slab
, priority
);
768 sk
= kmalloc(prot
->obj_size
, priority
);
772 memset(sk
, 0, prot
->obj_size
);
773 sk
->sk_family
= family
;
775 * See comment in struct sock definition to understand
776 * why we need sk_prot_creator -acme
778 sk
->sk_prot
= sk
->sk_prot_creator
= prot
;
782 if (security_sk_alloc(sk
, family
, priority
))
785 if (!try_module_get(prot
->owner
))
792 kmem_cache_free(slab
, sk
);
798 void sk_free(struct sock
*sk
)
800 struct sk_filter
*filter
;
801 struct module
*owner
= sk
->sk_prot_creator
->owner
;
806 filter
= sk
->sk_filter
;
808 sk_filter_release(sk
, filter
);
809 sk
->sk_filter
= NULL
;
812 sock_disable_timestamp(sk
);
814 if (atomic_read(&sk
->sk_omem_alloc
))
815 printk(KERN_DEBUG
"%s: optmem leakage (%d bytes) detected.\n",
816 __FUNCTION__
, atomic_read(&sk
->sk_omem_alloc
));
818 security_sk_free(sk
);
819 if (sk
->sk_prot_creator
->slab
!= NULL
)
820 kmem_cache_free(sk
->sk_prot_creator
->slab
, sk
);
826 struct sock
*sk_clone(const struct sock
*sk
, const gfp_t priority
)
828 struct sock
*newsk
= sk_alloc(sk
->sk_family
, priority
, sk
->sk_prot
, 0);
831 struct sk_filter
*filter
;
833 memcpy(newsk
, sk
, sk
->sk_prot
->obj_size
);
836 sk_node_init(&newsk
->sk_node
);
837 sock_lock_init(newsk
);
840 atomic_set(&newsk
->sk_rmem_alloc
, 0);
841 atomic_set(&newsk
->sk_wmem_alloc
, 0);
842 atomic_set(&newsk
->sk_omem_alloc
, 0);
843 skb_queue_head_init(&newsk
->sk_receive_queue
);
844 skb_queue_head_init(&newsk
->sk_write_queue
);
845 #ifdef CONFIG_NET_DMA
846 skb_queue_head_init(&newsk
->sk_async_wait_queue
);
849 rwlock_init(&newsk
->sk_dst_lock
);
850 rwlock_init(&newsk
->sk_callback_lock
);
852 newsk
->sk_dst_cache
= NULL
;
853 newsk
->sk_wmem_queued
= 0;
854 newsk
->sk_forward_alloc
= 0;
855 newsk
->sk_send_head
= NULL
;
856 newsk
->sk_backlog
.head
= newsk
->sk_backlog
.tail
= NULL
;
857 newsk
->sk_userlocks
= sk
->sk_userlocks
& ~SOCK_BINDPORT_LOCK
;
859 sock_reset_flag(newsk
, SOCK_DONE
);
860 skb_queue_head_init(&newsk
->sk_error_queue
);
862 filter
= newsk
->sk_filter
;
864 sk_filter_charge(newsk
, filter
);
866 if (unlikely(xfrm_sk_clone_policy(newsk
))) {
867 /* It is still raw copy of parent, so invalidate
868 * destructor and make plain sk_free() */
869 newsk
->sk_destruct
= NULL
;
876 newsk
->sk_priority
= 0;
877 atomic_set(&newsk
->sk_refcnt
, 2);
880 * Increment the counter in the same struct proto as the master
881 * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
882 * is the same as sk->sk_prot->socks, as this field was copied
885 * This _changes_ the previous behaviour, where
886 * tcp_create_openreq_child always was incrementing the
887 * equivalent to tcp_prot->socks (inet_sock_nr), so this have
888 * to be taken into account in all callers. -acme
890 sk_refcnt_debug_inc(newsk
);
891 newsk
->sk_socket
= NULL
;
892 newsk
->sk_sleep
= NULL
;
894 if (newsk
->sk_prot
->sockets_allocated
)
895 atomic_inc(newsk
->sk_prot
->sockets_allocated
);
901 EXPORT_SYMBOL_GPL(sk_clone
);
903 void __init
sk_init(void)
905 if (num_physpages
<= 4096) {
906 sysctl_wmem_max
= 32767;
907 sysctl_rmem_max
= 32767;
908 sysctl_wmem_default
= 32767;
909 sysctl_rmem_default
= 32767;
910 } else if (num_physpages
>= 131072) {
911 sysctl_wmem_max
= 131071;
912 sysctl_rmem_max
= 131071;
917 * Simple resource managers for sockets.
922 * Write buffer destructor automatically called from kfree_skb.
924 void sock_wfree(struct sk_buff
*skb
)
926 struct sock
*sk
= skb
->sk
;
928 /* In case it might be waiting for more memory. */
929 atomic_sub(skb
->truesize
, &sk
->sk_wmem_alloc
);
930 if (!sock_flag(sk
, SOCK_USE_WRITE_QUEUE
))
931 sk
->sk_write_space(sk
);
936 * Read buffer destructor automatically called from kfree_skb.
938 void sock_rfree(struct sk_buff
*skb
)
940 struct sock
*sk
= skb
->sk
;
942 atomic_sub(skb
->truesize
, &sk
->sk_rmem_alloc
);
946 int sock_i_uid(struct sock
*sk
)
950 read_lock(&sk
->sk_callback_lock
);
951 uid
= sk
->sk_socket
? SOCK_INODE(sk
->sk_socket
)->i_uid
: 0;
952 read_unlock(&sk
->sk_callback_lock
);
956 unsigned long sock_i_ino(struct sock
*sk
)
960 read_lock(&sk
->sk_callback_lock
);
961 ino
= sk
->sk_socket
? SOCK_INODE(sk
->sk_socket
)->i_ino
: 0;
962 read_unlock(&sk
->sk_callback_lock
);
967 * Allocate a skb from the socket's send buffer.
969 struct sk_buff
*sock_wmalloc(struct sock
*sk
, unsigned long size
, int force
,
972 if (force
|| atomic_read(&sk
->sk_wmem_alloc
) < sk
->sk_sndbuf
) {
973 struct sk_buff
* skb
= alloc_skb(size
, priority
);
975 skb_set_owner_w(skb
, sk
);
983 * Allocate a skb from the socket's receive buffer.
985 struct sk_buff
*sock_rmalloc(struct sock
*sk
, unsigned long size
, int force
,
988 if (force
|| atomic_read(&sk
->sk_rmem_alloc
) < sk
->sk_rcvbuf
) {
989 struct sk_buff
*skb
= alloc_skb(size
, priority
);
991 skb_set_owner_r(skb
, sk
);
999 * Allocate a memory block from the socket's option memory buffer.
1001 void *sock_kmalloc(struct sock
*sk
, int size
, gfp_t priority
)
1003 if ((unsigned)size
<= sysctl_optmem_max
&&
1004 atomic_read(&sk
->sk_omem_alloc
) + size
< sysctl_optmem_max
) {
1006 /* First do the add, to avoid the race if kmalloc
1009 atomic_add(size
, &sk
->sk_omem_alloc
);
1010 mem
= kmalloc(size
, priority
);
1013 atomic_sub(size
, &sk
->sk_omem_alloc
);
1019 * Free an option memory block.
1021 void sock_kfree_s(struct sock
*sk
, void *mem
, int size
)
1024 atomic_sub(size
, &sk
->sk_omem_alloc
);
1027 /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
1028 I think, these locks should be removed for datagram sockets.
1030 static long sock_wait_for_wmem(struct sock
* sk
, long timeo
)
1034 clear_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
1038 if (signal_pending(current
))
1040 set_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
1041 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
1042 if (atomic_read(&sk
->sk_wmem_alloc
) < sk
->sk_sndbuf
)
1044 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
1048 timeo
= schedule_timeout(timeo
);
1050 finish_wait(sk
->sk_sleep
, &wait
);
1056 * Generic send/receive buffer handlers
1059 static struct sk_buff
*sock_alloc_send_pskb(struct sock
*sk
,
1060 unsigned long header_len
,
1061 unsigned long data_len
,
1062 int noblock
, int *errcode
)
1064 struct sk_buff
*skb
;
1069 gfp_mask
= sk
->sk_allocation
;
1070 if (gfp_mask
& __GFP_WAIT
)
1071 gfp_mask
|= __GFP_REPEAT
;
1073 timeo
= sock_sndtimeo(sk
, noblock
);
1075 err
= sock_error(sk
);
1080 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
1083 if (atomic_read(&sk
->sk_wmem_alloc
) < sk
->sk_sndbuf
) {
1084 skb
= alloc_skb(header_len
, sk
->sk_allocation
);
1089 /* No pages, we're done... */
1093 npages
= (data_len
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
1094 skb
->truesize
+= data_len
;
1095 skb_shinfo(skb
)->nr_frags
= npages
;
1096 for (i
= 0; i
< npages
; i
++) {
1100 page
= alloc_pages(sk
->sk_allocation
, 0);
1103 skb_shinfo(skb
)->nr_frags
= i
;
1108 frag
= &skb_shinfo(skb
)->frags
[i
];
1110 frag
->page_offset
= 0;
1111 frag
->size
= (data_len
>= PAGE_SIZE
?
1114 data_len
-= PAGE_SIZE
;
1117 /* Full success... */
1123 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
1124 set_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
1128 if (signal_pending(current
))
1130 timeo
= sock_wait_for_wmem(sk
, timeo
);
1133 skb_set_owner_w(skb
, sk
);
1137 err
= sock_intr_errno(timeo
);
1143 struct sk_buff
*sock_alloc_send_skb(struct sock
*sk
, unsigned long size
,
1144 int noblock
, int *errcode
)
1146 return sock_alloc_send_pskb(sk
, size
, 0, noblock
, errcode
);
1149 static void __lock_sock(struct sock
*sk
)
1154 prepare_to_wait_exclusive(&sk
->sk_lock
.wq
, &wait
,
1155 TASK_UNINTERRUPTIBLE
);
1156 spin_unlock_bh(&sk
->sk_lock
.slock
);
1158 spin_lock_bh(&sk
->sk_lock
.slock
);
1159 if(!sock_owned_by_user(sk
))
1162 finish_wait(&sk
->sk_lock
.wq
, &wait
);
1165 static void __release_sock(struct sock
*sk
)
1167 struct sk_buff
*skb
= sk
->sk_backlog
.head
;
1170 sk
->sk_backlog
.head
= sk
->sk_backlog
.tail
= NULL
;
1174 struct sk_buff
*next
= skb
->next
;
1177 sk
->sk_backlog_rcv(sk
, skb
);
1180 * We are in process context here with softirqs
1181 * disabled, use cond_resched_softirq() to preempt.
1182 * This is safe to do because we've taken the backlog
1185 cond_resched_softirq();
1188 } while (skb
!= NULL
);
1191 } while((skb
= sk
->sk_backlog
.head
) != NULL
);
1195 * sk_wait_data - wait for data to arrive at sk_receive_queue
1196 * @sk: sock to wait on
1197 * @timeo: for how long
1199 * Now socket state including sk->sk_err is changed only under lock,
1200 * hence we may omit checks after joining wait queue.
1201 * We check receive queue before schedule() only as optimization;
1202 * it is very likely that release_sock() added new data.
1204 int sk_wait_data(struct sock
*sk
, long *timeo
)
1209 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
1210 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
1211 rc
= sk_wait_event(sk
, timeo
, !skb_queue_empty(&sk
->sk_receive_queue
));
1212 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
1213 finish_wait(sk
->sk_sleep
, &wait
);
1217 EXPORT_SYMBOL(sk_wait_data
);
1220 * Set of default routines for initialising struct proto_ops when
1221 * the protocol does not support a particular function. In certain
1222 * cases where it makes no sense for a protocol to have a "do nothing"
1223 * function, some default processing is provided.
1226 int sock_no_bind(struct socket
*sock
, struct sockaddr
*saddr
, int len
)
1231 int sock_no_connect(struct socket
*sock
, struct sockaddr
*saddr
,
1237 int sock_no_socketpair(struct socket
*sock1
, struct socket
*sock2
)
1242 int sock_no_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
1247 int sock_no_getname(struct socket
*sock
, struct sockaddr
*saddr
,
1253 unsigned int sock_no_poll(struct file
* file
, struct socket
*sock
, poll_table
*pt
)
1258 int sock_no_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1263 int sock_no_listen(struct socket
*sock
, int backlog
)
1268 int sock_no_shutdown(struct socket
*sock
, int how
)
1273 int sock_no_setsockopt(struct socket
*sock
, int level
, int optname
,
1274 char __user
*optval
, int optlen
)
1279 int sock_no_getsockopt(struct socket
*sock
, int level
, int optname
,
1280 char __user
*optval
, int __user
*optlen
)
1285 int sock_no_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*m
,
1291 int sock_no_recvmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*m
,
1292 size_t len
, int flags
)
1297 int sock_no_mmap(struct file
*file
, struct socket
*sock
, struct vm_area_struct
*vma
)
1299 /* Mirror missing mmap method error code */
1303 ssize_t
sock_no_sendpage(struct socket
*sock
, struct page
*page
, int offset
, size_t size
, int flags
)
1306 struct msghdr msg
= {.msg_flags
= flags
};
1308 char *kaddr
= kmap(page
);
1309 iov
.iov_base
= kaddr
+ offset
;
1311 res
= kernel_sendmsg(sock
, &msg
, &iov
, 1, size
);
1317 * Default Socket Callbacks
1320 static void sock_def_wakeup(struct sock
*sk
)
1322 read_lock(&sk
->sk_callback_lock
);
1323 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
1324 wake_up_interruptible_all(sk
->sk_sleep
);
1325 read_unlock(&sk
->sk_callback_lock
);
1328 static void sock_def_error_report(struct sock
*sk
)
1330 read_lock(&sk
->sk_callback_lock
);
1331 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
1332 wake_up_interruptible(sk
->sk_sleep
);
1333 sk_wake_async(sk
,0,POLL_ERR
);
1334 read_unlock(&sk
->sk_callback_lock
);
1337 static void sock_def_readable(struct sock
*sk
, int len
)
1339 read_lock(&sk
->sk_callback_lock
);
1340 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
1341 wake_up_interruptible(sk
->sk_sleep
);
1342 sk_wake_async(sk
,1,POLL_IN
);
1343 read_unlock(&sk
->sk_callback_lock
);
1346 static void sock_def_write_space(struct sock
*sk
)
1348 read_lock(&sk
->sk_callback_lock
);
1350 /* Do not wake up a writer until he can make "significant"
1353 if((atomic_read(&sk
->sk_wmem_alloc
) << 1) <= sk
->sk_sndbuf
) {
1354 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
1355 wake_up_interruptible(sk
->sk_sleep
);
1357 /* Should agree with poll, otherwise some programs break */
1358 if (sock_writeable(sk
))
1359 sk_wake_async(sk
, 2, POLL_OUT
);
1362 read_unlock(&sk
->sk_callback_lock
);
1365 static void sock_def_destruct(struct sock
*sk
)
1367 kfree(sk
->sk_protinfo
);
1370 void sk_send_sigurg(struct sock
*sk
)
1372 if (sk
->sk_socket
&& sk
->sk_socket
->file
)
1373 if (send_sigurg(&sk
->sk_socket
->file
->f_owner
))
1374 sk_wake_async(sk
, 3, POLL_PRI
);
1377 void sk_reset_timer(struct sock
*sk
, struct timer_list
* timer
,
1378 unsigned long expires
)
1380 if (!mod_timer(timer
, expires
))
1384 EXPORT_SYMBOL(sk_reset_timer
);
1386 void sk_stop_timer(struct sock
*sk
, struct timer_list
* timer
)
1388 if (timer_pending(timer
) && del_timer(timer
))
1392 EXPORT_SYMBOL(sk_stop_timer
);
1394 void sock_init_data(struct socket
*sock
, struct sock
*sk
)
1396 skb_queue_head_init(&sk
->sk_receive_queue
);
1397 skb_queue_head_init(&sk
->sk_write_queue
);
1398 skb_queue_head_init(&sk
->sk_error_queue
);
1399 #ifdef CONFIG_NET_DMA
1400 skb_queue_head_init(&sk
->sk_async_wait_queue
);
1403 sk
->sk_send_head
= NULL
;
1405 init_timer(&sk
->sk_timer
);
1407 sk
->sk_allocation
= GFP_KERNEL
;
1408 sk
->sk_rcvbuf
= sysctl_rmem_default
;
1409 sk
->sk_sndbuf
= sysctl_wmem_default
;
1410 sk
->sk_state
= TCP_CLOSE
;
1411 sk
->sk_socket
= sock
;
1413 sock_set_flag(sk
, SOCK_ZAPPED
);
1417 sk
->sk_type
= sock
->type
;
1418 sk
->sk_sleep
= &sock
->wait
;
1421 sk
->sk_sleep
= NULL
;
1423 rwlock_init(&sk
->sk_dst_lock
);
1424 rwlock_init(&sk
->sk_callback_lock
);
1426 sk
->sk_state_change
= sock_def_wakeup
;
1427 sk
->sk_data_ready
= sock_def_readable
;
1428 sk
->sk_write_space
= sock_def_write_space
;
1429 sk
->sk_error_report
= sock_def_error_report
;
1430 sk
->sk_destruct
= sock_def_destruct
;
1432 sk
->sk_sndmsg_page
= NULL
;
1433 sk
->sk_sndmsg_off
= 0;
1435 sk
->sk_peercred
.pid
= 0;
1436 sk
->sk_peercred
.uid
= -1;
1437 sk
->sk_peercred
.gid
= -1;
1438 sk
->sk_write_pending
= 0;
1439 sk
->sk_rcvlowat
= 1;
1440 sk
->sk_rcvtimeo
= MAX_SCHEDULE_TIMEOUT
;
1441 sk
->sk_sndtimeo
= MAX_SCHEDULE_TIMEOUT
;
1443 sk
->sk_stamp
.tv_sec
= -1L;
1444 sk
->sk_stamp
.tv_usec
= -1L;
1446 atomic_set(&sk
->sk_refcnt
, 1);
1449 void fastcall
lock_sock(struct sock
*sk
)
1452 spin_lock_bh(&(sk
->sk_lock
.slock
));
1453 if (sk
->sk_lock
.owner
)
1455 sk
->sk_lock
.owner
= (void *)1;
1456 spin_unlock_bh(&(sk
->sk_lock
.slock
));
1459 EXPORT_SYMBOL(lock_sock
);
1461 void fastcall
release_sock(struct sock
*sk
)
1463 spin_lock_bh(&(sk
->sk_lock
.slock
));
1464 if (sk
->sk_backlog
.tail
)
1466 sk
->sk_lock
.owner
= NULL
;
1467 if (waitqueue_active(&(sk
->sk_lock
.wq
)))
1468 wake_up(&(sk
->sk_lock
.wq
));
1469 spin_unlock_bh(&(sk
->sk_lock
.slock
));
1471 EXPORT_SYMBOL(release_sock
);
1473 int sock_get_timestamp(struct sock
*sk
, struct timeval __user
*userstamp
)
1475 if (!sock_flag(sk
, SOCK_TIMESTAMP
))
1476 sock_enable_timestamp(sk
);
1477 if (sk
->sk_stamp
.tv_sec
== -1)
1479 if (sk
->sk_stamp
.tv_sec
== 0)
1480 do_gettimeofday(&sk
->sk_stamp
);
1481 return copy_to_user(userstamp
, &sk
->sk_stamp
, sizeof(struct timeval
)) ?
1484 EXPORT_SYMBOL(sock_get_timestamp
);
1486 void sock_enable_timestamp(struct sock
*sk
)
1488 if (!sock_flag(sk
, SOCK_TIMESTAMP
)) {
1489 sock_set_flag(sk
, SOCK_TIMESTAMP
);
1490 net_enable_timestamp();
1493 EXPORT_SYMBOL(sock_enable_timestamp
);
1496 * Get a socket option on an socket.
1498 * FIX: POSIX 1003.1g is very ambiguous here. It states that
1499 * asynchronous errors should be reported by getsockopt. We assume
1500 * this means if you specify SO_ERROR (otherwise whats the point of it).
1502 int sock_common_getsockopt(struct socket
*sock
, int level
, int optname
,
1503 char __user
*optval
, int __user
*optlen
)
1505 struct sock
*sk
= sock
->sk
;
1507 return sk
->sk_prot
->getsockopt(sk
, level
, optname
, optval
, optlen
);
1510 EXPORT_SYMBOL(sock_common_getsockopt
);
1512 #ifdef CONFIG_COMPAT
1513 int compat_sock_common_getsockopt(struct socket
*sock
, int level
, int optname
,
1514 char __user
*optval
, int __user
*optlen
)
1516 struct sock
*sk
= sock
->sk
;
1518 if (sk
->sk_prot
->compat_setsockopt
!= NULL
)
1519 return sk
->sk_prot
->compat_getsockopt(sk
, level
, optname
,
1521 return sk
->sk_prot
->getsockopt(sk
, level
, optname
, optval
, optlen
);
1523 EXPORT_SYMBOL(compat_sock_common_getsockopt
);
1526 int sock_common_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1527 struct msghdr
*msg
, size_t size
, int flags
)
1529 struct sock
*sk
= sock
->sk
;
1533 err
= sk
->sk_prot
->recvmsg(iocb
, sk
, msg
, size
, flags
& MSG_DONTWAIT
,
1534 flags
& ~MSG_DONTWAIT
, &addr_len
);
1536 msg
->msg_namelen
= addr_len
;
1540 EXPORT_SYMBOL(sock_common_recvmsg
);
1543 * Set socket options on an inet socket.
1545 int sock_common_setsockopt(struct socket
*sock
, int level
, int optname
,
1546 char __user
*optval
, int optlen
)
1548 struct sock
*sk
= sock
->sk
;
1550 return sk
->sk_prot
->setsockopt(sk
, level
, optname
, optval
, optlen
);
1553 EXPORT_SYMBOL(sock_common_setsockopt
);
1555 #ifdef CONFIG_COMPAT
1556 int compat_sock_common_setsockopt(struct socket
*sock
, int level
, int optname
,
1557 char __user
*optval
, int optlen
)
1559 struct sock
*sk
= sock
->sk
;
1561 if (sk
->sk_prot
->compat_setsockopt
!= NULL
)
1562 return sk
->sk_prot
->compat_setsockopt(sk
, level
, optname
,
1564 return sk
->sk_prot
->setsockopt(sk
, level
, optname
, optval
, optlen
);
1566 EXPORT_SYMBOL(compat_sock_common_setsockopt
);
1569 void sk_common_release(struct sock
*sk
)
1571 if (sk
->sk_prot
->destroy
)
1572 sk
->sk_prot
->destroy(sk
);
1575 * Observation: when sock_common_release is called, processes have
1576 * no access to socket. But net still has.
1577 * Step one, detach it from networking:
1579 * A. Remove from hash tables.
1582 sk
->sk_prot
->unhash(sk
);
1585 * In this point socket cannot receive new packets, but it is possible
1586 * that some packets are in flight because some CPU runs receiver and
1587 * did hash table lookup before we unhashed socket. They will achieve
1588 * receive queue and will be purged by socket destructor.
1590 * Also we still have packets pending on receive queue and probably,
1591 * our own packets waiting in device queues. sock_destroy will drain
1592 * receive queue, but transmitted packets will delay socket destruction
1593 * until the last reference will be released.
1598 xfrm_sk_free_policy(sk
);
1600 sk_refcnt_debug_release(sk
);
1604 EXPORT_SYMBOL(sk_common_release
);
1606 static DEFINE_RWLOCK(proto_list_lock
);
1607 static LIST_HEAD(proto_list
);
1609 int proto_register(struct proto
*prot
, int alloc_slab
)
1611 char *request_sock_slab_name
= NULL
;
1612 char *timewait_sock_slab_name
;
1616 prot
->slab
= kmem_cache_create(prot
->name
, prot
->obj_size
, 0,
1617 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
1619 if (prot
->slab
== NULL
) {
1620 printk(KERN_CRIT
"%s: Can't create sock SLAB cache!\n",
1625 if (prot
->rsk_prot
!= NULL
) {
1626 static const char mask
[] = "request_sock_%s";
1628 request_sock_slab_name
= kmalloc(strlen(prot
->name
) + sizeof(mask
) - 1, GFP_KERNEL
);
1629 if (request_sock_slab_name
== NULL
)
1630 goto out_free_sock_slab
;
1632 sprintf(request_sock_slab_name
, mask
, prot
->name
);
1633 prot
->rsk_prot
->slab
= kmem_cache_create(request_sock_slab_name
,
1634 prot
->rsk_prot
->obj_size
, 0,
1635 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
1637 if (prot
->rsk_prot
->slab
== NULL
) {
1638 printk(KERN_CRIT
"%s: Can't create request sock SLAB cache!\n",
1640 goto out_free_request_sock_slab_name
;
1644 if (prot
->twsk_prot
!= NULL
) {
1645 static const char mask
[] = "tw_sock_%s";
1647 timewait_sock_slab_name
= kmalloc(strlen(prot
->name
) + sizeof(mask
) - 1, GFP_KERNEL
);
1649 if (timewait_sock_slab_name
== NULL
)
1650 goto out_free_request_sock_slab
;
1652 sprintf(timewait_sock_slab_name
, mask
, prot
->name
);
1653 prot
->twsk_prot
->twsk_slab
=
1654 kmem_cache_create(timewait_sock_slab_name
,
1655 prot
->twsk_prot
->twsk_obj_size
,
1656 0, SLAB_HWCACHE_ALIGN
,
1658 if (prot
->twsk_prot
->twsk_slab
== NULL
)
1659 goto out_free_timewait_sock_slab_name
;
1663 write_lock(&proto_list_lock
);
1664 list_add(&prot
->node
, &proto_list
);
1665 write_unlock(&proto_list_lock
);
1669 out_free_timewait_sock_slab_name
:
1670 kfree(timewait_sock_slab_name
);
1671 out_free_request_sock_slab
:
1672 if (prot
->rsk_prot
&& prot
->rsk_prot
->slab
) {
1673 kmem_cache_destroy(prot
->rsk_prot
->slab
);
1674 prot
->rsk_prot
->slab
= NULL
;
1676 out_free_request_sock_slab_name
:
1677 kfree(request_sock_slab_name
);
1679 kmem_cache_destroy(prot
->slab
);
1684 EXPORT_SYMBOL(proto_register
);
1686 void proto_unregister(struct proto
*prot
)
1688 write_lock(&proto_list_lock
);
1689 list_del(&prot
->node
);
1690 write_unlock(&proto_list_lock
);
1692 if (prot
->slab
!= NULL
) {
1693 kmem_cache_destroy(prot
->slab
);
1697 if (prot
->rsk_prot
!= NULL
&& prot
->rsk_prot
->slab
!= NULL
) {
1698 const char *name
= kmem_cache_name(prot
->rsk_prot
->slab
);
1700 kmem_cache_destroy(prot
->rsk_prot
->slab
);
1702 prot
->rsk_prot
->slab
= NULL
;
1705 if (prot
->twsk_prot
!= NULL
&& prot
->twsk_prot
->twsk_slab
!= NULL
) {
1706 const char *name
= kmem_cache_name(prot
->twsk_prot
->twsk_slab
);
1708 kmem_cache_destroy(prot
->twsk_prot
->twsk_slab
);
1710 prot
->twsk_prot
->twsk_slab
= NULL
;
1714 EXPORT_SYMBOL(proto_unregister
);
1716 #ifdef CONFIG_PROC_FS
1717 static inline struct proto
*__proto_head(void)
1719 return list_entry(proto_list
.next
, struct proto
, node
);
1722 static inline struct proto
*proto_head(void)
1724 return list_empty(&proto_list
) ? NULL
: __proto_head();
1727 static inline struct proto
*proto_next(struct proto
*proto
)
1729 return proto
->node
.next
== &proto_list
? NULL
:
1730 list_entry(proto
->node
.next
, struct proto
, node
);
1733 static inline struct proto
*proto_get_idx(loff_t pos
)
1735 struct proto
*proto
;
1738 list_for_each_entry(proto
, &proto_list
, node
)
1747 static void *proto_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1749 read_lock(&proto_list_lock
);
1750 return *pos
? proto_get_idx(*pos
- 1) : SEQ_START_TOKEN
;
1753 static void *proto_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1756 return v
== SEQ_START_TOKEN
? proto_head() : proto_next(v
);
1759 static void proto_seq_stop(struct seq_file
*seq
, void *v
)
1761 read_unlock(&proto_list_lock
);
1764 static char proto_method_implemented(const void *method
)
1766 return method
== NULL
? 'n' : 'y';
1769 static void proto_seq_printf(struct seq_file
*seq
, struct proto
*proto
)
1771 seq_printf(seq
, "%-9s %4u %6d %6d %-3s %6u %-3s %-10s "
1772 "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
1775 proto
->sockets_allocated
!= NULL
? atomic_read(proto
->sockets_allocated
) : -1,
1776 proto
->memory_allocated
!= NULL
? atomic_read(proto
->memory_allocated
) : -1,
1777 proto
->memory_pressure
!= NULL
? *proto
->memory_pressure
? "yes" : "no" : "NI",
1779 proto
->slab
== NULL
? "no" : "yes",
1780 module_name(proto
->owner
),
1781 proto_method_implemented(proto
->close
),
1782 proto_method_implemented(proto
->connect
),
1783 proto_method_implemented(proto
->disconnect
),
1784 proto_method_implemented(proto
->accept
),
1785 proto_method_implemented(proto
->ioctl
),
1786 proto_method_implemented(proto
->init
),
1787 proto_method_implemented(proto
->destroy
),
1788 proto_method_implemented(proto
->shutdown
),
1789 proto_method_implemented(proto
->setsockopt
),
1790 proto_method_implemented(proto
->getsockopt
),
1791 proto_method_implemented(proto
->sendmsg
),
1792 proto_method_implemented(proto
->recvmsg
),
1793 proto_method_implemented(proto
->sendpage
),
1794 proto_method_implemented(proto
->bind
),
1795 proto_method_implemented(proto
->backlog_rcv
),
1796 proto_method_implemented(proto
->hash
),
1797 proto_method_implemented(proto
->unhash
),
1798 proto_method_implemented(proto
->get_port
),
1799 proto_method_implemented(proto
->enter_memory_pressure
));
1802 static int proto_seq_show(struct seq_file
*seq
, void *v
)
1804 if (v
== SEQ_START_TOKEN
)
1805 seq_printf(seq
, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
1814 "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
1816 proto_seq_printf(seq
, v
);
1820 static struct seq_operations proto_seq_ops
= {
1821 .start
= proto_seq_start
,
1822 .next
= proto_seq_next
,
1823 .stop
= proto_seq_stop
,
1824 .show
= proto_seq_show
,
1827 static int proto_seq_open(struct inode
*inode
, struct file
*file
)
1829 return seq_open(file
, &proto_seq_ops
);
1832 static struct file_operations proto_seq_fops
= {
1833 .owner
= THIS_MODULE
,
1834 .open
= proto_seq_open
,
1836 .llseek
= seq_lseek
,
1837 .release
= seq_release
,
1840 static int __init
proto_init(void)
1842 /* register /proc/net/protocols */
1843 return proc_net_fops_create("protocols", S_IRUGO
, &proto_seq_fops
) == NULL
? -ENOBUFS
: 0;
1846 subsys_initcall(proto_init
);
1848 #endif /* PROC_FS */
1850 EXPORT_SYMBOL(sk_alloc
);
1851 EXPORT_SYMBOL(sk_free
);
1852 EXPORT_SYMBOL(sk_send_sigurg
);
1853 EXPORT_SYMBOL(sock_alloc_send_skb
);
1854 EXPORT_SYMBOL(sock_init_data
);
1855 EXPORT_SYMBOL(sock_kfree_s
);
1856 EXPORT_SYMBOL(sock_kmalloc
);
1857 EXPORT_SYMBOL(sock_no_accept
);
1858 EXPORT_SYMBOL(sock_no_bind
);
1859 EXPORT_SYMBOL(sock_no_connect
);
1860 EXPORT_SYMBOL(sock_no_getname
);
1861 EXPORT_SYMBOL(sock_no_getsockopt
);
1862 EXPORT_SYMBOL(sock_no_ioctl
);
1863 EXPORT_SYMBOL(sock_no_listen
);
1864 EXPORT_SYMBOL(sock_no_mmap
);
1865 EXPORT_SYMBOL(sock_no_poll
);
1866 EXPORT_SYMBOL(sock_no_recvmsg
);
1867 EXPORT_SYMBOL(sock_no_sendmsg
);
1868 EXPORT_SYMBOL(sock_no_sendpage
);
1869 EXPORT_SYMBOL(sock_no_setsockopt
);
1870 EXPORT_SYMBOL(sock_no_shutdown
);
1871 EXPORT_SYMBOL(sock_no_socketpair
);
1872 EXPORT_SYMBOL(sock_rfree
);
1873 EXPORT_SYMBOL(sock_setsockopt
);
1874 EXPORT_SYMBOL(sock_wfree
);
1875 EXPORT_SYMBOL(sock_wmalloc
);
1876 EXPORT_SYMBOL(sock_i_uid
);
1877 EXPORT_SYMBOL(sock_i_ino
);
1878 EXPORT_SYMBOL(sysctl_optmem_max
);
1879 #ifdef CONFIG_SYSCTL
1880 EXPORT_SYMBOL(sysctl_rmem_max
);
1881 EXPORT_SYMBOL(sysctl_wmem_max
);