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