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