blob: 0c67facf42f3bb7c5de83b007579b0a358a1ca5f [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 Viroa1f8e7f2006-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
Glauber Costa36b77a52011-12-16 00:51:59 +0000139static DEFINE_MUTEX(proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000140static LIST_HEAD(proto_list);
141
142#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
143int mem_cgroup_sockets_init(struct cgroup *cgrp, struct cgroup_subsys *ss)
144{
145 struct proto *proto;
146 int ret = 0;
147
Glauber Costa36b77a52011-12-16 00:51:59 +0000148 mutex_lock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000149 list_for_each_entry(proto, &proto_list, node) {
150 if (proto->init_cgroup) {
151 ret = proto->init_cgroup(cgrp, ss);
152 if (ret)
153 goto out;
154 }
155 }
156
Glauber Costa36b77a52011-12-16 00:51:59 +0000157 mutex_unlock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000158 return ret;
159out:
160 list_for_each_entry_continue_reverse(proto, &proto_list, node)
161 if (proto->destroy_cgroup)
162 proto->destroy_cgroup(cgrp, ss);
Glauber Costa36b77a52011-12-16 00:51:59 +0000163 mutex_unlock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000164 return ret;
165}
166
167void mem_cgroup_sockets_destroy(struct cgroup *cgrp, struct cgroup_subsys *ss)
168{
169 struct proto *proto;
170
Glauber Costa36b77a52011-12-16 00:51:59 +0000171 mutex_lock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000172 list_for_each_entry_reverse(proto, &proto_list, node)
173 if (proto->destroy_cgroup)
174 proto->destroy_cgroup(cgrp, ss);
Glauber Costa36b77a52011-12-16 00:51:59 +0000175 mutex_unlock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000176}
177#endif
178
Ingo Molnarda21f242006-07-03 00:25:12 -0700179/*
180 * Each address family might have different locking rules, so we have
181 * one slock key per address family:
182 */
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700183static struct lock_class_key af_family_keys[AF_MAX];
184static struct lock_class_key af_family_slock_keys[AF_MAX];
185
Glauber Costae1aab162011-12-11 21:47:03 +0000186struct jump_label_key memcg_socket_limit_enabled;
187EXPORT_SYMBOL(memcg_socket_limit_enabled);
188
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700189/*
190 * Make lock validator output more readable. (we pre-construct these
191 * strings build-time, so that runtime initialization of socket
192 * locks is fast):
193 */
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700194static const char *const af_family_key_strings[AF_MAX+1] = {
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700195 "sk_lock-AF_UNSPEC", "sk_lock-AF_UNIX" , "sk_lock-AF_INET" ,
196 "sk_lock-AF_AX25" , "sk_lock-AF_IPX" , "sk_lock-AF_APPLETALK",
197 "sk_lock-AF_NETROM", "sk_lock-AF_BRIDGE" , "sk_lock-AF_ATMPVC" ,
198 "sk_lock-AF_X25" , "sk_lock-AF_INET6" , "sk_lock-AF_ROSE" ,
199 "sk_lock-AF_DECnet", "sk_lock-AF_NETBEUI" , "sk_lock-AF_SECURITY" ,
200 "sk_lock-AF_KEY" , "sk_lock-AF_NETLINK" , "sk_lock-AF_PACKET" ,
201 "sk_lock-AF_ASH" , "sk_lock-AF_ECONET" , "sk_lock-AF_ATMSVC" ,
Andy Grovercbd151b2009-02-26 23:43:19 -0800202 "sk_lock-AF_RDS" , "sk_lock-AF_SNA" , "sk_lock-AF_IRDA" ,
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700203 "sk_lock-AF_PPPOX" , "sk_lock-AF_WANPIPE" , "sk_lock-AF_LLC" ,
Oliver Hartkoppcd05acf2007-12-16 15:59:24 -0800204 "sk_lock-27" , "sk_lock-28" , "sk_lock-AF_CAN" ,
David Howells17926a72007-04-26 15:48:28 -0700205 "sk_lock-AF_TIPC" , "sk_lock-AF_BLUETOOTH", "sk_lock-IUCV" ,
Remi Denis-Courmontbce7b152008-09-22 19:51:15 -0700206 "sk_lock-AF_RXRPC" , "sk_lock-AF_ISDN" , "sk_lock-AF_PHONET" ,
Miloslav Trmač6f107b52010-12-08 14:35:34 +0800207 "sk_lock-AF_IEEE802154", "sk_lock-AF_CAIF" , "sk_lock-AF_ALG" ,
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300208 "sk_lock-AF_NFC" , "sk_lock-AF_MAX"
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700209};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700210static const char *const af_family_slock_key_strings[AF_MAX+1] = {
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700211 "slock-AF_UNSPEC", "slock-AF_UNIX" , "slock-AF_INET" ,
212 "slock-AF_AX25" , "slock-AF_IPX" , "slock-AF_APPLETALK",
213 "slock-AF_NETROM", "slock-AF_BRIDGE" , "slock-AF_ATMPVC" ,
214 "slock-AF_X25" , "slock-AF_INET6" , "slock-AF_ROSE" ,
215 "slock-AF_DECnet", "slock-AF_NETBEUI" , "slock-AF_SECURITY" ,
216 "slock-AF_KEY" , "slock-AF_NETLINK" , "slock-AF_PACKET" ,
217 "slock-AF_ASH" , "slock-AF_ECONET" , "slock-AF_ATMSVC" ,
Andy Grovercbd151b2009-02-26 23:43:19 -0800218 "slock-AF_RDS" , "slock-AF_SNA" , "slock-AF_IRDA" ,
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700219 "slock-AF_PPPOX" , "slock-AF_WANPIPE" , "slock-AF_LLC" ,
Oliver Hartkoppcd05acf2007-12-16 15:59:24 -0800220 "slock-27" , "slock-28" , "slock-AF_CAN" ,
David Howells17926a72007-04-26 15:48:28 -0700221 "slock-AF_TIPC" , "slock-AF_BLUETOOTH", "slock-AF_IUCV" ,
Remi Denis-Courmontbce7b152008-09-22 19:51:15 -0700222 "slock-AF_RXRPC" , "slock-AF_ISDN" , "slock-AF_PHONET" ,
Miloslav Trmač6f107b52010-12-08 14:35:34 +0800223 "slock-AF_IEEE802154", "slock-AF_CAIF" , "slock-AF_ALG" ,
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300224 "slock-AF_NFC" , "slock-AF_MAX"
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700225};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700226static const char *const af_family_clock_key_strings[AF_MAX+1] = {
Peter Zijlstra443aef02007-07-19 01:49:00 -0700227 "clock-AF_UNSPEC", "clock-AF_UNIX" , "clock-AF_INET" ,
228 "clock-AF_AX25" , "clock-AF_IPX" , "clock-AF_APPLETALK",
229 "clock-AF_NETROM", "clock-AF_BRIDGE" , "clock-AF_ATMPVC" ,
230 "clock-AF_X25" , "clock-AF_INET6" , "clock-AF_ROSE" ,
231 "clock-AF_DECnet", "clock-AF_NETBEUI" , "clock-AF_SECURITY" ,
232 "clock-AF_KEY" , "clock-AF_NETLINK" , "clock-AF_PACKET" ,
233 "clock-AF_ASH" , "clock-AF_ECONET" , "clock-AF_ATMSVC" ,
Andy Grovercbd151b2009-02-26 23:43:19 -0800234 "clock-AF_RDS" , "clock-AF_SNA" , "clock-AF_IRDA" ,
Peter Zijlstra443aef02007-07-19 01:49:00 -0700235 "clock-AF_PPPOX" , "clock-AF_WANPIPE" , "clock-AF_LLC" ,
Oliver Hartkoppb4942af2008-07-23 14:06:04 -0700236 "clock-27" , "clock-28" , "clock-AF_CAN" ,
David Howellse51f8022007-07-21 19:30:16 -0700237 "clock-AF_TIPC" , "clock-AF_BLUETOOTH", "clock-AF_IUCV" ,
Remi Denis-Courmontbce7b152008-09-22 19:51:15 -0700238 "clock-AF_RXRPC" , "clock-AF_ISDN" , "clock-AF_PHONET" ,
Miloslav Trmač6f107b52010-12-08 14:35:34 +0800239 "clock-AF_IEEE802154", "clock-AF_CAIF" , "clock-AF_ALG" ,
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300240 "clock-AF_NFC" , "clock-AF_MAX"
Peter Zijlstra443aef02007-07-19 01:49:00 -0700241};
Ingo Molnarda21f242006-07-03 00:25:12 -0700242
243/*
244 * sk_callback_lock locking rules are per-address-family,
245 * so split the lock classes by using a per-AF key:
246 */
247static struct lock_class_key af_callback_keys[AF_MAX];
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/* Take into consideration the size of the struct sk_buff overhead in the
250 * determination of these values, since that is non-constant across
251 * platforms. This makes socket queueing behavior and performance
252 * not depend upon such differences.
253 */
254#define _SK_MEM_PACKETS 256
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000255#define _SK_MEM_OVERHEAD SKB_TRUESIZE(256)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
257#define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
258
259/* Run time adjustable parameters. */
Brian Haleyab32ea52006-09-22 14:15:41 -0700260__u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
261__u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
262__u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
263__u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300265/* Maximal space eaten by iovec or ancillary data plus some space */
Brian Haleyab32ea52006-09-22 14:15:41 -0700266int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
Eric Dumazet2a915252009-05-27 11:30:05 +0000267EXPORT_SYMBOL(sysctl_optmem_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Neil Horman5bc14212011-11-22 05:10:51 +0000269#if defined(CONFIG_CGROUPS)
270#if !defined(CONFIG_NET_CLS_CGROUP)
Herbert Xuf8451722010-05-24 00:12:34 -0700271int net_cls_subsys_id = -1;
272EXPORT_SYMBOL_GPL(net_cls_subsys_id);
273#endif
Neil Horman5bc14212011-11-22 05:10:51 +0000274#if !defined(CONFIG_NETPRIO_CGROUP)
275int net_prio_subsys_id = -1;
276EXPORT_SYMBOL_GPL(net_prio_subsys_id);
277#endif
278#endif
Herbert Xuf8451722010-05-24 00:12:34 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
281{
282 struct timeval tv;
283
284 if (optlen < sizeof(tv))
285 return -EINVAL;
286 if (copy_from_user(&tv, optval, sizeof(tv)))
287 return -EFAULT;
Vasily Averinba780732007-05-24 16:58:54 -0700288 if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
289 return -EDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Vasily Averinba780732007-05-24 16:58:54 -0700291 if (tv.tv_sec < 0) {
Andrew Morton6f11df82007-07-09 13:16:00 -0700292 static int warned __read_mostly;
293
Vasily Averinba780732007-05-24 16:58:54 -0700294 *timeo_p = 0;
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700295 if (warned < 10 && net_ratelimit()) {
Vasily Averinba780732007-05-24 16:58:54 -0700296 warned++;
297 printk(KERN_INFO "sock_set_timeout: `%s' (pid %d) "
298 "tries to set negative timeout\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700299 current->comm, task_pid_nr(current));
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700300 }
Vasily Averinba780732007-05-24 16:58:54 -0700301 return 0;
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 *timeo_p = MAX_SCHEDULE_TIMEOUT;
304 if (tv.tv_sec == 0 && tv.tv_usec == 0)
305 return 0;
306 if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
307 *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
308 return 0;
309}
310
311static void sock_warn_obsolete_bsdism(const char *name)
312{
313 static int warned;
314 static char warncomm[TASK_COMM_LEN];
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900315 if (strcmp(warncomm, current->comm) && warned < 5) {
316 strcpy(warncomm, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 printk(KERN_WARNING "process `%s' is using obsolete "
318 "%s SO_BSDCOMPAT\n", warncomm, name);
319 warned++;
320 }
321}
322
Eric Dumazet08e29af2011-11-28 12:04:18 +0000323#define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE))
324
325static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900326{
Eric Dumazet08e29af2011-11-28 12:04:18 +0000327 if (sk->sk_flags & flags) {
328 sk->sk_flags &= ~flags;
329 if (!(sk->sk_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +0000330 net_disable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332}
333
334
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800335int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
336{
Eric Dumazet766e90372009-10-14 20:40:11 -0700337 int err;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800338 int skb_len;
Neil Horman3b885782009-10-12 13:26:31 -0700339 unsigned long flags;
340 struct sk_buff_head *list = &sk->sk_receive_queue;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800341
Rami Rosen9ee6b7f2008-05-14 03:50:03 -0700342 /* Cast sk->rcvbuf to unsigned... It's pointless, but reduces
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800343 number of warnings when compiling with -W --ANK
344 */
345 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
346 (unsigned)sk->sk_rcvbuf) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700347 atomic_inc(&sk->sk_drops);
Satoru Moriya3847ce32011-06-17 12:00:03 +0000348 trace_sock_rcvqueue_full(sk, skb);
Eric Dumazet766e90372009-10-14 20:40:11 -0700349 return -ENOMEM;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800350 }
351
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700352 err = sk_filter(sk, skb);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800353 if (err)
Eric Dumazet766e90372009-10-14 20:40:11 -0700354 return err;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800355
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800356 if (!sk_rmem_schedule(sk, skb->truesize)) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700357 atomic_inc(&sk->sk_drops);
358 return -ENOBUFS;
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800359 }
360
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800361 skb->dev = NULL;
362 skb_set_owner_r(skb, sk);
David S. Miller49ad9592008-12-17 22:11:38 -0800363
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800364 /* Cache the SKB length before we tack it onto the receive
365 * queue. Once it is added it no longer belongs to us and
366 * may be freed by other threads of control pulling packets
367 * from the queue.
368 */
369 skb_len = skb->len;
370
Eric Dumazet7fee2262010-05-11 23:19:48 +0000371 /* we escape from rcu protected region, make sure we dont leak
372 * a norefcounted dst
373 */
374 skb_dst_force(skb);
375
Neil Horman3b885782009-10-12 13:26:31 -0700376 spin_lock_irqsave(&list->lock, flags);
377 skb->dropcount = atomic_read(&sk->sk_drops);
378 __skb_queue_tail(list, skb);
379 spin_unlock_irqrestore(&list->lock, flags);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800380
381 if (!sock_flag(sk, SOCK_DEAD))
382 sk->sk_data_ready(sk, skb_len);
Eric Dumazet766e90372009-10-14 20:40:11 -0700383 return 0;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800384}
385EXPORT_SYMBOL(sock_queue_rcv_skb);
386
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200387int sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested)
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800388{
389 int rc = NET_RX_SUCCESS;
390
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700391 if (sk_filter(sk, skb))
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800392 goto discard_and_relse;
393
394 skb->dev = NULL;
395
Eric Dumazetc3774112010-04-27 15:13:20 -0700396 if (sk_rcvqueues_full(sk, skb)) {
397 atomic_inc(&sk->sk_drops);
398 goto discard_and_relse;
399 }
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200400 if (nested)
401 bh_lock_sock_nested(sk);
402 else
403 bh_lock_sock(sk);
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700404 if (!sock_owned_by_user(sk)) {
405 /*
406 * trylock + unlock semantics:
407 */
408 mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
409
Peter Zijlstrac57943a2008-10-07 14:18:42 -0700410 rc = sk_backlog_rcv(sk, skb);
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700411
412 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
Zhu Yia3a858f2010-03-04 18:01:47 +0000413 } else if (sk_add_backlog(sk, skb)) {
Zhu Yi8eae9392010-03-04 18:01:40 +0000414 bh_unlock_sock(sk);
415 atomic_inc(&sk->sk_drops);
416 goto discard_and_relse;
417 }
418
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800419 bh_unlock_sock(sk);
420out:
421 sock_put(sk);
422 return rc;
423discard_and_relse:
424 kfree_skb(skb);
425 goto out;
426}
427EXPORT_SYMBOL(sk_receive_skb);
428
Krishna Kumarea94ff32009-10-19 23:46:45 +0000429void sk_reset_txq(struct sock *sk)
430{
431 sk_tx_queue_clear(sk);
432}
433EXPORT_SYMBOL(sk_reset_txq);
434
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800435struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
436{
Eric Dumazetb6c67122010-04-08 23:03:29 +0000437 struct dst_entry *dst = __sk_dst_get(sk);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800438
439 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
Krishna Kumare022f0b2009-10-19 23:46:20 +0000440 sk_tx_queue_clear(sk);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000441 RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800442 dst_release(dst);
443 return NULL;
444 }
445
446 return dst;
447}
448EXPORT_SYMBOL(__sk_dst_check);
449
450struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
451{
452 struct dst_entry *dst = sk_dst_get(sk);
453
454 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
455 sk_dst_reset(sk);
456 dst_release(dst);
457 return NULL;
458 }
459
460 return dst;
461}
462EXPORT_SYMBOL(sk_dst_check);
463
David S. Miller48788092007-09-14 16:41:03 -0700464static int sock_bindtodevice(struct sock *sk, char __user *optval, int optlen)
465{
466 int ret = -ENOPROTOOPT;
467#ifdef CONFIG_NETDEVICES
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900468 struct net *net = sock_net(sk);
David S. Miller48788092007-09-14 16:41:03 -0700469 char devname[IFNAMSIZ];
470 int index;
471
472 /* Sorry... */
473 ret = -EPERM;
474 if (!capable(CAP_NET_RAW))
475 goto out;
476
477 ret = -EINVAL;
478 if (optlen < 0)
479 goto out;
480
481 /* Bind this socket to a particular device like "eth0",
482 * as specified in the passed interface name. If the
483 * name is "" or the option length is zero the socket
484 * is not bound.
485 */
486 if (optlen > IFNAMSIZ - 1)
487 optlen = IFNAMSIZ - 1;
488 memset(devname, 0, sizeof(devname));
489
490 ret = -EFAULT;
491 if (copy_from_user(devname, optval, optlen))
492 goto out;
493
David S. Miller000ba2e2009-11-05 22:37:11 -0800494 index = 0;
495 if (devname[0] != '\0') {
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800496 struct net_device *dev;
David S. Miller48788092007-09-14 16:41:03 -0700497
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800498 rcu_read_lock();
499 dev = dev_get_by_name_rcu(net, devname);
500 if (dev)
501 index = dev->ifindex;
502 rcu_read_unlock();
David S. Miller48788092007-09-14 16:41:03 -0700503 ret = -ENODEV;
504 if (!dev)
505 goto out;
David S. Miller48788092007-09-14 16:41:03 -0700506 }
507
508 lock_sock(sk);
509 sk->sk_bound_dev_if = index;
510 sk_dst_reset(sk);
511 release_sock(sk);
512
513 ret = 0;
514
515out:
516#endif
517
518 return ret;
519}
520
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800521static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
522{
523 if (valbool)
524 sock_set_flag(sk, bit);
525 else
526 sock_reset_flag(sk, bit);
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/*
530 * This is meant for all protocols to use and covers goings on
531 * at the socket level. Everything here is generic.
532 */
533
534int sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700535 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Eric Dumazet2a915252009-05-27 11:30:05 +0000537 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int val;
539 int valbool;
540 struct linger ling;
541 int ret = 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /*
544 * Options without arguments
545 */
546
David S. Miller48788092007-09-14 16:41:03 -0700547 if (optname == SO_BINDTODEVICE)
548 return sock_bindtodevice(sk, optval, optlen);
549
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700550 if (optlen < sizeof(int))
551 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (get_user(val, (int __user *)optval))
554 return -EFAULT;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900555
Eric Dumazet2a915252009-05-27 11:30:05 +0000556 valbool = val ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 lock_sock(sk);
559
Eric Dumazet2a915252009-05-27 11:30:05 +0000560 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700561 case SO_DEBUG:
Eric Dumazet2a915252009-05-27 11:30:05 +0000562 if (val && !capable(CAP_NET_ADMIN))
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700563 ret = -EACCES;
Eric Dumazet2a915252009-05-27 11:30:05 +0000564 else
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800565 sock_valbool_flag(sk, SOCK_DBG, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700566 break;
567 case SO_REUSEADDR:
568 sk->sk_reuse = valbool;
569 break;
570 case SO_TYPE:
Jan Engelhardt49c794e2009-08-04 07:28:28 +0000571 case SO_PROTOCOL:
Jan Engelhardt0d6038e2009-08-04 07:28:29 +0000572 case SO_DOMAIN:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700573 case SO_ERROR:
574 ret = -ENOPROTOOPT;
575 break;
576 case SO_DONTROUTE:
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800577 sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700578 break;
579 case SO_BROADCAST:
580 sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
581 break;
582 case SO_SNDBUF:
583 /* Don't error on this BSD doesn't and if you think
584 about it this is right. Otherwise apps have to
585 play 'guess the biggest size' games. RCVBUF/SNDBUF
586 are treated in BSD as hints */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900587
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700588 if (val > sysctl_wmem_max)
589 val = sysctl_wmem_max;
Patrick McHardyb0573de2005-08-09 19:30:51 -0700590set_sndbuf:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700591 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
592 if ((val * 2) < SOCK_MIN_SNDBUF)
593 sk->sk_sndbuf = SOCK_MIN_SNDBUF;
594 else
595 sk->sk_sndbuf = val * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700597 /*
598 * Wake up sending tasks if we
599 * upped the value.
600 */
601 sk->sk_write_space(sk);
602 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700604 case SO_SNDBUFFORCE:
605 if (!capable(CAP_NET_ADMIN)) {
606 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 break;
608 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700609 goto set_sndbuf;
610
611 case SO_RCVBUF:
612 /* Don't error on this BSD doesn't and if you think
613 about it this is right. Otherwise apps have to
614 play 'guess the biggest size' games. RCVBUF/SNDBUF
615 are treated in BSD as hints */
616
617 if (val > sysctl_rmem_max)
618 val = sysctl_rmem_max;
619set_rcvbuf:
620 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
621 /*
622 * We double it on the way in to account for
623 * "struct sk_buff" etc. overhead. Applications
624 * assume that the SO_RCVBUF setting they make will
625 * allow that much actual data to be received on that
626 * socket.
627 *
628 * Applications are unaware that "struct sk_buff" and
629 * other overheads allocate from the receive buffer
630 * during socket buffer allocation.
631 *
632 * And after considering the possible alternatives,
633 * returning the value we actually used in getsockopt
634 * is the most desirable behavior.
635 */
636 if ((val * 2) < SOCK_MIN_RCVBUF)
637 sk->sk_rcvbuf = SOCK_MIN_RCVBUF;
638 else
639 sk->sk_rcvbuf = val * 2;
640 break;
641
642 case SO_RCVBUFFORCE:
643 if (!capable(CAP_NET_ADMIN)) {
644 ret = -EPERM;
645 break;
646 }
647 goto set_rcvbuf;
648
649 case SO_KEEPALIVE:
650#ifdef CONFIG_INET
651 if (sk->sk_protocol == IPPROTO_TCP)
652 tcp_set_keepalive(sk, valbool);
653#endif
654 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
655 break;
656
657 case SO_OOBINLINE:
658 sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
659 break;
660
661 case SO_NO_CHECK:
662 sk->sk_no_check = valbool;
663 break;
664
665 case SO_PRIORITY:
666 if ((val >= 0 && val <= 6) || capable(CAP_NET_ADMIN))
667 sk->sk_priority = val;
668 else
669 ret = -EPERM;
670 break;
671
672 case SO_LINGER:
673 if (optlen < sizeof(ling)) {
674 ret = -EINVAL; /* 1003.1g */
675 break;
676 }
Eric Dumazet2a915252009-05-27 11:30:05 +0000677 if (copy_from_user(&ling, optval, sizeof(ling))) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700678 ret = -EFAULT;
679 break;
680 }
681 if (!ling.l_onoff)
682 sock_reset_flag(sk, SOCK_LINGER);
683 else {
684#if (BITS_PER_LONG == 32)
685 if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
686 sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
687 else
688#endif
689 sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
690 sock_set_flag(sk, SOCK_LINGER);
691 }
692 break;
693
694 case SO_BSDCOMPAT:
695 sock_warn_obsolete_bsdism("setsockopt");
696 break;
697
698 case SO_PASSCRED:
699 if (valbool)
700 set_bit(SOCK_PASSCRED, &sock->flags);
701 else
702 clear_bit(SOCK_PASSCRED, &sock->flags);
703 break;
704
705 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700706 case SO_TIMESTAMPNS:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700707 if (valbool) {
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700708 if (optname == SO_TIMESTAMP)
709 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
710 else
711 sock_set_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700712 sock_set_flag(sk, SOCK_RCVTSTAMP);
Patrick Ohly20d49472009-02-12 05:03:38 +0000713 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700714 } else {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700715 sock_reset_flag(sk, SOCK_RCVTSTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700716 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
717 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700718 break;
719
Patrick Ohly20d49472009-02-12 05:03:38 +0000720 case SO_TIMESTAMPING:
721 if (val & ~SOF_TIMESTAMPING_MASK) {
Rémi Denis-Courmontf249fb72009-07-20 00:47:04 +0000722 ret = -EINVAL;
Patrick Ohly20d49472009-02-12 05:03:38 +0000723 break;
724 }
725 sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE,
726 val & SOF_TIMESTAMPING_TX_HARDWARE);
727 sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE,
728 val & SOF_TIMESTAMPING_TX_SOFTWARE);
729 sock_valbool_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE,
730 val & SOF_TIMESTAMPING_RX_HARDWARE);
731 if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
732 sock_enable_timestamp(sk,
733 SOCK_TIMESTAMPING_RX_SOFTWARE);
734 else
735 sock_disable_timestamp(sk,
Eric Dumazet08e29af2011-11-28 12:04:18 +0000736 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
Patrick Ohly20d49472009-02-12 05:03:38 +0000737 sock_valbool_flag(sk, SOCK_TIMESTAMPING_SOFTWARE,
738 val & SOF_TIMESTAMPING_SOFTWARE);
739 sock_valbool_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE,
740 val & SOF_TIMESTAMPING_SYS_HARDWARE);
741 sock_valbool_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE,
742 val & SOF_TIMESTAMPING_RAW_HARDWARE);
743 break;
744
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700745 case SO_RCVLOWAT:
746 if (val < 0)
747 val = INT_MAX;
748 sk->sk_rcvlowat = val ? : 1;
749 break;
750
751 case SO_RCVTIMEO:
752 ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
753 break;
754
755 case SO_SNDTIMEO:
756 ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
757 break;
758
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700759 case SO_ATTACH_FILTER:
760 ret = -EINVAL;
761 if (optlen == sizeof(struct sock_fprog)) {
762 struct sock_fprog fprog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700764 ret = -EFAULT;
765 if (copy_from_user(&fprog, optval, sizeof(fprog)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700768 ret = sk_attach_filter(&fprog, sk);
769 }
770 break;
771
772 case SO_DETACH_FILTER:
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700773 ret = sk_detach_filter(sk);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700774 break;
775
776 case SO_PASSSEC:
777 if (valbool)
778 set_bit(SOCK_PASSSEC, &sock->flags);
779 else
780 clear_bit(SOCK_PASSSEC, &sock->flags);
781 break;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800782 case SO_MARK:
783 if (!capable(CAP_NET_ADMIN))
784 ret = -EPERM;
Eric Dumazet2a915252009-05-27 11:30:05 +0000785 else
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800786 sk->sk_mark = val;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800787 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* We implement the SO_SNDLOWAT etc to
790 not be settable (1003.1g 5.3) */
Neil Horman3b885782009-10-12 13:26:31 -0700791 case SO_RXQ_OVFL:
Johannes Berg8083f0f2011-10-07 03:30:20 +0000792 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
Neil Horman3b885782009-10-12 13:26:31 -0700793 break;
Johannes Berg6e3e9392011-11-09 10:15:42 +0100794
795 case SO_WIFI_STATUS:
796 sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
797 break;
798
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700799 default:
800 ret = -ENOPROTOOPT;
801 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 release_sock(sk);
804 return ret;
805}
Eric Dumazet2a915252009-05-27 11:30:05 +0000806EXPORT_SYMBOL(sock_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000809void cred_to_ucred(struct pid *pid, const struct cred *cred,
810 struct ucred *ucred)
811{
812 ucred->pid = pid_vnr(pid);
813 ucred->uid = ucred->gid = -1;
814 if (cred) {
815 struct user_namespace *current_ns = current_user_ns();
816
817 ucred->uid = user_ns_map_uid(current_ns, cred, cred->euid);
818 ucred->gid = user_ns_map_gid(current_ns, cred, cred->egid);
819 }
820}
David S. Miller39247732010-06-16 16:18:25 -0700821EXPORT_SYMBOL_GPL(cred_to_ucred);
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823int sock_getsockopt(struct socket *sock, int level, int optname,
824 char __user *optval, int __user *optlen)
825{
826 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900827
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700828 union {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900829 int val;
830 struct linger ling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct timeval tm;
832 } v;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900833
H Hartley Sweeten4d0392b2010-01-15 01:08:58 -0800834 int lv = sizeof(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int len;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900836
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700837 if (get_user(len, optlen))
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900838 return -EFAULT;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700839 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900841
Eugene Teo50fee1d2009-02-23 15:38:41 -0800842 memset(&v, 0, sizeof(v));
Clément Lecignedf0bca02009-02-12 16:59:09 -0800843
Eric Dumazet2a915252009-05-27 11:30:05 +0000844 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700845 case SO_DEBUG:
846 v.val = sock_flag(sk, SOCK_DBG);
847 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900848
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700849 case SO_DONTROUTE:
850 v.val = sock_flag(sk, SOCK_LOCALROUTE);
851 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900852
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700853 case SO_BROADCAST:
854 v.val = !!sock_flag(sk, SOCK_BROADCAST);
855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700857 case SO_SNDBUF:
858 v.val = sk->sk_sndbuf;
859 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900860
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700861 case SO_RCVBUF:
862 v.val = sk->sk_rcvbuf;
863 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700865 case SO_REUSEADDR:
866 v.val = sk->sk_reuse;
867 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700869 case SO_KEEPALIVE:
870 v.val = !!sock_flag(sk, SOCK_KEEPOPEN);
871 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700873 case SO_TYPE:
874 v.val = sk->sk_type;
875 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Jan Engelhardt49c794e2009-08-04 07:28:28 +0000877 case SO_PROTOCOL:
878 v.val = sk->sk_protocol;
879 break;
880
Jan Engelhardt0d6038e2009-08-04 07:28:29 +0000881 case SO_DOMAIN:
882 v.val = sk->sk_family;
883 break;
884
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700885 case SO_ERROR:
886 v.val = -sock_error(sk);
Eric Dumazet2a915252009-05-27 11:30:05 +0000887 if (v.val == 0)
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700888 v.val = xchg(&sk->sk_err_soft, 0);
889 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700891 case SO_OOBINLINE:
892 v.val = !!sock_flag(sk, SOCK_URGINLINE);
893 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900894
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700895 case SO_NO_CHECK:
896 v.val = sk->sk_no_check;
897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700899 case SO_PRIORITY:
900 v.val = sk->sk_priority;
901 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900902
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700903 case SO_LINGER:
904 lv = sizeof(v.ling);
905 v.ling.l_onoff = !!sock_flag(sk, SOCK_LINGER);
906 v.ling.l_linger = sk->sk_lingertime / HZ;
907 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900908
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700909 case SO_BSDCOMPAT:
910 sock_warn_obsolete_bsdism("getsockopt");
911 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700913 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700914 v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
915 !sock_flag(sk, SOCK_RCVTSTAMPNS);
916 break;
917
918 case SO_TIMESTAMPNS:
919 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Patrick Ohly20d49472009-02-12 05:03:38 +0000922 case SO_TIMESTAMPING:
923 v.val = 0;
924 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
925 v.val |= SOF_TIMESTAMPING_TX_HARDWARE;
926 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
927 v.val |= SOF_TIMESTAMPING_TX_SOFTWARE;
928 if (sock_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE))
929 v.val |= SOF_TIMESTAMPING_RX_HARDWARE;
930 if (sock_flag(sk, SOCK_TIMESTAMPING_RX_SOFTWARE))
931 v.val |= SOF_TIMESTAMPING_RX_SOFTWARE;
932 if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE))
933 v.val |= SOF_TIMESTAMPING_SOFTWARE;
934 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE))
935 v.val |= SOF_TIMESTAMPING_SYS_HARDWARE;
936 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE))
937 v.val |= SOF_TIMESTAMPING_RAW_HARDWARE;
938 break;
939
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700940 case SO_RCVTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +0000941 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700942 if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
943 v.tm.tv_sec = 0;
944 v.tm.tv_usec = 0;
945 } else {
946 v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
947 v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * 1000000) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700949 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700951 case SO_SNDTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +0000952 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700953 if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
954 v.tm.tv_sec = 0;
955 v.tm.tv_usec = 0;
956 } else {
957 v.tm.tv_sec = sk->sk_sndtimeo / HZ;
958 v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * 1000000) / HZ;
959 }
960 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700962 case SO_RCVLOWAT:
963 v.val = sk->sk_rcvlowat;
964 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700965
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700966 case SO_SNDLOWAT:
Eric Dumazet2a915252009-05-27 11:30:05 +0000967 v.val = 1;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700970 case SO_PASSCRED:
971 v.val = test_bit(SOCK_PASSCRED, &sock->flags) ? 1 : 0;
972 break;
973
974 case SO_PEERCRED:
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000975 {
976 struct ucred peercred;
977 if (len > sizeof(peercred))
978 len = sizeof(peercred);
979 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
980 if (copy_to_user(optval, &peercred, len))
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700981 return -EFAULT;
982 goto lenout;
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000983 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700984
985 case SO_PEERNAME:
986 {
987 char address[128];
988
989 if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
990 return -ENOTCONN;
991 if (lv < len)
992 return -EINVAL;
993 if (copy_to_user(optval, address, len))
994 return -EFAULT;
995 goto lenout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700997
998 /* Dubious BSD thing... Probably nobody even uses it, but
999 * the UNIX standard wants it for whatever reason... -DaveM
1000 */
1001 case SO_ACCEPTCONN:
1002 v.val = sk->sk_state == TCP_LISTEN;
1003 break;
1004
1005 case SO_PASSSEC:
1006 v.val = test_bit(SOCK_PASSSEC, &sock->flags) ? 1 : 0;
1007 break;
1008
1009 case SO_PEERSEC:
1010 return security_socket_getpeersec_stream(sock, optval, optlen, len);
1011
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001012 case SO_MARK:
1013 v.val = sk->sk_mark;
1014 break;
1015
Neil Horman3b885782009-10-12 13:26:31 -07001016 case SO_RXQ_OVFL:
1017 v.val = !!sock_flag(sk, SOCK_RXQ_OVFL);
1018 break;
1019
Johannes Berg6e3e9392011-11-09 10:15:42 +01001020 case SO_WIFI_STATUS:
1021 v.val = !!sock_flag(sk, SOCK_WIFI_STATUS);
1022 break;
1023
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001024 default:
1025 return -ENOPROTOOPT;
1026 }
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (len > lv)
1029 len = lv;
1030 if (copy_to_user(optval, &v, len))
1031 return -EFAULT;
1032lenout:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001033 if (put_user(len, optlen))
1034 return -EFAULT;
1035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001038/*
1039 * Initialize an sk_lock.
1040 *
1041 * (We also register the sk_lock with the lock validator.)
1042 */
Dave Jonesb6f99a22007-03-22 12:27:49 -07001043static inline void sock_lock_init(struct sock *sk)
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001044{
Peter Zijlstraed075362006-12-06 20:35:24 -08001045 sock_lock_init_class_and_name(sk,
1046 af_family_slock_key_strings[sk->sk_family],
1047 af_family_slock_keys + sk->sk_family,
1048 af_family_key_strings[sk->sk_family],
1049 af_family_keys + sk->sk_family);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001050}
1051
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001052/*
1053 * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
1054 * even temporarly, because of RCU lookups. sk_node should also be left as is.
Eric Dumazet68835ab2010-11-30 19:04:07 +00001055 * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001056 */
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001057static void sock_copy(struct sock *nsk, const struct sock *osk)
1058{
1059#ifdef CONFIG_SECURITY_NETWORK
1060 void *sptr = nsk->sk_security;
1061#endif
Eric Dumazet68835ab2010-11-30 19:04:07 +00001062 memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
1063
1064 memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
1065 osk->sk_prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
1066
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001067#ifdef CONFIG_SECURITY_NETWORK
1068 nsk->sk_security = sptr;
1069 security_sk_clone(osk, nsk);
1070#endif
1071}
1072
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001073/*
1074 * caches using SLAB_DESTROY_BY_RCU should let .next pointer from nulls nodes
1075 * un-modified. Special care is taken when initializing object to zero.
1076 */
1077static inline void sk_prot_clear_nulls(struct sock *sk, int size)
1078{
1079 if (offsetof(struct sock, sk_node.next) != 0)
1080 memset(sk, 0, offsetof(struct sock, sk_node.next));
1081 memset(&sk->sk_node.pprev, 0,
1082 size - offsetof(struct sock, sk_node.pprev));
1083}
1084
1085void sk_prot_clear_portaddr_nulls(struct sock *sk, int size)
1086{
1087 unsigned long nulls1, nulls2;
1088
1089 nulls1 = offsetof(struct sock, __sk_common.skc_node.next);
1090 nulls2 = offsetof(struct sock, __sk_common.skc_portaddr_node.next);
1091 if (nulls1 > nulls2)
1092 swap(nulls1, nulls2);
1093
1094 if (nulls1 != 0)
1095 memset((char *)sk, 0, nulls1);
1096 memset((char *)sk + nulls1 + sizeof(void *), 0,
1097 nulls2 - nulls1 - sizeof(void *));
1098 memset((char *)sk + nulls2 + sizeof(void *), 0,
1099 size - nulls2 - sizeof(void *));
1100}
1101EXPORT_SYMBOL(sk_prot_clear_portaddr_nulls);
1102
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001103static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
1104 int family)
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001105{
1106 struct sock *sk;
1107 struct kmem_cache *slab;
1108
1109 slab = prot->slab;
Eric Dumazete912b112009-07-08 19:36:05 +00001110 if (slab != NULL) {
1111 sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
1112 if (!sk)
1113 return sk;
1114 if (priority & __GFP_ZERO) {
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001115 if (prot->clear_sk)
1116 prot->clear_sk(sk, prot->obj_size);
1117 else
1118 sk_prot_clear_nulls(sk, prot->obj_size);
Eric Dumazete912b112009-07-08 19:36:05 +00001119 }
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001120 } else
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001121 sk = kmalloc(prot->obj_size, priority);
1122
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001123 if (sk != NULL) {
Vegard Nossuma98b65a2009-02-26 14:46:57 +01001124 kmemcheck_annotate_bitfield(sk, flags);
1125
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001126 if (security_sk_alloc(sk, family, priority))
1127 goto out_free;
1128
1129 if (!try_module_get(prot->owner))
1130 goto out_free_sec;
Krishna Kumare022f0b2009-10-19 23:46:20 +00001131 sk_tx_queue_clear(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001132 }
1133
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001134 return sk;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001135
1136out_free_sec:
1137 security_sk_free(sk);
1138out_free:
1139 if (slab != NULL)
1140 kmem_cache_free(slab, sk);
1141 else
1142 kfree(sk);
1143 return NULL;
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001144}
1145
1146static void sk_prot_free(struct proto *prot, struct sock *sk)
1147{
1148 struct kmem_cache *slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001149 struct module *owner;
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001150
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001151 owner = prot->owner;
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001152 slab = prot->slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001153
1154 security_sk_free(sk);
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001155 if (slab != NULL)
1156 kmem_cache_free(slab, sk);
1157 else
1158 kfree(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001159 module_put(owner);
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001160}
1161
Herbert Xuf8451722010-05-24 00:12:34 -07001162#ifdef CONFIG_CGROUPS
1163void sock_update_classid(struct sock *sk)
1164{
Paul E. McKenney11441822010-10-06 17:15:35 -07001165 u32 classid;
Herbert Xuf8451722010-05-24 00:12:34 -07001166
Paul E. McKenney11441822010-10-06 17:15:35 -07001167 rcu_read_lock(); /* doing current task, which cannot vanish. */
1168 classid = task_cls_classid(current);
1169 rcu_read_unlock();
Herbert Xuf8451722010-05-24 00:12:34 -07001170 if (classid && classid != sk->sk_classid)
1171 sk->sk_classid = classid;
1172}
Herbert Xu82862742010-05-24 00:14:10 -07001173EXPORT_SYMBOL(sock_update_classid);
Neil Horman5bc14212011-11-22 05:10:51 +00001174
1175void sock_update_netprioidx(struct sock *sk)
1176{
1177 struct cgroup_netprio_state *state;
1178 if (in_interrupt())
1179 return;
1180 rcu_read_lock();
1181 state = task_netprio_state(current);
1182 sk->sk_cgrp_prioidx = state ? state->prioidx : 0;
1183 rcu_read_unlock();
1184}
1185EXPORT_SYMBOL_GPL(sock_update_netprioidx);
Herbert Xuf8451722010-05-24 00:12:34 -07001186#endif
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/**
1189 * sk_alloc - All socket objects are allocated here
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001190 * @net: the applicable net namespace
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001191 * @family: protocol family
1192 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1193 * @prot: struct proto associated with this new sock instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001195struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001196 struct proto *prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001198 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001200 sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (sk) {
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001202 sk->sk_family = family;
1203 /*
1204 * See comment in struct sock definition to understand
1205 * why we need sk_prot_creator -acme
1206 */
1207 sk->sk_prot = sk->sk_prot_creator = prot;
1208 sock_lock_init(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001209 sock_net_set(sk, get_net(net));
Jarek Poplawskid66ee052009-08-30 23:15:36 +00001210 atomic_set(&sk->sk_wmem_alloc, 1);
Herbert Xuf8451722010-05-24 00:12:34 -07001211
1212 sock_update_classid(sk);
Neil Horman5bc14212011-11-22 05:10:51 +00001213 sock_update_netprioidx(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Frank Filza79af592005-09-27 15:23:38 -07001215
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001216 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
Eric Dumazet2a915252009-05-27 11:30:05 +00001218EXPORT_SYMBOL(sk_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Eric Dumazet2b85a342009-06-11 02:55:43 -07001220static void __sk_free(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 struct sk_filter *filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (sk->sk_destruct)
1225 sk->sk_destruct(sk);
1226
Paul E. McKenneya898def2010-02-22 17:04:49 -08001227 filter = rcu_dereference_check(sk->sk_filter,
1228 atomic_read(&sk->sk_wmem_alloc) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (filter) {
Pavel Emelyanov309dd5f2007-10-17 21:21:51 -07001230 sk_filter_uncharge(sk, filter);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001231 RCU_INIT_POINTER(sk->sk_filter, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
Eric Dumazet08e29af2011-11-28 12:04:18 +00001234 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (atomic_read(&sk->sk_omem_alloc))
1237 printk(KERN_DEBUG "%s: optmem leakage (%d bytes) detected.\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001238 __func__, atomic_read(&sk->sk_omem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001240 if (sk->sk_peer_cred)
1241 put_cred(sk->sk_peer_cred);
1242 put_pid(sk->sk_peer_pid);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001243 put_net(sock_net(sk));
Pavel Emelyanovc308c1b2007-11-01 00:33:50 -07001244 sk_prot_free(sk->sk_prot_creator, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
Eric Dumazet2b85a342009-06-11 02:55:43 -07001246
1247void sk_free(struct sock *sk)
1248{
1249 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001250 * We subtract one from sk_wmem_alloc and can know if
Eric Dumazet2b85a342009-06-11 02:55:43 -07001251 * some packets are still in some tx queue.
1252 * If not null, sock_wfree() will call __sk_free(sk) later
1253 */
1254 if (atomic_dec_and_test(&sk->sk_wmem_alloc))
1255 __sk_free(sk);
1256}
Eric Dumazet2a915252009-05-27 11:30:05 +00001257EXPORT_SYMBOL(sk_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Denis V. Lunevedf02082008-02-29 11:18:32 -08001259/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001260 * Last sock_put should drop reference to sk->sk_net. It has already
1261 * been dropped in sk_change_net. Taking reference to stopping namespace
Denis V. Lunevedf02082008-02-29 11:18:32 -08001262 * is not an option.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001263 * Take reference to a socket to remove it from hash _alive_ and after that
Denis V. Lunevedf02082008-02-29 11:18:32 -08001264 * destroy it in the context of init_net.
1265 */
1266void sk_release_kernel(struct sock *sk)
1267{
1268 if (sk == NULL || sk->sk_socket == NULL)
1269 return;
1270
1271 sock_hold(sk);
1272 sock_release(sk->sk_socket);
Denis V. Lunev65a18ec2008-04-16 01:59:46 -07001273 release_net(sock_net(sk));
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001274 sock_net_set(sk, get_net(&init_net));
Denis V. Lunevedf02082008-02-29 11:18:32 -08001275 sock_put(sk);
1276}
David S. Miller45af1752008-02-29 11:33:19 -08001277EXPORT_SYMBOL(sk_release_kernel);
Denis V. Lunevedf02082008-02-29 11:18:32 -08001278
Eric Dumazete56c57d2011-11-08 17:07:07 -05001279/**
1280 * sk_clone_lock - clone a socket, and lock its clone
1281 * @sk: the socket to clone
1282 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1283 *
1284 * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
1285 */
1286struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001287{
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001288 struct sock *newsk;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001289
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001290 newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001291 if (newsk != NULL) {
1292 struct sk_filter *filter;
1293
Venkat Yekkirala892c1412006-08-04 23:08:56 -07001294 sock_copy(newsk, sk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001295
1296 /* SANITY */
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001297 get_net(sock_net(newsk));
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001298 sk_node_init(&newsk->sk_node);
1299 sock_lock_init(newsk);
1300 bh_lock_sock(newsk);
Eric Dumazetfa438cc2007-03-04 16:05:44 -08001301 newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
Zhu Yi8eae9392010-03-04 18:01:40 +00001302 newsk->sk_backlog.len = 0;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001303
1304 atomic_set(&newsk->sk_rmem_alloc, 0);
Eric Dumazet2b85a342009-06-11 02:55:43 -07001305 /*
1306 * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
1307 */
1308 atomic_set(&newsk->sk_wmem_alloc, 1);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001309 atomic_set(&newsk->sk_omem_alloc, 0);
1310 skb_queue_head_init(&newsk->sk_receive_queue);
1311 skb_queue_head_init(&newsk->sk_write_queue);
Chris Leech97fc2f02006-05-23 17:55:33 -07001312#ifdef CONFIG_NET_DMA
1313 skb_queue_head_init(&newsk->sk_async_wait_queue);
1314#endif
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001315
Eric Dumazetb6c67122010-04-08 23:03:29 +00001316 spin_lock_init(&newsk->sk_dst_lock);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001317 rwlock_init(&newsk->sk_callback_lock);
Peter Zijlstra443aef02007-07-19 01:49:00 -07001318 lockdep_set_class_and_name(&newsk->sk_callback_lock,
1319 af_callback_keys + newsk->sk_family,
1320 af_family_clock_key_strings[newsk->sk_family]);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001321
1322 newsk->sk_dst_cache = NULL;
1323 newsk->sk_wmem_queued = 0;
1324 newsk->sk_forward_alloc = 0;
1325 newsk->sk_send_head = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001326 newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
1327
1328 sock_reset_flag(newsk, SOCK_DONE);
1329 skb_queue_head_init(&newsk->sk_error_queue);
1330
Eric Dumazet0d7da9d2010-10-25 03:47:05 +00001331 filter = rcu_dereference_protected(newsk->sk_filter, 1);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001332 if (filter != NULL)
1333 sk_filter_charge(newsk, filter);
1334
1335 if (unlikely(xfrm_sk_clone_policy(newsk))) {
1336 /* It is still raw copy of parent, so invalidate
1337 * destructor and make plain sk_free() */
1338 newsk->sk_destruct = NULL;
Thomas Gleixnerb0691c82011-10-25 02:30:50 +00001339 bh_unlock_sock(newsk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001340 sk_free(newsk);
1341 newsk = NULL;
1342 goto out;
1343 }
1344
1345 newsk->sk_err = 0;
1346 newsk->sk_priority = 0;
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001347 /*
1348 * Before updating sk_refcnt, we must commit prior changes to memory
1349 * (Documentation/RCU/rculist_nulls.txt for details)
1350 */
1351 smp_wmb();
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001352 atomic_set(&newsk->sk_refcnt, 2);
1353
1354 /*
1355 * Increment the counter in the same struct proto as the master
1356 * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
1357 * is the same as sk->sk_prot->socks, as this field was copied
1358 * with memcpy).
1359 *
1360 * This _changes_ the previous behaviour, where
1361 * tcp_create_openreq_child always was incrementing the
1362 * equivalent to tcp_prot->socks (inet_sock_nr), so this have
1363 * to be taken into account in all callers. -acme
1364 */
1365 sk_refcnt_debug_inc(newsk);
David S. Miller972692e2008-06-17 22:41:38 -07001366 sk_set_socket(newsk, NULL);
Eric Dumazet43815482010-04-29 11:01:49 +00001367 newsk->sk_wq = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001368
1369 if (newsk->sk_prot->sockets_allocated)
Glauber Costa180d8cd2011-12-11 21:47:02 +00001370 sk_sockets_allocated_inc(newsk);
Octavian Purdila704da5602010-01-08 00:00:09 -08001371
Eric Dumazet08e29af2011-11-28 12:04:18 +00001372 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
Octavian Purdila704da5602010-01-08 00:00:09 -08001373 net_enable_timestamp();
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001374 }
1375out:
1376 return newsk;
1377}
Eric Dumazete56c57d2011-11-08 17:07:07 -05001378EXPORT_SYMBOL_GPL(sk_clone_lock);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001379
Andi Kleen99580892007-04-20 17:12:43 -07001380void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
1381{
1382 __sk_dst_set(sk, dst);
1383 sk->sk_route_caps = dst->dev->features;
1384 if (sk->sk_route_caps & NETIF_F_GSO)
Herbert Xu4fcd6b92007-05-31 22:15:50 -07001385 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
Eric Dumazeta4654192010-05-16 00:36:33 -07001386 sk->sk_route_caps &= ~sk->sk_route_nocaps;
Andi Kleen99580892007-04-20 17:12:43 -07001387 if (sk_can_gso(sk)) {
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001388 if (dst->header_len) {
Andi Kleen99580892007-04-20 17:12:43 -07001389 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001390 } else {
Andi Kleen99580892007-04-20 17:12:43 -07001391 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001392 sk->sk_gso_max_size = dst->dev->gso_max_size;
1393 }
Andi Kleen99580892007-04-20 17:12:43 -07001394 }
1395}
1396EXPORT_SYMBOL_GPL(sk_setup_caps);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398void __init sk_init(void)
1399{
Jan Beulich44813742009-09-21 17:03:05 -07001400 if (totalram_pages <= 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 sysctl_wmem_max = 32767;
1402 sysctl_rmem_max = 32767;
1403 sysctl_wmem_default = 32767;
1404 sysctl_rmem_default = 32767;
Jan Beulich44813742009-09-21 17:03:05 -07001405 } else if (totalram_pages >= 131072) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 sysctl_wmem_max = 131071;
1407 sysctl_rmem_max = 131071;
1408 }
1409}
1410
1411/*
1412 * Simple resource managers for sockets.
1413 */
1414
1415
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001416/*
1417 * Write buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
1419void sock_wfree(struct sk_buff *skb)
1420{
1421 struct sock *sk = skb->sk;
Eric Dumazetd99927f2009-09-24 10:49:24 +00001422 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Eric Dumazetd99927f2009-09-24 10:49:24 +00001424 if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
1425 /*
1426 * Keep a reference on sk_wmem_alloc, this will be released
1427 * after sk_write_space() call
1428 */
1429 atomic_sub(len - 1, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 sk->sk_write_space(sk);
Eric Dumazetd99927f2009-09-24 10:49:24 +00001431 len = 1;
1432 }
Eric Dumazet2b85a342009-06-11 02:55:43 -07001433 /*
Eric Dumazetd99927f2009-09-24 10:49:24 +00001434 * if sk_wmem_alloc reaches 0, we must finish what sk_free()
1435 * could not do because of in-flight packets
Eric Dumazet2b85a342009-06-11 02:55:43 -07001436 */
Eric Dumazetd99927f2009-09-24 10:49:24 +00001437 if (atomic_sub_and_test(len, &sk->sk_wmem_alloc))
Eric Dumazet2b85a342009-06-11 02:55:43 -07001438 __sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
Eric Dumazet2a915252009-05-27 11:30:05 +00001440EXPORT_SYMBOL(sock_wfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001442/*
1443 * Read buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 */
1445void sock_rfree(struct sk_buff *skb)
1446{
1447 struct sock *sk = skb->sk;
Eric Dumazetd361fd52010-07-10 22:45:17 +00001448 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Eric Dumazetd361fd52010-07-10 22:45:17 +00001450 atomic_sub(len, &sk->sk_rmem_alloc);
1451 sk_mem_uncharge(sk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
Eric Dumazet2a915252009-05-27 11:30:05 +00001453EXPORT_SYMBOL(sock_rfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455
1456int sock_i_uid(struct sock *sk)
1457{
1458 int uid;
1459
Eric Dumazetf064af12010-09-22 12:43:39 +00001460 read_lock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : 0;
Eric Dumazetf064af12010-09-22 12:43:39 +00001462 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return uid;
1464}
Eric Dumazet2a915252009-05-27 11:30:05 +00001465EXPORT_SYMBOL(sock_i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467unsigned long sock_i_ino(struct sock *sk)
1468{
1469 unsigned long ino;
1470
Eric Dumazetf064af12010-09-22 12:43:39 +00001471 read_lock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
Eric Dumazetf064af12010-09-22 12:43:39 +00001473 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return ino;
1475}
Eric Dumazet2a915252009-05-27 11:30:05 +00001476EXPORT_SYMBOL(sock_i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478/*
1479 * Allocate a skb from the socket's send buffer.
1480 */
Victor Fusco86a76ca2005-07-08 14:57:47 -07001481struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
Al Virodd0fc662005-10-07 07:46:04 +01001482 gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
Eric Dumazet2a915252009-05-27 11:30:05 +00001485 struct sk_buff *skb = alloc_skb(size, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (skb) {
1487 skb_set_owner_w(skb, sk);
1488 return skb;
1489 }
1490 }
1491 return NULL;
1492}
Eric Dumazet2a915252009-05-27 11:30:05 +00001493EXPORT_SYMBOL(sock_wmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495/*
1496 * Allocate a skb from the socket's receive buffer.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001497 */
Victor Fusco86a76ca2005-07-08 14:57:47 -07001498struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force,
Al Virodd0fc662005-10-07 07:46:04 +01001499 gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 if (force || atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
1502 struct sk_buff *skb = alloc_skb(size, priority);
1503 if (skb) {
1504 skb_set_owner_r(skb, sk);
1505 return skb;
1506 }
1507 }
1508 return NULL;
1509}
1510
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001511/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 * Allocate a memory block from the socket's option memory buffer.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001513 */
Al Virodd0fc662005-10-07 07:46:04 +01001514void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 if ((unsigned)size <= sysctl_optmem_max &&
1517 atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
1518 void *mem;
1519 /* First do the add, to avoid the race if kmalloc
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001520 * might sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 */
1522 atomic_add(size, &sk->sk_omem_alloc);
1523 mem = kmalloc(size, priority);
1524 if (mem)
1525 return mem;
1526 atomic_sub(size, &sk->sk_omem_alloc);
1527 }
1528 return NULL;
1529}
Eric Dumazet2a915252009-05-27 11:30:05 +00001530EXPORT_SYMBOL(sock_kmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532/*
1533 * Free an option memory block.
1534 */
1535void sock_kfree_s(struct sock *sk, void *mem, int size)
1536{
1537 kfree(mem);
1538 atomic_sub(size, &sk->sk_omem_alloc);
1539}
Eric Dumazet2a915252009-05-27 11:30:05 +00001540EXPORT_SYMBOL(sock_kfree_s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542/* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
1543 I think, these locks should be removed for datagram sockets.
1544 */
Eric Dumazet2a915252009-05-27 11:30:05 +00001545static long sock_wait_for_wmem(struct sock *sk, long timeo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 DEFINE_WAIT(wait);
1548
1549 clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1550 for (;;) {
1551 if (!timeo)
1552 break;
1553 if (signal_pending(current))
1554 break;
1555 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Eric Dumazetaa395142010-04-20 13:03:51 +00001556 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
1558 break;
1559 if (sk->sk_shutdown & SEND_SHUTDOWN)
1560 break;
1561 if (sk->sk_err)
1562 break;
1563 timeo = schedule_timeout(timeo);
1564 }
Eric Dumazetaa395142010-04-20 13:03:51 +00001565 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return timeo;
1567}
1568
1569
1570/*
1571 * Generic send/receive buffer handlers
1572 */
1573
Herbert Xu4cc7f682009-02-04 16:55:54 -08001574struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
1575 unsigned long data_len, int noblock,
1576 int *errcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 struct sk_buff *skb;
Al Viro7d877f32005-10-21 03:20:43 -04001579 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 long timeo;
1581 int err;
1582
1583 gfp_mask = sk->sk_allocation;
1584 if (gfp_mask & __GFP_WAIT)
1585 gfp_mask |= __GFP_REPEAT;
1586
1587 timeo = sock_sndtimeo(sk, noblock);
1588 while (1) {
1589 err = sock_error(sk);
1590 if (err != 0)
1591 goto failure;
1592
1593 err = -EPIPE;
1594 if (sk->sk_shutdown & SEND_SHUTDOWN)
1595 goto failure;
1596
1597 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
Larry Woodmandb38c1792006-11-03 16:05:45 -08001598 skb = alloc_skb(header_len, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (skb) {
1600 int npages;
1601 int i;
1602
1603 /* No pages, we're done... */
1604 if (!data_len)
1605 break;
1606
1607 npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
1608 skb->truesize += data_len;
1609 skb_shinfo(skb)->nr_frags = npages;
1610 for (i = 0; i < npages; i++) {
1611 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 page = alloc_pages(sk->sk_allocation, 0);
1614 if (!page) {
1615 err = -ENOBUFS;
1616 skb_shinfo(skb)->nr_frags = i;
1617 kfree_skb(skb);
1618 goto failure;
1619 }
1620
Ian Campbellea2ab692011-08-22 23:44:58 +00001621 __skb_fill_page_desc(skb, i,
1622 page, 0,
1623 (data_len >= PAGE_SIZE ?
1624 PAGE_SIZE :
1625 data_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 data_len -= PAGE_SIZE;
1627 }
1628
1629 /* Full success... */
1630 break;
1631 }
1632 err = -ENOBUFS;
1633 goto failure;
1634 }
1635 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1636 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1637 err = -EAGAIN;
1638 if (!timeo)
1639 goto failure;
1640 if (signal_pending(current))
1641 goto interrupted;
1642 timeo = sock_wait_for_wmem(sk, timeo);
1643 }
1644
1645 skb_set_owner_w(skb, sk);
1646 return skb;
1647
1648interrupted:
1649 err = sock_intr_errno(timeo);
1650failure:
1651 *errcode = err;
1652 return NULL;
1653}
Herbert Xu4cc7f682009-02-04 16:55:54 -08001654EXPORT_SYMBOL(sock_alloc_send_pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001656struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 int noblock, int *errcode)
1658{
1659 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode);
1660}
Eric Dumazet2a915252009-05-27 11:30:05 +00001661EXPORT_SYMBOL(sock_alloc_send_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663static void __lock_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00001664 __releases(&sk->sk_lock.slock)
1665 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 DEFINE_WAIT(wait);
1668
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001669 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
1671 TASK_UNINTERRUPTIBLE);
1672 spin_unlock_bh(&sk->sk_lock.slock);
1673 schedule();
1674 spin_lock_bh(&sk->sk_lock.slock);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001675 if (!sock_owned_by_user(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 break;
1677 }
1678 finish_wait(&sk->sk_lock.wq, &wait);
1679}
1680
1681static void __release_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00001682 __releases(&sk->sk_lock.slock)
1683 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 struct sk_buff *skb = sk->sk_backlog.head;
1686
1687 do {
1688 sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
1689 bh_unlock_sock(sk);
1690
1691 do {
1692 struct sk_buff *next = skb->next;
1693
Eric Dumazet7fee2262010-05-11 23:19:48 +00001694 WARN_ON_ONCE(skb_dst_is_noref(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 skb->next = NULL;
Peter Zijlstrac57943a2008-10-07 14:18:42 -07001696 sk_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 /*
1699 * We are in process context here with softirqs
1700 * disabled, use cond_resched_softirq() to preempt.
1701 * This is safe to do because we've taken the backlog
1702 * queue private:
1703 */
1704 cond_resched_softirq();
1705
1706 skb = next;
1707 } while (skb != NULL);
1708
1709 bh_lock_sock(sk);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001710 } while ((skb = sk->sk_backlog.head) != NULL);
Zhu Yi8eae9392010-03-04 18:01:40 +00001711
1712 /*
1713 * Doing the zeroing here guarantee we can not loop forever
1714 * while a wild producer attempts to flood us.
1715 */
1716 sk->sk_backlog.len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719/**
1720 * sk_wait_data - wait for data to arrive at sk_receive_queue
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001721 * @sk: sock to wait on
1722 * @timeo: for how long
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 *
1724 * Now socket state including sk->sk_err is changed only under lock,
1725 * hence we may omit checks after joining wait queue.
1726 * We check receive queue before schedule() only as optimization;
1727 * it is very likely that release_sock() added new data.
1728 */
1729int sk_wait_data(struct sock *sk, long *timeo)
1730{
1731 int rc;
1732 DEFINE_WAIT(wait);
1733
Eric Dumazetaa395142010-04-20 13:03:51 +00001734 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1736 rc = sk_wait_event(sk, timeo, !skb_queue_empty(&sk->sk_receive_queue));
1737 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
Eric Dumazetaa395142010-04-20 13:03:51 +00001738 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return rc;
1740}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741EXPORT_SYMBOL(sk_wait_data);
1742
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001743/**
1744 * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
1745 * @sk: socket
1746 * @size: memory size to allocate
1747 * @kind: allocation type
1748 *
1749 * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
1750 * rmem allocation. This function assumes that protocols which have
1751 * memory_pressure use sk_wmem_queued as write buffer accounting.
1752 */
1753int __sk_mem_schedule(struct sock *sk, int size, int kind)
1754{
1755 struct proto *prot = sk->sk_prot;
1756 int amt = sk_mem_pages(size);
Eric Dumazet8d987e52010-11-09 23:24:26 +00001757 long allocated;
Glauber Costae1aab162011-12-11 21:47:03 +00001758 int parent_status = UNDER_LIMIT;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001759
1760 sk->sk_forward_alloc += amt * SK_MEM_QUANTUM;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001761
Glauber Costae1aab162011-12-11 21:47:03 +00001762 allocated = sk_memory_allocated_add(sk, amt, &parent_status);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001763
1764 /* Under limit. */
Glauber Costae1aab162011-12-11 21:47:03 +00001765 if (parent_status == UNDER_LIMIT &&
1766 allocated <= sk_prot_mem_limits(sk, 0)) {
Glauber Costa180d8cd2011-12-11 21:47:02 +00001767 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001768 return 1;
1769 }
1770
Glauber Costae1aab162011-12-11 21:47:03 +00001771 /* Under pressure. (we or our parents) */
1772 if ((parent_status > SOFT_LIMIT) ||
1773 allocated > sk_prot_mem_limits(sk, 1))
Glauber Costa180d8cd2011-12-11 21:47:02 +00001774 sk_enter_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001775
Glauber Costae1aab162011-12-11 21:47:03 +00001776 /* Over hard limit (we or our parents) */
1777 if ((parent_status == OVER_LIMIT) ||
1778 (allocated > sk_prot_mem_limits(sk, 2)))
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001779 goto suppress_allocation;
1780
1781 /* guarantee minimum buffer size under pressure */
1782 if (kind == SK_MEM_RECV) {
1783 if (atomic_read(&sk->sk_rmem_alloc) < prot->sysctl_rmem[0])
1784 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001785
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001786 } else { /* SK_MEM_SEND */
1787 if (sk->sk_type == SOCK_STREAM) {
1788 if (sk->sk_wmem_queued < prot->sysctl_wmem[0])
1789 return 1;
1790 } else if (atomic_read(&sk->sk_wmem_alloc) <
1791 prot->sysctl_wmem[0])
1792 return 1;
1793 }
1794
Glauber Costa180d8cd2011-12-11 21:47:02 +00001795 if (sk_has_memory_pressure(sk)) {
Eric Dumazet17483762008-11-25 21:16:35 -08001796 int alloc;
1797
Glauber Costa180d8cd2011-12-11 21:47:02 +00001798 if (!sk_under_memory_pressure(sk))
Eric Dumazet17483762008-11-25 21:16:35 -08001799 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001800 alloc = sk_sockets_allocated_read_positive(sk);
1801 if (sk_prot_mem_limits(sk, 2) > alloc *
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001802 sk_mem_pages(sk->sk_wmem_queued +
1803 atomic_read(&sk->sk_rmem_alloc) +
1804 sk->sk_forward_alloc))
1805 return 1;
1806 }
1807
1808suppress_allocation:
1809
1810 if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
1811 sk_stream_moderate_sndbuf(sk);
1812
1813 /* Fail only if socket is _under_ its sndbuf.
1814 * In this case we cannot block, so that we have to fail.
1815 */
1816 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
1817 return 1;
1818 }
1819
Satoru Moriya3847ce32011-06-17 12:00:03 +00001820 trace_sock_exceed_buf_limit(sk, prot, allocated);
1821
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001822 /* Alas. Undo changes. */
1823 sk->sk_forward_alloc -= amt * SK_MEM_QUANTUM;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001824
Glauber Costae1aab162011-12-11 21:47:03 +00001825 sk_memory_allocated_sub(sk, amt, parent_status);
Glauber Costa180d8cd2011-12-11 21:47:02 +00001826
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001827 return 0;
1828}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001829EXPORT_SYMBOL(__sk_mem_schedule);
1830
1831/**
1832 * __sk_reclaim - reclaim memory_allocated
1833 * @sk: socket
1834 */
1835void __sk_mem_reclaim(struct sock *sk)
1836{
Glauber Costa180d8cd2011-12-11 21:47:02 +00001837 sk_memory_allocated_sub(sk,
Glauber Costae1aab162011-12-11 21:47:03 +00001838 sk->sk_forward_alloc >> SK_MEM_QUANTUM_SHIFT, 0);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001839 sk->sk_forward_alloc &= SK_MEM_QUANTUM - 1;
1840
Glauber Costa180d8cd2011-12-11 21:47:02 +00001841 if (sk_under_memory_pressure(sk) &&
1842 (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
1843 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001844}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001845EXPORT_SYMBOL(__sk_mem_reclaim);
1846
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/*
1849 * Set of default routines for initialising struct proto_ops when
1850 * the protocol does not support a particular function. In certain
1851 * cases where it makes no sense for a protocol to have a "do nothing"
1852 * function, some default processing is provided.
1853 */
1854
1855int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
1856{
1857 return -EOPNOTSUPP;
1858}
Eric Dumazet2a915252009-05-27 11:30:05 +00001859EXPORT_SYMBOL(sock_no_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001861int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 int len, int flags)
1863{
1864 return -EOPNOTSUPP;
1865}
Eric Dumazet2a915252009-05-27 11:30:05 +00001866EXPORT_SYMBOL(sock_no_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
1869{
1870 return -EOPNOTSUPP;
1871}
Eric Dumazet2a915252009-05-27 11:30:05 +00001872EXPORT_SYMBOL(sock_no_socketpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
1875{
1876 return -EOPNOTSUPP;
1877}
Eric Dumazet2a915252009-05-27 11:30:05 +00001878EXPORT_SYMBOL(sock_no_accept);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001880int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int *len, int peer)
1882{
1883 return -EOPNOTSUPP;
1884}
Eric Dumazet2a915252009-05-27 11:30:05 +00001885EXPORT_SYMBOL(sock_no_getname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Eric Dumazet2a915252009-05-27 11:30:05 +00001887unsigned int sock_no_poll(struct file *file, struct socket *sock, poll_table *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 return 0;
1890}
Eric Dumazet2a915252009-05-27 11:30:05 +00001891EXPORT_SYMBOL(sock_no_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1894{
1895 return -EOPNOTSUPP;
1896}
Eric Dumazet2a915252009-05-27 11:30:05 +00001897EXPORT_SYMBOL(sock_no_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899int sock_no_listen(struct socket *sock, int backlog)
1900{
1901 return -EOPNOTSUPP;
1902}
Eric Dumazet2a915252009-05-27 11:30:05 +00001903EXPORT_SYMBOL(sock_no_listen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905int sock_no_shutdown(struct socket *sock, int how)
1906{
1907 return -EOPNOTSUPP;
1908}
Eric Dumazet2a915252009-05-27 11:30:05 +00001909EXPORT_SYMBOL(sock_no_shutdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911int sock_no_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001912 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 return -EOPNOTSUPP;
1915}
Eric Dumazet2a915252009-05-27 11:30:05 +00001916EXPORT_SYMBOL(sock_no_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918int sock_no_getsockopt(struct socket *sock, int level, int optname,
1919 char __user *optval, int __user *optlen)
1920{
1921 return -EOPNOTSUPP;
1922}
Eric Dumazet2a915252009-05-27 11:30:05 +00001923EXPORT_SYMBOL(sock_no_getsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925int sock_no_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
1926 size_t len)
1927{
1928 return -EOPNOTSUPP;
1929}
Eric Dumazet2a915252009-05-27 11:30:05 +00001930EXPORT_SYMBOL(sock_no_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932int sock_no_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
1933 size_t len, int flags)
1934{
1935 return -EOPNOTSUPP;
1936}
Eric Dumazet2a915252009-05-27 11:30:05 +00001937EXPORT_SYMBOL(sock_no_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1940{
1941 /* Mirror missing mmap method error code */
1942 return -ENODEV;
1943}
Eric Dumazet2a915252009-05-27 11:30:05 +00001944EXPORT_SYMBOL(sock_no_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
1947{
1948 ssize_t res;
1949 struct msghdr msg = {.msg_flags = flags};
1950 struct kvec iov;
1951 char *kaddr = kmap(page);
1952 iov.iov_base = kaddr + offset;
1953 iov.iov_len = size;
1954 res = kernel_sendmsg(sock, &msg, &iov, 1, size);
1955 kunmap(page);
1956 return res;
1957}
Eric Dumazet2a915252009-05-27 11:30:05 +00001958EXPORT_SYMBOL(sock_no_sendpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960/*
1961 * Default Socket Callbacks
1962 */
1963
1964static void sock_def_wakeup(struct sock *sk)
1965{
Eric Dumazet43815482010-04-29 11:01:49 +00001966 struct socket_wq *wq;
1967
1968 rcu_read_lock();
1969 wq = rcu_dereference(sk->sk_wq);
1970 if (wq_has_sleeper(wq))
1971 wake_up_interruptible_all(&wq->wait);
1972 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
1975static void sock_def_error_report(struct sock *sk)
1976{
Eric Dumazet43815482010-04-29 11:01:49 +00001977 struct socket_wq *wq;
1978
1979 rcu_read_lock();
1980 wq = rcu_dereference(sk->sk_wq);
1981 if (wq_has_sleeper(wq))
1982 wake_up_interruptible_poll(&wq->wait, POLLERR);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001983 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
Eric Dumazet43815482010-04-29 11:01:49 +00001984 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
1987static void sock_def_readable(struct sock *sk, int len)
1988{
Eric Dumazet43815482010-04-29 11:01:49 +00001989 struct socket_wq *wq;
1990
1991 rcu_read_lock();
1992 wq = rcu_dereference(sk->sk_wq);
1993 if (wq_has_sleeper(wq))
Eric Dumazet2c6607c2011-01-06 10:54:29 -08001994 wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI |
Davide Libenzi37e55402009-03-31 15:24:21 -07001995 POLLRDNORM | POLLRDBAND);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001996 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Eric Dumazet43815482010-04-29 11:01:49 +00001997 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000static void sock_def_write_space(struct sock *sk)
2001{
Eric Dumazet43815482010-04-29 11:01:49 +00002002 struct socket_wq *wq;
2003
2004 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 /* Do not wake up a writer until he can make "significant"
2007 * progress. --DaveM
2008 */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002009 if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
Eric Dumazet43815482010-04-29 11:01:49 +00002010 wq = rcu_dereference(sk->sk_wq);
2011 if (wq_has_sleeper(wq))
2012 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
Davide Libenzi37e55402009-03-31 15:24:21 -07002013 POLLWRNORM | POLLWRBAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 /* Should agree with poll, otherwise some programs break */
2016 if (sock_writeable(sk))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002017 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019
Eric Dumazet43815482010-04-29 11:01:49 +00002020 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023static void sock_def_destruct(struct sock *sk)
2024{
Jesper Juhla51482b2005-11-08 09:41:34 -08002025 kfree(sk->sk_protinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
2028void sk_send_sigurg(struct sock *sk)
2029{
2030 if (sk->sk_socket && sk->sk_socket->file)
2031 if (send_sigurg(&sk->sk_socket->file->f_owner))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002032 sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
Eric Dumazet2a915252009-05-27 11:30:05 +00002034EXPORT_SYMBOL(sk_send_sigurg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036void sk_reset_timer(struct sock *sk, struct timer_list* timer,
2037 unsigned long expires)
2038{
2039 if (!mod_timer(timer, expires))
2040 sock_hold(sk);
2041}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042EXPORT_SYMBOL(sk_reset_timer);
2043
2044void sk_stop_timer(struct sock *sk, struct timer_list* timer)
2045{
2046 if (timer_pending(timer) && del_timer(timer))
2047 __sock_put(sk);
2048}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049EXPORT_SYMBOL(sk_stop_timer);
2050
2051void sock_init_data(struct socket *sock, struct sock *sk)
2052{
2053 skb_queue_head_init(&sk->sk_receive_queue);
2054 skb_queue_head_init(&sk->sk_write_queue);
2055 skb_queue_head_init(&sk->sk_error_queue);
Chris Leech97fc2f02006-05-23 17:55:33 -07002056#ifdef CONFIG_NET_DMA
2057 skb_queue_head_init(&sk->sk_async_wait_queue);
2058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 sk->sk_send_head = NULL;
2061
2062 init_timer(&sk->sk_timer);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 sk->sk_allocation = GFP_KERNEL;
2065 sk->sk_rcvbuf = sysctl_rmem_default;
2066 sk->sk_sndbuf = sysctl_wmem_default;
2067 sk->sk_state = TCP_CLOSE;
David S. Miller972692e2008-06-17 22:41:38 -07002068 sk_set_socket(sk, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 sock_set_flag(sk, SOCK_ZAPPED);
2071
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002072 if (sock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 sk->sk_type = sock->type;
Eric Dumazet43815482010-04-29 11:01:49 +00002074 sk->sk_wq = sock->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 sock->sk = sk;
2076 } else
Eric Dumazet43815482010-04-29 11:01:49 +00002077 sk->sk_wq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Eric Dumazetb6c67122010-04-08 23:03:29 +00002079 spin_lock_init(&sk->sk_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 rwlock_init(&sk->sk_callback_lock);
Peter Zijlstra443aef02007-07-19 01:49:00 -07002081 lockdep_set_class_and_name(&sk->sk_callback_lock,
2082 af_callback_keys + sk->sk_family,
2083 af_family_clock_key_strings[sk->sk_family]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 sk->sk_state_change = sock_def_wakeup;
2086 sk->sk_data_ready = sock_def_readable;
2087 sk->sk_write_space = sock_def_write_space;
2088 sk->sk_error_report = sock_def_error_report;
2089 sk->sk_destruct = sock_def_destruct;
2090
2091 sk->sk_sndmsg_page = NULL;
2092 sk->sk_sndmsg_off = 0;
2093
Eric W. Biederman109f6e32010-06-13 03:30:14 +00002094 sk->sk_peer_pid = NULL;
2095 sk->sk_peer_cred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 sk->sk_write_pending = 0;
2097 sk->sk_rcvlowat = 1;
2098 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
2099 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
2100
Eric Dumazetf37f0af2008-04-13 21:39:26 -07002101 sk->sk_stamp = ktime_set(-1L, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00002103 /*
2104 * Before updating sk_refcnt, we must commit prior changes to memory
2105 * (Documentation/RCU/rculist_nulls.txt for details)
2106 */
2107 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 atomic_set(&sk->sk_refcnt, 1);
Wang Chen33c732c2007-11-13 20:30:01 -08002109 atomic_set(&sk->sk_drops, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
Eric Dumazet2a915252009-05-27 11:30:05 +00002111EXPORT_SYMBOL(sock_init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002113void lock_sock_nested(struct sock *sk, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 might_sleep();
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002116 spin_lock_bh(&sk->sk_lock.slock);
John Heffnerd2e91172007-09-12 10:44:19 +02002117 if (sk->sk_lock.owned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 __lock_sock(sk);
John Heffnerd2e91172007-09-12 10:44:19 +02002119 sk->sk_lock.owned = 1;
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002120 spin_unlock(&sk->sk_lock.slock);
2121 /*
2122 * The sk_lock has mutex_lock() semantics here:
2123 */
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002124 mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002125 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002127EXPORT_SYMBOL(lock_sock_nested);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002129void release_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002131 /*
2132 * The sk_lock has mutex_unlock() semantics:
2133 */
2134 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
2135
2136 spin_lock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (sk->sk_backlog.tail)
2138 __release_sock(sk);
John Heffnerd2e91172007-09-12 10:44:19 +02002139 sk->sk_lock.owned = 0;
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002140 if (waitqueue_active(&sk->sk_lock.wq))
2141 wake_up(&sk->sk_lock.wq);
2142 spin_unlock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144EXPORT_SYMBOL(release_sock);
2145
Eric Dumazet8a74ad62010-05-26 19:20:18 +00002146/**
2147 * lock_sock_fast - fast version of lock_sock
2148 * @sk: socket
2149 *
2150 * This version should be used for very small section, where process wont block
2151 * return false if fast path is taken
2152 * sk_lock.slock locked, owned = 0, BH disabled
2153 * return true if slow path is taken
2154 * sk_lock.slock unlocked, owned = 1, BH enabled
2155 */
2156bool lock_sock_fast(struct sock *sk)
2157{
2158 might_sleep();
2159 spin_lock_bh(&sk->sk_lock.slock);
2160
2161 if (!sk->sk_lock.owned)
2162 /*
2163 * Note : We must disable BH
2164 */
2165 return false;
2166
2167 __lock_sock(sk);
2168 sk->sk_lock.owned = 1;
2169 spin_unlock(&sk->sk_lock.slock);
2170 /*
2171 * The sk_lock has mutex_lock() semantics here:
2172 */
2173 mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
2174 local_bh_enable();
2175 return true;
2176}
2177EXPORT_SYMBOL(lock_sock_fast);
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002180{
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002181 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (!sock_flag(sk, SOCK_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002183 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002184 tv = ktime_to_timeval(sk->sk_stamp);
2185 if (tv.tv_sec == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return -ENOENT;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002187 if (tv.tv_sec == 0) {
2188 sk->sk_stamp = ktime_get_real();
2189 tv = ktime_to_timeval(sk->sk_stamp);
2190 }
2191 return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193EXPORT_SYMBOL(sock_get_timestamp);
2194
Eric Dumazetae40eb12007-03-18 17:33:16 -07002195int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
2196{
2197 struct timespec ts;
2198 if (!sock_flag(sk, SOCK_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002199 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazetae40eb12007-03-18 17:33:16 -07002200 ts = ktime_to_timespec(sk->sk_stamp);
2201 if (ts.tv_sec == -1)
2202 return -ENOENT;
2203 if (ts.tv_sec == 0) {
2204 sk->sk_stamp = ktime_get_real();
2205 ts = ktime_to_timespec(sk->sk_stamp);
2206 }
2207 return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
2208}
2209EXPORT_SYMBOL(sock_get_timestampns);
2210
Patrick Ohly20d49472009-02-12 05:03:38 +00002211void sock_enable_timestamp(struct sock *sk, int flag)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002212{
Patrick Ohly20d49472009-02-12 05:03:38 +00002213 if (!sock_flag(sk, flag)) {
Eric Dumazet08e29af2011-11-28 12:04:18 +00002214 unsigned long previous_flags = sk->sk_flags;
2215
Patrick Ohly20d49472009-02-12 05:03:38 +00002216 sock_set_flag(sk, flag);
2217 /*
2218 * we just set one of the two flags which require net
2219 * time stamping, but time stamping might have been on
2220 * already because of the other one
2221 */
Eric Dumazet08e29af2011-11-28 12:04:18 +00002222 if (!(previous_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002223 net_enable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
2225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227/*
2228 * Get a socket option on an socket.
2229 *
2230 * FIX: POSIX 1003.1g is very ambiguous here. It states that
2231 * asynchronous errors should be reported by getsockopt. We assume
2232 * this means if you specify SO_ERROR (otherwise whats the point of it).
2233 */
2234int sock_common_getsockopt(struct socket *sock, int level, int optname,
2235 char __user *optval, int __user *optlen)
2236{
2237 struct sock *sk = sock->sk;
2238
2239 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2240}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241EXPORT_SYMBOL(sock_common_getsockopt);
2242
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002243#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002244int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
2245 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002246{
2247 struct sock *sk = sock->sk;
2248
Johannes Berg1e51f952007-03-06 13:44:06 -08002249 if (sk->sk_prot->compat_getsockopt != NULL)
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002250 return sk->sk_prot->compat_getsockopt(sk, level, optname,
2251 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002252 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2253}
2254EXPORT_SYMBOL(compat_sock_common_getsockopt);
2255#endif
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
2258 struct msghdr *msg, size_t size, int flags)
2259{
2260 struct sock *sk = sock->sk;
2261 int addr_len = 0;
2262 int err;
2263
2264 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
2265 flags & ~MSG_DONTWAIT, &addr_len);
2266 if (err >= 0)
2267 msg->msg_namelen = addr_len;
2268 return err;
2269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270EXPORT_SYMBOL(sock_common_recvmsg);
2271
2272/*
2273 * Set socket options on an inet socket.
2274 */
2275int sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002276 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
2278 struct sock *sk = sock->sk;
2279
2280 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282EXPORT_SYMBOL(sock_common_setsockopt);
2283
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002284#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002285int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002286 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002287{
2288 struct sock *sk = sock->sk;
2289
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002290 if (sk->sk_prot->compat_setsockopt != NULL)
2291 return sk->sk_prot->compat_setsockopt(sk, level, optname,
2292 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002293 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2294}
2295EXPORT_SYMBOL(compat_sock_common_setsockopt);
2296#endif
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298void sk_common_release(struct sock *sk)
2299{
2300 if (sk->sk_prot->destroy)
2301 sk->sk_prot->destroy(sk);
2302
2303 /*
2304 * Observation: when sock_common_release is called, processes have
2305 * no access to socket. But net still has.
2306 * Step one, detach it from networking:
2307 *
2308 * A. Remove from hash tables.
2309 */
2310
2311 sk->sk_prot->unhash(sk);
2312
2313 /*
2314 * In this point socket cannot receive new packets, but it is possible
2315 * that some packets are in flight because some CPU runs receiver and
2316 * did hash table lookup before we unhashed socket. They will achieve
2317 * receive queue and will be purged by socket destructor.
2318 *
2319 * Also we still have packets pending on receive queue and probably,
2320 * our own packets waiting in device queues. sock_destroy will drain
2321 * receive queue, but transmitted packets will delay socket destruction
2322 * until the last reference will be released.
2323 */
2324
2325 sock_orphan(sk);
2326
2327 xfrm_sk_free_policy(sk);
2328
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -07002329 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 sock_put(sk);
2331}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332EXPORT_SYMBOL(sk_common_release);
2333
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002334#ifdef CONFIG_PROC_FS
2335#define PROTO_INUSE_NR 64 /* should be enough for the first time */
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002336struct prot_inuse {
2337 int val[PROTO_INUSE_NR];
2338};
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002339
2340static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002341
2342#ifdef CONFIG_NET_NS
2343void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
2344{
Eric Dumazetd6d9ca02010-07-19 10:48:49 +00002345 __this_cpu_add(net->core.inuse->val[prot->inuse_idx], val);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002346}
2347EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
2348
2349int sock_prot_inuse_get(struct net *net, struct proto *prot)
2350{
2351 int cpu, idx = prot->inuse_idx;
2352 int res = 0;
2353
2354 for_each_possible_cpu(cpu)
2355 res += per_cpu_ptr(net->core.inuse, cpu)->val[idx];
2356
2357 return res >= 0 ? res : 0;
2358}
2359EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
2360
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002361static int __net_init sock_inuse_init_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002362{
2363 net->core.inuse = alloc_percpu(struct prot_inuse);
2364 return net->core.inuse ? 0 : -ENOMEM;
2365}
2366
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002367static void __net_exit sock_inuse_exit_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002368{
2369 free_percpu(net->core.inuse);
2370}
2371
2372static struct pernet_operations net_inuse_ops = {
2373 .init = sock_inuse_init_net,
2374 .exit = sock_inuse_exit_net,
2375};
2376
2377static __init int net_inuse_init(void)
2378{
2379 if (register_pernet_subsys(&net_inuse_ops))
2380 panic("Cannot initialize net inuse counters");
2381
2382 return 0;
2383}
2384
2385core_initcall(net_inuse_init);
2386#else
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002387static DEFINE_PER_CPU(struct prot_inuse, prot_inuse);
2388
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -07002389void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002390{
Eric Dumazetd6d9ca02010-07-19 10:48:49 +00002391 __this_cpu_add(prot_inuse.val[prot->inuse_idx], val);
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002392}
2393EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
2394
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -07002395int sock_prot_inuse_get(struct net *net, struct proto *prot)
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002396{
2397 int cpu, idx = prot->inuse_idx;
2398 int res = 0;
2399
2400 for_each_possible_cpu(cpu)
2401 res += per_cpu(prot_inuse, cpu).val[idx];
2402
2403 return res >= 0 ? res : 0;
2404}
2405EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002406#endif
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002407
2408static void assign_proto_idx(struct proto *prot)
2409{
2410 prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
2411
2412 if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
2413 printk(KERN_ERR "PROTO_INUSE_NR exhausted\n");
2414 return;
2415 }
2416
2417 set_bit(prot->inuse_idx, proto_inuse_idx);
2418}
2419
2420static void release_proto_idx(struct proto *prot)
2421{
2422 if (prot->inuse_idx != PROTO_INUSE_NR - 1)
2423 clear_bit(prot->inuse_idx, proto_inuse_idx);
2424}
2425#else
2426static inline void assign_proto_idx(struct proto *prot)
2427{
2428}
2429
2430static inline void release_proto_idx(struct proto *prot)
2431{
2432}
2433#endif
2434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435int proto_register(struct proto *prot, int alloc_slab)
2436{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (alloc_slab) {
2438 prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
Eric Dumazet271b72c2008-10-29 02:11:14 -07002439 SLAB_HWCACHE_ALIGN | prot->slab_flags,
2440 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 if (prot->slab == NULL) {
2443 printk(KERN_CRIT "%s: Can't create sock SLAB cache!\n",
2444 prot->name);
Pavel Emelyanov60e76632008-03-28 16:39:10 -07002445 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002447
2448 if (prot->rsk_prot != NULL) {
Alexey Dobriyanfaf23422010-02-17 09:34:12 +00002449 prot->rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s", prot->name);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002450 if (prot->rsk_prot->slab_name == NULL)
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002451 goto out_free_sock_slab;
2452
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002453 prot->rsk_prot->slab = kmem_cache_create(prot->rsk_prot->slab_name,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002454 prot->rsk_prot->obj_size, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002455 SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002456
2457 if (prot->rsk_prot->slab == NULL) {
2458 printk(KERN_CRIT "%s: Can't create request sock SLAB cache!\n",
2459 prot->name);
2460 goto out_free_request_sock_slab_name;
2461 }
2462 }
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002463
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002464 if (prot->twsk_prot != NULL) {
Alexey Dobriyanfaf23422010-02-17 09:34:12 +00002465 prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002466
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002467 if (prot->twsk_prot->twsk_slab_name == NULL)
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002468 goto out_free_request_sock_slab;
2469
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002470 prot->twsk_prot->twsk_slab =
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002471 kmem_cache_create(prot->twsk_prot->twsk_slab_name,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002472 prot->twsk_prot->twsk_obj_size,
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002473 0,
2474 SLAB_HWCACHE_ALIGN |
2475 prot->slab_flags,
Paul Mundt20c2df82007-07-20 10:11:58 +09002476 NULL);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002477 if (prot->twsk_prot->twsk_slab == NULL)
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002478 goto out_free_timewait_sock_slab_name;
2479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481
Glauber Costa36b77a52011-12-16 00:51:59 +00002482 mutex_lock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 list_add(&prot->node, &proto_list);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002484 assign_proto_idx(prot);
Glauber Costa36b77a52011-12-16 00:51:59 +00002485 mutex_unlock(&proto_list_mutex);
Pavel Emelyanovb733c002007-11-07 02:23:38 -08002486 return 0;
2487
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002488out_free_timewait_sock_slab_name:
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002489 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002490out_free_request_sock_slab:
2491 if (prot->rsk_prot && prot->rsk_prot->slab) {
2492 kmem_cache_destroy(prot->rsk_prot->slab);
2493 prot->rsk_prot->slab = NULL;
2494 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002495out_free_request_sock_slab_name:
Dan Carpenter72150e92010-03-06 01:04:45 +00002496 if (prot->rsk_prot)
2497 kfree(prot->rsk_prot->slab_name);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002498out_free_sock_slab:
2499 kmem_cache_destroy(prot->slab);
2500 prot->slab = NULL;
Pavel Emelyanovb733c002007-11-07 02:23:38 -08002501out:
2502 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504EXPORT_SYMBOL(proto_register);
2505
2506void proto_unregister(struct proto *prot)
2507{
Glauber Costa36b77a52011-12-16 00:51:59 +00002508 mutex_lock(&proto_list_mutex);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002509 release_proto_idx(prot);
Patrick McHardy0a3f4352005-09-06 19:47:50 -07002510 list_del(&prot->node);
Glauber Costa36b77a52011-12-16 00:51:59 +00002511 mutex_unlock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 if (prot->slab != NULL) {
2514 kmem_cache_destroy(prot->slab);
2515 prot->slab = NULL;
2516 }
2517
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002518 if (prot->rsk_prot != NULL && prot->rsk_prot->slab != NULL) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002519 kmem_cache_destroy(prot->rsk_prot->slab);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002520 kfree(prot->rsk_prot->slab_name);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002521 prot->rsk_prot->slab = NULL;
2522 }
2523
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002524 if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002525 kmem_cache_destroy(prot->twsk_prot->twsk_slab);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002526 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002527 prot->twsk_prot->twsk_slab = NULL;
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530EXPORT_SYMBOL(proto_unregister);
2531
2532#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
Glauber Costa36b77a52011-12-16 00:51:59 +00002534 __acquires(proto_list_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Glauber Costa36b77a52011-12-16 00:51:59 +00002536 mutex_lock(&proto_list_mutex);
Pavel Emelianov60f04382007-07-09 13:15:14 -07002537 return seq_list_start_head(&proto_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
2540static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2541{
Pavel Emelianov60f04382007-07-09 13:15:14 -07002542 return seq_list_next(v, &proto_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
2545static void proto_seq_stop(struct seq_file *seq, void *v)
Glauber Costa36b77a52011-12-16 00:51:59 +00002546 __releases(proto_list_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Glauber Costa36b77a52011-12-16 00:51:59 +00002548 mutex_unlock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
2551static char proto_method_implemented(const void *method)
2552{
2553 return method == NULL ? 'n' : 'y';
2554}
Glauber Costa180d8cd2011-12-11 21:47:02 +00002555static long sock_prot_memory_allocated(struct proto *proto)
2556{
2557 return proto->memory_allocated != NULL ? proto_memory_allocated(proto): -1L;
2558}
2559
2560static char *sock_prot_memory_pressure(struct proto *proto)
2561{
2562 return proto->memory_pressure != NULL ?
2563 proto_memory_pressure(proto) ? "yes" : "no" : "NI";
2564}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
2567{
Glauber Costa180d8cd2011-12-11 21:47:02 +00002568
Eric Dumazet8d987e52010-11-09 23:24:26 +00002569 seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
2571 proto->name,
2572 proto->obj_size,
Eric Dumazet14e943d2008-11-19 15:14:01 -08002573 sock_prot_inuse_get(seq_file_net(seq), proto),
Glauber Costa180d8cd2011-12-11 21:47:02 +00002574 sock_prot_memory_allocated(proto),
2575 sock_prot_memory_pressure(proto),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 proto->max_header,
2577 proto->slab == NULL ? "no" : "yes",
2578 module_name(proto->owner),
2579 proto_method_implemented(proto->close),
2580 proto_method_implemented(proto->connect),
2581 proto_method_implemented(proto->disconnect),
2582 proto_method_implemented(proto->accept),
2583 proto_method_implemented(proto->ioctl),
2584 proto_method_implemented(proto->init),
2585 proto_method_implemented(proto->destroy),
2586 proto_method_implemented(proto->shutdown),
2587 proto_method_implemented(proto->setsockopt),
2588 proto_method_implemented(proto->getsockopt),
2589 proto_method_implemented(proto->sendmsg),
2590 proto_method_implemented(proto->recvmsg),
2591 proto_method_implemented(proto->sendpage),
2592 proto_method_implemented(proto->bind),
2593 proto_method_implemented(proto->backlog_rcv),
2594 proto_method_implemented(proto->hash),
2595 proto_method_implemented(proto->unhash),
2596 proto_method_implemented(proto->get_port),
2597 proto_method_implemented(proto->enter_memory_pressure));
2598}
2599
2600static int proto_seq_show(struct seq_file *seq, void *v)
2601{
Pavel Emelianov60f04382007-07-09 13:15:14 -07002602 if (v == &proto_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
2604 "protocol",
2605 "size",
2606 "sockets",
2607 "memory",
2608 "press",
2609 "maxhdr",
2610 "slab",
2611 "module",
2612 "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
2613 else
Pavel Emelianov60f04382007-07-09 13:15:14 -07002614 proto_seq_printf(seq, list_entry(v, struct proto, node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return 0;
2616}
2617
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002618static const struct seq_operations proto_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 .start = proto_seq_start,
2620 .next = proto_seq_next,
2621 .stop = proto_seq_stop,
2622 .show = proto_seq_show,
2623};
2624
2625static int proto_seq_open(struct inode *inode, struct file *file)
2626{
Eric Dumazet14e943d2008-11-19 15:14:01 -08002627 return seq_open_net(inode, file, &proto_seq_ops,
2628 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
Arjan van de Ven9a321442007-02-12 00:55:35 -08002631static const struct file_operations proto_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 .owner = THIS_MODULE,
2633 .open = proto_seq_open,
2634 .read = seq_read,
2635 .llseek = seq_lseek,
Eric Dumazet14e943d2008-11-19 15:14:01 -08002636 .release = seq_release_net,
2637};
2638
2639static __net_init int proto_init_net(struct net *net)
2640{
2641 if (!proc_net_fops_create(net, "protocols", S_IRUGO, &proto_seq_fops))
2642 return -ENOMEM;
2643
2644 return 0;
2645}
2646
2647static __net_exit void proto_exit_net(struct net *net)
2648{
2649 proc_net_remove(net, "protocols");
2650}
2651
2652
2653static __net_initdata struct pernet_operations proto_net_ops = {
2654 .init = proto_init_net,
2655 .exit = proto_exit_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656};
2657
2658static int __init proto_init(void)
2659{
Eric Dumazet14e943d2008-11-19 15:14:01 -08002660 return register_pernet_subsys(&proto_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
2663subsys_initcall(proto_init);
2664
2665#endif /* PROC_FS */