aboutsummaryrefslogtreecommitdiffstats
path: root/net/core/sock.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/core/sock.c')
-rw-r--r--net/core/sock.c73
1 files changed, 45 insertions, 28 deletions
diff --git a/net/core/sock.c b/net/core/sock.c
index 1e4dd54bfb5a..51270238e269 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -134,6 +134,7 @@
134#include <linux/sock_diag.h> 134#include <linux/sock_diag.h>
135 135
136#include <linux/filter.h> 136#include <linux/filter.h>
137#include <net/sock_reuseport.h>
137 138
138#include <trace/events/sock.h> 139#include <trace/events/sock.h>
139 140
@@ -433,8 +434,6 @@ static bool sock_needs_netstamp(const struct sock *sk)
433 } 434 }
434} 435}
435 436
436#define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE))
437
438static void sock_disable_timestamp(struct sock *sk, unsigned long flags) 437static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
439{ 438{
440 if (sk->sk_flags & flags) { 439 if (sk->sk_flags & flags) {
@@ -874,7 +873,8 @@ set_rcvbuf:
874 873
875 if (val & SOF_TIMESTAMPING_OPT_ID && 874 if (val & SOF_TIMESTAMPING_OPT_ID &&
876 !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) { 875 !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) {
877 if (sk->sk_protocol == IPPROTO_TCP) { 876 if (sk->sk_protocol == IPPROTO_TCP &&
877 sk->sk_type == SOCK_STREAM) {
878 if (sk->sk_state != TCP_ESTABLISHED) { 878 if (sk->sk_state != TCP_ESTABLISHED) {
879 ret = -EINVAL; 879 ret = -EINVAL;
880 break; 880 break;
@@ -933,6 +933,32 @@ set_rcvbuf:
933 } 933 }
934 break; 934 break;
935 935
936 case SO_ATTACH_REUSEPORT_CBPF:
937 ret = -EINVAL;
938 if (optlen == sizeof(struct sock_fprog)) {
939 struct sock_fprog fprog;
940
941 ret = -EFAULT;
942 if (copy_from_user(&fprog, optval, sizeof(fprog)))
943 break;
944
945 ret = sk_reuseport_attach_filter(&fprog, sk);
946 }
947 break;
948
949 case SO_ATTACH_REUSEPORT_EBPF:
950 ret = -EINVAL;
951 if (optlen == sizeof(u32)) {
952 u32 ufd;
953
954 ret = -EFAULT;
955 if (copy_from_user(&ufd, optval, sizeof(ufd)))
956 break;
957
958 ret = sk_reuseport_attach_bpf(ufd, sk);
959 }
960 break;
961
936 case SO_DETACH_FILTER: 962 case SO_DETACH_FILTER:
937 ret = sk_detach_filter(sk); 963 ret = sk_detach_filter(sk);
938 break; 964 break;
@@ -1363,6 +1389,7 @@ static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
1363 if (!try_module_get(prot->owner)) 1389 if (!try_module_get(prot->owner))
1364 goto out_free_sec; 1390 goto out_free_sec;
1365 sk_tx_queue_clear(sk); 1391 sk_tx_queue_clear(sk);
1392 cgroup_sk_alloc(&sk->sk_cgrp_data);
1366 } 1393 }
1367 1394
1368 return sk; 1395 return sk;
@@ -1385,6 +1412,7 @@ static void sk_prot_free(struct proto *prot, struct sock *sk)
1385 owner = prot->owner; 1412 owner = prot->owner;
1386 slab = prot->slab; 1413 slab = prot->slab;
1387 1414
1415 cgroup_sk_free(&sk->sk_cgrp_data);
1388 security_sk_free(sk); 1416 security_sk_free(sk);
1389 if (slab != NULL) 1417 if (slab != NULL)
1390 kmem_cache_free(slab, sk); 1418 kmem_cache_free(slab, sk);
@@ -1393,17 +1421,6 @@ static void sk_prot_free(struct proto *prot, struct sock *sk)
1393 module_put(owner); 1421 module_put(owner);
1394} 1422}
1395 1423
1396#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
1397void sock_update_netprioidx(struct sock *sk)
1398{
1399 if (in_interrupt())
1400 return;
1401
1402 sk->sk_cgrp_prioidx = task_netprioidx(current);
1403}
1404EXPORT_SYMBOL_GPL(sock_update_netprioidx);
1405#endif
1406
1407/** 1424/**
1408 * sk_alloc - All socket objects are allocated here 1425 * sk_alloc - All socket objects are allocated here
1409 * @net: the applicable net namespace 1426 * @net: the applicable net namespace
@@ -1432,8 +1449,8 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
1432 sock_net_set(sk, net); 1449 sock_net_set(sk, net);
1433 atomic_set(&sk->sk_wmem_alloc, 1); 1450 atomic_set(&sk->sk_wmem_alloc, 1);
1434 1451
1435 sock_update_classid(sk); 1452 sock_update_classid(&sk->sk_cgrp_data);
1436 sock_update_netprioidx(sk); 1453 sock_update_netprioidx(&sk->sk_cgrp_data);
1437 } 1454 }
1438 1455
1439 return sk; 1456 return sk;
@@ -1453,6 +1470,8 @@ void sk_destruct(struct sock *sk)
1453 sk_filter_uncharge(sk, filter); 1470 sk_filter_uncharge(sk, filter);
1454 RCU_INIT_POINTER(sk->sk_filter, NULL); 1471 RCU_INIT_POINTER(sk->sk_filter, NULL);
1455 } 1472 }
1473 if (rcu_access_pointer(sk->sk_reuseport_cb))
1474 reuseport_detach_sock(sk);
1456 1475
1457 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP); 1476 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
1458 1477
@@ -1530,7 +1549,6 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
1530 skb_queue_head_init(&newsk->sk_receive_queue); 1549 skb_queue_head_init(&newsk->sk_receive_queue);
1531 skb_queue_head_init(&newsk->sk_write_queue); 1550 skb_queue_head_init(&newsk->sk_write_queue);
1532 1551
1533 spin_lock_init(&newsk->sk_dst_lock);
1534 rwlock_init(&newsk->sk_callback_lock); 1552 rwlock_init(&newsk->sk_callback_lock);
1535 lockdep_set_class_and_name(&newsk->sk_callback_lock, 1553 lockdep_set_class_and_name(&newsk->sk_callback_lock,
1536 af_callback_keys + newsk->sk_family, 1554 af_callback_keys + newsk->sk_family,
@@ -1553,7 +1571,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
1553 */ 1571 */
1554 is_charged = sk_filter_charge(newsk, filter); 1572 is_charged = sk_filter_charge(newsk, filter);
1555 1573
1556 if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk))) { 1574 if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
1557 /* It is still raw copy of parent, so invalidate 1575 /* It is still raw copy of parent, so invalidate
1558 * destructor and make plain sk_free() */ 1576 * destructor and make plain sk_free() */
1559 newsk->sk_destruct = NULL; 1577 newsk->sk_destruct = NULL;
@@ -1607,7 +1625,7 @@ void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
1607{ 1625{
1608 u32 max_segs = 1; 1626 u32 max_segs = 1;
1609 1627
1610 __sk_dst_set(sk, dst); 1628 sk_dst_set(sk, dst);
1611 sk->sk_route_caps = dst->dev->features; 1629 sk->sk_route_caps = dst->dev->features;
1612 if (sk->sk_route_caps & NETIF_F_GSO) 1630 if (sk->sk_route_caps & NETIF_F_GSO)
1613 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE; 1631 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
@@ -1815,7 +1833,7 @@ static long sock_wait_for_wmem(struct sock *sk, long timeo)
1815{ 1833{
1816 DEFINE_WAIT(wait); 1834 DEFINE_WAIT(wait);
1817 1835
1818 clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); 1836 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1819 for (;;) { 1837 for (;;) {
1820 if (!timeo) 1838 if (!timeo)
1821 break; 1839 break;
@@ -1861,7 +1879,7 @@ struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
1861 if (sk_wmem_alloc_get(sk) < sk->sk_sndbuf) 1879 if (sk_wmem_alloc_get(sk) < sk->sk_sndbuf)
1862 break; 1880 break;
1863 1881
1864 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); 1882 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1865 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1883 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1866 err = -EAGAIN; 1884 err = -EAGAIN;
1867 if (!timeo) 1885 if (!timeo)
@@ -2048,9 +2066,9 @@ int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb)
2048 DEFINE_WAIT(wait); 2066 DEFINE_WAIT(wait);
2049 2067
2050 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 2068 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
2051 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); 2069 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
2052 rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb); 2070 rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb);
2053 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); 2071 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
2054 finish_wait(sk_sleep(sk), &wait); 2072 finish_wait(sk_sleep(sk), &wait);
2055 return rc; 2073 return rc;
2056} 2074}
@@ -2283,7 +2301,7 @@ static void sock_def_wakeup(struct sock *sk)
2283 2301
2284 rcu_read_lock(); 2302 rcu_read_lock();
2285 wq = rcu_dereference(sk->sk_wq); 2303 wq = rcu_dereference(sk->sk_wq);
2286 if (wq_has_sleeper(wq)) 2304 if (skwq_has_sleeper(wq))
2287 wake_up_interruptible_all(&wq->wait); 2305 wake_up_interruptible_all(&wq->wait);
2288 rcu_read_unlock(); 2306 rcu_read_unlock();
2289} 2307}
@@ -2294,7 +2312,7 @@ static void sock_def_error_report(struct sock *sk)
2294 2312
2295 rcu_read_lock(); 2313 rcu_read_lock();
2296 wq = rcu_dereference(sk->sk_wq); 2314 wq = rcu_dereference(sk->sk_wq);
2297 if (wq_has_sleeper(wq)) 2315 if (skwq_has_sleeper(wq))
2298 wake_up_interruptible_poll(&wq->wait, POLLERR); 2316 wake_up_interruptible_poll(&wq->wait, POLLERR);
2299 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR); 2317 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
2300 rcu_read_unlock(); 2318 rcu_read_unlock();
@@ -2306,7 +2324,7 @@ static void sock_def_readable(struct sock *sk)
2306 2324
2307 rcu_read_lock(); 2325 rcu_read_lock();
2308 wq = rcu_dereference(sk->sk_wq); 2326 wq = rcu_dereference(sk->sk_wq);
2309 if (wq_has_sleeper(wq)) 2327 if (skwq_has_sleeper(wq))
2310 wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI | 2328 wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI |
2311 POLLRDNORM | POLLRDBAND); 2329 POLLRDNORM | POLLRDBAND);
2312 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 2330 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
@@ -2324,7 +2342,7 @@ static void sock_def_write_space(struct sock *sk)
2324 */ 2342 */
2325 if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) { 2343 if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
2326 wq = rcu_dereference(sk->sk_wq); 2344 wq = rcu_dereference(sk->sk_wq);
2327 if (wq_has_sleeper(wq)) 2345 if (skwq_has_sleeper(wq))
2328 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | 2346 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
2329 POLLWRNORM | POLLWRBAND); 2347 POLLWRNORM | POLLWRBAND);
2330 2348
@@ -2388,7 +2406,6 @@ void sock_init_data(struct socket *sock, struct sock *sk)
2388 } else 2406 } else
2389 sk->sk_wq = NULL; 2407 sk->sk_wq = NULL;
2390 2408
2391 spin_lock_init(&sk->sk_dst_lock);
2392 rwlock_init(&sk->sk_callback_lock); 2409 rwlock_init(&sk->sk_callback_lock);
2393 lockdep_set_class_and_name(&sk->sk_callback_lock, 2410 lockdep_set_class_and_name(&sk->sk_callback_lock,
2394 af_callback_keys + sk->sk_family, 2411 af_callback_keys + sk->sk_family,