blob: 6a871b8fdd20eacab1ba49afb68c02e804e95e8f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Jesper Juhl02c30a82005-05-05 16:16:16 -070010 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Florian La Roche, <flla@stud.uni-sb.de>
13 * Alan Cox, <A.Cox@swansea.ac.uk>
14 *
15 * Fixes:
16 * Alan Cox : Numerous verify_area() problems
17 * Alan Cox : Connecting on a connecting socket
18 * now returns an error for tcp.
19 * Alan Cox : sock->protocol is set correctly.
20 * and is not sometimes left as 0.
21 * Alan Cox : connect handles icmp errors on a
22 * connect properly. Unfortunately there
23 * is a restart syscall nasty there. I
24 * can't match BSD without hacking the C
25 * library. Ideas urgently sought!
26 * Alan Cox : Disallow bind() to addresses that are
27 * not ours - especially broadcast ones!!
28 * Alan Cox : Socket 1024 _IS_ ok for users. (fencepost)
29 * Alan Cox : sock_wfree/sock_rfree don't destroy sockets,
30 * instead they leave that for the DESTROY timer.
31 * Alan Cox : Clean up error flag in accept
32 * Alan Cox : TCP ack handling is buggy, the DESTROY timer
33 * was buggy. Put a remove_sock() in the handler
34 * for memory when we hit 0. Also altered the timer
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090035 * code. The ACK stuff can wait and needs major
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * TCP layer surgery.
37 * Alan Cox : Fixed TCP ack bug, removed remove sock
38 * and fixed timer/inet_bh race.
39 * Alan Cox : Added zapped flag for TCP
40 * Alan Cox : Move kfree_skb into skbuff.c and tidied up surplus code
41 * Alan Cox : for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
42 * Alan Cox : kfree_s calls now are kfree_skbmem so we can track skb resources
43 * Alan Cox : Supports socket option broadcast now as does udp. Packet and raw need fixing.
44 * Alan Cox : Added RCVBUF,SNDBUF size setting. It suddenly occurred to me how easy it was so...
45 * Rick Sladkey : Relaxed UDP rules for matching packets.
46 * C.E.Hawkins : IFF_PROMISC/SIOCGHWADDR support
47 * Pauline Middelink : identd support
48 * Alan Cox : Fixed connect() taking signals I think.
49 * Alan Cox : SO_LINGER supported
50 * Alan Cox : Error reporting fixes
51 * Anonymous : inet_create tidied up (sk->reuse setting)
52 * Alan Cox : inet sockets don't set sk->type!
53 * Alan Cox : Split socket option code
54 * Alan Cox : Callbacks
55 * Alan Cox : Nagle flag for Charles & Johannes stuff
56 * Alex : Removed restriction on inet fioctl
57 * Alan Cox : Splitting INET from NET core
58 * Alan Cox : Fixed bogus SO_TYPE handling in getsockopt()
59 * Adam Caldwell : Missing return in SO_DONTROUTE/SO_DEBUG code
60 * Alan Cox : Split IP from generic code
61 * Alan Cox : New kfree_skbmem()
62 * Alan Cox : Make SO_DEBUG superuser only.
63 * Alan Cox : Allow anyone to clear SO_DEBUG
64 * (compatibility fix)
65 * Alan Cox : Added optimistic memory grabbing for AF_UNIX throughput.
66 * Alan Cox : Allocator for a socket is settable.
67 * Alan Cox : SO_ERROR includes soft errors.
68 * Alan Cox : Allow NULL arguments on some SO_ opts
69 * Alan Cox : Generic socket allocation to make hooks
70 * easier (suggested by Craig Metz).
71 * Michael Pall : SO_ERROR returns positive errno again
72 * Steve Whitehouse: Added default destructor to free
73 * protocol private data.
74 * Steve Whitehouse: Added various other default routines
75 * common to several socket families.
76 * Chris Evans : Call suser() check last on F_SETOWN
77 * Jay Schulist : Added SO_ATTACH_FILTER and SO_DETACH_FILTER.
78 * Andi Kleen : Add sock_kmalloc()/sock_kfree_s()
79 * Andi Kleen : Fix write_space callback
80 * Chris Evans : Security fixes - signedness again
81 * Arnaldo C. Melo : cleanups, use skb_queue_purge
82 *
83 * To Fix:
84 *
85 *
86 * This program is free software; you can redistribute it and/or
87 * modify it under the terms of the GNU General Public License
88 * as published by the Free Software Foundation; either version
89 * 2 of the License, or (at your option) any later version.
90 */
91
Randy Dunlap4fc268d2006-01-11 12:17:47 -080092#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/errno.h>
94#include <linux/types.h>
95#include <linux/socket.h>
96#include <linux/in.h>
97#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/module.h>
99#include <linux/proc_fs.h>
100#include <linux/seq_file.h>
101#include <linux/sched.h>
102#include <linux/timer.h>
103#include <linux/string.h>
104#include <linux/sockios.h>
105#include <linux/net.h>
106#include <linux/mm.h>
107#include <linux/slab.h>
108#include <linux/interrupt.h>
109#include <linux/poll.h>
110#include <linux/tcp.h>
111#include <linux/init.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -0400112#include <linux/highmem.h>
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000113#include <linux/user_namespace.h>
Glauber Costae1aab162011-12-11 21:47:03 +0000114#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#include <asm/uaccess.h>
117#include <asm/system.h>
118
119#include <linux/netdevice.h>
120#include <net/protocol.h>
121#include <linux/skbuff.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200122#include <net/net_namespace.h>
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700123#include <net/request_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <net/sock.h>
Patrick Ohly20d49472009-02-12 05:03:38 +0000125#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#include <net/xfrm.h>
127#include <linux/ipsec.h>
Herbert Xuf8451722010-05-24 00:12:34 -0700128#include <net/cls_cgroup.h>
Neil Horman5bc14212011-11-22 05:10:51 +0000129#include <net/netprio_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131#include <linux/filter.h>
132
Satoru Moriya3847ce32011-06-17 12:00:03 +0000133#include <trace/events/sock.h>
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#ifdef CONFIG_INET
136#include <net/tcp.h>
137#endif
138
Ingo Molnarda21f242006-07-03 00:25:12 -0700139/*
140 * Each address family might have different locking rules, so we have
141 * one slock key per address family:
142 */
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700143static struct lock_class_key af_family_keys[AF_MAX];
144static struct lock_class_key af_family_slock_keys[AF_MAX];
145
Glauber Costae1aab162011-12-11 21:47:03 +0000146struct jump_label_key memcg_socket_limit_enabled;
147EXPORT_SYMBOL(memcg_socket_limit_enabled);
148
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700149/*
150 * Make lock validator output more readable. (we pre-construct these
151 * strings build-time, so that runtime initialization of socket
152 * locks is fast):
153 */
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700154static const char *const af_family_key_strings[AF_MAX+1] = {
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700155 "sk_lock-AF_UNSPEC", "sk_lock-AF_UNIX" , "sk_lock-AF_INET" ,
156 "sk_lock-AF_AX25" , "sk_lock-AF_IPX" , "sk_lock-AF_APPLETALK",
157 "sk_lock-AF_NETROM", "sk_lock-AF_BRIDGE" , "sk_lock-AF_ATMPVC" ,
158 "sk_lock-AF_X25" , "sk_lock-AF_INET6" , "sk_lock-AF_ROSE" ,
159 "sk_lock-AF_DECnet", "sk_lock-AF_NETBEUI" , "sk_lock-AF_SECURITY" ,
160 "sk_lock-AF_KEY" , "sk_lock-AF_NETLINK" , "sk_lock-AF_PACKET" ,
161 "sk_lock-AF_ASH" , "sk_lock-AF_ECONET" , "sk_lock-AF_ATMSVC" ,
Andy Grovercbd151b2009-02-26 23:43:19 -0800162 "sk_lock-AF_RDS" , "sk_lock-AF_SNA" , "sk_lock-AF_IRDA" ,
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700163 "sk_lock-AF_PPPOX" , "sk_lock-AF_WANPIPE" , "sk_lock-AF_LLC" ,
Oliver Hartkoppcd05acf2007-12-16 15:59:24 -0800164 "sk_lock-27" , "sk_lock-28" , "sk_lock-AF_CAN" ,
David Howells17926a72007-04-26 15:48:28 -0700165 "sk_lock-AF_TIPC" , "sk_lock-AF_BLUETOOTH", "sk_lock-IUCV" ,
Remi Denis-Courmontbce7b152008-09-22 19:51:15 -0700166 "sk_lock-AF_RXRPC" , "sk_lock-AF_ISDN" , "sk_lock-AF_PHONET" ,
Miloslav Trmač6f107b52010-12-08 14:35:34 +0800167 "sk_lock-AF_IEEE802154", "sk_lock-AF_CAIF" , "sk_lock-AF_ALG" ,
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300168 "sk_lock-AF_NFC" , "sk_lock-AF_MAX"
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700169};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700170static const char *const af_family_slock_key_strings[AF_MAX+1] = {
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700171 "slock-AF_UNSPEC", "slock-AF_UNIX" , "slock-AF_INET" ,
172 "slock-AF_AX25" , "slock-AF_IPX" , "slock-AF_APPLETALK",
173 "slock-AF_NETROM", "slock-AF_BRIDGE" , "slock-AF_ATMPVC" ,
174 "slock-AF_X25" , "slock-AF_INET6" , "slock-AF_ROSE" ,
175 "slock-AF_DECnet", "slock-AF_NETBEUI" , "slock-AF_SECURITY" ,
176 "slock-AF_KEY" , "slock-AF_NETLINK" , "slock-AF_PACKET" ,
177 "slock-AF_ASH" , "slock-AF_ECONET" , "slock-AF_ATMSVC" ,
Andy Grovercbd151b2009-02-26 23:43:19 -0800178 "slock-AF_RDS" , "slock-AF_SNA" , "slock-AF_IRDA" ,
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700179 "slock-AF_PPPOX" , "slock-AF_WANPIPE" , "slock-AF_LLC" ,
Oliver Hartkoppcd05acf2007-12-16 15:59:24 -0800180 "slock-27" , "slock-28" , "slock-AF_CAN" ,
David Howells17926a72007-04-26 15:48:28 -0700181 "slock-AF_TIPC" , "slock-AF_BLUETOOTH", "slock-AF_IUCV" ,
Remi Denis-Courmontbce7b152008-09-22 19:51:15 -0700182 "slock-AF_RXRPC" , "slock-AF_ISDN" , "slock-AF_PHONET" ,
Miloslav Trmač6f107b52010-12-08 14:35:34 +0800183 "slock-AF_IEEE802154", "slock-AF_CAIF" , "slock-AF_ALG" ,
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300184 "slock-AF_NFC" , "slock-AF_MAX"
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700185};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700186static const char *const af_family_clock_key_strings[AF_MAX+1] = {
Peter Zijlstra443aef0e2007-07-19 01:49:00 -0700187 "clock-AF_UNSPEC", "clock-AF_UNIX" , "clock-AF_INET" ,
188 "clock-AF_AX25" , "clock-AF_IPX" , "clock-AF_APPLETALK",
189 "clock-AF_NETROM", "clock-AF_BRIDGE" , "clock-AF_ATMPVC" ,
190 "clock-AF_X25" , "clock-AF_INET6" , "clock-AF_ROSE" ,
191 "clock-AF_DECnet", "clock-AF_NETBEUI" , "clock-AF_SECURITY" ,
192 "clock-AF_KEY" , "clock-AF_NETLINK" , "clock-AF_PACKET" ,
193 "clock-AF_ASH" , "clock-AF_ECONET" , "clock-AF_ATMSVC" ,
Andy Grovercbd151b2009-02-26 23:43:19 -0800194 "clock-AF_RDS" , "clock-AF_SNA" , "clock-AF_IRDA" ,
Peter Zijlstra443aef0e2007-07-19 01:49:00 -0700195 "clock-AF_PPPOX" , "clock-AF_WANPIPE" , "clock-AF_LLC" ,
Oliver Hartkoppb4942af2008-07-23 14:06:04 -0700196 "clock-27" , "clock-28" , "clock-AF_CAN" ,
David Howellse51f8022007-07-21 19:30:16 -0700197 "clock-AF_TIPC" , "clock-AF_BLUETOOTH", "clock-AF_IUCV" ,
Remi Denis-Courmontbce7b152008-09-22 19:51:15 -0700198 "clock-AF_RXRPC" , "clock-AF_ISDN" , "clock-AF_PHONET" ,
Miloslav Trmač6f107b52010-12-08 14:35:34 +0800199 "clock-AF_IEEE802154", "clock-AF_CAIF" , "clock-AF_ALG" ,
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300200 "clock-AF_NFC" , "clock-AF_MAX"
Peter Zijlstra443aef0e2007-07-19 01:49:00 -0700201};
Ingo Molnarda21f242006-07-03 00:25:12 -0700202
203/*
204 * sk_callback_lock locking rules are per-address-family,
205 * so split the lock classes by using a per-AF key:
206 */
207static struct lock_class_key af_callback_keys[AF_MAX];
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* Take into consideration the size of the struct sk_buff overhead in the
210 * determination of these values, since that is non-constant across
211 * platforms. This makes socket queueing behavior and performance
212 * not depend upon such differences.
213 */
214#define _SK_MEM_PACKETS 256
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000215#define _SK_MEM_OVERHEAD SKB_TRUESIZE(256)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
217#define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
218
219/* Run time adjustable parameters. */
Brian Haleyab32ea52006-09-22 14:15:41 -0700220__u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
221__u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
222__u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
223__u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300225/* Maximal space eaten by iovec or ancillary data plus some space */
Brian Haleyab32ea52006-09-22 14:15:41 -0700226int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
Eric Dumazet2a915252009-05-27 11:30:05 +0000227EXPORT_SYMBOL(sysctl_optmem_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Neil Horman5bc14212011-11-22 05:10:51 +0000229#if defined(CONFIG_CGROUPS)
230#if !defined(CONFIG_NET_CLS_CGROUP)
Herbert Xuf8451722010-05-24 00:12:34 -0700231int net_cls_subsys_id = -1;
232EXPORT_SYMBOL_GPL(net_cls_subsys_id);
233#endif
Neil Horman5bc14212011-11-22 05:10:51 +0000234#if !defined(CONFIG_NETPRIO_CGROUP)
235int net_prio_subsys_id = -1;
236EXPORT_SYMBOL_GPL(net_prio_subsys_id);
237#endif
238#endif
Herbert Xuf8451722010-05-24 00:12:34 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
241{
242 struct timeval tv;
243
244 if (optlen < sizeof(tv))
245 return -EINVAL;
246 if (copy_from_user(&tv, optval, sizeof(tv)))
247 return -EFAULT;
Vasily Averinba780732007-05-24 16:58:54 -0700248 if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
249 return -EDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Vasily Averinba780732007-05-24 16:58:54 -0700251 if (tv.tv_sec < 0) {
Andrew Morton6f11df82007-07-09 13:16:00 -0700252 static int warned __read_mostly;
253
Vasily Averinba780732007-05-24 16:58:54 -0700254 *timeo_p = 0;
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700255 if (warned < 10 && net_ratelimit()) {
Vasily Averinba780732007-05-24 16:58:54 -0700256 warned++;
257 printk(KERN_INFO "sock_set_timeout: `%s' (pid %d) "
258 "tries to set negative timeout\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700259 current->comm, task_pid_nr(current));
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700260 }
Vasily Averinba780732007-05-24 16:58:54 -0700261 return 0;
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *timeo_p = MAX_SCHEDULE_TIMEOUT;
264 if (tv.tv_sec == 0 && tv.tv_usec == 0)
265 return 0;
266 if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
267 *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
268 return 0;
269}
270
271static void sock_warn_obsolete_bsdism(const char *name)
272{
273 static int warned;
274 static char warncomm[TASK_COMM_LEN];
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900275 if (strcmp(warncomm, current->comm) && warned < 5) {
276 strcpy(warncomm, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 printk(KERN_WARNING "process `%s' is using obsolete "
278 "%s SO_BSDCOMPAT\n", warncomm, name);
279 warned++;
280 }
281}
282
Eric Dumazet08e29af2011-11-28 12:04:18 +0000283#define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE))
284
285static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900286{
Eric Dumazet08e29af2011-11-28 12:04:18 +0000287 if (sk->sk_flags & flags) {
288 sk->sk_flags &= ~flags;
289 if (!(sk->sk_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +0000290 net_disable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292}
293
294
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800295int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
296{
Eric Dumazet766e90372009-10-14 20:40:11 -0700297 int err;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800298 int skb_len;
Neil Horman3b885782009-10-12 13:26:31 -0700299 unsigned long flags;
300 struct sk_buff_head *list = &sk->sk_receive_queue;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800301
Rami Rosen9ee6b7f2008-05-14 03:50:03 -0700302 /* Cast sk->rcvbuf to unsigned... It's pointless, but reduces
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800303 number of warnings when compiling with -W --ANK
304 */
305 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
306 (unsigned)sk->sk_rcvbuf) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700307 atomic_inc(&sk->sk_drops);
Satoru Moriya3847ce32011-06-17 12:00:03 +0000308 trace_sock_rcvqueue_full(sk, skb);
Eric Dumazet766e90372009-10-14 20:40:11 -0700309 return -ENOMEM;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800310 }
311
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700312 err = sk_filter(sk, skb);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800313 if (err)
Eric Dumazet766e90372009-10-14 20:40:11 -0700314 return err;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800315
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800316 if (!sk_rmem_schedule(sk, skb->truesize)) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700317 atomic_inc(&sk->sk_drops);
318 return -ENOBUFS;
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800319 }
320
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800321 skb->dev = NULL;
322 skb_set_owner_r(skb, sk);
David S. Miller49ad9592008-12-17 22:11:38 -0800323
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800324 /* Cache the SKB length before we tack it onto the receive
325 * queue. Once it is added it no longer belongs to us and
326 * may be freed by other threads of control pulling packets
327 * from the queue.
328 */
329 skb_len = skb->len;
330
Eric Dumazet7fee2262010-05-11 23:19:48 +0000331 /* we escape from rcu protected region, make sure we dont leak
332 * a norefcounted dst
333 */
334 skb_dst_force(skb);
335
Neil Horman3b885782009-10-12 13:26:31 -0700336 spin_lock_irqsave(&list->lock, flags);
337 skb->dropcount = atomic_read(&sk->sk_drops);
338 __skb_queue_tail(list, skb);
339 spin_unlock_irqrestore(&list->lock, flags);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800340
341 if (!sock_flag(sk, SOCK_DEAD))
342 sk->sk_data_ready(sk, skb_len);
Eric Dumazet766e90372009-10-14 20:40:11 -0700343 return 0;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800344}
345EXPORT_SYMBOL(sock_queue_rcv_skb);
346
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200347int sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested)
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800348{
349 int rc = NET_RX_SUCCESS;
350
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700351 if (sk_filter(sk, skb))
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800352 goto discard_and_relse;
353
354 skb->dev = NULL;
355
Eric Dumazetc3774112010-04-27 15:13:20 -0700356 if (sk_rcvqueues_full(sk, skb)) {
357 atomic_inc(&sk->sk_drops);
358 goto discard_and_relse;
359 }
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200360 if (nested)
361 bh_lock_sock_nested(sk);
362 else
363 bh_lock_sock(sk);
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700364 if (!sock_owned_by_user(sk)) {
365 /*
366 * trylock + unlock semantics:
367 */
368 mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
369
Peter Zijlstrac57943a2008-10-07 14:18:42 -0700370 rc = sk_backlog_rcv(sk, skb);
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700371
372 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
Zhu Yia3a858f2010-03-04 18:01:47 +0000373 } else if (sk_add_backlog(sk, skb)) {
Zhu Yi8eae9392010-03-04 18:01:40 +0000374 bh_unlock_sock(sk);
375 atomic_inc(&sk->sk_drops);
376 goto discard_and_relse;
377 }
378
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800379 bh_unlock_sock(sk);
380out:
381 sock_put(sk);
382 return rc;
383discard_and_relse:
384 kfree_skb(skb);
385 goto out;
386}
387EXPORT_SYMBOL(sk_receive_skb);
388
Krishna Kumarea94ff32009-10-19 23:46:45 +0000389void sk_reset_txq(struct sock *sk)
390{
391 sk_tx_queue_clear(sk);
392}
393EXPORT_SYMBOL(sk_reset_txq);
394
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800395struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
396{
Eric Dumazetb6c67122010-04-08 23:03:29 +0000397 struct dst_entry *dst = __sk_dst_get(sk);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800398
399 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
Krishna Kumare022f0b2009-10-19 23:46:20 +0000400 sk_tx_queue_clear(sk);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000401 RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800402 dst_release(dst);
403 return NULL;
404 }
405
406 return dst;
407}
408EXPORT_SYMBOL(__sk_dst_check);
409
410struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
411{
412 struct dst_entry *dst = sk_dst_get(sk);
413
414 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
415 sk_dst_reset(sk);
416 dst_release(dst);
417 return NULL;
418 }
419
420 return dst;
421}
422EXPORT_SYMBOL(sk_dst_check);
423
David S. Miller48788092007-09-14 16:41:03 -0700424static int sock_bindtodevice(struct sock *sk, char __user *optval, int optlen)
425{
426 int ret = -ENOPROTOOPT;
427#ifdef CONFIG_NETDEVICES
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900428 struct net *net = sock_net(sk);
David S. Miller48788092007-09-14 16:41:03 -0700429 char devname[IFNAMSIZ];
430 int index;
431
432 /* Sorry... */
433 ret = -EPERM;
434 if (!capable(CAP_NET_RAW))
435 goto out;
436
437 ret = -EINVAL;
438 if (optlen < 0)
439 goto out;
440
441 /* Bind this socket to a particular device like "eth0",
442 * as specified in the passed interface name. If the
443 * name is "" or the option length is zero the socket
444 * is not bound.
445 */
446 if (optlen > IFNAMSIZ - 1)
447 optlen = IFNAMSIZ - 1;
448 memset(devname, 0, sizeof(devname));
449
450 ret = -EFAULT;
451 if (copy_from_user(devname, optval, optlen))
452 goto out;
453
David S. Miller000ba2e2009-11-05 22:37:11 -0800454 index = 0;
455 if (devname[0] != '\0') {
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800456 struct net_device *dev;
David S. Miller48788092007-09-14 16:41:03 -0700457
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800458 rcu_read_lock();
459 dev = dev_get_by_name_rcu(net, devname);
460 if (dev)
461 index = dev->ifindex;
462 rcu_read_unlock();
David S. Miller48788092007-09-14 16:41:03 -0700463 ret = -ENODEV;
464 if (!dev)
465 goto out;
David S. Miller48788092007-09-14 16:41:03 -0700466 }
467
468 lock_sock(sk);
469 sk->sk_bound_dev_if = index;
470 sk_dst_reset(sk);
471 release_sock(sk);
472
473 ret = 0;
474
475out:
476#endif
477
478 return ret;
479}
480
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800481static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
482{
483 if (valbool)
484 sock_set_flag(sk, bit);
485 else
486 sock_reset_flag(sk, bit);
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
490 * This is meant for all protocols to use and covers goings on
491 * at the socket level. Everything here is generic.
492 */
493
494int sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700495 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Eric Dumazet2a915252009-05-27 11:30:05 +0000497 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int val;
499 int valbool;
500 struct linger ling;
501 int ret = 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /*
504 * Options without arguments
505 */
506
David S. Miller48788092007-09-14 16:41:03 -0700507 if (optname == SO_BINDTODEVICE)
508 return sock_bindtodevice(sk, optval, optlen);
509
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700510 if (optlen < sizeof(int))
511 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (get_user(val, (int __user *)optval))
514 return -EFAULT;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900515
Eric Dumazet2a915252009-05-27 11:30:05 +0000516 valbool = val ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 lock_sock(sk);
519
Eric Dumazet2a915252009-05-27 11:30:05 +0000520 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700521 case SO_DEBUG:
Eric Dumazet2a915252009-05-27 11:30:05 +0000522 if (val && !capable(CAP_NET_ADMIN))
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700523 ret = -EACCES;
Eric Dumazet2a915252009-05-27 11:30:05 +0000524 else
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800525 sock_valbool_flag(sk, SOCK_DBG, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700526 break;
527 case SO_REUSEADDR:
528 sk->sk_reuse = valbool;
529 break;
530 case SO_TYPE:
Jan Engelhardt49c794e2009-08-04 07:28:28 +0000531 case SO_PROTOCOL:
Jan Engelhardt0d6038e2009-08-04 07:28:29 +0000532 case SO_DOMAIN:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700533 case SO_ERROR:
534 ret = -ENOPROTOOPT;
535 break;
536 case SO_DONTROUTE:
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800537 sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700538 break;
539 case SO_BROADCAST:
540 sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
541 break;
542 case SO_SNDBUF:
543 /* Don't error on this BSD doesn't and if you think
544 about it this is right. Otherwise apps have to
545 play 'guess the biggest size' games. RCVBUF/SNDBUF
546 are treated in BSD as hints */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900547
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700548 if (val > sysctl_wmem_max)
549 val = sysctl_wmem_max;
Patrick McHardyb0573de2005-08-09 19:30:51 -0700550set_sndbuf:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700551 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
552 if ((val * 2) < SOCK_MIN_SNDBUF)
553 sk->sk_sndbuf = SOCK_MIN_SNDBUF;
554 else
555 sk->sk_sndbuf = val * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700557 /*
558 * Wake up sending tasks if we
559 * upped the value.
560 */
561 sk->sk_write_space(sk);
562 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700564 case SO_SNDBUFFORCE:
565 if (!capable(CAP_NET_ADMIN)) {
566 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700569 goto set_sndbuf;
570
571 case SO_RCVBUF:
572 /* Don't error on this BSD doesn't and if you think
573 about it this is right. Otherwise apps have to
574 play 'guess the biggest size' games. RCVBUF/SNDBUF
575 are treated in BSD as hints */
576
577 if (val > sysctl_rmem_max)
578 val = sysctl_rmem_max;
579set_rcvbuf:
580 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
581 /*
582 * We double it on the way in to account for
583 * "struct sk_buff" etc. overhead. Applications
584 * assume that the SO_RCVBUF setting they make will
585 * allow that much actual data to be received on that
586 * socket.
587 *
588 * Applications are unaware that "struct sk_buff" and
589 * other overheads allocate from the receive buffer
590 * during socket buffer allocation.
591 *
592 * And after considering the possible alternatives,
593 * returning the value we actually used in getsockopt
594 * is the most desirable behavior.
595 */
596 if ((val * 2) < SOCK_MIN_RCVBUF)
597 sk->sk_rcvbuf = SOCK_MIN_RCVBUF;
598 else
599 sk->sk_rcvbuf = val * 2;
600 break;
601
602 case SO_RCVBUFFORCE:
603 if (!capable(CAP_NET_ADMIN)) {
604 ret = -EPERM;
605 break;
606 }
607 goto set_rcvbuf;
608
609 case SO_KEEPALIVE:
610#ifdef CONFIG_INET
611 if (sk->sk_protocol == IPPROTO_TCP)
612 tcp_set_keepalive(sk, valbool);
613#endif
614 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
615 break;
616
617 case SO_OOBINLINE:
618 sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
619 break;
620
621 case SO_NO_CHECK:
622 sk->sk_no_check = valbool;
623 break;
624
625 case SO_PRIORITY:
626 if ((val >= 0 && val <= 6) || capable(CAP_NET_ADMIN))
627 sk->sk_priority = val;
628 else
629 ret = -EPERM;
630 break;
631
632 case SO_LINGER:
633 if (optlen < sizeof(ling)) {
634 ret = -EINVAL; /* 1003.1g */
635 break;
636 }
Eric Dumazet2a915252009-05-27 11:30:05 +0000637 if (copy_from_user(&ling, optval, sizeof(ling))) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700638 ret = -EFAULT;
639 break;
640 }
641 if (!ling.l_onoff)
642 sock_reset_flag(sk, SOCK_LINGER);
643 else {
644#if (BITS_PER_LONG == 32)
645 if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
646 sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
647 else
648#endif
649 sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
650 sock_set_flag(sk, SOCK_LINGER);
651 }
652 break;
653
654 case SO_BSDCOMPAT:
655 sock_warn_obsolete_bsdism("setsockopt");
656 break;
657
658 case SO_PASSCRED:
659 if (valbool)
660 set_bit(SOCK_PASSCRED, &sock->flags);
661 else
662 clear_bit(SOCK_PASSCRED, &sock->flags);
663 break;
664
665 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700666 case SO_TIMESTAMPNS:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700667 if (valbool) {
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700668 if (optname == SO_TIMESTAMP)
669 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
670 else
671 sock_set_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700672 sock_set_flag(sk, SOCK_RCVTSTAMP);
Patrick Ohly20d49472009-02-12 05:03:38 +0000673 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700674 } else {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700675 sock_reset_flag(sk, SOCK_RCVTSTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700676 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
677 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700678 break;
679
Patrick Ohly20d49472009-02-12 05:03:38 +0000680 case SO_TIMESTAMPING:
681 if (val & ~SOF_TIMESTAMPING_MASK) {
Rémi Denis-Courmontf249fb72009-07-20 00:47:04 +0000682 ret = -EINVAL;
Patrick Ohly20d49472009-02-12 05:03:38 +0000683 break;
684 }
685 sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE,
686 val & SOF_TIMESTAMPING_TX_HARDWARE);
687 sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE,
688 val & SOF_TIMESTAMPING_TX_SOFTWARE);
689 sock_valbool_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE,
690 val & SOF_TIMESTAMPING_RX_HARDWARE);
691 if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
692 sock_enable_timestamp(sk,
693 SOCK_TIMESTAMPING_RX_SOFTWARE);
694 else
695 sock_disable_timestamp(sk,
Eric Dumazet08e29af2011-11-28 12:04:18 +0000696 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
Patrick Ohly20d49472009-02-12 05:03:38 +0000697 sock_valbool_flag(sk, SOCK_TIMESTAMPING_SOFTWARE,
698 val & SOF_TIMESTAMPING_SOFTWARE);
699 sock_valbool_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE,
700 val & SOF_TIMESTAMPING_SYS_HARDWARE);
701 sock_valbool_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE,
702 val & SOF_TIMESTAMPING_RAW_HARDWARE);
703 break;
704
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700705 case SO_RCVLOWAT:
706 if (val < 0)
707 val = INT_MAX;
708 sk->sk_rcvlowat = val ? : 1;
709 break;
710
711 case SO_RCVTIMEO:
712 ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
713 break;
714
715 case SO_SNDTIMEO:
716 ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
717 break;
718
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700719 case SO_ATTACH_FILTER:
720 ret = -EINVAL;
721 if (optlen == sizeof(struct sock_fprog)) {
722 struct sock_fprog fprog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700724 ret = -EFAULT;
725 if (copy_from_user(&fprog, optval, sizeof(fprog)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700728 ret = sk_attach_filter(&fprog, sk);
729 }
730 break;
731
732 case SO_DETACH_FILTER:
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700733 ret = sk_detach_filter(sk);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700734 break;
735
736 case SO_PASSSEC:
737 if (valbool)
738 set_bit(SOCK_PASSSEC, &sock->flags);
739 else
740 clear_bit(SOCK_PASSSEC, &sock->flags);
741 break;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800742 case SO_MARK:
743 if (!capable(CAP_NET_ADMIN))
744 ret = -EPERM;
Eric Dumazet2a915252009-05-27 11:30:05 +0000745 else
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800746 sk->sk_mark = val;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800747 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /* We implement the SO_SNDLOWAT etc to
750 not be settable (1003.1g 5.3) */
Neil Horman3b885782009-10-12 13:26:31 -0700751 case SO_RXQ_OVFL:
Johannes Berg8083f0f2011-10-07 03:30:20 +0000752 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
Neil Horman3b885782009-10-12 13:26:31 -0700753 break;
Johannes Berg6e3e9392011-11-09 10:15:42 +0100754
755 case SO_WIFI_STATUS:
756 sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
757 break;
758
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700759 default:
760 ret = -ENOPROTOOPT;
761 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 release_sock(sk);
764 return ret;
765}
Eric Dumazet2a915252009-05-27 11:30:05 +0000766EXPORT_SYMBOL(sock_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000769void cred_to_ucred(struct pid *pid, const struct cred *cred,
770 struct ucred *ucred)
771{
772 ucred->pid = pid_vnr(pid);
773 ucred->uid = ucred->gid = -1;
774 if (cred) {
775 struct user_namespace *current_ns = current_user_ns();
776
777 ucred->uid = user_ns_map_uid(current_ns, cred, cred->euid);
778 ucred->gid = user_ns_map_gid(current_ns, cred, cred->egid);
779 }
780}
David S. Miller39247732010-06-16 16:18:25 -0700781EXPORT_SYMBOL_GPL(cred_to_ucred);
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783int sock_getsockopt(struct socket *sock, int level, int optname,
784 char __user *optval, int __user *optlen)
785{
786 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900787
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700788 union {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900789 int val;
790 struct linger ling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct timeval tm;
792 } v;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900793
H Hartley Sweeten4d0392b2010-01-15 01:08:58 -0800794 int lv = sizeof(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int len;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900796
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700797 if (get_user(len, optlen))
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900798 return -EFAULT;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700799 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900801
Eugene Teo50fee1d2009-02-23 15:38:41 -0800802 memset(&v, 0, sizeof(v));
Clément Lecignedf0bca02009-02-12 16:59:09 -0800803
Eric Dumazet2a915252009-05-27 11:30:05 +0000804 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700805 case SO_DEBUG:
806 v.val = sock_flag(sk, SOCK_DBG);
807 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900808
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700809 case SO_DONTROUTE:
810 v.val = sock_flag(sk, SOCK_LOCALROUTE);
811 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900812
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700813 case SO_BROADCAST:
814 v.val = !!sock_flag(sk, SOCK_BROADCAST);
815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700817 case SO_SNDBUF:
818 v.val = sk->sk_sndbuf;
819 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900820
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700821 case SO_RCVBUF:
822 v.val = sk->sk_rcvbuf;
823 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700825 case SO_REUSEADDR:
826 v.val = sk->sk_reuse;
827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700829 case SO_KEEPALIVE:
830 v.val = !!sock_flag(sk, SOCK_KEEPOPEN);
831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700833 case SO_TYPE:
834 v.val = sk->sk_type;
835 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Jan Engelhardt49c794e2009-08-04 07:28:28 +0000837 case SO_PROTOCOL:
838 v.val = sk->sk_protocol;
839 break;
840
Jan Engelhardt0d6038e2009-08-04 07:28:29 +0000841 case SO_DOMAIN:
842 v.val = sk->sk_family;
843 break;
844
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700845 case SO_ERROR:
846 v.val = -sock_error(sk);
Eric Dumazet2a915252009-05-27 11:30:05 +0000847 if (v.val == 0)
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700848 v.val = xchg(&sk->sk_err_soft, 0);
849 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700851 case SO_OOBINLINE:
852 v.val = !!sock_flag(sk, SOCK_URGINLINE);
853 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900854
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700855 case SO_NO_CHECK:
856 v.val = sk->sk_no_check;
857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700859 case SO_PRIORITY:
860 v.val = sk->sk_priority;
861 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900862
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700863 case SO_LINGER:
864 lv = sizeof(v.ling);
865 v.ling.l_onoff = !!sock_flag(sk, SOCK_LINGER);
866 v.ling.l_linger = sk->sk_lingertime / HZ;
867 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900868
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700869 case SO_BSDCOMPAT:
870 sock_warn_obsolete_bsdism("getsockopt");
871 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700873 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700874 v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
875 !sock_flag(sk, SOCK_RCVTSTAMPNS);
876 break;
877
878 case SO_TIMESTAMPNS:
879 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700880 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Patrick Ohly20d49472009-02-12 05:03:38 +0000882 case SO_TIMESTAMPING:
883 v.val = 0;
884 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
885 v.val |= SOF_TIMESTAMPING_TX_HARDWARE;
886 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
887 v.val |= SOF_TIMESTAMPING_TX_SOFTWARE;
888 if (sock_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE))
889 v.val |= SOF_TIMESTAMPING_RX_HARDWARE;
890 if (sock_flag(sk, SOCK_TIMESTAMPING_RX_SOFTWARE))
891 v.val |= SOF_TIMESTAMPING_RX_SOFTWARE;
892 if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE))
893 v.val |= SOF_TIMESTAMPING_SOFTWARE;
894 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE))
895 v.val |= SOF_TIMESTAMPING_SYS_HARDWARE;
896 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE))
897 v.val |= SOF_TIMESTAMPING_RAW_HARDWARE;
898 break;
899
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700900 case SO_RCVTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +0000901 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700902 if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
903 v.tm.tv_sec = 0;
904 v.tm.tv_usec = 0;
905 } else {
906 v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
907 v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * 1000000) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700911 case SO_SNDTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +0000912 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700913 if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
914 v.tm.tv_sec = 0;
915 v.tm.tv_usec = 0;
916 } else {
917 v.tm.tv_sec = sk->sk_sndtimeo / HZ;
918 v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * 1000000) / HZ;
919 }
920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700922 case SO_RCVLOWAT:
923 v.val = sk->sk_rcvlowat;
924 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700925
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700926 case SO_SNDLOWAT:
Eric Dumazet2a915252009-05-27 11:30:05 +0000927 v.val = 1;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700928 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700930 case SO_PASSCRED:
931 v.val = test_bit(SOCK_PASSCRED, &sock->flags) ? 1 : 0;
932 break;
933
934 case SO_PEERCRED:
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000935 {
936 struct ucred peercred;
937 if (len > sizeof(peercred))
938 len = sizeof(peercred);
939 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
940 if (copy_to_user(optval, &peercred, len))
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700941 return -EFAULT;
942 goto lenout;
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000943 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700944
945 case SO_PEERNAME:
946 {
947 char address[128];
948
949 if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
950 return -ENOTCONN;
951 if (lv < len)
952 return -EINVAL;
953 if (copy_to_user(optval, address, len))
954 return -EFAULT;
955 goto lenout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700957
958 /* Dubious BSD thing... Probably nobody even uses it, but
959 * the UNIX standard wants it for whatever reason... -DaveM
960 */
961 case SO_ACCEPTCONN:
962 v.val = sk->sk_state == TCP_LISTEN;
963 break;
964
965 case SO_PASSSEC:
966 v.val = test_bit(SOCK_PASSSEC, &sock->flags) ? 1 : 0;
967 break;
968
969 case SO_PEERSEC:
970 return security_socket_getpeersec_stream(sock, optval, optlen, len);
971
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800972 case SO_MARK:
973 v.val = sk->sk_mark;
974 break;
975
Neil Horman3b885782009-10-12 13:26:31 -0700976 case SO_RXQ_OVFL:
977 v.val = !!sock_flag(sk, SOCK_RXQ_OVFL);
978 break;
979
Johannes Berg6e3e9392011-11-09 10:15:42 +0100980 case SO_WIFI_STATUS:
981 v.val = !!sock_flag(sk, SOCK_WIFI_STATUS);
982 break;
983
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700984 default:
985 return -ENOPROTOOPT;
986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (len > lv)
989 len = lv;
990 if (copy_to_user(optval, &v, len))
991 return -EFAULT;
992lenout:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900993 if (put_user(len, optlen))
994 return -EFAULT;
995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -0700998/*
999 * Initialize an sk_lock.
1000 *
1001 * (We also register the sk_lock with the lock validator.)
1002 */
Dave Jonesb6f99a22007-03-22 12:27:49 -07001003static inline void sock_lock_init(struct sock *sk)
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -07001004{
Peter Zijlstraed075362006-12-06 20:35:24 -08001005 sock_lock_init_class_and_name(sk,
1006 af_family_slock_key_strings[sk->sk_family],
1007 af_family_slock_keys + sk->sk_family,
1008 af_family_key_strings[sk->sk_family],
1009 af_family_keys + sk->sk_family);
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -07001010}
1011
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001012/*
1013 * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
1014 * even temporarly, because of RCU lookups. sk_node should also be left as is.
Eric Dumazet68835ab2010-11-30 19:04:07 +00001015 * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001016 */
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001017static void sock_copy(struct sock *nsk, const struct sock *osk)
1018{
1019#ifdef CONFIG_SECURITY_NETWORK
1020 void *sptr = nsk->sk_security;
1021#endif
Eric Dumazet68835ab2010-11-30 19:04:07 +00001022 memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
1023
1024 memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
1025 osk->sk_prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
1026
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001027#ifdef CONFIG_SECURITY_NETWORK
1028 nsk->sk_security = sptr;
1029 security_sk_clone(osk, nsk);
1030#endif
1031}
1032
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001033/*
1034 * caches using SLAB_DESTROY_BY_RCU should let .next pointer from nulls nodes
1035 * un-modified. Special care is taken when initializing object to zero.
1036 */
1037static inline void sk_prot_clear_nulls(struct sock *sk, int size)
1038{
1039 if (offsetof(struct sock, sk_node.next) != 0)
1040 memset(sk, 0, offsetof(struct sock, sk_node.next));
1041 memset(&sk->sk_node.pprev, 0,
1042 size - offsetof(struct sock, sk_node.pprev));
1043}
1044
1045void sk_prot_clear_portaddr_nulls(struct sock *sk, int size)
1046{
1047 unsigned long nulls1, nulls2;
1048
1049 nulls1 = offsetof(struct sock, __sk_common.skc_node.next);
1050 nulls2 = offsetof(struct sock, __sk_common.skc_portaddr_node.next);
1051 if (nulls1 > nulls2)
1052 swap(nulls1, nulls2);
1053
1054 if (nulls1 != 0)
1055 memset((char *)sk, 0, nulls1);
1056 memset((char *)sk + nulls1 + sizeof(void *), 0,
1057 nulls2 - nulls1 - sizeof(void *));
1058 memset((char *)sk + nulls2 + sizeof(void *), 0,
1059 size - nulls2 - sizeof(void *));
1060}
1061EXPORT_SYMBOL(sk_prot_clear_portaddr_nulls);
1062
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001063static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
1064 int family)
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001065{
1066 struct sock *sk;
1067 struct kmem_cache *slab;
1068
1069 slab = prot->slab;
Eric Dumazete912b112009-07-08 19:36:05 +00001070 if (slab != NULL) {
1071 sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
1072 if (!sk)
1073 return sk;
1074 if (priority & __GFP_ZERO) {
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001075 if (prot->clear_sk)
1076 prot->clear_sk(sk, prot->obj_size);
1077 else
1078 sk_prot_clear_nulls(sk, prot->obj_size);
Eric Dumazete912b112009-07-08 19:36:05 +00001079 }
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001080 } else
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001081 sk = kmalloc(prot->obj_size, priority);
1082
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001083 if (sk != NULL) {
Vegard Nossuma98b65a2009-02-26 14:46:57 +01001084 kmemcheck_annotate_bitfield(sk, flags);
1085
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001086 if (security_sk_alloc(sk, family, priority))
1087 goto out_free;
1088
1089 if (!try_module_get(prot->owner))
1090 goto out_free_sec;
Krishna Kumare022f0b2009-10-19 23:46:20 +00001091 sk_tx_queue_clear(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001092 }
1093
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001094 return sk;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001095
1096out_free_sec:
1097 security_sk_free(sk);
1098out_free:
1099 if (slab != NULL)
1100 kmem_cache_free(slab, sk);
1101 else
1102 kfree(sk);
1103 return NULL;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001104}
1105
1106static void sk_prot_free(struct proto *prot, struct sock *sk)
1107{
1108 struct kmem_cache *slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001109 struct module *owner;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001110
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001111 owner = prot->owner;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001112 slab = prot->slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001113
1114 security_sk_free(sk);
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001115 if (slab != NULL)
1116 kmem_cache_free(slab, sk);
1117 else
1118 kfree(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001119 module_put(owner);
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001120}
1121
Herbert Xuf8451722010-05-24 00:12:34 -07001122#ifdef CONFIG_CGROUPS
1123void sock_update_classid(struct sock *sk)
1124{
Paul E. McKenney11441822010-10-06 17:15:35 -07001125 u32 classid;
Herbert Xuf8451722010-05-24 00:12:34 -07001126
Paul E. McKenney11441822010-10-06 17:15:35 -07001127 rcu_read_lock(); /* doing current task, which cannot vanish. */
1128 classid = task_cls_classid(current);
1129 rcu_read_unlock();
Herbert Xuf8451722010-05-24 00:12:34 -07001130 if (classid && classid != sk->sk_classid)
1131 sk->sk_classid = classid;
1132}
Herbert Xu82862742010-05-24 00:14:10 -07001133EXPORT_SYMBOL(sock_update_classid);
Neil Horman5bc14212011-11-22 05:10:51 +00001134
1135void sock_update_netprioidx(struct sock *sk)
1136{
1137 struct cgroup_netprio_state *state;
1138 if (in_interrupt())
1139 return;
1140 rcu_read_lock();
1141 state = task_netprio_state(current);
1142 sk->sk_cgrp_prioidx = state ? state->prioidx : 0;
1143 rcu_read_unlock();
1144}
1145EXPORT_SYMBOL_GPL(sock_update_netprioidx);
Herbert Xuf8451722010-05-24 00:12:34 -07001146#endif
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/**
1149 * sk_alloc - All socket objects are allocated here
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001150 * @net: the applicable net namespace
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001151 * @family: protocol family
1152 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1153 * @prot: struct proto associated with this new sock instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001155struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001156 struct proto *prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001158 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001160 sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (sk) {
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001162 sk->sk_family = family;
1163 /*
1164 * See comment in struct sock definition to understand
1165 * why we need sk_prot_creator -acme
1166 */
1167 sk->sk_prot = sk->sk_prot_creator = prot;
1168 sock_lock_init(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001169 sock_net_set(sk, get_net(net));
Jarek Poplawskid66ee052009-08-30 23:15:36 +00001170 atomic_set(&sk->sk_wmem_alloc, 1);
Herbert Xuf8451722010-05-24 00:12:34 -07001171
1172 sock_update_classid(sk);
Neil Horman5bc14212011-11-22 05:10:51 +00001173 sock_update_netprioidx(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Frank Filza79af592005-09-27 15:23:38 -07001175
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001176 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
Eric Dumazet2a915252009-05-27 11:30:05 +00001178EXPORT_SYMBOL(sk_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Eric Dumazet2b85a342009-06-11 02:55:43 -07001180static void __sk_free(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 struct sk_filter *filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if (sk->sk_destruct)
1185 sk->sk_destruct(sk);
1186
Paul E. McKenneya898def2010-02-22 17:04:49 -08001187 filter = rcu_dereference_check(sk->sk_filter,
1188 atomic_read(&sk->sk_wmem_alloc) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (filter) {
Pavel Emelyanov309dd5f2007-10-17 21:21:51 -07001190 sk_filter_uncharge(sk, filter);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001191 RCU_INIT_POINTER(sk->sk_filter, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
Eric Dumazet08e29af2011-11-28 12:04:18 +00001194 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (atomic_read(&sk->sk_omem_alloc))
1197 printk(KERN_DEBUG "%s: optmem leakage (%d bytes) detected.\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001198 __func__, atomic_read(&sk->sk_omem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001200 if (sk->sk_peer_cred)
1201 put_cred(sk->sk_peer_cred);
1202 put_pid(sk->sk_peer_pid);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001203 put_net(sock_net(sk));
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001204 sk_prot_free(sk->sk_prot_creator, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
Eric Dumazet2b85a342009-06-11 02:55:43 -07001206
1207void sk_free(struct sock *sk)
1208{
1209 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001210 * We subtract one from sk_wmem_alloc and can know if
Eric Dumazet2b85a342009-06-11 02:55:43 -07001211 * some packets are still in some tx queue.
1212 * If not null, sock_wfree() will call __sk_free(sk) later
1213 */
1214 if (atomic_dec_and_test(&sk->sk_wmem_alloc))
1215 __sk_free(sk);
1216}
Eric Dumazet2a915252009-05-27 11:30:05 +00001217EXPORT_SYMBOL(sk_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Denis V. Lunevedf02082008-02-29 11:18:32 -08001219/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001220 * Last sock_put should drop reference to sk->sk_net. It has already
1221 * been dropped in sk_change_net. Taking reference to stopping namespace
Denis V. Lunevedf02082008-02-29 11:18:32 -08001222 * is not an option.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001223 * Take reference to a socket to remove it from hash _alive_ and after that
Denis V. Lunevedf02082008-02-29 11:18:32 -08001224 * destroy it in the context of init_net.
1225 */
1226void sk_release_kernel(struct sock *sk)
1227{
1228 if (sk == NULL || sk->sk_socket == NULL)
1229 return;
1230
1231 sock_hold(sk);
1232 sock_release(sk->sk_socket);
Denis V. Lunev65a18ec2008-04-16 01:59:46 -07001233 release_net(sock_net(sk));
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001234 sock_net_set(sk, get_net(&init_net));
Denis V. Lunevedf02082008-02-29 11:18:32 -08001235 sock_put(sk);
1236}
David S. Miller45af1752008-02-29 11:33:19 -08001237EXPORT_SYMBOL(sk_release_kernel);
Denis V. Lunevedf02082008-02-29 11:18:32 -08001238
Eric Dumazete56c57d2011-11-08 17:07:07 -05001239/**
1240 * sk_clone_lock - clone a socket, and lock its clone
1241 * @sk: the socket to clone
1242 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1243 *
1244 * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
1245 */
1246struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001247{
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001248 struct sock *newsk;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001249
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001250 newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001251 if (newsk != NULL) {
1252 struct sk_filter *filter;
1253
Venkat Yekkirala892c1412006-08-04 23:08:56 -07001254 sock_copy(newsk, sk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001255
1256 /* SANITY */
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001257 get_net(sock_net(newsk));
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001258 sk_node_init(&newsk->sk_node);
1259 sock_lock_init(newsk);
1260 bh_lock_sock(newsk);
Eric Dumazetfa438cc2007-03-04 16:05:44 -08001261 newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
Zhu Yi8eae9392010-03-04 18:01:40 +00001262 newsk->sk_backlog.len = 0;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001263
1264 atomic_set(&newsk->sk_rmem_alloc, 0);
Eric Dumazet2b85a342009-06-11 02:55:43 -07001265 /*
1266 * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
1267 */
1268 atomic_set(&newsk->sk_wmem_alloc, 1);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001269 atomic_set(&newsk->sk_omem_alloc, 0);
1270 skb_queue_head_init(&newsk->sk_receive_queue);
1271 skb_queue_head_init(&newsk->sk_write_queue);
Chris Leech97fc2f02006-05-23 17:55:33 -07001272#ifdef CONFIG_NET_DMA
1273 skb_queue_head_init(&newsk->sk_async_wait_queue);
1274#endif
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001275
Eric Dumazetb6c67122010-04-08 23:03:29 +00001276 spin_lock_init(&newsk->sk_dst_lock);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001277 rwlock_init(&newsk->sk_callback_lock);
Peter Zijlstra443aef0e2007-07-19 01:49:00 -07001278 lockdep_set_class_and_name(&newsk->sk_callback_lock,
1279 af_callback_keys + newsk->sk_family,
1280 af_family_clock_key_strings[newsk->sk_family]);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001281
1282 newsk->sk_dst_cache = NULL;
1283 newsk->sk_wmem_queued = 0;
1284 newsk->sk_forward_alloc = 0;
1285 newsk->sk_send_head = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001286 newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
1287
1288 sock_reset_flag(newsk, SOCK_DONE);
1289 skb_queue_head_init(&newsk->sk_error_queue);
1290
Eric Dumazet0d7da9d2010-10-25 03:47:05 +00001291 filter = rcu_dereference_protected(newsk->sk_filter, 1);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001292 if (filter != NULL)
1293 sk_filter_charge(newsk, filter);
1294
1295 if (unlikely(xfrm_sk_clone_policy(newsk))) {
1296 /* It is still raw copy of parent, so invalidate
1297 * destructor and make plain sk_free() */
1298 newsk->sk_destruct = NULL;
Thomas Gleixnerb0691c82011-10-25 02:30:50 +00001299 bh_unlock_sock(newsk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001300 sk_free(newsk);
1301 newsk = NULL;
1302 goto out;
1303 }
1304
1305 newsk->sk_err = 0;
1306 newsk->sk_priority = 0;
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001307 /*
1308 * Before updating sk_refcnt, we must commit prior changes to memory
1309 * (Documentation/RCU/rculist_nulls.txt for details)
1310 */
1311 smp_wmb();
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001312 atomic_set(&newsk->sk_refcnt, 2);
1313
1314 /*
1315 * Increment the counter in the same struct proto as the master
1316 * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
1317 * is the same as sk->sk_prot->socks, as this field was copied
1318 * with memcpy).
1319 *
1320 * This _changes_ the previous behaviour, where
1321 * tcp_create_openreq_child always was incrementing the
1322 * equivalent to tcp_prot->socks (inet_sock_nr), so this have
1323 * to be taken into account in all callers. -acme
1324 */
1325 sk_refcnt_debug_inc(newsk);
David S. Miller972692e2008-06-17 22:41:38 -07001326 sk_set_socket(newsk, NULL);
Eric Dumazet43815482010-04-29 11:01:49 +00001327 newsk->sk_wq = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001328
1329 if (newsk->sk_prot->sockets_allocated)
Glauber Costa180d8cd2011-12-11 21:47:02 +00001330 sk_sockets_allocated_inc(newsk);
Octavian Purdila704da5602010-01-08 00:00:09 -08001331
Eric Dumazet08e29af2011-11-28 12:04:18 +00001332 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
Octavian Purdila704da5602010-01-08 00:00:09 -08001333 net_enable_timestamp();
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001334 }
1335out:
1336 return newsk;
1337}
Eric Dumazete56c57d2011-11-08 17:07:07 -05001338EXPORT_SYMBOL_GPL(sk_clone_lock);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001339
Andi Kleen99580892007-04-20 17:12:43 -07001340void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
1341{
1342 __sk_dst_set(sk, dst);
1343 sk->sk_route_caps = dst->dev->features;
1344 if (sk->sk_route_caps & NETIF_F_GSO)
Herbert Xu4fcd6b92007-05-31 22:15:50 -07001345 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
Eric Dumazeta4654192010-05-16 00:36:33 -07001346 sk->sk_route_caps &= ~sk->sk_route_nocaps;
Andi Kleen99580892007-04-20 17:12:43 -07001347 if (sk_can_gso(sk)) {
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001348 if (dst->header_len) {
Andi Kleen99580892007-04-20 17:12:43 -07001349 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001350 } else {
Andi Kleen99580892007-04-20 17:12:43 -07001351 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001352 sk->sk_gso_max_size = dst->dev->gso_max_size;
1353 }
Andi Kleen99580892007-04-20 17:12:43 -07001354 }
1355}
1356EXPORT_SYMBOL_GPL(sk_setup_caps);
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358void __init sk_init(void)
1359{
Jan Beulich44813742009-09-21 17:03:05 -07001360 if (totalram_pages <= 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 sysctl_wmem_max = 32767;
1362 sysctl_rmem_max = 32767;
1363 sysctl_wmem_default = 32767;
1364 sysctl_rmem_default = 32767;
Jan Beulich44813742009-09-21 17:03:05 -07001365 } else if (totalram_pages >= 131072) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 sysctl_wmem_max = 131071;
1367 sysctl_rmem_max = 131071;
1368 }
1369}
1370
1371/*
1372 * Simple resource managers for sockets.
1373 */
1374
1375
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001376/*
1377 * Write buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
1379void sock_wfree(struct sk_buff *skb)
1380{
1381 struct sock *sk = skb->sk;
Eric Dumazetd99927f2009-09-24 10:49:24 +00001382 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Eric Dumazetd99927f2009-09-24 10:49:24 +00001384 if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
1385 /*
1386 * Keep a reference on sk_wmem_alloc, this will be released
1387 * after sk_write_space() call
1388 */
1389 atomic_sub(len - 1, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 sk->sk_write_space(sk);
Eric Dumazetd99927f2009-09-24 10:49:24 +00001391 len = 1;
1392 }
Eric Dumazet2b85a342009-06-11 02:55:43 -07001393 /*
Eric Dumazetd99927f2009-09-24 10:49:24 +00001394 * if sk_wmem_alloc reaches 0, we must finish what sk_free()
1395 * could not do because of in-flight packets
Eric Dumazet2b85a342009-06-11 02:55:43 -07001396 */
Eric Dumazetd99927f2009-09-24 10:49:24 +00001397 if (atomic_sub_and_test(len, &sk->sk_wmem_alloc))
Eric Dumazet2b85a342009-06-11 02:55:43 -07001398 __sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
Eric Dumazet2a915252009-05-27 11:30:05 +00001400EXPORT_SYMBOL(sock_wfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001402/*
1403 * Read buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
1405void sock_rfree(struct sk_buff *skb)
1406{
1407 struct sock *sk = skb->sk;
Eric Dumazetd361fd52010-07-10 22:45:17 +00001408 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Eric Dumazetd361fd52010-07-10 22:45:17 +00001410 atomic_sub(len, &sk->sk_rmem_alloc);
1411 sk_mem_uncharge(sk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
Eric Dumazet2a915252009-05-27 11:30:05 +00001413EXPORT_SYMBOL(sock_rfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415
1416int sock_i_uid(struct sock *sk)
1417{
1418 int uid;
1419
Eric Dumazetf064af12010-09-22 12:43:39 +00001420 read_lock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : 0;
Eric Dumazetf064af12010-09-22 12:43:39 +00001422 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return uid;
1424}
Eric Dumazet2a915252009-05-27 11:30:05 +00001425EXPORT_SYMBOL(sock_i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427unsigned long sock_i_ino(struct sock *sk)
1428{
1429 unsigned long ino;
1430
Eric Dumazetf064af12010-09-22 12:43:39 +00001431 read_lock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
Eric Dumazetf064af12010-09-22 12:43:39 +00001433 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return ino;
1435}
Eric Dumazet2a915252009-05-27 11:30:05 +00001436EXPORT_SYMBOL(sock_i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438/*
1439 * Allocate a skb from the socket's send buffer.
1440 */
Victor Fusco86a76ca2005-07-08 14:57:47 -07001441struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
Al Virodd0fc662005-10-07 07:46:04 +01001442 gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
Eric Dumazet2a915252009-05-27 11:30:05 +00001445 struct sk_buff *skb = alloc_skb(size, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (skb) {
1447 skb_set_owner_w(skb, sk);
1448 return skb;
1449 }
1450 }
1451 return NULL;
1452}
Eric Dumazet2a915252009-05-27 11:30:05 +00001453EXPORT_SYMBOL(sock_wmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455/*
1456 * Allocate a skb from the socket's receive buffer.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001457 */
Victor Fusco86a76ca2005-07-08 14:57:47 -07001458struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force,
Al Virodd0fc662005-10-07 07:46:04 +01001459 gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 if (force || atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
1462 struct sk_buff *skb = alloc_skb(size, priority);
1463 if (skb) {
1464 skb_set_owner_r(skb, sk);
1465 return skb;
1466 }
1467 }
1468 return NULL;
1469}
1470
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001471/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 * Allocate a memory block from the socket's option memory buffer.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001473 */
Al Virodd0fc662005-10-07 07:46:04 +01001474void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 if ((unsigned)size <= sysctl_optmem_max &&
1477 atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
1478 void *mem;
1479 /* First do the add, to avoid the race if kmalloc
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001480 * might sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
1482 atomic_add(size, &sk->sk_omem_alloc);
1483 mem = kmalloc(size, priority);
1484 if (mem)
1485 return mem;
1486 atomic_sub(size, &sk->sk_omem_alloc);
1487 }
1488 return NULL;
1489}
Eric Dumazet2a915252009-05-27 11:30:05 +00001490EXPORT_SYMBOL(sock_kmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492/*
1493 * Free an option memory block.
1494 */
1495void sock_kfree_s(struct sock *sk, void *mem, int size)
1496{
1497 kfree(mem);
1498 atomic_sub(size, &sk->sk_omem_alloc);
1499}
Eric Dumazet2a915252009-05-27 11:30:05 +00001500EXPORT_SYMBOL(sock_kfree_s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502/* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
1503 I think, these locks should be removed for datagram sockets.
1504 */
Eric Dumazet2a915252009-05-27 11:30:05 +00001505static long sock_wait_for_wmem(struct sock *sk, long timeo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 DEFINE_WAIT(wait);
1508
1509 clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1510 for (;;) {
1511 if (!timeo)
1512 break;
1513 if (signal_pending(current))
1514 break;
1515 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Eric Dumazetaa395142010-04-20 13:03:51 +00001516 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
1518 break;
1519 if (sk->sk_shutdown & SEND_SHUTDOWN)
1520 break;
1521 if (sk->sk_err)
1522 break;
1523 timeo = schedule_timeout(timeo);
1524 }
Eric Dumazetaa395142010-04-20 13:03:51 +00001525 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return timeo;
1527}
1528
1529
1530/*
1531 * Generic send/receive buffer handlers
1532 */
1533
Herbert Xu4cc7f682009-02-04 16:55:54 -08001534struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
1535 unsigned long data_len, int noblock,
1536 int *errcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 struct sk_buff *skb;
Al Viro7d877f32005-10-21 03:20:43 -04001539 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 long timeo;
1541 int err;
1542
1543 gfp_mask = sk->sk_allocation;
1544 if (gfp_mask & __GFP_WAIT)
1545 gfp_mask |= __GFP_REPEAT;
1546
1547 timeo = sock_sndtimeo(sk, noblock);
1548 while (1) {
1549 err = sock_error(sk);
1550 if (err != 0)
1551 goto failure;
1552
1553 err = -EPIPE;
1554 if (sk->sk_shutdown & SEND_SHUTDOWN)
1555 goto failure;
1556
1557 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
Larry Woodmandb38c1792006-11-03 16:05:45 -08001558 skb = alloc_skb(header_len, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (skb) {
1560 int npages;
1561 int i;
1562
1563 /* No pages, we're done... */
1564 if (!data_len)
1565 break;
1566
1567 npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
1568 skb->truesize += data_len;
1569 skb_shinfo(skb)->nr_frags = npages;
1570 for (i = 0; i < npages; i++) {
1571 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 page = alloc_pages(sk->sk_allocation, 0);
1574 if (!page) {
1575 err = -ENOBUFS;
1576 skb_shinfo(skb)->nr_frags = i;
1577 kfree_skb(skb);
1578 goto failure;
1579 }
1580
Ian Campbellea2ab692011-08-22 23:44:58 +00001581 __skb_fill_page_desc(skb, i,
1582 page, 0,
1583 (data_len >= PAGE_SIZE ?
1584 PAGE_SIZE :
1585 data_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 data_len -= PAGE_SIZE;
1587 }
1588
1589 /* Full success... */
1590 break;
1591 }
1592 err = -ENOBUFS;
1593 goto failure;
1594 }
1595 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1596 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1597 err = -EAGAIN;
1598 if (!timeo)
1599 goto failure;
1600 if (signal_pending(current))
1601 goto interrupted;
1602 timeo = sock_wait_for_wmem(sk, timeo);
1603 }
1604
1605 skb_set_owner_w(skb, sk);
1606 return skb;
1607
1608interrupted:
1609 err = sock_intr_errno(timeo);
1610failure:
1611 *errcode = err;
1612 return NULL;
1613}
Herbert Xu4cc7f682009-02-04 16:55:54 -08001614EXPORT_SYMBOL(sock_alloc_send_pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001616struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int noblock, int *errcode)
1618{
1619 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode);
1620}
Eric Dumazet2a915252009-05-27 11:30:05 +00001621EXPORT_SYMBOL(sock_alloc_send_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623static void __lock_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00001624 __releases(&sk->sk_lock.slock)
1625 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 DEFINE_WAIT(wait);
1628
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001629 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
1631 TASK_UNINTERRUPTIBLE);
1632 spin_unlock_bh(&sk->sk_lock.slock);
1633 schedule();
1634 spin_lock_bh(&sk->sk_lock.slock);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001635 if (!sock_owned_by_user(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 break;
1637 }
1638 finish_wait(&sk->sk_lock.wq, &wait);
1639}
1640
1641static void __release_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00001642 __releases(&sk->sk_lock.slock)
1643 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 struct sk_buff *skb = sk->sk_backlog.head;
1646
1647 do {
1648 sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
1649 bh_unlock_sock(sk);
1650
1651 do {
1652 struct sk_buff *next = skb->next;
1653
Eric Dumazet7fee2262010-05-11 23:19:48 +00001654 WARN_ON_ONCE(skb_dst_is_noref(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 skb->next = NULL;
Peter Zijlstrac57943a2008-10-07 14:18:42 -07001656 sk_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /*
1659 * We are in process context here with softirqs
1660 * disabled, use cond_resched_softirq() to preempt.
1661 * This is safe to do because we've taken the backlog
1662 * queue private:
1663 */
1664 cond_resched_softirq();
1665
1666 skb = next;
1667 } while (skb != NULL);
1668
1669 bh_lock_sock(sk);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001670 } while ((skb = sk->sk_backlog.head) != NULL);
Zhu Yi8eae9392010-03-04 18:01:40 +00001671
1672 /*
1673 * Doing the zeroing here guarantee we can not loop forever
1674 * while a wild producer attempts to flood us.
1675 */
1676 sk->sk_backlog.len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
1679/**
1680 * sk_wait_data - wait for data to arrive at sk_receive_queue
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001681 * @sk: sock to wait on
1682 * @timeo: for how long
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 *
1684 * Now socket state including sk->sk_err is changed only under lock,
1685 * hence we may omit checks after joining wait queue.
1686 * We check receive queue before schedule() only as optimization;
1687 * it is very likely that release_sock() added new data.
1688 */
1689int sk_wait_data(struct sock *sk, long *timeo)
1690{
1691 int rc;
1692 DEFINE_WAIT(wait);
1693
Eric Dumazetaa395142010-04-20 13:03:51 +00001694 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1696 rc = sk_wait_event(sk, timeo, !skb_queue_empty(&sk->sk_receive_queue));
1697 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
Eric Dumazetaa395142010-04-20 13:03:51 +00001698 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return rc;
1700}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701EXPORT_SYMBOL(sk_wait_data);
1702
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001703/**
1704 * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
1705 * @sk: socket
1706 * @size: memory size to allocate
1707 * @kind: allocation type
1708 *
1709 * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
1710 * rmem allocation. This function assumes that protocols which have
1711 * memory_pressure use sk_wmem_queued as write buffer accounting.
1712 */
1713int __sk_mem_schedule(struct sock *sk, int size, int kind)
1714{
1715 struct proto *prot = sk->sk_prot;
1716 int amt = sk_mem_pages(size);
Eric Dumazet8d987e52010-11-09 23:24:26 +00001717 long allocated;
Glauber Costae1aab162011-12-11 21:47:03 +00001718 int parent_status = UNDER_LIMIT;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001719
1720 sk->sk_forward_alloc += amt * SK_MEM_QUANTUM;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001721
Glauber Costae1aab162011-12-11 21:47:03 +00001722 allocated = sk_memory_allocated_add(sk, amt, &parent_status);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001723
1724 /* Under limit. */
Glauber Costae1aab162011-12-11 21:47:03 +00001725 if (parent_status == UNDER_LIMIT &&
1726 allocated <= sk_prot_mem_limits(sk, 0)) {
Glauber Costa180d8cd2011-12-11 21:47:02 +00001727 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001728 return 1;
1729 }
1730
Glauber Costae1aab162011-12-11 21:47:03 +00001731 /* Under pressure. (we or our parents) */
1732 if ((parent_status > SOFT_LIMIT) ||
1733 allocated > sk_prot_mem_limits(sk, 1))
Glauber Costa180d8cd2011-12-11 21:47:02 +00001734 sk_enter_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001735
Glauber Costae1aab162011-12-11 21:47:03 +00001736 /* Over hard limit (we or our parents) */
1737 if ((parent_status == OVER_LIMIT) ||
1738 (allocated > sk_prot_mem_limits(sk, 2)))
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001739 goto suppress_allocation;
1740
1741 /* guarantee minimum buffer size under pressure */
1742 if (kind == SK_MEM_RECV) {
1743 if (atomic_read(&sk->sk_rmem_alloc) < prot->sysctl_rmem[0])
1744 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001745
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001746 } else { /* SK_MEM_SEND */
1747 if (sk->sk_type == SOCK_STREAM) {
1748 if (sk->sk_wmem_queued < prot->sysctl_wmem[0])
1749 return 1;
1750 } else if (atomic_read(&sk->sk_wmem_alloc) <
1751 prot->sysctl_wmem[0])
1752 return 1;
1753 }
1754
Glauber Costa180d8cd2011-12-11 21:47:02 +00001755 if (sk_has_memory_pressure(sk)) {
Eric Dumazet17483762008-11-25 21:16:35 -08001756 int alloc;
1757
Glauber Costa180d8cd2011-12-11 21:47:02 +00001758 if (!sk_under_memory_pressure(sk))
Eric Dumazet17483762008-11-25 21:16:35 -08001759 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001760 alloc = sk_sockets_allocated_read_positive(sk);
1761 if (sk_prot_mem_limits(sk, 2) > alloc *
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001762 sk_mem_pages(sk->sk_wmem_queued +
1763 atomic_read(&sk->sk_rmem_alloc) +
1764 sk->sk_forward_alloc))
1765 return 1;
1766 }
1767
1768suppress_allocation:
1769
1770 if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
1771 sk_stream_moderate_sndbuf(sk);
1772
1773 /* Fail only if socket is _under_ its sndbuf.
1774 * In this case we cannot block, so that we have to fail.
1775 */
1776 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
1777 return 1;
1778 }
1779
Satoru Moriya3847ce32011-06-17 12:00:03 +00001780 trace_sock_exceed_buf_limit(sk, prot, allocated);
1781
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001782 /* Alas. Undo changes. */
1783 sk->sk_forward_alloc -= amt * SK_MEM_QUANTUM;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001784
Glauber Costae1aab162011-12-11 21:47:03 +00001785 sk_memory_allocated_sub(sk, amt, parent_status);
Glauber Costa180d8cd2011-12-11 21:47:02 +00001786
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001787 return 0;
1788}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001789EXPORT_SYMBOL(__sk_mem_schedule);
1790
1791/**
1792 * __sk_reclaim - reclaim memory_allocated
1793 * @sk: socket
1794 */
1795void __sk_mem_reclaim(struct sock *sk)
1796{
Glauber Costa180d8cd2011-12-11 21:47:02 +00001797 sk_memory_allocated_sub(sk,
Glauber Costae1aab162011-12-11 21:47:03 +00001798 sk->sk_forward_alloc >> SK_MEM_QUANTUM_SHIFT, 0);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001799 sk->sk_forward_alloc &= SK_MEM_QUANTUM - 1;
1800
Glauber Costa180d8cd2011-12-11 21:47:02 +00001801 if (sk_under_memory_pressure(sk) &&
1802 (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
1803 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001804}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001805EXPORT_SYMBOL(__sk_mem_reclaim);
1806
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/*
1809 * Set of default routines for initialising struct proto_ops when
1810 * the protocol does not support a particular function. In certain
1811 * cases where it makes no sense for a protocol to have a "do nothing"
1812 * function, some default processing is provided.
1813 */
1814
1815int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
1816{
1817 return -EOPNOTSUPP;
1818}
Eric Dumazet2a915252009-05-27 11:30:05 +00001819EXPORT_SYMBOL(sock_no_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001821int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int len, int flags)
1823{
1824 return -EOPNOTSUPP;
1825}
Eric Dumazet2a915252009-05-27 11:30:05 +00001826EXPORT_SYMBOL(sock_no_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
1829{
1830 return -EOPNOTSUPP;
1831}
Eric Dumazet2a915252009-05-27 11:30:05 +00001832EXPORT_SYMBOL(sock_no_socketpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
1835{
1836 return -EOPNOTSUPP;
1837}
Eric Dumazet2a915252009-05-27 11:30:05 +00001838EXPORT_SYMBOL(sock_no_accept);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001840int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 int *len, int peer)
1842{
1843 return -EOPNOTSUPP;
1844}
Eric Dumazet2a915252009-05-27 11:30:05 +00001845EXPORT_SYMBOL(sock_no_getname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Eric Dumazet2a915252009-05-27 11:30:05 +00001847unsigned int sock_no_poll(struct file *file, struct socket *sock, poll_table *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 return 0;
1850}
Eric Dumazet2a915252009-05-27 11:30:05 +00001851EXPORT_SYMBOL(sock_no_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1854{
1855 return -EOPNOTSUPP;
1856}
Eric Dumazet2a915252009-05-27 11:30:05 +00001857EXPORT_SYMBOL(sock_no_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859int sock_no_listen(struct socket *sock, int backlog)
1860{
1861 return -EOPNOTSUPP;
1862}
Eric Dumazet2a915252009-05-27 11:30:05 +00001863EXPORT_SYMBOL(sock_no_listen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865int sock_no_shutdown(struct socket *sock, int how)
1866{
1867 return -EOPNOTSUPP;
1868}
Eric Dumazet2a915252009-05-27 11:30:05 +00001869EXPORT_SYMBOL(sock_no_shutdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871int sock_no_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001872 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 return -EOPNOTSUPP;
1875}
Eric Dumazet2a915252009-05-27 11:30:05 +00001876EXPORT_SYMBOL(sock_no_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878int sock_no_getsockopt(struct socket *sock, int level, int optname,
1879 char __user *optval, int __user *optlen)
1880{
1881 return -EOPNOTSUPP;
1882}
Eric Dumazet2a915252009-05-27 11:30:05 +00001883EXPORT_SYMBOL(sock_no_getsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885int sock_no_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
1886 size_t len)
1887{
1888 return -EOPNOTSUPP;
1889}
Eric Dumazet2a915252009-05-27 11:30:05 +00001890EXPORT_SYMBOL(sock_no_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892int sock_no_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
1893 size_t len, int flags)
1894{
1895 return -EOPNOTSUPP;
1896}
Eric Dumazet2a915252009-05-27 11:30:05 +00001897EXPORT_SYMBOL(sock_no_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1900{
1901 /* Mirror missing mmap method error code */
1902 return -ENODEV;
1903}
Eric Dumazet2a915252009-05-27 11:30:05 +00001904EXPORT_SYMBOL(sock_no_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
1907{
1908 ssize_t res;
1909 struct msghdr msg = {.msg_flags = flags};
1910 struct kvec iov;
1911 char *kaddr = kmap(page);
1912 iov.iov_base = kaddr + offset;
1913 iov.iov_len = size;
1914 res = kernel_sendmsg(sock, &msg, &iov, 1, size);
1915 kunmap(page);
1916 return res;
1917}
Eric Dumazet2a915252009-05-27 11:30:05 +00001918EXPORT_SYMBOL(sock_no_sendpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920/*
1921 * Default Socket Callbacks
1922 */
1923
1924static void sock_def_wakeup(struct sock *sk)
1925{
Eric Dumazet43815482010-04-29 11:01:49 +00001926 struct socket_wq *wq;
1927
1928 rcu_read_lock();
1929 wq = rcu_dereference(sk->sk_wq);
1930 if (wq_has_sleeper(wq))
1931 wake_up_interruptible_all(&wq->wait);
1932 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
1935static void sock_def_error_report(struct sock *sk)
1936{
Eric Dumazet43815482010-04-29 11:01:49 +00001937 struct socket_wq *wq;
1938
1939 rcu_read_lock();
1940 wq = rcu_dereference(sk->sk_wq);
1941 if (wq_has_sleeper(wq))
1942 wake_up_interruptible_poll(&wq->wait, POLLERR);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001943 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
Eric Dumazet43815482010-04-29 11:01:49 +00001944 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
1947static void sock_def_readable(struct sock *sk, int len)
1948{
Eric Dumazet43815482010-04-29 11:01:49 +00001949 struct socket_wq *wq;
1950
1951 rcu_read_lock();
1952 wq = rcu_dereference(sk->sk_wq);
1953 if (wq_has_sleeper(wq))
Eric Dumazet2c6607c2011-01-06 10:54:29 -08001954 wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI |
Davide Libenzi37e55402009-03-31 15:24:21 -07001955 POLLRDNORM | POLLRDBAND);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001956 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Eric Dumazet43815482010-04-29 11:01:49 +00001957 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960static void sock_def_write_space(struct sock *sk)
1961{
Eric Dumazet43815482010-04-29 11:01:49 +00001962 struct socket_wq *wq;
1963
1964 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Do not wake up a writer until he can make "significant"
1967 * progress. --DaveM
1968 */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001969 if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
Eric Dumazet43815482010-04-29 11:01:49 +00001970 wq = rcu_dereference(sk->sk_wq);
1971 if (wq_has_sleeper(wq))
1972 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
Davide Libenzi37e55402009-03-31 15:24:21 -07001973 POLLWRNORM | POLLWRBAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* Should agree with poll, otherwise some programs break */
1976 if (sock_writeable(sk))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001977 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
Eric Dumazet43815482010-04-29 11:01:49 +00001980 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983static void sock_def_destruct(struct sock *sk)
1984{
Jesper Juhla51482b2005-11-08 09:41:34 -08001985 kfree(sk->sk_protinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
1988void sk_send_sigurg(struct sock *sk)
1989{
1990 if (sk->sk_socket && sk->sk_socket->file)
1991 if (send_sigurg(&sk->sk_socket->file->f_owner))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001992 sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
Eric Dumazet2a915252009-05-27 11:30:05 +00001994EXPORT_SYMBOL(sk_send_sigurg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996void sk_reset_timer(struct sock *sk, struct timer_list* timer,
1997 unsigned long expires)
1998{
1999 if (!mod_timer(timer, expires))
2000 sock_hold(sk);
2001}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002EXPORT_SYMBOL(sk_reset_timer);
2003
2004void sk_stop_timer(struct sock *sk, struct timer_list* timer)
2005{
2006 if (timer_pending(timer) && del_timer(timer))
2007 __sock_put(sk);
2008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009EXPORT_SYMBOL(sk_stop_timer);
2010
2011void sock_init_data(struct socket *sock, struct sock *sk)
2012{
2013 skb_queue_head_init(&sk->sk_receive_queue);
2014 skb_queue_head_init(&sk->sk_write_queue);
2015 skb_queue_head_init(&sk->sk_error_queue);
Chris Leech97fc2f02006-05-23 17:55:33 -07002016#ifdef CONFIG_NET_DMA
2017 skb_queue_head_init(&sk->sk_async_wait_queue);
2018#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 sk->sk_send_head = NULL;
2021
2022 init_timer(&sk->sk_timer);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 sk->sk_allocation = GFP_KERNEL;
2025 sk->sk_rcvbuf = sysctl_rmem_default;
2026 sk->sk_sndbuf = sysctl_wmem_default;
2027 sk->sk_state = TCP_CLOSE;
David S. Miller972692e2008-06-17 22:41:38 -07002028 sk_set_socket(sk, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 sock_set_flag(sk, SOCK_ZAPPED);
2031
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002032 if (sock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 sk->sk_type = sock->type;
Eric Dumazet43815482010-04-29 11:01:49 +00002034 sk->sk_wq = sock->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 sock->sk = sk;
2036 } else
Eric Dumazet43815482010-04-29 11:01:49 +00002037 sk->sk_wq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Eric Dumazetb6c67122010-04-08 23:03:29 +00002039 spin_lock_init(&sk->sk_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 rwlock_init(&sk->sk_callback_lock);
Peter Zijlstra443aef0e2007-07-19 01:49:00 -07002041 lockdep_set_class_and_name(&sk->sk_callback_lock,
2042 af_callback_keys + sk->sk_family,
2043 af_family_clock_key_strings[sk->sk_family]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 sk->sk_state_change = sock_def_wakeup;
2046 sk->sk_data_ready = sock_def_readable;
2047 sk->sk_write_space = sock_def_write_space;
2048 sk->sk_error_report = sock_def_error_report;
2049 sk->sk_destruct = sock_def_destruct;
2050
2051 sk->sk_sndmsg_page = NULL;
2052 sk->sk_sndmsg_off = 0;
2053
Eric W. Biederman109f6e32010-06-13 03:30:14 +00002054 sk->sk_peer_pid = NULL;
2055 sk->sk_peer_cred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 sk->sk_write_pending = 0;
2057 sk->sk_rcvlowat = 1;
2058 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
2059 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
2060
Eric Dumazetf37f0af2008-04-13 21:39:26 -07002061 sk->sk_stamp = ktime_set(-1L, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00002063 /*
2064 * Before updating sk_refcnt, we must commit prior changes to memory
2065 * (Documentation/RCU/rculist_nulls.txt for details)
2066 */
2067 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 atomic_set(&sk->sk_refcnt, 1);
Wang Chen33c732c2007-11-13 20:30:01 -08002069 atomic_set(&sk->sk_drops, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
Eric Dumazet2a915252009-05-27 11:30:05 +00002071EXPORT_SYMBOL(sock_init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002073void lock_sock_nested(struct sock *sk, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 might_sleep();
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -07002076 spin_lock_bh(&sk->sk_lock.slock);
John Heffnerd2e91172007-09-12 10:44:19 +02002077 if (sk->sk_lock.owned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 __lock_sock(sk);
John Heffnerd2e91172007-09-12 10:44:19 +02002079 sk->sk_lock.owned = 1;
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -07002080 spin_unlock(&sk->sk_lock.slock);
2081 /*
2082 * The sk_lock has mutex_lock() semantics here:
2083 */
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002084 mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -07002085 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002087EXPORT_SYMBOL(lock_sock_nested);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002089void release_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -07002091 /*
2092 * The sk_lock has mutex_unlock() semantics:
2093 */
2094 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
2095
2096 spin_lock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (sk->sk_backlog.tail)
2098 __release_sock(sk);
John Heffnerd2e91172007-09-12 10:44:19 +02002099 sk->sk_lock.owned = 0;
Ingo Molnara5b5bb9a2006-07-03 00:25:35 -07002100 if (waitqueue_active(&sk->sk_lock.wq))
2101 wake_up(&sk->sk_lock.wq);
2102 spin_unlock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104EXPORT_SYMBOL(release_sock);
2105
Eric Dumazet8a74ad62010-05-26 19:20:18 +00002106/**
2107 * lock_sock_fast - fast version of lock_sock
2108 * @sk: socket
2109 *
2110 * This version should be used for very small section, where process wont block
2111 * return false if fast path is taken
2112 * sk_lock.slock locked, owned = 0, BH disabled
2113 * return true if slow path is taken
2114 * sk_lock.slock unlocked, owned = 1, BH enabled
2115 */
2116bool lock_sock_fast(struct sock *sk)
2117{
2118 might_sleep();
2119 spin_lock_bh(&sk->sk_lock.slock);
2120
2121 if (!sk->sk_lock.owned)
2122 /*
2123 * Note : We must disable BH
2124 */
2125 return false;
2126
2127 __lock_sock(sk);
2128 sk->sk_lock.owned = 1;
2129 spin_unlock(&sk->sk_lock.slock);
2130 /*
2131 * The sk_lock has mutex_lock() semantics here:
2132 */
2133 mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
2134 local_bh_enable();
2135 return true;
2136}
2137EXPORT_SYMBOL(lock_sock_fast);
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002140{
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002141 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (!sock_flag(sk, SOCK_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002143 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002144 tv = ktime_to_timeval(sk->sk_stamp);
2145 if (tv.tv_sec == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return -ENOENT;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002147 if (tv.tv_sec == 0) {
2148 sk->sk_stamp = ktime_get_real();
2149 tv = ktime_to_timeval(sk->sk_stamp);
2150 }
2151 return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002152}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153EXPORT_SYMBOL(sock_get_timestamp);
2154
Eric Dumazetae40eb12007-03-18 17:33:16 -07002155int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
2156{
2157 struct timespec ts;
2158 if (!sock_flag(sk, SOCK_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002159 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazetae40eb12007-03-18 17:33:16 -07002160 ts = ktime_to_timespec(sk->sk_stamp);
2161 if (ts.tv_sec == -1)
2162 return -ENOENT;
2163 if (ts.tv_sec == 0) {
2164 sk->sk_stamp = ktime_get_real();
2165 ts = ktime_to_timespec(sk->sk_stamp);
2166 }
2167 return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
2168}
2169EXPORT_SYMBOL(sock_get_timestampns);
2170
Patrick Ohly20d49472009-02-12 05:03:38 +00002171void sock_enable_timestamp(struct sock *sk, int flag)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002172{
Patrick Ohly20d49472009-02-12 05:03:38 +00002173 if (!sock_flag(sk, flag)) {
Eric Dumazet08e29af2011-11-28 12:04:18 +00002174 unsigned long previous_flags = sk->sk_flags;
2175
Patrick Ohly20d49472009-02-12 05:03:38 +00002176 sock_set_flag(sk, flag);
2177 /*
2178 * we just set one of the two flags which require net
2179 * time stamping, but time stamping might have been on
2180 * already because of the other one
2181 */
Eric Dumazet08e29af2011-11-28 12:04:18 +00002182 if (!(previous_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002183 net_enable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187/*
2188 * Get a socket option on an socket.
2189 *
2190 * FIX: POSIX 1003.1g is very ambiguous here. It states that
2191 * asynchronous errors should be reported by getsockopt. We assume
2192 * this means if you specify SO_ERROR (otherwise whats the point of it).
2193 */
2194int sock_common_getsockopt(struct socket *sock, int level, int optname,
2195 char __user *optval, int __user *optlen)
2196{
2197 struct sock *sk = sock->sk;
2198
2199 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2200}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201EXPORT_SYMBOL(sock_common_getsockopt);
2202
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002203#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002204int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
2205 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002206{
2207 struct sock *sk = sock->sk;
2208
Johannes Berg1e51f952007-03-06 13:44:06 -08002209 if (sk->sk_prot->compat_getsockopt != NULL)
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002210 return sk->sk_prot->compat_getsockopt(sk, level, optname,
2211 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002212 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2213}
2214EXPORT_SYMBOL(compat_sock_common_getsockopt);
2215#endif
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
2218 struct msghdr *msg, size_t size, int flags)
2219{
2220 struct sock *sk = sock->sk;
2221 int addr_len = 0;
2222 int err;
2223
2224 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
2225 flags & ~MSG_DONTWAIT, &addr_len);
2226 if (err >= 0)
2227 msg->msg_namelen = addr_len;
2228 return err;
2229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230EXPORT_SYMBOL(sock_common_recvmsg);
2231
2232/*
2233 * Set socket options on an inet socket.
2234 */
2235int sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002236 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 struct sock *sk = sock->sk;
2239
2240 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242EXPORT_SYMBOL(sock_common_setsockopt);
2243
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002244#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002245int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002246 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002247{
2248 struct sock *sk = sock->sk;
2249
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002250 if (sk->sk_prot->compat_setsockopt != NULL)
2251 return sk->sk_prot->compat_setsockopt(sk, level, optname,
2252 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002253 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2254}
2255EXPORT_SYMBOL(compat_sock_common_setsockopt);
2256#endif
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258void sk_common_release(struct sock *sk)
2259{
2260 if (sk->sk_prot->destroy)
2261 sk->sk_prot->destroy(sk);
2262
2263 /*
2264 * Observation: when sock_common_release is called, processes have
2265 * no access to socket. But net still has.
2266 * Step one, detach it from networking:
2267 *
2268 * A. Remove from hash tables.
2269 */
2270
2271 sk->sk_prot->unhash(sk);
2272
2273 /*
2274 * In this point socket cannot receive new packets, but it is possible
2275 * that some packets are in flight because some CPU runs receiver and
2276 * did hash table lookup before we unhashed socket. They will achieve
2277 * receive queue and will be purged by socket destructor.
2278 *
2279 * Also we still have packets pending on receive queue and probably,
2280 * our own packets waiting in device queues. sock_destroy will drain
2281 * receive queue, but transmitted packets will delay socket destruction
2282 * until the last reference will be released.
2283 */
2284
2285 sock_orphan(sk);
2286
2287 xfrm_sk_free_policy(sk);
2288
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -07002289 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 sock_put(sk);
2291}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292EXPORT_SYMBOL(sk_common_release);
2293
2294static DEFINE_RWLOCK(proto_list_lock);
2295static LIST_HEAD(proto_list);
2296
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002297#ifdef CONFIG_PROC_FS
2298#define PROTO_INUSE_NR 64 /* should be enough for the first time */
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002299struct prot_inuse {
2300 int val[PROTO_INUSE_NR];
2301};
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002302
2303static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002304
2305#ifdef CONFIG_NET_NS
2306void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
2307{
Eric Dumazetd6d9ca02010-07-19 10:48:49 +00002308 __this_cpu_add(net->core.inuse->val[prot->inuse_idx], val);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002309}
2310EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
2311
2312int sock_prot_inuse_get(struct net *net, struct proto *prot)
2313{
2314 int cpu, idx = prot->inuse_idx;
2315 int res = 0;
2316
2317 for_each_possible_cpu(cpu)
2318 res += per_cpu_ptr(net->core.inuse, cpu)->val[idx];
2319
2320 return res >= 0 ? res : 0;
2321}
2322EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
2323
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002324static int __net_init sock_inuse_init_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002325{
2326 net->core.inuse = alloc_percpu(struct prot_inuse);
2327 return net->core.inuse ? 0 : -ENOMEM;
2328}
2329
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002330static void __net_exit sock_inuse_exit_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002331{
2332 free_percpu(net->core.inuse);
2333}
2334
2335static struct pernet_operations net_inuse_ops = {
2336 .init = sock_inuse_init_net,
2337 .exit = sock_inuse_exit_net,
2338};
2339
2340static __init int net_inuse_init(void)
2341{
2342 if (register_pernet_subsys(&net_inuse_ops))
2343 panic("Cannot initialize net inuse counters");
2344
2345 return 0;
2346}
2347
2348core_initcall(net_inuse_init);
2349#else
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002350static DEFINE_PER_CPU(struct prot_inuse, prot_inuse);
2351
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -07002352void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002353{
Eric Dumazetd6d9ca02010-07-19 10:48:49 +00002354 __this_cpu_add(prot_inuse.val[prot->inuse_idx], val);
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002355}
2356EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
2357
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -07002358int sock_prot_inuse_get(struct net *net, struct proto *prot)
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002359{
2360 int cpu, idx = prot->inuse_idx;
2361 int res = 0;
2362
2363 for_each_possible_cpu(cpu)
2364 res += per_cpu(prot_inuse, cpu).val[idx];
2365
2366 return res >= 0 ? res : 0;
2367}
2368EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002369#endif
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002370
2371static void assign_proto_idx(struct proto *prot)
2372{
2373 prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
2374
2375 if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
2376 printk(KERN_ERR "PROTO_INUSE_NR exhausted\n");
2377 return;
2378 }
2379
2380 set_bit(prot->inuse_idx, proto_inuse_idx);
2381}
2382
2383static void release_proto_idx(struct proto *prot)
2384{
2385 if (prot->inuse_idx != PROTO_INUSE_NR - 1)
2386 clear_bit(prot->inuse_idx, proto_inuse_idx);
2387}
2388#else
2389static inline void assign_proto_idx(struct proto *prot)
2390{
2391}
2392
2393static inline void release_proto_idx(struct proto *prot)
2394{
2395}
2396#endif
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398int proto_register(struct proto *prot, int alloc_slab)
2399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (alloc_slab) {
2401 prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
Eric Dumazet271b72c2008-10-29 02:11:14 -07002402 SLAB_HWCACHE_ALIGN | prot->slab_flags,
2403 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 if (prot->slab == NULL) {
2406 printk(KERN_CRIT "%s: Can't create sock SLAB cache!\n",
2407 prot->name);
Pavel Emelyanov60e76632008-03-28 16:39:10 -07002408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002410
2411 if (prot->rsk_prot != NULL) {
Alexey Dobriyanfaf23422010-02-17 09:34:12 +00002412 prot->rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s", prot->name);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002413 if (prot->rsk_prot->slab_name == NULL)
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002414 goto out_free_sock_slab;
2415
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002416 prot->rsk_prot->slab = kmem_cache_create(prot->rsk_prot->slab_name,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002417 prot->rsk_prot->obj_size, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002418 SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002419
2420 if (prot->rsk_prot->slab == NULL) {
2421 printk(KERN_CRIT "%s: Can't create request sock SLAB cache!\n",
2422 prot->name);
2423 goto out_free_request_sock_slab_name;
2424 }
2425 }
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002426
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002427 if (prot->twsk_prot != NULL) {
Alexey Dobriyanfaf23422010-02-17 09:34:12 +00002428 prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002429
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002430 if (prot->twsk_prot->twsk_slab_name == NULL)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002431 goto out_free_request_sock_slab;
2432
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002433 prot->twsk_prot->twsk_slab =
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002434 kmem_cache_create(prot->twsk_prot->twsk_slab_name,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002435 prot->twsk_prot->twsk_obj_size,
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002436 0,
2437 SLAB_HWCACHE_ALIGN |
2438 prot->slab_flags,
Paul Mundt20c2df82007-07-20 10:11:58 +09002439 NULL);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002440 if (prot->twsk_prot->twsk_slab == NULL)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002441 goto out_free_timewait_sock_slab_name;
2442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444
Arnaldo Carvalho de Melo2a278052005-04-16 15:24:09 -07002445 write_lock(&proto_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 list_add(&prot->node, &proto_list);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002447 assign_proto_idx(prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 write_unlock(&proto_list_lock);
Pavel Emelyanovb733c002007-11-07 02:23:38 -08002449 return 0;
2450
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002451out_free_timewait_sock_slab_name:
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002452 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002453out_free_request_sock_slab:
2454 if (prot->rsk_prot && prot->rsk_prot->slab) {
2455 kmem_cache_destroy(prot->rsk_prot->slab);
2456 prot->rsk_prot->slab = NULL;
2457 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002458out_free_request_sock_slab_name:
Dan Carpenter72150e92010-03-06 01:04:45 +00002459 if (prot->rsk_prot)
2460 kfree(prot->rsk_prot->slab_name);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002461out_free_sock_slab:
2462 kmem_cache_destroy(prot->slab);
2463 prot->slab = NULL;
Pavel Emelyanovb733c002007-11-07 02:23:38 -08002464out:
2465 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467EXPORT_SYMBOL(proto_register);
2468
2469void proto_unregister(struct proto *prot)
2470{
2471 write_lock(&proto_list_lock);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002472 release_proto_idx(prot);
Patrick McHardy0a3f4352005-09-06 19:47:50 -07002473 list_del(&prot->node);
2474 write_unlock(&proto_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 if (prot->slab != NULL) {
2477 kmem_cache_destroy(prot->slab);
2478 prot->slab = NULL;
2479 }
2480
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002481 if (prot->rsk_prot != NULL && prot->rsk_prot->slab != NULL) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002482 kmem_cache_destroy(prot->rsk_prot->slab);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002483 kfree(prot->rsk_prot->slab_name);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002484 prot->rsk_prot->slab = NULL;
2485 }
2486
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002487 if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002488 kmem_cache_destroy(prot->twsk_prot->twsk_slab);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002489 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002490 prot->twsk_prot->twsk_slab = NULL;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493EXPORT_SYMBOL(proto_unregister);
2494
2495#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002497 __acquires(proto_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 read_lock(&proto_list_lock);
Pavel Emelianov60f04382007-07-09 13:15:14 -07002500 return seq_list_start_head(&proto_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
2503static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2504{
Pavel Emelianov60f04382007-07-09 13:15:14 -07002505 return seq_list_next(v, &proto_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508static void proto_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002509 __releases(proto_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 read_unlock(&proto_list_lock);
2512}
2513
2514static char proto_method_implemented(const void *method)
2515{
2516 return method == NULL ? 'n' : 'y';
2517}
Glauber Costa180d8cd2011-12-11 21:47:02 +00002518static long sock_prot_memory_allocated(struct proto *proto)
2519{
2520 return proto->memory_allocated != NULL ? proto_memory_allocated(proto): -1L;
2521}
2522
2523static char *sock_prot_memory_pressure(struct proto *proto)
2524{
2525 return proto->memory_pressure != NULL ?
2526 proto_memory_pressure(proto) ? "yes" : "no" : "NI";
2527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
2530{
Glauber Costa180d8cd2011-12-11 21:47:02 +00002531
Eric Dumazet8d987e52010-11-09 23:24:26 +00002532 seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
2534 proto->name,
2535 proto->obj_size,
Eric Dumazet14e943db2008-11-19 15:14:01 -08002536 sock_prot_inuse_get(seq_file_net(seq), proto),
Glauber Costa180d8cd2011-12-11 21:47:02 +00002537 sock_prot_memory_allocated(proto),
2538 sock_prot_memory_pressure(proto),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 proto->max_header,
2540 proto->slab == NULL ? "no" : "yes",
2541 module_name(proto->owner),
2542 proto_method_implemented(proto->close),
2543 proto_method_implemented(proto->connect),
2544 proto_method_implemented(proto->disconnect),
2545 proto_method_implemented(proto->accept),
2546 proto_method_implemented(proto->ioctl),
2547 proto_method_implemented(proto->init),
2548 proto_method_implemented(proto->destroy),
2549 proto_method_implemented(proto->shutdown),
2550 proto_method_implemented(proto->setsockopt),
2551 proto_method_implemented(proto->getsockopt),
2552 proto_method_implemented(proto->sendmsg),
2553 proto_method_implemented(proto->recvmsg),
2554 proto_method_implemented(proto->sendpage),
2555 proto_method_implemented(proto->bind),
2556 proto_method_implemented(proto->backlog_rcv),
2557 proto_method_implemented(proto->hash),
2558 proto_method_implemented(proto->unhash),
2559 proto_method_implemented(proto->get_port),
2560 proto_method_implemented(proto->enter_memory_pressure));
2561}
2562
2563static int proto_seq_show(struct seq_file *seq, void *v)
2564{
Pavel Emelianov60f04382007-07-09 13:15:14 -07002565 if (v == &proto_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
2567 "protocol",
2568 "size",
2569 "sockets",
2570 "memory",
2571 "press",
2572 "maxhdr",
2573 "slab",
2574 "module",
2575 "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
2576 else
Pavel Emelianov60f04382007-07-09 13:15:14 -07002577 proto_seq_printf(seq, list_entry(v, struct proto, node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return 0;
2579}
2580
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002581static const struct seq_operations proto_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 .start = proto_seq_start,
2583 .next = proto_seq_next,
2584 .stop = proto_seq_stop,
2585 .show = proto_seq_show,
2586};
2587
2588static int proto_seq_open(struct inode *inode, struct file *file)
2589{
Eric Dumazet14e943db2008-11-19 15:14:01 -08002590 return seq_open_net(inode, file, &proto_seq_ops,
2591 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Arjan van de Ven9a321442007-02-12 00:55:35 -08002594static const struct file_operations proto_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 .owner = THIS_MODULE,
2596 .open = proto_seq_open,
2597 .read = seq_read,
2598 .llseek = seq_lseek,
Eric Dumazet14e943db2008-11-19 15:14:01 -08002599 .release = seq_release_net,
2600};
2601
2602static __net_init int proto_init_net(struct net *net)
2603{
2604 if (!proc_net_fops_create(net, "protocols", S_IRUGO, &proto_seq_fops))
2605 return -ENOMEM;
2606
2607 return 0;
2608}
2609
2610static __net_exit void proto_exit_net(struct net *net)
2611{
2612 proc_net_remove(net, "protocols");
2613}
2614
2615
2616static __net_initdata struct pernet_operations proto_net_ops = {
2617 .init = proto_init_net,
2618 .exit = proto_exit_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619};
2620
2621static int __init proto_init(void)
2622{
Eric Dumazet14e943db2008-11-19 15:14:01 -08002623 return register_pernet_subsys(&proto_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
2626subsys_initcall(proto_init);
2627
2628#endif /* PROC_FS */