Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * The IP to API glue. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Authors: see ip.c |
| 10 | * |
| 11 | * Fixes: |
| 12 | * Many : Split from ip.c , see ip.c for history. |
| 13 | * Martin Mares : TOS setting fixed. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 14 | * Alan Cox : Fixed a couple of oopses in Martin's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * TOS tweaks. |
| 16 | * Mike McLagan : Routing by source |
| 17 | */ |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/ip.h> |
| 24 | #include <linux/icmp.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 25 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <net/sock.h> |
| 29 | #include <net/ip.h> |
| 30 | #include <net/icmp.h> |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 31 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/udp.h> |
| 33 | #include <linux/igmp.h> |
| 34 | #include <linux/netfilter.h> |
| 35 | #include <linux/route.h> |
| 36 | #include <linux/mroute.h> |
Maciej Żenczykowski | 2c67e9a | 2011-10-22 00:07:47 -0400 | [diff] [blame] | 37 | #include <net/inet_ecn.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <net/route.h> |
| 39 | #include <net/xfrm.h> |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 40 | #include <net/compat.h> |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 41 | #include <net/checksum.h> |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 42 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <net/transp_v6.h> |
| 44 | #endif |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 45 | #include <net/ip_fib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #include <linux/errqueue.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 48 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Alexei Starovoitov | d2ba09c | 2018-05-21 19:22:30 -0700 | [diff] [blame] | 50 | #include <linux/bpfilter.h> |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
| 53 | * SOL_IP control messages. |
| 54 | */ |
| 55 | |
| 56 | static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) |
| 57 | { |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 58 | struct in_pktinfo info = *PKTINFO_SKB_CB(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 60 | info.ipi_addr.s_addr = ip_hdr(skb)->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 63 | } |
| 64 | |
| 65 | static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) |
| 66 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 67 | int ttl = ip_hdr(skb)->ttl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); |
| 69 | } |
| 70 | |
| 71 | static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) |
| 72 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 73 | put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) |
| 77 | { |
| 78 | if (IPCB(skb)->opt.optlen == 0) |
| 79 | return; |
| 80 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 81 | put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, |
| 82 | ip_hdr(skb) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Paolo Abeni | 91ed1e6 | 2017-08-03 18:07:06 +0200 | [diff] [blame] | 86 | static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg, |
| 87 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | unsigned char optbuf[sizeof(struct ip_options) + 40]; |
Daniel Baluta | 5e73ea1 | 2012-04-15 01:34:41 +0000 | [diff] [blame] | 90 | struct ip_options *opt = (struct ip_options *)optbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | if (IPCB(skb)->opt.optlen == 0) |
| 93 | return; |
| 94 | |
Paolo Abeni | 91ed1e6 | 2017-08-03 18:07:06 +0200 | [diff] [blame] | 95 | if (ip_options_echo(net, opt, skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | msg->msg_flags |= MSG_CTRUNC; |
| 97 | return; |
| 98 | } |
| 99 | ip_options_undo(opt); |
| 100 | |
| 101 | put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data); |
| 102 | } |
| 103 | |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 104 | static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb) |
| 105 | { |
| 106 | int val; |
| 107 | |
| 108 | if (IPCB(skb)->frag_max_size == 0) |
| 109 | return; |
| 110 | |
| 111 | val = IPCB(skb)->frag_max_size; |
| 112 | put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val); |
| 113 | } |
| 114 | |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 115 | static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb, |
Eric Dumazet | 10df8e6 | 2016-10-23 18:03:06 -0700 | [diff] [blame] | 116 | int tlen, int offset) |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 117 | { |
| 118 | __wsum csum = skb->csum; |
| 119 | |
| 120 | if (skb->ip_summed != CHECKSUM_COMPLETE) |
| 121 | return; |
| 122 | |
Paolo Abeni | ca4ef45 | 2017-02-21 09:33:18 +0100 | [diff] [blame] | 123 | if (offset != 0) { |
| 124 | int tend_off = skb_transport_offset(skb) + tlen; |
| 125 | csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0)); |
| 126 | } |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 127 | |
| 128 | put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum); |
| 129 | } |
| 130 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 131 | static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb) |
| 132 | { |
| 133 | char *secdata; |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 134 | u32 seclen, secid; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 135 | int err; |
| 136 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 137 | err = security_socket_getpeersec_dgram(NULL, skb, &secid); |
| 138 | if (err) |
| 139 | return; |
| 140 | |
| 141 | err = security_secid_to_secctx(secid, &secdata, &seclen); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 142 | if (err) |
| 143 | return; |
| 144 | |
| 145 | put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 146 | security_release_secctx(secdata, seclen); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Harvey Harrison | 21d1a16 | 2008-11-20 01:54:27 -0800 | [diff] [blame] | 149 | static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb) |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 150 | { |
Willem de Bruijn | 4a06fa6 | 2019-01-07 16:47:33 -0500 | [diff] [blame] | 151 | __be16 _ports[2], *ports; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 152 | struct sockaddr_in sin; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 153 | |
| 154 | /* All current transport protocols have the port numbers in the |
| 155 | * first four bytes of the transport header and this function is |
| 156 | * written with this assumption in mind. |
| 157 | */ |
Willem de Bruijn | 4a06fa6 | 2019-01-07 16:47:33 -0500 | [diff] [blame] | 158 | ports = skb_header_pointer(skb, skb_transport_offset(skb), |
| 159 | sizeof(_ports), &_ports); |
| 160 | if (!ports) |
| 161 | return; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 162 | |
| 163 | sin.sin_family = AF_INET; |
Eric Dumazet | 64199fc | 2018-09-30 11:33:39 -0700 | [diff] [blame] | 164 | sin.sin_addr.s_addr = ip_hdr(skb)->daddr; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 165 | sin.sin_port = ports[1]; |
| 166 | memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); |
| 167 | |
| 168 | put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin); |
| 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Paolo Abeni | ad95903 | 2016-11-04 11:28:58 +0100 | [diff] [blame] | 171 | void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk, |
| 172 | struct sk_buff *skb, int tlen, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Paolo Abeni | ad95903 | 2016-11-04 11:28:58 +0100 | [diff] [blame] | 174 | struct inet_sock *inet = inet_sk(sk); |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 175 | unsigned int flags = inet->cmsg_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | /* Ordered by supposed usage frequency */ |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 178 | if (flags & IP_CMSG_PKTINFO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | ip_cmsg_recv_pktinfo(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 181 | flags &= ~IP_CMSG_PKTINFO; |
| 182 | if (!flags) |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if (flags & IP_CMSG_TTL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | ip_cmsg_recv_ttl(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 189 | flags &= ~IP_CMSG_TTL; |
| 190 | if (!flags) |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | if (flags & IP_CMSG_TOS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | ip_cmsg_recv_tos(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 197 | flags &= ~IP_CMSG_TOS; |
| 198 | if (!flags) |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | if (flags & IP_CMSG_RECVOPTS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | ip_cmsg_recv_opts(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 205 | flags &= ~IP_CMSG_RECVOPTS; |
| 206 | if (!flags) |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | if (flags & IP_CMSG_RETOPTS) { |
Paolo Abeni | 91ed1e6 | 2017-08-03 18:07:06 +0200 | [diff] [blame] | 211 | ip_cmsg_recv_retopts(sock_net(sk), msg, skb); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 212 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 213 | flags &= ~IP_CMSG_RETOPTS; |
| 214 | if (!flags) |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | if (flags & IP_CMSG_PASSSEC) { |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 219 | ip_cmsg_recv_security(msg, skb); |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 220 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 221 | flags &= ~IP_CMSG_PASSSEC; |
| 222 | if (!flags) |
| 223 | return; |
| 224 | } |
| 225 | |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 226 | if (flags & IP_CMSG_ORIGDSTADDR) { |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 227 | ip_cmsg_recv_dstaddr(msg, skb); |
| 228 | |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 229 | flags &= ~IP_CMSG_ORIGDSTADDR; |
| 230 | if (!flags) |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (flags & IP_CMSG_CHECKSUM) |
Eric Dumazet | 10df8e6 | 2016-10-23 18:03:06 -0700 | [diff] [blame] | 235 | ip_cmsg_recv_checksum(msg, skb, tlen, offset); |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 236 | |
| 237 | if (flags & IP_CMSG_RECVFRAGSIZE) |
| 238 | ip_cmsg_recv_fragsize(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Tom Herbert | 5961de9 | 2015-01-05 13:56:16 -0800 | [diff] [blame] | 240 | EXPORT_SYMBOL(ip_cmsg_recv_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 242 | int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc, |
Hannes Frederic Sowa | c8e6ad0 | 2014-02-18 21:38:08 +0100 | [diff] [blame] | 243 | bool allow_ipv6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Francesco Fusco | f02db31 | 2013-09-24 15:43:08 +0200 | [diff] [blame] | 245 | int err, val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | struct cmsghdr *cmsg; |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 247 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Gu Zheng | f95b414 | 2014-12-11 11:22:04 +0800 | [diff] [blame] | 249 | for_each_cmsghdr(cmsg, msg) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | if (!CMSG_OK(msg, cmsg)) |
| 251 | return -EINVAL; |
Eric Dumazet | 5337b5b | 2014-11-10 17:54:25 -0800 | [diff] [blame] | 252 | #if IS_ENABLED(CONFIG_IPV6) |
Hannes Frederic Sowa | c8e6ad0 | 2014-02-18 21:38:08 +0100 | [diff] [blame] | 253 | if (allow_ipv6 && |
| 254 | cmsg->cmsg_level == SOL_IPV6 && |
| 255 | cmsg->cmsg_type == IPV6_PKTINFO) { |
| 256 | struct in6_pktinfo *src_info; |
| 257 | |
| 258 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info))) |
| 259 | return -EINVAL; |
| 260 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); |
| 261 | if (!ipv6_addr_v4mapped(&src_info->ipi6_addr)) |
| 262 | return -EINVAL; |
David Ahern | 1cbec07 | 2018-02-16 11:03:03 -0800 | [diff] [blame] | 263 | if (src_info->ipi6_ifindex) |
| 264 | ipc->oif = src_info->ipi6_ifindex; |
Hannes Frederic Sowa | c8e6ad0 | 2014-02-18 21:38:08 +0100 | [diff] [blame] | 265 | ipc->addr = src_info->ipi6_addr.s6_addr32[3]; |
| 266 | continue; |
| 267 | } |
| 268 | #endif |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 269 | if (cmsg->cmsg_level == SOL_SOCKET) { |
Eric Dumazet | 2632616 | 2016-05-13 06:14:37 -0700 | [diff] [blame] | 270 | err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc); |
| 271 | if (err) |
| 272 | return err; |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 273 | continue; |
| 274 | } |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | if (cmsg->cmsg_level != SOL_IP) |
| 277 | continue; |
| 278 | switch (cmsg->cmsg_type) { |
| 279 | case IP_RETOPTS: |
yuan linyu | 1ff8ceb | 2017-01-03 20:42:17 +0800 | [diff] [blame] | 280 | err = cmsg->cmsg_len - sizeof(struct cmsghdr); |
Eric Dumazet | 9194830 | 2016-02-04 06:23:28 -0800 | [diff] [blame] | 281 | |
| 282 | /* Our caller is responsible for freeing ipc->opt */ |
Christoph Hellwig | de40a3e | 2020-07-23 08:08:57 +0200 | [diff] [blame] | 283 | err = ip_options_get(net, &ipc->opt, |
| 284 | KERNEL_SOCKPTR(CMSG_DATA(cmsg)), |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 285 | err < 40 ? err : 40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (err) |
| 287 | return err; |
| 288 | break; |
| 289 | case IP_PKTINFO: |
| 290 | { |
| 291 | struct in_pktinfo *info; |
| 292 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo))) |
| 293 | return -EINVAL; |
| 294 | info = (struct in_pktinfo *)CMSG_DATA(cmsg); |
David Ahern | 1cbec07 | 2018-02-16 11:03:03 -0800 | [diff] [blame] | 295 | if (info->ipi_ifindex) |
| 296 | ipc->oif = info->ipi_ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | ipc->addr = info->ipi_spec_dst.s_addr; |
| 298 | break; |
| 299 | } |
Francesco Fusco | f02db31 | 2013-09-24 15:43:08 +0200 | [diff] [blame] | 300 | case IP_TTL: |
| 301 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) |
| 302 | return -EINVAL; |
| 303 | val = *(int *)CMSG_DATA(cmsg); |
| 304 | if (val < 1 || val > 255) |
| 305 | return -EINVAL; |
| 306 | ipc->ttl = val; |
| 307 | break; |
| 308 | case IP_TOS: |
Eric Dumazet | e895cdc | 2016-09-07 21:52:56 -0700 | [diff] [blame] | 309 | if (cmsg->cmsg_len == CMSG_LEN(sizeof(int))) |
| 310 | val = *(int *)CMSG_DATA(cmsg); |
| 311 | else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8))) |
| 312 | val = *(u8 *)CMSG_DATA(cmsg); |
| 313 | else |
Francesco Fusco | f02db31 | 2013-09-24 15:43:08 +0200 | [diff] [blame] | 314 | return -EINVAL; |
Francesco Fusco | f02db31 | 2013-09-24 15:43:08 +0200 | [diff] [blame] | 315 | if (val < 0 || val > 255) |
| 316 | return -EINVAL; |
| 317 | ipc->tos = val; |
| 318 | ipc->priority = rt_tos2priority(ipc->tos); |
| 319 | break; |
| 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | default: |
| 322 | return -EINVAL; |
| 323 | } |
| 324 | } |
| 325 | return 0; |
| 326 | } |
| 327 | |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 328 | static void ip_ra_destroy_rcu(struct rcu_head *head) |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 329 | { |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 330 | struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu); |
| 331 | |
| 332 | sock_put(ra->saved_sk); |
| 333 | kfree(ra); |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 336 | int ip_ra_control(struct sock *sk, unsigned char on, |
| 337 | void (*destructor)(struct sock *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
Eric Dumazet | 43a951e | 2010-10-25 03:32:44 +0000 | [diff] [blame] | 339 | struct ip_ra_chain *ra, *new_ra; |
| 340 | struct ip_ra_chain __rcu **rap; |
Kirill Tkhai | 5796ef7 | 2018-03-22 12:45:32 +0300 | [diff] [blame] | 341 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 343 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | return -EINVAL; |
| 345 | |
| 346 | new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; |
Gen Zhang | 425aa0e | 2019-05-24 11:24:26 +0800 | [diff] [blame] | 347 | if (on && !new_ra) |
| 348 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 350 | mutex_lock(&net->ipv4.ra_mutex); |
Kirill Tkhai | 5796ef7 | 2018-03-22 12:45:32 +0300 | [diff] [blame] | 351 | for (rap = &net->ipv4.ra_chain; |
Kirill Tkhai | 76d3e15 | 2018-03-22 12:45:02 +0300 | [diff] [blame] | 352 | (ra = rcu_dereference_protected(*rap, |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 353 | lockdep_is_held(&net->ipv4.ra_mutex))) != NULL; |
Eric Dumazet | 43a951e | 2010-10-25 03:32:44 +0000 | [diff] [blame] | 354 | rap = &ra->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (ra->sk == sk) { |
| 356 | if (on) { |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 357 | mutex_unlock(&net->ipv4.ra_mutex); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 358 | kfree(new_ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | return -EADDRINUSE; |
| 360 | } |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 361 | /* dont let ip_call_ra_chain() use sk again */ |
| 362 | ra->sk = NULL; |
Eric Dumazet | 8e380f0 | 2014-09-09 08:11:41 -0700 | [diff] [blame] | 363 | RCU_INIT_POINTER(*rap, ra->next); |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 364 | mutex_unlock(&net->ipv4.ra_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | if (ra->destructor) |
| 367 | ra->destructor(sk); |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 368 | /* |
| 369 | * Delay sock_put(sk) and kfree(ra) after one rcu grace |
| 370 | * period. This guarantee ip_call_ra_chain() dont need |
| 371 | * to mess with socket refcounts. |
| 372 | */ |
| 373 | ra->saved_sk = sk; |
| 374 | call_rcu(&ra->rcu, ip_ra_destroy_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | } |
Kirill Tkhai | 76d3e15 | 2018-03-22 12:45:02 +0300 | [diff] [blame] | 378 | if (!new_ra) { |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 379 | mutex_unlock(&net->ipv4.ra_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return -ENOBUFS; |
Kirill Tkhai | 76d3e15 | 2018-03-22 12:45:02 +0300 | [diff] [blame] | 381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | new_ra->sk = sk; |
| 383 | new_ra->destructor = destructor; |
| 384 | |
Eric Dumazet | 8e380f0 | 2014-09-09 08:11:41 -0700 | [diff] [blame] | 385 | RCU_INIT_POINTER(new_ra->next, ra); |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 386 | rcu_assign_pointer(*rap, new_ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | sock_hold(sk); |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 388 | mutex_unlock(&net->ipv4.ra_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Willem de Bruijn | 178c49d | 2020-07-24 09:03:09 -0400 | [diff] [blame] | 393 | static void ipv4_icmp_error_rfc4884(const struct sk_buff *skb, |
| 394 | struct sock_ee_data_rfc4884 *out) |
| 395 | { |
| 396 | switch (icmp_hdr(skb)->type) { |
| 397 | case ICMP_DEST_UNREACH: |
| 398 | case ICMP_TIME_EXCEEDED: |
| 399 | case ICMP_PARAMETERPROB: |
| 400 | ip_icmp_error_rfc4884(skb, out, sizeof(struct icmphdr), |
| 401 | icmp_hdr(skb)->un.reserved[1] * 4); |
| 402 | } |
| 403 | } |
| 404 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 405 | void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
Al Viro | 35986b3 | 2006-09-27 18:34:21 -0700 | [diff] [blame] | 406 | __be16 port, u32 info, u8 *payload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | struct sock_exterr_skb *serr; |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | skb = skb_clone(skb, GFP_ATOMIC); |
| 411 | if (!skb) |
| 412 | return; |
| 413 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 414 | serr = SKB_EXT_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | serr->ee.ee_errno = err; |
| 416 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 417 | serr->ee.ee_type = icmp_hdr(skb)->type; |
| 418 | serr->ee.ee_code = icmp_hdr(skb)->code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | serr->ee.ee_pad = 0; |
| 420 | serr->ee.ee_info = info; |
| 421 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 422 | serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 423 | skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | serr->port = port; |
| 425 | |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 426 | if (skb_pull(skb, payload - skb->data)) { |
Willem de Bruijn | eba75c5 | 2020-07-10 09:29:02 -0400 | [diff] [blame] | 427 | if (inet_sk(sk)->recverr_rfc4884) |
Willem de Bruijn | 178c49d | 2020-07-24 09:03:09 -0400 | [diff] [blame] | 428 | ipv4_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884); |
Willem de Bruijn | eba75c5 | 2020-07-10 09:29:02 -0400 | [diff] [blame] | 429 | |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 430 | skb_reset_transport_header(skb); |
| 431 | if (sock_queue_err_skb(sk, skb) == 0) |
| 432 | return; |
| 433 | } |
| 434 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Al Viro | 0579016 | 2006-09-27 18:33:40 -0700 | [diff] [blame] | 437 | void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
| 439 | struct inet_sock *inet = inet_sk(sk); |
| 440 | struct sock_exterr_skb *serr; |
| 441 | struct iphdr *iph; |
| 442 | struct sk_buff *skb; |
| 443 | |
| 444 | if (!inet->recverr) |
| 445 | return; |
| 446 | |
| 447 | skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); |
| 448 | if (!skb) |
| 449 | return; |
| 450 | |
Arnaldo Carvalho de Melo | 2ca9e6f | 2007-03-10 19:15:25 -0300 | [diff] [blame] | 451 | skb_put(skb, sizeof(struct iphdr)); |
| 452 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 453 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | iph->daddr = daddr; |
| 455 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 456 | serr = SKB_EXT_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | serr->ee.ee_errno = err; |
| 458 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 459 | serr->ee.ee_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | serr->ee.ee_code = 0; |
| 461 | serr->ee.ee_pad = 0; |
| 462 | serr->ee.ee_info = info; |
| 463 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 464 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | serr->port = port; |
| 466 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 467 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 468 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
| 470 | if (sock_queue_err_skb(sk, skb)) |
| 471 | kfree_skb(skb); |
| 472 | } |
| 473 | |
Julian Anastasov | 34b99df | 2015-06-23 08:34:39 +0300 | [diff] [blame] | 474 | /* For some errors we have valid addr_offset even with zero payload and |
| 475 | * zero port. Also, addr_offset should be supported if port is set. |
| 476 | */ |
| 477 | static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr) |
| 478 | { |
| 479 | return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || |
| 480 | serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port; |
| 481 | } |
| 482 | |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 483 | /* IPv4 supports cmsg on all imcp errors and some timestamps |
| 484 | * |
| 485 | * Timestamp code paths do not initialize the fields expected by cmsg: |
| 486 | * the PKTINFO fields in skb->cb[]. Fill those in here. |
| 487 | */ |
| 488 | static bool ipv4_datagram_support_cmsg(const struct sock *sk, |
| 489 | struct sk_buff *skb, |
| 490 | int ee_origin) |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 491 | { |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 492 | struct in_pktinfo *info; |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 493 | |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 494 | if (ee_origin == SO_EE_ORIGIN_ICMP) |
| 495 | return true; |
| 496 | |
| 497 | if (ee_origin == SO_EE_ORIGIN_LOCAL) |
| 498 | return false; |
| 499 | |
| 500 | /* Support IP_PKTINFO on tstamp packets if requested, to correlate |
Willem de Bruijn | 1862d62 | 2017-04-12 19:24:35 -0400 | [diff] [blame] | 501 | * timestamp with egress dev. Not possible for packets without iif |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 502 | * or without payload (SOF_TIMESTAMPING_OPT_TSONLY). |
| 503 | */ |
Willem de Bruijn | 1862d62 | 2017-04-12 19:24:35 -0400 | [diff] [blame] | 504 | info = PKTINFO_SKB_CB(skb); |
| 505 | if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) || |
| 506 | !info->ipi_ifindex) |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 507 | return false; |
| 508 | |
| 509 | info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr; |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 510 | return true; |
| 511 | } |
| 512 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 513 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | * Handle MSG_ERRQUEUE |
| 515 | */ |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 516 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
| 518 | struct sock_exterr_skb *serr; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 519 | struct sk_buff *skb; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 520 | DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | struct { |
| 522 | struct sock_extended_err ee; |
| 523 | struct sockaddr_in offender; |
| 524 | } errhdr; |
| 525 | int err; |
| 526 | int copied; |
| 527 | |
| 528 | err = -EAGAIN; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 529 | skb = sock_dequeue_err_skb(sk); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 530 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | goto out; |
| 532 | |
| 533 | copied = skb->len; |
| 534 | if (copied > len) { |
| 535 | msg->msg_flags |= MSG_TRUNC; |
| 536 | copied = len; |
| 537 | } |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 538 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Eric Dumazet | 960a262 | 2016-04-21 22:27:32 -0700 | [diff] [blame] | 539 | if (unlikely(err)) { |
| 540 | kfree_skb(skb); |
| 541 | return err; |
| 542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | sock_recv_timestamp(msg, sk, skb); |
| 544 | |
| 545 | serr = SKB_EXT_ERR(skb); |
| 546 | |
Julian Anastasov | 34b99df | 2015-06-23 08:34:39 +0300 | [diff] [blame] | 547 | if (sin && ipv4_datagram_support_addr(serr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 549 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
| 550 | serr->addr_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | sin->sin_port = serr->port; |
| 552 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 553 | *addr_len = sizeof(*sin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); |
| 557 | sin = &errhdr.offender; |
Willem de Bruijn | f812116 | 2015-01-15 13:18:40 -0500 | [diff] [blame] | 558 | memset(sin, 0, sizeof(*sin)); |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 559 | |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 560 | if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 562 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
Willem de Bruijn | f812116 | 2015-01-15 13:18:40 -0500 | [diff] [blame] | 563 | if (inet_sk(sk)->cmsg_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | ip_cmsg_recv(msg, skb); |
| 565 | } |
| 566 | |
| 567 | put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); |
| 568 | |
| 569 | /* Now we could try to dump offended packet options */ |
| 570 | |
| 571 | msg->msg_flags |= MSG_ERRQUEUE; |
| 572 | err = copied; |
| 573 | |
Eric Dumazet | 960a262 | 2016-04-21 22:27:32 -0700 | [diff] [blame] | 574 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | out: |
| 576 | return err; |
| 577 | } |
| 578 | |
Poorva Sonparote | 4f47d5d | 2021-11-19 12:41:34 -0800 | [diff] [blame] | 579 | void __ip_sock_set_tos(struct sock *sk, int val) |
Christoph Hellwig | 6ebf71b | 2020-05-28 07:12:26 +0200 | [diff] [blame] | 580 | { |
| 581 | if (sk->sk_type == SOCK_STREAM) { |
| 582 | val &= ~INET_ECN_MASK; |
| 583 | val |= inet_sk(sk)->tos & INET_ECN_MASK; |
| 584 | } |
| 585 | if (inet_sk(sk)->tos != val) { |
| 586 | inet_sk(sk)->tos = val; |
| 587 | sk->sk_priority = rt_tos2priority(val); |
| 588 | sk_dst_reset(sk); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | void ip_sock_set_tos(struct sock *sk, int val) |
| 593 | { |
| 594 | lock_sock(sk); |
| 595 | __ip_sock_set_tos(sk, val); |
| 596 | release_sock(sk); |
| 597 | } |
| 598 | EXPORT_SYMBOL(ip_sock_set_tos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Christoph Hellwig | c4e446b | 2020-05-28 07:12:27 +0200 | [diff] [blame] | 600 | void ip_sock_set_freebind(struct sock *sk) |
| 601 | { |
| 602 | lock_sock(sk); |
| 603 | inet_sk(sk)->freebind = true; |
| 604 | release_sock(sk); |
| 605 | } |
| 606 | EXPORT_SYMBOL(ip_sock_set_freebind); |
| 607 | |
Christoph Hellwig | db45c0e | 2020-05-28 07:12:28 +0200 | [diff] [blame] | 608 | void ip_sock_set_recverr(struct sock *sk) |
| 609 | { |
| 610 | lock_sock(sk); |
| 611 | inet_sk(sk)->recverr = true; |
| 612 | release_sock(sk); |
| 613 | } |
| 614 | EXPORT_SYMBOL(ip_sock_set_recverr); |
| 615 | |
Christoph Hellwig | 2de569b | 2020-05-28 07:12:29 +0200 | [diff] [blame] | 616 | int ip_sock_set_mtu_discover(struct sock *sk, int val) |
| 617 | { |
| 618 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT) |
| 619 | return -EINVAL; |
| 620 | lock_sock(sk); |
| 621 | inet_sk(sk)->pmtudisc = val; |
| 622 | release_sock(sk); |
| 623 | return 0; |
| 624 | } |
| 625 | EXPORT_SYMBOL(ip_sock_set_mtu_discover); |
| 626 | |
Christoph Hellwig | c1f9ec5 | 2020-05-28 07:12:30 +0200 | [diff] [blame] | 627 | void ip_sock_set_pktinfo(struct sock *sk) |
| 628 | { |
| 629 | lock_sock(sk); |
| 630 | inet_sk(sk)->cmsg_flags |= IP_CMSG_PKTINFO; |
| 631 | release_sock(sk); |
| 632 | } |
| 633 | EXPORT_SYMBOL(ip_sock_set_pktinfo); |
| 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 636 | * Socket option code for IP. This is the end of the line after any |
| 637 | * TCP,UDP etc options on an IP socket. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | */ |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 639 | static bool setsockopt_needs_rtnl(int optname) |
| 640 | { |
| 641 | switch (optname) { |
| 642 | case IP_ADD_MEMBERSHIP: |
| 643 | case IP_ADD_SOURCE_MEMBERSHIP: |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 644 | case IP_BLOCK_SOURCE: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 645 | case IP_DROP_MEMBERSHIP: |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 646 | case IP_DROP_SOURCE_MEMBERSHIP: |
| 647 | case IP_MSFILTER: |
| 648 | case IP_UNBLOCK_SOURCE: |
| 649 | case MCAST_BLOCK_SOURCE: |
| 650 | case MCAST_MSFILTER: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 651 | case MCAST_JOIN_GROUP: |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 652 | case MCAST_JOIN_SOURCE_GROUP: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 653 | case MCAST_LEAVE_GROUP: |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 654 | case MCAST_LEAVE_SOURCE_GROUP: |
| 655 | case MCAST_UNBLOCK_SOURCE: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 656 | return true; |
| 657 | } |
| 658 | return false; |
| 659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Al Viro | e986d4d | 2020-03-29 22:37:56 -0400 | [diff] [blame] | 661 | static int set_mcast_msfilter(struct sock *sk, int ifindex, |
| 662 | int numsrc, int fmode, |
| 663 | struct sockaddr_storage *group, |
| 664 | struct sockaddr_storage *list) |
| 665 | { |
Al Viro | e986d4d | 2020-03-29 22:37:56 -0400 | [diff] [blame] | 666 | struct ip_msfilter *msf; |
| 667 | struct sockaddr_in *psin; |
| 668 | int err, i; |
| 669 | |
Gustavo A. R. Silva | 4167a96 | 2021-08-04 13:23:25 -0500 | [diff] [blame] | 670 | msf = kmalloc(IP_MSFILTER_SIZE(numsrc), GFP_KERNEL); |
Al Viro | e986d4d | 2020-03-29 22:37:56 -0400 | [diff] [blame] | 671 | if (!msf) |
| 672 | return -ENOBUFS; |
| 673 | |
| 674 | psin = (struct sockaddr_in *)group; |
| 675 | if (psin->sin_family != AF_INET) |
| 676 | goto Eaddrnotavail; |
| 677 | msf->imsf_multiaddr = psin->sin_addr.s_addr; |
| 678 | msf->imsf_interface = 0; |
| 679 | msf->imsf_fmode = fmode; |
| 680 | msf->imsf_numsrc = numsrc; |
| 681 | for (i = 0; i < numsrc; ++i) { |
| 682 | psin = (struct sockaddr_in *)&list[i]; |
| 683 | |
| 684 | if (psin->sin_family != AF_INET) |
| 685 | goto Eaddrnotavail; |
Gustavo A. R. Silva | 2d3e5ca | 2021-07-31 12:08:30 -0500 | [diff] [blame] | 686 | msf->imsf_slist_flex[i] = psin->sin_addr.s_addr; |
Al Viro | e986d4d | 2020-03-29 22:37:56 -0400 | [diff] [blame] | 687 | } |
| 688 | err = ip_mc_msfilter(sk, msf, ifindex); |
| 689 | kfree(msf); |
| 690 | return err; |
| 691 | |
| 692 | Eaddrnotavail: |
| 693 | kfree(msf); |
| 694 | return -EADDRNOTAVAIL; |
| 695 | } |
| 696 | |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 697 | static int copy_group_source_from_sockptr(struct group_source_req *greqs, |
| 698 | sockptr_t optval, int optlen) |
Al Viro | 2bbf8c1 | 2020-04-27 10:49:26 -0400 | [diff] [blame] | 699 | { |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 700 | if (in_compat_syscall()) { |
| 701 | struct compat_group_source_req gr32; |
| 702 | |
| 703 | if (optlen != sizeof(gr32)) |
| 704 | return -EINVAL; |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 705 | if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 706 | return -EFAULT; |
| 707 | greqs->gsr_interface = gr32.gsr_interface; |
| 708 | greqs->gsr_group = gr32.gsr_group; |
| 709 | greqs->gsr_source = gr32.gsr_source; |
| 710 | } else { |
| 711 | if (optlen != sizeof(*greqs)) |
| 712 | return -EINVAL; |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 713 | if (copy_from_sockptr(greqs, optval, sizeof(*greqs))) |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 714 | return -EFAULT; |
| 715 | } |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | static int do_mcast_group_source(struct sock *sk, int optname, |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 721 | sockptr_t optval, int optlen) |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 722 | { |
| 723 | struct group_source_req greqs; |
Al Viro | 2bbf8c1 | 2020-04-27 10:49:26 -0400 | [diff] [blame] | 724 | struct ip_mreq_source mreqs; |
| 725 | struct sockaddr_in *psin; |
| 726 | int omode, add, err; |
| 727 | |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 728 | err = copy_group_source_from_sockptr(&greqs, optval, optlen); |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 729 | if (err) |
| 730 | return err; |
| 731 | |
| 732 | if (greqs.gsr_group.ss_family != AF_INET || |
| 733 | greqs.gsr_source.ss_family != AF_INET) |
Al Viro | 2bbf8c1 | 2020-04-27 10:49:26 -0400 | [diff] [blame] | 734 | return -EADDRNOTAVAIL; |
| 735 | |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 736 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
Al Viro | 2bbf8c1 | 2020-04-27 10:49:26 -0400 | [diff] [blame] | 737 | mreqs.imr_multiaddr = psin->sin_addr.s_addr; |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 738 | psin = (struct sockaddr_in *)&greqs.gsr_source; |
Al Viro | 2bbf8c1 | 2020-04-27 10:49:26 -0400 | [diff] [blame] | 739 | mreqs.imr_sourceaddr = psin->sin_addr.s_addr; |
| 740 | mreqs.imr_interface = 0; /* use index for mc_source */ |
| 741 | |
| 742 | if (optname == MCAST_BLOCK_SOURCE) { |
| 743 | omode = MCAST_EXCLUDE; |
| 744 | add = 1; |
| 745 | } else if (optname == MCAST_UNBLOCK_SOURCE) { |
| 746 | omode = MCAST_EXCLUDE; |
| 747 | add = 0; |
| 748 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { |
| 749 | struct ip_mreqn mreq; |
| 750 | |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 751 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
Al Viro | 2bbf8c1 | 2020-04-27 10:49:26 -0400 | [diff] [blame] | 752 | mreq.imr_multiaddr = psin->sin_addr; |
| 753 | mreq.imr_address.s_addr = 0; |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 754 | mreq.imr_ifindex = greqs.gsr_interface; |
Al Viro | 2bbf8c1 | 2020-04-27 10:49:26 -0400 | [diff] [blame] | 755 | err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); |
| 756 | if (err && err != -EADDRINUSE) |
| 757 | return err; |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 758 | greqs.gsr_interface = mreq.imr_ifindex; |
Al Viro | 2bbf8c1 | 2020-04-27 10:49:26 -0400 | [diff] [blame] | 759 | omode = MCAST_INCLUDE; |
| 760 | add = 1; |
| 761 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { |
| 762 | omode = MCAST_INCLUDE; |
| 763 | add = 0; |
| 764 | } |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 765 | return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface); |
Al Viro | 2bbf8c1 | 2020-04-27 10:49:26 -0400 | [diff] [blame] | 766 | } |
| 767 | |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 768 | static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen) |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 769 | { |
| 770 | struct group_filter *gsf = NULL; |
| 771 | int err; |
| 772 | |
| 773 | if (optlen < GROUP_FILTER_SIZE(0)) |
| 774 | return -EINVAL; |
| 775 | if (optlen > sysctl_optmem_max) |
| 776 | return -ENOBUFS; |
| 777 | |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 778 | gsf = memdup_sockptr(optval, optlen); |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 779 | if (IS_ERR(gsf)) |
| 780 | return PTR_ERR(gsf); |
| 781 | |
| 782 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 783 | err = -ENOBUFS; |
| 784 | if (gsf->gf_numsrc >= 0x1ffffff || |
| 785 | gsf->gf_numsrc > sock_net(sk)->ipv4.sysctl_igmp_max_msf) |
| 786 | goto out_free_gsf; |
| 787 | |
| 788 | err = -EINVAL; |
| 789 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) |
| 790 | goto out_free_gsf; |
| 791 | |
| 792 | err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc, |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 793 | gsf->gf_fmode, &gsf->gf_group, |
| 794 | gsf->gf_slist_flex); |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 795 | out_free_gsf: |
| 796 | kfree(gsf); |
| 797 | return err; |
| 798 | } |
| 799 | |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 800 | static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 801 | int optlen) |
| 802 | { |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 803 | const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 804 | struct compat_group_filter *gf32; |
| 805 | unsigned int n; |
| 806 | void *p; |
| 807 | int err; |
| 808 | |
| 809 | if (optlen < size0) |
| 810 | return -EINVAL; |
| 811 | if (optlen > sysctl_optmem_max - 4) |
| 812 | return -ENOBUFS; |
| 813 | |
| 814 | p = kmalloc(optlen + 4, GFP_KERNEL); |
| 815 | if (!p) |
| 816 | return -ENOMEM; |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 817 | gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */ |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 818 | |
| 819 | err = -EFAULT; |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 820 | if (copy_from_sockptr(gf32, optval, optlen)) |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 821 | goto out_free_gsf; |
| 822 | |
| 823 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 824 | n = gf32->gf_numsrc; |
| 825 | err = -ENOBUFS; |
| 826 | if (n >= 0x1ffffff) |
| 827 | goto out_free_gsf; |
| 828 | |
| 829 | err = -EINVAL; |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 830 | if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen) |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 831 | goto out_free_gsf; |
| 832 | |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 833 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 834 | err = -ENOBUFS; |
| 835 | if (n > sock_net(sk)->ipv4.sysctl_igmp_max_msf) |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 836 | goto out_free_gsf; |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 837 | err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode, |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 838 | &gf32->gf_group, gf32->gf_slist_flex); |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 839 | out_free_gsf: |
| 840 | kfree(p); |
| 841 | return err; |
| 842 | } |
Christoph Hellwig | d62c38f | 2020-07-17 08:23:24 +0200 | [diff] [blame] | 843 | |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 844 | static int ip_mcast_join_leave(struct sock *sk, int optname, |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 845 | sockptr_t optval, int optlen) |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 846 | { |
| 847 | struct ip_mreqn mreq = { }; |
| 848 | struct sockaddr_in *psin; |
| 849 | struct group_req greq; |
| 850 | |
| 851 | if (optlen < sizeof(struct group_req)) |
| 852 | return -EINVAL; |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 853 | if (copy_from_sockptr(&greq, optval, sizeof(greq))) |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 854 | return -EFAULT; |
| 855 | |
| 856 | psin = (struct sockaddr_in *)&greq.gr_group; |
| 857 | if (psin->sin_family != AF_INET) |
| 858 | return -EINVAL; |
| 859 | mreq.imr_multiaddr = psin->sin_addr; |
| 860 | mreq.imr_ifindex = greq.gr_interface; |
| 861 | if (optname == MCAST_JOIN_GROUP) |
| 862 | return ip_mc_join_group(sk, &mreq); |
| 863 | return ip_mc_leave_group(sk, &mreq); |
| 864 | } |
| 865 | |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 866 | static int compat_ip_mcast_join_leave(struct sock *sk, int optname, |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 867 | sockptr_t optval, int optlen) |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 868 | { |
| 869 | struct compat_group_req greq; |
| 870 | struct ip_mreqn mreq = { }; |
| 871 | struct sockaddr_in *psin; |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 872 | |
| 873 | if (optlen < sizeof(struct compat_group_req)) |
| 874 | return -EINVAL; |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 875 | if (copy_from_sockptr(&greq, optval, sizeof(greq))) |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 876 | return -EFAULT; |
| 877 | |
| 878 | psin = (struct sockaddr_in *)&greq.gr_group; |
| 879 | if (psin->sin_family != AF_INET) |
| 880 | return -EINVAL; |
| 881 | mreq.imr_multiaddr = psin->sin_addr; |
| 882 | mreq.imr_ifindex = greq.gr_interface; |
| 883 | |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 884 | if (optname == MCAST_JOIN_GROUP) |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 885 | return ip_mc_join_group(sk, &mreq); |
| 886 | return ip_mc_leave_group(sk, &mreq); |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 887 | } |
Christoph Hellwig | 02caad7 | 2020-07-17 08:23:25 +0200 | [diff] [blame] | 888 | |
Eric Dumazet | 020e71a | 2021-10-25 09:48:24 -0700 | [diff] [blame] | 889 | DEFINE_STATIC_KEY_FALSE(ip4_min_ttl); |
| 890 | |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 891 | static int do_ip_setsockopt(struct sock *sk, int level, int optname, |
| 892 | sockptr_t optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | { |
| 894 | struct inet_sock *inet = inet_sk(sk); |
Nikolay Borisov | 166b6b2 | 2016-02-08 23:29:22 +0200 | [diff] [blame] | 895 | struct net *net = sock_net(sk); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 896 | int val = 0, err; |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 897 | bool needs_rtnl = setsockopt_needs_rtnl(optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
Xi Wang | 0c9f79b | 2012-11-11 11:20:01 +0000 | [diff] [blame] | 899 | switch (optname) { |
| 900 | case IP_PKTINFO: |
| 901 | case IP_RECVTTL: |
| 902 | case IP_RECVOPTS: |
| 903 | case IP_RECVTOS: |
| 904 | case IP_RETOPTS: |
| 905 | case IP_TOS: |
| 906 | case IP_TTL: |
| 907 | case IP_HDRINCL: |
| 908 | case IP_MTU_DISCOVER: |
| 909 | case IP_RECVERR: |
| 910 | case IP_ROUTER_ALERT: |
| 911 | case IP_FREEBIND: |
| 912 | case IP_PASSSEC: |
| 913 | case IP_TRANSPARENT: |
| 914 | case IP_MINTTL: |
| 915 | case IP_NODEFRAG: |
Eric Dumazet | 90c337d | 2015-06-06 21:17:57 -0700 | [diff] [blame] | 916 | case IP_BIND_ADDRESS_NO_PORT: |
Xi Wang | 0c9f79b | 2012-11-11 11:20:01 +0000 | [diff] [blame] | 917 | case IP_UNICAST_IF: |
| 918 | case IP_MULTICAST_TTL: |
| 919 | case IP_MULTICAST_ALL: |
| 920 | case IP_MULTICAST_LOOP: |
| 921 | case IP_RECVORIGDSTADDR: |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 922 | case IP_CHECKSUM: |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 923 | case IP_RECVFRAGSIZE: |
Willem de Bruijn | eba75c5 | 2020-07-10 09:29:02 -0400 | [diff] [blame] | 924 | case IP_RECVERR_RFC4884: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | if (optlen >= sizeof(int)) { |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 926 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return -EFAULT; |
| 928 | } else if (optlen >= sizeof(char)) { |
| 929 | unsigned char ucval; |
| 930 | |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 931 | if (copy_from_sockptr(&ucval, optval, sizeof(ucval))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | return -EFAULT; |
| 933 | val = (int) ucval; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | /* If optlen==0, it is equivalent to val == 0 */ |
| 938 | |
Kirill Tkhai | 0526947 | 2018-03-22 12:45:12 +0300 | [diff] [blame] | 939 | if (optname == IP_ROUTER_ALERT) |
| 940 | return ip_ra_control(sk, val ? 1 : 0, NULL); |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 941 | if (ip_mroute_opt(optname)) |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 942 | return ip_mroute_setsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
| 944 | err = 0; |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 945 | if (needs_rtnl) |
| 946 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | lock_sock(sk); |
| 948 | |
| 949 | switch (optname) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 950 | case IP_OPTIONS: |
| 951 | { |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 952 | struct ip_options_rcu *old, *opt = NULL; |
| 953 | |
roel kluin | 65a1c4f | 2009-10-23 05:59:21 +0000 | [diff] [blame] | 954 | if (optlen > 40) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 955 | goto e_inval; |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 956 | err = ip_options_get(sock_net(sk), &opt, optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 957 | if (err) |
| 958 | break; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 959 | old = rcu_dereference_protected(inet->inet_opt, |
Hannes Frederic Sowa | 1e1d04e | 2016-04-05 17:10:15 +0200 | [diff] [blame] | 960 | lockdep_sock_is_held(sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 961 | if (inet->is_icsk) { |
| 962 | struct inet_connection_sock *icsk = inet_csk(sk); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 963 | #if IS_ENABLED(CONFIG_IPV6) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 964 | if (sk->sk_family == PF_INET || |
| 965 | (!((1 << sk->sk_state) & |
| 966 | (TCPF_LISTEN | TCPF_CLOSE)) && |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 967 | inet->inet_daddr != LOOPBACK4_IPV6)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | #endif |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 969 | if (old) |
| 970 | icsk->icsk_ext_hdr_len -= old->opt.optlen; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 971 | if (opt) |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 972 | icsk->icsk_ext_hdr_len += opt->opt.optlen; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 973 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 974 | #if IS_ENABLED(CONFIG_IPV6) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 975 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | } |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 978 | rcu_assign_pointer(inet->inet_opt, opt); |
| 979 | if (old) |
Paul E. McKenney | 605b4af | 2012-01-06 17:08:33 -0800 | [diff] [blame] | 980 | kfree_rcu(old, rcu); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 981 | break; |
| 982 | } |
| 983 | case IP_PKTINFO: |
| 984 | if (val) |
| 985 | inet->cmsg_flags |= IP_CMSG_PKTINFO; |
| 986 | else |
| 987 | inet->cmsg_flags &= ~IP_CMSG_PKTINFO; |
| 988 | break; |
| 989 | case IP_RECVTTL: |
| 990 | if (val) |
| 991 | inet->cmsg_flags |= IP_CMSG_TTL; |
| 992 | else |
| 993 | inet->cmsg_flags &= ~IP_CMSG_TTL; |
| 994 | break; |
| 995 | case IP_RECVTOS: |
| 996 | if (val) |
| 997 | inet->cmsg_flags |= IP_CMSG_TOS; |
| 998 | else |
| 999 | inet->cmsg_flags &= ~IP_CMSG_TOS; |
| 1000 | break; |
| 1001 | case IP_RECVOPTS: |
| 1002 | if (val) |
| 1003 | inet->cmsg_flags |= IP_CMSG_RECVOPTS; |
| 1004 | else |
| 1005 | inet->cmsg_flags &= ~IP_CMSG_RECVOPTS; |
| 1006 | break; |
| 1007 | case IP_RETOPTS: |
| 1008 | if (val) |
| 1009 | inet->cmsg_flags |= IP_CMSG_RETOPTS; |
| 1010 | else |
| 1011 | inet->cmsg_flags &= ~IP_CMSG_RETOPTS; |
| 1012 | break; |
| 1013 | case IP_PASSSEC: |
| 1014 | if (val) |
| 1015 | inet->cmsg_flags |= IP_CMSG_PASSSEC; |
| 1016 | else |
| 1017 | inet->cmsg_flags &= ~IP_CMSG_PASSSEC; |
| 1018 | break; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 1019 | case IP_RECVORIGDSTADDR: |
| 1020 | if (val) |
| 1021 | inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR; |
| 1022 | else |
| 1023 | inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR; |
| 1024 | break; |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 1025 | case IP_CHECKSUM: |
| 1026 | if (val) { |
| 1027 | if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) { |
| 1028 | inet_inc_convert_csum(sk); |
| 1029 | inet->cmsg_flags |= IP_CMSG_CHECKSUM; |
| 1030 | } |
| 1031 | } else { |
| 1032 | if (inet->cmsg_flags & IP_CMSG_CHECKSUM) { |
| 1033 | inet_dec_convert_csum(sk); |
| 1034 | inet->cmsg_flags &= ~IP_CMSG_CHECKSUM; |
| 1035 | } |
| 1036 | } |
| 1037 | break; |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 1038 | case IP_RECVFRAGSIZE: |
| 1039 | if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM) |
| 1040 | goto e_inval; |
| 1041 | if (val) |
| 1042 | inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE; |
| 1043 | else |
| 1044 | inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE; |
| 1045 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1046 | case IP_TOS: /* This sets both TOS and Precedence */ |
Christoph Hellwig | 6ebf71b | 2020-05-28 07:12:26 +0200 | [diff] [blame] | 1047 | __ip_sock_set_tos(sk, val); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1048 | break; |
| 1049 | case IP_TTL: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1050 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1051 | goto e_inval; |
Cong Wang | c9be4a5 | 2013-01-07 21:17:00 +0000 | [diff] [blame] | 1052 | if (val != -1 && (val < 1 || val > 255)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1053 | goto e_inval; |
| 1054 | inet->uc_ttl = val; |
| 1055 | break; |
| 1056 | case IP_HDRINCL: |
| 1057 | if (sk->sk_type != SOCK_RAW) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | err = -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1060 | } |
| 1061 | inet->hdrincl = val ? 1 : 0; |
| 1062 | break; |
Jiri Olsa | 7b2ff18 | 2010-06-15 01:07:31 +0000 | [diff] [blame] | 1063 | case IP_NODEFRAG: |
| 1064 | if (sk->sk_type != SOCK_RAW) { |
| 1065 | err = -ENOPROTOOPT; |
| 1066 | break; |
| 1067 | } |
| 1068 | inet->nodefrag = val ? 1 : 0; |
| 1069 | break; |
Eric Dumazet | 90c337d | 2015-06-06 21:17:57 -0700 | [diff] [blame] | 1070 | case IP_BIND_ADDRESS_NO_PORT: |
| 1071 | inet->bind_address_no_port = val ? 1 : 0; |
| 1072 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1073 | case IP_MTU_DISCOVER: |
Hannes Frederic Sowa | 1b34657 | 2014-02-26 01:20:42 +0100 | [diff] [blame] | 1074 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1075 | goto e_inval; |
| 1076 | inet->pmtudisc = val; |
| 1077 | break; |
| 1078 | case IP_RECVERR: |
| 1079 | inet->recverr = !!val; |
| 1080 | if (!val) |
| 1081 | skb_queue_purge(&sk->sk_error_queue); |
| 1082 | break; |
Willem de Bruijn | eba75c5 | 2020-07-10 09:29:02 -0400 | [diff] [blame] | 1083 | case IP_RECVERR_RFC4884: |
| 1084 | if (val < 0 || val > 1) |
| 1085 | goto e_inval; |
| 1086 | inet->recverr_rfc4884 = !!val; |
| 1087 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1088 | case IP_MULTICAST_TTL: |
| 1089 | if (sk->sk_type == SOCK_STREAM) |
| 1090 | goto e_inval; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1091 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1092 | goto e_inval; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1093 | if (val == -1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1094 | val = 1; |
| 1095 | if (val < 0 || val > 255) |
| 1096 | goto e_inval; |
| 1097 | inet->mc_ttl = val; |
| 1098 | break; |
| 1099 | case IP_MULTICAST_LOOP: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1100 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1101 | goto e_inval; |
| 1102 | inet->mc_loop = !!val; |
| 1103 | break; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 1104 | case IP_UNICAST_IF: |
| 1105 | { |
| 1106 | struct net_device *dev = NULL; |
| 1107 | int ifindex; |
David Ahern | 9515a2e | 2018-01-24 19:37:38 -0800 | [diff] [blame] | 1108 | int midx; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 1109 | |
| 1110 | if (optlen != sizeof(int)) |
| 1111 | goto e_inval; |
| 1112 | |
| 1113 | ifindex = (__force int)ntohl((__force __be32)val); |
| 1114 | if (ifindex == 0) { |
| 1115 | inet->uc_index = 0; |
| 1116 | err = 0; |
| 1117 | break; |
| 1118 | } |
| 1119 | |
| 1120 | dev = dev_get_by_index(sock_net(sk), ifindex); |
| 1121 | err = -EADDRNOTAVAIL; |
| 1122 | if (!dev) |
| 1123 | break; |
David Ahern | 9515a2e | 2018-01-24 19:37:38 -0800 | [diff] [blame] | 1124 | |
| 1125 | midx = l3mdev_master_ifindex(dev); |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 1126 | dev_put(dev); |
| 1127 | |
| 1128 | err = -EINVAL; |
Miaohe Lin | fdf1923 | 2020-08-25 08:10:37 -0400 | [diff] [blame] | 1129 | if (sk->sk_bound_dev_if && midx != sk->sk_bound_dev_if) |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 1130 | break; |
| 1131 | |
| 1132 | inet->uc_index = ifindex; |
| 1133 | err = 0; |
| 1134 | break; |
| 1135 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1136 | case IP_MULTICAST_IF: |
| 1137 | { |
| 1138 | struct ip_mreqn mreq; |
| 1139 | struct net_device *dev = NULL; |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 1140 | int midx; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1141 | |
| 1142 | if (sk->sk_type == SOCK_STREAM) |
| 1143 | goto e_inval; |
| 1144 | /* |
| 1145 | * Check the arguments are allowable |
| 1146 | */ |
| 1147 | |
Shan Wei | 0915921 | 2009-09-22 15:41:10 +0000 | [diff] [blame] | 1148 | if (optlen < sizeof(struct in_addr)) |
| 1149 | goto e_inval; |
| 1150 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1151 | err = -EFAULT; |
| 1152 | if (optlen >= sizeof(struct ip_mreqn)) { |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 1153 | if (copy_from_sockptr(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1154 | break; |
| 1155 | } else { |
| 1156 | memset(&mreq, 0, sizeof(mreq)); |
Jiri Pirko | 3a084dd | 2012-05-03 22:37:45 +0000 | [diff] [blame] | 1157 | if (optlen >= sizeof(struct ip_mreq)) { |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 1158 | if (copy_from_sockptr(&mreq, optval, |
| 1159 | sizeof(struct ip_mreq))) |
Jiri Pirko | 3a084dd | 2012-05-03 22:37:45 +0000 | [diff] [blame] | 1160 | break; |
| 1161 | } else if (optlen >= sizeof(struct in_addr)) { |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 1162 | if (copy_from_sockptr(&mreq.imr_address, optval, |
| 1163 | sizeof(struct in_addr))) |
Jiri Pirko | 3a084dd | 2012-05-03 22:37:45 +0000 | [diff] [blame] | 1164 | break; |
| 1165 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | if (!mreq.imr_ifindex) { |
Al Viro | e6f1ceb | 2008-03-17 22:44:53 -0700 | [diff] [blame] | 1169 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1170 | inet->mc_index = 0; |
| 1171 | inet->mc_addr = 0; |
| 1172 | err = 0; |
| 1173 | break; |
| 1174 | } |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1175 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 1176 | if (dev) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1177 | mreq.imr_ifindex = dev->ifindex; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1178 | } else |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 1179 | dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1180 | |
| 1181 | |
| 1182 | err = -EADDRNOTAVAIL; |
| 1183 | if (!dev) |
| 1184 | break; |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 1185 | |
| 1186 | midx = l3mdev_master_ifindex(dev); |
| 1187 | |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 1188 | dev_put(dev); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1189 | |
| 1190 | err = -EINVAL; |
| 1191 | if (sk->sk_bound_dev_if && |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 1192 | mreq.imr_ifindex != sk->sk_bound_dev_if && |
Miaohe Lin | fdf1923 | 2020-08-25 08:10:37 -0400 | [diff] [blame] | 1193 | midx != sk->sk_bound_dev_if) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1194 | break; |
| 1195 | |
| 1196 | inet->mc_index = mreq.imr_ifindex; |
| 1197 | inet->mc_addr = mreq.imr_address.s_addr; |
| 1198 | err = 0; |
| 1199 | break; |
| 1200 | } |
| 1201 | |
| 1202 | case IP_ADD_MEMBERSHIP: |
| 1203 | case IP_DROP_MEMBERSHIP: |
| 1204 | { |
| 1205 | struct ip_mreqn mreq; |
| 1206 | |
Flavio Leitner | a96fb49 | 2007-08-24 22:16:39 -0700 | [diff] [blame] | 1207 | err = -EPROTO; |
| 1208 | if (inet_sk(sk)->is_icsk) |
| 1209 | break; |
| 1210 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1211 | if (optlen < sizeof(struct ip_mreq)) |
| 1212 | goto e_inval; |
| 1213 | err = -EFAULT; |
| 1214 | if (optlen >= sizeof(struct ip_mreqn)) { |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 1215 | if (copy_from_sockptr(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1216 | break; |
| 1217 | } else { |
| 1218 | memset(&mreq, 0, sizeof(mreq)); |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 1219 | if (copy_from_sockptr(&mreq, optval, |
| 1220 | sizeof(struct ip_mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1221 | break; |
| 1222 | } |
| 1223 | |
| 1224 | if (optname == IP_ADD_MEMBERSHIP) |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 1225 | err = ip_mc_join_group(sk, &mreq); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1226 | else |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 1227 | err = ip_mc_leave_group(sk, &mreq); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1228 | break; |
| 1229 | } |
| 1230 | case IP_MSFILTER: |
| 1231 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1232 | struct ip_msfilter *msf; |
| 1233 | |
Gustavo A. R. Silva | 4167a96 | 2021-08-04 13:23:25 -0500 | [diff] [blame] | 1234 | if (optlen < IP_MSFILTER_SIZE(0)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1235 | goto e_inval; |
| 1236 | if (optlen > sysctl_optmem_max) { |
| 1237 | err = -ENOBUFS; |
| 1238 | break; |
| 1239 | } |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 1240 | msf = memdup_sockptr(optval, optlen); |
Al Viro | a2c841d | 2017-05-13 18:26:06 -0400 | [diff] [blame] | 1241 | if (IS_ERR(msf)) { |
| 1242 | err = PTR_ERR(msf); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1243 | break; |
| 1244 | } |
| 1245 | /* numsrc >= (1G-4) overflow in 32 bits */ |
| 1246 | if (msf->imsf_numsrc >= 0x3ffffffcU || |
Nikolay Borisov | 166b6b2 | 2016-02-08 23:29:22 +0200 | [diff] [blame] | 1247 | msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1248 | kfree(msf); |
| 1249 | err = -ENOBUFS; |
| 1250 | break; |
| 1251 | } |
Gustavo A. R. Silva | 4167a96 | 2021-08-04 13:23:25 -0500 | [diff] [blame] | 1252 | if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1253 | kfree(msf); |
| 1254 | err = -EINVAL; |
| 1255 | break; |
| 1256 | } |
| 1257 | err = ip_mc_msfilter(sk, msf, 0); |
| 1258 | kfree(msf); |
| 1259 | break; |
| 1260 | } |
| 1261 | case IP_BLOCK_SOURCE: |
| 1262 | case IP_UNBLOCK_SOURCE: |
| 1263 | case IP_ADD_SOURCE_MEMBERSHIP: |
| 1264 | case IP_DROP_SOURCE_MEMBERSHIP: |
| 1265 | { |
| 1266 | struct ip_mreq_source mreqs; |
| 1267 | int omode, add; |
| 1268 | |
| 1269 | if (optlen != sizeof(struct ip_mreq_source)) |
| 1270 | goto e_inval; |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 1271 | if (copy_from_sockptr(&mreqs, optval, sizeof(mreqs))) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1272 | err = -EFAULT; |
| 1273 | break; |
| 1274 | } |
| 1275 | if (optname == IP_BLOCK_SOURCE) { |
| 1276 | omode = MCAST_EXCLUDE; |
| 1277 | add = 1; |
| 1278 | } else if (optname == IP_UNBLOCK_SOURCE) { |
| 1279 | omode = MCAST_EXCLUDE; |
| 1280 | add = 0; |
| 1281 | } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { |
| 1282 | struct ip_mreqn mreq; |
| 1283 | |
| 1284 | mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; |
| 1285 | mreq.imr_address.s_addr = mreqs.imr_interface; |
| 1286 | mreq.imr_ifindex = 0; |
Hangbin Liu | 6e2059b | 2018-07-10 22:41:26 +0800 | [diff] [blame] | 1287 | err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1288 | if (err && err != -EADDRINUSE) |
| 1289 | break; |
| 1290 | omode = MCAST_INCLUDE; |
| 1291 | add = 1; |
| 1292 | } else /* IP_DROP_SOURCE_MEMBERSHIP */ { |
| 1293 | omode = MCAST_INCLUDE; |
| 1294 | add = 0; |
| 1295 | } |
| 1296 | err = ip_mc_source(add, omode, sk, &mreqs, 0); |
| 1297 | break; |
| 1298 | } |
| 1299 | case MCAST_JOIN_GROUP: |
| 1300 | case MCAST_LEAVE_GROUP: |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 1301 | if (in_compat_syscall()) |
| 1302 | err = compat_ip_mcast_join_leave(sk, optname, optval, |
| 1303 | optlen); |
| 1304 | else |
| 1305 | err = ip_mcast_join_leave(sk, optname, optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1306 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1307 | case MCAST_JOIN_SOURCE_GROUP: |
| 1308 | case MCAST_LEAVE_SOURCE_GROUP: |
| 1309 | case MCAST_BLOCK_SOURCE: |
| 1310 | case MCAST_UNBLOCK_SOURCE: |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 1311 | err = do_mcast_group_source(sk, optname, optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1312 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1313 | case MCAST_MSFILTER: |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 1314 | if (in_compat_syscall()) |
| 1315 | err = compat_ip_set_mcast_msfilter(sk, optval, optlen); |
| 1316 | else |
| 1317 | err = ip_set_mcast_msfilter(sk, optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1318 | break; |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 1319 | case IP_MULTICAST_ALL: |
| 1320 | if (optlen < 1) |
| 1321 | goto e_inval; |
| 1322 | if (val != 0 && val != 1) |
| 1323 | goto e_inval; |
| 1324 | inet->mc_all = val; |
| 1325 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1326 | |
| 1327 | case IP_FREEBIND: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1328 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1329 | goto e_inval; |
| 1330 | inet->freebind = !!val; |
| 1331 | break; |
| 1332 | |
| 1333 | case IP_IPSEC_POLICY: |
| 1334 | case IP_XFRM_POLICY: |
| 1335 | err = -EPERM; |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 1336 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1337 | break; |
Christoph Hellwig | 89654c5 | 2020-07-23 08:08:58 +0200 | [diff] [blame] | 1338 | err = xfrm_user_policy(sk, optname, optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1339 | break; |
| 1340 | |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1341 | case IP_TRANSPARENT: |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 1342 | if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && |
| 1343 | !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1344 | err = -EPERM; |
| 1345 | break; |
| 1346 | } |
| 1347 | if (optlen < 1) |
| 1348 | goto e_inval; |
| 1349 | inet->transparent = !!val; |
| 1350 | break; |
| 1351 | |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 1352 | case IP_MINTTL: |
| 1353 | if (optlen < 1) |
| 1354 | goto e_inval; |
| 1355 | if (val < 0 || val > 255) |
| 1356 | goto e_inval; |
Eric Dumazet | 020e71a | 2021-10-25 09:48:24 -0700 | [diff] [blame] | 1357 | |
| 1358 | if (val) |
| 1359 | static_branch_enable(&ip4_min_ttl); |
| 1360 | |
Eric Dumazet | 14834c4 | 2021-10-25 09:48:23 -0700 | [diff] [blame] | 1361 | /* tcp_v4_err() and tcp_v4_rcv() might read min_ttl |
| 1362 | * while we are changint it. |
| 1363 | */ |
| 1364 | WRITE_ONCE(inet->min_ttl, val); |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 1365 | break; |
| 1366 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1367 | default: |
| 1368 | err = -ENOPROTOOPT; |
| 1369 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | } |
| 1371 | release_sock(sk); |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 1372 | if (needs_rtnl) |
| 1373 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | return err; |
| 1375 | |
| 1376 | e_inval: |
| 1377 | release_sock(sk); |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 1378 | if (needs_rtnl) |
| 1379 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | return -EINVAL; |
| 1381 | } |
| 1382 | |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1383 | /** |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 1384 | * ipv4_pktinfo_prepare - transfer some info from rtable to skb |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1385 | * @sk: socket |
| 1386 | * @skb: buffer |
| 1387 | * |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 1388 | * To support IP_CMSG_PKTINFO option, we store rt_iif and specific |
| 1389 | * destination in skb->cb[] before dst drop. |
stephen hemminger | 8e3bff9 | 2013-12-08 12:15:44 -0800 | [diff] [blame] | 1390 | * This way, receiver doesn't make cache line misses to read rtable. |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1391 | */ |
Shawn Bohrer | fbf8866 | 2013-10-07 11:01:40 -0500 | [diff] [blame] | 1392 | void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb) |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1393 | { |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1394 | struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 1395 | bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) || |
| 1396 | ipv6_sk_rxinfo(sk); |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1397 | |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 1398 | if (prepare && skb_rtable(skb)) { |
David Ahern | 0b922b7 | 2016-05-10 11:19:51 -0700 | [diff] [blame] | 1399 | /* skb->cb is overloaded: prior to this point it is IP{6}CB |
| 1400 | * which has interface index (iif) as the first member of the |
| 1401 | * underlying inet{6}_skb_parm struct. This code then overlays |
| 1402 | * PKTINFO_SKB_CB and in_pktinfo also has iif as the first |
Wei Zhang | f0c16ba | 2016-12-29 16:45:04 +0800 | [diff] [blame] | 1403 | * element so the iif is picked up from the prior IPCB. If iif |
| 1404 | * is the loopback interface, then return the sending interface |
| 1405 | * (e.g., process binds socket to eth0 for Tx which is |
| 1406 | * redirected to loopback in the rtable/dst). |
David Ahern | 0b922b7 | 2016-05-10 11:19:51 -0700 | [diff] [blame] | 1407 | */ |
David Ahern | cbea8f0 | 2017-09-13 17:11:37 -0700 | [diff] [blame] | 1408 | struct rtable *rt = skb_rtable(skb); |
| 1409 | bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags); |
| 1410 | |
| 1411 | if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX) |
Wei Zhang | f0c16ba | 2016-12-29 16:45:04 +0800 | [diff] [blame] | 1412 | pktinfo->ipi_ifindex = inet_iif(skb); |
David Ahern | cbea8f0 | 2017-09-13 17:11:37 -0700 | [diff] [blame] | 1413 | else if (l3slave && rt && rt->rt_iif) |
| 1414 | pktinfo->ipi_ifindex = rt->rt_iif; |
Wei Zhang | f0c16ba | 2016-12-29 16:45:04 +0800 | [diff] [blame] | 1415 | |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 1416 | pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1417 | } else { |
| 1418 | pktinfo->ipi_ifindex = 0; |
| 1419 | pktinfo->ipi_spec_dst.s_addr = 0; |
| 1420 | } |
Paolo Abeni | 61a1030 | 2017-08-03 18:07:07 +0200 | [diff] [blame] | 1421 | skb_dst_drop(skb); |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1422 | } |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1423 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1424 | int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, |
| 1425 | unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1426 | { |
| 1427 | int err; |
| 1428 | |
| 1429 | if (level != SOL_IP) |
| 1430 | return -ENOPROTOOPT; |
| 1431 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1432 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
Taehee Yoo | 97adadd | 2018-11-05 22:31:41 +0900 | [diff] [blame] | 1433 | #if IS_ENABLED(CONFIG_BPFILTER_UMH) |
Alexei Starovoitov | d2ba09c | 2018-05-21 19:22:30 -0700 | [diff] [blame] | 1434 | if (optname >= BPFILTER_IPT_SO_SET_REPLACE && |
| 1435 | optname < BPFILTER_IPT_SET_MAX) |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1436 | err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen); |
Alexei Starovoitov | d2ba09c | 2018-05-21 19:22:30 -0700 | [diff] [blame] | 1437 | #endif |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1438 | #ifdef CONFIG_NETFILTER |
| 1439 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1440 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1441 | optname != IP_IPSEC_POLICY && |
| 1442 | optname != IP_XFRM_POLICY && |
Paolo Abeni | 3f34cfa | 2018-01-30 19:01:40 +0100 | [diff] [blame] | 1443 | !ip_mroute_opt(optname)) |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1444 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1445 | #endif |
| 1446 | return err; |
| 1447 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1448 | EXPORT_SYMBOL(ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1451 | * Get the options. Note for future reference. The GET of IP options gets |
| 1452 | * the _received_ ones. The set sets the _sent_ ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | */ |
| 1454 | |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1455 | static bool getsockopt_needs_rtnl(int optname) |
| 1456 | { |
| 1457 | switch (optname) { |
| 1458 | case IP_MSFILTER: |
| 1459 | case MCAST_MSFILTER: |
| 1460 | return true; |
| 1461 | } |
| 1462 | return false; |
| 1463 | } |
| 1464 | |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1465 | static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval, |
| 1466 | int __user *optlen, int len) |
| 1467 | { |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 1468 | const int size0 = offsetof(struct group_filter, gf_slist_flex); |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1469 | struct group_filter __user *p = optval; |
| 1470 | struct group_filter gsf; |
| 1471 | int num; |
| 1472 | int err; |
| 1473 | |
| 1474 | if (len < size0) |
| 1475 | return -EINVAL; |
| 1476 | if (copy_from_user(&gsf, p, size0)) |
| 1477 | return -EFAULT; |
| 1478 | |
| 1479 | num = gsf.gf_numsrc; |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 1480 | err = ip_mc_gsfget(sk, &gsf, p->gf_slist_flex); |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1481 | if (err) |
| 1482 | return err; |
| 1483 | if (gsf.gf_numsrc < num) |
| 1484 | num = gsf.gf_numsrc; |
| 1485 | if (put_user(GROUP_FILTER_SIZE(num), optlen) || |
| 1486 | copy_to_user(p, &gsf, size0)) |
| 1487 | return -EFAULT; |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1491 | static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval, |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 1492 | int __user *optlen, int len) |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1493 | { |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 1494 | const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1495 | struct compat_group_filter __user *p = optval; |
| 1496 | struct compat_group_filter gf32; |
| 1497 | struct group_filter gf; |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1498 | int num; |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 1499 | int err; |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1500 | |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1501 | if (len < size0) |
| 1502 | return -EINVAL; |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1503 | if (copy_from_user(&gf32, p, size0)) |
| 1504 | return -EFAULT; |
| 1505 | |
| 1506 | gf.gf_interface = gf32.gf_interface; |
| 1507 | gf.gf_fmode = gf32.gf_fmode; |
| 1508 | num = gf.gf_numsrc = gf32.gf_numsrc; |
| 1509 | gf.gf_group = gf32.gf_group; |
| 1510 | |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 1511 | err = ip_mc_gsfget(sk, &gf, p->gf_slist_flex); |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1512 | if (err) |
| 1513 | return err; |
| 1514 | if (gf.gf_numsrc < num) |
| 1515 | num = gf.gf_numsrc; |
| 1516 | len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32)); |
| 1517 | if (put_user(len, optlen) || |
| 1518 | put_user(gf.gf_fmode, &p->gf_fmode) || |
| 1519 | put_user(gf.gf_numsrc, &p->gf_numsrc)) |
| 1520 | return -EFAULT; |
| 1521 | return 0; |
| 1522 | } |
Christoph Hellwig | 49e74c2 | 2020-07-17 08:23:23 +0200 | [diff] [blame] | 1523 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1524 | static int do_ip_getsockopt(struct sock *sk, int level, int optname, |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 1525 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | { |
| 1527 | struct inet_sock *inet = inet_sk(sk); |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1528 | bool needs_rtnl = getsockopt_needs_rtnl(optname); |
| 1529 | int val, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | int len; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1531 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1532 | if (level != SOL_IP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | return -EOPNOTSUPP; |
| 1534 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1535 | if (ip_mroute_opt(optname)) |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1536 | return ip_mroute_getsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1538 | if (get_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | return -EFAULT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1540 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | return -EINVAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1542 | |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1543 | if (needs_rtnl) |
| 1544 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | lock_sock(sk); |
| 1546 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1547 | switch (optname) { |
| 1548 | case IP_OPTIONS: |
| 1549 | { |
| 1550 | unsigned char optbuf[sizeof(struct ip_options)+40]; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1551 | struct ip_options *opt = (struct ip_options *)optbuf; |
| 1552 | struct ip_options_rcu *inet_opt; |
| 1553 | |
| 1554 | inet_opt = rcu_dereference_protected(inet->inet_opt, |
Hannes Frederic Sowa | 1e1d04e | 2016-04-05 17:10:15 +0200 | [diff] [blame] | 1555 | lockdep_sock_is_held(sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1556 | opt->optlen = 0; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1557 | if (inet_opt) |
| 1558 | memcpy(optbuf, &inet_opt->opt, |
| 1559 | sizeof(struct ip_options) + |
| 1560 | inet_opt->opt.optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1561 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1563 | if (opt->optlen == 0) |
| 1564 | return put_user(0, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1566 | ip_options_undo(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1568 | len = min_t(unsigned int, len, opt->optlen); |
| 1569 | if (put_user(len, optlen)) |
| 1570 | return -EFAULT; |
| 1571 | if (copy_to_user(optval, opt->__data, len)) |
| 1572 | return -EFAULT; |
| 1573 | return 0; |
| 1574 | } |
| 1575 | case IP_PKTINFO: |
| 1576 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; |
| 1577 | break; |
| 1578 | case IP_RECVTTL: |
| 1579 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; |
| 1580 | break; |
| 1581 | case IP_RECVTOS: |
| 1582 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; |
| 1583 | break; |
| 1584 | case IP_RECVOPTS: |
| 1585 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; |
| 1586 | break; |
| 1587 | case IP_RETOPTS: |
| 1588 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; |
| 1589 | break; |
| 1590 | case IP_PASSSEC: |
| 1591 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; |
| 1592 | break; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 1593 | case IP_RECVORIGDSTADDR: |
| 1594 | val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; |
| 1595 | break; |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 1596 | case IP_CHECKSUM: |
| 1597 | val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0; |
| 1598 | break; |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 1599 | case IP_RECVFRAGSIZE: |
| 1600 | val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0; |
| 1601 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1602 | case IP_TOS: |
| 1603 | val = inet->tos; |
| 1604 | break; |
| 1605 | case IP_TTL: |
Nikolay Borisov | fa50d97 | 2016-02-15 12:11:27 +0200 | [diff] [blame] | 1606 | { |
| 1607 | struct net *net = sock_net(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1608 | val = (inet->uc_ttl == -1 ? |
Nikolay Borisov | fa50d97 | 2016-02-15 12:11:27 +0200 | [diff] [blame] | 1609 | net->ipv4.sysctl_ip_default_ttl : |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1610 | inet->uc_ttl); |
| 1611 | break; |
Nikolay Borisov | fa50d97 | 2016-02-15 12:11:27 +0200 | [diff] [blame] | 1612 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1613 | case IP_HDRINCL: |
| 1614 | val = inet->hdrincl; |
| 1615 | break; |
Michael Kerrisk | a89b476 | 2010-09-10 20:26:56 +0000 | [diff] [blame] | 1616 | case IP_NODEFRAG: |
| 1617 | val = inet->nodefrag; |
| 1618 | break; |
Eric Dumazet | 90c337d | 2015-06-06 21:17:57 -0700 | [diff] [blame] | 1619 | case IP_BIND_ADDRESS_NO_PORT: |
| 1620 | val = inet->bind_address_no_port; |
| 1621 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1622 | case IP_MTU_DISCOVER: |
| 1623 | val = inet->pmtudisc; |
| 1624 | break; |
| 1625 | case IP_MTU: |
| 1626 | { |
| 1627 | struct dst_entry *dst; |
| 1628 | val = 0; |
| 1629 | dst = sk_dst_get(sk); |
| 1630 | if (dst) { |
| 1631 | val = dst_mtu(dst); |
| 1632 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1634 | if (!val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1636 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1638 | break; |
| 1639 | } |
| 1640 | case IP_RECVERR: |
| 1641 | val = inet->recverr; |
| 1642 | break; |
Willem de Bruijn | eba75c5 | 2020-07-10 09:29:02 -0400 | [diff] [blame] | 1643 | case IP_RECVERR_RFC4884: |
| 1644 | val = inet->recverr_rfc4884; |
| 1645 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1646 | case IP_MULTICAST_TTL: |
| 1647 | val = inet->mc_ttl; |
| 1648 | break; |
| 1649 | case IP_MULTICAST_LOOP: |
| 1650 | val = inet->mc_loop; |
| 1651 | break; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 1652 | case IP_UNICAST_IF: |
| 1653 | val = (__force int)htonl((__u32) inet->uc_index); |
| 1654 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1655 | case IP_MULTICAST_IF: |
| 1656 | { |
| 1657 | struct in_addr addr; |
| 1658 | len = min_t(unsigned int, len, sizeof(struct in_addr)); |
| 1659 | addr.s_addr = inet->mc_addr; |
| 1660 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1662 | if (put_user(len, optlen)) |
| 1663 | return -EFAULT; |
| 1664 | if (copy_to_user(optval, &addr, len)) |
| 1665 | return -EFAULT; |
| 1666 | return 0; |
| 1667 | } |
| 1668 | case IP_MSFILTER: |
| 1669 | { |
| 1670 | struct ip_msfilter msf; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1671 | |
Gustavo A. R. Silva | 4167a96 | 2021-08-04 13:23:25 -0500 | [diff] [blame] | 1672 | if (len < IP_MSFILTER_SIZE(0)) { |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1673 | err = -EINVAL; |
| 1674 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | } |
Gustavo A. R. Silva | 4167a96 | 2021-08-04 13:23:25 -0500 | [diff] [blame] | 1676 | if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1677 | err = -EFAULT; |
| 1678 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1680 | err = ip_mc_msfget(sk, &msf, |
| 1681 | (struct ip_msfilter __user *)optval, optlen); |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1682 | goto out; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1683 | } |
| 1684 | case MCAST_MSFILTER: |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 1685 | if (in_compat_syscall()) |
| 1686 | err = compat_ip_get_mcast_msfilter(sk, optval, optlen, |
| 1687 | len); |
| 1688 | else |
| 1689 | err = ip_get_mcast_msfilter(sk, optval, optlen, len); |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1690 | goto out; |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 1691 | case IP_MULTICAST_ALL: |
| 1692 | val = inet->mc_all; |
| 1693 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1694 | case IP_PKTOPTIONS: |
| 1695 | { |
| 1696 | struct msghdr msg; |
| 1697 | |
| 1698 | release_sock(sk); |
| 1699 | |
| 1700 | if (sk->sk_type != SOCK_STREAM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | return -ENOPROTOOPT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1702 | |
Christoph Hellwig | 1f466e1 | 2020-05-11 13:59:13 +0200 | [diff] [blame] | 1703 | msg.msg_control_is_user = true; |
| 1704 | msg.msg_control_user = optval; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1705 | msg.msg_controllen = len; |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 1706 | msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1707 | |
| 1708 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
| 1709 | struct in_pktinfo info; |
| 1710 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1711 | info.ipi_addr.s_addr = inet->inet_rcv_saddr; |
| 1712 | info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1713 | info.ipi_ifindex = inet->mc_index; |
| 1714 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 1715 | } |
| 1716 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
| 1717 | int hlim = inet->mc_ttl; |
| 1718 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); |
| 1719 | } |
Jiri Benc | 4c507d2 | 2012-02-09 09:35:49 +0000 | [diff] [blame] | 1720 | if (inet->cmsg_flags & IP_CMSG_TOS) { |
| 1721 | int tos = inet->rcv_tos; |
| 1722 | put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos); |
| 1723 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1724 | len -= msg.msg_controllen; |
| 1725 | return put_user(len, optlen); |
| 1726 | } |
| 1727 | case IP_FREEBIND: |
| 1728 | val = inet->freebind; |
| 1729 | break; |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1730 | case IP_TRANSPARENT: |
| 1731 | val = inet->transparent; |
| 1732 | break; |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 1733 | case IP_MINTTL: |
| 1734 | val = inet->min_ttl; |
| 1735 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1736 | default: |
| 1737 | release_sock(sk); |
| 1738 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | } |
| 1740 | release_sock(sk); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1741 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1742 | if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | unsigned char ucval = (unsigned char)val; |
| 1744 | len = 1; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1745 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1747 | if (copy_to_user(optval, &ucval, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | return -EFAULT; |
| 1749 | } else { |
| 1750 | len = min_t(unsigned int, sizeof(int), len); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1751 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1753 | if (copy_to_user(optval, &val, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | return -EFAULT; |
| 1755 | } |
| 1756 | return 0; |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1757 | |
| 1758 | out: |
| 1759 | release_sock(sk); |
| 1760 | if (needs_rtnl) |
| 1761 | rtnl_unlock(); |
| 1762 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1765 | int ip_getsockopt(struct sock *sk, int level, |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1766 | int optname, char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1767 | { |
| 1768 | int err; |
| 1769 | |
Christoph Hellwig | b6238c0 | 2020-07-17 08:23:26 +0200 | [diff] [blame] | 1770 | err = do_ip_getsockopt(sk, level, optname, optval, optlen); |
| 1771 | |
Taehee Yoo | 97adadd | 2018-11-05 22:31:41 +0900 | [diff] [blame] | 1772 | #if IS_ENABLED(CONFIG_BPFILTER_UMH) |
Alexei Starovoitov | d2ba09c | 2018-05-21 19:22:30 -0700 | [diff] [blame] | 1773 | if (optname >= BPFILTER_IPT_SO_GET_INFO && |
| 1774 | optname < BPFILTER_IPT_GET_MAX) |
| 1775 | err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen); |
| 1776 | #endif |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1777 | #ifdef CONFIG_NETFILTER |
| 1778 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1779 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1780 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1781 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1782 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1783 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1784 | return -EFAULT; |
| 1785 | |
Paolo Abeni | 01ea306 | 2018-02-08 12:19:00 +0100 | [diff] [blame] | 1786 | err = nf_getsockopt(sk, PF_INET, optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1787 | if (err >= 0) |
| 1788 | err = put_user(len, optlen); |
| 1789 | return err; |
| 1790 | } |
| 1791 | #endif |
| 1792 | return err; |
| 1793 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1794 | EXPORT_SYMBOL(ip_getsockopt); |