net: Remove redundant calls of sk_tx_queue_clear().
[platform/kernel/linux-starfive.git] / net / core / sock.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * INET         An implementation of the TCP/IP protocol suite for the LINUX
4  *              operating system.  INET is implemented using the  BSD Socket
5  *              interface as the means of communication with the user level.
6  *
7  *              Generic socket support routines. Memory allocators, socket lock/release
8  *              handler for protocols to use and generic option handler.
9  *
10  * Authors:     Ross Biro
11  *              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
35  *                                      code. The ACK stuff can wait and needs major
36  *                                      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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
87
88 #include <asm/unaligned.h>
89 #include <linux/capability.h>
90 #include <linux/errno.h>
91 #include <linux/errqueue.h>
92 #include <linux/types.h>
93 #include <linux/socket.h>
94 #include <linux/in.h>
95 #include <linux/kernel.h>
96 #include <linux/module.h>
97 #include <linux/proc_fs.h>
98 #include <linux/seq_file.h>
99 #include <linux/sched.h>
100 #include <linux/sched/mm.h>
101 #include <linux/timer.h>
102 #include <linux/string.h>
103 #include <linux/sockios.h>
104 #include <linux/net.h>
105 #include <linux/mm.h>
106 #include <linux/slab.h>
107 #include <linux/interrupt.h>
108 #include <linux/poll.h>
109 #include <linux/tcp.h>
110 #include <linux/init.h>
111 #include <linux/highmem.h>
112 #include <linux/user_namespace.h>
113 #include <linux/static_key.h>
114 #include <linux/memcontrol.h>
115 #include <linux/prefetch.h>
116 #include <linux/compat.h>
117
118 #include <linux/uaccess.h>
119
120 #include <linux/netdevice.h>
121 #include <net/protocol.h>
122 #include <linux/skbuff.h>
123 #include <net/net_namespace.h>
124 #include <net/request_sock.h>
125 #include <net/sock.h>
126 #include <linux/net_tstamp.h>
127 #include <net/xfrm.h>
128 #include <linux/ipsec.h>
129 #include <net/cls_cgroup.h>
130 #include <net/netprio_cgroup.h>
131 #include <linux/sock_diag.h>
132
133 #include <linux/filter.h>
134 #include <net/sock_reuseport.h>
135 #include <net/bpf_sk_storage.h>
136
137 #include <trace/events/sock.h>
138
139 #include <net/tcp.h>
140 #include <net/busy_poll.h>
141
142 static DEFINE_MUTEX(proto_list_mutex);
143 static LIST_HEAD(proto_list);
144
145 static void sock_inuse_add(struct net *net, int val);
146
147 /**
148  * sk_ns_capable - General socket capability test
149  * @sk: Socket to use a capability on or through
150  * @user_ns: The user namespace of the capability to use
151  * @cap: The capability to use
152  *
153  * Test to see if the opener of the socket had when the socket was
154  * created and the current process has the capability @cap in the user
155  * namespace @user_ns.
156  */
157 bool sk_ns_capable(const struct sock *sk,
158                    struct user_namespace *user_ns, int cap)
159 {
160         return file_ns_capable(sk->sk_socket->file, user_ns, cap) &&
161                 ns_capable(user_ns, cap);
162 }
163 EXPORT_SYMBOL(sk_ns_capable);
164
165 /**
166  * sk_capable - Socket global capability test
167  * @sk: Socket to use a capability on or through
168  * @cap: The global capability to use
169  *
170  * Test to see if the opener of the socket had when the socket was
171  * created and the current process has the capability @cap in all user
172  * namespaces.
173  */
174 bool sk_capable(const struct sock *sk, int cap)
175 {
176         return sk_ns_capable(sk, &init_user_ns, cap);
177 }
178 EXPORT_SYMBOL(sk_capable);
179
180 /**
181  * sk_net_capable - Network namespace socket capability test
182  * @sk: Socket to use a capability on or through
183  * @cap: The capability to use
184  *
185  * Test to see if the opener of the socket had when the socket was created
186  * and the current process has the capability @cap over the network namespace
187  * the socket is a member of.
188  */
189 bool sk_net_capable(const struct sock *sk, int cap)
190 {
191         return sk_ns_capable(sk, sock_net(sk)->user_ns, cap);
192 }
193 EXPORT_SYMBOL(sk_net_capable);
194
195 /*
196  * Each address family might have different locking rules, so we have
197  * one slock key per address family and separate keys for internal and
198  * userspace sockets.
199  */
200 static struct lock_class_key af_family_keys[AF_MAX];
201 static struct lock_class_key af_family_kern_keys[AF_MAX];
202 static struct lock_class_key af_family_slock_keys[AF_MAX];
203 static struct lock_class_key af_family_kern_slock_keys[AF_MAX];
204
205 /*
206  * Make lock validator output more readable. (we pre-construct these
207  * strings build-time, so that runtime initialization of socket
208  * locks is fast):
209  */
210
211 #define _sock_locks(x)                                            \
212   x "AF_UNSPEC",        x "AF_UNIX"     ,       x "AF_INET"     , \
213   x "AF_AX25"  ,        x "AF_IPX"      ,       x "AF_APPLETALK", \
214   x "AF_NETROM",        x "AF_BRIDGE"   ,       x "AF_ATMPVC"   , \
215   x "AF_X25"   ,        x "AF_INET6"    ,       x "AF_ROSE"     , \
216   x "AF_DECnet",        x "AF_NETBEUI"  ,       x "AF_SECURITY" , \
217   x "AF_KEY"   ,        x "AF_NETLINK"  ,       x "AF_PACKET"   , \
218   x "AF_ASH"   ,        x "AF_ECONET"   ,       x "AF_ATMSVC"   , \
219   x "AF_RDS"   ,        x "AF_SNA"      ,       x "AF_IRDA"     , \
220   x "AF_PPPOX" ,        x "AF_WANPIPE"  ,       x "AF_LLC"      , \
221   x "27"       ,        x "28"          ,       x "AF_CAN"      , \
222   x "AF_TIPC"  ,        x "AF_BLUETOOTH",       x "IUCV"        , \
223   x "AF_RXRPC" ,        x "AF_ISDN"     ,       x "AF_PHONET"   , \
224   x "AF_IEEE802154",    x "AF_CAIF"     ,       x "AF_ALG"      , \
225   x "AF_NFC"   ,        x "AF_VSOCK"    ,       x "AF_KCM"      , \
226   x "AF_QIPCRTR",       x "AF_SMC"      ,       x "AF_XDP"      , \
227   x "AF_MAX"
228
229 static const char *const af_family_key_strings[AF_MAX+1] = {
230         _sock_locks("sk_lock-")
231 };
232 static const char *const af_family_slock_key_strings[AF_MAX+1] = {
233         _sock_locks("slock-")
234 };
235 static const char *const af_family_clock_key_strings[AF_MAX+1] = {
236         _sock_locks("clock-")
237 };
238
239 static const char *const af_family_kern_key_strings[AF_MAX+1] = {
240         _sock_locks("k-sk_lock-")
241 };
242 static const char *const af_family_kern_slock_key_strings[AF_MAX+1] = {
243         _sock_locks("k-slock-")
244 };
245 static const char *const af_family_kern_clock_key_strings[AF_MAX+1] = {
246         _sock_locks("k-clock-")
247 };
248 static const char *const af_family_rlock_key_strings[AF_MAX+1] = {
249         _sock_locks("rlock-")
250 };
251 static const char *const af_family_wlock_key_strings[AF_MAX+1] = {
252         _sock_locks("wlock-")
253 };
254 static const char *const af_family_elock_key_strings[AF_MAX+1] = {
255         _sock_locks("elock-")
256 };
257
258 /*
259  * sk_callback_lock and sk queues locking rules are per-address-family,
260  * so split the lock classes by using a per-AF key:
261  */
262 static struct lock_class_key af_callback_keys[AF_MAX];
263 static struct lock_class_key af_rlock_keys[AF_MAX];
264 static struct lock_class_key af_wlock_keys[AF_MAX];
265 static struct lock_class_key af_elock_keys[AF_MAX];
266 static struct lock_class_key af_kern_callback_keys[AF_MAX];
267
268 /* Run time adjustable parameters. */
269 __u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
270 EXPORT_SYMBOL(sysctl_wmem_max);
271 __u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
272 EXPORT_SYMBOL(sysctl_rmem_max);
273 __u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
274 __u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
275
276 /* Maximal space eaten by iovec or ancillary data plus some space */
277 int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
278 EXPORT_SYMBOL(sysctl_optmem_max);
279
280 int sysctl_tstamp_allow_data __read_mostly = 1;
281
282 DEFINE_STATIC_KEY_FALSE(memalloc_socks_key);
283 EXPORT_SYMBOL_GPL(memalloc_socks_key);
284
285 /**
286  * sk_set_memalloc - sets %SOCK_MEMALLOC
287  * @sk: socket to set it on
288  *
289  * Set %SOCK_MEMALLOC on a socket for access to emergency reserves.
290  * It's the responsibility of the admin to adjust min_free_kbytes
291  * to meet the requirements
292  */
293 void sk_set_memalloc(struct sock *sk)
294 {
295         sock_set_flag(sk, SOCK_MEMALLOC);
296         sk->sk_allocation |= __GFP_MEMALLOC;
297         static_branch_inc(&memalloc_socks_key);
298 }
299 EXPORT_SYMBOL_GPL(sk_set_memalloc);
300
301 void sk_clear_memalloc(struct sock *sk)
302 {
303         sock_reset_flag(sk, SOCK_MEMALLOC);
304         sk->sk_allocation &= ~__GFP_MEMALLOC;
305         static_branch_dec(&memalloc_socks_key);
306
307         /*
308          * SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
309          * progress of swapping. SOCK_MEMALLOC may be cleared while
310          * it has rmem allocations due to the last swapfile being deactivated
311          * but there is a risk that the socket is unusable due to exceeding
312          * the rmem limits. Reclaim the reserves and obey rmem limits again.
313          */
314         sk_mem_reclaim(sk);
315 }
316 EXPORT_SYMBOL_GPL(sk_clear_memalloc);
317
318 int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
319 {
320         int ret;
321         unsigned int noreclaim_flag;
322
323         /* these should have been dropped before queueing */
324         BUG_ON(!sock_flag(sk, SOCK_MEMALLOC));
325
326         noreclaim_flag = memalloc_noreclaim_save();
327         ret = sk->sk_backlog_rcv(sk, skb);
328         memalloc_noreclaim_restore(noreclaim_flag);
329
330         return ret;
331 }
332 EXPORT_SYMBOL(__sk_backlog_rcv);
333
334 static int sock_get_timeout(long timeo, void *optval, bool old_timeval)
335 {
336         struct __kernel_sock_timeval tv;
337
338         if (timeo == MAX_SCHEDULE_TIMEOUT) {
339                 tv.tv_sec = 0;
340                 tv.tv_usec = 0;
341         } else {
342                 tv.tv_sec = timeo / HZ;
343                 tv.tv_usec = ((timeo % HZ) * USEC_PER_SEC) / HZ;
344         }
345
346         if (old_timeval && in_compat_syscall() && !COMPAT_USE_64BIT_TIME) {
347                 struct old_timeval32 tv32 = { tv.tv_sec, tv.tv_usec };
348                 *(struct old_timeval32 *)optval = tv32;
349                 return sizeof(tv32);
350         }
351
352         if (old_timeval) {
353                 struct __kernel_old_timeval old_tv;
354                 old_tv.tv_sec = tv.tv_sec;
355                 old_tv.tv_usec = tv.tv_usec;
356                 *(struct __kernel_old_timeval *)optval = old_tv;
357                 return sizeof(old_tv);
358         }
359
360         *(struct __kernel_sock_timeval *)optval = tv;
361         return sizeof(tv);
362 }
363
364 static int sock_set_timeout(long *timeo_p, sockptr_t optval, int optlen,
365                             bool old_timeval)
366 {
367         struct __kernel_sock_timeval tv;
368
369         if (old_timeval && in_compat_syscall() && !COMPAT_USE_64BIT_TIME) {
370                 struct old_timeval32 tv32;
371
372                 if (optlen < sizeof(tv32))
373                         return -EINVAL;
374
375                 if (copy_from_sockptr(&tv32, optval, sizeof(tv32)))
376                         return -EFAULT;
377                 tv.tv_sec = tv32.tv_sec;
378                 tv.tv_usec = tv32.tv_usec;
379         } else if (old_timeval) {
380                 struct __kernel_old_timeval old_tv;
381
382                 if (optlen < sizeof(old_tv))
383                         return -EINVAL;
384                 if (copy_from_sockptr(&old_tv, optval, sizeof(old_tv)))
385                         return -EFAULT;
386                 tv.tv_sec = old_tv.tv_sec;
387                 tv.tv_usec = old_tv.tv_usec;
388         } else {
389                 if (optlen < sizeof(tv))
390                         return -EINVAL;
391                 if (copy_from_sockptr(&tv, optval, sizeof(tv)))
392                         return -EFAULT;
393         }
394         if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
395                 return -EDOM;
396
397         if (tv.tv_sec < 0) {
398                 static int warned __read_mostly;
399
400                 *timeo_p = 0;
401                 if (warned < 10 && net_ratelimit()) {
402                         warned++;
403                         pr_info("%s: `%s' (pid %d) tries to set negative timeout\n",
404                                 __func__, current->comm, task_pid_nr(current));
405                 }
406                 return 0;
407         }
408         *timeo_p = MAX_SCHEDULE_TIMEOUT;
409         if (tv.tv_sec == 0 && tv.tv_usec == 0)
410                 return 0;
411         if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1))
412                 *timeo_p = tv.tv_sec * HZ + DIV_ROUND_UP((unsigned long)tv.tv_usec, USEC_PER_SEC / HZ);
413         return 0;
414 }
415
416 static bool sock_needs_netstamp(const struct sock *sk)
417 {
418         switch (sk->sk_family) {
419         case AF_UNSPEC:
420         case AF_UNIX:
421                 return false;
422         default:
423                 return true;
424         }
425 }
426
427 static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
428 {
429         if (sk->sk_flags & flags) {
430                 sk->sk_flags &= ~flags;
431                 if (sock_needs_netstamp(sk) &&
432                     !(sk->sk_flags & SK_FLAGS_TIMESTAMP))
433                         net_disable_timestamp();
434         }
435 }
436
437
438 int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
439 {
440         unsigned long flags;
441         struct sk_buff_head *list = &sk->sk_receive_queue;
442
443         if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) {
444                 atomic_inc(&sk->sk_drops);
445                 trace_sock_rcvqueue_full(sk, skb);
446                 return -ENOMEM;
447         }
448
449         if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
450                 atomic_inc(&sk->sk_drops);
451                 return -ENOBUFS;
452         }
453
454         skb->dev = NULL;
455         skb_set_owner_r(skb, sk);
456
457         /* we escape from rcu protected region, make sure we dont leak
458          * a norefcounted dst
459          */
460         skb_dst_force(skb);
461
462         spin_lock_irqsave(&list->lock, flags);
463         sock_skb_set_dropcount(sk, skb);
464         __skb_queue_tail(list, skb);
465         spin_unlock_irqrestore(&list->lock, flags);
466
467         if (!sock_flag(sk, SOCK_DEAD))
468                 sk->sk_data_ready(sk);
469         return 0;
470 }
471 EXPORT_SYMBOL(__sock_queue_rcv_skb);
472
473 int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
474 {
475         int err;
476
477         err = sk_filter(sk, skb);
478         if (err)
479                 return err;
480
481         return __sock_queue_rcv_skb(sk, skb);
482 }
483 EXPORT_SYMBOL(sock_queue_rcv_skb);
484
485 int __sk_receive_skb(struct sock *sk, struct sk_buff *skb,
486                      const int nested, unsigned int trim_cap, bool refcounted)
487 {
488         int rc = NET_RX_SUCCESS;
489
490         if (sk_filter_trim_cap(sk, skb, trim_cap))
491                 goto discard_and_relse;
492
493         skb->dev = NULL;
494
495         if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
496                 atomic_inc(&sk->sk_drops);
497                 goto discard_and_relse;
498         }
499         if (nested)
500                 bh_lock_sock_nested(sk);
501         else
502                 bh_lock_sock(sk);
503         if (!sock_owned_by_user(sk)) {
504                 /*
505                  * trylock + unlock semantics:
506                  */
507                 mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
508
509                 rc = sk_backlog_rcv(sk, skb);
510
511                 mutex_release(&sk->sk_lock.dep_map, _RET_IP_);
512         } else if (sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf))) {
513                 bh_unlock_sock(sk);
514                 atomic_inc(&sk->sk_drops);
515                 goto discard_and_relse;
516         }
517
518         bh_unlock_sock(sk);
519 out:
520         if (refcounted)
521                 sock_put(sk);
522         return rc;
523 discard_and_relse:
524         kfree_skb(skb);
525         goto out;
526 }
527 EXPORT_SYMBOL(__sk_receive_skb);
528
529 struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
530 {
531         struct dst_entry *dst = __sk_dst_get(sk);
532
533         if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
534                 sk_tx_queue_clear(sk);
535                 sk->sk_dst_pending_confirm = 0;
536                 RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
537                 dst_release(dst);
538                 return NULL;
539         }
540
541         return dst;
542 }
543 EXPORT_SYMBOL(__sk_dst_check);
544
545 struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
546 {
547         struct dst_entry *dst = sk_dst_get(sk);
548
549         if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
550                 sk_dst_reset(sk);
551                 dst_release(dst);
552                 return NULL;
553         }
554
555         return dst;
556 }
557 EXPORT_SYMBOL(sk_dst_check);
558
559 static int sock_bindtoindex_locked(struct sock *sk, int ifindex)
560 {
561         int ret = -ENOPROTOOPT;
562 #ifdef CONFIG_NETDEVICES
563         struct net *net = sock_net(sk);
564
565         /* Sorry... */
566         ret = -EPERM;
567         if (sk->sk_bound_dev_if && !ns_capable(net->user_ns, CAP_NET_RAW))
568                 goto out;
569
570         ret = -EINVAL;
571         if (ifindex < 0)
572                 goto out;
573
574         sk->sk_bound_dev_if = ifindex;
575         if (sk->sk_prot->rehash)
576                 sk->sk_prot->rehash(sk);
577         sk_dst_reset(sk);
578
579         ret = 0;
580
581 out:
582 #endif
583
584         return ret;
585 }
586
587 int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk)
588 {
589         int ret;
590
591         if (lock_sk)
592                 lock_sock(sk);
593         ret = sock_bindtoindex_locked(sk, ifindex);
594         if (lock_sk)
595                 release_sock(sk);
596
597         return ret;
598 }
599 EXPORT_SYMBOL(sock_bindtoindex);
600
601 static int sock_setbindtodevice(struct sock *sk, sockptr_t optval, int optlen)
602 {
603         int ret = -ENOPROTOOPT;
604 #ifdef CONFIG_NETDEVICES
605         struct net *net = sock_net(sk);
606         char devname[IFNAMSIZ];
607         int index;
608
609         ret = -EINVAL;
610         if (optlen < 0)
611                 goto out;
612
613         /* Bind this socket to a particular device like "eth0",
614          * as specified in the passed interface name. If the
615          * name is "" or the option length is zero the socket
616          * is not bound.
617          */
618         if (optlen > IFNAMSIZ - 1)
619                 optlen = IFNAMSIZ - 1;
620         memset(devname, 0, sizeof(devname));
621
622         ret = -EFAULT;
623         if (copy_from_sockptr(devname, optval, optlen))
624                 goto out;
625
626         index = 0;
627         if (devname[0] != '\0') {
628                 struct net_device *dev;
629
630                 rcu_read_lock();
631                 dev = dev_get_by_name_rcu(net, devname);
632                 if (dev)
633                         index = dev->ifindex;
634                 rcu_read_unlock();
635                 ret = -ENODEV;
636                 if (!dev)
637                         goto out;
638         }
639
640         return sock_bindtoindex(sk, index, true);
641 out:
642 #endif
643
644         return ret;
645 }
646
647 static int sock_getbindtodevice(struct sock *sk, char __user *optval,
648                                 int __user *optlen, int len)
649 {
650         int ret = -ENOPROTOOPT;
651 #ifdef CONFIG_NETDEVICES
652         struct net *net = sock_net(sk);
653         char devname[IFNAMSIZ];
654
655         if (sk->sk_bound_dev_if == 0) {
656                 len = 0;
657                 goto zero;
658         }
659
660         ret = -EINVAL;
661         if (len < IFNAMSIZ)
662                 goto out;
663
664         ret = netdev_get_name(net, devname, sk->sk_bound_dev_if);
665         if (ret)
666                 goto out;
667
668         len = strlen(devname) + 1;
669
670         ret = -EFAULT;
671         if (copy_to_user(optval, devname, len))
672                 goto out;
673
674 zero:
675         ret = -EFAULT;
676         if (put_user(len, optlen))
677                 goto out;
678
679         ret = 0;
680
681 out:
682 #endif
683
684         return ret;
685 }
686
687 bool sk_mc_loop(struct sock *sk)
688 {
689         if (dev_recursion_level())
690                 return false;
691         if (!sk)
692                 return true;
693         switch (sk->sk_family) {
694         case AF_INET:
695                 return inet_sk(sk)->mc_loop;
696 #if IS_ENABLED(CONFIG_IPV6)
697         case AF_INET6:
698                 return inet6_sk(sk)->mc_loop;
699 #endif
700         }
701         WARN_ON_ONCE(1);
702         return true;
703 }
704 EXPORT_SYMBOL(sk_mc_loop);
705
706 void sock_set_reuseaddr(struct sock *sk)
707 {
708         lock_sock(sk);
709         sk->sk_reuse = SK_CAN_REUSE;
710         release_sock(sk);
711 }
712 EXPORT_SYMBOL(sock_set_reuseaddr);
713
714 void sock_set_reuseport(struct sock *sk)
715 {
716         lock_sock(sk);
717         sk->sk_reuseport = true;
718         release_sock(sk);
719 }
720 EXPORT_SYMBOL(sock_set_reuseport);
721
722 void sock_no_linger(struct sock *sk)
723 {
724         lock_sock(sk);
725         sk->sk_lingertime = 0;
726         sock_set_flag(sk, SOCK_LINGER);
727         release_sock(sk);
728 }
729 EXPORT_SYMBOL(sock_no_linger);
730
731 void sock_set_priority(struct sock *sk, u32 priority)
732 {
733         lock_sock(sk);
734         sk->sk_priority = priority;
735         release_sock(sk);
736 }
737 EXPORT_SYMBOL(sock_set_priority);
738
739 void sock_set_sndtimeo(struct sock *sk, s64 secs)
740 {
741         lock_sock(sk);
742         if (secs && secs < MAX_SCHEDULE_TIMEOUT / HZ - 1)
743                 sk->sk_sndtimeo = secs * HZ;
744         else
745                 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
746         release_sock(sk);
747 }
748 EXPORT_SYMBOL(sock_set_sndtimeo);
749
750 static void __sock_set_timestamps(struct sock *sk, bool val, bool new, bool ns)
751 {
752         if (val)  {
753                 sock_valbool_flag(sk, SOCK_TSTAMP_NEW, new);
754                 sock_valbool_flag(sk, SOCK_RCVTSTAMPNS, ns);
755                 sock_set_flag(sk, SOCK_RCVTSTAMP);
756                 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
757         } else {
758                 sock_reset_flag(sk, SOCK_RCVTSTAMP);
759                 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
760         }
761 }
762
763 void sock_enable_timestamps(struct sock *sk)
764 {
765         lock_sock(sk);
766         __sock_set_timestamps(sk, true, false, true);
767         release_sock(sk);
768 }
769 EXPORT_SYMBOL(sock_enable_timestamps);
770
771 void sock_set_keepalive(struct sock *sk)
772 {
773         lock_sock(sk);
774         if (sk->sk_prot->keepalive)
775                 sk->sk_prot->keepalive(sk, true);
776         sock_valbool_flag(sk, SOCK_KEEPOPEN, true);
777         release_sock(sk);
778 }
779 EXPORT_SYMBOL(sock_set_keepalive);
780
781 static void __sock_set_rcvbuf(struct sock *sk, int val)
782 {
783         /* Ensure val * 2 fits into an int, to prevent max_t() from treating it
784          * as a negative value.
785          */
786         val = min_t(int, val, INT_MAX / 2);
787         sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
788
789         /* We double it on the way in to account for "struct sk_buff" etc.
790          * overhead.   Applications assume that the SO_RCVBUF setting they make
791          * will allow that much actual data to be received on that socket.
792          *
793          * Applications are unaware that "struct sk_buff" and other overheads
794          * allocate from the receive buffer during socket buffer allocation.
795          *
796          * And after considering the possible alternatives, returning the value
797          * we actually used in getsockopt is the most desirable behavior.
798          */
799         WRITE_ONCE(sk->sk_rcvbuf, max_t(int, val * 2, SOCK_MIN_RCVBUF));
800 }
801
802 void sock_set_rcvbuf(struct sock *sk, int val)
803 {
804         lock_sock(sk);
805         __sock_set_rcvbuf(sk, val);
806         release_sock(sk);
807 }
808 EXPORT_SYMBOL(sock_set_rcvbuf);
809
810 void sock_set_mark(struct sock *sk, u32 val)
811 {
812         lock_sock(sk);
813         sk->sk_mark = val;
814         release_sock(sk);
815 }
816 EXPORT_SYMBOL(sock_set_mark);
817
818 /*
819  *      This is meant for all protocols to use and covers goings on
820  *      at the socket level. Everything here is generic.
821  */
822
823 int sock_setsockopt(struct socket *sock, int level, int optname,
824                     sockptr_t optval, unsigned int optlen)
825 {
826         struct sock_txtime sk_txtime;
827         struct sock *sk = sock->sk;
828         int val;
829         int valbool;
830         struct linger ling;
831         int ret = 0;
832
833         /*
834          *      Options without arguments
835          */
836
837         if (optname == SO_BINDTODEVICE)
838                 return sock_setbindtodevice(sk, optval, optlen);
839
840         if (optlen < sizeof(int))
841                 return -EINVAL;
842
843         if (copy_from_sockptr(&val, optval, sizeof(val)))
844                 return -EFAULT;
845
846         valbool = val ? 1 : 0;
847
848         lock_sock(sk);
849
850         switch (optname) {
851         case SO_DEBUG:
852                 if (val && !capable(CAP_NET_ADMIN))
853                         ret = -EACCES;
854                 else
855                         sock_valbool_flag(sk, SOCK_DBG, valbool);
856                 break;
857         case SO_REUSEADDR:
858                 sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
859                 break;
860         case SO_REUSEPORT:
861                 sk->sk_reuseport = valbool;
862                 break;
863         case SO_TYPE:
864         case SO_PROTOCOL:
865         case SO_DOMAIN:
866         case SO_ERROR:
867                 ret = -ENOPROTOOPT;
868                 break;
869         case SO_DONTROUTE:
870                 sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
871                 sk_dst_reset(sk);
872                 break;
873         case SO_BROADCAST:
874                 sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
875                 break;
876         case SO_SNDBUF:
877                 /* Don't error on this BSD doesn't and if you think
878                  * about it this is right. Otherwise apps have to
879                  * play 'guess the biggest size' games. RCVBUF/SNDBUF
880                  * are treated in BSD as hints
881                  */
882                 val = min_t(u32, val, sysctl_wmem_max);
883 set_sndbuf:
884                 /* Ensure val * 2 fits into an int, to prevent max_t()
885                  * from treating it as a negative value.
886                  */
887                 val = min_t(int, val, INT_MAX / 2);
888                 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
889                 WRITE_ONCE(sk->sk_sndbuf,
890                            max_t(int, val * 2, SOCK_MIN_SNDBUF));
891                 /* Wake up sending tasks if we upped the value. */
892                 sk->sk_write_space(sk);
893                 break;
894
895         case SO_SNDBUFFORCE:
896                 if (!capable(CAP_NET_ADMIN)) {
897                         ret = -EPERM;
898                         break;
899                 }
900
901                 /* No negative values (to prevent underflow, as val will be
902                  * multiplied by 2).
903                  */
904                 if (val < 0)
905                         val = 0;
906                 goto set_sndbuf;
907
908         case SO_RCVBUF:
909                 /* Don't error on this BSD doesn't and if you think
910                  * about it this is right. Otherwise apps have to
911                  * play 'guess the biggest size' games. RCVBUF/SNDBUF
912                  * are treated in BSD as hints
913                  */
914                 __sock_set_rcvbuf(sk, min_t(u32, val, sysctl_rmem_max));
915                 break;
916
917         case SO_RCVBUFFORCE:
918                 if (!capable(CAP_NET_ADMIN)) {
919                         ret = -EPERM;
920                         break;
921                 }
922
923                 /* No negative values (to prevent underflow, as val will be
924                  * multiplied by 2).
925                  */
926                 __sock_set_rcvbuf(sk, max(val, 0));
927                 break;
928
929         case SO_KEEPALIVE:
930                 if (sk->sk_prot->keepalive)
931                         sk->sk_prot->keepalive(sk, valbool);
932                 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
933                 break;
934
935         case SO_OOBINLINE:
936                 sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
937                 break;
938
939         case SO_NO_CHECK:
940                 sk->sk_no_check_tx = valbool;
941                 break;
942
943         case SO_PRIORITY:
944                 if ((val >= 0 && val <= 6) ||
945                     ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
946                         sk->sk_priority = val;
947                 else
948                         ret = -EPERM;
949                 break;
950
951         case SO_LINGER:
952                 if (optlen < sizeof(ling)) {
953                         ret = -EINVAL;  /* 1003.1g */
954                         break;
955                 }
956                 if (copy_from_sockptr(&ling, optval, sizeof(ling))) {
957                         ret = -EFAULT;
958                         break;
959                 }
960                 if (!ling.l_onoff)
961                         sock_reset_flag(sk, SOCK_LINGER);
962                 else {
963 #if (BITS_PER_LONG == 32)
964                         if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
965                                 sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
966                         else
967 #endif
968                                 sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
969                         sock_set_flag(sk, SOCK_LINGER);
970                 }
971                 break;
972
973         case SO_BSDCOMPAT:
974                 break;
975
976         case SO_PASSCRED:
977                 if (valbool)
978                         set_bit(SOCK_PASSCRED, &sock->flags);
979                 else
980                         clear_bit(SOCK_PASSCRED, &sock->flags);
981                 break;
982
983         case SO_TIMESTAMP_OLD:
984                 __sock_set_timestamps(sk, valbool, false, false);
985                 break;
986         case SO_TIMESTAMP_NEW:
987                 __sock_set_timestamps(sk, valbool, true, false);
988                 break;
989         case SO_TIMESTAMPNS_OLD:
990                 __sock_set_timestamps(sk, valbool, false, true);
991                 break;
992         case SO_TIMESTAMPNS_NEW:
993                 __sock_set_timestamps(sk, valbool, true, true);
994                 break;
995         case SO_TIMESTAMPING_NEW:
996         case SO_TIMESTAMPING_OLD:
997                 if (val & ~SOF_TIMESTAMPING_MASK) {
998                         ret = -EINVAL;
999                         break;
1000                 }
1001
1002                 if (val & SOF_TIMESTAMPING_OPT_ID &&
1003                     !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) {
1004                         if (sk->sk_protocol == IPPROTO_TCP &&
1005                             sk->sk_type == SOCK_STREAM) {
1006                                 if ((1 << sk->sk_state) &
1007                                     (TCPF_CLOSE | TCPF_LISTEN)) {
1008                                         ret = -EINVAL;
1009                                         break;
1010                                 }
1011                                 sk->sk_tskey = tcp_sk(sk)->snd_una;
1012                         } else {
1013                                 sk->sk_tskey = 0;
1014                         }
1015                 }
1016
1017                 if (val & SOF_TIMESTAMPING_OPT_STATS &&
1018                     !(val & SOF_TIMESTAMPING_OPT_TSONLY)) {
1019                         ret = -EINVAL;
1020                         break;
1021                 }
1022
1023                 sk->sk_tsflags = val;
1024                 sock_valbool_flag(sk, SOCK_TSTAMP_NEW, optname == SO_TIMESTAMPING_NEW);
1025
1026                 if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
1027                         sock_enable_timestamp(sk,
1028                                               SOCK_TIMESTAMPING_RX_SOFTWARE);
1029                 else
1030                         sock_disable_timestamp(sk,
1031                                                (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
1032                 break;
1033
1034         case SO_RCVLOWAT:
1035                 if (val < 0)
1036                         val = INT_MAX;
1037                 if (sock->ops->set_rcvlowat)
1038                         ret = sock->ops->set_rcvlowat(sk, val);
1039                 else
1040                         WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
1041                 break;
1042
1043         case SO_RCVTIMEO_OLD:
1044         case SO_RCVTIMEO_NEW:
1045                 ret = sock_set_timeout(&sk->sk_rcvtimeo, optval,
1046                                        optlen, optname == SO_RCVTIMEO_OLD);
1047                 break;
1048
1049         case SO_SNDTIMEO_OLD:
1050         case SO_SNDTIMEO_NEW:
1051                 ret = sock_set_timeout(&sk->sk_sndtimeo, optval,
1052                                        optlen, optname == SO_SNDTIMEO_OLD);
1053                 break;
1054
1055         case SO_ATTACH_FILTER: {
1056                 struct sock_fprog fprog;
1057
1058                 ret = copy_bpf_fprog_from_user(&fprog, optval, optlen);
1059                 if (!ret)
1060                         ret = sk_attach_filter(&fprog, sk);
1061                 break;
1062         }
1063         case SO_ATTACH_BPF:
1064                 ret = -EINVAL;
1065                 if (optlen == sizeof(u32)) {
1066                         u32 ufd;
1067
1068                         ret = -EFAULT;
1069                         if (copy_from_sockptr(&ufd, optval, sizeof(ufd)))
1070                                 break;
1071
1072                         ret = sk_attach_bpf(ufd, sk);
1073                 }
1074                 break;
1075
1076         case SO_ATTACH_REUSEPORT_CBPF: {
1077                 struct sock_fprog fprog;
1078
1079                 ret = copy_bpf_fprog_from_user(&fprog, optval, optlen);
1080                 if (!ret)
1081                         ret = sk_reuseport_attach_filter(&fprog, sk);
1082                 break;
1083         }
1084         case SO_ATTACH_REUSEPORT_EBPF:
1085                 ret = -EINVAL;
1086                 if (optlen == sizeof(u32)) {
1087                         u32 ufd;
1088
1089                         ret = -EFAULT;
1090                         if (copy_from_sockptr(&ufd, optval, sizeof(ufd)))
1091                                 break;
1092
1093                         ret = sk_reuseport_attach_bpf(ufd, sk);
1094                 }
1095                 break;
1096
1097         case SO_DETACH_REUSEPORT_BPF:
1098                 ret = reuseport_detach_prog(sk);
1099                 break;
1100
1101         case SO_DETACH_FILTER:
1102                 ret = sk_detach_filter(sk);
1103                 break;
1104
1105         case SO_LOCK_FILTER:
1106                 if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool)
1107                         ret = -EPERM;
1108                 else
1109                         sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool);
1110                 break;
1111
1112         case SO_PASSSEC:
1113                 if (valbool)
1114                         set_bit(SOCK_PASSSEC, &sock->flags);
1115                 else
1116                         clear_bit(SOCK_PASSSEC, &sock->flags);
1117                 break;
1118         case SO_MARK:
1119                 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1120                         ret = -EPERM;
1121                 } else if (val != sk->sk_mark) {
1122                         sk->sk_mark = val;
1123                         sk_dst_reset(sk);
1124                 }
1125                 break;
1126
1127         case SO_RXQ_OVFL:
1128                 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
1129                 break;
1130
1131         case SO_WIFI_STATUS:
1132                 sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
1133                 break;
1134
1135         case SO_PEEK_OFF:
1136                 if (sock->ops->set_peek_off)
1137                         ret = sock->ops->set_peek_off(sk, val);
1138                 else
1139                         ret = -EOPNOTSUPP;
1140                 break;
1141
1142         case SO_NOFCS:
1143                 sock_valbool_flag(sk, SOCK_NOFCS, valbool);
1144                 break;
1145
1146         case SO_SELECT_ERR_QUEUE:
1147                 sock_valbool_flag(sk, SOCK_SELECT_ERR_QUEUE, valbool);
1148                 break;
1149
1150 #ifdef CONFIG_NET_RX_BUSY_POLL
1151         case SO_BUSY_POLL:
1152                 /* allow unprivileged users to decrease the value */
1153                 if ((val > sk->sk_ll_usec) && !capable(CAP_NET_ADMIN))
1154                         ret = -EPERM;
1155                 else {
1156                         if (val < 0)
1157                                 ret = -EINVAL;
1158                         else
1159                                 sk->sk_ll_usec = val;
1160                 }
1161                 break;
1162         case SO_PREFER_BUSY_POLL:
1163                 if (valbool && !capable(CAP_NET_ADMIN))
1164                         ret = -EPERM;
1165                 else
1166                         WRITE_ONCE(sk->sk_prefer_busy_poll, valbool);
1167                 break;
1168         case SO_BUSY_POLL_BUDGET:
1169                 if (val > READ_ONCE(sk->sk_busy_poll_budget) && !capable(CAP_NET_ADMIN)) {
1170                         ret = -EPERM;
1171                 } else {
1172                         if (val < 0 || val > U16_MAX)
1173                                 ret = -EINVAL;
1174                         else
1175                                 WRITE_ONCE(sk->sk_busy_poll_budget, val);
1176                 }
1177                 break;
1178 #endif
1179
1180         case SO_MAX_PACING_RATE:
1181                 {
1182                 unsigned long ulval = (val == ~0U) ? ~0UL : (unsigned int)val;
1183
1184                 if (sizeof(ulval) != sizeof(val) &&
1185                     optlen >= sizeof(ulval) &&
1186                     copy_from_sockptr(&ulval, optval, sizeof(ulval))) {
1187                         ret = -EFAULT;
1188                         break;
1189                 }
1190                 if (ulval != ~0UL)
1191                         cmpxchg(&sk->sk_pacing_status,
1192                                 SK_PACING_NONE,
1193                                 SK_PACING_NEEDED);
1194                 sk->sk_max_pacing_rate = ulval;
1195                 sk->sk_pacing_rate = min(sk->sk_pacing_rate, ulval);
1196                 break;
1197                 }
1198         case SO_INCOMING_CPU:
1199                 WRITE_ONCE(sk->sk_incoming_cpu, val);
1200                 break;
1201
1202         case SO_CNX_ADVICE:
1203                 if (val == 1)
1204                         dst_negative_advice(sk);
1205                 break;
1206
1207         case SO_ZEROCOPY:
1208                 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6) {
1209                         if (!((sk->sk_type == SOCK_STREAM &&
1210                                sk->sk_protocol == IPPROTO_TCP) ||
1211                               (sk->sk_type == SOCK_DGRAM &&
1212                                sk->sk_protocol == IPPROTO_UDP)))
1213                                 ret = -ENOTSUPP;
1214                 } else if (sk->sk_family != PF_RDS) {
1215                         ret = -ENOTSUPP;
1216                 }
1217                 if (!ret) {
1218                         if (val < 0 || val > 1)
1219                                 ret = -EINVAL;
1220                         else
1221                                 sock_valbool_flag(sk, SOCK_ZEROCOPY, valbool);
1222                 }
1223                 break;
1224
1225         case SO_TXTIME:
1226                 if (optlen != sizeof(struct sock_txtime)) {
1227                         ret = -EINVAL;
1228                         break;
1229                 } else if (copy_from_sockptr(&sk_txtime, optval,
1230                            sizeof(struct sock_txtime))) {
1231                         ret = -EFAULT;
1232                         break;
1233                 } else if (sk_txtime.flags & ~SOF_TXTIME_FLAGS_MASK) {
1234                         ret = -EINVAL;
1235                         break;
1236                 }
1237                 /* CLOCK_MONOTONIC is only used by sch_fq, and this packet
1238                  * scheduler has enough safe guards.
1239                  */
1240                 if (sk_txtime.clockid != CLOCK_MONOTONIC &&
1241                     !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1242                         ret = -EPERM;
1243                         break;
1244                 }
1245                 sock_valbool_flag(sk, SOCK_TXTIME, true);
1246                 sk->sk_clockid = sk_txtime.clockid;
1247                 sk->sk_txtime_deadline_mode =
1248                         !!(sk_txtime.flags & SOF_TXTIME_DEADLINE_MODE);
1249                 sk->sk_txtime_report_errors =
1250                         !!(sk_txtime.flags & SOF_TXTIME_REPORT_ERRORS);
1251                 break;
1252
1253         case SO_BINDTOIFINDEX:
1254                 ret = sock_bindtoindex_locked(sk, val);
1255                 break;
1256
1257         default:
1258                 ret = -ENOPROTOOPT;
1259                 break;
1260         }
1261         release_sock(sk);
1262         return ret;
1263 }
1264 EXPORT_SYMBOL(sock_setsockopt);
1265
1266
1267 static void cred_to_ucred(struct pid *pid, const struct cred *cred,
1268                           struct ucred *ucred)
1269 {
1270         ucred->pid = pid_vnr(pid);
1271         ucred->uid = ucred->gid = -1;
1272         if (cred) {
1273                 struct user_namespace *current_ns = current_user_ns();
1274
1275                 ucred->uid = from_kuid_munged(current_ns, cred->euid);
1276                 ucred->gid = from_kgid_munged(current_ns, cred->egid);
1277         }
1278 }
1279
1280 static int groups_to_user(gid_t __user *dst, const struct group_info *src)
1281 {
1282         struct user_namespace *user_ns = current_user_ns();
1283         int i;
1284
1285         for (i = 0; i < src->ngroups; i++)
1286                 if (put_user(from_kgid_munged(user_ns, src->gid[i]), dst + i))
1287                         return -EFAULT;
1288
1289         return 0;
1290 }
1291
1292 int sock_getsockopt(struct socket *sock, int level, int optname,
1293                     char __user *optval, int __user *optlen)
1294 {
1295         struct sock *sk = sock->sk;
1296
1297         union {
1298                 int val;
1299                 u64 val64;
1300                 unsigned long ulval;
1301                 struct linger ling;
1302                 struct old_timeval32 tm32;
1303                 struct __kernel_old_timeval tm;
1304                 struct  __kernel_sock_timeval stm;
1305                 struct sock_txtime txtime;
1306         } v;
1307
1308         int lv = sizeof(int);
1309         int len;
1310
1311         if (get_user(len, optlen))
1312                 return -EFAULT;
1313         if (len < 0)
1314                 return -EINVAL;
1315
1316         memset(&v, 0, sizeof(v));
1317
1318         switch (optname) {
1319         case SO_DEBUG:
1320                 v.val = sock_flag(sk, SOCK_DBG);
1321                 break;
1322
1323         case SO_DONTROUTE:
1324                 v.val = sock_flag(sk, SOCK_LOCALROUTE);
1325                 break;
1326
1327         case SO_BROADCAST:
1328                 v.val = sock_flag(sk, SOCK_BROADCAST);
1329                 break;
1330
1331         case SO_SNDBUF:
1332                 v.val = sk->sk_sndbuf;
1333                 break;
1334
1335         case SO_RCVBUF:
1336                 v.val = sk->sk_rcvbuf;
1337                 break;
1338
1339         case SO_REUSEADDR:
1340                 v.val = sk->sk_reuse;
1341                 break;
1342
1343         case SO_REUSEPORT:
1344                 v.val = sk->sk_reuseport;
1345                 break;
1346
1347         case SO_KEEPALIVE:
1348                 v.val = sock_flag(sk, SOCK_KEEPOPEN);
1349                 break;
1350
1351         case SO_TYPE:
1352                 v.val = sk->sk_type;
1353                 break;
1354
1355         case SO_PROTOCOL:
1356                 v.val = sk->sk_protocol;
1357                 break;
1358
1359         case SO_DOMAIN:
1360                 v.val = sk->sk_family;
1361                 break;
1362
1363         case SO_ERROR:
1364                 v.val = -sock_error(sk);
1365                 if (v.val == 0)
1366                         v.val = xchg(&sk->sk_err_soft, 0);
1367                 break;
1368
1369         case SO_OOBINLINE:
1370                 v.val = sock_flag(sk, SOCK_URGINLINE);
1371                 break;
1372
1373         case SO_NO_CHECK:
1374                 v.val = sk->sk_no_check_tx;
1375                 break;
1376
1377         case SO_PRIORITY:
1378                 v.val = sk->sk_priority;
1379                 break;
1380
1381         case SO_LINGER:
1382                 lv              = sizeof(v.ling);
1383                 v.ling.l_onoff  = sock_flag(sk, SOCK_LINGER);
1384                 v.ling.l_linger = sk->sk_lingertime / HZ;
1385                 break;
1386
1387         case SO_BSDCOMPAT:
1388                 break;
1389
1390         case SO_TIMESTAMP_OLD:
1391                 v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
1392                                 !sock_flag(sk, SOCK_TSTAMP_NEW) &&
1393                                 !sock_flag(sk, SOCK_RCVTSTAMPNS);
1394                 break;
1395
1396         case SO_TIMESTAMPNS_OLD:
1397                 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS) && !sock_flag(sk, SOCK_TSTAMP_NEW);
1398                 break;
1399
1400         case SO_TIMESTAMP_NEW:
1401                 v.val = sock_flag(sk, SOCK_RCVTSTAMP) && sock_flag(sk, SOCK_TSTAMP_NEW);
1402                 break;
1403
1404         case SO_TIMESTAMPNS_NEW:
1405                 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS) && sock_flag(sk, SOCK_TSTAMP_NEW);
1406                 break;
1407
1408         case SO_TIMESTAMPING_OLD:
1409                 v.val = sk->sk_tsflags;
1410                 break;
1411
1412         case SO_RCVTIMEO_OLD:
1413         case SO_RCVTIMEO_NEW:
1414                 lv = sock_get_timeout(sk->sk_rcvtimeo, &v, SO_RCVTIMEO_OLD == optname);
1415                 break;
1416
1417         case SO_SNDTIMEO_OLD:
1418         case SO_SNDTIMEO_NEW:
1419                 lv = sock_get_timeout(sk->sk_sndtimeo, &v, SO_SNDTIMEO_OLD == optname);
1420                 break;
1421
1422         case SO_RCVLOWAT:
1423                 v.val = sk->sk_rcvlowat;
1424                 break;
1425
1426         case SO_SNDLOWAT:
1427                 v.val = 1;
1428                 break;
1429
1430         case SO_PASSCRED:
1431                 v.val = !!test_bit(SOCK_PASSCRED, &sock->flags);
1432                 break;
1433
1434         case SO_PEERCRED:
1435         {
1436                 struct ucred peercred;
1437                 if (len > sizeof(peercred))
1438                         len = sizeof(peercred);
1439                 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
1440                 if (copy_to_user(optval, &peercred, len))
1441                         return -EFAULT;
1442                 goto lenout;
1443         }
1444
1445         case SO_PEERGROUPS:
1446         {
1447                 int ret, n;
1448
1449                 if (!sk->sk_peer_cred)
1450                         return -ENODATA;
1451
1452                 n = sk->sk_peer_cred->group_info->ngroups;
1453                 if (len < n * sizeof(gid_t)) {
1454                         len = n * sizeof(gid_t);
1455                         return put_user(len, optlen) ? -EFAULT : -ERANGE;
1456                 }
1457                 len = n * sizeof(gid_t);
1458
1459                 ret = groups_to_user((gid_t __user *)optval,
1460                                      sk->sk_peer_cred->group_info);
1461                 if (ret)
1462                         return ret;
1463                 goto lenout;
1464         }
1465
1466         case SO_PEERNAME:
1467         {
1468                 char address[128];
1469
1470                 lv = sock->ops->getname(sock, (struct sockaddr *)address, 2);
1471                 if (lv < 0)
1472                         return -ENOTCONN;
1473                 if (lv < len)
1474                         return -EINVAL;
1475                 if (copy_to_user(optval, address, len))
1476                         return -EFAULT;
1477                 goto lenout;
1478         }
1479
1480         /* Dubious BSD thing... Probably nobody even uses it, but
1481          * the UNIX standard wants it for whatever reason... -DaveM
1482          */
1483         case SO_ACCEPTCONN:
1484                 v.val = sk->sk_state == TCP_LISTEN;
1485                 break;
1486
1487         case SO_PASSSEC:
1488                 v.val = !!test_bit(SOCK_PASSSEC, &sock->flags);
1489                 break;
1490
1491         case SO_PEERSEC:
1492                 return security_socket_getpeersec_stream(sock, optval, optlen, len);
1493
1494         case SO_MARK:
1495                 v.val = sk->sk_mark;
1496                 break;
1497
1498         case SO_RXQ_OVFL:
1499                 v.val = sock_flag(sk, SOCK_RXQ_OVFL);
1500                 break;
1501
1502         case SO_WIFI_STATUS:
1503                 v.val = sock_flag(sk, SOCK_WIFI_STATUS);
1504                 break;
1505
1506         case SO_PEEK_OFF:
1507                 if (!sock->ops->set_peek_off)
1508                         return -EOPNOTSUPP;
1509
1510                 v.val = sk->sk_peek_off;
1511                 break;
1512         case SO_NOFCS:
1513                 v.val = sock_flag(sk, SOCK_NOFCS);
1514                 break;
1515
1516         case SO_BINDTODEVICE:
1517                 return sock_getbindtodevice(sk, optval, optlen, len);
1518
1519         case SO_GET_FILTER:
1520                 len = sk_get_filter(sk, (struct sock_filter __user *)optval, len);
1521                 if (len < 0)
1522                         return len;
1523
1524                 goto lenout;
1525
1526         case SO_LOCK_FILTER:
1527                 v.val = sock_flag(sk, SOCK_FILTER_LOCKED);
1528                 break;
1529
1530         case SO_BPF_EXTENSIONS:
1531                 v.val = bpf_tell_extensions();
1532                 break;
1533
1534         case SO_SELECT_ERR_QUEUE:
1535                 v.val = sock_flag(sk, SOCK_SELECT_ERR_QUEUE);
1536                 break;
1537
1538 #ifdef CONFIG_NET_RX_BUSY_POLL
1539         case SO_BUSY_POLL:
1540                 v.val = sk->sk_ll_usec;
1541                 break;
1542         case SO_PREFER_BUSY_POLL:
1543                 v.val = READ_ONCE(sk->sk_prefer_busy_poll);
1544                 break;
1545 #endif
1546
1547         case SO_MAX_PACING_RATE:
1548                 if (sizeof(v.ulval) != sizeof(v.val) && len >= sizeof(v.ulval)) {
1549                         lv = sizeof(v.ulval);
1550                         v.ulval = sk->sk_max_pacing_rate;
1551                 } else {
1552                         /* 32bit version */
1553                         v.val = min_t(unsigned long, sk->sk_max_pacing_rate, ~0U);
1554                 }
1555                 break;
1556
1557         case SO_INCOMING_CPU:
1558                 v.val = READ_ONCE(sk->sk_incoming_cpu);
1559                 break;
1560
1561         case SO_MEMINFO:
1562         {
1563                 u32 meminfo[SK_MEMINFO_VARS];
1564
1565                 sk_get_meminfo(sk, meminfo);
1566
1567                 len = min_t(unsigned int, len, sizeof(meminfo));
1568                 if (copy_to_user(optval, &meminfo, len))
1569                         return -EFAULT;
1570
1571                 goto lenout;
1572         }
1573
1574 #ifdef CONFIG_NET_RX_BUSY_POLL
1575         case SO_INCOMING_NAPI_ID:
1576                 v.val = READ_ONCE(sk->sk_napi_id);
1577
1578                 /* aggregate non-NAPI IDs down to 0 */
1579                 if (v.val < MIN_NAPI_ID)
1580                         v.val = 0;
1581
1582                 break;
1583 #endif
1584
1585         case SO_COOKIE:
1586                 lv = sizeof(u64);
1587                 if (len < lv)
1588                         return -EINVAL;
1589                 v.val64 = sock_gen_cookie(sk);
1590                 break;
1591
1592         case SO_ZEROCOPY:
1593                 v.val = sock_flag(sk, SOCK_ZEROCOPY);
1594                 break;
1595
1596         case SO_TXTIME:
1597                 lv = sizeof(v.txtime);
1598                 v.txtime.clockid = sk->sk_clockid;
1599                 v.txtime.flags |= sk->sk_txtime_deadline_mode ?
1600                                   SOF_TXTIME_DEADLINE_MODE : 0;
1601                 v.txtime.flags |= sk->sk_txtime_report_errors ?
1602                                   SOF_TXTIME_REPORT_ERRORS : 0;
1603                 break;
1604
1605         case SO_BINDTOIFINDEX:
1606                 v.val = sk->sk_bound_dev_if;
1607                 break;
1608
1609         default:
1610                 /* We implement the SO_SNDLOWAT etc to not be settable
1611                  * (1003.1g 7).
1612                  */
1613                 return -ENOPROTOOPT;
1614         }
1615
1616         if (len > lv)
1617                 len = lv;
1618         if (copy_to_user(optval, &v, len))
1619                 return -EFAULT;
1620 lenout:
1621         if (put_user(len, optlen))
1622                 return -EFAULT;
1623         return 0;
1624 }
1625
1626 /*
1627  * Initialize an sk_lock.
1628  *
1629  * (We also register the sk_lock with the lock validator.)
1630  */
1631 static inline void sock_lock_init(struct sock *sk)
1632 {
1633         if (sk->sk_kern_sock)
1634                 sock_lock_init_class_and_name(
1635                         sk,
1636                         af_family_kern_slock_key_strings[sk->sk_family],
1637                         af_family_kern_slock_keys + sk->sk_family,
1638                         af_family_kern_key_strings[sk->sk_family],
1639                         af_family_kern_keys + sk->sk_family);
1640         else
1641                 sock_lock_init_class_and_name(
1642                         sk,
1643                         af_family_slock_key_strings[sk->sk_family],
1644                         af_family_slock_keys + sk->sk_family,
1645                         af_family_key_strings[sk->sk_family],
1646                         af_family_keys + sk->sk_family);
1647 }
1648
1649 /*
1650  * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
1651  * even temporarly, because of RCU lookups. sk_node should also be left as is.
1652  * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
1653  */
1654 static void sock_copy(struct sock *nsk, const struct sock *osk)
1655 {
1656         const struct proto *prot = READ_ONCE(osk->sk_prot);
1657 #ifdef CONFIG_SECURITY_NETWORK
1658         void *sptr = nsk->sk_security;
1659 #endif
1660
1661         /* If we move sk_tx_queue_mapping out of the private section,
1662          * we must check if sk_tx_queue_clear() is called after
1663          * sock_copy() in sk_clone_lock().
1664          */
1665         BUILD_BUG_ON(offsetof(struct sock, sk_tx_queue_mapping) <
1666                      offsetof(struct sock, sk_dontcopy_begin) ||
1667                      offsetof(struct sock, sk_tx_queue_mapping) >=
1668                      offsetof(struct sock, sk_dontcopy_end));
1669
1670         memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
1671
1672         memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
1673                prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
1674
1675 #ifdef CONFIG_SECURITY_NETWORK
1676         nsk->sk_security = sptr;
1677         security_sk_clone(osk, nsk);
1678 #endif
1679 }
1680
1681 static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
1682                 int family)
1683 {
1684         struct sock *sk;
1685         struct kmem_cache *slab;
1686
1687         slab = prot->slab;
1688         if (slab != NULL) {
1689                 sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
1690                 if (!sk)
1691                         return sk;
1692                 if (want_init_on_alloc(priority))
1693                         sk_prot_clear_nulls(sk, prot->obj_size);
1694         } else
1695                 sk = kmalloc(prot->obj_size, priority);
1696
1697         if (sk != NULL) {
1698                 if (security_sk_alloc(sk, family, priority))
1699                         goto out_free;
1700
1701                 if (!try_module_get(prot->owner))
1702                         goto out_free_sec;
1703         }
1704
1705         return sk;
1706
1707 out_free_sec:
1708         security_sk_free(sk);
1709 out_free:
1710         if (slab != NULL)
1711                 kmem_cache_free(slab, sk);
1712         else
1713                 kfree(sk);
1714         return NULL;
1715 }
1716
1717 static void sk_prot_free(struct proto *prot, struct sock *sk)
1718 {
1719         struct kmem_cache *slab;
1720         struct module *owner;
1721
1722         owner = prot->owner;
1723         slab = prot->slab;
1724
1725         cgroup_sk_free(&sk->sk_cgrp_data);
1726         mem_cgroup_sk_free(sk);
1727         security_sk_free(sk);
1728         if (slab != NULL)
1729                 kmem_cache_free(slab, sk);
1730         else
1731                 kfree(sk);
1732         module_put(owner);
1733 }
1734
1735 /**
1736  *      sk_alloc - All socket objects are allocated here
1737  *      @net: the applicable net namespace
1738  *      @family: protocol family
1739  *      @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1740  *      @prot: struct proto associated with this new sock instance
1741  *      @kern: is this to be a kernel socket?
1742  */
1743 struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
1744                       struct proto *prot, int kern)
1745 {
1746         struct sock *sk;
1747
1748         sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
1749         if (sk) {
1750                 sk->sk_family = family;
1751                 /*
1752                  * See comment in struct sock definition to understand
1753                  * why we need sk_prot_creator -acme
1754                  */
1755                 sk->sk_prot = sk->sk_prot_creator = prot;
1756                 sk->sk_kern_sock = kern;
1757                 sock_lock_init(sk);
1758                 sk->sk_net_refcnt = kern ? 0 : 1;
1759                 if (likely(sk->sk_net_refcnt)) {
1760                         get_net(net);
1761                         sock_inuse_add(net, 1);
1762                 }
1763
1764                 sock_net_set(sk, net);
1765                 refcount_set(&sk->sk_wmem_alloc, 1);
1766
1767                 mem_cgroup_sk_alloc(sk);
1768                 cgroup_sk_alloc(&sk->sk_cgrp_data);
1769                 sock_update_classid(&sk->sk_cgrp_data);
1770                 sock_update_netprioidx(&sk->sk_cgrp_data);
1771                 sk_tx_queue_clear(sk);
1772         }
1773
1774         return sk;
1775 }
1776 EXPORT_SYMBOL(sk_alloc);
1777
1778 /* Sockets having SOCK_RCU_FREE will call this function after one RCU
1779  * grace period. This is the case for UDP sockets and TCP listeners.
1780  */
1781 static void __sk_destruct(struct rcu_head *head)
1782 {
1783         struct sock *sk = container_of(head, struct sock, sk_rcu);
1784         struct sk_filter *filter;
1785
1786         if (sk->sk_destruct)
1787                 sk->sk_destruct(sk);
1788
1789         filter = rcu_dereference_check(sk->sk_filter,
1790                                        refcount_read(&sk->sk_wmem_alloc) == 0);
1791         if (filter) {
1792                 sk_filter_uncharge(sk, filter);
1793                 RCU_INIT_POINTER(sk->sk_filter, NULL);
1794         }
1795
1796         sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
1797
1798 #ifdef CONFIG_BPF_SYSCALL
1799         bpf_sk_storage_free(sk);
1800 #endif
1801
1802         if (atomic_read(&sk->sk_omem_alloc))
1803                 pr_debug("%s: optmem leakage (%d bytes) detected\n",
1804                          __func__, atomic_read(&sk->sk_omem_alloc));
1805
1806         if (sk->sk_frag.page) {
1807                 put_page(sk->sk_frag.page);
1808                 sk->sk_frag.page = NULL;
1809         }
1810
1811         if (sk->sk_peer_cred)
1812                 put_cred(sk->sk_peer_cred);
1813         put_pid(sk->sk_peer_pid);
1814         if (likely(sk->sk_net_refcnt))
1815                 put_net(sock_net(sk));
1816         sk_prot_free(sk->sk_prot_creator, sk);
1817 }
1818
1819 void sk_destruct(struct sock *sk)
1820 {
1821         bool use_call_rcu = sock_flag(sk, SOCK_RCU_FREE);
1822
1823         if (rcu_access_pointer(sk->sk_reuseport_cb)) {
1824                 reuseport_detach_sock(sk);
1825                 use_call_rcu = true;
1826         }
1827
1828         if (use_call_rcu)
1829                 call_rcu(&sk->sk_rcu, __sk_destruct);
1830         else
1831                 __sk_destruct(&sk->sk_rcu);
1832 }
1833
1834 static void __sk_free(struct sock *sk)
1835 {
1836         if (likely(sk->sk_net_refcnt))
1837                 sock_inuse_add(sock_net(sk), -1);
1838
1839         if (unlikely(sk->sk_net_refcnt && sock_diag_has_destroy_listeners(sk)))
1840                 sock_diag_broadcast_destroy(sk);
1841         else
1842                 sk_destruct(sk);
1843 }
1844
1845 void sk_free(struct sock *sk)
1846 {
1847         /*
1848          * We subtract one from sk_wmem_alloc and can know if
1849          * some packets are still in some tx queue.
1850          * If not null, sock_wfree() will call __sk_free(sk) later
1851          */
1852         if (refcount_dec_and_test(&sk->sk_wmem_alloc))
1853                 __sk_free(sk);
1854 }
1855 EXPORT_SYMBOL(sk_free);
1856
1857 static void sk_init_common(struct sock *sk)
1858 {
1859         skb_queue_head_init(&sk->sk_receive_queue);
1860         skb_queue_head_init(&sk->sk_write_queue);
1861         skb_queue_head_init(&sk->sk_error_queue);
1862
1863         rwlock_init(&sk->sk_callback_lock);
1864         lockdep_set_class_and_name(&sk->sk_receive_queue.lock,
1865                         af_rlock_keys + sk->sk_family,
1866                         af_family_rlock_key_strings[sk->sk_family]);
1867         lockdep_set_class_and_name(&sk->sk_write_queue.lock,
1868                         af_wlock_keys + sk->sk_family,
1869                         af_family_wlock_key_strings[sk->sk_family]);
1870         lockdep_set_class_and_name(&sk->sk_error_queue.lock,
1871                         af_elock_keys + sk->sk_family,
1872                         af_family_elock_key_strings[sk->sk_family]);
1873         lockdep_set_class_and_name(&sk->sk_callback_lock,
1874                         af_callback_keys + sk->sk_family,
1875                         af_family_clock_key_strings[sk->sk_family]);
1876 }
1877
1878 /**
1879  *      sk_clone_lock - clone a socket, and lock its clone
1880  *      @sk: the socket to clone
1881  *      @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1882  *
1883  *      Caller must unlock socket even in error path (bh_unlock_sock(newsk))
1884  */
1885 struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
1886 {
1887         struct proto *prot = READ_ONCE(sk->sk_prot);
1888         struct sk_filter *filter;
1889         bool is_charged = true;
1890         struct sock *newsk;
1891
1892         newsk = sk_prot_alloc(prot, priority, sk->sk_family);
1893         if (!newsk)
1894                 goto out;
1895
1896         sock_copy(newsk, sk);
1897
1898         newsk->sk_prot_creator = prot;
1899
1900         /* SANITY */
1901         if (likely(newsk->sk_net_refcnt))
1902                 get_net(sock_net(newsk));
1903         sk_node_init(&newsk->sk_node);
1904         sock_lock_init(newsk);
1905         bh_lock_sock(newsk);
1906         newsk->sk_backlog.head  = newsk->sk_backlog.tail = NULL;
1907         newsk->sk_backlog.len = 0;
1908
1909         atomic_set(&newsk->sk_rmem_alloc, 0);
1910
1911         /* sk_wmem_alloc set to one (see sk_free() and sock_wfree()) */
1912         refcount_set(&newsk->sk_wmem_alloc, 1);
1913
1914         atomic_set(&newsk->sk_omem_alloc, 0);
1915         sk_init_common(newsk);
1916
1917         newsk->sk_dst_cache     = NULL;
1918         newsk->sk_dst_pending_confirm = 0;
1919         newsk->sk_wmem_queued   = 0;
1920         newsk->sk_forward_alloc = 0;
1921         atomic_set(&newsk->sk_drops, 0);
1922         newsk->sk_send_head     = NULL;
1923         newsk->sk_userlocks     = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
1924         atomic_set(&newsk->sk_zckey, 0);
1925
1926         sock_reset_flag(newsk, SOCK_DONE);
1927
1928         /* sk->sk_memcg will be populated at accept() time */
1929         newsk->sk_memcg = NULL;
1930
1931         cgroup_sk_clone(&newsk->sk_cgrp_data);
1932
1933         rcu_read_lock();
1934         filter = rcu_dereference(sk->sk_filter);
1935         if (filter != NULL)
1936                 /* though it's an empty new sock, the charging may fail
1937                  * if sysctl_optmem_max was changed between creation of
1938                  * original socket and cloning
1939                  */
1940                 is_charged = sk_filter_charge(newsk, filter);
1941         RCU_INIT_POINTER(newsk->sk_filter, filter);
1942         rcu_read_unlock();
1943
1944         if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
1945                 /* We need to make sure that we don't uncharge the new
1946                  * socket if we couldn't charge it in the first place
1947                  * as otherwise we uncharge the parent's filter.
1948                  */
1949                 if (!is_charged)
1950                         RCU_INIT_POINTER(newsk->sk_filter, NULL);
1951                 sk_free_unlock_clone(newsk);
1952                 newsk = NULL;
1953                 goto out;
1954         }
1955         RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
1956
1957         if (bpf_sk_storage_clone(sk, newsk)) {
1958                 sk_free_unlock_clone(newsk);
1959                 newsk = NULL;
1960                 goto out;
1961         }
1962
1963         /* Clear sk_user_data if parent had the pointer tagged
1964          * as not suitable for copying when cloning.
1965          */
1966         if (sk_user_data_is_nocopy(newsk))
1967                 newsk->sk_user_data = NULL;
1968
1969         newsk->sk_err      = 0;
1970         newsk->sk_err_soft = 0;
1971         newsk->sk_priority = 0;
1972         newsk->sk_incoming_cpu = raw_smp_processor_id();
1973         if (likely(newsk->sk_net_refcnt))
1974                 sock_inuse_add(sock_net(newsk), 1);
1975
1976         /* Before updating sk_refcnt, we must commit prior changes to memory
1977          * (Documentation/RCU/rculist_nulls.rst for details)
1978          */
1979         smp_wmb();
1980         refcount_set(&newsk->sk_refcnt, 2);
1981
1982         /* Increment the counter in the same struct proto as the master
1983          * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
1984          * is the same as sk->sk_prot->socks, as this field was copied
1985          * with memcpy).
1986          *
1987          * This _changes_ the previous behaviour, where
1988          * tcp_create_openreq_child always was incrementing the
1989          * equivalent to tcp_prot->socks (inet_sock_nr), so this have
1990          * to be taken into account in all callers. -acme
1991          */
1992         sk_refcnt_debug_inc(newsk);
1993         sk_set_socket(newsk, NULL);
1994         sk_tx_queue_clear(newsk);
1995         RCU_INIT_POINTER(newsk->sk_wq, NULL);
1996
1997         if (newsk->sk_prot->sockets_allocated)
1998                 sk_sockets_allocated_inc(newsk);
1999
2000         if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP)
2001                 net_enable_timestamp();
2002 out:
2003         return newsk;
2004 }
2005 EXPORT_SYMBOL_GPL(sk_clone_lock);
2006
2007 void sk_free_unlock_clone(struct sock *sk)
2008 {
2009         /* It is still raw copy of parent, so invalidate
2010          * destructor and make plain sk_free() */
2011         sk->sk_destruct = NULL;
2012         bh_unlock_sock(sk);
2013         sk_free(sk);
2014 }
2015 EXPORT_SYMBOL_GPL(sk_free_unlock_clone);
2016
2017 void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
2018 {
2019         u32 max_segs = 1;
2020
2021         sk_dst_set(sk, dst);
2022         sk->sk_route_caps = dst->dev->features | sk->sk_route_forced_caps;
2023         if (sk->sk_route_caps & NETIF_F_GSO)
2024                 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
2025         sk->sk_route_caps &= ~sk->sk_route_nocaps;
2026         if (sk_can_gso(sk)) {
2027                 if (dst->header_len && !xfrm_dst_offload_ok(dst)) {
2028                         sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
2029                 } else {
2030                         sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
2031                         sk->sk_gso_max_size = dst->dev->gso_max_size;
2032                         max_segs = max_t(u32, dst->dev->gso_max_segs, 1);
2033                 }
2034         }
2035         sk->sk_gso_max_segs = max_segs;
2036 }
2037 EXPORT_SYMBOL_GPL(sk_setup_caps);
2038
2039 /*
2040  *      Simple resource managers for sockets.
2041  */
2042
2043
2044 /*
2045  * Write buffer destructor automatically called from kfree_skb.
2046  */
2047 void sock_wfree(struct sk_buff *skb)
2048 {
2049         struct sock *sk = skb->sk;
2050         unsigned int len = skb->truesize;
2051
2052         if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
2053                 /*
2054                  * Keep a reference on sk_wmem_alloc, this will be released
2055                  * after sk_write_space() call
2056                  */
2057                 WARN_ON(refcount_sub_and_test(len - 1, &sk->sk_wmem_alloc));
2058                 sk->sk_write_space(sk);
2059                 len = 1;
2060         }
2061         /*
2062          * if sk_wmem_alloc reaches 0, we must finish what sk_free()
2063          * could not do because of in-flight packets
2064          */
2065         if (refcount_sub_and_test(len, &sk->sk_wmem_alloc))
2066                 __sk_free(sk);
2067 }
2068 EXPORT_SYMBOL(sock_wfree);
2069
2070 /* This variant of sock_wfree() is used by TCP,
2071  * since it sets SOCK_USE_WRITE_QUEUE.
2072  */
2073 void __sock_wfree(struct sk_buff *skb)
2074 {
2075         struct sock *sk = skb->sk;
2076
2077         if (refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc))
2078                 __sk_free(sk);
2079 }
2080
2081 void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
2082 {
2083         skb_orphan(skb);
2084         skb->sk = sk;
2085 #ifdef CONFIG_INET
2086         if (unlikely(!sk_fullsock(sk))) {
2087                 skb->destructor = sock_edemux;
2088                 sock_hold(sk);
2089                 return;
2090         }
2091 #endif
2092         skb->destructor = sock_wfree;
2093         skb_set_hash_from_sk(skb, sk);
2094         /*
2095          * We used to take a refcount on sk, but following operation
2096          * is enough to guarantee sk_free() wont free this sock until
2097          * all in-flight packets are completed
2098          */
2099         refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2100 }
2101 EXPORT_SYMBOL(skb_set_owner_w);
2102
2103 static bool can_skb_orphan_partial(const struct sk_buff *skb)
2104 {
2105 #ifdef CONFIG_TLS_DEVICE
2106         /* Drivers depend on in-order delivery for crypto offload,
2107          * partial orphan breaks out-of-order-OK logic.
2108          */
2109         if (skb->decrypted)
2110                 return false;
2111 #endif
2112         return (skb->destructor == sock_wfree ||
2113                 (IS_ENABLED(CONFIG_INET) && skb->destructor == tcp_wfree));
2114 }
2115
2116 /* This helper is used by netem, as it can hold packets in its
2117  * delay queue. We want to allow the owner socket to send more
2118  * packets, as if they were already TX completed by a typical driver.
2119  * But we also want to keep skb->sk set because some packet schedulers
2120  * rely on it (sch_fq for example).
2121  */
2122 void skb_orphan_partial(struct sk_buff *skb)
2123 {
2124         if (skb_is_tcp_pure_ack(skb))
2125                 return;
2126
2127         if (can_skb_orphan_partial(skb)) {
2128                 struct sock *sk = skb->sk;
2129
2130                 if (refcount_inc_not_zero(&sk->sk_refcnt)) {
2131                         WARN_ON(refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc));
2132                         skb->destructor = sock_efree;
2133                 }
2134         } else {
2135                 skb_orphan(skb);
2136         }
2137 }
2138 EXPORT_SYMBOL(skb_orphan_partial);
2139
2140 /*
2141  * Read buffer destructor automatically called from kfree_skb.
2142  */
2143 void sock_rfree(struct sk_buff *skb)
2144 {
2145         struct sock *sk = skb->sk;
2146         unsigned int len = skb->truesize;
2147
2148         atomic_sub(len, &sk->sk_rmem_alloc);
2149         sk_mem_uncharge(sk, len);
2150 }
2151 EXPORT_SYMBOL(sock_rfree);
2152
2153 /*
2154  * Buffer destructor for skbs that are not used directly in read or write
2155  * path, e.g. for error handler skbs. Automatically called from kfree_skb.
2156  */
2157 void sock_efree(struct sk_buff *skb)
2158 {
2159         sock_put(skb->sk);
2160 }
2161 EXPORT_SYMBOL(sock_efree);
2162
2163 /* Buffer destructor for prefetch/receive path where reference count may
2164  * not be held, e.g. for listen sockets.
2165  */
2166 #ifdef CONFIG_INET
2167 void sock_pfree(struct sk_buff *skb)
2168 {
2169         if (sk_is_refcounted(skb->sk))
2170                 sock_gen_put(skb->sk);
2171 }
2172 EXPORT_SYMBOL(sock_pfree);
2173 #endif /* CONFIG_INET */
2174
2175 kuid_t sock_i_uid(struct sock *sk)
2176 {
2177         kuid_t uid;
2178
2179         read_lock_bh(&sk->sk_callback_lock);
2180         uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID;
2181         read_unlock_bh(&sk->sk_callback_lock);
2182         return uid;
2183 }
2184 EXPORT_SYMBOL(sock_i_uid);
2185
2186 unsigned long sock_i_ino(struct sock *sk)
2187 {
2188         unsigned long ino;
2189
2190         read_lock_bh(&sk->sk_callback_lock);
2191         ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
2192         read_unlock_bh(&sk->sk_callback_lock);
2193         return ino;
2194 }
2195 EXPORT_SYMBOL(sock_i_ino);
2196
2197 /*
2198  * Allocate a skb from the socket's send buffer.
2199  */
2200 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
2201                              gfp_t priority)
2202 {
2203         if (force ||
2204             refcount_read(&sk->sk_wmem_alloc) < READ_ONCE(sk->sk_sndbuf)) {
2205                 struct sk_buff *skb = alloc_skb(size, priority);
2206
2207                 if (skb) {
2208                         skb_set_owner_w(skb, sk);
2209                         return skb;
2210                 }
2211         }
2212         return NULL;
2213 }
2214 EXPORT_SYMBOL(sock_wmalloc);
2215
2216 static void sock_ofree(struct sk_buff *skb)
2217 {
2218         struct sock *sk = skb->sk;
2219
2220         atomic_sub(skb->truesize, &sk->sk_omem_alloc);
2221 }
2222
2223 struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
2224                              gfp_t priority)
2225 {
2226         struct sk_buff *skb;
2227
2228         /* small safe race: SKB_TRUESIZE may differ from final skb->truesize */
2229         if (atomic_read(&sk->sk_omem_alloc) + SKB_TRUESIZE(size) >
2230             sysctl_optmem_max)
2231                 return NULL;
2232
2233         skb = alloc_skb(size, priority);
2234         if (!skb)
2235                 return NULL;
2236
2237         atomic_add(skb->truesize, &sk->sk_omem_alloc);
2238         skb->sk = sk;
2239         skb->destructor = sock_ofree;
2240         return skb;
2241 }
2242
2243 /*
2244  * Allocate a memory block from the socket's option memory buffer.
2245  */
2246 void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
2247 {
2248         if ((unsigned int)size <= sysctl_optmem_max &&
2249             atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
2250                 void *mem;
2251                 /* First do the add, to avoid the race if kmalloc
2252                  * might sleep.
2253                  */
2254                 atomic_add(size, &sk->sk_omem_alloc);
2255                 mem = kmalloc(size, priority);
2256                 if (mem)
2257                         return mem;
2258                 atomic_sub(size, &sk->sk_omem_alloc);
2259         }
2260         return NULL;
2261 }
2262 EXPORT_SYMBOL(sock_kmalloc);
2263
2264 /* Free an option memory block. Note, we actually want the inline
2265  * here as this allows gcc to detect the nullify and fold away the
2266  * condition entirely.
2267  */
2268 static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
2269                                   const bool nullify)
2270 {
2271         if (WARN_ON_ONCE(!mem))
2272                 return;
2273         if (nullify)
2274                 kfree_sensitive(mem);
2275         else
2276                 kfree(mem);
2277         atomic_sub(size, &sk->sk_omem_alloc);
2278 }
2279
2280 void sock_kfree_s(struct sock *sk, void *mem, int size)
2281 {
2282         __sock_kfree_s(sk, mem, size, false);
2283 }
2284 EXPORT_SYMBOL(sock_kfree_s);
2285
2286 void sock_kzfree_s(struct sock *sk, void *mem, int size)
2287 {
2288         __sock_kfree_s(sk, mem, size, true);
2289 }
2290 EXPORT_SYMBOL(sock_kzfree_s);
2291
2292 /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
2293    I think, these locks should be removed for datagram sockets.
2294  */
2295 static long sock_wait_for_wmem(struct sock *sk, long timeo)
2296 {
2297         DEFINE_WAIT(wait);
2298
2299         sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2300         for (;;) {
2301                 if (!timeo)
2302                         break;
2303                 if (signal_pending(current))
2304                         break;
2305                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2306                 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
2307                 if (refcount_read(&sk->sk_wmem_alloc) < READ_ONCE(sk->sk_sndbuf))
2308                         break;
2309                 if (sk->sk_shutdown & SEND_SHUTDOWN)
2310                         break;
2311                 if (sk->sk_err)
2312                         break;
2313                 timeo = schedule_timeout(timeo);
2314         }
2315         finish_wait(sk_sleep(sk), &wait);
2316         return timeo;
2317 }
2318
2319
2320 /*
2321  *      Generic send/receive buffer handlers
2322  */
2323
2324 struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
2325                                      unsigned long data_len, int noblock,
2326                                      int *errcode, int max_page_order)
2327 {
2328         struct sk_buff *skb;
2329         long timeo;
2330         int err;
2331
2332         timeo = sock_sndtimeo(sk, noblock);
2333         for (;;) {
2334                 err = sock_error(sk);
2335                 if (err != 0)
2336                         goto failure;
2337
2338                 err = -EPIPE;
2339                 if (sk->sk_shutdown & SEND_SHUTDOWN)
2340                         goto failure;
2341
2342                 if (sk_wmem_alloc_get(sk) < READ_ONCE(sk->sk_sndbuf))
2343                         break;
2344
2345                 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2346                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2347                 err = -EAGAIN;
2348                 if (!timeo)
2349                         goto failure;
2350                 if (signal_pending(current))
2351                         goto interrupted;
2352                 timeo = sock_wait_for_wmem(sk, timeo);
2353         }
2354         skb = alloc_skb_with_frags(header_len, data_len, max_page_order,
2355                                    errcode, sk->sk_allocation);
2356         if (skb)
2357                 skb_set_owner_w(skb, sk);
2358         return skb;
2359
2360 interrupted:
2361         err = sock_intr_errno(timeo);
2362 failure:
2363         *errcode = err;
2364         return NULL;
2365 }
2366 EXPORT_SYMBOL(sock_alloc_send_pskb);
2367
2368 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
2369                                     int noblock, int *errcode)
2370 {
2371         return sock_alloc_send_pskb(sk, size, 0, noblock, errcode, 0);
2372 }
2373 EXPORT_SYMBOL(sock_alloc_send_skb);
2374
2375 int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,
2376                      struct sockcm_cookie *sockc)
2377 {
2378         u32 tsflags;
2379
2380         switch (cmsg->cmsg_type) {
2381         case SO_MARK:
2382                 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2383                         return -EPERM;
2384                 if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
2385                         return -EINVAL;
2386                 sockc->mark = *(u32 *)CMSG_DATA(cmsg);
2387                 break;
2388         case SO_TIMESTAMPING_OLD:
2389                 if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
2390                         return -EINVAL;
2391
2392                 tsflags = *(u32 *)CMSG_DATA(cmsg);
2393                 if (tsflags & ~SOF_TIMESTAMPING_TX_RECORD_MASK)
2394                         return -EINVAL;
2395
2396                 sockc->tsflags &= ~SOF_TIMESTAMPING_TX_RECORD_MASK;
2397                 sockc->tsflags |= tsflags;
2398                 break;
2399         case SCM_TXTIME:
2400                 if (!sock_flag(sk, SOCK_TXTIME))
2401                         return -EINVAL;
2402                 if (cmsg->cmsg_len != CMSG_LEN(sizeof(u64)))
2403                         return -EINVAL;
2404                 sockc->transmit_time = get_unaligned((u64 *)CMSG_DATA(cmsg));
2405                 break;
2406         /* SCM_RIGHTS and SCM_CREDENTIALS are semantically in SOL_UNIX. */
2407         case SCM_RIGHTS:
2408         case SCM_CREDENTIALS:
2409                 break;
2410         default:
2411                 return -EINVAL;
2412         }
2413         return 0;
2414 }
2415 EXPORT_SYMBOL(__sock_cmsg_send);
2416
2417 int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
2418                    struct sockcm_cookie *sockc)
2419 {
2420         struct cmsghdr *cmsg;
2421         int ret;
2422
2423         for_each_cmsghdr(cmsg, msg) {
2424                 if (!CMSG_OK(msg, cmsg))
2425                         return -EINVAL;
2426                 if (cmsg->cmsg_level != SOL_SOCKET)
2427                         continue;
2428                 ret = __sock_cmsg_send(sk, msg, cmsg, sockc);
2429                 if (ret)
2430                         return ret;
2431         }
2432         return 0;
2433 }
2434 EXPORT_SYMBOL(sock_cmsg_send);
2435
2436 static void sk_enter_memory_pressure(struct sock *sk)
2437 {
2438         if (!sk->sk_prot->enter_memory_pressure)
2439                 return;
2440
2441         sk->sk_prot->enter_memory_pressure(sk);
2442 }
2443
2444 static void sk_leave_memory_pressure(struct sock *sk)
2445 {
2446         if (sk->sk_prot->leave_memory_pressure) {
2447                 sk->sk_prot->leave_memory_pressure(sk);
2448         } else {
2449                 unsigned long *memory_pressure = sk->sk_prot->memory_pressure;
2450
2451                 if (memory_pressure && READ_ONCE(*memory_pressure))
2452                         WRITE_ONCE(*memory_pressure, 0);
2453         }
2454 }
2455
2456 #define SKB_FRAG_PAGE_ORDER     get_order(32768)
2457 DEFINE_STATIC_KEY_FALSE(net_high_order_alloc_disable_key);
2458
2459 /**
2460  * skb_page_frag_refill - check that a page_frag contains enough room
2461  * @sz: minimum size of the fragment we want to get
2462  * @pfrag: pointer to page_frag
2463  * @gfp: priority for memory allocation
2464  *
2465  * Note: While this allocator tries to use high order pages, there is
2466  * no guarantee that allocations succeed. Therefore, @sz MUST be
2467  * less or equal than PAGE_SIZE.
2468  */
2469 bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp)
2470 {
2471         if (pfrag->page) {
2472                 if (page_ref_count(pfrag->page) == 1) {
2473                         pfrag->offset = 0;
2474                         return true;
2475                 }
2476                 if (pfrag->offset + sz <= pfrag->size)
2477                         return true;
2478                 put_page(pfrag->page);
2479         }
2480
2481         pfrag->offset = 0;
2482         if (SKB_FRAG_PAGE_ORDER &&
2483             !static_branch_unlikely(&net_high_order_alloc_disable_key)) {
2484                 /* Avoid direct reclaim but allow kswapd to wake */
2485                 pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) |
2486                                           __GFP_COMP | __GFP_NOWARN |
2487                                           __GFP_NORETRY,
2488                                           SKB_FRAG_PAGE_ORDER);
2489                 if (likely(pfrag->page)) {
2490                         pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
2491                         return true;
2492                 }
2493         }
2494         pfrag->page = alloc_page(gfp);
2495         if (likely(pfrag->page)) {
2496                 pfrag->size = PAGE_SIZE;
2497                 return true;
2498         }
2499         return false;
2500 }
2501 EXPORT_SYMBOL(skb_page_frag_refill);
2502
2503 bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
2504 {
2505         if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation)))
2506                 return true;
2507
2508         sk_enter_memory_pressure(sk);
2509         sk_stream_moderate_sndbuf(sk);
2510         return false;
2511 }
2512 EXPORT_SYMBOL(sk_page_frag_refill);
2513
2514 void __lock_sock(struct sock *sk)
2515         __releases(&sk->sk_lock.slock)
2516         __acquires(&sk->sk_lock.slock)
2517 {
2518         DEFINE_WAIT(wait);
2519
2520         for (;;) {
2521                 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
2522                                         TASK_UNINTERRUPTIBLE);
2523                 spin_unlock_bh(&sk->sk_lock.slock);
2524                 schedule();
2525                 spin_lock_bh(&sk->sk_lock.slock);
2526                 if (!sock_owned_by_user(sk))
2527                         break;
2528         }
2529         finish_wait(&sk->sk_lock.wq, &wait);
2530 }
2531
2532 void __release_sock(struct sock *sk)
2533         __releases(&sk->sk_lock.slock)
2534         __acquires(&sk->sk_lock.slock)
2535 {
2536         struct sk_buff *skb, *next;
2537
2538         while ((skb = sk->sk_backlog.head) != NULL) {
2539                 sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
2540
2541                 spin_unlock_bh(&sk->sk_lock.slock);
2542
2543                 do {
2544                         next = skb->next;
2545                         prefetch(next);
2546                         WARN_ON_ONCE(skb_dst_is_noref(skb));
2547                         skb_mark_not_on_list(skb);
2548                         sk_backlog_rcv(sk, skb);
2549
2550                         cond_resched();
2551
2552                         skb = next;
2553                 } while (skb != NULL);
2554
2555                 spin_lock_bh(&sk->sk_lock.slock);
2556         }
2557
2558         /*
2559          * Doing the zeroing here guarantee we can not loop forever
2560          * while a wild producer attempts to flood us.
2561          */
2562         sk->sk_backlog.len = 0;
2563 }
2564
2565 void __sk_flush_backlog(struct sock *sk)
2566 {
2567         spin_lock_bh(&sk->sk_lock.slock);
2568         __release_sock(sk);
2569         spin_unlock_bh(&sk->sk_lock.slock);
2570 }
2571
2572 /**
2573  * sk_wait_data - wait for data to arrive at sk_receive_queue
2574  * @sk:    sock to wait on
2575  * @timeo: for how long
2576  * @skb:   last skb seen on sk_receive_queue
2577  *
2578  * Now socket state including sk->sk_err is changed only under lock,
2579  * hence we may omit checks after joining wait queue.
2580  * We check receive queue before schedule() only as optimization;
2581  * it is very likely that release_sock() added new data.
2582  */
2583 int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb)
2584 {
2585         DEFINE_WAIT_FUNC(wait, woken_wake_function);
2586         int rc;
2587
2588         add_wait_queue(sk_sleep(sk), &wait);
2589         sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
2590         rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb, &wait);
2591         sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
2592         remove_wait_queue(sk_sleep(sk), &wait);
2593         return rc;
2594 }
2595 EXPORT_SYMBOL(sk_wait_data);
2596
2597 /**
2598  *      __sk_mem_raise_allocated - increase memory_allocated
2599  *      @sk: socket
2600  *      @size: memory size to allocate
2601  *      @amt: pages to allocate
2602  *      @kind: allocation type
2603  *
2604  *      Similar to __sk_mem_schedule(), but does not update sk_forward_alloc
2605  */
2606 int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)
2607 {
2608         struct proto *prot = sk->sk_prot;
2609         long allocated = sk_memory_allocated_add(sk, amt);
2610         bool charged = true;
2611
2612         if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
2613             !(charged = mem_cgroup_charge_skmem(sk->sk_memcg, amt)))
2614                 goto suppress_allocation;
2615
2616         /* Under limit. */
2617         if (allocated <= sk_prot_mem_limits(sk, 0)) {
2618                 sk_leave_memory_pressure(sk);
2619                 return 1;
2620         }
2621
2622         /* Under pressure. */
2623         if (allocated > sk_prot_mem_limits(sk, 1))
2624                 sk_enter_memory_pressure(sk);
2625
2626         /* Over hard limit. */
2627         if (allocated > sk_prot_mem_limits(sk, 2))
2628                 goto suppress_allocation;
2629
2630         /* guarantee minimum buffer size under pressure */
2631         if (kind == SK_MEM_RECV) {
2632                 if (atomic_read(&sk->sk_rmem_alloc) < sk_get_rmem0(sk, prot))
2633                         return 1;
2634
2635         } else { /* SK_MEM_SEND */
2636                 int wmem0 = sk_get_wmem0(sk, prot);
2637
2638                 if (sk->sk_type == SOCK_STREAM) {
2639                         if (sk->sk_wmem_queued < wmem0)
2640                                 return 1;
2641                 } else if (refcount_read(&sk->sk_wmem_alloc) < wmem0) {
2642                                 return 1;
2643                 }
2644         }
2645
2646         if (sk_has_memory_pressure(sk)) {
2647                 u64 alloc;
2648
2649                 if (!sk_under_memory_pressure(sk))
2650                         return 1;
2651                 alloc = sk_sockets_allocated_read_positive(sk);
2652                 if (sk_prot_mem_limits(sk, 2) > alloc *
2653                     sk_mem_pages(sk->sk_wmem_queued +
2654                                  atomic_read(&sk->sk_rmem_alloc) +
2655                                  sk->sk_forward_alloc))
2656                         return 1;
2657         }
2658
2659 suppress_allocation:
2660
2661         if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
2662                 sk_stream_moderate_sndbuf(sk);
2663
2664                 /* Fail only if socket is _under_ its sndbuf.
2665                  * In this case we cannot block, so that we have to fail.
2666                  */
2667                 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
2668                         return 1;
2669         }
2670
2671         if (kind == SK_MEM_SEND || (kind == SK_MEM_RECV && charged))
2672                 trace_sock_exceed_buf_limit(sk, prot, allocated, kind);
2673
2674         sk_memory_allocated_sub(sk, amt);
2675
2676         if (mem_cgroup_sockets_enabled && sk->sk_memcg)
2677                 mem_cgroup_uncharge_skmem(sk->sk_memcg, amt);
2678
2679         return 0;
2680 }
2681 EXPORT_SYMBOL(__sk_mem_raise_allocated);
2682
2683 /**
2684  *      __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
2685  *      @sk: socket
2686  *      @size: memory size to allocate
2687  *      @kind: allocation type
2688  *
2689  *      If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
2690  *      rmem allocation. This function assumes that protocols which have
2691  *      memory_pressure use sk_wmem_queued as write buffer accounting.
2692  */
2693 int __sk_mem_schedule(struct sock *sk, int size, int kind)
2694 {
2695         int ret, amt = sk_mem_pages(size);
2696
2697         sk->sk_forward_alloc += amt << SK_MEM_QUANTUM_SHIFT;
2698         ret = __sk_mem_raise_allocated(sk, size, amt, kind);
2699         if (!ret)
2700                 sk->sk_forward_alloc -= amt << SK_MEM_QUANTUM_SHIFT;
2701         return ret;
2702 }
2703 EXPORT_SYMBOL(__sk_mem_schedule);
2704
2705 /**
2706  *      __sk_mem_reduce_allocated - reclaim memory_allocated
2707  *      @sk: socket
2708  *      @amount: number of quanta
2709  *
2710  *      Similar to __sk_mem_reclaim(), but does not update sk_forward_alloc
2711  */
2712 void __sk_mem_reduce_allocated(struct sock *sk, int amount)
2713 {
2714         sk_memory_allocated_sub(sk, amount);
2715
2716         if (mem_cgroup_sockets_enabled && sk->sk_memcg)
2717                 mem_cgroup_uncharge_skmem(sk->sk_memcg, amount);
2718
2719         if (sk_under_memory_pressure(sk) &&
2720             (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
2721                 sk_leave_memory_pressure(sk);
2722 }
2723 EXPORT_SYMBOL(__sk_mem_reduce_allocated);
2724
2725 /**
2726  *      __sk_mem_reclaim - reclaim sk_forward_alloc and memory_allocated
2727  *      @sk: socket
2728  *      @amount: number of bytes (rounded down to a SK_MEM_QUANTUM multiple)
2729  */
2730 void __sk_mem_reclaim(struct sock *sk, int amount)
2731 {
2732         amount >>= SK_MEM_QUANTUM_SHIFT;
2733         sk->sk_forward_alloc -= amount << SK_MEM_QUANTUM_SHIFT;
2734         __sk_mem_reduce_allocated(sk, amount);
2735 }
2736 EXPORT_SYMBOL(__sk_mem_reclaim);
2737
2738 int sk_set_peek_off(struct sock *sk, int val)
2739 {
2740         sk->sk_peek_off = val;
2741         return 0;
2742 }
2743 EXPORT_SYMBOL_GPL(sk_set_peek_off);
2744
2745 /*
2746  * Set of default routines for initialising struct proto_ops when
2747  * the protocol does not support a particular function. In certain
2748  * cases where it makes no sense for a protocol to have a "do nothing"
2749  * function, some default processing is provided.
2750  */
2751
2752 int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
2753 {
2754         return -EOPNOTSUPP;
2755 }
2756 EXPORT_SYMBOL(sock_no_bind);
2757
2758 int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
2759                     int len, int flags)
2760 {
2761         return -EOPNOTSUPP;
2762 }
2763 EXPORT_SYMBOL(sock_no_connect);
2764
2765 int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
2766 {
2767         return -EOPNOTSUPP;
2768 }
2769 EXPORT_SYMBOL(sock_no_socketpair);
2770
2771 int sock_no_accept(struct socket *sock, struct socket *newsock, int flags,
2772                    bool kern)
2773 {
2774         return -EOPNOTSUPP;
2775 }
2776 EXPORT_SYMBOL(sock_no_accept);
2777
2778 int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
2779                     int peer)
2780 {
2781         return -EOPNOTSUPP;
2782 }
2783 EXPORT_SYMBOL(sock_no_getname);
2784
2785 int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2786 {
2787         return -EOPNOTSUPP;
2788 }
2789 EXPORT_SYMBOL(sock_no_ioctl);
2790
2791 int sock_no_listen(struct socket *sock, int backlog)
2792 {
2793         return -EOPNOTSUPP;
2794 }
2795 EXPORT_SYMBOL(sock_no_listen);
2796
2797 int sock_no_shutdown(struct socket *sock, int how)
2798 {
2799         return -EOPNOTSUPP;
2800 }
2801 EXPORT_SYMBOL(sock_no_shutdown);
2802
2803 int sock_no_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
2804 {
2805         return -EOPNOTSUPP;
2806 }
2807 EXPORT_SYMBOL(sock_no_sendmsg);
2808
2809 int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *m, size_t len)
2810 {
2811         return -EOPNOTSUPP;
2812 }
2813 EXPORT_SYMBOL(sock_no_sendmsg_locked);
2814
2815 int sock_no_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
2816                     int flags)
2817 {
2818         return -EOPNOTSUPP;
2819 }
2820 EXPORT_SYMBOL(sock_no_recvmsg);
2821
2822 int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
2823 {
2824         /* Mirror missing mmap method error code */
2825         return -ENODEV;
2826 }
2827 EXPORT_SYMBOL(sock_no_mmap);
2828
2829 /*
2830  * When a file is received (via SCM_RIGHTS, etc), we must bump the
2831  * various sock-based usage counts.
2832  */
2833 void __receive_sock(struct file *file)
2834 {
2835         struct socket *sock;
2836
2837         sock = sock_from_file(file);
2838         if (sock) {
2839                 sock_update_netprioidx(&sock->sk->sk_cgrp_data);
2840                 sock_update_classid(&sock->sk->sk_cgrp_data);
2841         }
2842 }
2843
2844 ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
2845 {
2846         ssize_t res;
2847         struct msghdr msg = {.msg_flags = flags};
2848         struct kvec iov;
2849         char *kaddr = kmap(page);
2850         iov.iov_base = kaddr + offset;
2851         iov.iov_len = size;
2852         res = kernel_sendmsg(sock, &msg, &iov, 1, size);
2853         kunmap(page);
2854         return res;
2855 }
2856 EXPORT_SYMBOL(sock_no_sendpage);
2857
2858 ssize_t sock_no_sendpage_locked(struct sock *sk, struct page *page,
2859                                 int offset, size_t size, int flags)
2860 {
2861         ssize_t res;
2862         struct msghdr msg = {.msg_flags = flags};
2863         struct kvec iov;
2864         char *kaddr = kmap(page);
2865
2866         iov.iov_base = kaddr + offset;
2867         iov.iov_len = size;
2868         res = kernel_sendmsg_locked(sk, &msg, &iov, 1, size);
2869         kunmap(page);
2870         return res;
2871 }
2872 EXPORT_SYMBOL(sock_no_sendpage_locked);
2873
2874 /*
2875  *      Default Socket Callbacks
2876  */
2877
2878 static void sock_def_wakeup(struct sock *sk)
2879 {
2880         struct socket_wq *wq;
2881
2882         rcu_read_lock();
2883         wq = rcu_dereference(sk->sk_wq);
2884         if (skwq_has_sleeper(wq))
2885                 wake_up_interruptible_all(&wq->wait);
2886         rcu_read_unlock();
2887 }
2888
2889 static void sock_def_error_report(struct sock *sk)
2890 {
2891         struct socket_wq *wq;
2892
2893         rcu_read_lock();
2894         wq = rcu_dereference(sk->sk_wq);
2895         if (skwq_has_sleeper(wq))
2896                 wake_up_interruptible_poll(&wq->wait, EPOLLERR);
2897         sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
2898         rcu_read_unlock();
2899 }
2900
2901 void sock_def_readable(struct sock *sk)
2902 {
2903         struct socket_wq *wq;
2904
2905         rcu_read_lock();
2906         wq = rcu_dereference(sk->sk_wq);
2907         if (skwq_has_sleeper(wq))
2908                 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI |
2909                                                 EPOLLRDNORM | EPOLLRDBAND);
2910         sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
2911         rcu_read_unlock();
2912 }
2913
2914 static void sock_def_write_space(struct sock *sk)
2915 {
2916         struct socket_wq *wq;
2917
2918         rcu_read_lock();
2919
2920         /* Do not wake up a writer until he can make "significant"
2921          * progress.  --DaveM
2922          */
2923         if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= READ_ONCE(sk->sk_sndbuf)) {
2924                 wq = rcu_dereference(sk->sk_wq);
2925                 if (skwq_has_sleeper(wq))
2926                         wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
2927                                                 EPOLLWRNORM | EPOLLWRBAND);
2928
2929                 /* Should agree with poll, otherwise some programs break */
2930                 if (sock_writeable(sk))
2931                         sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
2932         }
2933
2934         rcu_read_unlock();
2935 }
2936
2937 static void sock_def_destruct(struct sock *sk)
2938 {
2939 }
2940
2941 void sk_send_sigurg(struct sock *sk)
2942 {
2943         if (sk->sk_socket && sk->sk_socket->file)
2944                 if (send_sigurg(&sk->sk_socket->file->f_owner))
2945                         sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
2946 }
2947 EXPORT_SYMBOL(sk_send_sigurg);
2948
2949 void sk_reset_timer(struct sock *sk, struct timer_list* timer,
2950                     unsigned long expires)
2951 {
2952         if (!mod_timer(timer, expires))
2953                 sock_hold(sk);
2954 }
2955 EXPORT_SYMBOL(sk_reset_timer);
2956
2957 void sk_stop_timer(struct sock *sk, struct timer_list* timer)
2958 {
2959         if (del_timer(timer))
2960                 __sock_put(sk);
2961 }
2962 EXPORT_SYMBOL(sk_stop_timer);
2963
2964 void sk_stop_timer_sync(struct sock *sk, struct timer_list *timer)
2965 {
2966         if (del_timer_sync(timer))
2967                 __sock_put(sk);
2968 }
2969 EXPORT_SYMBOL(sk_stop_timer_sync);
2970
2971 void sock_init_data(struct socket *sock, struct sock *sk)
2972 {
2973         sk_init_common(sk);
2974         sk->sk_send_head        =       NULL;
2975
2976         timer_setup(&sk->sk_timer, NULL, 0);
2977
2978         sk->sk_allocation       =       GFP_KERNEL;
2979         sk->sk_rcvbuf           =       sysctl_rmem_default;
2980         sk->sk_sndbuf           =       sysctl_wmem_default;
2981         sk->sk_state            =       TCP_CLOSE;
2982         sk_set_socket(sk, sock);
2983
2984         sock_set_flag(sk, SOCK_ZAPPED);
2985
2986         if (sock) {
2987                 sk->sk_type     =       sock->type;
2988                 RCU_INIT_POINTER(sk->sk_wq, &sock->wq);
2989                 sock->sk        =       sk;
2990                 sk->sk_uid      =       SOCK_INODE(sock)->i_uid;
2991         } else {
2992                 RCU_INIT_POINTER(sk->sk_wq, NULL);
2993                 sk->sk_uid      =       make_kuid(sock_net(sk)->user_ns, 0);
2994         }
2995
2996         rwlock_init(&sk->sk_callback_lock);
2997         if (sk->sk_kern_sock)
2998                 lockdep_set_class_and_name(
2999                         &sk->sk_callback_lock,
3000                         af_kern_callback_keys + sk->sk_family,
3001                         af_family_kern_clock_key_strings[sk->sk_family]);
3002         else
3003                 lockdep_set_class_and_name(
3004                         &sk->sk_callback_lock,
3005                         af_callback_keys + sk->sk_family,
3006                         af_family_clock_key_strings[sk->sk_family]);
3007
3008         sk->sk_state_change     =       sock_def_wakeup;
3009         sk->sk_data_ready       =       sock_def_readable;
3010         sk->sk_write_space      =       sock_def_write_space;
3011         sk->sk_error_report     =       sock_def_error_report;
3012         sk->sk_destruct         =       sock_def_destruct;
3013
3014         sk->sk_frag.page        =       NULL;
3015         sk->sk_frag.offset      =       0;
3016         sk->sk_peek_off         =       -1;
3017
3018         sk->sk_peer_pid         =       NULL;
3019         sk->sk_peer_cred        =       NULL;
3020         sk->sk_write_pending    =       0;
3021         sk->sk_rcvlowat         =       1;
3022         sk->sk_rcvtimeo         =       MAX_SCHEDULE_TIMEOUT;
3023         sk->sk_sndtimeo         =       MAX_SCHEDULE_TIMEOUT;
3024
3025         sk->sk_stamp = SK_DEFAULT_STAMP;
3026 #if BITS_PER_LONG==32
3027         seqlock_init(&sk->sk_stamp_seq);
3028 #endif
3029         atomic_set(&sk->sk_zckey, 0);
3030
3031 #ifdef CONFIG_NET_RX_BUSY_POLL
3032         sk->sk_napi_id          =       0;
3033         sk->sk_ll_usec          =       sysctl_net_busy_read;
3034 #endif
3035
3036         sk->sk_max_pacing_rate = ~0UL;
3037         sk->sk_pacing_rate = ~0UL;
3038         WRITE_ONCE(sk->sk_pacing_shift, 10);
3039         sk->sk_incoming_cpu = -1;
3040
3041         sk_rx_queue_clear(sk);
3042         /*
3043          * Before updating sk_refcnt, we must commit prior changes to memory
3044          * (Documentation/RCU/rculist_nulls.rst for details)
3045          */
3046         smp_wmb();
3047         refcount_set(&sk->sk_refcnt, 1);
3048         atomic_set(&sk->sk_drops, 0);
3049 }
3050 EXPORT_SYMBOL(sock_init_data);
3051
3052 void lock_sock_nested(struct sock *sk, int subclass)
3053 {
3054         might_sleep();
3055         spin_lock_bh(&sk->sk_lock.slock);
3056         if (sk->sk_lock.owned)
3057                 __lock_sock(sk);
3058         sk->sk_lock.owned = 1;
3059         spin_unlock(&sk->sk_lock.slock);
3060         /*
3061          * The sk_lock has mutex_lock() semantics here:
3062          */
3063         mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
3064         local_bh_enable();
3065 }
3066 EXPORT_SYMBOL(lock_sock_nested);
3067
3068 void release_sock(struct sock *sk)
3069 {
3070         spin_lock_bh(&sk->sk_lock.slock);
3071         if (sk->sk_backlog.tail)
3072                 __release_sock(sk);
3073
3074         /* Warning : release_cb() might need to release sk ownership,
3075          * ie call sock_release_ownership(sk) before us.
3076          */
3077         if (sk->sk_prot->release_cb)
3078                 sk->sk_prot->release_cb(sk);
3079
3080         sock_release_ownership(sk);
3081         if (waitqueue_active(&sk->sk_lock.wq))
3082                 wake_up(&sk->sk_lock.wq);
3083         spin_unlock_bh(&sk->sk_lock.slock);
3084 }
3085 EXPORT_SYMBOL(release_sock);
3086
3087 /**
3088  * lock_sock_fast - fast version of lock_sock
3089  * @sk: socket
3090  *
3091  * This version should be used for very small section, where process wont block
3092  * return false if fast path is taken:
3093  *
3094  *   sk_lock.slock locked, owned = 0, BH disabled
3095  *
3096  * return true if slow path is taken:
3097  *
3098  *   sk_lock.slock unlocked, owned = 1, BH enabled
3099  */
3100 bool lock_sock_fast(struct sock *sk) __acquires(&sk->sk_lock.slock)
3101 {
3102         might_sleep();
3103         spin_lock_bh(&sk->sk_lock.slock);
3104
3105         if (!sk->sk_lock.owned)
3106                 /*
3107                  * Note : We must disable BH
3108                  */
3109                 return false;
3110
3111         __lock_sock(sk);
3112         sk->sk_lock.owned = 1;
3113         spin_unlock(&sk->sk_lock.slock);
3114         /*
3115          * The sk_lock has mutex_lock() semantics here:
3116          */
3117         mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
3118         __acquire(&sk->sk_lock.slock);
3119         local_bh_enable();
3120         return true;
3121 }
3122 EXPORT_SYMBOL(lock_sock_fast);
3123
3124 int sock_gettstamp(struct socket *sock, void __user *userstamp,
3125                    bool timeval, bool time32)
3126 {
3127         struct sock *sk = sock->sk;
3128         struct timespec64 ts;
3129
3130         sock_enable_timestamp(sk, SOCK_TIMESTAMP);
3131         ts = ktime_to_timespec64(sock_read_timestamp(sk));
3132         if (ts.tv_sec == -1)
3133                 return -ENOENT;
3134         if (ts.tv_sec == 0) {
3135                 ktime_t kt = ktime_get_real();
3136                 sock_write_timestamp(sk, kt);
3137                 ts = ktime_to_timespec64(kt);
3138         }
3139
3140         if (timeval)
3141                 ts.tv_nsec /= 1000;
3142
3143 #ifdef CONFIG_COMPAT_32BIT_TIME
3144         if (time32)
3145                 return put_old_timespec32(&ts, userstamp);
3146 #endif
3147 #ifdef CONFIG_SPARC64
3148         /* beware of padding in sparc64 timeval */
3149         if (timeval && !in_compat_syscall()) {
3150                 struct __kernel_old_timeval __user tv = {
3151                         .tv_sec = ts.tv_sec,
3152                         .tv_usec = ts.tv_nsec,
3153                 };
3154                 if (copy_to_user(userstamp, &tv, sizeof(tv)))
3155                         return -EFAULT;
3156                 return 0;
3157         }
3158 #endif
3159         return put_timespec64(&ts, userstamp);
3160 }
3161 EXPORT_SYMBOL(sock_gettstamp);
3162
3163 void sock_enable_timestamp(struct sock *sk, enum sock_flags flag)
3164 {
3165         if (!sock_flag(sk, flag)) {
3166                 unsigned long previous_flags = sk->sk_flags;
3167
3168                 sock_set_flag(sk, flag);
3169                 /*
3170                  * we just set one of the two flags which require net
3171                  * time stamping, but time stamping might have been on
3172                  * already because of the other one
3173                  */
3174                 if (sock_needs_netstamp(sk) &&
3175                     !(previous_flags & SK_FLAGS_TIMESTAMP))
3176                         net_enable_timestamp();
3177         }
3178 }
3179
3180 int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len,
3181                        int level, int type)
3182 {
3183         struct sock_exterr_skb *serr;
3184         struct sk_buff *skb;
3185         int copied, err;
3186
3187         err = -EAGAIN;
3188         skb = sock_dequeue_err_skb(sk);
3189         if (skb == NULL)
3190                 goto out;
3191
3192         copied = skb->len;
3193         if (copied > len) {
3194                 msg->msg_flags |= MSG_TRUNC;
3195                 copied = len;
3196         }
3197         err = skb_copy_datagram_msg(skb, 0, msg, copied);
3198         if (err)
3199                 goto out_free_skb;
3200
3201         sock_recv_timestamp(msg, sk, skb);
3202
3203         serr = SKB_EXT_ERR(skb);
3204         put_cmsg(msg, level, type, sizeof(serr->ee), &serr->ee);
3205
3206         msg->msg_flags |= MSG_ERRQUEUE;
3207         err = copied;
3208
3209 out_free_skb:
3210         kfree_skb(skb);
3211 out:
3212         return err;
3213 }
3214 EXPORT_SYMBOL(sock_recv_errqueue);
3215
3216 /*
3217  *      Get a socket option on an socket.
3218  *
3219  *      FIX: POSIX 1003.1g is very ambiguous here. It states that
3220  *      asynchronous errors should be reported by getsockopt. We assume
3221  *      this means if you specify SO_ERROR (otherwise whats the point of it).
3222  */
3223 int sock_common_getsockopt(struct socket *sock, int level, int optname,
3224                            char __user *optval, int __user *optlen)
3225 {
3226         struct sock *sk = sock->sk;
3227
3228         return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
3229 }
3230 EXPORT_SYMBOL(sock_common_getsockopt);
3231
3232 int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
3233                         int flags)
3234 {
3235         struct sock *sk = sock->sk;
3236         int addr_len = 0;
3237         int err;
3238
3239         err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
3240                                    flags & ~MSG_DONTWAIT, &addr_len);
3241         if (err >= 0)
3242                 msg->msg_namelen = addr_len;
3243         return err;
3244 }
3245 EXPORT_SYMBOL(sock_common_recvmsg);
3246
3247 /*
3248  *      Set socket options on an inet socket.
3249  */
3250 int sock_common_setsockopt(struct socket *sock, int level, int optname,
3251                            sockptr_t optval, unsigned int optlen)
3252 {
3253         struct sock *sk = sock->sk;
3254
3255         return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
3256 }
3257 EXPORT_SYMBOL(sock_common_setsockopt);
3258
3259 void sk_common_release(struct sock *sk)
3260 {
3261         if (sk->sk_prot->destroy)
3262                 sk->sk_prot->destroy(sk);
3263
3264         /*
3265          * Observation: when sk_common_release is called, processes have
3266          * no access to socket. But net still has.
3267          * Step one, detach it from networking:
3268          *
3269          * A. Remove from hash tables.
3270          */
3271
3272         sk->sk_prot->unhash(sk);
3273
3274         /*
3275          * In this point socket cannot receive new packets, but it is possible
3276          * that some packets are in flight because some CPU runs receiver and
3277          * did hash table lookup before we unhashed socket. They will achieve
3278          * receive queue and will be purged by socket destructor.
3279          *
3280          * Also we still have packets pending on receive queue and probably,
3281          * our own packets waiting in device queues. sock_destroy will drain
3282          * receive queue, but transmitted packets will delay socket destruction
3283          * until the last reference will be released.
3284          */
3285
3286         sock_orphan(sk);
3287
3288         xfrm_sk_free_policy(sk);
3289
3290         sk_refcnt_debug_release(sk);
3291
3292         sock_put(sk);
3293 }
3294 EXPORT_SYMBOL(sk_common_release);
3295
3296 void sk_get_meminfo(const struct sock *sk, u32 *mem)
3297 {
3298         memset(mem, 0, sizeof(*mem) * SK_MEMINFO_VARS);
3299
3300         mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
3301         mem[SK_MEMINFO_RCVBUF] = READ_ONCE(sk->sk_rcvbuf);
3302         mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
3303         mem[SK_MEMINFO_SNDBUF] = READ_ONCE(sk->sk_sndbuf);
3304         mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
3305         mem[SK_MEMINFO_WMEM_QUEUED] = READ_ONCE(sk->sk_wmem_queued);
3306         mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
3307         mem[SK_MEMINFO_BACKLOG] = READ_ONCE(sk->sk_backlog.len);
3308         mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
3309 }
3310
3311 #ifdef CONFIG_PROC_FS
3312 #define PROTO_INUSE_NR  64      /* should be enough for the first time */
3313 struct prot_inuse {
3314         int val[PROTO_INUSE_NR];
3315 };
3316
3317 static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
3318
3319 void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
3320 {
3321         __this_cpu_add(net->core.prot_inuse->val[prot->inuse_idx], val);
3322 }
3323 EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
3324
3325 int sock_prot_inuse_get(struct net *net, struct proto *prot)
3326 {
3327         int cpu, idx = prot->inuse_idx;
3328         int res = 0;
3329
3330         for_each_possible_cpu(cpu)
3331                 res += per_cpu_ptr(net->core.prot_inuse, cpu)->val[idx];
3332
3333         return res >= 0 ? res : 0;
3334 }
3335 EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
3336
3337 static void sock_inuse_add(struct net *net, int val)
3338 {
3339         this_cpu_add(*net->core.sock_inuse, val);
3340 }
3341
3342 int sock_inuse_get(struct net *net)
3343 {
3344         int cpu, res = 0;
3345
3346         for_each_possible_cpu(cpu)
3347                 res += *per_cpu_ptr(net->core.sock_inuse, cpu);
3348
3349         return res;
3350 }
3351
3352 EXPORT_SYMBOL_GPL(sock_inuse_get);
3353
3354 static int __net_init sock_inuse_init_net(struct net *net)
3355 {
3356         net->core.prot_inuse = alloc_percpu(struct prot_inuse);
3357         if (net->core.prot_inuse == NULL)
3358                 return -ENOMEM;
3359
3360         net->core.sock_inuse = alloc_percpu(int);
3361         if (net->core.sock_inuse == NULL)
3362                 goto out;
3363
3364         return 0;
3365
3366 out:
3367         free_percpu(net->core.prot_inuse);
3368         return -ENOMEM;
3369 }
3370
3371 static void __net_exit sock_inuse_exit_net(struct net *net)
3372 {
3373         free_percpu(net->core.prot_inuse);
3374         free_percpu(net->core.sock_inuse);
3375 }
3376
3377 static struct pernet_operations net_inuse_ops = {
3378         .init = sock_inuse_init_net,
3379         .exit = sock_inuse_exit_net,
3380 };
3381
3382 static __init int net_inuse_init(void)
3383 {
3384         if (register_pernet_subsys(&net_inuse_ops))
3385                 panic("Cannot initialize net inuse counters");
3386
3387         return 0;
3388 }
3389
3390 core_initcall(net_inuse_init);
3391
3392 static int assign_proto_idx(struct proto *prot)
3393 {
3394         prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
3395
3396         if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
3397                 pr_err("PROTO_INUSE_NR exhausted\n");
3398                 return -ENOSPC;
3399         }
3400
3401         set_bit(prot->inuse_idx, proto_inuse_idx);
3402         return 0;
3403 }
3404
3405 static void release_proto_idx(struct proto *prot)
3406 {
3407         if (prot->inuse_idx != PROTO_INUSE_NR - 1)
3408                 clear_bit(prot->inuse_idx, proto_inuse_idx);
3409 }
3410 #else
3411 static inline int assign_proto_idx(struct proto *prot)
3412 {
3413         return 0;
3414 }
3415
3416 static inline void release_proto_idx(struct proto *prot)
3417 {
3418 }
3419
3420 static void sock_inuse_add(struct net *net, int val)
3421 {
3422 }
3423 #endif
3424
3425 static void tw_prot_cleanup(struct timewait_sock_ops *twsk_prot)
3426 {
3427         if (!twsk_prot)
3428                 return;
3429         kfree(twsk_prot->twsk_slab_name);
3430         twsk_prot->twsk_slab_name = NULL;
3431         kmem_cache_destroy(twsk_prot->twsk_slab);
3432         twsk_prot->twsk_slab = NULL;
3433 }
3434
3435 static void req_prot_cleanup(struct request_sock_ops *rsk_prot)
3436 {
3437         if (!rsk_prot)
3438                 return;
3439         kfree(rsk_prot->slab_name);
3440         rsk_prot->slab_name = NULL;
3441         kmem_cache_destroy(rsk_prot->slab);
3442         rsk_prot->slab = NULL;
3443 }
3444
3445 static int req_prot_init(const struct proto *prot)
3446 {
3447         struct request_sock_ops *rsk_prot = prot->rsk_prot;
3448
3449         if (!rsk_prot)
3450                 return 0;
3451
3452         rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s",
3453                                         prot->name);
3454         if (!rsk_prot->slab_name)
3455                 return -ENOMEM;
3456
3457         rsk_prot->slab = kmem_cache_create(rsk_prot->slab_name,
3458                                            rsk_prot->obj_size, 0,
3459                                            SLAB_ACCOUNT | prot->slab_flags,
3460                                            NULL);
3461
3462         if (!rsk_prot->slab) {
3463                 pr_crit("%s: Can't create request sock SLAB cache!\n",
3464                         prot->name);
3465                 return -ENOMEM;
3466         }
3467         return 0;
3468 }
3469
3470 int proto_register(struct proto *prot, int alloc_slab)
3471 {
3472         int ret = -ENOBUFS;
3473
3474         if (alloc_slab) {
3475                 prot->slab = kmem_cache_create_usercopy(prot->name,
3476                                         prot->obj_size, 0,
3477                                         SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT |
3478                                         prot->slab_flags,
3479                                         prot->useroffset, prot->usersize,
3480                                         NULL);
3481
3482                 if (prot->slab == NULL) {
3483                         pr_crit("%s: Can't create sock SLAB cache!\n",
3484                                 prot->name);
3485                         goto out;
3486                 }
3487
3488                 if (req_prot_init(prot))
3489                         goto out_free_request_sock_slab;
3490
3491                 if (prot->twsk_prot != NULL) {
3492                         prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
3493
3494                         if (prot->twsk_prot->twsk_slab_name == NULL)
3495                                 goto out_free_request_sock_slab;
3496
3497                         prot->twsk_prot->twsk_slab =
3498                                 kmem_cache_create(prot->twsk_prot->twsk_slab_name,
3499                                                   prot->twsk_prot->twsk_obj_size,
3500                                                   0,
3501                                                   SLAB_ACCOUNT |
3502                                                   prot->slab_flags,
3503                                                   NULL);
3504                         if (prot->twsk_prot->twsk_slab == NULL)
3505                                 goto out_free_timewait_sock_slab;
3506                 }
3507         }
3508
3509         mutex_lock(&proto_list_mutex);
3510         ret = assign_proto_idx(prot);
3511         if (ret) {
3512                 mutex_unlock(&proto_list_mutex);
3513                 goto out_free_timewait_sock_slab;
3514         }
3515         list_add(&prot->node, &proto_list);
3516         mutex_unlock(&proto_list_mutex);
3517         return ret;
3518
3519 out_free_timewait_sock_slab:
3520         if (alloc_slab && prot->twsk_prot)
3521                 tw_prot_cleanup(prot->twsk_prot);
3522 out_free_request_sock_slab:
3523         if (alloc_slab) {
3524                 req_prot_cleanup(prot->rsk_prot);
3525
3526                 kmem_cache_destroy(prot->slab);
3527                 prot->slab = NULL;
3528         }
3529 out:
3530         return ret;
3531 }
3532 EXPORT_SYMBOL(proto_register);
3533
3534 void proto_unregister(struct proto *prot)
3535 {
3536         mutex_lock(&proto_list_mutex);
3537         release_proto_idx(prot);
3538         list_del(&prot->node);
3539         mutex_unlock(&proto_list_mutex);
3540
3541         kmem_cache_destroy(prot->slab);
3542         prot->slab = NULL;
3543
3544         req_prot_cleanup(prot->rsk_prot);
3545         tw_prot_cleanup(prot->twsk_prot);
3546 }
3547 EXPORT_SYMBOL(proto_unregister);
3548
3549 int sock_load_diag_module(int family, int protocol)
3550 {
3551         if (!protocol) {
3552                 if (!sock_is_registered(family))
3553                         return -ENOENT;
3554
3555                 return request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
3556                                       NETLINK_SOCK_DIAG, family);
3557         }
3558
3559 #ifdef CONFIG_INET
3560         if (family == AF_INET &&
3561             protocol != IPPROTO_RAW &&
3562             protocol < MAX_INET_PROTOS &&
3563             !rcu_access_pointer(inet_protos[protocol]))
3564                 return -ENOENT;
3565 #endif
3566
3567         return request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
3568                               NETLINK_SOCK_DIAG, family, protocol);
3569 }
3570 EXPORT_SYMBOL(sock_load_diag_module);
3571
3572 #ifdef CONFIG_PROC_FS
3573 static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
3574         __acquires(proto_list_mutex)
3575 {
3576         mutex_lock(&proto_list_mutex);
3577         return seq_list_start_head(&proto_list, *pos);
3578 }
3579
3580 static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3581 {
3582         return seq_list_next(v, &proto_list, pos);
3583 }
3584
3585 static void proto_seq_stop(struct seq_file *seq, void *v)
3586         __releases(proto_list_mutex)
3587 {
3588         mutex_unlock(&proto_list_mutex);
3589 }
3590
3591 static char proto_method_implemented(const void *method)
3592 {
3593         return method == NULL ? 'n' : 'y';
3594 }
3595 static long sock_prot_memory_allocated(struct proto *proto)
3596 {
3597         return proto->memory_allocated != NULL ? proto_memory_allocated(proto) : -1L;
3598 }
3599
3600 static const char *sock_prot_memory_pressure(struct proto *proto)
3601 {
3602         return proto->memory_pressure != NULL ?
3603         proto_memory_pressure(proto) ? "yes" : "no" : "NI";
3604 }
3605
3606 static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
3607 {
3608
3609         seq_printf(seq, "%-9s %4u %6d  %6ld   %-3s %6u   %-3s  %-10s "
3610                         "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
3611                    proto->name,
3612                    proto->obj_size,
3613                    sock_prot_inuse_get(seq_file_net(seq), proto),
3614                    sock_prot_memory_allocated(proto),
3615                    sock_prot_memory_pressure(proto),
3616                    proto->max_header,
3617                    proto->slab == NULL ? "no" : "yes",
3618                    module_name(proto->owner),
3619                    proto_method_implemented(proto->close),
3620                    proto_method_implemented(proto->connect),
3621                    proto_method_implemented(proto->disconnect),
3622                    proto_method_implemented(proto->accept),
3623                    proto_method_implemented(proto->ioctl),
3624                    proto_method_implemented(proto->init),
3625                    proto_method_implemented(proto->destroy),
3626                    proto_method_implemented(proto->shutdown),
3627                    proto_method_implemented(proto->setsockopt),
3628                    proto_method_implemented(proto->getsockopt),
3629                    proto_method_implemented(proto->sendmsg),
3630                    proto_method_implemented(proto->recvmsg),
3631                    proto_method_implemented(proto->sendpage),
3632                    proto_method_implemented(proto->bind),
3633                    proto_method_implemented(proto->backlog_rcv),
3634                    proto_method_implemented(proto->hash),
3635                    proto_method_implemented(proto->unhash),
3636                    proto_method_implemented(proto->get_port),
3637                    proto_method_implemented(proto->enter_memory_pressure));
3638 }
3639
3640 static int proto_seq_show(struct seq_file *seq, void *v)
3641 {
3642         if (v == &proto_list)
3643                 seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
3644                            "protocol",
3645                            "size",
3646                            "sockets",
3647                            "memory",
3648                            "press",
3649                            "maxhdr",
3650                            "slab",
3651                            "module",
3652                            "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
3653         else
3654                 proto_seq_printf(seq, list_entry(v, struct proto, node));
3655         return 0;
3656 }
3657
3658 static const struct seq_operations proto_seq_ops = {
3659         .start  = proto_seq_start,
3660         .next   = proto_seq_next,
3661         .stop   = proto_seq_stop,
3662         .show   = proto_seq_show,
3663 };
3664
3665 static __net_init int proto_init_net(struct net *net)
3666 {
3667         if (!proc_create_net("protocols", 0444, net->proc_net, &proto_seq_ops,
3668                         sizeof(struct seq_net_private)))
3669                 return -ENOMEM;
3670
3671         return 0;
3672 }
3673
3674 static __net_exit void proto_exit_net(struct net *net)
3675 {
3676         remove_proc_entry("protocols", net->proc_net);
3677 }
3678
3679
3680 static __net_initdata struct pernet_operations proto_net_ops = {
3681         .init = proto_init_net,
3682         .exit = proto_exit_net,
3683 };
3684
3685 static int __init proto_init(void)
3686 {
3687         return register_pernet_subsys(&proto_net_ops);
3688 }
3689
3690 subsys_initcall(proto_init);
3691
3692 #endif /* PROC_FS */
3693
3694 #ifdef CONFIG_NET_RX_BUSY_POLL
3695 bool sk_busy_loop_end(void *p, unsigned long start_time)
3696 {
3697         struct sock *sk = p;
3698
3699         return !skb_queue_empty_lockless(&sk->sk_receive_queue) ||
3700                sk_busy_loop_timeout(sk, start_time);
3701 }
3702 EXPORT_SYMBOL(sk_busy_loop_end);
3703 #endif /* CONFIG_NET_RX_BUSY_POLL */
3704
3705 int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len)
3706 {
3707         if (!sk->sk_prot->bind_add)
3708                 return -EOPNOTSUPP;
3709         return sk->sk_prot->bind_add(sk, addr, addr_len);
3710 }
3711 EXPORT_SYMBOL(sock_bind_add);