Lines Matching refs:sk
157 bool sk_ns_capable(const struct sock *sk, in sk_ns_capable() argument
160 return file_ns_capable(sk->sk_socket->file, user_ns, cap) && in sk_ns_capable()
174 bool sk_capable(const struct sock *sk, int cap) in sk_capable() argument
176 return sk_ns_capable(sk, &init_user_ns, cap); in sk_capable()
189 bool sk_net_capable(const struct sock *sk, int cap) in sk_net_capable() argument
191 return sk_ns_capable(sk, sock_net(sk)->user_ns, cap); in sk_net_capable()
300 void sk_set_memalloc(struct sock *sk) in sk_set_memalloc() argument
302 sock_set_flag(sk, SOCK_MEMALLOC); in sk_set_memalloc()
303 sk->sk_allocation |= __GFP_MEMALLOC; in sk_set_memalloc()
308 void sk_clear_memalloc(struct sock *sk) in sk_clear_memalloc() argument
310 sock_reset_flag(sk, SOCK_MEMALLOC); in sk_clear_memalloc()
311 sk->sk_allocation &= ~__GFP_MEMALLOC; in sk_clear_memalloc()
321 sk_mem_reclaim(sk); in sk_clear_memalloc()
325 int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb) in __sk_backlog_rcv() argument
331 BUG_ON(!sock_flag(sk, SOCK_MEMALLOC)); in __sk_backlog_rcv()
334 ret = sk->sk_backlog_rcv(sk, skb); in __sk_backlog_rcv()
383 static bool sock_needs_netstamp(const struct sock *sk) in sock_needs_netstamp() argument
385 switch (sk->sk_family) { in sock_needs_netstamp()
394 static void sock_disable_timestamp(struct sock *sk, unsigned long flags) in sock_disable_timestamp() argument
396 if (sk->sk_flags & flags) { in sock_disable_timestamp()
397 sk->sk_flags &= ~flags; in sock_disable_timestamp()
398 if (sock_needs_netstamp(sk) && in sock_disable_timestamp()
399 !(sk->sk_flags & SK_FLAGS_TIMESTAMP)) in sock_disable_timestamp()
405 int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) in __sock_queue_rcv_skb() argument
408 struct sk_buff_head *list = &sk->sk_receive_queue; in __sock_queue_rcv_skb()
410 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) { in __sock_queue_rcv_skb()
411 atomic_inc(&sk->sk_drops); in __sock_queue_rcv_skb()
412 trace_sock_rcvqueue_full(sk, skb); in __sock_queue_rcv_skb()
416 if (!sk_rmem_schedule(sk, skb, skb->truesize)) { in __sock_queue_rcv_skb()
417 atomic_inc(&sk->sk_drops); in __sock_queue_rcv_skb()
422 skb_set_owner_r(skb, sk); in __sock_queue_rcv_skb()
430 sock_skb_set_dropcount(sk, skb); in __sock_queue_rcv_skb()
434 if (!sock_flag(sk, SOCK_DEAD)) in __sock_queue_rcv_skb()
435 sk->sk_data_ready(sk); in __sock_queue_rcv_skb()
440 int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) in sock_queue_rcv_skb() argument
444 err = sk_filter(sk, skb); in sock_queue_rcv_skb()
448 return __sock_queue_rcv_skb(sk, skb); in sock_queue_rcv_skb()
452 int __sk_receive_skb(struct sock *sk, struct sk_buff *skb, in __sk_receive_skb() argument
457 if (sk_filter_trim_cap(sk, skb, trim_cap)) in __sk_receive_skb()
462 if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) { in __sk_receive_skb()
463 atomic_inc(&sk->sk_drops); in __sk_receive_skb()
467 bh_lock_sock_nested(sk); in __sk_receive_skb()
469 bh_lock_sock(sk); in __sk_receive_skb()
470 if (!sock_owned_by_user(sk)) { in __sk_receive_skb()
474 mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_); in __sk_receive_skb()
476 rc = sk_backlog_rcv(sk, skb); in __sk_receive_skb()
478 mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_); in __sk_receive_skb()
479 } else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) { in __sk_receive_skb()
480 bh_unlock_sock(sk); in __sk_receive_skb()
481 atomic_inc(&sk->sk_drops); in __sk_receive_skb()
485 bh_unlock_sock(sk); in __sk_receive_skb()
488 sock_put(sk); in __sk_receive_skb()
496 struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie) in __sk_dst_check() argument
498 struct dst_entry *dst = __sk_dst_get(sk); in __sk_dst_check()
501 sk_tx_queue_clear(sk); in __sk_dst_check()
502 RCU_INIT_POINTER(sk->sk_dst_cache, NULL); in __sk_dst_check()
511 struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie) in sk_dst_check() argument
513 struct dst_entry *dst = sk_dst_get(sk); in sk_dst_check()
516 sk_dst_reset(sk); in sk_dst_check()
525 static int sock_setbindtodevice(struct sock *sk, char __user *optval, in sock_setbindtodevice() argument
530 struct net *net = sock_net(sk); in sock_setbindtodevice()
570 lock_sock(sk); in sock_setbindtodevice()
571 sk->sk_bound_dev_if = index; in sock_setbindtodevice()
572 sk_dst_reset(sk); in sock_setbindtodevice()
573 release_sock(sk); in sock_setbindtodevice()
583 static int sock_getbindtodevice(struct sock *sk, char __user *optval, in sock_getbindtodevice() argument
588 struct net *net = sock_net(sk); in sock_getbindtodevice()
591 if (sk->sk_bound_dev_if == 0) { in sock_getbindtodevice()
600 ret = netdev_get_name(net, devname, sk->sk_bound_dev_if); in sock_getbindtodevice()
623 static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool) in sock_valbool_flag() argument
626 sock_set_flag(sk, bit); in sock_valbool_flag()
628 sock_reset_flag(sk, bit); in sock_valbool_flag()
631 bool sk_mc_loop(struct sock *sk) in sk_mc_loop() argument
635 if (!sk) in sk_mc_loop()
637 switch (sk->sk_family) { in sk_mc_loop()
639 return inet_sk(sk)->mc_loop; in sk_mc_loop()
642 return inet6_sk(sk)->mc_loop; in sk_mc_loop()
658 struct sock *sk = sock->sk; in sock_setsockopt() local
669 return sock_setbindtodevice(sk, optval, optlen); in sock_setsockopt()
679 lock_sock(sk); in sock_setsockopt()
686 sock_valbool_flag(sk, SOCK_DBG, valbool); in sock_setsockopt()
689 sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE); in sock_setsockopt()
692 sk->sk_reuseport = valbool; in sock_setsockopt()
701 sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool); in sock_setsockopt()
704 sock_valbool_flag(sk, SOCK_BROADCAST, valbool); in sock_setsockopt()
714 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; in sock_setsockopt()
715 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF); in sock_setsockopt()
717 sk->sk_write_space(sk); in sock_setsockopt()
735 sk->sk_userlocks |= SOCK_RCVBUF_LOCK; in sock_setsockopt()
751 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF); in sock_setsockopt()
763 if (sk->sk_protocol == IPPROTO_TCP && in sock_setsockopt()
764 sk->sk_type == SOCK_STREAM) in sock_setsockopt()
765 tcp_set_keepalive(sk, valbool); in sock_setsockopt()
767 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool); in sock_setsockopt()
771 sock_valbool_flag(sk, SOCK_URGINLINE, valbool); in sock_setsockopt()
775 sk->sk_no_check_tx = valbool; in sock_setsockopt()
780 ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) in sock_setsockopt()
781 sk->sk_priority = val; in sock_setsockopt()
796 sock_reset_flag(sk, SOCK_LINGER); in sock_setsockopt()
800 sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT; in sock_setsockopt()
803 sk->sk_lingertime = (unsigned int)ling.l_linger * HZ; in sock_setsockopt()
804 sock_set_flag(sk, SOCK_LINGER); in sock_setsockopt()
823 sock_reset_flag(sk, SOCK_RCVTSTAMPNS); in sock_setsockopt()
825 sock_set_flag(sk, SOCK_RCVTSTAMPNS); in sock_setsockopt()
826 sock_set_flag(sk, SOCK_RCVTSTAMP); in sock_setsockopt()
827 sock_enable_timestamp(sk, SOCK_TIMESTAMP); in sock_setsockopt()
829 sock_reset_flag(sk, SOCK_RCVTSTAMP); in sock_setsockopt()
830 sock_reset_flag(sk, SOCK_RCVTSTAMPNS); in sock_setsockopt()
841 !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) { in sock_setsockopt()
842 if (sk->sk_protocol == IPPROTO_TCP && in sock_setsockopt()
843 sk->sk_type == SOCK_STREAM) { in sock_setsockopt()
844 if ((1 << sk->sk_state) & in sock_setsockopt()
849 sk->sk_tskey = tcp_sk(sk)->snd_una; in sock_setsockopt()
851 sk->sk_tskey = 0; in sock_setsockopt()
854 sk->sk_tsflags = val; in sock_setsockopt()
856 sock_enable_timestamp(sk, in sock_setsockopt()
859 sock_disable_timestamp(sk, in sock_setsockopt()
866 sk->sk_rcvlowat = val ? : 1; in sock_setsockopt()
870 ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen); in sock_setsockopt()
874 ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen); in sock_setsockopt()
886 ret = sk_attach_filter(&fprog, sk); in sock_setsockopt()
899 ret = sk_attach_bpf(ufd, sk); in sock_setsockopt()
912 ret = sk_reuseport_attach_filter(&fprog, sk); in sock_setsockopt()
925 ret = sk_reuseport_attach_bpf(ufd, sk); in sock_setsockopt()
930 ret = sk_detach_filter(sk); in sock_setsockopt()
934 if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool) in sock_setsockopt()
937 sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool); in sock_setsockopt()
947 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) in sock_setsockopt()
950 sk->sk_mark = val; in sock_setsockopt()
954 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool); in sock_setsockopt()
958 sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool); in sock_setsockopt()
963 ret = sock->ops->set_peek_off(sk, val); in sock_setsockopt()
969 sock_valbool_flag(sk, SOCK_NOFCS, valbool); in sock_setsockopt()
973 sock_valbool_flag(sk, SOCK_SELECT_ERR_QUEUE, valbool); in sock_setsockopt()
979 if ((val > sk->sk_ll_usec) && !capable(CAP_NET_ADMIN)) in sock_setsockopt()
985 sk->sk_ll_usec = val; in sock_setsockopt()
991 sk->sk_max_pacing_rate = val; in sock_setsockopt()
992 sk->sk_pacing_rate = min(sk->sk_pacing_rate, in sock_setsockopt()
993 sk->sk_max_pacing_rate); in sock_setsockopt()
997 sk->sk_incoming_cpu = val; in sock_setsockopt()
1002 dst_negative_advice(sk); in sock_setsockopt()
1008 release_sock(sk); in sock_setsockopt()
1030 struct sock *sk = sock->sk; in sock_getsockopt() local
1051 v.val = sock_flag(sk, SOCK_DBG); in sock_getsockopt()
1055 v.val = sock_flag(sk, SOCK_LOCALROUTE); in sock_getsockopt()
1059 v.val = sock_flag(sk, SOCK_BROADCAST); in sock_getsockopt()
1063 v.val = sk->sk_sndbuf; in sock_getsockopt()
1067 v.val = sk->sk_rcvbuf; in sock_getsockopt()
1071 v.val = sk->sk_reuse; in sock_getsockopt()
1075 v.val = sk->sk_reuseport; in sock_getsockopt()
1079 v.val = sock_flag(sk, SOCK_KEEPOPEN); in sock_getsockopt()
1083 v.val = sk->sk_type; in sock_getsockopt()
1087 v.val = sk->sk_protocol; in sock_getsockopt()
1091 v.val = sk->sk_family; in sock_getsockopt()
1095 v.val = -sock_error(sk); in sock_getsockopt()
1097 v.val = xchg(&sk->sk_err_soft, 0); in sock_getsockopt()
1101 v.val = sock_flag(sk, SOCK_URGINLINE); in sock_getsockopt()
1105 v.val = sk->sk_no_check_tx; in sock_getsockopt()
1109 v.val = sk->sk_priority; in sock_getsockopt()
1114 v.ling.l_onoff = sock_flag(sk, SOCK_LINGER); in sock_getsockopt()
1115 v.ling.l_linger = sk->sk_lingertime / HZ; in sock_getsockopt()
1123 v.val = sock_flag(sk, SOCK_RCVTSTAMP) && in sock_getsockopt()
1124 !sock_flag(sk, SOCK_RCVTSTAMPNS); in sock_getsockopt()
1128 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS); in sock_getsockopt()
1132 v.val = sk->sk_tsflags; in sock_getsockopt()
1137 if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) { in sock_getsockopt()
1141 v.tm.tv_sec = sk->sk_rcvtimeo / HZ; in sock_getsockopt()
1142 v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * 1000000) / HZ; in sock_getsockopt()
1148 if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) { in sock_getsockopt()
1152 v.tm.tv_sec = sk->sk_sndtimeo / HZ; in sock_getsockopt()
1153 v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * 1000000) / HZ; in sock_getsockopt()
1158 v.val = sk->sk_rcvlowat; in sock_getsockopt()
1174 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred); in sock_getsockopt()
1197 v.val = sk->sk_state == TCP_LISTEN; in sock_getsockopt()
1208 v.val = sk->sk_mark; in sock_getsockopt()
1212 v.val = sock_flag(sk, SOCK_RXQ_OVFL); in sock_getsockopt()
1216 v.val = sock_flag(sk, SOCK_WIFI_STATUS); in sock_getsockopt()
1223 v.val = sk->sk_peek_off; in sock_getsockopt()
1226 v.val = sock_flag(sk, SOCK_NOFCS); in sock_getsockopt()
1230 return sock_getbindtodevice(sk, optval, optlen, len); in sock_getsockopt()
1233 len = sk_get_filter(sk, (struct sock_filter __user *)optval, len); in sock_getsockopt()
1240 v.val = sock_flag(sk, SOCK_FILTER_LOCKED); in sock_getsockopt()
1248 v.val = sock_flag(sk, SOCK_SELECT_ERR_QUEUE); in sock_getsockopt()
1253 v.val = sk->sk_ll_usec; in sock_getsockopt()
1258 v.val = sk->sk_max_pacing_rate; in sock_getsockopt()
1262 v.val = sk->sk_incoming_cpu; in sock_getsockopt()
1270 v.val64 = sock_gen_cookie(sk); in sock_getsockopt()
1294 static inline void sock_lock_init(struct sock *sk) in sock_lock_init() argument
1296 sock_lock_init_class_and_name(sk, in sock_lock_init()
1297 af_family_slock_key_strings[sk->sk_family], in sock_lock_init()
1298 af_family_slock_keys + sk->sk_family, in sock_lock_init()
1299 af_family_key_strings[sk->sk_family], in sock_lock_init()
1300 af_family_keys + sk->sk_family); in sock_lock_init()
1327 struct sock *sk; in sk_prot_alloc() local
1332 sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO); in sk_prot_alloc()
1333 if (!sk) in sk_prot_alloc()
1334 return sk; in sk_prot_alloc()
1336 sk_prot_clear_nulls(sk, prot->obj_size); in sk_prot_alloc()
1338 sk = kmalloc(prot->obj_size, priority); in sk_prot_alloc()
1340 if (sk != NULL) { in sk_prot_alloc()
1341 kmemcheck_annotate_bitfield(sk, flags); in sk_prot_alloc()
1343 if (security_sk_alloc(sk, family, priority)) in sk_prot_alloc()
1348 sk_tx_queue_clear(sk); in sk_prot_alloc()
1351 return sk; in sk_prot_alloc()
1354 security_sk_free(sk); in sk_prot_alloc()
1357 kmem_cache_free(slab, sk); in sk_prot_alloc()
1359 kfree(sk); in sk_prot_alloc()
1363 static void sk_prot_free(struct proto *prot, struct sock *sk) in sk_prot_free() argument
1371 cgroup_sk_free(&sk->sk_cgrp_data); in sk_prot_free()
1372 mem_cgroup_sk_free(sk); in sk_prot_free()
1373 security_sk_free(sk); in sk_prot_free()
1375 kmem_cache_free(slab, sk); in sk_prot_free()
1377 kfree(sk); in sk_prot_free()
1392 struct sock *sk; in sk_alloc() local
1394 sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family); in sk_alloc()
1395 if (sk) { in sk_alloc()
1396 sk->sk_family = family; in sk_alloc()
1401 sk->sk_prot = sk->sk_prot_creator = prot; in sk_alloc()
1402 sock_lock_init(sk); in sk_alloc()
1403 sk->sk_net_refcnt = kern ? 0 : 1; in sk_alloc()
1404 if (likely(sk->sk_net_refcnt)) in sk_alloc()
1406 sock_net_set(sk, net); in sk_alloc()
1407 atomic_set(&sk->sk_wmem_alloc, 1); in sk_alloc()
1409 mem_cgroup_sk_alloc(sk); in sk_alloc()
1410 cgroup_sk_alloc(&sk->sk_cgrp_data); in sk_alloc()
1411 sock_update_classid(&sk->sk_cgrp_data); in sk_alloc()
1412 sock_update_netprioidx(&sk->sk_cgrp_data); in sk_alloc()
1415 return sk; in sk_alloc()
1424 struct sock *sk = container_of(head, struct sock, sk_rcu); in __sk_destruct() local
1427 if (sk->sk_destruct) in __sk_destruct()
1428 sk->sk_destruct(sk); in __sk_destruct()
1430 filter = rcu_dereference_check(sk->sk_filter, in __sk_destruct()
1431 atomic_read(&sk->sk_wmem_alloc) == 0); in __sk_destruct()
1433 sk_filter_uncharge(sk, filter); in __sk_destruct()
1434 RCU_INIT_POINTER(sk->sk_filter, NULL); in __sk_destruct()
1436 if (rcu_access_pointer(sk->sk_reuseport_cb)) in __sk_destruct()
1437 reuseport_detach_sock(sk); in __sk_destruct()
1439 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP); in __sk_destruct()
1441 if (atomic_read(&sk->sk_omem_alloc)) in __sk_destruct()
1443 __func__, atomic_read(&sk->sk_omem_alloc)); in __sk_destruct()
1445 if (sk->sk_frag.page) { in __sk_destruct()
1446 put_page(sk->sk_frag.page); in __sk_destruct()
1447 sk->sk_frag.page = NULL; in __sk_destruct()
1450 if (sk->sk_peer_cred) in __sk_destruct()
1451 put_cred(sk->sk_peer_cred); in __sk_destruct()
1452 put_pid(sk->sk_peer_pid); in __sk_destruct()
1453 if (likely(sk->sk_net_refcnt)) in __sk_destruct()
1454 put_net(sock_net(sk)); in __sk_destruct()
1455 sk_prot_free(sk->sk_prot_creator, sk); in __sk_destruct()
1458 void sk_destruct(struct sock *sk) in sk_destruct() argument
1460 if (sock_flag(sk, SOCK_RCU_FREE)) in sk_destruct()
1461 call_rcu(&sk->sk_rcu, __sk_destruct); in sk_destruct()
1463 __sk_destruct(&sk->sk_rcu); in sk_destruct()
1466 static void __sk_free(struct sock *sk) in __sk_free() argument
1468 if (unlikely(sock_diag_has_destroy_listeners(sk) && sk->sk_net_refcnt)) in __sk_free()
1469 sock_diag_broadcast_destroy(sk); in __sk_free()
1471 sk_destruct(sk); in __sk_free()
1474 void sk_free(struct sock *sk) in sk_free() argument
1481 if (atomic_dec_and_test(&sk->sk_wmem_alloc)) in sk_free()
1482 __sk_free(sk); in sk_free()
1493 struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) in sk_clone_lock() argument
1498 newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family); in sk_clone_lock()
1502 sock_copy(newsk, sk); in sk_clone_lock()
1504 newsk->sk_prot_creator = sk->sk_prot; in sk_clone_lock()
1534 newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK; in sk_clone_lock()
1548 if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) { in sk_clone_lock()
1597 if (sock_needs_netstamp(sk) && in sk_clone_lock()
1606 void sk_setup_caps(struct sock *sk, struct dst_entry *dst) in sk_setup_caps() argument
1610 sk_dst_set(sk, dst); in sk_setup_caps()
1611 sk->sk_route_caps = dst->dev->features; in sk_setup_caps()
1612 if (sk->sk_route_caps & NETIF_F_GSO) in sk_setup_caps()
1613 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE; in sk_setup_caps()
1614 sk->sk_route_caps &= ~sk->sk_route_nocaps; in sk_setup_caps()
1615 if (sk_can_gso(sk)) { in sk_setup_caps()
1617 sk->sk_route_caps &= ~NETIF_F_GSO_MASK; in sk_setup_caps()
1619 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM; in sk_setup_caps()
1620 sk->sk_gso_max_size = dst->dev->gso_max_size; in sk_setup_caps()
1624 sk->sk_gso_max_segs = max_segs; in sk_setup_caps()
1638 struct sock *sk = skb->sk; in sock_wfree() local
1641 if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) { in sock_wfree()
1646 atomic_sub(len - 1, &sk->sk_wmem_alloc); in sock_wfree()
1647 sk->sk_write_space(sk); in sock_wfree()
1654 if (atomic_sub_and_test(len, &sk->sk_wmem_alloc)) in sock_wfree()
1655 __sk_free(sk); in sock_wfree()
1664 struct sock *sk = skb->sk; in __sock_wfree() local
1666 if (atomic_sub_and_test(skb->truesize, &sk->sk_wmem_alloc)) in __sock_wfree()
1667 __sk_free(sk); in __sock_wfree()
1670 void skb_set_owner_w(struct sk_buff *skb, struct sock *sk) in skb_set_owner_w() argument
1673 skb->sk = sk; in skb_set_owner_w()
1675 if (unlikely(!sk_fullsock(sk))) { in skb_set_owner_w()
1677 sock_hold(sk); in skb_set_owner_w()
1682 skb_set_hash_from_sk(skb, sk); in skb_set_owner_w()
1688 atomic_add(skb->truesize, &sk->sk_wmem_alloc); in skb_set_owner_w()
1708 struct sock *sk = skb->sk; in skb_orphan_partial() local
1710 if (atomic_inc_not_zero(&sk->sk_refcnt)) { in skb_orphan_partial()
1711 atomic_sub(skb->truesize, &sk->sk_wmem_alloc); in skb_orphan_partial()
1725 struct sock *sk = skb->sk; in sock_rfree() local
1728 atomic_sub(len, &sk->sk_rmem_alloc); in sock_rfree()
1729 sk_mem_uncharge(sk, len); in sock_rfree()
1739 sock_put(skb->sk); in sock_efree()
1743 kuid_t sock_i_uid(struct sock *sk) in sock_i_uid() argument
1747 read_lock_bh(&sk->sk_callback_lock); in sock_i_uid()
1748 uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID; in sock_i_uid()
1749 read_unlock_bh(&sk->sk_callback_lock); in sock_i_uid()
1754 unsigned long sock_i_ino(struct sock *sk) in sock_i_ino() argument
1758 read_lock_bh(&sk->sk_callback_lock); in sock_i_ino()
1759 ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0; in sock_i_ino()
1760 read_unlock_bh(&sk->sk_callback_lock); in sock_i_ino()
1768 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, in sock_wmalloc() argument
1771 if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) { in sock_wmalloc()
1774 skb_set_owner_w(skb, sk); in sock_wmalloc()
1785 void *sock_kmalloc(struct sock *sk, int size, gfp_t priority) in sock_kmalloc() argument
1788 atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) { in sock_kmalloc()
1793 atomic_add(size, &sk->sk_omem_alloc); in sock_kmalloc()
1797 atomic_sub(size, &sk->sk_omem_alloc); in sock_kmalloc()
1807 static inline void __sock_kfree_s(struct sock *sk, void *mem, int size, in __sock_kfree_s() argument
1816 atomic_sub(size, &sk->sk_omem_alloc); in __sock_kfree_s()
1819 void sock_kfree_s(struct sock *sk, void *mem, int size) in sock_kfree_s() argument
1821 __sock_kfree_s(sk, mem, size, false); in sock_kfree_s()
1825 void sock_kzfree_s(struct sock *sk, void *mem, int size) in sock_kzfree_s() argument
1827 __sock_kfree_s(sk, mem, size, true); in sock_kzfree_s()
1834 static long sock_wait_for_wmem(struct sock *sk, long timeo) in sock_wait_for_wmem() argument
1838 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); in sock_wait_for_wmem()
1844 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); in sock_wait_for_wmem()
1845 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); in sock_wait_for_wmem()
1846 if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) in sock_wait_for_wmem()
1848 if (sk->sk_shutdown & SEND_SHUTDOWN) in sock_wait_for_wmem()
1850 if (sk->sk_err) in sock_wait_for_wmem()
1854 finish_wait(sk_sleep(sk), &wait); in sock_wait_for_wmem()
1863 struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len, in sock_alloc_send_pskb() argument
1871 timeo = sock_sndtimeo(sk, noblock); in sock_alloc_send_pskb()
1873 err = sock_error(sk); in sock_alloc_send_pskb()
1878 if (sk->sk_shutdown & SEND_SHUTDOWN) in sock_alloc_send_pskb()
1881 if (sk_wmem_alloc_get(sk) < sk->sk_sndbuf) in sock_alloc_send_pskb()
1884 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); in sock_alloc_send_pskb()
1885 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); in sock_alloc_send_pskb()
1891 timeo = sock_wait_for_wmem(sk, timeo); in sock_alloc_send_pskb()
1894 errcode, sk->sk_allocation); in sock_alloc_send_pskb()
1896 skb_set_owner_w(skb, sk); in sock_alloc_send_pskb()
1907 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size, in sock_alloc_send_skb() argument
1910 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode, 0); in sock_alloc_send_skb()
1914 int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg, in __sock_cmsg_send() argument
1921 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) in __sock_cmsg_send()
1949 int sock_cmsg_send(struct sock *sk, struct msghdr *msg, in sock_cmsg_send() argument
1960 ret = __sock_cmsg_send(sk, msg, cmsg, sockc); in sock_cmsg_send()
2014 bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag) in sk_page_frag_refill() argument
2016 if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation))) in sk_page_frag_refill()
2019 sk_enter_memory_pressure(sk); in sk_page_frag_refill()
2020 sk_stream_moderate_sndbuf(sk); in sk_page_frag_refill()
2025 static void __lock_sock(struct sock *sk) in __lock_sock() argument
2026 __releases(&sk->sk_lock.slock) in __lock_sock()
2027 __acquires(&sk->sk_lock.slock) in __lock_sock()
2032 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait, in __lock_sock()
2034 spin_unlock_bh(&sk->sk_lock.slock); in __lock_sock()
2036 spin_lock_bh(&sk->sk_lock.slock); in __lock_sock()
2037 if (!sock_owned_by_user(sk)) in __lock_sock()
2040 finish_wait(&sk->sk_lock.wq, &wait); in __lock_sock()
2043 static void __release_sock(struct sock *sk) in __release_sock() argument
2044 __releases(&sk->sk_lock.slock) in __release_sock()
2045 __acquires(&sk->sk_lock.slock) in __release_sock()
2049 while ((skb = sk->sk_backlog.head) != NULL) { in __release_sock()
2050 sk->sk_backlog.head = sk->sk_backlog.tail = NULL; in __release_sock()
2052 spin_unlock_bh(&sk->sk_lock.slock); in __release_sock()
2059 sk_backlog_rcv(sk, skb); in __release_sock()
2066 spin_lock_bh(&sk->sk_lock.slock); in __release_sock()
2073 sk->sk_backlog.len = 0; in __release_sock()
2076 void __sk_flush_backlog(struct sock *sk) in __sk_flush_backlog() argument
2078 spin_lock_bh(&sk->sk_lock.slock); in __sk_flush_backlog()
2079 __release_sock(sk); in __sk_flush_backlog()
2080 spin_unlock_bh(&sk->sk_lock.slock); in __sk_flush_backlog()
2094 int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb) in sk_wait_data() argument
2099 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); in sk_wait_data()
2100 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); in sk_wait_data()
2101 rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb); in sk_wait_data()
2102 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); in sk_wait_data()
2103 finish_wait(sk_sleep(sk), &wait); in sk_wait_data()
2118 int __sk_mem_schedule(struct sock *sk, int size, int kind) in __sk_mem_schedule() argument
2120 struct proto *prot = sk->sk_prot; in __sk_mem_schedule()
2124 sk->sk_forward_alloc += amt * SK_MEM_QUANTUM; in __sk_mem_schedule()
2126 allocated = sk_memory_allocated_add(sk, amt); in __sk_mem_schedule()
2128 if (mem_cgroup_sockets_enabled && sk->sk_memcg && in __sk_mem_schedule()
2129 !mem_cgroup_charge_skmem(sk->sk_memcg, amt)) in __sk_mem_schedule()
2133 if (allocated <= sk_prot_mem_limits(sk, 0)) { in __sk_mem_schedule()
2134 sk_leave_memory_pressure(sk); in __sk_mem_schedule()
2139 if (allocated > sk_prot_mem_limits(sk, 1)) in __sk_mem_schedule()
2140 sk_enter_memory_pressure(sk); in __sk_mem_schedule()
2143 if (allocated > sk_prot_mem_limits(sk, 2)) in __sk_mem_schedule()
2148 if (atomic_read(&sk->sk_rmem_alloc) < prot->sysctl_rmem[0]) in __sk_mem_schedule()
2152 if (sk->sk_type == SOCK_STREAM) { in __sk_mem_schedule()
2153 if (sk->sk_wmem_queued < prot->sysctl_wmem[0]) in __sk_mem_schedule()
2155 } else if (atomic_read(&sk->sk_wmem_alloc) < in __sk_mem_schedule()
2160 if (sk_has_memory_pressure(sk)) { in __sk_mem_schedule()
2163 if (!sk_under_memory_pressure(sk)) in __sk_mem_schedule()
2165 alloc = sk_sockets_allocated_read_positive(sk); in __sk_mem_schedule()
2166 if (sk_prot_mem_limits(sk, 2) > alloc * in __sk_mem_schedule()
2167 sk_mem_pages(sk->sk_wmem_queued + in __sk_mem_schedule()
2168 atomic_read(&sk->sk_rmem_alloc) + in __sk_mem_schedule()
2169 sk->sk_forward_alloc)) in __sk_mem_schedule()
2175 if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) { in __sk_mem_schedule()
2176 sk_stream_moderate_sndbuf(sk); in __sk_mem_schedule()
2181 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf) in __sk_mem_schedule()
2185 trace_sock_exceed_buf_limit(sk, prot, allocated); in __sk_mem_schedule()
2188 sk->sk_forward_alloc -= amt * SK_MEM_QUANTUM; in __sk_mem_schedule()
2190 sk_memory_allocated_sub(sk, amt); in __sk_mem_schedule()
2192 if (mem_cgroup_sockets_enabled && sk->sk_memcg) in __sk_mem_schedule()
2193 mem_cgroup_uncharge_skmem(sk->sk_memcg, amt); in __sk_mem_schedule()
2204 void __sk_mem_reclaim(struct sock *sk, int amount) in __sk_mem_reclaim() argument
2207 sk_memory_allocated_sub(sk, amount); in __sk_mem_reclaim()
2208 sk->sk_forward_alloc -= amount << SK_MEM_QUANTUM_SHIFT; in __sk_mem_reclaim()
2210 if (mem_cgroup_sockets_enabled && sk->sk_memcg) in __sk_mem_reclaim()
2211 mem_cgroup_uncharge_skmem(sk->sk_memcg, amount); in __sk_mem_reclaim()
2213 if (sk_under_memory_pressure(sk) && in __sk_mem_reclaim()
2214 (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0))) in __sk_mem_reclaim()
2215 sk_leave_memory_pressure(sk); in __sk_mem_reclaim()
2219 int sk_set_peek_off(struct sock *sk, int val) in sk_set_peek_off() argument
2224 sk->sk_peek_off = val; in sk_set_peek_off()
2344 static void sock_def_wakeup(struct sock *sk) in sock_def_wakeup() argument
2349 wq = rcu_dereference(sk->sk_wq); in sock_def_wakeup()
2355 static void sock_def_error_report(struct sock *sk) in sock_def_error_report() argument
2360 wq = rcu_dereference(sk->sk_wq); in sock_def_error_report()
2363 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR); in sock_def_error_report()
2367 static void sock_def_readable(struct sock *sk) in sock_def_readable() argument
2372 wq = rcu_dereference(sk->sk_wq); in sock_def_readable()
2376 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); in sock_def_readable()
2380 static void sock_def_write_space(struct sock *sk) in sock_def_write_space() argument
2389 if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) { in sock_def_write_space()
2390 wq = rcu_dereference(sk->sk_wq); in sock_def_write_space()
2396 if (sock_writeable(sk)) in sock_def_write_space()
2397 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); in sock_def_write_space()
2403 static void sock_def_destruct(struct sock *sk) in sock_def_destruct() argument
2407 void sk_send_sigurg(struct sock *sk) in sk_send_sigurg() argument
2409 if (sk->sk_socket && sk->sk_socket->file) in sk_send_sigurg()
2410 if (send_sigurg(&sk->sk_socket->file->f_owner)) in sk_send_sigurg()
2411 sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI); in sk_send_sigurg()
2415 void sk_reset_timer(struct sock *sk, struct timer_list* timer, in sk_reset_timer() argument
2419 sock_hold(sk); in sk_reset_timer()
2423 void sk_stop_timer(struct sock *sk, struct timer_list* timer) in sk_stop_timer() argument
2426 __sock_put(sk); in sk_stop_timer()
2430 void sock_init_data(struct socket *sock, struct sock *sk) in sock_init_data() argument
2432 skb_queue_head_init(&sk->sk_receive_queue); in sock_init_data()
2433 skb_queue_head_init(&sk->sk_write_queue); in sock_init_data()
2434 skb_queue_head_init(&sk->sk_error_queue); in sock_init_data()
2436 sk->sk_send_head = NULL; in sock_init_data()
2438 init_timer(&sk->sk_timer); in sock_init_data()
2440 sk->sk_allocation = GFP_KERNEL; in sock_init_data()
2441 sk->sk_rcvbuf = sysctl_rmem_default; in sock_init_data()
2442 sk->sk_sndbuf = sysctl_wmem_default; in sock_init_data()
2443 sk->sk_state = TCP_CLOSE; in sock_init_data()
2444 sk_set_socket(sk, sock); in sock_init_data()
2446 sock_set_flag(sk, SOCK_ZAPPED); in sock_init_data()
2449 sk->sk_type = sock->type; in sock_init_data()
2450 sk->sk_wq = sock->wq; in sock_init_data()
2451 sock->sk = sk; in sock_init_data()
2452 sk->sk_uid = SOCK_INODE(sock)->i_uid; in sock_init_data()
2454 sk->sk_wq = NULL; in sock_init_data()
2455 sk->sk_uid = make_kuid(sock_net(sk)->user_ns, 0); in sock_init_data()
2458 rwlock_init(&sk->sk_callback_lock); in sock_init_data()
2459 lockdep_set_class_and_name(&sk->sk_callback_lock, in sock_init_data()
2460 af_callback_keys + sk->sk_family, in sock_init_data()
2461 af_family_clock_key_strings[sk->sk_family]); in sock_init_data()
2463 sk->sk_state_change = sock_def_wakeup; in sock_init_data()
2464 sk->sk_data_ready = sock_def_readable; in sock_init_data()
2465 sk->sk_write_space = sock_def_write_space; in sock_init_data()
2466 sk->sk_error_report = sock_def_error_report; in sock_init_data()
2467 sk->sk_destruct = sock_def_destruct; in sock_init_data()
2469 sk->sk_frag.page = NULL; in sock_init_data()
2470 sk->sk_frag.offset = 0; in sock_init_data()
2471 sk->sk_peek_off = -1; in sock_init_data()
2473 sk->sk_peer_pid = NULL; in sock_init_data()
2474 sk->sk_peer_cred = NULL; in sock_init_data()
2475 sk->sk_write_pending = 0; in sock_init_data()
2476 sk->sk_rcvlowat = 1; in sock_init_data()
2477 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; in sock_init_data()
2478 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; in sock_init_data()
2480 sk->sk_stamp = ktime_set(-1L, 0); in sock_init_data()
2483 sk->sk_napi_id = 0; in sock_init_data()
2484 sk->sk_ll_usec = sysctl_net_busy_read; in sock_init_data()
2487 sk->sk_max_pacing_rate = ~0U; in sock_init_data()
2488 sk->sk_pacing_rate = ~0U; in sock_init_data()
2489 sk->sk_incoming_cpu = -1; in sock_init_data()
2495 atomic_set(&sk->sk_refcnt, 1); in sock_init_data()
2496 atomic_set(&sk->sk_drops, 0); in sock_init_data()
2500 void lock_sock_nested(struct sock *sk, int subclass) in lock_sock_nested() argument
2503 spin_lock_bh(&sk->sk_lock.slock); in lock_sock_nested()
2504 if (sk->sk_lock.owned) in lock_sock_nested()
2505 __lock_sock(sk); in lock_sock_nested()
2506 sk->sk_lock.owned = 1; in lock_sock_nested()
2507 spin_unlock(&sk->sk_lock.slock); in lock_sock_nested()
2511 mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_); in lock_sock_nested()
2516 void release_sock(struct sock *sk) in release_sock() argument
2518 spin_lock_bh(&sk->sk_lock.slock); in release_sock()
2519 if (sk->sk_backlog.tail) in release_sock()
2520 __release_sock(sk); in release_sock()
2525 if (sk->sk_prot->release_cb) in release_sock()
2526 sk->sk_prot->release_cb(sk); in release_sock()
2528 sock_release_ownership(sk); in release_sock()
2529 if (waitqueue_active(&sk->sk_lock.wq)) in release_sock()
2530 wake_up(&sk->sk_lock.wq); in release_sock()
2531 spin_unlock_bh(&sk->sk_lock.slock); in release_sock()
2545 bool lock_sock_fast(struct sock *sk) in lock_sock_fast() argument
2548 spin_lock_bh(&sk->sk_lock.slock); in lock_sock_fast()
2550 if (!sk->sk_lock.owned) in lock_sock_fast()
2556 __lock_sock(sk); in lock_sock_fast()
2557 sk->sk_lock.owned = 1; in lock_sock_fast()
2558 spin_unlock(&sk->sk_lock.slock); in lock_sock_fast()
2562 mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_); in lock_sock_fast()
2568 int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp) in sock_get_timestamp() argument
2571 if (!sock_flag(sk, SOCK_TIMESTAMP)) in sock_get_timestamp()
2572 sock_enable_timestamp(sk, SOCK_TIMESTAMP); in sock_get_timestamp()
2573 tv = ktime_to_timeval(sk->sk_stamp); in sock_get_timestamp()
2577 sk->sk_stamp = ktime_get_real(); in sock_get_timestamp()
2578 tv = ktime_to_timeval(sk->sk_stamp); in sock_get_timestamp()
2584 int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp) in sock_get_timestampns() argument
2587 if (!sock_flag(sk, SOCK_TIMESTAMP)) in sock_get_timestampns()
2588 sock_enable_timestamp(sk, SOCK_TIMESTAMP); in sock_get_timestampns()
2589 ts = ktime_to_timespec(sk->sk_stamp); in sock_get_timestampns()
2593 sk->sk_stamp = ktime_get_real(); in sock_get_timestampns()
2594 ts = ktime_to_timespec(sk->sk_stamp); in sock_get_timestampns()
2600 void sock_enable_timestamp(struct sock *sk, int flag) in sock_enable_timestamp() argument
2602 if (!sock_flag(sk, flag)) { in sock_enable_timestamp()
2603 unsigned long previous_flags = sk->sk_flags; in sock_enable_timestamp()
2605 sock_set_flag(sk, flag); in sock_enable_timestamp()
2611 if (sock_needs_netstamp(sk) && in sock_enable_timestamp()
2617 int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, in sock_recv_errqueue() argument
2625 skb = sock_dequeue_err_skb(sk); in sock_recv_errqueue()
2638 sock_recv_timestamp(msg, sk, skb); in sock_recv_errqueue()
2663 struct sock *sk = sock->sk; in sock_common_getsockopt() local
2665 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen); in sock_common_getsockopt()
2673 struct sock *sk = sock->sk; in compat_sock_common_getsockopt() local
2675 if (sk->sk_prot->compat_getsockopt != NULL) in compat_sock_common_getsockopt()
2676 return sk->sk_prot->compat_getsockopt(sk, level, optname, in compat_sock_common_getsockopt()
2678 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen); in compat_sock_common_getsockopt()
2686 struct sock *sk = sock->sk; in sock_common_recvmsg() local
2690 err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT, in sock_common_recvmsg()
2704 struct sock *sk = sock->sk; in sock_common_setsockopt() local
2706 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen); in sock_common_setsockopt()
2714 struct sock *sk = sock->sk; in compat_sock_common_setsockopt() local
2716 if (sk->sk_prot->compat_setsockopt != NULL) in compat_sock_common_setsockopt()
2717 return sk->sk_prot->compat_setsockopt(sk, level, optname, in compat_sock_common_setsockopt()
2719 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen); in compat_sock_common_setsockopt()
2724 void sk_common_release(struct sock *sk) in sk_common_release() argument
2726 if (sk->sk_prot->destroy) in sk_common_release()
2727 sk->sk_prot->destroy(sk); in sk_common_release()
2737 sk->sk_prot->unhash(sk); in sk_common_release()
2751 sock_orphan(sk); in sk_common_release()
2753 xfrm_sk_free_policy(sk); in sk_common_release()
2755 sk_refcnt_debug_release(sk); in sk_common_release()
2757 sock_put(sk); in sk_common_release()