xref: /openbmc/linux/net/ipv4/ip_sockglue.c (revision 29c37341)
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, msg, 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 
437 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
438 {
439 	struct inet_sock *inet = inet_sk(sk);
440 	struct sock_exterr_skb *serr;
441 	struct iphdr *iph;
442 	struct sk_buff *skb;
443 
444 	if (!inet->recverr)
445 		return;
446 
447 	skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
448 	if (!skb)
449 		return;
450 
451 	skb_put(skb, sizeof(struct iphdr));
452 	skb_reset_network_header(skb);
453 	iph = ip_hdr(skb);
454 	iph->daddr = daddr;
455 
456 	serr = SKB_EXT_ERR(skb);
457 	serr->ee.ee_errno = err;
458 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
459 	serr->ee.ee_type = 0;
460 	serr->ee.ee_code = 0;
461 	serr->ee.ee_pad = 0;
462 	serr->ee.ee_info = info;
463 	serr->ee.ee_data = 0;
464 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
465 	serr->port = port;
466 
467 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
468 	skb_reset_transport_header(skb);
469 
470 	if (sock_queue_err_skb(sk, skb))
471 		kfree_skb(skb);
472 }
473 
474 /* For some errors we have valid addr_offset even with zero payload and
475  * zero port. Also, addr_offset should be supported if port is set.
476  */
477 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
478 {
479 	return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
480 	       serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
481 }
482 
483 /* IPv4 supports cmsg on all imcp errors and some timestamps
484  *
485  * Timestamp code paths do not initialize the fields expected by cmsg:
486  * the PKTINFO fields in skb->cb[]. Fill those in here.
487  */
488 static bool ipv4_datagram_support_cmsg(const struct sock *sk,
489 				       struct sk_buff *skb,
490 				       int ee_origin)
491 {
492 	struct in_pktinfo *info;
493 
494 	if (ee_origin == SO_EE_ORIGIN_ICMP)
495 		return true;
496 
497 	if (ee_origin == SO_EE_ORIGIN_LOCAL)
498 		return false;
499 
500 	/* Support IP_PKTINFO on tstamp packets if requested, to correlate
501 	 * timestamp with egress dev. Not possible for packets without iif
502 	 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
503 	 */
504 	info = PKTINFO_SKB_CB(skb);
505 	if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
506 	    !info->ipi_ifindex)
507 		return false;
508 
509 	info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
510 	return true;
511 }
512 
513 /*
514  *	Handle MSG_ERRQUEUE
515  */
516 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
517 {
518 	struct sock_exterr_skb *serr;
519 	struct sk_buff *skb;
520 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
521 	struct {
522 		struct sock_extended_err ee;
523 		struct sockaddr_in	 offender;
524 	} errhdr;
525 	int err;
526 	int copied;
527 
528 	err = -EAGAIN;
529 	skb = sock_dequeue_err_skb(sk);
530 	if (!skb)
531 		goto out;
532 
533 	copied = skb->len;
534 	if (copied > len) {
535 		msg->msg_flags |= MSG_TRUNC;
536 		copied = len;
537 	}
538 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
539 	if (unlikely(err)) {
540 		kfree_skb(skb);
541 		return err;
542 	}
543 	sock_recv_timestamp(msg, sk, skb);
544 
545 	serr = SKB_EXT_ERR(skb);
546 
547 	if (sin && ipv4_datagram_support_addr(serr)) {
548 		sin->sin_family = AF_INET;
549 		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
550 						   serr->addr_offset);
551 		sin->sin_port = serr->port;
552 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
553 		*addr_len = sizeof(*sin);
554 	}
555 
556 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
557 	sin = &errhdr.offender;
558 	memset(sin, 0, sizeof(*sin));
559 
560 	if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
561 		sin->sin_family = AF_INET;
562 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
563 		if (inet_sk(sk)->cmsg_flags)
564 			ip_cmsg_recv(msg, skb);
565 	}
566 
567 	put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
568 
569 	/* Now we could try to dump offended packet options */
570 
571 	msg->msg_flags |= MSG_ERRQUEUE;
572 	err = copied;
573 
574 	consume_skb(skb);
575 out:
576 	return err;
577 }
578 
579 static 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 
592 void ip_sock_set_tos(struct sock *sk, int val)
593 {
594 	lock_sock(sk);
595 	__ip_sock_set_tos(sk, val);
596 	release_sock(sk);
597 }
598 EXPORT_SYMBOL(ip_sock_set_tos);
599 
600 void ip_sock_set_freebind(struct sock *sk)
601 {
602 	lock_sock(sk);
603 	inet_sk(sk)->freebind = true;
604 	release_sock(sk);
605 }
606 EXPORT_SYMBOL(ip_sock_set_freebind);
607 
608 void ip_sock_set_recverr(struct sock *sk)
609 {
610 	lock_sock(sk);
611 	inet_sk(sk)->recverr = true;
612 	release_sock(sk);
613 }
614 EXPORT_SYMBOL(ip_sock_set_recverr);
615 
616 int ip_sock_set_mtu_discover(struct sock *sk, int val)
617 {
618 	if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
619 		return -EINVAL;
620 	lock_sock(sk);
621 	inet_sk(sk)->pmtudisc = val;
622 	release_sock(sk);
623 	return 0;
624 }
625 EXPORT_SYMBOL(ip_sock_set_mtu_discover);
626 
627 void ip_sock_set_pktinfo(struct sock *sk)
628 {
629 	lock_sock(sk);
630 	inet_sk(sk)->cmsg_flags |= IP_CMSG_PKTINFO;
631 	release_sock(sk);
632 }
633 EXPORT_SYMBOL(ip_sock_set_pktinfo);
634 
635 /*
636  *	Socket option code for IP. This is the end of the line after any
637  *	TCP,UDP etc options on an IP socket.
638  */
639 static bool setsockopt_needs_rtnl(int optname)
640 {
641 	switch (optname) {
642 	case IP_ADD_MEMBERSHIP:
643 	case IP_ADD_SOURCE_MEMBERSHIP:
644 	case IP_BLOCK_SOURCE:
645 	case IP_DROP_MEMBERSHIP:
646 	case IP_DROP_SOURCE_MEMBERSHIP:
647 	case IP_MSFILTER:
648 	case IP_UNBLOCK_SOURCE:
649 	case MCAST_BLOCK_SOURCE:
650 	case MCAST_MSFILTER:
651 	case MCAST_JOIN_GROUP:
652 	case MCAST_JOIN_SOURCE_GROUP:
653 	case MCAST_LEAVE_GROUP:
654 	case MCAST_LEAVE_SOURCE_GROUP:
655 	case MCAST_UNBLOCK_SOURCE:
656 		return true;
657 	}
658 	return false;
659 }
660 
661 static int set_mcast_msfilter(struct sock *sk, int ifindex,
662 			      int numsrc, int fmode,
663 			      struct sockaddr_storage *group,
664 			      struct sockaddr_storage *list)
665 {
666 	int msize = IP_MSFILTER_SIZE(numsrc);
667 	struct ip_msfilter *msf;
668 	struct sockaddr_in *psin;
669 	int err, i;
670 
671 	msf = kmalloc(msize, 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[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 > 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 > 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, gsf->gf_slist);
795 out_free_gsf:
796 	kfree(gsf);
797 	return err;
798 }
799 
800 static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
801 		int optlen)
802 {
803 	const int size0 = offsetof(struct compat_group_filter, gf_slist);
804 	struct compat_group_filter *gf32;
805 	unsigned int n;
806 	void *p;
807 	int err;
808 
809 	if (optlen < size0)
810 		return -EINVAL;
811 	if (optlen > sysctl_optmem_max - 4)
812 		return -ENOBUFS;
813 
814 	p = kmalloc(optlen + 4, GFP_KERNEL);
815 	if (!p)
816 		return -ENOMEM;
817 	gf32 = p + 4; /* we want ->gf_group and ->gf_slist aligned */
818 
819 	err = -EFAULT;
820 	if (copy_from_sockptr(gf32, optval, optlen))
821 		goto out_free_gsf;
822 
823 	/* numsrc >= (4G-140)/128 overflow in 32 bits */
824 	n = gf32->gf_numsrc;
825 	err = -ENOBUFS;
826 	if (n >= 0x1ffffff)
827 		goto out_free_gsf;
828 
829 	err = -EINVAL;
830 	if (offsetof(struct compat_group_filter, gf_slist[n]) > optlen)
831 		goto out_free_gsf;
832 
833 	/* numsrc >= (4G-140)/128 overflow in 32 bits */
834 	err = -ENOBUFS;
835 	if (n > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
836 		goto out_free_gsf;
837 	err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode,
838 				 &gf32->gf_group, gf32->gf_slist);
839 out_free_gsf:
840 	kfree(p);
841 	return err;
842 }
843 
844 static int ip_mcast_join_leave(struct sock *sk, int optname,
845 		sockptr_t optval, int optlen)
846 {
847 	struct ip_mreqn mreq = { };
848 	struct sockaddr_in *psin;
849 	struct group_req greq;
850 
851 	if (optlen < sizeof(struct group_req))
852 		return -EINVAL;
853 	if (copy_from_sockptr(&greq, optval, sizeof(greq)))
854 		return -EFAULT;
855 
856 	psin = (struct sockaddr_in *)&greq.gr_group;
857 	if (psin->sin_family != AF_INET)
858 		return -EINVAL;
859 	mreq.imr_multiaddr = psin->sin_addr;
860 	mreq.imr_ifindex = greq.gr_interface;
861 	if (optname == MCAST_JOIN_GROUP)
862 		return ip_mc_join_group(sk, &mreq);
863 	return ip_mc_leave_group(sk, &mreq);
864 }
865 
866 static int compat_ip_mcast_join_leave(struct sock *sk, int optname,
867 		sockptr_t optval, int optlen)
868 {
869 	struct compat_group_req greq;
870 	struct ip_mreqn mreq = { };
871 	struct sockaddr_in *psin;
872 
873 	if (optlen < sizeof(struct compat_group_req))
874 		return -EINVAL;
875 	if (copy_from_sockptr(&greq, optval, sizeof(greq)))
876 		return -EFAULT;
877 
878 	psin = (struct sockaddr_in *)&greq.gr_group;
879 	if (psin->sin_family != AF_INET)
880 		return -EINVAL;
881 	mreq.imr_multiaddr = psin->sin_addr;
882 	mreq.imr_ifindex = greq.gr_interface;
883 
884 	if (optname == MCAST_JOIN_GROUP)
885 		return ip_mc_join_group(sk, &mreq);
886 	return ip_mc_leave_group(sk, &mreq);
887 }
888 
889 static int do_ip_setsockopt(struct sock *sk, int level, int optname,
890 		sockptr_t optval, unsigned int optlen)
891 {
892 	struct inet_sock *inet = inet_sk(sk);
893 	struct net *net = sock_net(sk);
894 	int val = 0, err;
895 	bool needs_rtnl = setsockopt_needs_rtnl(optname);
896 
897 	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:
914 	case IP_BIND_ADDRESS_NO_PORT:
915 	case IP_UNICAST_IF:
916 	case IP_MULTICAST_TTL:
917 	case IP_MULTICAST_ALL:
918 	case IP_MULTICAST_LOOP:
919 	case IP_RECVORIGDSTADDR:
920 	case IP_CHECKSUM:
921 	case IP_RECVFRAGSIZE:
922 	case IP_RECVERR_RFC4884:
923 		if (optlen >= sizeof(int)) {
924 			if (copy_from_sockptr(&val, optval, sizeof(val)))
925 				return -EFAULT;
926 		} else if (optlen >= sizeof(char)) {
927 			unsigned char ucval;
928 
929 			if (copy_from_sockptr(&ucval, optval, sizeof(ucval)))
930 				return -EFAULT;
931 			val = (int) ucval;
932 		}
933 	}
934 
935 	/* If optlen==0, it is equivalent to val == 0 */
936 
937 	if (optname == IP_ROUTER_ALERT)
938 		return ip_ra_control(sk, val ? 1 : 0, NULL);
939 	if (ip_mroute_opt(optname))
940 		return ip_mroute_setsockopt(sk, optname, optval, optlen);
941 
942 	err = 0;
943 	if (needs_rtnl)
944 		rtnl_lock();
945 	lock_sock(sk);
946 
947 	switch (optname) {
948 	case IP_OPTIONS:
949 	{
950 		struct ip_options_rcu *old, *opt = NULL;
951 
952 		if (optlen > 40)
953 			goto e_inval;
954 		err = ip_options_get(sock_net(sk), &opt, optval, optlen);
955 		if (err)
956 			break;
957 		old = rcu_dereference_protected(inet->inet_opt,
958 						lockdep_sock_is_held(sk));
959 		if (inet->is_icsk) {
960 			struct inet_connection_sock *icsk = inet_csk(sk);
961 #if IS_ENABLED(CONFIG_IPV6)
962 			if (sk->sk_family == PF_INET ||
963 			    (!((1 << sk->sk_state) &
964 			       (TCPF_LISTEN | TCPF_CLOSE)) &&
965 			     inet->inet_daddr != LOOPBACK4_IPV6)) {
966 #endif
967 				if (old)
968 					icsk->icsk_ext_hdr_len -= old->opt.optlen;
969 				if (opt)
970 					icsk->icsk_ext_hdr_len += opt->opt.optlen;
971 				icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
972 #if IS_ENABLED(CONFIG_IPV6)
973 			}
974 #endif
975 		}
976 		rcu_assign_pointer(inet->inet_opt, opt);
977 		if (old)
978 			kfree_rcu(old, rcu);
979 		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;
1017 	case IP_RECVORIGDSTADDR:
1018 		if (val)
1019 			inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
1020 		else
1021 			inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
1022 		break;
1023 	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;
1036 	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;
1044 	case IP_TOS:	/* This sets both TOS and Precedence */
1045 		__ip_sock_set_tos(sk, val);
1046 		break;
1047 	case IP_TTL:
1048 		if (optlen < 1)
1049 			goto e_inval;
1050 		if (val != -1 && (val < 1 || val > 255))
1051 			goto e_inval;
1052 		inet->uc_ttl = val;
1053 		break;
1054 	case IP_HDRINCL:
1055 		if (sk->sk_type != SOCK_RAW) {
1056 			err = -ENOPROTOOPT;
1057 			break;
1058 		}
1059 		inet->hdrincl = val ? 1 : 0;
1060 		break;
1061 	case IP_NODEFRAG:
1062 		if (sk->sk_type != SOCK_RAW) {
1063 			err = -ENOPROTOOPT;
1064 			break;
1065 		}
1066 		inet->nodefrag = val ? 1 : 0;
1067 		break;
1068 	case IP_BIND_ADDRESS_NO_PORT:
1069 		inet->bind_address_no_port = val ? 1 : 0;
1070 		break;
1071 	case IP_MTU_DISCOVER:
1072 		if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
1073 			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;
1081 	case IP_RECVERR_RFC4884:
1082 		if (val < 0 || val > 1)
1083 			goto e_inval;
1084 		inet->recverr_rfc4884 = !!val;
1085 		break;
1086 	case IP_MULTICAST_TTL:
1087 		if (sk->sk_type == SOCK_STREAM)
1088 			goto e_inval;
1089 		if (optlen < 1)
1090 			goto e_inval;
1091 		if (val == -1)
1092 			val = 1;
1093 		if (val < 0 || val > 255)
1094 			goto e_inval;
1095 		inet->mc_ttl = val;
1096 		break;
1097 	case IP_MULTICAST_LOOP:
1098 		if (optlen < 1)
1099 			goto e_inval;
1100 		inet->mc_loop = !!val;
1101 		break;
1102 	case IP_UNICAST_IF:
1103 	{
1104 		struct net_device *dev = NULL;
1105 		int ifindex;
1106 		int midx;
1107 
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;
1122 
1123 		midx = l3mdev_master_ifindex(dev);
1124 		dev_put(dev);
1125 
1126 		err = -EINVAL;
1127 		if (sk->sk_bound_dev_if &&
1128 		    (!midx || midx != sk->sk_bound_dev_if))
1129 			break;
1130 
1131 		inet->uc_index = ifindex;
1132 		err = 0;
1133 		break;
1134 	}
1135 	case IP_MULTICAST_IF:
1136 	{
1137 		struct ip_mreqn mreq;
1138 		struct net_device *dev = NULL;
1139 		int midx;
1140 
1141 		if (sk->sk_type == SOCK_STREAM)
1142 			goto e_inval;
1143 		/*
1144 		 *	Check the arguments are allowable
1145 		 */
1146 
1147 		if (optlen < sizeof(struct in_addr))
1148 			goto e_inval;
1149 
1150 		err = -EFAULT;
1151 		if (optlen >= sizeof(struct ip_mreqn)) {
1152 			if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
1153 				break;
1154 		} else {
1155 			memset(&mreq, 0, sizeof(mreq));
1156 			if (optlen >= sizeof(struct ip_mreq)) {
1157 				if (copy_from_sockptr(&mreq, optval,
1158 						      sizeof(struct ip_mreq)))
1159 					break;
1160 			} else if (optlen >= sizeof(struct in_addr)) {
1161 				if (copy_from_sockptr(&mreq.imr_address, optval,
1162 						      sizeof(struct in_addr)))
1163 					break;
1164 			}
1165 		}
1166 
1167 		if (!mreq.imr_ifindex) {
1168 			if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
1169 				inet->mc_index = 0;
1170 				inet->mc_addr  = 0;
1171 				err = 0;
1172 				break;
1173 			}
1174 			dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
1175 			if (dev)
1176 				mreq.imr_ifindex = dev->ifindex;
1177 		} else
1178 			dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
1179 
1180 
1181 		err = -EADDRNOTAVAIL;
1182 		if (!dev)
1183 			break;
1184 
1185 		midx = l3mdev_master_ifindex(dev);
1186 
1187 		dev_put(dev);
1188 
1189 		err = -EINVAL;
1190 		if (sk->sk_bound_dev_if &&
1191 		    mreq.imr_ifindex != sk->sk_bound_dev_if &&
1192 		    (!midx || midx != sk->sk_bound_dev_if))
1193 			break;
1194 
1195 		inet->mc_index = mreq.imr_ifindex;
1196 		inet->mc_addr  = mreq.imr_address.s_addr;
1197 		err = 0;
1198 		break;
1199 	}
1200 
1201 	case IP_ADD_MEMBERSHIP:
1202 	case IP_DROP_MEMBERSHIP:
1203 	{
1204 		struct ip_mreqn mreq;
1205 
1206 		err = -EPROTO;
1207 		if (inet_sk(sk)->is_icsk)
1208 			break;
1209 
1210 		if (optlen < sizeof(struct ip_mreq))
1211 			goto e_inval;
1212 		err = -EFAULT;
1213 		if (optlen >= sizeof(struct ip_mreqn)) {
1214 			if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
1215 				break;
1216 		} else {
1217 			memset(&mreq, 0, sizeof(mreq));
1218 			if (copy_from_sockptr(&mreq, optval,
1219 					      sizeof(struct ip_mreq)))
1220 				break;
1221 		}
1222 
1223 		if (optname == IP_ADD_MEMBERSHIP)
1224 			err = ip_mc_join_group(sk, &mreq);
1225 		else
1226 			err = ip_mc_leave_group(sk, &mreq);
1227 		break;
1228 	}
1229 	case IP_MSFILTER:
1230 	{
1231 		struct ip_msfilter *msf;
1232 
1233 		if (optlen < IP_MSFILTER_SIZE(0))
1234 			goto e_inval;
1235 		if (optlen > sysctl_optmem_max) {
1236 			err = -ENOBUFS;
1237 			break;
1238 		}
1239 		msf = memdup_sockptr(optval, optlen);
1240 		if (IS_ERR(msf)) {
1241 			err = PTR_ERR(msf);
1242 			break;
1243 		}
1244 		/* numsrc >= (1G-4) overflow in 32 bits */
1245 		if (msf->imsf_numsrc >= 0x3ffffffcU ||
1246 		    msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
1247 			kfree(msf);
1248 			err = -ENOBUFS;
1249 			break;
1250 		}
1251 		if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
1252 			kfree(msf);
1253 			err = -EINVAL;
1254 			break;
1255 		}
1256 		err = ip_mc_msfilter(sk, msf, 0);
1257 		kfree(msf);
1258 		break;
1259 	}
1260 	case IP_BLOCK_SOURCE:
1261 	case IP_UNBLOCK_SOURCE:
1262 	case IP_ADD_SOURCE_MEMBERSHIP:
1263 	case IP_DROP_SOURCE_MEMBERSHIP:
1264 	{
1265 		struct ip_mreq_source mreqs;
1266 		int omode, add;
1267 
1268 		if (optlen != sizeof(struct ip_mreq_source))
1269 			goto e_inval;
1270 		if (copy_from_sockptr(&mreqs, optval, sizeof(mreqs))) {
1271 			err = -EFAULT;
1272 			break;
1273 		}
1274 		if (optname == IP_BLOCK_SOURCE) {
1275 			omode = MCAST_EXCLUDE;
1276 			add = 1;
1277 		} else if (optname == IP_UNBLOCK_SOURCE) {
1278 			omode = MCAST_EXCLUDE;
1279 			add = 0;
1280 		} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
1281 			struct ip_mreqn mreq;
1282 
1283 			mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
1284 			mreq.imr_address.s_addr = mreqs.imr_interface;
1285 			mreq.imr_ifindex = 0;
1286 			err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
1287 			if (err && err != -EADDRINUSE)
1288 				break;
1289 			omode = MCAST_INCLUDE;
1290 			add = 1;
1291 		} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1292 			omode = MCAST_INCLUDE;
1293 			add = 0;
1294 		}
1295 		err = ip_mc_source(add, omode, sk, &mreqs, 0);
1296 		break;
1297 	}
1298 	case MCAST_JOIN_GROUP:
1299 	case MCAST_LEAVE_GROUP:
1300 		if (in_compat_syscall())
1301 			err = compat_ip_mcast_join_leave(sk, optname, optval,
1302 							 optlen);
1303 		else
1304 			err = ip_mcast_join_leave(sk, optname, optval, optlen);
1305 		break;
1306 	case MCAST_JOIN_SOURCE_GROUP:
1307 	case MCAST_LEAVE_SOURCE_GROUP:
1308 	case MCAST_BLOCK_SOURCE:
1309 	case MCAST_UNBLOCK_SOURCE:
1310 		err = do_mcast_group_source(sk, optname, optval, optlen);
1311 		break;
1312 	case MCAST_MSFILTER:
1313 		if (in_compat_syscall())
1314 			err = compat_ip_set_mcast_msfilter(sk, optval, optlen);
1315 		else
1316 			err = ip_set_mcast_msfilter(sk, optval, optlen);
1317 		break;
1318 	case IP_MULTICAST_ALL:
1319 		if (optlen < 1)
1320 			goto e_inval;
1321 		if (val != 0 && val != 1)
1322 			goto e_inval;
1323 		inet->mc_all = val;
1324 		break;
1325 
1326 	case IP_FREEBIND:
1327 		if (optlen < 1)
1328 			goto e_inval;
1329 		inet->freebind = !!val;
1330 		break;
1331 
1332 	case IP_IPSEC_POLICY:
1333 	case IP_XFRM_POLICY:
1334 		err = -EPERM;
1335 		if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1336 			break;
1337 		err = xfrm_user_policy(sk, optname, optval, optlen);
1338 		break;
1339 
1340 	case IP_TRANSPARENT:
1341 		if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1342 		    !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1343 			err = -EPERM;
1344 			break;
1345 		}
1346 		if (optlen < 1)
1347 			goto e_inval;
1348 		inet->transparent = !!val;
1349 		break;
1350 
1351 	case IP_MINTTL:
1352 		if (optlen < 1)
1353 			goto e_inval;
1354 		if (val < 0 || val > 255)
1355 			goto e_inval;
1356 		inet->min_ttl = val;
1357 		break;
1358 
1359 	default:
1360 		err = -ENOPROTOOPT;
1361 		break;
1362 	}
1363 	release_sock(sk);
1364 	if (needs_rtnl)
1365 		rtnl_unlock();
1366 	return err;
1367 
1368 e_inval:
1369 	release_sock(sk);
1370 	if (needs_rtnl)
1371 		rtnl_unlock();
1372 	return -EINVAL;
1373 }
1374 
1375 /**
1376  * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1377  * @sk: socket
1378  * @skb: buffer
1379  *
1380  * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1381  * destination in skb->cb[] before dst drop.
1382  * This way, receiver doesn't make cache line misses to read rtable.
1383  */
1384 void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
1385 {
1386 	struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1387 	bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1388 		       ipv6_sk_rxinfo(sk);
1389 
1390 	if (prepare && skb_rtable(skb)) {
1391 		/* skb->cb is overloaded: prior to this point it is IP{6}CB
1392 		 * which has interface index (iif) as the first member of the
1393 		 * underlying inet{6}_skb_parm struct. This code then overlays
1394 		 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1395 		 * element so the iif is picked up from the prior IPCB. If iif
1396 		 * is the loopback interface, then return the sending interface
1397 		 * (e.g., process binds socket to eth0 for Tx which is
1398 		 * redirected to loopback in the rtable/dst).
1399 		 */
1400 		struct rtable *rt = skb_rtable(skb);
1401 		bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1402 
1403 		if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
1404 			pktinfo->ipi_ifindex = inet_iif(skb);
1405 		else if (l3slave && rt && rt->rt_iif)
1406 			pktinfo->ipi_ifindex = rt->rt_iif;
1407 
1408 		pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1409 	} else {
1410 		pktinfo->ipi_ifindex = 0;
1411 		pktinfo->ipi_spec_dst.s_addr = 0;
1412 	}
1413 	skb_dst_drop(skb);
1414 }
1415 
1416 int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1417 		unsigned int optlen)
1418 {
1419 	int err;
1420 
1421 	if (level != SOL_IP)
1422 		return -ENOPROTOOPT;
1423 
1424 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1425 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1426 	if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1427 	    optname < BPFILTER_IPT_SET_MAX)
1428 		err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
1429 #endif
1430 #ifdef CONFIG_NETFILTER
1431 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1432 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
1433 			optname != IP_IPSEC_POLICY &&
1434 			optname != IP_XFRM_POLICY &&
1435 			!ip_mroute_opt(optname))
1436 		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1437 #endif
1438 	return err;
1439 }
1440 EXPORT_SYMBOL(ip_setsockopt);
1441 
1442 /*
1443  *	Get the options. Note for future reference. The GET of IP options gets
1444  *	the _received_ ones. The set sets the _sent_ ones.
1445  */
1446 
1447 static bool getsockopt_needs_rtnl(int optname)
1448 {
1449 	switch (optname) {
1450 	case IP_MSFILTER:
1451 	case MCAST_MSFILTER:
1452 		return true;
1453 	}
1454 	return false;
1455 }
1456 
1457 static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
1458 		int __user *optlen, int len)
1459 {
1460 	const int size0 = offsetof(struct group_filter, gf_slist);
1461 	struct group_filter __user *p = optval;
1462 	struct group_filter gsf;
1463 	int num;
1464 	int err;
1465 
1466 	if (len < size0)
1467 		return -EINVAL;
1468 	if (copy_from_user(&gsf, p, size0))
1469 		return -EFAULT;
1470 
1471 	num = gsf.gf_numsrc;
1472 	err = ip_mc_gsfget(sk, &gsf, p->gf_slist);
1473 	if (err)
1474 		return err;
1475 	if (gsf.gf_numsrc < num)
1476 		num = gsf.gf_numsrc;
1477 	if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1478 	    copy_to_user(p, &gsf, size0))
1479 		return -EFAULT;
1480 	return 0;
1481 }
1482 
1483 static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
1484 		int __user *optlen, int len)
1485 {
1486 	const int size0 = offsetof(struct compat_group_filter, gf_slist);
1487 	struct compat_group_filter __user *p = optval;
1488 	struct compat_group_filter gf32;
1489 	struct group_filter gf;
1490 	int num;
1491 	int err;
1492 
1493 	if (len < size0)
1494 		return -EINVAL;
1495 	if (copy_from_user(&gf32, p, size0))
1496 		return -EFAULT;
1497 
1498 	gf.gf_interface = gf32.gf_interface;
1499 	gf.gf_fmode = gf32.gf_fmode;
1500 	num = gf.gf_numsrc = gf32.gf_numsrc;
1501 	gf.gf_group = gf32.gf_group;
1502 
1503 	err = ip_mc_gsfget(sk, &gf, p->gf_slist);
1504 	if (err)
1505 		return err;
1506 	if (gf.gf_numsrc < num)
1507 		num = gf.gf_numsrc;
1508 	len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32));
1509 	if (put_user(len, optlen) ||
1510 	    put_user(gf.gf_fmode, &p->gf_fmode) ||
1511 	    put_user(gf.gf_numsrc, &p->gf_numsrc))
1512 		return -EFAULT;
1513 	return 0;
1514 }
1515 
1516 static int do_ip_getsockopt(struct sock *sk, int level, int optname,
1517 			    char __user *optval, int __user *optlen)
1518 {
1519 	struct inet_sock *inet = inet_sk(sk);
1520 	bool needs_rtnl = getsockopt_needs_rtnl(optname);
1521 	int val, err = 0;
1522 	int len;
1523 
1524 	if (level != SOL_IP)
1525 		return -EOPNOTSUPP;
1526 
1527 	if (ip_mroute_opt(optname))
1528 		return ip_mroute_getsockopt(sk, optname, optval, optlen);
1529 
1530 	if (get_user(len, optlen))
1531 		return -EFAULT;
1532 	if (len < 0)
1533 		return -EINVAL;
1534 
1535 	if (needs_rtnl)
1536 		rtnl_lock();
1537 	lock_sock(sk);
1538 
1539 	switch (optname) {
1540 	case IP_OPTIONS:
1541 	{
1542 		unsigned char optbuf[sizeof(struct ip_options)+40];
1543 		struct ip_options *opt = (struct ip_options *)optbuf;
1544 		struct ip_options_rcu *inet_opt;
1545 
1546 		inet_opt = rcu_dereference_protected(inet->inet_opt,
1547 						     lockdep_sock_is_held(sk));
1548 		opt->optlen = 0;
1549 		if (inet_opt)
1550 			memcpy(optbuf, &inet_opt->opt,
1551 			       sizeof(struct ip_options) +
1552 			       inet_opt->opt.optlen);
1553 		release_sock(sk);
1554 
1555 		if (opt->optlen == 0)
1556 			return put_user(0, optlen);
1557 
1558 		ip_options_undo(opt);
1559 
1560 		len = min_t(unsigned int, len, opt->optlen);
1561 		if (put_user(len, optlen))
1562 			return -EFAULT;
1563 		if (copy_to_user(optval, opt->__data, len))
1564 			return -EFAULT;
1565 		return 0;
1566 	}
1567 	case IP_PKTINFO:
1568 		val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1569 		break;
1570 	case IP_RECVTTL:
1571 		val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1572 		break;
1573 	case IP_RECVTOS:
1574 		val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1575 		break;
1576 	case IP_RECVOPTS:
1577 		val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1578 		break;
1579 	case IP_RETOPTS:
1580 		val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1581 		break;
1582 	case IP_PASSSEC:
1583 		val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1584 		break;
1585 	case IP_RECVORIGDSTADDR:
1586 		val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1587 		break;
1588 	case IP_CHECKSUM:
1589 		val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1590 		break;
1591 	case IP_RECVFRAGSIZE:
1592 		val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1593 		break;
1594 	case IP_TOS:
1595 		val = inet->tos;
1596 		break;
1597 	case IP_TTL:
1598 	{
1599 		struct net *net = sock_net(sk);
1600 		val = (inet->uc_ttl == -1 ?
1601 		       net->ipv4.sysctl_ip_default_ttl :
1602 		       inet->uc_ttl);
1603 		break;
1604 	}
1605 	case IP_HDRINCL:
1606 		val = inet->hdrincl;
1607 		break;
1608 	case IP_NODEFRAG:
1609 		val = inet->nodefrag;
1610 		break;
1611 	case IP_BIND_ADDRESS_NO_PORT:
1612 		val = inet->bind_address_no_port;
1613 		break;
1614 	case IP_MTU_DISCOVER:
1615 		val = inet->pmtudisc;
1616 		break;
1617 	case IP_MTU:
1618 	{
1619 		struct dst_entry *dst;
1620 		val = 0;
1621 		dst = sk_dst_get(sk);
1622 		if (dst) {
1623 			val = dst_mtu(dst);
1624 			dst_release(dst);
1625 		}
1626 		if (!val) {
1627 			release_sock(sk);
1628 			return -ENOTCONN;
1629 		}
1630 		break;
1631 	}
1632 	case IP_RECVERR:
1633 		val = inet->recverr;
1634 		break;
1635 	case IP_RECVERR_RFC4884:
1636 		val = inet->recverr_rfc4884;
1637 		break;
1638 	case IP_MULTICAST_TTL:
1639 		val = inet->mc_ttl;
1640 		break;
1641 	case IP_MULTICAST_LOOP:
1642 		val = inet->mc_loop;
1643 		break;
1644 	case IP_UNICAST_IF:
1645 		val = (__force int)htonl((__u32) inet->uc_index);
1646 		break;
1647 	case IP_MULTICAST_IF:
1648 	{
1649 		struct in_addr addr;
1650 		len = min_t(unsigned int, len, sizeof(struct in_addr));
1651 		addr.s_addr = inet->mc_addr;
1652 		release_sock(sk);
1653 
1654 		if (put_user(len, optlen))
1655 			return -EFAULT;
1656 		if (copy_to_user(optval, &addr, len))
1657 			return -EFAULT;
1658 		return 0;
1659 	}
1660 	case IP_MSFILTER:
1661 	{
1662 		struct ip_msfilter msf;
1663 
1664 		if (len < IP_MSFILTER_SIZE(0)) {
1665 			err = -EINVAL;
1666 			goto out;
1667 		}
1668 		if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1669 			err = -EFAULT;
1670 			goto out;
1671 		}
1672 		err = ip_mc_msfget(sk, &msf,
1673 				   (struct ip_msfilter __user *)optval, optlen);
1674 		goto out;
1675 	}
1676 	case MCAST_MSFILTER:
1677 		if (in_compat_syscall())
1678 			err = compat_ip_get_mcast_msfilter(sk, optval, optlen,
1679 							   len);
1680 		else
1681 			err = ip_get_mcast_msfilter(sk, optval, optlen, len);
1682 		goto out;
1683 	case IP_MULTICAST_ALL:
1684 		val = inet->mc_all;
1685 		break;
1686 	case IP_PKTOPTIONS:
1687 	{
1688 		struct msghdr msg;
1689 
1690 		release_sock(sk);
1691 
1692 		if (sk->sk_type != SOCK_STREAM)
1693 			return -ENOPROTOOPT;
1694 
1695 		msg.msg_control_is_user = true;
1696 		msg.msg_control_user = optval;
1697 		msg.msg_controllen = len;
1698 		msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0;
1699 
1700 		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1701 			struct in_pktinfo info;
1702 
1703 			info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1704 			info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
1705 			info.ipi_ifindex = inet->mc_index;
1706 			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1707 		}
1708 		if (inet->cmsg_flags & IP_CMSG_TTL) {
1709 			int hlim = inet->mc_ttl;
1710 			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1711 		}
1712 		if (inet->cmsg_flags & IP_CMSG_TOS) {
1713 			int tos = inet->rcv_tos;
1714 			put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1715 		}
1716 		len -= msg.msg_controllen;
1717 		return put_user(len, optlen);
1718 	}
1719 	case IP_FREEBIND:
1720 		val = inet->freebind;
1721 		break;
1722 	case IP_TRANSPARENT:
1723 		val = inet->transparent;
1724 		break;
1725 	case IP_MINTTL:
1726 		val = inet->min_ttl;
1727 		break;
1728 	default:
1729 		release_sock(sk);
1730 		return -ENOPROTOOPT;
1731 	}
1732 	release_sock(sk);
1733 
1734 	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1735 		unsigned char ucval = (unsigned char)val;
1736 		len = 1;
1737 		if (put_user(len, optlen))
1738 			return -EFAULT;
1739 		if (copy_to_user(optval, &ucval, 1))
1740 			return -EFAULT;
1741 	} else {
1742 		len = min_t(unsigned int, sizeof(int), len);
1743 		if (put_user(len, optlen))
1744 			return -EFAULT;
1745 		if (copy_to_user(optval, &val, len))
1746 			return -EFAULT;
1747 	}
1748 	return 0;
1749 
1750 out:
1751 	release_sock(sk);
1752 	if (needs_rtnl)
1753 		rtnl_unlock();
1754 	return err;
1755 }
1756 
1757 int ip_getsockopt(struct sock *sk, int level,
1758 		  int optname, char __user *optval, int __user *optlen)
1759 {
1760 	int err;
1761 
1762 	err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1763 
1764 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1765 	if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1766 	    optname < BPFILTER_IPT_GET_MAX)
1767 		err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1768 #endif
1769 #ifdef CONFIG_NETFILTER
1770 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1771 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1772 			!ip_mroute_opt(optname)) {
1773 		int len;
1774 
1775 		if (get_user(len, optlen))
1776 			return -EFAULT;
1777 
1778 		err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
1779 		if (err >= 0)
1780 			err = put_user(len, optlen);
1781 		return err;
1782 	}
1783 #endif
1784 	return err;
1785 }
1786 EXPORT_SYMBOL(ip_getsockopt);
1787