blob: d5487c8580674a01df8c7d8ce88f97c9add846b6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Hideakie905a9e2007-02-09 23:24:47 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 Hideakie905a9e2007-02-09 23:24:47 +090014 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * TOS tweaks.
16 * Mike McLagan : Routing by source
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/skbuff.h>
23#include <linux/ip.h>
24#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020025#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/sock.h>
29#include <net/ip.h>
30#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080031#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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 Żenczykowski2c67e9a2011-10-22 00:07:47 -040037#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/route.h>
39#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070040#include <net/compat.h>
Tom Herbertad6f9392015-01-05 13:56:17 -080041#include <net/checksum.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000042#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/transp_v6.h>
44#endif
David S. Miller35ebf652012-06-28 03:59:11 -070045#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/errqueue.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080048#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070050#include <linux/bpfilter.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * SOL_IP control messages.
54 */
55
56static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
57{
Eric Dumazetd826eb12011-11-09 07:24:35 +000058 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070060 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
63}
64
65static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
66{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070067 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
69}
70
71static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
72{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070073 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76static 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 Meloeddc9ec2007-04-20 22:47:35 -070081 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
82 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85
Paolo Abeni91ed1e62017-08-03 18:07:06 +020086static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg,
87 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000090 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 if (IPCB(skb)->opt.optlen == 0)
93 return;
94
Paolo Abeni91ed1e62017-08-03 18:07:06 +020095 if (ip_options_echo(net, opt, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 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 Bruijn70ecc242016-11-02 11:02:16 -0400104static 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 Herbertad6f9392015-01-05 13:56:17 -0800115static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700116 int tlen, int offset)
Tom Herbertad6f9392015-01-05 13:56:17 -0800117{
118 __wsum csum = skb->csum;
119
120 if (skb->ip_summed != CHECKSUM_COMPLETE)
121 return;
122
Paolo Abenica4ef452017-02-21 09:33:18 +0100123 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 Herbertad6f9392015-01-05 13:56:17 -0800127
128 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
129}
130
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800131static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
132{
133 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700134 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800135 int err;
136
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700137 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
138 if (err)
139 return;
140
141 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800142 if (err)
143 return;
144
145 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700146 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800147}
148
Harvey Harrison21d1a162008-11-20 01:54:27 -0800149static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800150{
Willem de Bruijn4a06fa62019-01-07 16:47:33 -0500151 __be16 _ports[2], *ports;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800152 struct sockaddr_in sin;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800153
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 Bruijn4a06fa62019-01-07 16:47:33 -0500158 ports = skb_header_pointer(skb, skb_transport_offset(skb),
159 sizeof(_ports), &_ports);
160 if (!ports)
161 return;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800162
163 sin.sin_family = AF_INET;
Eric Dumazet64199fc2018-09-30 11:33:39 -0700164 sin.sin_addr.s_addr = ip_hdr(skb)->daddr;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800165 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 Torvalds1da177e2005-04-16 15:20:36 -0700170
Paolo Abeniad959032016-11-04 11:28:58 +0100171void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
172 struct sk_buff *skb, int tlen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Paolo Abeniad959032016-11-04 11:28:58 +0100174 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000175 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800178 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Tom Herbertc44d13d2015-01-05 13:56:15 -0800181 flags &= ~IP_CMSG_PKTINFO;
182 if (!flags)
183 return;
184 }
185
186 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Tom Herbertc44d13d2015-01-05 13:56:15 -0800189 flags &= ~IP_CMSG_TTL;
190 if (!flags)
191 return;
192 }
193
194 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Tom Herbertc44d13d2015-01-05 13:56:15 -0800197 flags &= ~IP_CMSG_TOS;
198 if (!flags)
199 return;
200 }
201
202 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Tom Herbertc44d13d2015-01-05 13:56:15 -0800205 flags &= ~IP_CMSG_RECVOPTS;
206 if (!flags)
207 return;
208 }
209
210 if (flags & IP_CMSG_RETOPTS) {
Paolo Abeni91ed1e62017-08-03 18:07:06 +0200211 ip_cmsg_recv_retopts(sock_net(sk), msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800212
Tom Herbertc44d13d2015-01-05 13:56:15 -0800213 flags &= ~IP_CMSG_RETOPTS;
214 if (!flags)
215 return;
216 }
217
218 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800219 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800220
Tom Herbertc44d13d2015-01-05 13:56:15 -0800221 flags &= ~IP_CMSG_PASSSEC;
222 if (!flags)
223 return;
224 }
225
Tom Herbertad6f9392015-01-05 13:56:17 -0800226 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800227 ip_cmsg_recv_dstaddr(msg, skb);
228
Tom Herbertad6f9392015-01-05 13:56:17 -0800229 flags &= ~IP_CMSG_ORIGDSTADDR;
230 if (!flags)
231 return;
232 }
233
234 if (flags & IP_CMSG_CHECKSUM)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700235 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400236
237 if (flags & IP_CMSG_RECVFRAGSIZE)
238 ip_cmsg_recv_fragsize(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Tom Herbert5961de92015-01-05 13:56:16 -0800240EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400242int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100243 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Francesco Fuscof02db312013-09-24 15:43:08 +0200245 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400247 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Gu Zhengf95b4142014-12-11 11:22:04 +0800249 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (!CMSG_OK(msg, cmsg))
251 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800252#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100253 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 Ahern1cbec072018-02-16 11:03:03 -0800263 if (src_info->ipi6_ifindex)
264 ipc->oif = src_info->ipi6_ifindex;
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100265 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
266 continue;
267 }
268#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400269 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700270 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
271 if (err)
272 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400273 continue;
274 }
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (cmsg->cmsg_level != SOL_IP)
277 continue;
278 switch (cmsg->cmsg_type) {
279 case IP_RETOPTS:
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800280 err = cmsg->cmsg_len - sizeof(struct cmsghdr);
Eric Dumazet91948302016-02-04 06:23:28 -0800281
282 /* Our caller is responsible for freeing ipc->opt */
Christoph Hellwigde40a3e2020-07-23 08:08:57 +0200283 err = ip_options_get(net, &ipc->opt,
284 KERNEL_SOCKPTR(CMSG_DATA(cmsg)),
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700285 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 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 Ahern1cbec072018-02-16 11:03:03 -0800295 if (info->ipi_ifindex)
296 ipc->oif = info->ipi_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ipc->addr = info->ipi_spec_dst.s_addr;
298 break;
299 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200300 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 Dumazete895cdc2016-09-07 21:52:56 -0700309 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 Fuscof02db312013-09-24 15:43:08 +0200314 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200315 if (val < 0 || val > 255)
316 return -EINVAL;
317 ipc->tos = val;
318 ipc->priority = rt_tos2priority(ipc->tos);
319 break;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 default:
322 return -EINVAL;
323 }
324 }
325 return 0;
326}
327
Eric Dumazet592fcb92010-06-09 16:21:07 +0000328static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000329{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000330 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
331
332 sock_put(ra->saved_sk);
333 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700336int ip_ra_control(struct sock *sk, unsigned char on,
337 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000339 struct ip_ra_chain *ra, *new_ra;
340 struct ip_ra_chain __rcu **rap;
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300341 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000343 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -EINVAL;
345
346 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
Gen Zhang425aa0e2019-05-24 11:24:26 +0800347 if (on && !new_ra)
348 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300350 mutex_lock(&net->ipv4.ra_mutex);
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300351 for (rap = &net->ipv4.ra_chain;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300352 (ra = rcu_dereference_protected(*rap,
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300353 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL;
Eric Dumazet43a951e2010-10-25 03:32:44 +0000354 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (ra->sk == sk) {
356 if (on) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300357 mutex_unlock(&net->ipv4.ra_mutex);
Jesper Juhla51482b2005-11-08 09:41:34 -0800358 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return -EADDRINUSE;
360 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000361 /* dont let ip_call_ra_chain() use sk again */
362 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700363 RCU_INIT_POINTER(*rap, ra->next);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300364 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (ra->destructor)
367 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000368 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376 }
377 }
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300378 if (!new_ra) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300379 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -ENOBUFS;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 new_ra->sk = sk;
383 new_ra->destructor = destructor;
384
Eric Dumazet8e380f02014-09-09 08:11:41 -0700385 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000386 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sock_hold(sk);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300388 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 return 0;
391}
392
Willem de Bruijn178c49d2020-07-24 09:03:09 -0400393static 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 Hideakie905a9e2007-02-09 23:24:47 +0900405void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700406 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct sock_exterr_skb *serr;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 skb = skb_clone(skb, GFP_ATOMIC);
411 if (!skb)
412 return;
413
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900414 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 serr->ee.ee_errno = err;
416 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300417 serr->ee.ee_type = icmp_hdr(skb)->type;
418 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 serr->ee.ee_pad = 0;
420 serr->ee.ee_info = info;
421 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300422 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700423 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 serr->port = port;
425
Ian Morris00db4122015-04-03 09:17:27 +0100426 if (skb_pull(skb, payload - skb->data)) {
Willem de Bruijneba75c52020-07-10 09:29:02 -0400427 if (inet_sk(sk)->recverr_rfc4884)
Willem de Bruijn178c49d2020-07-24 09:03:09 -0400428 ipv4_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884);
Willem de Bruijneba75c52020-07-10 09:29:02 -0400429
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300430 skb_reset_transport_header(skb);
431 if (sock_queue_err_skb(sk, skb) == 0)
432 return;
433 }
434 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Al Viro05790162006-09-27 18:33:40 -0700437void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
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 Melo2ca9e6f2007-03-10 19:15:25 -0300451 skb_put(skb, sizeof(struct iphdr));
452 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700453 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 iph->daddr = daddr;
455
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900456 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 serr->ee.ee_errno = err;
458 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900459 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 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 Melod56f90a2007-04-10 20:50:43 -0700464 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 serr->port = port;
466
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700467 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300468 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if (sock_queue_err_skb(sk, skb))
471 kfree_skb(skb);
472}
473
Julian Anastasov34b99df2015-06-23 08:34:39 +0300474/* 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 */
477static 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 Bruijnc247f052015-03-07 20:33:22 -0500483/* 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 */
488static bool ipv4_datagram_support_cmsg(const struct sock *sk,
489 struct sk_buff *skb,
490 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500491{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500492 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500493
Willem de Bruijnc247f052015-03-07 20:33:22 -0500494 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 Bruijn1862d622017-04-12 19:24:35 -0400501 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500502 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
503 */
Willem de Bruijn1862d622017-04-12 19:24:35 -0400504 info = PKTINFO_SKB_CB(skb);
505 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
506 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500507 return false;
508
509 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500510 return true;
511}
512
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900513/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * Handle MSG_ERRQUEUE
515 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100516int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400519 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100520 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 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 Bruijn364a9e92014-08-31 21:30:27 -0400529 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100530 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto out;
532
533 copied = skb->len;
534 if (copied > len) {
535 msg->msg_flags |= MSG_TRUNC;
536 copied = len;
537 }
David S. Miller51f3d022014-11-05 16:46:40 -0500538 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700539 if (unlikely(err)) {
540 kfree_skb(skb);
541 return err;
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 sock_recv_timestamp(msg, sk, skb);
544
545 serr = SKB_EXT_ERR(skb);
546
Julian Anastasov34b99df2015-06-23 08:34:39 +0300547 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700549 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
550 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 sin->sin_port = serr->port;
552 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100553 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
556 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
557 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500558 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500559
Willem de Bruijnc247f052015-03-07 20:33:22 -0500560 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700562 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500563 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 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 Dumazet960a2622016-04-21 22:27:32 -0700574 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575out:
576 return err;
577}
578
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +0200579static void __ip_sock_set_tos(struct sock *sk, int val)
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
592void 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}
598EXPORT_SYMBOL(ip_sock_set_tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Christoph Hellwigc4e446b2020-05-28 07:12:27 +0200600void ip_sock_set_freebind(struct sock *sk)
601{
602 lock_sock(sk);
603 inet_sk(sk)->freebind = true;
604 release_sock(sk);
605}
606EXPORT_SYMBOL(ip_sock_set_freebind);
607
Christoph Hellwigdb45c0e2020-05-28 07:12:28 +0200608void ip_sock_set_recverr(struct sock *sk)
609{
610 lock_sock(sk);
611 inet_sk(sk)->recverr = true;
612 release_sock(sk);
613}
614EXPORT_SYMBOL(ip_sock_set_recverr);
615
Christoph Hellwig2de569b2020-05-28 07:12:29 +0200616int 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}
625EXPORT_SYMBOL(ip_sock_set_mtu_discover);
626
Christoph Hellwigc1f9ec52020-05-28 07:12:30 +0200627void 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}
633EXPORT_SYMBOL(ip_sock_set_pktinfo);
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700636 * Socket option code for IP. This is the end of the line after any
637 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300639static bool setsockopt_needs_rtnl(int optname)
640{
641 switch (optname) {
642 case IP_ADD_MEMBERSHIP:
643 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300644 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300645 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300646 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 Leitnerbaf606d2015-03-18 14:50:42 -0300651 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300652 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300653 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300654 case MCAST_LEAVE_SOURCE_GROUP:
655 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300656 return true;
657 }
658 return false;
659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Al Viroe986d4d2020-03-29 22:37:56 -0400661static 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 Viroe986d4d2020-03-29 22:37:56 -0400666 struct ip_msfilter *msf;
667 struct sockaddr_in *psin;
668 int err, i;
669
Gustavo A. R. Silva4167a962021-08-04 13:23:25 -0500670 msf = kmalloc(IP_MSFILTER_SIZE(numsrc), GFP_KERNEL);
Al Viroe986d4d2020-03-29 22:37:56 -0400671 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. Silva2d3e5ca2021-07-31 12:08:30 -0500686 msf->imsf_slist_flex[i] = psin->sin_addr.s_addr;
Al Viroe986d4d2020-03-29 22:37:56 -0400687 }
688 err = ip_mc_msfilter(sk, msf, ifindex);
689 kfree(msf);
690 return err;
691
692Eaddrnotavail:
693 kfree(msf);
694 return -EADDRNOTAVAIL;
695}
696
Christoph Hellwig89654c52020-07-23 08:08:58 +0200697static int copy_group_source_from_sockptr(struct group_source_req *greqs,
698 sockptr_t optval, int optlen)
Al Viro2bbf8c12020-04-27 10:49:26 -0400699{
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200700 if (in_compat_syscall()) {
701 struct compat_group_source_req gr32;
702
703 if (optlen != sizeof(gr32))
704 return -EINVAL;
Christoph Hellwig89654c52020-07-23 08:08:58 +0200705 if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200706 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 Hellwig89654c52020-07-23 08:08:58 +0200713 if (copy_from_sockptr(greqs, optval, sizeof(*greqs)))
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200714 return -EFAULT;
715 }
716
717 return 0;
718}
719
720static int do_mcast_group_source(struct sock *sk, int optname,
Christoph Hellwig89654c52020-07-23 08:08:58 +0200721 sockptr_t optval, int optlen)
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200722{
723 struct group_source_req greqs;
Al Viro2bbf8c12020-04-27 10:49:26 -0400724 struct ip_mreq_source mreqs;
725 struct sockaddr_in *psin;
726 int omode, add, err;
727
Christoph Hellwig89654c52020-07-23 08:08:58 +0200728 err = copy_group_source_from_sockptr(&greqs, optval, optlen);
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200729 if (err)
730 return err;
731
732 if (greqs.gsr_group.ss_family != AF_INET ||
733 greqs.gsr_source.ss_family != AF_INET)
Al Viro2bbf8c12020-04-27 10:49:26 -0400734 return -EADDRNOTAVAIL;
735
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200736 psin = (struct sockaddr_in *)&greqs.gsr_group;
Al Viro2bbf8c12020-04-27 10:49:26 -0400737 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200738 psin = (struct sockaddr_in *)&greqs.gsr_source;
Al Viro2bbf8c12020-04-27 10:49:26 -0400739 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 Hellwigb6238c02020-07-17 08:23:26 +0200751 psin = (struct sockaddr_in *)&greqs.gsr_group;
Al Viro2bbf8c12020-04-27 10:49:26 -0400752 mreq.imr_multiaddr = psin->sin_addr;
753 mreq.imr_address.s_addr = 0;
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200754 mreq.imr_ifindex = greqs.gsr_interface;
Al Viro2bbf8c12020-04-27 10:49:26 -0400755 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
756 if (err && err != -EADDRINUSE)
757 return err;
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200758 greqs.gsr_interface = mreq.imr_ifindex;
Al Viro2bbf8c12020-04-27 10:49:26 -0400759 omode = MCAST_INCLUDE;
760 add = 1;
761 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
762 omode = MCAST_INCLUDE;
763 add = 0;
764 }
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200765 return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface);
Al Viro2bbf8c12020-04-27 10:49:26 -0400766}
767
Christoph Hellwig89654c52020-07-23 08:08:58 +0200768static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen)
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200769{
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 Hellwig89654c52020-07-23 08:08:58 +0200778 gsf = memdup_sockptr(optval, optlen);
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200779 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. Silvadb243b72021-08-04 15:45:36 -0500793 gsf->gf_fmode, &gsf->gf_group,
794 gsf->gf_slist_flex);
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200795out_free_gsf:
796 kfree(gsf);
797 return err;
798}
799
Christoph Hellwig89654c52020-07-23 08:08:58 +0200800static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200801 int optlen)
802{
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -0500803 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200804 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. Silvadb243b72021-08-04 15:45:36 -0500817 gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200818
819 err = -EFAULT;
Christoph Hellwig89654c52020-07-23 08:08:58 +0200820 if (copy_from_sockptr(gf32, optval, optlen))
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200821 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. Silvadb243b72021-08-04 15:45:36 -0500830 if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200831 goto out_free_gsf;
832
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200833 /* numsrc >= (4G-140)/128 overflow in 32 bits */
834 err = -ENOBUFS;
835 if (n > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200836 goto out_free_gsf;
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200837 err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode,
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -0500838 &gf32->gf_group, gf32->gf_slist_flex);
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200839out_free_gsf:
840 kfree(p);
841 return err;
842}
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200843
Christoph Hellwig02caad72020-07-17 08:23:25 +0200844static int ip_mcast_join_leave(struct sock *sk, int optname,
Christoph Hellwig89654c52020-07-23 08:08:58 +0200845 sockptr_t optval, int optlen)
Christoph Hellwig02caad72020-07-17 08:23:25 +0200846{
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 Hellwig89654c52020-07-23 08:08:58 +0200853 if (copy_from_sockptr(&greq, optval, sizeof(greq)))
Christoph Hellwig02caad72020-07-17 08:23:25 +0200854 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 Hellwig02caad72020-07-17 08:23:25 +0200866static int compat_ip_mcast_join_leave(struct sock *sk, int optname,
Christoph Hellwig89654c52020-07-23 08:08:58 +0200867 sockptr_t optval, int optlen)
Christoph Hellwig02caad72020-07-17 08:23:25 +0200868{
869 struct compat_group_req greq;
870 struct ip_mreqn mreq = { };
871 struct sockaddr_in *psin;
Christoph Hellwig02caad72020-07-17 08:23:25 +0200872
873 if (optlen < sizeof(struct compat_group_req))
874 return -EINVAL;
Christoph Hellwig89654c52020-07-23 08:08:58 +0200875 if (copy_from_sockptr(&greq, optval, sizeof(greq)))
Christoph Hellwig02caad72020-07-17 08:23:25 +0200876 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 Hellwig02caad72020-07-17 08:23:25 +0200884 if (optname == MCAST_JOIN_GROUP)
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200885 return ip_mc_join_group(sk, &mreq);
886 return ip_mc_leave_group(sk, &mreq);
Christoph Hellwig02caad72020-07-17 08:23:25 +0200887}
Christoph Hellwig02caad72020-07-17 08:23:25 +0200888
Christoph Hellwig89654c52020-07-23 08:08:58 +0200889static int do_ip_setsockopt(struct sock *sk, int level, int optname,
890 sockptr_t optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200893 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800894 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300895 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Xi Wang0c9f79b2012-11-11 11:20:01 +0000897 switch (optname) {
898 case IP_PKTINFO:
899 case IP_RECVTTL:
900 case IP_RECVOPTS:
901 case IP_RECVTOS:
902 case IP_RETOPTS:
903 case IP_TOS:
904 case IP_TTL:
905 case IP_HDRINCL:
906 case IP_MTU_DISCOVER:
907 case IP_RECVERR:
908 case IP_ROUTER_ALERT:
909 case IP_FREEBIND:
910 case IP_PASSSEC:
911 case IP_TRANSPARENT:
912 case IP_MINTTL:
913 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700914 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000915 case IP_UNICAST_IF:
916 case IP_MULTICAST_TTL:
917 case IP_MULTICAST_ALL:
918 case IP_MULTICAST_LOOP:
919 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800920 case IP_CHECKSUM:
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400921 case IP_RECVFRAGSIZE:
Willem de Bruijneba75c52020-07-10 09:29:02 -0400922 case IP_RECVERR_RFC4884:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (optlen >= sizeof(int)) {
Christoph Hellwig89654c52020-07-23 08:08:58 +0200924 if (copy_from_sockptr(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -EFAULT;
926 } else if (optlen >= sizeof(char)) {
927 unsigned char ucval;
928
Christoph Hellwig89654c52020-07-23 08:08:58 +0200929 if (copy_from_sockptr(&ucval, optval, sizeof(ucval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return -EFAULT;
931 val = (int) ucval;
932 }
933 }
934
935 /* If optlen==0, it is equivalent to val == 0 */
936
Kirill Tkhai05269472018-03-22 12:45:12 +0300937 if (optname == IP_ROUTER_ALERT)
938 return ip_ra_control(sk, val ? 1 : 0, NULL);
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800939 if (ip_mroute_opt(optname))
Christoph Hellwig89654c52020-07-23 08:08:58 +0200940 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300943 if (needs_rtnl)
944 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 lock_sock(sk);
946
947 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800948 case IP_OPTIONS:
949 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000950 struct ip_options_rcu *old, *opt = NULL;
951
roel kluin65a1c4f2009-10-23 05:59:21 +0000952 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800953 goto e_inval;
Christoph Hellwig89654c52020-07-23 08:08:58 +0200954 err = ip_options_get(sock_net(sk), &opt, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800955 if (err)
956 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000957 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200958 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800959 if (inet->is_icsk) {
960 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000961#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800962 if (sk->sk_family == PF_INET ||
963 (!((1 << sk->sk_state) &
964 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000965 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000967 if (old)
968 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800969 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000970 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800971 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000972#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000976 rcu_assign_pointer(inet->inet_opt, opt);
977 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800978 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800979 break;
980 }
981 case IP_PKTINFO:
982 if (val)
983 inet->cmsg_flags |= IP_CMSG_PKTINFO;
984 else
985 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
986 break;
987 case IP_RECVTTL:
988 if (val)
989 inet->cmsg_flags |= IP_CMSG_TTL;
990 else
991 inet->cmsg_flags &= ~IP_CMSG_TTL;
992 break;
993 case IP_RECVTOS:
994 if (val)
995 inet->cmsg_flags |= IP_CMSG_TOS;
996 else
997 inet->cmsg_flags &= ~IP_CMSG_TOS;
998 break;
999 case IP_RECVOPTS:
1000 if (val)
1001 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
1002 else
1003 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
1004 break;
1005 case IP_RETOPTS:
1006 if (val)
1007 inet->cmsg_flags |= IP_CMSG_RETOPTS;
1008 else
1009 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
1010 break;
1011 case IP_PASSSEC:
1012 if (val)
1013 inet->cmsg_flags |= IP_CMSG_PASSSEC;
1014 else
1015 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
1016 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001017 case IP_RECVORIGDSTADDR:
1018 if (val)
1019 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
1020 else
1021 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
1022 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001023 case IP_CHECKSUM:
1024 if (val) {
1025 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
1026 inet_inc_convert_csum(sk);
1027 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
1028 }
1029 } else {
1030 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
1031 inet_dec_convert_csum(sk);
1032 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
1033 }
1034 }
1035 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001036 case IP_RECVFRAGSIZE:
1037 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
1038 goto e_inval;
1039 if (val)
1040 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
1041 else
1042 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
1043 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001044 case IP_TOS: /* This sets both TOS and Precedence */
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +02001045 __ip_sock_set_tos(sk, val);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001046 break;
1047 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001048 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001049 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +00001050 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001051 goto e_inval;
1052 inet->uc_ttl = val;
1053 break;
1054 case IP_HDRINCL:
1055 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001058 }
1059 inet->hdrincl = val ? 1 : 0;
1060 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +00001061 case IP_NODEFRAG:
1062 if (sk->sk_type != SOCK_RAW) {
1063 err = -ENOPROTOOPT;
1064 break;
1065 }
1066 inet->nodefrag = val ? 1 : 0;
1067 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001068 case IP_BIND_ADDRESS_NO_PORT:
1069 inet->bind_address_no_port = val ? 1 : 0;
1070 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001071 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +01001072 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001073 goto e_inval;
1074 inet->pmtudisc = val;
1075 break;
1076 case IP_RECVERR:
1077 inet->recverr = !!val;
1078 if (!val)
1079 skb_queue_purge(&sk->sk_error_queue);
1080 break;
Willem de Bruijneba75c52020-07-10 09:29:02 -04001081 case IP_RECVERR_RFC4884:
1082 if (val < 0 || val > 1)
1083 goto e_inval;
1084 inet->recverr_rfc4884 = !!val;
1085 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001086 case IP_MULTICAST_TTL:
1087 if (sk->sk_type == SOCK_STREAM)
1088 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001089 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001090 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001091 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001092 val = 1;
1093 if (val < 0 || val > 255)
1094 goto e_inval;
1095 inet->mc_ttl = val;
1096 break;
1097 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001098 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001099 goto e_inval;
1100 inet->mc_loop = !!val;
1101 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001102 case IP_UNICAST_IF:
1103 {
1104 struct net_device *dev = NULL;
1105 int ifindex;
David Ahern9515a2e2018-01-24 19:37:38 -08001106 int midx;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001107
1108 if (optlen != sizeof(int))
1109 goto e_inval;
1110
1111 ifindex = (__force int)ntohl((__force __be32)val);
1112 if (ifindex == 0) {
1113 inet->uc_index = 0;
1114 err = 0;
1115 break;
1116 }
1117
1118 dev = dev_get_by_index(sock_net(sk), ifindex);
1119 err = -EADDRNOTAVAIL;
1120 if (!dev)
1121 break;
David Ahern9515a2e2018-01-24 19:37:38 -08001122
1123 midx = l3mdev_master_ifindex(dev);
Erich E. Hoover76e21052012-02-08 09:11:07 +00001124 dev_put(dev);
1125
1126 err = -EINVAL;
Miaohe Linfdf19232020-08-25 08:10:37 -04001127 if (sk->sk_bound_dev_if && midx != sk->sk_bound_dev_if)
Erich E. Hoover76e21052012-02-08 09:11:07 +00001128 break;
1129
1130 inet->uc_index = ifindex;
1131 err = 0;
1132 break;
1133 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001134 case IP_MULTICAST_IF:
1135 {
1136 struct ip_mreqn mreq;
1137 struct net_device *dev = NULL;
David Ahern7bb387c2016-12-29 15:39:37 -08001138 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001139
1140 if (sk->sk_type == SOCK_STREAM)
1141 goto e_inval;
1142 /*
1143 * Check the arguments are allowable
1144 */
1145
Shan Wei09159212009-09-22 15:41:10 +00001146 if (optlen < sizeof(struct in_addr))
1147 goto e_inval;
1148
Stephen Hemminger132adf52007-03-08 20:44:43 -08001149 err = -EFAULT;
1150 if (optlen >= sizeof(struct ip_mreqn)) {
Christoph Hellwig89654c52020-07-23 08:08:58 +02001151 if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001152 break;
1153 } else {
1154 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +00001155 if (optlen >= sizeof(struct ip_mreq)) {
Christoph Hellwig89654c52020-07-23 08:08:58 +02001156 if (copy_from_sockptr(&mreq, optval,
1157 sizeof(struct ip_mreq)))
Jiri Pirko3a084dd2012-05-03 22:37:45 +00001158 break;
1159 } else if (optlen >= sizeof(struct in_addr)) {
Christoph Hellwig89654c52020-07-23 08:08:58 +02001160 if (copy_from_sockptr(&mreq.imr_address, optval,
1161 sizeof(struct in_addr)))
Jiri Pirko3a084dd2012-05-03 22:37:45 +00001162 break;
1163 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001164 }
1165
1166 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -07001167 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001168 inet->mc_index = 0;
1169 inet->mc_addr = 0;
1170 err = 0;
1171 break;
1172 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001173 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +00001174 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001175 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001176 } else
Eric Dumazet55b80502009-10-19 06:41:58 +00001177 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001178
1179
1180 err = -EADDRNOTAVAIL;
1181 if (!dev)
1182 break;
David Ahern7bb387c2016-12-29 15:39:37 -08001183
1184 midx = l3mdev_master_ifindex(dev);
1185
Eric Dumazet55b80502009-10-19 06:41:58 +00001186 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001187
1188 err = -EINVAL;
1189 if (sk->sk_bound_dev_if &&
David Ahern7bb387c2016-12-29 15:39:37 -08001190 mreq.imr_ifindex != sk->sk_bound_dev_if &&
Miaohe Linfdf19232020-08-25 08:10:37 -04001191 midx != sk->sk_bound_dev_if)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001192 break;
1193
1194 inet->mc_index = mreq.imr_ifindex;
1195 inet->mc_addr = mreq.imr_address.s_addr;
1196 err = 0;
1197 break;
1198 }
1199
1200 case IP_ADD_MEMBERSHIP:
1201 case IP_DROP_MEMBERSHIP:
1202 {
1203 struct ip_mreqn mreq;
1204
Flavio Leitnera96fb492007-08-24 22:16:39 -07001205 err = -EPROTO;
1206 if (inet_sk(sk)->is_icsk)
1207 break;
1208
Stephen Hemminger132adf52007-03-08 20:44:43 -08001209 if (optlen < sizeof(struct ip_mreq))
1210 goto e_inval;
1211 err = -EFAULT;
1212 if (optlen >= sizeof(struct ip_mreqn)) {
Christoph Hellwig89654c52020-07-23 08:08:58 +02001213 if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001214 break;
1215 } else {
1216 memset(&mreq, 0, sizeof(mreq));
Christoph Hellwig89654c52020-07-23 08:08:58 +02001217 if (copy_from_sockptr(&mreq, optval,
1218 sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001219 break;
1220 }
1221
1222 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001223 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001224 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001225 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001226 break;
1227 }
1228 case IP_MSFILTER:
1229 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001230 struct ip_msfilter *msf;
1231
Gustavo A. R. Silva4167a962021-08-04 13:23:25 -05001232 if (optlen < IP_MSFILTER_SIZE(0))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001233 goto e_inval;
1234 if (optlen > sysctl_optmem_max) {
1235 err = -ENOBUFS;
1236 break;
1237 }
Christoph Hellwig89654c52020-07-23 08:08:58 +02001238 msf = memdup_sockptr(optval, optlen);
Al Viroa2c841d2017-05-13 18:26:06 -04001239 if (IS_ERR(msf)) {
1240 err = PTR_ERR(msf);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001241 break;
1242 }
1243 /* numsrc >= (1G-4) overflow in 32 bits */
1244 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001245 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001246 kfree(msf);
1247 err = -ENOBUFS;
1248 break;
1249 }
Gustavo A. R. Silva4167a962021-08-04 13:23:25 -05001250 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001251 kfree(msf);
1252 err = -EINVAL;
1253 break;
1254 }
1255 err = ip_mc_msfilter(sk, msf, 0);
1256 kfree(msf);
1257 break;
1258 }
1259 case IP_BLOCK_SOURCE:
1260 case IP_UNBLOCK_SOURCE:
1261 case IP_ADD_SOURCE_MEMBERSHIP:
1262 case IP_DROP_SOURCE_MEMBERSHIP:
1263 {
1264 struct ip_mreq_source mreqs;
1265 int omode, add;
1266
1267 if (optlen != sizeof(struct ip_mreq_source))
1268 goto e_inval;
Christoph Hellwig89654c52020-07-23 08:08:58 +02001269 if (copy_from_sockptr(&mreqs, optval, sizeof(mreqs))) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001270 err = -EFAULT;
1271 break;
1272 }
1273 if (optname == IP_BLOCK_SOURCE) {
1274 omode = MCAST_EXCLUDE;
1275 add = 1;
1276 } else if (optname == IP_UNBLOCK_SOURCE) {
1277 omode = MCAST_EXCLUDE;
1278 add = 0;
1279 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
1280 struct ip_mreqn mreq;
1281
1282 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
1283 mreq.imr_address.s_addr = mreqs.imr_interface;
1284 mreq.imr_ifindex = 0;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001285 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001286 if (err && err != -EADDRINUSE)
1287 break;
1288 omode = MCAST_INCLUDE;
1289 add = 1;
1290 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1291 omode = MCAST_INCLUDE;
1292 add = 0;
1293 }
1294 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1295 break;
1296 }
1297 case MCAST_JOIN_GROUP:
1298 case MCAST_LEAVE_GROUP:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001299 if (in_compat_syscall())
1300 err = compat_ip_mcast_join_leave(sk, optname, optval,
1301 optlen);
1302 else
1303 err = ip_mcast_join_leave(sk, optname, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001304 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 case MCAST_JOIN_SOURCE_GROUP:
1306 case MCAST_LEAVE_SOURCE_GROUP:
1307 case MCAST_BLOCK_SOURCE:
1308 case MCAST_UNBLOCK_SOURCE:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001309 err = do_mcast_group_source(sk, optname, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001310 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001311 case MCAST_MSFILTER:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001312 if (in_compat_syscall())
1313 err = compat_ip_set_mcast_msfilter(sk, optval, optlen);
1314 else
1315 err = ip_set_mcast_msfilter(sk, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001316 break;
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001317 case IP_MULTICAST_ALL:
1318 if (optlen < 1)
1319 goto e_inval;
1320 if (val != 0 && val != 1)
1321 goto e_inval;
1322 inet->mc_all = val;
1323 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001324
1325 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001326 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001327 goto e_inval;
1328 inet->freebind = !!val;
1329 break;
1330
1331 case IP_IPSEC_POLICY:
1332 case IP_XFRM_POLICY:
1333 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001334 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001335 break;
Christoph Hellwig89654c52020-07-23 08:08:58 +02001336 err = xfrm_user_policy(sk, optname, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001337 break;
1338
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001339 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001340 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1341 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001342 err = -EPERM;
1343 break;
1344 }
1345 if (optlen < 1)
1346 goto e_inval;
1347 inet->transparent = !!val;
1348 break;
1349
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001350 case IP_MINTTL:
1351 if (optlen < 1)
1352 goto e_inval;
1353 if (val < 0 || val > 255)
1354 goto e_inval;
Eric Dumazet14834c42021-10-25 09:48:23 -07001355 /* tcp_v4_err() and tcp_v4_rcv() might read min_ttl
1356 * while we are changint it.
1357 */
1358 WRITE_ONCE(inet->min_ttl, val);
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001359 break;
1360
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 default:
1362 err = -ENOPROTOOPT;
1363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001366 if (needs_rtnl)
1367 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return err;
1369
1370e_inval:
1371 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001372 if (needs_rtnl)
1373 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -EINVAL;
1375}
1376
Eric Dumazetf84af322010-04-28 15:31:51 -07001377/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001378 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001379 * @sk: socket
1380 * @skb: buffer
1381 *
David S. Miller35ebf652012-06-28 03:59:11 -07001382 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1383 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001384 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001385 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001386void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001387{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001388 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001389 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1390 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001391
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001392 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001393 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1394 * which has interface index (iif) as the first member of the
1395 * underlying inet{6}_skb_parm struct. This code then overlays
1396 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001397 * element so the iif is picked up from the prior IPCB. If iif
1398 * is the loopback interface, then return the sending interface
1399 * (e.g., process binds socket to eth0 for Tx which is
1400 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001401 */
David Aherncbea8f02017-09-13 17:11:37 -07001402 struct rtable *rt = skb_rtable(skb);
1403 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1404
1405 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001406 pktinfo->ipi_ifindex = inet_iif(skb);
David Aherncbea8f02017-09-13 17:11:37 -07001407 else if (l3slave && rt && rt->rt_iif)
1408 pktinfo->ipi_ifindex = rt->rt_iif;
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001409
David S. Miller35ebf652012-06-28 03:59:11 -07001410 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001411 } else {
1412 pktinfo->ipi_ifindex = 0;
1413 pktinfo->ipi_spec_dst.s_addr = 0;
1414 }
Paolo Abeni61a10302017-08-03 18:07:07 +02001415 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001416}
Eric Dumazetf84af322010-04-28 15:31:51 -07001417
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001418int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1419 unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001420{
1421 int err;
1422
1423 if (level != SOL_IP)
1424 return -ENOPROTOOPT;
1425
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001426 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
Taehee Yoo97adadd2018-11-05 22:31:41 +09001427#if IS_ENABLED(CONFIG_BPFILTER_UMH)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001428 if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1429 optname < BPFILTER_IPT_SET_MAX)
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001430 err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001431#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001432#ifdef CONFIG_NETFILTER
1433 /* we need to exclude all possible ENOPROTOOPTs except default case */
1434 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001435 optname != IP_IPSEC_POLICY &&
1436 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001437 !ip_mroute_opt(optname))
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001438 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001439#endif
1440 return err;
1441}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001442EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001445 * Get the options. Note for future reference. The GET of IP options gets
1446 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
1448
WANG Cong87e9f032015-11-03 15:41:16 -08001449static bool getsockopt_needs_rtnl(int optname)
1450{
1451 switch (optname) {
1452 case IP_MSFILTER:
1453 case MCAST_MSFILTER:
1454 return true;
1455 }
1456 return false;
1457}
1458
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001459static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
1460 int __user *optlen, int len)
1461{
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -05001462 const int size0 = offsetof(struct group_filter, gf_slist_flex);
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001463 struct group_filter __user *p = optval;
1464 struct group_filter gsf;
1465 int num;
1466 int err;
1467
1468 if (len < size0)
1469 return -EINVAL;
1470 if (copy_from_user(&gsf, p, size0))
1471 return -EFAULT;
1472
1473 num = gsf.gf_numsrc;
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -05001474 err = ip_mc_gsfget(sk, &gsf, p->gf_slist_flex);
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001475 if (err)
1476 return err;
1477 if (gsf.gf_numsrc < num)
1478 num = gsf.gf_numsrc;
1479 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1480 copy_to_user(p, &gsf, size0))
1481 return -EFAULT;
1482 return 0;
1483}
1484
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001485static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001486 int __user *optlen, int len)
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001487{
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -05001488 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001489 struct compat_group_filter __user *p = optval;
1490 struct compat_group_filter gf32;
1491 struct group_filter gf;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001492 int num;
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001493 int err;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001494
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001495 if (len < size0)
1496 return -EINVAL;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001497 if (copy_from_user(&gf32, p, size0))
1498 return -EFAULT;
1499
1500 gf.gf_interface = gf32.gf_interface;
1501 gf.gf_fmode = gf32.gf_fmode;
1502 num = gf.gf_numsrc = gf32.gf_numsrc;
1503 gf.gf_group = gf32.gf_group;
1504
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -05001505 err = ip_mc_gsfget(sk, &gf, p->gf_slist_flex);
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001506 if (err)
1507 return err;
1508 if (gf.gf_numsrc < num)
1509 num = gf.gf_numsrc;
1510 len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32));
1511 if (put_user(len, optlen) ||
1512 put_user(gf.gf_fmode, &p->gf_fmode) ||
1513 put_user(gf.gf_numsrc, &p->gf_numsrc))
1514 return -EFAULT;
1515 return 0;
1516}
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001517
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001518static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001519 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001522 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1523 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001525
Stephen Hemminger132adf52007-03-08 20:44:43 -08001526 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return -EOPNOTSUPP;
1528
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001529 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001530 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Jianjun Kong09cb1052008-11-03 00:27:11 -08001532 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001534 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001536
WANG Cong87e9f032015-11-03 15:41:16 -08001537 if (needs_rtnl)
1538 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 lock_sock(sk);
1540
Stephen Hemminger132adf52007-03-08 20:44:43 -08001541 switch (optname) {
1542 case IP_OPTIONS:
1543 {
1544 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001545 struct ip_options *opt = (struct ip_options *)optbuf;
1546 struct ip_options_rcu *inet_opt;
1547
1548 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001549 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001550 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001551 if (inet_opt)
1552 memcpy(optbuf, &inet_opt->opt,
1553 sizeof(struct ip_options) +
1554 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001555 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Stephen Hemminger132adf52007-03-08 20:44:43 -08001557 if (opt->optlen == 0)
1558 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Stephen Hemminger132adf52007-03-08 20:44:43 -08001560 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Stephen Hemminger132adf52007-03-08 20:44:43 -08001562 len = min_t(unsigned int, len, opt->optlen);
1563 if (put_user(len, optlen))
1564 return -EFAULT;
1565 if (copy_to_user(optval, opt->__data, len))
1566 return -EFAULT;
1567 return 0;
1568 }
1569 case IP_PKTINFO:
1570 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1571 break;
1572 case IP_RECVTTL:
1573 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1574 break;
1575 case IP_RECVTOS:
1576 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1577 break;
1578 case IP_RECVOPTS:
1579 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1580 break;
1581 case IP_RETOPTS:
1582 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1583 break;
1584 case IP_PASSSEC:
1585 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1586 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001587 case IP_RECVORIGDSTADDR:
1588 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1589 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001590 case IP_CHECKSUM:
1591 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1592 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001593 case IP_RECVFRAGSIZE:
1594 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1595 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001596 case IP_TOS:
1597 val = inet->tos;
1598 break;
1599 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001600 {
1601 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001602 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001603 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001604 inet->uc_ttl);
1605 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001606 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001607 case IP_HDRINCL:
1608 val = inet->hdrincl;
1609 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001610 case IP_NODEFRAG:
1611 val = inet->nodefrag;
1612 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001613 case IP_BIND_ADDRESS_NO_PORT:
1614 val = inet->bind_address_no_port;
1615 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001616 case IP_MTU_DISCOVER:
1617 val = inet->pmtudisc;
1618 break;
1619 case IP_MTU:
1620 {
1621 struct dst_entry *dst;
1622 val = 0;
1623 dst = sk_dst_get(sk);
1624 if (dst) {
1625 val = dst_mtu(dst);
1626 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001628 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001630 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001632 break;
1633 }
1634 case IP_RECVERR:
1635 val = inet->recverr;
1636 break;
Willem de Bruijneba75c52020-07-10 09:29:02 -04001637 case IP_RECVERR_RFC4884:
1638 val = inet->recverr_rfc4884;
1639 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001640 case IP_MULTICAST_TTL:
1641 val = inet->mc_ttl;
1642 break;
1643 case IP_MULTICAST_LOOP:
1644 val = inet->mc_loop;
1645 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001646 case IP_UNICAST_IF:
1647 val = (__force int)htonl((__u32) inet->uc_index);
1648 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001649 case IP_MULTICAST_IF:
1650 {
1651 struct in_addr addr;
1652 len = min_t(unsigned int, len, sizeof(struct in_addr));
1653 addr.s_addr = inet->mc_addr;
1654 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Stephen Hemminger132adf52007-03-08 20:44:43 -08001656 if (put_user(len, optlen))
1657 return -EFAULT;
1658 if (copy_to_user(optval, &addr, len))
1659 return -EFAULT;
1660 return 0;
1661 }
1662 case IP_MSFILTER:
1663 {
1664 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001665
Gustavo A. R. Silva4167a962021-08-04 13:23:25 -05001666 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001667 err = -EINVAL;
1668 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Gustavo A. R. Silva4167a962021-08-04 13:23:25 -05001670 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001671 err = -EFAULT;
1672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001674 err = ip_mc_msfget(sk, &msf,
1675 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001676 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001677 }
1678 case MCAST_MSFILTER:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001679 if (in_compat_syscall())
1680 err = compat_ip_get_mcast_msfilter(sk, optval, optlen,
1681 len);
1682 else
1683 err = ip_get_mcast_msfilter(sk, optval, optlen, len);
WANG Cong87e9f032015-11-03 15:41:16 -08001684 goto out;
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001685 case IP_MULTICAST_ALL:
1686 val = inet->mc_all;
1687 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001688 case IP_PKTOPTIONS:
1689 {
1690 struct msghdr msg;
1691
1692 release_sock(sk);
1693
1694 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001696
Christoph Hellwig1f466e12020-05-11 13:59:13 +02001697 msg.msg_control_is_user = true;
1698 msg.msg_control_user = optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001699 msg.msg_controllen = len;
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001700 msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001701
1702 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1703 struct in_pktinfo info;
1704
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001705 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1706 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001707 info.ipi_ifindex = inet->mc_index;
1708 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1709 }
1710 if (inet->cmsg_flags & IP_CMSG_TTL) {
1711 int hlim = inet->mc_ttl;
1712 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1713 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001714 if (inet->cmsg_flags & IP_CMSG_TOS) {
1715 int tos = inet->rcv_tos;
1716 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1717 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001718 len -= msg.msg_controllen;
1719 return put_user(len, optlen);
1720 }
1721 case IP_FREEBIND:
1722 val = inet->freebind;
1723 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001724 case IP_TRANSPARENT:
1725 val = inet->transparent;
1726 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001727 case IP_MINTTL:
1728 val = inet->min_ttl;
1729 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001730 default:
1731 release_sock(sk);
1732 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001735
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001736 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 unsigned char ucval = (unsigned char)val;
1738 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001739 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001741 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return -EFAULT;
1743 } else {
1744 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001745 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001747 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return -EFAULT;
1749 }
1750 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001751
1752out:
1753 release_sock(sk);
1754 if (needs_rtnl)
1755 rtnl_unlock();
1756 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001759int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001760 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001761{
1762 int err;
1763
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001764 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1765
Taehee Yoo97adadd2018-11-05 22:31:41 +09001766#if IS_ENABLED(CONFIG_BPFILTER_UMH)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001767 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1768 optname < BPFILTER_IPT_GET_MAX)
1769 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1770#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001771#ifdef CONFIG_NETFILTER
1772 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001773 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1774 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001775 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001776
Jianjun Kong09cb1052008-11-03 00:27:11 -08001777 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001778 return -EFAULT;
1779
Paolo Abeni01ea3062018-02-08 12:19:00 +01001780 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001781 if (err >= 0)
1782 err = put_user(len, optlen);
1783 return err;
1784 }
1785#endif
1786 return err;
1787}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001788EXPORT_SYMBOL(ip_getsockopt);