xref: /linux/net/ipv4/ip_sockglue.c (revision 592fcb9dfafaa02dd0edc207bf5d3a0ee7a1f8df)
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>
361da177e4SLinus Torvalds #include <net/route.h>
371da177e4SLinus Torvalds #include <net/xfrm.h>
38dae50295SDavid L Stevens #include <net/compat.h>
391da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
401da177e4SLinus Torvalds #include <net/transp_v6.h>
411da177e4SLinus Torvalds #endif
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <linux/errqueue.h>
441da177e4SLinus Torvalds #include <asm/uaccess.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define IP_CMSG_PKTINFO		1
471da177e4SLinus Torvalds #define IP_CMSG_TTL		2
481da177e4SLinus Torvalds #define IP_CMSG_TOS		4
491da177e4SLinus Torvalds #define IP_CMSG_RECVOPTS	8
501da177e4SLinus Torvalds #define IP_CMSG_RETOPTS		16
512c7946a7SCatherine Zhang #define IP_CMSG_PASSSEC		32
52e8b2dfe9SBalazs Scheidler #define IP_CMSG_ORIGDSTADDR     64
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  *	SOL_IP control messages.
561da177e4SLinus Torvalds  */
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	struct in_pktinfo info;
61511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
621da177e4SLinus Torvalds 
63eddc9ec5SArnaldo Carvalho de Melo 	info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
641da177e4SLinus Torvalds 	if (rt) {
651da177e4SLinus Torvalds 		info.ipi_ifindex = rt->rt_iif;
661da177e4SLinus Torvalds 		info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
671da177e4SLinus Torvalds 	} else {
681da177e4SLinus Torvalds 		info.ipi_ifindex = 0;
691da177e4SLinus Torvalds 		info.ipi_spec_dst.s_addr = 0;
701da177e4SLinus Torvalds 	}
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
761da177e4SLinus Torvalds {
77eddc9ec5SArnaldo Carvalho de Melo 	int ttl = ip_hdr(skb)->ttl;
781da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
821da177e4SLinus Torvalds {
83eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
891da177e4SLinus Torvalds 		return;
901da177e4SLinus Torvalds 
91eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
92eddc9ec5SArnaldo Carvalho de Melo 		 ip_hdr(skb) + 1);
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	unsigned char optbuf[sizeof(struct ip_options) + 40];
991da177e4SLinus Torvalds 	struct ip_options * opt = (struct ip_options *)optbuf;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
1021da177e4SLinus Torvalds 		return;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	if (ip_options_echo(opt, skb)) {
1051da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
1061da177e4SLinus Torvalds 		return;
1071da177e4SLinus Torvalds 	}
1081da177e4SLinus Torvalds 	ip_options_undo(opt);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1132c7946a7SCatherine Zhang static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
1142c7946a7SCatherine Zhang {
1152c7946a7SCatherine Zhang 	char *secdata;
116dc49c1f9SCatherine Zhang 	u32 seclen, secid;
1172c7946a7SCatherine Zhang 	int err;
1182c7946a7SCatherine Zhang 
119dc49c1f9SCatherine Zhang 	err = security_socket_getpeersec_dgram(NULL, skb, &secid);
120dc49c1f9SCatherine Zhang 	if (err)
121dc49c1f9SCatherine Zhang 		return;
122dc49c1f9SCatherine Zhang 
123dc49c1f9SCatherine Zhang 	err = security_secid_to_secctx(secid, &secdata, &seclen);
1242c7946a7SCatherine Zhang 	if (err)
1252c7946a7SCatherine Zhang 		return;
1262c7946a7SCatherine Zhang 
1272c7946a7SCatherine Zhang 	put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
128dc49c1f9SCatherine Zhang 	security_release_secctx(secdata, seclen);
1292c7946a7SCatherine Zhang }
1302c7946a7SCatherine Zhang 
13121d1a161SHarvey Harrison static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
132e8b2dfe9SBalazs Scheidler {
133e8b2dfe9SBalazs Scheidler 	struct sockaddr_in sin;
134e8b2dfe9SBalazs Scheidler 	struct iphdr *iph = ip_hdr(skb);
13521d1a161SHarvey Harrison 	__be16 *ports = (__be16 *)skb_transport_header(skb);
136e8b2dfe9SBalazs Scheidler 
137e8b2dfe9SBalazs Scheidler 	if (skb_transport_offset(skb) + 4 > skb->len)
138e8b2dfe9SBalazs Scheidler 		return;
139e8b2dfe9SBalazs Scheidler 
140e8b2dfe9SBalazs Scheidler 	/* All current transport protocols have the port numbers in the
141e8b2dfe9SBalazs Scheidler 	 * first four bytes of the transport header and this function is
142e8b2dfe9SBalazs Scheidler 	 * written with this assumption in mind.
143e8b2dfe9SBalazs Scheidler 	 */
144e8b2dfe9SBalazs Scheidler 
145e8b2dfe9SBalazs Scheidler 	sin.sin_family = AF_INET;
146e8b2dfe9SBalazs Scheidler 	sin.sin_addr.s_addr = iph->daddr;
147e8b2dfe9SBalazs Scheidler 	sin.sin_port = ports[1];
148e8b2dfe9SBalazs Scheidler 	memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
149e8b2dfe9SBalazs Scheidler 
150e8b2dfe9SBalazs Scheidler 	put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
151e8b2dfe9SBalazs Scheidler }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(skb->sk);
1561da177e4SLinus Torvalds 	unsigned flags = inet->cmsg_flags;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	/* Ordered by supposed usage frequency */
1591da177e4SLinus Torvalds 	if (flags & 1)
1601da177e4SLinus Torvalds 		ip_cmsg_recv_pktinfo(msg, skb);
1611da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1621da177e4SLinus Torvalds 		return;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	if (flags & 1)
1651da177e4SLinus Torvalds 		ip_cmsg_recv_ttl(msg, skb);
1661da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1671da177e4SLinus Torvalds 		return;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	if (flags & 1)
1701da177e4SLinus Torvalds 		ip_cmsg_recv_tos(msg, skb);
1711da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1721da177e4SLinus Torvalds 		return;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	if (flags & 1)
1751da177e4SLinus Torvalds 		ip_cmsg_recv_opts(msg, skb);
1761da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1771da177e4SLinus Torvalds 		return;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	if (flags & 1)
1801da177e4SLinus Torvalds 		ip_cmsg_recv_retopts(msg, skb);
1812c7946a7SCatherine Zhang 	if ((flags >>= 1) == 0)
1822c7946a7SCatherine Zhang 		return;
1832c7946a7SCatherine Zhang 
1842c7946a7SCatherine Zhang 	if (flags & 1)
1852c7946a7SCatherine Zhang 		ip_cmsg_recv_security(msg, skb);
186e8b2dfe9SBalazs Scheidler 
187e8b2dfe9SBalazs Scheidler 	if ((flags >>= 1) == 0)
188e8b2dfe9SBalazs Scheidler 		return;
189e8b2dfe9SBalazs Scheidler 	if (flags & 1)
190e8b2dfe9SBalazs Scheidler 		ip_cmsg_recv_dstaddr(msg, skb);
191e8b2dfe9SBalazs Scheidler 
1921da177e4SLinus Torvalds }
1934d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_cmsg_recv);
1941da177e4SLinus Torvalds 
1957a6adb92SDenis V. Lunev int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	int err;
1981da177e4SLinus Torvalds 	struct cmsghdr *cmsg;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2011da177e4SLinus Torvalds 		if (!CMSG_OK(msg, cmsg))
2021da177e4SLinus Torvalds 			return -EINVAL;
2031da177e4SLinus Torvalds 		if (cmsg->cmsg_level != SOL_IP)
2041da177e4SLinus Torvalds 			continue;
2051da177e4SLinus Torvalds 		switch (cmsg->cmsg_type) {
2061da177e4SLinus Torvalds 		case IP_RETOPTS:
2071da177e4SLinus Torvalds 			err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
2084d52cfbeSEric Dumazet 			err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
2094d52cfbeSEric Dumazet 					     err < 40 ? err : 40);
2101da177e4SLinus Torvalds 			if (err)
2111da177e4SLinus Torvalds 				return err;
2121da177e4SLinus Torvalds 			break;
2131da177e4SLinus Torvalds 		case IP_PKTINFO:
2141da177e4SLinus Torvalds 		{
2151da177e4SLinus Torvalds 			struct in_pktinfo *info;
2161da177e4SLinus Torvalds 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
2171da177e4SLinus Torvalds 				return -EINVAL;
2181da177e4SLinus Torvalds 			info = (struct in_pktinfo *)CMSG_DATA(cmsg);
2191da177e4SLinus Torvalds 			ipc->oif = info->ipi_ifindex;
2201da177e4SLinus Torvalds 			ipc->addr = info->ipi_spec_dst.s_addr;
2211da177e4SLinus Torvalds 			break;
2221da177e4SLinus Torvalds 		}
2231da177e4SLinus Torvalds 		default:
2241da177e4SLinus Torvalds 			return -EINVAL;
2251da177e4SLinus Torvalds 		}
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 	return 0;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds /* Special input handler for packets caught by router alert option.
2321da177e4SLinus Torvalds    They are selected only by protocol field, and then processed likely
2331da177e4SLinus Torvalds    local ones; but only if someone wants them! Otherwise, router
2341da177e4SLinus Torvalds    not running rsvpd will kill RSVP.
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds    It is user level problem, what it will make with them.
2371da177e4SLinus Torvalds    I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
2381da177e4SLinus Torvalds    but receiver should be enough clever f.e. to forward mtrace requests,
2391da177e4SLinus Torvalds    sent to multicast group to reach destination designated router.
2401da177e4SLinus Torvalds  */
2411da177e4SLinus Torvalds struct ip_ra_chain *ip_ra_chain;
24266018506SEric Dumazet static DEFINE_SPINLOCK(ip_ra_lock);
24366018506SEric Dumazet 
244*592fcb9dSEric Dumazet 
245*592fcb9dSEric Dumazet static void ip_ra_destroy_rcu(struct rcu_head *head)
24666018506SEric Dumazet {
247*592fcb9dSEric Dumazet 	struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
248*592fcb9dSEric Dumazet 
249*592fcb9dSEric Dumazet 	sock_put(ra->saved_sk);
250*592fcb9dSEric Dumazet 	kfree(ra);
25166018506SEric Dumazet }
2521da177e4SLinus Torvalds 
2534d52cfbeSEric Dumazet int ip_ra_control(struct sock *sk, unsigned char on,
2544d52cfbeSEric Dumazet 		  void (*destructor)(struct sock *))
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct ip_ra_chain *ra, *new_ra, **rap;
2571da177e4SLinus Torvalds 
258c720c7e8SEric Dumazet 	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
2591da177e4SLinus Torvalds 		return -EINVAL;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
2621da177e4SLinus Torvalds 
26366018506SEric Dumazet 	spin_lock_bh(&ip_ra_lock);
2641da177e4SLinus Torvalds 	for (rap = &ip_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
2651da177e4SLinus Torvalds 		if (ra->sk == sk) {
2661da177e4SLinus Torvalds 			if (on) {
26766018506SEric Dumazet 				spin_unlock_bh(&ip_ra_lock);
2681da177e4SLinus Torvalds 				kfree(new_ra);
2691da177e4SLinus Torvalds 				return -EADDRINUSE;
2701da177e4SLinus Torvalds 			}
271*592fcb9dSEric Dumazet 			/* dont let ip_call_ra_chain() use sk again */
272*592fcb9dSEric Dumazet 			ra->sk = NULL;
27366018506SEric Dumazet 			rcu_assign_pointer(*rap, ra->next);
27466018506SEric Dumazet 			spin_unlock_bh(&ip_ra_lock);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 			if (ra->destructor)
2771da177e4SLinus Torvalds 				ra->destructor(sk);
278*592fcb9dSEric Dumazet 			/*
279*592fcb9dSEric Dumazet 			 * Delay sock_put(sk) and kfree(ra) after one rcu grace
280*592fcb9dSEric Dumazet 			 * period. This guarantee ip_call_ra_chain() dont need
281*592fcb9dSEric Dumazet 			 * to mess with socket refcounts.
282*592fcb9dSEric Dumazet 			 */
283*592fcb9dSEric Dumazet 			ra->saved_sk = sk;
284*592fcb9dSEric Dumazet 			call_rcu(&ra->rcu, ip_ra_destroy_rcu);
2851da177e4SLinus Torvalds 			return 0;
2861da177e4SLinus Torvalds 		}
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 	if (new_ra == NULL) {
28966018506SEric Dumazet 		spin_unlock_bh(&ip_ra_lock);
2901da177e4SLinus Torvalds 		return -ENOBUFS;
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 	new_ra->sk = sk;
2931da177e4SLinus Torvalds 	new_ra->destructor = destructor;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	new_ra->next = ra;
29666018506SEric Dumazet 	rcu_assign_pointer(*rap, new_ra);
2971da177e4SLinus Torvalds 	sock_hold(sk);
29866018506SEric Dumazet 	spin_unlock_bh(&ip_ra_lock);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	return 0;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
30435986b32SAl Viro 		   __be16 port, u32 info, u8 *payload)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	skb = skb_clone(skb, GFP_ATOMIC);
3091da177e4SLinus Torvalds 	if (!skb)
3101da177e4SLinus Torvalds 		return;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3131da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
3141da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
31588c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_type = icmp_hdr(skb)->type;
31688c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_code = icmp_hdr(skb)->code;
3171da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
3181da177e4SLinus Torvalds 	serr->ee.ee_info = info;
3191da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
32088c7664fSArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
321d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb);
3221da177e4SLinus Torvalds 	serr->port = port;
3231da177e4SLinus Torvalds 
324bd82393cSArnaldo Carvalho de Melo 	if (skb_pull(skb, payload - skb->data) != NULL) {
325bd82393cSArnaldo Carvalho de Melo 		skb_reset_transport_header(skb);
326bd82393cSArnaldo Carvalho de Melo 		if (sock_queue_err_skb(sk, skb) == 0)
327bd82393cSArnaldo Carvalho de Melo 			return;
328bd82393cSArnaldo Carvalho de Melo 	}
3291da177e4SLinus Torvalds 	kfree_skb(skb);
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3320579016eSAl Viro void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3351da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3361da177e4SLinus Torvalds 	struct iphdr *iph;
3371da177e4SLinus Torvalds 	struct sk_buff *skb;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	if (!inet->recverr)
3401da177e4SLinus Torvalds 		return;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
3431da177e4SLinus Torvalds 	if (!skb)
3441da177e4SLinus Torvalds 		return;
3451da177e4SLinus Torvalds 
3462ca9e6f2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr));
3472ca9e6f2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
348eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
3491da177e4SLinus Torvalds 	iph->daddr = daddr;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3521da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
3531da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
3541da177e4SLinus Torvalds 	serr->ee.ee_type = 0;
3551da177e4SLinus Torvalds 	serr->ee.ee_code = 0;
3561da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
3571da177e4SLinus Torvalds 	serr->ee.ee_info = info;
3581da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
359d56f90a7SArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
3601da177e4SLinus Torvalds 	serr->port = port;
3611da177e4SLinus Torvalds 
36227a884dcSArnaldo Carvalho de Melo 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
363bd82393cSArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	if (sock_queue_err_skb(sk, skb))
3661da177e4SLinus Torvalds 		kfree_skb(skb);
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds /*
3701da177e4SLinus Torvalds  *	Handle MSG_ERRQUEUE
3711da177e4SLinus Torvalds  */
3721da177e4SLinus Torvalds int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3751da177e4SLinus Torvalds 	struct sk_buff *skb, *skb2;
3761da177e4SLinus Torvalds 	struct sockaddr_in *sin;
3771da177e4SLinus Torvalds 	struct {
3781da177e4SLinus Torvalds 		struct sock_extended_err ee;
3791da177e4SLinus Torvalds 		struct sockaddr_in	 offender;
3801da177e4SLinus Torvalds 	} errhdr;
3811da177e4SLinus Torvalds 	int err;
3821da177e4SLinus Torvalds 	int copied;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	err = -EAGAIN;
3851da177e4SLinus Torvalds 	skb = skb_dequeue(&sk->sk_error_queue);
3861da177e4SLinus Torvalds 	if (skb == NULL)
3871da177e4SLinus Torvalds 		goto out;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	copied = skb->len;
3901da177e4SLinus Torvalds 	if (copied > len) {
3911da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
3921da177e4SLinus Torvalds 		copied = len;
3931da177e4SLinus Torvalds 	}
3941da177e4SLinus Torvalds 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3951da177e4SLinus Torvalds 	if (err)
3961da177e4SLinus Torvalds 		goto out_free_skb;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	sock_recv_timestamp(msg, sk, skb);
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	sin = (struct sockaddr_in *)msg->msg_name;
4031da177e4SLinus Torvalds 	if (sin) {
4041da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
405d56f90a7SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
406d56f90a7SArnaldo Carvalho de Melo 						   serr->addr_offset);
4071da177e4SLinus Torvalds 		sin->sin_port = serr->port;
4081da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
4121da177e4SLinus Torvalds 	sin = &errhdr.offender;
4131da177e4SLinus Torvalds 	sin->sin_family = AF_UNSPEC;
4141da177e4SLinus Torvalds 	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
4151da177e4SLinus Torvalds 		struct inet_sock *inet = inet_sk(sk);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
418eddc9ec5SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
4191da177e4SLinus Torvalds 		sin->sin_port = 0;
4201da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4211da177e4SLinus Torvalds 		if (inet->cmsg_flags)
4221da177e4SLinus Torvalds 			ip_cmsg_recv(msg, skb);
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/* Now we could try to dump offended packet options */
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	msg->msg_flags |= MSG_ERRQUEUE;
4301da177e4SLinus Torvalds 	err = copied;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	/* Reset and regenerate socket error */
433e0f9f858SHerbert Xu 	spin_lock_bh(&sk->sk_error_queue.lock);
4341da177e4SLinus Torvalds 	sk->sk_err = 0;
4354d52cfbeSEric Dumazet 	skb2 = skb_peek(&sk->sk_error_queue);
4364d52cfbeSEric Dumazet 	if (skb2 != NULL) {
4371da177e4SLinus Torvalds 		sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
438e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
4391da177e4SLinus Torvalds 		sk->sk_error_report(sk);
4401da177e4SLinus Torvalds 	} else
441e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds out_free_skb:
4441da177e4SLinus Torvalds 	kfree_skb(skb);
4451da177e4SLinus Torvalds out:
4461da177e4SLinus Torvalds 	return err;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds /*
4514d52cfbeSEric Dumazet  *	Socket option code for IP. This is the end of the line after any
4524d52cfbeSEric Dumazet  *	TCP,UDP etc options on an IP socket.
4531da177e4SLinus Torvalds  */
4541da177e4SLinus Torvalds 
4553fdadf7dSDmitry Mishin static int do_ip_setsockopt(struct sock *sk, int level,
456b7058842SDavid S. Miller 			    int optname, char __user *optval, unsigned int optlen)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4591da177e4SLinus Torvalds 	int val = 0, err;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
4621da177e4SLinus Torvalds 			     (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
4631da177e4SLinus Torvalds 			     (1<<IP_RETOPTS) | (1<<IP_TOS) |
4641da177e4SLinus Torvalds 			     (1<<IP_TTL) | (1<<IP_HDRINCL) |
4651da177e4SLinus Torvalds 			     (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
4662c7946a7SCatherine Zhang 			     (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
467d218d111SStephen Hemminger 			     (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) |
468d218d111SStephen Hemminger 			     (1<<IP_MINTTL))) ||
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 	{
4961da177e4SLinus Torvalds 		struct ip_options *opt = NULL;
49765a1c4ffSroel kluin 		if (optlen > 40)
4981da177e4SLinus Torvalds 			goto e_inval;
4993b1e0a65SYOSHIFUJI Hideaki 		err = ip_options_get_from_user(sock_net(sk), &opt,
500cb84663eSDenis V. Lunev 					       optval, optlen);
5011da177e4SLinus Torvalds 		if (err)
5021da177e4SLinus Torvalds 			break;
503d83d8461SArnaldo Carvalho de Melo 		if (inet->is_icsk) {
504d83d8461SArnaldo Carvalho de Melo 			struct inet_connection_sock *icsk = inet_csk(sk);
5051da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5061da177e4SLinus Torvalds 			if (sk->sk_family == PF_INET ||
5071da177e4SLinus Torvalds 			    (!((1 << sk->sk_state) &
5081da177e4SLinus Torvalds 			       (TCPF_LISTEN | TCPF_CLOSE)) &&
509c720c7e8SEric Dumazet 			     inet->inet_daddr != LOOPBACK4_IPV6)) {
5101da177e4SLinus Torvalds #endif
5111da177e4SLinus Torvalds 				if (inet->opt)
512d83d8461SArnaldo Carvalho de Melo 					icsk->icsk_ext_hdr_len -= inet->opt->optlen;
5131da177e4SLinus Torvalds 				if (opt)
514d83d8461SArnaldo Carvalho de Melo 					icsk->icsk_ext_hdr_len += opt->optlen;
515d83d8461SArnaldo Carvalho de Melo 				icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
5161da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5171da177e4SLinus Torvalds 			}
5181da177e4SLinus Torvalds #endif
5191da177e4SLinus Torvalds 		}
5201da177e4SLinus Torvalds 		opt = xchg(&inet->opt, opt);
5211da177e4SLinus Torvalds 		kfree(opt);
5221da177e4SLinus Torvalds 		break;
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds 	case IP_PKTINFO:
5251da177e4SLinus Torvalds 		if (val)
5261da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_PKTINFO;
5271da177e4SLinus Torvalds 		else
5281da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
5291da177e4SLinus Torvalds 		break;
5301da177e4SLinus Torvalds 	case IP_RECVTTL:
5311da177e4SLinus Torvalds 		if (val)
5321da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TTL;
5331da177e4SLinus Torvalds 		else
5341da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TTL;
5351da177e4SLinus Torvalds 		break;
5361da177e4SLinus Torvalds 	case IP_RECVTOS:
5371da177e4SLinus Torvalds 		if (val)
5381da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TOS;
5391da177e4SLinus Torvalds 		else
5401da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TOS;
5411da177e4SLinus Torvalds 		break;
5421da177e4SLinus Torvalds 	case IP_RECVOPTS:
5431da177e4SLinus Torvalds 		if (val)
5441da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_RECVOPTS;
5451da177e4SLinus Torvalds 		else
5461da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
5471da177e4SLinus Torvalds 		break;
5481da177e4SLinus Torvalds 	case IP_RETOPTS:
5491da177e4SLinus Torvalds 		if (val)
5501da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_RETOPTS;
5511da177e4SLinus Torvalds 		else
5521da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
5531da177e4SLinus Torvalds 		break;
5542c7946a7SCatherine Zhang 	case IP_PASSSEC:
5552c7946a7SCatherine Zhang 		if (val)
5562c7946a7SCatherine Zhang 			inet->cmsg_flags |= IP_CMSG_PASSSEC;
5572c7946a7SCatherine Zhang 		else
5582c7946a7SCatherine Zhang 			inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
5592c7946a7SCatherine Zhang 		break;
560e8b2dfe9SBalazs Scheidler 	case IP_RECVORIGDSTADDR:
561e8b2dfe9SBalazs Scheidler 		if (val)
562e8b2dfe9SBalazs Scheidler 			inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
563e8b2dfe9SBalazs Scheidler 		else
564e8b2dfe9SBalazs Scheidler 			inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
565e8b2dfe9SBalazs Scheidler 		break;
5661da177e4SLinus Torvalds 	case IP_TOS:	/* This sets both TOS and Precedence */
5671da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM) {
5681da177e4SLinus Torvalds 			val &= ~3;
5691da177e4SLinus Torvalds 			val |= inet->tos & 3;
5701da177e4SLinus Torvalds 		}
5711da177e4SLinus Torvalds 		if (inet->tos != val) {
5721da177e4SLinus Torvalds 			inet->tos = val;
5731da177e4SLinus Torvalds 			sk->sk_priority = rt_tos2priority(val);
5741da177e4SLinus Torvalds 			sk_dst_reset(sk);
5751da177e4SLinus Torvalds 		}
5761da177e4SLinus Torvalds 		break;
5771da177e4SLinus Torvalds 	case IP_TTL:
5781da177e4SLinus Torvalds 		if (optlen < 1)
5791da177e4SLinus Torvalds 			goto e_inval;
5804d52cfbeSEric Dumazet 		if (val != -1 && (val < 0 || val > 255))
5811da177e4SLinus Torvalds 			goto e_inval;
5821da177e4SLinus Torvalds 		inet->uc_ttl = val;
5831da177e4SLinus Torvalds 		break;
5841da177e4SLinus Torvalds 	case IP_HDRINCL:
5851da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_RAW) {
5861da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
5871da177e4SLinus Torvalds 			break;
5881da177e4SLinus Torvalds 		}
5891da177e4SLinus Torvalds 		inet->hdrincl = val ? 1 : 0;
5901da177e4SLinus Torvalds 		break;
5911da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
5920eae750eSJohn Dykstra 		if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
5931da177e4SLinus Torvalds 			goto e_inval;
5941da177e4SLinus Torvalds 		inet->pmtudisc = val;
5951da177e4SLinus Torvalds 		break;
5961da177e4SLinus Torvalds 	case IP_RECVERR:
5971da177e4SLinus Torvalds 		inet->recverr = !!val;
5981da177e4SLinus Torvalds 		if (!val)
5991da177e4SLinus Torvalds 			skb_queue_purge(&sk->sk_error_queue);
6001da177e4SLinus Torvalds 		break;
6011da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
6021da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
6031da177e4SLinus Torvalds 			goto e_inval;
6041da177e4SLinus Torvalds 		if (optlen < 1)
6051da177e4SLinus Torvalds 			goto e_inval;
6061da177e4SLinus Torvalds 		if (val == -1)
6071da177e4SLinus Torvalds 			val = 1;
6081da177e4SLinus Torvalds 		if (val < 0 || val > 255)
6091da177e4SLinus Torvalds 			goto e_inval;
6101da177e4SLinus Torvalds 		inet->mc_ttl = val;
6111da177e4SLinus Torvalds 		break;
6121da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
6131da177e4SLinus Torvalds 		if (optlen < 1)
6141da177e4SLinus Torvalds 			goto e_inval;
6151da177e4SLinus Torvalds 		inet->mc_loop = !!val;
6161da177e4SLinus Torvalds 		break;
6171da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
6181da177e4SLinus Torvalds 	{
6191da177e4SLinus Torvalds 		struct ip_mreqn mreq;
6201da177e4SLinus Torvalds 		struct net_device *dev = NULL;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
6231da177e4SLinus Torvalds 			goto e_inval;
6241da177e4SLinus Torvalds 		/*
6251da177e4SLinus Torvalds 		 *	Check the arguments are allowable
6261da177e4SLinus Torvalds 		 */
6271da177e4SLinus Torvalds 
6280915921bSShan Wei 		if (optlen < sizeof(struct in_addr))
6290915921bSShan Wei 			goto e_inval;
6300915921bSShan Wei 
6311da177e4SLinus Torvalds 		err = -EFAULT;
6321da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
6331da177e4SLinus Torvalds 			if (copy_from_user(&mreq, optval, sizeof(mreq)))
6341da177e4SLinus Torvalds 				break;
6351da177e4SLinus Torvalds 		} else {
6361da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
6371da177e4SLinus Torvalds 			if (optlen >= sizeof(struct in_addr) &&
6384d52cfbeSEric Dumazet 			    copy_from_user(&mreq.imr_address, optval,
6394d52cfbeSEric Dumazet 					   sizeof(struct in_addr)))
6401da177e4SLinus Torvalds 				break;
6411da177e4SLinus Torvalds 		}
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 		if (!mreq.imr_ifindex) {
644e6f1cebfSAl Viro 			if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
6451da177e4SLinus Torvalds 				inet->mc_index = 0;
6461da177e4SLinus Torvalds 				inet->mc_addr  = 0;
6471da177e4SLinus Torvalds 				err = 0;
6481da177e4SLinus Torvalds 				break;
6491da177e4SLinus Torvalds 			}
6503b1e0a65SYOSHIFUJI Hideaki 			dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
65155b80503SEric Dumazet 			if (dev)
6521da177e4SLinus Torvalds 				mreq.imr_ifindex = dev->ifindex;
6531da177e4SLinus Torvalds 		} else
65455b80503SEric Dumazet 			dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
6581da177e4SLinus Torvalds 		if (!dev)
6591da177e4SLinus Torvalds 			break;
66055b80503SEric Dumazet 		dev_put(dev);
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 		err = -EINVAL;
6631da177e4SLinus Torvalds 		if (sk->sk_bound_dev_if &&
6641da177e4SLinus Torvalds 		    mreq.imr_ifindex != sk->sk_bound_dev_if)
6651da177e4SLinus Torvalds 			break;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 		inet->mc_index = mreq.imr_ifindex;
6681da177e4SLinus Torvalds 		inet->mc_addr  = mreq.imr_address.s_addr;
6691da177e4SLinus Torvalds 		err = 0;
6701da177e4SLinus Torvalds 		break;
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	case IP_ADD_MEMBERSHIP:
6741da177e4SLinus Torvalds 	case IP_DROP_MEMBERSHIP:
6751da177e4SLinus Torvalds 	{
6761da177e4SLinus Torvalds 		struct ip_mreqn mreq;
6771da177e4SLinus Torvalds 
678a96fb49bSFlavio Leitner 		err = -EPROTO;
679a96fb49bSFlavio Leitner 		if (inet_sk(sk)->is_icsk)
680a96fb49bSFlavio Leitner 			break;
681a96fb49bSFlavio Leitner 
6821da177e4SLinus Torvalds 		if (optlen < sizeof(struct ip_mreq))
6831da177e4SLinus Torvalds 			goto e_inval;
6841da177e4SLinus Torvalds 		err = -EFAULT;
6851da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
6861da177e4SLinus Torvalds 			if (copy_from_user(&mreq, optval, sizeof(mreq)))
6871da177e4SLinus Torvalds 				break;
6881da177e4SLinus Torvalds 		} else {
6891da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
6901da177e4SLinus Torvalds 			if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
6911da177e4SLinus Torvalds 				break;
6921da177e4SLinus Torvalds 		}
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 		if (optname == IP_ADD_MEMBERSHIP)
6951da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
6961da177e4SLinus Torvalds 		else
6971da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
6981da177e4SLinus Torvalds 		break;
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 	case IP_MSFILTER:
7011da177e4SLinus Torvalds 	{
7021da177e4SLinus Torvalds 		struct ip_msfilter *msf;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 		if (optlen < IP_MSFILTER_SIZE(0))
7051da177e4SLinus Torvalds 			goto e_inval;
7061da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
7071da177e4SLinus Torvalds 			err = -ENOBUFS;
7081da177e4SLinus Torvalds 			break;
7091da177e4SLinus Torvalds 		}
7108b3a7005SKris Katterjohn 		msf = kmalloc(optlen, GFP_KERNEL);
711cfcabdccSStephen Hemminger 		if (!msf) {
7121da177e4SLinus Torvalds 			err = -ENOBUFS;
7131da177e4SLinus Torvalds 			break;
7141da177e4SLinus Torvalds 		}
7151da177e4SLinus Torvalds 		err = -EFAULT;
7161da177e4SLinus Torvalds 		if (copy_from_user(msf, optval, optlen)) {
7171da177e4SLinus Torvalds 			kfree(msf);
7181da177e4SLinus Torvalds 			break;
7191da177e4SLinus Torvalds 		}
7201da177e4SLinus Torvalds 		/* numsrc >= (1G-4) overflow in 32 bits */
7211da177e4SLinus Torvalds 		if (msf->imsf_numsrc >= 0x3ffffffcU ||
7221da177e4SLinus Torvalds 		    msf->imsf_numsrc > sysctl_igmp_max_msf) {
7231da177e4SLinus Torvalds 			kfree(msf);
7241da177e4SLinus Torvalds 			err = -ENOBUFS;
7251da177e4SLinus Torvalds 			break;
7261da177e4SLinus Torvalds 		}
7271da177e4SLinus Torvalds 		if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
7281da177e4SLinus Torvalds 			kfree(msf);
7291da177e4SLinus Torvalds 			err = -EINVAL;
7301da177e4SLinus Torvalds 			break;
7311da177e4SLinus Torvalds 		}
7321da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, 0);
7331da177e4SLinus Torvalds 		kfree(msf);
7341da177e4SLinus Torvalds 		break;
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds 	case IP_BLOCK_SOURCE:
7371da177e4SLinus Torvalds 	case IP_UNBLOCK_SOURCE:
7381da177e4SLinus Torvalds 	case IP_ADD_SOURCE_MEMBERSHIP:
7391da177e4SLinus Torvalds 	case IP_DROP_SOURCE_MEMBERSHIP:
7401da177e4SLinus Torvalds 	{
7411da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
7421da177e4SLinus Torvalds 		int omode, add;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 		if (optlen != sizeof(struct ip_mreq_source))
7451da177e4SLinus Torvalds 			goto e_inval;
7461da177e4SLinus Torvalds 		if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
7471da177e4SLinus Torvalds 			err = -EFAULT;
7481da177e4SLinus Torvalds 			break;
7491da177e4SLinus Torvalds 		}
7501da177e4SLinus Torvalds 		if (optname == IP_BLOCK_SOURCE) {
7511da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7521da177e4SLinus Torvalds 			add = 1;
7531da177e4SLinus Torvalds 		} else if (optname == IP_UNBLOCK_SOURCE) {
7541da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7551da177e4SLinus Torvalds 			add = 0;
7561da177e4SLinus Torvalds 		} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
7571da177e4SLinus Torvalds 			struct ip_mreqn mreq;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 			mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
7601da177e4SLinus Torvalds 			mreq.imr_address.s_addr = mreqs.imr_interface;
7611da177e4SLinus Torvalds 			mreq.imr_ifindex = 0;
7621da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7638cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
7641da177e4SLinus Torvalds 				break;
7651da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7661da177e4SLinus Torvalds 			add = 1;
7671da177e4SLinus Torvalds 		} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
7681da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7691da177e4SLinus Torvalds 			add = 0;
7701da177e4SLinus Torvalds 		}
7711da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs, 0);
7721da177e4SLinus Torvalds 		break;
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 	case MCAST_JOIN_GROUP:
7751da177e4SLinus Torvalds 	case MCAST_LEAVE_GROUP:
7761da177e4SLinus Torvalds 	{
7771da177e4SLinus Torvalds 		struct group_req greq;
7781da177e4SLinus Torvalds 		struct sockaddr_in *psin;
7791da177e4SLinus Torvalds 		struct ip_mreqn mreq;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 		if (optlen < sizeof(struct group_req))
7821da177e4SLinus Torvalds 			goto e_inval;
7831da177e4SLinus Torvalds 		err = -EFAULT;
7841da177e4SLinus Torvalds 		if (copy_from_user(&greq, optval, sizeof(greq)))
7851da177e4SLinus Torvalds 			break;
7861da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greq.gr_group;
7871da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET)
7881da177e4SLinus Torvalds 			goto e_inval;
7891da177e4SLinus Torvalds 		memset(&mreq, 0, sizeof(mreq));
7901da177e4SLinus Torvalds 		mreq.imr_multiaddr = psin->sin_addr;
7911da177e4SLinus Torvalds 		mreq.imr_ifindex = greq.gr_interface;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 		if (optname == MCAST_JOIN_GROUP)
7941da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7951da177e4SLinus Torvalds 		else
7961da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
7971da177e4SLinus Torvalds 		break;
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds 	case MCAST_JOIN_SOURCE_GROUP:
8001da177e4SLinus Torvalds 	case MCAST_LEAVE_SOURCE_GROUP:
8011da177e4SLinus Torvalds 	case MCAST_BLOCK_SOURCE:
8021da177e4SLinus Torvalds 	case MCAST_UNBLOCK_SOURCE:
8031da177e4SLinus Torvalds 	{
8041da177e4SLinus Torvalds 		struct group_source_req greqs;
8051da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
8061da177e4SLinus Torvalds 		struct sockaddr_in *psin;
8071da177e4SLinus Torvalds 		int omode, add;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 		if (optlen != sizeof(struct group_source_req))
8101da177e4SLinus Torvalds 			goto e_inval;
8111da177e4SLinus Torvalds 		if (copy_from_user(&greqs, optval, sizeof(greqs))) {
8121da177e4SLinus Torvalds 			err = -EFAULT;
8131da177e4SLinus Torvalds 			break;
8141da177e4SLinus Torvalds 		}
8151da177e4SLinus Torvalds 		if (greqs.gsr_group.ss_family != AF_INET ||
8161da177e4SLinus Torvalds 		    greqs.gsr_source.ss_family != AF_INET) {
8171da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
8181da177e4SLinus Torvalds 			break;
8191da177e4SLinus Torvalds 		}
8201da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_group;
8211da177e4SLinus Torvalds 		mreqs.imr_multiaddr = psin->sin_addr.s_addr;
8221da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_source;
8231da177e4SLinus Torvalds 		mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
8241da177e4SLinus Torvalds 		mreqs.imr_interface = 0; /* use index for mc_source */
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 		if (optname == MCAST_BLOCK_SOURCE) {
8271da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
8281da177e4SLinus Torvalds 			add = 1;
8291da177e4SLinus Torvalds 		} else if (optname == MCAST_UNBLOCK_SOURCE) {
8301da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
8311da177e4SLinus Torvalds 			add = 0;
8321da177e4SLinus Torvalds 		} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
8331da177e4SLinus Torvalds 			struct ip_mreqn mreq;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&greqs.gsr_group;
8361da177e4SLinus Torvalds 			mreq.imr_multiaddr = psin->sin_addr;
8371da177e4SLinus Torvalds 			mreq.imr_address.s_addr = 0;
8381da177e4SLinus Torvalds 			mreq.imr_ifindex = greqs.gsr_interface;
8391da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
8408cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
8411da177e4SLinus Torvalds 				break;
8421da177e4SLinus Torvalds 			greqs.gsr_interface = mreq.imr_ifindex;
8431da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
8441da177e4SLinus Torvalds 			add = 1;
8451da177e4SLinus Torvalds 		} else /* MCAST_LEAVE_SOURCE_GROUP */ {
8461da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
8471da177e4SLinus Torvalds 			add = 0;
8481da177e4SLinus Torvalds 		}
8491da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs,
8501da177e4SLinus Torvalds 				   greqs.gsr_interface);
8511da177e4SLinus Torvalds 		break;
8521da177e4SLinus Torvalds 	}
8531da177e4SLinus Torvalds 	case MCAST_MSFILTER:
8541da177e4SLinus Torvalds 	{
8551da177e4SLinus Torvalds 		struct sockaddr_in *psin;
8561da177e4SLinus Torvalds 		struct ip_msfilter *msf = NULL;
8571da177e4SLinus Torvalds 		struct group_filter *gsf = NULL;
8581da177e4SLinus Torvalds 		int msize, i, ifindex;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 		if (optlen < GROUP_FILTER_SIZE(0))
8611da177e4SLinus Torvalds 			goto e_inval;
8621da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
8631da177e4SLinus Torvalds 			err = -ENOBUFS;
8641da177e4SLinus Torvalds 			break;
8651da177e4SLinus Torvalds 		}
8668b3a7005SKris Katterjohn 		gsf = kmalloc(optlen, GFP_KERNEL);
867cfcabdccSStephen Hemminger 		if (!gsf) {
8681da177e4SLinus Torvalds 			err = -ENOBUFS;
8691da177e4SLinus Torvalds 			break;
8701da177e4SLinus Torvalds 		}
8711da177e4SLinus Torvalds 		err = -EFAULT;
8724d52cfbeSEric Dumazet 		if (copy_from_user(gsf, optval, optlen))
8731da177e4SLinus Torvalds 			goto mc_msf_out;
8744d52cfbeSEric Dumazet 
8751da177e4SLinus Torvalds 		/* numsrc >= (4G-140)/128 overflow in 32 bits */
8761da177e4SLinus Torvalds 		if (gsf->gf_numsrc >= 0x1ffffff ||
8771da177e4SLinus Torvalds 		    gsf->gf_numsrc > sysctl_igmp_max_msf) {
8781da177e4SLinus Torvalds 			err = -ENOBUFS;
8791da177e4SLinus Torvalds 			goto mc_msf_out;
8801da177e4SLinus Torvalds 		}
8811da177e4SLinus Torvalds 		if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
8821da177e4SLinus Torvalds 			err = -EINVAL;
8831da177e4SLinus Torvalds 			goto mc_msf_out;
8841da177e4SLinus Torvalds 		}
8851da177e4SLinus Torvalds 		msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
8868b3a7005SKris Katterjohn 		msf = kmalloc(msize, GFP_KERNEL);
887cfcabdccSStephen Hemminger 		if (!msf) {
8881da177e4SLinus Torvalds 			err = -ENOBUFS;
8891da177e4SLinus Torvalds 			goto mc_msf_out;
8901da177e4SLinus Torvalds 		}
8911da177e4SLinus Torvalds 		ifindex = gsf->gf_interface;
8921da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&gsf->gf_group;
8931da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET) {
8941da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
8951da177e4SLinus Torvalds 			goto mc_msf_out;
8961da177e4SLinus Torvalds 		}
8971da177e4SLinus Torvalds 		msf->imsf_multiaddr = psin->sin_addr.s_addr;
8981da177e4SLinus Torvalds 		msf->imsf_interface = 0;
8991da177e4SLinus Torvalds 		msf->imsf_fmode = gsf->gf_fmode;
9001da177e4SLinus Torvalds 		msf->imsf_numsrc = gsf->gf_numsrc;
9011da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
9021da177e4SLinus Torvalds 		for (i = 0; i < gsf->gf_numsrc; ++i) {
9031da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&gsf->gf_slist[i];
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 			if (psin->sin_family != AF_INET)
9061da177e4SLinus Torvalds 				goto mc_msf_out;
9071da177e4SLinus Torvalds 			msf->imsf_slist[i] = psin->sin_addr.s_addr;
9081da177e4SLinus Torvalds 		}
9091da177e4SLinus Torvalds 		kfree(gsf);
9101da177e4SLinus Torvalds 		gsf = NULL;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, ifindex);
9131da177e4SLinus Torvalds mc_msf_out:
9141da177e4SLinus Torvalds 		kfree(msf);
9151da177e4SLinus Torvalds 		kfree(gsf);
9161da177e4SLinus Torvalds 		break;
9171da177e4SLinus Torvalds 	}
918f771bef9SNivedita Singhvi 	case IP_MULTICAST_ALL:
919f771bef9SNivedita Singhvi 		if (optlen < 1)
920f771bef9SNivedita Singhvi 			goto e_inval;
921f771bef9SNivedita Singhvi 		if (val != 0 && val != 1)
922f771bef9SNivedita Singhvi 			goto e_inval;
923f771bef9SNivedita Singhvi 		inet->mc_all = val;
924f771bef9SNivedita Singhvi 		break;
9251da177e4SLinus Torvalds 	case IP_ROUTER_ALERT:
9261da177e4SLinus Torvalds 		err = ip_ra_control(sk, val ? 1 : 0, NULL);
9271da177e4SLinus Torvalds 		break;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	case IP_FREEBIND:
9301da177e4SLinus Torvalds 		if (optlen < 1)
9311da177e4SLinus Torvalds 			goto e_inval;
9321da177e4SLinus Torvalds 		inet->freebind = !!val;
9331da177e4SLinus Torvalds 		break;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	case IP_IPSEC_POLICY:
9361da177e4SLinus Torvalds 	case IP_XFRM_POLICY:
9376fc0b4a7SHerbert Xu 		err = -EPERM;
9386fc0b4a7SHerbert Xu 		if (!capable(CAP_NET_ADMIN))
9396fc0b4a7SHerbert Xu 			break;
9401da177e4SLinus Torvalds 		err = xfrm_user_policy(sk, optname, optval, optlen);
9411da177e4SLinus Torvalds 		break;
9421da177e4SLinus Torvalds 
943f5715aeaSKOVACS Krisztian 	case IP_TRANSPARENT:
944f5715aeaSKOVACS Krisztian 		if (!capable(CAP_NET_ADMIN)) {
945f5715aeaSKOVACS Krisztian 			err = -EPERM;
946f5715aeaSKOVACS Krisztian 			break;
947f5715aeaSKOVACS Krisztian 		}
948f5715aeaSKOVACS Krisztian 		if (optlen < 1)
949f5715aeaSKOVACS Krisztian 			goto e_inval;
950f5715aeaSKOVACS Krisztian 		inet->transparent = !!val;
951f5715aeaSKOVACS Krisztian 		break;
952f5715aeaSKOVACS Krisztian 
953d218d111SStephen Hemminger 	case IP_MINTTL:
954d218d111SStephen Hemminger 		if (optlen < 1)
955d218d111SStephen Hemminger 			goto e_inval;
956d218d111SStephen Hemminger 		if (val < 0 || val > 255)
957d218d111SStephen Hemminger 			goto e_inval;
958d218d111SStephen Hemminger 		inet->min_ttl = val;
959d218d111SStephen Hemminger 		break;
960d218d111SStephen Hemminger 
9611da177e4SLinus Torvalds 	default:
9621da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
9631da177e4SLinus Torvalds 		break;
9641da177e4SLinus Torvalds 	}
9651da177e4SLinus Torvalds 	release_sock(sk);
9661da177e4SLinus Torvalds 	return err;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds e_inval:
9691da177e4SLinus Torvalds 	release_sock(sk);
9701da177e4SLinus Torvalds 	return -EINVAL;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
973f84af32cSEric Dumazet /**
974f84af32cSEric Dumazet  * ip_queue_rcv_skb - Queue an skb into sock receive queue
975f84af32cSEric Dumazet  * @sk: socket
976f84af32cSEric Dumazet  * @skb: buffer
977f84af32cSEric Dumazet  *
978f84af32cSEric Dumazet  * Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option
979f84af32cSEric Dumazet  * is not set, we drop skb dst entry now, while dst cache line is hot.
980f84af32cSEric Dumazet  */
981f84af32cSEric Dumazet int ip_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
982f84af32cSEric Dumazet {
983f84af32cSEric Dumazet 	if (!(inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO))
984f84af32cSEric Dumazet 		skb_dst_drop(skb);
985f84af32cSEric Dumazet 	return sock_queue_rcv_skb(sk, skb);
986f84af32cSEric Dumazet }
987f84af32cSEric Dumazet EXPORT_SYMBOL(ip_queue_rcv_skb);
988f84af32cSEric Dumazet 
9893fdadf7dSDmitry Mishin int ip_setsockopt(struct sock *sk, int level,
990b7058842SDavid S. Miller 		int optname, char __user *optval, unsigned int optlen)
9913fdadf7dSDmitry Mishin {
9923fdadf7dSDmitry Mishin 	int err;
9933fdadf7dSDmitry Mishin 
9943fdadf7dSDmitry Mishin 	if (level != SOL_IP)
9953fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
9963fdadf7dSDmitry Mishin 
9973fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
9983fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
9993fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
10003fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
10016a9fb947SPavel Emelyanov 			optname != IP_IPSEC_POLICY &&
10026a9fb947SPavel Emelyanov 			optname != IP_XFRM_POLICY &&
10036a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
10043fdadf7dSDmitry Mishin 		lock_sock(sk);
10053fdadf7dSDmitry Mishin 		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
10063fdadf7dSDmitry Mishin 		release_sock(sk);
10073fdadf7dSDmitry Mishin 	}
10083fdadf7dSDmitry Mishin #endif
10093fdadf7dSDmitry Mishin 	return err;
10103fdadf7dSDmitry Mishin }
10114d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_setsockopt);
10123fdadf7dSDmitry Mishin 
10133fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
1014543d9cfeSArnaldo Carvalho de Melo int compat_ip_setsockopt(struct sock *sk, int level, int optname,
1015b7058842SDavid S. Miller 			 char __user *optval, unsigned int optlen)
10163fdadf7dSDmitry Mishin {
10173fdadf7dSDmitry Mishin 	int err;
10183fdadf7dSDmitry Mishin 
10193fdadf7dSDmitry Mishin 	if (level != SOL_IP)
10203fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
10213fdadf7dSDmitry Mishin 
1022dae50295SDavid L Stevens 	if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1023dae50295SDavid L Stevens 		return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1024dae50295SDavid L Stevens 			ip_setsockopt);
1025dae50295SDavid L Stevens 
10263fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
10273fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
10283fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
10293fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
10306a9fb947SPavel Emelyanov 			optname != IP_IPSEC_POLICY &&
10316a9fb947SPavel Emelyanov 			optname != IP_XFRM_POLICY &&
10326a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
10333fdadf7dSDmitry Mishin 		lock_sock(sk);
1034543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_setsockopt(sk, PF_INET, optname,
1035543d9cfeSArnaldo Carvalho de Melo 					   optval, optlen);
10363fdadf7dSDmitry Mishin 		release_sock(sk);
10373fdadf7dSDmitry Mishin 	}
10383fdadf7dSDmitry Mishin #endif
10393fdadf7dSDmitry Mishin 	return err;
10403fdadf7dSDmitry Mishin }
1041543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_setsockopt);
10423fdadf7dSDmitry Mishin #endif
10433fdadf7dSDmitry Mishin 
10441da177e4SLinus Torvalds /*
10454d52cfbeSEric Dumazet  *	Get the options. Note for future reference. The GET of IP options gets
10464d52cfbeSEric Dumazet  *	the _received_ ones. The set sets the _sent_ ones.
10471da177e4SLinus Torvalds  */
10481da177e4SLinus Torvalds 
10493fdadf7dSDmitry Mishin static int do_ip_getsockopt(struct sock *sk, int level, int optname,
10503fdadf7dSDmitry Mishin 			    char __user *optval, int __user *optlen)
10511da177e4SLinus Torvalds {
10521da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
10531da177e4SLinus Torvalds 	int val;
10541da177e4SLinus Torvalds 	int len;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	if (level != SOL_IP)
10571da177e4SLinus Torvalds 		return -EOPNOTSUPP;
10581da177e4SLinus Torvalds 
10596a9fb947SPavel Emelyanov 	if (ip_mroute_opt(optname))
10601da177e4SLinus Torvalds 		return ip_mroute_getsockopt(sk, optname, optval, optlen);
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	if (get_user(len, optlen))
10631da177e4SLinus Torvalds 		return -EFAULT;
10641da177e4SLinus Torvalds 	if (len < 0)
10651da177e4SLinus Torvalds 		return -EINVAL;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	lock_sock(sk);
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	switch (optname) {
10701da177e4SLinus Torvalds 	case IP_OPTIONS:
10711da177e4SLinus Torvalds 	{
10721da177e4SLinus Torvalds 		unsigned char optbuf[sizeof(struct ip_options)+40];
10731da177e4SLinus Torvalds 		struct ip_options * opt = (struct ip_options *)optbuf;
10741da177e4SLinus Torvalds 		opt->optlen = 0;
10751da177e4SLinus Torvalds 		if (inet->opt)
10761da177e4SLinus Torvalds 			memcpy(optbuf, inet->opt,
10771da177e4SLinus Torvalds 			       sizeof(struct ip_options)+
10781da177e4SLinus Torvalds 			       inet->opt->optlen);
10791da177e4SLinus Torvalds 		release_sock(sk);
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 		if (opt->optlen == 0)
10821da177e4SLinus Torvalds 			return put_user(0, optlen);
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 		ip_options_undo(opt);
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 		len = min_t(unsigned int, len, opt->optlen);
10871da177e4SLinus Torvalds 		if (put_user(len, optlen))
10881da177e4SLinus Torvalds 			return -EFAULT;
10891da177e4SLinus Torvalds 		if (copy_to_user(optval, opt->__data, len))
10901da177e4SLinus Torvalds 			return -EFAULT;
10911da177e4SLinus Torvalds 		return 0;
10921da177e4SLinus Torvalds 	}
10931da177e4SLinus Torvalds 	case IP_PKTINFO:
10941da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
10951da177e4SLinus Torvalds 		break;
10961da177e4SLinus Torvalds 	case IP_RECVTTL:
10971da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
10981da177e4SLinus Torvalds 		break;
10991da177e4SLinus Torvalds 	case IP_RECVTOS:
11001da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
11011da177e4SLinus Torvalds 		break;
11021da177e4SLinus Torvalds 	case IP_RECVOPTS:
11031da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
11041da177e4SLinus Torvalds 		break;
11051da177e4SLinus Torvalds 	case IP_RETOPTS:
11061da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
11071da177e4SLinus Torvalds 		break;
11082c7946a7SCatherine Zhang 	case IP_PASSSEC:
11092c7946a7SCatherine Zhang 		val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
11102c7946a7SCatherine Zhang 		break;
1111e8b2dfe9SBalazs Scheidler 	case IP_RECVORIGDSTADDR:
1112e8b2dfe9SBalazs Scheidler 		val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1113e8b2dfe9SBalazs Scheidler 		break;
11141da177e4SLinus Torvalds 	case IP_TOS:
11151da177e4SLinus Torvalds 		val = inet->tos;
11161da177e4SLinus Torvalds 		break;
11171da177e4SLinus Torvalds 	case IP_TTL:
11181da177e4SLinus Torvalds 		val = (inet->uc_ttl == -1 ?
11191da177e4SLinus Torvalds 		       sysctl_ip_default_ttl :
11201da177e4SLinus Torvalds 		       inet->uc_ttl);
11211da177e4SLinus Torvalds 		break;
11221da177e4SLinus Torvalds 	case IP_HDRINCL:
11231da177e4SLinus Torvalds 		val = inet->hdrincl;
11241da177e4SLinus Torvalds 		break;
11251da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
11261da177e4SLinus Torvalds 		val = inet->pmtudisc;
11271da177e4SLinus Torvalds 		break;
11281da177e4SLinus Torvalds 	case IP_MTU:
11291da177e4SLinus Torvalds 	{
11301da177e4SLinus Torvalds 		struct dst_entry *dst;
11311da177e4SLinus Torvalds 		val = 0;
11321da177e4SLinus Torvalds 		dst = sk_dst_get(sk);
11331da177e4SLinus Torvalds 		if (dst) {
11341da177e4SLinus Torvalds 			val = dst_mtu(dst);
11351da177e4SLinus Torvalds 			dst_release(dst);
11361da177e4SLinus Torvalds 		}
11371da177e4SLinus Torvalds 		if (!val) {
11381da177e4SLinus Torvalds 			release_sock(sk);
11391da177e4SLinus Torvalds 			return -ENOTCONN;
11401da177e4SLinus Torvalds 		}
11411da177e4SLinus Torvalds 		break;
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 	case IP_RECVERR:
11441da177e4SLinus Torvalds 		val = inet->recverr;
11451da177e4SLinus Torvalds 		break;
11461da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
11471da177e4SLinus Torvalds 		val = inet->mc_ttl;
11481da177e4SLinus Torvalds 		break;
11491da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
11501da177e4SLinus Torvalds 		val = inet->mc_loop;
11511da177e4SLinus Torvalds 		break;
11521da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
11531da177e4SLinus Torvalds 	{
11541da177e4SLinus Torvalds 		struct in_addr addr;
11551da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(struct in_addr));
11561da177e4SLinus Torvalds 		addr.s_addr = inet->mc_addr;
11571da177e4SLinus Torvalds 		release_sock(sk);
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 		if (put_user(len, optlen))
11601da177e4SLinus Torvalds 			return -EFAULT;
11611da177e4SLinus Torvalds 		if (copy_to_user(optval, &addr, len))
11621da177e4SLinus Torvalds 			return -EFAULT;
11631da177e4SLinus Torvalds 		return 0;
11641da177e4SLinus Torvalds 	}
11651da177e4SLinus Torvalds 	case IP_MSFILTER:
11661da177e4SLinus Torvalds 	{
11671da177e4SLinus Torvalds 		struct ip_msfilter msf;
11681da177e4SLinus Torvalds 		int err;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		if (len < IP_MSFILTER_SIZE(0)) {
11711da177e4SLinus Torvalds 			release_sock(sk);
11721da177e4SLinus Torvalds 			return -EINVAL;
11731da177e4SLinus Torvalds 		}
11741da177e4SLinus Torvalds 		if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
11751da177e4SLinus Torvalds 			release_sock(sk);
11761da177e4SLinus Torvalds 			return -EFAULT;
11771da177e4SLinus Torvalds 		}
11781da177e4SLinus Torvalds 		err = ip_mc_msfget(sk, &msf,
11791da177e4SLinus Torvalds 				   (struct ip_msfilter __user *)optval, optlen);
11801da177e4SLinus Torvalds 		release_sock(sk);
11811da177e4SLinus Torvalds 		return err;
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 	case MCAST_MSFILTER:
11841da177e4SLinus Torvalds 	{
11851da177e4SLinus Torvalds 		struct group_filter gsf;
11861da177e4SLinus Torvalds 		int err;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 		if (len < GROUP_FILTER_SIZE(0)) {
11891da177e4SLinus Torvalds 			release_sock(sk);
11901da177e4SLinus Torvalds 			return -EINVAL;
11911da177e4SLinus Torvalds 		}
11921da177e4SLinus Torvalds 		if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
11931da177e4SLinus Torvalds 			release_sock(sk);
11941da177e4SLinus Torvalds 			return -EFAULT;
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 		err = ip_mc_gsfget(sk, &gsf,
11974d52cfbeSEric Dumazet 				   (struct group_filter __user *)optval,
11984d52cfbeSEric Dumazet 				   optlen);
11991da177e4SLinus Torvalds 		release_sock(sk);
12001da177e4SLinus Torvalds 		return err;
12011da177e4SLinus Torvalds 	}
1202f771bef9SNivedita Singhvi 	case IP_MULTICAST_ALL:
1203f771bef9SNivedita Singhvi 		val = inet->mc_all;
1204f771bef9SNivedita Singhvi 		break;
12051da177e4SLinus Torvalds 	case IP_PKTOPTIONS:
12061da177e4SLinus Torvalds 	{
12071da177e4SLinus Torvalds 		struct msghdr msg;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 		release_sock(sk);
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_STREAM)
12121da177e4SLinus Torvalds 			return -ENOPROTOOPT;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 		msg.msg_control = optval;
12151da177e4SLinus Torvalds 		msg.msg_controllen = len;
12161da177e4SLinus Torvalds 		msg.msg_flags = 0;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
12191da177e4SLinus Torvalds 			struct in_pktinfo info;
12201da177e4SLinus Torvalds 
1221c720c7e8SEric Dumazet 			info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1222c720c7e8SEric Dumazet 			info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
12231da177e4SLinus Torvalds 			info.ipi_ifindex = inet->mc_index;
12241da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
12251da177e4SLinus Torvalds 		}
12261da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_TTL) {
12271da177e4SLinus Torvalds 			int hlim = inet->mc_ttl;
12281da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
12291da177e4SLinus Torvalds 		}
12301da177e4SLinus Torvalds 		len -= msg.msg_controllen;
12311da177e4SLinus Torvalds 		return put_user(len, optlen);
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 	case IP_FREEBIND:
12341da177e4SLinus Torvalds 		val = inet->freebind;
12351da177e4SLinus Torvalds 		break;
1236f5715aeaSKOVACS Krisztian 	case IP_TRANSPARENT:
1237f5715aeaSKOVACS Krisztian 		val = inet->transparent;
1238f5715aeaSKOVACS Krisztian 		break;
1239d218d111SStephen Hemminger 	case IP_MINTTL:
1240d218d111SStephen Hemminger 		val = inet->min_ttl;
1241d218d111SStephen Hemminger 		break;
12421da177e4SLinus Torvalds 	default:
12431da177e4SLinus Torvalds 		release_sock(sk);
12441da177e4SLinus Torvalds 		return -ENOPROTOOPT;
12451da177e4SLinus Torvalds 	}
12461da177e4SLinus Torvalds 	release_sock(sk);
12471da177e4SLinus Torvalds 
1248951e07c9SDavid S. Miller 	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
12491da177e4SLinus Torvalds 		unsigned char ucval = (unsigned char)val;
12501da177e4SLinus Torvalds 		len = 1;
12511da177e4SLinus Torvalds 		if (put_user(len, optlen))
12521da177e4SLinus Torvalds 			return -EFAULT;
12531da177e4SLinus Torvalds 		if (copy_to_user(optval, &ucval, 1))
12541da177e4SLinus Torvalds 			return -EFAULT;
12551da177e4SLinus Torvalds 	} else {
12561da177e4SLinus Torvalds 		len = min_t(unsigned int, sizeof(int), len);
12571da177e4SLinus Torvalds 		if (put_user(len, optlen))
12581da177e4SLinus Torvalds 			return -EFAULT;
12591da177e4SLinus Torvalds 		if (copy_to_user(optval, &val, len))
12601da177e4SLinus Torvalds 			return -EFAULT;
12611da177e4SLinus Torvalds 	}
12621da177e4SLinus Torvalds 	return 0;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12653fdadf7dSDmitry Mishin int ip_getsockopt(struct sock *sk, int level,
12663fdadf7dSDmitry Mishin 		  int optname, char __user *optval, int __user *optlen)
12673fdadf7dSDmitry Mishin {
12683fdadf7dSDmitry Mishin 	int err;
12693fdadf7dSDmitry Mishin 
12703fdadf7dSDmitry Mishin 	err = do_ip_getsockopt(sk, level, optname, optval, optlen);
12713fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
12723fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
12736a9fb947SPavel Emelyanov 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
12746a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
12753fdadf7dSDmitry Mishin 		int len;
12763fdadf7dSDmitry Mishin 
12773fdadf7dSDmitry Mishin 		if (get_user(len, optlen))
12783fdadf7dSDmitry Mishin 			return -EFAULT;
12793fdadf7dSDmitry Mishin 
12803fdadf7dSDmitry Mishin 		lock_sock(sk);
12813fdadf7dSDmitry Mishin 		err = nf_getsockopt(sk, PF_INET, optname, optval,
12823fdadf7dSDmitry Mishin 				&len);
12833fdadf7dSDmitry Mishin 		release_sock(sk);
12843fdadf7dSDmitry Mishin 		if (err >= 0)
12853fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
12863fdadf7dSDmitry Mishin 		return err;
12873fdadf7dSDmitry Mishin 	}
12883fdadf7dSDmitry Mishin #endif
12893fdadf7dSDmitry Mishin 	return err;
12903fdadf7dSDmitry Mishin }
12914d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_getsockopt);
12923fdadf7dSDmitry Mishin 
12933fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
1294543d9cfeSArnaldo Carvalho de Melo int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1295543d9cfeSArnaldo Carvalho de Melo 			 char __user *optval, int __user *optlen)
12963fdadf7dSDmitry Mishin {
129742908c69SDavid L Stevens 	int err;
129842908c69SDavid L Stevens 
129942908c69SDavid L Stevens 	if (optname == MCAST_MSFILTER)
130042908c69SDavid L Stevens 		return compat_mc_getsockopt(sk, level, optname, optval, optlen,
130142908c69SDavid L Stevens 			ip_getsockopt);
130242908c69SDavid L Stevens 
130342908c69SDavid L Stevens 	err = do_ip_getsockopt(sk, level, optname, optval, optlen);
130442908c69SDavid L Stevens 
13053fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
13063fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
13076a9fb947SPavel Emelyanov 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
13086a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
13093fdadf7dSDmitry Mishin 		int len;
13103fdadf7dSDmitry Mishin 
13113fdadf7dSDmitry Mishin 		if (get_user(len, optlen))
13123fdadf7dSDmitry Mishin 			return -EFAULT;
13133fdadf7dSDmitry Mishin 
13143fdadf7dSDmitry Mishin 		lock_sock(sk);
1315543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
13163fdadf7dSDmitry Mishin 		release_sock(sk);
13173fdadf7dSDmitry Mishin 		if (err >= 0)
13183fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
13193fdadf7dSDmitry Mishin 		return err;
13203fdadf7dSDmitry Mishin 	}
13213fdadf7dSDmitry Mishin #endif
13223fdadf7dSDmitry Mishin 	return err;
13233fdadf7dSDmitry Mishin }
1324543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_getsockopt);
13253fdadf7dSDmitry Mishin #endif
1326