xref: /linux/net/ipv4/ip_sockglue.c (revision 91d0b78c5177f3e42a4d8738af8ac19c3a90d002)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		The IP to API glue.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	see ip.c
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Fixes:
121da177e4SLinus Torvalds  *		Many		:	Split from ip.c , see ip.c for history.
131da177e4SLinus Torvalds  *		Martin Mares	:	TOS setting fixed.
141da177e4SLinus Torvalds  *		Alan Cox	:	Fixed a couple of oopses in Martin's
151da177e4SLinus Torvalds  *					TOS tweaks.
161da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/types.h>
211da177e4SLinus Torvalds #include <linux/mm.h>
221da177e4SLinus Torvalds #include <linux/skbuff.h>
231da177e4SLinus Torvalds #include <linux/ip.h>
241da177e4SLinus Torvalds #include <linux/icmp.h>
2514c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
261da177e4SLinus Torvalds #include <linux/netdevice.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
281da177e4SLinus Torvalds #include <net/sock.h>
291da177e4SLinus Torvalds #include <net/ip.h>
301da177e4SLinus Torvalds #include <net/icmp.h>
31d83d8461SArnaldo Carvalho de Melo #include <net/tcp_states.h>
321da177e4SLinus Torvalds #include <linux/udp.h>
331da177e4SLinus Torvalds #include <linux/igmp.h>
341da177e4SLinus Torvalds #include <linux/netfilter.h>
351da177e4SLinus Torvalds #include <linux/route.h>
361da177e4SLinus Torvalds #include <linux/mroute.h>
372c67e9acSMaciej Żenczykowski #include <net/inet_ecn.h>
381da177e4SLinus Torvalds #include <net/route.h>
391da177e4SLinus Torvalds #include <net/xfrm.h>
40dae50295SDavid L Stevens #include <net/compat.h>
41ad6f939aSTom Herbert #include <net/checksum.h>
42dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
431da177e4SLinus Torvalds #include <net/transp_v6.h>
441da177e4SLinus Torvalds #endif
4535ebf65eSDavid S. Miller #include <net/ip_fib.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <linux/errqueue.h>
487c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
491da177e4SLinus Torvalds 
50d2ba09c1SAlexei Starovoitov #include <linux/bpfilter.h>
51d2ba09c1SAlexei Starovoitov 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  *	SOL_IP control messages.
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
571da177e4SLinus Torvalds {
58d826eb14SEric Dumazet 	struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
591da177e4SLinus Torvalds 
60eddc9ec5SArnaldo Carvalho de Melo 	info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
661da177e4SLinus Torvalds {
67eddc9ec5SArnaldo Carvalho de Melo 	int ttl = ip_hdr(skb)->ttl;
681da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
721da177e4SLinus Torvalds {
73eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
791da177e4SLinus Torvalds 		return;
801da177e4SLinus Torvalds 
81eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
82eddc9ec5SArnaldo Carvalho de Melo 		 ip_hdr(skb) + 1);
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 
8691ed1e66SPaolo Abeni static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg,
8791ed1e66SPaolo Abeni 				 struct sk_buff *skb)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	unsigned char optbuf[sizeof(struct ip_options) + 40];
901da177e4SLinus Torvalds 	struct ip_options *opt = (struct ip_options *)optbuf;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
931da177e4SLinus Torvalds 		return;
941da177e4SLinus Torvalds 
9591ed1e66SPaolo Abeni 	if (ip_options_echo(net, opt, skb)) {
961da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
971da177e4SLinus Torvalds 		return;
981da177e4SLinus Torvalds 	}
991da177e4SLinus Torvalds 	ip_options_undo(opt);
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
10470ecc248SWillem de Bruijn static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb)
10570ecc248SWillem de Bruijn {
10670ecc248SWillem de Bruijn 	int val;
10770ecc248SWillem de Bruijn 
10870ecc248SWillem de Bruijn 	if (IPCB(skb)->frag_max_size == 0)
10970ecc248SWillem de Bruijn 		return;
11070ecc248SWillem de Bruijn 
11170ecc248SWillem de Bruijn 	val = IPCB(skb)->frag_max_size;
11270ecc248SWillem de Bruijn 	put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val);
11370ecc248SWillem de Bruijn }
11470ecc248SWillem de Bruijn 
115ad6f939aSTom Herbert static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
11610df8e61SEric Dumazet 				  int tlen, int offset)
117ad6f939aSTom Herbert {
118ad6f939aSTom Herbert 	__wsum csum = skb->csum;
119ad6f939aSTom Herbert 
120ad6f939aSTom Herbert 	if (skb->ip_summed != CHECKSUM_COMPLETE)
121ad6f939aSTom Herbert 		return;
122ad6f939aSTom Herbert 
123ca4ef457SPaolo Abeni 	if (offset != 0) {
124ca4ef457SPaolo Abeni 		int tend_off = skb_transport_offset(skb) + tlen;
125ca4ef457SPaolo Abeni 		csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0));
126ca4ef457SPaolo Abeni 	}
127ad6f939aSTom Herbert 
128ad6f939aSTom Herbert 	put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
129ad6f939aSTom Herbert }
130ad6f939aSTom Herbert 
1312c7946a7SCatherine Zhang static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
1322c7946a7SCatherine Zhang {
1332c7946a7SCatherine Zhang 	char *secdata;
134dc49c1f9SCatherine Zhang 	u32 seclen, secid;
1352c7946a7SCatherine Zhang 	int err;
1362c7946a7SCatherine Zhang 
137dc49c1f9SCatherine Zhang 	err = security_socket_getpeersec_dgram(NULL, skb, &secid);
138dc49c1f9SCatherine Zhang 	if (err)
139dc49c1f9SCatherine Zhang 		return;
140dc49c1f9SCatherine Zhang 
141dc49c1f9SCatherine Zhang 	err = security_secid_to_secctx(secid, &secdata, &seclen);
1422c7946a7SCatherine Zhang 	if (err)
1432c7946a7SCatherine Zhang 		return;
1442c7946a7SCatherine Zhang 
1452c7946a7SCatherine Zhang 	put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
146dc49c1f9SCatherine Zhang 	security_release_secctx(secdata, seclen);
1472c7946a7SCatherine Zhang }
1482c7946a7SCatherine Zhang 
14921d1a161SHarvey Harrison static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
150e8b2dfe9SBalazs Scheidler {
1514a06fa67SWillem de Bruijn 	__be16 _ports[2], *ports;
152e8b2dfe9SBalazs Scheidler 	struct sockaddr_in sin;
153e8b2dfe9SBalazs Scheidler 
154e8b2dfe9SBalazs Scheidler 	/* All current transport protocols have the port numbers in the
155e8b2dfe9SBalazs Scheidler 	 * first four bytes of the transport header and this function is
156e8b2dfe9SBalazs Scheidler 	 * written with this assumption in mind.
157e8b2dfe9SBalazs Scheidler 	 */
1584a06fa67SWillem de Bruijn 	ports = skb_header_pointer(skb, skb_transport_offset(skb),
1594a06fa67SWillem de Bruijn 				   sizeof(_ports), &_ports);
1604a06fa67SWillem de Bruijn 	if (!ports)
1614a06fa67SWillem de Bruijn 		return;
162e8b2dfe9SBalazs Scheidler 
163e8b2dfe9SBalazs Scheidler 	sin.sin_family = AF_INET;
16464199fc0SEric Dumazet 	sin.sin_addr.s_addr = ip_hdr(skb)->daddr;
165e8b2dfe9SBalazs Scheidler 	sin.sin_port = ports[1];
166e8b2dfe9SBalazs Scheidler 	memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
167e8b2dfe9SBalazs Scheidler 
168e8b2dfe9SBalazs Scheidler 	put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
169e8b2dfe9SBalazs Scheidler }
1701da177e4SLinus Torvalds 
171ad959036SPaolo Abeni void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
172ad959036SPaolo Abeni 			 struct sk_buff *skb, int tlen, int offset)
1731da177e4SLinus Torvalds {
174ad959036SPaolo Abeni 	struct inet_sock *inet = inet_sk(sk);
17595c96174SEric Dumazet 	unsigned int flags = inet->cmsg_flags;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	/* Ordered by supposed usage frequency */
178c44d13d6STom Herbert 	if (flags & IP_CMSG_PKTINFO) {
1791da177e4SLinus Torvalds 		ip_cmsg_recv_pktinfo(msg, skb);
1801da177e4SLinus Torvalds 
181c44d13d6STom Herbert 		flags &= ~IP_CMSG_PKTINFO;
182c44d13d6STom Herbert 		if (!flags)
183c44d13d6STom Herbert 			return;
184c44d13d6STom Herbert 	}
185c44d13d6STom Herbert 
186c44d13d6STom Herbert 	if (flags & IP_CMSG_TTL) {
1871da177e4SLinus Torvalds 		ip_cmsg_recv_ttl(msg, skb);
1881da177e4SLinus Torvalds 
189c44d13d6STom Herbert 		flags &= ~IP_CMSG_TTL;
190c44d13d6STom Herbert 		if (!flags)
191c44d13d6STom Herbert 			return;
192c44d13d6STom Herbert 	}
193c44d13d6STom Herbert 
194c44d13d6STom Herbert 	if (flags & IP_CMSG_TOS) {
1951da177e4SLinus Torvalds 		ip_cmsg_recv_tos(msg, skb);
1961da177e4SLinus Torvalds 
197c44d13d6STom Herbert 		flags &= ~IP_CMSG_TOS;
198c44d13d6STom Herbert 		if (!flags)
199c44d13d6STom Herbert 			return;
200c44d13d6STom Herbert 	}
201c44d13d6STom Herbert 
202c44d13d6STom Herbert 	if (flags & IP_CMSG_RECVOPTS) {
2031da177e4SLinus Torvalds 		ip_cmsg_recv_opts(msg, skb);
2041da177e4SLinus Torvalds 
205c44d13d6STom Herbert 		flags &= ~IP_CMSG_RECVOPTS;
206c44d13d6STom Herbert 		if (!flags)
207c44d13d6STom Herbert 			return;
208c44d13d6STom Herbert 	}
209c44d13d6STom Herbert 
210c44d13d6STom Herbert 	if (flags & IP_CMSG_RETOPTS) {
21191ed1e66SPaolo Abeni 		ip_cmsg_recv_retopts(sock_net(sk), msg, skb);
2122c7946a7SCatherine Zhang 
213c44d13d6STom Herbert 		flags &= ~IP_CMSG_RETOPTS;
214c44d13d6STom Herbert 		if (!flags)
215c44d13d6STom Herbert 			return;
216c44d13d6STom Herbert 	}
217c44d13d6STom Herbert 
218c44d13d6STom Herbert 	if (flags & IP_CMSG_PASSSEC) {
2192c7946a7SCatherine Zhang 		ip_cmsg_recv_security(msg, skb);
220e8b2dfe9SBalazs Scheidler 
221c44d13d6STom Herbert 		flags &= ~IP_CMSG_PASSSEC;
222c44d13d6STom Herbert 		if (!flags)
223e8b2dfe9SBalazs Scheidler 			return;
224c44d13d6STom Herbert 	}
225c44d13d6STom Herbert 
226ad6f939aSTom Herbert 	if (flags & IP_CMSG_ORIGDSTADDR) {
227e8b2dfe9SBalazs Scheidler 		ip_cmsg_recv_dstaddr(msg, skb);
228e8b2dfe9SBalazs Scheidler 
229ad6f939aSTom Herbert 		flags &= ~IP_CMSG_ORIGDSTADDR;
230ad6f939aSTom Herbert 		if (!flags)
231ad6f939aSTom Herbert 			return;
232ad6f939aSTom Herbert 	}
233ad6f939aSTom Herbert 
234ad6f939aSTom Herbert 	if (flags & IP_CMSG_CHECKSUM)
23510df8e61SEric Dumazet 		ip_cmsg_recv_checksum(msg, skb, tlen, offset);
23670ecc248SWillem de Bruijn 
23770ecc248SWillem de Bruijn 	if (flags & IP_CMSG_RECVFRAGSIZE)
23870ecc248SWillem de Bruijn 		ip_cmsg_recv_fragsize(msg, skb);
2391da177e4SLinus Torvalds }
2405961de9fSTom Herbert EXPORT_SYMBOL(ip_cmsg_recv_offset);
2411da177e4SLinus Torvalds 
24224025c46SSoheil Hassas Yeganeh int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
243c8e6ad08SHannes Frederic Sowa 		 bool allow_ipv6)
2441da177e4SLinus Torvalds {
245f02db315SFrancesco Fusco 	int err, val;
2461da177e4SLinus Torvalds 	struct cmsghdr *cmsg;
24724025c46SSoheil Hassas Yeganeh 	struct net *net = sock_net(sk);
2481da177e4SLinus Torvalds 
249f95b414eSGu Zheng 	for_each_cmsghdr(cmsg, msg) {
2501da177e4SLinus Torvalds 		if (!CMSG_OK(msg, cmsg))
2511da177e4SLinus Torvalds 			return -EINVAL;
2525337b5b7SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
253c8e6ad08SHannes Frederic Sowa 		if (allow_ipv6 &&
254c8e6ad08SHannes Frederic Sowa 		    cmsg->cmsg_level == SOL_IPV6 &&
255c8e6ad08SHannes Frederic Sowa 		    cmsg->cmsg_type == IPV6_PKTINFO) {
256c8e6ad08SHannes Frederic Sowa 			struct in6_pktinfo *src_info;
257c8e6ad08SHannes Frederic Sowa 
258c8e6ad08SHannes Frederic Sowa 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
259c8e6ad08SHannes Frederic Sowa 				return -EINVAL;
260c8e6ad08SHannes Frederic Sowa 			src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
261c8e6ad08SHannes Frederic Sowa 			if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
262c8e6ad08SHannes Frederic Sowa 				return -EINVAL;
2631cbec076SDavid Ahern 			if (src_info->ipi6_ifindex)
264c8e6ad08SHannes Frederic Sowa 				ipc->oif = src_info->ipi6_ifindex;
265c8e6ad08SHannes Frederic Sowa 			ipc->addr = src_info->ipi6_addr.s6_addr32[3];
266c8e6ad08SHannes Frederic Sowa 			continue;
267c8e6ad08SHannes Frederic Sowa 		}
268c8e6ad08SHannes Frederic Sowa #endif
26924025c46SSoheil Hassas Yeganeh 		if (cmsg->cmsg_level == SOL_SOCKET) {
270233baf9aSxu xin 			err = __sock_cmsg_send(sk, cmsg, &ipc->sockc);
2712632616bSEric Dumazet 			if (err)
2722632616bSEric Dumazet 				return err;
27324025c46SSoheil Hassas Yeganeh 			continue;
27424025c46SSoheil Hassas Yeganeh 		}
27524025c46SSoheil Hassas Yeganeh 
2761da177e4SLinus Torvalds 		if (cmsg->cmsg_level != SOL_IP)
2771da177e4SLinus Torvalds 			continue;
2781da177e4SLinus Torvalds 		switch (cmsg->cmsg_type) {
2791da177e4SLinus Torvalds 		case IP_RETOPTS:
2801ff8cebfSyuan linyu 			err = cmsg->cmsg_len - sizeof(struct cmsghdr);
28191948309SEric Dumazet 
28291948309SEric Dumazet 			/* Our caller is responsible for freeing ipc->opt */
283de40a3e8SChristoph Hellwig 			err = ip_options_get(net, &ipc->opt,
284de40a3e8SChristoph Hellwig 					     KERNEL_SOCKPTR(CMSG_DATA(cmsg)),
2854d52cfbeSEric Dumazet 					     err < 40 ? err : 40);
2861da177e4SLinus Torvalds 			if (err)
2871da177e4SLinus Torvalds 				return err;
2881da177e4SLinus Torvalds 			break;
2891da177e4SLinus Torvalds 		case IP_PKTINFO:
2901da177e4SLinus Torvalds 		{
2911da177e4SLinus Torvalds 			struct in_pktinfo *info;
2921da177e4SLinus Torvalds 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
2931da177e4SLinus Torvalds 				return -EINVAL;
2941da177e4SLinus Torvalds 			info = (struct in_pktinfo *)CMSG_DATA(cmsg);
2951cbec076SDavid Ahern 			if (info->ipi_ifindex)
2961da177e4SLinus Torvalds 				ipc->oif = info->ipi_ifindex;
2971da177e4SLinus Torvalds 			ipc->addr = info->ipi_spec_dst.s_addr;
2981da177e4SLinus Torvalds 			break;
2991da177e4SLinus Torvalds 		}
300f02db315SFrancesco Fusco 		case IP_TTL:
301f02db315SFrancesco Fusco 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
302f02db315SFrancesco Fusco 				return -EINVAL;
303f02db315SFrancesco Fusco 			val = *(int *)CMSG_DATA(cmsg);
304f02db315SFrancesco Fusco 			if (val < 1 || val > 255)
305f02db315SFrancesco Fusco 				return -EINVAL;
306f02db315SFrancesco Fusco 			ipc->ttl = val;
307f02db315SFrancesco Fusco 			break;
308f02db315SFrancesco Fusco 		case IP_TOS:
309e895cdceSEric Dumazet 			if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
310f02db315SFrancesco Fusco 				val = *(int *)CMSG_DATA(cmsg);
311e895cdceSEric Dumazet 			else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
312e895cdceSEric Dumazet 				val = *(u8 *)CMSG_DATA(cmsg);
313e895cdceSEric Dumazet 			else
314e895cdceSEric Dumazet 				return -EINVAL;
315f02db315SFrancesco Fusco 			if (val < 0 || val > 255)
316f02db315SFrancesco Fusco 				return -EINVAL;
317f02db315SFrancesco Fusco 			ipc->tos = val;
318f02db315SFrancesco Fusco 			ipc->priority = rt_tos2priority(ipc->tos);
319f02db315SFrancesco Fusco 			break;
320f02db315SFrancesco Fusco 
3211da177e4SLinus Torvalds 		default:
3221da177e4SLinus Torvalds 			return -EINVAL;
3231da177e4SLinus Torvalds 		}
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 	return 0;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
328592fcb9dSEric Dumazet static void ip_ra_destroy_rcu(struct rcu_head *head)
32966018506SEric Dumazet {
330592fcb9dSEric Dumazet 	struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
331592fcb9dSEric Dumazet 
332592fcb9dSEric Dumazet 	sock_put(ra->saved_sk);
333592fcb9dSEric Dumazet 	kfree(ra);
33466018506SEric Dumazet }
3351da177e4SLinus Torvalds 
3364d52cfbeSEric Dumazet int ip_ra_control(struct sock *sk, unsigned char on,
3374d52cfbeSEric Dumazet 		  void (*destructor)(struct sock *))
3381da177e4SLinus Torvalds {
33943a951e9SEric Dumazet 	struct ip_ra_chain *ra, *new_ra;
34043a951e9SEric Dumazet 	struct ip_ra_chain __rcu **rap;
3415796ef75SKirill Tkhai 	struct net *net = sock_net(sk);
3421da177e4SLinus Torvalds 
343c720c7e8SEric Dumazet 	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
3441da177e4SLinus Torvalds 		return -EINVAL;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
347425aa0e1SGen Zhang 	if (on && !new_ra)
348425aa0e1SGen Zhang 		return -ENOMEM;
3491da177e4SLinus Torvalds 
350d9ff3049SKirill Tkhai 	mutex_lock(&net->ipv4.ra_mutex);
3515796ef75SKirill Tkhai 	for (rap = &net->ipv4.ra_chain;
35276d3e153SKirill Tkhai 	     (ra = rcu_dereference_protected(*rap,
353d9ff3049SKirill Tkhai 			lockdep_is_held(&net->ipv4.ra_mutex))) != NULL;
35443a951e9SEric Dumazet 	     rap = &ra->next) {
3551da177e4SLinus Torvalds 		if (ra->sk == sk) {
3561da177e4SLinus Torvalds 			if (on) {
357d9ff3049SKirill Tkhai 				mutex_unlock(&net->ipv4.ra_mutex);
3581da177e4SLinus Torvalds 				kfree(new_ra);
3591da177e4SLinus Torvalds 				return -EADDRINUSE;
3601da177e4SLinus Torvalds 			}
361592fcb9dSEric Dumazet 			/* dont let ip_call_ra_chain() use sk again */
362592fcb9dSEric Dumazet 			ra->sk = NULL;
3638e380f00SEric Dumazet 			RCU_INIT_POINTER(*rap, ra->next);
364d9ff3049SKirill Tkhai 			mutex_unlock(&net->ipv4.ra_mutex);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 			if (ra->destructor)
3671da177e4SLinus Torvalds 				ra->destructor(sk);
368592fcb9dSEric Dumazet 			/*
369592fcb9dSEric Dumazet 			 * Delay sock_put(sk) and kfree(ra) after one rcu grace
370592fcb9dSEric Dumazet 			 * period. This guarantee ip_call_ra_chain() dont need
371592fcb9dSEric Dumazet 			 * to mess with socket refcounts.
372592fcb9dSEric Dumazet 			 */
373592fcb9dSEric Dumazet 			ra->saved_sk = sk;
374592fcb9dSEric Dumazet 			call_rcu(&ra->rcu, ip_ra_destroy_rcu);
3751da177e4SLinus Torvalds 			return 0;
3761da177e4SLinus Torvalds 		}
3771da177e4SLinus Torvalds 	}
37876d3e153SKirill Tkhai 	if (!new_ra) {
379d9ff3049SKirill Tkhai 		mutex_unlock(&net->ipv4.ra_mutex);
3801da177e4SLinus Torvalds 		return -ENOBUFS;
38176d3e153SKirill Tkhai 	}
3821da177e4SLinus Torvalds 	new_ra->sk = sk;
3831da177e4SLinus Torvalds 	new_ra->destructor = destructor;
3841da177e4SLinus Torvalds 
3858e380f00SEric Dumazet 	RCU_INIT_POINTER(new_ra->next, ra);
38666018506SEric Dumazet 	rcu_assign_pointer(*rap, new_ra);
3871da177e4SLinus Torvalds 	sock_hold(sk);
388d9ff3049SKirill Tkhai 	mutex_unlock(&net->ipv4.ra_mutex);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	return 0;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
393178c49d9SWillem de Bruijn static void ipv4_icmp_error_rfc4884(const struct sk_buff *skb,
394178c49d9SWillem de Bruijn 				    struct sock_ee_data_rfc4884 *out)
395178c49d9SWillem de Bruijn {
396178c49d9SWillem de Bruijn 	switch (icmp_hdr(skb)->type) {
397178c49d9SWillem de Bruijn 	case ICMP_DEST_UNREACH:
398178c49d9SWillem de Bruijn 	case ICMP_TIME_EXCEEDED:
399178c49d9SWillem de Bruijn 	case ICMP_PARAMETERPROB:
400178c49d9SWillem de Bruijn 		ip_icmp_error_rfc4884(skb, out, sizeof(struct icmphdr),
401178c49d9SWillem de Bruijn 				      icmp_hdr(skb)->un.reserved[1] * 4);
402178c49d9SWillem de Bruijn 	}
403178c49d9SWillem de Bruijn }
404178c49d9SWillem de Bruijn 
4051da177e4SLinus Torvalds void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
40635986b32SAl Viro 		   __be16 port, u32 info, u8 *payload)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	skb = skb_clone(skb, GFP_ATOMIC);
4111da177e4SLinus Torvalds 	if (!skb)
4121da177e4SLinus Torvalds 		return;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
4151da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
4161da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
41788c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_type = icmp_hdr(skb)->type;
41888c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_code = icmp_hdr(skb)->code;
4191da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
4201da177e4SLinus Torvalds 	serr->ee.ee_info = info;
4211da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
42288c7664fSArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
423d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb);
4241da177e4SLinus Torvalds 	serr->port = port;
4251da177e4SLinus Torvalds 
42600db4124SIan Morris 	if (skb_pull(skb, payload - skb->data)) {
427eba75c58SWillem de Bruijn 		if (inet_sk(sk)->recverr_rfc4884)
428178c49d9SWillem de Bruijn 			ipv4_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884);
429eba75c58SWillem de Bruijn 
430bd82393cSArnaldo Carvalho de Melo 		skb_reset_transport_header(skb);
431bd82393cSArnaldo Carvalho de Melo 		if (sock_queue_err_skb(sk, skb) == 0)
432bd82393cSArnaldo Carvalho de Melo 			return;
433bd82393cSArnaldo Carvalho de Melo 	}
4341da177e4SLinus Torvalds 	kfree_skb(skb);
4351da177e4SLinus Torvalds }
43642fb06b3SDavid Howells EXPORT_SYMBOL_GPL(ip_icmp_error);
4371da177e4SLinus Torvalds 
4380579016eSAl Viro void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4411da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
4421da177e4SLinus Torvalds 	struct iphdr *iph;
4431da177e4SLinus Torvalds 	struct sk_buff *skb;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	if (!inet->recverr)
4461da177e4SLinus Torvalds 		return;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
4491da177e4SLinus Torvalds 	if (!skb)
4501da177e4SLinus Torvalds 		return;
4511da177e4SLinus Torvalds 
4522ca9e6f2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr));
4532ca9e6f2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
454eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
4551da177e4SLinus Torvalds 	iph->daddr = daddr;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
4581da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
4591da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
4601da177e4SLinus Torvalds 	serr->ee.ee_type = 0;
4611da177e4SLinus Torvalds 	serr->ee.ee_code = 0;
4621da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
4631da177e4SLinus Torvalds 	serr->ee.ee_info = info;
4641da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
465d56f90a7SArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
4661da177e4SLinus Torvalds 	serr->port = port;
4671da177e4SLinus Torvalds 
46827a884dcSArnaldo Carvalho de Melo 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
469bd82393cSArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	if (sock_queue_err_skb(sk, skb))
4721da177e4SLinus Torvalds 		kfree_skb(skb);
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
47534b99df4SJulian Anastasov /* For some errors we have valid addr_offset even with zero payload and
47634b99df4SJulian Anastasov  * zero port. Also, addr_offset should be supported if port is set.
47734b99df4SJulian Anastasov  */
47834b99df4SJulian Anastasov static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
47934b99df4SJulian Anastasov {
48034b99df4SJulian Anastasov 	return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
48134b99df4SJulian Anastasov 	       serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
48234b99df4SJulian Anastasov }
48334b99df4SJulian Anastasov 
484c247f053SWillem de Bruijn /* IPv4 supports cmsg on all imcp errors and some timestamps
485c247f053SWillem de Bruijn  *
486c247f053SWillem de Bruijn  * Timestamp code paths do not initialize the fields expected by cmsg:
487c247f053SWillem de Bruijn  * the PKTINFO fields in skb->cb[]. Fill those in here.
488c247f053SWillem de Bruijn  */
489c247f053SWillem de Bruijn static bool ipv4_datagram_support_cmsg(const struct sock *sk,
490c247f053SWillem de Bruijn 				       struct sk_buff *skb,
491829ae9d6SWillem de Bruijn 				       int ee_origin)
492829ae9d6SWillem de Bruijn {
493c247f053SWillem de Bruijn 	struct in_pktinfo *info;
494829ae9d6SWillem de Bruijn 
495c247f053SWillem de Bruijn 	if (ee_origin == SO_EE_ORIGIN_ICMP)
496c247f053SWillem de Bruijn 		return true;
497c247f053SWillem de Bruijn 
498c247f053SWillem de Bruijn 	if (ee_origin == SO_EE_ORIGIN_LOCAL)
499c247f053SWillem de Bruijn 		return false;
500c247f053SWillem de Bruijn 
501c247f053SWillem de Bruijn 	/* Support IP_PKTINFO on tstamp packets if requested, to correlate
5021862d620SWillem de Bruijn 	 * timestamp with egress dev. Not possible for packets without iif
503c247f053SWillem de Bruijn 	 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
504c247f053SWillem de Bruijn 	 */
5051862d620SWillem de Bruijn 	info = PKTINFO_SKB_CB(skb);
5061862d620SWillem de Bruijn 	if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
5071862d620SWillem de Bruijn 	    !info->ipi_ifindex)
508829ae9d6SWillem de Bruijn 		return false;
509829ae9d6SWillem de Bruijn 
510829ae9d6SWillem de Bruijn 	info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
511829ae9d6SWillem de Bruijn 	return true;
512829ae9d6SWillem de Bruijn }
513829ae9d6SWillem de Bruijn 
5141da177e4SLinus Torvalds /*
5151da177e4SLinus Torvalds  *	Handle MSG_ERRQUEUE
5161da177e4SLinus Torvalds  */
51785fbaa75SHannes Frederic Sowa int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
520364a9e93SWillem de Bruijn 	struct sk_buff *skb;
521342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
5221da177e4SLinus Torvalds 	struct {
5231da177e4SLinus Torvalds 		struct sock_extended_err ee;
5241da177e4SLinus Torvalds 		struct sockaddr_in	 offender;
5251da177e4SLinus Torvalds 	} errhdr;
5261da177e4SLinus Torvalds 	int err;
5271da177e4SLinus Torvalds 	int copied;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	err = -EAGAIN;
530364a9e93SWillem de Bruijn 	skb = sock_dequeue_err_skb(sk);
53151456b29SIan Morris 	if (!skb)
5321da177e4SLinus Torvalds 		goto out;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	copied = skb->len;
5351da177e4SLinus Torvalds 	if (copied > len) {
5361da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
5371da177e4SLinus Torvalds 		copied = len;
5381da177e4SLinus Torvalds 	}
53951f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
540960a2628SEric Dumazet 	if (unlikely(err)) {
541960a2628SEric Dumazet 		kfree_skb(skb);
542960a2628SEric Dumazet 		return err;
543960a2628SEric Dumazet 	}
5441da177e4SLinus Torvalds 	sock_recv_timestamp(msg, sk, skb);
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
5471da177e4SLinus Torvalds 
54834b99df4SJulian Anastasov 	if (sin && ipv4_datagram_support_addr(serr)) {
5491da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
550d56f90a7SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
551d56f90a7SArnaldo Carvalho de Melo 						   serr->addr_offset);
5521da177e4SLinus Torvalds 		sin->sin_port = serr->port;
5531da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
55485fbaa75SHannes Frederic Sowa 		*addr_len = sizeof(*sin);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
5581da177e4SLinus Torvalds 	sin = &errhdr.offender;
559f812116bSWillem de Bruijn 	memset(sin, 0, sizeof(*sin));
560829ae9d6SWillem de Bruijn 
561c247f053SWillem de Bruijn 	if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
5621da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
563eddc9ec5SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
564f812116bSWillem de Bruijn 		if (inet_sk(sk)->cmsg_flags)
5651da177e4SLinus Torvalds 			ip_cmsg_recv(msg, skb);
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	/* Now we could try to dump offended packet options */
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	msg->msg_flags |= MSG_ERRQUEUE;
5731da177e4SLinus Torvalds 	err = copied;
5741da177e4SLinus Torvalds 
575960a2628SEric Dumazet 	consume_skb(skb);
5761da177e4SLinus Torvalds out:
5771da177e4SLinus Torvalds 	return err;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5804f47d5d5SPoorva Sonparote void __ip_sock_set_tos(struct sock *sk, int val)
5816ebf71baSChristoph Hellwig {
5826ebf71baSChristoph Hellwig 	if (sk->sk_type == SOCK_STREAM) {
5836ebf71baSChristoph Hellwig 		val &= ~INET_ECN_MASK;
5846ebf71baSChristoph Hellwig 		val |= inet_sk(sk)->tos & INET_ECN_MASK;
5856ebf71baSChristoph Hellwig 	}
5866ebf71baSChristoph Hellwig 	if (inet_sk(sk)->tos != val) {
5876ebf71baSChristoph Hellwig 		inet_sk(sk)->tos = val;
5886ebf71baSChristoph Hellwig 		sk->sk_priority = rt_tos2priority(val);
5896ebf71baSChristoph Hellwig 		sk_dst_reset(sk);
5906ebf71baSChristoph Hellwig 	}
5916ebf71baSChristoph Hellwig }
5926ebf71baSChristoph Hellwig 
5936ebf71baSChristoph Hellwig void ip_sock_set_tos(struct sock *sk, int val)
5946ebf71baSChristoph Hellwig {
5956ebf71baSChristoph Hellwig 	lock_sock(sk);
5966ebf71baSChristoph Hellwig 	__ip_sock_set_tos(sk, val);
5976ebf71baSChristoph Hellwig 	release_sock(sk);
5986ebf71baSChristoph Hellwig }
5996ebf71baSChristoph Hellwig EXPORT_SYMBOL(ip_sock_set_tos);
6001da177e4SLinus Torvalds 
601c4e446bfSChristoph Hellwig void ip_sock_set_freebind(struct sock *sk)
602c4e446bfSChristoph Hellwig {
603c4e446bfSChristoph Hellwig 	lock_sock(sk);
604c4e446bfSChristoph Hellwig 	inet_sk(sk)->freebind = true;
605c4e446bfSChristoph Hellwig 	release_sock(sk);
606c4e446bfSChristoph Hellwig }
607c4e446bfSChristoph Hellwig EXPORT_SYMBOL(ip_sock_set_freebind);
608c4e446bfSChristoph Hellwig 
609db45c0efSChristoph Hellwig void ip_sock_set_recverr(struct sock *sk)
610db45c0efSChristoph Hellwig {
611db45c0efSChristoph Hellwig 	lock_sock(sk);
612db45c0efSChristoph Hellwig 	inet_sk(sk)->recverr = true;
613db45c0efSChristoph Hellwig 	release_sock(sk);
614db45c0efSChristoph Hellwig }
615db45c0efSChristoph Hellwig EXPORT_SYMBOL(ip_sock_set_recverr);
616db45c0efSChristoph Hellwig 
6172de569bdSChristoph Hellwig int ip_sock_set_mtu_discover(struct sock *sk, int val)
6182de569bdSChristoph Hellwig {
6192de569bdSChristoph Hellwig 	if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
6202de569bdSChristoph Hellwig 		return -EINVAL;
6212de569bdSChristoph Hellwig 	lock_sock(sk);
6222de569bdSChristoph Hellwig 	inet_sk(sk)->pmtudisc = val;
6232de569bdSChristoph Hellwig 	release_sock(sk);
6242de569bdSChristoph Hellwig 	return 0;
6252de569bdSChristoph Hellwig }
6262de569bdSChristoph Hellwig EXPORT_SYMBOL(ip_sock_set_mtu_discover);
6272de569bdSChristoph Hellwig 
628c1f9ec57SChristoph Hellwig void ip_sock_set_pktinfo(struct sock *sk)
629c1f9ec57SChristoph Hellwig {
630c1f9ec57SChristoph Hellwig 	lock_sock(sk);
631c1f9ec57SChristoph Hellwig 	inet_sk(sk)->cmsg_flags |= IP_CMSG_PKTINFO;
632c1f9ec57SChristoph Hellwig 	release_sock(sk);
633c1f9ec57SChristoph Hellwig }
634c1f9ec57SChristoph Hellwig EXPORT_SYMBOL(ip_sock_set_pktinfo);
635c1f9ec57SChristoph Hellwig 
6361da177e4SLinus Torvalds /*
6374d52cfbeSEric Dumazet  *	Socket option code for IP. This is the end of the line after any
6384d52cfbeSEric Dumazet  *	TCP,UDP etc options on an IP socket.
6391da177e4SLinus Torvalds  */
640baf606d9SMarcelo Ricardo Leitner static bool setsockopt_needs_rtnl(int optname)
641baf606d9SMarcelo Ricardo Leitner {
642baf606d9SMarcelo Ricardo Leitner 	switch (optname) {
643baf606d9SMarcelo Ricardo Leitner 	case IP_ADD_MEMBERSHIP:
644baf606d9SMarcelo Ricardo Leitner 	case IP_ADD_SOURCE_MEMBERSHIP:
64554ff9ef3SMarcelo Ricardo Leitner 	case IP_BLOCK_SOURCE:
646baf606d9SMarcelo Ricardo Leitner 	case IP_DROP_MEMBERSHIP:
64754ff9ef3SMarcelo Ricardo Leitner 	case IP_DROP_SOURCE_MEMBERSHIP:
64854ff9ef3SMarcelo Ricardo Leitner 	case IP_MSFILTER:
64954ff9ef3SMarcelo Ricardo Leitner 	case IP_UNBLOCK_SOURCE:
65054ff9ef3SMarcelo Ricardo Leitner 	case MCAST_BLOCK_SOURCE:
65154ff9ef3SMarcelo Ricardo Leitner 	case MCAST_MSFILTER:
652baf606d9SMarcelo Ricardo Leitner 	case MCAST_JOIN_GROUP:
65354ff9ef3SMarcelo Ricardo Leitner 	case MCAST_JOIN_SOURCE_GROUP:
654baf606d9SMarcelo Ricardo Leitner 	case MCAST_LEAVE_GROUP:
65554ff9ef3SMarcelo Ricardo Leitner 	case MCAST_LEAVE_SOURCE_GROUP:
65654ff9ef3SMarcelo Ricardo Leitner 	case MCAST_UNBLOCK_SOURCE:
657baf606d9SMarcelo Ricardo Leitner 		return true;
658baf606d9SMarcelo Ricardo Leitner 	}
659baf606d9SMarcelo Ricardo Leitner 	return false;
660baf606d9SMarcelo Ricardo Leitner }
6611da177e4SLinus Torvalds 
662e986d4daSAl Viro static int set_mcast_msfilter(struct sock *sk, int ifindex,
663e986d4daSAl Viro 			      int numsrc, int fmode,
664e986d4daSAl Viro 			      struct sockaddr_storage *group,
665e986d4daSAl Viro 			      struct sockaddr_storage *list)
666e986d4daSAl Viro {
667e986d4daSAl Viro 	struct ip_msfilter *msf;
668e986d4daSAl Viro 	struct sockaddr_in *psin;
669e986d4daSAl Viro 	int err, i;
670e986d4daSAl Viro 
6714167a960SGustavo A. R. Silva 	msf = kmalloc(IP_MSFILTER_SIZE(numsrc), GFP_KERNEL);
672e986d4daSAl Viro 	if (!msf)
673e986d4daSAl Viro 		return -ENOBUFS;
674e986d4daSAl Viro 
675e986d4daSAl Viro 	psin = (struct sockaddr_in *)group;
676e986d4daSAl Viro 	if (psin->sin_family != AF_INET)
677e986d4daSAl Viro 		goto Eaddrnotavail;
678e986d4daSAl Viro 	msf->imsf_multiaddr = psin->sin_addr.s_addr;
679e986d4daSAl Viro 	msf->imsf_interface = 0;
680e986d4daSAl Viro 	msf->imsf_fmode = fmode;
681e986d4daSAl Viro 	msf->imsf_numsrc = numsrc;
682e986d4daSAl Viro 	for (i = 0; i < numsrc; ++i) {
683e986d4daSAl Viro 		psin = (struct sockaddr_in *)&list[i];
684e986d4daSAl Viro 
685e986d4daSAl Viro 		if (psin->sin_family != AF_INET)
686e986d4daSAl Viro 			goto Eaddrnotavail;
6872d3e5cafSGustavo A. R. Silva 		msf->imsf_slist_flex[i] = psin->sin_addr.s_addr;
688e986d4daSAl Viro 	}
689e986d4daSAl Viro 	err = ip_mc_msfilter(sk, msf, ifindex);
690e986d4daSAl Viro 	kfree(msf);
691e986d4daSAl Viro 	return err;
692e986d4daSAl Viro 
693e986d4daSAl Viro Eaddrnotavail:
694e986d4daSAl Viro 	kfree(msf);
695e986d4daSAl Viro 	return -EADDRNOTAVAIL;
696e986d4daSAl Viro }
697e986d4daSAl Viro 
69889654c5fSChristoph Hellwig static int copy_group_source_from_sockptr(struct group_source_req *greqs,
69989654c5fSChristoph Hellwig 		sockptr_t optval, int optlen)
7002bbf8c1eSAl Viro {
701b6238c04SChristoph Hellwig 	if (in_compat_syscall()) {
702b6238c04SChristoph Hellwig 		struct compat_group_source_req gr32;
703b6238c04SChristoph Hellwig 
704b6238c04SChristoph Hellwig 		if (optlen != sizeof(gr32))
705b6238c04SChristoph Hellwig 			return -EINVAL;
70689654c5fSChristoph Hellwig 		if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
707b6238c04SChristoph Hellwig 			return -EFAULT;
708b6238c04SChristoph Hellwig 		greqs->gsr_interface = gr32.gsr_interface;
709b6238c04SChristoph Hellwig 		greqs->gsr_group = gr32.gsr_group;
710b6238c04SChristoph Hellwig 		greqs->gsr_source = gr32.gsr_source;
711b6238c04SChristoph Hellwig 	} else {
712b6238c04SChristoph Hellwig 		if (optlen != sizeof(*greqs))
713b6238c04SChristoph Hellwig 			return -EINVAL;
71489654c5fSChristoph Hellwig 		if (copy_from_sockptr(greqs, optval, sizeof(*greqs)))
715b6238c04SChristoph Hellwig 			return -EFAULT;
716b6238c04SChristoph Hellwig 	}
717b6238c04SChristoph Hellwig 
718b6238c04SChristoph Hellwig 	return 0;
719b6238c04SChristoph Hellwig }
720b6238c04SChristoph Hellwig 
721b6238c04SChristoph Hellwig static int do_mcast_group_source(struct sock *sk, int optname,
72289654c5fSChristoph Hellwig 		sockptr_t optval, int optlen)
723b6238c04SChristoph Hellwig {
724b6238c04SChristoph Hellwig 	struct group_source_req greqs;
7252bbf8c1eSAl Viro 	struct ip_mreq_source mreqs;
7262bbf8c1eSAl Viro 	struct sockaddr_in *psin;
7272bbf8c1eSAl Viro 	int omode, add, err;
7282bbf8c1eSAl Viro 
72989654c5fSChristoph Hellwig 	err = copy_group_source_from_sockptr(&greqs, optval, optlen);
730b6238c04SChristoph Hellwig 	if (err)
731b6238c04SChristoph Hellwig 		return err;
732b6238c04SChristoph Hellwig 
733b6238c04SChristoph Hellwig 	if (greqs.gsr_group.ss_family != AF_INET ||
734b6238c04SChristoph Hellwig 	    greqs.gsr_source.ss_family != AF_INET)
7352bbf8c1eSAl Viro 		return -EADDRNOTAVAIL;
7362bbf8c1eSAl Viro 
737b6238c04SChristoph Hellwig 	psin = (struct sockaddr_in *)&greqs.gsr_group;
7382bbf8c1eSAl Viro 	mreqs.imr_multiaddr = psin->sin_addr.s_addr;
739b6238c04SChristoph Hellwig 	psin = (struct sockaddr_in *)&greqs.gsr_source;
7402bbf8c1eSAl Viro 	mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
7412bbf8c1eSAl Viro 	mreqs.imr_interface = 0; /* use index for mc_source */
7422bbf8c1eSAl Viro 
7432bbf8c1eSAl Viro 	if (optname == MCAST_BLOCK_SOURCE) {
7442bbf8c1eSAl Viro 		omode = MCAST_EXCLUDE;
7452bbf8c1eSAl Viro 		add = 1;
7462bbf8c1eSAl Viro 	} else if (optname == MCAST_UNBLOCK_SOURCE) {
7472bbf8c1eSAl Viro 		omode = MCAST_EXCLUDE;
7482bbf8c1eSAl Viro 		add = 0;
7492bbf8c1eSAl Viro 	} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
7502bbf8c1eSAl Viro 		struct ip_mreqn mreq;
7512bbf8c1eSAl Viro 
752b6238c04SChristoph Hellwig 		psin = (struct sockaddr_in *)&greqs.gsr_group;
7532bbf8c1eSAl Viro 		mreq.imr_multiaddr = psin->sin_addr;
7542bbf8c1eSAl Viro 		mreq.imr_address.s_addr = 0;
755b6238c04SChristoph Hellwig 		mreq.imr_ifindex = greqs.gsr_interface;
7562bbf8c1eSAl Viro 		err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
7572bbf8c1eSAl Viro 		if (err && err != -EADDRINUSE)
7582bbf8c1eSAl Viro 			return err;
759b6238c04SChristoph Hellwig 		greqs.gsr_interface = mreq.imr_ifindex;
7602bbf8c1eSAl Viro 		omode = MCAST_INCLUDE;
7612bbf8c1eSAl Viro 		add = 1;
7622bbf8c1eSAl Viro 	} else /* MCAST_LEAVE_SOURCE_GROUP */ {
7632bbf8c1eSAl Viro 		omode = MCAST_INCLUDE;
7642bbf8c1eSAl Viro 		add = 0;
7652bbf8c1eSAl Viro 	}
766b6238c04SChristoph Hellwig 	return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface);
7672bbf8c1eSAl Viro }
7682bbf8c1eSAl Viro 
76989654c5fSChristoph Hellwig static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen)
770d62c38f6SChristoph Hellwig {
771d62c38f6SChristoph Hellwig 	struct group_filter *gsf = NULL;
772d62c38f6SChristoph Hellwig 	int err;
773d62c38f6SChristoph Hellwig 
774d62c38f6SChristoph Hellwig 	if (optlen < GROUP_FILTER_SIZE(0))
775d62c38f6SChristoph Hellwig 		return -EINVAL;
7767de6d09fSKuniyuki Iwashima 	if (optlen > READ_ONCE(sysctl_optmem_max))
777d62c38f6SChristoph Hellwig 		return -ENOBUFS;
778d62c38f6SChristoph Hellwig 
77989654c5fSChristoph Hellwig 	gsf = memdup_sockptr(optval, optlen);
780d62c38f6SChristoph Hellwig 	if (IS_ERR(gsf))
781d62c38f6SChristoph Hellwig 		return PTR_ERR(gsf);
782d62c38f6SChristoph Hellwig 
783d62c38f6SChristoph Hellwig 	/* numsrc >= (4G-140)/128 overflow in 32 bits */
784d62c38f6SChristoph Hellwig 	err = -ENOBUFS;
785d62c38f6SChristoph Hellwig 	if (gsf->gf_numsrc >= 0x1ffffff ||
7866ae0f2e5SKuniyuki Iwashima 	    gsf->gf_numsrc > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf))
787d62c38f6SChristoph Hellwig 		goto out_free_gsf;
788d62c38f6SChristoph Hellwig 
789d62c38f6SChristoph Hellwig 	err = -EINVAL;
790d62c38f6SChristoph Hellwig 	if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
791d62c38f6SChristoph Hellwig 		goto out_free_gsf;
792d62c38f6SChristoph Hellwig 
793d62c38f6SChristoph Hellwig 	err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc,
794db243b79SGustavo A. R. Silva 				 gsf->gf_fmode, &gsf->gf_group,
795db243b79SGustavo A. R. Silva 				 gsf->gf_slist_flex);
796d62c38f6SChristoph Hellwig out_free_gsf:
797d62c38f6SChristoph Hellwig 	kfree(gsf);
798d62c38f6SChristoph Hellwig 	return err;
799d62c38f6SChristoph Hellwig }
800d62c38f6SChristoph Hellwig 
80189654c5fSChristoph Hellwig static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
802d62c38f6SChristoph Hellwig 		int optlen)
803d62c38f6SChristoph Hellwig {
804db243b79SGustavo A. R. Silva 	const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
805d62c38f6SChristoph Hellwig 	struct compat_group_filter *gf32;
806d62c38f6SChristoph Hellwig 	unsigned int n;
807d62c38f6SChristoph Hellwig 	void *p;
808d62c38f6SChristoph Hellwig 	int err;
809d62c38f6SChristoph Hellwig 
810d62c38f6SChristoph Hellwig 	if (optlen < size0)
811d62c38f6SChristoph Hellwig 		return -EINVAL;
8127de6d09fSKuniyuki Iwashima 	if (optlen > READ_ONCE(sysctl_optmem_max) - 4)
813d62c38f6SChristoph Hellwig 		return -ENOBUFS;
814d62c38f6SChristoph Hellwig 
815d62c38f6SChristoph Hellwig 	p = kmalloc(optlen + 4, GFP_KERNEL);
816d62c38f6SChristoph Hellwig 	if (!p)
817d62c38f6SChristoph Hellwig 		return -ENOMEM;
818db243b79SGustavo A. R. Silva 	gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
819d62c38f6SChristoph Hellwig 
820d62c38f6SChristoph Hellwig 	err = -EFAULT;
82189654c5fSChristoph Hellwig 	if (copy_from_sockptr(gf32, optval, optlen))
822d62c38f6SChristoph Hellwig 		goto out_free_gsf;
823d62c38f6SChristoph Hellwig 
824d62c38f6SChristoph Hellwig 	/* numsrc >= (4G-140)/128 overflow in 32 bits */
825d62c38f6SChristoph Hellwig 	n = gf32->gf_numsrc;
826d62c38f6SChristoph Hellwig 	err = -ENOBUFS;
827d62c38f6SChristoph Hellwig 	if (n >= 0x1ffffff)
828d62c38f6SChristoph Hellwig 		goto out_free_gsf;
829d62c38f6SChristoph Hellwig 
830d62c38f6SChristoph Hellwig 	err = -EINVAL;
831db243b79SGustavo A. R. Silva 	if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
832d62c38f6SChristoph Hellwig 		goto out_free_gsf;
833d62c38f6SChristoph Hellwig 
834d62c38f6SChristoph Hellwig 	/* numsrc >= (4G-140)/128 overflow in 32 bits */
835d62c38f6SChristoph Hellwig 	err = -ENOBUFS;
8366ae0f2e5SKuniyuki Iwashima 	if (n > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf))
837b6238c04SChristoph Hellwig 		goto out_free_gsf;
838d62c38f6SChristoph Hellwig 	err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode,
839db243b79SGustavo A. R. Silva 				 &gf32->gf_group, gf32->gf_slist_flex);
840d62c38f6SChristoph Hellwig out_free_gsf:
841d62c38f6SChristoph Hellwig 	kfree(p);
842d62c38f6SChristoph Hellwig 	return err;
843d62c38f6SChristoph Hellwig }
844d62c38f6SChristoph Hellwig 
84502caad7cSChristoph Hellwig static int ip_mcast_join_leave(struct sock *sk, int optname,
84689654c5fSChristoph Hellwig 		sockptr_t optval, int optlen)
84702caad7cSChristoph Hellwig {
84802caad7cSChristoph Hellwig 	struct ip_mreqn mreq = { };
84902caad7cSChristoph Hellwig 	struct sockaddr_in *psin;
85002caad7cSChristoph Hellwig 	struct group_req greq;
85102caad7cSChristoph Hellwig 
85202caad7cSChristoph Hellwig 	if (optlen < sizeof(struct group_req))
85302caad7cSChristoph Hellwig 		return -EINVAL;
85489654c5fSChristoph Hellwig 	if (copy_from_sockptr(&greq, optval, sizeof(greq)))
85502caad7cSChristoph Hellwig 		return -EFAULT;
85602caad7cSChristoph Hellwig 
85702caad7cSChristoph Hellwig 	psin = (struct sockaddr_in *)&greq.gr_group;
85802caad7cSChristoph Hellwig 	if (psin->sin_family != AF_INET)
85902caad7cSChristoph Hellwig 		return -EINVAL;
86002caad7cSChristoph Hellwig 	mreq.imr_multiaddr = psin->sin_addr;
86102caad7cSChristoph Hellwig 	mreq.imr_ifindex = greq.gr_interface;
86202caad7cSChristoph Hellwig 	if (optname == MCAST_JOIN_GROUP)
86302caad7cSChristoph Hellwig 		return ip_mc_join_group(sk, &mreq);
86402caad7cSChristoph Hellwig 	return ip_mc_leave_group(sk, &mreq);
86502caad7cSChristoph Hellwig }
86602caad7cSChristoph Hellwig 
86702caad7cSChristoph Hellwig static int compat_ip_mcast_join_leave(struct sock *sk, int optname,
86889654c5fSChristoph Hellwig 		sockptr_t optval, int optlen)
86902caad7cSChristoph Hellwig {
87002caad7cSChristoph Hellwig 	struct compat_group_req greq;
87102caad7cSChristoph Hellwig 	struct ip_mreqn mreq = { };
87202caad7cSChristoph Hellwig 	struct sockaddr_in *psin;
87302caad7cSChristoph Hellwig 
87402caad7cSChristoph Hellwig 	if (optlen < sizeof(struct compat_group_req))
87502caad7cSChristoph Hellwig 		return -EINVAL;
87689654c5fSChristoph Hellwig 	if (copy_from_sockptr(&greq, optval, sizeof(greq)))
87702caad7cSChristoph Hellwig 		return -EFAULT;
87802caad7cSChristoph Hellwig 
87902caad7cSChristoph Hellwig 	psin = (struct sockaddr_in *)&greq.gr_group;
88002caad7cSChristoph Hellwig 	if (psin->sin_family != AF_INET)
88102caad7cSChristoph Hellwig 		return -EINVAL;
88202caad7cSChristoph Hellwig 	mreq.imr_multiaddr = psin->sin_addr;
88302caad7cSChristoph Hellwig 	mreq.imr_ifindex = greq.gr_interface;
88402caad7cSChristoph Hellwig 
88502caad7cSChristoph Hellwig 	if (optname == MCAST_JOIN_GROUP)
886b6238c04SChristoph Hellwig 		return ip_mc_join_group(sk, &mreq);
887b6238c04SChristoph Hellwig 	return ip_mc_leave_group(sk, &mreq);
88802caad7cSChristoph Hellwig }
88902caad7cSChristoph Hellwig 
890020e71a3SEric Dumazet DEFINE_STATIC_KEY_FALSE(ip4_min_ttl);
891020e71a3SEric Dumazet 
892ee7f1e13SMartin KaFai Lau int do_ip_setsockopt(struct sock *sk, int level, int optname,
89389654c5fSChristoph Hellwig 		     sockptr_t optval, unsigned int optlen)
8941da177e4SLinus Torvalds {
8951da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
896166b6b2dSNikolay Borisov 	struct net *net = sock_net(sk);
8971da177e4SLinus Torvalds 	int val = 0, err;
898baf606d9SMarcelo Ricardo Leitner 	bool needs_rtnl = setsockopt_needs_rtnl(optname);
8991da177e4SLinus Torvalds 
9000c9f79beSXi Wang 	switch (optname) {
9010c9f79beSXi Wang 	case IP_PKTINFO:
9020c9f79beSXi Wang 	case IP_RECVTTL:
9030c9f79beSXi Wang 	case IP_RECVOPTS:
9040c9f79beSXi Wang 	case IP_RECVTOS:
9050c9f79beSXi Wang 	case IP_RETOPTS:
9060c9f79beSXi Wang 	case IP_TOS:
9070c9f79beSXi Wang 	case IP_TTL:
9080c9f79beSXi Wang 	case IP_HDRINCL:
9090c9f79beSXi Wang 	case IP_MTU_DISCOVER:
9100c9f79beSXi Wang 	case IP_RECVERR:
9110c9f79beSXi Wang 	case IP_ROUTER_ALERT:
9120c9f79beSXi Wang 	case IP_FREEBIND:
9130c9f79beSXi Wang 	case IP_PASSSEC:
9140c9f79beSXi Wang 	case IP_TRANSPARENT:
9150c9f79beSXi Wang 	case IP_MINTTL:
9160c9f79beSXi Wang 	case IP_NODEFRAG:
91790c337daSEric Dumazet 	case IP_BIND_ADDRESS_NO_PORT:
9180c9f79beSXi Wang 	case IP_UNICAST_IF:
9190c9f79beSXi Wang 	case IP_MULTICAST_TTL:
9200c9f79beSXi Wang 	case IP_MULTICAST_ALL:
9210c9f79beSXi Wang 	case IP_MULTICAST_LOOP:
9220c9f79beSXi Wang 	case IP_RECVORIGDSTADDR:
923ad6f939aSTom Herbert 	case IP_CHECKSUM:
92470ecc248SWillem de Bruijn 	case IP_RECVFRAGSIZE:
925eba75c58SWillem de Bruijn 	case IP_RECVERR_RFC4884:
926*91d0b78cSJakub Sitnicki 	case IP_LOCAL_PORT_RANGE:
9271da177e4SLinus Torvalds 		if (optlen >= sizeof(int)) {
92889654c5fSChristoph Hellwig 			if (copy_from_sockptr(&val, optval, sizeof(val)))
9291da177e4SLinus Torvalds 				return -EFAULT;
9301da177e4SLinus Torvalds 		} else if (optlen >= sizeof(char)) {
9311da177e4SLinus Torvalds 			unsigned char ucval;
9321da177e4SLinus Torvalds 
93389654c5fSChristoph Hellwig 			if (copy_from_sockptr(&ucval, optval, sizeof(ucval)))
9341da177e4SLinus Torvalds 				return -EFAULT;
9351da177e4SLinus Torvalds 			val = (int) ucval;
9361da177e4SLinus Torvalds 		}
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	/* If optlen==0, it is equivalent to val == 0 */
9401da177e4SLinus Torvalds 
9410526947fSKirill Tkhai 	if (optname == IP_ROUTER_ALERT)
9420526947fSKirill Tkhai 		return ip_ra_control(sk, val ? 1 : 0, NULL);
9436a9fb947SPavel Emelyanov 	if (ip_mroute_opt(optname))
94489654c5fSChristoph Hellwig 		return ip_mroute_setsockopt(sk, optname, optval, optlen);
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	err = 0;
947baf606d9SMarcelo Ricardo Leitner 	if (needs_rtnl)
948baf606d9SMarcelo Ricardo Leitner 		rtnl_lock();
9491df055d3SMartin KaFai Lau 	sockopt_lock_sock(sk);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	switch (optname) {
9521da177e4SLinus Torvalds 	case IP_OPTIONS:
9531da177e4SLinus Torvalds 	{
954f6d8bd05SEric Dumazet 		struct ip_options_rcu *old, *opt = NULL;
955f6d8bd05SEric Dumazet 
95665a1c4ffSroel kluin 		if (optlen > 40)
9571da177e4SLinus Torvalds 			goto e_inval;
95889654c5fSChristoph Hellwig 		err = ip_options_get(sock_net(sk), &opt, optval, optlen);
9591da177e4SLinus Torvalds 		if (err)
9601da177e4SLinus Torvalds 			break;
961f6d8bd05SEric Dumazet 		old = rcu_dereference_protected(inet->inet_opt,
9621e1d04e6SHannes Frederic Sowa 						lockdep_sock_is_held(sk));
963d83d8461SArnaldo Carvalho de Melo 		if (inet->is_icsk) {
964d83d8461SArnaldo Carvalho de Melo 			struct inet_connection_sock *icsk = inet_csk(sk);
965dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
9661da177e4SLinus Torvalds 			if (sk->sk_family == PF_INET ||
9671da177e4SLinus Torvalds 			    (!((1 << sk->sk_state) &
9681da177e4SLinus Torvalds 			       (TCPF_LISTEN | TCPF_CLOSE)) &&
969c720c7e8SEric Dumazet 			     inet->inet_daddr != LOOPBACK4_IPV6)) {
9701da177e4SLinus Torvalds #endif
971f6d8bd05SEric Dumazet 				if (old)
972f6d8bd05SEric Dumazet 					icsk->icsk_ext_hdr_len -= old->opt.optlen;
9731da177e4SLinus Torvalds 				if (opt)
974f6d8bd05SEric Dumazet 					icsk->icsk_ext_hdr_len += opt->opt.optlen;
975d83d8461SArnaldo Carvalho de Melo 				icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
976dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
9771da177e4SLinus Torvalds 			}
9781da177e4SLinus Torvalds #endif
9791da177e4SLinus Torvalds 		}
980f6d8bd05SEric Dumazet 		rcu_assign_pointer(inet->inet_opt, opt);
981f6d8bd05SEric Dumazet 		if (old)
982605b4afeSPaul E. McKenney 			kfree_rcu(old, rcu);
9831da177e4SLinus Torvalds 		break;
9841da177e4SLinus Torvalds 	}
9851da177e4SLinus Torvalds 	case IP_PKTINFO:
9861da177e4SLinus Torvalds 		if (val)
9871da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_PKTINFO;
9881da177e4SLinus Torvalds 		else
9891da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
9901da177e4SLinus Torvalds 		break;
9911da177e4SLinus Torvalds 	case IP_RECVTTL:
9921da177e4SLinus Torvalds 		if (val)
9931da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TTL;
9941da177e4SLinus Torvalds 		else
9951da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TTL;
9961da177e4SLinus Torvalds 		break;
9971da177e4SLinus Torvalds 	case IP_RECVTOS:
9981da177e4SLinus Torvalds 		if (val)
9991da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TOS;
10001da177e4SLinus Torvalds 		else
10011da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TOS;
10021da177e4SLinus Torvalds 		break;
10031da177e4SLinus Torvalds 	case IP_RECVOPTS:
10041da177e4SLinus Torvalds 		if (val)
10051da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_RECVOPTS;
10061da177e4SLinus Torvalds 		else
10071da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
10081da177e4SLinus Torvalds 		break;
10091da177e4SLinus Torvalds 	case IP_RETOPTS:
10101da177e4SLinus Torvalds 		if (val)
10111da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_RETOPTS;
10121da177e4SLinus Torvalds 		else
10131da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
10141da177e4SLinus Torvalds 		break;
10152c7946a7SCatherine Zhang 	case IP_PASSSEC:
10162c7946a7SCatherine Zhang 		if (val)
10172c7946a7SCatherine Zhang 			inet->cmsg_flags |= IP_CMSG_PASSSEC;
10182c7946a7SCatherine Zhang 		else
10192c7946a7SCatherine Zhang 			inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
10202c7946a7SCatherine Zhang 		break;
1021e8b2dfe9SBalazs Scheidler 	case IP_RECVORIGDSTADDR:
1022e8b2dfe9SBalazs Scheidler 		if (val)
1023e8b2dfe9SBalazs Scheidler 			inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
1024e8b2dfe9SBalazs Scheidler 		else
1025e8b2dfe9SBalazs Scheidler 			inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
1026e8b2dfe9SBalazs Scheidler 		break;
1027ad6f939aSTom Herbert 	case IP_CHECKSUM:
1028ad6f939aSTom Herbert 		if (val) {
1029ad6f939aSTom Herbert 			if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
1030ad6f939aSTom Herbert 				inet_inc_convert_csum(sk);
1031ad6f939aSTom Herbert 				inet->cmsg_flags |= IP_CMSG_CHECKSUM;
1032ad6f939aSTom Herbert 			}
1033ad6f939aSTom Herbert 		} else {
1034ad6f939aSTom Herbert 			if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
1035ad6f939aSTom Herbert 				inet_dec_convert_csum(sk);
1036ad6f939aSTom Herbert 				inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
1037ad6f939aSTom Herbert 			}
1038ad6f939aSTom Herbert 		}
1039ad6f939aSTom Herbert 		break;
104070ecc248SWillem de Bruijn 	case IP_RECVFRAGSIZE:
104170ecc248SWillem de Bruijn 		if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
104270ecc248SWillem de Bruijn 			goto e_inval;
104370ecc248SWillem de Bruijn 		if (val)
104470ecc248SWillem de Bruijn 			inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
104570ecc248SWillem de Bruijn 		else
104670ecc248SWillem de Bruijn 			inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
104770ecc248SWillem de Bruijn 		break;
10481da177e4SLinus Torvalds 	case IP_TOS:	/* This sets both TOS and Precedence */
10496ebf71baSChristoph Hellwig 		__ip_sock_set_tos(sk, val);
10501da177e4SLinus Torvalds 		break;
10511da177e4SLinus Torvalds 	case IP_TTL:
10521da177e4SLinus Torvalds 		if (optlen < 1)
10531da177e4SLinus Torvalds 			goto e_inval;
1054c9be4a5cSCong Wang 		if (val != -1 && (val < 1 || val > 255))
10551da177e4SLinus Torvalds 			goto e_inval;
10561da177e4SLinus Torvalds 		inet->uc_ttl = val;
10571da177e4SLinus Torvalds 		break;
10581da177e4SLinus Torvalds 	case IP_HDRINCL:
10591da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_RAW) {
10601da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
10611da177e4SLinus Torvalds 			break;
10621da177e4SLinus Torvalds 		}
10631da177e4SLinus Torvalds 		inet->hdrincl = val ? 1 : 0;
10641da177e4SLinus Torvalds 		break;
10657b2ff18eSJiri Olsa 	case IP_NODEFRAG:
10667b2ff18eSJiri Olsa 		if (sk->sk_type != SOCK_RAW) {
10677b2ff18eSJiri Olsa 			err = -ENOPROTOOPT;
10687b2ff18eSJiri Olsa 			break;
10697b2ff18eSJiri Olsa 		}
10707b2ff18eSJiri Olsa 		inet->nodefrag = val ? 1 : 0;
10717b2ff18eSJiri Olsa 		break;
107290c337daSEric Dumazet 	case IP_BIND_ADDRESS_NO_PORT:
107390c337daSEric Dumazet 		inet->bind_address_no_port = val ? 1 : 0;
107490c337daSEric Dumazet 		break;
10751da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
10761b346576SHannes Frederic Sowa 		if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
10771da177e4SLinus Torvalds 			goto e_inval;
10781da177e4SLinus Torvalds 		inet->pmtudisc = val;
10791da177e4SLinus Torvalds 		break;
10801da177e4SLinus Torvalds 	case IP_RECVERR:
10811da177e4SLinus Torvalds 		inet->recverr = !!val;
10821da177e4SLinus Torvalds 		if (!val)
10831da177e4SLinus Torvalds 			skb_queue_purge(&sk->sk_error_queue);
10841da177e4SLinus Torvalds 		break;
1085eba75c58SWillem de Bruijn 	case IP_RECVERR_RFC4884:
1086eba75c58SWillem de Bruijn 		if (val < 0 || val > 1)
1087eba75c58SWillem de Bruijn 			goto e_inval;
1088eba75c58SWillem de Bruijn 		inet->recverr_rfc4884 = !!val;
1089eba75c58SWillem de Bruijn 		break;
10901da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
10911da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
10921da177e4SLinus Torvalds 			goto e_inval;
10931da177e4SLinus Torvalds 		if (optlen < 1)
10941da177e4SLinus Torvalds 			goto e_inval;
10951da177e4SLinus Torvalds 		if (val == -1)
10961da177e4SLinus Torvalds 			val = 1;
10971da177e4SLinus Torvalds 		if (val < 0 || val > 255)
10981da177e4SLinus Torvalds 			goto e_inval;
10991da177e4SLinus Torvalds 		inet->mc_ttl = val;
11001da177e4SLinus Torvalds 		break;
11011da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
11021da177e4SLinus Torvalds 		if (optlen < 1)
11031da177e4SLinus Torvalds 			goto e_inval;
11041da177e4SLinus Torvalds 		inet->mc_loop = !!val;
11051da177e4SLinus Torvalds 		break;
110676e21053SErich E. Hoover 	case IP_UNICAST_IF:
110776e21053SErich E. Hoover 	{
110876e21053SErich E. Hoover 		struct net_device *dev = NULL;
110976e21053SErich E. Hoover 		int ifindex;
11109515a2e0SDavid Ahern 		int midx;
111176e21053SErich E. Hoover 
111276e21053SErich E. Hoover 		if (optlen != sizeof(int))
111376e21053SErich E. Hoover 			goto e_inval;
111476e21053SErich E. Hoover 
111576e21053SErich E. Hoover 		ifindex = (__force int)ntohl((__force __be32)val);
111676e21053SErich E. Hoover 		if (ifindex == 0) {
111776e21053SErich E. Hoover 			inet->uc_index = 0;
111876e21053SErich E. Hoover 			err = 0;
111976e21053SErich E. Hoover 			break;
112076e21053SErich E. Hoover 		}
112176e21053SErich E. Hoover 
112276e21053SErich E. Hoover 		dev = dev_get_by_index(sock_net(sk), ifindex);
112376e21053SErich E. Hoover 		err = -EADDRNOTAVAIL;
112476e21053SErich E. Hoover 		if (!dev)
112576e21053SErich E. Hoover 			break;
11269515a2e0SDavid Ahern 
11279515a2e0SDavid Ahern 		midx = l3mdev_master_ifindex(dev);
112876e21053SErich E. Hoover 		dev_put(dev);
112976e21053SErich E. Hoover 
113076e21053SErich E. Hoover 		err = -EINVAL;
1131fdf1923bSMiaohe Lin 		if (sk->sk_bound_dev_if && midx != sk->sk_bound_dev_if)
113276e21053SErich E. Hoover 			break;
113376e21053SErich E. Hoover 
113476e21053SErich E. Hoover 		inet->uc_index = ifindex;
113576e21053SErich E. Hoover 		err = 0;
113676e21053SErich E. Hoover 		break;
113776e21053SErich E. Hoover 	}
11381da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
11391da177e4SLinus Torvalds 	{
11401da177e4SLinus Torvalds 		struct ip_mreqn mreq;
11411da177e4SLinus Torvalds 		struct net_device *dev = NULL;
11427bb387c5SDavid Ahern 		int midx;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
11451da177e4SLinus Torvalds 			goto e_inval;
11461da177e4SLinus Torvalds 		/*
11471da177e4SLinus Torvalds 		 *	Check the arguments are allowable
11481da177e4SLinus Torvalds 		 */
11491da177e4SLinus Torvalds 
11500915921bSShan Wei 		if (optlen < sizeof(struct in_addr))
11510915921bSShan Wei 			goto e_inval;
11520915921bSShan Wei 
11531da177e4SLinus Torvalds 		err = -EFAULT;
11541da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
115589654c5fSChristoph Hellwig 			if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
11561da177e4SLinus Torvalds 				break;
11571da177e4SLinus Torvalds 		} else {
11581da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
11593a084ddbSJiri Pirko 			if (optlen >= sizeof(struct ip_mreq)) {
116089654c5fSChristoph Hellwig 				if (copy_from_sockptr(&mreq, optval,
11613a084ddbSJiri Pirko 						      sizeof(struct ip_mreq)))
11623a084ddbSJiri Pirko 					break;
11633a084ddbSJiri Pirko 			} else if (optlen >= sizeof(struct in_addr)) {
116489654c5fSChristoph Hellwig 				if (copy_from_sockptr(&mreq.imr_address, optval,
11654d52cfbeSEric Dumazet 						      sizeof(struct in_addr)))
11661da177e4SLinus Torvalds 					break;
11671da177e4SLinus Torvalds 			}
11683a084ddbSJiri Pirko 		}
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		if (!mreq.imr_ifindex) {
1171e6f1cebfSAl Viro 			if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
11721da177e4SLinus Torvalds 				inet->mc_index = 0;
11731da177e4SLinus Torvalds 				inet->mc_addr  = 0;
11741da177e4SLinus Torvalds 				err = 0;
11751da177e4SLinus Torvalds 				break;
11761da177e4SLinus Torvalds 			}
11773b1e0a65SYOSHIFUJI Hideaki 			dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
117855b80503SEric Dumazet 			if (dev)
11791da177e4SLinus Torvalds 				mreq.imr_ifindex = dev->ifindex;
11801da177e4SLinus Torvalds 		} else
118155b80503SEric Dumazet 			dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
11851da177e4SLinus Torvalds 		if (!dev)
11861da177e4SLinus Torvalds 			break;
11877bb387c5SDavid Ahern 
11887bb387c5SDavid Ahern 		midx = l3mdev_master_ifindex(dev);
11897bb387c5SDavid Ahern 
119055b80503SEric Dumazet 		dev_put(dev);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 		err = -EINVAL;
11931da177e4SLinus Torvalds 		if (sk->sk_bound_dev_if &&
11947bb387c5SDavid Ahern 		    mreq.imr_ifindex != sk->sk_bound_dev_if &&
1195fdf1923bSMiaohe Lin 		    midx != sk->sk_bound_dev_if)
11961da177e4SLinus Torvalds 			break;
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 		inet->mc_index = mreq.imr_ifindex;
11991da177e4SLinus Torvalds 		inet->mc_addr  = mreq.imr_address.s_addr;
12001da177e4SLinus Torvalds 		err = 0;
12011da177e4SLinus Torvalds 		break;
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	case IP_ADD_MEMBERSHIP:
12051da177e4SLinus Torvalds 	case IP_DROP_MEMBERSHIP:
12061da177e4SLinus Torvalds 	{
12071da177e4SLinus Torvalds 		struct ip_mreqn mreq;
12081da177e4SLinus Torvalds 
1209a96fb49bSFlavio Leitner 		err = -EPROTO;
1210a96fb49bSFlavio Leitner 		if (inet_sk(sk)->is_icsk)
1211a96fb49bSFlavio Leitner 			break;
1212a96fb49bSFlavio Leitner 
12131da177e4SLinus Torvalds 		if (optlen < sizeof(struct ip_mreq))
12141da177e4SLinus Torvalds 			goto e_inval;
12151da177e4SLinus Torvalds 		err = -EFAULT;
12161da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
121789654c5fSChristoph Hellwig 			if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
12181da177e4SLinus Torvalds 				break;
12191da177e4SLinus Torvalds 		} else {
12201da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
122189654c5fSChristoph Hellwig 			if (copy_from_sockptr(&mreq, optval,
122289654c5fSChristoph Hellwig 					      sizeof(struct ip_mreq)))
12231da177e4SLinus Torvalds 				break;
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 		if (optname == IP_ADD_MEMBERSHIP)
122754ff9ef3SMarcelo Ricardo Leitner 			err = ip_mc_join_group(sk, &mreq);
12281da177e4SLinus Torvalds 		else
122954ff9ef3SMarcelo Ricardo Leitner 			err = ip_mc_leave_group(sk, &mreq);
12301da177e4SLinus Torvalds 		break;
12311da177e4SLinus Torvalds 	}
12321da177e4SLinus Torvalds 	case IP_MSFILTER:
12331da177e4SLinus Torvalds 	{
12341da177e4SLinus Torvalds 		struct ip_msfilter *msf;
12351da177e4SLinus Torvalds 
12364167a960SGustavo A. R. Silva 		if (optlen < IP_MSFILTER_SIZE(0))
12371da177e4SLinus Torvalds 			goto e_inval;
12387de6d09fSKuniyuki Iwashima 		if (optlen > READ_ONCE(sysctl_optmem_max)) {
12391da177e4SLinus Torvalds 			err = -ENOBUFS;
12401da177e4SLinus Torvalds 			break;
12411da177e4SLinus Torvalds 		}
124289654c5fSChristoph Hellwig 		msf = memdup_sockptr(optval, optlen);
1243a2c841d9SAl Viro 		if (IS_ERR(msf)) {
1244a2c841d9SAl Viro 			err = PTR_ERR(msf);
12451da177e4SLinus Torvalds 			break;
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 		/* numsrc >= (1G-4) overflow in 32 bits */
12481da177e4SLinus Torvalds 		if (msf->imsf_numsrc >= 0x3ffffffcU ||
12496ae0f2e5SKuniyuki Iwashima 		    msf->imsf_numsrc > READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) {
12501da177e4SLinus Torvalds 			kfree(msf);
12511da177e4SLinus Torvalds 			err = -ENOBUFS;
12521da177e4SLinus Torvalds 			break;
12531da177e4SLinus Torvalds 		}
12544167a960SGustavo A. R. Silva 		if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
12551da177e4SLinus Torvalds 			kfree(msf);
12561da177e4SLinus Torvalds 			err = -EINVAL;
12571da177e4SLinus Torvalds 			break;
12581da177e4SLinus Torvalds 		}
12591da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, 0);
12601da177e4SLinus Torvalds 		kfree(msf);
12611da177e4SLinus Torvalds 		break;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 	case IP_BLOCK_SOURCE:
12641da177e4SLinus Torvalds 	case IP_UNBLOCK_SOURCE:
12651da177e4SLinus Torvalds 	case IP_ADD_SOURCE_MEMBERSHIP:
12661da177e4SLinus Torvalds 	case IP_DROP_SOURCE_MEMBERSHIP:
12671da177e4SLinus Torvalds 	{
12681da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
12691da177e4SLinus Torvalds 		int omode, add;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 		if (optlen != sizeof(struct ip_mreq_source))
12721da177e4SLinus Torvalds 			goto e_inval;
127389654c5fSChristoph Hellwig 		if (copy_from_sockptr(&mreqs, optval, sizeof(mreqs))) {
12741da177e4SLinus Torvalds 			err = -EFAULT;
12751da177e4SLinus Torvalds 			break;
12761da177e4SLinus Torvalds 		}
12771da177e4SLinus Torvalds 		if (optname == IP_BLOCK_SOURCE) {
12781da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
12791da177e4SLinus Torvalds 			add = 1;
12801da177e4SLinus Torvalds 		} else if (optname == IP_UNBLOCK_SOURCE) {
12811da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
12821da177e4SLinus Torvalds 			add = 0;
12831da177e4SLinus Torvalds 		} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
12841da177e4SLinus Torvalds 			struct ip_mreqn mreq;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 			mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
12871da177e4SLinus Torvalds 			mreq.imr_address.s_addr = mreqs.imr_interface;
12881da177e4SLinus Torvalds 			mreq.imr_ifindex = 0;
12896e2059b5SHangbin Liu 			err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
12908cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
12911da177e4SLinus Torvalds 				break;
12921da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
12931da177e4SLinus Torvalds 			add = 1;
12941da177e4SLinus Torvalds 		} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
12951da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
12961da177e4SLinus Torvalds 			add = 0;
12971da177e4SLinus Torvalds 		}
12981da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs, 0);
12991da177e4SLinus Torvalds 		break;
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 	case MCAST_JOIN_GROUP:
13021da177e4SLinus Torvalds 	case MCAST_LEAVE_GROUP:
1303b6238c04SChristoph Hellwig 		if (in_compat_syscall())
1304b6238c04SChristoph Hellwig 			err = compat_ip_mcast_join_leave(sk, optname, optval,
1305b6238c04SChristoph Hellwig 							 optlen);
1306b6238c04SChristoph Hellwig 		else
130702caad7cSChristoph Hellwig 			err = ip_mcast_join_leave(sk, optname, optval, optlen);
13081da177e4SLinus Torvalds 		break;
13091da177e4SLinus Torvalds 	case MCAST_JOIN_SOURCE_GROUP:
13101da177e4SLinus Torvalds 	case MCAST_LEAVE_SOURCE_GROUP:
13111da177e4SLinus Torvalds 	case MCAST_BLOCK_SOURCE:
13121da177e4SLinus Torvalds 	case MCAST_UNBLOCK_SOURCE:
1313b6238c04SChristoph Hellwig 		err = do_mcast_group_source(sk, optname, optval, optlen);
13141da177e4SLinus Torvalds 		break;
13151da177e4SLinus Torvalds 	case MCAST_MSFILTER:
1316b6238c04SChristoph Hellwig 		if (in_compat_syscall())
1317b6238c04SChristoph Hellwig 			err = compat_ip_set_mcast_msfilter(sk, optval, optlen);
1318b6238c04SChristoph Hellwig 		else
1319d62c38f6SChristoph Hellwig 			err = ip_set_mcast_msfilter(sk, optval, optlen);
13201da177e4SLinus Torvalds 		break;
1321f771bef9SNivedita Singhvi 	case IP_MULTICAST_ALL:
1322f771bef9SNivedita Singhvi 		if (optlen < 1)
1323f771bef9SNivedita Singhvi 			goto e_inval;
1324f771bef9SNivedita Singhvi 		if (val != 0 && val != 1)
1325f771bef9SNivedita Singhvi 			goto e_inval;
1326f771bef9SNivedita Singhvi 		inet->mc_all = val;
1327f771bef9SNivedita Singhvi 		break;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	case IP_FREEBIND:
13301da177e4SLinus Torvalds 		if (optlen < 1)
13311da177e4SLinus Torvalds 			goto e_inval;
13321da177e4SLinus Torvalds 		inet->freebind = !!val;
13331da177e4SLinus Torvalds 		break;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	case IP_IPSEC_POLICY:
13361da177e4SLinus Torvalds 	case IP_XFRM_POLICY:
13376fc0b4a7SHerbert Xu 		err = -EPERM;
13381df055d3SMartin KaFai Lau 		if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
13396fc0b4a7SHerbert Xu 			break;
134089654c5fSChristoph Hellwig 		err = xfrm_user_policy(sk, optname, optval, optlen);
13411da177e4SLinus Torvalds 		break;
13421da177e4SLinus Torvalds 
1343f5715aeaSKOVACS Krisztian 	case IP_TRANSPARENT:
13441df055d3SMartin KaFai Lau 		if (!!val && !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
13451df055d3SMartin KaFai Lau 		    !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1346f5715aeaSKOVACS Krisztian 			err = -EPERM;
1347f5715aeaSKOVACS Krisztian 			break;
1348f5715aeaSKOVACS Krisztian 		}
1349f5715aeaSKOVACS Krisztian 		if (optlen < 1)
1350f5715aeaSKOVACS Krisztian 			goto e_inval;
1351f5715aeaSKOVACS Krisztian 		inet->transparent = !!val;
1352f5715aeaSKOVACS Krisztian 		break;
1353f5715aeaSKOVACS Krisztian 
1354d218d111SStephen Hemminger 	case IP_MINTTL:
1355d218d111SStephen Hemminger 		if (optlen < 1)
1356d218d111SStephen Hemminger 			goto e_inval;
1357d218d111SStephen Hemminger 		if (val < 0 || val > 255)
1358d218d111SStephen Hemminger 			goto e_inval;
1359020e71a3SEric Dumazet 
1360020e71a3SEric Dumazet 		if (val)
1361020e71a3SEric Dumazet 			static_branch_enable(&ip4_min_ttl);
1362020e71a3SEric Dumazet 
136314834c4fSEric Dumazet 		/* tcp_v4_err() and tcp_v4_rcv() might read min_ttl
136414834c4fSEric Dumazet 		 * while we are changint it.
136514834c4fSEric Dumazet 		 */
136614834c4fSEric Dumazet 		WRITE_ONCE(inet->min_ttl, val);
1367d218d111SStephen Hemminger 		break;
1368d218d111SStephen Hemminger 
1369*91d0b78cSJakub Sitnicki 	case IP_LOCAL_PORT_RANGE:
1370*91d0b78cSJakub Sitnicki 	{
1371*91d0b78cSJakub Sitnicki 		const __u16 lo = val;
1372*91d0b78cSJakub Sitnicki 		const __u16 hi = val >> 16;
1373*91d0b78cSJakub Sitnicki 
1374*91d0b78cSJakub Sitnicki 		if (optlen != sizeof(__u32))
1375*91d0b78cSJakub Sitnicki 			goto e_inval;
1376*91d0b78cSJakub Sitnicki 		if (lo != 0 && hi != 0 && lo > hi)
1377*91d0b78cSJakub Sitnicki 			goto e_inval;
1378*91d0b78cSJakub Sitnicki 
1379*91d0b78cSJakub Sitnicki 		inet->local_port_range.lo = lo;
1380*91d0b78cSJakub Sitnicki 		inet->local_port_range.hi = hi;
1381*91d0b78cSJakub Sitnicki 		break;
1382*91d0b78cSJakub Sitnicki 	}
13831da177e4SLinus Torvalds 	default:
13841da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
13851da177e4SLinus Torvalds 		break;
13861da177e4SLinus Torvalds 	}
13871df055d3SMartin KaFai Lau 	sockopt_release_sock(sk);
1388baf606d9SMarcelo Ricardo Leitner 	if (needs_rtnl)
1389baf606d9SMarcelo Ricardo Leitner 		rtnl_unlock();
13901da177e4SLinus Torvalds 	return err;
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds e_inval:
13931df055d3SMartin KaFai Lau 	sockopt_release_sock(sk);
1394baf606d9SMarcelo Ricardo Leitner 	if (needs_rtnl)
1395baf606d9SMarcelo Ricardo Leitner 		rtnl_unlock();
13961da177e4SLinus Torvalds 	return -EINVAL;
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
1399f84af32cSEric Dumazet /**
1400829ae9d6SWillem de Bruijn  * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1401f84af32cSEric Dumazet  * @sk: socket
1402f84af32cSEric Dumazet  * @skb: buffer
1403f84af32cSEric Dumazet  *
140435ebf65eSDavid S. Miller  * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
140535ebf65eSDavid S. Miller  * destination in skb->cb[] before dst drop.
14068e3bff96Sstephen hemminger  * This way, receiver doesn't make cache line misses to read rtable.
1407f84af32cSEric Dumazet  */
1408fbf8866dSShawn Bohrer void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
1409f84af32cSEric Dumazet {
1410d826eb14SEric Dumazet 	struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
14114b261c75SHannes Frederic Sowa 	bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
14124b261c75SHannes Frederic Sowa 		       ipv6_sk_rxinfo(sk);
1413d826eb14SEric Dumazet 
14144b261c75SHannes Frederic Sowa 	if (prepare && skb_rtable(skb)) {
14150b922b7aSDavid Ahern 		/* skb->cb is overloaded: prior to this point it is IP{6}CB
14160b922b7aSDavid Ahern 		 * which has interface index (iif) as the first member of the
14170b922b7aSDavid Ahern 		 * underlying inet{6}_skb_parm struct. This code then overlays
14180b922b7aSDavid Ahern 		 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1419f0c16ba8SWei Zhang 		 * element so the iif is picked up from the prior IPCB. If iif
1420f0c16ba8SWei Zhang 		 * is the loopback interface, then return the sending interface
1421f0c16ba8SWei Zhang 		 * (e.g., process binds socket to eth0 for Tx which is
1422f0c16ba8SWei Zhang 		 * redirected to loopback in the rtable/dst).
14230b922b7aSDavid Ahern 		 */
1424cbea8f02SDavid Ahern 		struct rtable *rt = skb_rtable(skb);
1425cbea8f02SDavid Ahern 		bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1426cbea8f02SDavid Ahern 
1427cbea8f02SDavid Ahern 		if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
1428f0c16ba8SWei Zhang 			pktinfo->ipi_ifindex = inet_iif(skb);
1429cbea8f02SDavid Ahern 		else if (l3slave && rt && rt->rt_iif)
1430cbea8f02SDavid Ahern 			pktinfo->ipi_ifindex = rt->rt_iif;
1431f0c16ba8SWei Zhang 
143235ebf65eSDavid S. Miller 		pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1433d826eb14SEric Dumazet 	} else {
1434d826eb14SEric Dumazet 		pktinfo->ipi_ifindex = 0;
1435d826eb14SEric Dumazet 		pktinfo->ipi_spec_dst.s_addr = 0;
1436f84af32cSEric Dumazet 	}
1437d826eb14SEric Dumazet 	skb_dst_drop(skb);
1438d826eb14SEric Dumazet }
1439f84af32cSEric Dumazet 
1440a7b75c5aSChristoph Hellwig int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1441a7b75c5aSChristoph Hellwig 		unsigned int optlen)
14423fdadf7dSDmitry Mishin {
14433fdadf7dSDmitry Mishin 	int err;
14443fdadf7dSDmitry Mishin 
14453fdadf7dSDmitry Mishin 	if (level != SOL_IP)
14463fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
14473fdadf7dSDmitry Mishin 
1448a7b75c5aSChristoph Hellwig 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
144997adaddaSTaehee Yoo #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1450d2ba09c1SAlexei Starovoitov 	if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1451d2ba09c1SAlexei Starovoitov 	    optname < BPFILTER_IPT_SET_MAX)
1452a7b75c5aSChristoph Hellwig 		err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
1453d2ba09c1SAlexei Starovoitov #endif
14543fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
14553fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
14563fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
14576a9fb947SPavel Emelyanov 			optname != IP_IPSEC_POLICY &&
14586a9fb947SPavel Emelyanov 			optname != IP_XFRM_POLICY &&
14593f34cfaeSPaolo Abeni 			!ip_mroute_opt(optname))
1460a7b75c5aSChristoph Hellwig 		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
14613fdadf7dSDmitry Mishin #endif
14623fdadf7dSDmitry Mishin 	return err;
14633fdadf7dSDmitry Mishin }
14644d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_setsockopt);
14653fdadf7dSDmitry Mishin 
14661da177e4SLinus Torvalds /*
14674d52cfbeSEric Dumazet  *	Get the options. Note for future reference. The GET of IP options gets
14684d52cfbeSEric Dumazet  *	the _received_ ones. The set sets the _sent_ ones.
14691da177e4SLinus Torvalds  */
14701da177e4SLinus Torvalds 
147187e9f031SWANG Cong static bool getsockopt_needs_rtnl(int optname)
147287e9f031SWANG Cong {
147387e9f031SWANG Cong 	switch (optname) {
147487e9f031SWANG Cong 	case IP_MSFILTER:
147587e9f031SWANG Cong 	case MCAST_MSFILTER:
147687e9f031SWANG Cong 		return true;
147787e9f031SWANG Cong 	}
147887e9f031SWANG Cong 	return false;
147987e9f031SWANG Cong }
148087e9f031SWANG Cong 
1481728f064cSMartin KaFai Lau static int ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval,
1482728f064cSMartin KaFai Lau 				 sockptr_t optlen, int len)
148349e74c24SChristoph Hellwig {
1484db243b79SGustavo A. R. Silva 	const int size0 = offsetof(struct group_filter, gf_slist_flex);
148549e74c24SChristoph Hellwig 	struct group_filter gsf;
1486728f064cSMartin KaFai Lau 	int num, gsf_size;
148749e74c24SChristoph Hellwig 	int err;
148849e74c24SChristoph Hellwig 
148949e74c24SChristoph Hellwig 	if (len < size0)
149049e74c24SChristoph Hellwig 		return -EINVAL;
1491728f064cSMartin KaFai Lau 	if (copy_from_sockptr(&gsf, optval, size0))
149249e74c24SChristoph Hellwig 		return -EFAULT;
149349e74c24SChristoph Hellwig 
149449e74c24SChristoph Hellwig 	num = gsf.gf_numsrc;
1495728f064cSMartin KaFai Lau 	err = ip_mc_gsfget(sk, &gsf, optval,
1496728f064cSMartin KaFai Lau 			   offsetof(struct group_filter, gf_slist_flex));
149749e74c24SChristoph Hellwig 	if (err)
149849e74c24SChristoph Hellwig 		return err;
149949e74c24SChristoph Hellwig 	if (gsf.gf_numsrc < num)
150049e74c24SChristoph Hellwig 		num = gsf.gf_numsrc;
1501728f064cSMartin KaFai Lau 	gsf_size = GROUP_FILTER_SIZE(num);
1502728f064cSMartin KaFai Lau 	if (copy_to_sockptr(optlen, &gsf_size, sizeof(int)) ||
1503728f064cSMartin KaFai Lau 	    copy_to_sockptr(optval, &gsf, size0))
150449e74c24SChristoph Hellwig 		return -EFAULT;
150549e74c24SChristoph Hellwig 	return 0;
150649e74c24SChristoph Hellwig }
150749e74c24SChristoph Hellwig 
1508728f064cSMartin KaFai Lau static int compat_ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval,
1509728f064cSMartin KaFai Lau 					sockptr_t optlen, int len)
151049e74c24SChristoph Hellwig {
1511db243b79SGustavo A. R. Silva 	const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
151249e74c24SChristoph Hellwig 	struct compat_group_filter gf32;
151349e74c24SChristoph Hellwig 	struct group_filter gf;
151449e74c24SChristoph Hellwig 	int num;
1515b6238c04SChristoph Hellwig 	int err;
151649e74c24SChristoph Hellwig 
151749e74c24SChristoph Hellwig 	if (len < size0)
151849e74c24SChristoph Hellwig 		return -EINVAL;
1519728f064cSMartin KaFai Lau 	if (copy_from_sockptr(&gf32, optval, size0))
152049e74c24SChristoph Hellwig 		return -EFAULT;
152149e74c24SChristoph Hellwig 
152249e74c24SChristoph Hellwig 	gf.gf_interface = gf32.gf_interface;
152349e74c24SChristoph Hellwig 	gf.gf_fmode = gf32.gf_fmode;
152449e74c24SChristoph Hellwig 	num = gf.gf_numsrc = gf32.gf_numsrc;
152549e74c24SChristoph Hellwig 	gf.gf_group = gf32.gf_group;
152649e74c24SChristoph Hellwig 
1527728f064cSMartin KaFai Lau 	err = ip_mc_gsfget(sk, &gf, optval,
1528728f064cSMartin KaFai Lau 			   offsetof(struct compat_group_filter, gf_slist_flex));
152949e74c24SChristoph Hellwig 	if (err)
153049e74c24SChristoph Hellwig 		return err;
153149e74c24SChristoph Hellwig 	if (gf.gf_numsrc < num)
153249e74c24SChristoph Hellwig 		num = gf.gf_numsrc;
153349e74c24SChristoph Hellwig 	len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32));
1534728f064cSMartin KaFai Lau 	if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1535728f064cSMartin KaFai Lau 	    copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode),
1536728f064cSMartin KaFai Lau 				   &gf.gf_fmode, sizeof(gf.gf_fmode)) ||
1537728f064cSMartin KaFai Lau 	    copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc),
1538728f064cSMartin KaFai Lau 				   &gf.gf_numsrc, sizeof(gf.gf_numsrc)))
153949e74c24SChristoph Hellwig 		return -EFAULT;
154049e74c24SChristoph Hellwig 	return 0;
154149e74c24SChristoph Hellwig }
154249e74c24SChristoph Hellwig 
1543fd969f25SMartin KaFai Lau int do_ip_getsockopt(struct sock *sk, int level, int optname,
1544728f064cSMartin KaFai Lau 		     sockptr_t optval, sockptr_t optlen)
15451da177e4SLinus Torvalds {
15461da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
154787e9f031SWANG Cong 	bool needs_rtnl = getsockopt_needs_rtnl(optname);
154887e9f031SWANG Cong 	int val, err = 0;
15491da177e4SLinus Torvalds 	int len;
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 	if (level != SOL_IP)
15521da177e4SLinus Torvalds 		return -EOPNOTSUPP;
15531da177e4SLinus Torvalds 
15546a9fb947SPavel Emelyanov 	if (ip_mroute_opt(optname))
15551da177e4SLinus Torvalds 		return ip_mroute_getsockopt(sk, optname, optval, optlen);
15561da177e4SLinus Torvalds 
1557728f064cSMartin KaFai Lau 	if (copy_from_sockptr(&len, optlen, sizeof(int)))
15581da177e4SLinus Torvalds 		return -EFAULT;
15591da177e4SLinus Torvalds 	if (len < 0)
15601da177e4SLinus Torvalds 		return -EINVAL;
15611da177e4SLinus Torvalds 
156287e9f031SWANG Cong 	if (needs_rtnl)
156387e9f031SWANG Cong 		rtnl_lock();
15641985320cSMartin KaFai Lau 	sockopt_lock_sock(sk);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	switch (optname) {
15671da177e4SLinus Torvalds 	case IP_OPTIONS:
15681da177e4SLinus Torvalds 	{
15691da177e4SLinus Torvalds 		unsigned char optbuf[sizeof(struct ip_options)+40];
15701da177e4SLinus Torvalds 		struct ip_options *opt = (struct ip_options *)optbuf;
1571f6d8bd05SEric Dumazet 		struct ip_options_rcu *inet_opt;
1572f6d8bd05SEric Dumazet 
1573f6d8bd05SEric Dumazet 		inet_opt = rcu_dereference_protected(inet->inet_opt,
15741e1d04e6SHannes Frederic Sowa 						     lockdep_sock_is_held(sk));
15751da177e4SLinus Torvalds 		opt->optlen = 0;
1576f6d8bd05SEric Dumazet 		if (inet_opt)
1577f6d8bd05SEric Dumazet 			memcpy(optbuf, &inet_opt->opt,
15781da177e4SLinus Torvalds 			       sizeof(struct ip_options) +
1579f6d8bd05SEric Dumazet 			       inet_opt->opt.optlen);
15801985320cSMartin KaFai Lau 		sockopt_release_sock(sk);
15811da177e4SLinus Torvalds 
1582728f064cSMartin KaFai Lau 		if (opt->optlen == 0) {
1583728f064cSMartin KaFai Lau 			len = 0;
1584728f064cSMartin KaFai Lau 			return copy_to_sockptr(optlen, &len, sizeof(int));
1585728f064cSMartin KaFai Lau 		}
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 		ip_options_undo(opt);
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds 		len = min_t(unsigned int, len, opt->optlen);
1590728f064cSMartin KaFai Lau 		if (copy_to_sockptr(optlen, &len, sizeof(int)))
15911da177e4SLinus Torvalds 			return -EFAULT;
1592728f064cSMartin KaFai Lau 		if (copy_to_sockptr(optval, opt->__data, len))
15931da177e4SLinus Torvalds 			return -EFAULT;
15941da177e4SLinus Torvalds 		return 0;
15951da177e4SLinus Torvalds 	}
15961da177e4SLinus Torvalds 	case IP_PKTINFO:
15971da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
15981da177e4SLinus Torvalds 		break;
15991da177e4SLinus Torvalds 	case IP_RECVTTL:
16001da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
16011da177e4SLinus Torvalds 		break;
16021da177e4SLinus Torvalds 	case IP_RECVTOS:
16031da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
16041da177e4SLinus Torvalds 		break;
16051da177e4SLinus Torvalds 	case IP_RECVOPTS:
16061da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
16071da177e4SLinus Torvalds 		break;
16081da177e4SLinus Torvalds 	case IP_RETOPTS:
16091da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
16101da177e4SLinus Torvalds 		break;
16112c7946a7SCatherine Zhang 	case IP_PASSSEC:
16122c7946a7SCatherine Zhang 		val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
16132c7946a7SCatherine Zhang 		break;
1614e8b2dfe9SBalazs Scheidler 	case IP_RECVORIGDSTADDR:
1615e8b2dfe9SBalazs Scheidler 		val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1616e8b2dfe9SBalazs Scheidler 		break;
1617ad6f939aSTom Herbert 	case IP_CHECKSUM:
1618ad6f939aSTom Herbert 		val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1619ad6f939aSTom Herbert 		break;
162070ecc248SWillem de Bruijn 	case IP_RECVFRAGSIZE:
162170ecc248SWillem de Bruijn 		val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
162270ecc248SWillem de Bruijn 		break;
16231da177e4SLinus Torvalds 	case IP_TOS:
16241da177e4SLinus Torvalds 		val = inet->tos;
16251da177e4SLinus Torvalds 		break;
16261da177e4SLinus Torvalds 	case IP_TTL:
1627fa50d974SNikolay Borisov 	{
1628fa50d974SNikolay Borisov 		struct net *net = sock_net(sk);
16291da177e4SLinus Torvalds 		val = (inet->uc_ttl == -1 ?
16308281b7ecSKuniyuki Iwashima 		       READ_ONCE(net->ipv4.sysctl_ip_default_ttl) :
16311da177e4SLinus Torvalds 		       inet->uc_ttl);
16321da177e4SLinus Torvalds 		break;
1633fa50d974SNikolay Borisov 	}
16341da177e4SLinus Torvalds 	case IP_HDRINCL:
16351da177e4SLinus Torvalds 		val = inet->hdrincl;
16361da177e4SLinus Torvalds 		break;
1637a89b4763SMichael Kerrisk 	case IP_NODEFRAG:
1638a89b4763SMichael Kerrisk 		val = inet->nodefrag;
1639a89b4763SMichael Kerrisk 		break;
164090c337daSEric Dumazet 	case IP_BIND_ADDRESS_NO_PORT:
164190c337daSEric Dumazet 		val = inet->bind_address_no_port;
164290c337daSEric Dumazet 		break;
16431da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
16441da177e4SLinus Torvalds 		val = inet->pmtudisc;
16451da177e4SLinus Torvalds 		break;
16461da177e4SLinus Torvalds 	case IP_MTU:
16471da177e4SLinus Torvalds 	{
16481da177e4SLinus Torvalds 		struct dst_entry *dst;
16491da177e4SLinus Torvalds 		val = 0;
16501da177e4SLinus Torvalds 		dst = sk_dst_get(sk);
16511da177e4SLinus Torvalds 		if (dst) {
16521da177e4SLinus Torvalds 			val = dst_mtu(dst);
16531da177e4SLinus Torvalds 			dst_release(dst);
16541da177e4SLinus Torvalds 		}
16551da177e4SLinus Torvalds 		if (!val) {
16561985320cSMartin KaFai Lau 			sockopt_release_sock(sk);
16571da177e4SLinus Torvalds 			return -ENOTCONN;
16581da177e4SLinus Torvalds 		}
16591da177e4SLinus Torvalds 		break;
16601da177e4SLinus Torvalds 	}
16611da177e4SLinus Torvalds 	case IP_RECVERR:
16621da177e4SLinus Torvalds 		val = inet->recverr;
16631da177e4SLinus Torvalds 		break;
1664eba75c58SWillem de Bruijn 	case IP_RECVERR_RFC4884:
1665eba75c58SWillem de Bruijn 		val = inet->recverr_rfc4884;
1666eba75c58SWillem de Bruijn 		break;
16671da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
16681da177e4SLinus Torvalds 		val = inet->mc_ttl;
16691da177e4SLinus Torvalds 		break;
16701da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
16711da177e4SLinus Torvalds 		val = inet->mc_loop;
16721da177e4SLinus Torvalds 		break;
167376e21053SErich E. Hoover 	case IP_UNICAST_IF:
167476e21053SErich E. Hoover 		val = (__force int)htonl((__u32) inet->uc_index);
167576e21053SErich E. Hoover 		break;
16761da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
16771da177e4SLinus Torvalds 	{
16781da177e4SLinus Torvalds 		struct in_addr addr;
16791da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(struct in_addr));
16801da177e4SLinus Torvalds 		addr.s_addr = inet->mc_addr;
16811985320cSMartin KaFai Lau 		sockopt_release_sock(sk);
16821da177e4SLinus Torvalds 
1683728f064cSMartin KaFai Lau 		if (copy_to_sockptr(optlen, &len, sizeof(int)))
16841da177e4SLinus Torvalds 			return -EFAULT;
1685728f064cSMartin KaFai Lau 		if (copy_to_sockptr(optval, &addr, len))
16861da177e4SLinus Torvalds 			return -EFAULT;
16871da177e4SLinus Torvalds 		return 0;
16881da177e4SLinus Torvalds 	}
16891da177e4SLinus Torvalds 	case IP_MSFILTER:
16901da177e4SLinus Torvalds 	{
16911da177e4SLinus Torvalds 		struct ip_msfilter msf;
16921da177e4SLinus Torvalds 
16934167a960SGustavo A. R. Silva 		if (len < IP_MSFILTER_SIZE(0)) {
169487e9f031SWANG Cong 			err = -EINVAL;
169587e9f031SWANG Cong 			goto out;
16961da177e4SLinus Torvalds 		}
1697728f064cSMartin KaFai Lau 		if (copy_from_sockptr(&msf, optval, IP_MSFILTER_SIZE(0))) {
169887e9f031SWANG Cong 			err = -EFAULT;
169987e9f031SWANG Cong 			goto out;
17001da177e4SLinus Torvalds 		}
1701728f064cSMartin KaFai Lau 		err = ip_mc_msfget(sk, &msf, optval, optlen);
170287e9f031SWANG Cong 		goto out;
17031da177e4SLinus Torvalds 	}
17041da177e4SLinus Torvalds 	case MCAST_MSFILTER:
1705b6238c04SChristoph Hellwig 		if (in_compat_syscall())
1706b6238c04SChristoph Hellwig 			err = compat_ip_get_mcast_msfilter(sk, optval, optlen,
1707b6238c04SChristoph Hellwig 							   len);
1708b6238c04SChristoph Hellwig 		else
170949e74c24SChristoph Hellwig 			err = ip_get_mcast_msfilter(sk, optval, optlen, len);
171087e9f031SWANG Cong 		goto out;
1711f771bef9SNivedita Singhvi 	case IP_MULTICAST_ALL:
1712f771bef9SNivedita Singhvi 		val = inet->mc_all;
1713f771bef9SNivedita Singhvi 		break;
17141da177e4SLinus Torvalds 	case IP_PKTOPTIONS:
17151da177e4SLinus Torvalds 	{
17161da177e4SLinus Torvalds 		struct msghdr msg;
17171da177e4SLinus Torvalds 
17181985320cSMartin KaFai Lau 		sockopt_release_sock(sk);
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_STREAM)
17211da177e4SLinus Torvalds 			return -ENOPROTOOPT;
17221da177e4SLinus Torvalds 
1723728f064cSMartin KaFai Lau 		if (optval.is_kernel) {
1724728f064cSMartin KaFai Lau 			msg.msg_control_is_user = false;
1725728f064cSMartin KaFai Lau 			msg.msg_control = optval.kernel;
1726728f064cSMartin KaFai Lau 		} else {
17271f466e1fSChristoph Hellwig 			msg.msg_control_is_user = true;
1728728f064cSMartin KaFai Lau 			msg.msg_control_user = optval.user;
1729728f064cSMartin KaFai Lau 		}
17301da177e4SLinus Torvalds 		msg.msg_controllen = len;
1731b6238c04SChristoph Hellwig 		msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
17341da177e4SLinus Torvalds 			struct in_pktinfo info;
17351da177e4SLinus Torvalds 
1736c720c7e8SEric Dumazet 			info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1737c720c7e8SEric Dumazet 			info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
17381da177e4SLinus Torvalds 			info.ipi_ifindex = inet->mc_index;
17391da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
17401da177e4SLinus Torvalds 		}
17411da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_TTL) {
17421da177e4SLinus Torvalds 			int hlim = inet->mc_ttl;
17431da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
17441da177e4SLinus Torvalds 		}
17454c507d28SJiri Benc 		if (inet->cmsg_flags & IP_CMSG_TOS) {
17464c507d28SJiri Benc 			int tos = inet->rcv_tos;
17474c507d28SJiri Benc 			put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
17484c507d28SJiri Benc 		}
17491da177e4SLinus Torvalds 		len -= msg.msg_controllen;
1750728f064cSMartin KaFai Lau 		return copy_to_sockptr(optlen, &len, sizeof(int));
17511da177e4SLinus Torvalds 	}
17521da177e4SLinus Torvalds 	case IP_FREEBIND:
17531da177e4SLinus Torvalds 		val = inet->freebind;
17541da177e4SLinus Torvalds 		break;
1755f5715aeaSKOVACS Krisztian 	case IP_TRANSPARENT:
1756f5715aeaSKOVACS Krisztian 		val = inet->transparent;
1757f5715aeaSKOVACS Krisztian 		break;
1758d218d111SStephen Hemminger 	case IP_MINTTL:
1759d218d111SStephen Hemminger 		val = inet->min_ttl;
1760d218d111SStephen Hemminger 		break;
1761*91d0b78cSJakub Sitnicki 	case IP_LOCAL_PORT_RANGE:
1762*91d0b78cSJakub Sitnicki 		val = inet->local_port_range.hi << 16 | inet->local_port_range.lo;
1763*91d0b78cSJakub Sitnicki 		break;
17641da177e4SLinus Torvalds 	default:
17651985320cSMartin KaFai Lau 		sockopt_release_sock(sk);
17661da177e4SLinus Torvalds 		return -ENOPROTOOPT;
17671da177e4SLinus Torvalds 	}
17681985320cSMartin KaFai Lau 	sockopt_release_sock(sk);
17691da177e4SLinus Torvalds 
1770951e07c9SDavid S. Miller 	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
17711da177e4SLinus Torvalds 		unsigned char ucval = (unsigned char)val;
17721da177e4SLinus Torvalds 		len = 1;
1773728f064cSMartin KaFai Lau 		if (copy_to_sockptr(optlen, &len, sizeof(int)))
17741da177e4SLinus Torvalds 			return -EFAULT;
1775728f064cSMartin KaFai Lau 		if (copy_to_sockptr(optval, &ucval, 1))
17761da177e4SLinus Torvalds 			return -EFAULT;
17771da177e4SLinus Torvalds 	} else {
17781da177e4SLinus Torvalds 		len = min_t(unsigned int, sizeof(int), len);
1779728f064cSMartin KaFai Lau 		if (copy_to_sockptr(optlen, &len, sizeof(int)))
17801da177e4SLinus Torvalds 			return -EFAULT;
1781728f064cSMartin KaFai Lau 		if (copy_to_sockptr(optval, &val, len))
17821da177e4SLinus Torvalds 			return -EFAULT;
17831da177e4SLinus Torvalds 	}
17841da177e4SLinus Torvalds 	return 0;
178587e9f031SWANG Cong 
178687e9f031SWANG Cong out:
17871985320cSMartin KaFai Lau 	sockopt_release_sock(sk);
178887e9f031SWANG Cong 	if (needs_rtnl)
178987e9f031SWANG Cong 		rtnl_unlock();
179087e9f031SWANG Cong 	return err;
17911da177e4SLinus Torvalds }
17921da177e4SLinus Torvalds 
17933fdadf7dSDmitry Mishin int ip_getsockopt(struct sock *sk, int level,
17943fdadf7dSDmitry Mishin 		  int optname, char __user *optval, int __user *optlen)
17953fdadf7dSDmitry Mishin {
17963fdadf7dSDmitry Mishin 	int err;
17973fdadf7dSDmitry Mishin 
1798728f064cSMartin KaFai Lau 	err = do_ip_getsockopt(sk, level, optname,
1799728f064cSMartin KaFai Lau 			       USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
1800b6238c04SChristoph Hellwig 
180197adaddaSTaehee Yoo #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1802d2ba09c1SAlexei Starovoitov 	if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1803d2ba09c1SAlexei Starovoitov 	    optname < BPFILTER_IPT_GET_MAX)
1804d2ba09c1SAlexei Starovoitov 		err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1805d2ba09c1SAlexei Starovoitov #endif
18063fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
18073fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
18086a9fb947SPavel Emelyanov 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
18096a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
18103fdadf7dSDmitry Mishin 		int len;
18113fdadf7dSDmitry Mishin 
18123fdadf7dSDmitry Mishin 		if (get_user(len, optlen))
18133fdadf7dSDmitry Mishin 			return -EFAULT;
18143fdadf7dSDmitry Mishin 
181501ea306fSPaolo Abeni 		err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
18163fdadf7dSDmitry Mishin 		if (err >= 0)
18173fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
18183fdadf7dSDmitry Mishin 		return err;
18193fdadf7dSDmitry Mishin 	}
18203fdadf7dSDmitry Mishin #endif
18213fdadf7dSDmitry Mishin 	return err;
18223fdadf7dSDmitry Mishin }
18234d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_getsockopt);
1824