xref: /linux/net/ipv4/ip_sockglue.c (revision 92101b3b2e3178087127709a556b091dae314e9e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		The IP to API glue.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	see ip.c
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Fixes:
111da177e4SLinus Torvalds  *		Many		:	Split from ip.c , see ip.c for history.
121da177e4SLinus Torvalds  *		Martin Mares	:	TOS setting fixed.
131da177e4SLinus Torvalds  *		Alan Cox	:	Fixed a couple of oopses in Martin's
141da177e4SLinus Torvalds  *					TOS tweaks.
151da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/types.h>
201da177e4SLinus Torvalds #include <linux/mm.h>
211da177e4SLinus Torvalds #include <linux/skbuff.h>
221da177e4SLinus Torvalds #include <linux/ip.h>
231da177e4SLinus Torvalds #include <linux/icmp.h>
2414c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
251da177e4SLinus Torvalds #include <linux/netdevice.h>
265a0e3ad6STejun Heo #include <linux/slab.h>
271da177e4SLinus Torvalds #include <net/sock.h>
281da177e4SLinus Torvalds #include <net/ip.h>
291da177e4SLinus Torvalds #include <net/icmp.h>
30d83d8461SArnaldo Carvalho de Melo #include <net/tcp_states.h>
311da177e4SLinus Torvalds #include <linux/udp.h>
321da177e4SLinus Torvalds #include <linux/igmp.h>
331da177e4SLinus Torvalds #include <linux/netfilter.h>
341da177e4SLinus Torvalds #include <linux/route.h>
351da177e4SLinus Torvalds #include <linux/mroute.h>
362c67e9acSMaciej Żenczykowski #include <net/inet_ecn.h>
371da177e4SLinus Torvalds #include <net/route.h>
381da177e4SLinus Torvalds #include <net/xfrm.h>
39dae50295SDavid L Stevens #include <net/compat.h>
40dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
411da177e4SLinus Torvalds #include <net/transp_v6.h>
421da177e4SLinus Torvalds #endif
4335ebf65eSDavid S. Miller #include <net/ip_fib.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <linux/errqueue.h>
461da177e4SLinus Torvalds #include <asm/uaccess.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #define IP_CMSG_PKTINFO		1
491da177e4SLinus Torvalds #define IP_CMSG_TTL		2
501da177e4SLinus Torvalds #define IP_CMSG_TOS		4
511da177e4SLinus Torvalds #define IP_CMSG_RECVOPTS	8
521da177e4SLinus Torvalds #define IP_CMSG_RETOPTS		16
532c7946a7SCatherine Zhang #define IP_CMSG_PASSSEC		32
54e8b2dfe9SBalazs Scheidler #define IP_CMSG_ORIGDSTADDR     64
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  *	SOL_IP control messages.
581da177e4SLinus Torvalds  */
59d826eb14SEric Dumazet #define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb))
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
621da177e4SLinus Torvalds {
63d826eb14SEric Dumazet 	struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
641da177e4SLinus Torvalds 
65eddc9ec5SArnaldo Carvalho de Melo 	info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
711da177e4SLinus Torvalds {
72eddc9ec5SArnaldo Carvalho de Melo 	int ttl = ip_hdr(skb)->ttl;
731da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
771da177e4SLinus Torvalds {
78eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
841da177e4SLinus Torvalds 		return;
851da177e4SLinus Torvalds 
86eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
87eddc9ec5SArnaldo Carvalho de Melo 		 ip_hdr(skb) + 1);
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	unsigned char optbuf[sizeof(struct ip_options) + 40];
941da177e4SLinus Torvalds 	struct ip_options *opt = (struct ip_options *)optbuf;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
971da177e4SLinus Torvalds 		return;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	if (ip_options_echo(opt, skb)) {
1001da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
1011da177e4SLinus Torvalds 		return;
1021da177e4SLinus Torvalds 	}
1031da177e4SLinus Torvalds 	ip_options_undo(opt);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1082c7946a7SCatherine Zhang static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
1092c7946a7SCatherine Zhang {
1102c7946a7SCatherine Zhang 	char *secdata;
111dc49c1f9SCatherine Zhang 	u32 seclen, secid;
1122c7946a7SCatherine Zhang 	int err;
1132c7946a7SCatherine Zhang 
114dc49c1f9SCatherine Zhang 	err = security_socket_getpeersec_dgram(NULL, skb, &secid);
115dc49c1f9SCatherine Zhang 	if (err)
116dc49c1f9SCatherine Zhang 		return;
117dc49c1f9SCatherine Zhang 
118dc49c1f9SCatherine Zhang 	err = security_secid_to_secctx(secid, &secdata, &seclen);
1192c7946a7SCatherine Zhang 	if (err)
1202c7946a7SCatherine Zhang 		return;
1212c7946a7SCatherine Zhang 
1222c7946a7SCatherine Zhang 	put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
123dc49c1f9SCatherine Zhang 	security_release_secctx(secdata, seclen);
1242c7946a7SCatherine Zhang }
1252c7946a7SCatherine Zhang 
12621d1a161SHarvey Harrison static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
127e8b2dfe9SBalazs Scheidler {
128e8b2dfe9SBalazs Scheidler 	struct sockaddr_in sin;
129b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
13021d1a161SHarvey Harrison 	__be16 *ports = (__be16 *)skb_transport_header(skb);
131e8b2dfe9SBalazs Scheidler 
132e8b2dfe9SBalazs Scheidler 	if (skb_transport_offset(skb) + 4 > skb->len)
133e8b2dfe9SBalazs Scheidler 		return;
134e8b2dfe9SBalazs Scheidler 
135e8b2dfe9SBalazs Scheidler 	/* All current transport protocols have the port numbers in the
136e8b2dfe9SBalazs Scheidler 	 * first four bytes of the transport header and this function is
137e8b2dfe9SBalazs Scheidler 	 * written with this assumption in mind.
138e8b2dfe9SBalazs Scheidler 	 */
139e8b2dfe9SBalazs Scheidler 
140e8b2dfe9SBalazs Scheidler 	sin.sin_family = AF_INET;
141e8b2dfe9SBalazs Scheidler 	sin.sin_addr.s_addr = iph->daddr;
142e8b2dfe9SBalazs Scheidler 	sin.sin_port = ports[1];
143e8b2dfe9SBalazs Scheidler 	memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
144e8b2dfe9SBalazs Scheidler 
145e8b2dfe9SBalazs Scheidler 	put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
146e8b2dfe9SBalazs Scheidler }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(skb->sk);
15195c96174SEric Dumazet 	unsigned int flags = inet->cmsg_flags;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	/* Ordered by supposed usage frequency */
1541da177e4SLinus Torvalds 	if (flags & 1)
1551da177e4SLinus Torvalds 		ip_cmsg_recv_pktinfo(msg, skb);
1561da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1571da177e4SLinus Torvalds 		return;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	if (flags & 1)
1601da177e4SLinus Torvalds 		ip_cmsg_recv_ttl(msg, skb);
1611da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1621da177e4SLinus Torvalds 		return;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	if (flags & 1)
1651da177e4SLinus Torvalds 		ip_cmsg_recv_tos(msg, skb);
1661da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1671da177e4SLinus Torvalds 		return;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	if (flags & 1)
1701da177e4SLinus Torvalds 		ip_cmsg_recv_opts(msg, skb);
1711da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1721da177e4SLinus Torvalds 		return;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	if (flags & 1)
1751da177e4SLinus Torvalds 		ip_cmsg_recv_retopts(msg, skb);
1762c7946a7SCatherine Zhang 	if ((flags >>= 1) == 0)
1772c7946a7SCatherine Zhang 		return;
1782c7946a7SCatherine Zhang 
1792c7946a7SCatherine Zhang 	if (flags & 1)
1802c7946a7SCatherine Zhang 		ip_cmsg_recv_security(msg, skb);
181e8b2dfe9SBalazs Scheidler 
182e8b2dfe9SBalazs Scheidler 	if ((flags >>= 1) == 0)
183e8b2dfe9SBalazs Scheidler 		return;
184e8b2dfe9SBalazs Scheidler 	if (flags & 1)
185e8b2dfe9SBalazs Scheidler 		ip_cmsg_recv_dstaddr(msg, skb);
186e8b2dfe9SBalazs Scheidler 
1871da177e4SLinus Torvalds }
1884d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_cmsg_recv);
1891da177e4SLinus Torvalds 
1907a6adb92SDenis V. Lunev int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	int err;
1931da177e4SLinus Torvalds 	struct cmsghdr *cmsg;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1961da177e4SLinus Torvalds 		if (!CMSG_OK(msg, cmsg))
1971da177e4SLinus Torvalds 			return -EINVAL;
1981da177e4SLinus Torvalds 		if (cmsg->cmsg_level != SOL_IP)
1991da177e4SLinus Torvalds 			continue;
2001da177e4SLinus Torvalds 		switch (cmsg->cmsg_type) {
2011da177e4SLinus Torvalds 		case IP_RETOPTS:
2021da177e4SLinus Torvalds 			err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
2034d52cfbeSEric Dumazet 			err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
2044d52cfbeSEric Dumazet 					     err < 40 ? err : 40);
2051da177e4SLinus Torvalds 			if (err)
2061da177e4SLinus Torvalds 				return err;
2071da177e4SLinus Torvalds 			break;
2081da177e4SLinus Torvalds 		case IP_PKTINFO:
2091da177e4SLinus Torvalds 		{
2101da177e4SLinus Torvalds 			struct in_pktinfo *info;
2111da177e4SLinus Torvalds 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
2121da177e4SLinus Torvalds 				return -EINVAL;
2131da177e4SLinus Torvalds 			info = (struct in_pktinfo *)CMSG_DATA(cmsg);
2141da177e4SLinus Torvalds 			ipc->oif = info->ipi_ifindex;
2151da177e4SLinus Torvalds 			ipc->addr = info->ipi_spec_dst.s_addr;
2161da177e4SLinus Torvalds 			break;
2171da177e4SLinus Torvalds 		}
2181da177e4SLinus Torvalds 		default:
2191da177e4SLinus Torvalds 			return -EINVAL;
2201da177e4SLinus Torvalds 		}
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 	return 0;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds /* Special input handler for packets caught by router alert option.
2271da177e4SLinus Torvalds    They are selected only by protocol field, and then processed likely
2281da177e4SLinus Torvalds    local ones; but only if someone wants them! Otherwise, router
2291da177e4SLinus Torvalds    not running rsvpd will kill RSVP.
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds    It is user level problem, what it will make with them.
2321da177e4SLinus Torvalds    I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
2331da177e4SLinus Torvalds    but receiver should be enough clever f.e. to forward mtrace requests,
2341da177e4SLinus Torvalds    sent to multicast group to reach destination designated router.
2351da177e4SLinus Torvalds  */
23643a951e9SEric Dumazet struct ip_ra_chain __rcu *ip_ra_chain;
23766018506SEric Dumazet static DEFINE_SPINLOCK(ip_ra_lock);
23866018506SEric Dumazet 
239592fcb9dSEric Dumazet 
240592fcb9dSEric Dumazet static void ip_ra_destroy_rcu(struct rcu_head *head)
24166018506SEric Dumazet {
242592fcb9dSEric Dumazet 	struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
243592fcb9dSEric Dumazet 
244592fcb9dSEric Dumazet 	sock_put(ra->saved_sk);
245592fcb9dSEric Dumazet 	kfree(ra);
24666018506SEric Dumazet }
2471da177e4SLinus Torvalds 
2484d52cfbeSEric Dumazet int ip_ra_control(struct sock *sk, unsigned char on,
2494d52cfbeSEric Dumazet 		  void (*destructor)(struct sock *))
2501da177e4SLinus Torvalds {
25143a951e9SEric Dumazet 	struct ip_ra_chain *ra, *new_ra;
25243a951e9SEric Dumazet 	struct ip_ra_chain __rcu **rap;
2531da177e4SLinus Torvalds 
254c720c7e8SEric Dumazet 	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
2551da177e4SLinus Torvalds 		return -EINVAL;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
2581da177e4SLinus Torvalds 
25966018506SEric Dumazet 	spin_lock_bh(&ip_ra_lock);
26043a951e9SEric Dumazet 	for (rap = &ip_ra_chain;
26143a951e9SEric Dumazet 	     (ra = rcu_dereference_protected(*rap,
26243a951e9SEric Dumazet 			lockdep_is_held(&ip_ra_lock))) != NULL;
26343a951e9SEric Dumazet 	     rap = &ra->next) {
2641da177e4SLinus Torvalds 		if (ra->sk == sk) {
2651da177e4SLinus Torvalds 			if (on) {
26666018506SEric Dumazet 				spin_unlock_bh(&ip_ra_lock);
2671da177e4SLinus Torvalds 				kfree(new_ra);
2681da177e4SLinus Torvalds 				return -EADDRINUSE;
2691da177e4SLinus Torvalds 			}
270592fcb9dSEric Dumazet 			/* dont let ip_call_ra_chain() use sk again */
271592fcb9dSEric Dumazet 			ra->sk = NULL;
27266018506SEric Dumazet 			rcu_assign_pointer(*rap, ra->next);
27366018506SEric Dumazet 			spin_unlock_bh(&ip_ra_lock);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 			if (ra->destructor)
2761da177e4SLinus Torvalds 				ra->destructor(sk);
277592fcb9dSEric Dumazet 			/*
278592fcb9dSEric Dumazet 			 * Delay sock_put(sk) and kfree(ra) after one rcu grace
279592fcb9dSEric Dumazet 			 * period. This guarantee ip_call_ra_chain() dont need
280592fcb9dSEric Dumazet 			 * to mess with socket refcounts.
281592fcb9dSEric Dumazet 			 */
282592fcb9dSEric Dumazet 			ra->saved_sk = sk;
283592fcb9dSEric Dumazet 			call_rcu(&ra->rcu, ip_ra_destroy_rcu);
2841da177e4SLinus Torvalds 			return 0;
2851da177e4SLinus Torvalds 		}
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds 	if (new_ra == NULL) {
28866018506SEric Dumazet 		spin_unlock_bh(&ip_ra_lock);
2891da177e4SLinus Torvalds 		return -ENOBUFS;
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 	new_ra->sk = sk;
2921da177e4SLinus Torvalds 	new_ra->destructor = destructor;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	new_ra->next = ra;
29566018506SEric Dumazet 	rcu_assign_pointer(*rap, new_ra);
2961da177e4SLinus Torvalds 	sock_hold(sk);
29766018506SEric Dumazet 	spin_unlock_bh(&ip_ra_lock);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	return 0;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
30335986b32SAl Viro 		   __be16 port, u32 info, u8 *payload)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	skb = skb_clone(skb, GFP_ATOMIC);
3081da177e4SLinus Torvalds 	if (!skb)
3091da177e4SLinus Torvalds 		return;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3121da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
3131da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
31488c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_type = icmp_hdr(skb)->type;
31588c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_code = icmp_hdr(skb)->code;
3161da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
3171da177e4SLinus Torvalds 	serr->ee.ee_info = info;
3181da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
31988c7664fSArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
320d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb);
3211da177e4SLinus Torvalds 	serr->port = port;
3221da177e4SLinus Torvalds 
323bd82393cSArnaldo Carvalho de Melo 	if (skb_pull(skb, payload - skb->data) != NULL) {
324bd82393cSArnaldo Carvalho de Melo 		skb_reset_transport_header(skb);
325bd82393cSArnaldo Carvalho de Melo 		if (sock_queue_err_skb(sk, skb) == 0)
326bd82393cSArnaldo Carvalho de Melo 			return;
327bd82393cSArnaldo Carvalho de Melo 	}
3281da177e4SLinus Torvalds 	kfree_skb(skb);
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3310579016eSAl Viro void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3341da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3351da177e4SLinus Torvalds 	struct iphdr *iph;
3361da177e4SLinus Torvalds 	struct sk_buff *skb;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	if (!inet->recverr)
3391da177e4SLinus Torvalds 		return;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
3421da177e4SLinus Torvalds 	if (!skb)
3431da177e4SLinus Torvalds 		return;
3441da177e4SLinus Torvalds 
3452ca9e6f2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr));
3462ca9e6f2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
347eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
3481da177e4SLinus Torvalds 	iph->daddr = daddr;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3511da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
3521da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
3531da177e4SLinus Torvalds 	serr->ee.ee_type = 0;
3541da177e4SLinus Torvalds 	serr->ee.ee_code = 0;
3551da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
3561da177e4SLinus Torvalds 	serr->ee.ee_info = info;
3571da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
358d56f90a7SArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
3591da177e4SLinus Torvalds 	serr->port = port;
3601da177e4SLinus Torvalds 
36127a884dcSArnaldo Carvalho de Melo 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
362bd82393cSArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	if (sock_queue_err_skb(sk, skb))
3651da177e4SLinus Torvalds 		kfree_skb(skb);
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds /*
3691da177e4SLinus Torvalds  *	Handle MSG_ERRQUEUE
3701da177e4SLinus Torvalds  */
3711da177e4SLinus Torvalds int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3741da177e4SLinus Torvalds 	struct sk_buff *skb, *skb2;
3751da177e4SLinus Torvalds 	struct sockaddr_in *sin;
3761da177e4SLinus Torvalds 	struct {
3771da177e4SLinus Torvalds 		struct sock_extended_err ee;
3781da177e4SLinus Torvalds 		struct sockaddr_in	 offender;
3791da177e4SLinus Torvalds 	} errhdr;
3801da177e4SLinus Torvalds 	int err;
3811da177e4SLinus Torvalds 	int copied;
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	err = -EAGAIN;
3841da177e4SLinus Torvalds 	skb = skb_dequeue(&sk->sk_error_queue);
3851da177e4SLinus Torvalds 	if (skb == NULL)
3861da177e4SLinus Torvalds 		goto out;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	copied = skb->len;
3891da177e4SLinus Torvalds 	if (copied > len) {
3901da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
3911da177e4SLinus Torvalds 		copied = len;
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3941da177e4SLinus Torvalds 	if (err)
3951da177e4SLinus Torvalds 		goto out_free_skb;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	sock_recv_timestamp(msg, sk, skb);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	sin = (struct sockaddr_in *)msg->msg_name;
4021da177e4SLinus Torvalds 	if (sin) {
4031da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
404d56f90a7SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
405d56f90a7SArnaldo Carvalho de Melo 						   serr->addr_offset);
4061da177e4SLinus Torvalds 		sin->sin_port = serr->port;
4071da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4081da177e4SLinus Torvalds 	}
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
4111da177e4SLinus Torvalds 	sin = &errhdr.offender;
4121da177e4SLinus Torvalds 	sin->sin_family = AF_UNSPEC;
4131da177e4SLinus Torvalds 	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
4141da177e4SLinus Torvalds 		struct inet_sock *inet = inet_sk(sk);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
417eddc9ec5SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
4181da177e4SLinus Torvalds 		sin->sin_port = 0;
4191da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4201da177e4SLinus Torvalds 		if (inet->cmsg_flags)
4211da177e4SLinus Torvalds 			ip_cmsg_recv(msg, skb);
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	/* Now we could try to dump offended packet options */
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	msg->msg_flags |= MSG_ERRQUEUE;
4291da177e4SLinus Torvalds 	err = copied;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	/* Reset and regenerate socket error */
432e0f9f858SHerbert Xu 	spin_lock_bh(&sk->sk_error_queue.lock);
4331da177e4SLinus Torvalds 	sk->sk_err = 0;
4344d52cfbeSEric Dumazet 	skb2 = skb_peek(&sk->sk_error_queue);
4354d52cfbeSEric Dumazet 	if (skb2 != NULL) {
4361da177e4SLinus Torvalds 		sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
437e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
4381da177e4SLinus Torvalds 		sk->sk_error_report(sk);
4391da177e4SLinus Torvalds 	} else
440e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds out_free_skb:
4431da177e4SLinus Torvalds 	kfree_skb(skb);
4441da177e4SLinus Torvalds out:
4451da177e4SLinus Torvalds 	return err;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds /*
4504d52cfbeSEric Dumazet  *	Socket option code for IP. This is the end of the line after any
4514d52cfbeSEric Dumazet  *	TCP,UDP etc options on an IP socket.
4521da177e4SLinus Torvalds  */
4531da177e4SLinus Torvalds 
4543fdadf7dSDmitry Mishin static int do_ip_setsockopt(struct sock *sk, int level,
455b7058842SDavid S. Miller 			    int optname, char __user *optval, unsigned int optlen)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4581da177e4SLinus Torvalds 	int val = 0, err;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
4611da177e4SLinus Torvalds 			     (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
4621da177e4SLinus Torvalds 			     (1<<IP_RETOPTS) | (1<<IP_TOS) |
4631da177e4SLinus Torvalds 			     (1<<IP_TTL) | (1<<IP_HDRINCL) |
4641da177e4SLinus Torvalds 			     (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
4652c7946a7SCatherine Zhang 			     (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
466d218d111SStephen Hemminger 			     (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) |
4677b2ff18eSJiri Olsa 			     (1<<IP_MINTTL) | (1<<IP_NODEFRAG))) ||
46876e21053SErich E. Hoover 	    optname == IP_UNICAST_IF ||
4691da177e4SLinus Torvalds 	    optname == IP_MULTICAST_TTL ||
470f771bef9SNivedita Singhvi 	    optname == IP_MULTICAST_ALL ||
471e8b2dfe9SBalazs Scheidler 	    optname == IP_MULTICAST_LOOP ||
472e8b2dfe9SBalazs Scheidler 	    optname == IP_RECVORIGDSTADDR) {
4731da177e4SLinus Torvalds 		if (optlen >= sizeof(int)) {
4741da177e4SLinus Torvalds 			if (get_user(val, (int __user *) optval))
4751da177e4SLinus Torvalds 				return -EFAULT;
4761da177e4SLinus Torvalds 		} else if (optlen >= sizeof(char)) {
4771da177e4SLinus Torvalds 			unsigned char ucval;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 			if (get_user(ucval, (unsigned char __user *) optval))
4801da177e4SLinus Torvalds 				return -EFAULT;
4811da177e4SLinus Torvalds 			val = (int) ucval;
4821da177e4SLinus Torvalds 		}
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/* If optlen==0, it is equivalent to val == 0 */
4861da177e4SLinus Torvalds 
4876a9fb947SPavel Emelyanov 	if (ip_mroute_opt(optname))
4881da177e4SLinus Torvalds 		return ip_mroute_setsockopt(sk, optname, optval, optlen);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	err = 0;
4911da177e4SLinus Torvalds 	lock_sock(sk);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	switch (optname) {
4941da177e4SLinus Torvalds 	case IP_OPTIONS:
4951da177e4SLinus Torvalds 	{
496f6d8bd05SEric Dumazet 		struct ip_options_rcu *old, *opt = NULL;
497f6d8bd05SEric Dumazet 
49865a1c4ffSroel kluin 		if (optlen > 40)
4991da177e4SLinus Torvalds 			goto e_inval;
5003b1e0a65SYOSHIFUJI Hideaki 		err = ip_options_get_from_user(sock_net(sk), &opt,
501cb84663eSDenis V. Lunev 					       optval, optlen);
5021da177e4SLinus Torvalds 		if (err)
5031da177e4SLinus Torvalds 			break;
504f6d8bd05SEric Dumazet 		old = rcu_dereference_protected(inet->inet_opt,
505f6d8bd05SEric Dumazet 						sock_owned_by_user(sk));
506d83d8461SArnaldo Carvalho de Melo 		if (inet->is_icsk) {
507d83d8461SArnaldo Carvalho de Melo 			struct inet_connection_sock *icsk = inet_csk(sk);
508dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
5091da177e4SLinus Torvalds 			if (sk->sk_family == PF_INET ||
5101da177e4SLinus Torvalds 			    (!((1 << sk->sk_state) &
5111da177e4SLinus Torvalds 			       (TCPF_LISTEN | TCPF_CLOSE)) &&
512c720c7e8SEric Dumazet 			     inet->inet_daddr != LOOPBACK4_IPV6)) {
5131da177e4SLinus Torvalds #endif
514f6d8bd05SEric Dumazet 				if (old)
515f6d8bd05SEric Dumazet 					icsk->icsk_ext_hdr_len -= old->opt.optlen;
5161da177e4SLinus Torvalds 				if (opt)
517f6d8bd05SEric Dumazet 					icsk->icsk_ext_hdr_len += opt->opt.optlen;
518d83d8461SArnaldo Carvalho de Melo 				icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
519dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
5201da177e4SLinus Torvalds 			}
5211da177e4SLinus Torvalds #endif
5221da177e4SLinus Torvalds 		}
523f6d8bd05SEric Dumazet 		rcu_assign_pointer(inet->inet_opt, opt);
524f6d8bd05SEric Dumazet 		if (old)
525605b4afeSPaul E. McKenney 			kfree_rcu(old, rcu);
5261da177e4SLinus Torvalds 		break;
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds 	case IP_PKTINFO:
5291da177e4SLinus Torvalds 		if (val)
5301da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_PKTINFO;
5311da177e4SLinus Torvalds 		else
5321da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
5331da177e4SLinus Torvalds 		break;
5341da177e4SLinus Torvalds 	case IP_RECVTTL:
5351da177e4SLinus Torvalds 		if (val)
5361da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TTL;
5371da177e4SLinus Torvalds 		else
5381da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TTL;
5391da177e4SLinus Torvalds 		break;
5401da177e4SLinus Torvalds 	case IP_RECVTOS:
5411da177e4SLinus Torvalds 		if (val)
5421da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TOS;
5431da177e4SLinus Torvalds 		else
5441da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TOS;
5451da177e4SLinus Torvalds 		break;
5461da177e4SLinus Torvalds 	case IP_RECVOPTS:
5471da177e4SLinus Torvalds 		if (val)
5481da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_RECVOPTS;
5491da177e4SLinus Torvalds 		else
5501da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
5511da177e4SLinus Torvalds 		break;
5521da177e4SLinus Torvalds 	case IP_RETOPTS:
5531da177e4SLinus Torvalds 		if (val)
5541da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_RETOPTS;
5551da177e4SLinus Torvalds 		else
5561da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
5571da177e4SLinus Torvalds 		break;
5582c7946a7SCatherine Zhang 	case IP_PASSSEC:
5592c7946a7SCatherine Zhang 		if (val)
5602c7946a7SCatherine Zhang 			inet->cmsg_flags |= IP_CMSG_PASSSEC;
5612c7946a7SCatherine Zhang 		else
5622c7946a7SCatherine Zhang 			inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
5632c7946a7SCatherine Zhang 		break;
564e8b2dfe9SBalazs Scheidler 	case IP_RECVORIGDSTADDR:
565e8b2dfe9SBalazs Scheidler 		if (val)
566e8b2dfe9SBalazs Scheidler 			inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
567e8b2dfe9SBalazs Scheidler 		else
568e8b2dfe9SBalazs Scheidler 			inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
569e8b2dfe9SBalazs Scheidler 		break;
5701da177e4SLinus Torvalds 	case IP_TOS:	/* This sets both TOS and Precedence */
5711da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM) {
5722c67e9acSMaciej Żenczykowski 			val &= ~INET_ECN_MASK;
5732c67e9acSMaciej Żenczykowski 			val |= inet->tos & INET_ECN_MASK;
5741da177e4SLinus Torvalds 		}
5751da177e4SLinus Torvalds 		if (inet->tos != val) {
5761da177e4SLinus Torvalds 			inet->tos = val;
5771da177e4SLinus Torvalds 			sk->sk_priority = rt_tos2priority(val);
5781da177e4SLinus Torvalds 			sk_dst_reset(sk);
5791da177e4SLinus Torvalds 		}
5801da177e4SLinus Torvalds 		break;
5811da177e4SLinus Torvalds 	case IP_TTL:
5821da177e4SLinus Torvalds 		if (optlen < 1)
5831da177e4SLinus Torvalds 			goto e_inval;
5844d52cfbeSEric Dumazet 		if (val != -1 && (val < 0 || val > 255))
5851da177e4SLinus Torvalds 			goto e_inval;
5861da177e4SLinus Torvalds 		inet->uc_ttl = val;
5871da177e4SLinus Torvalds 		break;
5881da177e4SLinus Torvalds 	case IP_HDRINCL:
5891da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_RAW) {
5901da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
5911da177e4SLinus Torvalds 			break;
5921da177e4SLinus Torvalds 		}
5931da177e4SLinus Torvalds 		inet->hdrincl = val ? 1 : 0;
5941da177e4SLinus Torvalds 		break;
5957b2ff18eSJiri Olsa 	case IP_NODEFRAG:
5967b2ff18eSJiri Olsa 		if (sk->sk_type != SOCK_RAW) {
5977b2ff18eSJiri Olsa 			err = -ENOPROTOOPT;
5987b2ff18eSJiri Olsa 			break;
5997b2ff18eSJiri Olsa 		}
6007b2ff18eSJiri Olsa 		inet->nodefrag = val ? 1 : 0;
6017b2ff18eSJiri Olsa 		break;
6021da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
6030eae750eSJohn Dykstra 		if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
6041da177e4SLinus Torvalds 			goto e_inval;
6051da177e4SLinus Torvalds 		inet->pmtudisc = val;
6061da177e4SLinus Torvalds 		break;
6071da177e4SLinus Torvalds 	case IP_RECVERR:
6081da177e4SLinus Torvalds 		inet->recverr = !!val;
6091da177e4SLinus Torvalds 		if (!val)
6101da177e4SLinus Torvalds 			skb_queue_purge(&sk->sk_error_queue);
6111da177e4SLinus Torvalds 		break;
6121da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
6131da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
6141da177e4SLinus Torvalds 			goto e_inval;
6151da177e4SLinus Torvalds 		if (optlen < 1)
6161da177e4SLinus Torvalds 			goto e_inval;
6171da177e4SLinus Torvalds 		if (val == -1)
6181da177e4SLinus Torvalds 			val = 1;
6191da177e4SLinus Torvalds 		if (val < 0 || val > 255)
6201da177e4SLinus Torvalds 			goto e_inval;
6211da177e4SLinus Torvalds 		inet->mc_ttl = val;
6221da177e4SLinus Torvalds 		break;
6231da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
6241da177e4SLinus Torvalds 		if (optlen < 1)
6251da177e4SLinus Torvalds 			goto e_inval;
6261da177e4SLinus Torvalds 		inet->mc_loop = !!val;
6271da177e4SLinus Torvalds 		break;
62876e21053SErich E. Hoover 	case IP_UNICAST_IF:
62976e21053SErich E. Hoover 	{
63076e21053SErich E. Hoover 		struct net_device *dev = NULL;
63176e21053SErich E. Hoover 		int ifindex;
63276e21053SErich E. Hoover 
63376e21053SErich E. Hoover 		if (optlen != sizeof(int))
63476e21053SErich E. Hoover 			goto e_inval;
63576e21053SErich E. Hoover 
63676e21053SErich E. Hoover 		ifindex = (__force int)ntohl((__force __be32)val);
63776e21053SErich E. Hoover 		if (ifindex == 0) {
63876e21053SErich E. Hoover 			inet->uc_index = 0;
63976e21053SErich E. Hoover 			err = 0;
64076e21053SErich E. Hoover 			break;
64176e21053SErich E. Hoover 		}
64276e21053SErich E. Hoover 
64376e21053SErich E. Hoover 		dev = dev_get_by_index(sock_net(sk), ifindex);
64476e21053SErich E. Hoover 		err = -EADDRNOTAVAIL;
64576e21053SErich E. Hoover 		if (!dev)
64676e21053SErich E. Hoover 			break;
64776e21053SErich E. Hoover 		dev_put(dev);
64876e21053SErich E. Hoover 
64976e21053SErich E. Hoover 		err = -EINVAL;
65076e21053SErich E. Hoover 		if (sk->sk_bound_dev_if)
65176e21053SErich E. Hoover 			break;
65276e21053SErich E. Hoover 
65376e21053SErich E. Hoover 		inet->uc_index = ifindex;
65476e21053SErich E. Hoover 		err = 0;
65576e21053SErich E. Hoover 		break;
65676e21053SErich E. Hoover 	}
6571da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
6581da177e4SLinus Torvalds 	{
6591da177e4SLinus Torvalds 		struct ip_mreqn mreq;
6601da177e4SLinus Torvalds 		struct net_device *dev = NULL;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
6631da177e4SLinus Torvalds 			goto e_inval;
6641da177e4SLinus Torvalds 		/*
6651da177e4SLinus Torvalds 		 *	Check the arguments are allowable
6661da177e4SLinus Torvalds 		 */
6671da177e4SLinus Torvalds 
6680915921bSShan Wei 		if (optlen < sizeof(struct in_addr))
6690915921bSShan Wei 			goto e_inval;
6700915921bSShan Wei 
6711da177e4SLinus Torvalds 		err = -EFAULT;
6721da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
6731da177e4SLinus Torvalds 			if (copy_from_user(&mreq, optval, sizeof(mreq)))
6741da177e4SLinus Torvalds 				break;
6751da177e4SLinus Torvalds 		} else {
6761da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
6773a084ddbSJiri Pirko 			if (optlen >= sizeof(struct ip_mreq)) {
6783a084ddbSJiri Pirko 				if (copy_from_user(&mreq, optval,
6793a084ddbSJiri Pirko 						   sizeof(struct ip_mreq)))
6803a084ddbSJiri Pirko 					break;
6813a084ddbSJiri Pirko 			} else if (optlen >= sizeof(struct in_addr)) {
6823a084ddbSJiri Pirko 				if (copy_from_user(&mreq.imr_address, optval,
6834d52cfbeSEric Dumazet 						   sizeof(struct in_addr)))
6841da177e4SLinus Torvalds 					break;
6851da177e4SLinus Torvalds 			}
6863a084ddbSJiri Pirko 		}
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 		if (!mreq.imr_ifindex) {
689e6f1cebfSAl Viro 			if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
6901da177e4SLinus Torvalds 				inet->mc_index = 0;
6911da177e4SLinus Torvalds 				inet->mc_addr  = 0;
6921da177e4SLinus Torvalds 				err = 0;
6931da177e4SLinus Torvalds 				break;
6941da177e4SLinus Torvalds 			}
6953b1e0a65SYOSHIFUJI Hideaki 			dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
69655b80503SEric Dumazet 			if (dev)
6971da177e4SLinus Torvalds 				mreq.imr_ifindex = dev->ifindex;
6981da177e4SLinus Torvalds 		} else
69955b80503SEric Dumazet 			dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
7031da177e4SLinus Torvalds 		if (!dev)
7041da177e4SLinus Torvalds 			break;
70555b80503SEric Dumazet 		dev_put(dev);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 		err = -EINVAL;
7081da177e4SLinus Torvalds 		if (sk->sk_bound_dev_if &&
7091da177e4SLinus Torvalds 		    mreq.imr_ifindex != sk->sk_bound_dev_if)
7101da177e4SLinus Torvalds 			break;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 		inet->mc_index = mreq.imr_ifindex;
7131da177e4SLinus Torvalds 		inet->mc_addr  = mreq.imr_address.s_addr;
7141da177e4SLinus Torvalds 		err = 0;
7151da177e4SLinus Torvalds 		break;
7161da177e4SLinus Torvalds 	}
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	case IP_ADD_MEMBERSHIP:
7191da177e4SLinus Torvalds 	case IP_DROP_MEMBERSHIP:
7201da177e4SLinus Torvalds 	{
7211da177e4SLinus Torvalds 		struct ip_mreqn mreq;
7221da177e4SLinus Torvalds 
723a96fb49bSFlavio Leitner 		err = -EPROTO;
724a96fb49bSFlavio Leitner 		if (inet_sk(sk)->is_icsk)
725a96fb49bSFlavio Leitner 			break;
726a96fb49bSFlavio Leitner 
7271da177e4SLinus Torvalds 		if (optlen < sizeof(struct ip_mreq))
7281da177e4SLinus Torvalds 			goto e_inval;
7291da177e4SLinus Torvalds 		err = -EFAULT;
7301da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
7311da177e4SLinus Torvalds 			if (copy_from_user(&mreq, optval, sizeof(mreq)))
7321da177e4SLinus Torvalds 				break;
7331da177e4SLinus Torvalds 		} else {
7341da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
7351da177e4SLinus Torvalds 			if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
7361da177e4SLinus Torvalds 				break;
7371da177e4SLinus Torvalds 		}
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 		if (optname == IP_ADD_MEMBERSHIP)
7401da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7411da177e4SLinus Torvalds 		else
7421da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
7431da177e4SLinus Torvalds 		break;
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 	case IP_MSFILTER:
7461da177e4SLinus Torvalds 	{
7471da177e4SLinus Torvalds 		struct ip_msfilter *msf;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 		if (optlen < IP_MSFILTER_SIZE(0))
7501da177e4SLinus Torvalds 			goto e_inval;
7511da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
7521da177e4SLinus Torvalds 			err = -ENOBUFS;
7531da177e4SLinus Torvalds 			break;
7541da177e4SLinus Torvalds 		}
7558b3a7005SKris Katterjohn 		msf = kmalloc(optlen, GFP_KERNEL);
756cfcabdccSStephen Hemminger 		if (!msf) {
7571da177e4SLinus Torvalds 			err = -ENOBUFS;
7581da177e4SLinus Torvalds 			break;
7591da177e4SLinus Torvalds 		}
7601da177e4SLinus Torvalds 		err = -EFAULT;
7611da177e4SLinus Torvalds 		if (copy_from_user(msf, optval, optlen)) {
7621da177e4SLinus Torvalds 			kfree(msf);
7631da177e4SLinus Torvalds 			break;
7641da177e4SLinus Torvalds 		}
7651da177e4SLinus Torvalds 		/* numsrc >= (1G-4) overflow in 32 bits */
7661da177e4SLinus Torvalds 		if (msf->imsf_numsrc >= 0x3ffffffcU ||
7671da177e4SLinus Torvalds 		    msf->imsf_numsrc > sysctl_igmp_max_msf) {
7681da177e4SLinus Torvalds 			kfree(msf);
7691da177e4SLinus Torvalds 			err = -ENOBUFS;
7701da177e4SLinus Torvalds 			break;
7711da177e4SLinus Torvalds 		}
7721da177e4SLinus Torvalds 		if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
7731da177e4SLinus Torvalds 			kfree(msf);
7741da177e4SLinus Torvalds 			err = -EINVAL;
7751da177e4SLinus Torvalds 			break;
7761da177e4SLinus Torvalds 		}
7771da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, 0);
7781da177e4SLinus Torvalds 		kfree(msf);
7791da177e4SLinus Torvalds 		break;
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds 	case IP_BLOCK_SOURCE:
7821da177e4SLinus Torvalds 	case IP_UNBLOCK_SOURCE:
7831da177e4SLinus Torvalds 	case IP_ADD_SOURCE_MEMBERSHIP:
7841da177e4SLinus Torvalds 	case IP_DROP_SOURCE_MEMBERSHIP:
7851da177e4SLinus Torvalds 	{
7861da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
7871da177e4SLinus Torvalds 		int omode, add;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		if (optlen != sizeof(struct ip_mreq_source))
7901da177e4SLinus Torvalds 			goto e_inval;
7911da177e4SLinus Torvalds 		if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
7921da177e4SLinus Torvalds 			err = -EFAULT;
7931da177e4SLinus Torvalds 			break;
7941da177e4SLinus Torvalds 		}
7951da177e4SLinus Torvalds 		if (optname == IP_BLOCK_SOURCE) {
7961da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7971da177e4SLinus Torvalds 			add = 1;
7981da177e4SLinus Torvalds 		} else if (optname == IP_UNBLOCK_SOURCE) {
7991da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
8001da177e4SLinus Torvalds 			add = 0;
8011da177e4SLinus Torvalds 		} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
8021da177e4SLinus Torvalds 			struct ip_mreqn mreq;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 			mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
8051da177e4SLinus Torvalds 			mreq.imr_address.s_addr = mreqs.imr_interface;
8061da177e4SLinus Torvalds 			mreq.imr_ifindex = 0;
8071da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
8088cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
8091da177e4SLinus Torvalds 				break;
8101da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
8111da177e4SLinus Torvalds 			add = 1;
8121da177e4SLinus Torvalds 		} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
8131da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
8141da177e4SLinus Torvalds 			add = 0;
8151da177e4SLinus Torvalds 		}
8161da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs, 0);
8171da177e4SLinus Torvalds 		break;
8181da177e4SLinus Torvalds 	}
8191da177e4SLinus Torvalds 	case MCAST_JOIN_GROUP:
8201da177e4SLinus Torvalds 	case MCAST_LEAVE_GROUP:
8211da177e4SLinus Torvalds 	{
8221da177e4SLinus Torvalds 		struct group_req greq;
8231da177e4SLinus Torvalds 		struct sockaddr_in *psin;
8241da177e4SLinus Torvalds 		struct ip_mreqn mreq;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 		if (optlen < sizeof(struct group_req))
8271da177e4SLinus Torvalds 			goto e_inval;
8281da177e4SLinus Torvalds 		err = -EFAULT;
8291da177e4SLinus Torvalds 		if (copy_from_user(&greq, optval, sizeof(greq)))
8301da177e4SLinus Torvalds 			break;
8311da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greq.gr_group;
8321da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET)
8331da177e4SLinus Torvalds 			goto e_inval;
8341da177e4SLinus Torvalds 		memset(&mreq, 0, sizeof(mreq));
8351da177e4SLinus Torvalds 		mreq.imr_multiaddr = psin->sin_addr;
8361da177e4SLinus Torvalds 		mreq.imr_ifindex = greq.gr_interface;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 		if (optname == MCAST_JOIN_GROUP)
8391da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
8401da177e4SLinus Torvalds 		else
8411da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
8421da177e4SLinus Torvalds 		break;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 	case MCAST_JOIN_SOURCE_GROUP:
8451da177e4SLinus Torvalds 	case MCAST_LEAVE_SOURCE_GROUP:
8461da177e4SLinus Torvalds 	case MCAST_BLOCK_SOURCE:
8471da177e4SLinus Torvalds 	case MCAST_UNBLOCK_SOURCE:
8481da177e4SLinus Torvalds 	{
8491da177e4SLinus Torvalds 		struct group_source_req greqs;
8501da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
8511da177e4SLinus Torvalds 		struct sockaddr_in *psin;
8521da177e4SLinus Torvalds 		int omode, add;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 		if (optlen != sizeof(struct group_source_req))
8551da177e4SLinus Torvalds 			goto e_inval;
8561da177e4SLinus Torvalds 		if (copy_from_user(&greqs, optval, sizeof(greqs))) {
8571da177e4SLinus Torvalds 			err = -EFAULT;
8581da177e4SLinus Torvalds 			break;
8591da177e4SLinus Torvalds 		}
8601da177e4SLinus Torvalds 		if (greqs.gsr_group.ss_family != AF_INET ||
8611da177e4SLinus Torvalds 		    greqs.gsr_source.ss_family != AF_INET) {
8621da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
8631da177e4SLinus Torvalds 			break;
8641da177e4SLinus Torvalds 		}
8651da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_group;
8661da177e4SLinus Torvalds 		mreqs.imr_multiaddr = psin->sin_addr.s_addr;
8671da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_source;
8681da177e4SLinus Torvalds 		mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
8691da177e4SLinus Torvalds 		mreqs.imr_interface = 0; /* use index for mc_source */
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 		if (optname == MCAST_BLOCK_SOURCE) {
8721da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
8731da177e4SLinus Torvalds 			add = 1;
8741da177e4SLinus Torvalds 		} else if (optname == MCAST_UNBLOCK_SOURCE) {
8751da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
8761da177e4SLinus Torvalds 			add = 0;
8771da177e4SLinus Torvalds 		} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
8781da177e4SLinus Torvalds 			struct ip_mreqn mreq;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&greqs.gsr_group;
8811da177e4SLinus Torvalds 			mreq.imr_multiaddr = psin->sin_addr;
8821da177e4SLinus Torvalds 			mreq.imr_address.s_addr = 0;
8831da177e4SLinus Torvalds 			mreq.imr_ifindex = greqs.gsr_interface;
8841da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
8858cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
8861da177e4SLinus Torvalds 				break;
8871da177e4SLinus Torvalds 			greqs.gsr_interface = mreq.imr_ifindex;
8881da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
8891da177e4SLinus Torvalds 			add = 1;
8901da177e4SLinus Torvalds 		} else /* MCAST_LEAVE_SOURCE_GROUP */ {
8911da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
8921da177e4SLinus Torvalds 			add = 0;
8931da177e4SLinus Torvalds 		}
8941da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs,
8951da177e4SLinus Torvalds 				   greqs.gsr_interface);
8961da177e4SLinus Torvalds 		break;
8971da177e4SLinus Torvalds 	}
8981da177e4SLinus Torvalds 	case MCAST_MSFILTER:
8991da177e4SLinus Torvalds 	{
9001da177e4SLinus Torvalds 		struct sockaddr_in *psin;
9011da177e4SLinus Torvalds 		struct ip_msfilter *msf = NULL;
9021da177e4SLinus Torvalds 		struct group_filter *gsf = NULL;
9031da177e4SLinus Torvalds 		int msize, i, ifindex;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 		if (optlen < GROUP_FILTER_SIZE(0))
9061da177e4SLinus Torvalds 			goto e_inval;
9071da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
9081da177e4SLinus Torvalds 			err = -ENOBUFS;
9091da177e4SLinus Torvalds 			break;
9101da177e4SLinus Torvalds 		}
9118b3a7005SKris Katterjohn 		gsf = kmalloc(optlen, GFP_KERNEL);
912cfcabdccSStephen Hemminger 		if (!gsf) {
9131da177e4SLinus Torvalds 			err = -ENOBUFS;
9141da177e4SLinus Torvalds 			break;
9151da177e4SLinus Torvalds 		}
9161da177e4SLinus Torvalds 		err = -EFAULT;
9174d52cfbeSEric Dumazet 		if (copy_from_user(gsf, optval, optlen))
9181da177e4SLinus Torvalds 			goto mc_msf_out;
9194d52cfbeSEric Dumazet 
9201da177e4SLinus Torvalds 		/* numsrc >= (4G-140)/128 overflow in 32 bits */
9211da177e4SLinus Torvalds 		if (gsf->gf_numsrc >= 0x1ffffff ||
9221da177e4SLinus Torvalds 		    gsf->gf_numsrc > sysctl_igmp_max_msf) {
9231da177e4SLinus Torvalds 			err = -ENOBUFS;
9241da177e4SLinus Torvalds 			goto mc_msf_out;
9251da177e4SLinus Torvalds 		}
9261da177e4SLinus Torvalds 		if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
9271da177e4SLinus Torvalds 			err = -EINVAL;
9281da177e4SLinus Torvalds 			goto mc_msf_out;
9291da177e4SLinus Torvalds 		}
9301da177e4SLinus Torvalds 		msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
9318b3a7005SKris Katterjohn 		msf = kmalloc(msize, GFP_KERNEL);
932cfcabdccSStephen Hemminger 		if (!msf) {
9331da177e4SLinus Torvalds 			err = -ENOBUFS;
9341da177e4SLinus Torvalds 			goto mc_msf_out;
9351da177e4SLinus Torvalds 		}
9361da177e4SLinus Torvalds 		ifindex = gsf->gf_interface;
9371da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&gsf->gf_group;
9381da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET) {
9391da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
9401da177e4SLinus Torvalds 			goto mc_msf_out;
9411da177e4SLinus Torvalds 		}
9421da177e4SLinus Torvalds 		msf->imsf_multiaddr = psin->sin_addr.s_addr;
9431da177e4SLinus Torvalds 		msf->imsf_interface = 0;
9441da177e4SLinus Torvalds 		msf->imsf_fmode = gsf->gf_fmode;
9451da177e4SLinus Torvalds 		msf->imsf_numsrc = gsf->gf_numsrc;
9461da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
9471da177e4SLinus Torvalds 		for (i = 0; i < gsf->gf_numsrc; ++i) {
9481da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&gsf->gf_slist[i];
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 			if (psin->sin_family != AF_INET)
9511da177e4SLinus Torvalds 				goto mc_msf_out;
9521da177e4SLinus Torvalds 			msf->imsf_slist[i] = psin->sin_addr.s_addr;
9531da177e4SLinus Torvalds 		}
9541da177e4SLinus Torvalds 		kfree(gsf);
9551da177e4SLinus Torvalds 		gsf = NULL;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, ifindex);
9581da177e4SLinus Torvalds mc_msf_out:
9591da177e4SLinus Torvalds 		kfree(msf);
9601da177e4SLinus Torvalds 		kfree(gsf);
9611da177e4SLinus Torvalds 		break;
9621da177e4SLinus Torvalds 	}
963f771bef9SNivedita Singhvi 	case IP_MULTICAST_ALL:
964f771bef9SNivedita Singhvi 		if (optlen < 1)
965f771bef9SNivedita Singhvi 			goto e_inval;
966f771bef9SNivedita Singhvi 		if (val != 0 && val != 1)
967f771bef9SNivedita Singhvi 			goto e_inval;
968f771bef9SNivedita Singhvi 		inet->mc_all = val;
969f771bef9SNivedita Singhvi 		break;
9701da177e4SLinus Torvalds 	case IP_ROUTER_ALERT:
9711da177e4SLinus Torvalds 		err = ip_ra_control(sk, val ? 1 : 0, NULL);
9721da177e4SLinus Torvalds 		break;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	case IP_FREEBIND:
9751da177e4SLinus Torvalds 		if (optlen < 1)
9761da177e4SLinus Torvalds 			goto e_inval;
9771da177e4SLinus Torvalds 		inet->freebind = !!val;
9781da177e4SLinus Torvalds 		break;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	case IP_IPSEC_POLICY:
9811da177e4SLinus Torvalds 	case IP_XFRM_POLICY:
9826fc0b4a7SHerbert Xu 		err = -EPERM;
9836fc0b4a7SHerbert Xu 		if (!capable(CAP_NET_ADMIN))
9846fc0b4a7SHerbert Xu 			break;
9851da177e4SLinus Torvalds 		err = xfrm_user_policy(sk, optname, optval, optlen);
9861da177e4SLinus Torvalds 		break;
9871da177e4SLinus Torvalds 
988f5715aeaSKOVACS Krisztian 	case IP_TRANSPARENT:
9896cc7a765SMaciej Żenczykowski 		if (!!val && !capable(CAP_NET_RAW) && !capable(CAP_NET_ADMIN)) {
990f5715aeaSKOVACS Krisztian 			err = -EPERM;
991f5715aeaSKOVACS Krisztian 			break;
992f5715aeaSKOVACS Krisztian 		}
993f5715aeaSKOVACS Krisztian 		if (optlen < 1)
994f5715aeaSKOVACS Krisztian 			goto e_inval;
995f5715aeaSKOVACS Krisztian 		inet->transparent = !!val;
996f5715aeaSKOVACS Krisztian 		break;
997f5715aeaSKOVACS Krisztian 
998d218d111SStephen Hemminger 	case IP_MINTTL:
999d218d111SStephen Hemminger 		if (optlen < 1)
1000d218d111SStephen Hemminger 			goto e_inval;
1001d218d111SStephen Hemminger 		if (val < 0 || val > 255)
1002d218d111SStephen Hemminger 			goto e_inval;
1003d218d111SStephen Hemminger 		inet->min_ttl = val;
1004d218d111SStephen Hemminger 		break;
1005d218d111SStephen Hemminger 
10061da177e4SLinus Torvalds 	default:
10071da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
10081da177e4SLinus Torvalds 		break;
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 	release_sock(sk);
10111da177e4SLinus Torvalds 	return err;
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds e_inval:
10141da177e4SLinus Torvalds 	release_sock(sk);
10151da177e4SLinus Torvalds 	return -EINVAL;
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
1018f84af32cSEric Dumazet /**
1019d826eb14SEric Dumazet  * ipv4_pktinfo_prepare - transfert some info from rtable to skb
1020f84af32cSEric Dumazet  * @sk: socket
1021f84af32cSEric Dumazet  * @skb: buffer
1022f84af32cSEric Dumazet  *
102335ebf65eSDavid S. Miller  * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
102435ebf65eSDavid S. Miller  * destination in skb->cb[] before dst drop.
1025d826eb14SEric Dumazet  * This way, receiver doesnt make cache line misses to read rtable.
1026f84af32cSEric Dumazet  */
1027d826eb14SEric Dumazet void ipv4_pktinfo_prepare(struct sk_buff *skb)
1028f84af32cSEric Dumazet {
1029d826eb14SEric Dumazet 	struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1030d826eb14SEric Dumazet 
1031*92101b3bSDavid S. Miller 	if (skb_rtable(skb)) {
1032*92101b3bSDavid S. Miller 		pktinfo->ipi_ifindex = inet_iif(skb);
103335ebf65eSDavid S. Miller 		pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1034d826eb14SEric Dumazet 	} else {
1035d826eb14SEric Dumazet 		pktinfo->ipi_ifindex = 0;
1036d826eb14SEric Dumazet 		pktinfo->ipi_spec_dst.s_addr = 0;
1037f84af32cSEric Dumazet 	}
1038d826eb14SEric Dumazet 	skb_dst_drop(skb);
1039d826eb14SEric Dumazet }
1040f84af32cSEric Dumazet 
10413fdadf7dSDmitry Mishin int ip_setsockopt(struct sock *sk, int level,
1042b7058842SDavid S. Miller 		int optname, char __user *optval, unsigned int optlen)
10433fdadf7dSDmitry Mishin {
10443fdadf7dSDmitry Mishin 	int err;
10453fdadf7dSDmitry Mishin 
10463fdadf7dSDmitry Mishin 	if (level != SOL_IP)
10473fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
10483fdadf7dSDmitry Mishin 
10493fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
10503fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
10513fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
10523fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
10536a9fb947SPavel Emelyanov 			optname != IP_IPSEC_POLICY &&
10546a9fb947SPavel Emelyanov 			optname != IP_XFRM_POLICY &&
10556a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
10563fdadf7dSDmitry Mishin 		lock_sock(sk);
10573fdadf7dSDmitry Mishin 		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
10583fdadf7dSDmitry Mishin 		release_sock(sk);
10593fdadf7dSDmitry Mishin 	}
10603fdadf7dSDmitry Mishin #endif
10613fdadf7dSDmitry Mishin 	return err;
10623fdadf7dSDmitry Mishin }
10634d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_setsockopt);
10643fdadf7dSDmitry Mishin 
10653fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
1066543d9cfeSArnaldo Carvalho de Melo int compat_ip_setsockopt(struct sock *sk, int level, int optname,
1067b7058842SDavid S. Miller 			 char __user *optval, unsigned int optlen)
10683fdadf7dSDmitry Mishin {
10693fdadf7dSDmitry Mishin 	int err;
10703fdadf7dSDmitry Mishin 
10713fdadf7dSDmitry Mishin 	if (level != SOL_IP)
10723fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
10733fdadf7dSDmitry Mishin 
1074dae50295SDavid L Stevens 	if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1075dae50295SDavid L Stevens 		return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1076dae50295SDavid L Stevens 			ip_setsockopt);
1077dae50295SDavid L Stevens 
10783fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
10793fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
10803fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
10813fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
10826a9fb947SPavel Emelyanov 			optname != IP_IPSEC_POLICY &&
10836a9fb947SPavel Emelyanov 			optname != IP_XFRM_POLICY &&
10846a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
10853fdadf7dSDmitry Mishin 		lock_sock(sk);
1086543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_setsockopt(sk, PF_INET, optname,
1087543d9cfeSArnaldo Carvalho de Melo 					   optval, optlen);
10883fdadf7dSDmitry Mishin 		release_sock(sk);
10893fdadf7dSDmitry Mishin 	}
10903fdadf7dSDmitry Mishin #endif
10913fdadf7dSDmitry Mishin 	return err;
10923fdadf7dSDmitry Mishin }
1093543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_setsockopt);
10943fdadf7dSDmitry Mishin #endif
10953fdadf7dSDmitry Mishin 
10961da177e4SLinus Torvalds /*
10974d52cfbeSEric Dumazet  *	Get the options. Note for future reference. The GET of IP options gets
10984d52cfbeSEric Dumazet  *	the _received_ ones. The set sets the _sent_ ones.
10991da177e4SLinus Torvalds  */
11001da177e4SLinus Torvalds 
11013fdadf7dSDmitry Mishin static int do_ip_getsockopt(struct sock *sk, int level, int optname,
110295c96174SEric Dumazet 			    char __user *optval, int __user *optlen, unsigned int flags)
11031da177e4SLinus Torvalds {
11041da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
11051da177e4SLinus Torvalds 	int val;
11061da177e4SLinus Torvalds 	int len;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	if (level != SOL_IP)
11091da177e4SLinus Torvalds 		return -EOPNOTSUPP;
11101da177e4SLinus Torvalds 
11116a9fb947SPavel Emelyanov 	if (ip_mroute_opt(optname))
11121da177e4SLinus Torvalds 		return ip_mroute_getsockopt(sk, optname, optval, optlen);
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 	if (get_user(len, optlen))
11151da177e4SLinus Torvalds 		return -EFAULT;
11161da177e4SLinus Torvalds 	if (len < 0)
11171da177e4SLinus Torvalds 		return -EINVAL;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	lock_sock(sk);
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	switch (optname) {
11221da177e4SLinus Torvalds 	case IP_OPTIONS:
11231da177e4SLinus Torvalds 	{
11241da177e4SLinus Torvalds 		unsigned char optbuf[sizeof(struct ip_options)+40];
11251da177e4SLinus Torvalds 		struct ip_options *opt = (struct ip_options *)optbuf;
1126f6d8bd05SEric Dumazet 		struct ip_options_rcu *inet_opt;
1127f6d8bd05SEric Dumazet 
1128f6d8bd05SEric Dumazet 		inet_opt = rcu_dereference_protected(inet->inet_opt,
1129f6d8bd05SEric Dumazet 						     sock_owned_by_user(sk));
11301da177e4SLinus Torvalds 		opt->optlen = 0;
1131f6d8bd05SEric Dumazet 		if (inet_opt)
1132f6d8bd05SEric Dumazet 			memcpy(optbuf, &inet_opt->opt,
11331da177e4SLinus Torvalds 			       sizeof(struct ip_options) +
1134f6d8bd05SEric Dumazet 			       inet_opt->opt.optlen);
11351da177e4SLinus Torvalds 		release_sock(sk);
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 		if (opt->optlen == 0)
11381da177e4SLinus Torvalds 			return put_user(0, optlen);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 		ip_options_undo(opt);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 		len = min_t(unsigned int, len, opt->optlen);
11431da177e4SLinus Torvalds 		if (put_user(len, optlen))
11441da177e4SLinus Torvalds 			return -EFAULT;
11451da177e4SLinus Torvalds 		if (copy_to_user(optval, opt->__data, len))
11461da177e4SLinus Torvalds 			return -EFAULT;
11471da177e4SLinus Torvalds 		return 0;
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds 	case IP_PKTINFO:
11501da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
11511da177e4SLinus Torvalds 		break;
11521da177e4SLinus Torvalds 	case IP_RECVTTL:
11531da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
11541da177e4SLinus Torvalds 		break;
11551da177e4SLinus Torvalds 	case IP_RECVTOS:
11561da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
11571da177e4SLinus Torvalds 		break;
11581da177e4SLinus Torvalds 	case IP_RECVOPTS:
11591da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
11601da177e4SLinus Torvalds 		break;
11611da177e4SLinus Torvalds 	case IP_RETOPTS:
11621da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
11631da177e4SLinus Torvalds 		break;
11642c7946a7SCatherine Zhang 	case IP_PASSSEC:
11652c7946a7SCatherine Zhang 		val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
11662c7946a7SCatherine Zhang 		break;
1167e8b2dfe9SBalazs Scheidler 	case IP_RECVORIGDSTADDR:
1168e8b2dfe9SBalazs Scheidler 		val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1169e8b2dfe9SBalazs Scheidler 		break;
11701da177e4SLinus Torvalds 	case IP_TOS:
11711da177e4SLinus Torvalds 		val = inet->tos;
11721da177e4SLinus Torvalds 		break;
11731da177e4SLinus Torvalds 	case IP_TTL:
11741da177e4SLinus Torvalds 		val = (inet->uc_ttl == -1 ?
11751da177e4SLinus Torvalds 		       sysctl_ip_default_ttl :
11761da177e4SLinus Torvalds 		       inet->uc_ttl);
11771da177e4SLinus Torvalds 		break;
11781da177e4SLinus Torvalds 	case IP_HDRINCL:
11791da177e4SLinus Torvalds 		val = inet->hdrincl;
11801da177e4SLinus Torvalds 		break;
1181a89b4763SMichael Kerrisk 	case IP_NODEFRAG:
1182a89b4763SMichael Kerrisk 		val = inet->nodefrag;
1183a89b4763SMichael Kerrisk 		break;
11841da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
11851da177e4SLinus Torvalds 		val = inet->pmtudisc;
11861da177e4SLinus Torvalds 		break;
11871da177e4SLinus Torvalds 	case IP_MTU:
11881da177e4SLinus Torvalds 	{
11891da177e4SLinus Torvalds 		struct dst_entry *dst;
11901da177e4SLinus Torvalds 		val = 0;
11911da177e4SLinus Torvalds 		dst = sk_dst_get(sk);
11921da177e4SLinus Torvalds 		if (dst) {
11931da177e4SLinus Torvalds 			val = dst_mtu(dst);
11941da177e4SLinus Torvalds 			dst_release(dst);
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 		if (!val) {
11971da177e4SLinus Torvalds 			release_sock(sk);
11981da177e4SLinus Torvalds 			return -ENOTCONN;
11991da177e4SLinus Torvalds 		}
12001da177e4SLinus Torvalds 		break;
12011da177e4SLinus Torvalds 	}
12021da177e4SLinus Torvalds 	case IP_RECVERR:
12031da177e4SLinus Torvalds 		val = inet->recverr;
12041da177e4SLinus Torvalds 		break;
12051da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
12061da177e4SLinus Torvalds 		val = inet->mc_ttl;
12071da177e4SLinus Torvalds 		break;
12081da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
12091da177e4SLinus Torvalds 		val = inet->mc_loop;
12101da177e4SLinus Torvalds 		break;
121176e21053SErich E. Hoover 	case IP_UNICAST_IF:
121276e21053SErich E. Hoover 		val = (__force int)htonl((__u32) inet->uc_index);
121376e21053SErich E. Hoover 		break;
12141da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
12151da177e4SLinus Torvalds 	{
12161da177e4SLinus Torvalds 		struct in_addr addr;
12171da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(struct in_addr));
12181da177e4SLinus Torvalds 		addr.s_addr = inet->mc_addr;
12191da177e4SLinus Torvalds 		release_sock(sk);
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 		if (put_user(len, optlen))
12221da177e4SLinus Torvalds 			return -EFAULT;
12231da177e4SLinus Torvalds 		if (copy_to_user(optval, &addr, len))
12241da177e4SLinus Torvalds 			return -EFAULT;
12251da177e4SLinus Torvalds 		return 0;
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 	case IP_MSFILTER:
12281da177e4SLinus Torvalds 	{
12291da177e4SLinus Torvalds 		struct ip_msfilter msf;
12301da177e4SLinus Torvalds 		int err;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 		if (len < IP_MSFILTER_SIZE(0)) {
12331da177e4SLinus Torvalds 			release_sock(sk);
12341da177e4SLinus Torvalds 			return -EINVAL;
12351da177e4SLinus Torvalds 		}
12361da177e4SLinus Torvalds 		if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
12371da177e4SLinus Torvalds 			release_sock(sk);
12381da177e4SLinus Torvalds 			return -EFAULT;
12391da177e4SLinus Torvalds 		}
12401da177e4SLinus Torvalds 		err = ip_mc_msfget(sk, &msf,
12411da177e4SLinus Torvalds 				   (struct ip_msfilter __user *)optval, optlen);
12421da177e4SLinus Torvalds 		release_sock(sk);
12431da177e4SLinus Torvalds 		return err;
12441da177e4SLinus Torvalds 	}
12451da177e4SLinus Torvalds 	case MCAST_MSFILTER:
12461da177e4SLinus Torvalds 	{
12471da177e4SLinus Torvalds 		struct group_filter gsf;
12481da177e4SLinus Torvalds 		int err;
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 		if (len < GROUP_FILTER_SIZE(0)) {
12511da177e4SLinus Torvalds 			release_sock(sk);
12521da177e4SLinus Torvalds 			return -EINVAL;
12531da177e4SLinus Torvalds 		}
12541da177e4SLinus Torvalds 		if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
12551da177e4SLinus Torvalds 			release_sock(sk);
12561da177e4SLinus Torvalds 			return -EFAULT;
12571da177e4SLinus Torvalds 		}
12581da177e4SLinus Torvalds 		err = ip_mc_gsfget(sk, &gsf,
12594d52cfbeSEric Dumazet 				   (struct group_filter __user *)optval,
12604d52cfbeSEric Dumazet 				   optlen);
12611da177e4SLinus Torvalds 		release_sock(sk);
12621da177e4SLinus Torvalds 		return err;
12631da177e4SLinus Torvalds 	}
1264f771bef9SNivedita Singhvi 	case IP_MULTICAST_ALL:
1265f771bef9SNivedita Singhvi 		val = inet->mc_all;
1266f771bef9SNivedita Singhvi 		break;
12671da177e4SLinus Torvalds 	case IP_PKTOPTIONS:
12681da177e4SLinus Torvalds 	{
12691da177e4SLinus Torvalds 		struct msghdr msg;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 		release_sock(sk);
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_STREAM)
12741da177e4SLinus Torvalds 			return -ENOPROTOOPT;
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 		msg.msg_control = optval;
12771da177e4SLinus Torvalds 		msg.msg_controllen = len;
1278dd23198eSDaniel Baluta 		msg.msg_flags = flags;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
12811da177e4SLinus Torvalds 			struct in_pktinfo info;
12821da177e4SLinus Torvalds 
1283c720c7e8SEric Dumazet 			info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1284c720c7e8SEric Dumazet 			info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
12851da177e4SLinus Torvalds 			info.ipi_ifindex = inet->mc_index;
12861da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
12871da177e4SLinus Torvalds 		}
12881da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_TTL) {
12891da177e4SLinus Torvalds 			int hlim = inet->mc_ttl;
12901da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
12911da177e4SLinus Torvalds 		}
12924c507d28SJiri Benc 		if (inet->cmsg_flags & IP_CMSG_TOS) {
12934c507d28SJiri Benc 			int tos = inet->rcv_tos;
12944c507d28SJiri Benc 			put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
12954c507d28SJiri Benc 		}
12961da177e4SLinus Torvalds 		len -= msg.msg_controllen;
12971da177e4SLinus Torvalds 		return put_user(len, optlen);
12981da177e4SLinus Torvalds 	}
12991da177e4SLinus Torvalds 	case IP_FREEBIND:
13001da177e4SLinus Torvalds 		val = inet->freebind;
13011da177e4SLinus Torvalds 		break;
1302f5715aeaSKOVACS Krisztian 	case IP_TRANSPARENT:
1303f5715aeaSKOVACS Krisztian 		val = inet->transparent;
1304f5715aeaSKOVACS Krisztian 		break;
1305d218d111SStephen Hemminger 	case IP_MINTTL:
1306d218d111SStephen Hemminger 		val = inet->min_ttl;
1307d218d111SStephen Hemminger 		break;
13081da177e4SLinus Torvalds 	default:
13091da177e4SLinus Torvalds 		release_sock(sk);
13101da177e4SLinus Torvalds 		return -ENOPROTOOPT;
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds 	release_sock(sk);
13131da177e4SLinus Torvalds 
1314951e07c9SDavid S. Miller 	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
13151da177e4SLinus Torvalds 		unsigned char ucval = (unsigned char)val;
13161da177e4SLinus Torvalds 		len = 1;
13171da177e4SLinus Torvalds 		if (put_user(len, optlen))
13181da177e4SLinus Torvalds 			return -EFAULT;
13191da177e4SLinus Torvalds 		if (copy_to_user(optval, &ucval, 1))
13201da177e4SLinus Torvalds 			return -EFAULT;
13211da177e4SLinus Torvalds 	} else {
13221da177e4SLinus Torvalds 		len = min_t(unsigned int, sizeof(int), len);
13231da177e4SLinus Torvalds 		if (put_user(len, optlen))
13241da177e4SLinus Torvalds 			return -EFAULT;
13251da177e4SLinus Torvalds 		if (copy_to_user(optval, &val, len))
13261da177e4SLinus Torvalds 			return -EFAULT;
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 	return 0;
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
13313fdadf7dSDmitry Mishin int ip_getsockopt(struct sock *sk, int level,
13323fdadf7dSDmitry Mishin 		  int optname, char __user *optval, int __user *optlen)
13333fdadf7dSDmitry Mishin {
13343fdadf7dSDmitry Mishin 	int err;
13353fdadf7dSDmitry Mishin 
1336dd23198eSDaniel Baluta 	err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
13373fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
13383fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
13396a9fb947SPavel Emelyanov 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
13406a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
13413fdadf7dSDmitry Mishin 		int len;
13423fdadf7dSDmitry Mishin 
13433fdadf7dSDmitry Mishin 		if (get_user(len, optlen))
13443fdadf7dSDmitry Mishin 			return -EFAULT;
13453fdadf7dSDmitry Mishin 
13463fdadf7dSDmitry Mishin 		lock_sock(sk);
13473fdadf7dSDmitry Mishin 		err = nf_getsockopt(sk, PF_INET, optname, optval,
13483fdadf7dSDmitry Mishin 				&len);
13493fdadf7dSDmitry Mishin 		release_sock(sk);
13503fdadf7dSDmitry Mishin 		if (err >= 0)
13513fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
13523fdadf7dSDmitry Mishin 		return err;
13533fdadf7dSDmitry Mishin 	}
13543fdadf7dSDmitry Mishin #endif
13553fdadf7dSDmitry Mishin 	return err;
13563fdadf7dSDmitry Mishin }
13574d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_getsockopt);
13583fdadf7dSDmitry Mishin 
13593fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
1360543d9cfeSArnaldo Carvalho de Melo int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1361543d9cfeSArnaldo Carvalho de Melo 			 char __user *optval, int __user *optlen)
13623fdadf7dSDmitry Mishin {
136342908c69SDavid L Stevens 	int err;
136442908c69SDavid L Stevens 
136542908c69SDavid L Stevens 	if (optname == MCAST_MSFILTER)
136642908c69SDavid L Stevens 		return compat_mc_getsockopt(sk, level, optname, optval, optlen,
136742908c69SDavid L Stevens 			ip_getsockopt);
136842908c69SDavid L Stevens 
1369dd23198eSDaniel Baluta 	err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1370dd23198eSDaniel Baluta 		MSG_CMSG_COMPAT);
137142908c69SDavid L Stevens 
13723fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
13733fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
13746a9fb947SPavel Emelyanov 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
13756a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
13763fdadf7dSDmitry Mishin 		int len;
13773fdadf7dSDmitry Mishin 
13783fdadf7dSDmitry Mishin 		if (get_user(len, optlen))
13793fdadf7dSDmitry Mishin 			return -EFAULT;
13803fdadf7dSDmitry Mishin 
13813fdadf7dSDmitry Mishin 		lock_sock(sk);
1382543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
13833fdadf7dSDmitry Mishin 		release_sock(sk);
13843fdadf7dSDmitry Mishin 		if (err >= 0)
13853fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
13863fdadf7dSDmitry Mishin 		return err;
13873fdadf7dSDmitry Mishin 	}
13883fdadf7dSDmitry Mishin #endif
13893fdadf7dSDmitry Mishin 	return err;
13903fdadf7dSDmitry Mishin }
1391543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_getsockopt);
13923fdadf7dSDmitry Mishin #endif
1393