xref: /linux/net/ipv4/ip_sockglue.c (revision d826eb14ecef3574b6b3be55e5f4329f4a76fbf3)
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>
401da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
411da177e4SLinus Torvalds #include <net/transp_v6.h>
421da177e4SLinus Torvalds #endif
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/errqueue.h>
451da177e4SLinus Torvalds #include <asm/uaccess.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #define IP_CMSG_PKTINFO		1
481da177e4SLinus Torvalds #define IP_CMSG_TTL		2
491da177e4SLinus Torvalds #define IP_CMSG_TOS		4
501da177e4SLinus Torvalds #define IP_CMSG_RECVOPTS	8
511da177e4SLinus Torvalds #define IP_CMSG_RETOPTS		16
522c7946a7SCatherine Zhang #define IP_CMSG_PASSSEC		32
53e8b2dfe9SBalazs Scheidler #define IP_CMSG_ORIGDSTADDR     64
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  *	SOL_IP control messages.
571da177e4SLinus Torvalds  */
58*d826eb14SEric Dumazet #define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb))
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
611da177e4SLinus Torvalds {
62*d826eb14SEric Dumazet 	struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
631da177e4SLinus Torvalds 
64eddc9ec5SArnaldo Carvalho de Melo 	info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
701da177e4SLinus Torvalds {
71eddc9ec5SArnaldo Carvalho de Melo 	int ttl = ip_hdr(skb)->ttl;
721da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
761da177e4SLinus Torvalds {
77eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
831da177e4SLinus Torvalds 		return;
841da177e4SLinus Torvalds 
85eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
86eddc9ec5SArnaldo Carvalho de Melo 		 ip_hdr(skb) + 1);
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	unsigned char optbuf[sizeof(struct ip_options) + 40];
931da177e4SLinus Torvalds 	struct ip_options * opt = (struct ip_options *)optbuf;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
961da177e4SLinus Torvalds 		return;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	if (ip_options_echo(opt, skb)) {
991da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
1001da177e4SLinus Torvalds 		return;
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds 	ip_options_undo(opt);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1072c7946a7SCatherine Zhang static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
1082c7946a7SCatherine Zhang {
1092c7946a7SCatherine Zhang 	char *secdata;
110dc49c1f9SCatherine Zhang 	u32 seclen, secid;
1112c7946a7SCatherine Zhang 	int err;
1122c7946a7SCatherine Zhang 
113dc49c1f9SCatherine Zhang 	err = security_socket_getpeersec_dgram(NULL, skb, &secid);
114dc49c1f9SCatherine Zhang 	if (err)
115dc49c1f9SCatherine Zhang 		return;
116dc49c1f9SCatherine Zhang 
117dc49c1f9SCatherine Zhang 	err = security_secid_to_secctx(secid, &secdata, &seclen);
1182c7946a7SCatherine Zhang 	if (err)
1192c7946a7SCatherine Zhang 		return;
1202c7946a7SCatherine Zhang 
1212c7946a7SCatherine Zhang 	put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
122dc49c1f9SCatherine Zhang 	security_release_secctx(secdata, seclen);
1232c7946a7SCatherine Zhang }
1242c7946a7SCatherine Zhang 
12521d1a161SHarvey Harrison static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
126e8b2dfe9SBalazs Scheidler {
127e8b2dfe9SBalazs Scheidler 	struct sockaddr_in sin;
128b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
12921d1a161SHarvey Harrison 	__be16 *ports = (__be16 *)skb_transport_header(skb);
130e8b2dfe9SBalazs Scheidler 
131e8b2dfe9SBalazs Scheidler 	if (skb_transport_offset(skb) + 4 > skb->len)
132e8b2dfe9SBalazs Scheidler 		return;
133e8b2dfe9SBalazs Scheidler 
134e8b2dfe9SBalazs Scheidler 	/* All current transport protocols have the port numbers in the
135e8b2dfe9SBalazs Scheidler 	 * first four bytes of the transport header and this function is
136e8b2dfe9SBalazs Scheidler 	 * written with this assumption in mind.
137e8b2dfe9SBalazs Scheidler 	 */
138e8b2dfe9SBalazs Scheidler 
139e8b2dfe9SBalazs Scheidler 	sin.sin_family = AF_INET;
140e8b2dfe9SBalazs Scheidler 	sin.sin_addr.s_addr = iph->daddr;
141e8b2dfe9SBalazs Scheidler 	sin.sin_port = ports[1];
142e8b2dfe9SBalazs Scheidler 	memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
143e8b2dfe9SBalazs Scheidler 
144e8b2dfe9SBalazs Scheidler 	put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
145e8b2dfe9SBalazs Scheidler }
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(skb->sk);
1501da177e4SLinus Torvalds 	unsigned flags = inet->cmsg_flags;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	/* Ordered by supposed usage frequency */
1531da177e4SLinus Torvalds 	if (flags & 1)
1541da177e4SLinus Torvalds 		ip_cmsg_recv_pktinfo(msg, skb);
1551da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1561da177e4SLinus Torvalds 		return;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	if (flags & 1)
1591da177e4SLinus Torvalds 		ip_cmsg_recv_ttl(msg, skb);
1601da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1611da177e4SLinus Torvalds 		return;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	if (flags & 1)
1641da177e4SLinus Torvalds 		ip_cmsg_recv_tos(msg, skb);
1651da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1661da177e4SLinus Torvalds 		return;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	if (flags & 1)
1691da177e4SLinus Torvalds 		ip_cmsg_recv_opts(msg, skb);
1701da177e4SLinus Torvalds 	if ((flags >>= 1) == 0)
1711da177e4SLinus Torvalds 		return;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	if (flags & 1)
1741da177e4SLinus Torvalds 		ip_cmsg_recv_retopts(msg, skb);
1752c7946a7SCatherine Zhang 	if ((flags >>= 1) == 0)
1762c7946a7SCatherine Zhang 		return;
1772c7946a7SCatherine Zhang 
1782c7946a7SCatherine Zhang 	if (flags & 1)
1792c7946a7SCatherine Zhang 		ip_cmsg_recv_security(msg, skb);
180e8b2dfe9SBalazs Scheidler 
181e8b2dfe9SBalazs Scheidler 	if ((flags >>= 1) == 0)
182e8b2dfe9SBalazs Scheidler 		return;
183e8b2dfe9SBalazs Scheidler 	if (flags & 1)
184e8b2dfe9SBalazs Scheidler 		ip_cmsg_recv_dstaddr(msg, skb);
185e8b2dfe9SBalazs Scheidler 
1861da177e4SLinus Torvalds }
1874d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_cmsg_recv);
1881da177e4SLinus Torvalds 
1897a6adb92SDenis V. Lunev int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	int err;
1921da177e4SLinus Torvalds 	struct cmsghdr *cmsg;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1951da177e4SLinus Torvalds 		if (!CMSG_OK(msg, cmsg))
1961da177e4SLinus Torvalds 			return -EINVAL;
1971da177e4SLinus Torvalds 		if (cmsg->cmsg_level != SOL_IP)
1981da177e4SLinus Torvalds 			continue;
1991da177e4SLinus Torvalds 		switch (cmsg->cmsg_type) {
2001da177e4SLinus Torvalds 		case IP_RETOPTS:
2011da177e4SLinus Torvalds 			err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
2024d52cfbeSEric Dumazet 			err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
2034d52cfbeSEric Dumazet 					     err < 40 ? err : 40);
2041da177e4SLinus Torvalds 			if (err)
2051da177e4SLinus Torvalds 				return err;
2061da177e4SLinus Torvalds 			break;
2071da177e4SLinus Torvalds 		case IP_PKTINFO:
2081da177e4SLinus Torvalds 		{
2091da177e4SLinus Torvalds 			struct in_pktinfo *info;
2101da177e4SLinus Torvalds 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
2111da177e4SLinus Torvalds 				return -EINVAL;
2121da177e4SLinus Torvalds 			info = (struct in_pktinfo *)CMSG_DATA(cmsg);
2131da177e4SLinus Torvalds 			ipc->oif = info->ipi_ifindex;
2141da177e4SLinus Torvalds 			ipc->addr = info->ipi_spec_dst.s_addr;
2151da177e4SLinus Torvalds 			break;
2161da177e4SLinus Torvalds 		}
2171da177e4SLinus Torvalds 		default:
2181da177e4SLinus Torvalds 			return -EINVAL;
2191da177e4SLinus Torvalds 		}
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 	return 0;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds /* Special input handler for packets caught by router alert option.
2261da177e4SLinus Torvalds    They are selected only by protocol field, and then processed likely
2271da177e4SLinus Torvalds    local ones; but only if someone wants them! Otherwise, router
2281da177e4SLinus Torvalds    not running rsvpd will kill RSVP.
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds    It is user level problem, what it will make with them.
2311da177e4SLinus Torvalds    I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
2321da177e4SLinus Torvalds    but receiver should be enough clever f.e. to forward mtrace requests,
2331da177e4SLinus Torvalds    sent to multicast group to reach destination designated router.
2341da177e4SLinus Torvalds  */
23543a951e9SEric Dumazet struct ip_ra_chain __rcu *ip_ra_chain;
23666018506SEric Dumazet static DEFINE_SPINLOCK(ip_ra_lock);
23766018506SEric Dumazet 
238592fcb9dSEric Dumazet 
239592fcb9dSEric Dumazet static void ip_ra_destroy_rcu(struct rcu_head *head)
24066018506SEric Dumazet {
241592fcb9dSEric Dumazet 	struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
242592fcb9dSEric Dumazet 
243592fcb9dSEric Dumazet 	sock_put(ra->saved_sk);
244592fcb9dSEric Dumazet 	kfree(ra);
24566018506SEric Dumazet }
2461da177e4SLinus Torvalds 
2474d52cfbeSEric Dumazet int ip_ra_control(struct sock *sk, unsigned char on,
2484d52cfbeSEric Dumazet 		  void (*destructor)(struct sock *))
2491da177e4SLinus Torvalds {
25043a951e9SEric Dumazet 	struct ip_ra_chain *ra, *new_ra;
25143a951e9SEric Dumazet 	struct ip_ra_chain __rcu **rap;
2521da177e4SLinus Torvalds 
253c720c7e8SEric Dumazet 	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
2541da177e4SLinus Torvalds 		return -EINVAL;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
2571da177e4SLinus Torvalds 
25866018506SEric Dumazet 	spin_lock_bh(&ip_ra_lock);
25943a951e9SEric Dumazet 	for (rap = &ip_ra_chain;
26043a951e9SEric Dumazet 	     (ra = rcu_dereference_protected(*rap,
26143a951e9SEric Dumazet 			lockdep_is_held(&ip_ra_lock))) != NULL;
26243a951e9SEric Dumazet 	     rap = &ra->next) {
2631da177e4SLinus Torvalds 		if (ra->sk == sk) {
2641da177e4SLinus Torvalds 			if (on) {
26566018506SEric Dumazet 				spin_unlock_bh(&ip_ra_lock);
2661da177e4SLinus Torvalds 				kfree(new_ra);
2671da177e4SLinus Torvalds 				return -EADDRINUSE;
2681da177e4SLinus Torvalds 			}
269592fcb9dSEric Dumazet 			/* dont let ip_call_ra_chain() use sk again */
270592fcb9dSEric Dumazet 			ra->sk = NULL;
27166018506SEric Dumazet 			rcu_assign_pointer(*rap, ra->next);
27266018506SEric Dumazet 			spin_unlock_bh(&ip_ra_lock);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 			if (ra->destructor)
2751da177e4SLinus Torvalds 				ra->destructor(sk);
276592fcb9dSEric Dumazet 			/*
277592fcb9dSEric Dumazet 			 * Delay sock_put(sk) and kfree(ra) after one rcu grace
278592fcb9dSEric Dumazet 			 * period. This guarantee ip_call_ra_chain() dont need
279592fcb9dSEric Dumazet 			 * to mess with socket refcounts.
280592fcb9dSEric Dumazet 			 */
281592fcb9dSEric Dumazet 			ra->saved_sk = sk;
282592fcb9dSEric Dumazet 			call_rcu(&ra->rcu, ip_ra_destroy_rcu);
2831da177e4SLinus Torvalds 			return 0;
2841da177e4SLinus Torvalds 		}
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 	if (new_ra == NULL) {
28766018506SEric Dumazet 		spin_unlock_bh(&ip_ra_lock);
2881da177e4SLinus Torvalds 		return -ENOBUFS;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 	new_ra->sk = sk;
2911da177e4SLinus Torvalds 	new_ra->destructor = destructor;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	new_ra->next = ra;
29466018506SEric Dumazet 	rcu_assign_pointer(*rap, new_ra);
2951da177e4SLinus Torvalds 	sock_hold(sk);
29666018506SEric Dumazet 	spin_unlock_bh(&ip_ra_lock);
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	return 0;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
30235986b32SAl Viro 		   __be16 port, u32 info, u8 *payload)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	skb = skb_clone(skb, GFP_ATOMIC);
3071da177e4SLinus Torvalds 	if (!skb)
3081da177e4SLinus Torvalds 		return;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3111da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
3121da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
31388c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_type = icmp_hdr(skb)->type;
31488c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_code = icmp_hdr(skb)->code;
3151da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
3161da177e4SLinus Torvalds 	serr->ee.ee_info = info;
3171da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
31888c7664fSArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
319d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb);
3201da177e4SLinus Torvalds 	serr->port = port;
3211da177e4SLinus Torvalds 
322bd82393cSArnaldo Carvalho de Melo 	if (skb_pull(skb, payload - skb->data) != NULL) {
323bd82393cSArnaldo Carvalho de Melo 		skb_reset_transport_header(skb);
324bd82393cSArnaldo Carvalho de Melo 		if (sock_queue_err_skb(sk, skb) == 0)
325bd82393cSArnaldo Carvalho de Melo 			return;
326bd82393cSArnaldo Carvalho de Melo 	}
3271da177e4SLinus Torvalds 	kfree_skb(skb);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3300579016eSAl Viro void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3331da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3341da177e4SLinus Torvalds 	struct iphdr *iph;
3351da177e4SLinus Torvalds 	struct sk_buff *skb;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	if (!inet->recverr)
3381da177e4SLinus Torvalds 		return;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
3411da177e4SLinus Torvalds 	if (!skb)
3421da177e4SLinus Torvalds 		return;
3431da177e4SLinus Torvalds 
3442ca9e6f2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr));
3452ca9e6f2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
346eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
3471da177e4SLinus Torvalds 	iph->daddr = daddr;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3501da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
3511da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
3521da177e4SLinus Torvalds 	serr->ee.ee_type = 0;
3531da177e4SLinus Torvalds 	serr->ee.ee_code = 0;
3541da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
3551da177e4SLinus Torvalds 	serr->ee.ee_info = info;
3561da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
357d56f90a7SArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
3581da177e4SLinus Torvalds 	serr->port = port;
3591da177e4SLinus Torvalds 
36027a884dcSArnaldo Carvalho de Melo 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
361bd82393cSArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	if (sock_queue_err_skb(sk, skb))
3641da177e4SLinus Torvalds 		kfree_skb(skb);
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds /*
3681da177e4SLinus Torvalds  *	Handle MSG_ERRQUEUE
3691da177e4SLinus Torvalds  */
3701da177e4SLinus Torvalds int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3731da177e4SLinus Torvalds 	struct sk_buff *skb, *skb2;
3741da177e4SLinus Torvalds 	struct sockaddr_in *sin;
3751da177e4SLinus Torvalds 	struct {
3761da177e4SLinus Torvalds 		struct sock_extended_err ee;
3771da177e4SLinus Torvalds 		struct sockaddr_in	 offender;
3781da177e4SLinus Torvalds 	} errhdr;
3791da177e4SLinus Torvalds 	int err;
3801da177e4SLinus Torvalds 	int copied;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	err = -EAGAIN;
3831da177e4SLinus Torvalds 	skb = skb_dequeue(&sk->sk_error_queue);
3841da177e4SLinus Torvalds 	if (skb == NULL)
3851da177e4SLinus Torvalds 		goto out;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	copied = skb->len;
3881da177e4SLinus Torvalds 	if (copied > len) {
3891da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
3901da177e4SLinus Torvalds 		copied = len;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3931da177e4SLinus Torvalds 	if (err)
3941da177e4SLinus Torvalds 		goto out_free_skb;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	sock_recv_timestamp(msg, sk, skb);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	sin = (struct sockaddr_in *)msg->msg_name;
4011da177e4SLinus Torvalds 	if (sin) {
4021da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
403d56f90a7SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
404d56f90a7SArnaldo Carvalho de Melo 						   serr->addr_offset);
4051da177e4SLinus Torvalds 		sin->sin_port = serr->port;
4061da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
4101da177e4SLinus Torvalds 	sin = &errhdr.offender;
4111da177e4SLinus Torvalds 	sin->sin_family = AF_UNSPEC;
4121da177e4SLinus Torvalds 	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
4131da177e4SLinus Torvalds 		struct inet_sock *inet = inet_sk(sk);
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
416eddc9ec5SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
4171da177e4SLinus Torvalds 		sin->sin_port = 0;
4181da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4191da177e4SLinus Torvalds 		if (inet->cmsg_flags)
4201da177e4SLinus Torvalds 			ip_cmsg_recv(msg, skb);
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	/* Now we could try to dump offended packet options */
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	msg->msg_flags |= MSG_ERRQUEUE;
4281da177e4SLinus Torvalds 	err = copied;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	/* Reset and regenerate socket error */
431e0f9f858SHerbert Xu 	spin_lock_bh(&sk->sk_error_queue.lock);
4321da177e4SLinus Torvalds 	sk->sk_err = 0;
4334d52cfbeSEric Dumazet 	skb2 = skb_peek(&sk->sk_error_queue);
4344d52cfbeSEric Dumazet 	if (skb2 != NULL) {
4351da177e4SLinus Torvalds 		sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
436e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
4371da177e4SLinus Torvalds 		sk->sk_error_report(sk);
4381da177e4SLinus Torvalds 	} else
439e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds out_free_skb:
4421da177e4SLinus Torvalds 	kfree_skb(skb);
4431da177e4SLinus Torvalds out:
4441da177e4SLinus Torvalds 	return err;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 
448f6d8bd05SEric Dumazet static void opt_kfree_rcu(struct rcu_head *head)
449f6d8bd05SEric Dumazet {
450f6d8bd05SEric Dumazet 	kfree(container_of(head, struct ip_options_rcu, rcu));
451f6d8bd05SEric Dumazet }
452f6d8bd05SEric Dumazet 
4531da177e4SLinus Torvalds /*
4544d52cfbeSEric Dumazet  *	Socket option code for IP. This is the end of the line after any
4554d52cfbeSEric Dumazet  *	TCP,UDP etc options on an IP socket.
4561da177e4SLinus Torvalds  */
4571da177e4SLinus Torvalds 
4583fdadf7dSDmitry Mishin static int do_ip_setsockopt(struct sock *sk, int level,
459b7058842SDavid S. Miller 			    int optname, char __user *optval, unsigned int optlen)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4621da177e4SLinus Torvalds 	int val = 0, err;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
4651da177e4SLinus Torvalds 			     (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
4661da177e4SLinus Torvalds 			     (1<<IP_RETOPTS) | (1<<IP_TOS) |
4671da177e4SLinus Torvalds 			     (1<<IP_TTL) | (1<<IP_HDRINCL) |
4681da177e4SLinus Torvalds 			     (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
4692c7946a7SCatherine Zhang 			     (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
470d218d111SStephen Hemminger 			     (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) |
4717b2ff18eSJiri Olsa 			     (1<<IP_MINTTL) | (1<<IP_NODEFRAG))) ||
4721da177e4SLinus Torvalds 	    optname == IP_MULTICAST_TTL ||
473f771bef9SNivedita Singhvi 	    optname == IP_MULTICAST_ALL ||
474e8b2dfe9SBalazs Scheidler 	    optname == IP_MULTICAST_LOOP ||
475e8b2dfe9SBalazs Scheidler 	    optname == IP_RECVORIGDSTADDR) {
4761da177e4SLinus Torvalds 		if (optlen >= sizeof(int)) {
4771da177e4SLinus Torvalds 			if (get_user(val, (int __user *) optval))
4781da177e4SLinus Torvalds 				return -EFAULT;
4791da177e4SLinus Torvalds 		} else if (optlen >= sizeof(char)) {
4801da177e4SLinus Torvalds 			unsigned char ucval;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 			if (get_user(ucval, (unsigned char __user *) optval))
4831da177e4SLinus Torvalds 				return -EFAULT;
4841da177e4SLinus Torvalds 			val = (int) ucval;
4851da177e4SLinus Torvalds 		}
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	/* If optlen==0, it is equivalent to val == 0 */
4891da177e4SLinus Torvalds 
4906a9fb947SPavel Emelyanov 	if (ip_mroute_opt(optname))
4911da177e4SLinus Torvalds 		return ip_mroute_setsockopt(sk, optname, optval, optlen);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	err = 0;
4941da177e4SLinus Torvalds 	lock_sock(sk);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	switch (optname) {
4971da177e4SLinus Torvalds 	case IP_OPTIONS:
4981da177e4SLinus Torvalds 	{
499f6d8bd05SEric Dumazet 		struct ip_options_rcu *old, *opt = NULL;
500f6d8bd05SEric Dumazet 
50165a1c4ffSroel kluin 		if (optlen > 40)
5021da177e4SLinus Torvalds 			goto e_inval;
5033b1e0a65SYOSHIFUJI Hideaki 		err = ip_options_get_from_user(sock_net(sk), &opt,
504cb84663eSDenis V. Lunev 					       optval, optlen);
5051da177e4SLinus Torvalds 		if (err)
5061da177e4SLinus Torvalds 			break;
507f6d8bd05SEric Dumazet 		old = rcu_dereference_protected(inet->inet_opt,
508f6d8bd05SEric Dumazet 						sock_owned_by_user(sk));
509d83d8461SArnaldo Carvalho de Melo 		if (inet->is_icsk) {
510d83d8461SArnaldo Carvalho de Melo 			struct inet_connection_sock *icsk = inet_csk(sk);
5111da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5121da177e4SLinus Torvalds 			if (sk->sk_family == PF_INET ||
5131da177e4SLinus Torvalds 			    (!((1 << sk->sk_state) &
5141da177e4SLinus Torvalds 			       (TCPF_LISTEN | TCPF_CLOSE)) &&
515c720c7e8SEric Dumazet 			     inet->inet_daddr != LOOPBACK4_IPV6)) {
5161da177e4SLinus Torvalds #endif
517f6d8bd05SEric Dumazet 				if (old)
518f6d8bd05SEric Dumazet 					icsk->icsk_ext_hdr_len -= old->opt.optlen;
5191da177e4SLinus Torvalds 				if (opt)
520f6d8bd05SEric Dumazet 					icsk->icsk_ext_hdr_len += opt->opt.optlen;
521d83d8461SArnaldo Carvalho de Melo 				icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
5221da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5231da177e4SLinus Torvalds 			}
5241da177e4SLinus Torvalds #endif
5251da177e4SLinus Torvalds 		}
526f6d8bd05SEric Dumazet 		rcu_assign_pointer(inet->inet_opt, opt);
527f6d8bd05SEric Dumazet 		if (old)
528f6d8bd05SEric Dumazet 			call_rcu(&old->rcu, opt_kfree_rcu);
5291da177e4SLinus Torvalds 		break;
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds 	case IP_PKTINFO:
5321da177e4SLinus Torvalds 		if (val)
5331da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_PKTINFO;
5341da177e4SLinus Torvalds 		else
5351da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
5361da177e4SLinus Torvalds 		break;
5371da177e4SLinus Torvalds 	case IP_RECVTTL:
5381da177e4SLinus Torvalds 		if (val)
5391da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TTL;
5401da177e4SLinus Torvalds 		else
5411da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TTL;
5421da177e4SLinus Torvalds 		break;
5431da177e4SLinus Torvalds 	case IP_RECVTOS:
5441da177e4SLinus Torvalds 		if (val)
5451da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TOS;
5461da177e4SLinus Torvalds 		else
5471da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TOS;
5481da177e4SLinus Torvalds 		break;
5491da177e4SLinus Torvalds 	case IP_RECVOPTS:
5501da177e4SLinus Torvalds 		if (val)
5511da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_RECVOPTS;
5521da177e4SLinus Torvalds 		else
5531da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
5541da177e4SLinus Torvalds 		break;
5551da177e4SLinus Torvalds 	case IP_RETOPTS:
5561da177e4SLinus Torvalds 		if (val)
5571da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_RETOPTS;
5581da177e4SLinus Torvalds 		else
5591da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
5601da177e4SLinus Torvalds 		break;
5612c7946a7SCatherine Zhang 	case IP_PASSSEC:
5622c7946a7SCatherine Zhang 		if (val)
5632c7946a7SCatherine Zhang 			inet->cmsg_flags |= IP_CMSG_PASSSEC;
5642c7946a7SCatherine Zhang 		else
5652c7946a7SCatherine Zhang 			inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
5662c7946a7SCatherine Zhang 		break;
567e8b2dfe9SBalazs Scheidler 	case IP_RECVORIGDSTADDR:
568e8b2dfe9SBalazs Scheidler 		if (val)
569e8b2dfe9SBalazs Scheidler 			inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
570e8b2dfe9SBalazs Scheidler 		else
571e8b2dfe9SBalazs Scheidler 			inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
572e8b2dfe9SBalazs Scheidler 		break;
5731da177e4SLinus Torvalds 	case IP_TOS:	/* This sets both TOS and Precedence */
5741da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM) {
5752c67e9acSMaciej Żenczykowski 			val &= ~INET_ECN_MASK;
5762c67e9acSMaciej Żenczykowski 			val |= inet->tos & INET_ECN_MASK;
5771da177e4SLinus Torvalds 		}
5781da177e4SLinus Torvalds 		if (inet->tos != val) {
5791da177e4SLinus Torvalds 			inet->tos = val;
5801da177e4SLinus Torvalds 			sk->sk_priority = rt_tos2priority(val);
5811da177e4SLinus Torvalds 			sk_dst_reset(sk);
5821da177e4SLinus Torvalds 		}
5831da177e4SLinus Torvalds 		break;
5841da177e4SLinus Torvalds 	case IP_TTL:
5851da177e4SLinus Torvalds 		if (optlen < 1)
5861da177e4SLinus Torvalds 			goto e_inval;
5874d52cfbeSEric Dumazet 		if (val != -1 && (val < 0 || val > 255))
5881da177e4SLinus Torvalds 			goto e_inval;
5891da177e4SLinus Torvalds 		inet->uc_ttl = val;
5901da177e4SLinus Torvalds 		break;
5911da177e4SLinus Torvalds 	case IP_HDRINCL:
5921da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_RAW) {
5931da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
5941da177e4SLinus Torvalds 			break;
5951da177e4SLinus Torvalds 		}
5961da177e4SLinus Torvalds 		inet->hdrincl = val ? 1 : 0;
5971da177e4SLinus Torvalds 		break;
5987b2ff18eSJiri Olsa 	case IP_NODEFRAG:
5997b2ff18eSJiri Olsa 		if (sk->sk_type != SOCK_RAW) {
6007b2ff18eSJiri Olsa 			err = -ENOPROTOOPT;
6017b2ff18eSJiri Olsa 			break;
6027b2ff18eSJiri Olsa 		}
6037b2ff18eSJiri Olsa 		inet->nodefrag = val ? 1 : 0;
6047b2ff18eSJiri Olsa 		break;
6051da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
6060eae750eSJohn Dykstra 		if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
6071da177e4SLinus Torvalds 			goto e_inval;
6081da177e4SLinus Torvalds 		inet->pmtudisc = val;
6091da177e4SLinus Torvalds 		break;
6101da177e4SLinus Torvalds 	case IP_RECVERR:
6111da177e4SLinus Torvalds 		inet->recverr = !!val;
6121da177e4SLinus Torvalds 		if (!val)
6131da177e4SLinus Torvalds 			skb_queue_purge(&sk->sk_error_queue);
6141da177e4SLinus Torvalds 		break;
6151da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
6161da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
6171da177e4SLinus Torvalds 			goto e_inval;
6181da177e4SLinus Torvalds 		if (optlen < 1)
6191da177e4SLinus Torvalds 			goto e_inval;
6201da177e4SLinus Torvalds 		if (val == -1)
6211da177e4SLinus Torvalds 			val = 1;
6221da177e4SLinus Torvalds 		if (val < 0 || val > 255)
6231da177e4SLinus Torvalds 			goto e_inval;
6241da177e4SLinus Torvalds 		inet->mc_ttl = val;
6251da177e4SLinus Torvalds 		break;
6261da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
6271da177e4SLinus Torvalds 		if (optlen < 1)
6281da177e4SLinus Torvalds 			goto e_inval;
6291da177e4SLinus Torvalds 		inet->mc_loop = !!val;
6301da177e4SLinus Torvalds 		break;
6311da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
6321da177e4SLinus Torvalds 	{
6331da177e4SLinus Torvalds 		struct ip_mreqn mreq;
6341da177e4SLinus Torvalds 		struct net_device *dev = NULL;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
6371da177e4SLinus Torvalds 			goto e_inval;
6381da177e4SLinus Torvalds 		/*
6391da177e4SLinus Torvalds 		 *	Check the arguments are allowable
6401da177e4SLinus Torvalds 		 */
6411da177e4SLinus Torvalds 
6420915921bSShan Wei 		if (optlen < sizeof(struct in_addr))
6430915921bSShan Wei 			goto e_inval;
6440915921bSShan Wei 
6451da177e4SLinus Torvalds 		err = -EFAULT;
6461da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
6471da177e4SLinus Torvalds 			if (copy_from_user(&mreq, optval, sizeof(mreq)))
6481da177e4SLinus Torvalds 				break;
6491da177e4SLinus Torvalds 		} else {
6501da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
6511da177e4SLinus Torvalds 			if (optlen >= sizeof(struct in_addr) &&
6524d52cfbeSEric Dumazet 			    copy_from_user(&mreq.imr_address, optval,
6534d52cfbeSEric Dumazet 					   sizeof(struct in_addr)))
6541da177e4SLinus Torvalds 				break;
6551da177e4SLinus Torvalds 		}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 		if (!mreq.imr_ifindex) {
658e6f1cebfSAl Viro 			if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
6591da177e4SLinus Torvalds 				inet->mc_index = 0;
6601da177e4SLinus Torvalds 				inet->mc_addr  = 0;
6611da177e4SLinus Torvalds 				err = 0;
6621da177e4SLinus Torvalds 				break;
6631da177e4SLinus Torvalds 			}
6643b1e0a65SYOSHIFUJI Hideaki 			dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
66555b80503SEric Dumazet 			if (dev)
6661da177e4SLinus Torvalds 				mreq.imr_ifindex = dev->ifindex;
6671da177e4SLinus Torvalds 		} else
66855b80503SEric Dumazet 			dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
6721da177e4SLinus Torvalds 		if (!dev)
6731da177e4SLinus Torvalds 			break;
67455b80503SEric Dumazet 		dev_put(dev);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 		err = -EINVAL;
6771da177e4SLinus Torvalds 		if (sk->sk_bound_dev_if &&
6781da177e4SLinus Torvalds 		    mreq.imr_ifindex != sk->sk_bound_dev_if)
6791da177e4SLinus Torvalds 			break;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 		inet->mc_index = mreq.imr_ifindex;
6821da177e4SLinus Torvalds 		inet->mc_addr  = mreq.imr_address.s_addr;
6831da177e4SLinus Torvalds 		err = 0;
6841da177e4SLinus Torvalds 		break;
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	case IP_ADD_MEMBERSHIP:
6881da177e4SLinus Torvalds 	case IP_DROP_MEMBERSHIP:
6891da177e4SLinus Torvalds 	{
6901da177e4SLinus Torvalds 		struct ip_mreqn mreq;
6911da177e4SLinus Torvalds 
692a96fb49bSFlavio Leitner 		err = -EPROTO;
693a96fb49bSFlavio Leitner 		if (inet_sk(sk)->is_icsk)
694a96fb49bSFlavio Leitner 			break;
695a96fb49bSFlavio Leitner 
6961da177e4SLinus Torvalds 		if (optlen < sizeof(struct ip_mreq))
6971da177e4SLinus Torvalds 			goto e_inval;
6981da177e4SLinus Torvalds 		err = -EFAULT;
6991da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
7001da177e4SLinus Torvalds 			if (copy_from_user(&mreq, optval, sizeof(mreq)))
7011da177e4SLinus Torvalds 				break;
7021da177e4SLinus Torvalds 		} else {
7031da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
7041da177e4SLinus Torvalds 			if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
7051da177e4SLinus Torvalds 				break;
7061da177e4SLinus Torvalds 		}
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 		if (optname == IP_ADD_MEMBERSHIP)
7091da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7101da177e4SLinus Torvalds 		else
7111da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
7121da177e4SLinus Torvalds 		break;
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 	case IP_MSFILTER:
7151da177e4SLinus Torvalds 	{
7161da177e4SLinus Torvalds 		struct ip_msfilter *msf;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 		if (optlen < IP_MSFILTER_SIZE(0))
7191da177e4SLinus Torvalds 			goto e_inval;
7201da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
7211da177e4SLinus Torvalds 			err = -ENOBUFS;
7221da177e4SLinus Torvalds 			break;
7231da177e4SLinus Torvalds 		}
7248b3a7005SKris Katterjohn 		msf = kmalloc(optlen, GFP_KERNEL);
725cfcabdccSStephen Hemminger 		if (!msf) {
7261da177e4SLinus Torvalds 			err = -ENOBUFS;
7271da177e4SLinus Torvalds 			break;
7281da177e4SLinus Torvalds 		}
7291da177e4SLinus Torvalds 		err = -EFAULT;
7301da177e4SLinus Torvalds 		if (copy_from_user(msf, optval, optlen)) {
7311da177e4SLinus Torvalds 			kfree(msf);
7321da177e4SLinus Torvalds 			break;
7331da177e4SLinus Torvalds 		}
7341da177e4SLinus Torvalds 		/* numsrc >= (1G-4) overflow in 32 bits */
7351da177e4SLinus Torvalds 		if (msf->imsf_numsrc >= 0x3ffffffcU ||
7361da177e4SLinus Torvalds 		    msf->imsf_numsrc > sysctl_igmp_max_msf) {
7371da177e4SLinus Torvalds 			kfree(msf);
7381da177e4SLinus Torvalds 			err = -ENOBUFS;
7391da177e4SLinus Torvalds 			break;
7401da177e4SLinus Torvalds 		}
7411da177e4SLinus Torvalds 		if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
7421da177e4SLinus Torvalds 			kfree(msf);
7431da177e4SLinus Torvalds 			err = -EINVAL;
7441da177e4SLinus Torvalds 			break;
7451da177e4SLinus Torvalds 		}
7461da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, 0);
7471da177e4SLinus Torvalds 		kfree(msf);
7481da177e4SLinus Torvalds 		break;
7491da177e4SLinus Torvalds 	}
7501da177e4SLinus Torvalds 	case IP_BLOCK_SOURCE:
7511da177e4SLinus Torvalds 	case IP_UNBLOCK_SOURCE:
7521da177e4SLinus Torvalds 	case IP_ADD_SOURCE_MEMBERSHIP:
7531da177e4SLinus Torvalds 	case IP_DROP_SOURCE_MEMBERSHIP:
7541da177e4SLinus Torvalds 	{
7551da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
7561da177e4SLinus Torvalds 		int omode, add;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 		if (optlen != sizeof(struct ip_mreq_source))
7591da177e4SLinus Torvalds 			goto e_inval;
7601da177e4SLinus Torvalds 		if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
7611da177e4SLinus Torvalds 			err = -EFAULT;
7621da177e4SLinus Torvalds 			break;
7631da177e4SLinus Torvalds 		}
7641da177e4SLinus Torvalds 		if (optname == IP_BLOCK_SOURCE) {
7651da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7661da177e4SLinus Torvalds 			add = 1;
7671da177e4SLinus Torvalds 		} else if (optname == IP_UNBLOCK_SOURCE) {
7681da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7691da177e4SLinus Torvalds 			add = 0;
7701da177e4SLinus Torvalds 		} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
7711da177e4SLinus Torvalds 			struct ip_mreqn mreq;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 			mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
7741da177e4SLinus Torvalds 			mreq.imr_address.s_addr = mreqs.imr_interface;
7751da177e4SLinus Torvalds 			mreq.imr_ifindex = 0;
7761da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7778cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
7781da177e4SLinus Torvalds 				break;
7791da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7801da177e4SLinus Torvalds 			add = 1;
7811da177e4SLinus Torvalds 		} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
7821da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7831da177e4SLinus Torvalds 			add = 0;
7841da177e4SLinus Torvalds 		}
7851da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs, 0);
7861da177e4SLinus Torvalds 		break;
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 	case MCAST_JOIN_GROUP:
7891da177e4SLinus Torvalds 	case MCAST_LEAVE_GROUP:
7901da177e4SLinus Torvalds 	{
7911da177e4SLinus Torvalds 		struct group_req greq;
7921da177e4SLinus Torvalds 		struct sockaddr_in *psin;
7931da177e4SLinus Torvalds 		struct ip_mreqn mreq;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 		if (optlen < sizeof(struct group_req))
7961da177e4SLinus Torvalds 			goto e_inval;
7971da177e4SLinus Torvalds 		err = -EFAULT;
7981da177e4SLinus Torvalds 		if (copy_from_user(&greq, optval, sizeof(greq)))
7991da177e4SLinus Torvalds 			break;
8001da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greq.gr_group;
8011da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET)
8021da177e4SLinus Torvalds 			goto e_inval;
8031da177e4SLinus Torvalds 		memset(&mreq, 0, sizeof(mreq));
8041da177e4SLinus Torvalds 		mreq.imr_multiaddr = psin->sin_addr;
8051da177e4SLinus Torvalds 		mreq.imr_ifindex = greq.gr_interface;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 		if (optname == MCAST_JOIN_GROUP)
8081da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
8091da177e4SLinus Torvalds 		else
8101da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
8111da177e4SLinus Torvalds 		break;
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 	case MCAST_JOIN_SOURCE_GROUP:
8141da177e4SLinus Torvalds 	case MCAST_LEAVE_SOURCE_GROUP:
8151da177e4SLinus Torvalds 	case MCAST_BLOCK_SOURCE:
8161da177e4SLinus Torvalds 	case MCAST_UNBLOCK_SOURCE:
8171da177e4SLinus Torvalds 	{
8181da177e4SLinus Torvalds 		struct group_source_req greqs;
8191da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
8201da177e4SLinus Torvalds 		struct sockaddr_in *psin;
8211da177e4SLinus Torvalds 		int omode, add;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 		if (optlen != sizeof(struct group_source_req))
8241da177e4SLinus Torvalds 			goto e_inval;
8251da177e4SLinus Torvalds 		if (copy_from_user(&greqs, optval, sizeof(greqs))) {
8261da177e4SLinus Torvalds 			err = -EFAULT;
8271da177e4SLinus Torvalds 			break;
8281da177e4SLinus Torvalds 		}
8291da177e4SLinus Torvalds 		if (greqs.gsr_group.ss_family != AF_INET ||
8301da177e4SLinus Torvalds 		    greqs.gsr_source.ss_family != AF_INET) {
8311da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
8321da177e4SLinus Torvalds 			break;
8331da177e4SLinus Torvalds 		}
8341da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_group;
8351da177e4SLinus Torvalds 		mreqs.imr_multiaddr = psin->sin_addr.s_addr;
8361da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_source;
8371da177e4SLinus Torvalds 		mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
8381da177e4SLinus Torvalds 		mreqs.imr_interface = 0; /* use index for mc_source */
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 		if (optname == MCAST_BLOCK_SOURCE) {
8411da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
8421da177e4SLinus Torvalds 			add = 1;
8431da177e4SLinus Torvalds 		} else if (optname == MCAST_UNBLOCK_SOURCE) {
8441da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
8451da177e4SLinus Torvalds 			add = 0;
8461da177e4SLinus Torvalds 		} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
8471da177e4SLinus Torvalds 			struct ip_mreqn mreq;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&greqs.gsr_group;
8501da177e4SLinus Torvalds 			mreq.imr_multiaddr = psin->sin_addr;
8511da177e4SLinus Torvalds 			mreq.imr_address.s_addr = 0;
8521da177e4SLinus Torvalds 			mreq.imr_ifindex = greqs.gsr_interface;
8531da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
8548cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
8551da177e4SLinus Torvalds 				break;
8561da177e4SLinus Torvalds 			greqs.gsr_interface = mreq.imr_ifindex;
8571da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
8581da177e4SLinus Torvalds 			add = 1;
8591da177e4SLinus Torvalds 		} else /* MCAST_LEAVE_SOURCE_GROUP */ {
8601da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
8611da177e4SLinus Torvalds 			add = 0;
8621da177e4SLinus Torvalds 		}
8631da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs,
8641da177e4SLinus Torvalds 				   greqs.gsr_interface);
8651da177e4SLinus Torvalds 		break;
8661da177e4SLinus Torvalds 	}
8671da177e4SLinus Torvalds 	case MCAST_MSFILTER:
8681da177e4SLinus Torvalds 	{
8691da177e4SLinus Torvalds 		struct sockaddr_in *psin;
8701da177e4SLinus Torvalds 		struct ip_msfilter *msf = NULL;
8711da177e4SLinus Torvalds 		struct group_filter *gsf = NULL;
8721da177e4SLinus Torvalds 		int msize, i, ifindex;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 		if (optlen < GROUP_FILTER_SIZE(0))
8751da177e4SLinus Torvalds 			goto e_inval;
8761da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
8771da177e4SLinus Torvalds 			err = -ENOBUFS;
8781da177e4SLinus Torvalds 			break;
8791da177e4SLinus Torvalds 		}
8808b3a7005SKris Katterjohn 		gsf = kmalloc(optlen, GFP_KERNEL);
881cfcabdccSStephen Hemminger 		if (!gsf) {
8821da177e4SLinus Torvalds 			err = -ENOBUFS;
8831da177e4SLinus Torvalds 			break;
8841da177e4SLinus Torvalds 		}
8851da177e4SLinus Torvalds 		err = -EFAULT;
8864d52cfbeSEric Dumazet 		if (copy_from_user(gsf, optval, optlen))
8871da177e4SLinus Torvalds 			goto mc_msf_out;
8884d52cfbeSEric Dumazet 
8891da177e4SLinus Torvalds 		/* numsrc >= (4G-140)/128 overflow in 32 bits */
8901da177e4SLinus Torvalds 		if (gsf->gf_numsrc >= 0x1ffffff ||
8911da177e4SLinus Torvalds 		    gsf->gf_numsrc > sysctl_igmp_max_msf) {
8921da177e4SLinus Torvalds 			err = -ENOBUFS;
8931da177e4SLinus Torvalds 			goto mc_msf_out;
8941da177e4SLinus Torvalds 		}
8951da177e4SLinus Torvalds 		if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
8961da177e4SLinus Torvalds 			err = -EINVAL;
8971da177e4SLinus Torvalds 			goto mc_msf_out;
8981da177e4SLinus Torvalds 		}
8991da177e4SLinus Torvalds 		msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
9008b3a7005SKris Katterjohn 		msf = kmalloc(msize, GFP_KERNEL);
901cfcabdccSStephen Hemminger 		if (!msf) {
9021da177e4SLinus Torvalds 			err = -ENOBUFS;
9031da177e4SLinus Torvalds 			goto mc_msf_out;
9041da177e4SLinus Torvalds 		}
9051da177e4SLinus Torvalds 		ifindex = gsf->gf_interface;
9061da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&gsf->gf_group;
9071da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET) {
9081da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
9091da177e4SLinus Torvalds 			goto mc_msf_out;
9101da177e4SLinus Torvalds 		}
9111da177e4SLinus Torvalds 		msf->imsf_multiaddr = psin->sin_addr.s_addr;
9121da177e4SLinus Torvalds 		msf->imsf_interface = 0;
9131da177e4SLinus Torvalds 		msf->imsf_fmode = gsf->gf_fmode;
9141da177e4SLinus Torvalds 		msf->imsf_numsrc = gsf->gf_numsrc;
9151da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
9161da177e4SLinus Torvalds 		for (i = 0; i < gsf->gf_numsrc; ++i) {
9171da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&gsf->gf_slist[i];
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 			if (psin->sin_family != AF_INET)
9201da177e4SLinus Torvalds 				goto mc_msf_out;
9211da177e4SLinus Torvalds 			msf->imsf_slist[i] = psin->sin_addr.s_addr;
9221da177e4SLinus Torvalds 		}
9231da177e4SLinus Torvalds 		kfree(gsf);
9241da177e4SLinus Torvalds 		gsf = NULL;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, ifindex);
9271da177e4SLinus Torvalds mc_msf_out:
9281da177e4SLinus Torvalds 		kfree(msf);
9291da177e4SLinus Torvalds 		kfree(gsf);
9301da177e4SLinus Torvalds 		break;
9311da177e4SLinus Torvalds 	}
932f771bef9SNivedita Singhvi 	case IP_MULTICAST_ALL:
933f771bef9SNivedita Singhvi 		if (optlen < 1)
934f771bef9SNivedita Singhvi 			goto e_inval;
935f771bef9SNivedita Singhvi 		if (val != 0 && val != 1)
936f771bef9SNivedita Singhvi 			goto e_inval;
937f771bef9SNivedita Singhvi 		inet->mc_all = val;
938f771bef9SNivedita Singhvi 		break;
9391da177e4SLinus Torvalds 	case IP_ROUTER_ALERT:
9401da177e4SLinus Torvalds 		err = ip_ra_control(sk, val ? 1 : 0, NULL);
9411da177e4SLinus Torvalds 		break;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	case IP_FREEBIND:
9441da177e4SLinus Torvalds 		if (optlen < 1)
9451da177e4SLinus Torvalds 			goto e_inval;
9461da177e4SLinus Torvalds 		inet->freebind = !!val;
9471da177e4SLinus Torvalds 		break;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	case IP_IPSEC_POLICY:
9501da177e4SLinus Torvalds 	case IP_XFRM_POLICY:
9516fc0b4a7SHerbert Xu 		err = -EPERM;
9526fc0b4a7SHerbert Xu 		if (!capable(CAP_NET_ADMIN))
9536fc0b4a7SHerbert Xu 			break;
9541da177e4SLinus Torvalds 		err = xfrm_user_policy(sk, optname, optval, optlen);
9551da177e4SLinus Torvalds 		break;
9561da177e4SLinus Torvalds 
957f5715aeaSKOVACS Krisztian 	case IP_TRANSPARENT:
9586cc7a765SMaciej Żenczykowski 		if (!!val && !capable(CAP_NET_RAW) && !capable(CAP_NET_ADMIN)) {
959f5715aeaSKOVACS Krisztian 			err = -EPERM;
960f5715aeaSKOVACS Krisztian 			break;
961f5715aeaSKOVACS Krisztian 		}
962f5715aeaSKOVACS Krisztian 		if (optlen < 1)
963f5715aeaSKOVACS Krisztian 			goto e_inval;
964f5715aeaSKOVACS Krisztian 		inet->transparent = !!val;
965f5715aeaSKOVACS Krisztian 		break;
966f5715aeaSKOVACS Krisztian 
967d218d111SStephen Hemminger 	case IP_MINTTL:
968d218d111SStephen Hemminger 		if (optlen < 1)
969d218d111SStephen Hemminger 			goto e_inval;
970d218d111SStephen Hemminger 		if (val < 0 || val > 255)
971d218d111SStephen Hemminger 			goto e_inval;
972d218d111SStephen Hemminger 		inet->min_ttl = val;
973d218d111SStephen Hemminger 		break;
974d218d111SStephen Hemminger 
9751da177e4SLinus Torvalds 	default:
9761da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
9771da177e4SLinus Torvalds 		break;
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 	release_sock(sk);
9801da177e4SLinus Torvalds 	return err;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds e_inval:
9831da177e4SLinus Torvalds 	release_sock(sk);
9841da177e4SLinus Torvalds 	return -EINVAL;
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds 
987f84af32cSEric Dumazet /**
988*d826eb14SEric Dumazet  * ipv4_pktinfo_prepare - transfert some info from rtable to skb
989f84af32cSEric Dumazet  * @sk: socket
990f84af32cSEric Dumazet  * @skb: buffer
991f84af32cSEric Dumazet  *
992*d826eb14SEric Dumazet  * To support IP_CMSG_PKTINFO option, we store rt_iif and rt_spec_dst
993*d826eb14SEric Dumazet  * in skb->cb[] before dst drop.
994*d826eb14SEric Dumazet  * This way, receiver doesnt make cache line misses to read rtable.
995f84af32cSEric Dumazet  */
996*d826eb14SEric Dumazet void ipv4_pktinfo_prepare(struct sk_buff *skb)
997f84af32cSEric Dumazet {
998*d826eb14SEric Dumazet 	struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
999*d826eb14SEric Dumazet 	const struct rtable *rt = skb_rtable(skb);
1000*d826eb14SEric Dumazet 
1001*d826eb14SEric Dumazet 	if (rt) {
1002*d826eb14SEric Dumazet 		pktinfo->ipi_ifindex = rt->rt_iif;
1003*d826eb14SEric Dumazet 		pktinfo->ipi_spec_dst.s_addr = rt->rt_spec_dst;
1004*d826eb14SEric Dumazet 	} else {
1005*d826eb14SEric Dumazet 		pktinfo->ipi_ifindex = 0;
1006*d826eb14SEric Dumazet 		pktinfo->ipi_spec_dst.s_addr = 0;
1007f84af32cSEric Dumazet 	}
1008*d826eb14SEric Dumazet 	skb_dst_drop(skb);
1009*d826eb14SEric Dumazet }
1010f84af32cSEric Dumazet 
10113fdadf7dSDmitry Mishin int ip_setsockopt(struct sock *sk, int level,
1012b7058842SDavid S. Miller 		int optname, char __user *optval, unsigned int optlen)
10133fdadf7dSDmitry Mishin {
10143fdadf7dSDmitry Mishin 	int err;
10153fdadf7dSDmitry Mishin 
10163fdadf7dSDmitry Mishin 	if (level != SOL_IP)
10173fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
10183fdadf7dSDmitry Mishin 
10193fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
10203fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
10213fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
10223fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
10236a9fb947SPavel Emelyanov 			optname != IP_IPSEC_POLICY &&
10246a9fb947SPavel Emelyanov 			optname != IP_XFRM_POLICY &&
10256a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
10263fdadf7dSDmitry Mishin 		lock_sock(sk);
10273fdadf7dSDmitry Mishin 		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
10283fdadf7dSDmitry Mishin 		release_sock(sk);
10293fdadf7dSDmitry Mishin 	}
10303fdadf7dSDmitry Mishin #endif
10313fdadf7dSDmitry Mishin 	return err;
10323fdadf7dSDmitry Mishin }
10334d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_setsockopt);
10343fdadf7dSDmitry Mishin 
10353fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
1036543d9cfeSArnaldo Carvalho de Melo int compat_ip_setsockopt(struct sock *sk, int level, int optname,
1037b7058842SDavid S. Miller 			 char __user *optval, unsigned int optlen)
10383fdadf7dSDmitry Mishin {
10393fdadf7dSDmitry Mishin 	int err;
10403fdadf7dSDmitry Mishin 
10413fdadf7dSDmitry Mishin 	if (level != SOL_IP)
10423fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
10433fdadf7dSDmitry Mishin 
1044dae50295SDavid L Stevens 	if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1045dae50295SDavid L Stevens 		return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1046dae50295SDavid L Stevens 			ip_setsockopt);
1047dae50295SDavid L Stevens 
10483fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
10493fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
10503fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
10513fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
10526a9fb947SPavel Emelyanov 			optname != IP_IPSEC_POLICY &&
10536a9fb947SPavel Emelyanov 			optname != IP_XFRM_POLICY &&
10546a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
10553fdadf7dSDmitry Mishin 		lock_sock(sk);
1056543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_setsockopt(sk, PF_INET, optname,
1057543d9cfeSArnaldo Carvalho de Melo 					   optval, optlen);
10583fdadf7dSDmitry Mishin 		release_sock(sk);
10593fdadf7dSDmitry Mishin 	}
10603fdadf7dSDmitry Mishin #endif
10613fdadf7dSDmitry Mishin 	return err;
10623fdadf7dSDmitry Mishin }
1063543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_setsockopt);
10643fdadf7dSDmitry Mishin #endif
10653fdadf7dSDmitry Mishin 
10661da177e4SLinus Torvalds /*
10674d52cfbeSEric Dumazet  *	Get the options. Note for future reference. The GET of IP options gets
10684d52cfbeSEric Dumazet  *	the _received_ ones. The set sets the _sent_ ones.
10691da177e4SLinus Torvalds  */
10701da177e4SLinus Torvalds 
10713fdadf7dSDmitry Mishin static int do_ip_getsockopt(struct sock *sk, int level, int optname,
1072dd23198eSDaniel Baluta 			    char __user *optval, int __user *optlen, unsigned flags)
10731da177e4SLinus Torvalds {
10741da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
10751da177e4SLinus Torvalds 	int val;
10761da177e4SLinus Torvalds 	int len;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	if (level != SOL_IP)
10791da177e4SLinus Torvalds 		return -EOPNOTSUPP;
10801da177e4SLinus Torvalds 
10816a9fb947SPavel Emelyanov 	if (ip_mroute_opt(optname))
10821da177e4SLinus Torvalds 		return ip_mroute_getsockopt(sk, optname, optval, optlen);
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	if (get_user(len, optlen))
10851da177e4SLinus Torvalds 		return -EFAULT;
10861da177e4SLinus Torvalds 	if (len < 0)
10871da177e4SLinus Torvalds 		return -EINVAL;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	lock_sock(sk);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	switch (optname) {
10921da177e4SLinus Torvalds 	case IP_OPTIONS:
10931da177e4SLinus Torvalds 	{
10941da177e4SLinus Torvalds 		unsigned char optbuf[sizeof(struct ip_options)+40];
10951da177e4SLinus Torvalds 		struct ip_options *opt = (struct ip_options *)optbuf;
1096f6d8bd05SEric Dumazet 		struct ip_options_rcu *inet_opt;
1097f6d8bd05SEric Dumazet 
1098f6d8bd05SEric Dumazet 		inet_opt = rcu_dereference_protected(inet->inet_opt,
1099f6d8bd05SEric Dumazet 						     sock_owned_by_user(sk));
11001da177e4SLinus Torvalds 		opt->optlen = 0;
1101f6d8bd05SEric Dumazet 		if (inet_opt)
1102f6d8bd05SEric Dumazet 			memcpy(optbuf, &inet_opt->opt,
11031da177e4SLinus Torvalds 			       sizeof(struct ip_options) +
1104f6d8bd05SEric Dumazet 			       inet_opt->opt.optlen);
11051da177e4SLinus Torvalds 		release_sock(sk);
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 		if (opt->optlen == 0)
11081da177e4SLinus Torvalds 			return put_user(0, optlen);
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 		ip_options_undo(opt);
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 		len = min_t(unsigned int, len, opt->optlen);
11131da177e4SLinus Torvalds 		if (put_user(len, optlen))
11141da177e4SLinus Torvalds 			return -EFAULT;
11151da177e4SLinus Torvalds 		if (copy_to_user(optval, opt->__data, len))
11161da177e4SLinus Torvalds 			return -EFAULT;
11171da177e4SLinus Torvalds 		return 0;
11181da177e4SLinus Torvalds 	}
11191da177e4SLinus Torvalds 	case IP_PKTINFO:
11201da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
11211da177e4SLinus Torvalds 		break;
11221da177e4SLinus Torvalds 	case IP_RECVTTL:
11231da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
11241da177e4SLinus Torvalds 		break;
11251da177e4SLinus Torvalds 	case IP_RECVTOS:
11261da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
11271da177e4SLinus Torvalds 		break;
11281da177e4SLinus Torvalds 	case IP_RECVOPTS:
11291da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
11301da177e4SLinus Torvalds 		break;
11311da177e4SLinus Torvalds 	case IP_RETOPTS:
11321da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
11331da177e4SLinus Torvalds 		break;
11342c7946a7SCatherine Zhang 	case IP_PASSSEC:
11352c7946a7SCatherine Zhang 		val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
11362c7946a7SCatherine Zhang 		break;
1137e8b2dfe9SBalazs Scheidler 	case IP_RECVORIGDSTADDR:
1138e8b2dfe9SBalazs Scheidler 		val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1139e8b2dfe9SBalazs Scheidler 		break;
11401da177e4SLinus Torvalds 	case IP_TOS:
11411da177e4SLinus Torvalds 		val = inet->tos;
11421da177e4SLinus Torvalds 		break;
11431da177e4SLinus Torvalds 	case IP_TTL:
11441da177e4SLinus Torvalds 		val = (inet->uc_ttl == -1 ?
11451da177e4SLinus Torvalds 		       sysctl_ip_default_ttl :
11461da177e4SLinus Torvalds 		       inet->uc_ttl);
11471da177e4SLinus Torvalds 		break;
11481da177e4SLinus Torvalds 	case IP_HDRINCL:
11491da177e4SLinus Torvalds 		val = inet->hdrincl;
11501da177e4SLinus Torvalds 		break;
1151a89b4763SMichael Kerrisk 	case IP_NODEFRAG:
1152a89b4763SMichael Kerrisk 		val = inet->nodefrag;
1153a89b4763SMichael Kerrisk 		break;
11541da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
11551da177e4SLinus Torvalds 		val = inet->pmtudisc;
11561da177e4SLinus Torvalds 		break;
11571da177e4SLinus Torvalds 	case IP_MTU:
11581da177e4SLinus Torvalds 	{
11591da177e4SLinus Torvalds 		struct dst_entry *dst;
11601da177e4SLinus Torvalds 		val = 0;
11611da177e4SLinus Torvalds 		dst = sk_dst_get(sk);
11621da177e4SLinus Torvalds 		if (dst) {
11631da177e4SLinus Torvalds 			val = dst_mtu(dst);
11641da177e4SLinus Torvalds 			dst_release(dst);
11651da177e4SLinus Torvalds 		}
11661da177e4SLinus Torvalds 		if (!val) {
11671da177e4SLinus Torvalds 			release_sock(sk);
11681da177e4SLinus Torvalds 			return -ENOTCONN;
11691da177e4SLinus Torvalds 		}
11701da177e4SLinus Torvalds 		break;
11711da177e4SLinus Torvalds 	}
11721da177e4SLinus Torvalds 	case IP_RECVERR:
11731da177e4SLinus Torvalds 		val = inet->recverr;
11741da177e4SLinus Torvalds 		break;
11751da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
11761da177e4SLinus Torvalds 		val = inet->mc_ttl;
11771da177e4SLinus Torvalds 		break;
11781da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
11791da177e4SLinus Torvalds 		val = inet->mc_loop;
11801da177e4SLinus Torvalds 		break;
11811da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
11821da177e4SLinus Torvalds 	{
11831da177e4SLinus Torvalds 		struct in_addr addr;
11841da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(struct in_addr));
11851da177e4SLinus Torvalds 		addr.s_addr = inet->mc_addr;
11861da177e4SLinus Torvalds 		release_sock(sk);
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 		if (put_user(len, optlen))
11891da177e4SLinus Torvalds 			return -EFAULT;
11901da177e4SLinus Torvalds 		if (copy_to_user(optval, &addr, len))
11911da177e4SLinus Torvalds 			return -EFAULT;
11921da177e4SLinus Torvalds 		return 0;
11931da177e4SLinus Torvalds 	}
11941da177e4SLinus Torvalds 	case IP_MSFILTER:
11951da177e4SLinus Torvalds 	{
11961da177e4SLinus Torvalds 		struct ip_msfilter msf;
11971da177e4SLinus Torvalds 		int err;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 		if (len < IP_MSFILTER_SIZE(0)) {
12001da177e4SLinus Torvalds 			release_sock(sk);
12011da177e4SLinus Torvalds 			return -EINVAL;
12021da177e4SLinus Torvalds 		}
12031da177e4SLinus Torvalds 		if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
12041da177e4SLinus Torvalds 			release_sock(sk);
12051da177e4SLinus Torvalds 			return -EFAULT;
12061da177e4SLinus Torvalds 		}
12071da177e4SLinus Torvalds 		err = ip_mc_msfget(sk, &msf,
12081da177e4SLinus Torvalds 				   (struct ip_msfilter __user *)optval, optlen);
12091da177e4SLinus Torvalds 		release_sock(sk);
12101da177e4SLinus Torvalds 		return err;
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds 	case MCAST_MSFILTER:
12131da177e4SLinus Torvalds 	{
12141da177e4SLinus Torvalds 		struct group_filter gsf;
12151da177e4SLinus Torvalds 		int err;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 		if (len < GROUP_FILTER_SIZE(0)) {
12181da177e4SLinus Torvalds 			release_sock(sk);
12191da177e4SLinus Torvalds 			return -EINVAL;
12201da177e4SLinus Torvalds 		}
12211da177e4SLinus Torvalds 		if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
12221da177e4SLinus Torvalds 			release_sock(sk);
12231da177e4SLinus Torvalds 			return -EFAULT;
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 		err = ip_mc_gsfget(sk, &gsf,
12264d52cfbeSEric Dumazet 				   (struct group_filter __user *)optval,
12274d52cfbeSEric Dumazet 				   optlen);
12281da177e4SLinus Torvalds 		release_sock(sk);
12291da177e4SLinus Torvalds 		return err;
12301da177e4SLinus Torvalds 	}
1231f771bef9SNivedita Singhvi 	case IP_MULTICAST_ALL:
1232f771bef9SNivedita Singhvi 		val = inet->mc_all;
1233f771bef9SNivedita Singhvi 		break;
12341da177e4SLinus Torvalds 	case IP_PKTOPTIONS:
12351da177e4SLinus Torvalds 	{
12361da177e4SLinus Torvalds 		struct msghdr msg;
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 		release_sock(sk);
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_STREAM)
12411da177e4SLinus Torvalds 			return -ENOPROTOOPT;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 		msg.msg_control = optval;
12441da177e4SLinus Torvalds 		msg.msg_controllen = len;
1245dd23198eSDaniel Baluta 		msg.msg_flags = flags;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
12481da177e4SLinus Torvalds 			struct in_pktinfo info;
12491da177e4SLinus Torvalds 
1250c720c7e8SEric Dumazet 			info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1251c720c7e8SEric Dumazet 			info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
12521da177e4SLinus Torvalds 			info.ipi_ifindex = inet->mc_index;
12531da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
12541da177e4SLinus Torvalds 		}
12551da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_TTL) {
12561da177e4SLinus Torvalds 			int hlim = inet->mc_ttl;
12571da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
12581da177e4SLinus Torvalds 		}
12591da177e4SLinus Torvalds 		len -= msg.msg_controllen;
12601da177e4SLinus Torvalds 		return put_user(len, optlen);
12611da177e4SLinus Torvalds 	}
12621da177e4SLinus Torvalds 	case IP_FREEBIND:
12631da177e4SLinus Torvalds 		val = inet->freebind;
12641da177e4SLinus Torvalds 		break;
1265f5715aeaSKOVACS Krisztian 	case IP_TRANSPARENT:
1266f5715aeaSKOVACS Krisztian 		val = inet->transparent;
1267f5715aeaSKOVACS Krisztian 		break;
1268d218d111SStephen Hemminger 	case IP_MINTTL:
1269d218d111SStephen Hemminger 		val = inet->min_ttl;
1270d218d111SStephen Hemminger 		break;
12711da177e4SLinus Torvalds 	default:
12721da177e4SLinus Torvalds 		release_sock(sk);
12731da177e4SLinus Torvalds 		return -ENOPROTOOPT;
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds 	release_sock(sk);
12761da177e4SLinus Torvalds 
1277951e07c9SDavid S. Miller 	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
12781da177e4SLinus Torvalds 		unsigned char ucval = (unsigned char)val;
12791da177e4SLinus Torvalds 		len = 1;
12801da177e4SLinus Torvalds 		if (put_user(len, optlen))
12811da177e4SLinus Torvalds 			return -EFAULT;
12821da177e4SLinus Torvalds 		if (copy_to_user(optval, &ucval, 1))
12831da177e4SLinus Torvalds 			return -EFAULT;
12841da177e4SLinus Torvalds 	} else {
12851da177e4SLinus Torvalds 		len = min_t(unsigned int, sizeof(int), len);
12861da177e4SLinus Torvalds 		if (put_user(len, optlen))
12871da177e4SLinus Torvalds 			return -EFAULT;
12881da177e4SLinus Torvalds 		if (copy_to_user(optval, &val, len))
12891da177e4SLinus Torvalds 			return -EFAULT;
12901da177e4SLinus Torvalds 	}
12911da177e4SLinus Torvalds 	return 0;
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
12943fdadf7dSDmitry Mishin int ip_getsockopt(struct sock *sk, int level,
12953fdadf7dSDmitry Mishin 		  int optname, char __user *optval, int __user *optlen)
12963fdadf7dSDmitry Mishin {
12973fdadf7dSDmitry Mishin 	int err;
12983fdadf7dSDmitry Mishin 
1299dd23198eSDaniel Baluta 	err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
13003fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
13013fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
13026a9fb947SPavel Emelyanov 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
13036a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
13043fdadf7dSDmitry Mishin 		int len;
13053fdadf7dSDmitry Mishin 
13063fdadf7dSDmitry Mishin 		if (get_user(len, optlen))
13073fdadf7dSDmitry Mishin 			return -EFAULT;
13083fdadf7dSDmitry Mishin 
13093fdadf7dSDmitry Mishin 		lock_sock(sk);
13103fdadf7dSDmitry Mishin 		err = nf_getsockopt(sk, PF_INET, optname, optval,
13113fdadf7dSDmitry Mishin 				&len);
13123fdadf7dSDmitry Mishin 		release_sock(sk);
13133fdadf7dSDmitry Mishin 		if (err >= 0)
13143fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
13153fdadf7dSDmitry Mishin 		return err;
13163fdadf7dSDmitry Mishin 	}
13173fdadf7dSDmitry Mishin #endif
13183fdadf7dSDmitry Mishin 	return err;
13193fdadf7dSDmitry Mishin }
13204d52cfbeSEric Dumazet EXPORT_SYMBOL(ip_getsockopt);
13213fdadf7dSDmitry Mishin 
13223fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
1323543d9cfeSArnaldo Carvalho de Melo int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1324543d9cfeSArnaldo Carvalho de Melo 			 char __user *optval, int __user *optlen)
13253fdadf7dSDmitry Mishin {
132642908c69SDavid L Stevens 	int err;
132742908c69SDavid L Stevens 
132842908c69SDavid L Stevens 	if (optname == MCAST_MSFILTER)
132942908c69SDavid L Stevens 		return compat_mc_getsockopt(sk, level, optname, optval, optlen,
133042908c69SDavid L Stevens 			ip_getsockopt);
133142908c69SDavid L Stevens 
1332dd23198eSDaniel Baluta 	err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1333dd23198eSDaniel Baluta 		MSG_CMSG_COMPAT);
133442908c69SDavid L Stevens 
13353fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
13363fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
13376a9fb947SPavel Emelyanov 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
13386a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
13393fdadf7dSDmitry Mishin 		int len;
13403fdadf7dSDmitry Mishin 
13413fdadf7dSDmitry Mishin 		if (get_user(len, optlen))
13423fdadf7dSDmitry Mishin 			return -EFAULT;
13433fdadf7dSDmitry Mishin 
13443fdadf7dSDmitry Mishin 		lock_sock(sk);
1345543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
13463fdadf7dSDmitry Mishin 		release_sock(sk);
13473fdadf7dSDmitry Mishin 		if (err >= 0)
13483fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
13493fdadf7dSDmitry Mishin 		return err;
13503fdadf7dSDmitry Mishin 	}
13513fdadf7dSDmitry Mishin #endif
13523fdadf7dSDmitry Mishin 	return err;
13533fdadf7dSDmitry Mishin }
1354543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_getsockopt);
13553fdadf7dSDmitry Mishin #endif
1356