sock.c (db6da59cf27b5661ced03754ae0550f8914eda9e) sock.c (e1d001fa5b477c4da46a29be1fcece91db7c7c6f)
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * Generic socket support routines. Memory allocators, socket lock/release
8 * handler for protocols to use and generic option handler.

--- 100 unchanged lines hidden (view full) ---

109#include <linux/tcp.h>
110#include <linux/init.h>
111#include <linux/highmem.h>
112#include <linux/user_namespace.h>
113#include <linux/static_key.h>
114#include <linux/memcontrol.h>
115#include <linux/prefetch.h>
116#include <linux/compat.h>
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * Generic socket support routines. Memory allocators, socket lock/release
8 * handler for protocols to use and generic option handler.

--- 100 unchanged lines hidden (view full) ---

109#include <linux/tcp.h>
110#include <linux/init.h>
111#include <linux/highmem.h>
112#include <linux/user_namespace.h>
113#include <linux/static_key.h>
114#include <linux/memcontrol.h>
115#include <linux/prefetch.h>
116#include <linux/compat.h>
117#include <linux/mroute.h>
118#include <linux/mroute6.h>
119#include <linux/icmpv6.h>
117
118#include <linux/uaccess.h>
119
120#include <linux/netdevice.h>
121#include <net/protocol.h>
122#include <linux/skbuff.h>
123#include <net/net_namespace.h>
124#include <net/request_sock.h>

--- 8 unchanged lines hidden (view full) ---

133#include <linux/filter.h>
134#include <net/sock_reuseport.h>
135#include <net/bpf_sk_storage.h>
136
137#include <trace/events/sock.h>
138
139#include <net/tcp.h>
140#include <net/busy_poll.h>
120
121#include <linux/uaccess.h>
122
123#include <linux/netdevice.h>
124#include <net/protocol.h>
125#include <linux/skbuff.h>
126#include <net/net_namespace.h>
127#include <net/request_sock.h>

--- 8 unchanged lines hidden (view full) ---

136#include <linux/filter.h>
137#include <net/sock_reuseport.h>
138#include <net/bpf_sk_storage.h>
139
140#include <trace/events/sock.h>
141
142#include <net/tcp.h>
143#include <net/busy_poll.h>
144#include <net/phonet/phonet.h>
141
142#include <linux/ethtool.h>
143
144#include "dev.h"
145
146static DEFINE_MUTEX(proto_list_mutex);
147static LIST_HEAD(proto_list);
148

--- 1092 unchanged lines hidden (view full) ---

1241
1242 case SO_PASSCRED:
1243 if (valbool)
1244 set_bit(SOCK_PASSCRED, &sock->flags);
1245 else
1246 clear_bit(SOCK_PASSCRED, &sock->flags);
1247 break;
1248
145
146#include <linux/ethtool.h>
147
148#include "dev.h"
149
150static DEFINE_MUTEX(proto_list_mutex);
151static LIST_HEAD(proto_list);
152

--- 1092 unchanged lines hidden (view full) ---

1245
1246 case SO_PASSCRED:
1247 if (valbool)
1248 set_bit(SOCK_PASSCRED, &sock->flags);
1249 else
1250 clear_bit(SOCK_PASSCRED, &sock->flags);
1251 break;
1252
1253 case SO_PASSPIDFD:
1254 if (valbool)
1255 set_bit(SOCK_PASSPIDFD, &sock->flags);
1256 else
1257 clear_bit(SOCK_PASSPIDFD, &sock->flags);
1258 break;
1259
1249 case SO_TIMESTAMP_OLD:
1250 case SO_TIMESTAMP_NEW:
1251 case SO_TIMESTAMPNS_OLD:
1252 case SO_TIMESTAMPNS_NEW:
1253 sock_set_timestamp(sk, optname, valbool);
1254 break;
1255
1256 case SO_TIMESTAMPING_NEW:

--- 470 unchanged lines hidden (view full) ---

1727 case SO_SNDLOWAT:
1728 v.val = 1;
1729 break;
1730
1731 case SO_PASSCRED:
1732 v.val = !!test_bit(SOCK_PASSCRED, &sock->flags);
1733 break;
1734
1260 case SO_TIMESTAMP_OLD:
1261 case SO_TIMESTAMP_NEW:
1262 case SO_TIMESTAMPNS_OLD:
1263 case SO_TIMESTAMPNS_NEW:
1264 sock_set_timestamp(sk, optname, valbool);
1265 break;
1266
1267 case SO_TIMESTAMPING_NEW:

--- 470 unchanged lines hidden (view full) ---

1738 case SO_SNDLOWAT:
1739 v.val = 1;
1740 break;
1741
1742 case SO_PASSCRED:
1743 v.val = !!test_bit(SOCK_PASSCRED, &sock->flags);
1744 break;
1745
1746 case SO_PASSPIDFD:
1747 v.val = !!test_bit(SOCK_PASSPIDFD, &sock->flags);
1748 break;
1749
1735 case SO_PEERCRED:
1736 {
1737 struct ucred peercred;
1738 if (len > sizeof(peercred))
1739 len = sizeof(peercred);
1740
1741 spin_lock(&sk->sk_peer_lock);
1742 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
1743 spin_unlock(&sk->sk_peer_lock);
1744
1745 if (copy_to_sockptr(optval, &peercred, len))
1746 return -EFAULT;
1747 goto lenout;
1748 }
1749
1750 case SO_PEERCRED:
1751 {
1752 struct ucred peercred;
1753 if (len > sizeof(peercred))
1754 len = sizeof(peercred);
1755
1756 spin_lock(&sk->sk_peer_lock);
1757 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
1758 spin_unlock(&sk->sk_peer_lock);
1759
1760 if (copy_to_sockptr(optval, &peercred, len))
1761 return -EFAULT;
1762 goto lenout;
1763 }
1764
1765 case SO_PEERPIDFD:
1766 {
1767 struct pid *peer_pid;
1768 struct file *pidfd_file = NULL;
1769 int pidfd;
1770
1771 if (len > sizeof(pidfd))
1772 len = sizeof(pidfd);
1773
1774 spin_lock(&sk->sk_peer_lock);
1775 peer_pid = get_pid(sk->sk_peer_pid);
1776 spin_unlock(&sk->sk_peer_lock);
1777
1778 if (!peer_pid)
1779 return -ESRCH;
1780
1781 pidfd = pidfd_prepare(peer_pid, 0, &pidfd_file);
1782 put_pid(peer_pid);
1783 if (pidfd < 0)
1784 return pidfd;
1785
1786 if (copy_to_sockptr(optval, &pidfd, len) ||
1787 copy_to_sockptr(optlen, &len, sizeof(int))) {
1788 put_unused_fd(pidfd);
1789 fput(pidfd_file);
1790
1791 return -EFAULT;
1792 }
1793
1794 fd_install(pidfd, pidfd_file);
1795 return 0;
1796 }
1797
1750 case SO_PEERGROUPS:
1751 {
1752 const struct cred *cred;
1753 int ret, n;
1754
1755 cred = sk_get_peer_cred(sk);
1756 if (!cred)
1757 return -ENODATA;

--- 618 unchanged lines hidden (view full) ---

2376
2377 return max_size - (MAX_TCP_HEADER + 1);
2378}
2379
2380void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
2381{
2382 u32 max_segs = 1;
2383
1798 case SO_PEERGROUPS:
1799 {
1800 const struct cred *cred;
1801 int ret, n;
1802
1803 cred = sk_get_peer_cred(sk);
1804 if (!cred)
1805 return -ENODATA;

--- 618 unchanged lines hidden (view full) ---

2424
2425 return max_size - (MAX_TCP_HEADER + 1);
2426}
2427
2428void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
2429{
2430 u32 max_segs = 1;
2431
2384 sk_dst_set(sk, dst);
2385 sk->sk_route_caps = dst->dev->features;
2386 if (sk_is_tcp(sk))
2387 sk->sk_route_caps |= NETIF_F_GSO;
2388 if (sk->sk_route_caps & NETIF_F_GSO)
2389 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
2390 if (unlikely(sk->sk_gso_disabled))
2391 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
2392 if (sk_can_gso(sk)) {
2393 if (dst->header_len && !xfrm_dst_offload_ok(dst)) {
2394 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
2395 } else {
2396 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
2397 sk->sk_gso_max_size = sk_dst_gso_max_size(sk, dst);
2398 /* pairs with the WRITE_ONCE() in netif_set_gso_max_segs() */
2399 max_segs = max_t(u32, READ_ONCE(dst->dev->gso_max_segs), 1);
2400 }
2401 }
2402 sk->sk_gso_max_segs = max_segs;
2432 sk->sk_route_caps = dst->dev->features;
2433 if (sk_is_tcp(sk))
2434 sk->sk_route_caps |= NETIF_F_GSO;
2435 if (sk->sk_route_caps & NETIF_F_GSO)
2436 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
2437 if (unlikely(sk->sk_gso_disabled))
2438 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
2439 if (sk_can_gso(sk)) {
2440 if (dst->header_len && !xfrm_dst_offload_ok(dst)) {
2441 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
2442 } else {
2443 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
2444 sk->sk_gso_max_size = sk_dst_gso_max_size(sk, dst);
2445 /* pairs with the WRITE_ONCE() in netif_set_gso_max_segs() */
2446 max_segs = max_t(u32, READ_ONCE(dst->dev->gso_max_segs), 1);
2447 }
2448 }
2449 sk->sk_gso_max_segs = max_segs;
2450 sk_dst_set(sk, dst);
2403}
2404EXPORT_SYMBOL_GPL(sk_setup_caps);
2405
2406/*
2407 * Simple resource managers for sockets.
2408 */
2409
2410

--- 1690 unchanged lines hidden (view full) ---

4101
4102int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len)
4103{
4104 if (!sk->sk_prot->bind_add)
4105 return -EOPNOTSUPP;
4106 return sk->sk_prot->bind_add(sk, addr, addr_len);
4107}
4108EXPORT_SYMBOL(sock_bind_add);
2451}
2452EXPORT_SYMBOL_GPL(sk_setup_caps);
2453
2454/*
2455 * Simple resource managers for sockets.
2456 */
2457
2458

--- 1690 unchanged lines hidden (view full) ---

4149
4150int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len)
4151{
4152 if (!sk->sk_prot->bind_add)
4153 return -EOPNOTSUPP;
4154 return sk->sk_prot->bind_add(sk, addr, addr_len);
4155}
4156EXPORT_SYMBOL(sock_bind_add);
4157
4158/* Copy 'size' bytes from userspace and return `size` back to userspace */
4159int sock_ioctl_inout(struct sock *sk, unsigned int cmd,
4160 void __user *arg, void *karg, size_t size)
4161{
4162 int ret;
4163
4164 if (copy_from_user(karg, arg, size))
4165 return -EFAULT;
4166
4167 ret = READ_ONCE(sk->sk_prot)->ioctl(sk, cmd, karg);
4168 if (ret)
4169 return ret;
4170
4171 if (copy_to_user(arg, karg, size))
4172 return -EFAULT;
4173
4174 return 0;
4175}
4176EXPORT_SYMBOL(sock_ioctl_inout);
4177
4178/* This is the most common ioctl prep function, where the result (4 bytes) is
4179 * copied back to userspace if the ioctl() returns successfully. No input is
4180 * copied from userspace as input argument.
4181 */
4182static int sock_ioctl_out(struct sock *sk, unsigned int cmd, void __user *arg)
4183{
4184 int ret, karg = 0;
4185
4186 ret = READ_ONCE(sk->sk_prot)->ioctl(sk, cmd, &karg);
4187 if (ret)
4188 return ret;
4189
4190 return put_user(karg, (int __user *)arg);
4191}
4192
4193/* A wrapper around sock ioctls, which copies the data from userspace
4194 * (depending on the protocol/ioctl), and copies back the result to userspace.
4195 * The main motivation for this function is to pass kernel memory to the
4196 * protocol ioctl callbacks, instead of userspace memory.
4197 */
4198int sk_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
4199{
4200 int rc = 1;
4201
4202 if (sk_is_ipmr(sk))
4203 rc = ipmr_sk_ioctl(sk, cmd, arg);
4204 else if (sk_is_icmpv6(sk))
4205 rc = ip6mr_sk_ioctl(sk, cmd, arg);
4206 else if (sk_is_phonet(sk))
4207 rc = phonet_sk_ioctl(sk, cmd, arg);
4208
4209 /* If ioctl was processed, returns its value */
4210 if (rc <= 0)
4211 return rc;
4212
4213 /* Otherwise call the default handler */
4214 return sock_ioctl_out(sk, cmd, arg);
4215}
4216EXPORT_SYMBOL(sk_ioctl);