blob: 06286006a2cc2be32a472a4453df7d088e1e17b6 [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
Joe Perchese005d192012-05-16 19:58:40 +000092#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
93
Randy Dunlap4fc268d2006-01-11 12:17:47 -080094#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/errno.h>
96#include <linux/types.h>
97#include <linux/socket.h>
98#include <linux/in.h>
99#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <linux/module.h>
101#include <linux/proc_fs.h>
102#include <linux/seq_file.h>
103#include <linux/sched.h>
104#include <linux/timer.h>
105#include <linux/string.h>
106#include <linux/sockios.h>
107#include <linux/net.h>
108#include <linux/mm.h>
109#include <linux/slab.h>
110#include <linux/interrupt.h>
111#include <linux/poll.h>
112#include <linux/tcp.h>
113#include <linux/init.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -0400114#include <linux/highmem.h>
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000115#include <linux/user_namespace.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100116#include <linux/static_key.h>
David S. Miller3969eb32012-01-09 13:44:23 -0800117#include <linux/memcontrol.h>
David S. Miller8c1ae102012-05-03 02:25:55 -0400118#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122#include <linux/netdevice.h>
123#include <net/protocol.h>
124#include <linux/skbuff.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200125#include <net/net_namespace.h>
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700126#include <net/request_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <net/sock.h>
Patrick Ohly20d49472009-02-12 05:03:38 +0000128#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#include <net/xfrm.h>
130#include <linux/ipsec.h>
Herbert Xuf8451722010-05-24 00:12:34 -0700131#include <net/cls_cgroup.h>
Neil Horman5bc14212011-11-22 05:10:51 +0000132#include <net/netprio_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134#include <linux/filter.h>
135
Satoru Moriya3847ce32011-06-17 12:00:03 +0000136#include <trace/events/sock.h>
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#ifdef CONFIG_INET
139#include <net/tcp.h>
140#endif
141
Glauber Costa36b77a52011-12-16 00:51:59 +0000142static DEFINE_MUTEX(proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000143static LIST_HEAD(proto_list);
144
Andrew Mortonc255a452012-07-31 16:43:02 -0700145#ifdef CONFIG_MEMCG_KMEM
Glauber Costa1d62e432012-04-09 19:36:33 -0300146int mem_cgroup_sockets_init(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000147{
148 struct proto *proto;
149 int ret = 0;
150
Glauber Costa36b77a52011-12-16 00:51:59 +0000151 mutex_lock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000152 list_for_each_entry(proto, &proto_list, node) {
153 if (proto->init_cgroup) {
Glauber Costa1d62e432012-04-09 19:36:33 -0300154 ret = proto->init_cgroup(memcg, ss);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000155 if (ret)
156 goto out;
157 }
158 }
159
Glauber Costa36b77a52011-12-16 00:51:59 +0000160 mutex_unlock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000161 return ret;
162out:
163 list_for_each_entry_continue_reverse(proto, &proto_list, node)
164 if (proto->destroy_cgroup)
Glauber Costa1d62e432012-04-09 19:36:33 -0300165 proto->destroy_cgroup(memcg);
Glauber Costa36b77a52011-12-16 00:51:59 +0000166 mutex_unlock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000167 return ret;
168}
169
Glauber Costa1d62e432012-04-09 19:36:33 -0300170void mem_cgroup_sockets_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000171{
172 struct proto *proto;
173
Glauber Costa36b77a52011-12-16 00:51:59 +0000174 mutex_lock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000175 list_for_each_entry_reverse(proto, &proto_list, node)
176 if (proto->destroy_cgroup)
Glauber Costa1d62e432012-04-09 19:36:33 -0300177 proto->destroy_cgroup(memcg);
Glauber Costa36b77a52011-12-16 00:51:59 +0000178 mutex_unlock(&proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000179}
180#endif
181
Ingo Molnarda21f242006-07-03 00:25:12 -0700182/*
183 * Each address family might have different locking rules, so we have
184 * one slock key per address family:
185 */
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700186static struct lock_class_key af_family_keys[AF_MAX];
187static struct lock_class_key af_family_slock_keys[AF_MAX];
188
Ingo Molnarc5905af2012-02-24 08:31:31 +0100189struct static_key memcg_socket_limit_enabled;
Glauber Costae1aab162011-12-11 21:47:03 +0000190EXPORT_SYMBOL(memcg_socket_limit_enabled);
191
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700192/*
193 * Make lock validator output more readable. (we pre-construct these
194 * strings build-time, so that runtime initialization of socket
195 * locks is fast):
196 */
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700197static const char *const af_family_key_strings[AF_MAX+1] = {
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700198 "sk_lock-AF_UNSPEC", "sk_lock-AF_UNIX" , "sk_lock-AF_INET" ,
199 "sk_lock-AF_AX25" , "sk_lock-AF_IPX" , "sk_lock-AF_APPLETALK",
200 "sk_lock-AF_NETROM", "sk_lock-AF_BRIDGE" , "sk_lock-AF_ATMPVC" ,
201 "sk_lock-AF_X25" , "sk_lock-AF_INET6" , "sk_lock-AF_ROSE" ,
202 "sk_lock-AF_DECnet", "sk_lock-AF_NETBEUI" , "sk_lock-AF_SECURITY" ,
203 "sk_lock-AF_KEY" , "sk_lock-AF_NETLINK" , "sk_lock-AF_PACKET" ,
204 "sk_lock-AF_ASH" , "sk_lock-AF_ECONET" , "sk_lock-AF_ATMSVC" ,
Andy Grovercbd151b2009-02-26 23:43:19 -0800205 "sk_lock-AF_RDS" , "sk_lock-AF_SNA" , "sk_lock-AF_IRDA" ,
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700206 "sk_lock-AF_PPPOX" , "sk_lock-AF_WANPIPE" , "sk_lock-AF_LLC" ,
Oliver Hartkoppcd05acf2007-12-16 15:59:24 -0800207 "sk_lock-27" , "sk_lock-28" , "sk_lock-AF_CAN" ,
David Howells17926a72007-04-26 15:48:28 -0700208 "sk_lock-AF_TIPC" , "sk_lock-AF_BLUETOOTH", "sk_lock-IUCV" ,
Remi Denis-Courmontbce7b152008-09-22 19:51:15 -0700209 "sk_lock-AF_RXRPC" , "sk_lock-AF_ISDN" , "sk_lock-AF_PHONET" ,
Miloslav Trmač6f107b52010-12-08 14:35:34 +0800210 "sk_lock-AF_IEEE802154", "sk_lock-AF_CAIF" , "sk_lock-AF_ALG" ,
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300211 "sk_lock-AF_NFC" , "sk_lock-AF_MAX"
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700212};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700213static const char *const af_family_slock_key_strings[AF_MAX+1] = {
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700214 "slock-AF_UNSPEC", "slock-AF_UNIX" , "slock-AF_INET" ,
215 "slock-AF_AX25" , "slock-AF_IPX" , "slock-AF_APPLETALK",
216 "slock-AF_NETROM", "slock-AF_BRIDGE" , "slock-AF_ATMPVC" ,
217 "slock-AF_X25" , "slock-AF_INET6" , "slock-AF_ROSE" ,
218 "slock-AF_DECnet", "slock-AF_NETBEUI" , "slock-AF_SECURITY" ,
219 "slock-AF_KEY" , "slock-AF_NETLINK" , "slock-AF_PACKET" ,
220 "slock-AF_ASH" , "slock-AF_ECONET" , "slock-AF_ATMSVC" ,
Andy Grovercbd151b2009-02-26 23:43:19 -0800221 "slock-AF_RDS" , "slock-AF_SNA" , "slock-AF_IRDA" ,
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700222 "slock-AF_PPPOX" , "slock-AF_WANPIPE" , "slock-AF_LLC" ,
Oliver Hartkoppcd05acf2007-12-16 15:59:24 -0800223 "slock-27" , "slock-28" , "slock-AF_CAN" ,
David Howells17926a72007-04-26 15:48:28 -0700224 "slock-AF_TIPC" , "slock-AF_BLUETOOTH", "slock-AF_IUCV" ,
Remi Denis-Courmontbce7b152008-09-22 19:51:15 -0700225 "slock-AF_RXRPC" , "slock-AF_ISDN" , "slock-AF_PHONET" ,
Miloslav Trmač6f107b52010-12-08 14:35:34 +0800226 "slock-AF_IEEE802154", "slock-AF_CAIF" , "slock-AF_ALG" ,
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300227 "slock-AF_NFC" , "slock-AF_MAX"
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700228};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700229static const char *const af_family_clock_key_strings[AF_MAX+1] = {
Peter Zijlstra443aef02007-07-19 01:49:00 -0700230 "clock-AF_UNSPEC", "clock-AF_UNIX" , "clock-AF_INET" ,
231 "clock-AF_AX25" , "clock-AF_IPX" , "clock-AF_APPLETALK",
232 "clock-AF_NETROM", "clock-AF_BRIDGE" , "clock-AF_ATMPVC" ,
233 "clock-AF_X25" , "clock-AF_INET6" , "clock-AF_ROSE" ,
234 "clock-AF_DECnet", "clock-AF_NETBEUI" , "clock-AF_SECURITY" ,
235 "clock-AF_KEY" , "clock-AF_NETLINK" , "clock-AF_PACKET" ,
236 "clock-AF_ASH" , "clock-AF_ECONET" , "clock-AF_ATMSVC" ,
Andy Grovercbd151b2009-02-26 23:43:19 -0800237 "clock-AF_RDS" , "clock-AF_SNA" , "clock-AF_IRDA" ,
Peter Zijlstra443aef02007-07-19 01:49:00 -0700238 "clock-AF_PPPOX" , "clock-AF_WANPIPE" , "clock-AF_LLC" ,
Oliver Hartkoppb4942af2008-07-23 14:06:04 -0700239 "clock-27" , "clock-28" , "clock-AF_CAN" ,
David Howellse51f8022007-07-21 19:30:16 -0700240 "clock-AF_TIPC" , "clock-AF_BLUETOOTH", "clock-AF_IUCV" ,
Remi Denis-Courmontbce7b152008-09-22 19:51:15 -0700241 "clock-AF_RXRPC" , "clock-AF_ISDN" , "clock-AF_PHONET" ,
Miloslav Trmač6f107b52010-12-08 14:35:34 +0800242 "clock-AF_IEEE802154", "clock-AF_CAIF" , "clock-AF_ALG" ,
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300243 "clock-AF_NFC" , "clock-AF_MAX"
Peter Zijlstra443aef02007-07-19 01:49:00 -0700244};
Ingo Molnarda21f242006-07-03 00:25:12 -0700245
246/*
247 * sk_callback_lock locking rules are per-address-family,
248 * so split the lock classes by using a per-AF key:
249 */
250static struct lock_class_key af_callback_keys[AF_MAX];
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/* Take into consideration the size of the struct sk_buff overhead in the
253 * determination of these values, since that is non-constant across
254 * platforms. This makes socket queueing behavior and performance
255 * not depend upon such differences.
256 */
257#define _SK_MEM_PACKETS 256
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000258#define _SK_MEM_OVERHEAD SKB_TRUESIZE(256)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
260#define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
261
262/* Run time adjustable parameters. */
Brian Haleyab32ea52006-09-22 14:15:41 -0700263__u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
Hans Schillstrom6d8ebc82012-04-30 08:13:50 +0200264EXPORT_SYMBOL(sysctl_wmem_max);
Brian Haleyab32ea52006-09-22 14:15:41 -0700265__u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
Hans Schillstrom6d8ebc82012-04-30 08:13:50 +0200266EXPORT_SYMBOL(sysctl_rmem_max);
Brian Haleyab32ea52006-09-22 14:15:41 -0700267__u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
268__u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300270/* Maximal space eaten by iovec or ancillary data plus some space */
Brian Haleyab32ea52006-09-22 14:15:41 -0700271int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
Eric Dumazet2a915252009-05-27 11:30:05 +0000272EXPORT_SYMBOL(sysctl_optmem_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Mel Gormanc93bdd02012-07-31 16:44:19 -0700274struct static_key memalloc_socks = STATIC_KEY_INIT_FALSE;
275EXPORT_SYMBOL_GPL(memalloc_socks);
276
Mel Gorman7cb02402012-07-31 16:44:16 -0700277/**
278 * sk_set_memalloc - sets %SOCK_MEMALLOC
279 * @sk: socket to set it on
280 *
281 * Set %SOCK_MEMALLOC on a socket for access to emergency reserves.
282 * It's the responsibility of the admin to adjust min_free_kbytes
283 * to meet the requirements
284 */
285void sk_set_memalloc(struct sock *sk)
286{
287 sock_set_flag(sk, SOCK_MEMALLOC);
288 sk->sk_allocation |= __GFP_MEMALLOC;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700289 static_key_slow_inc(&memalloc_socks);
Mel Gorman7cb02402012-07-31 16:44:16 -0700290}
291EXPORT_SYMBOL_GPL(sk_set_memalloc);
292
293void sk_clear_memalloc(struct sock *sk)
294{
295 sock_reset_flag(sk, SOCK_MEMALLOC);
296 sk->sk_allocation &= ~__GFP_MEMALLOC;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700297 static_key_slow_dec(&memalloc_socks);
Mel Gormanc76562b2012-07-31 16:44:41 -0700298
299 /*
300 * SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
301 * progress of swapping. However, if SOCK_MEMALLOC is cleared while
302 * it has rmem allocations there is a risk that the user of the
303 * socket cannot make forward progress due to exceeding the rmem
304 * limits. By rights, sk_clear_memalloc() should only be called
305 * on sockets being torn down but warn and reset the accounting if
306 * that assumption breaks.
307 */
308 if (WARN_ON(sk->sk_forward_alloc))
309 sk_mem_reclaim(sk);
Mel Gorman7cb02402012-07-31 16:44:16 -0700310}
311EXPORT_SYMBOL_GPL(sk_clear_memalloc);
312
Mel Gormanb4b9e352012-07-31 16:44:26 -0700313int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
314{
315 int ret;
316 unsigned long pflags = current->flags;
317
318 /* these should have been dropped before queueing */
319 BUG_ON(!sock_flag(sk, SOCK_MEMALLOC));
320
321 current->flags |= PF_MEMALLOC;
322 ret = sk->sk_backlog_rcv(sk, skb);
323 tsk_restore_flags(current, pflags, PF_MEMALLOC);
324
325 return ret;
326}
327EXPORT_SYMBOL(__sk_backlog_rcv);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
330{
331 struct timeval tv;
332
333 if (optlen < sizeof(tv))
334 return -EINVAL;
335 if (copy_from_user(&tv, optval, sizeof(tv)))
336 return -EFAULT;
Vasily Averinba780732007-05-24 16:58:54 -0700337 if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
338 return -EDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Vasily Averinba780732007-05-24 16:58:54 -0700340 if (tv.tv_sec < 0) {
Andrew Morton6f11df82007-07-09 13:16:00 -0700341 static int warned __read_mostly;
342
Vasily Averinba780732007-05-24 16:58:54 -0700343 *timeo_p = 0;
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700344 if (warned < 10 && net_ratelimit()) {
Vasily Averinba780732007-05-24 16:58:54 -0700345 warned++;
Joe Perchese005d192012-05-16 19:58:40 +0000346 pr_info("%s: `%s' (pid %d) tries to set negative timeout\n",
347 __func__, current->comm, task_pid_nr(current));
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700348 }
Vasily Averinba780732007-05-24 16:58:54 -0700349 return 0;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *timeo_p = MAX_SCHEDULE_TIMEOUT;
352 if (tv.tv_sec == 0 && tv.tv_usec == 0)
353 return 0;
354 if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
355 *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
356 return 0;
357}
358
359static void sock_warn_obsolete_bsdism(const char *name)
360{
361 static int warned;
362 static char warncomm[TASK_COMM_LEN];
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900363 if (strcmp(warncomm, current->comm) && warned < 5) {
364 strcpy(warncomm, current->comm);
Joe Perchese005d192012-05-16 19:58:40 +0000365 pr_warn("process `%s' is using obsolete %s SO_BSDCOMPAT\n",
366 warncomm, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 warned++;
368 }
369}
370
Eric Dumazet08e29af2011-11-28 12:04:18 +0000371#define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE))
372
373static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900374{
Eric Dumazet08e29af2011-11-28 12:04:18 +0000375 if (sk->sk_flags & flags) {
376 sk->sk_flags &= ~flags;
377 if (!(sk->sk_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +0000378 net_disable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380}
381
382
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800383int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
384{
Eric Dumazet766e90372009-10-14 20:40:11 -0700385 int err;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800386 int skb_len;
Neil Horman3b885782009-10-12 13:26:31 -0700387 unsigned long flags;
388 struct sk_buff_head *list = &sk->sk_receive_queue;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800389
Eric Dumazet0fd7bac2011-12-21 07:11:44 +0000390 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700391 atomic_inc(&sk->sk_drops);
Satoru Moriya3847ce32011-06-17 12:00:03 +0000392 trace_sock_rcvqueue_full(sk, skb);
Eric Dumazet766e90372009-10-14 20:40:11 -0700393 return -ENOMEM;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800394 }
395
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700396 err = sk_filter(sk, skb);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800397 if (err)
Eric Dumazet766e90372009-10-14 20:40:11 -0700398 return err;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800399
Mel Gormanc76562b2012-07-31 16:44:41 -0700400 if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700401 atomic_inc(&sk->sk_drops);
402 return -ENOBUFS;
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800403 }
404
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800405 skb->dev = NULL;
406 skb_set_owner_r(skb, sk);
David S. Miller49ad9592008-12-17 22:11:38 -0800407
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800408 /* Cache the SKB length before we tack it onto the receive
409 * queue. Once it is added it no longer belongs to us and
410 * may be freed by other threads of control pulling packets
411 * from the queue.
412 */
413 skb_len = skb->len;
414
Eric Dumazet7fee2262010-05-11 23:19:48 +0000415 /* we escape from rcu protected region, make sure we dont leak
416 * a norefcounted dst
417 */
418 skb_dst_force(skb);
419
Neil Horman3b885782009-10-12 13:26:31 -0700420 spin_lock_irqsave(&list->lock, flags);
421 skb->dropcount = atomic_read(&sk->sk_drops);
422 __skb_queue_tail(list, skb);
423 spin_unlock_irqrestore(&list->lock, flags);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800424
425 if (!sock_flag(sk, SOCK_DEAD))
426 sk->sk_data_ready(sk, skb_len);
Eric Dumazet766e90372009-10-14 20:40:11 -0700427 return 0;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800428}
429EXPORT_SYMBOL(sock_queue_rcv_skb);
430
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200431int sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested)
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800432{
433 int rc = NET_RX_SUCCESS;
434
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700435 if (sk_filter(sk, skb))
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800436 goto discard_and_relse;
437
438 skb->dev = NULL;
439
Eric Dumazetf545a382012-04-22 23:34:26 +0000440 if (sk_rcvqueues_full(sk, skb, sk->sk_rcvbuf)) {
Eric Dumazetc3774112010-04-27 15:13:20 -0700441 atomic_inc(&sk->sk_drops);
442 goto discard_and_relse;
443 }
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200444 if (nested)
445 bh_lock_sock_nested(sk);
446 else
447 bh_lock_sock(sk);
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700448 if (!sock_owned_by_user(sk)) {
449 /*
450 * trylock + unlock semantics:
451 */
452 mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
453
Peter Zijlstrac57943a2008-10-07 14:18:42 -0700454 rc = sk_backlog_rcv(sk, skb);
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700455
456 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
Eric Dumazetf545a382012-04-22 23:34:26 +0000457 } else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
Zhu Yi8eae9392010-03-04 18:01:40 +0000458 bh_unlock_sock(sk);
459 atomic_inc(&sk->sk_drops);
460 goto discard_and_relse;
461 }
462
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800463 bh_unlock_sock(sk);
464out:
465 sock_put(sk);
466 return rc;
467discard_and_relse:
468 kfree_skb(skb);
469 goto out;
470}
471EXPORT_SYMBOL(sk_receive_skb);
472
Krishna Kumarea94ff32009-10-19 23:46:45 +0000473void sk_reset_txq(struct sock *sk)
474{
475 sk_tx_queue_clear(sk);
476}
477EXPORT_SYMBOL(sk_reset_txq);
478
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800479struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
480{
Eric Dumazetb6c67122010-04-08 23:03:29 +0000481 struct dst_entry *dst = __sk_dst_get(sk);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800482
483 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
Krishna Kumare022f0b2009-10-19 23:46:20 +0000484 sk_tx_queue_clear(sk);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000485 RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800486 dst_release(dst);
487 return NULL;
488 }
489
490 return dst;
491}
492EXPORT_SYMBOL(__sk_dst_check);
493
494struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
495{
496 struct dst_entry *dst = sk_dst_get(sk);
497
498 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
499 sk_dst_reset(sk);
500 dst_release(dst);
501 return NULL;
502 }
503
504 return dst;
505}
506EXPORT_SYMBOL(sk_dst_check);
507
David S. Miller48788092007-09-14 16:41:03 -0700508static int sock_bindtodevice(struct sock *sk, char __user *optval, int optlen)
509{
510 int ret = -ENOPROTOOPT;
511#ifdef CONFIG_NETDEVICES
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900512 struct net *net = sock_net(sk);
David S. Miller48788092007-09-14 16:41:03 -0700513 char devname[IFNAMSIZ];
514 int index;
515
516 /* Sorry... */
517 ret = -EPERM;
518 if (!capable(CAP_NET_RAW))
519 goto out;
520
521 ret = -EINVAL;
522 if (optlen < 0)
523 goto out;
524
525 /* Bind this socket to a particular device like "eth0",
526 * as specified in the passed interface name. If the
527 * name is "" or the option length is zero the socket
528 * is not bound.
529 */
530 if (optlen > IFNAMSIZ - 1)
531 optlen = IFNAMSIZ - 1;
532 memset(devname, 0, sizeof(devname));
533
534 ret = -EFAULT;
535 if (copy_from_user(devname, optval, optlen))
536 goto out;
537
David S. Miller000ba2e2009-11-05 22:37:11 -0800538 index = 0;
539 if (devname[0] != '\0') {
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800540 struct net_device *dev;
David S. Miller48788092007-09-14 16:41:03 -0700541
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800542 rcu_read_lock();
543 dev = dev_get_by_name_rcu(net, devname);
544 if (dev)
545 index = dev->ifindex;
546 rcu_read_unlock();
David S. Miller48788092007-09-14 16:41:03 -0700547 ret = -ENODEV;
548 if (!dev)
549 goto out;
David S. Miller48788092007-09-14 16:41:03 -0700550 }
551
552 lock_sock(sk);
553 sk->sk_bound_dev_if = index;
554 sk_dst_reset(sk);
555 release_sock(sk);
556
557 ret = 0;
558
559out:
560#endif
561
562 return ret;
563}
564
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800565static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
566{
567 if (valbool)
568 sock_set_flag(sk, bit);
569 else
570 sock_reset_flag(sk, bit);
571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
574 * This is meant for all protocols to use and covers goings on
575 * at the socket level. Everything here is generic.
576 */
577
578int sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700579 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Eric Dumazet2a915252009-05-27 11:30:05 +0000581 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int val;
583 int valbool;
584 struct linger ling;
585 int ret = 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /*
588 * Options without arguments
589 */
590
David S. Miller48788092007-09-14 16:41:03 -0700591 if (optname == SO_BINDTODEVICE)
592 return sock_bindtodevice(sk, optval, optlen);
593
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700594 if (optlen < sizeof(int))
595 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (get_user(val, (int __user *)optval))
598 return -EFAULT;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900599
Eric Dumazet2a915252009-05-27 11:30:05 +0000600 valbool = val ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 lock_sock(sk);
603
Eric Dumazet2a915252009-05-27 11:30:05 +0000604 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700605 case SO_DEBUG:
Eric Dumazet2a915252009-05-27 11:30:05 +0000606 if (val && !capable(CAP_NET_ADMIN))
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700607 ret = -EACCES;
Eric Dumazet2a915252009-05-27 11:30:05 +0000608 else
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800609 sock_valbool_flag(sk, SOCK_DBG, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700610 break;
611 case SO_REUSEADDR:
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +0000612 sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700613 break;
614 case SO_TYPE:
Jan Engelhardt49c794e2009-08-04 07:28:28 +0000615 case SO_PROTOCOL:
Jan Engelhardt0d6038e2009-08-04 07:28:29 +0000616 case SO_DOMAIN:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700617 case SO_ERROR:
618 ret = -ENOPROTOOPT;
619 break;
620 case SO_DONTROUTE:
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800621 sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700622 break;
623 case SO_BROADCAST:
624 sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
625 break;
626 case SO_SNDBUF:
627 /* Don't error on this BSD doesn't and if you think
Eric Dumazet82981932012-04-26 20:07:59 +0000628 * about it this is right. Otherwise apps have to
629 * play 'guess the biggest size' games. RCVBUF/SNDBUF
630 * are treated in BSD as hints
631 */
632 val = min_t(u32, val, sysctl_wmem_max);
Patrick McHardyb0573de2005-08-09 19:30:51 -0700633set_sndbuf:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700634 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Eric Dumazet82981932012-04-26 20:07:59 +0000635 sk->sk_sndbuf = max_t(u32, val * 2, SOCK_MIN_SNDBUF);
636 /* Wake up sending tasks if we upped the value. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700637 sk->sk_write_space(sk);
638 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700640 case SO_SNDBUFFORCE:
641 if (!capable(CAP_NET_ADMIN)) {
642 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 break;
644 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700645 goto set_sndbuf;
646
647 case SO_RCVBUF:
648 /* Don't error on this BSD doesn't and if you think
Eric Dumazet82981932012-04-26 20:07:59 +0000649 * about it this is right. Otherwise apps have to
650 * play 'guess the biggest size' games. RCVBUF/SNDBUF
651 * are treated in BSD as hints
652 */
653 val = min_t(u32, val, sysctl_rmem_max);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700654set_rcvbuf:
655 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
656 /*
657 * We double it on the way in to account for
658 * "struct sk_buff" etc. overhead. Applications
659 * assume that the SO_RCVBUF setting they make will
660 * allow that much actual data to be received on that
661 * socket.
662 *
663 * Applications are unaware that "struct sk_buff" and
664 * other overheads allocate from the receive buffer
665 * during socket buffer allocation.
666 *
667 * And after considering the possible alternatives,
668 * returning the value we actually used in getsockopt
669 * is the most desirable behavior.
670 */
Eric Dumazet82981932012-04-26 20:07:59 +0000671 sk->sk_rcvbuf = max_t(u32, val * 2, SOCK_MIN_RCVBUF);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700672 break;
673
674 case SO_RCVBUFFORCE:
675 if (!capable(CAP_NET_ADMIN)) {
676 ret = -EPERM;
677 break;
678 }
679 goto set_rcvbuf;
680
681 case SO_KEEPALIVE:
682#ifdef CONFIG_INET
Eric Dumazet3e109862012-09-24 07:00:11 +0000683 if (sk->sk_protocol == IPPROTO_TCP &&
684 sk->sk_type == SOCK_STREAM)
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700685 tcp_set_keepalive(sk, valbool);
686#endif
687 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
688 break;
689
690 case SO_OOBINLINE:
691 sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
692 break;
693
694 case SO_NO_CHECK:
695 sk->sk_no_check = valbool;
696 break;
697
698 case SO_PRIORITY:
699 if ((val >= 0 && val <= 6) || capable(CAP_NET_ADMIN))
700 sk->sk_priority = val;
701 else
702 ret = -EPERM;
703 break;
704
705 case SO_LINGER:
706 if (optlen < sizeof(ling)) {
707 ret = -EINVAL; /* 1003.1g */
708 break;
709 }
Eric Dumazet2a915252009-05-27 11:30:05 +0000710 if (copy_from_user(&ling, optval, sizeof(ling))) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700711 ret = -EFAULT;
712 break;
713 }
714 if (!ling.l_onoff)
715 sock_reset_flag(sk, SOCK_LINGER);
716 else {
717#if (BITS_PER_LONG == 32)
718 if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
719 sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
720 else
721#endif
722 sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
723 sock_set_flag(sk, SOCK_LINGER);
724 }
725 break;
726
727 case SO_BSDCOMPAT:
728 sock_warn_obsolete_bsdism("setsockopt");
729 break;
730
731 case SO_PASSCRED:
732 if (valbool)
733 set_bit(SOCK_PASSCRED, &sock->flags);
734 else
735 clear_bit(SOCK_PASSCRED, &sock->flags);
736 break;
737
738 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700739 case SO_TIMESTAMPNS:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700740 if (valbool) {
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700741 if (optname == SO_TIMESTAMP)
742 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
743 else
744 sock_set_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700745 sock_set_flag(sk, SOCK_RCVTSTAMP);
Patrick Ohly20d49472009-02-12 05:03:38 +0000746 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700747 } else {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700748 sock_reset_flag(sk, SOCK_RCVTSTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700749 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
750 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700751 break;
752
Patrick Ohly20d49472009-02-12 05:03:38 +0000753 case SO_TIMESTAMPING:
754 if (val & ~SOF_TIMESTAMPING_MASK) {
Rémi Denis-Courmontf249fb72009-07-20 00:47:04 +0000755 ret = -EINVAL;
Patrick Ohly20d49472009-02-12 05:03:38 +0000756 break;
757 }
758 sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE,
759 val & SOF_TIMESTAMPING_TX_HARDWARE);
760 sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE,
761 val & SOF_TIMESTAMPING_TX_SOFTWARE);
762 sock_valbool_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE,
763 val & SOF_TIMESTAMPING_RX_HARDWARE);
764 if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
765 sock_enable_timestamp(sk,
766 SOCK_TIMESTAMPING_RX_SOFTWARE);
767 else
768 sock_disable_timestamp(sk,
Eric Dumazet08e29af2011-11-28 12:04:18 +0000769 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
Patrick Ohly20d49472009-02-12 05:03:38 +0000770 sock_valbool_flag(sk, SOCK_TIMESTAMPING_SOFTWARE,
771 val & SOF_TIMESTAMPING_SOFTWARE);
772 sock_valbool_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE,
773 val & SOF_TIMESTAMPING_SYS_HARDWARE);
774 sock_valbool_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE,
775 val & SOF_TIMESTAMPING_RAW_HARDWARE);
776 break;
777
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700778 case SO_RCVLOWAT:
779 if (val < 0)
780 val = INT_MAX;
781 sk->sk_rcvlowat = val ? : 1;
782 break;
783
784 case SO_RCVTIMEO:
785 ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
786 break;
787
788 case SO_SNDTIMEO:
789 ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
790 break;
791
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700792 case SO_ATTACH_FILTER:
793 ret = -EINVAL;
794 if (optlen == sizeof(struct sock_fprog)) {
795 struct sock_fprog fprog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700797 ret = -EFAULT;
798 if (copy_from_user(&fprog, optval, sizeof(fprog)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700801 ret = sk_attach_filter(&fprog, sk);
802 }
803 break;
804
805 case SO_DETACH_FILTER:
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700806 ret = sk_detach_filter(sk);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700807 break;
808
809 case SO_PASSSEC:
810 if (valbool)
811 set_bit(SOCK_PASSSEC, &sock->flags);
812 else
813 clear_bit(SOCK_PASSSEC, &sock->flags);
814 break;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800815 case SO_MARK:
816 if (!capable(CAP_NET_ADMIN))
817 ret = -EPERM;
Eric Dumazet2a915252009-05-27 11:30:05 +0000818 else
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800819 sk->sk_mark = val;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800820 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* We implement the SO_SNDLOWAT etc to
823 not be settable (1003.1g 5.3) */
Neil Horman3b885782009-10-12 13:26:31 -0700824 case SO_RXQ_OVFL:
Johannes Berg8083f0f2011-10-07 03:30:20 +0000825 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
Neil Horman3b885782009-10-12 13:26:31 -0700826 break;
Johannes Berg6e3e9392011-11-09 10:15:42 +0100827
828 case SO_WIFI_STATUS:
829 sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
830 break;
831
Pavel Emelyanovef64a542012-02-21 07:31:34 +0000832 case SO_PEEK_OFF:
833 if (sock->ops->set_peek_off)
834 sock->ops->set_peek_off(sk, val);
835 else
836 ret = -EOPNOTSUPP;
837 break;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000838
839 case SO_NOFCS:
840 sock_valbool_flag(sk, SOCK_NOFCS, valbool);
841 break;
842
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700843 default:
844 ret = -ENOPROTOOPT;
845 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 release_sock(sk);
848 return ret;
849}
Eric Dumazet2a915252009-05-27 11:30:05 +0000850EXPORT_SYMBOL(sock_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000853void cred_to_ucred(struct pid *pid, const struct cred *cred,
854 struct ucred *ucred)
855{
856 ucred->pid = pid_vnr(pid);
857 ucred->uid = ucred->gid = -1;
858 if (cred) {
859 struct user_namespace *current_ns = current_user_ns();
860
Eric W. Biedermanb2e4f542012-05-23 16:39:45 -0600861 ucred->uid = from_kuid_munged(current_ns, cred->euid);
862 ucred->gid = from_kgid_munged(current_ns, cred->egid);
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000863 }
864}
David S. Miller39247732010-06-16 16:18:25 -0700865EXPORT_SYMBOL_GPL(cred_to_ucred);
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867int sock_getsockopt(struct socket *sock, int level, int optname,
868 char __user *optval, int __user *optlen)
869{
870 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900871
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700872 union {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900873 int val;
874 struct linger ling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 struct timeval tm;
876 } v;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900877
H Hartley Sweeten4d0392b2010-01-15 01:08:58 -0800878 int lv = sizeof(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int len;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900880
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700881 if (get_user(len, optlen))
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900882 return -EFAULT;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700883 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900885
Eugene Teo50fee1d2009-02-23 15:38:41 -0800886 memset(&v, 0, sizeof(v));
Clément Lecignedf0bca02009-02-12 16:59:09 -0800887
Eric Dumazet2a915252009-05-27 11:30:05 +0000888 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700889 case SO_DEBUG:
890 v.val = sock_flag(sk, SOCK_DBG);
891 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900892
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700893 case SO_DONTROUTE:
894 v.val = sock_flag(sk, SOCK_LOCALROUTE);
895 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900896
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700897 case SO_BROADCAST:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +0000898 v.val = sock_flag(sk, SOCK_BROADCAST);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700901 case SO_SNDBUF:
902 v.val = sk->sk_sndbuf;
903 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900904
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700905 case SO_RCVBUF:
906 v.val = sk->sk_rcvbuf;
907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700909 case SO_REUSEADDR:
910 v.val = sk->sk_reuse;
911 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700913 case SO_KEEPALIVE:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +0000914 v.val = sock_flag(sk, SOCK_KEEPOPEN);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700915 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700917 case SO_TYPE:
918 v.val = sk->sk_type;
919 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Jan Engelhardt49c794e2009-08-04 07:28:28 +0000921 case SO_PROTOCOL:
922 v.val = sk->sk_protocol;
923 break;
924
Jan Engelhardt0d6038e2009-08-04 07:28:29 +0000925 case SO_DOMAIN:
926 v.val = sk->sk_family;
927 break;
928
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700929 case SO_ERROR:
930 v.val = -sock_error(sk);
Eric Dumazet2a915252009-05-27 11:30:05 +0000931 if (v.val == 0)
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700932 v.val = xchg(&sk->sk_err_soft, 0);
933 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700935 case SO_OOBINLINE:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +0000936 v.val = sock_flag(sk, SOCK_URGINLINE);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700937 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900938
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700939 case SO_NO_CHECK:
940 v.val = sk->sk_no_check;
941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700943 case SO_PRIORITY:
944 v.val = sk->sk_priority;
945 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900946
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700947 case SO_LINGER:
948 lv = sizeof(v.ling);
Eric Dumazet1b23a5d2012-05-16 05:57:07 +0000949 v.ling.l_onoff = sock_flag(sk, SOCK_LINGER);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700950 v.ling.l_linger = sk->sk_lingertime / HZ;
951 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900952
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700953 case SO_BSDCOMPAT:
954 sock_warn_obsolete_bsdism("getsockopt");
955 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700957 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700958 v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
959 !sock_flag(sk, SOCK_RCVTSTAMPNS);
960 break;
961
962 case SO_TIMESTAMPNS:
963 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700964 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Patrick Ohly20d49472009-02-12 05:03:38 +0000966 case SO_TIMESTAMPING:
967 v.val = 0;
968 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
969 v.val |= SOF_TIMESTAMPING_TX_HARDWARE;
970 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
971 v.val |= SOF_TIMESTAMPING_TX_SOFTWARE;
972 if (sock_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE))
973 v.val |= SOF_TIMESTAMPING_RX_HARDWARE;
974 if (sock_flag(sk, SOCK_TIMESTAMPING_RX_SOFTWARE))
975 v.val |= SOF_TIMESTAMPING_RX_SOFTWARE;
976 if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE))
977 v.val |= SOF_TIMESTAMPING_SOFTWARE;
978 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE))
979 v.val |= SOF_TIMESTAMPING_SYS_HARDWARE;
980 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE))
981 v.val |= SOF_TIMESTAMPING_RAW_HARDWARE;
982 break;
983
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700984 case SO_RCVTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +0000985 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700986 if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
987 v.tm.tv_sec = 0;
988 v.tm.tv_usec = 0;
989 } else {
990 v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
991 v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * 1000000) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700993 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700995 case SO_SNDTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +0000996 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700997 if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
998 v.tm.tv_sec = 0;
999 v.tm.tv_usec = 0;
1000 } else {
1001 v.tm.tv_sec = sk->sk_sndtimeo / HZ;
1002 v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * 1000000) / HZ;
1003 }
1004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001006 case SO_RCVLOWAT:
1007 v.val = sk->sk_rcvlowat;
1008 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07001009
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001010 case SO_SNDLOWAT:
Eric Dumazet2a915252009-05-27 11:30:05 +00001011 v.val = 1;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001014 case SO_PASSCRED:
Eric Dumazet82981932012-04-26 20:07:59 +00001015 v.val = !!test_bit(SOCK_PASSCRED, &sock->flags);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001016 break;
1017
1018 case SO_PEERCRED:
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001019 {
1020 struct ucred peercred;
1021 if (len > sizeof(peercred))
1022 len = sizeof(peercred);
1023 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
1024 if (copy_to_user(optval, &peercred, len))
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001025 return -EFAULT;
1026 goto lenout;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001027 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001028
1029 case SO_PEERNAME:
1030 {
1031 char address[128];
1032
1033 if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
1034 return -ENOTCONN;
1035 if (lv < len)
1036 return -EINVAL;
1037 if (copy_to_user(optval, address, len))
1038 return -EFAULT;
1039 goto lenout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001041
1042 /* Dubious BSD thing... Probably nobody even uses it, but
1043 * the UNIX standard wants it for whatever reason... -DaveM
1044 */
1045 case SO_ACCEPTCONN:
1046 v.val = sk->sk_state == TCP_LISTEN;
1047 break;
1048
1049 case SO_PASSSEC:
Eric Dumazet82981932012-04-26 20:07:59 +00001050 v.val = !!test_bit(SOCK_PASSSEC, &sock->flags);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001051 break;
1052
1053 case SO_PEERSEC:
1054 return security_socket_getpeersec_stream(sock, optval, optlen, len);
1055
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001056 case SO_MARK:
1057 v.val = sk->sk_mark;
1058 break;
1059
Neil Horman3b885782009-10-12 13:26:31 -07001060 case SO_RXQ_OVFL:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001061 v.val = sock_flag(sk, SOCK_RXQ_OVFL);
Neil Horman3b885782009-10-12 13:26:31 -07001062 break;
1063
Johannes Berg6e3e9392011-11-09 10:15:42 +01001064 case SO_WIFI_STATUS:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001065 v.val = sock_flag(sk, SOCK_WIFI_STATUS);
Johannes Berg6e3e9392011-11-09 10:15:42 +01001066 break;
1067
Pavel Emelyanovef64a542012-02-21 07:31:34 +00001068 case SO_PEEK_OFF:
1069 if (!sock->ops->set_peek_off)
1070 return -EOPNOTSUPP;
1071
1072 v.val = sk->sk_peek_off;
1073 break;
David S. Millerbc2f7992012-02-24 14:48:34 -05001074 case SO_NOFCS:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001075 v.val = sock_flag(sk, SOCK_NOFCS);
David S. Millerbc2f7992012-02-24 14:48:34 -05001076 break;
Pavel Emelyanovf7b86bf2012-10-18 23:55:56 +00001077 case SO_BINDTODEVICE:
1078 v.val = sk->sk_bound_dev_if;
1079 break;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001080 case SO_GET_FILTER:
1081 len = sk_get_filter(sk, (struct sock_filter __user *)optval, len);
1082 if (len < 0)
1083 return len;
1084
1085 goto lenout;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001086 default:
1087 return -ENOPROTOOPT;
1088 }
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (len > lv)
1091 len = lv;
1092 if (copy_to_user(optval, &v, len))
1093 return -EFAULT;
1094lenout:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001095 if (put_user(len, optlen))
1096 return -EFAULT;
1097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001100/*
1101 * Initialize an sk_lock.
1102 *
1103 * (We also register the sk_lock with the lock validator.)
1104 */
Dave Jonesb6f99a22007-03-22 12:27:49 -07001105static inline void sock_lock_init(struct sock *sk)
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001106{
Peter Zijlstraed075362006-12-06 20:35:24 -08001107 sock_lock_init_class_and_name(sk,
1108 af_family_slock_key_strings[sk->sk_family],
1109 af_family_slock_keys + sk->sk_family,
1110 af_family_key_strings[sk->sk_family],
1111 af_family_keys + sk->sk_family);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001112}
1113
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001114/*
1115 * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
1116 * even temporarly, because of RCU lookups. sk_node should also be left as is.
Eric Dumazet68835ab2010-11-30 19:04:07 +00001117 * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001118 */
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001119static void sock_copy(struct sock *nsk, const struct sock *osk)
1120{
1121#ifdef CONFIG_SECURITY_NETWORK
1122 void *sptr = nsk->sk_security;
1123#endif
Eric Dumazet68835ab2010-11-30 19:04:07 +00001124 memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
1125
1126 memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
1127 osk->sk_prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
1128
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001129#ifdef CONFIG_SECURITY_NETWORK
1130 nsk->sk_security = sptr;
1131 security_sk_clone(osk, nsk);
1132#endif
1133}
1134
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001135/*
1136 * caches using SLAB_DESTROY_BY_RCU should let .next pointer from nulls nodes
1137 * un-modified. Special care is taken when initializing object to zero.
1138 */
1139static inline void sk_prot_clear_nulls(struct sock *sk, int size)
1140{
1141 if (offsetof(struct sock, sk_node.next) != 0)
1142 memset(sk, 0, offsetof(struct sock, sk_node.next));
1143 memset(&sk->sk_node.pprev, 0,
1144 size - offsetof(struct sock, sk_node.pprev));
1145}
1146
1147void sk_prot_clear_portaddr_nulls(struct sock *sk, int size)
1148{
1149 unsigned long nulls1, nulls2;
1150
1151 nulls1 = offsetof(struct sock, __sk_common.skc_node.next);
1152 nulls2 = offsetof(struct sock, __sk_common.skc_portaddr_node.next);
1153 if (nulls1 > nulls2)
1154 swap(nulls1, nulls2);
1155
1156 if (nulls1 != 0)
1157 memset((char *)sk, 0, nulls1);
1158 memset((char *)sk + nulls1 + sizeof(void *), 0,
1159 nulls2 - nulls1 - sizeof(void *));
1160 memset((char *)sk + nulls2 + sizeof(void *), 0,
1161 size - nulls2 - sizeof(void *));
1162}
1163EXPORT_SYMBOL(sk_prot_clear_portaddr_nulls);
1164
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001165static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
1166 int family)
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001167{
1168 struct sock *sk;
1169 struct kmem_cache *slab;
1170
1171 slab = prot->slab;
Eric Dumazete912b112009-07-08 19:36:05 +00001172 if (slab != NULL) {
1173 sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
1174 if (!sk)
1175 return sk;
1176 if (priority & __GFP_ZERO) {
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001177 if (prot->clear_sk)
1178 prot->clear_sk(sk, prot->obj_size);
1179 else
1180 sk_prot_clear_nulls(sk, prot->obj_size);
Eric Dumazete912b112009-07-08 19:36:05 +00001181 }
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001182 } else
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001183 sk = kmalloc(prot->obj_size, priority);
1184
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001185 if (sk != NULL) {
Vegard Nossuma98b65a2009-02-26 14:46:57 +01001186 kmemcheck_annotate_bitfield(sk, flags);
1187
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001188 if (security_sk_alloc(sk, family, priority))
1189 goto out_free;
1190
1191 if (!try_module_get(prot->owner))
1192 goto out_free_sec;
Krishna Kumare022f0b2009-10-19 23:46:20 +00001193 sk_tx_queue_clear(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001194 }
1195
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001196 return sk;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001197
1198out_free_sec:
1199 security_sk_free(sk);
1200out_free:
1201 if (slab != NULL)
1202 kmem_cache_free(slab, sk);
1203 else
1204 kfree(sk);
1205 return NULL;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001206}
1207
1208static void sk_prot_free(struct proto *prot, struct sock *sk)
1209{
1210 struct kmem_cache *slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001211 struct module *owner;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001212
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001213 owner = prot->owner;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001214 slab = prot->slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001215
1216 security_sk_free(sk);
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001217 if (slab != NULL)
1218 kmem_cache_free(slab, sk);
1219 else
1220 kfree(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001221 module_put(owner);
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001222}
1223
Herbert Xuf8451722010-05-24 00:12:34 -07001224#ifdef CONFIG_CGROUPS
Daniel Wagner8fb974c2012-09-12 16:12:02 +02001225#if IS_ENABLED(CONFIG_NET_CLS_CGROUP)
Daniel Wagnerfd9a08a2012-10-25 04:16:58 +00001226void sock_update_classid(struct sock *sk, struct task_struct *task)
Herbert Xuf8451722010-05-24 00:12:34 -07001227{
Paul E. McKenney11441822010-10-06 17:15:35 -07001228 u32 classid;
Herbert Xuf8451722010-05-24 00:12:34 -07001229
Daniel Wagnerfd9a08a2012-10-25 04:16:58 +00001230 classid = task_cls_classid(task);
Neil Horman3afa6d02012-08-20 07:59:10 +00001231 if (classid != sk->sk_classid)
Herbert Xuf8451722010-05-24 00:12:34 -07001232 sk->sk_classid = classid;
1233}
Herbert Xu82862742010-05-24 00:14:10 -07001234EXPORT_SYMBOL(sock_update_classid);
Daniel Wagner8fb974c2012-09-12 16:12:02 +02001235#endif
Neil Horman5bc14212011-11-22 05:10:51 +00001236
Daniel Wagner51e4e7f2012-09-12 16:12:03 +02001237#if IS_ENABLED(CONFIG_NETPRIO_CGROUP)
John Fastabend406a3c62012-07-20 10:39:25 +00001238void sock_update_netprioidx(struct sock *sk, struct task_struct *task)
Neil Horman5bc14212011-11-22 05:10:51 +00001239{
Neil Horman5bc14212011-11-22 05:10:51 +00001240 if (in_interrupt())
1241 return;
Neil Horman2b73bc62012-02-10 05:43:38 +00001242
John Fastabend406a3c62012-07-20 10:39:25 +00001243 sk->sk_cgrp_prioidx = task_netprioidx(task);
Neil Horman5bc14212011-11-22 05:10:51 +00001244}
1245EXPORT_SYMBOL_GPL(sock_update_netprioidx);
Herbert Xuf8451722010-05-24 00:12:34 -07001246#endif
Daniel Wagner51e4e7f2012-09-12 16:12:03 +02001247#endif
Herbert Xuf8451722010-05-24 00:12:34 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/**
1250 * sk_alloc - All socket objects are allocated here
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001251 * @net: the applicable net namespace
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001252 * @family: protocol family
1253 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1254 * @prot: struct proto associated with this new sock instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001256struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001257 struct proto *prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001259 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001261 sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (sk) {
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001263 sk->sk_family = family;
1264 /*
1265 * See comment in struct sock definition to understand
1266 * why we need sk_prot_creator -acme
1267 */
1268 sk->sk_prot = sk->sk_prot_creator = prot;
1269 sock_lock_init(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001270 sock_net_set(sk, get_net(net));
Jarek Poplawskid66ee052009-08-30 23:15:36 +00001271 atomic_set(&sk->sk_wmem_alloc, 1);
Herbert Xuf8451722010-05-24 00:12:34 -07001272
Daniel Wagnerfd9a08a2012-10-25 04:16:58 +00001273 sock_update_classid(sk, current);
John Fastabend406a3c62012-07-20 10:39:25 +00001274 sock_update_netprioidx(sk, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Frank Filza79af592005-09-27 15:23:38 -07001276
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001277 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
Eric Dumazet2a915252009-05-27 11:30:05 +00001279EXPORT_SYMBOL(sk_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Eric Dumazet2b85a342009-06-11 02:55:43 -07001281static void __sk_free(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct sk_filter *filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (sk->sk_destruct)
1286 sk->sk_destruct(sk);
1287
Paul E. McKenneya898def2010-02-22 17:04:49 -08001288 filter = rcu_dereference_check(sk->sk_filter,
1289 atomic_read(&sk->sk_wmem_alloc) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (filter) {
Pavel Emelyanov309dd5f2007-10-17 21:21:51 -07001291 sk_filter_uncharge(sk, filter);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001292 RCU_INIT_POINTER(sk->sk_filter, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
Eric Dumazet08e29af2011-11-28 12:04:18 +00001295 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (atomic_read(&sk->sk_omem_alloc))
Joe Perchese005d192012-05-16 19:58:40 +00001298 pr_debug("%s: optmem leakage (%d bytes) detected\n",
1299 __func__, atomic_read(&sk->sk_omem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001301 if (sk->sk_peer_cred)
1302 put_cred(sk->sk_peer_cred);
1303 put_pid(sk->sk_peer_pid);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001304 put_net(sock_net(sk));
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001305 sk_prot_free(sk->sk_prot_creator, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
Eric Dumazet2b85a342009-06-11 02:55:43 -07001307
1308void sk_free(struct sock *sk)
1309{
1310 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001311 * We subtract one from sk_wmem_alloc and can know if
Eric Dumazet2b85a342009-06-11 02:55:43 -07001312 * some packets are still in some tx queue.
1313 * If not null, sock_wfree() will call __sk_free(sk) later
1314 */
1315 if (atomic_dec_and_test(&sk->sk_wmem_alloc))
1316 __sk_free(sk);
1317}
Eric Dumazet2a915252009-05-27 11:30:05 +00001318EXPORT_SYMBOL(sk_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Denis V. Lunevedf02082008-02-29 11:18:32 -08001320/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001321 * Last sock_put should drop reference to sk->sk_net. It has already
1322 * been dropped in sk_change_net. Taking reference to stopping namespace
Denis V. Lunevedf02082008-02-29 11:18:32 -08001323 * is not an option.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001324 * Take reference to a socket to remove it from hash _alive_ and after that
Denis V. Lunevedf02082008-02-29 11:18:32 -08001325 * destroy it in the context of init_net.
1326 */
1327void sk_release_kernel(struct sock *sk)
1328{
1329 if (sk == NULL || sk->sk_socket == NULL)
1330 return;
1331
1332 sock_hold(sk);
1333 sock_release(sk->sk_socket);
Denis V. Lunev65a18ec2008-04-16 01:59:46 -07001334 release_net(sock_net(sk));
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001335 sock_net_set(sk, get_net(&init_net));
Denis V. Lunevedf02082008-02-29 11:18:32 -08001336 sock_put(sk);
1337}
David S. Miller45af1752008-02-29 11:33:19 -08001338EXPORT_SYMBOL(sk_release_kernel);
Denis V. Lunevedf02082008-02-29 11:18:32 -08001339
Stephen Rothwell475f1b52012-01-09 16:33:16 +11001340static void sk_update_clone(const struct sock *sk, struct sock *newsk)
1341{
1342 if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
1343 sock_update_memcg(newsk);
1344}
1345
Eric Dumazete56c57d2011-11-08 17:07:07 -05001346/**
1347 * sk_clone_lock - clone a socket, and lock its clone
1348 * @sk: the socket to clone
1349 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1350 *
1351 * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
1352 */
1353struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001354{
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001355 struct sock *newsk;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001356
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001357 newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001358 if (newsk != NULL) {
1359 struct sk_filter *filter;
1360
Venkat Yekkirala892c1412006-08-04 23:08:56 -07001361 sock_copy(newsk, sk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001362
1363 /* SANITY */
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001364 get_net(sock_net(newsk));
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001365 sk_node_init(&newsk->sk_node);
1366 sock_lock_init(newsk);
1367 bh_lock_sock(newsk);
Eric Dumazetfa438cc2007-03-04 16:05:44 -08001368 newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
Zhu Yi8eae9392010-03-04 18:01:40 +00001369 newsk->sk_backlog.len = 0;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001370
1371 atomic_set(&newsk->sk_rmem_alloc, 0);
Eric Dumazet2b85a342009-06-11 02:55:43 -07001372 /*
1373 * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
1374 */
1375 atomic_set(&newsk->sk_wmem_alloc, 1);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001376 atomic_set(&newsk->sk_omem_alloc, 0);
1377 skb_queue_head_init(&newsk->sk_receive_queue);
1378 skb_queue_head_init(&newsk->sk_write_queue);
Chris Leech97fc2f02006-05-23 17:55:33 -07001379#ifdef CONFIG_NET_DMA
1380 skb_queue_head_init(&newsk->sk_async_wait_queue);
1381#endif
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001382
Eric Dumazetb6c67122010-04-08 23:03:29 +00001383 spin_lock_init(&newsk->sk_dst_lock);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001384 rwlock_init(&newsk->sk_callback_lock);
Peter Zijlstra443aef02007-07-19 01:49:00 -07001385 lockdep_set_class_and_name(&newsk->sk_callback_lock,
1386 af_callback_keys + newsk->sk_family,
1387 af_family_clock_key_strings[newsk->sk_family]);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001388
1389 newsk->sk_dst_cache = NULL;
1390 newsk->sk_wmem_queued = 0;
1391 newsk->sk_forward_alloc = 0;
1392 newsk->sk_send_head = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001393 newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
1394
1395 sock_reset_flag(newsk, SOCK_DONE);
1396 skb_queue_head_init(&newsk->sk_error_queue);
1397
Eric Dumazet0d7da9d2010-10-25 03:47:05 +00001398 filter = rcu_dereference_protected(newsk->sk_filter, 1);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001399 if (filter != NULL)
1400 sk_filter_charge(newsk, filter);
1401
1402 if (unlikely(xfrm_sk_clone_policy(newsk))) {
1403 /* It is still raw copy of parent, so invalidate
1404 * destructor and make plain sk_free() */
1405 newsk->sk_destruct = NULL;
Thomas Gleixnerb0691c82011-10-25 02:30:50 +00001406 bh_unlock_sock(newsk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001407 sk_free(newsk);
1408 newsk = NULL;
1409 goto out;
1410 }
1411
1412 newsk->sk_err = 0;
1413 newsk->sk_priority = 0;
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001414 /*
1415 * Before updating sk_refcnt, we must commit prior changes to memory
1416 * (Documentation/RCU/rculist_nulls.txt for details)
1417 */
1418 smp_wmb();
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001419 atomic_set(&newsk->sk_refcnt, 2);
1420
1421 /*
1422 * Increment the counter in the same struct proto as the master
1423 * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
1424 * is the same as sk->sk_prot->socks, as this field was copied
1425 * with memcpy).
1426 *
1427 * This _changes_ the previous behaviour, where
1428 * tcp_create_openreq_child always was incrementing the
1429 * equivalent to tcp_prot->socks (inet_sock_nr), so this have
1430 * to be taken into account in all callers. -acme
1431 */
1432 sk_refcnt_debug_inc(newsk);
David S. Miller972692e2008-06-17 22:41:38 -07001433 sk_set_socket(newsk, NULL);
Eric Dumazet43815482010-04-29 11:01:49 +00001434 newsk->sk_wq = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001435
Glauber Costaf3f511e2012-01-05 20:16:39 +00001436 sk_update_clone(sk, newsk);
1437
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001438 if (newsk->sk_prot->sockets_allocated)
Glauber Costa180d8cd2011-12-11 21:47:02 +00001439 sk_sockets_allocated_inc(newsk);
Octavian Purdila704da5602010-01-08 00:00:09 -08001440
Eric Dumazet08e29af2011-11-28 12:04:18 +00001441 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
Octavian Purdila704da5602010-01-08 00:00:09 -08001442 net_enable_timestamp();
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001443 }
1444out:
1445 return newsk;
1446}
Eric Dumazete56c57d2011-11-08 17:07:07 -05001447EXPORT_SYMBOL_GPL(sk_clone_lock);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001448
Andi Kleen99580892007-04-20 17:12:43 -07001449void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
1450{
1451 __sk_dst_set(sk, dst);
1452 sk->sk_route_caps = dst->dev->features;
1453 if (sk->sk_route_caps & NETIF_F_GSO)
Herbert Xu4fcd6b92007-05-31 22:15:50 -07001454 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
Eric Dumazeta4654192010-05-16 00:36:33 -07001455 sk->sk_route_caps &= ~sk->sk_route_nocaps;
Andi Kleen99580892007-04-20 17:12:43 -07001456 if (sk_can_gso(sk)) {
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001457 if (dst->header_len) {
Andi Kleen99580892007-04-20 17:12:43 -07001458 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001459 } else {
Andi Kleen99580892007-04-20 17:12:43 -07001460 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001461 sk->sk_gso_max_size = dst->dev->gso_max_size;
Ben Hutchings14853482012-07-30 16:11:42 +00001462 sk->sk_gso_max_segs = dst->dev->gso_max_segs;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001463 }
Andi Kleen99580892007-04-20 17:12:43 -07001464 }
1465}
1466EXPORT_SYMBOL_GPL(sk_setup_caps);
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468/*
1469 * Simple resource managers for sockets.
1470 */
1471
1472
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001473/*
1474 * Write buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 */
1476void sock_wfree(struct sk_buff *skb)
1477{
1478 struct sock *sk = skb->sk;
Eric Dumazetd99927f2009-09-24 10:49:24 +00001479 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Eric Dumazetd99927f2009-09-24 10:49:24 +00001481 if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
1482 /*
1483 * Keep a reference on sk_wmem_alloc, this will be released
1484 * after sk_write_space() call
1485 */
1486 atomic_sub(len - 1, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 sk->sk_write_space(sk);
Eric Dumazetd99927f2009-09-24 10:49:24 +00001488 len = 1;
1489 }
Eric Dumazet2b85a342009-06-11 02:55:43 -07001490 /*
Eric Dumazetd99927f2009-09-24 10:49:24 +00001491 * if sk_wmem_alloc reaches 0, we must finish what sk_free()
1492 * could not do because of in-flight packets
Eric Dumazet2b85a342009-06-11 02:55:43 -07001493 */
Eric Dumazetd99927f2009-09-24 10:49:24 +00001494 if (atomic_sub_and_test(len, &sk->sk_wmem_alloc))
Eric Dumazet2b85a342009-06-11 02:55:43 -07001495 __sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
Eric Dumazet2a915252009-05-27 11:30:05 +00001497EXPORT_SYMBOL(sock_wfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001499/*
1500 * Read buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 */
1502void sock_rfree(struct sk_buff *skb)
1503{
1504 struct sock *sk = skb->sk;
Eric Dumazetd361fd52010-07-10 22:45:17 +00001505 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Eric Dumazetd361fd52010-07-10 22:45:17 +00001507 atomic_sub(len, &sk->sk_rmem_alloc);
1508 sk_mem_uncharge(sk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
Eric Dumazet2a915252009-05-27 11:30:05 +00001510EXPORT_SYMBOL(sock_rfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
David S. Miller41063e92012-06-19 21:22:05 -07001512void sock_edemux(struct sk_buff *skb)
1513{
Eric Dumazete8123472012-09-02 23:57:18 +00001514 struct sock *sk = skb->sk;
1515
Randy Dunlap1c463e52012-09-10 09:13:07 -07001516#ifdef CONFIG_INET
Eric Dumazete8123472012-09-02 23:57:18 +00001517 if (sk->sk_state == TCP_TIME_WAIT)
1518 inet_twsk_put(inet_twsk(sk));
1519 else
Randy Dunlap1c463e52012-09-10 09:13:07 -07001520#endif
Eric Dumazete8123472012-09-02 23:57:18 +00001521 sock_put(sk);
David S. Miller41063e92012-06-19 21:22:05 -07001522}
1523EXPORT_SYMBOL(sock_edemux);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Eric W. Biederman976d02012012-05-23 17:16:53 -06001525kuid_t sock_i_uid(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Eric W. Biederman976d02012012-05-23 17:16:53 -06001527 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Eric Dumazetf064af12010-09-22 12:43:39 +00001529 read_lock_bh(&sk->sk_callback_lock);
Eric W. Biederman976d02012012-05-23 17:16:53 -06001530 uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID;
Eric Dumazetf064af12010-09-22 12:43:39 +00001531 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return uid;
1533}
Eric Dumazet2a915252009-05-27 11:30:05 +00001534EXPORT_SYMBOL(sock_i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536unsigned long sock_i_ino(struct sock *sk)
1537{
1538 unsigned long ino;
1539
Eric Dumazetf064af12010-09-22 12:43:39 +00001540 read_lock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
Eric Dumazetf064af12010-09-22 12:43:39 +00001542 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return ino;
1544}
Eric Dumazet2a915252009-05-27 11:30:05 +00001545EXPORT_SYMBOL(sock_i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547/*
1548 * Allocate a skb from the socket's send buffer.
1549 */
Victor Fusco86a76ca2005-07-08 14:57:47 -07001550struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
Al Virodd0fc662005-10-07 07:46:04 +01001551 gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
Eric Dumazet2a915252009-05-27 11:30:05 +00001554 struct sk_buff *skb = alloc_skb(size, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (skb) {
1556 skb_set_owner_w(skb, sk);
1557 return skb;
1558 }
1559 }
1560 return NULL;
1561}
Eric Dumazet2a915252009-05-27 11:30:05 +00001562EXPORT_SYMBOL(sock_wmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564/*
1565 * Allocate a skb from the socket's receive buffer.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001566 */
Victor Fusco86a76ca2005-07-08 14:57:47 -07001567struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force,
Al Virodd0fc662005-10-07 07:46:04 +01001568 gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 if (force || atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
1571 struct sk_buff *skb = alloc_skb(size, priority);
1572 if (skb) {
1573 skb_set_owner_r(skb, sk);
1574 return skb;
1575 }
1576 }
1577 return NULL;
1578}
1579
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001580/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * Allocate a memory block from the socket's option memory buffer.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001582 */
Al Virodd0fc662005-10-07 07:46:04 +01001583void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Eric Dumazet95c96172012-04-15 05:58:06 +00001585 if ((unsigned int)size <= sysctl_optmem_max &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
1587 void *mem;
1588 /* First do the add, to avoid the race if kmalloc
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001589 * might sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 */
1591 atomic_add(size, &sk->sk_omem_alloc);
1592 mem = kmalloc(size, priority);
1593 if (mem)
1594 return mem;
1595 atomic_sub(size, &sk->sk_omem_alloc);
1596 }
1597 return NULL;
1598}
Eric Dumazet2a915252009-05-27 11:30:05 +00001599EXPORT_SYMBOL(sock_kmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601/*
1602 * Free an option memory block.
1603 */
1604void sock_kfree_s(struct sock *sk, void *mem, int size)
1605{
1606 kfree(mem);
1607 atomic_sub(size, &sk->sk_omem_alloc);
1608}
Eric Dumazet2a915252009-05-27 11:30:05 +00001609EXPORT_SYMBOL(sock_kfree_s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611/* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
1612 I think, these locks should be removed for datagram sockets.
1613 */
Eric Dumazet2a915252009-05-27 11:30:05 +00001614static long sock_wait_for_wmem(struct sock *sk, long timeo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 DEFINE_WAIT(wait);
1617
1618 clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1619 for (;;) {
1620 if (!timeo)
1621 break;
1622 if (signal_pending(current))
1623 break;
1624 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Eric Dumazetaa395142010-04-20 13:03:51 +00001625 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
1627 break;
1628 if (sk->sk_shutdown & SEND_SHUTDOWN)
1629 break;
1630 if (sk->sk_err)
1631 break;
1632 timeo = schedule_timeout(timeo);
1633 }
Eric Dumazetaa395142010-04-20 13:03:51 +00001634 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return timeo;
1636}
1637
1638
1639/*
1640 * Generic send/receive buffer handlers
1641 */
1642
Herbert Xu4cc7f682009-02-04 16:55:54 -08001643struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
1644 unsigned long data_len, int noblock,
1645 int *errcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 struct sk_buff *skb;
Al Viro7d877f32005-10-21 03:20:43 -04001648 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 long timeo;
1650 int err;
Jason Wangcc9b17a2012-05-30 21:18:10 +00001651 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
1652
1653 err = -EMSGSIZE;
1654 if (npages > MAX_SKB_FRAGS)
1655 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 gfp_mask = sk->sk_allocation;
1658 if (gfp_mask & __GFP_WAIT)
1659 gfp_mask |= __GFP_REPEAT;
1660
1661 timeo = sock_sndtimeo(sk, noblock);
1662 while (1) {
1663 err = sock_error(sk);
1664 if (err != 0)
1665 goto failure;
1666
1667 err = -EPIPE;
1668 if (sk->sk_shutdown & SEND_SHUTDOWN)
1669 goto failure;
1670
1671 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
Larry Woodmandb38c1792006-11-03 16:05:45 -08001672 skb = alloc_skb(header_len, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 int i;
1675
1676 /* No pages, we're done... */
1677 if (!data_len)
1678 break;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 skb->truesize += data_len;
1681 skb_shinfo(skb)->nr_frags = npages;
1682 for (i = 0; i < npages; i++) {
1683 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 page = alloc_pages(sk->sk_allocation, 0);
1686 if (!page) {
1687 err = -ENOBUFS;
1688 skb_shinfo(skb)->nr_frags = i;
1689 kfree_skb(skb);
1690 goto failure;
1691 }
1692
Ian Campbellea2ab692011-08-22 23:44:58 +00001693 __skb_fill_page_desc(skb, i,
1694 page, 0,
1695 (data_len >= PAGE_SIZE ?
1696 PAGE_SIZE :
1697 data_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 data_len -= PAGE_SIZE;
1699 }
1700
1701 /* Full success... */
1702 break;
1703 }
1704 err = -ENOBUFS;
1705 goto failure;
1706 }
1707 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1708 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1709 err = -EAGAIN;
1710 if (!timeo)
1711 goto failure;
1712 if (signal_pending(current))
1713 goto interrupted;
1714 timeo = sock_wait_for_wmem(sk, timeo);
1715 }
1716
1717 skb_set_owner_w(skb, sk);
1718 return skb;
1719
1720interrupted:
1721 err = sock_intr_errno(timeo);
1722failure:
1723 *errcode = err;
1724 return NULL;
1725}
Herbert Xu4cc7f682009-02-04 16:55:54 -08001726EXPORT_SYMBOL(sock_alloc_send_pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001728struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 int noblock, int *errcode)
1730{
1731 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode);
1732}
Eric Dumazet2a915252009-05-27 11:30:05 +00001733EXPORT_SYMBOL(sock_alloc_send_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Eric Dumazet5640f762012-09-23 23:04:42 +00001735/* On 32bit arches, an skb frag is limited to 2^15 */
1736#define SKB_FRAG_PAGE_ORDER get_order(32768)
1737
1738bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1739{
1740 int order;
1741
1742 if (pfrag->page) {
1743 if (atomic_read(&pfrag->page->_count) == 1) {
1744 pfrag->offset = 0;
1745 return true;
1746 }
1747 if (pfrag->offset < pfrag->size)
1748 return true;
1749 put_page(pfrag->page);
1750 }
1751
1752 /* We restrict high order allocations to users that can afford to wait */
1753 order = (sk->sk_allocation & __GFP_WAIT) ? SKB_FRAG_PAGE_ORDER : 0;
1754
1755 do {
1756 gfp_t gfp = sk->sk_allocation;
1757
1758 if (order)
1759 gfp |= __GFP_COMP | __GFP_NOWARN;
1760 pfrag->page = alloc_pages(gfp, order);
1761 if (likely(pfrag->page)) {
1762 pfrag->offset = 0;
1763 pfrag->size = PAGE_SIZE << order;
1764 return true;
1765 }
1766 } while (--order >= 0);
1767
1768 sk_enter_memory_pressure(sk);
1769 sk_stream_moderate_sndbuf(sk);
1770 return false;
1771}
1772EXPORT_SYMBOL(sk_page_frag_refill);
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774static void __lock_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00001775 __releases(&sk->sk_lock.slock)
1776 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 DEFINE_WAIT(wait);
1779
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001780 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
1782 TASK_UNINTERRUPTIBLE);
1783 spin_unlock_bh(&sk->sk_lock.slock);
1784 schedule();
1785 spin_lock_bh(&sk->sk_lock.slock);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001786 if (!sock_owned_by_user(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 break;
1788 }
1789 finish_wait(&sk->sk_lock.wq, &wait);
1790}
1791
1792static void __release_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00001793 __releases(&sk->sk_lock.slock)
1794 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 struct sk_buff *skb = sk->sk_backlog.head;
1797
1798 do {
1799 sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
1800 bh_unlock_sock(sk);
1801
1802 do {
1803 struct sk_buff *next = skb->next;
1804
Eric Dumazete4cbb022012-04-30 16:07:09 +00001805 prefetch(next);
Eric Dumazet7fee2262010-05-11 23:19:48 +00001806 WARN_ON_ONCE(skb_dst_is_noref(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 skb->next = NULL;
Peter Zijlstrac57943a2008-10-07 14:18:42 -07001808 sk_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /*
1811 * We are in process context here with softirqs
1812 * disabled, use cond_resched_softirq() to preempt.
1813 * This is safe to do because we've taken the backlog
1814 * queue private:
1815 */
1816 cond_resched_softirq();
1817
1818 skb = next;
1819 } while (skb != NULL);
1820
1821 bh_lock_sock(sk);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001822 } while ((skb = sk->sk_backlog.head) != NULL);
Zhu Yi8eae9392010-03-04 18:01:40 +00001823
1824 /*
1825 * Doing the zeroing here guarantee we can not loop forever
1826 * while a wild producer attempts to flood us.
1827 */
1828 sk->sk_backlog.len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
1831/**
1832 * sk_wait_data - wait for data to arrive at sk_receive_queue
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001833 * @sk: sock to wait on
1834 * @timeo: for how long
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 *
1836 * Now socket state including sk->sk_err is changed only under lock,
1837 * hence we may omit checks after joining wait queue.
1838 * We check receive queue before schedule() only as optimization;
1839 * it is very likely that release_sock() added new data.
1840 */
1841int sk_wait_data(struct sock *sk, long *timeo)
1842{
1843 int rc;
1844 DEFINE_WAIT(wait);
1845
Eric Dumazetaa395142010-04-20 13:03:51 +00001846 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1848 rc = sk_wait_event(sk, timeo, !skb_queue_empty(&sk->sk_receive_queue));
1849 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
Eric Dumazetaa395142010-04-20 13:03:51 +00001850 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return rc;
1852}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853EXPORT_SYMBOL(sk_wait_data);
1854
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001855/**
1856 * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
1857 * @sk: socket
1858 * @size: memory size to allocate
1859 * @kind: allocation type
1860 *
1861 * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
1862 * rmem allocation. This function assumes that protocols which have
1863 * memory_pressure use sk_wmem_queued as write buffer accounting.
1864 */
1865int __sk_mem_schedule(struct sock *sk, int size, int kind)
1866{
1867 struct proto *prot = sk->sk_prot;
1868 int amt = sk_mem_pages(size);
Eric Dumazet8d987e52010-11-09 23:24:26 +00001869 long allocated;
Glauber Costae1aab162011-12-11 21:47:03 +00001870 int parent_status = UNDER_LIMIT;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001871
1872 sk->sk_forward_alloc += amt * SK_MEM_QUANTUM;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001873
Glauber Costae1aab162011-12-11 21:47:03 +00001874 allocated = sk_memory_allocated_add(sk, amt, &parent_status);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001875
1876 /* Under limit. */
Glauber Costae1aab162011-12-11 21:47:03 +00001877 if (parent_status == UNDER_LIMIT &&
1878 allocated <= sk_prot_mem_limits(sk, 0)) {
Glauber Costa180d8cd2011-12-11 21:47:02 +00001879 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001880 return 1;
1881 }
1882
Glauber Costae1aab162011-12-11 21:47:03 +00001883 /* Under pressure. (we or our parents) */
1884 if ((parent_status > SOFT_LIMIT) ||
1885 allocated > sk_prot_mem_limits(sk, 1))
Glauber Costa180d8cd2011-12-11 21:47:02 +00001886 sk_enter_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001887
Glauber Costae1aab162011-12-11 21:47:03 +00001888 /* Over hard limit (we or our parents) */
1889 if ((parent_status == OVER_LIMIT) ||
1890 (allocated > sk_prot_mem_limits(sk, 2)))
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001891 goto suppress_allocation;
1892
1893 /* guarantee minimum buffer size under pressure */
1894 if (kind == SK_MEM_RECV) {
1895 if (atomic_read(&sk->sk_rmem_alloc) < prot->sysctl_rmem[0])
1896 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001897
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001898 } else { /* SK_MEM_SEND */
1899 if (sk->sk_type == SOCK_STREAM) {
1900 if (sk->sk_wmem_queued < prot->sysctl_wmem[0])
1901 return 1;
1902 } else if (atomic_read(&sk->sk_wmem_alloc) <
1903 prot->sysctl_wmem[0])
1904 return 1;
1905 }
1906
Glauber Costa180d8cd2011-12-11 21:47:02 +00001907 if (sk_has_memory_pressure(sk)) {
Eric Dumazet17483762008-11-25 21:16:35 -08001908 int alloc;
1909
Glauber Costa180d8cd2011-12-11 21:47:02 +00001910 if (!sk_under_memory_pressure(sk))
Eric Dumazet17483762008-11-25 21:16:35 -08001911 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001912 alloc = sk_sockets_allocated_read_positive(sk);
1913 if (sk_prot_mem_limits(sk, 2) > alloc *
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001914 sk_mem_pages(sk->sk_wmem_queued +
1915 atomic_read(&sk->sk_rmem_alloc) +
1916 sk->sk_forward_alloc))
1917 return 1;
1918 }
1919
1920suppress_allocation:
1921
1922 if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
1923 sk_stream_moderate_sndbuf(sk);
1924
1925 /* Fail only if socket is _under_ its sndbuf.
1926 * In this case we cannot block, so that we have to fail.
1927 */
1928 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
1929 return 1;
1930 }
1931
Satoru Moriya3847ce32011-06-17 12:00:03 +00001932 trace_sock_exceed_buf_limit(sk, prot, allocated);
1933
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001934 /* Alas. Undo changes. */
1935 sk->sk_forward_alloc -= amt * SK_MEM_QUANTUM;
Glauber Costa180d8cd2011-12-11 21:47:02 +00001936
Glauber Costa0e90b312012-01-20 04:57:16 +00001937 sk_memory_allocated_sub(sk, amt);
Glauber Costa180d8cd2011-12-11 21:47:02 +00001938
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001939 return 0;
1940}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001941EXPORT_SYMBOL(__sk_mem_schedule);
1942
1943/**
1944 * __sk_reclaim - reclaim memory_allocated
1945 * @sk: socket
1946 */
1947void __sk_mem_reclaim(struct sock *sk)
1948{
Glauber Costa180d8cd2011-12-11 21:47:02 +00001949 sk_memory_allocated_sub(sk,
Glauber Costa0e90b312012-01-20 04:57:16 +00001950 sk->sk_forward_alloc >> SK_MEM_QUANTUM_SHIFT);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001951 sk->sk_forward_alloc &= SK_MEM_QUANTUM - 1;
1952
Glauber Costa180d8cd2011-12-11 21:47:02 +00001953 if (sk_under_memory_pressure(sk) &&
1954 (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
1955 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001956}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001957EXPORT_SYMBOL(__sk_mem_reclaim);
1958
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960/*
1961 * Set of default routines for initialising struct proto_ops when
1962 * the protocol does not support a particular function. In certain
1963 * cases where it makes no sense for a protocol to have a "do nothing"
1964 * function, some default processing is provided.
1965 */
1966
1967int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
1968{
1969 return -EOPNOTSUPP;
1970}
Eric Dumazet2a915252009-05-27 11:30:05 +00001971EXPORT_SYMBOL(sock_no_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001973int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 int len, int flags)
1975{
1976 return -EOPNOTSUPP;
1977}
Eric Dumazet2a915252009-05-27 11:30:05 +00001978EXPORT_SYMBOL(sock_no_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
1981{
1982 return -EOPNOTSUPP;
1983}
Eric Dumazet2a915252009-05-27 11:30:05 +00001984EXPORT_SYMBOL(sock_no_socketpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
1987{
1988 return -EOPNOTSUPP;
1989}
Eric Dumazet2a915252009-05-27 11:30:05 +00001990EXPORT_SYMBOL(sock_no_accept);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001992int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 int *len, int peer)
1994{
1995 return -EOPNOTSUPP;
1996}
Eric Dumazet2a915252009-05-27 11:30:05 +00001997EXPORT_SYMBOL(sock_no_getname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Eric Dumazet2a915252009-05-27 11:30:05 +00001999unsigned int sock_no_poll(struct file *file, struct socket *sock, poll_table *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 return 0;
2002}
Eric Dumazet2a915252009-05-27 11:30:05 +00002003EXPORT_SYMBOL(sock_no_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2006{
2007 return -EOPNOTSUPP;
2008}
Eric Dumazet2a915252009-05-27 11:30:05 +00002009EXPORT_SYMBOL(sock_no_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011int sock_no_listen(struct socket *sock, int backlog)
2012{
2013 return -EOPNOTSUPP;
2014}
Eric Dumazet2a915252009-05-27 11:30:05 +00002015EXPORT_SYMBOL(sock_no_listen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017int sock_no_shutdown(struct socket *sock, int how)
2018{
2019 return -EOPNOTSUPP;
2020}
Eric Dumazet2a915252009-05-27 11:30:05 +00002021EXPORT_SYMBOL(sock_no_shutdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023int sock_no_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002024 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
2026 return -EOPNOTSUPP;
2027}
Eric Dumazet2a915252009-05-27 11:30:05 +00002028EXPORT_SYMBOL(sock_no_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030int sock_no_getsockopt(struct socket *sock, int level, int optname,
2031 char __user *optval, int __user *optlen)
2032{
2033 return -EOPNOTSUPP;
2034}
Eric Dumazet2a915252009-05-27 11:30:05 +00002035EXPORT_SYMBOL(sock_no_getsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037int sock_no_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
2038 size_t len)
2039{
2040 return -EOPNOTSUPP;
2041}
Eric Dumazet2a915252009-05-27 11:30:05 +00002042EXPORT_SYMBOL(sock_no_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044int sock_no_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
2045 size_t len, int flags)
2046{
2047 return -EOPNOTSUPP;
2048}
Eric Dumazet2a915252009-05-27 11:30:05 +00002049EXPORT_SYMBOL(sock_no_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
2052{
2053 /* Mirror missing mmap method error code */
2054 return -ENODEV;
2055}
Eric Dumazet2a915252009-05-27 11:30:05 +00002056EXPORT_SYMBOL(sock_no_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
2059{
2060 ssize_t res;
2061 struct msghdr msg = {.msg_flags = flags};
2062 struct kvec iov;
2063 char *kaddr = kmap(page);
2064 iov.iov_base = kaddr + offset;
2065 iov.iov_len = size;
2066 res = kernel_sendmsg(sock, &msg, &iov, 1, size);
2067 kunmap(page);
2068 return res;
2069}
Eric Dumazet2a915252009-05-27 11:30:05 +00002070EXPORT_SYMBOL(sock_no_sendpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072/*
2073 * Default Socket Callbacks
2074 */
2075
2076static void sock_def_wakeup(struct sock *sk)
2077{
Eric Dumazet43815482010-04-29 11:01:49 +00002078 struct socket_wq *wq;
2079
2080 rcu_read_lock();
2081 wq = rcu_dereference(sk->sk_wq);
2082 if (wq_has_sleeper(wq))
2083 wake_up_interruptible_all(&wq->wait);
2084 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
2087static void sock_def_error_report(struct sock *sk)
2088{
Eric Dumazet43815482010-04-29 11:01:49 +00002089 struct socket_wq *wq;
2090
2091 rcu_read_lock();
2092 wq = rcu_dereference(sk->sk_wq);
2093 if (wq_has_sleeper(wq))
2094 wake_up_interruptible_poll(&wq->wait, POLLERR);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002095 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
Eric Dumazet43815482010-04-29 11:01:49 +00002096 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
2099static void sock_def_readable(struct sock *sk, int len)
2100{
Eric Dumazet43815482010-04-29 11:01:49 +00002101 struct socket_wq *wq;
2102
2103 rcu_read_lock();
2104 wq = rcu_dereference(sk->sk_wq);
2105 if (wq_has_sleeper(wq))
Eric Dumazet2c6607c2011-01-06 10:54:29 -08002106 wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI |
Davide Libenzi37e55402009-03-31 15:24:21 -07002107 POLLRDNORM | POLLRDBAND);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002108 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Eric Dumazet43815482010-04-29 11:01:49 +00002109 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112static void sock_def_write_space(struct sock *sk)
2113{
Eric Dumazet43815482010-04-29 11:01:49 +00002114 struct socket_wq *wq;
2115
2116 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 /* Do not wake up a writer until he can make "significant"
2119 * progress. --DaveM
2120 */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002121 if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
Eric Dumazet43815482010-04-29 11:01:49 +00002122 wq = rcu_dereference(sk->sk_wq);
2123 if (wq_has_sleeper(wq))
2124 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
Davide Libenzi37e55402009-03-31 15:24:21 -07002125 POLLWRNORM | POLLWRBAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /* Should agree with poll, otherwise some programs break */
2128 if (sock_writeable(sk))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002129 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
2131
Eric Dumazet43815482010-04-29 11:01:49 +00002132 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135static void sock_def_destruct(struct sock *sk)
2136{
Jesper Juhla51482b2005-11-08 09:41:34 -08002137 kfree(sk->sk_protinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
2140void sk_send_sigurg(struct sock *sk)
2141{
2142 if (sk->sk_socket && sk->sk_socket->file)
2143 if (send_sigurg(&sk->sk_socket->file->f_owner))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002144 sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
Eric Dumazet2a915252009-05-27 11:30:05 +00002146EXPORT_SYMBOL(sk_send_sigurg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148void sk_reset_timer(struct sock *sk, struct timer_list* timer,
2149 unsigned long expires)
2150{
2151 if (!mod_timer(timer, expires))
2152 sock_hold(sk);
2153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154EXPORT_SYMBOL(sk_reset_timer);
2155
2156void sk_stop_timer(struct sock *sk, struct timer_list* timer)
2157{
2158 if (timer_pending(timer) && del_timer(timer))
2159 __sock_put(sk);
2160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161EXPORT_SYMBOL(sk_stop_timer);
2162
2163void sock_init_data(struct socket *sock, struct sock *sk)
2164{
2165 skb_queue_head_init(&sk->sk_receive_queue);
2166 skb_queue_head_init(&sk->sk_write_queue);
2167 skb_queue_head_init(&sk->sk_error_queue);
Chris Leech97fc2f02006-05-23 17:55:33 -07002168#ifdef CONFIG_NET_DMA
2169 skb_queue_head_init(&sk->sk_async_wait_queue);
2170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 sk->sk_send_head = NULL;
2173
2174 init_timer(&sk->sk_timer);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 sk->sk_allocation = GFP_KERNEL;
2177 sk->sk_rcvbuf = sysctl_rmem_default;
2178 sk->sk_sndbuf = sysctl_wmem_default;
2179 sk->sk_state = TCP_CLOSE;
David S. Miller972692e2008-06-17 22:41:38 -07002180 sk_set_socket(sk, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 sock_set_flag(sk, SOCK_ZAPPED);
2183
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002184 if (sock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 sk->sk_type = sock->type;
Eric Dumazet43815482010-04-29 11:01:49 +00002186 sk->sk_wq = sock->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 sock->sk = sk;
2188 } else
Eric Dumazet43815482010-04-29 11:01:49 +00002189 sk->sk_wq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Eric Dumazetb6c67122010-04-08 23:03:29 +00002191 spin_lock_init(&sk->sk_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 rwlock_init(&sk->sk_callback_lock);
Peter Zijlstra443aef02007-07-19 01:49:00 -07002193 lockdep_set_class_and_name(&sk->sk_callback_lock,
2194 af_callback_keys + sk->sk_family,
2195 af_family_clock_key_strings[sk->sk_family]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 sk->sk_state_change = sock_def_wakeup;
2198 sk->sk_data_ready = sock_def_readable;
2199 sk->sk_write_space = sock_def_write_space;
2200 sk->sk_error_report = sock_def_error_report;
2201 sk->sk_destruct = sock_def_destruct;
2202
Eric Dumazet5640f762012-09-23 23:04:42 +00002203 sk->sk_frag.page = NULL;
2204 sk->sk_frag.offset = 0;
Pavel Emelyanovef64a542012-02-21 07:31:34 +00002205 sk->sk_peek_off = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Eric W. Biederman109f6e32010-06-13 03:30:14 +00002207 sk->sk_peer_pid = NULL;
2208 sk->sk_peer_cred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 sk->sk_write_pending = 0;
2210 sk->sk_rcvlowat = 1;
2211 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
2212 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
2213
Eric Dumazetf37f0af2008-04-13 21:39:26 -07002214 sk->sk_stamp = ktime_set(-1L, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00002216 /*
2217 * Before updating sk_refcnt, we must commit prior changes to memory
2218 * (Documentation/RCU/rculist_nulls.txt for details)
2219 */
2220 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 atomic_set(&sk->sk_refcnt, 1);
Wang Chen33c732c2007-11-13 20:30:01 -08002222 atomic_set(&sk->sk_drops, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
Eric Dumazet2a915252009-05-27 11:30:05 +00002224EXPORT_SYMBOL(sock_init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002226void lock_sock_nested(struct sock *sk, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
2228 might_sleep();
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002229 spin_lock_bh(&sk->sk_lock.slock);
John Heffnerd2e91172007-09-12 10:44:19 +02002230 if (sk->sk_lock.owned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 __lock_sock(sk);
John Heffnerd2e91172007-09-12 10:44:19 +02002232 sk->sk_lock.owned = 1;
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002233 spin_unlock(&sk->sk_lock.slock);
2234 /*
2235 * The sk_lock has mutex_lock() semantics here:
2236 */
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002237 mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002238 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002240EXPORT_SYMBOL(lock_sock_nested);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002242void release_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002244 /*
2245 * The sk_lock has mutex_unlock() semantics:
2246 */
2247 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
2248
2249 spin_lock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (sk->sk_backlog.tail)
2251 __release_sock(sk);
Eric Dumazet46d3cea2012-07-11 05:50:31 +00002252
2253 if (sk->sk_prot->release_cb)
2254 sk->sk_prot->release_cb(sk);
2255
John Heffnerd2e91172007-09-12 10:44:19 +02002256 sk->sk_lock.owned = 0;
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002257 if (waitqueue_active(&sk->sk_lock.wq))
2258 wake_up(&sk->sk_lock.wq);
2259 spin_unlock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261EXPORT_SYMBOL(release_sock);
2262
Eric Dumazet8a74ad62010-05-26 19:20:18 +00002263/**
2264 * lock_sock_fast - fast version of lock_sock
2265 * @sk: socket
2266 *
2267 * This version should be used for very small section, where process wont block
2268 * return false if fast path is taken
2269 * sk_lock.slock locked, owned = 0, BH disabled
2270 * return true if slow path is taken
2271 * sk_lock.slock unlocked, owned = 1, BH enabled
2272 */
2273bool lock_sock_fast(struct sock *sk)
2274{
2275 might_sleep();
2276 spin_lock_bh(&sk->sk_lock.slock);
2277
2278 if (!sk->sk_lock.owned)
2279 /*
2280 * Note : We must disable BH
2281 */
2282 return false;
2283
2284 __lock_sock(sk);
2285 sk->sk_lock.owned = 1;
2286 spin_unlock(&sk->sk_lock.slock);
2287 /*
2288 * The sk_lock has mutex_lock() semantics here:
2289 */
2290 mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
2291 local_bh_enable();
2292 return true;
2293}
2294EXPORT_SYMBOL(lock_sock_fast);
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002297{
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002298 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (!sock_flag(sk, SOCK_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002300 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002301 tv = ktime_to_timeval(sk->sk_stamp);
2302 if (tv.tv_sec == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return -ENOENT;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002304 if (tv.tv_sec == 0) {
2305 sk->sk_stamp = ktime_get_real();
2306 tv = ktime_to_timeval(sk->sk_stamp);
2307 }
2308 return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002309}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310EXPORT_SYMBOL(sock_get_timestamp);
2311
Eric Dumazetae40eb12007-03-18 17:33:16 -07002312int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
2313{
2314 struct timespec ts;
2315 if (!sock_flag(sk, SOCK_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002316 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazetae40eb12007-03-18 17:33:16 -07002317 ts = ktime_to_timespec(sk->sk_stamp);
2318 if (ts.tv_sec == -1)
2319 return -ENOENT;
2320 if (ts.tv_sec == 0) {
2321 sk->sk_stamp = ktime_get_real();
2322 ts = ktime_to_timespec(sk->sk_stamp);
2323 }
2324 return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
2325}
2326EXPORT_SYMBOL(sock_get_timestampns);
2327
Patrick Ohly20d49472009-02-12 05:03:38 +00002328void sock_enable_timestamp(struct sock *sk, int flag)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002329{
Patrick Ohly20d49472009-02-12 05:03:38 +00002330 if (!sock_flag(sk, flag)) {
Eric Dumazet08e29af2011-11-28 12:04:18 +00002331 unsigned long previous_flags = sk->sk_flags;
2332
Patrick Ohly20d49472009-02-12 05:03:38 +00002333 sock_set_flag(sk, flag);
2334 /*
2335 * we just set one of the two flags which require net
2336 * time stamping, but time stamping might have been on
2337 * already because of the other one
2338 */
Eric Dumazet08e29af2011-11-28 12:04:18 +00002339 if (!(previous_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002340 net_enable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
2342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344/*
2345 * Get a socket option on an socket.
2346 *
2347 * FIX: POSIX 1003.1g is very ambiguous here. It states that
2348 * asynchronous errors should be reported by getsockopt. We assume
2349 * this means if you specify SO_ERROR (otherwise whats the point of it).
2350 */
2351int sock_common_getsockopt(struct socket *sock, int level, int optname,
2352 char __user *optval, int __user *optlen)
2353{
2354 struct sock *sk = sock->sk;
2355
2356 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2357}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358EXPORT_SYMBOL(sock_common_getsockopt);
2359
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002360#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002361int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
2362 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002363{
2364 struct sock *sk = sock->sk;
2365
Johannes Berg1e51f952007-03-06 13:44:06 -08002366 if (sk->sk_prot->compat_getsockopt != NULL)
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002367 return sk->sk_prot->compat_getsockopt(sk, level, optname,
2368 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002369 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2370}
2371EXPORT_SYMBOL(compat_sock_common_getsockopt);
2372#endif
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
2375 struct msghdr *msg, size_t size, int flags)
2376{
2377 struct sock *sk = sock->sk;
2378 int addr_len = 0;
2379 int err;
2380
2381 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
2382 flags & ~MSG_DONTWAIT, &addr_len);
2383 if (err >= 0)
2384 msg->msg_namelen = addr_len;
2385 return err;
2386}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387EXPORT_SYMBOL(sock_common_recvmsg);
2388
2389/*
2390 * Set socket options on an inet socket.
2391 */
2392int sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002393 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 struct sock *sk = sock->sk;
2396
2397 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399EXPORT_SYMBOL(sock_common_setsockopt);
2400
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002401#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002402int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002403 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002404{
2405 struct sock *sk = sock->sk;
2406
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002407 if (sk->sk_prot->compat_setsockopt != NULL)
2408 return sk->sk_prot->compat_setsockopt(sk, level, optname,
2409 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002410 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2411}
2412EXPORT_SYMBOL(compat_sock_common_setsockopt);
2413#endif
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415void sk_common_release(struct sock *sk)
2416{
2417 if (sk->sk_prot->destroy)
2418 sk->sk_prot->destroy(sk);
2419
2420 /*
2421 * Observation: when sock_common_release is called, processes have
2422 * no access to socket. But net still has.
2423 * Step one, detach it from networking:
2424 *
2425 * A. Remove from hash tables.
2426 */
2427
2428 sk->sk_prot->unhash(sk);
2429
2430 /*
2431 * In this point socket cannot receive new packets, but it is possible
2432 * that some packets are in flight because some CPU runs receiver and
2433 * did hash table lookup before we unhashed socket. They will achieve
2434 * receive queue and will be purged by socket destructor.
2435 *
2436 * Also we still have packets pending on receive queue and probably,
2437 * our own packets waiting in device queues. sock_destroy will drain
2438 * receive queue, but transmitted packets will delay socket destruction
2439 * until the last reference will be released.
2440 */
2441
2442 sock_orphan(sk);
2443
2444 xfrm_sk_free_policy(sk);
2445
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -07002446 sk_refcnt_debug_release(sk);
Eric Dumazet5640f762012-09-23 23:04:42 +00002447
2448 if (sk->sk_frag.page) {
2449 put_page(sk->sk_frag.page);
2450 sk->sk_frag.page = NULL;
2451 }
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 sock_put(sk);
2454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455EXPORT_SYMBOL(sk_common_release);
2456
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002457#ifdef CONFIG_PROC_FS
2458#define PROTO_INUSE_NR 64 /* should be enough for the first time */
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002459struct prot_inuse {
2460 int val[PROTO_INUSE_NR];
2461};
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002462
2463static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002464
2465#ifdef CONFIG_NET_NS
2466void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
2467{
Eric Dumazetd6d9ca02010-07-19 10:48:49 +00002468 __this_cpu_add(net->core.inuse->val[prot->inuse_idx], val);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002469}
2470EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
2471
2472int sock_prot_inuse_get(struct net *net, struct proto *prot)
2473{
2474 int cpu, idx = prot->inuse_idx;
2475 int res = 0;
2476
2477 for_each_possible_cpu(cpu)
2478 res += per_cpu_ptr(net->core.inuse, cpu)->val[idx];
2479
2480 return res >= 0 ? res : 0;
2481}
2482EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
2483
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002484static int __net_init sock_inuse_init_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002485{
2486 net->core.inuse = alloc_percpu(struct prot_inuse);
2487 return net->core.inuse ? 0 : -ENOMEM;
2488}
2489
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002490static void __net_exit sock_inuse_exit_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002491{
2492 free_percpu(net->core.inuse);
2493}
2494
2495static struct pernet_operations net_inuse_ops = {
2496 .init = sock_inuse_init_net,
2497 .exit = sock_inuse_exit_net,
2498};
2499
2500static __init int net_inuse_init(void)
2501{
2502 if (register_pernet_subsys(&net_inuse_ops))
2503 panic("Cannot initialize net inuse counters");
2504
2505 return 0;
2506}
2507
2508core_initcall(net_inuse_init);
2509#else
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002510static DEFINE_PER_CPU(struct prot_inuse, prot_inuse);
2511
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -07002512void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002513{
Eric Dumazetd6d9ca02010-07-19 10:48:49 +00002514 __this_cpu_add(prot_inuse.val[prot->inuse_idx], val);
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002515}
2516EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
2517
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -07002518int sock_prot_inuse_get(struct net *net, struct proto *prot)
Pavel Emelyanov1338d462008-03-28 16:38:43 -07002519{
2520 int cpu, idx = prot->inuse_idx;
2521 int res = 0;
2522
2523 for_each_possible_cpu(cpu)
2524 res += per_cpu(prot_inuse, cpu).val[idx];
2525
2526 return res >= 0 ? res : 0;
2527}
2528EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07002529#endif
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002530
2531static void assign_proto_idx(struct proto *prot)
2532{
2533 prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
2534
2535 if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
Joe Perchese005d192012-05-16 19:58:40 +00002536 pr_err("PROTO_INUSE_NR exhausted\n");
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002537 return;
2538 }
2539
2540 set_bit(prot->inuse_idx, proto_inuse_idx);
2541}
2542
2543static void release_proto_idx(struct proto *prot)
2544{
2545 if (prot->inuse_idx != PROTO_INUSE_NR - 1)
2546 clear_bit(prot->inuse_idx, proto_inuse_idx);
2547}
2548#else
2549static inline void assign_proto_idx(struct proto *prot)
2550{
2551}
2552
2553static inline void release_proto_idx(struct proto *prot)
2554{
2555}
2556#endif
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558int proto_register(struct proto *prot, int alloc_slab)
2559{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (alloc_slab) {
2561 prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
Eric Dumazet271b72c2008-10-29 02:11:14 -07002562 SLAB_HWCACHE_ALIGN | prot->slab_flags,
2563 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 if (prot->slab == NULL) {
Joe Perchese005d192012-05-16 19:58:40 +00002566 pr_crit("%s: Can't create sock SLAB cache!\n",
2567 prot->name);
Pavel Emelyanov60e76632008-03-28 16:39:10 -07002568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002570
2571 if (prot->rsk_prot != NULL) {
Alexey Dobriyanfaf23422010-02-17 09:34:12 +00002572 prot->rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s", prot->name);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002573 if (prot->rsk_prot->slab_name == NULL)
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002574 goto out_free_sock_slab;
2575
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002576 prot->rsk_prot->slab = kmem_cache_create(prot->rsk_prot->slab_name,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002577 prot->rsk_prot->obj_size, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002578 SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002579
2580 if (prot->rsk_prot->slab == NULL) {
Joe Perchese005d192012-05-16 19:58:40 +00002581 pr_crit("%s: Can't create request sock SLAB cache!\n",
2582 prot->name);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002583 goto out_free_request_sock_slab_name;
2584 }
2585 }
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002586
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002587 if (prot->twsk_prot != NULL) {
Alexey Dobriyanfaf23422010-02-17 09:34:12 +00002588 prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002589
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002590 if (prot->twsk_prot->twsk_slab_name == NULL)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002591 goto out_free_request_sock_slab;
2592
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002593 prot->twsk_prot->twsk_slab =
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002594 kmem_cache_create(prot->twsk_prot->twsk_slab_name,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002595 prot->twsk_prot->twsk_obj_size,
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002596 0,
2597 SLAB_HWCACHE_ALIGN |
2598 prot->slab_flags,
Paul Mundt20c2df82007-07-20 10:11:58 +09002599 NULL);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002600 if (prot->twsk_prot->twsk_slab == NULL)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002601 goto out_free_timewait_sock_slab_name;
2602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
2604
Glauber Costa36b77a52011-12-16 00:51:59 +00002605 mutex_lock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 list_add(&prot->node, &proto_list);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002607 assign_proto_idx(prot);
Glauber Costa36b77a52011-12-16 00:51:59 +00002608 mutex_unlock(&proto_list_mutex);
Pavel Emelyanovb733c002007-11-07 02:23:38 -08002609 return 0;
2610
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002611out_free_timewait_sock_slab_name:
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002612 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002613out_free_request_sock_slab:
2614 if (prot->rsk_prot && prot->rsk_prot->slab) {
2615 kmem_cache_destroy(prot->rsk_prot->slab);
2616 prot->rsk_prot->slab = NULL;
2617 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002618out_free_request_sock_slab_name:
Dan Carpenter72150e92010-03-06 01:04:45 +00002619 if (prot->rsk_prot)
2620 kfree(prot->rsk_prot->slab_name);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002621out_free_sock_slab:
2622 kmem_cache_destroy(prot->slab);
2623 prot->slab = NULL;
Pavel Emelyanovb733c002007-11-07 02:23:38 -08002624out:
2625 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627EXPORT_SYMBOL(proto_register);
2628
2629void proto_unregister(struct proto *prot)
2630{
Glauber Costa36b77a52011-12-16 00:51:59 +00002631 mutex_lock(&proto_list_mutex);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07002632 release_proto_idx(prot);
Patrick McHardy0a3f4352005-09-06 19:47:50 -07002633 list_del(&prot->node);
Glauber Costa36b77a52011-12-16 00:51:59 +00002634 mutex_unlock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 if (prot->slab != NULL) {
2637 kmem_cache_destroy(prot->slab);
2638 prot->slab = NULL;
2639 }
2640
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002641 if (prot->rsk_prot != NULL && prot->rsk_prot->slab != NULL) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002642 kmem_cache_destroy(prot->rsk_prot->slab);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002643 kfree(prot->rsk_prot->slab_name);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002644 prot->rsk_prot->slab = NULL;
2645 }
2646
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002647 if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002648 kmem_cache_destroy(prot->twsk_prot->twsk_slab);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08002649 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002650 prot->twsk_prot->twsk_slab = NULL;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653EXPORT_SYMBOL(proto_unregister);
2654
2655#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
Glauber Costa36b77a52011-12-16 00:51:59 +00002657 __acquires(proto_list_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Glauber Costa36b77a52011-12-16 00:51:59 +00002659 mutex_lock(&proto_list_mutex);
Pavel Emelianov60f04382007-07-09 13:15:14 -07002660 return seq_list_start_head(&proto_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
2663static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2664{
Pavel Emelianov60f04382007-07-09 13:15:14 -07002665 return seq_list_next(v, &proto_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
2668static void proto_seq_stop(struct seq_file *seq, void *v)
Glauber Costa36b77a52011-12-16 00:51:59 +00002669 __releases(proto_list_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
Glauber Costa36b77a52011-12-16 00:51:59 +00002671 mutex_unlock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
2674static char proto_method_implemented(const void *method)
2675{
2676 return method == NULL ? 'n' : 'y';
2677}
Glauber Costa180d8cd2011-12-11 21:47:02 +00002678static long sock_prot_memory_allocated(struct proto *proto)
2679{
Jeffrin Josecb75a362012-04-25 19:17:29 +05302680 return proto->memory_allocated != NULL ? proto_memory_allocated(proto) : -1L;
Glauber Costa180d8cd2011-12-11 21:47:02 +00002681}
2682
2683static char *sock_prot_memory_pressure(struct proto *proto)
2684{
2685 return proto->memory_pressure != NULL ?
2686 proto_memory_pressure(proto) ? "yes" : "no" : "NI";
2687}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
2690{
Glauber Costa180d8cd2011-12-11 21:47:02 +00002691
Eric Dumazet8d987e52010-11-09 23:24:26 +00002692 seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
2694 proto->name,
2695 proto->obj_size,
Eric Dumazet14e943d2008-11-19 15:14:01 -08002696 sock_prot_inuse_get(seq_file_net(seq), proto),
Glauber Costa180d8cd2011-12-11 21:47:02 +00002697 sock_prot_memory_allocated(proto),
2698 sock_prot_memory_pressure(proto),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 proto->max_header,
2700 proto->slab == NULL ? "no" : "yes",
2701 module_name(proto->owner),
2702 proto_method_implemented(proto->close),
2703 proto_method_implemented(proto->connect),
2704 proto_method_implemented(proto->disconnect),
2705 proto_method_implemented(proto->accept),
2706 proto_method_implemented(proto->ioctl),
2707 proto_method_implemented(proto->init),
2708 proto_method_implemented(proto->destroy),
2709 proto_method_implemented(proto->shutdown),
2710 proto_method_implemented(proto->setsockopt),
2711 proto_method_implemented(proto->getsockopt),
2712 proto_method_implemented(proto->sendmsg),
2713 proto_method_implemented(proto->recvmsg),
2714 proto_method_implemented(proto->sendpage),
2715 proto_method_implemented(proto->bind),
2716 proto_method_implemented(proto->backlog_rcv),
2717 proto_method_implemented(proto->hash),
2718 proto_method_implemented(proto->unhash),
2719 proto_method_implemented(proto->get_port),
2720 proto_method_implemented(proto->enter_memory_pressure));
2721}
2722
2723static int proto_seq_show(struct seq_file *seq, void *v)
2724{
Pavel Emelianov60f04382007-07-09 13:15:14 -07002725 if (v == &proto_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
2727 "protocol",
2728 "size",
2729 "sockets",
2730 "memory",
2731 "press",
2732 "maxhdr",
2733 "slab",
2734 "module",
2735 "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
2736 else
Pavel Emelianov60f04382007-07-09 13:15:14 -07002737 proto_seq_printf(seq, list_entry(v, struct proto, node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return 0;
2739}
2740
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002741static const struct seq_operations proto_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 .start = proto_seq_start,
2743 .next = proto_seq_next,
2744 .stop = proto_seq_stop,
2745 .show = proto_seq_show,
2746};
2747
2748static int proto_seq_open(struct inode *inode, struct file *file)
2749{
Eric Dumazet14e943d2008-11-19 15:14:01 -08002750 return seq_open_net(inode, file, &proto_seq_ops,
2751 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
2753
Arjan van de Ven9a321442007-02-12 00:55:35 -08002754static const struct file_operations proto_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 .owner = THIS_MODULE,
2756 .open = proto_seq_open,
2757 .read = seq_read,
2758 .llseek = seq_lseek,
Eric Dumazet14e943d2008-11-19 15:14:01 -08002759 .release = seq_release_net,
2760};
2761
2762static __net_init int proto_init_net(struct net *net)
2763{
2764 if (!proc_net_fops_create(net, "protocols", S_IRUGO, &proto_seq_fops))
2765 return -ENOMEM;
2766
2767 return 0;
2768}
2769
2770static __net_exit void proto_exit_net(struct net *net)
2771{
2772 proc_net_remove(net, "protocols");
2773}
2774
2775
2776static __net_initdata struct pernet_operations proto_net_ops = {
2777 .init = proto_init_net,
2778 .exit = proto_exit_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779};
2780
2781static int __init proto_init(void)
2782{
Eric Dumazet14e943d2008-11-19 15:14:01 -08002783 return register_pernet_subsys(&proto_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786subsys_initcall(proto_init);
2787
2788#endif /* PROC_FS */