44 #include <linux/kernel.h>
45 #include <linux/list.h>
49 #include <linux/bitops.h>
51 #include <linux/netdevice.h>
55 #include <linux/slab.h>
61 #include <linux/sched.h>
63 #include <linux/filter.h>
65 #include <linux/poll.h>
74 int mem_cgroup_sockets_init(
struct mem_cgroup *memcg,
struct cgroup_subsys *
ss);
75 void mem_cgroup_sockets_destroy(
struct mem_cgroup *memcg);
78 int mem_cgroup_sockets_init(
struct mem_cgroup *memcg,
struct cgroup_subsys *
ss)
83 void mem_cgroup_sockets_destroy(
struct mem_cgroup *memcg)
94 #define SOCK_DEBUGGING
96 #define SOCK_DEBUG(sk, msg...) do { if ((sk) && sock_flag((sk), SOCK_DBG)) \
97 printk(KERN_DEBUG msg); } while (0)
120 #ifdef CONFIG_DEBUG_LOCK_ALLOC
121 struct lockdep_map dep_map;
271 #define sk_node __sk_common.skc_node
272 #define sk_nulls_node __sk_common.skc_nulls_node
273 #define sk_refcnt __sk_common.skc_refcnt
274 #define sk_tx_queue_mapping __sk_common.skc_tx_queue_mapping
276 #define sk_dontcopy_begin __sk_common.skc_dontcopy_begin
277 #define sk_dontcopy_end __sk_common.skc_dontcopy_end
278 #define sk_hash __sk_common.skc_hash
279 #define sk_family __sk_common.skc_family
280 #define sk_state __sk_common.skc_state
281 #define sk_reuse __sk_common.skc_reuse
282 #define sk_bound_dev_if __sk_common.skc_bound_dev_if
283 #define sk_bind_node __sk_common.skc_bind_node
284 #define sk_prot __sk_common.skc_prot
285 #define sk_net __sk_common.skc_net
302 #define sk_rmem_alloc sk_backlog.rmem_alloc
313 #ifdef CONFIG_NET_DMA
352 #ifdef CONFIG_CGROUPS
353 __u32 sk_cgrp_prioidx;
368 #ifdef CONFIG_SECURITY
390 #define SK_NO_REUSE 0
391 #define SK_CAN_REUSE 1
392 #define SK_FORCE_REUSE 2
394 static inline int sk_peek_offset(
struct sock *
sk,
int flags)
402 static inline void sk_peek_offset_bwd(
struct sock *
sk,
int val)
412 static inline void sk_peek_offset_fwd(
struct sock *sk,
int val)
433 return hlist_empty(head) ?
NULL : __sk_head(head);
443 return hlist_nulls_empty(head) ?
NULL : __sk_nulls_head(head);
446 static inline struct sock *sk_next(
const struct sock *sk)
448 return sk->sk_node.next ?
452 static inline struct sock *sk_nulls_next(
const struct sock *sk)
454 return (!is_a_nulls(sk->sk_nulls_node.next)) ?
460 static inline bool sk_unhashed(
const struct sock *sk)
462 return hlist_unhashed(&sk->sk_node);
465 static inline bool sk_hashed(
const struct sock *sk)
467 return !sk_unhashed(sk);
480 static inline void __sk_del_node(
struct sock *sk)
482 __hlist_del(&sk->sk_node);
486 static inline bool __sk_del_node_init(
struct sock *sk)
490 sk_node_init(&sk->sk_node);
502 static inline void sock_hold(
struct sock *sk)
510 static inline void __sock_put(
struct sock *sk)
515 static inline bool sk_del_node_init(
struct sock *sk)
517 bool rc = __sk_del_node_init(sk);
526 #define sk_del_node_init_rcu(sk) sk_del_node_init(sk)
528 static inline bool __sk_nulls_del_node_init_rcu(
struct sock *sk)
531 hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
537 static inline bool sk_nulls_del_node_init_rcu(
struct sock *sk)
539 bool rc = __sk_nulls_del_node_init_rcu(sk);
551 hlist_add_head(&sk->sk_node, list);
557 __sk_add_node(sk, list);
563 hlist_add_head_rcu(&sk->sk_node, list);
568 hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
574 __sk_nulls_add_node_rcu(sk, list);
577 static inline void __sk_del_bind_node(
struct sock *sk)
579 __hlist_del(&sk->sk_bind_node);
582 static inline void sk_add_bind_node(
struct sock *sk,
585 hlist_add_head(&sk->sk_bind_node, list);
588 #define sk_for_each(__sk, node, list) \
589 hlist_for_each_entry(__sk, node, list, sk_node)
590 #define sk_for_each_rcu(__sk, node, list) \
591 hlist_for_each_entry_rcu(__sk, node, list, sk_node)
592 #define sk_nulls_for_each(__sk, node, list) \
593 hlist_nulls_for_each_entry(__sk, node, list, sk_nulls_node)
594 #define sk_nulls_for_each_rcu(__sk, node, list) \
595 hlist_nulls_for_each_entry_rcu(__sk, node, list, sk_nulls_node)
596 #define sk_for_each_from(__sk, node) \
597 if (__sk && ({ node = &(__sk)->sk_node; 1; })) \
598 hlist_for_each_entry_from(__sk, node, sk_node)
599 #define sk_nulls_for_each_from(__sk, node) \
600 if (__sk && ({ node = &(__sk)->sk_nulls_node; 1; })) \
601 hlist_nulls_for_each_entry_from(__sk, node, sk_nulls_node)
602 #define sk_for_each_safe(__sk, node, tmp, list) \
603 hlist_for_each_entry_safe(__sk, node, tmp, list, sk_node)
604 #define sk_for_each_bound(__sk, node, list) \
605 hlist_for_each_entry(__sk, node, list, sk_bind_node)
613 return sk->
sk_socket->file->f_cred->user_ns;
651 static inline void sock_copy_flags(
struct sock *nsk,
struct sock *osk)
673 static inline int sk_memalloc_socks(
void)
679 static inline int sk_memalloc_socks(
void)
691 static inline void sk_acceptq_removed(
struct sock *sk)
696 static inline void sk_acceptq_added(
struct sock *sk)
701 static inline bool sk_acceptq_is_full(
const struct sock *sk)
709 static inline int sk_stream_min_wspace(
const struct sock *sk)
714 static inline int sk_stream_wspace(
const struct sock *sk)
721 static inline bool sk_stream_memory_free(
const struct sock *sk)
727 static inline void __sk_add_backlog(
struct sock *sk,
struct sk_buff *
skb)
746 static inline bool sk_rcvqueues_full(
const struct sock *sk,
const struct sk_buff *skb,
751 return qsize >
limit;
758 if (sk_rcvqueues_full(sk, skb, limit))
761 __sk_add_backlog(sk, skb);
768 static inline int sk_backlog_rcv(
struct sock *sk,
struct sk_buff *skb)
770 if (sk_memalloc_socks() && skb_pfmemalloc(skb))
776 static inline void sock_rps_record_flow(
const struct sock *sk)
779 struct rps_sock_flow_table *sock_flow_table;
783 rps_record_sock_flow(sock_flow_table, sk->sk_rxhash);
788 static inline void sock_rps_reset_flow(
const struct sock *sk)
791 struct rps_sock_flow_table *sock_flow_table;
795 rps_reset_sock_flow(sock_flow_table, sk->sk_rxhash);
800 static inline void sock_rps_save_rxhash(
struct sock *sk,
805 sock_rps_reset_flow(sk);
806 sk->sk_rxhash = skb->
rxhash;
811 static inline void sock_rps_reset_rxhash(
struct sock *sk)
814 sock_rps_reset_flow(sk);
819 #define sk_wait_event(__sk, __timeo, __condition) \
821 release_sock(__sk); \
822 __rc = __condition; \
824 *(__timeo) = schedule_timeout(*(__timeo)); \
827 __rc = __condition; \
867 int optname,
char __user *optval,
868 unsigned int optlen);
870 int optname,
char __user *optval,
873 int (*compat_setsockopt)(
struct sock *
sk,
875 int optname,
char __user *optval,
876 unsigned int optlen);
877 int (*compat_getsockopt)(
struct sock *
sk,
879 int optname,
char __user *optval,
882 unsigned int cmd,
unsigned long arg);
909 #ifdef CONFIG_PROC_FS
910 unsigned int inuse_idx;
950 #ifdef SOCK_REFCNT_DEBUG
953 #ifdef CONFIG_MEMCG_KMEM
961 struct cgroup_subsys *
ss);
1009 #ifdef SOCK_REFCNT_DEBUG
1019 sk->sk_prot->name, sk,
atomic_read(&sk->sk_prot->socks));
1026 sk->sk_prot->name, sk,
atomic_read(&sk->sk_refcnt));
1029 #define sk_refcnt_debug_inc(sk) do { } while (0)
1030 #define sk_refcnt_debug_dec(sk) do { } while (0)
1031 #define sk_refcnt_debug_release(sk) do { } while (0)
1034 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_NET)
1041 #define mem_cgroup_sockets_enabled static_key_false(&memcg_socket_limit_enabled)
1043 #define mem_cgroup_sockets_enabled 0
1044 static inline struct cg_proto *parent_cg_proto(
struct proto *proto,
1045 struct cg_proto *cg_proto)
1052 static inline bool sk_has_memory_pressure(
const struct sock *sk)
1054 return sk->sk_prot->memory_pressure !=
NULL;
1057 static inline bool sk_under_memory_pressure(
const struct sock *sk)
1059 if (!sk->sk_prot->memory_pressure)
1063 return !!*sk->
sk_cgrp->memory_pressure;
1065 return !!*sk->sk_prot->memory_pressure;
1068 static inline void sk_leave_memory_pressure(
struct sock *sk)
1072 if (!memory_pressure)
1075 if (*memory_pressure)
1076 *memory_pressure = 0;
1079 struct cg_proto *cg_proto = sk->
sk_cgrp;
1080 struct proto *prot = sk->sk_prot;
1082 for (; cg_proto; cg_proto = parent_cg_proto(prot, cg_proto))
1089 static inline void sk_enter_memory_pressure(
struct sock *sk)
1091 if (!sk->sk_prot->enter_memory_pressure)
1095 struct cg_proto *cg_proto = sk->
sk_cgrp;
1096 struct proto *prot = sk->sk_prot;
1098 for (; cg_proto; cg_proto = parent_cg_proto(prot, cg_proto))
1102 sk->sk_prot->enter_memory_pressure(sk);
1105 static inline long sk_prot_mem_limits(
const struct sock *sk,
int index)
1107 long *prot = sk->sk_prot->sysctl_mem;
1109 prot = sk->
sk_cgrp->sysctl_mem;
1113 static inline void memcg_memory_allocated_add(
struct cg_proto *prot,
1126 static inline void memcg_memory_allocated_sub(
struct cg_proto *prot,
1132 static inline u64 memcg_memory_allocated_read(
struct cg_proto *prot)
1140 sk_memory_allocated(
const struct sock *sk)
1142 struct proto *prot = sk->sk_prot;
1144 return memcg_memory_allocated_read(sk->
sk_cgrp);
1150 sk_memory_allocated_add(
struct sock *sk,
int amt,
int *parent_status)
1152 struct proto *prot = sk->sk_prot;
1155 memcg_memory_allocated_add(sk->
sk_cgrp, amt, parent_status);
1158 return memcg_memory_allocated_read(sk->
sk_cgrp);
1165 sk_memory_allocated_sub(
struct sock *sk,
int amt)
1167 struct proto *prot = sk->sk_prot;
1170 memcg_memory_allocated_sub(sk->
sk_cgrp, amt);
1175 static inline void sk_sockets_allocated_dec(
struct sock *sk)
1177 struct proto *prot = sk->sk_prot;
1180 struct cg_proto *cg_proto = sk->
sk_cgrp;
1182 for (; cg_proto; cg_proto = parent_cg_proto(prot, cg_proto))
1189 static inline void sk_sockets_allocated_inc(
struct sock *sk)
1191 struct proto *prot = sk->sk_prot;
1194 struct cg_proto *cg_proto = sk->
sk_cgrp;
1196 for (; cg_proto; cg_proto = parent_cg_proto(prot, cg_proto))
1204 sk_sockets_allocated_read_positive(
struct sock *sk)
1206 struct proto *prot = sk->sk_prot;
1209 return percpu_counter_read_positive(sk->
sk_cgrp->sockets_allocated);
1215 proto_sockets_allocated_sum_positive(
struct proto *prot)
1221 proto_memory_allocated(
struct proto *prot)
1227 proto_memory_pressure(
struct proto *prot)
1235 #ifdef CONFIG_PROC_FS
1237 extern void sock_prot_inuse_add(
struct net *
net,
struct proto *prot,
int inc);
1238 extern int sock_prot_inuse_get(
struct net *
net,
struct proto *proto);
1240 static inline void sock_prot_inuse_add(
struct net *
net,
struct proto *prot,
1250 static inline void __sk_prot_rehash(
struct sock *sk)
1252 sk->sk_prot->unhash(sk);
1253 sk->sk_prot->hash(sk);
1259 #define SOCK_DESTROY_TIME (10*HZ)
1262 #define PROT_SOCK 1024
1264 #define SHUTDOWN_MASK 3
1265 #define RCV_SHUTDOWN 1
1266 #define SEND_SHUTDOWN 2
1268 #define SOCK_SNDBUF_LOCK 1
1269 #define SOCK_RCVBUF_LOCK 2
1270 #define SOCK_BINDADDR_LOCK 4
1271 #define SOCK_BINDPORT_LOCK 8
1317 #define SK_MEM_QUANTUM ((int)PAGE_SIZE)
1318 #define SK_MEM_QUANTUM_SHIFT ilog2(SK_MEM_QUANTUM)
1319 #define SK_MEM_SEND 0
1320 #define SK_MEM_RECV 1
1322 static inline int sk_mem_pages(
int amt)
1327 static inline bool sk_has_account(
struct sock *sk)
1330 return !!sk->sk_prot->memory_allocated;
1333 static inline bool sk_wmem_schedule(
struct sock *sk,
int size)
1335 if (!sk_has_account(sk))
1342 sk_rmem_schedule(
struct sock *sk,
struct sk_buff *skb,
int size)
1344 if (!sk_has_account(sk))
1348 skb_pfmemalloc(skb);
1351 static inline void sk_mem_reclaim(
struct sock *sk)
1353 if (!sk_has_account(sk))
1359 static inline void sk_mem_reclaim_partial(
struct sock *sk)
1361 if (!sk_has_account(sk))
1367 static inline void sk_mem_charge(
struct sock *sk,
int size)
1369 if (!sk_has_account(sk))
1374 static inline void sk_mem_uncharge(
struct sock *sk,
int size)
1376 if (!sk_has_account(sk))
1381 static inline void sk_wmem_free_skb(
struct sock *sk,
struct sk_buff *skb)
1385 sk_mem_uncharge(sk, skb->
truesize);
1402 #define sock_owned_by_user(sk) ((sk)->sk_lock.owned)
1411 #define sock_lock_init_class_and_name(sk, sname, skey, name, key) \
1413 sk->sk_lock.owned = 0; \
1414 init_waitqueue_head(&sk->sk_lock.wq); \
1415 spin_lock_init(&(sk)->sk_lock.slock); \
1416 debug_check_no_locks_freed((void *)&(sk)->sk_lock, \
1417 sizeof((sk)->sk_lock)); \
1418 lockdep_set_class_and_name(&(sk)->sk_lock.slock, \
1420 lockdep_init_map(&(sk)->sk_lock.dep_map, (name), (key), 0); \
1425 static inline void lock_sock(
struct sock *sk)
1433 #define bh_lock_sock(__sk) spin_lock(&((__sk)->sk_lock.slock))
1434 #define bh_lock_sock_nested(__sk) \
1435 spin_lock_nested(&((__sk)->sk_lock.slock), \
1436 SINGLE_DEPTH_NESTING)
1437 #define bh_unlock_sock(__sk) spin_unlock(&((__sk)->sk_lock.slock))
1448 static inline void unlock_sock_fast(
struct sock *sk,
bool slow)
1459 struct proto *prot);
1466 unsigned long size,
int force,
1469 unsigned long size,
int force,
1476 int op,
char __user *optval,
1477 unsigned int optlen);
1480 int op,
char __user *optval,
1481 int __user *optlen);
1517 char __user *,
int __user *);
1519 char __user *,
unsigned int);
1521 struct msghdr *,
size_t);
1523 struct msghdr *,
size_t,
int);
1537 char __user *optval,
int __user *optlen);
1541 char __user *optval,
unsigned int optlen);
1543 int optname,
char __user *optval,
int __user *optlen);
1545 int optname,
char __user *optval,
unsigned int optlen);
1565 static inline void sk_filter_release(
struct sk_filter *
fp)
1571 static inline void sk_filter_uncharge(
struct sock *sk,
struct sk_filter *fp)
1573 unsigned int size = sk_filter_len(fp);
1576 sk_filter_release(fp);
1579 static inline void sk_filter_charge(
struct sock *sk,
struct sk_filter *fp)
1611 static inline void sock_put(
struct sock *sk)
1620 static inline void sk_tx_queue_set(
struct sock *sk,
int tx_queue)
1622 sk->sk_tx_queue_mapping = tx_queue;
1625 static inline void sk_tx_queue_clear(
struct sock *sk)
1627 sk->sk_tx_queue_mapping = -1;
1630 static inline int sk_tx_queue_get(
const struct sock *sk)
1632 return sk ? sk->sk_tx_queue_mapping : -1;
1635 static inline void sk_set_socket(
struct sock *sk,
struct socket *
sock)
1637 sk_tx_queue_clear(sk);
1653 static inline void sock_orphan(
struct sock *sk)
1657 sk_set_socket(sk,
NULL);
1662 static inline void sock_graft(
struct sock *sk,
struct socket *parent)
1667 sk_set_socket(sk, parent);
1668 security_sock_graft(sk, parent);
1676 __sk_dst_get(
struct sock *sk)
1683 sk_dst_get(
struct sock *sk)
1697 static inline void dst_negative_advice(
struct sock *sk)
1699 struct dst_entry *ndst, *dst = __sk_dst_get(sk);
1701 if (dst && dst->
ops->negative_advice) {
1702 ndst = dst->
ops->negative_advice(dst);
1716 sk_tx_queue_clear(sk);
1730 __sk_dst_set(sk, dst);
1735 __sk_dst_reset(
struct sock *sk)
1737 __sk_dst_set(sk,
NULL);
1741 sk_dst_reset(
struct sock *sk)
1752 static inline bool sk_can_gso(
const struct sock *sk)
1765 static inline int skb_do_copy_data_nocache(
struct sock *sk,
struct sk_buff *skb,
1766 char __user *
from,
char *to,
1774 skb->
csum = csum_block_add(skb->
csum, csum, offset);
1777 __copy_from_user_nocache(to, from, copy))
1785 static inline int skb_add_data_nocache(
struct sock *sk,
struct sk_buff *skb,
1786 char __user *from,
int copy)
1788 int err, offset = skb->
len;
1790 err = skb_do_copy_data_nocache(sk, skb, from,
skb_put(skb, copy),
1793 __skb_trim(skb, offset);
1798 static inline int skb_copy_to_page_nocache(
struct sock *sk,
char __user *from,
1805 err = skb_do_copy_data_nocache(sk, skb, from,
page_address(page) + off,
1814 sk_mem_charge(sk, copy);
1818 static inline int skb_copy_to_page(
struct sock *sk,
char __user *from,
1819 struct sk_buff *skb,
struct page *page,
1829 skb->
csum = csum_block_add(skb->
csum, csum, skb->
len);
1837 sk_mem_charge(sk, copy);
1847 static inline int sk_wmem_alloc_get(
const struct sock *sk)
1858 static inline int sk_rmem_alloc_get(
const struct sock *sk)
1869 static inline bool sk_has_allocations(
const struct sock *sk)
1871 return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk);
1906 static inline bool wq_has_sleeper(
struct socket_wq *wq)
1914 return wq && waitqueue_active(&wq->
wait);
1925 static inline void sock_poll_wait(
struct file *filp,
1928 if (!poll_does_not_wait(p) && wait_address) {
1929 poll_wait(filp, wait_address, p);
1948 static inline void skb_set_owner_w(
struct sk_buff *skb,
struct sock *sk)
1961 static inline void skb_set_owner_r(
struct sk_buff *skb,
struct sock *sk)
1983 static inline int sock_error(
struct sock *sk)
1992 static inline unsigned long sock_wspace(
struct sock *sk)
2004 static inline void sk_wake_async(
struct sock *sk,
int how,
int band)
2010 #define SOCK_MIN_SNDBUF 2048
2015 #define SOCK_MIN_RCVBUF (2048 + sizeof(struct sk_buff))
2017 static inline void sk_stream_moderate_sndbuf(
struct sock *sk)
2034 static inline struct page_frag *sk_page_frag(
struct sock *sk)
2047 static inline bool sock_writeable(
const struct sock *sk)
2052 static inline gfp_t gfp_any(
void)
2057 static inline long sock_rcvtimeo(
const struct sock *sk,
bool noblock)
2062 static inline long sock_sndtimeo(
const struct sock *sk,
bool noblock)
2067 static inline int sock_rcvlowat(
const struct sock *sk,
int waitall,
int len)
2075 static inline int sock_intr_errno(
long timeo)
2119 static inline void sock_recv_ts_and_drops(
struct msghdr *msg,
struct sock *sk,
2122 #define FLAGS_TS_OR_DROPS ((1UL << SOCK_RXQ_OVFL) | \
2123 (1UL << SOCK_RCVTSTAMP) | \
2124 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
2125 (1UL << SOCK_TIMESTAMPING_SOFTWARE) | \
2126 (1UL << SOCK_TIMESTAMPING_RAW_HARDWARE) | \
2127 (1UL << SOCK_TIMESTAMPING_SYS_HARDWARE))
2129 if (sk->
sk_flags & FLAGS_TS_OR_DROPS)
2154 #ifdef CONFIG_NET_DMA
2155 static inline void sk_eat_skb(
struct sock *sk,
struct sk_buff *skb,
bool copied_early)
2161 __skb_queue_tail(&sk->sk_async_wait_queue, skb);
2164 static inline void sk_eat_skb(
struct sock *sk,
struct sk_buff *skb,
bool copied_early)
2172 struct net *sock_net(
const struct sock *sk)
2178 void sock_net_set(
struct sock *sk,
struct net *
net)
2189 static inline void sk_change_net(
struct sock *sk,
struct net *
net)
2191 put_net(sock_net(sk));
2192 sock_net_set(sk, hold_net(net));
2195 static inline struct sock *skb_steal_sock(
struct sk_buff *skb)
2198 struct sock *sk = skb->
sk;
2215 #define NETDEBUG(fmt, args...) \
2216 do { if (net_msg_warn) printk(fmt,##args); } while (0)
2218 #define LIMIT_NETDEBUG(fmt, args...) \
2219 do { if (net_msg_warn && net_ratelimit()) printk(fmt,##args); } while(0)