blob: e8b696858cadf5cf6ea81abbfbc4725f82987d66 [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>
Richard Cochrancb820f82013-07-19 19:40:09 +020096#include <linux/errqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/types.h>
98#include <linux/socket.h>
99#include <linux/in.h>
100#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <linux/module.h>
102#include <linux/proc_fs.h>
103#include <linux/seq_file.h>
104#include <linux/sched.h>
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700105#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <linux/timer.h>
107#include <linux/string.h>
108#include <linux/sockios.h>
109#include <linux/net.h>
110#include <linux/mm.h>
111#include <linux/slab.h>
112#include <linux/interrupt.h>
113#include <linux/poll.h>
114#include <linux/tcp.h>
115#include <linux/init.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -0400116#include <linux/highmem.h>
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000117#include <linux/user_namespace.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100118#include <linux/static_key.h>
David S. Miller3969eb32012-01-09 13:44:23 -0800119#include <linux/memcontrol.h>
David S. Miller8c1ae102012-05-03 02:25:55 -0400120#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800122#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124#include <linux/netdevice.h>
125#include <net/protocol.h>
126#include <linux/skbuff.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200127#include <net/net_namespace.h>
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700128#include <net/request_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#include <net/sock.h>
Patrick Ohly20d49472009-02-12 05:03:38 +0000130#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#include <net/xfrm.h>
132#include <linux/ipsec.h>
Herbert Xuf8451722010-05-24 00:12:34 -0700133#include <net/cls_cgroup.h>
Neil Horman5bc14212011-11-22 05:10:51 +0000134#include <net/netprio_cgroup.h>
Craig Gallekeb4cb002015-06-15 11:26:18 -0400135#include <linux/sock_diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137#include <linux/filter.h>
Craig Gallek538950a2016-01-04 17:41:47 -0500138#include <net/sock_reuseport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Satoru Moriya3847ce32011-06-17 12:00:03 +0000140#include <trace/events/sock.h>
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#include <net/tcp.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300143#include <net/busy_poll.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300144
Glauber Costa36b77a52011-12-16 00:51:59 +0000145static DEFINE_MUTEX(proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000146static LIST_HEAD(proto_list);
147
Eric W. Biedermana3b299d2014-04-23 14:26:56 -0700148/**
149 * sk_ns_capable - General socket capability test
150 * @sk: Socket to use a capability on or through
151 * @user_ns: The user namespace of the capability to use
152 * @cap: The capability to use
153 *
154 * Test to see if the opener of the socket had when the socket was
155 * created and the current process has the capability @cap in the user
156 * namespace @user_ns.
157 */
158bool sk_ns_capable(const struct sock *sk,
159 struct user_namespace *user_ns, int cap)
160{
161 return file_ns_capable(sk->sk_socket->file, user_ns, cap) &&
162 ns_capable(user_ns, cap);
163}
164EXPORT_SYMBOL(sk_ns_capable);
165
166/**
167 * sk_capable - Socket global capability test
168 * @sk: Socket to use a capability on or through
Masanari Iidae793c0f2014-09-04 23:44:36 +0900169 * @cap: The global capability to use
Eric W. Biedermana3b299d2014-04-23 14:26:56 -0700170 *
171 * Test to see if the opener of the socket had when the socket was
172 * created and the current process has the capability @cap in all user
173 * namespaces.
174 */
175bool sk_capable(const struct sock *sk, int cap)
176{
177 return sk_ns_capable(sk, &init_user_ns, cap);
178}
179EXPORT_SYMBOL(sk_capable);
180
181/**
182 * sk_net_capable - Network namespace socket capability test
183 * @sk: Socket to use a capability on or through
184 * @cap: The capability to use
185 *
Masanari Iidae793c0f2014-09-04 23:44:36 +0900186 * Test to see if the opener of the socket had when the socket was created
Eric W. Biedermana3b299d2014-04-23 14:26:56 -0700187 * and the current process has the capability @cap over the network namespace
188 * the socket is a member of.
189 */
190bool sk_net_capable(const struct sock *sk, int cap)
191{
192 return sk_ns_capable(sk, sock_net(sk)->user_ns, cap);
193}
194EXPORT_SYMBOL(sk_net_capable);
195
Ingo Molnarda21f242006-07-03 00:25:12 -0700196/*
197 * Each address family might have different locking rules, so we have
David Howellscdfbabf2017-03-09 08:09:05 +0000198 * one slock key per address family and separate keys for internal and
199 * userspace sockets.
Ingo Molnarda21f242006-07-03 00:25:12 -0700200 */
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700201static struct lock_class_key af_family_keys[AF_MAX];
David Howellscdfbabf2017-03-09 08:09:05 +0000202static struct lock_class_key af_family_kern_keys[AF_MAX];
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700203static struct lock_class_key af_family_slock_keys[AF_MAX];
David Howellscdfbabf2017-03-09 08:09:05 +0000204static struct lock_class_key af_family_kern_slock_keys[AF_MAX];
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700205
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700206/*
207 * Make lock validator output more readable. (we pre-construct these
208 * strings build-time, so that runtime initialization of socket
209 * locks is fast):
210 */
David Howellscdfbabf2017-03-09 08:09:05 +0000211
212#define _sock_locks(x) \
213 x "AF_UNSPEC", x "AF_UNIX" , x "AF_INET" , \
214 x "AF_AX25" , x "AF_IPX" , x "AF_APPLETALK", \
215 x "AF_NETROM", x "AF_BRIDGE" , x "AF_ATMPVC" , \
216 x "AF_X25" , x "AF_INET6" , x "AF_ROSE" , \
217 x "AF_DECnet", x "AF_NETBEUI" , x "AF_SECURITY" , \
218 x "AF_KEY" , x "AF_NETLINK" , x "AF_PACKET" , \
219 x "AF_ASH" , x "AF_ECONET" , x "AF_ATMSVC" , \
220 x "AF_RDS" , x "AF_SNA" , x "AF_IRDA" , \
221 x "AF_PPPOX" , x "AF_WANPIPE" , x "AF_LLC" , \
222 x "27" , x "28" , x "AF_CAN" , \
223 x "AF_TIPC" , x "AF_BLUETOOTH", x "IUCV" , \
224 x "AF_RXRPC" , x "AF_ISDN" , x "AF_PHONET" , \
225 x "AF_IEEE802154", x "AF_CAIF" , x "AF_ALG" , \
226 x "AF_NFC" , x "AF_VSOCK" , x "AF_KCM" , \
227 x "AF_QIPCRTR", x "AF_SMC" , x "AF_MAX"
228
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700229static const char *const af_family_key_strings[AF_MAX+1] = {
David Howellscdfbabf2017-03-09 08:09:05 +0000230 _sock_locks("sk_lock-")
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700231};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700232static const char *const af_family_slock_key_strings[AF_MAX+1] = {
David Howellscdfbabf2017-03-09 08:09:05 +0000233 _sock_locks("slock-")
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700234};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700235static const char *const af_family_clock_key_strings[AF_MAX+1] = {
David Howellscdfbabf2017-03-09 08:09:05 +0000236 _sock_locks("clock-")
237};
238
239static const char *const af_family_kern_key_strings[AF_MAX+1] = {
240 _sock_locks("k-sk_lock-")
241};
242static const char *const af_family_kern_slock_key_strings[AF_MAX+1] = {
243 _sock_locks("k-slock-")
244};
245static const char *const af_family_kern_clock_key_strings[AF_MAX+1] = {
246 _sock_locks("k-clock-")
Peter Zijlstra443aef0e2007-07-19 01:49:00 -0700247};
Paolo Abeni581319c2017-03-09 13:54:08 +0100248static const char *const af_family_rlock_key_strings[AF_MAX+1] = {
249 "rlock-AF_UNSPEC", "rlock-AF_UNIX" , "rlock-AF_INET" ,
250 "rlock-AF_AX25" , "rlock-AF_IPX" , "rlock-AF_APPLETALK",
251 "rlock-AF_NETROM", "rlock-AF_BRIDGE" , "rlock-AF_ATMPVC" ,
252 "rlock-AF_X25" , "rlock-AF_INET6" , "rlock-AF_ROSE" ,
253 "rlock-AF_DECnet", "rlock-AF_NETBEUI" , "rlock-AF_SECURITY" ,
254 "rlock-AF_KEY" , "rlock-AF_NETLINK" , "rlock-AF_PACKET" ,
255 "rlock-AF_ASH" , "rlock-AF_ECONET" , "rlock-AF_ATMSVC" ,
256 "rlock-AF_RDS" , "rlock-AF_SNA" , "rlock-AF_IRDA" ,
257 "rlock-AF_PPPOX" , "rlock-AF_WANPIPE" , "rlock-AF_LLC" ,
258 "rlock-27" , "rlock-28" , "rlock-AF_CAN" ,
259 "rlock-AF_TIPC" , "rlock-AF_BLUETOOTH", "rlock-AF_IUCV" ,
260 "rlock-AF_RXRPC" , "rlock-AF_ISDN" , "rlock-AF_PHONET" ,
261 "rlock-AF_IEEE802154", "rlock-AF_CAIF" , "rlock-AF_ALG" ,
262 "rlock-AF_NFC" , "rlock-AF_VSOCK" , "rlock-AF_KCM" ,
263 "rlock-AF_QIPCRTR", "rlock-AF_SMC" , "rlock-AF_MAX"
264};
265static const char *const af_family_wlock_key_strings[AF_MAX+1] = {
266 "wlock-AF_UNSPEC", "wlock-AF_UNIX" , "wlock-AF_INET" ,
267 "wlock-AF_AX25" , "wlock-AF_IPX" , "wlock-AF_APPLETALK",
268 "wlock-AF_NETROM", "wlock-AF_BRIDGE" , "wlock-AF_ATMPVC" ,
269 "wlock-AF_X25" , "wlock-AF_INET6" , "wlock-AF_ROSE" ,
270 "wlock-AF_DECnet", "wlock-AF_NETBEUI" , "wlock-AF_SECURITY" ,
271 "wlock-AF_KEY" , "wlock-AF_NETLINK" , "wlock-AF_PACKET" ,
272 "wlock-AF_ASH" , "wlock-AF_ECONET" , "wlock-AF_ATMSVC" ,
273 "wlock-AF_RDS" , "wlock-AF_SNA" , "wlock-AF_IRDA" ,
274 "wlock-AF_PPPOX" , "wlock-AF_WANPIPE" , "wlock-AF_LLC" ,
275 "wlock-27" , "wlock-28" , "wlock-AF_CAN" ,
276 "wlock-AF_TIPC" , "wlock-AF_BLUETOOTH", "wlock-AF_IUCV" ,
277 "wlock-AF_RXRPC" , "wlock-AF_ISDN" , "wlock-AF_PHONET" ,
278 "wlock-AF_IEEE802154", "wlock-AF_CAIF" , "wlock-AF_ALG" ,
279 "wlock-AF_NFC" , "wlock-AF_VSOCK" , "wlock-AF_KCM" ,
280 "wlock-AF_QIPCRTR", "wlock-AF_SMC" , "wlock-AF_MAX"
281};
282static const char *const af_family_elock_key_strings[AF_MAX+1] = {
283 "elock-AF_UNSPEC", "elock-AF_UNIX" , "elock-AF_INET" ,
284 "elock-AF_AX25" , "elock-AF_IPX" , "elock-AF_APPLETALK",
285 "elock-AF_NETROM", "elock-AF_BRIDGE" , "elock-AF_ATMPVC" ,
286 "elock-AF_X25" , "elock-AF_INET6" , "elock-AF_ROSE" ,
287 "elock-AF_DECnet", "elock-AF_NETBEUI" , "elock-AF_SECURITY" ,
288 "elock-AF_KEY" , "elock-AF_NETLINK" , "elock-AF_PACKET" ,
289 "elock-AF_ASH" , "elock-AF_ECONET" , "elock-AF_ATMSVC" ,
290 "elock-AF_RDS" , "elock-AF_SNA" , "elock-AF_IRDA" ,
291 "elock-AF_PPPOX" , "elock-AF_WANPIPE" , "elock-AF_LLC" ,
292 "elock-27" , "elock-28" , "elock-AF_CAN" ,
293 "elock-AF_TIPC" , "elock-AF_BLUETOOTH", "elock-AF_IUCV" ,
294 "elock-AF_RXRPC" , "elock-AF_ISDN" , "elock-AF_PHONET" ,
295 "elock-AF_IEEE802154", "elock-AF_CAIF" , "elock-AF_ALG" ,
296 "elock-AF_NFC" , "elock-AF_VSOCK" , "elock-AF_KCM" ,
297 "elock-AF_QIPCRTR", "elock-AF_SMC" , "elock-AF_MAX"
298};
Ingo Molnarda21f242006-07-03 00:25:12 -0700299
300/*
Paolo Abeni581319c2017-03-09 13:54:08 +0100301 * sk_callback_lock and sk queues locking rules are per-address-family,
Ingo Molnarda21f242006-07-03 00:25:12 -0700302 * so split the lock classes by using a per-AF key:
303 */
304static struct lock_class_key af_callback_keys[AF_MAX];
Paolo Abeni581319c2017-03-09 13:54:08 +0100305static struct lock_class_key af_rlock_keys[AF_MAX];
306static struct lock_class_key af_wlock_keys[AF_MAX];
307static struct lock_class_key af_elock_keys[AF_MAX];
David Howellscdfbabf2017-03-09 08:09:05 +0000308static struct lock_class_key af_kern_callback_keys[AF_MAX];
Ingo Molnarda21f242006-07-03 00:25:12 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/* Take into consideration the size of the struct sk_buff overhead in the
311 * determination of these values, since that is non-constant across
312 * platforms. This makes socket queueing behavior and performance
313 * not depend upon such differences.
314 */
315#define _SK_MEM_PACKETS 256
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000316#define _SK_MEM_OVERHEAD SKB_TRUESIZE(256)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317#define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
318#define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
319
320/* Run time adjustable parameters. */
Brian Haleyab32ea52006-09-22 14:15:41 -0700321__u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
Hans Schillstrom6d8ebc82012-04-30 08:13:50 +0200322EXPORT_SYMBOL(sysctl_wmem_max);
Brian Haleyab32ea52006-09-22 14:15:41 -0700323__u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
Hans Schillstrom6d8ebc82012-04-30 08:13:50 +0200324EXPORT_SYMBOL(sysctl_rmem_max);
Brian Haleyab32ea52006-09-22 14:15:41 -0700325__u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
326__u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300328/* Maximal space eaten by iovec or ancillary data plus some space */
Brian Haleyab32ea52006-09-22 14:15:41 -0700329int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
Eric Dumazet2a915252009-05-27 11:30:05 +0000330EXPORT_SYMBOL(sysctl_optmem_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Willem de Bruijnb245be12015-01-30 13:29:32 -0500332int sysctl_tstamp_allow_data __read_mostly = 1;
333
Mel Gormanc93bdd02012-07-31 16:44:19 -0700334struct static_key memalloc_socks = STATIC_KEY_INIT_FALSE;
335EXPORT_SYMBOL_GPL(memalloc_socks);
336
Mel Gorman7cb02402012-07-31 16:44:16 -0700337/**
338 * sk_set_memalloc - sets %SOCK_MEMALLOC
339 * @sk: socket to set it on
340 *
341 * Set %SOCK_MEMALLOC on a socket for access to emergency reserves.
342 * It's the responsibility of the admin to adjust min_free_kbytes
343 * to meet the requirements
344 */
345void sk_set_memalloc(struct sock *sk)
346{
347 sock_set_flag(sk, SOCK_MEMALLOC);
348 sk->sk_allocation |= __GFP_MEMALLOC;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700349 static_key_slow_inc(&memalloc_socks);
Mel Gorman7cb02402012-07-31 16:44:16 -0700350}
351EXPORT_SYMBOL_GPL(sk_set_memalloc);
352
353void sk_clear_memalloc(struct sock *sk)
354{
355 sock_reset_flag(sk, SOCK_MEMALLOC);
356 sk->sk_allocation &= ~__GFP_MEMALLOC;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700357 static_key_slow_dec(&memalloc_socks);
Mel Gormanc76562b2012-07-31 16:44:41 -0700358
359 /*
360 * SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
Mel Gorman5d753612015-06-10 21:02:04 -0400361 * progress of swapping. SOCK_MEMALLOC may be cleared while
362 * it has rmem allocations due to the last swapfile being deactivated
363 * but there is a risk that the socket is unusable due to exceeding
364 * the rmem limits. Reclaim the reserves and obey rmem limits again.
Mel Gormanc76562b2012-07-31 16:44:41 -0700365 */
Mel Gorman5d753612015-06-10 21:02:04 -0400366 sk_mem_reclaim(sk);
Mel Gorman7cb02402012-07-31 16:44:16 -0700367}
368EXPORT_SYMBOL_GPL(sk_clear_memalloc);
369
Mel Gormanb4b9e352012-07-31 16:44:26 -0700370int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
371{
372 int ret;
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700373 unsigned int noreclaim_flag;
Mel Gormanb4b9e352012-07-31 16:44:26 -0700374
375 /* these should have been dropped before queueing */
376 BUG_ON(!sock_flag(sk, SOCK_MEMALLOC));
377
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700378 noreclaim_flag = memalloc_noreclaim_save();
Mel Gormanb4b9e352012-07-31 16:44:26 -0700379 ret = sk->sk_backlog_rcv(sk, skb);
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700380 memalloc_noreclaim_restore(noreclaim_flag);
Mel Gormanb4b9e352012-07-31 16:44:26 -0700381
382 return ret;
383}
384EXPORT_SYMBOL(__sk_backlog_rcv);
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
387{
388 struct timeval tv;
389
390 if (optlen < sizeof(tv))
391 return -EINVAL;
392 if (copy_from_user(&tv, optval, sizeof(tv)))
393 return -EFAULT;
Vasily Averinba780732007-05-24 16:58:54 -0700394 if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
395 return -EDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Vasily Averinba780732007-05-24 16:58:54 -0700397 if (tv.tv_sec < 0) {
Andrew Morton6f11df82007-07-09 13:16:00 -0700398 static int warned __read_mostly;
399
Vasily Averinba780732007-05-24 16:58:54 -0700400 *timeo_p = 0;
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700401 if (warned < 10 && net_ratelimit()) {
Vasily Averinba780732007-05-24 16:58:54 -0700402 warned++;
Joe Perchese005d192012-05-16 19:58:40 +0000403 pr_info("%s: `%s' (pid %d) tries to set negative timeout\n",
404 __func__, current->comm, task_pid_nr(current));
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700405 }
Vasily Averinba780732007-05-24 16:58:54 -0700406 return 0;
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *timeo_p = MAX_SCHEDULE_TIMEOUT;
409 if (tv.tv_sec == 0 && tv.tv_usec == 0)
410 return 0;
411 if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
Gao Feng8ccde4c2017-02-21 17:09:19 +0800412 *timeo_p = tv.tv_sec * HZ + DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 0;
414}
415
416static void sock_warn_obsolete_bsdism(const char *name)
417{
418 static int warned;
419 static char warncomm[TASK_COMM_LEN];
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900420 if (strcmp(warncomm, current->comm) && warned < 5) {
421 strcpy(warncomm, current->comm);
Joe Perchese005d192012-05-16 19:58:40 +0000422 pr_warn("process `%s' is using obsolete %s SO_BSDCOMPAT\n",
423 warncomm, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 warned++;
425 }
426}
427
Hannes Frederic Sowa080a2702015-10-26 13:51:37 +0100428static bool sock_needs_netstamp(const struct sock *sk)
429{
430 switch (sk->sk_family) {
431 case AF_UNSPEC:
432 case AF_UNIX:
433 return false;
434 default:
435 return true;
436 }
437}
438
Eric Dumazet08e29af2011-11-28 12:04:18 +0000439static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900440{
Eric Dumazet08e29af2011-11-28 12:04:18 +0000441 if (sk->sk_flags & flags) {
442 sk->sk_flags &= ~flags;
Hannes Frederic Sowa080a2702015-10-26 13:51:37 +0100443 if (sock_needs_netstamp(sk) &&
444 !(sk->sk_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +0000445 net_disable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447}
448
449
samanthakumare6afc8a2016-04-05 12:41:15 -0400450int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800451{
Neil Horman3b885782009-10-12 13:26:31 -0700452 unsigned long flags;
453 struct sk_buff_head *list = &sk->sk_receive_queue;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800454
Eric Dumazet0fd7bac2011-12-21 07:11:44 +0000455 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700456 atomic_inc(&sk->sk_drops);
Satoru Moriya3847ce32011-06-17 12:00:03 +0000457 trace_sock_rcvqueue_full(sk, skb);
Eric Dumazet766e90372009-10-14 20:40:11 -0700458 return -ENOMEM;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800459 }
460
Mel Gormanc76562b2012-07-31 16:44:41 -0700461 if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700462 atomic_inc(&sk->sk_drops);
463 return -ENOBUFS;
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800464 }
465
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800466 skb->dev = NULL;
467 skb_set_owner_r(skb, sk);
David S. Miller49ad9592008-12-17 22:11:38 -0800468
Eric Dumazet7fee2262010-05-11 23:19:48 +0000469 /* we escape from rcu protected region, make sure we dont leak
470 * a norefcounted dst
471 */
472 skb_dst_force(skb);
473
Neil Horman3b885782009-10-12 13:26:31 -0700474 spin_lock_irqsave(&list->lock, flags);
Eyal Birger3bc3b962015-03-01 14:58:30 +0200475 sock_skb_set_dropcount(sk, skb);
Neil Horman3b885782009-10-12 13:26:31 -0700476 __skb_queue_tail(list, skb);
477 spin_unlock_irqrestore(&list->lock, flags);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800478
479 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -0400480 sk->sk_data_ready(sk);
Eric Dumazet766e90372009-10-14 20:40:11 -0700481 return 0;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800482}
samanthakumare6afc8a2016-04-05 12:41:15 -0400483EXPORT_SYMBOL(__sock_queue_rcv_skb);
484
485int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
486{
487 int err;
488
489 err = sk_filter(sk, skb);
490 if (err)
491 return err;
492
493 return __sock_queue_rcv_skb(sk, skb);
494}
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800495EXPORT_SYMBOL(sock_queue_rcv_skb);
496
Willem de Bruijn4f0c40d92016-07-12 18:18:57 -0400497int __sk_receive_skb(struct sock *sk, struct sk_buff *skb,
Eric Dumazetc3f24cf2016-11-02 17:14:41 -0700498 const int nested, unsigned int trim_cap, bool refcounted)
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800499{
500 int rc = NET_RX_SUCCESS;
501
Willem de Bruijn4f0c40d92016-07-12 18:18:57 -0400502 if (sk_filter_trim_cap(sk, skb, trim_cap))
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800503 goto discard_and_relse;
504
505 skb->dev = NULL;
506
Sorin Dumitru274f4822014-07-22 21:16:51 +0300507 if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
Eric Dumazetc3774112010-04-27 15:13:20 -0700508 atomic_inc(&sk->sk_drops);
509 goto discard_and_relse;
510 }
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200511 if (nested)
512 bh_lock_sock_nested(sk);
513 else
514 bh_lock_sock(sk);
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700515 if (!sock_owned_by_user(sk)) {
516 /*
517 * trylock + unlock semantics:
518 */
519 mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
520
Peter Zijlstrac57943a2008-10-07 14:18:42 -0700521 rc = sk_backlog_rcv(sk, skb);
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700522
523 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
Eric Dumazetf545a382012-04-22 23:34:26 +0000524 } else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
Zhu Yi8eae9392010-03-04 18:01:40 +0000525 bh_unlock_sock(sk);
526 atomic_inc(&sk->sk_drops);
527 goto discard_and_relse;
528 }
529
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800530 bh_unlock_sock(sk);
531out:
Eric Dumazetc3f24cf2016-11-02 17:14:41 -0700532 if (refcounted)
533 sock_put(sk);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800534 return rc;
535discard_and_relse:
536 kfree_skb(skb);
537 goto out;
538}
Willem de Bruijn4f0c40d92016-07-12 18:18:57 -0400539EXPORT_SYMBOL(__sk_receive_skb);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800540
541struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
542{
Eric Dumazetb6c67122010-04-08 23:03:29 +0000543 struct dst_entry *dst = __sk_dst_get(sk);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800544
545 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
Krishna Kumare022f0b2009-10-19 23:46:20 +0000546 sk_tx_queue_clear(sk);
Julian Anastasov9b8805a2017-02-06 23:14:11 +0200547 sk->sk_dst_pending_confirm = 0;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000548 RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800549 dst_release(dst);
550 return NULL;
551 }
552
553 return dst;
554}
555EXPORT_SYMBOL(__sk_dst_check);
556
557struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
558{
559 struct dst_entry *dst = sk_dst_get(sk);
560
561 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
562 sk_dst_reset(sk);
563 dst_release(dst);
564 return NULL;
565 }
566
567 return dst;
568}
569EXPORT_SYMBOL(sk_dst_check);
570
Brian Haleyc91f6df2012-11-26 05:21:08 +0000571static int sock_setbindtodevice(struct sock *sk, char __user *optval,
572 int optlen)
David S. Miller48788092007-09-14 16:41:03 -0700573{
574 int ret = -ENOPROTOOPT;
575#ifdef CONFIG_NETDEVICES
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900576 struct net *net = sock_net(sk);
David S. Miller48788092007-09-14 16:41:03 -0700577 char devname[IFNAMSIZ];
578 int index;
579
580 /* Sorry... */
581 ret = -EPERM;
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000582 if (!ns_capable(net->user_ns, CAP_NET_RAW))
David S. Miller48788092007-09-14 16:41:03 -0700583 goto out;
584
585 ret = -EINVAL;
586 if (optlen < 0)
587 goto out;
588
589 /* Bind this socket to a particular device like "eth0",
590 * as specified in the passed interface name. If the
591 * name is "" or the option length is zero the socket
592 * is not bound.
593 */
594 if (optlen > IFNAMSIZ - 1)
595 optlen = IFNAMSIZ - 1;
596 memset(devname, 0, sizeof(devname));
597
598 ret = -EFAULT;
599 if (copy_from_user(devname, optval, optlen))
600 goto out;
601
David S. Miller000ba2e2009-11-05 22:37:11 -0800602 index = 0;
603 if (devname[0] != '\0') {
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800604 struct net_device *dev;
David S. Miller48788092007-09-14 16:41:03 -0700605
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800606 rcu_read_lock();
607 dev = dev_get_by_name_rcu(net, devname);
608 if (dev)
609 index = dev->ifindex;
610 rcu_read_unlock();
David S. Miller48788092007-09-14 16:41:03 -0700611 ret = -ENODEV;
612 if (!dev)
613 goto out;
David S. Miller48788092007-09-14 16:41:03 -0700614 }
615
616 lock_sock(sk);
617 sk->sk_bound_dev_if = index;
618 sk_dst_reset(sk);
619 release_sock(sk);
620
621 ret = 0;
622
623out:
624#endif
625
626 return ret;
627}
628
Brian Haleyc91f6df2012-11-26 05:21:08 +0000629static int sock_getbindtodevice(struct sock *sk, char __user *optval,
630 int __user *optlen, int len)
631{
632 int ret = -ENOPROTOOPT;
633#ifdef CONFIG_NETDEVICES
634 struct net *net = sock_net(sk);
Brian Haleyc91f6df2012-11-26 05:21:08 +0000635 char devname[IFNAMSIZ];
Brian Haleyc91f6df2012-11-26 05:21:08 +0000636
637 if (sk->sk_bound_dev_if == 0) {
638 len = 0;
639 goto zero;
640 }
641
642 ret = -EINVAL;
643 if (len < IFNAMSIZ)
644 goto out;
645
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200646 ret = netdev_get_name(net, devname, sk->sk_bound_dev_if);
647 if (ret)
Brian Haleyc91f6df2012-11-26 05:21:08 +0000648 goto out;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000649
650 len = strlen(devname) + 1;
651
652 ret = -EFAULT;
653 if (copy_to_user(optval, devname, len))
654 goto out;
655
656zero:
657 ret = -EFAULT;
658 if (put_user(len, optlen))
659 goto out;
660
661 ret = 0;
662
663out:
664#endif
665
666 return ret;
667}
668
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800669static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
670{
671 if (valbool)
672 sock_set_flag(sk, bit);
673 else
674 sock_reset_flag(sk, bit);
675}
676
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +0200677bool sk_mc_loop(struct sock *sk)
678{
679 if (dev_recursion_level())
680 return false;
681 if (!sk)
682 return true;
683 switch (sk->sk_family) {
684 case AF_INET:
685 return inet_sk(sk)->mc_loop;
686#if IS_ENABLED(CONFIG_IPV6)
687 case AF_INET6:
688 return inet6_sk(sk)->mc_loop;
689#endif
690 }
691 WARN_ON(1);
692 return true;
693}
694EXPORT_SYMBOL(sk_mc_loop);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/*
697 * This is meant for all protocols to use and covers goings on
698 * at the socket level. Everything here is generic.
699 */
700
701int sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700702 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Eric Dumazet2a915252009-05-27 11:30:05 +0000704 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int val;
706 int valbool;
707 struct linger ling;
708 int ret = 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * Options without arguments
712 */
713
David S. Miller48788092007-09-14 16:41:03 -0700714 if (optname == SO_BINDTODEVICE)
Brian Haleyc91f6df2012-11-26 05:21:08 +0000715 return sock_setbindtodevice(sk, optval, optlen);
David S. Miller48788092007-09-14 16:41:03 -0700716
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700717 if (optlen < sizeof(int))
718 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (get_user(val, (int __user *)optval))
721 return -EFAULT;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900722
Eric Dumazet2a915252009-05-27 11:30:05 +0000723 valbool = val ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 lock_sock(sk);
726
Eric Dumazet2a915252009-05-27 11:30:05 +0000727 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700728 case SO_DEBUG:
Eric Dumazet2a915252009-05-27 11:30:05 +0000729 if (val && !capable(CAP_NET_ADMIN))
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700730 ret = -EACCES;
Eric Dumazet2a915252009-05-27 11:30:05 +0000731 else
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800732 sock_valbool_flag(sk, SOCK_DBG, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700733 break;
734 case SO_REUSEADDR:
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +0000735 sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700736 break;
Tom Herbert055dc212013-01-22 09:49:50 +0000737 case SO_REUSEPORT:
738 sk->sk_reuseport = valbool;
739 break;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700740 case SO_TYPE:
Jan Engelhardt49c794e2009-08-04 07:28:28 +0000741 case SO_PROTOCOL:
Jan Engelhardt0d6038e2009-08-04 07:28:29 +0000742 case SO_DOMAIN:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700743 case SO_ERROR:
744 ret = -ENOPROTOOPT;
745 break;
746 case SO_DONTROUTE:
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800747 sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700748 break;
749 case SO_BROADCAST:
750 sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
751 break;
752 case SO_SNDBUF:
753 /* Don't error on this BSD doesn't and if you think
Eric Dumazet82981932012-04-26 20:07:59 +0000754 * about it this is right. Otherwise apps have to
755 * play 'guess the biggest size' games. RCVBUF/SNDBUF
756 * are treated in BSD as hints
757 */
758 val = min_t(u32, val, sysctl_wmem_max);
Patrick McHardyb0573de2005-08-09 19:30:51 -0700759set_sndbuf:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700760 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Eric Dumazetb98b0bc2016-12-02 09:44:53 -0800761 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
Eric Dumazet82981932012-04-26 20:07:59 +0000762 /* Wake up sending tasks if we upped the value. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700763 sk->sk_write_space(sk);
764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700766 case SO_SNDBUFFORCE:
767 if (!capable(CAP_NET_ADMIN)) {
768 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
770 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700771 goto set_sndbuf;
772
773 case SO_RCVBUF:
774 /* Don't error on this BSD doesn't and if you think
Eric Dumazet82981932012-04-26 20:07:59 +0000775 * about it this is right. Otherwise apps have to
776 * play 'guess the biggest size' games. RCVBUF/SNDBUF
777 * are treated in BSD as hints
778 */
779 val = min_t(u32, val, sysctl_rmem_max);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700780set_rcvbuf:
781 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
782 /*
783 * We double it on the way in to account for
784 * "struct sk_buff" etc. overhead. Applications
785 * assume that the SO_RCVBUF setting they make will
786 * allow that much actual data to be received on that
787 * socket.
788 *
789 * Applications are unaware that "struct sk_buff" and
790 * other overheads allocate from the receive buffer
791 * during socket buffer allocation.
792 *
793 * And after considering the possible alternatives,
794 * returning the value we actually used in getsockopt
795 * is the most desirable behavior.
796 */
Eric Dumazetb98b0bc2016-12-02 09:44:53 -0800797 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700798 break;
799
800 case SO_RCVBUFFORCE:
801 if (!capable(CAP_NET_ADMIN)) {
802 ret = -EPERM;
803 break;
804 }
805 goto set_rcvbuf;
806
807 case SO_KEEPALIVE:
Ursula Braun4b9d07a2017-01-09 16:55:12 +0100808 if (sk->sk_prot->keepalive)
809 sk->sk_prot->keepalive(sk, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700810 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
811 break;
812
813 case SO_OOBINLINE:
814 sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
815 break;
816
817 case SO_NO_CHECK:
Tom Herbert28448b82014-05-23 08:47:19 -0700818 sk->sk_no_check_tx = valbool;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700819 break;
820
821 case SO_PRIORITY:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000822 if ((val >= 0 && val <= 6) ||
823 ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700824 sk->sk_priority = val;
825 else
826 ret = -EPERM;
827 break;
828
829 case SO_LINGER:
830 if (optlen < sizeof(ling)) {
831 ret = -EINVAL; /* 1003.1g */
832 break;
833 }
Eric Dumazet2a915252009-05-27 11:30:05 +0000834 if (copy_from_user(&ling, optval, sizeof(ling))) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700835 ret = -EFAULT;
836 break;
837 }
838 if (!ling.l_onoff)
839 sock_reset_flag(sk, SOCK_LINGER);
840 else {
841#if (BITS_PER_LONG == 32)
842 if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
843 sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
844 else
845#endif
846 sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
847 sock_set_flag(sk, SOCK_LINGER);
848 }
849 break;
850
851 case SO_BSDCOMPAT:
852 sock_warn_obsolete_bsdism("setsockopt");
853 break;
854
855 case SO_PASSCRED:
856 if (valbool)
857 set_bit(SOCK_PASSCRED, &sock->flags);
858 else
859 clear_bit(SOCK_PASSCRED, &sock->flags);
860 break;
861
862 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700863 case SO_TIMESTAMPNS:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700864 if (valbool) {
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700865 if (optname == SO_TIMESTAMP)
866 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
867 else
868 sock_set_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700869 sock_set_flag(sk, SOCK_RCVTSTAMP);
Patrick Ohly20d49472009-02-12 05:03:38 +0000870 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700871 } else {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700872 sock_reset_flag(sk, SOCK_RCVTSTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700873 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
874 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700875 break;
876
Patrick Ohly20d49472009-02-12 05:03:38 +0000877 case SO_TIMESTAMPING:
878 if (val & ~SOF_TIMESTAMPING_MASK) {
Rémi Denis-Courmontf249fb72009-07-20 00:47:04 +0000879 ret = -EINVAL;
Patrick Ohly20d49472009-02-12 05:03:38 +0000880 break;
881 }
Willem de Bruijnb245be12015-01-30 13:29:32 -0500882
Willem de Bruijn09c2d252014-08-04 22:11:47 -0400883 if (val & SOF_TIMESTAMPING_OPT_ID &&
Willem de Bruijn4ed2d762014-08-04 22:11:49 -0400884 !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) {
WANG Congac5cc972015-12-16 23:39:04 -0800885 if (sk->sk_protocol == IPPROTO_TCP &&
886 sk->sk_type == SOCK_STREAM) {
Soheil Hassas Yeganeh6db8b962016-04-02 23:08:07 -0400887 if ((1 << sk->sk_state) &
888 (TCPF_CLOSE | TCPF_LISTEN)) {
Willem de Bruijn4ed2d762014-08-04 22:11:49 -0400889 ret = -EINVAL;
890 break;
891 }
892 sk->sk_tskey = tcp_sk(sk)->snd_una;
893 } else {
894 sk->sk_tskey = 0;
895 }
896 }
Francis Yan1c885802016-11-27 23:07:18 -0800897
898 if (val & SOF_TIMESTAMPING_OPT_STATS &&
899 !(val & SOF_TIMESTAMPING_OPT_TSONLY)) {
900 ret = -EINVAL;
901 break;
902 }
903
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400904 sk->sk_tsflags = val;
Patrick Ohly20d49472009-02-12 05:03:38 +0000905 if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
906 sock_enable_timestamp(sk,
907 SOCK_TIMESTAMPING_RX_SOFTWARE);
908 else
909 sock_disable_timestamp(sk,
Eric Dumazet08e29af2011-11-28 12:04:18 +0000910 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
Patrick Ohly20d49472009-02-12 05:03:38 +0000911 break;
912
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700913 case SO_RCVLOWAT:
914 if (val < 0)
915 val = INT_MAX;
916 sk->sk_rcvlowat = val ? : 1;
917 break;
918
919 case SO_RCVTIMEO:
920 ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
921 break;
922
923 case SO_SNDTIMEO:
924 ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
925 break;
926
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700927 case SO_ATTACH_FILTER:
928 ret = -EINVAL;
929 if (optlen == sizeof(struct sock_fprog)) {
930 struct sock_fprog fprog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700932 ret = -EFAULT;
933 if (copy_from_user(&fprog, optval, sizeof(fprog)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700936 ret = sk_attach_filter(&fprog, sk);
937 }
938 break;
939
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800940 case SO_ATTACH_BPF:
941 ret = -EINVAL;
942 if (optlen == sizeof(u32)) {
943 u32 ufd;
944
945 ret = -EFAULT;
946 if (copy_from_user(&ufd, optval, sizeof(ufd)))
947 break;
948
949 ret = sk_attach_bpf(ufd, sk);
950 }
951 break;
952
Craig Gallek538950a2016-01-04 17:41:47 -0500953 case SO_ATTACH_REUSEPORT_CBPF:
954 ret = -EINVAL;
955 if (optlen == sizeof(struct sock_fprog)) {
956 struct sock_fprog fprog;
957
958 ret = -EFAULT;
959 if (copy_from_user(&fprog, optval, sizeof(fprog)))
960 break;
961
962 ret = sk_reuseport_attach_filter(&fprog, sk);
963 }
964 break;
965
966 case SO_ATTACH_REUSEPORT_EBPF:
967 ret = -EINVAL;
968 if (optlen == sizeof(u32)) {
969 u32 ufd;
970
971 ret = -EFAULT;
972 if (copy_from_user(&ufd, optval, sizeof(ufd)))
973 break;
974
975 ret = sk_reuseport_attach_bpf(ufd, sk);
976 }
977 break;
978
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700979 case SO_DETACH_FILTER:
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700980 ret = sk_detach_filter(sk);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700981 break;
982
Vincent Bernatd59577b2013-01-16 22:55:49 +0100983 case SO_LOCK_FILTER:
984 if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool)
985 ret = -EPERM;
986 else
987 sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool);
988 break;
989
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700990 case SO_PASSSEC:
991 if (valbool)
992 set_bit(SOCK_PASSSEC, &sock->flags);
993 else
994 clear_bit(SOCK_PASSSEC, &sock->flags);
995 break;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800996 case SO_MARK:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000997 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800998 ret = -EPERM;
Eric Dumazet2a915252009-05-27 11:30:05 +0000999 else
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001000 sk->sk_mark = val;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001001 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07001002
Neil Horman3b885782009-10-12 13:26:31 -07001003 case SO_RXQ_OVFL:
Johannes Berg8083f0f2011-10-07 03:30:20 +00001004 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
Neil Horman3b885782009-10-12 13:26:31 -07001005 break;
Johannes Berg6e3e9392011-11-09 10:15:42 +01001006
1007 case SO_WIFI_STATUS:
1008 sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
1009 break;
1010
Pavel Emelyanovef64a542012-02-21 07:31:34 +00001011 case SO_PEEK_OFF:
1012 if (sock->ops->set_peek_off)
Sasha Levin12663bf2013-12-07 17:26:27 -05001013 ret = sock->ops->set_peek_off(sk, val);
Pavel Emelyanovef64a542012-02-21 07:31:34 +00001014 else
1015 ret = -EOPNOTSUPP;
1016 break;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001017
1018 case SO_NOFCS:
1019 sock_valbool_flag(sk, SOCK_NOFCS, valbool);
1020 break;
1021
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00001022 case SO_SELECT_ERR_QUEUE:
1023 sock_valbool_flag(sk, SOCK_SELECT_ERR_QUEUE, valbool);
1024 break;
1025
Cong Wange0d10952013-08-01 11:10:25 +08001026#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +03001027 case SO_BUSY_POLL:
Eliezer Tamirdafcc432013-06-14 16:33:57 +03001028 /* allow unprivileged users to decrease the value */
1029 if ((val > sk->sk_ll_usec) && !capable(CAP_NET_ADMIN))
1030 ret = -EPERM;
1031 else {
1032 if (val < 0)
1033 ret = -EINVAL;
1034 else
1035 sk->sk_ll_usec = val;
1036 }
1037 break;
1038#endif
Eric Dumazet62748f32013-09-24 08:20:52 -07001039
1040 case SO_MAX_PACING_RATE:
Eric Dumazet218af592017-05-16 04:24:36 -07001041 if (val != ~0U)
1042 cmpxchg(&sk->sk_pacing_status,
1043 SK_PACING_NONE,
1044 SK_PACING_NEEDED);
Eric Dumazet62748f32013-09-24 08:20:52 -07001045 sk->sk_max_pacing_rate = val;
1046 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
1047 sk->sk_max_pacing_rate);
1048 break;
1049
Eric Dumazet70da2682015-10-08 19:33:21 -07001050 case SO_INCOMING_CPU:
1051 sk->sk_incoming_cpu = val;
1052 break;
1053
Tom Herberta87cb3e2016-02-24 10:02:52 -08001054 case SO_CNX_ADVICE:
1055 if (val == 1)
1056 dst_negative_advice(sk);
1057 break;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001058 default:
1059 ret = -ENOPROTOOPT;
1060 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 release_sock(sk);
1063 return ret;
1064}
Eric Dumazet2a915252009-05-27 11:30:05 +00001065EXPORT_SYMBOL(sock_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067
stephen hemminger8f098982014-01-03 09:17:14 -08001068static void cred_to_ucred(struct pid *pid, const struct cred *cred,
1069 struct ucred *ucred)
Eric W. Biederman3f551f92010-06-13 03:28:59 +00001070{
1071 ucred->pid = pid_vnr(pid);
1072 ucred->uid = ucred->gid = -1;
1073 if (cred) {
1074 struct user_namespace *current_ns = current_user_ns();
1075
Eric W. Biedermanb2e4f542012-05-23 16:39:45 -06001076 ucred->uid = from_kuid_munged(current_ns, cred->euid);
1077 ucred->gid = from_kgid_munged(current_ns, cred->egid);
Eric W. Biederman3f551f92010-06-13 03:28:59 +00001078 }
1079}
1080
David Herrmann28b5ba2a2017-06-21 10:47:15 +02001081static int groups_to_user(gid_t __user *dst, const struct group_info *src)
1082{
1083 struct user_namespace *user_ns = current_user_ns();
1084 int i;
1085
1086 for (i = 0; i < src->ngroups; i++)
1087 if (put_user(from_kgid_munged(user_ns, src->gid[i]), dst + i))
1088 return -EFAULT;
1089
1090 return 0;
1091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093int sock_getsockopt(struct socket *sock, int level, int optname,
1094 char __user *optval, int __user *optlen)
1095{
1096 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001097
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001098 union {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001099 int val;
Chenbo Feng5daab9d2017-04-05 19:00:55 -07001100 u64 val64;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001101 struct linger ling;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct timeval tm;
1103 } v;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001104
H Hartley Sweeten4d0392b2010-01-15 01:08:58 -08001105 int lv = sizeof(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 int len;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001107
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001108 if (get_user(len, optlen))
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001109 return -EFAULT;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001110 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001112
Eugene Teo50fee1d2009-02-23 15:38:41 -08001113 memset(&v, 0, sizeof(v));
Clément Lecignedf0bca02009-02-12 16:59:09 -08001114
Eric Dumazet2a915252009-05-27 11:30:05 +00001115 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001116 case SO_DEBUG:
1117 v.val = sock_flag(sk, SOCK_DBG);
1118 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001119
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001120 case SO_DONTROUTE:
1121 v.val = sock_flag(sk, SOCK_LOCALROUTE);
1122 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001123
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001124 case SO_BROADCAST:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001125 v.val = sock_flag(sk, SOCK_BROADCAST);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001126 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001128 case SO_SNDBUF:
1129 v.val = sk->sk_sndbuf;
1130 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001131
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001132 case SO_RCVBUF:
1133 v.val = sk->sk_rcvbuf;
1134 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001136 case SO_REUSEADDR:
1137 v.val = sk->sk_reuse;
1138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Tom Herbert055dc212013-01-22 09:49:50 +00001140 case SO_REUSEPORT:
1141 v.val = sk->sk_reuseport;
1142 break;
1143
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001144 case SO_KEEPALIVE:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001145 v.val = sock_flag(sk, SOCK_KEEPOPEN);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001148 case SO_TYPE:
1149 v.val = sk->sk_type;
1150 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Jan Engelhardt49c794e2009-08-04 07:28:28 +00001152 case SO_PROTOCOL:
1153 v.val = sk->sk_protocol;
1154 break;
1155
Jan Engelhardt0d6038e2009-08-04 07:28:29 +00001156 case SO_DOMAIN:
1157 v.val = sk->sk_family;
1158 break;
1159
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001160 case SO_ERROR:
1161 v.val = -sock_error(sk);
Eric Dumazet2a915252009-05-27 11:30:05 +00001162 if (v.val == 0)
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001163 v.val = xchg(&sk->sk_err_soft, 0);
1164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001166 case SO_OOBINLINE:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001167 v.val = sock_flag(sk, SOCK_URGINLINE);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001168 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001169
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001170 case SO_NO_CHECK:
Tom Herbert28448b82014-05-23 08:47:19 -07001171 v.val = sk->sk_no_check_tx;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001174 case SO_PRIORITY:
1175 v.val = sk->sk_priority;
1176 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001177
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001178 case SO_LINGER:
1179 lv = sizeof(v.ling);
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001180 v.ling.l_onoff = sock_flag(sk, SOCK_LINGER);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001181 v.ling.l_linger = sk->sk_lingertime / HZ;
1182 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001183
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001184 case SO_BSDCOMPAT:
1185 sock_warn_obsolete_bsdism("getsockopt");
1186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001188 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -07001189 v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
1190 !sock_flag(sk, SOCK_RCVTSTAMPNS);
1191 break;
1192
1193 case SO_TIMESTAMPNS:
1194 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Patrick Ohly20d49472009-02-12 05:03:38 +00001197 case SO_TIMESTAMPING:
Willem de Bruijnb9f40e22014-08-04 22:11:46 -04001198 v.val = sk->sk_tsflags;
Patrick Ohly20d49472009-02-12 05:03:38 +00001199 break;
1200
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001201 case SO_RCVTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +00001202 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001203 if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
1204 v.tm.tv_sec = 0;
1205 v.tm.tv_usec = 0;
1206 } else {
1207 v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
Gao Feng8ccde4c2017-02-21 17:09:19 +08001208 v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * USEC_PER_SEC) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001212 case SO_SNDTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +00001213 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001214 if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
1215 v.tm.tv_sec = 0;
1216 v.tm.tv_usec = 0;
1217 } else {
1218 v.tm.tv_sec = sk->sk_sndtimeo / HZ;
Gao Feng8ccde4c2017-02-21 17:09:19 +08001219 v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * USEC_PER_SEC) / HZ;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001220 }
1221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001223 case SO_RCVLOWAT:
1224 v.val = sk->sk_rcvlowat;
1225 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07001226
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001227 case SO_SNDLOWAT:
Eric Dumazet2a915252009-05-27 11:30:05 +00001228 v.val = 1;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001231 case SO_PASSCRED:
Eric Dumazet82981932012-04-26 20:07:59 +00001232 v.val = !!test_bit(SOCK_PASSCRED, &sock->flags);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001233 break;
1234
1235 case SO_PEERCRED:
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001236 {
1237 struct ucred peercred;
1238 if (len > sizeof(peercred))
1239 len = sizeof(peercred);
1240 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
1241 if (copy_to_user(optval, &peercred, len))
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001242 return -EFAULT;
1243 goto lenout;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001244 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001245
David Herrmann28b5ba2a2017-06-21 10:47:15 +02001246 case SO_PEERGROUPS:
1247 {
1248 int ret, n;
1249
1250 if (!sk->sk_peer_cred)
1251 return -ENODATA;
1252
1253 n = sk->sk_peer_cred->group_info->ngroups;
1254 if (len < n * sizeof(gid_t)) {
1255 len = n * sizeof(gid_t);
1256 return put_user(len, optlen) ? -EFAULT : -ERANGE;
1257 }
1258 len = n * sizeof(gid_t);
1259
1260 ret = groups_to_user((gid_t __user *)optval,
1261 sk->sk_peer_cred->group_info);
1262 if (ret)
1263 return ret;
1264 goto lenout;
1265 }
1266
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001267 case SO_PEERNAME:
1268 {
1269 char address[128];
1270
1271 if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
1272 return -ENOTCONN;
1273 if (lv < len)
1274 return -EINVAL;
1275 if (copy_to_user(optval, address, len))
1276 return -EFAULT;
1277 goto lenout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001279
1280 /* Dubious BSD thing... Probably nobody even uses it, but
1281 * the UNIX standard wants it for whatever reason... -DaveM
1282 */
1283 case SO_ACCEPTCONN:
1284 v.val = sk->sk_state == TCP_LISTEN;
1285 break;
1286
1287 case SO_PASSSEC:
Eric Dumazet82981932012-04-26 20:07:59 +00001288 v.val = !!test_bit(SOCK_PASSSEC, &sock->flags);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001289 break;
1290
1291 case SO_PEERSEC:
1292 return security_socket_getpeersec_stream(sock, optval, optlen, len);
1293
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001294 case SO_MARK:
1295 v.val = sk->sk_mark;
1296 break;
1297
Neil Horman3b885782009-10-12 13:26:31 -07001298 case SO_RXQ_OVFL:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001299 v.val = sock_flag(sk, SOCK_RXQ_OVFL);
Neil Horman3b885782009-10-12 13:26:31 -07001300 break;
1301
Johannes Berg6e3e9392011-11-09 10:15:42 +01001302 case SO_WIFI_STATUS:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001303 v.val = sock_flag(sk, SOCK_WIFI_STATUS);
Johannes Berg6e3e9392011-11-09 10:15:42 +01001304 break;
1305
Pavel Emelyanovef64a542012-02-21 07:31:34 +00001306 case SO_PEEK_OFF:
1307 if (!sock->ops->set_peek_off)
1308 return -EOPNOTSUPP;
1309
1310 v.val = sk->sk_peek_off;
1311 break;
David S. Millerbc2f7992012-02-24 14:48:34 -05001312 case SO_NOFCS:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001313 v.val = sock_flag(sk, SOCK_NOFCS);
David S. Millerbc2f7992012-02-24 14:48:34 -05001314 break;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001315
Pavel Emelyanovf7b86bf2012-10-18 23:55:56 +00001316 case SO_BINDTODEVICE:
Brian Haleyc91f6df2012-11-26 05:21:08 +00001317 return sock_getbindtodevice(sk, optval, optlen, len);
1318
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001319 case SO_GET_FILTER:
1320 len = sk_get_filter(sk, (struct sock_filter __user *)optval, len);
1321 if (len < 0)
1322 return len;
1323
1324 goto lenout;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001325
Vincent Bernatd59577b2013-01-16 22:55:49 +01001326 case SO_LOCK_FILTER:
1327 v.val = sock_flag(sk, SOCK_FILTER_LOCKED);
1328 break;
1329
Michal Sekletarea02f942014-01-17 17:09:45 +01001330 case SO_BPF_EXTENSIONS:
1331 v.val = bpf_tell_extensions();
1332 break;
1333
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00001334 case SO_SELECT_ERR_QUEUE:
1335 v.val = sock_flag(sk, SOCK_SELECT_ERR_QUEUE);
1336 break;
1337
Cong Wange0d10952013-08-01 11:10:25 +08001338#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +03001339 case SO_BUSY_POLL:
Eliezer Tamirdafcc432013-06-14 16:33:57 +03001340 v.val = sk->sk_ll_usec;
1341 break;
1342#endif
1343
Eric Dumazet62748f32013-09-24 08:20:52 -07001344 case SO_MAX_PACING_RATE:
1345 v.val = sk->sk_max_pacing_rate;
1346 break;
1347
Eric Dumazet2c8c56e2014-11-11 05:54:28 -08001348 case SO_INCOMING_CPU:
1349 v.val = sk->sk_incoming_cpu;
1350 break;
1351
Josh Hunta2d133b2017-03-20 15:22:03 -04001352 case SO_MEMINFO:
1353 {
1354 u32 meminfo[SK_MEMINFO_VARS];
1355
1356 if (get_user(len, optlen))
1357 return -EFAULT;
1358
1359 sk_get_meminfo(sk, meminfo);
1360
1361 len = min_t(unsigned int, len, sizeof(meminfo));
1362 if (copy_to_user(optval, &meminfo, len))
1363 return -EFAULT;
1364
1365 goto lenout;
1366 }
Sridhar Samudrala6d433902017-03-24 10:08:36 -07001367
1368#ifdef CONFIG_NET_RX_BUSY_POLL
1369 case SO_INCOMING_NAPI_ID:
1370 v.val = READ_ONCE(sk->sk_napi_id);
1371
1372 /* aggregate non-NAPI IDs down to 0 */
1373 if (v.val < MIN_NAPI_ID)
1374 v.val = 0;
1375
1376 break;
1377#endif
1378
Chenbo Feng5daab9d2017-04-05 19:00:55 -07001379 case SO_COOKIE:
1380 lv = sizeof(u64);
1381 if (len < lv)
1382 return -EINVAL;
1383 v.val64 = sock_gen_cookie(sk);
1384 break;
1385
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001386 default:
YOSHIFUJI Hideaki/吉藤英明443b5992015-03-23 18:04:13 +09001387 /* We implement the SO_SNDLOWAT etc to not be settable
1388 * (1003.1g 7).
1389 */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001390 return -ENOPROTOOPT;
1391 }
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (len > lv)
1394 len = lv;
1395 if (copy_to_user(optval, &v, len))
1396 return -EFAULT;
1397lenout:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001398 if (put_user(len, optlen))
1399 return -EFAULT;
1400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001403/*
1404 * Initialize an sk_lock.
1405 *
1406 * (We also register the sk_lock with the lock validator.)
1407 */
Dave Jonesb6f99a22007-03-22 12:27:49 -07001408static inline void sock_lock_init(struct sock *sk)
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001409{
David Howellscdfbabf2017-03-09 08:09:05 +00001410 if (sk->sk_kern_sock)
1411 sock_lock_init_class_and_name(
1412 sk,
1413 af_family_kern_slock_key_strings[sk->sk_family],
1414 af_family_kern_slock_keys + sk->sk_family,
1415 af_family_kern_key_strings[sk->sk_family],
1416 af_family_kern_keys + sk->sk_family);
1417 else
1418 sock_lock_init_class_and_name(
1419 sk,
Peter Zijlstraed075362006-12-06 20:35:24 -08001420 af_family_slock_key_strings[sk->sk_family],
1421 af_family_slock_keys + sk->sk_family,
1422 af_family_key_strings[sk->sk_family],
1423 af_family_keys + sk->sk_family);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001424}
1425
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001426/*
1427 * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
1428 * even temporarly, because of RCU lookups. sk_node should also be left as is.
Eric Dumazet68835ab2010-11-30 19:04:07 +00001429 * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001430 */
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001431static void sock_copy(struct sock *nsk, const struct sock *osk)
1432{
1433#ifdef CONFIG_SECURITY_NETWORK
1434 void *sptr = nsk->sk_security;
1435#endif
Eric Dumazet68835ab2010-11-30 19:04:07 +00001436 memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
1437
1438 memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
1439 osk->sk_prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
1440
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001441#ifdef CONFIG_SECURITY_NETWORK
1442 nsk->sk_security = sptr;
1443 security_sk_clone(osk, nsk);
1444#endif
1445}
1446
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001447static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
1448 int family)
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001449{
1450 struct sock *sk;
1451 struct kmem_cache *slab;
1452
1453 slab = prot->slab;
Eric Dumazete912b112009-07-08 19:36:05 +00001454 if (slab != NULL) {
1455 sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
1456 if (!sk)
1457 return sk;
Eric Dumazetba2489b2016-08-23 11:39:29 -07001458 if (priority & __GFP_ZERO)
1459 sk_prot_clear_nulls(sk, prot->obj_size);
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001460 } else
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001461 sk = kmalloc(prot->obj_size, priority);
1462
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001463 if (sk != NULL) {
Vegard Nossuma98b65a2009-02-26 14:46:57 +01001464 kmemcheck_annotate_bitfield(sk, flags);
1465
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001466 if (security_sk_alloc(sk, family, priority))
1467 goto out_free;
1468
1469 if (!try_module_get(prot->owner))
1470 goto out_free_sec;
Krishna Kumare022f0b2009-10-19 23:46:20 +00001471 sk_tx_queue_clear(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001472 }
1473
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001474 return sk;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001475
1476out_free_sec:
1477 security_sk_free(sk);
1478out_free:
1479 if (slab != NULL)
1480 kmem_cache_free(slab, sk);
1481 else
1482 kfree(sk);
1483 return NULL;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001484}
1485
1486static void sk_prot_free(struct proto *prot, struct sock *sk)
1487{
1488 struct kmem_cache *slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001489 struct module *owner;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001490
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001491 owner = prot->owner;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001492 slab = prot->slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001493
Tejun Heobd1060a2015-12-07 17:38:53 -05001494 cgroup_sk_free(&sk->sk_cgrp_data);
Johannes Weiner2d758072016-10-07 17:00:58 -07001495 mem_cgroup_sk_free(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001496 security_sk_free(sk);
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001497 if (slab != NULL)
1498 kmem_cache_free(slab, sk);
1499 else
1500 kfree(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001501 module_put(owner);
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/**
1505 * sk_alloc - All socket objects are allocated here
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001506 * @net: the applicable net namespace
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001507 * @family: protocol family
1508 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1509 * @prot: struct proto associated with this new sock instance
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001510 * @kern: is this to be a kernel socket?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001512struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001513 struct proto *prot, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001515 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001517 sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (sk) {
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001519 sk->sk_family = family;
1520 /*
1521 * See comment in struct sock definition to understand
1522 * why we need sk_prot_creator -acme
1523 */
1524 sk->sk_prot = sk->sk_prot_creator = prot;
David Howellscdfbabf2017-03-09 08:09:05 +00001525 sk->sk_kern_sock = kern;
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001526 sock_lock_init(sk);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001527 sk->sk_net_refcnt = kern ? 0 : 1;
1528 if (likely(sk->sk_net_refcnt))
1529 get_net(net);
1530 sock_net_set(sk, net);
Reshetova, Elena14afee42017-06-30 13:08:00 +03001531 refcount_set(&sk->sk_wmem_alloc, 1);
Herbert Xuf8451722010-05-24 00:12:34 -07001532
Johannes Weiner2d758072016-10-07 17:00:58 -07001533 mem_cgroup_sk_alloc(sk);
Johannes Weinerd979a392016-09-19 14:44:38 -07001534 cgroup_sk_alloc(&sk->sk_cgrp_data);
Tejun Heo2a56a1f2015-12-07 17:38:52 -05001535 sock_update_classid(&sk->sk_cgrp_data);
1536 sock_update_netprioidx(&sk->sk_cgrp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Frank Filza79af592005-09-27 15:23:38 -07001538
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001539 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
Eric Dumazet2a915252009-05-27 11:30:05 +00001541EXPORT_SYMBOL(sk_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Eric Dumazeta4298e42016-04-01 08:52:12 -07001543/* Sockets having SOCK_RCU_FREE will call this function after one RCU
1544 * grace period. This is the case for UDP sockets and TCP listeners.
1545 */
1546static void __sk_destruct(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Eric Dumazeta4298e42016-04-01 08:52:12 -07001548 struct sock *sk = container_of(head, struct sock, sk_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 struct sk_filter *filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 if (sk->sk_destruct)
1552 sk->sk_destruct(sk);
1553
Paul E. McKenneya898def2010-02-22 17:04:49 -08001554 filter = rcu_dereference_check(sk->sk_filter,
Reshetova, Elena14afee42017-06-30 13:08:00 +03001555 refcount_read(&sk->sk_wmem_alloc) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (filter) {
Pavel Emelyanov309dd5f2007-10-17 21:21:51 -07001557 sk_filter_uncharge(sk, filter);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001558 RCU_INIT_POINTER(sk->sk_filter, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Craig Gallek538950a2016-01-04 17:41:47 -05001560 if (rcu_access_pointer(sk->sk_reuseport_cb))
1561 reuseport_detach_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Eric Dumazet08e29af2011-11-28 12:04:18 +00001563 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 if (atomic_read(&sk->sk_omem_alloc))
Joe Perchese005d192012-05-16 19:58:40 +00001566 pr_debug("%s: optmem leakage (%d bytes) detected\n",
1567 __func__, atomic_read(&sk->sk_omem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Eric Dumazet22a0e182017-03-15 13:21:28 -07001569 if (sk->sk_frag.page) {
1570 put_page(sk->sk_frag.page);
1571 sk->sk_frag.page = NULL;
1572 }
1573
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001574 if (sk->sk_peer_cred)
1575 put_cred(sk->sk_peer_cred);
1576 put_pid(sk->sk_peer_pid);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001577 if (likely(sk->sk_net_refcnt))
1578 put_net(sock_net(sk));
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001579 sk_prot_free(sk->sk_prot_creator, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
Eric Dumazet2b85a342009-06-11 02:55:43 -07001581
Eric Dumazeta4298e42016-04-01 08:52:12 -07001582void sk_destruct(struct sock *sk)
1583{
1584 if (sock_flag(sk, SOCK_RCU_FREE))
1585 call_rcu(&sk->sk_rcu, __sk_destruct);
1586 else
1587 __sk_destruct(&sk->sk_rcu);
1588}
1589
Craig Gallekeb4cb002015-06-15 11:26:18 -04001590static void __sk_free(struct sock *sk)
1591{
Craig Gallekb9226222015-06-30 12:49:32 -04001592 if (unlikely(sock_diag_has_destroy_listeners(sk) && sk->sk_net_refcnt))
Craig Gallekeb4cb002015-06-15 11:26:18 -04001593 sock_diag_broadcast_destroy(sk);
1594 else
1595 sk_destruct(sk);
1596}
1597
Eric Dumazet2b85a342009-06-11 02:55:43 -07001598void sk_free(struct sock *sk)
1599{
1600 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001601 * We subtract one from sk_wmem_alloc and can know if
Eric Dumazet2b85a342009-06-11 02:55:43 -07001602 * some packets are still in some tx queue.
1603 * If not null, sock_wfree() will call __sk_free(sk) later
1604 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001605 if (refcount_dec_and_test(&sk->sk_wmem_alloc))
Eric Dumazet2b85a342009-06-11 02:55:43 -07001606 __sk_free(sk);
1607}
Eric Dumazet2a915252009-05-27 11:30:05 +00001608EXPORT_SYMBOL(sk_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Paolo Abeni581319c2017-03-09 13:54:08 +01001610static void sk_init_common(struct sock *sk)
1611{
1612 skb_queue_head_init(&sk->sk_receive_queue);
1613 skb_queue_head_init(&sk->sk_write_queue);
1614 skb_queue_head_init(&sk->sk_error_queue);
1615
1616 rwlock_init(&sk->sk_callback_lock);
1617 lockdep_set_class_and_name(&sk->sk_receive_queue.lock,
1618 af_rlock_keys + sk->sk_family,
1619 af_family_rlock_key_strings[sk->sk_family]);
1620 lockdep_set_class_and_name(&sk->sk_write_queue.lock,
1621 af_wlock_keys + sk->sk_family,
1622 af_family_wlock_key_strings[sk->sk_family]);
1623 lockdep_set_class_and_name(&sk->sk_error_queue.lock,
1624 af_elock_keys + sk->sk_family,
1625 af_family_elock_key_strings[sk->sk_family]);
1626 lockdep_set_class_and_name(&sk->sk_callback_lock,
1627 af_callback_keys + sk->sk_family,
1628 af_family_clock_key_strings[sk->sk_family]);
1629}
1630
Eric Dumazete56c57d2011-11-08 17:07:07 -05001631/**
1632 * sk_clone_lock - clone a socket, and lock its clone
1633 * @sk: the socket to clone
1634 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1635 *
1636 * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
1637 */
1638struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001639{
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001640 struct sock *newsk;
Alexei Starovoitov278571b2014-07-30 20:34:12 -07001641 bool is_charged = true;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001642
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001643 newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001644 if (newsk != NULL) {
1645 struct sk_filter *filter;
1646
Venkat Yekkirala892c1412006-08-04 23:08:56 -07001647 sock_copy(newsk, sk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001648
1649 /* SANITY */
Sowmini Varadhan8a681732015-07-30 15:50:36 +02001650 if (likely(newsk->sk_net_refcnt))
1651 get_net(sock_net(newsk));
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001652 sk_node_init(&newsk->sk_node);
1653 sock_lock_init(newsk);
1654 bh_lock_sock(newsk);
Eric Dumazetfa438cc2007-03-04 16:05:44 -08001655 newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
Zhu Yi8eae9392010-03-04 18:01:40 +00001656 newsk->sk_backlog.len = 0;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001657
1658 atomic_set(&newsk->sk_rmem_alloc, 0);
Eric Dumazet2b85a342009-06-11 02:55:43 -07001659 /*
1660 * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
1661 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001662 refcount_set(&newsk->sk_wmem_alloc, 1);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001663 atomic_set(&newsk->sk_omem_alloc, 0);
Paolo Abeni581319c2017-03-09 13:54:08 +01001664 sk_init_common(newsk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001665
1666 newsk->sk_dst_cache = NULL;
Julian Anastasov9b8805a2017-02-06 23:14:11 +02001667 newsk->sk_dst_pending_confirm = 0;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001668 newsk->sk_wmem_queued = 0;
1669 newsk->sk_forward_alloc = 0;
Eric Dumazet9caad862016-04-01 08:52:20 -07001670 atomic_set(&newsk->sk_drops, 0);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001671 newsk->sk_send_head = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001672 newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
Willem de Bruijn52267792017-08-03 16:29:39 -04001673 atomic_set(&newsk->sk_zckey, 0);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001674
1675 sock_reset_flag(newsk, SOCK_DONE);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001676
Eric Dumazet0d7da9d2010-10-25 03:47:05 +00001677 filter = rcu_dereference_protected(newsk->sk_filter, 1);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001678 if (filter != NULL)
Alexei Starovoitov278571b2014-07-30 20:34:12 -07001679 /* though it's an empty new sock, the charging may fail
1680 * if sysctl_optmem_max was changed between creation of
1681 * original socket and cloning
1682 */
1683 is_charged = sk_filter_charge(newsk, filter);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001684
Eric Dumazetd188ba82015-12-08 07:22:02 -08001685 if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
Daniel Borkmanna97e50c2017-03-22 13:08:08 +01001686 /* We need to make sure that we don't uncharge the new
1687 * socket if we couldn't charge it in the first place
1688 * as otherwise we uncharge the parent's filter.
1689 */
1690 if (!is_charged)
1691 RCU_INIT_POINTER(newsk->sk_filter, NULL);
Arnaldo Carvalho de Melo94352d42017-03-01 16:35:08 -03001692 sk_free_unlock_clone(newsk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001693 newsk = NULL;
1694 goto out;
1695 }
Craig Gallekfa463492016-02-10 11:50:39 -05001696 RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001697
1698 newsk->sk_err = 0;
Eric Dumazete551c322016-10-28 13:40:24 -07001699 newsk->sk_err_soft = 0;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001700 newsk->sk_priority = 0;
Eric Dumazet2c8c56e2014-11-11 05:54:28 -08001701 newsk->sk_incoming_cpu = raw_smp_processor_id();
Eric Dumazet33cf7c92015-03-11 18:53:14 -07001702 atomic64_set(&newsk->sk_cookie, 0);
Johannes Weinerd979a392016-09-19 14:44:38 -07001703
Johannes Weiner2d758072016-10-07 17:00:58 -07001704 mem_cgroup_sk_alloc(newsk);
Johannes Weinerd979a392016-09-19 14:44:38 -07001705 cgroup_sk_alloc(&newsk->sk_cgrp_data);
1706
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001707 /*
1708 * Before updating sk_refcnt, we must commit prior changes to memory
1709 * (Documentation/RCU/rculist_nulls.txt for details)
1710 */
1711 smp_wmb();
Reshetova, Elena41c6d652017-06-30 13:08:01 +03001712 refcount_set(&newsk->sk_refcnt, 2);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001713
1714 /*
1715 * Increment the counter in the same struct proto as the master
1716 * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
1717 * is the same as sk->sk_prot->socks, as this field was copied
1718 * with memcpy).
1719 *
1720 * This _changes_ the previous behaviour, where
1721 * tcp_create_openreq_child always was incrementing the
1722 * equivalent to tcp_prot->socks (inet_sock_nr), so this have
1723 * to be taken into account in all callers. -acme
1724 */
1725 sk_refcnt_debug_inc(newsk);
David S. Miller972692e2008-06-17 22:41:38 -07001726 sk_set_socket(newsk, NULL);
Eric Dumazet43815482010-04-29 11:01:49 +00001727 newsk->sk_wq = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001728
1729 if (newsk->sk_prot->sockets_allocated)
Glauber Costa180d8cd2011-12-11 21:47:02 +00001730 sk_sockets_allocated_inc(newsk);
Octavian Purdila704da5602010-01-08 00:00:09 -08001731
Hannes Frederic Sowa080a2702015-10-26 13:51:37 +01001732 if (sock_needs_netstamp(sk) &&
1733 newsk->sk_flags & SK_FLAGS_TIMESTAMP)
Octavian Purdila704da5602010-01-08 00:00:09 -08001734 net_enable_timestamp();
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001735 }
1736out:
1737 return newsk;
1738}
Eric Dumazete56c57d2011-11-08 17:07:07 -05001739EXPORT_SYMBOL_GPL(sk_clone_lock);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001740
Arnaldo Carvalho de Melo94352d42017-03-01 16:35:08 -03001741void sk_free_unlock_clone(struct sock *sk)
1742{
1743 /* It is still raw copy of parent, so invalidate
1744 * destructor and make plain sk_free() */
1745 sk->sk_destruct = NULL;
1746 bh_unlock_sock(sk);
1747 sk_free(sk);
1748}
1749EXPORT_SYMBOL_GPL(sk_free_unlock_clone);
1750
Andi Kleen99580892007-04-20 17:12:43 -07001751void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
1752{
Eric Dumazetd6a4e262015-05-26 08:55:28 -07001753 u32 max_segs = 1;
1754
Eric Dumazet6bd4f352015-12-02 21:53:57 -08001755 sk_dst_set(sk, dst);
Andi Kleen99580892007-04-20 17:12:43 -07001756 sk->sk_route_caps = dst->dev->features;
1757 if (sk->sk_route_caps & NETIF_F_GSO)
Herbert Xu4fcd6b92007-05-31 22:15:50 -07001758 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
Eric Dumazeta4654192010-05-16 00:36:33 -07001759 sk->sk_route_caps &= ~sk->sk_route_nocaps;
Andi Kleen99580892007-04-20 17:12:43 -07001760 if (sk_can_gso(sk)) {
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001761 if (dst->header_len) {
Andi Kleen99580892007-04-20 17:12:43 -07001762 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001763 } else {
Andi Kleen99580892007-04-20 17:12:43 -07001764 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001765 sk->sk_gso_max_size = dst->dev->gso_max_size;
Eric Dumazetd6a4e262015-05-26 08:55:28 -07001766 max_segs = max_t(u32, dst->dev->gso_max_segs, 1);
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001767 }
Andi Kleen99580892007-04-20 17:12:43 -07001768 }
Eric Dumazetd6a4e262015-05-26 08:55:28 -07001769 sk->sk_gso_max_segs = max_segs;
Andi Kleen99580892007-04-20 17:12:43 -07001770}
1771EXPORT_SYMBOL_GPL(sk_setup_caps);
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773/*
1774 * Simple resource managers for sockets.
1775 */
1776
1777
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001778/*
1779 * Write buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 */
1781void sock_wfree(struct sk_buff *skb)
1782{
1783 struct sock *sk = skb->sk;
Eric Dumazetd99927f2009-09-24 10:49:24 +00001784 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Eric Dumazetd99927f2009-09-24 10:49:24 +00001786 if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
1787 /*
1788 * Keep a reference on sk_wmem_alloc, this will be released
1789 * after sk_write_space() call
1790 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001791 WARN_ON(refcount_sub_and_test(len - 1, &sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 sk->sk_write_space(sk);
Eric Dumazetd99927f2009-09-24 10:49:24 +00001793 len = 1;
1794 }
Eric Dumazet2b85a342009-06-11 02:55:43 -07001795 /*
Eric Dumazetd99927f2009-09-24 10:49:24 +00001796 * if sk_wmem_alloc reaches 0, we must finish what sk_free()
1797 * could not do because of in-flight packets
Eric Dumazet2b85a342009-06-11 02:55:43 -07001798 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001799 if (refcount_sub_and_test(len, &sk->sk_wmem_alloc))
Eric Dumazet2b85a342009-06-11 02:55:43 -07001800 __sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
Eric Dumazet2a915252009-05-27 11:30:05 +00001802EXPORT_SYMBOL(sock_wfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001804/* This variant of sock_wfree() is used by TCP,
1805 * since it sets SOCK_USE_WRITE_QUEUE.
1806 */
1807void __sock_wfree(struct sk_buff *skb)
1808{
1809 struct sock *sk = skb->sk;
1810
Reshetova, Elena14afee42017-06-30 13:08:00 +03001811 if (refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc))
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001812 __sk_free(sk);
1813}
1814
Eric Dumazet9e17f8a2015-11-01 15:36:55 -08001815void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1816{
1817 skb_orphan(skb);
1818 skb->sk = sk;
1819#ifdef CONFIG_INET
1820 if (unlikely(!sk_fullsock(sk))) {
1821 skb->destructor = sock_edemux;
1822 sock_hold(sk);
1823 return;
1824 }
1825#endif
1826 skb->destructor = sock_wfree;
1827 skb_set_hash_from_sk(skb, sk);
1828 /*
1829 * We used to take a refcount on sk, but following operation
1830 * is enough to guarantee sk_free() wont free this sock until
1831 * all in-flight packets are completed
1832 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001833 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
Eric Dumazet9e17f8a2015-11-01 15:36:55 -08001834}
1835EXPORT_SYMBOL(skb_set_owner_w);
1836
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001837/* This helper is used by netem, as it can hold packets in its
1838 * delay queue. We want to allow the owner socket to send more
1839 * packets, as if they were already TX completed by a typical driver.
1840 * But we also want to keep skb->sk set because some packet schedulers
Eric Dumazetf6ba8d32017-05-11 15:24:41 -07001841 * rely on it (sch_fq for example).
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001842 */
Eric Dumazetf2f872f2013-07-30 17:55:08 -07001843void skb_orphan_partial(struct sk_buff *skb)
1844{
Eric Dumazetf6ba8d32017-05-11 15:24:41 -07001845 if (skb_is_tcp_pure_ack(skb))
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001846 return;
1847
Eric Dumazetf2f872f2013-07-30 17:55:08 -07001848 if (skb->destructor == sock_wfree
1849#ifdef CONFIG_INET
1850 || skb->destructor == tcp_wfree
1851#endif
1852 ) {
Eric Dumazetf6ba8d32017-05-11 15:24:41 -07001853 struct sock *sk = skb->sk;
1854
Reshetova, Elena41c6d652017-06-30 13:08:01 +03001855 if (refcount_inc_not_zero(&sk->sk_refcnt)) {
Reshetova, Elena14afee42017-06-30 13:08:00 +03001856 WARN_ON(refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc));
Eric Dumazetf6ba8d32017-05-11 15:24:41 -07001857 skb->destructor = sock_efree;
1858 }
Eric Dumazetf2f872f2013-07-30 17:55:08 -07001859 } else {
1860 skb_orphan(skb);
1861 }
1862}
1863EXPORT_SYMBOL(skb_orphan_partial);
1864
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001865/*
1866 * Read buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
1868void sock_rfree(struct sk_buff *skb)
1869{
1870 struct sock *sk = skb->sk;
Eric Dumazetd361fd52010-07-10 22:45:17 +00001871 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Eric Dumazetd361fd52010-07-10 22:45:17 +00001873 atomic_sub(len, &sk->sk_rmem_alloc);
1874 sk_mem_uncharge(sk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
Eric Dumazet2a915252009-05-27 11:30:05 +00001876EXPORT_SYMBOL(sock_rfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Oliver Hartkopp7768eed2015-03-10 19:03:46 +01001878/*
1879 * Buffer destructor for skbs that are not used directly in read or write
1880 * path, e.g. for error handler skbs. Automatically called from kfree_skb.
1881 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04001882void sock_efree(struct sk_buff *skb)
1883{
1884 sock_put(skb->sk);
1885}
1886EXPORT_SYMBOL(sock_efree);
1887
Eric W. Biederman976d02012012-05-23 17:16:53 -06001888kuid_t sock_i_uid(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Eric W. Biederman976d02012012-05-23 17:16:53 -06001890 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Eric Dumazetf064af12010-09-22 12:43:39 +00001892 read_lock_bh(&sk->sk_callback_lock);
Eric W. Biederman976d02012012-05-23 17:16:53 -06001893 uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID;
Eric Dumazetf064af12010-09-22 12:43:39 +00001894 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return uid;
1896}
Eric Dumazet2a915252009-05-27 11:30:05 +00001897EXPORT_SYMBOL(sock_i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899unsigned long sock_i_ino(struct sock *sk)
1900{
1901 unsigned long ino;
1902
Eric Dumazetf064af12010-09-22 12:43:39 +00001903 read_lock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
Eric Dumazetf064af12010-09-22 12:43:39 +00001905 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return ino;
1907}
Eric Dumazet2a915252009-05-27 11:30:05 +00001908EXPORT_SYMBOL(sock_i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910/*
1911 * Allocate a skb from the socket's send buffer.
1912 */
Victor Fusco86a76ca2005-07-08 14:57:47 -07001913struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
Al Virodd0fc662005-10-07 07:46:04 +01001914 gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Reshetova, Elena14afee42017-06-30 13:08:00 +03001916 if (force || refcount_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
Eric Dumazet2a915252009-05-27 11:30:05 +00001917 struct sk_buff *skb = alloc_skb(size, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (skb) {
1919 skb_set_owner_w(skb, sk);
1920 return skb;
1921 }
1922 }
1923 return NULL;
1924}
Eric Dumazet2a915252009-05-27 11:30:05 +00001925EXPORT_SYMBOL(sock_wmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Willem de Bruijn98ba0bd2017-08-03 16:29:37 -04001927static void sock_ofree(struct sk_buff *skb)
1928{
1929 struct sock *sk = skb->sk;
1930
1931 atomic_sub(skb->truesize, &sk->sk_omem_alloc);
1932}
1933
1934struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
1935 gfp_t priority)
1936{
1937 struct sk_buff *skb;
1938
1939 /* small safe race: SKB_TRUESIZE may differ from final skb->truesize */
1940 if (atomic_read(&sk->sk_omem_alloc) + SKB_TRUESIZE(size) >
1941 sysctl_optmem_max)
1942 return NULL;
1943
1944 skb = alloc_skb(size, priority);
1945 if (!skb)
1946 return NULL;
1947
1948 atomic_add(skb->truesize, &sk->sk_omem_alloc);
1949 skb->sk = sk;
1950 skb->destructor = sock_ofree;
1951 return skb;
1952}
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 * Allocate a memory block from the socket's option memory buffer.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001956 */
Al Virodd0fc662005-10-07 07:46:04 +01001957void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Eric Dumazet95c96172012-04-15 05:58:06 +00001959 if ((unsigned int)size <= sysctl_optmem_max &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
1961 void *mem;
1962 /* First do the add, to avoid the race if kmalloc
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001963 * might sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
1965 atomic_add(size, &sk->sk_omem_alloc);
1966 mem = kmalloc(size, priority);
1967 if (mem)
1968 return mem;
1969 atomic_sub(size, &sk->sk_omem_alloc);
1970 }
1971 return NULL;
1972}
Eric Dumazet2a915252009-05-27 11:30:05 +00001973EXPORT_SYMBOL(sock_kmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Daniel Borkmann79e88652014-11-19 17:13:11 +01001975/* Free an option memory block. Note, we actually want the inline
1976 * here as this allows gcc to detect the nullify and fold away the
1977 * condition entirely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
Daniel Borkmann79e88652014-11-19 17:13:11 +01001979static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
1980 const bool nullify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
David S. Millere53da5f2014-10-14 17:02:37 -04001982 if (WARN_ON_ONCE(!mem))
1983 return;
Daniel Borkmann79e88652014-11-19 17:13:11 +01001984 if (nullify)
1985 kzfree(mem);
1986 else
1987 kfree(mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 atomic_sub(size, &sk->sk_omem_alloc);
1989}
Daniel Borkmann79e88652014-11-19 17:13:11 +01001990
1991void sock_kfree_s(struct sock *sk, void *mem, int size)
1992{
1993 __sock_kfree_s(sk, mem, size, false);
1994}
Eric Dumazet2a915252009-05-27 11:30:05 +00001995EXPORT_SYMBOL(sock_kfree_s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Daniel Borkmann79e88652014-11-19 17:13:11 +01001997void sock_kzfree_s(struct sock *sk, void *mem, int size)
1998{
1999 __sock_kfree_s(sk, mem, size, true);
2000}
2001EXPORT_SYMBOL(sock_kzfree_s);
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003/* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
2004 I think, these locks should be removed for datagram sockets.
2005 */
Eric Dumazet2a915252009-05-27 11:30:05 +00002006static long sock_wait_for_wmem(struct sock *sk, long timeo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 DEFINE_WAIT(wait);
2009
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002010 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 for (;;) {
2012 if (!timeo)
2013 break;
2014 if (signal_pending(current))
2015 break;
2016 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Eric Dumazetaa395142010-04-20 13:03:51 +00002017 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Reshetova, Elena14afee42017-06-30 13:08:00 +03002018 if (refcount_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 break;
2020 if (sk->sk_shutdown & SEND_SHUTDOWN)
2021 break;
2022 if (sk->sk_err)
2023 break;
2024 timeo = schedule_timeout(timeo);
2025 }
Eric Dumazetaa395142010-04-20 13:03:51 +00002026 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 return timeo;
2028}
2029
2030
2031/*
2032 * Generic send/receive buffer handlers
2033 */
2034
Herbert Xu4cc7f682009-02-04 16:55:54 -08002035struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
2036 unsigned long data_len, int noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002037 int *errcode, int max_page_order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002039 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 long timeo;
2041 int err;
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 timeo = sock_sndtimeo(sk, noblock);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002044 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 err = sock_error(sk);
2046 if (err != 0)
2047 goto failure;
2048
2049 err = -EPIPE;
2050 if (sk->sk_shutdown & SEND_SHUTDOWN)
2051 goto failure;
2052
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002053 if (sk_wmem_alloc_get(sk) < sk->sk_sndbuf)
2054 break;
Eric Dumazet28d64272013-08-08 14:38:47 -07002055
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002056 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002057 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2058 err = -EAGAIN;
2059 if (!timeo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 goto failure;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002061 if (signal_pending(current))
2062 goto interrupted;
2063 timeo = sock_wait_for_wmem(sk, timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002065 skb = alloc_skb_with_frags(header_len, data_len, max_page_order,
2066 errcode, sk->sk_allocation);
2067 if (skb)
2068 skb_set_owner_w(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return skb;
2070
2071interrupted:
2072 err = sock_intr_errno(timeo);
2073failure:
2074 *errcode = err;
2075 return NULL;
2076}
Herbert Xu4cc7f682009-02-04 16:55:54 -08002077EXPORT_SYMBOL(sock_alloc_send_pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002079struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 int noblock, int *errcode)
2081{
Eric Dumazet28d64272013-08-08 14:38:47 -07002082 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
Eric Dumazet2a915252009-05-27 11:30:05 +00002084EXPORT_SYMBOL(sock_alloc_send_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Willem de Bruijn39771b12016-04-02 23:08:06 -04002086int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,
2087 struct sockcm_cookie *sockc)
2088{
Soheil Hassas Yeganeh3dd17e62016-04-02 23:08:09 -04002089 u32 tsflags;
2090
Willem de Bruijn39771b12016-04-02 23:08:06 -04002091 switch (cmsg->cmsg_type) {
2092 case SO_MARK:
2093 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2094 return -EPERM;
2095 if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
2096 return -EINVAL;
2097 sockc->mark = *(u32 *)CMSG_DATA(cmsg);
2098 break;
Soheil Hassas Yeganeh3dd17e62016-04-02 23:08:09 -04002099 case SO_TIMESTAMPING:
2100 if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
2101 return -EINVAL;
2102
2103 tsflags = *(u32 *)CMSG_DATA(cmsg);
2104 if (tsflags & ~SOF_TIMESTAMPING_TX_RECORD_MASK)
2105 return -EINVAL;
2106
2107 sockc->tsflags &= ~SOF_TIMESTAMPING_TX_RECORD_MASK;
2108 sockc->tsflags |= tsflags;
2109 break;
Soheil Hassas Yeganeh779f1ed2016-07-11 16:51:26 -04002110 /* SCM_RIGHTS and SCM_CREDENTIALS are semantically in SOL_UNIX. */
2111 case SCM_RIGHTS:
2112 case SCM_CREDENTIALS:
2113 break;
Willem de Bruijn39771b12016-04-02 23:08:06 -04002114 default:
2115 return -EINVAL;
2116 }
2117 return 0;
2118}
2119EXPORT_SYMBOL(__sock_cmsg_send);
2120
Edward Jeef28ea362015-10-08 14:56:48 -07002121int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
2122 struct sockcm_cookie *sockc)
2123{
2124 struct cmsghdr *cmsg;
Willem de Bruijn39771b12016-04-02 23:08:06 -04002125 int ret;
Edward Jeef28ea362015-10-08 14:56:48 -07002126
2127 for_each_cmsghdr(cmsg, msg) {
2128 if (!CMSG_OK(msg, cmsg))
2129 return -EINVAL;
2130 if (cmsg->cmsg_level != SOL_SOCKET)
2131 continue;
Willem de Bruijn39771b12016-04-02 23:08:06 -04002132 ret = __sock_cmsg_send(sk, msg, cmsg, sockc);
2133 if (ret)
2134 return ret;
Edward Jeef28ea362015-10-08 14:56:48 -07002135 }
2136 return 0;
2137}
2138EXPORT_SYMBOL(sock_cmsg_send);
2139
Eric Dumazet06044752017-06-07 13:29:12 -07002140static void sk_enter_memory_pressure(struct sock *sk)
2141{
2142 if (!sk->sk_prot->enter_memory_pressure)
2143 return;
2144
2145 sk->sk_prot->enter_memory_pressure(sk);
2146}
2147
2148static void sk_leave_memory_pressure(struct sock *sk)
2149{
2150 if (sk->sk_prot->leave_memory_pressure) {
2151 sk->sk_prot->leave_memory_pressure(sk);
2152 } else {
2153 unsigned long *memory_pressure = sk->sk_prot->memory_pressure;
2154
2155 if (memory_pressure && *memory_pressure)
2156 *memory_pressure = 0;
2157 }
2158}
2159
Eric Dumazet5640f762012-09-23 23:04:42 +00002160/* On 32bit arches, an skb frag is limited to 2^15 */
2161#define SKB_FRAG_PAGE_ORDER get_order(32768)
2162
Eric Dumazet400dfd32013-10-17 16:27:07 -07002163/**
2164 * skb_page_frag_refill - check that a page_frag contains enough room
2165 * @sz: minimum size of the fragment we want to get
2166 * @pfrag: pointer to page_frag
Eric Dumazet82d5e2b2014-09-08 04:00:00 -07002167 * @gfp: priority for memory allocation
Eric Dumazet400dfd32013-10-17 16:27:07 -07002168 *
2169 * Note: While this allocator tries to use high order pages, there is
2170 * no guarantee that allocations succeed. Therefore, @sz MUST be
2171 * less or equal than PAGE_SIZE.
2172 */
Eric Dumazetd9b29382014-08-27 20:49:34 -07002173bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp)
Eric Dumazet5640f762012-09-23 23:04:42 +00002174{
Eric Dumazet5640f762012-09-23 23:04:42 +00002175 if (pfrag->page) {
Joonsoo Kimfe896d12016-03-17 14:19:26 -07002176 if (page_ref_count(pfrag->page) == 1) {
Eric Dumazet5640f762012-09-23 23:04:42 +00002177 pfrag->offset = 0;
2178 return true;
2179 }
Eric Dumazet400dfd32013-10-17 16:27:07 -07002180 if (pfrag->offset + sz <= pfrag->size)
Eric Dumazet5640f762012-09-23 23:04:42 +00002181 return true;
2182 put_page(pfrag->page);
2183 }
2184
Eric Dumazetd9b29382014-08-27 20:49:34 -07002185 pfrag->offset = 0;
2186 if (SKB_FRAG_PAGE_ORDER) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002187 /* Avoid direct reclaim but allow kswapd to wake */
2188 pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) |
2189 __GFP_COMP | __GFP_NOWARN |
2190 __GFP_NORETRY,
Eric Dumazetd9b29382014-08-27 20:49:34 -07002191 SKB_FRAG_PAGE_ORDER);
Eric Dumazet5640f762012-09-23 23:04:42 +00002192 if (likely(pfrag->page)) {
Eric Dumazetd9b29382014-08-27 20:49:34 -07002193 pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
Eric Dumazet5640f762012-09-23 23:04:42 +00002194 return true;
2195 }
Eric Dumazetd9b29382014-08-27 20:49:34 -07002196 }
2197 pfrag->page = alloc_page(gfp);
2198 if (likely(pfrag->page)) {
2199 pfrag->size = PAGE_SIZE;
2200 return true;
2201 }
Eric Dumazet400dfd32013-10-17 16:27:07 -07002202 return false;
2203}
2204EXPORT_SYMBOL(skb_page_frag_refill);
2205
2206bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
2207{
2208 if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation)))
2209 return true;
2210
Eric Dumazet5640f762012-09-23 23:04:42 +00002211 sk_enter_memory_pressure(sk);
2212 sk_stream_moderate_sndbuf(sk);
2213 return false;
2214}
2215EXPORT_SYMBOL(sk_page_frag_refill);
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217static void __lock_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00002218 __releases(&sk->sk_lock.slock)
2219 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 DEFINE_WAIT(wait);
2222
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002223 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
2225 TASK_UNINTERRUPTIBLE);
2226 spin_unlock_bh(&sk->sk_lock.slock);
2227 schedule();
2228 spin_lock_bh(&sk->sk_lock.slock);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002229 if (!sock_owned_by_user(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 break;
2231 }
2232 finish_wait(&sk->sk_lock.wq, &wait);
2233}
2234
2235static void __release_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00002236 __releases(&sk->sk_lock.slock)
2237 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002239 struct sk_buff *skb, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002241 while ((skb = sk->sk_backlog.head) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002243
2244 spin_unlock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 do {
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002247 next = skb->next;
Eric Dumazete4cbb022012-04-30 16:07:09 +00002248 prefetch(next);
Eric Dumazet7fee2262010-05-11 23:19:48 +00002249 WARN_ON_ONCE(skb_dst_is_noref(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 skb->next = NULL;
Peter Zijlstrac57943a2008-10-07 14:18:42 -07002251 sk_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002253 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 skb = next;
2256 } while (skb != NULL);
2257
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002258 spin_lock_bh(&sk->sk_lock.slock);
2259 }
Zhu Yi8eae9392010-03-04 18:01:40 +00002260
2261 /*
2262 * Doing the zeroing here guarantee we can not loop forever
2263 * while a wild producer attempts to flood us.
2264 */
2265 sk->sk_backlog.len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Eric Dumazetd41a69f2016-04-29 14:16:53 -07002268void __sk_flush_backlog(struct sock *sk)
2269{
2270 spin_lock_bh(&sk->sk_lock.slock);
2271 __release_sock(sk);
2272 spin_unlock_bh(&sk->sk_lock.slock);
2273}
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275/**
2276 * sk_wait_data - wait for data to arrive at sk_receive_queue
Pavel Pisa4dc3b162005-05-01 08:59:25 -07002277 * @sk: sock to wait on
2278 * @timeo: for how long
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +02002279 * @skb: last skb seen on sk_receive_queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 *
2281 * Now socket state including sk->sk_err is changed only under lock,
2282 * hence we may omit checks after joining wait queue.
2283 * We check receive queue before schedule() only as optimization;
2284 * it is very likely that release_sock() added new data.
2285 */
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +02002286int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
WANG Congd9dc8b02016-11-11 10:20:50 -08002288 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
WANG Congd9dc8b02016-11-11 10:20:50 -08002291 add_wait_queue(sk_sleep(sk), &wait);
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002292 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
WANG Congd9dc8b02016-11-11 10:20:50 -08002293 rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb, &wait);
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002294 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
WANG Congd9dc8b02016-11-11 10:20:50 -08002295 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return rc;
2297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298EXPORT_SYMBOL(sk_wait_data);
2299
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002300/**
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002301 * __sk_mem_raise_allocated - increase memory_allocated
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002302 * @sk: socket
2303 * @size: memory size to allocate
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002304 * @amt: pages to allocate
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002305 * @kind: allocation type
2306 *
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002307 * Similar to __sk_mem_schedule(), but does not update sk_forward_alloc
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002308 */
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002309int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002310{
2311 struct proto *prot = sk->sk_prot;
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002312 long allocated = sk_memory_allocated_add(sk, amt);
Johannes Weinere8056052016-01-14 15:21:14 -08002313
Johannes Weinerbaac50b2016-01-14 15:21:17 -08002314 if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
2315 !mem_cgroup_charge_skmem(sk->sk_memcg, amt))
Johannes Weinere8056052016-01-14 15:21:14 -08002316 goto suppress_allocation;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002317
2318 /* Under limit. */
Johannes Weinere8056052016-01-14 15:21:14 -08002319 if (allocated <= sk_prot_mem_limits(sk, 0)) {
Glauber Costa180d8cd2011-12-11 21:47:02 +00002320 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002321 return 1;
2322 }
2323
Johannes Weinere8056052016-01-14 15:21:14 -08002324 /* Under pressure. */
2325 if (allocated > sk_prot_mem_limits(sk, 1))
Glauber Costa180d8cd2011-12-11 21:47:02 +00002326 sk_enter_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002327
Johannes Weinere8056052016-01-14 15:21:14 -08002328 /* Over hard limit. */
2329 if (allocated > sk_prot_mem_limits(sk, 2))
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002330 goto suppress_allocation;
2331
2332 /* guarantee minimum buffer size under pressure */
2333 if (kind == SK_MEM_RECV) {
2334 if (atomic_read(&sk->sk_rmem_alloc) < prot->sysctl_rmem[0])
2335 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00002336
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002337 } else { /* SK_MEM_SEND */
2338 if (sk->sk_type == SOCK_STREAM) {
2339 if (sk->sk_wmem_queued < prot->sysctl_wmem[0])
2340 return 1;
Reshetova, Elena14afee42017-06-30 13:08:00 +03002341 } else if (refcount_read(&sk->sk_wmem_alloc) <
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002342 prot->sysctl_wmem[0])
2343 return 1;
2344 }
2345
Glauber Costa180d8cd2011-12-11 21:47:02 +00002346 if (sk_has_memory_pressure(sk)) {
Eric Dumazet17483762008-11-25 21:16:35 -08002347 int alloc;
2348
Glauber Costa180d8cd2011-12-11 21:47:02 +00002349 if (!sk_under_memory_pressure(sk))
Eric Dumazet17483762008-11-25 21:16:35 -08002350 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00002351 alloc = sk_sockets_allocated_read_positive(sk);
2352 if (sk_prot_mem_limits(sk, 2) > alloc *
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002353 sk_mem_pages(sk->sk_wmem_queued +
2354 atomic_read(&sk->sk_rmem_alloc) +
2355 sk->sk_forward_alloc))
2356 return 1;
2357 }
2358
2359suppress_allocation:
2360
2361 if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
2362 sk_stream_moderate_sndbuf(sk);
2363
2364 /* Fail only if socket is _under_ its sndbuf.
2365 * In this case we cannot block, so that we have to fail.
2366 */
2367 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
2368 return 1;
2369 }
2370
Satoru Moriya3847ce32011-06-17 12:00:03 +00002371 trace_sock_exceed_buf_limit(sk, prot, allocated);
2372
Glauber Costa0e90b312012-01-20 04:57:16 +00002373 sk_memory_allocated_sub(sk, amt);
Glauber Costa180d8cd2011-12-11 21:47:02 +00002374
Johannes Weinerbaac50b2016-01-14 15:21:17 -08002375 if (mem_cgroup_sockets_enabled && sk->sk_memcg)
2376 mem_cgroup_uncharge_skmem(sk->sk_memcg, amt);
Johannes Weinere8056052016-01-14 15:21:14 -08002377
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002378 return 0;
2379}
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002380EXPORT_SYMBOL(__sk_mem_raise_allocated);
2381
2382/**
2383 * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
2384 * @sk: socket
2385 * @size: memory size to allocate
2386 * @kind: allocation type
2387 *
2388 * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
2389 * rmem allocation. This function assumes that protocols which have
2390 * memory_pressure use sk_wmem_queued as write buffer accounting.
2391 */
2392int __sk_mem_schedule(struct sock *sk, int size, int kind)
2393{
2394 int ret, amt = sk_mem_pages(size);
2395
2396 sk->sk_forward_alloc += amt << SK_MEM_QUANTUM_SHIFT;
2397 ret = __sk_mem_raise_allocated(sk, size, amt, kind);
2398 if (!ret)
2399 sk->sk_forward_alloc -= amt << SK_MEM_QUANTUM_SHIFT;
2400 return ret;
2401}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002402EXPORT_SYMBOL(__sk_mem_schedule);
2403
2404/**
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002405 * __sk_mem_reduce_allocated - reclaim memory_allocated
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002406 * @sk: socket
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002407 * @amount: number of quanta
2408 *
2409 * Similar to __sk_mem_reclaim(), but does not update sk_forward_alloc
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002410 */
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002411void __sk_mem_reduce_allocated(struct sock *sk, int amount)
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002412{
Eric Dumazet1a24e042015-05-15 12:39:25 -07002413 sk_memory_allocated_sub(sk, amount);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002414
Johannes Weinerbaac50b2016-01-14 15:21:17 -08002415 if (mem_cgroup_sockets_enabled && sk->sk_memcg)
2416 mem_cgroup_uncharge_skmem(sk->sk_memcg, amount);
Johannes Weinere8056052016-01-14 15:21:14 -08002417
Glauber Costa180d8cd2011-12-11 21:47:02 +00002418 if (sk_under_memory_pressure(sk) &&
2419 (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
2420 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002421}
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002422EXPORT_SYMBOL(__sk_mem_reduce_allocated);
2423
2424/**
2425 * __sk_mem_reclaim - reclaim sk_forward_alloc and memory_allocated
2426 * @sk: socket
2427 * @amount: number of bytes (rounded down to a SK_MEM_QUANTUM multiple)
2428 */
2429void __sk_mem_reclaim(struct sock *sk, int amount)
2430{
2431 amount >>= SK_MEM_QUANTUM_SHIFT;
2432 sk->sk_forward_alloc -= amount << SK_MEM_QUANTUM_SHIFT;
2433 __sk_mem_reduce_allocated(sk, amount);
2434}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002435EXPORT_SYMBOL(__sk_mem_reclaim);
2436
samanthakumar627d2d62016-04-05 12:41:16 -04002437int sk_set_peek_off(struct sock *sk, int val)
2438{
2439 if (val < 0)
2440 return -EINVAL;
2441
2442 sk->sk_peek_off = val;
2443 return 0;
2444}
2445EXPORT_SYMBOL_GPL(sk_set_peek_off);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447/*
2448 * Set of default routines for initialising struct proto_ops when
2449 * the protocol does not support a particular function. In certain
2450 * cases where it makes no sense for a protocol to have a "do nothing"
2451 * function, some default processing is provided.
2452 */
2453
2454int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
2455{
2456 return -EOPNOTSUPP;
2457}
Eric Dumazet2a915252009-05-27 11:30:05 +00002458EXPORT_SYMBOL(sock_no_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002460int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 int len, int flags)
2462{
2463 return -EOPNOTSUPP;
2464}
Eric Dumazet2a915252009-05-27 11:30:05 +00002465EXPORT_SYMBOL(sock_no_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
2468{
2469 return -EOPNOTSUPP;
2470}
Eric Dumazet2a915252009-05-27 11:30:05 +00002471EXPORT_SYMBOL(sock_no_socketpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
David Howellscdfbabf2017-03-09 08:09:05 +00002473int sock_no_accept(struct socket *sock, struct socket *newsock, int flags,
2474 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 return -EOPNOTSUPP;
2477}
Eric Dumazet2a915252009-05-27 11:30:05 +00002478EXPORT_SYMBOL(sock_no_accept);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002480int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 int *len, int peer)
2482{
2483 return -EOPNOTSUPP;
2484}
Eric Dumazet2a915252009-05-27 11:30:05 +00002485EXPORT_SYMBOL(sock_no_getname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Eric Dumazet2a915252009-05-27 11:30:05 +00002487unsigned int sock_no_poll(struct file *file, struct socket *sock, poll_table *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
2489 return 0;
2490}
Eric Dumazet2a915252009-05-27 11:30:05 +00002491EXPORT_SYMBOL(sock_no_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2494{
2495 return -EOPNOTSUPP;
2496}
Eric Dumazet2a915252009-05-27 11:30:05 +00002497EXPORT_SYMBOL(sock_no_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499int sock_no_listen(struct socket *sock, int backlog)
2500{
2501 return -EOPNOTSUPP;
2502}
Eric Dumazet2a915252009-05-27 11:30:05 +00002503EXPORT_SYMBOL(sock_no_listen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505int sock_no_shutdown(struct socket *sock, int how)
2506{
2507 return -EOPNOTSUPP;
2508}
Eric Dumazet2a915252009-05-27 11:30:05 +00002509EXPORT_SYMBOL(sock_no_shutdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511int sock_no_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002512 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 return -EOPNOTSUPP;
2515}
Eric Dumazet2a915252009-05-27 11:30:05 +00002516EXPORT_SYMBOL(sock_no_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518int sock_no_getsockopt(struct socket *sock, int level, int optname,
2519 char __user *optval, int __user *optlen)
2520{
2521 return -EOPNOTSUPP;
2522}
Eric Dumazet2a915252009-05-27 11:30:05 +00002523EXPORT_SYMBOL(sock_no_getsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Ying Xue1b784142015-03-02 15:37:48 +08002525int sock_no_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 return -EOPNOTSUPP;
2528}
Eric Dumazet2a915252009-05-27 11:30:05 +00002529EXPORT_SYMBOL(sock_no_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Tom Herbert306b13e2017-07-28 16:22:41 -07002531int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *m, size_t len)
2532{
2533 return -EOPNOTSUPP;
2534}
2535EXPORT_SYMBOL(sock_no_sendmsg_locked);
2536
Ying Xue1b784142015-03-02 15:37:48 +08002537int sock_no_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
2538 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
2540 return -EOPNOTSUPP;
2541}
Eric Dumazet2a915252009-05-27 11:30:05 +00002542EXPORT_SYMBOL(sock_no_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
2545{
2546 /* Mirror missing mmap method error code */
2547 return -ENODEV;
2548}
Eric Dumazet2a915252009-05-27 11:30:05 +00002549EXPORT_SYMBOL(sock_no_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
2552{
2553 ssize_t res;
2554 struct msghdr msg = {.msg_flags = flags};
2555 struct kvec iov;
2556 char *kaddr = kmap(page);
2557 iov.iov_base = kaddr + offset;
2558 iov.iov_len = size;
2559 res = kernel_sendmsg(sock, &msg, &iov, 1, size);
2560 kunmap(page);
2561 return res;
2562}
Eric Dumazet2a915252009-05-27 11:30:05 +00002563EXPORT_SYMBOL(sock_no_sendpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Tom Herbert306b13e2017-07-28 16:22:41 -07002565ssize_t sock_no_sendpage_locked(struct sock *sk, struct page *page,
2566 int offset, size_t size, int flags)
2567{
2568 ssize_t res;
2569 struct msghdr msg = {.msg_flags = flags};
2570 struct kvec iov;
2571 char *kaddr = kmap(page);
2572
2573 iov.iov_base = kaddr + offset;
2574 iov.iov_len = size;
2575 res = kernel_sendmsg_locked(sk, &msg, &iov, 1, size);
2576 kunmap(page);
2577 return res;
2578}
2579EXPORT_SYMBOL(sock_no_sendpage_locked);
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581/*
2582 * Default Socket Callbacks
2583 */
2584
2585static void sock_def_wakeup(struct sock *sk)
2586{
Eric Dumazet43815482010-04-29 11:01:49 +00002587 struct socket_wq *wq;
2588
2589 rcu_read_lock();
2590 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002591 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +00002592 wake_up_interruptible_all(&wq->wait);
2593 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
2596static void sock_def_error_report(struct sock *sk)
2597{
Eric Dumazet43815482010-04-29 11:01:49 +00002598 struct socket_wq *wq;
2599
2600 rcu_read_lock();
2601 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002602 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +00002603 wake_up_interruptible_poll(&wq->wait, POLLERR);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002604 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
Eric Dumazet43815482010-04-29 11:01:49 +00002605 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
David S. Miller676d2362014-04-11 16:15:36 -04002608static void sock_def_readable(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
Eric Dumazet43815482010-04-29 11:01:49 +00002610 struct socket_wq *wq;
2611
2612 rcu_read_lock();
2613 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002614 if (skwq_has_sleeper(wq))
Eric Dumazet2c6607c2011-01-06 10:54:29 -08002615 wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI |
Davide Libenzi37e55402009-03-31 15:24:21 -07002616 POLLRDNORM | POLLRDBAND);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002617 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Eric Dumazet43815482010-04-29 11:01:49 +00002618 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
2621static void sock_def_write_space(struct sock *sk)
2622{
Eric Dumazet43815482010-04-29 11:01:49 +00002623 struct socket_wq *wq;
2624
2625 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 /* Do not wake up a writer until he can make "significant"
2628 * progress. --DaveM
2629 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03002630 if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
Eric Dumazet43815482010-04-29 11:01:49 +00002631 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002632 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +00002633 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
Davide Libenzi37e55402009-03-31 15:24:21 -07002634 POLLWRNORM | POLLWRBAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 /* Should agree with poll, otherwise some programs break */
2637 if (sock_writeable(sk))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002638 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
2640
Eric Dumazet43815482010-04-29 11:01:49 +00002641 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
2644static void sock_def_destruct(struct sock *sk)
2645{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
2648void sk_send_sigurg(struct sock *sk)
2649{
2650 if (sk->sk_socket && sk->sk_socket->file)
2651 if (send_sigurg(&sk->sk_socket->file->f_owner))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002652 sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653}
Eric Dumazet2a915252009-05-27 11:30:05 +00002654EXPORT_SYMBOL(sk_send_sigurg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656void sk_reset_timer(struct sock *sk, struct timer_list* timer,
2657 unsigned long expires)
2658{
2659 if (!mod_timer(timer, expires))
2660 sock_hold(sk);
2661}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662EXPORT_SYMBOL(sk_reset_timer);
2663
2664void sk_stop_timer(struct sock *sk, struct timer_list* timer)
2665{
Ying Xue25cc4ae2013-02-03 20:32:57 +00002666 if (del_timer(timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 __sock_put(sk);
2668}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669EXPORT_SYMBOL(sk_stop_timer);
2670
2671void sock_init_data(struct socket *sock, struct sock *sk)
2672{
Paolo Abeni581319c2017-03-09 13:54:08 +01002673 sk_init_common(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 sk->sk_send_head = NULL;
2675
2676 init_timer(&sk->sk_timer);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 sk->sk_allocation = GFP_KERNEL;
2679 sk->sk_rcvbuf = sysctl_rmem_default;
2680 sk->sk_sndbuf = sysctl_wmem_default;
2681 sk->sk_state = TCP_CLOSE;
David S. Miller972692e2008-06-17 22:41:38 -07002682 sk_set_socket(sk, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 sock_set_flag(sk, SOCK_ZAPPED);
2685
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002686 if (sock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 sk->sk_type = sock->type;
Eric Dumazet43815482010-04-29 11:01:49 +00002688 sk->sk_wq = sock->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 sock->sk = sk;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +09002690 sk->sk_uid = SOCK_INODE(sock)->i_uid;
2691 } else {
Eric Dumazet43815482010-04-29 11:01:49 +00002692 sk->sk_wq = NULL;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +09002693 sk->sk_uid = make_kuid(sock_net(sk)->user_ns, 0);
2694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 rwlock_init(&sk->sk_callback_lock);
David Howellscdfbabf2017-03-09 08:09:05 +00002697 if (sk->sk_kern_sock)
2698 lockdep_set_class_and_name(
2699 &sk->sk_callback_lock,
2700 af_kern_callback_keys + sk->sk_family,
2701 af_family_kern_clock_key_strings[sk->sk_family]);
2702 else
2703 lockdep_set_class_and_name(
2704 &sk->sk_callback_lock,
Peter Zijlstra443aef0e2007-07-19 01:49:00 -07002705 af_callback_keys + sk->sk_family,
2706 af_family_clock_key_strings[sk->sk_family]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 sk->sk_state_change = sock_def_wakeup;
2709 sk->sk_data_ready = sock_def_readable;
2710 sk->sk_write_space = sock_def_write_space;
2711 sk->sk_error_report = sock_def_error_report;
2712 sk->sk_destruct = sock_def_destruct;
2713
Eric Dumazet5640f762012-09-23 23:04:42 +00002714 sk->sk_frag.page = NULL;
2715 sk->sk_frag.offset = 0;
Pavel Emelyanovef64a542012-02-21 07:31:34 +00002716 sk->sk_peek_off = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Eric W. Biederman109f6e32010-06-13 03:30:14 +00002718 sk->sk_peer_pid = NULL;
2719 sk->sk_peer_cred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 sk->sk_write_pending = 0;
2721 sk->sk_rcvlowat = 1;
2722 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
2723 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
2724
Paolo Abeni6c7c98b2017-03-30 14:03:06 +02002725 sk->sk_stamp = SK_DEFAULT_STAMP;
Willem de Bruijn52267792017-08-03 16:29:39 -04002726 atomic_set(&sk->sk_zckey, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Cong Wange0d10952013-08-01 11:10:25 +08002728#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir06021292013-06-10 11:39:50 +03002729 sk->sk_napi_id = 0;
Eliezer Tamir64b0dc52013-07-10 17:13:36 +03002730 sk->sk_ll_usec = sysctl_net_busy_read;
Eliezer Tamir06021292013-06-10 11:39:50 +03002731#endif
2732
Eric Dumazet62748f32013-09-24 08:20:52 -07002733 sk->sk_max_pacing_rate = ~0U;
Eric Dumazet7eec4172013-10-08 15:16:00 -07002734 sk->sk_pacing_rate = ~0U;
Eric Dumazet70da2682015-10-08 19:33:21 -07002735 sk->sk_incoming_cpu = -1;
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00002736 /*
2737 * Before updating sk_refcnt, we must commit prior changes to memory
2738 * (Documentation/RCU/rculist_nulls.txt for details)
2739 */
2740 smp_wmb();
Reshetova, Elena41c6d652017-06-30 13:08:01 +03002741 refcount_set(&sk->sk_refcnt, 1);
Wang Chen33c732c2007-11-13 20:30:01 -08002742 atomic_set(&sk->sk_drops, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
Eric Dumazet2a915252009-05-27 11:30:05 +00002744EXPORT_SYMBOL(sock_init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002746void lock_sock_nested(struct sock *sk, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
2748 might_sleep();
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002749 spin_lock_bh(&sk->sk_lock.slock);
John Heffnerd2e91172007-09-12 10:44:19 +02002750 if (sk->sk_lock.owned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 __lock_sock(sk);
John Heffnerd2e91172007-09-12 10:44:19 +02002752 sk->sk_lock.owned = 1;
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002753 spin_unlock(&sk->sk_lock.slock);
2754 /*
2755 * The sk_lock has mutex_lock() semantics here:
2756 */
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002757 mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002758 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002760EXPORT_SYMBOL(lock_sock_nested);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002762void release_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002764 spin_lock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (sk->sk_backlog.tail)
2766 __release_sock(sk);
Eric Dumazet46d3cea2012-07-11 05:50:31 +00002767
Eric Dumazetc3f9b012014-03-10 09:50:11 -07002768 /* Warning : release_cb() might need to release sk ownership,
2769 * ie call sock_release_ownership(sk) before us.
2770 */
Eric Dumazet46d3cea2012-07-11 05:50:31 +00002771 if (sk->sk_prot->release_cb)
2772 sk->sk_prot->release_cb(sk);
2773
Eric Dumazetc3f9b012014-03-10 09:50:11 -07002774 sock_release_ownership(sk);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002775 if (waitqueue_active(&sk->sk_lock.wq))
2776 wake_up(&sk->sk_lock.wq);
2777 spin_unlock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779EXPORT_SYMBOL(release_sock);
2780
Eric Dumazet8a74ad62010-05-26 19:20:18 +00002781/**
2782 * lock_sock_fast - fast version of lock_sock
2783 * @sk: socket
2784 *
2785 * This version should be used for very small section, where process wont block
Mauro Carvalho Chehabd6519832017-05-12 09:35:46 -03002786 * return false if fast path is taken:
2787 *
Eric Dumazet8a74ad62010-05-26 19:20:18 +00002788 * sk_lock.slock locked, owned = 0, BH disabled
Mauro Carvalho Chehabd6519832017-05-12 09:35:46 -03002789 *
2790 * return true if slow path is taken:
2791 *
Eric Dumazet8a74ad62010-05-26 19:20:18 +00002792 * sk_lock.slock unlocked, owned = 1, BH enabled
2793 */
2794bool lock_sock_fast(struct sock *sk)
2795{
2796 might_sleep();
2797 spin_lock_bh(&sk->sk_lock.slock);
2798
2799 if (!sk->sk_lock.owned)
2800 /*
2801 * Note : We must disable BH
2802 */
2803 return false;
2804
2805 __lock_sock(sk);
2806 sk->sk_lock.owned = 1;
2807 spin_unlock(&sk->sk_lock.slock);
2808 /*
2809 * The sk_lock has mutex_lock() semantics here:
2810 */
2811 mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
2812 local_bh_enable();
2813 return true;
2814}
2815EXPORT_SYMBOL(lock_sock_fast);
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002818{
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002819 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (!sock_flag(sk, SOCK_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002821 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002822 tv = ktime_to_timeval(sk->sk_stamp);
2823 if (tv.tv_sec == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return -ENOENT;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002825 if (tv.tv_sec == 0) {
2826 sk->sk_stamp = ktime_get_real();
2827 tv = ktime_to_timeval(sk->sk_stamp);
2828 }
2829 return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831EXPORT_SYMBOL(sock_get_timestamp);
2832
Eric Dumazetae40eb12007-03-18 17:33:16 -07002833int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
2834{
2835 struct timespec ts;
2836 if (!sock_flag(sk, SOCK_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002837 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazetae40eb12007-03-18 17:33:16 -07002838 ts = ktime_to_timespec(sk->sk_stamp);
2839 if (ts.tv_sec == -1)
2840 return -ENOENT;
2841 if (ts.tv_sec == 0) {
2842 sk->sk_stamp = ktime_get_real();
2843 ts = ktime_to_timespec(sk->sk_stamp);
2844 }
2845 return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
2846}
2847EXPORT_SYMBOL(sock_get_timestampns);
2848
Patrick Ohly20d49472009-02-12 05:03:38 +00002849void sock_enable_timestamp(struct sock *sk, int flag)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002850{
Patrick Ohly20d49472009-02-12 05:03:38 +00002851 if (!sock_flag(sk, flag)) {
Eric Dumazet08e29af2011-11-28 12:04:18 +00002852 unsigned long previous_flags = sk->sk_flags;
2853
Patrick Ohly20d49472009-02-12 05:03:38 +00002854 sock_set_flag(sk, flag);
2855 /*
2856 * we just set one of the two flags which require net
2857 * time stamping, but time stamping might have been on
2858 * already because of the other one
2859 */
Hannes Frederic Sowa080a2702015-10-26 13:51:37 +01002860 if (sock_needs_netstamp(sk) &&
2861 !(previous_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002862 net_enable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
2864}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Richard Cochrancb820f82013-07-19 19:40:09 +02002866int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len,
2867 int level, int type)
2868{
2869 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04002870 struct sk_buff *skb;
Richard Cochrancb820f82013-07-19 19:40:09 +02002871 int copied, err;
2872
2873 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04002874 skb = sock_dequeue_err_skb(sk);
Richard Cochrancb820f82013-07-19 19:40:09 +02002875 if (skb == NULL)
2876 goto out;
2877
2878 copied = skb->len;
2879 if (copied > len) {
2880 msg->msg_flags |= MSG_TRUNC;
2881 copied = len;
2882 }
David S. Miller51f3d022014-11-05 16:46:40 -05002883 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Richard Cochrancb820f82013-07-19 19:40:09 +02002884 if (err)
2885 goto out_free_skb;
2886
2887 sock_recv_timestamp(msg, sk, skb);
2888
2889 serr = SKB_EXT_ERR(skb);
2890 put_cmsg(msg, level, type, sizeof(serr->ee), &serr->ee);
2891
2892 msg->msg_flags |= MSG_ERRQUEUE;
2893 err = copied;
2894
Richard Cochrancb820f82013-07-19 19:40:09 +02002895out_free_skb:
2896 kfree_skb(skb);
2897out:
2898 return err;
2899}
2900EXPORT_SYMBOL(sock_recv_errqueue);
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902/*
2903 * Get a socket option on an socket.
2904 *
2905 * FIX: POSIX 1003.1g is very ambiguous here. It states that
2906 * asynchronous errors should be reported by getsockopt. We assume
2907 * this means if you specify SO_ERROR (otherwise whats the point of it).
2908 */
2909int sock_common_getsockopt(struct socket *sock, int level, int optname,
2910 char __user *optval, int __user *optlen)
2911{
2912 struct sock *sk = sock->sk;
2913
2914 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2915}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916EXPORT_SYMBOL(sock_common_getsockopt);
2917
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002918#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002919int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
2920 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002921{
2922 struct sock *sk = sock->sk;
2923
Johannes Berg1e51f952007-03-06 13:44:06 -08002924 if (sk->sk_prot->compat_getsockopt != NULL)
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002925 return sk->sk_prot->compat_getsockopt(sk, level, optname,
2926 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002927 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2928}
2929EXPORT_SYMBOL(compat_sock_common_getsockopt);
2930#endif
2931
Ying Xue1b784142015-03-02 15:37:48 +08002932int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
2933 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
2935 struct sock *sk = sock->sk;
2936 int addr_len = 0;
2937 int err;
2938
Ying Xue1b784142015-03-02 15:37:48 +08002939 err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 flags & ~MSG_DONTWAIT, &addr_len);
2941 if (err >= 0)
2942 msg->msg_namelen = addr_len;
2943 return err;
2944}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945EXPORT_SYMBOL(sock_common_recvmsg);
2946
2947/*
2948 * Set socket options on an inet socket.
2949 */
2950int sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002951 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
2953 struct sock *sk = sock->sk;
2954
2955 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2956}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957EXPORT_SYMBOL(sock_common_setsockopt);
2958
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002959#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002960int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002961 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002962{
2963 struct sock *sk = sock->sk;
2964
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002965 if (sk->sk_prot->compat_setsockopt != NULL)
2966 return sk->sk_prot->compat_setsockopt(sk, level, optname,
2967 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08002968 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2969}
2970EXPORT_SYMBOL(compat_sock_common_setsockopt);
2971#endif
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973void sk_common_release(struct sock *sk)
2974{
2975 if (sk->sk_prot->destroy)
2976 sk->sk_prot->destroy(sk);
2977
2978 /*
2979 * Observation: when sock_common_release is called, processes have
2980 * no access to socket. But net still has.
2981 * Step one, detach it from networking:
2982 *
2983 * A. Remove from hash tables.
2984 */
2985
2986 sk->sk_prot->unhash(sk);
2987
2988 /*
2989 * In this point socket cannot receive new packets, but it is possible
2990 * that some packets are in flight because some CPU runs receiver and
2991 * did hash table lookup before we unhashed socket. They will achieve
2992 * receive queue and will be purged by socket destructor.
2993 *
2994 * Also we still have packets pending on receive queue and probably,
2995 * our own packets waiting in device queues. sock_destroy will drain
2996 * receive queue, but transmitted packets will delay socket destruction
2997 * until the last reference will be released.
2998 */
2999
3000 sock_orphan(sk);
3001
3002 xfrm_sk_free_policy(sk);
3003
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -07003004 sk_refcnt_debug_release(sk);
Eric Dumazet5640f762012-09-23 23:04:42 +00003005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 sock_put(sk);
3007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008EXPORT_SYMBOL(sk_common_release);
3009
Josh Hunta2d133b2017-03-20 15:22:03 -04003010void sk_get_meminfo(const struct sock *sk, u32 *mem)
3011{
3012 memset(mem, 0, sizeof(*mem) * SK_MEMINFO_VARS);
3013
3014 mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
3015 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
3016 mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
3017 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
3018 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
3019 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
3020 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
3021 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
3022 mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
3023}
3024
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003025#ifdef CONFIG_PROC_FS
3026#define PROTO_INUSE_NR 64 /* should be enough for the first time */
Pavel Emelyanov1338d462008-03-28 16:38:43 -07003027struct prot_inuse {
3028 int val[PROTO_INUSE_NR];
3029};
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003030
3031static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003032
3033#ifdef CONFIG_NET_NS
3034void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
3035{
Eric Dumazetd6d9ca02010-07-19 10:48:49 +00003036 __this_cpu_add(net->core.inuse->val[prot->inuse_idx], val);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003037}
3038EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
3039
3040int sock_prot_inuse_get(struct net *net, struct proto *prot)
3041{
3042 int cpu, idx = prot->inuse_idx;
3043 int res = 0;
3044
3045 for_each_possible_cpu(cpu)
3046 res += per_cpu_ptr(net->core.inuse, cpu)->val[idx];
3047
3048 return res >= 0 ? res : 0;
3049}
3050EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
3051
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003052static int __net_init sock_inuse_init_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003053{
3054 net->core.inuse = alloc_percpu(struct prot_inuse);
3055 return net->core.inuse ? 0 : -ENOMEM;
3056}
3057
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003058static void __net_exit sock_inuse_exit_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003059{
3060 free_percpu(net->core.inuse);
3061}
3062
3063static struct pernet_operations net_inuse_ops = {
3064 .init = sock_inuse_init_net,
3065 .exit = sock_inuse_exit_net,
3066};
3067
3068static __init int net_inuse_init(void)
3069{
3070 if (register_pernet_subsys(&net_inuse_ops))
3071 panic("Cannot initialize net inuse counters");
3072
3073 return 0;
3074}
3075
3076core_initcall(net_inuse_init);
3077#else
Pavel Emelyanov1338d462008-03-28 16:38:43 -07003078static DEFINE_PER_CPU(struct prot_inuse, prot_inuse);
3079
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -07003080void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
Pavel Emelyanov1338d462008-03-28 16:38:43 -07003081{
Eric Dumazetd6d9ca02010-07-19 10:48:49 +00003082 __this_cpu_add(prot_inuse.val[prot->inuse_idx], val);
Pavel Emelyanov1338d462008-03-28 16:38:43 -07003083}
3084EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
3085
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -07003086int sock_prot_inuse_get(struct net *net, struct proto *prot)
Pavel Emelyanov1338d462008-03-28 16:38:43 -07003087{
3088 int cpu, idx = prot->inuse_idx;
3089 int res = 0;
3090
3091 for_each_possible_cpu(cpu)
3092 res += per_cpu(prot_inuse, cpu).val[idx];
3093
3094 return res >= 0 ? res : 0;
3095}
3096EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003097#endif
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003098
3099static void assign_proto_idx(struct proto *prot)
3100{
3101 prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
3102
3103 if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
Joe Perchese005d192012-05-16 19:58:40 +00003104 pr_err("PROTO_INUSE_NR exhausted\n");
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003105 return;
3106 }
3107
3108 set_bit(prot->inuse_idx, proto_inuse_idx);
3109}
3110
3111static void release_proto_idx(struct proto *prot)
3112{
3113 if (prot->inuse_idx != PROTO_INUSE_NR - 1)
3114 clear_bit(prot->inuse_idx, proto_inuse_idx);
3115}
3116#else
3117static inline void assign_proto_idx(struct proto *prot)
3118{
3119}
3120
3121static inline void release_proto_idx(struct proto *prot)
3122{
3123}
3124#endif
3125
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003126static void req_prot_cleanup(struct request_sock_ops *rsk_prot)
3127{
3128 if (!rsk_prot)
3129 return;
3130 kfree(rsk_prot->slab_name);
3131 rsk_prot->slab_name = NULL;
Julia Lawalladf78ed2015-09-13 14:15:18 +02003132 kmem_cache_destroy(rsk_prot->slab);
3133 rsk_prot->slab = NULL;
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003134}
3135
3136static int req_prot_init(const struct proto *prot)
3137{
3138 struct request_sock_ops *rsk_prot = prot->rsk_prot;
3139
3140 if (!rsk_prot)
3141 return 0;
3142
3143 rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s",
3144 prot->name);
3145 if (!rsk_prot->slab_name)
3146 return -ENOMEM;
3147
3148 rsk_prot->slab = kmem_cache_create(rsk_prot->slab_name,
3149 rsk_prot->obj_size, 0,
Eric Dumazete96f78a2015-10-03 06:27:28 -07003150 prot->slab_flags, NULL);
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003151
3152 if (!rsk_prot->slab) {
3153 pr_crit("%s: Can't create request sock SLAB cache!\n",
3154 prot->name);
3155 return -ENOMEM;
3156 }
3157 return 0;
3158}
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160int proto_register(struct proto *prot, int alloc_slab)
3161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (alloc_slab) {
3163 prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
Eric Dumazet271b72c2008-10-29 02:11:14 -07003164 SLAB_HWCACHE_ALIGN | prot->slab_flags,
3165 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
3167 if (prot->slab == NULL) {
Joe Perchese005d192012-05-16 19:58:40 +00003168 pr_crit("%s: Can't create sock SLAB cache!\n",
3169 prot->name);
Pavel Emelyanov60e76632008-03-28 16:39:10 -07003170 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07003172
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003173 if (req_prot_init(prot))
3174 goto out_free_request_sock_slab;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003175
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003176 if (prot->twsk_prot != NULL) {
Alexey Dobriyanfaf23422010-02-17 09:34:12 +00003177 prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003178
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08003179 if (prot->twsk_prot->twsk_slab_name == NULL)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003180 goto out_free_request_sock_slab;
3181
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003182 prot->twsk_prot->twsk_slab =
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08003183 kmem_cache_create(prot->twsk_prot->twsk_slab_name,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003184 prot->twsk_prot->twsk_obj_size,
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08003185 0,
Eric Dumazet52db70d2015-04-10 06:07:18 -07003186 prot->slab_flags,
Paul Mundt20c2df82007-07-20 10:11:58 +09003187 NULL);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003188 if (prot->twsk_prot->twsk_slab == NULL)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003189 goto out_free_timewait_sock_slab_name;
3190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192
Glauber Costa36b77a52011-12-16 00:51:59 +00003193 mutex_lock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 list_add(&prot->node, &proto_list);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003195 assign_proto_idx(prot);
Glauber Costa36b77a52011-12-16 00:51:59 +00003196 mutex_unlock(&proto_list_mutex);
Pavel Emelyanovb733c002007-11-07 02:23:38 -08003197 return 0;
3198
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003199out_free_timewait_sock_slab_name:
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08003200 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003201out_free_request_sock_slab:
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003202 req_prot_cleanup(prot->rsk_prot);
3203
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07003204 kmem_cache_destroy(prot->slab);
3205 prot->slab = NULL;
Pavel Emelyanovb733c002007-11-07 02:23:38 -08003206out:
3207 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209EXPORT_SYMBOL(proto_register);
3210
3211void proto_unregister(struct proto *prot)
3212{
Glauber Costa36b77a52011-12-16 00:51:59 +00003213 mutex_lock(&proto_list_mutex);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003214 release_proto_idx(prot);
Patrick McHardy0a3f4352005-09-06 19:47:50 -07003215 list_del(&prot->node);
Glauber Costa36b77a52011-12-16 00:51:59 +00003216 mutex_unlock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Julia Lawalladf78ed2015-09-13 14:15:18 +02003218 kmem_cache_destroy(prot->slab);
3219 prot->slab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003221 req_prot_cleanup(prot->rsk_prot);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07003222
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003223 if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003224 kmem_cache_destroy(prot->twsk_prot->twsk_slab);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08003225 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003226 prot->twsk_prot->twsk_slab = NULL;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229EXPORT_SYMBOL(proto_unregister);
3230
3231#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
Glauber Costa36b77a52011-12-16 00:51:59 +00003233 __acquires(proto_list_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Glauber Costa36b77a52011-12-16 00:51:59 +00003235 mutex_lock(&proto_list_mutex);
Pavel Emelianov60f04382007-07-09 13:15:14 -07003236 return seq_list_start_head(&proto_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
3239static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3240{
Pavel Emelianov60f04382007-07-09 13:15:14 -07003241 return seq_list_next(v, &proto_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
3244static void proto_seq_stop(struct seq_file *seq, void *v)
Glauber Costa36b77a52011-12-16 00:51:59 +00003245 __releases(proto_list_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Glauber Costa36b77a52011-12-16 00:51:59 +00003247 mutex_unlock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
3250static char proto_method_implemented(const void *method)
3251{
3252 return method == NULL ? 'n' : 'y';
3253}
Glauber Costa180d8cd2011-12-11 21:47:02 +00003254static long sock_prot_memory_allocated(struct proto *proto)
3255{
Jeffrin Josecb75a362012-04-25 19:17:29 +05303256 return proto->memory_allocated != NULL ? proto_memory_allocated(proto) : -1L;
Glauber Costa180d8cd2011-12-11 21:47:02 +00003257}
3258
3259static char *sock_prot_memory_pressure(struct proto *proto)
3260{
3261 return proto->memory_pressure != NULL ?
3262 proto_memory_pressure(proto) ? "yes" : "no" : "NI";
3263}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
3265static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
3266{
Glauber Costa180d8cd2011-12-11 21:47:02 +00003267
Eric Dumazet8d987e52010-11-09 23:24:26 +00003268 seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
3270 proto->name,
3271 proto->obj_size,
Eric Dumazet14e943d2008-11-19 15:14:01 -08003272 sock_prot_inuse_get(seq_file_net(seq), proto),
Glauber Costa180d8cd2011-12-11 21:47:02 +00003273 sock_prot_memory_allocated(proto),
3274 sock_prot_memory_pressure(proto),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 proto->max_header,
3276 proto->slab == NULL ? "no" : "yes",
3277 module_name(proto->owner),
3278 proto_method_implemented(proto->close),
3279 proto_method_implemented(proto->connect),
3280 proto_method_implemented(proto->disconnect),
3281 proto_method_implemented(proto->accept),
3282 proto_method_implemented(proto->ioctl),
3283 proto_method_implemented(proto->init),
3284 proto_method_implemented(proto->destroy),
3285 proto_method_implemented(proto->shutdown),
3286 proto_method_implemented(proto->setsockopt),
3287 proto_method_implemented(proto->getsockopt),
3288 proto_method_implemented(proto->sendmsg),
3289 proto_method_implemented(proto->recvmsg),
3290 proto_method_implemented(proto->sendpage),
3291 proto_method_implemented(proto->bind),
3292 proto_method_implemented(proto->backlog_rcv),
3293 proto_method_implemented(proto->hash),
3294 proto_method_implemented(proto->unhash),
3295 proto_method_implemented(proto->get_port),
3296 proto_method_implemented(proto->enter_memory_pressure));
3297}
3298
3299static int proto_seq_show(struct seq_file *seq, void *v)
3300{
Pavel Emelianov60f04382007-07-09 13:15:14 -07003301 if (v == &proto_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
3303 "protocol",
3304 "size",
3305 "sockets",
3306 "memory",
3307 "press",
3308 "maxhdr",
3309 "slab",
3310 "module",
3311 "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
3312 else
Pavel Emelianov60f04382007-07-09 13:15:14 -07003313 proto_seq_printf(seq, list_entry(v, struct proto, node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 return 0;
3315}
3316
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003317static const struct seq_operations proto_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 .start = proto_seq_start,
3319 .next = proto_seq_next,
3320 .stop = proto_seq_stop,
3321 .show = proto_seq_show,
3322};
3323
3324static int proto_seq_open(struct inode *inode, struct file *file)
3325{
Eric Dumazet14e943d2008-11-19 15:14:01 -08003326 return seq_open_net(inode, file, &proto_seq_ops,
3327 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
Arjan van de Ven9a321442007-02-12 00:55:35 -08003330static const struct file_operations proto_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 .owner = THIS_MODULE,
3332 .open = proto_seq_open,
3333 .read = seq_read,
3334 .llseek = seq_lseek,
Eric Dumazet14e943d2008-11-19 15:14:01 -08003335 .release = seq_release_net,
3336};
3337
3338static __net_init int proto_init_net(struct net *net)
3339{
Gao fengd4beaa62013-02-18 01:34:54 +00003340 if (!proc_create("protocols", S_IRUGO, net->proc_net, &proto_seq_fops))
Eric Dumazet14e943d2008-11-19 15:14:01 -08003341 return -ENOMEM;
3342
3343 return 0;
3344}
3345
3346static __net_exit void proto_exit_net(struct net *net)
3347{
Gao fengece31ff2013-02-18 01:34:56 +00003348 remove_proc_entry("protocols", net->proc_net);
Eric Dumazet14e943d2008-11-19 15:14:01 -08003349}
3350
3351
3352static __net_initdata struct pernet_operations proto_net_ops = {
3353 .init = proto_init_net,
3354 .exit = proto_exit_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355};
3356
3357static int __init proto_init(void)
3358{
Eric Dumazet14e943d2008-11-19 15:14:01 -08003359 return register_pernet_subsys(&proto_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
3362subsys_initcall(proto_init);
3363
3364#endif /* PROC_FS */
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07003365
3366#ifdef CONFIG_NET_RX_BUSY_POLL
3367bool sk_busy_loop_end(void *p, unsigned long start_time)
3368{
3369 struct sock *sk = p;
3370
3371 return !skb_queue_empty(&sk->sk_receive_queue) ||
3372 sk_busy_loop_timeout(sk, start_time);
3373}
3374EXPORT_SYMBOL(sk_busy_loop_end);
3375#endif /* CONFIG_NET_RX_BUSY_POLL */