blob: c9668dcb5eb91e55f063448d5a57f29d91225e47 [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
Richard Cochran80b14de2018-07-03 15:42:48 -070094#include <asm/unaligned.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080095#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/errno.h>
Richard Cochrancb820f82013-07-19 19:40:09 +020097#include <linux/errqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/types.h>
99#include <linux/socket.h>
100#include <linux/in.h>
101#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <linux/module.h>
103#include <linux/proc_fs.h>
104#include <linux/seq_file.h>
105#include <linux/sched.h>
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700106#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#include <linux/timer.h>
108#include <linux/string.h>
109#include <linux/sockios.h>
110#include <linux/net.h>
111#include <linux/mm.h>
112#include <linux/slab.h>
113#include <linux/interrupt.h>
114#include <linux/poll.h>
115#include <linux/tcp.h>
116#include <linux/init.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -0400117#include <linux/highmem.h>
Eric W. Biederman3f551f92010-06-13 03:28:59 +0000118#include <linux/user_namespace.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100119#include <linux/static_key.h>
David S. Miller3969eb32012-01-09 13:44:23 -0800120#include <linux/memcontrol.h>
David S. Miller8c1ae102012-05-03 02:25:55 -0400121#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800123#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125#include <linux/netdevice.h>
126#include <net/protocol.h>
127#include <linux/skbuff.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200128#include <net/net_namespace.h>
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700129#include <net/request_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#include <net/sock.h>
Patrick Ohly20d49472009-02-12 05:03:38 +0000131#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#include <net/xfrm.h>
133#include <linux/ipsec.h>
Herbert Xuf8451722010-05-24 00:12:34 -0700134#include <net/cls_cgroup.h>
Neil Horman5bc14212011-11-22 05:10:51 +0000135#include <net/netprio_cgroup.h>
Craig Gallekeb4cb002015-06-15 11:26:18 -0400136#include <linux/sock_diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138#include <linux/filter.h>
Craig Gallek538950a2016-01-04 17:41:47 -0500139#include <net/sock_reuseport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Satoru Moriya3847ce32011-06-17 12:00:03 +0000141#include <trace/events/sock.h>
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#include <net/tcp.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300144#include <net/busy_poll.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300145
Glauber Costa36b77a52011-12-16 00:51:59 +0000146static DEFINE_MUTEX(proto_list_mutex);
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000147static LIST_HEAD(proto_list);
148
Tonghao Zhang648845a2017-12-14 05:51:58 -0800149static void sock_inuse_add(struct net *net, int val);
150
Eric W. Biedermana3b299d2014-04-23 14:26:56 -0700151/**
152 * sk_ns_capable - General socket capability test
153 * @sk: Socket to use a capability on or through
154 * @user_ns: The user namespace of the capability to use
155 * @cap: The capability to use
156 *
157 * Test to see if the opener of the socket had when the socket was
158 * created and the current process has the capability @cap in the user
159 * namespace @user_ns.
160 */
161bool sk_ns_capable(const struct sock *sk,
162 struct user_namespace *user_ns, int cap)
163{
164 return file_ns_capable(sk->sk_socket->file, user_ns, cap) &&
165 ns_capable(user_ns, cap);
166}
167EXPORT_SYMBOL(sk_ns_capable);
168
169/**
170 * sk_capable - Socket global capability test
171 * @sk: Socket to use a capability on or through
Masanari Iidae793c0f2014-09-04 23:44:36 +0900172 * @cap: The global capability to use
Eric W. Biedermana3b299d2014-04-23 14:26:56 -0700173 *
174 * Test to see if the opener of the socket had when the socket was
175 * created and the current process has the capability @cap in all user
176 * namespaces.
177 */
178bool sk_capable(const struct sock *sk, int cap)
179{
180 return sk_ns_capable(sk, &init_user_ns, cap);
181}
182EXPORT_SYMBOL(sk_capable);
183
184/**
185 * sk_net_capable - Network namespace socket capability test
186 * @sk: Socket to use a capability on or through
187 * @cap: The capability to use
188 *
Masanari Iidae793c0f2014-09-04 23:44:36 +0900189 * Test to see if the opener of the socket had when the socket was created
Eric W. Biedermana3b299d2014-04-23 14:26:56 -0700190 * and the current process has the capability @cap over the network namespace
191 * the socket is a member of.
192 */
193bool sk_net_capable(const struct sock *sk, int cap)
194{
195 return sk_ns_capable(sk, sock_net(sk)->user_ns, cap);
196}
197EXPORT_SYMBOL(sk_net_capable);
198
Ingo Molnarda21f242006-07-03 00:25:12 -0700199/*
200 * Each address family might have different locking rules, so we have
David Howellscdfbabf2017-03-09 08:09:05 +0000201 * one slock key per address family and separate keys for internal and
202 * userspace sockets.
Ingo Molnarda21f242006-07-03 00:25:12 -0700203 */
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700204static struct lock_class_key af_family_keys[AF_MAX];
David Howellscdfbabf2017-03-09 08:09:05 +0000205static struct lock_class_key af_family_kern_keys[AF_MAX];
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700206static struct lock_class_key af_family_slock_keys[AF_MAX];
David Howellscdfbabf2017-03-09 08:09:05 +0000207static struct lock_class_key af_family_kern_slock_keys[AF_MAX];
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700208
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700209/*
210 * Make lock validator output more readable. (we pre-construct these
211 * strings build-time, so that runtime initialization of socket
212 * locks is fast):
213 */
David Howellscdfbabf2017-03-09 08:09:05 +0000214
215#define _sock_locks(x) \
216 x "AF_UNSPEC", x "AF_UNIX" , x "AF_INET" , \
217 x "AF_AX25" , x "AF_IPX" , x "AF_APPLETALK", \
218 x "AF_NETROM", x "AF_BRIDGE" , x "AF_ATMPVC" , \
219 x "AF_X25" , x "AF_INET6" , x "AF_ROSE" , \
220 x "AF_DECnet", x "AF_NETBEUI" , x "AF_SECURITY" , \
221 x "AF_KEY" , x "AF_NETLINK" , x "AF_PACKET" , \
222 x "AF_ASH" , x "AF_ECONET" , x "AF_ATMSVC" , \
223 x "AF_RDS" , x "AF_SNA" , x "AF_IRDA" , \
224 x "AF_PPPOX" , x "AF_WANPIPE" , x "AF_LLC" , \
225 x "27" , x "28" , x "AF_CAN" , \
226 x "AF_TIPC" , x "AF_BLUETOOTH", x "IUCV" , \
227 x "AF_RXRPC" , x "AF_ISDN" , x "AF_PHONET" , \
228 x "AF_IEEE802154", x "AF_CAIF" , x "AF_ALG" , \
229 x "AF_NFC" , x "AF_VSOCK" , x "AF_KCM" , \
Björn Töpel68e8b842018-05-02 13:01:22 +0200230 x "AF_QIPCRTR", x "AF_SMC" , x "AF_XDP" , \
231 x "AF_MAX"
David Howellscdfbabf2017-03-09 08:09:05 +0000232
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700233static const char *const af_family_key_strings[AF_MAX+1] = {
David Howellscdfbabf2017-03-09 08:09:05 +0000234 _sock_locks("sk_lock-")
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700235};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700236static const char *const af_family_slock_key_strings[AF_MAX+1] = {
David Howellscdfbabf2017-03-09 08:09:05 +0000237 _sock_locks("slock-")
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700238};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700239static const char *const af_family_clock_key_strings[AF_MAX+1] = {
David Howellscdfbabf2017-03-09 08:09:05 +0000240 _sock_locks("clock-")
241};
242
243static const char *const af_family_kern_key_strings[AF_MAX+1] = {
244 _sock_locks("k-sk_lock-")
245};
246static const char *const af_family_kern_slock_key_strings[AF_MAX+1] = {
247 _sock_locks("k-slock-")
248};
249static const char *const af_family_kern_clock_key_strings[AF_MAX+1] = {
250 _sock_locks("k-clock-")
Peter Zijlstra443aef02007-07-19 01:49:00 -0700251};
Paolo Abeni581319c2017-03-09 13:54:08 +0100252static const char *const af_family_rlock_key_strings[AF_MAX+1] = {
Matthieu Baerts6b431d52018-08-02 18:14:33 +0200253 _sock_locks("rlock-")
Paolo Abeni581319c2017-03-09 13:54:08 +0100254};
255static const char *const af_family_wlock_key_strings[AF_MAX+1] = {
Matthieu Baerts6b431d52018-08-02 18:14:33 +0200256 _sock_locks("wlock-")
Paolo Abeni581319c2017-03-09 13:54:08 +0100257};
258static const char *const af_family_elock_key_strings[AF_MAX+1] = {
Matthieu Baerts6b431d52018-08-02 18:14:33 +0200259 _sock_locks("elock-")
Paolo Abeni581319c2017-03-09 13:54:08 +0100260};
Ingo Molnarda21f242006-07-03 00:25:12 -0700261
262/*
Paolo Abeni581319c2017-03-09 13:54:08 +0100263 * sk_callback_lock and sk queues locking rules are per-address-family,
Ingo Molnarda21f242006-07-03 00:25:12 -0700264 * so split the lock classes by using a per-AF key:
265 */
266static struct lock_class_key af_callback_keys[AF_MAX];
Paolo Abeni581319c2017-03-09 13:54:08 +0100267static struct lock_class_key af_rlock_keys[AF_MAX];
268static struct lock_class_key af_wlock_keys[AF_MAX];
269static struct lock_class_key af_elock_keys[AF_MAX];
David Howellscdfbabf2017-03-09 08:09:05 +0000270static struct lock_class_key af_kern_callback_keys[AF_MAX];
Ingo Molnarda21f242006-07-03 00:25:12 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/* Run time adjustable parameters. */
Brian Haleyab32ea52006-09-22 14:15:41 -0700273__u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
Hans Schillstrom6d8ebc82012-04-30 08:13:50 +0200274EXPORT_SYMBOL(sysctl_wmem_max);
Brian Haleyab32ea52006-09-22 14:15:41 -0700275__u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
Hans Schillstrom6d8ebc82012-04-30 08:13:50 +0200276EXPORT_SYMBOL(sysctl_rmem_max);
Brian Haleyab32ea52006-09-22 14:15:41 -0700277__u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
278__u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300280/* Maximal space eaten by iovec or ancillary data plus some space */
Brian Haleyab32ea52006-09-22 14:15:41 -0700281int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
Eric Dumazet2a915252009-05-27 11:30:05 +0000282EXPORT_SYMBOL(sysctl_optmem_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Willem de Bruijnb245be12015-01-30 13:29:32 -0500284int sysctl_tstamp_allow_data __read_mostly = 1;
285
Davidlohr Buesoa7950ae2018-05-08 09:06:59 -0700286DEFINE_STATIC_KEY_FALSE(memalloc_socks_key);
287EXPORT_SYMBOL_GPL(memalloc_socks_key);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700288
Mel Gorman7cb02402012-07-31 16:44:16 -0700289/**
290 * sk_set_memalloc - sets %SOCK_MEMALLOC
291 * @sk: socket to set it on
292 *
293 * Set %SOCK_MEMALLOC on a socket for access to emergency reserves.
294 * It's the responsibility of the admin to adjust min_free_kbytes
295 * to meet the requirements
296 */
297void sk_set_memalloc(struct sock *sk)
298{
299 sock_set_flag(sk, SOCK_MEMALLOC);
300 sk->sk_allocation |= __GFP_MEMALLOC;
Davidlohr Buesoa7950ae2018-05-08 09:06:59 -0700301 static_branch_inc(&memalloc_socks_key);
Mel Gorman7cb02402012-07-31 16:44:16 -0700302}
303EXPORT_SYMBOL_GPL(sk_set_memalloc);
304
305void sk_clear_memalloc(struct sock *sk)
306{
307 sock_reset_flag(sk, SOCK_MEMALLOC);
308 sk->sk_allocation &= ~__GFP_MEMALLOC;
Davidlohr Buesoa7950ae2018-05-08 09:06:59 -0700309 static_branch_dec(&memalloc_socks_key);
Mel Gormanc76562b2012-07-31 16:44:41 -0700310
311 /*
312 * SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
Mel Gorman5d753612015-06-10 21:02:04 -0400313 * progress of swapping. SOCK_MEMALLOC may be cleared while
314 * it has rmem allocations due to the last swapfile being deactivated
315 * but there is a risk that the socket is unusable due to exceeding
316 * the rmem limits. Reclaim the reserves and obey rmem limits again.
Mel Gormanc76562b2012-07-31 16:44:41 -0700317 */
Mel Gorman5d753612015-06-10 21:02:04 -0400318 sk_mem_reclaim(sk);
Mel Gorman7cb02402012-07-31 16:44:16 -0700319}
320EXPORT_SYMBOL_GPL(sk_clear_memalloc);
321
Mel Gormanb4b9e352012-07-31 16:44:26 -0700322int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
323{
324 int ret;
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700325 unsigned int noreclaim_flag;
Mel Gormanb4b9e352012-07-31 16:44:26 -0700326
327 /* these should have been dropped before queueing */
328 BUG_ON(!sock_flag(sk, SOCK_MEMALLOC));
329
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700330 noreclaim_flag = memalloc_noreclaim_save();
Mel Gormanb4b9e352012-07-31 16:44:26 -0700331 ret = sk->sk_backlog_rcv(sk, skb);
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700332 memalloc_noreclaim_restore(noreclaim_flag);
Mel Gormanb4b9e352012-07-31 16:44:26 -0700333
334 return ret;
335}
336EXPORT_SYMBOL(__sk_backlog_rcv);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
339{
340 struct timeval tv;
341
342 if (optlen < sizeof(tv))
343 return -EINVAL;
344 if (copy_from_user(&tv, optval, sizeof(tv)))
345 return -EFAULT;
Vasily Averinba780732007-05-24 16:58:54 -0700346 if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
347 return -EDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Vasily Averinba780732007-05-24 16:58:54 -0700349 if (tv.tv_sec < 0) {
Andrew Morton6f11df82007-07-09 13:16:00 -0700350 static int warned __read_mostly;
351
Vasily Averinba780732007-05-24 16:58:54 -0700352 *timeo_p = 0;
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700353 if (warned < 10 && net_ratelimit()) {
Vasily Averinba780732007-05-24 16:58:54 -0700354 warned++;
Joe Perchese005d192012-05-16 19:58:40 +0000355 pr_info("%s: `%s' (pid %d) tries to set negative timeout\n",
356 __func__, current->comm, task_pid_nr(current));
Ilpo Järvinen50aab542008-05-02 16:20:10 -0700357 }
Vasily Averinba780732007-05-24 16:58:54 -0700358 return 0;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *timeo_p = MAX_SCHEDULE_TIMEOUT;
361 if (tv.tv_sec == 0 && tv.tv_usec == 0)
362 return 0;
363 if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
Gao Feng8ccde4c2017-02-21 17:09:19 +0800364 *timeo_p = tv.tv_sec * HZ + DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366}
367
368static void sock_warn_obsolete_bsdism(const char *name)
369{
370 static int warned;
371 static char warncomm[TASK_COMM_LEN];
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900372 if (strcmp(warncomm, current->comm) && warned < 5) {
373 strcpy(warncomm, current->comm);
Joe Perchese005d192012-05-16 19:58:40 +0000374 pr_warn("process `%s' is using obsolete %s SO_BSDCOMPAT\n",
375 warncomm, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 warned++;
377 }
378}
379
Hannes Frederic Sowa080a2702015-10-26 13:51:37 +0100380static bool sock_needs_netstamp(const struct sock *sk)
381{
382 switch (sk->sk_family) {
383 case AF_UNSPEC:
384 case AF_UNIX:
385 return false;
386 default:
387 return true;
388 }
389}
390
Eric Dumazet08e29af2011-11-28 12:04:18 +0000391static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900392{
Eric Dumazet08e29af2011-11-28 12:04:18 +0000393 if (sk->sk_flags & flags) {
394 sk->sk_flags &= ~flags;
Hannes Frederic Sowa080a2702015-10-26 13:51:37 +0100395 if (sock_needs_netstamp(sk) &&
396 !(sk->sk_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +0000397 net_disable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399}
400
401
samanthakumare6afc8a2016-04-05 12:41:15 -0400402int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800403{
Neil Horman3b885782009-10-12 13:26:31 -0700404 unsigned long flags;
405 struct sk_buff_head *list = &sk->sk_receive_queue;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800406
Eric Dumazet0fd7bac2011-12-21 07:11:44 +0000407 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700408 atomic_inc(&sk->sk_drops);
Satoru Moriya3847ce32011-06-17 12:00:03 +0000409 trace_sock_rcvqueue_full(sk, skb);
Eric Dumazet766e90372009-10-14 20:40:11 -0700410 return -ENOMEM;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800411 }
412
Mel Gormanc76562b2012-07-31 16:44:41 -0700413 if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
Eric Dumazet766e90372009-10-14 20:40:11 -0700414 atomic_inc(&sk->sk_drops);
415 return -ENOBUFS;
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800416 }
417
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800418 skb->dev = NULL;
419 skb_set_owner_r(skb, sk);
David S. Miller49ad9592008-12-17 22:11:38 -0800420
Eric Dumazet7fee2262010-05-11 23:19:48 +0000421 /* we escape from rcu protected region, make sure we dont leak
422 * a norefcounted dst
423 */
424 skb_dst_force(skb);
425
Neil Horman3b885782009-10-12 13:26:31 -0700426 spin_lock_irqsave(&list->lock, flags);
Eyal Birger3bc3b962015-03-01 14:58:30 +0200427 sock_skb_set_dropcount(sk, skb);
Neil Horman3b885782009-10-12 13:26:31 -0700428 __skb_queue_tail(list, skb);
429 spin_unlock_irqrestore(&list->lock, flags);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800430
431 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -0400432 sk->sk_data_ready(sk);
Eric Dumazet766e90372009-10-14 20:40:11 -0700433 return 0;
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800434}
samanthakumare6afc8a2016-04-05 12:41:15 -0400435EXPORT_SYMBOL(__sock_queue_rcv_skb);
436
437int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
438{
439 int err;
440
441 err = sk_filter(sk, skb);
442 if (err)
443 return err;
444
445 return __sock_queue_rcv_skb(sk, skb);
446}
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800447EXPORT_SYMBOL(sock_queue_rcv_skb);
448
Willem de Bruijn4f0c40d92016-07-12 18:18:57 -0400449int __sk_receive_skb(struct sock *sk, struct sk_buff *skb,
Eric Dumazetc3f24cf2016-11-02 17:14:41 -0700450 const int nested, unsigned int trim_cap, bool refcounted)
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800451{
452 int rc = NET_RX_SUCCESS;
453
Willem de Bruijn4f0c40d92016-07-12 18:18:57 -0400454 if (sk_filter_trim_cap(sk, skb, trim_cap))
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800455 goto discard_and_relse;
456
457 skb->dev = NULL;
458
Sorin Dumitru274f4822014-07-22 21:16:51 +0300459 if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
Eric Dumazetc3774112010-04-27 15:13:20 -0700460 atomic_inc(&sk->sk_drops);
461 goto discard_and_relse;
462 }
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200463 if (nested)
464 bh_lock_sock_nested(sk);
465 else
466 bh_lock_sock(sk);
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700467 if (!sock_owned_by_user(sk)) {
468 /*
469 * trylock + unlock semantics:
470 */
471 mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
472
Peter Zijlstrac57943a2008-10-07 14:18:42 -0700473 rc = sk_backlog_rcv(sk, skb);
Ingo Molnara5b5bb92006-07-03 00:25:35 -0700474
475 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
Eric Dumazetf545a382012-04-22 23:34:26 +0000476 } else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
Zhu Yi8eae9392010-03-04 18:01:40 +0000477 bh_unlock_sock(sk);
478 atomic_inc(&sk->sk_drops);
479 goto discard_and_relse;
480 }
481
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800482 bh_unlock_sock(sk);
483out:
Eric Dumazetc3f24cf2016-11-02 17:14:41 -0700484 if (refcounted)
485 sock_put(sk);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800486 return rc;
487discard_and_relse:
488 kfree_skb(skb);
489 goto out;
490}
Willem de Bruijn4f0c40d92016-07-12 18:18:57 -0400491EXPORT_SYMBOL(__sk_receive_skb);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800492
493struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
494{
Eric Dumazetb6c67122010-04-08 23:03:29 +0000495 struct dst_entry *dst = __sk_dst_get(sk);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800496
497 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
Krishna Kumare022f0b2009-10-19 23:46:20 +0000498 sk_tx_queue_clear(sk);
Julian Anastasov9b8805a2017-02-06 23:14:11 +0200499 sk->sk_dst_pending_confirm = 0;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000500 RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
Denis Vlasenkof0088a52006-03-28 01:08:21 -0800501 dst_release(dst);
502 return NULL;
503 }
504
505 return dst;
506}
507EXPORT_SYMBOL(__sk_dst_check);
508
509struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
510{
511 struct dst_entry *dst = sk_dst_get(sk);
512
513 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
514 sk_dst_reset(sk);
515 dst_release(dst);
516 return NULL;
517 }
518
519 return dst;
520}
521EXPORT_SYMBOL(sk_dst_check);
522
Brian Haleyc91f6df2012-11-26 05:21:08 +0000523static int sock_setbindtodevice(struct sock *sk, char __user *optval,
524 int optlen)
David S. Miller48788092007-09-14 16:41:03 -0700525{
526 int ret = -ENOPROTOOPT;
527#ifdef CONFIG_NETDEVICES
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900528 struct net *net = sock_net(sk);
David S. Miller48788092007-09-14 16:41:03 -0700529 char devname[IFNAMSIZ];
530 int index;
531
532 /* Sorry... */
533 ret = -EPERM;
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000534 if (!ns_capable(net->user_ns, CAP_NET_RAW))
David S. Miller48788092007-09-14 16:41:03 -0700535 goto out;
536
537 ret = -EINVAL;
538 if (optlen < 0)
539 goto out;
540
541 /* Bind this socket to a particular device like "eth0",
542 * as specified in the passed interface name. If the
543 * name is "" or the option length is zero the socket
544 * is not bound.
545 */
546 if (optlen > IFNAMSIZ - 1)
547 optlen = IFNAMSIZ - 1;
548 memset(devname, 0, sizeof(devname));
549
550 ret = -EFAULT;
551 if (copy_from_user(devname, optval, optlen))
552 goto out;
553
David S. Miller000ba2e2009-11-05 22:37:11 -0800554 index = 0;
555 if (devname[0] != '\0') {
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800556 struct net_device *dev;
David S. Miller48788092007-09-14 16:41:03 -0700557
Eric Dumazetbf8e56b2009-11-05 21:03:39 -0800558 rcu_read_lock();
559 dev = dev_get_by_name_rcu(net, devname);
560 if (dev)
561 index = dev->ifindex;
562 rcu_read_unlock();
David S. Miller48788092007-09-14 16:41:03 -0700563 ret = -ENODEV;
564 if (!dev)
565 goto out;
David S. Miller48788092007-09-14 16:41:03 -0700566 }
567
568 lock_sock(sk);
569 sk->sk_bound_dev_if = index;
570 sk_dst_reset(sk);
571 release_sock(sk);
572
573 ret = 0;
574
575out:
576#endif
577
578 return ret;
579}
580
Brian Haleyc91f6df2012-11-26 05:21:08 +0000581static int sock_getbindtodevice(struct sock *sk, char __user *optval,
582 int __user *optlen, int len)
583{
584 int ret = -ENOPROTOOPT;
585#ifdef CONFIG_NETDEVICES
586 struct net *net = sock_net(sk);
Brian Haleyc91f6df2012-11-26 05:21:08 +0000587 char devname[IFNAMSIZ];
Brian Haleyc91f6df2012-11-26 05:21:08 +0000588
589 if (sk->sk_bound_dev_if == 0) {
590 len = 0;
591 goto zero;
592 }
593
594 ret = -EINVAL;
595 if (len < IFNAMSIZ)
596 goto out;
597
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200598 ret = netdev_get_name(net, devname, sk->sk_bound_dev_if);
599 if (ret)
Brian Haleyc91f6df2012-11-26 05:21:08 +0000600 goto out;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000601
602 len = strlen(devname) + 1;
603
604 ret = -EFAULT;
605 if (copy_to_user(optval, devname, len))
606 goto out;
607
608zero:
609 ret = -EFAULT;
610 if (put_user(len, optlen))
611 goto out;
612
613 ret = 0;
614
615out:
616#endif
617
618 return ret;
619}
620
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800621static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
622{
623 if (valbool)
624 sock_set_flag(sk, bit);
625 else
626 sock_reset_flag(sk, bit);
627}
628
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +0200629bool sk_mc_loop(struct sock *sk)
630{
631 if (dev_recursion_level())
632 return false;
633 if (!sk)
634 return true;
635 switch (sk->sk_family) {
636 case AF_INET:
637 return inet_sk(sk)->mc_loop;
638#if IS_ENABLED(CONFIG_IPV6)
639 case AF_INET6:
640 return inet6_sk(sk)->mc_loop;
641#endif
642 }
643 WARN_ON(1);
644 return true;
645}
646EXPORT_SYMBOL(sk_mc_loop);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/*
649 * This is meant for all protocols to use and covers goings on
650 * at the socket level. Everything here is generic.
651 */
652
653int sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700654 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Richard Cochran80b14de2018-07-03 15:42:48 -0700656 struct sock_txtime sk_txtime;
Eric Dumazet2a915252009-05-27 11:30:05 +0000657 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int val;
659 int valbool;
660 struct linger ling;
661 int ret = 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * Options without arguments
665 */
666
David S. Miller48788092007-09-14 16:41:03 -0700667 if (optname == SO_BINDTODEVICE)
Brian Haleyc91f6df2012-11-26 05:21:08 +0000668 return sock_setbindtodevice(sk, optval, optlen);
David S. Miller48788092007-09-14 16:41:03 -0700669
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700670 if (optlen < sizeof(int))
671 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (get_user(val, (int __user *)optval))
674 return -EFAULT;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900675
Eric Dumazet2a915252009-05-27 11:30:05 +0000676 valbool = val ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 lock_sock(sk);
679
Eric Dumazet2a915252009-05-27 11:30:05 +0000680 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700681 case SO_DEBUG:
Eric Dumazet2a915252009-05-27 11:30:05 +0000682 if (val && !capable(CAP_NET_ADMIN))
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700683 ret = -EACCES;
Eric Dumazet2a915252009-05-27 11:30:05 +0000684 else
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800685 sock_valbool_flag(sk, SOCK_DBG, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700686 break;
687 case SO_REUSEADDR:
Bart Van Asschecdb87442018-06-12 10:05:55 -0700688 sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700689 break;
Tom Herbert055dc212013-01-22 09:49:50 +0000690 case SO_REUSEPORT:
691 sk->sk_reuseport = valbool;
692 break;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700693 case SO_TYPE:
Jan Engelhardt49c794e2009-08-04 07:28:28 +0000694 case SO_PROTOCOL:
Jan Engelhardt0d6038e2009-08-04 07:28:29 +0000695 case SO_DOMAIN:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700696 case SO_ERROR:
697 ret = -ENOPROTOOPT;
698 break;
699 case SO_DONTROUTE:
Pavel Emelyanovc0ef8772007-11-15 03:03:19 -0800700 sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
yupeng50deccd2018-12-05 18:56:28 -0800701 sk_dst_reset(sk);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700702 break;
703 case SO_BROADCAST:
704 sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
705 break;
706 case SO_SNDBUF:
707 /* Don't error on this BSD doesn't and if you think
Eric Dumazet82981932012-04-26 20:07:59 +0000708 * about it this is right. Otherwise apps have to
709 * play 'guess the biggest size' games. RCVBUF/SNDBUF
710 * are treated in BSD as hints
711 */
712 val = min_t(u32, val, sysctl_wmem_max);
Patrick McHardyb0573de2005-08-09 19:30:51 -0700713set_sndbuf:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700714 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Eric Dumazetb98b0bc2016-12-02 09:44:53 -0800715 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
Eric Dumazet82981932012-04-26 20:07:59 +0000716 /* Wake up sending tasks if we upped the value. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700717 sk->sk_write_space(sk);
718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700720 case SO_SNDBUFFORCE:
721 if (!capable(CAP_NET_ADMIN)) {
722 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
724 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700725 goto set_sndbuf;
726
727 case SO_RCVBUF:
728 /* Don't error on this BSD doesn't and if you think
Eric Dumazet82981932012-04-26 20:07:59 +0000729 * about it this is right. Otherwise apps have to
730 * play 'guess the biggest size' games. RCVBUF/SNDBUF
731 * are treated in BSD as hints
732 */
733 val = min_t(u32, val, sysctl_rmem_max);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700734set_rcvbuf:
735 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
736 /*
737 * We double it on the way in to account for
738 * "struct sk_buff" etc. overhead. Applications
739 * assume that the SO_RCVBUF setting they make will
740 * allow that much actual data to be received on that
741 * socket.
742 *
743 * Applications are unaware that "struct sk_buff" and
744 * other overheads allocate from the receive buffer
745 * during socket buffer allocation.
746 *
747 * And after considering the possible alternatives,
748 * returning the value we actually used in getsockopt
749 * is the most desirable behavior.
750 */
Eric Dumazetb98b0bc2016-12-02 09:44:53 -0800751 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700752 break;
753
754 case SO_RCVBUFFORCE:
755 if (!capable(CAP_NET_ADMIN)) {
756 ret = -EPERM;
757 break;
758 }
759 goto set_rcvbuf;
760
761 case SO_KEEPALIVE:
Ursula Braun4b9d07a2017-01-09 16:55:12 +0100762 if (sk->sk_prot->keepalive)
763 sk->sk_prot->keepalive(sk, valbool);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700764 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
765 break;
766
767 case SO_OOBINLINE:
768 sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
769 break;
770
771 case SO_NO_CHECK:
Tom Herbert28448b82014-05-23 08:47:19 -0700772 sk->sk_no_check_tx = valbool;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700773 break;
774
775 case SO_PRIORITY:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000776 if ((val >= 0 && val <= 6) ||
777 ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700778 sk->sk_priority = val;
779 else
780 ret = -EPERM;
781 break;
782
783 case SO_LINGER:
784 if (optlen < sizeof(ling)) {
785 ret = -EINVAL; /* 1003.1g */
786 break;
787 }
Eric Dumazet2a915252009-05-27 11:30:05 +0000788 if (copy_from_user(&ling, optval, sizeof(ling))) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700789 ret = -EFAULT;
790 break;
791 }
792 if (!ling.l_onoff)
793 sock_reset_flag(sk, SOCK_LINGER);
794 else {
795#if (BITS_PER_LONG == 32)
796 if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
797 sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
798 else
799#endif
800 sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
801 sock_set_flag(sk, SOCK_LINGER);
802 }
803 break;
804
805 case SO_BSDCOMPAT:
806 sock_warn_obsolete_bsdism("setsockopt");
807 break;
808
809 case SO_PASSCRED:
810 if (valbool)
811 set_bit(SOCK_PASSCRED, &sock->flags);
812 else
813 clear_bit(SOCK_PASSCRED, &sock->flags);
814 break;
815
816 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700817 case SO_TIMESTAMPNS:
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700818 if (valbool) {
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700819 if (optname == SO_TIMESTAMP)
820 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
821 else
822 sock_set_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700823 sock_set_flag(sk, SOCK_RCVTSTAMP);
Patrick Ohly20d49472009-02-12 05:03:38 +0000824 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700825 } else {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700826 sock_reset_flag(sk, SOCK_RCVTSTAMP);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700827 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
828 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700829 break;
830
Patrick Ohly20d49472009-02-12 05:03:38 +0000831 case SO_TIMESTAMPING:
832 if (val & ~SOF_TIMESTAMPING_MASK) {
Rémi Denis-Courmontf249fb72009-07-20 00:47:04 +0000833 ret = -EINVAL;
Patrick Ohly20d49472009-02-12 05:03:38 +0000834 break;
835 }
Willem de Bruijnb245be12015-01-30 13:29:32 -0500836
Willem de Bruijn09c2d252014-08-04 22:11:47 -0400837 if (val & SOF_TIMESTAMPING_OPT_ID &&
Willem de Bruijn4ed2d762014-08-04 22:11:49 -0400838 !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) {
WANG Congac5cc972015-12-16 23:39:04 -0800839 if (sk->sk_protocol == IPPROTO_TCP &&
840 sk->sk_type == SOCK_STREAM) {
Soheil Hassas Yeganeh6db8b962016-04-02 23:08:07 -0400841 if ((1 << sk->sk_state) &
842 (TCPF_CLOSE | TCPF_LISTEN)) {
Willem de Bruijn4ed2d762014-08-04 22:11:49 -0400843 ret = -EINVAL;
844 break;
845 }
846 sk->sk_tskey = tcp_sk(sk)->snd_una;
847 } else {
848 sk->sk_tskey = 0;
849 }
850 }
Francis Yan1c885802016-11-27 23:07:18 -0800851
852 if (val & SOF_TIMESTAMPING_OPT_STATS &&
853 !(val & SOF_TIMESTAMPING_OPT_TSONLY)) {
854 ret = -EINVAL;
855 break;
856 }
857
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400858 sk->sk_tsflags = val;
Patrick Ohly20d49472009-02-12 05:03:38 +0000859 if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
860 sock_enable_timestamp(sk,
861 SOCK_TIMESTAMPING_RX_SOFTWARE);
862 else
863 sock_disable_timestamp(sk,
Eric Dumazet08e29af2011-11-28 12:04:18 +0000864 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
Patrick Ohly20d49472009-02-12 05:03:38 +0000865 break;
866
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700867 case SO_RCVLOWAT:
868 if (val < 0)
869 val = INT_MAX;
Eric Dumazetd1361842018-04-16 10:33:35 -0700870 if (sock->ops->set_rcvlowat)
871 ret = sock->ops->set_rcvlowat(sk, val);
872 else
873 sk->sk_rcvlowat = val ? : 1;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700874 break;
875
876 case SO_RCVTIMEO:
877 ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
878 break;
879
880 case SO_SNDTIMEO:
881 ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
882 break;
883
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700884 case SO_ATTACH_FILTER:
885 ret = -EINVAL;
886 if (optlen == sizeof(struct sock_fprog)) {
887 struct sock_fprog fprog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700889 ret = -EFAULT;
890 if (copy_from_user(&fprog, optval, sizeof(fprog)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700893 ret = sk_attach_filter(&fprog, sk);
894 }
895 break;
896
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800897 case SO_ATTACH_BPF:
898 ret = -EINVAL;
899 if (optlen == sizeof(u32)) {
900 u32 ufd;
901
902 ret = -EFAULT;
903 if (copy_from_user(&ufd, optval, sizeof(ufd)))
904 break;
905
906 ret = sk_attach_bpf(ufd, sk);
907 }
908 break;
909
Craig Gallek538950a2016-01-04 17:41:47 -0500910 case SO_ATTACH_REUSEPORT_CBPF:
911 ret = -EINVAL;
912 if (optlen == sizeof(struct sock_fprog)) {
913 struct sock_fprog fprog;
914
915 ret = -EFAULT;
916 if (copy_from_user(&fprog, optval, sizeof(fprog)))
917 break;
918
919 ret = sk_reuseport_attach_filter(&fprog, sk);
920 }
921 break;
922
923 case SO_ATTACH_REUSEPORT_EBPF:
924 ret = -EINVAL;
925 if (optlen == sizeof(u32)) {
926 u32 ufd;
927
928 ret = -EFAULT;
929 if (copy_from_user(&ufd, optval, sizeof(ufd)))
930 break;
931
932 ret = sk_reuseport_attach_bpf(ufd, sk);
933 }
934 break;
935
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700936 case SO_DETACH_FILTER:
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700937 ret = sk_detach_filter(sk);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700938 break;
939
Vincent Bernatd59577b2013-01-16 22:55:49 +0100940 case SO_LOCK_FILTER:
941 if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool)
942 ret = -EPERM;
943 else
944 sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool);
945 break;
946
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700947 case SO_PASSSEC:
948 if (valbool)
949 set_bit(SOCK_PASSSEC, &sock->flags);
950 else
951 clear_bit(SOCK_PASSSEC, &sock->flags);
952 break;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800953 case SO_MARK:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000954 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800955 ret = -EPERM;
Eric Dumazet2a915252009-05-27 11:30:05 +0000956 else
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800957 sk->sk_mark = val;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800958 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700959
Neil Horman3b885782009-10-12 13:26:31 -0700960 case SO_RXQ_OVFL:
Johannes Berg8083f0f2011-10-07 03:30:20 +0000961 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
Neil Horman3b885782009-10-12 13:26:31 -0700962 break;
Johannes Berg6e3e9392011-11-09 10:15:42 +0100963
964 case SO_WIFI_STATUS:
965 sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
966 break;
967
Pavel Emelyanovef64a542012-02-21 07:31:34 +0000968 case SO_PEEK_OFF:
969 if (sock->ops->set_peek_off)
Sasha Levin12663bf2013-12-07 17:26:27 -0500970 ret = sock->ops->set_peek_off(sk, val);
Pavel Emelyanovef64a542012-02-21 07:31:34 +0000971 else
972 ret = -EOPNOTSUPP;
973 break;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000974
975 case SO_NOFCS:
976 sock_valbool_flag(sk, SOCK_NOFCS, valbool);
977 break;
978
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +0000979 case SO_SELECT_ERR_QUEUE:
980 sock_valbool_flag(sk, SOCK_SELECT_ERR_QUEUE, valbool);
981 break;
982
Cong Wange0d10952013-08-01 11:10:25 +0800983#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300984 case SO_BUSY_POLL:
Eliezer Tamirdafcc432013-06-14 16:33:57 +0300985 /* allow unprivileged users to decrease the value */
986 if ((val > sk->sk_ll_usec) && !capable(CAP_NET_ADMIN))
987 ret = -EPERM;
988 else {
989 if (val < 0)
990 ret = -EINVAL;
991 else
992 sk->sk_ll_usec = val;
993 }
994 break;
995#endif
Eric Dumazet62748f32013-09-24 08:20:52 -0700996
997 case SO_MAX_PACING_RATE:
Eric Dumazet218af592017-05-16 04:24:36 -0700998 if (val != ~0U)
999 cmpxchg(&sk->sk_pacing_status,
1000 SK_PACING_NONE,
1001 SK_PACING_NEEDED);
Eric Dumazet62748f32013-09-24 08:20:52 -07001002 sk->sk_max_pacing_rate = val;
1003 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
1004 sk->sk_max_pacing_rate);
1005 break;
1006
Eric Dumazet70da2682015-10-08 19:33:21 -07001007 case SO_INCOMING_CPU:
1008 sk->sk_incoming_cpu = val;
1009 break;
1010
Tom Herberta87cb3e2016-02-24 10:02:52 -08001011 case SO_CNX_ADVICE:
1012 if (val == 1)
1013 dst_negative_advice(sk);
1014 break;
Willem de Bruijn76851d12017-08-03 16:29:40 -04001015
1016 case SO_ZEROCOPY:
Sowmini Varadhan28190752018-02-15 10:49:34 -08001017 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6) {
1018 if (sk->sk_protocol != IPPROTO_TCP)
1019 ret = -ENOTSUPP;
Sowmini Varadhan28190752018-02-15 10:49:34 -08001020 } else if (sk->sk_family != PF_RDS) {
Willem de Bruijn76851d12017-08-03 16:29:40 -04001021 ret = -ENOTSUPP;
Sowmini Varadhan28190752018-02-15 10:49:34 -08001022 }
1023 if (!ret) {
1024 if (val < 0 || val > 1)
1025 ret = -EINVAL;
1026 else
1027 sock_valbool_flag(sk, SOCK_ZEROCOPY, valbool);
Sowmini Varadhan28190752018-02-15 10:49:34 -08001028 }
Jesus Sanchez-Palencia334e6412018-03-07 09:40:57 -08001029 break;
1030
Richard Cochran80b14de2018-07-03 15:42:48 -07001031 case SO_TXTIME:
1032 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1033 ret = -EPERM;
1034 } else if (optlen != sizeof(struct sock_txtime)) {
1035 ret = -EINVAL;
1036 } else if (copy_from_user(&sk_txtime, optval,
1037 sizeof(struct sock_txtime))) {
1038 ret = -EFAULT;
1039 } else if (sk_txtime.flags & ~SOF_TXTIME_FLAGS_MASK) {
1040 ret = -EINVAL;
1041 } else {
1042 sock_valbool_flag(sk, SOCK_TXTIME, true);
1043 sk->sk_clockid = sk_txtime.clockid;
1044 sk->sk_txtime_deadline_mode =
1045 !!(sk_txtime.flags & SOF_TXTIME_DEADLINE_MODE);
Jesus Sanchez-Palencia4b15c702018-07-03 15:43:00 -07001046 sk->sk_txtime_report_errors =
1047 !!(sk_txtime.flags & SOF_TXTIME_REPORT_ERRORS);
Richard Cochran80b14de2018-07-03 15:42:48 -07001048 }
1049 break;
1050
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001051 default:
1052 ret = -ENOPROTOOPT;
1053 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 release_sock(sk);
1056 return ret;
1057}
Eric Dumazet2a915252009-05-27 11:30:05 +00001058EXPORT_SYMBOL(sock_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060
stephen hemminger8f098982014-01-03 09:17:14 -08001061static void cred_to_ucred(struct pid *pid, const struct cred *cred,
1062 struct ucred *ucred)
Eric W. Biederman3f551f92010-06-13 03:28:59 +00001063{
1064 ucred->pid = pid_vnr(pid);
1065 ucred->uid = ucred->gid = -1;
1066 if (cred) {
1067 struct user_namespace *current_ns = current_user_ns();
1068
Eric W. Biedermanb2e4f542012-05-23 16:39:45 -06001069 ucred->uid = from_kuid_munged(current_ns, cred->euid);
1070 ucred->gid = from_kgid_munged(current_ns, cred->egid);
Eric W. Biederman3f551f92010-06-13 03:28:59 +00001071 }
1072}
1073
David Herrmann28b5ba2a2017-06-21 10:47:15 +02001074static int groups_to_user(gid_t __user *dst, const struct group_info *src)
1075{
1076 struct user_namespace *user_ns = current_user_ns();
1077 int i;
1078
1079 for (i = 0; i < src->ngroups; i++)
1080 if (put_user(from_kgid_munged(user_ns, src->gid[i]), dst + i))
1081 return -EFAULT;
1082
1083 return 0;
1084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086int sock_getsockopt(struct socket *sock, int level, int optname,
1087 char __user *optval, int __user *optlen)
1088{
1089 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001090
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001091 union {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001092 int val;
Chenbo Feng5daab9d2017-04-05 19:00:55 -07001093 u64 val64;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001094 struct linger ling;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct timeval tm;
Richard Cochran80b14de2018-07-03 15:42:48 -07001096 struct sock_txtime txtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 } v;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001098
H Hartley Sweeten4d0392b2010-01-15 01:08:58 -08001099 int lv = sizeof(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int len;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001101
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001102 if (get_user(len, optlen))
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001103 return -EFAULT;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001104 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001106
Eugene Teo50fee1d2009-02-23 15:38:41 -08001107 memset(&v, 0, sizeof(v));
Clément Lecignedf0bca02009-02-12 16:59:09 -08001108
Eric Dumazet2a915252009-05-27 11:30:05 +00001109 switch (optname) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001110 case SO_DEBUG:
1111 v.val = sock_flag(sk, SOCK_DBG);
1112 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001113
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001114 case SO_DONTROUTE:
1115 v.val = sock_flag(sk, SOCK_LOCALROUTE);
1116 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001117
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001118 case SO_BROADCAST:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001119 v.val = sock_flag(sk, SOCK_BROADCAST);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001122 case SO_SNDBUF:
1123 v.val = sk->sk_sndbuf;
1124 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001125
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001126 case SO_RCVBUF:
1127 v.val = sk->sk_rcvbuf;
1128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001130 case SO_REUSEADDR:
1131 v.val = sk->sk_reuse;
1132 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Tom Herbert055dc212013-01-22 09:49:50 +00001134 case SO_REUSEPORT:
1135 v.val = sk->sk_reuseport;
1136 break;
1137
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001138 case SO_KEEPALIVE:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001139 v.val = sock_flag(sk, SOCK_KEEPOPEN);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001140 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001142 case SO_TYPE:
1143 v.val = sk->sk_type;
1144 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Jan Engelhardt49c794e2009-08-04 07:28:28 +00001146 case SO_PROTOCOL:
1147 v.val = sk->sk_protocol;
1148 break;
1149
Jan Engelhardt0d6038e2009-08-04 07:28:29 +00001150 case SO_DOMAIN:
1151 v.val = sk->sk_family;
1152 break;
1153
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001154 case SO_ERROR:
1155 v.val = -sock_error(sk);
Eric Dumazet2a915252009-05-27 11:30:05 +00001156 if (v.val == 0)
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001157 v.val = xchg(&sk->sk_err_soft, 0);
1158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001160 case SO_OOBINLINE:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001161 v.val = sock_flag(sk, SOCK_URGINLINE);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001162 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001163
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001164 case SO_NO_CHECK:
Tom Herbert28448b82014-05-23 08:47:19 -07001165 v.val = sk->sk_no_check_tx;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001168 case SO_PRIORITY:
1169 v.val = sk->sk_priority;
1170 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001171
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001172 case SO_LINGER:
1173 lv = sizeof(v.ling);
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001174 v.ling.l_onoff = sock_flag(sk, SOCK_LINGER);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001175 v.ling.l_linger = sk->sk_lingertime / HZ;
1176 break;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001177
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001178 case SO_BSDCOMPAT:
1179 sock_warn_obsolete_bsdism("getsockopt");
1180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001182 case SO_TIMESTAMP:
Eric Dumazet92f37fd2007-03-25 22:14:49 -07001183 v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
1184 !sock_flag(sk, SOCK_RCVTSTAMPNS);
1185 break;
1186
1187 case SO_TIMESTAMPNS:
1188 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Patrick Ohly20d49472009-02-12 05:03:38 +00001191 case SO_TIMESTAMPING:
Willem de Bruijnb9f40e22014-08-04 22:11:46 -04001192 v.val = sk->sk_tsflags;
Patrick Ohly20d49472009-02-12 05:03:38 +00001193 break;
1194
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001195 case SO_RCVTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +00001196 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001197 if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
1198 v.tm.tv_sec = 0;
1199 v.tm.tv_usec = 0;
1200 } else {
1201 v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
Gao Feng8ccde4c2017-02-21 17:09:19 +08001202 v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * USEC_PER_SEC) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001204 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001206 case SO_SNDTIMEO:
Eric Dumazet2a915252009-05-27 11:30:05 +00001207 lv = sizeof(struct timeval);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001208 if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
1209 v.tm.tv_sec = 0;
1210 v.tm.tv_usec = 0;
1211 } else {
1212 v.tm.tv_sec = sk->sk_sndtimeo / HZ;
Gao Feng8ccde4c2017-02-21 17:09:19 +08001213 v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * USEC_PER_SEC) / HZ;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001214 }
1215 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001217 case SO_RCVLOWAT:
1218 v.val = sk->sk_rcvlowat;
1219 break;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07001220
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001221 case SO_SNDLOWAT:
Eric Dumazet2a915252009-05-27 11:30:05 +00001222 v.val = 1;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001225 case SO_PASSCRED:
Eric Dumazet82981932012-04-26 20:07:59 +00001226 v.val = !!test_bit(SOCK_PASSCRED, &sock->flags);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001227 break;
1228
1229 case SO_PEERCRED:
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001230 {
1231 struct ucred peercred;
1232 if (len > sizeof(peercred))
1233 len = sizeof(peercred);
1234 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
1235 if (copy_to_user(optval, &peercred, len))
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001236 return -EFAULT;
1237 goto lenout;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001238 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001239
David Herrmann28b5ba2a2017-06-21 10:47:15 +02001240 case SO_PEERGROUPS:
1241 {
1242 int ret, n;
1243
1244 if (!sk->sk_peer_cred)
1245 return -ENODATA;
1246
1247 n = sk->sk_peer_cred->group_info->ngroups;
1248 if (len < n * sizeof(gid_t)) {
1249 len = n * sizeof(gid_t);
1250 return put_user(len, optlen) ? -EFAULT : -ERANGE;
1251 }
1252 len = n * sizeof(gid_t);
1253
1254 ret = groups_to_user((gid_t __user *)optval,
1255 sk->sk_peer_cred->group_info);
1256 if (ret)
1257 return ret;
1258 goto lenout;
1259 }
1260
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001261 case SO_PEERNAME:
1262 {
1263 char address[128];
1264
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001265 lv = sock->ops->getname(sock, (struct sockaddr *)address, 2);
1266 if (lv < 0)
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001267 return -ENOTCONN;
1268 if (lv < len)
1269 return -EINVAL;
1270 if (copy_to_user(optval, address, len))
1271 return -EFAULT;
1272 goto lenout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001274
1275 /* Dubious BSD thing... Probably nobody even uses it, but
1276 * the UNIX standard wants it for whatever reason... -DaveM
1277 */
1278 case SO_ACCEPTCONN:
1279 v.val = sk->sk_state == TCP_LISTEN;
1280 break;
1281
1282 case SO_PASSSEC:
Eric Dumazet82981932012-04-26 20:07:59 +00001283 v.val = !!test_bit(SOCK_PASSSEC, &sock->flags);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001284 break;
1285
1286 case SO_PEERSEC:
1287 return security_socket_getpeersec_stream(sock, optval, optlen, len);
1288
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001289 case SO_MARK:
1290 v.val = sk->sk_mark;
1291 break;
1292
Neil Horman3b885782009-10-12 13:26:31 -07001293 case SO_RXQ_OVFL:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001294 v.val = sock_flag(sk, SOCK_RXQ_OVFL);
Neil Horman3b885782009-10-12 13:26:31 -07001295 break;
1296
Johannes Berg6e3e9392011-11-09 10:15:42 +01001297 case SO_WIFI_STATUS:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001298 v.val = sock_flag(sk, SOCK_WIFI_STATUS);
Johannes Berg6e3e9392011-11-09 10:15:42 +01001299 break;
1300
Pavel Emelyanovef64a542012-02-21 07:31:34 +00001301 case SO_PEEK_OFF:
1302 if (!sock->ops->set_peek_off)
1303 return -EOPNOTSUPP;
1304
1305 v.val = sk->sk_peek_off;
1306 break;
David S. Millerbc2f7992012-02-24 14:48:34 -05001307 case SO_NOFCS:
Eric Dumazet1b23a5d2012-05-16 05:57:07 +00001308 v.val = sock_flag(sk, SOCK_NOFCS);
David S. Millerbc2f7992012-02-24 14:48:34 -05001309 break;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001310
Pavel Emelyanovf7b86bf2012-10-18 23:55:56 +00001311 case SO_BINDTODEVICE:
Brian Haleyc91f6df2012-11-26 05:21:08 +00001312 return sock_getbindtodevice(sk, optval, optlen, len);
1313
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001314 case SO_GET_FILTER:
1315 len = sk_get_filter(sk, (struct sock_filter __user *)optval, len);
1316 if (len < 0)
1317 return len;
1318
1319 goto lenout;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001320
Vincent Bernatd59577b2013-01-16 22:55:49 +01001321 case SO_LOCK_FILTER:
1322 v.val = sock_flag(sk, SOCK_FILTER_LOCKED);
1323 break;
1324
Michal Sekletarea02f942014-01-17 17:09:45 +01001325 case SO_BPF_EXTENSIONS:
1326 v.val = bpf_tell_extensions();
1327 break;
1328
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00001329 case SO_SELECT_ERR_QUEUE:
1330 v.val = sock_flag(sk, SOCK_SELECT_ERR_QUEUE);
1331 break;
1332
Cong Wange0d10952013-08-01 11:10:25 +08001333#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +03001334 case SO_BUSY_POLL:
Eliezer Tamirdafcc432013-06-14 16:33:57 +03001335 v.val = sk->sk_ll_usec;
1336 break;
1337#endif
1338
Eric Dumazet62748f32013-09-24 08:20:52 -07001339 case SO_MAX_PACING_RATE:
1340 v.val = sk->sk_max_pacing_rate;
1341 break;
1342
Eric Dumazet2c8c56e2014-11-11 05:54:28 -08001343 case SO_INCOMING_CPU:
1344 v.val = sk->sk_incoming_cpu;
1345 break;
1346
Josh Hunta2d133b2017-03-20 15:22:03 -04001347 case SO_MEMINFO:
1348 {
1349 u32 meminfo[SK_MEMINFO_VARS];
1350
1351 if (get_user(len, optlen))
1352 return -EFAULT;
1353
1354 sk_get_meminfo(sk, meminfo);
1355
1356 len = min_t(unsigned int, len, sizeof(meminfo));
1357 if (copy_to_user(optval, &meminfo, len))
1358 return -EFAULT;
1359
1360 goto lenout;
1361 }
Sridhar Samudrala6d433902017-03-24 10:08:36 -07001362
1363#ifdef CONFIG_NET_RX_BUSY_POLL
1364 case SO_INCOMING_NAPI_ID:
1365 v.val = READ_ONCE(sk->sk_napi_id);
1366
1367 /* aggregate non-NAPI IDs down to 0 */
1368 if (v.val < MIN_NAPI_ID)
1369 v.val = 0;
1370
1371 break;
1372#endif
1373
Chenbo Feng5daab9d2017-04-05 19:00:55 -07001374 case SO_COOKIE:
1375 lv = sizeof(u64);
1376 if (len < lv)
1377 return -EINVAL;
1378 v.val64 = sock_gen_cookie(sk);
1379 break;
1380
Willem de Bruijn76851d12017-08-03 16:29:40 -04001381 case SO_ZEROCOPY:
1382 v.val = sock_flag(sk, SOCK_ZEROCOPY);
1383 break;
1384
Richard Cochran80b14de2018-07-03 15:42:48 -07001385 case SO_TXTIME:
1386 lv = sizeof(v.txtime);
1387 v.txtime.clockid = sk->sk_clockid;
1388 v.txtime.flags |= sk->sk_txtime_deadline_mode ?
1389 SOF_TXTIME_DEADLINE_MODE : 0;
Jesus Sanchez-Palencia4b15c702018-07-03 15:43:00 -07001390 v.txtime.flags |= sk->sk_txtime_report_errors ?
1391 SOF_TXTIME_REPORT_ERRORS : 0;
Richard Cochran80b14de2018-07-03 15:42:48 -07001392 break;
1393
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001394 default:
YOSHIFUJI Hideaki/吉藤英明443b5992015-03-23 18:04:13 +09001395 /* We implement the SO_SNDLOWAT etc to not be settable
1396 * (1003.1g 7).
1397 */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001398 return -ENOPROTOOPT;
1399 }
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (len > lv)
1402 len = lv;
1403 if (copy_to_user(optval, &v, len))
1404 return -EFAULT;
1405lenout:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001406 if (put_user(len, optlen))
1407 return -EFAULT;
1408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001411/*
1412 * Initialize an sk_lock.
1413 *
1414 * (We also register the sk_lock with the lock validator.)
1415 */
Dave Jonesb6f99a22007-03-22 12:27:49 -07001416static inline void sock_lock_init(struct sock *sk)
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001417{
David Howellscdfbabf2017-03-09 08:09:05 +00001418 if (sk->sk_kern_sock)
1419 sock_lock_init_class_and_name(
1420 sk,
1421 af_family_kern_slock_key_strings[sk->sk_family],
1422 af_family_kern_slock_keys + sk->sk_family,
1423 af_family_kern_key_strings[sk->sk_family],
1424 af_family_kern_keys + sk->sk_family);
1425 else
1426 sock_lock_init_class_and_name(
1427 sk,
Peter Zijlstraed075362006-12-06 20:35:24 -08001428 af_family_slock_key_strings[sk->sk_family],
1429 af_family_slock_keys + sk->sk_family,
1430 af_family_key_strings[sk->sk_family],
1431 af_family_keys + sk->sk_family);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07001432}
1433
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001434/*
1435 * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
1436 * even temporarly, because of RCU lookups. sk_node should also be left as is.
Eric Dumazet68835ab2010-11-30 19:04:07 +00001437 * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001438 */
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001439static void sock_copy(struct sock *nsk, const struct sock *osk)
1440{
1441#ifdef CONFIG_SECURITY_NETWORK
1442 void *sptr = nsk->sk_security;
1443#endif
Eric Dumazet68835ab2010-11-30 19:04:07 +00001444 memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
1445
1446 memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
1447 osk->sk_prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
1448
Pavel Emelyanovf1a6c4d2007-11-01 00:29:45 -07001449#ifdef CONFIG_SECURITY_NETWORK
1450 nsk->sk_security = sptr;
1451 security_sk_clone(osk, nsk);
1452#endif
1453}
1454
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001455static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
1456 int family)
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001457{
1458 struct sock *sk;
1459 struct kmem_cache *slab;
1460
1461 slab = prot->slab;
Eric Dumazete912b112009-07-08 19:36:05 +00001462 if (slab != NULL) {
1463 sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
1464 if (!sk)
1465 return sk;
Eric Dumazetba2489b2016-08-23 11:39:29 -07001466 if (priority & __GFP_ZERO)
1467 sk_prot_clear_nulls(sk, prot->obj_size);
Octavian Purdilafcbdf092010-12-16 14:26:56 -08001468 } else
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001469 sk = kmalloc(prot->obj_size, priority);
1470
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001471 if (sk != NULL) {
1472 if (security_sk_alloc(sk, family, priority))
1473 goto out_free;
1474
1475 if (!try_module_get(prot->owner))
1476 goto out_free_sec;
Krishna Kumare022f0b2009-10-19 23:46:20 +00001477 sk_tx_queue_clear(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001478 }
1479
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001480 return sk;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001481
1482out_free_sec:
1483 security_sk_free(sk);
1484out_free:
1485 if (slab != NULL)
1486 kmem_cache_free(slab, sk);
1487 else
1488 kfree(sk);
1489 return NULL;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001490}
1491
1492static void sk_prot_free(struct proto *prot, struct sock *sk)
1493{
1494 struct kmem_cache *slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001495 struct module *owner;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001496
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001497 owner = prot->owner;
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001498 slab = prot->slab;
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001499
Tejun Heobd1060a2015-12-07 17:38:53 -05001500 cgroup_sk_free(&sk->sk_cgrp_data);
Johannes Weiner2d758072016-10-07 17:00:58 -07001501 mem_cgroup_sk_free(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001502 security_sk_free(sk);
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001503 if (slab != NULL)
1504 kmem_cache_free(slab, sk);
1505 else
1506 kfree(sk);
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001507 module_put(owner);
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510/**
1511 * sk_alloc - All socket objects are allocated here
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001512 * @net: the applicable net namespace
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001513 * @family: protocol family
1514 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1515 * @prot: struct proto associated with this new sock instance
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001516 * @kern: is this to be a kernel socket?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001518struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001519 struct proto *prot, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001521 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001523 sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (sk) {
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001525 sk->sk_family = family;
1526 /*
1527 * See comment in struct sock definition to understand
1528 * why we need sk_prot_creator -acme
1529 */
1530 sk->sk_prot = sk->sk_prot_creator = prot;
David Howellscdfbabf2017-03-09 08:09:05 +00001531 sk->sk_kern_sock = kern;
Pavel Emelyanov154adbc2007-11-01 00:38:43 -07001532 sock_lock_init(sk);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001533 sk->sk_net_refcnt = kern ? 0 : 1;
Tonghao Zhang648845a2017-12-14 05:51:58 -08001534 if (likely(sk->sk_net_refcnt)) {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001535 get_net(net);
Tonghao Zhang648845a2017-12-14 05:51:58 -08001536 sock_inuse_add(net, 1);
1537 }
1538
Eric W. Biederman26abe142015-05-08 21:10:31 -05001539 sock_net_set(sk, net);
Reshetova, Elena14afee42017-06-30 13:08:00 +03001540 refcount_set(&sk->sk_wmem_alloc, 1);
Herbert Xuf8451722010-05-24 00:12:34 -07001541
Johannes Weiner2d758072016-10-07 17:00:58 -07001542 mem_cgroup_sk_alloc(sk);
Johannes Weinerd979a392016-09-19 14:44:38 -07001543 cgroup_sk_alloc(&sk->sk_cgrp_data);
Tejun Heo2a56a1f2015-12-07 17:38:52 -05001544 sock_update_classid(&sk->sk_cgrp_data);
1545 sock_update_netprioidx(&sk->sk_cgrp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Frank Filza79af592005-09-27 15:23:38 -07001547
Pavel Emelyanov2e4afe72007-11-01 00:36:26 -07001548 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
Eric Dumazet2a915252009-05-27 11:30:05 +00001550EXPORT_SYMBOL(sk_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Eric Dumazeta4298e42016-04-01 08:52:12 -07001552/* Sockets having SOCK_RCU_FREE will call this function after one RCU
1553 * grace period. This is the case for UDP sockets and TCP listeners.
1554 */
1555static void __sk_destruct(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Eric Dumazeta4298e42016-04-01 08:52:12 -07001557 struct sock *sk = container_of(head, struct sock, sk_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 struct sk_filter *filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 if (sk->sk_destruct)
1561 sk->sk_destruct(sk);
1562
Paul E. McKenneya898def2010-02-22 17:04:49 -08001563 filter = rcu_dereference_check(sk->sk_filter,
Reshetova, Elena14afee42017-06-30 13:08:00 +03001564 refcount_read(&sk->sk_wmem_alloc) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (filter) {
Pavel Emelyanov309dd5f2007-10-17 21:21:51 -07001566 sk_filter_uncharge(sk, filter);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001567 RCU_INIT_POINTER(sk->sk_filter, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Craig Gallek538950a2016-01-04 17:41:47 -05001569 if (rcu_access_pointer(sk->sk_reuseport_cb))
1570 reuseport_detach_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Eric Dumazet08e29af2011-11-28 12:04:18 +00001572 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 if (atomic_read(&sk->sk_omem_alloc))
Joe Perchese005d192012-05-16 19:58:40 +00001575 pr_debug("%s: optmem leakage (%d bytes) detected\n",
1576 __func__, atomic_read(&sk->sk_omem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Eric Dumazet22a0e182017-03-15 13:21:28 -07001578 if (sk->sk_frag.page) {
1579 put_page(sk->sk_frag.page);
1580 sk->sk_frag.page = NULL;
1581 }
1582
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001583 if (sk->sk_peer_cred)
1584 put_cred(sk->sk_peer_cred);
1585 put_pid(sk->sk_peer_pid);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001586 if (likely(sk->sk_net_refcnt))
1587 put_net(sock_net(sk));
Pavel Emelyanovc308c1b22007-11-01 00:33:50 -07001588 sk_prot_free(sk->sk_prot_creator, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
Eric Dumazet2b85a342009-06-11 02:55:43 -07001590
Eric Dumazeta4298e42016-04-01 08:52:12 -07001591void sk_destruct(struct sock *sk)
1592{
1593 if (sock_flag(sk, SOCK_RCU_FREE))
1594 call_rcu(&sk->sk_rcu, __sk_destruct);
1595 else
1596 __sk_destruct(&sk->sk_rcu);
1597}
1598
Craig Gallekeb4cb002015-06-15 11:26:18 -04001599static void __sk_free(struct sock *sk)
1600{
Tonghao Zhang648845a2017-12-14 05:51:58 -08001601 if (likely(sk->sk_net_refcnt))
1602 sock_inuse_add(sock_net(sk), -1);
1603
Eric Dumazet97090202018-05-18 04:47:55 -07001604 if (unlikely(sk->sk_net_refcnt && sock_diag_has_destroy_listeners(sk)))
Craig Gallekeb4cb002015-06-15 11:26:18 -04001605 sock_diag_broadcast_destroy(sk);
1606 else
1607 sk_destruct(sk);
1608}
1609
Eric Dumazet2b85a342009-06-11 02:55:43 -07001610void sk_free(struct sock *sk)
1611{
1612 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001613 * We subtract one from sk_wmem_alloc and can know if
Eric Dumazet2b85a342009-06-11 02:55:43 -07001614 * some packets are still in some tx queue.
1615 * If not null, sock_wfree() will call __sk_free(sk) later
1616 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001617 if (refcount_dec_and_test(&sk->sk_wmem_alloc))
Eric Dumazet2b85a342009-06-11 02:55:43 -07001618 __sk_free(sk);
1619}
Eric Dumazet2a915252009-05-27 11:30:05 +00001620EXPORT_SYMBOL(sk_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Paolo Abeni581319c2017-03-09 13:54:08 +01001622static void sk_init_common(struct sock *sk)
1623{
1624 skb_queue_head_init(&sk->sk_receive_queue);
1625 skb_queue_head_init(&sk->sk_write_queue);
1626 skb_queue_head_init(&sk->sk_error_queue);
1627
1628 rwlock_init(&sk->sk_callback_lock);
1629 lockdep_set_class_and_name(&sk->sk_receive_queue.lock,
1630 af_rlock_keys + sk->sk_family,
1631 af_family_rlock_key_strings[sk->sk_family]);
1632 lockdep_set_class_and_name(&sk->sk_write_queue.lock,
1633 af_wlock_keys + sk->sk_family,
1634 af_family_wlock_key_strings[sk->sk_family]);
1635 lockdep_set_class_and_name(&sk->sk_error_queue.lock,
1636 af_elock_keys + sk->sk_family,
1637 af_family_elock_key_strings[sk->sk_family]);
1638 lockdep_set_class_and_name(&sk->sk_callback_lock,
1639 af_callback_keys + sk->sk_family,
1640 af_family_clock_key_strings[sk->sk_family]);
1641}
1642
Eric Dumazete56c57d2011-11-08 17:07:07 -05001643/**
1644 * sk_clone_lock - clone a socket, and lock its clone
1645 * @sk: the socket to clone
1646 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1647 *
1648 * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
1649 */
1650struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001651{
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001652 struct sock *newsk;
Alexei Starovoitov278571b2014-07-30 20:34:12 -07001653 bool is_charged = true;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001654
Pavel Emelyanov8fd1d172007-11-01 00:37:32 -07001655 newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001656 if (newsk != NULL) {
1657 struct sk_filter *filter;
1658
Venkat Yekkirala892c1412006-08-04 23:08:56 -07001659 sock_copy(newsk, sk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001660
Christoph Paasch9d538fa2017-09-26 17:38:50 -07001661 newsk->sk_prot_creator = sk->sk_prot;
1662
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001663 /* SANITY */
Sowmini Varadhan8a681732015-07-30 15:50:36 +02001664 if (likely(newsk->sk_net_refcnt))
1665 get_net(sock_net(newsk));
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001666 sk_node_init(&newsk->sk_node);
1667 sock_lock_init(newsk);
1668 bh_lock_sock(newsk);
Eric Dumazetfa438cc2007-03-04 16:05:44 -08001669 newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
Zhu Yi8eae9392010-03-04 18:01:40 +00001670 newsk->sk_backlog.len = 0;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001671
1672 atomic_set(&newsk->sk_rmem_alloc, 0);
Eric Dumazet2b85a342009-06-11 02:55:43 -07001673 /*
1674 * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
1675 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001676 refcount_set(&newsk->sk_wmem_alloc, 1);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001677 atomic_set(&newsk->sk_omem_alloc, 0);
Paolo Abeni581319c2017-03-09 13:54:08 +01001678 sk_init_common(newsk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001679
1680 newsk->sk_dst_cache = NULL;
Julian Anastasov9b8805a2017-02-06 23:14:11 +02001681 newsk->sk_dst_pending_confirm = 0;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001682 newsk->sk_wmem_queued = 0;
1683 newsk->sk_forward_alloc = 0;
Eric Dumazet9caad862016-04-01 08:52:20 -07001684 atomic_set(&newsk->sk_drops, 0);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001685 newsk->sk_send_head = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001686 newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
Willem de Bruijn52267792017-08-03 16:29:39 -04001687 atomic_set(&newsk->sk_zckey, 0);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001688
1689 sock_reset_flag(newsk, SOCK_DONE);
Roman Gushchinedbe69e2018-02-02 15:26:57 +00001690 mem_cgroup_sk_alloc(newsk);
Eric Dumazetc0576e32017-10-10 19:12:33 -07001691 cgroup_sk_alloc(&newsk->sk_cgrp_data);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001692
Eric Dumazeteefca202017-10-02 12:20:51 -07001693 rcu_read_lock();
1694 filter = rcu_dereference(sk->sk_filter);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001695 if (filter != NULL)
Alexei Starovoitov278571b2014-07-30 20:34:12 -07001696 /* though it's an empty new sock, the charging may fail
1697 * if sysctl_optmem_max was changed between creation of
1698 * original socket and cloning
1699 */
1700 is_charged = sk_filter_charge(newsk, filter);
Eric Dumazeteefca202017-10-02 12:20:51 -07001701 RCU_INIT_POINTER(newsk->sk_filter, filter);
1702 rcu_read_unlock();
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001703
Eric Dumazetd188ba82015-12-08 07:22:02 -08001704 if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
Daniel Borkmanna97e50c2017-03-22 13:08:08 +01001705 /* We need to make sure that we don't uncharge the new
1706 * socket if we couldn't charge it in the first place
1707 * as otherwise we uncharge the parent's filter.
1708 */
1709 if (!is_charged)
1710 RCU_INIT_POINTER(newsk->sk_filter, NULL);
Arnaldo Carvalho de Melo94352d42017-03-01 16:35:08 -03001711 sk_free_unlock_clone(newsk);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001712 newsk = NULL;
1713 goto out;
1714 }
Craig Gallekfa463492016-02-10 11:50:39 -05001715 RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001716
1717 newsk->sk_err = 0;
Eric Dumazete551c322016-10-28 13:40:24 -07001718 newsk->sk_err_soft = 0;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001719 newsk->sk_priority = 0;
Eric Dumazet2c8c56e2014-11-11 05:54:28 -08001720 newsk->sk_incoming_cpu = raw_smp_processor_id();
Eric Dumazet33cf7c92015-03-11 18:53:14 -07001721 atomic64_set(&newsk->sk_cookie, 0);
Tonghao Zhang648845a2017-12-14 05:51:58 -08001722 if (likely(newsk->sk_net_refcnt))
1723 sock_inuse_add(sock_net(newsk), 1);
Johannes Weinerd979a392016-09-19 14:44:38 -07001724
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00001725 /*
1726 * Before updating sk_refcnt, we must commit prior changes to memory
1727 * (Documentation/RCU/rculist_nulls.txt for details)
1728 */
1729 smp_wmb();
Reshetova, Elena41c6d652017-06-30 13:08:01 +03001730 refcount_set(&newsk->sk_refcnt, 2);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001731
1732 /*
1733 * Increment the counter in the same struct proto as the master
1734 * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
1735 * is the same as sk->sk_prot->socks, as this field was copied
1736 * with memcpy).
1737 *
1738 * This _changes_ the previous behaviour, where
1739 * tcp_create_openreq_child always was incrementing the
1740 * equivalent to tcp_prot->socks (inet_sock_nr), so this have
1741 * to be taken into account in all callers. -acme
1742 */
1743 sk_refcnt_debug_inc(newsk);
David S. Miller972692e2008-06-17 22:41:38 -07001744 sk_set_socket(newsk, NULL);
Eric Dumazet43815482010-04-29 11:01:49 +00001745 newsk->sk_wq = NULL;
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001746
1747 if (newsk->sk_prot->sockets_allocated)
Glauber Costa180d8cd2011-12-11 21:47:02 +00001748 sk_sockets_allocated_inc(newsk);
Octavian Purdila704da5602010-01-08 00:00:09 -08001749
Hannes Frederic Sowa080a2702015-10-26 13:51:37 +01001750 if (sock_needs_netstamp(sk) &&
1751 newsk->sk_flags & SK_FLAGS_TIMESTAMP)
Octavian Purdila704da5602010-01-08 00:00:09 -08001752 net_enable_timestamp();
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001753 }
1754out:
1755 return newsk;
1756}
Eric Dumazete56c57d2011-11-08 17:07:07 -05001757EXPORT_SYMBOL_GPL(sk_clone_lock);
Arnaldo Carvalho de Melo87d11ce2005-08-09 20:10:12 -07001758
Arnaldo Carvalho de Melo94352d42017-03-01 16:35:08 -03001759void sk_free_unlock_clone(struct sock *sk)
1760{
1761 /* It is still raw copy of parent, so invalidate
1762 * destructor and make plain sk_free() */
1763 sk->sk_destruct = NULL;
1764 bh_unlock_sock(sk);
1765 sk_free(sk);
1766}
1767EXPORT_SYMBOL_GPL(sk_free_unlock_clone);
1768
Andi Kleen99580892007-04-20 17:12:43 -07001769void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
1770{
Eric Dumazetd6a4e262015-05-26 08:55:28 -07001771 u32 max_segs = 1;
1772
Eric Dumazet6bd4f352015-12-02 21:53:57 -08001773 sk_dst_set(sk, dst);
Eric Dumazet0a6b2a12018-02-19 11:56:47 -08001774 sk->sk_route_caps = dst->dev->features | sk->sk_route_forced_caps;
Andi Kleen99580892007-04-20 17:12:43 -07001775 if (sk->sk_route_caps & NETIF_F_GSO)
Herbert Xu4fcd6b92007-05-31 22:15:50 -07001776 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
Eric Dumazeta4654192010-05-16 00:36:33 -07001777 sk->sk_route_caps &= ~sk->sk_route_nocaps;
Andi Kleen99580892007-04-20 17:12:43 -07001778 if (sk_can_gso(sk)) {
Steffen Klassertf70f2502017-08-01 12:49:10 +03001779 if (dst->header_len && !xfrm_dst_offload_ok(dst)) {
Andi Kleen99580892007-04-20 17:12:43 -07001780 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001781 } else {
Andi Kleen99580892007-04-20 17:12:43 -07001782 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001783 sk->sk_gso_max_size = dst->dev->gso_max_size;
Eric Dumazetd6a4e262015-05-26 08:55:28 -07001784 max_segs = max_t(u32, dst->dev->gso_max_segs, 1);
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001785 }
Andi Kleen99580892007-04-20 17:12:43 -07001786 }
Eric Dumazetd6a4e262015-05-26 08:55:28 -07001787 sk->sk_gso_max_segs = max_segs;
Andi Kleen99580892007-04-20 17:12:43 -07001788}
1789EXPORT_SYMBOL_GPL(sk_setup_caps);
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791/*
1792 * Simple resource managers for sockets.
1793 */
1794
1795
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001796/*
1797 * Write buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
1799void sock_wfree(struct sk_buff *skb)
1800{
1801 struct sock *sk = skb->sk;
Eric Dumazetd99927f2009-09-24 10:49:24 +00001802 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Eric Dumazetd99927f2009-09-24 10:49:24 +00001804 if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
1805 /*
1806 * Keep a reference on sk_wmem_alloc, this will be released
1807 * after sk_write_space() call
1808 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001809 WARN_ON(refcount_sub_and_test(len - 1, &sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 sk->sk_write_space(sk);
Eric Dumazetd99927f2009-09-24 10:49:24 +00001811 len = 1;
1812 }
Eric Dumazet2b85a342009-06-11 02:55:43 -07001813 /*
Eric Dumazetd99927f2009-09-24 10:49:24 +00001814 * if sk_wmem_alloc reaches 0, we must finish what sk_free()
1815 * could not do because of in-flight packets
Eric Dumazet2b85a342009-06-11 02:55:43 -07001816 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001817 if (refcount_sub_and_test(len, &sk->sk_wmem_alloc))
Eric Dumazet2b85a342009-06-11 02:55:43 -07001818 __sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
Eric Dumazet2a915252009-05-27 11:30:05 +00001820EXPORT_SYMBOL(sock_wfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001822/* This variant of sock_wfree() is used by TCP,
1823 * since it sets SOCK_USE_WRITE_QUEUE.
1824 */
1825void __sock_wfree(struct sk_buff *skb)
1826{
1827 struct sock *sk = skb->sk;
1828
Reshetova, Elena14afee42017-06-30 13:08:00 +03001829 if (refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc))
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001830 __sk_free(sk);
1831}
1832
Eric Dumazet9e17f8a2015-11-01 15:36:55 -08001833void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1834{
1835 skb_orphan(skb);
1836 skb->sk = sk;
1837#ifdef CONFIG_INET
1838 if (unlikely(!sk_fullsock(sk))) {
1839 skb->destructor = sock_edemux;
1840 sock_hold(sk);
1841 return;
1842 }
1843#endif
1844 skb->destructor = sock_wfree;
1845 skb_set_hash_from_sk(skb, sk);
1846 /*
1847 * We used to take a refcount on sk, but following operation
1848 * is enough to guarantee sk_free() wont free this sock until
1849 * all in-flight packets are completed
1850 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03001851 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
Eric Dumazet9e17f8a2015-11-01 15:36:55 -08001852}
1853EXPORT_SYMBOL(skb_set_owner_w);
1854
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001855/* This helper is used by netem, as it can hold packets in its
1856 * delay queue. We want to allow the owner socket to send more
1857 * packets, as if they were already TX completed by a typical driver.
1858 * But we also want to keep skb->sk set because some packet schedulers
Eric Dumazetf6ba8d32017-05-11 15:24:41 -07001859 * rely on it (sch_fq for example).
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001860 */
Eric Dumazetf2f872f2013-07-30 17:55:08 -07001861void skb_orphan_partial(struct sk_buff *skb)
1862{
Eric Dumazetf6ba8d32017-05-11 15:24:41 -07001863 if (skb_is_tcp_pure_ack(skb))
Eric Dumazet1d2077a2016-05-02 10:56:27 -07001864 return;
1865
Eric Dumazetf2f872f2013-07-30 17:55:08 -07001866 if (skb->destructor == sock_wfree
1867#ifdef CONFIG_INET
1868 || skb->destructor == tcp_wfree
1869#endif
1870 ) {
Eric Dumazetf6ba8d32017-05-11 15:24:41 -07001871 struct sock *sk = skb->sk;
1872
Reshetova, Elena41c6d652017-06-30 13:08:01 +03001873 if (refcount_inc_not_zero(&sk->sk_refcnt)) {
Reshetova, Elena14afee42017-06-30 13:08:00 +03001874 WARN_ON(refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc));
Eric Dumazetf6ba8d32017-05-11 15:24:41 -07001875 skb->destructor = sock_efree;
1876 }
Eric Dumazetf2f872f2013-07-30 17:55:08 -07001877 } else {
1878 skb_orphan(skb);
1879 }
1880}
1881EXPORT_SYMBOL(skb_orphan_partial);
1882
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001883/*
1884 * Read buffer destructor automatically called from kfree_skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 */
1886void sock_rfree(struct sk_buff *skb)
1887{
1888 struct sock *sk = skb->sk;
Eric Dumazetd361fd52010-07-10 22:45:17 +00001889 unsigned int len = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Eric Dumazetd361fd52010-07-10 22:45:17 +00001891 atomic_sub(len, &sk->sk_rmem_alloc);
1892 sk_mem_uncharge(sk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
Eric Dumazet2a915252009-05-27 11:30:05 +00001894EXPORT_SYMBOL(sock_rfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Oliver Hartkopp7768eed2015-03-10 19:03:46 +01001896/*
1897 * Buffer destructor for skbs that are not used directly in read or write
1898 * path, e.g. for error handler skbs. Automatically called from kfree_skb.
1899 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04001900void sock_efree(struct sk_buff *skb)
1901{
1902 sock_put(skb->sk);
1903}
1904EXPORT_SYMBOL(sock_efree);
1905
Eric W. Biederman976d02012012-05-23 17:16:53 -06001906kuid_t sock_i_uid(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Eric W. Biederman976d02012012-05-23 17:16:53 -06001908 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Eric Dumazetf064af12010-09-22 12:43:39 +00001910 read_lock_bh(&sk->sk_callback_lock);
Eric W. Biederman976d02012012-05-23 17:16:53 -06001911 uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID;
Eric Dumazetf064af12010-09-22 12:43:39 +00001912 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return uid;
1914}
Eric Dumazet2a915252009-05-27 11:30:05 +00001915EXPORT_SYMBOL(sock_i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917unsigned long sock_i_ino(struct sock *sk)
1918{
1919 unsigned long ino;
1920
Eric Dumazetf064af12010-09-22 12:43:39 +00001921 read_lock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
Eric Dumazetf064af12010-09-22 12:43:39 +00001923 read_unlock_bh(&sk->sk_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return ino;
1925}
Eric Dumazet2a915252009-05-27 11:30:05 +00001926EXPORT_SYMBOL(sock_i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928/*
1929 * Allocate a skb from the socket's send buffer.
1930 */
Victor Fusco86a76ca2005-07-08 14:57:47 -07001931struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
Al Virodd0fc662005-10-07 07:46:04 +01001932 gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Reshetova, Elena14afee42017-06-30 13:08:00 +03001934 if (force || refcount_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
Eric Dumazet2a915252009-05-27 11:30:05 +00001935 struct sk_buff *skb = alloc_skb(size, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (skb) {
1937 skb_set_owner_w(skb, sk);
1938 return skb;
1939 }
1940 }
1941 return NULL;
1942}
Eric Dumazet2a915252009-05-27 11:30:05 +00001943EXPORT_SYMBOL(sock_wmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Willem de Bruijn98ba0bd2017-08-03 16:29:37 -04001945static void sock_ofree(struct sk_buff *skb)
1946{
1947 struct sock *sk = skb->sk;
1948
1949 atomic_sub(skb->truesize, &sk->sk_omem_alloc);
1950}
1951
1952struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
1953 gfp_t priority)
1954{
1955 struct sk_buff *skb;
1956
1957 /* small safe race: SKB_TRUESIZE may differ from final skb->truesize */
1958 if (atomic_read(&sk->sk_omem_alloc) + SKB_TRUESIZE(size) >
1959 sysctl_optmem_max)
1960 return NULL;
1961
1962 skb = alloc_skb(size, priority);
1963 if (!skb)
1964 return NULL;
1965
1966 atomic_add(skb->truesize, &sk->sk_omem_alloc);
1967 skb->sk = sk;
1968 skb->destructor = sock_ofree;
1969 return skb;
1970}
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 * Allocate a memory block from the socket's option memory buffer.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001974 */
Al Virodd0fc662005-10-07 07:46:04 +01001975void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Eric Dumazet95c96172012-04-15 05:58:06 +00001977 if ((unsigned int)size <= sysctl_optmem_max &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
1979 void *mem;
1980 /* First do the add, to avoid the race if kmalloc
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001981 * might sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 */
1983 atomic_add(size, &sk->sk_omem_alloc);
1984 mem = kmalloc(size, priority);
1985 if (mem)
1986 return mem;
1987 atomic_sub(size, &sk->sk_omem_alloc);
1988 }
1989 return NULL;
1990}
Eric Dumazet2a915252009-05-27 11:30:05 +00001991EXPORT_SYMBOL(sock_kmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Daniel Borkmann79e88652014-11-19 17:13:11 +01001993/* Free an option memory block. Note, we actually want the inline
1994 * here as this allows gcc to detect the nullify and fold away the
1995 * condition entirely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 */
Daniel Borkmann79e88652014-11-19 17:13:11 +01001997static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
1998 const bool nullify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
David S. Millere53da5f2014-10-14 17:02:37 -04002000 if (WARN_ON_ONCE(!mem))
2001 return;
Daniel Borkmann79e88652014-11-19 17:13:11 +01002002 if (nullify)
2003 kzfree(mem);
2004 else
2005 kfree(mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 atomic_sub(size, &sk->sk_omem_alloc);
2007}
Daniel Borkmann79e88652014-11-19 17:13:11 +01002008
2009void sock_kfree_s(struct sock *sk, void *mem, int size)
2010{
2011 __sock_kfree_s(sk, mem, size, false);
2012}
Eric Dumazet2a915252009-05-27 11:30:05 +00002013EXPORT_SYMBOL(sock_kfree_s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Daniel Borkmann79e88652014-11-19 17:13:11 +01002015void sock_kzfree_s(struct sock *sk, void *mem, int size)
2016{
2017 __sock_kfree_s(sk, mem, size, true);
2018}
2019EXPORT_SYMBOL(sock_kzfree_s);
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021/* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
2022 I think, these locks should be removed for datagram sockets.
2023 */
Eric Dumazet2a915252009-05-27 11:30:05 +00002024static long sock_wait_for_wmem(struct sock *sk, long timeo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
2026 DEFINE_WAIT(wait);
2027
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002028 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 for (;;) {
2030 if (!timeo)
2031 break;
2032 if (signal_pending(current))
2033 break;
2034 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Eric Dumazetaa395142010-04-20 13:03:51 +00002035 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Reshetova, Elena14afee42017-06-30 13:08:00 +03002036 if (refcount_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 break;
2038 if (sk->sk_shutdown & SEND_SHUTDOWN)
2039 break;
2040 if (sk->sk_err)
2041 break;
2042 timeo = schedule_timeout(timeo);
2043 }
Eric Dumazetaa395142010-04-20 13:03:51 +00002044 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return timeo;
2046}
2047
2048
2049/*
2050 * Generic send/receive buffer handlers
2051 */
2052
Herbert Xu4cc7f682009-02-04 16:55:54 -08002053struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
2054 unsigned long data_len, int noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002055 int *errcode, int max_page_order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002057 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 long timeo;
2059 int err;
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 timeo = sock_sndtimeo(sk, noblock);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002062 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 err = sock_error(sk);
2064 if (err != 0)
2065 goto failure;
2066
2067 err = -EPIPE;
2068 if (sk->sk_shutdown & SEND_SHUTDOWN)
2069 goto failure;
2070
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002071 if (sk_wmem_alloc_get(sk) < sk->sk_sndbuf)
2072 break;
Eric Dumazet28d64272013-08-08 14:38:47 -07002073
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002074 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002075 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2076 err = -EAGAIN;
2077 if (!timeo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 goto failure;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002079 if (signal_pending(current))
2080 goto interrupted;
2081 timeo = sock_wait_for_wmem(sk, timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Eric Dumazet2e4e4412014-09-17 04:49:49 -07002083 skb = alloc_skb_with_frags(header_len, data_len, max_page_order,
2084 errcode, sk->sk_allocation);
2085 if (skb)
2086 skb_set_owner_w(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return skb;
2088
2089interrupted:
2090 err = sock_intr_errno(timeo);
2091failure:
2092 *errcode = err;
2093 return NULL;
2094}
Herbert Xu4cc7f682009-02-04 16:55:54 -08002095EXPORT_SYMBOL(sock_alloc_send_pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002097struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 int noblock, int *errcode)
2099{
Eric Dumazet28d64272013-08-08 14:38:47 -07002100 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
Eric Dumazet2a915252009-05-27 11:30:05 +00002102EXPORT_SYMBOL(sock_alloc_send_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Willem de Bruijn39771b12016-04-02 23:08:06 -04002104int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,
2105 struct sockcm_cookie *sockc)
2106{
Soheil Hassas Yeganeh3dd17e62016-04-02 23:08:09 -04002107 u32 tsflags;
2108
Willem de Bruijn39771b12016-04-02 23:08:06 -04002109 switch (cmsg->cmsg_type) {
2110 case SO_MARK:
2111 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2112 return -EPERM;
2113 if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
2114 return -EINVAL;
2115 sockc->mark = *(u32 *)CMSG_DATA(cmsg);
2116 break;
Soheil Hassas Yeganeh3dd17e62016-04-02 23:08:09 -04002117 case SO_TIMESTAMPING:
2118 if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
2119 return -EINVAL;
2120
2121 tsflags = *(u32 *)CMSG_DATA(cmsg);
2122 if (tsflags & ~SOF_TIMESTAMPING_TX_RECORD_MASK)
2123 return -EINVAL;
2124
2125 sockc->tsflags &= ~SOF_TIMESTAMPING_TX_RECORD_MASK;
2126 sockc->tsflags |= tsflags;
2127 break;
Richard Cochran80b14de2018-07-03 15:42:48 -07002128 case SCM_TXTIME:
2129 if (!sock_flag(sk, SOCK_TXTIME))
2130 return -EINVAL;
2131 if (cmsg->cmsg_len != CMSG_LEN(sizeof(u64)))
2132 return -EINVAL;
2133 sockc->transmit_time = get_unaligned((u64 *)CMSG_DATA(cmsg));
2134 break;
Soheil Hassas Yeganeh779f1ed2016-07-11 16:51:26 -04002135 /* SCM_RIGHTS and SCM_CREDENTIALS are semantically in SOL_UNIX. */
2136 case SCM_RIGHTS:
2137 case SCM_CREDENTIALS:
2138 break;
Willem de Bruijn39771b12016-04-02 23:08:06 -04002139 default:
2140 return -EINVAL;
2141 }
2142 return 0;
2143}
2144EXPORT_SYMBOL(__sock_cmsg_send);
2145
Edward Jeef28ea362015-10-08 14:56:48 -07002146int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
2147 struct sockcm_cookie *sockc)
2148{
2149 struct cmsghdr *cmsg;
Willem de Bruijn39771b12016-04-02 23:08:06 -04002150 int ret;
Edward Jeef28ea362015-10-08 14:56:48 -07002151
2152 for_each_cmsghdr(cmsg, msg) {
2153 if (!CMSG_OK(msg, cmsg))
2154 return -EINVAL;
2155 if (cmsg->cmsg_level != SOL_SOCKET)
2156 continue;
Willem de Bruijn39771b12016-04-02 23:08:06 -04002157 ret = __sock_cmsg_send(sk, msg, cmsg, sockc);
2158 if (ret)
2159 return ret;
Edward Jeef28ea362015-10-08 14:56:48 -07002160 }
2161 return 0;
2162}
2163EXPORT_SYMBOL(sock_cmsg_send);
2164
Eric Dumazet06044752017-06-07 13:29:12 -07002165static void sk_enter_memory_pressure(struct sock *sk)
2166{
2167 if (!sk->sk_prot->enter_memory_pressure)
2168 return;
2169
2170 sk->sk_prot->enter_memory_pressure(sk);
2171}
2172
2173static void sk_leave_memory_pressure(struct sock *sk)
2174{
2175 if (sk->sk_prot->leave_memory_pressure) {
2176 sk->sk_prot->leave_memory_pressure(sk);
2177 } else {
2178 unsigned long *memory_pressure = sk->sk_prot->memory_pressure;
2179
2180 if (memory_pressure && *memory_pressure)
2181 *memory_pressure = 0;
2182 }
2183}
2184
Eric Dumazet5640f762012-09-23 23:04:42 +00002185/* On 32bit arches, an skb frag is limited to 2^15 */
2186#define SKB_FRAG_PAGE_ORDER get_order(32768)
2187
Eric Dumazet400dfd32013-10-17 16:27:07 -07002188/**
2189 * skb_page_frag_refill - check that a page_frag contains enough room
2190 * @sz: minimum size of the fragment we want to get
2191 * @pfrag: pointer to page_frag
Eric Dumazet82d5e2b2014-09-08 04:00:00 -07002192 * @gfp: priority for memory allocation
Eric Dumazet400dfd32013-10-17 16:27:07 -07002193 *
2194 * Note: While this allocator tries to use high order pages, there is
2195 * no guarantee that allocations succeed. Therefore, @sz MUST be
2196 * less or equal than PAGE_SIZE.
2197 */
Eric Dumazetd9b29382014-08-27 20:49:34 -07002198bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp)
Eric Dumazet5640f762012-09-23 23:04:42 +00002199{
Eric Dumazet5640f762012-09-23 23:04:42 +00002200 if (pfrag->page) {
Joonsoo Kimfe896d12016-03-17 14:19:26 -07002201 if (page_ref_count(pfrag->page) == 1) {
Eric Dumazet5640f762012-09-23 23:04:42 +00002202 pfrag->offset = 0;
2203 return true;
2204 }
Eric Dumazet400dfd32013-10-17 16:27:07 -07002205 if (pfrag->offset + sz <= pfrag->size)
Eric Dumazet5640f762012-09-23 23:04:42 +00002206 return true;
2207 put_page(pfrag->page);
2208 }
2209
Eric Dumazetd9b29382014-08-27 20:49:34 -07002210 pfrag->offset = 0;
2211 if (SKB_FRAG_PAGE_ORDER) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002212 /* Avoid direct reclaim but allow kswapd to wake */
2213 pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) |
2214 __GFP_COMP | __GFP_NOWARN |
2215 __GFP_NORETRY,
Eric Dumazetd9b29382014-08-27 20:49:34 -07002216 SKB_FRAG_PAGE_ORDER);
Eric Dumazet5640f762012-09-23 23:04:42 +00002217 if (likely(pfrag->page)) {
Eric Dumazetd9b29382014-08-27 20:49:34 -07002218 pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
Eric Dumazet5640f762012-09-23 23:04:42 +00002219 return true;
2220 }
Eric Dumazetd9b29382014-08-27 20:49:34 -07002221 }
2222 pfrag->page = alloc_page(gfp);
2223 if (likely(pfrag->page)) {
2224 pfrag->size = PAGE_SIZE;
2225 return true;
2226 }
Eric Dumazet400dfd32013-10-17 16:27:07 -07002227 return false;
2228}
2229EXPORT_SYMBOL(skb_page_frag_refill);
2230
2231bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
2232{
2233 if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation)))
2234 return true;
2235
Eric Dumazet5640f762012-09-23 23:04:42 +00002236 sk_enter_memory_pressure(sk);
2237 sk_stream_moderate_sndbuf(sk);
2238 return false;
2239}
2240EXPORT_SYMBOL(sk_page_frag_refill);
2241
John Fastabend2c3682f2018-03-18 12:56:49 -07002242int sk_alloc_sg(struct sock *sk, int len, struct scatterlist *sg,
John Fastabend8c05dbf2018-03-18 12:57:05 -07002243 int sg_start, int *sg_curr_index, unsigned int *sg_curr_size,
John Fastabend2c3682f2018-03-18 12:56:49 -07002244 int first_coalesce)
2245{
John Fastabend8c05dbf2018-03-18 12:57:05 -07002246 int sg_curr = *sg_curr_index, use = 0, rc = 0;
2247 unsigned int size = *sg_curr_size;
John Fastabend2c3682f2018-03-18 12:56:49 -07002248 struct page_frag *pfrag;
John Fastabend2c3682f2018-03-18 12:56:49 -07002249 struct scatterlist *sge;
John Fastabend2c3682f2018-03-18 12:56:49 -07002250
2251 len -= size;
2252 pfrag = sk_page_frag(sk);
2253
2254 while (len > 0) {
John Fastabend8c05dbf2018-03-18 12:57:05 -07002255 unsigned int orig_offset;
2256
John Fastabend2c3682f2018-03-18 12:56:49 -07002257 if (!sk_page_frag_refill(sk, pfrag)) {
2258 rc = -ENOMEM;
2259 goto out;
2260 }
2261
2262 use = min_t(int, len, pfrag->size - pfrag->offset);
2263
2264 if (!sk_wmem_schedule(sk, use)) {
2265 rc = -ENOMEM;
2266 goto out;
2267 }
2268
2269 sk_mem_charge(sk, use);
2270 size += use;
2271 orig_offset = pfrag->offset;
2272 pfrag->offset += use;
2273
John Fastabend8c05dbf2018-03-18 12:57:05 -07002274 sge = sg + sg_curr - 1;
Daniel Borkmann144fe2b2018-07-23 22:37:54 +02002275 if (sg_curr > first_coalesce && sg_page(sge) == pfrag->page &&
2276 sge->offset + sge->length == orig_offset) {
2277 sge->length += use;
John Fastabend2c3682f2018-03-18 12:56:49 -07002278 } else {
John Fastabend8c05dbf2018-03-18 12:57:05 -07002279 sge = sg + sg_curr;
John Fastabend2c3682f2018-03-18 12:56:49 -07002280 sg_unmark_end(sge);
2281 sg_set_page(sge, pfrag->page, use, orig_offset);
2282 get_page(pfrag->page);
John Fastabend8c05dbf2018-03-18 12:57:05 -07002283 sg_curr++;
2284
2285 if (sg_curr == MAX_SKB_FRAGS)
2286 sg_curr = 0;
2287
2288 if (sg_curr == sg_start) {
John Fastabend2c3682f2018-03-18 12:56:49 -07002289 rc = -ENOSPC;
2290 break;
2291 }
2292 }
2293
2294 len -= use;
2295 }
2296out:
John Fastabend8c05dbf2018-03-18 12:57:05 -07002297 *sg_curr_size = size;
2298 *sg_curr_index = sg_curr;
John Fastabend2c3682f2018-03-18 12:56:49 -07002299 return rc;
2300}
2301EXPORT_SYMBOL(sk_alloc_sg);
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303static void __lock_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00002304 __releases(&sk->sk_lock.slock)
2305 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
2307 DEFINE_WAIT(wait);
2308
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002309 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
2311 TASK_UNINTERRUPTIBLE);
2312 spin_unlock_bh(&sk->sk_lock.slock);
2313 schedule();
2314 spin_lock_bh(&sk->sk_lock.slock);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002315 if (!sock_owned_by_user(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 break;
2317 }
2318 finish_wait(&sk->sk_lock.wq, &wait);
2319}
2320
Eric Dumazetcddcc992018-10-01 23:24:26 -07002321void __release_sock(struct sock *sk)
Namhyung Kimf39234d2010-09-08 03:48:48 +00002322 __releases(&sk->sk_lock.slock)
2323 __acquires(&sk->sk_lock.slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002325 struct sk_buff *skb, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002327 while ((skb = sk->sk_backlog.head) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002329
2330 spin_unlock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 do {
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002333 next = skb->next;
Eric Dumazete4cbb022012-04-30 16:07:09 +00002334 prefetch(next);
Eric Dumazet7fee2262010-05-11 23:19:48 +00002335 WARN_ON_ONCE(skb_dst_is_noref(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 skb->next = NULL;
Peter Zijlstrac57943a2008-10-07 14:18:42 -07002337 sk_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002339 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 skb = next;
2342 } while (skb != NULL);
2343
Eric Dumazet5413d1b2016-04-29 14:16:52 -07002344 spin_lock_bh(&sk->sk_lock.slock);
2345 }
Zhu Yi8eae9392010-03-04 18:01:40 +00002346
2347 /*
2348 * Doing the zeroing here guarantee we can not loop forever
2349 * while a wild producer attempts to flood us.
2350 */
2351 sk->sk_backlog.len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
Eric Dumazetd41a69f2016-04-29 14:16:53 -07002354void __sk_flush_backlog(struct sock *sk)
2355{
2356 spin_lock_bh(&sk->sk_lock.slock);
2357 __release_sock(sk);
2358 spin_unlock_bh(&sk->sk_lock.slock);
2359}
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361/**
2362 * sk_wait_data - wait for data to arrive at sk_receive_queue
Pavel Pisa4dc3b162005-05-01 08:59:25 -07002363 * @sk: sock to wait on
2364 * @timeo: for how long
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +02002365 * @skb: last skb seen on sk_receive_queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 *
2367 * Now socket state including sk->sk_err is changed only under lock,
2368 * hence we may omit checks after joining wait queue.
2369 * We check receive queue before schedule() only as optimization;
2370 * it is very likely that release_sock() added new data.
2371 */
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +02002372int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
WANG Congd9dc8b02016-11-11 10:20:50 -08002374 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
WANG Congd9dc8b02016-11-11 10:20:50 -08002377 add_wait_queue(sk_sleep(sk), &wait);
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002378 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
WANG Congd9dc8b02016-11-11 10:20:50 -08002379 rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb, &wait);
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002380 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
WANG Congd9dc8b02016-11-11 10:20:50 -08002381 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return rc;
2383}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384EXPORT_SYMBOL(sk_wait_data);
2385
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002386/**
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002387 * __sk_mem_raise_allocated - increase memory_allocated
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002388 * @sk: socket
2389 * @size: memory size to allocate
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002390 * @amt: pages to allocate
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002391 * @kind: allocation type
2392 *
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002393 * Similar to __sk_mem_schedule(), but does not update sk_forward_alloc
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002394 */
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002395int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002396{
2397 struct proto *prot = sk->sk_prot;
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002398 long allocated = sk_memory_allocated_add(sk, amt);
Yafang Shaod6f19932018-07-01 23:31:30 +08002399 bool charged = true;
Johannes Weinere8056052016-01-14 15:21:14 -08002400
Johannes Weinerbaac50b2016-01-14 15:21:17 -08002401 if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
Yafang Shaod6f19932018-07-01 23:31:30 +08002402 !(charged = mem_cgroup_charge_skmem(sk->sk_memcg, amt)))
Johannes Weinere8056052016-01-14 15:21:14 -08002403 goto suppress_allocation;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002404
2405 /* Under limit. */
Johannes Weinere8056052016-01-14 15:21:14 -08002406 if (allocated <= sk_prot_mem_limits(sk, 0)) {
Glauber Costa180d8cd2011-12-11 21:47:02 +00002407 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002408 return 1;
2409 }
2410
Johannes Weinere8056052016-01-14 15:21:14 -08002411 /* Under pressure. */
2412 if (allocated > sk_prot_mem_limits(sk, 1))
Glauber Costa180d8cd2011-12-11 21:47:02 +00002413 sk_enter_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002414
Johannes Weinere8056052016-01-14 15:21:14 -08002415 /* Over hard limit. */
2416 if (allocated > sk_prot_mem_limits(sk, 2))
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002417 goto suppress_allocation;
2418
2419 /* guarantee minimum buffer size under pressure */
2420 if (kind == SK_MEM_RECV) {
Eric Dumazeta3dcaf12017-11-07 00:29:27 -08002421 if (atomic_read(&sk->sk_rmem_alloc) < sk_get_rmem0(sk, prot))
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002422 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00002423
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002424 } else { /* SK_MEM_SEND */
Eric Dumazeta3dcaf12017-11-07 00:29:27 -08002425 int wmem0 = sk_get_wmem0(sk, prot);
2426
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002427 if (sk->sk_type == SOCK_STREAM) {
Eric Dumazeta3dcaf12017-11-07 00:29:27 -08002428 if (sk->sk_wmem_queued < wmem0)
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002429 return 1;
Eric Dumazeta3dcaf12017-11-07 00:29:27 -08002430 } else if (refcount_read(&sk->sk_wmem_alloc) < wmem0) {
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002431 return 1;
Eric Dumazeta3dcaf12017-11-07 00:29:27 -08002432 }
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002433 }
2434
Glauber Costa180d8cd2011-12-11 21:47:02 +00002435 if (sk_has_memory_pressure(sk)) {
Eric Dumazet17483762008-11-25 21:16:35 -08002436 int alloc;
2437
Glauber Costa180d8cd2011-12-11 21:47:02 +00002438 if (!sk_under_memory_pressure(sk))
Eric Dumazet17483762008-11-25 21:16:35 -08002439 return 1;
Glauber Costa180d8cd2011-12-11 21:47:02 +00002440 alloc = sk_sockets_allocated_read_positive(sk);
2441 if (sk_prot_mem_limits(sk, 2) > alloc *
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002442 sk_mem_pages(sk->sk_wmem_queued +
2443 atomic_read(&sk->sk_rmem_alloc) +
2444 sk->sk_forward_alloc))
2445 return 1;
2446 }
2447
2448suppress_allocation:
2449
2450 if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
2451 sk_stream_moderate_sndbuf(sk);
2452
2453 /* Fail only if socket is _under_ its sndbuf.
2454 * In this case we cannot block, so that we have to fail.
2455 */
2456 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
2457 return 1;
2458 }
2459
Yafang Shaod6f19932018-07-01 23:31:30 +08002460 if (kind == SK_MEM_SEND || (kind == SK_MEM_RECV && charged))
2461 trace_sock_exceed_buf_limit(sk, prot, allocated, kind);
Satoru Moriya3847ce32011-06-17 12:00:03 +00002462
Glauber Costa0e90b312012-01-20 04:57:16 +00002463 sk_memory_allocated_sub(sk, amt);
Glauber Costa180d8cd2011-12-11 21:47:02 +00002464
Johannes Weinerbaac50b2016-01-14 15:21:17 -08002465 if (mem_cgroup_sockets_enabled && sk->sk_memcg)
2466 mem_cgroup_uncharge_skmem(sk->sk_memcg, amt);
Johannes Weinere8056052016-01-14 15:21:14 -08002467
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002468 return 0;
2469}
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002470EXPORT_SYMBOL(__sk_mem_raise_allocated);
2471
2472/**
2473 * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
2474 * @sk: socket
2475 * @size: memory size to allocate
2476 * @kind: allocation type
2477 *
2478 * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
2479 * rmem allocation. This function assumes that protocols which have
2480 * memory_pressure use sk_wmem_queued as write buffer accounting.
2481 */
2482int __sk_mem_schedule(struct sock *sk, int size, int kind)
2483{
2484 int ret, amt = sk_mem_pages(size);
2485
2486 sk->sk_forward_alloc += amt << SK_MEM_QUANTUM_SHIFT;
2487 ret = __sk_mem_raise_allocated(sk, size, amt, kind);
2488 if (!ret)
2489 sk->sk_forward_alloc -= amt << SK_MEM_QUANTUM_SHIFT;
2490 return ret;
2491}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002492EXPORT_SYMBOL(__sk_mem_schedule);
2493
2494/**
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002495 * __sk_mem_reduce_allocated - reclaim memory_allocated
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002496 * @sk: socket
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002497 * @amount: number of quanta
2498 *
2499 * Similar to __sk_mem_reclaim(), but does not update sk_forward_alloc
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002500 */
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002501void __sk_mem_reduce_allocated(struct sock *sk, int amount)
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002502{
Eric Dumazet1a24e042015-05-15 12:39:25 -07002503 sk_memory_allocated_sub(sk, amount);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002504
Johannes Weinerbaac50b2016-01-14 15:21:17 -08002505 if (mem_cgroup_sockets_enabled && sk->sk_memcg)
2506 mem_cgroup_uncharge_skmem(sk->sk_memcg, amount);
Johannes Weinere8056052016-01-14 15:21:14 -08002507
Glauber Costa180d8cd2011-12-11 21:47:02 +00002508 if (sk_under_memory_pressure(sk) &&
2509 (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
2510 sk_leave_memory_pressure(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002511}
Paolo Abenif8c3bf02016-10-21 13:55:45 +02002512EXPORT_SYMBOL(__sk_mem_reduce_allocated);
2513
2514/**
2515 * __sk_mem_reclaim - reclaim sk_forward_alloc and memory_allocated
2516 * @sk: socket
2517 * @amount: number of bytes (rounded down to a SK_MEM_QUANTUM multiple)
2518 */
2519void __sk_mem_reclaim(struct sock *sk, int amount)
2520{
2521 amount >>= SK_MEM_QUANTUM_SHIFT;
2522 sk->sk_forward_alloc -= amount << SK_MEM_QUANTUM_SHIFT;
2523 __sk_mem_reduce_allocated(sk, amount);
2524}
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002525EXPORT_SYMBOL(__sk_mem_reclaim);
2526
samanthakumar627d2d62016-04-05 12:41:16 -04002527int sk_set_peek_off(struct sock *sk, int val)
2528{
samanthakumar627d2d62016-04-05 12:41:16 -04002529 sk->sk_peek_off = val;
2530 return 0;
2531}
2532EXPORT_SYMBOL_GPL(sk_set_peek_off);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534/*
2535 * Set of default routines for initialising struct proto_ops when
2536 * the protocol does not support a particular function. In certain
2537 * cases where it makes no sense for a protocol to have a "do nothing"
2538 * function, some default processing is provided.
2539 */
2540
2541int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
2542{
2543 return -EOPNOTSUPP;
2544}
Eric Dumazet2a915252009-05-27 11:30:05 +00002545EXPORT_SYMBOL(sock_no_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002547int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 int len, int flags)
2549{
2550 return -EOPNOTSUPP;
2551}
Eric Dumazet2a915252009-05-27 11:30:05 +00002552EXPORT_SYMBOL(sock_no_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
2555{
2556 return -EOPNOTSUPP;
2557}
Eric Dumazet2a915252009-05-27 11:30:05 +00002558EXPORT_SYMBOL(sock_no_socketpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
David Howellscdfbabf2017-03-09 08:09:05 +00002560int sock_no_accept(struct socket *sock, struct socket *newsock, int flags,
2561 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
2563 return -EOPNOTSUPP;
2564}
Eric Dumazet2a915252009-05-27 11:30:05 +00002565EXPORT_SYMBOL(sock_no_accept);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002567int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002568 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 return -EOPNOTSUPP;
2571}
Eric Dumazet2a915252009-05-27 11:30:05 +00002572EXPORT_SYMBOL(sock_no_getname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2575{
2576 return -EOPNOTSUPP;
2577}
Eric Dumazet2a915252009-05-27 11:30:05 +00002578EXPORT_SYMBOL(sock_no_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580int sock_no_listen(struct socket *sock, int backlog)
2581{
2582 return -EOPNOTSUPP;
2583}
Eric Dumazet2a915252009-05-27 11:30:05 +00002584EXPORT_SYMBOL(sock_no_listen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586int sock_no_shutdown(struct socket *sock, int how)
2587{
2588 return -EOPNOTSUPP;
2589}
Eric Dumazet2a915252009-05-27 11:30:05 +00002590EXPORT_SYMBOL(sock_no_shutdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592int sock_no_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002593 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
2595 return -EOPNOTSUPP;
2596}
Eric Dumazet2a915252009-05-27 11:30:05 +00002597EXPORT_SYMBOL(sock_no_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599int sock_no_getsockopt(struct socket *sock, int level, int optname,
2600 char __user *optval, int __user *optlen)
2601{
2602 return -EOPNOTSUPP;
2603}
Eric Dumazet2a915252009-05-27 11:30:05 +00002604EXPORT_SYMBOL(sock_no_getsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Ying Xue1b784142015-03-02 15:37:48 +08002606int sock_no_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
2608 return -EOPNOTSUPP;
2609}
Eric Dumazet2a915252009-05-27 11:30:05 +00002610EXPORT_SYMBOL(sock_no_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Tom Herbert306b13e2017-07-28 16:22:41 -07002612int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *m, size_t len)
2613{
2614 return -EOPNOTSUPP;
2615}
2616EXPORT_SYMBOL(sock_no_sendmsg_locked);
2617
Ying Xue1b784142015-03-02 15:37:48 +08002618int sock_no_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
2619 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
2621 return -EOPNOTSUPP;
2622}
Eric Dumazet2a915252009-05-27 11:30:05 +00002623EXPORT_SYMBOL(sock_no_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
2626{
2627 /* Mirror missing mmap method error code */
2628 return -ENODEV;
2629}
Eric Dumazet2a915252009-05-27 11:30:05 +00002630EXPORT_SYMBOL(sock_no_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
2633{
2634 ssize_t res;
2635 struct msghdr msg = {.msg_flags = flags};
2636 struct kvec iov;
2637 char *kaddr = kmap(page);
2638 iov.iov_base = kaddr + offset;
2639 iov.iov_len = size;
2640 res = kernel_sendmsg(sock, &msg, &iov, 1, size);
2641 kunmap(page);
2642 return res;
2643}
Eric Dumazet2a915252009-05-27 11:30:05 +00002644EXPORT_SYMBOL(sock_no_sendpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Tom Herbert306b13e2017-07-28 16:22:41 -07002646ssize_t sock_no_sendpage_locked(struct sock *sk, struct page *page,
2647 int offset, size_t size, int flags)
2648{
2649 ssize_t res;
2650 struct msghdr msg = {.msg_flags = flags};
2651 struct kvec iov;
2652 char *kaddr = kmap(page);
2653
2654 iov.iov_base = kaddr + offset;
2655 iov.iov_len = size;
2656 res = kernel_sendmsg_locked(sk, &msg, &iov, 1, size);
2657 kunmap(page);
2658 return res;
2659}
2660EXPORT_SYMBOL(sock_no_sendpage_locked);
2661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662/*
2663 * Default Socket Callbacks
2664 */
2665
2666static void sock_def_wakeup(struct sock *sk)
2667{
Eric Dumazet43815482010-04-29 11:01:49 +00002668 struct socket_wq *wq;
2669
2670 rcu_read_lock();
2671 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002672 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +00002673 wake_up_interruptible_all(&wq->wait);
2674 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
2677static void sock_def_error_report(struct sock *sk)
2678{
Eric Dumazet43815482010-04-29 11:01:49 +00002679 struct socket_wq *wq;
2680
2681 rcu_read_lock();
2682 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002683 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002684 wake_up_interruptible_poll(&wq->wait, EPOLLERR);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002685 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
Eric Dumazet43815482010-04-29 11:01:49 +00002686 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
David S. Miller676d2362014-04-11 16:15:36 -04002689static void sock_def_readable(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Eric Dumazet43815482010-04-29 11:01:49 +00002691 struct socket_wq *wq;
2692
2693 rcu_read_lock();
2694 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002695 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002696 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI |
2697 EPOLLRDNORM | EPOLLRDBAND);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002698 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Eric Dumazet43815482010-04-29 11:01:49 +00002699 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700}
2701
2702static void sock_def_write_space(struct sock *sk)
2703{
Eric Dumazet43815482010-04-29 11:01:49 +00002704 struct socket_wq *wq;
2705
2706 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 /* Do not wake up a writer until he can make "significant"
2709 * progress. --DaveM
2710 */
Reshetova, Elena14afee42017-06-30 13:08:00 +03002711 if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
Eric Dumazet43815482010-04-29 11:01:49 +00002712 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002713 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002714 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
2715 EPOLLWRNORM | EPOLLWRBAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 /* Should agree with poll, otherwise some programs break */
2718 if (sock_writeable(sk))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002719 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
2721
Eric Dumazet43815482010-04-29 11:01:49 +00002722 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
2725static void sock_def_destruct(struct sock *sk)
2726{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729void sk_send_sigurg(struct sock *sk)
2730{
2731 if (sk->sk_socket && sk->sk_socket->file)
2732 if (send_sigurg(&sk->sk_socket->file->f_owner))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08002733 sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734}
Eric Dumazet2a915252009-05-27 11:30:05 +00002735EXPORT_SYMBOL(sk_send_sigurg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737void sk_reset_timer(struct sock *sk, struct timer_list* timer,
2738 unsigned long expires)
2739{
2740 if (!mod_timer(timer, expires))
2741 sock_hold(sk);
2742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743EXPORT_SYMBOL(sk_reset_timer);
2744
2745void sk_stop_timer(struct sock *sk, struct timer_list* timer)
2746{
Ying Xue25cc4ae2013-02-03 20:32:57 +00002747 if (del_timer(timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 __sock_put(sk);
2749}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750EXPORT_SYMBOL(sk_stop_timer);
2751
2752void sock_init_data(struct socket *sock, struct sock *sk)
2753{
Paolo Abeni581319c2017-03-09 13:54:08 +01002754 sk_init_common(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 sk->sk_send_head = NULL;
2756
Kees Cook99767f22017-10-16 17:29:36 -07002757 timer_setup(&sk->sk_timer, NULL, 0);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 sk->sk_allocation = GFP_KERNEL;
2760 sk->sk_rcvbuf = sysctl_rmem_default;
2761 sk->sk_sndbuf = sysctl_wmem_default;
2762 sk->sk_state = TCP_CLOSE;
David S. Miller972692e2008-06-17 22:41:38 -07002763 sk_set_socket(sk, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 sock_set_flag(sk, SOCK_ZAPPED);
2766
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002767 if (sock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 sk->sk_type = sock->type;
Eric Dumazet43815482010-04-29 11:01:49 +00002769 sk->sk_wq = sock->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 sock->sk = sk;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +09002771 sk->sk_uid = SOCK_INODE(sock)->i_uid;
2772 } else {
Eric Dumazet43815482010-04-29 11:01:49 +00002773 sk->sk_wq = NULL;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +09002774 sk->sk_uid = make_kuid(sock_net(sk)->user_ns, 0);
2775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 rwlock_init(&sk->sk_callback_lock);
David Howellscdfbabf2017-03-09 08:09:05 +00002778 if (sk->sk_kern_sock)
2779 lockdep_set_class_and_name(
2780 &sk->sk_callback_lock,
2781 af_kern_callback_keys + sk->sk_family,
2782 af_family_kern_clock_key_strings[sk->sk_family]);
2783 else
2784 lockdep_set_class_and_name(
2785 &sk->sk_callback_lock,
Peter Zijlstra443aef02007-07-19 01:49:00 -07002786 af_callback_keys + sk->sk_family,
2787 af_family_clock_key_strings[sk->sk_family]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 sk->sk_state_change = sock_def_wakeup;
2790 sk->sk_data_ready = sock_def_readable;
2791 sk->sk_write_space = sock_def_write_space;
2792 sk->sk_error_report = sock_def_error_report;
2793 sk->sk_destruct = sock_def_destruct;
2794
Eric Dumazet5640f762012-09-23 23:04:42 +00002795 sk->sk_frag.page = NULL;
2796 sk->sk_frag.offset = 0;
Pavel Emelyanovef64a542012-02-21 07:31:34 +00002797 sk->sk_peek_off = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Eric W. Biederman109f6e32010-06-13 03:30:14 +00002799 sk->sk_peer_pid = NULL;
2800 sk->sk_peer_cred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 sk->sk_write_pending = 0;
2802 sk->sk_rcvlowat = 1;
2803 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
2804 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
2805
Paolo Abeni6c7c98b2017-03-30 14:03:06 +02002806 sk->sk_stamp = SK_DEFAULT_STAMP;
Deepa Dinamani60f05dd2018-12-27 18:55:09 -08002807#if BITS_PER_LONG==32
2808 seqlock_init(&sk->sk_stamp_seq);
2809#endif
Willem de Bruijn52267792017-08-03 16:29:39 -04002810 atomic_set(&sk->sk_zckey, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Cong Wange0d10952013-08-01 11:10:25 +08002812#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir06021292013-06-10 11:39:50 +03002813 sk->sk_napi_id = 0;
Eliezer Tamir64b0dc52013-07-10 17:13:36 +03002814 sk->sk_ll_usec = sysctl_net_busy_read;
Eliezer Tamir06021292013-06-10 11:39:50 +03002815#endif
2816
Eric Dumazet62748f32013-09-24 08:20:52 -07002817 sk->sk_max_pacing_rate = ~0U;
Eric Dumazet7eec4172013-10-08 15:16:00 -07002818 sk->sk_pacing_rate = ~0U;
Eric Dumazet3a9b76f2017-11-11 15:54:12 -08002819 sk->sk_pacing_shift = 10;
Eric Dumazet70da2682015-10-08 19:33:21 -07002820 sk->sk_incoming_cpu = -1;
Amritha Nambiarc6345ce2018-06-29 21:26:57 -07002821
2822 sk_rx_queue_clear(sk);
Eric Dumazet4dc6dc72009-07-15 23:13:10 +00002823 /*
2824 * Before updating sk_refcnt, we must commit prior changes to memory
2825 * (Documentation/RCU/rculist_nulls.txt for details)
2826 */
2827 smp_wmb();
Reshetova, Elena41c6d652017-06-30 13:08:01 +03002828 refcount_set(&sk->sk_refcnt, 1);
Wang Chen33c732c2007-11-13 20:30:01 -08002829 atomic_set(&sk->sk_drops, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
Eric Dumazet2a915252009-05-27 11:30:05 +00002831EXPORT_SYMBOL(sock_init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002833void lock_sock_nested(struct sock *sk, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
2835 might_sleep();
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002836 spin_lock_bh(&sk->sk_lock.slock);
John Heffnerd2e91172007-09-12 10:44:19 +02002837 if (sk->sk_lock.owned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 __lock_sock(sk);
John Heffnerd2e91172007-09-12 10:44:19 +02002839 sk->sk_lock.owned = 1;
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002840 spin_unlock(&sk->sk_lock.slock);
2841 /*
2842 * The sk_lock has mutex_lock() semantics here:
2843 */
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002844 mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002845 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08002847EXPORT_SYMBOL(lock_sock_nested);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002849void release_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002851 spin_lock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (sk->sk_backlog.tail)
2853 __release_sock(sk);
Eric Dumazet46d3cea2012-07-11 05:50:31 +00002854
Eric Dumazetc3f9b012014-03-10 09:50:11 -07002855 /* Warning : release_cb() might need to release sk ownership,
2856 * ie call sock_release_ownership(sk) before us.
2857 */
Eric Dumazet46d3cea2012-07-11 05:50:31 +00002858 if (sk->sk_prot->release_cb)
2859 sk->sk_prot->release_cb(sk);
2860
Eric Dumazetc3f9b012014-03-10 09:50:11 -07002861 sock_release_ownership(sk);
Ingo Molnara5b5bb92006-07-03 00:25:35 -07002862 if (waitqueue_active(&sk->sk_lock.wq))
2863 wake_up(&sk->sk_lock.wq);
2864 spin_unlock_bh(&sk->sk_lock.slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866EXPORT_SYMBOL(release_sock);
2867
Eric Dumazet8a74ad62010-05-26 19:20:18 +00002868/**
2869 * lock_sock_fast - fast version of lock_sock
2870 * @sk: socket
2871 *
2872 * This version should be used for very small section, where process wont block
Mauro Carvalho Chehabd6519832017-05-12 09:35:46 -03002873 * return false if fast path is taken:
2874 *
Eric Dumazet8a74ad62010-05-26 19:20:18 +00002875 * sk_lock.slock locked, owned = 0, BH disabled
Mauro Carvalho Chehabd6519832017-05-12 09:35:46 -03002876 *
2877 * return true if slow path is taken:
2878 *
Eric Dumazet8a74ad62010-05-26 19:20:18 +00002879 * sk_lock.slock unlocked, owned = 1, BH enabled
2880 */
2881bool lock_sock_fast(struct sock *sk)
2882{
2883 might_sleep();
2884 spin_lock_bh(&sk->sk_lock.slock);
2885
2886 if (!sk->sk_lock.owned)
2887 /*
2888 * Note : We must disable BH
2889 */
2890 return false;
2891
2892 __lock_sock(sk);
2893 sk->sk_lock.owned = 1;
2894 spin_unlock(&sk->sk_lock.slock);
2895 /*
2896 * The sk_lock has mutex_lock() semantics here:
2897 */
2898 mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
2899 local_bh_enable();
2900 return true;
2901}
2902EXPORT_SYMBOL(lock_sock_fast);
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002905{
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002906 struct timeval tv;
Yafang Shao9dae3492018-08-06 11:57:02 +08002907
2908 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Deepa Dinamani60f05dd2018-12-27 18:55:09 -08002909 tv = ktime_to_timeval(sock_read_timestamp(sk));
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002910 if (tv.tv_sec == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 return -ENOENT;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002912 if (tv.tv_sec == 0) {
Deepa Dinamani60f05dd2018-12-27 18:55:09 -08002913 ktime_t kt = ktime_get_real();
2914 sock_write_timestamp(sk, kt);
2915 tv = ktime_to_timeval(kt);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002916 }
2917 return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002918}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919EXPORT_SYMBOL(sock_get_timestamp);
2920
Eric Dumazetae40eb12007-03-18 17:33:16 -07002921int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
2922{
2923 struct timespec ts;
Yafang Shao9dae3492018-08-06 11:57:02 +08002924
2925 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
Deepa Dinamani60f05dd2018-12-27 18:55:09 -08002926 ts = ktime_to_timespec(sock_read_timestamp(sk));
Eric Dumazetae40eb12007-03-18 17:33:16 -07002927 if (ts.tv_sec == -1)
2928 return -ENOENT;
2929 if (ts.tv_sec == 0) {
Deepa Dinamani60f05dd2018-12-27 18:55:09 -08002930 ktime_t kt = ktime_get_real();
2931 sock_write_timestamp(sk, kt);
Eric Dumazetae40eb12007-03-18 17:33:16 -07002932 ts = ktime_to_timespec(sk->sk_stamp);
2933 }
2934 return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
2935}
2936EXPORT_SYMBOL(sock_get_timestampns);
2937
Patrick Ohly20d49472009-02-12 05:03:38 +00002938void sock_enable_timestamp(struct sock *sk, int flag)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002939{
Patrick Ohly20d49472009-02-12 05:03:38 +00002940 if (!sock_flag(sk, flag)) {
Eric Dumazet08e29af2011-11-28 12:04:18 +00002941 unsigned long previous_flags = sk->sk_flags;
2942
Patrick Ohly20d49472009-02-12 05:03:38 +00002943 sock_set_flag(sk, flag);
2944 /*
2945 * we just set one of the two flags which require net
2946 * time stamping, but time stamping might have been on
2947 * already because of the other one
2948 */
Hannes Frederic Sowa080a2702015-10-26 13:51:37 +01002949 if (sock_needs_netstamp(sk) &&
2950 !(previous_flags & SK_FLAGS_TIMESTAMP))
Patrick Ohly20d49472009-02-12 05:03:38 +00002951 net_enable_timestamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
2953}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Richard Cochrancb820f82013-07-19 19:40:09 +02002955int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len,
2956 int level, int type)
2957{
2958 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04002959 struct sk_buff *skb;
Richard Cochrancb820f82013-07-19 19:40:09 +02002960 int copied, err;
2961
2962 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04002963 skb = sock_dequeue_err_skb(sk);
Richard Cochrancb820f82013-07-19 19:40:09 +02002964 if (skb == NULL)
2965 goto out;
2966
2967 copied = skb->len;
2968 if (copied > len) {
2969 msg->msg_flags |= MSG_TRUNC;
2970 copied = len;
2971 }
David S. Miller51f3d022014-11-05 16:46:40 -05002972 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Richard Cochrancb820f82013-07-19 19:40:09 +02002973 if (err)
2974 goto out_free_skb;
2975
2976 sock_recv_timestamp(msg, sk, skb);
2977
2978 serr = SKB_EXT_ERR(skb);
2979 put_cmsg(msg, level, type, sizeof(serr->ee), &serr->ee);
2980
2981 msg->msg_flags |= MSG_ERRQUEUE;
2982 err = copied;
2983
Richard Cochrancb820f82013-07-19 19:40:09 +02002984out_free_skb:
2985 kfree_skb(skb);
2986out:
2987 return err;
2988}
2989EXPORT_SYMBOL(sock_recv_errqueue);
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991/*
2992 * Get a socket option on an socket.
2993 *
2994 * FIX: POSIX 1003.1g is very ambiguous here. It states that
2995 * asynchronous errors should be reported by getsockopt. We assume
2996 * this means if you specify SO_ERROR (otherwise whats the point of it).
2997 */
2998int sock_common_getsockopt(struct socket *sock, int level, int optname,
2999 char __user *optval, int __user *optlen)
3000{
3001 struct sock *sk = sock->sk;
3002
3003 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
3004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005EXPORT_SYMBOL(sock_common_getsockopt);
3006
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08003007#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08003008int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
3009 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08003010{
3011 struct sock *sk = sock->sk;
3012
Johannes Berg1e51f952007-03-06 13:44:06 -08003013 if (sk->sk_prot->compat_getsockopt != NULL)
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08003014 return sk->sk_prot->compat_getsockopt(sk, level, optname,
3015 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08003016 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
3017}
3018EXPORT_SYMBOL(compat_sock_common_getsockopt);
3019#endif
3020
Ying Xue1b784142015-03-02 15:37:48 +08003021int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
3022 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 struct sock *sk = sock->sk;
3025 int addr_len = 0;
3026 int err;
3027
Ying Xue1b784142015-03-02 15:37:48 +08003028 err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 flags & ~MSG_DONTWAIT, &addr_len);
3030 if (err >= 0)
3031 msg->msg_namelen = addr_len;
3032 return err;
3033}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034EXPORT_SYMBOL(sock_common_recvmsg);
3035
3036/*
3037 * Set socket options on an inet socket.
3038 */
3039int sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003040 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
3042 struct sock *sk = sock->sk;
3043
3044 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
3045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046EXPORT_SYMBOL(sock_common_setsockopt);
3047
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08003048#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08003049int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003050 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08003051{
3052 struct sock *sk = sock->sk;
3053
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08003054 if (sk->sk_prot->compat_setsockopt != NULL)
3055 return sk->sk_prot->compat_setsockopt(sk, level, optname,
3056 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08003057 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
3058}
3059EXPORT_SYMBOL(compat_sock_common_setsockopt);
3060#endif
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062void sk_common_release(struct sock *sk)
3063{
3064 if (sk->sk_prot->destroy)
3065 sk->sk_prot->destroy(sk);
3066
3067 /*
3068 * Observation: when sock_common_release is called, processes have
3069 * no access to socket. But net still has.
3070 * Step one, detach it from networking:
3071 *
3072 * A. Remove from hash tables.
3073 */
3074
3075 sk->sk_prot->unhash(sk);
3076
3077 /*
3078 * In this point socket cannot receive new packets, but it is possible
3079 * that some packets are in flight because some CPU runs receiver and
3080 * did hash table lookup before we unhashed socket. They will achieve
3081 * receive queue and will be purged by socket destructor.
3082 *
3083 * Also we still have packets pending on receive queue and probably,
3084 * our own packets waiting in device queues. sock_destroy will drain
3085 * receive queue, but transmitted packets will delay socket destruction
3086 * until the last reference will be released.
3087 */
3088
3089 sock_orphan(sk);
3090
3091 xfrm_sk_free_policy(sk);
3092
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -07003093 sk_refcnt_debug_release(sk);
Eric Dumazet5640f762012-09-23 23:04:42 +00003094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 sock_put(sk);
3096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097EXPORT_SYMBOL(sk_common_release);
3098
Josh Hunta2d133b2017-03-20 15:22:03 -04003099void sk_get_meminfo(const struct sock *sk, u32 *mem)
3100{
3101 memset(mem, 0, sizeof(*mem) * SK_MEMINFO_VARS);
3102
3103 mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
3104 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
3105 mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
3106 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
3107 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
3108 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
3109 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
3110 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
3111 mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
3112}
3113
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003114#ifdef CONFIG_PROC_FS
3115#define PROTO_INUSE_NR 64 /* should be enough for the first time */
Pavel Emelyanov1338d462008-03-28 16:38:43 -07003116struct prot_inuse {
3117 int val[PROTO_INUSE_NR];
3118};
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003119
3120static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003121
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003122void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
3123{
Tonghao Zhang08fc7f82017-12-14 05:51:57 -08003124 __this_cpu_add(net->core.prot_inuse->val[prot->inuse_idx], val);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003125}
3126EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
3127
3128int sock_prot_inuse_get(struct net *net, struct proto *prot)
3129{
3130 int cpu, idx = prot->inuse_idx;
3131 int res = 0;
3132
3133 for_each_possible_cpu(cpu)
Tonghao Zhang08fc7f82017-12-14 05:51:57 -08003134 res += per_cpu_ptr(net->core.prot_inuse, cpu)->val[idx];
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003135
3136 return res >= 0 ? res : 0;
3137}
3138EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
3139
Tonghao Zhang648845a2017-12-14 05:51:58 -08003140static void sock_inuse_add(struct net *net, int val)
3141{
3142 this_cpu_add(*net->core.sock_inuse, val);
3143}
3144
3145int sock_inuse_get(struct net *net)
3146{
3147 int cpu, res = 0;
3148
3149 for_each_possible_cpu(cpu)
3150 res += *per_cpu_ptr(net->core.sock_inuse, cpu);
3151
3152 return res;
3153}
3154
3155EXPORT_SYMBOL_GPL(sock_inuse_get);
3156
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003157static int __net_init sock_inuse_init_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003158{
Tonghao Zhang08fc7f82017-12-14 05:51:57 -08003159 net->core.prot_inuse = alloc_percpu(struct prot_inuse);
Tonghao Zhang648845a2017-12-14 05:51:58 -08003160 if (net->core.prot_inuse == NULL)
3161 return -ENOMEM;
3162
3163 net->core.sock_inuse = alloc_percpu(int);
3164 if (net->core.sock_inuse == NULL)
3165 goto out;
3166
3167 return 0;
3168
3169out:
3170 free_percpu(net->core.prot_inuse);
3171 return -ENOMEM;
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003172}
3173
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003174static void __net_exit sock_inuse_exit_net(struct net *net)
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003175{
Tonghao Zhang08fc7f82017-12-14 05:51:57 -08003176 free_percpu(net->core.prot_inuse);
Tonghao Zhang648845a2017-12-14 05:51:58 -08003177 free_percpu(net->core.sock_inuse);
Pavel Emelyanov70ee1152008-03-31 19:42:16 -07003178}
3179
3180static struct pernet_operations net_inuse_ops = {
3181 .init = sock_inuse_init_net,
3182 .exit = sock_inuse_exit_net,
3183};
3184
3185static __init int net_inuse_init(void)
3186{
3187 if (register_pernet_subsys(&net_inuse_ops))
3188 panic("Cannot initialize net inuse counters");
3189
3190 return 0;
3191}
3192
3193core_initcall(net_inuse_init);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003194
3195static void assign_proto_idx(struct proto *prot)
3196{
3197 prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
3198
3199 if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
Joe Perchese005d192012-05-16 19:58:40 +00003200 pr_err("PROTO_INUSE_NR exhausted\n");
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003201 return;
3202 }
3203
3204 set_bit(prot->inuse_idx, proto_inuse_idx);
3205}
3206
3207static void release_proto_idx(struct proto *prot)
3208{
3209 if (prot->inuse_idx != PROTO_INUSE_NR - 1)
3210 clear_bit(prot->inuse_idx, proto_inuse_idx);
3211}
3212#else
3213static inline void assign_proto_idx(struct proto *prot)
3214{
3215}
3216
3217static inline void release_proto_idx(struct proto *prot)
3218{
3219}
Tonghao Zhang648845a2017-12-14 05:51:58 -08003220
3221static void sock_inuse_add(struct net *net, int val)
3222{
3223}
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003224#endif
3225
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003226static void req_prot_cleanup(struct request_sock_ops *rsk_prot)
3227{
3228 if (!rsk_prot)
3229 return;
3230 kfree(rsk_prot->slab_name);
3231 rsk_prot->slab_name = NULL;
Julia Lawalladf78ed2015-09-13 14:15:18 +02003232 kmem_cache_destroy(rsk_prot->slab);
3233 rsk_prot->slab = NULL;
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003234}
3235
3236static int req_prot_init(const struct proto *prot)
3237{
3238 struct request_sock_ops *rsk_prot = prot->rsk_prot;
3239
3240 if (!rsk_prot)
3241 return 0;
3242
3243 rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s",
3244 prot->name);
3245 if (!rsk_prot->slab_name)
3246 return -ENOMEM;
3247
3248 rsk_prot->slab = kmem_cache_create(rsk_prot->slab_name,
3249 rsk_prot->obj_size, 0,
Shakeel Butte699e2c2018-06-27 15:16:42 -07003250 SLAB_ACCOUNT | prot->slab_flags,
3251 NULL);
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003252
3253 if (!rsk_prot->slab) {
3254 pr_crit("%s: Can't create request sock SLAB cache!\n",
3255 prot->name);
3256 return -ENOMEM;
3257 }
3258 return 0;
3259}
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261int proto_register(struct proto *prot, int alloc_slab)
3262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (alloc_slab) {
David Windsor30c2c9f2017-06-10 22:50:42 -04003264 prot->slab = kmem_cache_create_usercopy(prot->name,
3265 prot->obj_size, 0,
Shakeel Butte699e2c2018-06-27 15:16:42 -07003266 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT |
3267 prot->slab_flags,
Kees Cook289a48602017-08-24 16:59:38 -07003268 prot->useroffset, prot->usersize,
Eric Dumazet271b72c2008-10-29 02:11:14 -07003269 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271 if (prot->slab == NULL) {
Joe Perchese005d192012-05-16 19:58:40 +00003272 pr_crit("%s: Can't create sock SLAB cache!\n",
3273 prot->name);
Pavel Emelyanov60e76632008-03-28 16:39:10 -07003274 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07003276
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003277 if (req_prot_init(prot))
3278 goto out_free_request_sock_slab;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003279
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003280 if (prot->twsk_prot != NULL) {
Alexey Dobriyanfaf23422010-02-17 09:34:12 +00003281 prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003282
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08003283 if (prot->twsk_prot->twsk_slab_name == NULL)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003284 goto out_free_request_sock_slab;
3285
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003286 prot->twsk_prot->twsk_slab =
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08003287 kmem_cache_create(prot->twsk_prot->twsk_slab_name,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003288 prot->twsk_prot->twsk_obj_size,
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08003289 0,
Shakeel Butte699e2c2018-06-27 15:16:42 -07003290 SLAB_ACCOUNT |
Eric Dumazet52db70d2015-04-10 06:07:18 -07003291 prot->slab_flags,
Paul Mundt20c2df82007-07-20 10:11:58 +09003292 NULL);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003293 if (prot->twsk_prot->twsk_slab == NULL)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003294 goto out_free_timewait_sock_slab_name;
3295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 }
3297
Glauber Costa36b77a52011-12-16 00:51:59 +00003298 mutex_lock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 list_add(&prot->node, &proto_list);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003300 assign_proto_idx(prot);
Glauber Costa36b77a52011-12-16 00:51:59 +00003301 mutex_unlock(&proto_list_mutex);
Pavel Emelyanovb733c002007-11-07 02:23:38 -08003302 return 0;
3303
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003304out_free_timewait_sock_slab_name:
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08003305 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003306out_free_request_sock_slab:
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003307 req_prot_cleanup(prot->rsk_prot);
3308
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07003309 kmem_cache_destroy(prot->slab);
3310 prot->slab = NULL;
Pavel Emelyanovb733c002007-11-07 02:23:38 -08003311out:
3312 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314EXPORT_SYMBOL(proto_register);
3315
3316void proto_unregister(struct proto *prot)
3317{
Glauber Costa36b77a52011-12-16 00:51:59 +00003318 mutex_lock(&proto_list_mutex);
Pavel Emelyanov13ff3d62008-03-28 16:38:17 -07003319 release_proto_idx(prot);
Patrick McHardy0a3f4352005-09-06 19:47:50 -07003320 list_del(&prot->node);
Glauber Costa36b77a52011-12-16 00:51:59 +00003321 mutex_unlock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Julia Lawalladf78ed2015-09-13 14:15:18 +02003323 kmem_cache_destroy(prot->slab);
3324 prot->slab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Eric Dumazet0159dfd2015-03-12 16:44:07 -07003326 req_prot_cleanup(prot->rsk_prot);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07003327
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003328 if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003329 kmem_cache_destroy(prot->twsk_prot->twsk_slab);
Catalin Marinas7e56b5d2008-11-21 16:45:22 -08003330 kfree(prot->twsk_prot->twsk_slab_name);
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08003331 prot->twsk_prot->twsk_slab = NULL;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07003332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334EXPORT_SYMBOL(proto_unregister);
3335
Xin Longbf2ae2e2018-03-10 18:57:50 +08003336int sock_load_diag_module(int family, int protocol)
3337{
3338 if (!protocol) {
3339 if (!sock_is_registered(family))
3340 return -ENOENT;
3341
3342 return request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
3343 NETLINK_SOCK_DIAG, family);
3344 }
3345
3346#ifdef CONFIG_INET
3347 if (family == AF_INET &&
3348 !rcu_access_pointer(inet_protos[protocol]))
3349 return -ENOENT;
3350#endif
3351
3352 return request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
3353 NETLINK_SOCK_DIAG, family, protocol);
3354}
3355EXPORT_SYMBOL(sock_load_diag_module);
3356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
Glauber Costa36b77a52011-12-16 00:51:59 +00003359 __acquires(proto_list_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
Glauber Costa36b77a52011-12-16 00:51:59 +00003361 mutex_lock(&proto_list_mutex);
Pavel Emelianov60f04382007-07-09 13:15:14 -07003362 return seq_list_start_head(&proto_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
3364
3365static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3366{
Pavel Emelianov60f04382007-07-09 13:15:14 -07003367 return seq_list_next(v, &proto_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
3370static void proto_seq_stop(struct seq_file *seq, void *v)
Glauber Costa36b77a52011-12-16 00:51:59 +00003371 __releases(proto_list_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Glauber Costa36b77a52011-12-16 00:51:59 +00003373 mutex_unlock(&proto_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374}
3375
3376static char proto_method_implemented(const void *method)
3377{
3378 return method == NULL ? 'n' : 'y';
3379}
Glauber Costa180d8cd2011-12-11 21:47:02 +00003380static long sock_prot_memory_allocated(struct proto *proto)
3381{
Jeffrin Josecb75a362012-04-25 19:17:29 +05303382 return proto->memory_allocated != NULL ? proto_memory_allocated(proto) : -1L;
Glauber Costa180d8cd2011-12-11 21:47:02 +00003383}
3384
3385static char *sock_prot_memory_pressure(struct proto *proto)
3386{
3387 return proto->memory_pressure != NULL ?
3388 proto_memory_pressure(proto) ? "yes" : "no" : "NI";
3389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
3391static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
3392{
Glauber Costa180d8cd2011-12-11 21:47:02 +00003393
Eric Dumazet8d987e52010-11-09 23:24:26 +00003394 seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
3396 proto->name,
3397 proto->obj_size,
Eric Dumazet14e943d2008-11-19 15:14:01 -08003398 sock_prot_inuse_get(seq_file_net(seq), proto),
Glauber Costa180d8cd2011-12-11 21:47:02 +00003399 sock_prot_memory_allocated(proto),
3400 sock_prot_memory_pressure(proto),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 proto->max_header,
3402 proto->slab == NULL ? "no" : "yes",
3403 module_name(proto->owner),
3404 proto_method_implemented(proto->close),
3405 proto_method_implemented(proto->connect),
3406 proto_method_implemented(proto->disconnect),
3407 proto_method_implemented(proto->accept),
3408 proto_method_implemented(proto->ioctl),
3409 proto_method_implemented(proto->init),
3410 proto_method_implemented(proto->destroy),
3411 proto_method_implemented(proto->shutdown),
3412 proto_method_implemented(proto->setsockopt),
3413 proto_method_implemented(proto->getsockopt),
3414 proto_method_implemented(proto->sendmsg),
3415 proto_method_implemented(proto->recvmsg),
3416 proto_method_implemented(proto->sendpage),
3417 proto_method_implemented(proto->bind),
3418 proto_method_implemented(proto->backlog_rcv),
3419 proto_method_implemented(proto->hash),
3420 proto_method_implemented(proto->unhash),
3421 proto_method_implemented(proto->get_port),
3422 proto_method_implemented(proto->enter_memory_pressure));
3423}
3424
3425static int proto_seq_show(struct seq_file *seq, void *v)
3426{
Pavel Emelianov60f04382007-07-09 13:15:14 -07003427 if (v == &proto_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
3429 "protocol",
3430 "size",
3431 "sockets",
3432 "memory",
3433 "press",
3434 "maxhdr",
3435 "slab",
3436 "module",
3437 "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
3438 else
Pavel Emelianov60f04382007-07-09 13:15:14 -07003439 proto_seq_printf(seq, list_entry(v, struct proto, node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return 0;
3441}
3442
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003443static const struct seq_operations proto_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 .start = proto_seq_start,
3445 .next = proto_seq_next,
3446 .stop = proto_seq_stop,
3447 .show = proto_seq_show,
3448};
3449
Eric Dumazet14e943d2008-11-19 15:14:01 -08003450static __net_init int proto_init_net(struct net *net)
3451{
Christoph Hellwigc3506372018-04-10 19:42:55 +02003452 if (!proc_create_net("protocols", 0444, net->proc_net, &proto_seq_ops,
3453 sizeof(struct seq_net_private)))
Eric Dumazet14e943d2008-11-19 15:14:01 -08003454 return -ENOMEM;
3455
3456 return 0;
3457}
3458
3459static __net_exit void proto_exit_net(struct net *net)
3460{
Gao fengece31ff2013-02-18 01:34:56 +00003461 remove_proc_entry("protocols", net->proc_net);
Eric Dumazet14e943d2008-11-19 15:14:01 -08003462}
3463
3464
3465static __net_initdata struct pernet_operations proto_net_ops = {
3466 .init = proto_init_net,
3467 .exit = proto_exit_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468};
3469
3470static int __init proto_init(void)
3471{
Eric Dumazet14e943d2008-11-19 15:14:01 -08003472 return register_pernet_subsys(&proto_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
3475subsys_initcall(proto_init);
3476
3477#endif /* PROC_FS */
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07003478
3479#ifdef CONFIG_NET_RX_BUSY_POLL
3480bool sk_busy_loop_end(void *p, unsigned long start_time)
3481{
3482 struct sock *sk = p;
3483
3484 return !skb_queue_empty(&sk->sk_receive_queue) ||
3485 sk_busy_loop_timeout(sk, start_time);
3486}
3487EXPORT_SYMBOL(sk_busy_loop_end);
3488#endif /* CONFIG_NET_RX_BUSY_POLL */