socket: initial cgroup code.
[deliverable/linux.git] / net / core / sock.c
CommitLineData
1da177e4
LT
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Generic socket support routines. Memory allocators, socket lock/release
7 * handler for protocols to use and generic option handler.
8 *
9 *
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
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
4ec93edb 35 * code. The ACK stuff can wait and needs major
1da177e4
LT
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 * This program is free software; you can redistribute it and/or
87 * modify it under the terms of the GNU General Public License
88 * as published by the Free Software Foundation; either version
89 * 2 of the License, or (at your option) any later version.
90 */
91
4fc268d2 92#include <linux/capability.h>
1da177e4
LT
93#include <linux/errno.h>
94#include <linux/types.h>
95#include <linux/socket.h>
96#include <linux/in.h>
97#include <linux/kernel.h>
1da177e4
LT
98#include <linux/module.h>
99#include <linux/proc_fs.h>
100#include <linux/seq_file.h>
101#include <linux/sched.h>
102#include <linux/timer.h>
103#include <linux/string.h>
104#include <linux/sockios.h>
105#include <linux/net.h>
106#include <linux/mm.h>
107#include <linux/slab.h>
108#include <linux/interrupt.h>
109#include <linux/poll.h>
110#include <linux/tcp.h>
111#include <linux/init.h>
a1f8e7f7 112#include <linux/highmem.h>
3f551f94 113#include <linux/user_namespace.h>
e1aab161 114#include <linux/jump_label.h>
1da177e4
LT
115
116#include <asm/uaccess.h>
117#include <asm/system.h>
118
119#include <linux/netdevice.h>
120#include <net/protocol.h>
121#include <linux/skbuff.h>
457c4cbc 122#include <net/net_namespace.h>
2e6599cb 123#include <net/request_sock.h>
1da177e4 124#include <net/sock.h>
20d49473 125#include <linux/net_tstamp.h>
1da177e4
LT
126#include <net/xfrm.h>
127#include <linux/ipsec.h>
f8451725 128#include <net/cls_cgroup.h>
5bc1421e 129#include <net/netprio_cgroup.h>
1da177e4
LT
130
131#include <linux/filter.h>
132
3847ce32
SM
133#include <trace/events/sock.h>
134
1da177e4
LT
135#ifdef CONFIG_INET
136#include <net/tcp.h>
137#endif
138
da21f24d
IM
139/*
140 * Each address family might have different locking rules, so we have
141 * one slock key per address family:
142 */
a5b5bb9a
IM
143static struct lock_class_key af_family_keys[AF_MAX];
144static struct lock_class_key af_family_slock_keys[AF_MAX];
145
e1aab161
GC
146struct jump_label_key memcg_socket_limit_enabled;
147EXPORT_SYMBOL(memcg_socket_limit_enabled);
148
a5b5bb9a
IM
149/*
150 * Make lock validator output more readable. (we pre-construct these
151 * strings build-time, so that runtime initialization of socket
152 * locks is fast):
153 */
36cbd3dc 154static const char *const af_family_key_strings[AF_MAX+1] = {
a5b5bb9a
IM
155 "sk_lock-AF_UNSPEC", "sk_lock-AF_UNIX" , "sk_lock-AF_INET" ,
156 "sk_lock-AF_AX25" , "sk_lock-AF_IPX" , "sk_lock-AF_APPLETALK",
157 "sk_lock-AF_NETROM", "sk_lock-AF_BRIDGE" , "sk_lock-AF_ATMPVC" ,
158 "sk_lock-AF_X25" , "sk_lock-AF_INET6" , "sk_lock-AF_ROSE" ,
159 "sk_lock-AF_DECnet", "sk_lock-AF_NETBEUI" , "sk_lock-AF_SECURITY" ,
160 "sk_lock-AF_KEY" , "sk_lock-AF_NETLINK" , "sk_lock-AF_PACKET" ,
161 "sk_lock-AF_ASH" , "sk_lock-AF_ECONET" , "sk_lock-AF_ATMSVC" ,
cbd151bf 162 "sk_lock-AF_RDS" , "sk_lock-AF_SNA" , "sk_lock-AF_IRDA" ,
a5b5bb9a 163 "sk_lock-AF_PPPOX" , "sk_lock-AF_WANPIPE" , "sk_lock-AF_LLC" ,
cd05acfe 164 "sk_lock-27" , "sk_lock-28" , "sk_lock-AF_CAN" ,
17926a79 165 "sk_lock-AF_TIPC" , "sk_lock-AF_BLUETOOTH", "sk_lock-IUCV" ,
bce7b154 166 "sk_lock-AF_RXRPC" , "sk_lock-AF_ISDN" , "sk_lock-AF_PHONET" ,
6f107b58 167 "sk_lock-AF_IEEE802154", "sk_lock-AF_CAIF" , "sk_lock-AF_ALG" ,
c7fe3b52 168 "sk_lock-AF_NFC" , "sk_lock-AF_MAX"
a5b5bb9a 169};
36cbd3dc 170static const char *const af_family_slock_key_strings[AF_MAX+1] = {
a5b5bb9a
IM
171 "slock-AF_UNSPEC", "slock-AF_UNIX" , "slock-AF_INET" ,
172 "slock-AF_AX25" , "slock-AF_IPX" , "slock-AF_APPLETALK",
173 "slock-AF_NETROM", "slock-AF_BRIDGE" , "slock-AF_ATMPVC" ,
174 "slock-AF_X25" , "slock-AF_INET6" , "slock-AF_ROSE" ,
175 "slock-AF_DECnet", "slock-AF_NETBEUI" , "slock-AF_SECURITY" ,
176 "slock-AF_KEY" , "slock-AF_NETLINK" , "slock-AF_PACKET" ,
177 "slock-AF_ASH" , "slock-AF_ECONET" , "slock-AF_ATMSVC" ,
cbd151bf 178 "slock-AF_RDS" , "slock-AF_SNA" , "slock-AF_IRDA" ,
a5b5bb9a 179 "slock-AF_PPPOX" , "slock-AF_WANPIPE" , "slock-AF_LLC" ,
cd05acfe 180 "slock-27" , "slock-28" , "slock-AF_CAN" ,
17926a79 181 "slock-AF_TIPC" , "slock-AF_BLUETOOTH", "slock-AF_IUCV" ,
bce7b154 182 "slock-AF_RXRPC" , "slock-AF_ISDN" , "slock-AF_PHONET" ,
6f107b58 183 "slock-AF_IEEE802154", "slock-AF_CAIF" , "slock-AF_ALG" ,
c7fe3b52 184 "slock-AF_NFC" , "slock-AF_MAX"
a5b5bb9a 185};
36cbd3dc 186static const char *const af_family_clock_key_strings[AF_MAX+1] = {
443aef0e
PZ
187 "clock-AF_UNSPEC", "clock-AF_UNIX" , "clock-AF_INET" ,
188 "clock-AF_AX25" , "clock-AF_IPX" , "clock-AF_APPLETALK",
189 "clock-AF_NETROM", "clock-AF_BRIDGE" , "clock-AF_ATMPVC" ,
190 "clock-AF_X25" , "clock-AF_INET6" , "clock-AF_ROSE" ,
191 "clock-AF_DECnet", "clock-AF_NETBEUI" , "clock-AF_SECURITY" ,
192 "clock-AF_KEY" , "clock-AF_NETLINK" , "clock-AF_PACKET" ,
193 "clock-AF_ASH" , "clock-AF_ECONET" , "clock-AF_ATMSVC" ,
cbd151bf 194 "clock-AF_RDS" , "clock-AF_SNA" , "clock-AF_IRDA" ,
443aef0e 195 "clock-AF_PPPOX" , "clock-AF_WANPIPE" , "clock-AF_LLC" ,
b4942af6 196 "clock-27" , "clock-28" , "clock-AF_CAN" ,
e51f802b 197 "clock-AF_TIPC" , "clock-AF_BLUETOOTH", "clock-AF_IUCV" ,
bce7b154 198 "clock-AF_RXRPC" , "clock-AF_ISDN" , "clock-AF_PHONET" ,
6f107b58 199 "clock-AF_IEEE802154", "clock-AF_CAIF" , "clock-AF_ALG" ,
c7fe3b52 200 "clock-AF_NFC" , "clock-AF_MAX"
443aef0e 201};
da21f24d
IM
202
203/*
204 * sk_callback_lock locking rules are per-address-family,
205 * so split the lock classes by using a per-AF key:
206 */
207static struct lock_class_key af_callback_keys[AF_MAX];
208
1da177e4
LT
209/* Take into consideration the size of the struct sk_buff overhead in the
210 * determination of these values, since that is non-constant across
211 * platforms. This makes socket queueing behavior and performance
212 * not depend upon such differences.
213 */
214#define _SK_MEM_PACKETS 256
87fb4b7b 215#define _SK_MEM_OVERHEAD SKB_TRUESIZE(256)
1da177e4
LT
216#define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
217#define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
218
219/* Run time adjustable parameters. */
ab32ea5d
BH
220__u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
221__u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
222__u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
223__u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
1da177e4 224
25985edc 225/* Maximal space eaten by iovec or ancillary data plus some space */
ab32ea5d 226int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
2a91525c 227EXPORT_SYMBOL(sysctl_optmem_max);
1da177e4 228
5bc1421e
NH
229#if defined(CONFIG_CGROUPS)
230#if !defined(CONFIG_NET_CLS_CGROUP)
f8451725
HX
231int net_cls_subsys_id = -1;
232EXPORT_SYMBOL_GPL(net_cls_subsys_id);
233#endif
5bc1421e
NH
234#if !defined(CONFIG_NETPRIO_CGROUP)
235int net_prio_subsys_id = -1;
236EXPORT_SYMBOL_GPL(net_prio_subsys_id);
237#endif
238#endif
f8451725 239
1da177e4
LT
240static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
241{
242 struct timeval tv;
243
244 if (optlen < sizeof(tv))
245 return -EINVAL;
246 if (copy_from_user(&tv, optval, sizeof(tv)))
247 return -EFAULT;
ba78073e
VA
248 if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
249 return -EDOM;
1da177e4 250
ba78073e 251 if (tv.tv_sec < 0) {
6f11df83
AM
252 static int warned __read_mostly;
253
ba78073e 254 *timeo_p = 0;
50aab54f 255 if (warned < 10 && net_ratelimit()) {
ba78073e
VA
256 warned++;
257 printk(KERN_INFO "sock_set_timeout: `%s' (pid %d) "
258 "tries to set negative timeout\n",
ba25f9dc 259 current->comm, task_pid_nr(current));
50aab54f 260 }
ba78073e
VA
261 return 0;
262 }
1da177e4
LT
263 *timeo_p = MAX_SCHEDULE_TIMEOUT;
264 if (tv.tv_sec == 0 && tv.tv_usec == 0)
265 return 0;
266 if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
267 *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
268 return 0;
269}
270
271static void sock_warn_obsolete_bsdism(const char *name)
272{
273 static int warned;
274 static char warncomm[TASK_COMM_LEN];
4ec93edb
YH
275 if (strcmp(warncomm, current->comm) && warned < 5) {
276 strcpy(warncomm, current->comm);
1da177e4
LT
277 printk(KERN_WARNING "process `%s' is using obsolete "
278 "%s SO_BSDCOMPAT\n", warncomm, name);
279 warned++;
280 }
281}
282
08e29af3
ED
283#define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE))
284
285static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
4ec93edb 286{
08e29af3
ED
287 if (sk->sk_flags & flags) {
288 sk->sk_flags &= ~flags;
289 if (!(sk->sk_flags & SK_FLAGS_TIMESTAMP))
20d49473 290 net_disable_timestamp();
1da177e4
LT
291 }
292}
293
294
f0088a50
DV
295int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
296{
766e9037 297 int err;
f0088a50 298 int skb_len;
3b885787
NH
299 unsigned long flags;
300 struct sk_buff_head *list = &sk->sk_receive_queue;
f0088a50 301
9ee6b7f1 302 /* Cast sk->rcvbuf to unsigned... It's pointless, but reduces
f0088a50
DV
303 number of warnings when compiling with -W --ANK
304 */
305 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
306 (unsigned)sk->sk_rcvbuf) {
766e9037 307 atomic_inc(&sk->sk_drops);
3847ce32 308 trace_sock_rcvqueue_full(sk, skb);
766e9037 309 return -ENOMEM;
f0088a50
DV
310 }
311
fda9ef5d 312 err = sk_filter(sk, skb);
f0088a50 313 if (err)
766e9037 314 return err;
f0088a50 315
3ab224be 316 if (!sk_rmem_schedule(sk, skb->truesize)) {
766e9037
ED
317 atomic_inc(&sk->sk_drops);
318 return -ENOBUFS;
3ab224be
HA
319 }
320
f0088a50
DV
321 skb->dev = NULL;
322 skb_set_owner_r(skb, sk);
49ad9599 323
f0088a50
DV
324 /* Cache the SKB length before we tack it onto the receive
325 * queue. Once it is added it no longer belongs to us and
326 * may be freed by other threads of control pulling packets
327 * from the queue.
328 */
329 skb_len = skb->len;
330
7fee226a
ED
331 /* we escape from rcu protected region, make sure we dont leak
332 * a norefcounted dst
333 */
334 skb_dst_force(skb);
335
3b885787
NH
336 spin_lock_irqsave(&list->lock, flags);
337 skb->dropcount = atomic_read(&sk->sk_drops);
338 __skb_queue_tail(list, skb);
339 spin_unlock_irqrestore(&list->lock, flags);
f0088a50
DV
340
341 if (!sock_flag(sk, SOCK_DEAD))
342 sk->sk_data_ready(sk, skb_len);
766e9037 343 return 0;
f0088a50
DV
344}
345EXPORT_SYMBOL(sock_queue_rcv_skb);
346
58a5a7b9 347int sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested)
f0088a50
DV
348{
349 int rc = NET_RX_SUCCESS;
350
fda9ef5d 351 if (sk_filter(sk, skb))
f0088a50
DV
352 goto discard_and_relse;
353
354 skb->dev = NULL;
355
c377411f
ED
356 if (sk_rcvqueues_full(sk, skb)) {
357 atomic_inc(&sk->sk_drops);
358 goto discard_and_relse;
359 }
58a5a7b9
ACM
360 if (nested)
361 bh_lock_sock_nested(sk);
362 else
363 bh_lock_sock(sk);
a5b5bb9a
IM
364 if (!sock_owned_by_user(sk)) {
365 /*
366 * trylock + unlock semantics:
367 */
368 mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
369
c57943a1 370 rc = sk_backlog_rcv(sk, skb);
a5b5bb9a
IM
371
372 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
a3a858ff 373 } else if (sk_add_backlog(sk, skb)) {
8eae939f
ZY
374 bh_unlock_sock(sk);
375 atomic_inc(&sk->sk_drops);
376 goto discard_and_relse;
377 }
378
f0088a50
DV
379 bh_unlock_sock(sk);
380out:
381 sock_put(sk);
382 return rc;
383discard_and_relse:
384 kfree_skb(skb);
385 goto out;
386}
387EXPORT_SYMBOL(sk_receive_skb);
388
ea94ff3b
KK
389void sk_reset_txq(struct sock *sk)
390{
391 sk_tx_queue_clear(sk);
392}
393EXPORT_SYMBOL(sk_reset_txq);
394
f0088a50
DV
395struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
396{
b6c6712a 397 struct dst_entry *dst = __sk_dst_get(sk);
f0088a50
DV
398
399 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
e022f0b4 400 sk_tx_queue_clear(sk);
a9b3cd7f 401 RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
f0088a50
DV
402 dst_release(dst);
403 return NULL;
404 }
405
406 return dst;
407}
408EXPORT_SYMBOL(__sk_dst_check);
409
410struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
411{
412 struct dst_entry *dst = sk_dst_get(sk);
413
414 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
415 sk_dst_reset(sk);
416 dst_release(dst);
417 return NULL;
418 }
419
420 return dst;
421}
422EXPORT_SYMBOL(sk_dst_check);
423
4878809f
DM
424static int sock_bindtodevice(struct sock *sk, char __user *optval, int optlen)
425{
426 int ret = -ENOPROTOOPT;
427#ifdef CONFIG_NETDEVICES
3b1e0a65 428 struct net *net = sock_net(sk);
4878809f
DM
429 char devname[IFNAMSIZ];
430 int index;
431
432 /* Sorry... */
433 ret = -EPERM;
434 if (!capable(CAP_NET_RAW))
435 goto out;
436
437 ret = -EINVAL;
438 if (optlen < 0)
439 goto out;
440
441 /* Bind this socket to a particular device like "eth0",
442 * as specified in the passed interface name. If the
443 * name is "" or the option length is zero the socket
444 * is not bound.
445 */
446 if (optlen > IFNAMSIZ - 1)
447 optlen = IFNAMSIZ - 1;
448 memset(devname, 0, sizeof(devname));
449
450 ret = -EFAULT;
451 if (copy_from_user(devname, optval, optlen))
452 goto out;
453
000ba2e4
DM
454 index = 0;
455 if (devname[0] != '\0') {
bf8e56bf 456 struct net_device *dev;
4878809f 457
bf8e56bf
ED
458 rcu_read_lock();
459 dev = dev_get_by_name_rcu(net, devname);
460 if (dev)
461 index = dev->ifindex;
462 rcu_read_unlock();
4878809f
DM
463 ret = -ENODEV;
464 if (!dev)
465 goto out;
4878809f
DM
466 }
467
468 lock_sock(sk);
469 sk->sk_bound_dev_if = index;
470 sk_dst_reset(sk);
471 release_sock(sk);
472
473 ret = 0;
474
475out:
476#endif
477
478 return ret;
479}
480
c0ef877b
PE
481static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
482{
483 if (valbool)
484 sock_set_flag(sk, bit);
485 else
486 sock_reset_flag(sk, bit);
487}
488
1da177e4
LT
489/*
490 * This is meant for all protocols to use and covers goings on
491 * at the socket level. Everything here is generic.
492 */
493
494int sock_setsockopt(struct socket *sock, int level, int optname,
b7058842 495 char __user *optval, unsigned int optlen)
1da177e4 496{
2a91525c 497 struct sock *sk = sock->sk;
1da177e4
LT
498 int val;
499 int valbool;
500 struct linger ling;
501 int ret = 0;
4ec93edb 502
1da177e4
LT
503 /*
504 * Options without arguments
505 */
506
4878809f
DM
507 if (optname == SO_BINDTODEVICE)
508 return sock_bindtodevice(sk, optval, optlen);
509
e71a4783
SH
510 if (optlen < sizeof(int))
511 return -EINVAL;
4ec93edb 512
1da177e4
LT
513 if (get_user(val, (int __user *)optval))
514 return -EFAULT;
4ec93edb 515
2a91525c 516 valbool = val ? 1 : 0;
1da177e4
LT
517
518 lock_sock(sk);
519
2a91525c 520 switch (optname) {
e71a4783 521 case SO_DEBUG:
2a91525c 522 if (val && !capable(CAP_NET_ADMIN))
e71a4783 523 ret = -EACCES;
2a91525c 524 else
c0ef877b 525 sock_valbool_flag(sk, SOCK_DBG, valbool);
e71a4783
SH
526 break;
527 case SO_REUSEADDR:
528 sk->sk_reuse = valbool;
529 break;
530 case SO_TYPE:
49c794e9 531 case SO_PROTOCOL:
0d6038ee 532 case SO_DOMAIN:
e71a4783
SH
533 case SO_ERROR:
534 ret = -ENOPROTOOPT;
535 break;
536 case SO_DONTROUTE:
c0ef877b 537 sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
e71a4783
SH
538 break;
539 case SO_BROADCAST:
540 sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
541 break;
542 case SO_SNDBUF:
543 /* Don't error on this BSD doesn't and if you think
544 about it this is right. Otherwise apps have to
545 play 'guess the biggest size' games. RCVBUF/SNDBUF
546 are treated in BSD as hints */
547
548 if (val > sysctl_wmem_max)
549 val = sysctl_wmem_max;
b0573dea 550set_sndbuf:
e71a4783
SH
551 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
552 if ((val * 2) < SOCK_MIN_SNDBUF)
553 sk->sk_sndbuf = SOCK_MIN_SNDBUF;
554 else
555 sk->sk_sndbuf = val * 2;
1da177e4 556
e71a4783
SH
557 /*
558 * Wake up sending tasks if we
559 * upped the value.
560 */
561 sk->sk_write_space(sk);
562 break;
1da177e4 563
e71a4783
SH
564 case SO_SNDBUFFORCE:
565 if (!capable(CAP_NET_ADMIN)) {
566 ret = -EPERM;
567 break;
568 }
569 goto set_sndbuf;
b0573dea 570
e71a4783
SH
571 case SO_RCVBUF:
572 /* Don't error on this BSD doesn't and if you think
573 about it this is right. Otherwise apps have to
574 play 'guess the biggest size' games. RCVBUF/SNDBUF
575 are treated in BSD as hints */
4ec93edb 576
e71a4783
SH
577 if (val > sysctl_rmem_max)
578 val = sysctl_rmem_max;
b0573dea 579set_rcvbuf:
e71a4783
SH
580 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
581 /*
582 * We double it on the way in to account for
583 * "struct sk_buff" etc. overhead. Applications
584 * assume that the SO_RCVBUF setting they make will
585 * allow that much actual data to be received on that
586 * socket.
587 *
588 * Applications are unaware that "struct sk_buff" and
589 * other overheads allocate from the receive buffer
590 * during socket buffer allocation.
591 *
592 * And after considering the possible alternatives,
593 * returning the value we actually used in getsockopt
594 * is the most desirable behavior.
595 */
596 if ((val * 2) < SOCK_MIN_RCVBUF)
597 sk->sk_rcvbuf = SOCK_MIN_RCVBUF;
598 else
599 sk->sk_rcvbuf = val * 2;
600 break;
601
602 case SO_RCVBUFFORCE:
603 if (!capable(CAP_NET_ADMIN)) {
604 ret = -EPERM;
1da177e4 605 break;
e71a4783
SH
606 }
607 goto set_rcvbuf;
1da177e4 608
e71a4783 609 case SO_KEEPALIVE:
1da177e4 610#ifdef CONFIG_INET
e71a4783
SH
611 if (sk->sk_protocol == IPPROTO_TCP)
612 tcp_set_keepalive(sk, valbool);
1da177e4 613#endif
e71a4783
SH
614 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
615 break;
616
617 case SO_OOBINLINE:
618 sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
619 break;
620
621 case SO_NO_CHECK:
622 sk->sk_no_check = valbool;
623 break;
624
625 case SO_PRIORITY:
626 if ((val >= 0 && val <= 6) || capable(CAP_NET_ADMIN))
627 sk->sk_priority = val;
628 else
629 ret = -EPERM;
630 break;
631
632 case SO_LINGER:
633 if (optlen < sizeof(ling)) {
634 ret = -EINVAL; /* 1003.1g */
1da177e4 635 break;
e71a4783 636 }
2a91525c 637 if (copy_from_user(&ling, optval, sizeof(ling))) {
e71a4783 638 ret = -EFAULT;
1da177e4 639 break;
e71a4783
SH
640 }
641 if (!ling.l_onoff)
642 sock_reset_flag(sk, SOCK_LINGER);
643 else {
1da177e4 644#if (BITS_PER_LONG == 32)
e71a4783
SH
645 if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
646 sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
1da177e4 647 else
e71a4783
SH
648#endif
649 sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
650 sock_set_flag(sk, SOCK_LINGER);
651 }
652 break;
653
654 case SO_BSDCOMPAT:
655 sock_warn_obsolete_bsdism("setsockopt");
656 break;
657
658 case SO_PASSCRED:
659 if (valbool)
660 set_bit(SOCK_PASSCRED, &sock->flags);
661 else
662 clear_bit(SOCK_PASSCRED, &sock->flags);
663 break;
664
665 case SO_TIMESTAMP:
92f37fd2 666 case SO_TIMESTAMPNS:
e71a4783 667 if (valbool) {
92f37fd2
ED
668 if (optname == SO_TIMESTAMP)
669 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
670 else
671 sock_set_flag(sk, SOCK_RCVTSTAMPNS);
e71a4783 672 sock_set_flag(sk, SOCK_RCVTSTAMP);
20d49473 673 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
92f37fd2 674 } else {
e71a4783 675 sock_reset_flag(sk, SOCK_RCVTSTAMP);
92f37fd2
ED
676 sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
677 }
e71a4783
SH
678 break;
679
20d49473
PO
680 case SO_TIMESTAMPING:
681 if (val & ~SOF_TIMESTAMPING_MASK) {
f249fb78 682 ret = -EINVAL;
20d49473
PO
683 break;
684 }
685 sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE,
686 val & SOF_TIMESTAMPING_TX_HARDWARE);
687 sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE,
688 val & SOF_TIMESTAMPING_TX_SOFTWARE);
689 sock_valbool_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE,
690 val & SOF_TIMESTAMPING_RX_HARDWARE);
691 if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
692 sock_enable_timestamp(sk,
693 SOCK_TIMESTAMPING_RX_SOFTWARE);
694 else
695 sock_disable_timestamp(sk,
08e29af3 696 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
20d49473
PO
697 sock_valbool_flag(sk, SOCK_TIMESTAMPING_SOFTWARE,
698 val & SOF_TIMESTAMPING_SOFTWARE);
699 sock_valbool_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE,
700 val & SOF_TIMESTAMPING_SYS_HARDWARE);
701 sock_valbool_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE,
702 val & SOF_TIMESTAMPING_RAW_HARDWARE);
703 break;
704
e71a4783
SH
705 case SO_RCVLOWAT:
706 if (val < 0)
707 val = INT_MAX;
708 sk->sk_rcvlowat = val ? : 1;
709 break;
710
711 case SO_RCVTIMEO:
712 ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
713 break;
714
715 case SO_SNDTIMEO:
716 ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
717 break;
1da177e4 718
e71a4783
SH
719 case SO_ATTACH_FILTER:
720 ret = -EINVAL;
721 if (optlen == sizeof(struct sock_fprog)) {
722 struct sock_fprog fprog;
1da177e4 723
e71a4783
SH
724 ret = -EFAULT;
725 if (copy_from_user(&fprog, optval, sizeof(fprog)))
1da177e4 726 break;
e71a4783
SH
727
728 ret = sk_attach_filter(&fprog, sk);
729 }
730 break;
731
732 case SO_DETACH_FILTER:
55b33325 733 ret = sk_detach_filter(sk);
e71a4783 734 break;
1da177e4 735
e71a4783
SH
736 case SO_PASSSEC:
737 if (valbool)
738 set_bit(SOCK_PASSSEC, &sock->flags);
739 else
740 clear_bit(SOCK_PASSSEC, &sock->flags);
741 break;
4a19ec58
LAT
742 case SO_MARK:
743 if (!capable(CAP_NET_ADMIN))
744 ret = -EPERM;
2a91525c 745 else
4a19ec58 746 sk->sk_mark = val;
4a19ec58 747 break;
877ce7c1 748
1da177e4
LT
749 /* We implement the SO_SNDLOWAT etc to
750 not be settable (1003.1g 5.3) */
3b885787 751 case SO_RXQ_OVFL:
8083f0fc 752 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
3b885787 753 break;
6e3e939f
JB
754
755 case SO_WIFI_STATUS:
756 sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
757 break;
758
e71a4783
SH
759 default:
760 ret = -ENOPROTOOPT;
761 break;
4ec93edb 762 }
1da177e4
LT
763 release_sock(sk);
764 return ret;
765}
2a91525c 766EXPORT_SYMBOL(sock_setsockopt);
1da177e4
LT
767
768
3f551f94
EB
769void cred_to_ucred(struct pid *pid, const struct cred *cred,
770 struct ucred *ucred)
771{
772 ucred->pid = pid_vnr(pid);
773 ucred->uid = ucred->gid = -1;
774 if (cred) {
775 struct user_namespace *current_ns = current_user_ns();
776
777 ucred->uid = user_ns_map_uid(current_ns, cred, cred->euid);
778 ucred->gid = user_ns_map_gid(current_ns, cred, cred->egid);
779 }
780}
3924773a 781EXPORT_SYMBOL_GPL(cred_to_ucred);
3f551f94 782
1da177e4
LT
783int sock_getsockopt(struct socket *sock, int level, int optname,
784 char __user *optval, int __user *optlen)
785{
786 struct sock *sk = sock->sk;
4ec93edb 787
e71a4783 788 union {
4ec93edb
YH
789 int val;
790 struct linger ling;
1da177e4
LT
791 struct timeval tm;
792 } v;
4ec93edb 793
4d0392be 794 int lv = sizeof(int);
1da177e4 795 int len;
4ec93edb 796
e71a4783 797 if (get_user(len, optlen))
4ec93edb 798 return -EFAULT;
e71a4783 799 if (len < 0)
1da177e4 800 return -EINVAL;
4ec93edb 801
50fee1de 802 memset(&v, 0, sizeof(v));
df0bca04 803
2a91525c 804 switch (optname) {
e71a4783
SH
805 case SO_DEBUG:
806 v.val = sock_flag(sk, SOCK_DBG);
807 break;
808
809 case SO_DONTROUTE:
810 v.val = sock_flag(sk, SOCK_LOCALROUTE);
811 break;
812
813 case SO_BROADCAST:
814 v.val = !!sock_flag(sk, SOCK_BROADCAST);
815 break;
816
817 case SO_SNDBUF:
818 v.val = sk->sk_sndbuf;
819 break;
820
821 case SO_RCVBUF:
822 v.val = sk->sk_rcvbuf;
823 break;
824
825 case SO_REUSEADDR:
826 v.val = sk->sk_reuse;
827 break;
828
829 case SO_KEEPALIVE:
830 v.val = !!sock_flag(sk, SOCK_KEEPOPEN);
831 break;
832
833 case SO_TYPE:
834 v.val = sk->sk_type;
835 break;
836
49c794e9
JE
837 case SO_PROTOCOL:
838 v.val = sk->sk_protocol;
839 break;
840
0d6038ee
JE
841 case SO_DOMAIN:
842 v.val = sk->sk_family;
843 break;
844
e71a4783
SH
845 case SO_ERROR:
846 v.val = -sock_error(sk);
2a91525c 847 if (v.val == 0)
e71a4783
SH
848 v.val = xchg(&sk->sk_err_soft, 0);
849 break;
850
851 case SO_OOBINLINE:
852 v.val = !!sock_flag(sk, SOCK_URGINLINE);
853 break;
854
855 case SO_NO_CHECK:
856 v.val = sk->sk_no_check;
857 break;
858
859 case SO_PRIORITY:
860 v.val = sk->sk_priority;
861 break;
862
863 case SO_LINGER:
864 lv = sizeof(v.ling);
865 v.ling.l_onoff = !!sock_flag(sk, SOCK_LINGER);
866 v.ling.l_linger = sk->sk_lingertime / HZ;
867 break;
868
869 case SO_BSDCOMPAT:
870 sock_warn_obsolete_bsdism("getsockopt");
871 break;
872
873 case SO_TIMESTAMP:
92f37fd2
ED
874 v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
875 !sock_flag(sk, SOCK_RCVTSTAMPNS);
876 break;
877
878 case SO_TIMESTAMPNS:
879 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS);
e71a4783
SH
880 break;
881
20d49473
PO
882 case SO_TIMESTAMPING:
883 v.val = 0;
884 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
885 v.val |= SOF_TIMESTAMPING_TX_HARDWARE;
886 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
887 v.val |= SOF_TIMESTAMPING_TX_SOFTWARE;
888 if (sock_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE))
889 v.val |= SOF_TIMESTAMPING_RX_HARDWARE;
890 if (sock_flag(sk, SOCK_TIMESTAMPING_RX_SOFTWARE))
891 v.val |= SOF_TIMESTAMPING_RX_SOFTWARE;
892 if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE))
893 v.val |= SOF_TIMESTAMPING_SOFTWARE;
894 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE))
895 v.val |= SOF_TIMESTAMPING_SYS_HARDWARE;
896 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE))
897 v.val |= SOF_TIMESTAMPING_RAW_HARDWARE;
898 break;
899
e71a4783 900 case SO_RCVTIMEO:
2a91525c 901 lv = sizeof(struct timeval);
e71a4783
SH
902 if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
903 v.tm.tv_sec = 0;
904 v.tm.tv_usec = 0;
905 } else {
906 v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
907 v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * 1000000) / HZ;
908 }
909 break;
910
911 case SO_SNDTIMEO:
2a91525c 912 lv = sizeof(struct timeval);
e71a4783
SH
913 if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
914 v.tm.tv_sec = 0;
915 v.tm.tv_usec = 0;
916 } else {
917 v.tm.tv_sec = sk->sk_sndtimeo / HZ;
918 v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * 1000000) / HZ;
919 }
920 break;
1da177e4 921
e71a4783
SH
922 case SO_RCVLOWAT:
923 v.val = sk->sk_rcvlowat;
924 break;
1da177e4 925
e71a4783 926 case SO_SNDLOWAT:
2a91525c 927 v.val = 1;
e71a4783 928 break;
1da177e4 929
e71a4783
SH
930 case SO_PASSCRED:
931 v.val = test_bit(SOCK_PASSCRED, &sock->flags) ? 1 : 0;
932 break;
1da177e4 933
e71a4783 934 case SO_PEERCRED:
109f6e39
EB
935 {
936 struct ucred peercred;
937 if (len > sizeof(peercred))
938 len = sizeof(peercred);
939 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
940 if (copy_to_user(optval, &peercred, len))
e71a4783
SH
941 return -EFAULT;
942 goto lenout;
109f6e39 943 }
1da177e4 944
e71a4783
SH
945 case SO_PEERNAME:
946 {
947 char address[128];
948
949 if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
950 return -ENOTCONN;
951 if (lv < len)
952 return -EINVAL;
953 if (copy_to_user(optval, address, len))
954 return -EFAULT;
955 goto lenout;
956 }
1da177e4 957
e71a4783
SH
958 /* Dubious BSD thing... Probably nobody even uses it, but
959 * the UNIX standard wants it for whatever reason... -DaveM
960 */
961 case SO_ACCEPTCONN:
962 v.val = sk->sk_state == TCP_LISTEN;
963 break;
1da177e4 964
e71a4783
SH
965 case SO_PASSSEC:
966 v.val = test_bit(SOCK_PASSSEC, &sock->flags) ? 1 : 0;
967 break;
877ce7c1 968
e71a4783
SH
969 case SO_PEERSEC:
970 return security_socket_getpeersec_stream(sock, optval, optlen, len);
1da177e4 971
4a19ec58
LAT
972 case SO_MARK:
973 v.val = sk->sk_mark;
974 break;
975
3b885787
NH
976 case SO_RXQ_OVFL:
977 v.val = !!sock_flag(sk, SOCK_RXQ_OVFL);
978 break;
979
6e3e939f
JB
980 case SO_WIFI_STATUS:
981 v.val = !!sock_flag(sk, SOCK_WIFI_STATUS);
982 break;
983
e71a4783
SH
984 default:
985 return -ENOPROTOOPT;
1da177e4 986 }
e71a4783 987
1da177e4
LT
988 if (len > lv)
989 len = lv;
990 if (copy_to_user(optval, &v, len))
991 return -EFAULT;
992lenout:
4ec93edb
YH
993 if (put_user(len, optlen))
994 return -EFAULT;
995 return 0;
1da177e4
LT
996}
997
a5b5bb9a
IM
998/*
999 * Initialize an sk_lock.
1000 *
1001 * (We also register the sk_lock with the lock validator.)
1002 */
b6f99a21 1003static inline void sock_lock_init(struct sock *sk)
a5b5bb9a 1004{
ed07536e
PZ
1005 sock_lock_init_class_and_name(sk,
1006 af_family_slock_key_strings[sk->sk_family],
1007 af_family_slock_keys + sk->sk_family,
1008 af_family_key_strings[sk->sk_family],
1009 af_family_keys + sk->sk_family);
a5b5bb9a
IM
1010}
1011
4dc6dc71
ED
1012/*
1013 * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
1014 * even temporarly, because of RCU lookups. sk_node should also be left as is.
68835aba 1015 * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
4dc6dc71 1016 */
f1a6c4da
PE
1017static void sock_copy(struct sock *nsk, const struct sock *osk)
1018{
1019#ifdef CONFIG_SECURITY_NETWORK
1020 void *sptr = nsk->sk_security;
1021#endif
68835aba
ED
1022 memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
1023
1024 memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
1025 osk->sk_prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
1026
f1a6c4da
PE
1027#ifdef CONFIG_SECURITY_NETWORK
1028 nsk->sk_security = sptr;
1029 security_sk_clone(osk, nsk);
1030#endif
1031}
1032
fcbdf09d
OP
1033/*
1034 * caches using SLAB_DESTROY_BY_RCU should let .next pointer from nulls nodes
1035 * un-modified. Special care is taken when initializing object to zero.
1036 */
1037static inline void sk_prot_clear_nulls(struct sock *sk, int size)
1038{
1039 if (offsetof(struct sock, sk_node.next) != 0)
1040 memset(sk, 0, offsetof(struct sock, sk_node.next));
1041 memset(&sk->sk_node.pprev, 0,
1042 size - offsetof(struct sock, sk_node.pprev));
1043}
1044
1045void sk_prot_clear_portaddr_nulls(struct sock *sk, int size)
1046{
1047 unsigned long nulls1, nulls2;
1048
1049 nulls1 = offsetof(struct sock, __sk_common.skc_node.next);
1050 nulls2 = offsetof(struct sock, __sk_common.skc_portaddr_node.next);
1051 if (nulls1 > nulls2)
1052 swap(nulls1, nulls2);
1053
1054 if (nulls1 != 0)
1055 memset((char *)sk, 0, nulls1);
1056 memset((char *)sk + nulls1 + sizeof(void *), 0,
1057 nulls2 - nulls1 - sizeof(void *));
1058 memset((char *)sk + nulls2 + sizeof(void *), 0,
1059 size - nulls2 - sizeof(void *));
1060}
1061EXPORT_SYMBOL(sk_prot_clear_portaddr_nulls);
1062
2e4afe7b
PE
1063static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
1064 int family)
c308c1b2
PE
1065{
1066 struct sock *sk;
1067 struct kmem_cache *slab;
1068
1069 slab = prot->slab;
e912b114
ED
1070 if (slab != NULL) {
1071 sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
1072 if (!sk)
1073 return sk;
1074 if (priority & __GFP_ZERO) {
fcbdf09d
OP
1075 if (prot->clear_sk)
1076 prot->clear_sk(sk, prot->obj_size);
1077 else
1078 sk_prot_clear_nulls(sk, prot->obj_size);
e912b114 1079 }
fcbdf09d 1080 } else
c308c1b2
PE
1081 sk = kmalloc(prot->obj_size, priority);
1082
2e4afe7b 1083 if (sk != NULL) {
a98b65a3
VN
1084 kmemcheck_annotate_bitfield(sk, flags);
1085
2e4afe7b
PE
1086 if (security_sk_alloc(sk, family, priority))
1087 goto out_free;
1088
1089 if (!try_module_get(prot->owner))
1090 goto out_free_sec;
e022f0b4 1091 sk_tx_queue_clear(sk);
2e4afe7b
PE
1092 }
1093
c308c1b2 1094 return sk;
2e4afe7b
PE
1095
1096out_free_sec:
1097 security_sk_free(sk);
1098out_free:
1099 if (slab != NULL)
1100 kmem_cache_free(slab, sk);
1101 else
1102 kfree(sk);
1103 return NULL;
c308c1b2
PE
1104}
1105
1106static void sk_prot_free(struct proto *prot, struct sock *sk)
1107{
1108 struct kmem_cache *slab;
2e4afe7b 1109 struct module *owner;
c308c1b2 1110
2e4afe7b 1111 owner = prot->owner;
c308c1b2 1112 slab = prot->slab;
2e4afe7b
PE
1113
1114 security_sk_free(sk);
c308c1b2
PE
1115 if (slab != NULL)
1116 kmem_cache_free(slab, sk);
1117 else
1118 kfree(sk);
2e4afe7b 1119 module_put(owner);
c308c1b2
PE
1120}
1121
f8451725
HX
1122#ifdef CONFIG_CGROUPS
1123void sock_update_classid(struct sock *sk)
1124{
1144182a 1125 u32 classid;
f8451725 1126
1144182a
PM
1127 rcu_read_lock(); /* doing current task, which cannot vanish. */
1128 classid = task_cls_classid(current);
1129 rcu_read_unlock();
f8451725
HX
1130 if (classid && classid != sk->sk_classid)
1131 sk->sk_classid = classid;
1132}
82862742 1133EXPORT_SYMBOL(sock_update_classid);
5bc1421e
NH
1134
1135void sock_update_netprioidx(struct sock *sk)
1136{
1137 struct cgroup_netprio_state *state;
1138 if (in_interrupt())
1139 return;
1140 rcu_read_lock();
1141 state = task_netprio_state(current);
1142 sk->sk_cgrp_prioidx = state ? state->prioidx : 0;
1143 rcu_read_unlock();
1144}
1145EXPORT_SYMBOL_GPL(sock_update_netprioidx);
f8451725
HX
1146#endif
1147
1da177e4
LT
1148/**
1149 * sk_alloc - All socket objects are allocated here
c4ea43c5 1150 * @net: the applicable net namespace
4dc3b16b
PP
1151 * @family: protocol family
1152 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1153 * @prot: struct proto associated with this new sock instance
1da177e4 1154 */
1b8d7ae4 1155struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
6257ff21 1156 struct proto *prot)
1da177e4 1157{
c308c1b2 1158 struct sock *sk;
1da177e4 1159
154adbc8 1160 sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
1da177e4 1161 if (sk) {
154adbc8
PE
1162 sk->sk_family = family;
1163 /*
1164 * See comment in struct sock definition to understand
1165 * why we need sk_prot_creator -acme
1166 */
1167 sk->sk_prot = sk->sk_prot_creator = prot;
1168 sock_lock_init(sk);
3b1e0a65 1169 sock_net_set(sk, get_net(net));
d66ee058 1170 atomic_set(&sk->sk_wmem_alloc, 1);
f8451725
HX
1171
1172 sock_update_classid(sk);
5bc1421e 1173 sock_update_netprioidx(sk);
1da177e4 1174 }
a79af59e 1175
2e4afe7b 1176 return sk;
1da177e4 1177}
2a91525c 1178EXPORT_SYMBOL(sk_alloc);
1da177e4 1179
2b85a34e 1180static void __sk_free(struct sock *sk)
1da177e4
LT
1181{
1182 struct sk_filter *filter;
1da177e4
LT
1183
1184 if (sk->sk_destruct)
1185 sk->sk_destruct(sk);
1186
a898def2
PM
1187 filter = rcu_dereference_check(sk->sk_filter,
1188 atomic_read(&sk->sk_wmem_alloc) == 0);
1da177e4 1189 if (filter) {
309dd5fc 1190 sk_filter_uncharge(sk, filter);
a9b3cd7f 1191 RCU_INIT_POINTER(sk->sk_filter, NULL);
1da177e4
LT
1192 }
1193
08e29af3 1194 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
1da177e4
LT
1195
1196 if (atomic_read(&sk->sk_omem_alloc))
1197 printk(KERN_DEBUG "%s: optmem leakage (%d bytes) detected.\n",
0dc47877 1198 __func__, atomic_read(&sk->sk_omem_alloc));
1da177e4 1199
109f6e39
EB
1200 if (sk->sk_peer_cred)
1201 put_cred(sk->sk_peer_cred);
1202 put_pid(sk->sk_peer_pid);
3b1e0a65 1203 put_net(sock_net(sk));
c308c1b2 1204 sk_prot_free(sk->sk_prot_creator, sk);
1da177e4 1205}
2b85a34e
ED
1206
1207void sk_free(struct sock *sk)
1208{
1209 /*
25985edc 1210 * We subtract one from sk_wmem_alloc and can know if
2b85a34e
ED
1211 * some packets are still in some tx queue.
1212 * If not null, sock_wfree() will call __sk_free(sk) later
1213 */
1214 if (atomic_dec_and_test(&sk->sk_wmem_alloc))
1215 __sk_free(sk);
1216}
2a91525c 1217EXPORT_SYMBOL(sk_free);
1da177e4 1218
edf02087 1219/*
25985edc
LDM
1220 * Last sock_put should drop reference to sk->sk_net. It has already
1221 * been dropped in sk_change_net. Taking reference to stopping namespace
edf02087 1222 * is not an option.
25985edc 1223 * Take reference to a socket to remove it from hash _alive_ and after that
edf02087
DL
1224 * destroy it in the context of init_net.
1225 */
1226void sk_release_kernel(struct sock *sk)
1227{
1228 if (sk == NULL || sk->sk_socket == NULL)
1229 return;
1230
1231 sock_hold(sk);
1232 sock_release(sk->sk_socket);
65a18ec5 1233 release_net(sock_net(sk));
3b1e0a65 1234 sock_net_set(sk, get_net(&init_net));
edf02087
DL
1235 sock_put(sk);
1236}
45af1754 1237EXPORT_SYMBOL(sk_release_kernel);
edf02087 1238
e56c57d0
ED
1239/**
1240 * sk_clone_lock - clone a socket, and lock its clone
1241 * @sk: the socket to clone
1242 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
1243 *
1244 * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
1245 */
1246struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
87d11ceb 1247{
8fd1d178 1248 struct sock *newsk;
87d11ceb 1249
8fd1d178 1250 newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family);
87d11ceb
ACM
1251 if (newsk != NULL) {
1252 struct sk_filter *filter;
1253
892c141e 1254 sock_copy(newsk, sk);
87d11ceb
ACM
1255
1256 /* SANITY */
3b1e0a65 1257 get_net(sock_net(newsk));
87d11ceb
ACM
1258 sk_node_init(&newsk->sk_node);
1259 sock_lock_init(newsk);
1260 bh_lock_sock(newsk);
fa438ccf 1261 newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
8eae939f 1262 newsk->sk_backlog.len = 0;
87d11ceb
ACM
1263
1264 atomic_set(&newsk->sk_rmem_alloc, 0);
2b85a34e
ED
1265 /*
1266 * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
1267 */
1268 atomic_set(&newsk->sk_wmem_alloc, 1);
87d11ceb
ACM
1269 atomic_set(&newsk->sk_omem_alloc, 0);
1270 skb_queue_head_init(&newsk->sk_receive_queue);
1271 skb_queue_head_init(&newsk->sk_write_queue);
97fc2f08
CL
1272#ifdef CONFIG_NET_DMA
1273 skb_queue_head_init(&newsk->sk_async_wait_queue);
1274#endif
87d11ceb 1275
b6c6712a 1276 spin_lock_init(&newsk->sk_dst_lock);
87d11ceb 1277 rwlock_init(&newsk->sk_callback_lock);
443aef0e
PZ
1278 lockdep_set_class_and_name(&newsk->sk_callback_lock,
1279 af_callback_keys + newsk->sk_family,
1280 af_family_clock_key_strings[newsk->sk_family]);
87d11ceb
ACM
1281
1282 newsk->sk_dst_cache = NULL;
1283 newsk->sk_wmem_queued = 0;
1284 newsk->sk_forward_alloc = 0;
1285 newsk->sk_send_head = NULL;
87d11ceb
ACM
1286 newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
1287
1288 sock_reset_flag(newsk, SOCK_DONE);
1289 skb_queue_head_init(&newsk->sk_error_queue);
1290
0d7da9dd 1291 filter = rcu_dereference_protected(newsk->sk_filter, 1);
87d11ceb
ACM
1292 if (filter != NULL)
1293 sk_filter_charge(newsk, filter);
1294
1295 if (unlikely(xfrm_sk_clone_policy(newsk))) {
1296 /* It is still raw copy of parent, so invalidate
1297 * destructor and make plain sk_free() */
1298 newsk->sk_destruct = NULL;
b0691c8e 1299 bh_unlock_sock(newsk);
87d11ceb
ACM
1300 sk_free(newsk);
1301 newsk = NULL;
1302 goto out;
1303 }
1304
1305 newsk->sk_err = 0;
1306 newsk->sk_priority = 0;
4dc6dc71
ED
1307 /*
1308 * Before updating sk_refcnt, we must commit prior changes to memory
1309 * (Documentation/RCU/rculist_nulls.txt for details)
1310 */
1311 smp_wmb();
87d11ceb
ACM
1312 atomic_set(&newsk->sk_refcnt, 2);
1313
1314 /*
1315 * Increment the counter in the same struct proto as the master
1316 * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
1317 * is the same as sk->sk_prot->socks, as this field was copied
1318 * with memcpy).
1319 *
1320 * This _changes_ the previous behaviour, where
1321 * tcp_create_openreq_child always was incrementing the
1322 * equivalent to tcp_prot->socks (inet_sock_nr), so this have
1323 * to be taken into account in all callers. -acme
1324 */
1325 sk_refcnt_debug_inc(newsk);
972692e0 1326 sk_set_socket(newsk, NULL);
43815482 1327 newsk->sk_wq = NULL;
87d11ceb
ACM
1328
1329 if (newsk->sk_prot->sockets_allocated)
180d8cd9 1330 sk_sockets_allocated_inc(newsk);
704da560 1331
08e29af3 1332 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
704da560 1333 net_enable_timestamp();
87d11ceb
ACM
1334 }
1335out:
1336 return newsk;
1337}
e56c57d0 1338EXPORT_SYMBOL_GPL(sk_clone_lock);
87d11ceb 1339
9958089a
AK
1340void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
1341{
1342 __sk_dst_set(sk, dst);
1343 sk->sk_route_caps = dst->dev->features;
1344 if (sk->sk_route_caps & NETIF_F_GSO)
4fcd6b99 1345 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
a465419b 1346 sk->sk_route_caps &= ~sk->sk_route_nocaps;
9958089a 1347 if (sk_can_gso(sk)) {
82cc1a7a 1348 if (dst->header_len) {
9958089a 1349 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
82cc1a7a 1350 } else {
9958089a 1351 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
82cc1a7a
PWJ
1352 sk->sk_gso_max_size = dst->dev->gso_max_size;
1353 }
9958089a
AK
1354 }
1355}
1356EXPORT_SYMBOL_GPL(sk_setup_caps);
1357
1da177e4
LT
1358void __init sk_init(void)
1359{
4481374c 1360 if (totalram_pages <= 4096) {
1da177e4
LT
1361 sysctl_wmem_max = 32767;
1362 sysctl_rmem_max = 32767;
1363 sysctl_wmem_default = 32767;
1364 sysctl_rmem_default = 32767;
4481374c 1365 } else if (totalram_pages >= 131072) {
1da177e4
LT
1366 sysctl_wmem_max = 131071;
1367 sysctl_rmem_max = 131071;
1368 }
1369}
1370
1371/*
1372 * Simple resource managers for sockets.
1373 */
1374
1375
4ec93edb
YH
1376/*
1377 * Write buffer destructor automatically called from kfree_skb.
1da177e4
LT
1378 */
1379void sock_wfree(struct sk_buff *skb)
1380{
1381 struct sock *sk = skb->sk;
d99927f4 1382 unsigned int len = skb->truesize;
1da177e4 1383
d99927f4
ED
1384 if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
1385 /*
1386 * Keep a reference on sk_wmem_alloc, this will be released
1387 * after sk_write_space() call
1388 */
1389 atomic_sub(len - 1, &sk->sk_wmem_alloc);
1da177e4 1390 sk->sk_write_space(sk);
d99927f4
ED
1391 len = 1;
1392 }
2b85a34e 1393 /*
d99927f4
ED
1394 * if sk_wmem_alloc reaches 0, we must finish what sk_free()
1395 * could not do because of in-flight packets
2b85a34e 1396 */
d99927f4 1397 if (atomic_sub_and_test(len, &sk->sk_wmem_alloc))
2b85a34e 1398 __sk_free(sk);
1da177e4 1399}
2a91525c 1400EXPORT_SYMBOL(sock_wfree);
1da177e4 1401
4ec93edb
YH
1402/*
1403 * Read buffer destructor automatically called from kfree_skb.
1da177e4
LT
1404 */
1405void sock_rfree(struct sk_buff *skb)
1406{
1407 struct sock *sk = skb->sk;
d361fd59 1408 unsigned int len = skb->truesize;
1da177e4 1409
d361fd59
ED
1410 atomic_sub(len, &sk->sk_rmem_alloc);
1411 sk_mem_uncharge(sk, len);
1da177e4 1412}
2a91525c 1413EXPORT_SYMBOL(sock_rfree);
1da177e4
LT
1414
1415
1416int sock_i_uid(struct sock *sk)
1417{
1418 int uid;
1419
f064af1e 1420 read_lock_bh(&sk->sk_callback_lock);
1da177e4 1421 uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : 0;
f064af1e 1422 read_unlock_bh(&sk->sk_callback_lock);
1da177e4
LT
1423 return uid;
1424}
2a91525c 1425EXPORT_SYMBOL(sock_i_uid);
1da177e4
LT
1426
1427unsigned long sock_i_ino(struct sock *sk)
1428{
1429 unsigned long ino;
1430
f064af1e 1431 read_lock_bh(&sk->sk_callback_lock);
1da177e4 1432 ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
f064af1e 1433 read_unlock_bh(&sk->sk_callback_lock);
1da177e4
LT
1434 return ino;
1435}
2a91525c 1436EXPORT_SYMBOL(sock_i_ino);
1da177e4
LT
1437
1438/*
1439 * Allocate a skb from the socket's send buffer.
1440 */
86a76caf 1441struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
dd0fc66f 1442 gfp_t priority)
1da177e4
LT
1443{
1444 if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
2a91525c 1445 struct sk_buff *skb = alloc_skb(size, priority);
1da177e4
LT
1446 if (skb) {
1447 skb_set_owner_w(skb, sk);
1448 return skb;
1449 }
1450 }
1451 return NULL;
1452}
2a91525c 1453EXPORT_SYMBOL(sock_wmalloc);
1da177e4
LT
1454
1455/*
1456 * Allocate a skb from the socket's receive buffer.
4ec93edb 1457 */
86a76caf 1458struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force,
dd0fc66f 1459 gfp_t priority)
1da177e4
LT
1460{
1461 if (force || atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
1462 struct sk_buff *skb = alloc_skb(size, priority);
1463 if (skb) {
1464 skb_set_owner_r(skb, sk);
1465 return skb;
1466 }
1467 }
1468 return NULL;
1469}
1470
4ec93edb 1471/*
1da177e4 1472 * Allocate a memory block from the socket's option memory buffer.
4ec93edb 1473 */
dd0fc66f 1474void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
1da177e4
LT
1475{
1476 if ((unsigned)size <= sysctl_optmem_max &&
1477 atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
1478 void *mem;
1479 /* First do the add, to avoid the race if kmalloc
4ec93edb 1480 * might sleep.
1da177e4
LT
1481 */
1482 atomic_add(size, &sk->sk_omem_alloc);
1483 mem = kmalloc(size, priority);
1484 if (mem)
1485 return mem;
1486 atomic_sub(size, &sk->sk_omem_alloc);
1487 }
1488 return NULL;
1489}
2a91525c 1490EXPORT_SYMBOL(sock_kmalloc);
1da177e4
LT
1491
1492/*
1493 * Free an option memory block.
1494 */
1495void sock_kfree_s(struct sock *sk, void *mem, int size)
1496{
1497 kfree(mem);
1498 atomic_sub(size, &sk->sk_omem_alloc);
1499}
2a91525c 1500EXPORT_SYMBOL(sock_kfree_s);
1da177e4
LT
1501
1502/* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
1503 I think, these locks should be removed for datagram sockets.
1504 */
2a91525c 1505static long sock_wait_for_wmem(struct sock *sk, long timeo)
1da177e4
LT
1506{
1507 DEFINE_WAIT(wait);
1508
1509 clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1510 for (;;) {
1511 if (!timeo)
1512 break;
1513 if (signal_pending(current))
1514 break;
1515 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
aa395145 1516 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1da177e4
LT
1517 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
1518 break;
1519 if (sk->sk_shutdown & SEND_SHUTDOWN)
1520 break;
1521 if (sk->sk_err)
1522 break;
1523 timeo = schedule_timeout(timeo);
1524 }
aa395145 1525 finish_wait(sk_sleep(sk), &wait);
1da177e4
LT
1526 return timeo;
1527}
1528
1529
1530/*
1531 * Generic send/receive buffer handlers
1532 */
1533
4cc7f68d
HX
1534struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
1535 unsigned long data_len, int noblock,
1536 int *errcode)
1da177e4
LT
1537{
1538 struct sk_buff *skb;
7d877f3b 1539 gfp_t gfp_mask;
1da177e4
LT
1540 long timeo;
1541 int err;
1542
1543 gfp_mask = sk->sk_allocation;
1544 if (gfp_mask & __GFP_WAIT)
1545 gfp_mask |= __GFP_REPEAT;
1546
1547 timeo = sock_sndtimeo(sk, noblock);
1548 while (1) {
1549 err = sock_error(sk);
1550 if (err != 0)
1551 goto failure;
1552
1553 err = -EPIPE;
1554 if (sk->sk_shutdown & SEND_SHUTDOWN)
1555 goto failure;
1556
1557 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
db38c179 1558 skb = alloc_skb(header_len, gfp_mask);
1da177e4
LT
1559 if (skb) {
1560 int npages;
1561 int i;
1562
1563 /* No pages, we're done... */
1564 if (!data_len)
1565 break;
1566
1567 npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
1568 skb->truesize += data_len;
1569 skb_shinfo(skb)->nr_frags = npages;
1570 for (i = 0; i < npages; i++) {
1571 struct page *page;
1da177e4
LT
1572
1573 page = alloc_pages(sk->sk_allocation, 0);
1574 if (!page) {
1575 err = -ENOBUFS;
1576 skb_shinfo(skb)->nr_frags = i;
1577 kfree_skb(skb);
1578 goto failure;
1579 }
1580
ea2ab693
IC
1581 __skb_fill_page_desc(skb, i,
1582 page, 0,
1583 (data_len >= PAGE_SIZE ?
1584 PAGE_SIZE :
1585 data_len));
1da177e4
LT
1586 data_len -= PAGE_SIZE;
1587 }
1588
1589 /* Full success... */
1590 break;
1591 }
1592 err = -ENOBUFS;
1593 goto failure;
1594 }
1595 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1596 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1597 err = -EAGAIN;
1598 if (!timeo)
1599 goto failure;
1600 if (signal_pending(current))
1601 goto interrupted;
1602 timeo = sock_wait_for_wmem(sk, timeo);
1603 }
1604
1605 skb_set_owner_w(skb, sk);
1606 return skb;
1607
1608interrupted:
1609 err = sock_intr_errno(timeo);
1610failure:
1611 *errcode = err;
1612 return NULL;
1613}
4cc7f68d 1614EXPORT_SYMBOL(sock_alloc_send_pskb);
1da177e4 1615
4ec93edb 1616struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
1da177e4
LT
1617 int noblock, int *errcode)
1618{
1619 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode);
1620}
2a91525c 1621EXPORT_SYMBOL(sock_alloc_send_skb);
1da177e4
LT
1622
1623static void __lock_sock(struct sock *sk)
f39234d6
NK
1624 __releases(&sk->sk_lock.slock)
1625 __acquires(&sk->sk_lock.slock)
1da177e4
LT
1626{
1627 DEFINE_WAIT(wait);
1628
e71a4783 1629 for (;;) {
1da177e4
LT
1630 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
1631 TASK_UNINTERRUPTIBLE);
1632 spin_unlock_bh(&sk->sk_lock.slock);
1633 schedule();
1634 spin_lock_bh(&sk->sk_lock.slock);
e71a4783 1635 if (!sock_owned_by_user(sk))
1da177e4
LT
1636 break;
1637 }
1638 finish_wait(&sk->sk_lock.wq, &wait);
1639}
1640
1641static void __release_sock(struct sock *sk)
f39234d6
NK
1642 __releases(&sk->sk_lock.slock)
1643 __acquires(&sk->sk_lock.slock)
1da177e4
LT
1644{
1645 struct sk_buff *skb = sk->sk_backlog.head;
1646
1647 do {
1648 sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
1649 bh_unlock_sock(sk);
1650
1651 do {
1652 struct sk_buff *next = skb->next;
1653
7fee226a 1654 WARN_ON_ONCE(skb_dst_is_noref(skb));
1da177e4 1655 skb->next = NULL;
c57943a1 1656 sk_backlog_rcv(sk, skb);
1da177e4
LT
1657
1658 /*
1659 * We are in process context here with softirqs
1660 * disabled, use cond_resched_softirq() to preempt.
1661 * This is safe to do because we've taken the backlog
1662 * queue private:
1663 */
1664 cond_resched_softirq();
1665
1666 skb = next;
1667 } while (skb != NULL);
1668
1669 bh_lock_sock(sk);
e71a4783 1670 } while ((skb = sk->sk_backlog.head) != NULL);
8eae939f
ZY
1671
1672 /*
1673 * Doing the zeroing here guarantee we can not loop forever
1674 * while a wild producer attempts to flood us.
1675 */
1676 sk->sk_backlog.len = 0;
1da177e4
LT
1677}
1678
1679/**
1680 * sk_wait_data - wait for data to arrive at sk_receive_queue
4dc3b16b
PP
1681 * @sk: sock to wait on
1682 * @timeo: for how long
1da177e4
LT
1683 *
1684 * Now socket state including sk->sk_err is changed only under lock,
1685 * hence we may omit checks after joining wait queue.
1686 * We check receive queue before schedule() only as optimization;
1687 * it is very likely that release_sock() added new data.
1688 */
1689int sk_wait_data(struct sock *sk, long *timeo)
1690{
1691 int rc;
1692 DEFINE_WAIT(wait);
1693
aa395145 1694 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1da177e4
LT
1695 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1696 rc = sk_wait_event(sk, timeo, !skb_queue_empty(&sk->sk_receive_queue));
1697 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
aa395145 1698 finish_wait(sk_sleep(sk), &wait);
1da177e4
LT
1699 return rc;
1700}
1da177e4
LT
1701EXPORT_SYMBOL(sk_wait_data);
1702
3ab224be
HA
1703/**
1704 * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
1705 * @sk: socket
1706 * @size: memory size to allocate
1707 * @kind: allocation type
1708 *
1709 * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
1710 * rmem allocation. This function assumes that protocols which have
1711 * memory_pressure use sk_wmem_queued as write buffer accounting.
1712 */
1713int __sk_mem_schedule(struct sock *sk, int size, int kind)
1714{
1715 struct proto *prot = sk->sk_prot;
1716 int amt = sk_mem_pages(size);
8d987e5c 1717 long allocated;
e1aab161 1718 int parent_status = UNDER_LIMIT;
3ab224be
HA
1719
1720 sk->sk_forward_alloc += amt * SK_MEM_QUANTUM;
180d8cd9 1721
e1aab161 1722 allocated = sk_memory_allocated_add(sk, amt, &parent_status);
3ab224be
HA
1723
1724 /* Under limit. */
e1aab161
GC
1725 if (parent_status == UNDER_LIMIT &&
1726 allocated <= sk_prot_mem_limits(sk, 0)) {
180d8cd9 1727 sk_leave_memory_pressure(sk);
3ab224be
HA
1728 return 1;
1729 }
1730
e1aab161
GC
1731 /* Under pressure. (we or our parents) */
1732 if ((parent_status > SOFT_LIMIT) ||
1733 allocated > sk_prot_mem_limits(sk, 1))
180d8cd9 1734 sk_enter_memory_pressure(sk);
3ab224be 1735
e1aab161
GC
1736 /* Over hard limit (we or our parents) */
1737 if ((parent_status == OVER_LIMIT) ||
1738 (allocated > sk_prot_mem_limits(sk, 2)))
3ab224be
HA
1739 goto suppress_allocation;
1740
1741 /* guarantee minimum buffer size under pressure */
1742 if (kind == SK_MEM_RECV) {
1743 if (atomic_read(&sk->sk_rmem_alloc) < prot->sysctl_rmem[0])
1744 return 1;
180d8cd9 1745
3ab224be
HA
1746 } else { /* SK_MEM_SEND */
1747 if (sk->sk_type == SOCK_STREAM) {
1748 if (sk->sk_wmem_queued < prot->sysctl_wmem[0])
1749 return 1;
1750 } else if (atomic_read(&sk->sk_wmem_alloc) <
1751 prot->sysctl_wmem[0])
1752 return 1;
1753 }
1754
180d8cd9 1755 if (sk_has_memory_pressure(sk)) {
1748376b
ED
1756 int alloc;
1757
180d8cd9 1758 if (!sk_under_memory_pressure(sk))
1748376b 1759 return 1;
180d8cd9
GC
1760 alloc = sk_sockets_allocated_read_positive(sk);
1761 if (sk_prot_mem_limits(sk, 2) > alloc *
3ab224be
HA
1762 sk_mem_pages(sk->sk_wmem_queued +
1763 atomic_read(&sk->sk_rmem_alloc) +
1764 sk->sk_forward_alloc))
1765 return 1;
1766 }
1767
1768suppress_allocation:
1769
1770 if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
1771 sk_stream_moderate_sndbuf(sk);
1772
1773 /* Fail only if socket is _under_ its sndbuf.
1774 * In this case we cannot block, so that we have to fail.
1775 */
1776 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
1777 return 1;
1778 }
1779
3847ce32
SM
1780 trace_sock_exceed_buf_limit(sk, prot, allocated);
1781
3ab224be
HA
1782 /* Alas. Undo changes. */
1783 sk->sk_forward_alloc -= amt * SK_MEM_QUANTUM;
180d8cd9 1784
e1aab161 1785 sk_memory_allocated_sub(sk, amt, parent_status);
180d8cd9 1786
3ab224be
HA
1787 return 0;
1788}
3ab224be
HA
1789EXPORT_SYMBOL(__sk_mem_schedule);
1790
1791/**
1792 * __sk_reclaim - reclaim memory_allocated
1793 * @sk: socket
1794 */
1795void __sk_mem_reclaim(struct sock *sk)
1796{
180d8cd9 1797 sk_memory_allocated_sub(sk,
e1aab161 1798 sk->sk_forward_alloc >> SK_MEM_QUANTUM_SHIFT, 0);
3ab224be
HA
1799 sk->sk_forward_alloc &= SK_MEM_QUANTUM - 1;
1800
180d8cd9
GC
1801 if (sk_under_memory_pressure(sk) &&
1802 (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
1803 sk_leave_memory_pressure(sk);
3ab224be 1804}
3ab224be
HA
1805EXPORT_SYMBOL(__sk_mem_reclaim);
1806
1807
1da177e4
LT
1808/*
1809 * Set of default routines for initialising struct proto_ops when
1810 * the protocol does not support a particular function. In certain
1811 * cases where it makes no sense for a protocol to have a "do nothing"
1812 * function, some default processing is provided.
1813 */
1814
1815int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
1816{
1817 return -EOPNOTSUPP;
1818}
2a91525c 1819EXPORT_SYMBOL(sock_no_bind);
1da177e4 1820
4ec93edb 1821int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
1da177e4
LT
1822 int len, int flags)
1823{
1824 return -EOPNOTSUPP;
1825}
2a91525c 1826EXPORT_SYMBOL(sock_no_connect);
1da177e4
LT
1827
1828int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
1829{
1830 return -EOPNOTSUPP;
1831}
2a91525c 1832EXPORT_SYMBOL(sock_no_socketpair);
1da177e4
LT
1833
1834int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
1835{
1836 return -EOPNOTSUPP;
1837}
2a91525c 1838EXPORT_SYMBOL(sock_no_accept);
1da177e4 1839
4ec93edb 1840int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
1da177e4
LT
1841 int *len, int peer)
1842{
1843 return -EOPNOTSUPP;
1844}
2a91525c 1845EXPORT_SYMBOL(sock_no_getname);
1da177e4 1846
2a91525c 1847unsigned int sock_no_poll(struct file *file, struct socket *sock, poll_table *pt)
1da177e4
LT
1848{
1849 return 0;
1850}
2a91525c 1851EXPORT_SYMBOL(sock_no_poll);
1da177e4
LT
1852
1853int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1854{
1855 return -EOPNOTSUPP;
1856}
2a91525c 1857EXPORT_SYMBOL(sock_no_ioctl);
1da177e4
LT
1858
1859int sock_no_listen(struct socket *sock, int backlog)
1860{
1861 return -EOPNOTSUPP;
1862}
2a91525c 1863EXPORT_SYMBOL(sock_no_listen);
1da177e4
LT
1864
1865int sock_no_shutdown(struct socket *sock, int how)
1866{
1867 return -EOPNOTSUPP;
1868}
2a91525c 1869EXPORT_SYMBOL(sock_no_shutdown);
1da177e4
LT
1870
1871int sock_no_setsockopt(struct socket *sock, int level, int optname,
b7058842 1872 char __user *optval, unsigned int optlen)
1da177e4
LT
1873{
1874 return -EOPNOTSUPP;
1875}
2a91525c 1876EXPORT_SYMBOL(sock_no_setsockopt);
1da177e4
LT
1877
1878int sock_no_getsockopt(struct socket *sock, int level, int optname,
1879 char __user *optval, int __user *optlen)
1880{
1881 return -EOPNOTSUPP;
1882}
2a91525c 1883EXPORT_SYMBOL(sock_no_getsockopt);
1da177e4
LT
1884
1885int sock_no_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
1886 size_t len)
1887{
1888 return -EOPNOTSUPP;
1889}
2a91525c 1890EXPORT_SYMBOL(sock_no_sendmsg);
1da177e4
LT
1891
1892int sock_no_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
1893 size_t len, int flags)
1894{
1895 return -EOPNOTSUPP;
1896}
2a91525c 1897EXPORT_SYMBOL(sock_no_recvmsg);
1da177e4
LT
1898
1899int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1900{
1901 /* Mirror missing mmap method error code */
1902 return -ENODEV;
1903}
2a91525c 1904EXPORT_SYMBOL(sock_no_mmap);
1da177e4
LT
1905
1906ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
1907{
1908 ssize_t res;
1909 struct msghdr msg = {.msg_flags = flags};
1910 struct kvec iov;
1911 char *kaddr = kmap(page);
1912 iov.iov_base = kaddr + offset;
1913 iov.iov_len = size;
1914 res = kernel_sendmsg(sock, &msg, &iov, 1, size);
1915 kunmap(page);
1916 return res;
1917}
2a91525c 1918EXPORT_SYMBOL(sock_no_sendpage);
1da177e4
LT
1919
1920/*
1921 * Default Socket Callbacks
1922 */
1923
1924static void sock_def_wakeup(struct sock *sk)
1925{
43815482
ED
1926 struct socket_wq *wq;
1927
1928 rcu_read_lock();
1929 wq = rcu_dereference(sk->sk_wq);
1930 if (wq_has_sleeper(wq))
1931 wake_up_interruptible_all(&wq->wait);
1932 rcu_read_unlock();
1da177e4
LT
1933}
1934
1935static void sock_def_error_report(struct sock *sk)
1936{
43815482
ED
1937 struct socket_wq *wq;
1938
1939 rcu_read_lock();
1940 wq = rcu_dereference(sk->sk_wq);
1941 if (wq_has_sleeper(wq))
1942 wake_up_interruptible_poll(&wq->wait, POLLERR);
8d8ad9d7 1943 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
43815482 1944 rcu_read_unlock();
1da177e4
LT
1945}
1946
1947static void sock_def_readable(struct sock *sk, int len)
1948{
43815482
ED
1949 struct socket_wq *wq;
1950
1951 rcu_read_lock();
1952 wq = rcu_dereference(sk->sk_wq);
1953 if (wq_has_sleeper(wq))
2c6607c6 1954 wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI |
37e5540b 1955 POLLRDNORM | POLLRDBAND);
8d8ad9d7 1956 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
43815482 1957 rcu_read_unlock();
1da177e4
LT
1958}
1959
1960static void sock_def_write_space(struct sock *sk)
1961{
43815482
ED
1962 struct socket_wq *wq;
1963
1964 rcu_read_lock();
1da177e4
LT
1965
1966 /* Do not wake up a writer until he can make "significant"
1967 * progress. --DaveM
1968 */
e71a4783 1969 if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
43815482
ED
1970 wq = rcu_dereference(sk->sk_wq);
1971 if (wq_has_sleeper(wq))
1972 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
37e5540b 1973 POLLWRNORM | POLLWRBAND);
1da177e4
LT
1974
1975 /* Should agree with poll, otherwise some programs break */
1976 if (sock_writeable(sk))
8d8ad9d7 1977 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
1da177e4
LT
1978 }
1979
43815482 1980 rcu_read_unlock();
1da177e4
LT
1981}
1982
1983static void sock_def_destruct(struct sock *sk)
1984{
a51482bd 1985 kfree(sk->sk_protinfo);
1da177e4
LT
1986}
1987
1988void sk_send_sigurg(struct sock *sk)
1989{
1990 if (sk->sk_socket && sk->sk_socket->file)
1991 if (send_sigurg(&sk->sk_socket->file->f_owner))
8d8ad9d7 1992 sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
1da177e4 1993}
2a91525c 1994EXPORT_SYMBOL(sk_send_sigurg);
1da177e4
LT
1995
1996void sk_reset_timer(struct sock *sk, struct timer_list* timer,
1997 unsigned long expires)
1998{
1999 if (!mod_timer(timer, expires))
2000 sock_hold(sk);
2001}
1da177e4
LT
2002EXPORT_SYMBOL(sk_reset_timer);
2003
2004void sk_stop_timer(struct sock *sk, struct timer_list* timer)
2005{
2006 if (timer_pending(timer) && del_timer(timer))
2007 __sock_put(sk);
2008}
1da177e4
LT
2009EXPORT_SYMBOL(sk_stop_timer);
2010
2011void sock_init_data(struct socket *sock, struct sock *sk)
2012{
2013 skb_queue_head_init(&sk->sk_receive_queue);
2014 skb_queue_head_init(&sk->sk_write_queue);
2015 skb_queue_head_init(&sk->sk_error_queue);
97fc2f08
CL
2016#ifdef CONFIG_NET_DMA
2017 skb_queue_head_init(&sk->sk_async_wait_queue);
2018#endif
1da177e4
LT
2019
2020 sk->sk_send_head = NULL;
2021
2022 init_timer(&sk->sk_timer);
4ec93edb 2023
1da177e4
LT
2024 sk->sk_allocation = GFP_KERNEL;
2025 sk->sk_rcvbuf = sysctl_rmem_default;
2026 sk->sk_sndbuf = sysctl_wmem_default;
2027 sk->sk_state = TCP_CLOSE;
972692e0 2028 sk_set_socket(sk, sock);
1da177e4
LT
2029
2030 sock_set_flag(sk, SOCK_ZAPPED);
2031
e71a4783 2032 if (sock) {
1da177e4 2033 sk->sk_type = sock->type;
43815482 2034 sk->sk_wq = sock->wq;
1da177e4
LT
2035 sock->sk = sk;
2036 } else
43815482 2037 sk->sk_wq = NULL;
1da177e4 2038
b6c6712a 2039 spin_lock_init(&sk->sk_dst_lock);
1da177e4 2040 rwlock_init(&sk->sk_callback_lock);
443aef0e
PZ
2041 lockdep_set_class_and_name(&sk->sk_callback_lock,
2042 af_callback_keys + sk->sk_family,
2043 af_family_clock_key_strings[sk->sk_family]);
1da177e4
LT
2044
2045 sk->sk_state_change = sock_def_wakeup;
2046 sk->sk_data_ready = sock_def_readable;
2047 sk->sk_write_space = sock_def_write_space;
2048 sk->sk_error_report = sock_def_error_report;
2049 sk->sk_destruct = sock_def_destruct;
2050
2051 sk->sk_sndmsg_page = NULL;
2052 sk->sk_sndmsg_off = 0;
2053
109f6e39
EB
2054 sk->sk_peer_pid = NULL;
2055 sk->sk_peer_cred = NULL;
1da177e4
LT
2056 sk->sk_write_pending = 0;
2057 sk->sk_rcvlowat = 1;
2058 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
2059 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
2060
f37f0afb 2061 sk->sk_stamp = ktime_set(-1L, 0);
1da177e4 2062
4dc6dc71
ED
2063 /*
2064 * Before updating sk_refcnt, we must commit prior changes to memory
2065 * (Documentation/RCU/rculist_nulls.txt for details)
2066 */
2067 smp_wmb();
1da177e4 2068 atomic_set(&sk->sk_refcnt, 1);
33c732c3 2069 atomic_set(&sk->sk_drops, 0);
1da177e4 2070}
2a91525c 2071EXPORT_SYMBOL(sock_init_data);
1da177e4 2072
b5606c2d 2073void lock_sock_nested(struct sock *sk, int subclass)
1da177e4
LT
2074{
2075 might_sleep();
a5b5bb9a 2076 spin_lock_bh(&sk->sk_lock.slock);
d2e9117c 2077 if (sk->sk_lock.owned)
1da177e4 2078 __lock_sock(sk);
d2e9117c 2079 sk->sk_lock.owned = 1;
a5b5bb9a
IM
2080 spin_unlock(&sk->sk_lock.slock);
2081 /*
2082 * The sk_lock has mutex_lock() semantics here:
2083 */
fcc70d5f 2084 mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
a5b5bb9a 2085 local_bh_enable();
1da177e4 2086}
fcc70d5f 2087EXPORT_SYMBOL(lock_sock_nested);
1da177e4 2088
b5606c2d 2089void release_sock(struct sock *sk)
1da177e4 2090{
a5b5bb9a
IM
2091 /*
2092 * The sk_lock has mutex_unlock() semantics:
2093 */
2094 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
2095
2096 spin_lock_bh(&sk->sk_lock.slock);
1da177e4
LT
2097 if (sk->sk_backlog.tail)
2098 __release_sock(sk);
d2e9117c 2099 sk->sk_lock.owned = 0;
a5b5bb9a
IM
2100 if (waitqueue_active(&sk->sk_lock.wq))
2101 wake_up(&sk->sk_lock.wq);
2102 spin_unlock_bh(&sk->sk_lock.slock);
1da177e4
LT
2103}
2104EXPORT_SYMBOL(release_sock);
2105
8a74ad60
ED
2106/**
2107 * lock_sock_fast - fast version of lock_sock
2108 * @sk: socket
2109 *
2110 * This version should be used for very small section, where process wont block
2111 * return false if fast path is taken
2112 * sk_lock.slock locked, owned = 0, BH disabled
2113 * return true if slow path is taken
2114 * sk_lock.slock unlocked, owned = 1, BH enabled
2115 */
2116bool lock_sock_fast(struct sock *sk)
2117{
2118 might_sleep();
2119 spin_lock_bh(&sk->sk_lock.slock);
2120
2121 if (!sk->sk_lock.owned)
2122 /*
2123 * Note : We must disable BH
2124 */
2125 return false;
2126
2127 __lock_sock(sk);
2128 sk->sk_lock.owned = 1;
2129 spin_unlock(&sk->sk_lock.slock);
2130 /*
2131 * The sk_lock has mutex_lock() semantics here:
2132 */
2133 mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
2134 local_bh_enable();
2135 return true;
2136}
2137EXPORT_SYMBOL(lock_sock_fast);
2138
1da177e4 2139int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
4ec93edb 2140{
b7aa0bf7 2141 struct timeval tv;
1da177e4 2142 if (!sock_flag(sk, SOCK_TIMESTAMP))
20d49473 2143 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
b7aa0bf7
ED
2144 tv = ktime_to_timeval(sk->sk_stamp);
2145 if (tv.tv_sec == -1)
1da177e4 2146 return -ENOENT;
b7aa0bf7
ED
2147 if (tv.tv_sec == 0) {
2148 sk->sk_stamp = ktime_get_real();
2149 tv = ktime_to_timeval(sk->sk_stamp);
2150 }
2151 return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
4ec93edb 2152}
1da177e4
LT
2153EXPORT_SYMBOL(sock_get_timestamp);
2154
ae40eb1e
ED
2155int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
2156{
2157 struct timespec ts;
2158 if (!sock_flag(sk, SOCK_TIMESTAMP))
20d49473 2159 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
ae40eb1e
ED
2160 ts = ktime_to_timespec(sk->sk_stamp);
2161 if (ts.tv_sec == -1)
2162 return -ENOENT;
2163 if (ts.tv_sec == 0) {
2164 sk->sk_stamp = ktime_get_real();
2165 ts = ktime_to_timespec(sk->sk_stamp);
2166 }
2167 return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
2168}
2169EXPORT_SYMBOL(sock_get_timestampns);
2170
20d49473 2171void sock_enable_timestamp(struct sock *sk, int flag)
4ec93edb 2172{
20d49473 2173 if (!sock_flag(sk, flag)) {
08e29af3
ED
2174 unsigned long previous_flags = sk->sk_flags;
2175
20d49473
PO
2176 sock_set_flag(sk, flag);
2177 /*
2178 * we just set one of the two flags which require net
2179 * time stamping, but time stamping might have been on
2180 * already because of the other one
2181 */
08e29af3 2182 if (!(previous_flags & SK_FLAGS_TIMESTAMP))
20d49473 2183 net_enable_timestamp();
1da177e4
LT
2184 }
2185}
1da177e4
LT
2186
2187/*
2188 * Get a socket option on an socket.
2189 *
2190 * FIX: POSIX 1003.1g is very ambiguous here. It states that
2191 * asynchronous errors should be reported by getsockopt. We assume
2192 * this means if you specify SO_ERROR (otherwise whats the point of it).
2193 */
2194int sock_common_getsockopt(struct socket *sock, int level, int optname,
2195 char __user *optval, int __user *optlen)
2196{
2197 struct sock *sk = sock->sk;
2198
2199 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2200}
1da177e4
LT
2201EXPORT_SYMBOL(sock_common_getsockopt);
2202
3fdadf7d 2203#ifdef CONFIG_COMPAT
543d9cfe
ACM
2204int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
2205 char __user *optval, int __user *optlen)
3fdadf7d
DM
2206{
2207 struct sock *sk = sock->sk;
2208
1e51f951 2209 if (sk->sk_prot->compat_getsockopt != NULL)
543d9cfe
ACM
2210 return sk->sk_prot->compat_getsockopt(sk, level, optname,
2211 optval, optlen);
3fdadf7d
DM
2212 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
2213}
2214EXPORT_SYMBOL(compat_sock_common_getsockopt);
2215#endif
2216
1da177e4
LT
2217int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
2218 struct msghdr *msg, size_t size, int flags)
2219{
2220 struct sock *sk = sock->sk;
2221 int addr_len = 0;
2222 int err;
2223
2224 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
2225 flags & ~MSG_DONTWAIT, &addr_len);
2226 if (err >= 0)
2227 msg->msg_namelen = addr_len;
2228 return err;
2229}
1da177e4
LT
2230EXPORT_SYMBOL(sock_common_recvmsg);
2231
2232/*
2233 * Set socket options on an inet socket.
2234 */
2235int sock_common_setsockopt(struct socket *sock, int level, int optname,
b7058842 2236 char __user *optval, unsigned int optlen)
1da177e4
LT
2237{
2238 struct sock *sk = sock->sk;
2239
2240 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2241}
1da177e4
LT
2242EXPORT_SYMBOL(sock_common_setsockopt);
2243
3fdadf7d 2244#ifdef CONFIG_COMPAT
543d9cfe 2245int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
b7058842 2246 char __user *optval, unsigned int optlen)
3fdadf7d
DM
2247{
2248 struct sock *sk = sock->sk;
2249
543d9cfe
ACM
2250 if (sk->sk_prot->compat_setsockopt != NULL)
2251 return sk->sk_prot->compat_setsockopt(sk, level, optname,
2252 optval, optlen);
3fdadf7d
DM
2253 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
2254}
2255EXPORT_SYMBOL(compat_sock_common_setsockopt);
2256#endif
2257
1da177e4
LT
2258void sk_common_release(struct sock *sk)
2259{
2260 if (sk->sk_prot->destroy)
2261 sk->sk_prot->destroy(sk);
2262
2263 /*
2264 * Observation: when sock_common_release is called, processes have
2265 * no access to socket. But net still has.
2266 * Step one, detach it from networking:
2267 *
2268 * A. Remove from hash tables.
2269 */
2270
2271 sk->sk_prot->unhash(sk);
2272
2273 /*
2274 * In this point socket cannot receive new packets, but it is possible
2275 * that some packets are in flight because some CPU runs receiver and
2276 * did hash table lookup before we unhashed socket. They will achieve
2277 * receive queue and will be purged by socket destructor.
2278 *
2279 * Also we still have packets pending on receive queue and probably,
2280 * our own packets waiting in device queues. sock_destroy will drain
2281 * receive queue, but transmitted packets will delay socket destruction
2282 * until the last reference will be released.
2283 */
2284
2285 sock_orphan(sk);
2286
2287 xfrm_sk_free_policy(sk);
2288
e6848976 2289 sk_refcnt_debug_release(sk);
1da177e4
LT
2290 sock_put(sk);
2291}
1da177e4
LT
2292EXPORT_SYMBOL(sk_common_release);
2293
2294static DEFINE_RWLOCK(proto_list_lock);
2295static LIST_HEAD(proto_list);
2296
13ff3d6f
PE
2297#ifdef CONFIG_PROC_FS
2298#define PROTO_INUSE_NR 64 /* should be enough for the first time */
1338d466
PE
2299struct prot_inuse {
2300 int val[PROTO_INUSE_NR];
2301};
13ff3d6f
PE
2302
2303static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
70ee1159
PE
2304
2305#ifdef CONFIG_NET_NS
2306void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
2307{
d6d9ca0f 2308 __this_cpu_add(net->core.inuse->val[prot->inuse_idx], val);
70ee1159
PE
2309}
2310EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
2311
2312int sock_prot_inuse_get(struct net *net, struct proto *prot)
2313{
2314 int cpu, idx = prot->inuse_idx;
2315 int res = 0;
2316
2317 for_each_possible_cpu(cpu)
2318 res += per_cpu_ptr(net->core.inuse, cpu)->val[idx];
2319
2320 return res >= 0 ? res : 0;
2321}
2322EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
2323
2c8c1e72 2324static int __net_init sock_inuse_init_net(struct net *net)
70ee1159
PE
2325{
2326 net->core.inuse = alloc_percpu(struct prot_inuse);
2327 return net->core.inuse ? 0 : -ENOMEM;
2328}
2329
2c8c1e72 2330static void __net_exit sock_inuse_exit_net(struct net *net)
70ee1159
PE
2331{
2332 free_percpu(net->core.inuse);
2333}
2334
2335static struct pernet_operations net_inuse_ops = {
2336 .init = sock_inuse_init_net,
2337 .exit = sock_inuse_exit_net,
2338};
2339
2340static __init int net_inuse_init(void)
2341{
2342 if (register_pernet_subsys(&net_inuse_ops))
2343 panic("Cannot initialize net inuse counters");
2344
2345 return 0;
2346}
2347
2348core_initcall(net_inuse_init);
2349#else
1338d466
PE
2350static DEFINE_PER_CPU(struct prot_inuse, prot_inuse);
2351
c29a0bc4 2352void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
1338d466 2353{
d6d9ca0f 2354 __this_cpu_add(prot_inuse.val[prot->inuse_idx], val);
1338d466
PE
2355}
2356EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
2357
c29a0bc4 2358int sock_prot_inuse_get(struct net *net, struct proto *prot)
1338d466
PE
2359{
2360 int cpu, idx = prot->inuse_idx;
2361 int res = 0;
2362
2363 for_each_possible_cpu(cpu)
2364 res += per_cpu(prot_inuse, cpu).val[idx];
2365
2366 return res >= 0 ? res : 0;
2367}
2368EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
70ee1159 2369#endif
13ff3d6f
PE
2370
2371static void assign_proto_idx(struct proto *prot)
2372{
2373 prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
2374
2375 if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
2376 printk(KERN_ERR "PROTO_INUSE_NR exhausted\n");
2377 return;
2378 }
2379
2380 set_bit(prot->inuse_idx, proto_inuse_idx);
2381}
2382
2383static void release_proto_idx(struct proto *prot)
2384{
2385 if (prot->inuse_idx != PROTO_INUSE_NR - 1)
2386 clear_bit(prot->inuse_idx, proto_inuse_idx);
2387}
2388#else
2389static inline void assign_proto_idx(struct proto *prot)
2390{
2391}
2392
2393static inline void release_proto_idx(struct proto *prot)
2394{
2395}
2396#endif
2397
b733c007
PE
2398int proto_register(struct proto *prot, int alloc_slab)
2399{
1da177e4
LT
2400 if (alloc_slab) {
2401 prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
271b72c7
ED
2402 SLAB_HWCACHE_ALIGN | prot->slab_flags,
2403 NULL);
1da177e4
LT
2404
2405 if (prot->slab == NULL) {
2406 printk(KERN_CRIT "%s: Can't create sock SLAB cache!\n",
2407 prot->name);
60e7663d 2408 goto out;
1da177e4 2409 }
2e6599cb
ACM
2410
2411 if (prot->rsk_prot != NULL) {
faf23422 2412 prot->rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s", prot->name);
7e56b5d6 2413 if (prot->rsk_prot->slab_name == NULL)
2e6599cb
ACM
2414 goto out_free_sock_slab;
2415
7e56b5d6 2416 prot->rsk_prot->slab = kmem_cache_create(prot->rsk_prot->slab_name,
2e6599cb 2417 prot->rsk_prot->obj_size, 0,
20c2df83 2418 SLAB_HWCACHE_ALIGN, NULL);
2e6599cb
ACM
2419
2420 if (prot->rsk_prot->slab == NULL) {
2421 printk(KERN_CRIT "%s: Can't create request sock SLAB cache!\n",
2422 prot->name);
2423 goto out_free_request_sock_slab_name;
2424 }
2425 }
8feaf0c0 2426
6d6ee43e 2427 if (prot->twsk_prot != NULL) {
faf23422 2428 prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
8feaf0c0 2429
7e56b5d6 2430 if (prot->twsk_prot->twsk_slab_name == NULL)
8feaf0c0
ACM
2431 goto out_free_request_sock_slab;
2432
6d6ee43e 2433 prot->twsk_prot->twsk_slab =
7e56b5d6 2434 kmem_cache_create(prot->twsk_prot->twsk_slab_name,
6d6ee43e 2435 prot->twsk_prot->twsk_obj_size,
3ab5aee7
ED
2436 0,
2437 SLAB_HWCACHE_ALIGN |
2438 prot->slab_flags,
20c2df83 2439 NULL);
6d6ee43e 2440 if (prot->twsk_prot->twsk_slab == NULL)
8feaf0c0
ACM
2441 goto out_free_timewait_sock_slab_name;
2442 }
1da177e4
LT
2443 }
2444
2a278051 2445 write_lock(&proto_list_lock);
1da177e4 2446 list_add(&prot->node, &proto_list);
13ff3d6f 2447 assign_proto_idx(prot);
1da177e4 2448 write_unlock(&proto_list_lock);
b733c007
PE
2449 return 0;
2450
8feaf0c0 2451out_free_timewait_sock_slab_name:
7e56b5d6 2452 kfree(prot->twsk_prot->twsk_slab_name);
8feaf0c0
ACM
2453out_free_request_sock_slab:
2454 if (prot->rsk_prot && prot->rsk_prot->slab) {
2455 kmem_cache_destroy(prot->rsk_prot->slab);
2456 prot->rsk_prot->slab = NULL;
2457 }
2e6599cb 2458out_free_request_sock_slab_name:
72150e9b
DC
2459 if (prot->rsk_prot)
2460 kfree(prot->rsk_prot->slab_name);
2e6599cb
ACM
2461out_free_sock_slab:
2462 kmem_cache_destroy(prot->slab);
2463 prot->slab = NULL;
b733c007
PE
2464out:
2465 return -ENOBUFS;
1da177e4 2466}
1da177e4
LT
2467EXPORT_SYMBOL(proto_register);
2468
2469void proto_unregister(struct proto *prot)
2470{
2471 write_lock(&proto_list_lock);
13ff3d6f 2472 release_proto_idx(prot);
0a3f4358
PM
2473 list_del(&prot->node);
2474 write_unlock(&proto_list_lock);
1da177e4
LT
2475
2476 if (prot->slab != NULL) {
2477 kmem_cache_destroy(prot->slab);
2478 prot->slab = NULL;
2479 }
2480
2e6599cb 2481 if (prot->rsk_prot != NULL && prot->rsk_prot->slab != NULL) {
2e6599cb 2482 kmem_cache_destroy(prot->rsk_prot->slab);
7e56b5d6 2483 kfree(prot->rsk_prot->slab_name);
2e6599cb
ACM
2484 prot->rsk_prot->slab = NULL;
2485 }
2486
6d6ee43e 2487 if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
6d6ee43e 2488 kmem_cache_destroy(prot->twsk_prot->twsk_slab);
7e56b5d6 2489 kfree(prot->twsk_prot->twsk_slab_name);
6d6ee43e 2490 prot->twsk_prot->twsk_slab = NULL;
8feaf0c0 2491 }
1da177e4 2492}
1da177e4
LT
2493EXPORT_SYMBOL(proto_unregister);
2494
2495#ifdef CONFIG_PROC_FS
1da177e4 2496static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
9a429c49 2497 __acquires(proto_list_lock)
1da177e4
LT
2498{
2499 read_lock(&proto_list_lock);
60f0438a 2500 return seq_list_start_head(&proto_list, *pos);
1da177e4
LT
2501}
2502
2503static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2504{
60f0438a 2505 return seq_list_next(v, &proto_list, pos);
1da177e4
LT
2506}
2507
2508static void proto_seq_stop(struct seq_file *seq, void *v)
9a429c49 2509 __releases(proto_list_lock)
1da177e4
LT
2510{
2511 read_unlock(&proto_list_lock);
2512}
2513
2514static char proto_method_implemented(const void *method)
2515{
2516 return method == NULL ? 'n' : 'y';
2517}
180d8cd9
GC
2518static long sock_prot_memory_allocated(struct proto *proto)
2519{
2520 return proto->memory_allocated != NULL ? proto_memory_allocated(proto): -1L;
2521}
2522
2523static char *sock_prot_memory_pressure(struct proto *proto)
2524{
2525 return proto->memory_pressure != NULL ?
2526 proto_memory_pressure(proto) ? "yes" : "no" : "NI";
2527}
1da177e4
LT
2528
2529static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
2530{
180d8cd9 2531
8d987e5c 2532 seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
1da177e4
LT
2533 "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
2534 proto->name,
2535 proto->obj_size,
14e943db 2536 sock_prot_inuse_get(seq_file_net(seq), proto),
180d8cd9
GC
2537 sock_prot_memory_allocated(proto),
2538 sock_prot_memory_pressure(proto),
1da177e4
LT
2539 proto->max_header,
2540 proto->slab == NULL ? "no" : "yes",
2541 module_name(proto->owner),
2542 proto_method_implemented(proto->close),
2543 proto_method_implemented(proto->connect),
2544 proto_method_implemented(proto->disconnect),
2545 proto_method_implemented(proto->accept),
2546 proto_method_implemented(proto->ioctl),
2547 proto_method_implemented(proto->init),
2548 proto_method_implemented(proto->destroy),
2549 proto_method_implemented(proto->shutdown),
2550 proto_method_implemented(proto->setsockopt),
2551 proto_method_implemented(proto->getsockopt),
2552 proto_method_implemented(proto->sendmsg),
2553 proto_method_implemented(proto->recvmsg),
2554 proto_method_implemented(proto->sendpage),
2555 proto_method_implemented(proto->bind),
2556 proto_method_implemented(proto->backlog_rcv),
2557 proto_method_implemented(proto->hash),
2558 proto_method_implemented(proto->unhash),
2559 proto_method_implemented(proto->get_port),
2560 proto_method_implemented(proto->enter_memory_pressure));
2561}
2562
2563static int proto_seq_show(struct seq_file *seq, void *v)
2564{
60f0438a 2565 if (v == &proto_list)
1da177e4
LT
2566 seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
2567 "protocol",
2568 "size",
2569 "sockets",
2570 "memory",
2571 "press",
2572 "maxhdr",
2573 "slab",
2574 "module",
2575 "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
2576 else
60f0438a 2577 proto_seq_printf(seq, list_entry(v, struct proto, node));
1da177e4
LT
2578 return 0;
2579}
2580
f690808e 2581static const struct seq_operations proto_seq_ops = {
1da177e4
LT
2582 .start = proto_seq_start,
2583 .next = proto_seq_next,
2584 .stop = proto_seq_stop,
2585 .show = proto_seq_show,
2586};
2587
2588static int proto_seq_open(struct inode *inode, struct file *file)
2589{
14e943db
ED
2590 return seq_open_net(inode, file, &proto_seq_ops,
2591 sizeof(struct seq_net_private));
1da177e4
LT
2592}
2593
9a32144e 2594static const struct file_operations proto_seq_fops = {
1da177e4
LT
2595 .owner = THIS_MODULE,
2596 .open = proto_seq_open,
2597 .read = seq_read,
2598 .llseek = seq_lseek,
14e943db
ED
2599 .release = seq_release_net,
2600};
2601
2602static __net_init int proto_init_net(struct net *net)
2603{
2604 if (!proc_net_fops_create(net, "protocols", S_IRUGO, &proto_seq_fops))
2605 return -ENOMEM;
2606
2607 return 0;
2608}
2609
2610static __net_exit void proto_exit_net(struct net *net)
2611{
2612 proc_net_remove(net, "protocols");
2613}
2614
2615
2616static __net_initdata struct pernet_operations proto_net_ops = {
2617 .init = proto_init_net,
2618 .exit = proto_exit_net,
1da177e4
LT
2619};
2620
2621static int __init proto_init(void)
2622{
14e943db 2623 return register_pernet_subsys(&proto_net_ops);
1da177e4
LT
2624}
2625
2626subsys_initcall(proto_init);
2627
2628#endif /* PROC_FS */
This page took 0.77498 seconds and 5 git commands to generate.