xref: /linux/net/ipv4/ip_sockglue.c (revision eddc9ec53be2ecdbf4efe0efd4a83052594f0ac0)
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  * Version:	$Id: ip_sockglue.c,v 1.62 2002/02/01 22:01:04 davem Exp $
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Authors:	see ip.c
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Fixes:
131da177e4SLinus Torvalds  *		Many		:	Split from ip.c , see ip.c for history.
141da177e4SLinus Torvalds  *		Martin Mares	:	TOS setting fixed.
151da177e4SLinus Torvalds  *		Alan Cox	:	Fixed a couple of oopses in Martin's
161da177e4SLinus Torvalds  *					TOS tweaks.
171da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/mm.h>
231da177e4SLinus Torvalds #include <linux/skbuff.h>
241da177e4SLinus Torvalds #include <linux/ip.h>
251da177e4SLinus Torvalds #include <linux/icmp.h>
2614c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
271da177e4SLinus Torvalds #include <linux/netdevice.h>
281da177e4SLinus Torvalds #include <net/sock.h>
291da177e4SLinus Torvalds #include <net/ip.h>
301da177e4SLinus Torvalds #include <net/icmp.h>
31d83d8461SArnaldo Carvalho de Melo #include <net/tcp_states.h>
321da177e4SLinus Torvalds #include <linux/udp.h>
331da177e4SLinus Torvalds #include <linux/igmp.h>
341da177e4SLinus Torvalds #include <linux/netfilter.h>
351da177e4SLinus Torvalds #include <linux/route.h>
361da177e4SLinus Torvalds #include <linux/mroute.h>
371da177e4SLinus Torvalds #include <net/route.h>
381da177e4SLinus Torvalds #include <net/xfrm.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
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  *	SOL_IP control messages.
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	struct in_pktinfo info;
601da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *)skb->dst;
611da177e4SLinus Torvalds 
62*eddc9ec5SArnaldo Carvalho de Melo 	info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
631da177e4SLinus Torvalds 	if (rt) {
641da177e4SLinus Torvalds 		info.ipi_ifindex = rt->rt_iif;
651da177e4SLinus Torvalds 		info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
661da177e4SLinus Torvalds 	} else {
671da177e4SLinus Torvalds 		info.ipi_ifindex = 0;
681da177e4SLinus Torvalds 		info.ipi_spec_dst.s_addr = 0;
691da177e4SLinus Torvalds 	}
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
751da177e4SLinus Torvalds {
76*eddc9ec5SArnaldo Carvalho de Melo 	int ttl = ip_hdr(skb)->ttl;
771da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
811da177e4SLinus Torvalds {
82*eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
881da177e4SLinus Torvalds 		return;
891da177e4SLinus Torvalds 
90*eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
91*eddc9ec5SArnaldo Carvalho de Melo 		 ip_hdr(skb) + 1);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	unsigned char optbuf[sizeof(struct ip_options) + 40];
981da177e4SLinus Torvalds 	struct ip_options * opt = (struct ip_options*)optbuf;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	if (IPCB(skb)->opt.optlen == 0)
1011da177e4SLinus Torvalds 		return;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	if (ip_options_echo(opt, skb)) {
1041da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
1051da177e4SLinus Torvalds 		return;
1061da177e4SLinus Torvalds 	}
1071da177e4SLinus Torvalds 	ip_options_undo(opt);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1122c7946a7SCatherine Zhang static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
1132c7946a7SCatherine Zhang {
1142c7946a7SCatherine Zhang 	char *secdata;
115dc49c1f9SCatherine Zhang 	u32 seclen, secid;
1162c7946a7SCatherine Zhang 	int err;
1172c7946a7SCatherine Zhang 
118dc49c1f9SCatherine Zhang 	err = security_socket_getpeersec_dgram(NULL, skb, &secid);
119dc49c1f9SCatherine Zhang 	if (err)
120dc49c1f9SCatherine Zhang 		return;
121dc49c1f9SCatherine Zhang 
122dc49c1f9SCatherine Zhang 	err = security_secid_to_secctx(secid, &secdata, &seclen);
1232c7946a7SCatherine Zhang 	if (err)
1242c7946a7SCatherine Zhang 		return;
1252c7946a7SCatherine Zhang 
1262c7946a7SCatherine Zhang 	put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
127dc49c1f9SCatherine Zhang 	security_release_secctx(secdata, seclen);
1282c7946a7SCatherine Zhang }
1292c7946a7SCatherine Zhang 
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(skb->sk);
1341da177e4SLinus Torvalds 	unsigned flags = inet->cmsg_flags;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/* Ordered by supposed usage frequency */
1371da177e4SLinus Torvalds 	if (flags & 1)
1381da177e4SLinus Torvalds 		ip_cmsg_recv_pktinfo(msg, skb);
1391da177e4SLinus Torvalds 	if ((flags>>=1) == 0)
1401da177e4SLinus Torvalds 		return;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	if (flags & 1)
1431da177e4SLinus Torvalds 		ip_cmsg_recv_ttl(msg, skb);
1441da177e4SLinus Torvalds 	if ((flags>>=1) == 0)
1451da177e4SLinus Torvalds 		return;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	if (flags & 1)
1481da177e4SLinus Torvalds 		ip_cmsg_recv_tos(msg, skb);
1491da177e4SLinus Torvalds 	if ((flags>>=1) == 0)
1501da177e4SLinus Torvalds 		return;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	if (flags & 1)
1531da177e4SLinus Torvalds 		ip_cmsg_recv_opts(msg, skb);
1541da177e4SLinus Torvalds 	if ((flags>>=1) == 0)
1551da177e4SLinus Torvalds 		return;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	if (flags & 1)
1581da177e4SLinus Torvalds 		ip_cmsg_recv_retopts(msg, skb);
1592c7946a7SCatherine Zhang 	if ((flags>>=1) == 0)
1602c7946a7SCatherine Zhang 		return;
1612c7946a7SCatherine Zhang 
1622c7946a7SCatherine Zhang 	if (flags & 1)
1632c7946a7SCatherine Zhang 		ip_cmsg_recv_security(msg, skb);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds int ip_cmsg_send(struct msghdr *msg, struct ipcm_cookie *ipc)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	int err;
1691da177e4SLinus Torvalds 	struct cmsghdr *cmsg;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1721da177e4SLinus Torvalds 		if (!CMSG_OK(msg, cmsg))
1731da177e4SLinus Torvalds 			return -EINVAL;
1741da177e4SLinus Torvalds 		if (cmsg->cmsg_level != SOL_IP)
1751da177e4SLinus Torvalds 			continue;
1761da177e4SLinus Torvalds 		switch (cmsg->cmsg_type) {
1771da177e4SLinus Torvalds 		case IP_RETOPTS:
1781da177e4SLinus Torvalds 			err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
1794c6ea29dSArnaldo Carvalho de Melo 			err = ip_options_get(&ipc->opt, CMSG_DATA(cmsg), err < 40 ? err : 40);
1801da177e4SLinus Torvalds 			if (err)
1811da177e4SLinus Torvalds 				return err;
1821da177e4SLinus Torvalds 			break;
1831da177e4SLinus Torvalds 		case IP_PKTINFO:
1841da177e4SLinus Torvalds 		{
1851da177e4SLinus Torvalds 			struct in_pktinfo *info;
1861da177e4SLinus Torvalds 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
1871da177e4SLinus Torvalds 				return -EINVAL;
1881da177e4SLinus Torvalds 			info = (struct in_pktinfo *)CMSG_DATA(cmsg);
1891da177e4SLinus Torvalds 			ipc->oif = info->ipi_ifindex;
1901da177e4SLinus Torvalds 			ipc->addr = info->ipi_spec_dst.s_addr;
1911da177e4SLinus Torvalds 			break;
1921da177e4SLinus Torvalds 		}
1931da177e4SLinus Torvalds 		default:
1941da177e4SLinus Torvalds 			return -EINVAL;
1951da177e4SLinus Torvalds 		}
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds 	return 0;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /* Special input handler for packets caught by router alert option.
2021da177e4SLinus Torvalds    They are selected only by protocol field, and then processed likely
2031da177e4SLinus Torvalds    local ones; but only if someone wants them! Otherwise, router
2041da177e4SLinus Torvalds    not running rsvpd will kill RSVP.
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds    It is user level problem, what it will make with them.
2071da177e4SLinus Torvalds    I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
2081da177e4SLinus Torvalds    but receiver should be enough clever f.e. to forward mtrace requests,
2091da177e4SLinus Torvalds    sent to multicast group to reach destination designated router.
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds struct ip_ra_chain *ip_ra_chain;
2121da177e4SLinus Torvalds DEFINE_RWLOCK(ip_ra_lock);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds int ip_ra_control(struct sock *sk, unsigned char on, void (*destructor)(struct sock *))
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct ip_ra_chain *ra, *new_ra, **rap;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
2191da177e4SLinus Torvalds 		return -EINVAL;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	write_lock_bh(&ip_ra_lock);
2241da177e4SLinus Torvalds 	for (rap = &ip_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
2251da177e4SLinus Torvalds 		if (ra->sk == sk) {
2261da177e4SLinus Torvalds 			if (on) {
2271da177e4SLinus Torvalds 				write_unlock_bh(&ip_ra_lock);
2281da177e4SLinus Torvalds 				kfree(new_ra);
2291da177e4SLinus Torvalds 				return -EADDRINUSE;
2301da177e4SLinus Torvalds 			}
2311da177e4SLinus Torvalds 			*rap = ra->next;
2321da177e4SLinus Torvalds 			write_unlock_bh(&ip_ra_lock);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 			if (ra->destructor)
2351da177e4SLinus Torvalds 				ra->destructor(sk);
2361da177e4SLinus Torvalds 			sock_put(sk);
2371da177e4SLinus Torvalds 			kfree(ra);
2381da177e4SLinus Torvalds 			return 0;
2391da177e4SLinus Torvalds 		}
2401da177e4SLinus Torvalds 	}
2411da177e4SLinus Torvalds 	if (new_ra == NULL) {
2421da177e4SLinus Torvalds 		write_unlock_bh(&ip_ra_lock);
2431da177e4SLinus Torvalds 		return -ENOBUFS;
2441da177e4SLinus Torvalds 	}
2451da177e4SLinus Torvalds 	new_ra->sk = sk;
2461da177e4SLinus Torvalds 	new_ra->destructor = destructor;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	new_ra->next = ra;
2491da177e4SLinus Torvalds 	*rap = new_ra;
2501da177e4SLinus Torvalds 	sock_hold(sk);
2511da177e4SLinus Torvalds 	write_unlock_bh(&ip_ra_lock);
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	return 0;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
25735986b32SAl Viro 		   __be16 port, u32 info, u8 *payload)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2601da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	if (!inet->recverr)
2631da177e4SLinus Torvalds 		return;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	skb = skb_clone(skb, GFP_ATOMIC);
2661da177e4SLinus Torvalds 	if (!skb)
2671da177e4SLinus Torvalds 		return;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
2701da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
2711da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
2721da177e4SLinus Torvalds 	serr->ee.ee_type = skb->h.icmph->type;
2731da177e4SLinus Torvalds 	serr->ee.ee_code = skb->h.icmph->code;
2741da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
2751da177e4SLinus Torvalds 	serr->ee.ee_info = info;
2761da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
277d56f90a7SArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&(((struct iphdr *)(skb->h.icmph + 1))->daddr) -
278d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb);
2791da177e4SLinus Torvalds 	serr->port = port;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	skb->h.raw = payload;
2821da177e4SLinus Torvalds 	if (!skb_pull(skb, payload - skb->data) ||
2831da177e4SLinus Torvalds 	    sock_queue_err_skb(sk, skb))
2841da177e4SLinus Torvalds 		kfree_skb(skb);
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2870579016eSAl Viro void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2901da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
2911da177e4SLinus Torvalds 	struct iphdr *iph;
2921da177e4SLinus Torvalds 	struct sk_buff *skb;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	if (!inet->recverr)
2951da177e4SLinus Torvalds 		return;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
2981da177e4SLinus Torvalds 	if (!skb)
2991da177e4SLinus Torvalds 		return;
3001da177e4SLinus Torvalds 
3012ca9e6f2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr));
3022ca9e6f2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
303*eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
3041da177e4SLinus Torvalds 	iph->daddr = daddr;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3071da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
3081da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
3091da177e4SLinus Torvalds 	serr->ee.ee_type = 0;
3101da177e4SLinus Torvalds 	serr->ee.ee_code = 0;
3111da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
3121da177e4SLinus Torvalds 	serr->ee.ee_info = info;
3131da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
314d56f90a7SArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
3151da177e4SLinus Torvalds 	serr->port = port;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	skb->h.raw = skb->tail;
3181da177e4SLinus Torvalds 	__skb_pull(skb, skb->tail - skb->data);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	if (sock_queue_err_skb(sk, skb))
3211da177e4SLinus Torvalds 		kfree_skb(skb);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds /*
3251da177e4SLinus Torvalds  *	Handle MSG_ERRQUEUE
3261da177e4SLinus Torvalds  */
3271da177e4SLinus Torvalds int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3301da177e4SLinus Torvalds 	struct sk_buff *skb, *skb2;
3311da177e4SLinus Torvalds 	struct sockaddr_in *sin;
3321da177e4SLinus Torvalds 	struct {
3331da177e4SLinus Torvalds 		struct sock_extended_err ee;
3341da177e4SLinus Torvalds 		struct sockaddr_in	 offender;
3351da177e4SLinus Torvalds 	} errhdr;
3361da177e4SLinus Torvalds 	int err;
3371da177e4SLinus Torvalds 	int copied;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	err = -EAGAIN;
3401da177e4SLinus Torvalds 	skb = skb_dequeue(&sk->sk_error_queue);
3411da177e4SLinus Torvalds 	if (skb == NULL)
3421da177e4SLinus Torvalds 		goto out;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	copied = skb->len;
3451da177e4SLinus Torvalds 	if (copied > len) {
3461da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
3471da177e4SLinus Torvalds 		copied = len;
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3501da177e4SLinus Torvalds 	if (err)
3511da177e4SLinus Torvalds 		goto out_free_skb;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	sock_recv_timestamp(msg, sk, skb);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	sin = (struct sockaddr_in *)msg->msg_name;
3581da177e4SLinus Torvalds 	if (sin) {
3591da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
360d56f90a7SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
361d56f90a7SArnaldo Carvalho de Melo 						   serr->addr_offset);
3621da177e4SLinus Torvalds 		sin->sin_port = serr->port;
3631da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
3671da177e4SLinus Torvalds 	sin = &errhdr.offender;
3681da177e4SLinus Torvalds 	sin->sin_family = AF_UNSPEC;
3691da177e4SLinus Torvalds 	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
3701da177e4SLinus Torvalds 		struct inet_sock *inet = inet_sk(sk);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
373*eddc9ec5SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
3741da177e4SLinus Torvalds 		sin->sin_port = 0;
3751da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3761da177e4SLinus Torvalds 		if (inet->cmsg_flags)
3771da177e4SLinus Torvalds 			ip_cmsg_recv(msg, skb);
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	/* Now we could try to dump offended packet options */
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	msg->msg_flags |= MSG_ERRQUEUE;
3851da177e4SLinus Torvalds 	err = copied;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* Reset and regenerate socket error */
388e0f9f858SHerbert Xu 	spin_lock_bh(&sk->sk_error_queue.lock);
3891da177e4SLinus Torvalds 	sk->sk_err = 0;
3901da177e4SLinus Torvalds 	if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
3911da177e4SLinus Torvalds 		sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
392e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
3931da177e4SLinus Torvalds 		sk->sk_error_report(sk);
3941da177e4SLinus Torvalds 	} else
395e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds out_free_skb:
3981da177e4SLinus Torvalds 	kfree_skb(skb);
3991da177e4SLinus Torvalds out:
4001da177e4SLinus Torvalds 	return err;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds /*
4051da177e4SLinus Torvalds  *	Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
4061da177e4SLinus Torvalds  *	an IP socket.
4071da177e4SLinus Torvalds  */
4081da177e4SLinus Torvalds 
4093fdadf7dSDmitry Mishin static int do_ip_setsockopt(struct sock *sk, int level,
4103fdadf7dSDmitry Mishin 			    int optname, char __user *optval, int optlen)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4131da177e4SLinus Torvalds 	int val=0,err;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
4161da177e4SLinus Torvalds 			     (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
4171da177e4SLinus Torvalds 			     (1<<IP_RETOPTS) | (1<<IP_TOS) |
4181da177e4SLinus Torvalds 			     (1<<IP_TTL) | (1<<IP_HDRINCL) |
4191da177e4SLinus Torvalds 			     (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
4202c7946a7SCatherine Zhang 			     (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
4212c7946a7SCatherine Zhang 			     (1<<IP_PASSSEC))) ||
4221da177e4SLinus Torvalds 	    optname == IP_MULTICAST_TTL ||
4231da177e4SLinus Torvalds 	    optname == IP_MULTICAST_LOOP) {
4241da177e4SLinus Torvalds 		if (optlen >= sizeof(int)) {
4251da177e4SLinus Torvalds 			if (get_user(val, (int __user *) optval))
4261da177e4SLinus Torvalds 				return -EFAULT;
4271da177e4SLinus Torvalds 		} else if (optlen >= sizeof(char)) {
4281da177e4SLinus Torvalds 			unsigned char ucval;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 			if (get_user(ucval, (unsigned char __user *) optval))
4311da177e4SLinus Torvalds 				return -EFAULT;
4321da177e4SLinus Torvalds 			val = (int) ucval;
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	/* If optlen==0, it is equivalent to val == 0 */
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
4391da177e4SLinus Torvalds 	if (optname >= MRT_BASE && optname <= (MRT_BASE + 10))
4401da177e4SLinus Torvalds 		return ip_mroute_setsockopt(sk,optname,optval,optlen);
4411da177e4SLinus Torvalds #endif
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	err = 0;
4441da177e4SLinus Torvalds 	lock_sock(sk);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	switch (optname) {
4471da177e4SLinus Torvalds 	case IP_OPTIONS:
4481da177e4SLinus Torvalds 	{
4491da177e4SLinus Torvalds 		struct ip_options * opt = NULL;
4501da177e4SLinus Torvalds 		if (optlen > 40 || optlen < 0)
4511da177e4SLinus Torvalds 			goto e_inval;
4524c6ea29dSArnaldo Carvalho de Melo 		err = ip_options_get_from_user(&opt, optval, optlen);
4531da177e4SLinus Torvalds 		if (err)
4541da177e4SLinus Torvalds 			break;
455d83d8461SArnaldo Carvalho de Melo 		if (inet->is_icsk) {
456d83d8461SArnaldo Carvalho de Melo 			struct inet_connection_sock *icsk = inet_csk(sk);
4571da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4581da177e4SLinus Torvalds 			if (sk->sk_family == PF_INET ||
4591da177e4SLinus Torvalds 			    (!((1 << sk->sk_state) &
4601da177e4SLinus Torvalds 			       (TCPF_LISTEN | TCPF_CLOSE)) &&
4611da177e4SLinus Torvalds 			     inet->daddr != LOOPBACK4_IPV6)) {
4621da177e4SLinus Torvalds #endif
4631da177e4SLinus Torvalds 				if (inet->opt)
464d83d8461SArnaldo Carvalho de Melo 					icsk->icsk_ext_hdr_len -= inet->opt->optlen;
4651da177e4SLinus Torvalds 				if (opt)
466d83d8461SArnaldo Carvalho de Melo 					icsk->icsk_ext_hdr_len += opt->optlen;
467d83d8461SArnaldo Carvalho de Melo 				icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
4681da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4691da177e4SLinus Torvalds 			}
4701da177e4SLinus Torvalds #endif
4711da177e4SLinus Torvalds 		}
4721da177e4SLinus Torvalds 		opt = xchg(&inet->opt, opt);
4731da177e4SLinus Torvalds 		kfree(opt);
4741da177e4SLinus Torvalds 		break;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 	case IP_PKTINFO:
4771da177e4SLinus Torvalds 		if (val)
4781da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_PKTINFO;
4791da177e4SLinus Torvalds 		else
4801da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
4811da177e4SLinus Torvalds 		break;
4821da177e4SLinus Torvalds 	case IP_RECVTTL:
4831da177e4SLinus Torvalds 		if (val)
4841da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TTL;
4851da177e4SLinus Torvalds 		else
4861da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TTL;
4871da177e4SLinus Torvalds 		break;
4881da177e4SLinus Torvalds 	case IP_RECVTOS:
4891da177e4SLinus Torvalds 		if (val)
4901da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_TOS;
4911da177e4SLinus Torvalds 		else
4921da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_TOS;
4931da177e4SLinus Torvalds 		break;
4941da177e4SLinus Torvalds 	case IP_RECVOPTS:
4951da177e4SLinus Torvalds 		if (val)
4961da177e4SLinus Torvalds 			inet->cmsg_flags |=  IP_CMSG_RECVOPTS;
4971da177e4SLinus Torvalds 		else
4981da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
4991da177e4SLinus Torvalds 		break;
5001da177e4SLinus Torvalds 	case IP_RETOPTS:
5011da177e4SLinus Torvalds 		if (val)
5021da177e4SLinus Torvalds 			inet->cmsg_flags |= IP_CMSG_RETOPTS;
5031da177e4SLinus Torvalds 		else
5041da177e4SLinus Torvalds 			inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
5051da177e4SLinus Torvalds 		break;
5062c7946a7SCatherine Zhang 	case IP_PASSSEC:
5072c7946a7SCatherine Zhang 		if (val)
5082c7946a7SCatherine Zhang 			inet->cmsg_flags |= IP_CMSG_PASSSEC;
5092c7946a7SCatherine Zhang 		else
5102c7946a7SCatherine Zhang 			inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
5112c7946a7SCatherine Zhang 		break;
5121da177e4SLinus Torvalds 	case IP_TOS:	/* This sets both TOS and Precedence */
5131da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM) {
5141da177e4SLinus Torvalds 			val &= ~3;
5151da177e4SLinus Torvalds 			val |= inet->tos & 3;
5161da177e4SLinus Torvalds 		}
5171da177e4SLinus Torvalds 		if (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
5181da177e4SLinus Torvalds 		    !capable(CAP_NET_ADMIN)) {
5191da177e4SLinus Torvalds 			err = -EPERM;
5201da177e4SLinus Torvalds 			break;
5211da177e4SLinus Torvalds 		}
5221da177e4SLinus Torvalds 		if (inet->tos != val) {
5231da177e4SLinus Torvalds 			inet->tos = val;
5241da177e4SLinus Torvalds 			sk->sk_priority = rt_tos2priority(val);
5251da177e4SLinus Torvalds 			sk_dst_reset(sk);
5261da177e4SLinus Torvalds 		}
5271da177e4SLinus Torvalds 		break;
5281da177e4SLinus Torvalds 	case IP_TTL:
5291da177e4SLinus Torvalds 		if (optlen<1)
5301da177e4SLinus Torvalds 			goto e_inval;
5311da177e4SLinus Torvalds 		if (val != -1 && (val < 1 || val>255))
5321da177e4SLinus Torvalds 			goto e_inval;
5331da177e4SLinus Torvalds 		inet->uc_ttl = val;
5341da177e4SLinus Torvalds 		break;
5351da177e4SLinus Torvalds 	case IP_HDRINCL:
5361da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_RAW) {
5371da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
5381da177e4SLinus Torvalds 			break;
5391da177e4SLinus Torvalds 		}
5401da177e4SLinus Torvalds 		inet->hdrincl = val ? 1 : 0;
5411da177e4SLinus Torvalds 		break;
5421da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
5431da177e4SLinus Torvalds 		if (val<0 || val>2)
5441da177e4SLinus Torvalds 			goto e_inval;
5451da177e4SLinus Torvalds 		inet->pmtudisc = val;
5461da177e4SLinus Torvalds 		break;
5471da177e4SLinus Torvalds 	case IP_RECVERR:
5481da177e4SLinus Torvalds 		inet->recverr = !!val;
5491da177e4SLinus Torvalds 		if (!val)
5501da177e4SLinus Torvalds 			skb_queue_purge(&sk->sk_error_queue);
5511da177e4SLinus Torvalds 		break;
5521da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
5531da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
5541da177e4SLinus Torvalds 			goto e_inval;
5551da177e4SLinus Torvalds 		if (optlen<1)
5561da177e4SLinus Torvalds 			goto e_inval;
5571da177e4SLinus Torvalds 		if (val==-1)
5581da177e4SLinus Torvalds 			val = 1;
5591da177e4SLinus Torvalds 		if (val < 0 || val > 255)
5601da177e4SLinus Torvalds 			goto e_inval;
5611da177e4SLinus Torvalds 		inet->mc_ttl = val;
5621da177e4SLinus Torvalds 		break;
5631da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
5641da177e4SLinus Torvalds 		if (optlen<1)
5651da177e4SLinus Torvalds 			goto e_inval;
5661da177e4SLinus Torvalds 		inet->mc_loop = !!val;
5671da177e4SLinus Torvalds 		break;
5681da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
5691da177e4SLinus Torvalds 	{
5701da177e4SLinus Torvalds 		struct ip_mreqn mreq;
5711da177e4SLinus Torvalds 		struct net_device *dev = NULL;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
5741da177e4SLinus Torvalds 			goto e_inval;
5751da177e4SLinus Torvalds 		/*
5761da177e4SLinus Torvalds 		 *	Check the arguments are allowable
5771da177e4SLinus Torvalds 		 */
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 		err = -EFAULT;
5801da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
5811da177e4SLinus Torvalds 			if (copy_from_user(&mreq,optval,sizeof(mreq)))
5821da177e4SLinus Torvalds 				break;
5831da177e4SLinus Torvalds 		} else {
5841da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
5851da177e4SLinus Torvalds 			if (optlen >= sizeof(struct in_addr) &&
5861da177e4SLinus Torvalds 			    copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
5871da177e4SLinus Torvalds 				break;
5881da177e4SLinus Torvalds 		}
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 		if (!mreq.imr_ifindex) {
5911da177e4SLinus Torvalds 			if (mreq.imr_address.s_addr == INADDR_ANY) {
5921da177e4SLinus Torvalds 				inet->mc_index = 0;
5931da177e4SLinus Torvalds 				inet->mc_addr  = 0;
5941da177e4SLinus Torvalds 				err = 0;
5951da177e4SLinus Torvalds 				break;
5961da177e4SLinus Torvalds 			}
5971da177e4SLinus Torvalds 			dev = ip_dev_find(mreq.imr_address.s_addr);
5981da177e4SLinus Torvalds 			if (dev) {
5991da177e4SLinus Torvalds 				mreq.imr_ifindex = dev->ifindex;
6001da177e4SLinus Torvalds 				dev_put(dev);
6011da177e4SLinus Torvalds 			}
6021da177e4SLinus Torvalds 		} else
6031da177e4SLinus Torvalds 			dev = __dev_get_by_index(mreq.imr_ifindex);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
6071da177e4SLinus Torvalds 		if (!dev)
6081da177e4SLinus Torvalds 			break;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 		err = -EINVAL;
6111da177e4SLinus Torvalds 		if (sk->sk_bound_dev_if &&
6121da177e4SLinus Torvalds 		    mreq.imr_ifindex != sk->sk_bound_dev_if)
6131da177e4SLinus Torvalds 			break;
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 		inet->mc_index = mreq.imr_ifindex;
6161da177e4SLinus Torvalds 		inet->mc_addr  = mreq.imr_address.s_addr;
6171da177e4SLinus Torvalds 		err = 0;
6181da177e4SLinus Torvalds 		break;
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	case IP_ADD_MEMBERSHIP:
6221da177e4SLinus Torvalds 	case IP_DROP_MEMBERSHIP:
6231da177e4SLinus Torvalds 	{
6241da177e4SLinus Torvalds 		struct ip_mreqn mreq;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 		if (optlen < sizeof(struct ip_mreq))
6271da177e4SLinus Torvalds 			goto e_inval;
6281da177e4SLinus Torvalds 		err = -EFAULT;
6291da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
6301da177e4SLinus Torvalds 			if (copy_from_user(&mreq,optval,sizeof(mreq)))
6311da177e4SLinus Torvalds 				break;
6321da177e4SLinus Torvalds 		} else {
6331da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
6341da177e4SLinus Torvalds 			if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
6351da177e4SLinus Torvalds 				break;
6361da177e4SLinus Torvalds 		}
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 		if (optname == IP_ADD_MEMBERSHIP)
6391da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
6401da177e4SLinus Torvalds 		else
6411da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
6421da177e4SLinus Torvalds 		break;
6431da177e4SLinus Torvalds 	}
6441da177e4SLinus Torvalds 	case IP_MSFILTER:
6451da177e4SLinus Torvalds 	{
6461da177e4SLinus Torvalds 		extern int sysctl_igmp_max_msf;
6471da177e4SLinus Torvalds 		struct ip_msfilter *msf;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 		if (optlen < IP_MSFILTER_SIZE(0))
6501da177e4SLinus Torvalds 			goto e_inval;
6511da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
6521da177e4SLinus Torvalds 			err = -ENOBUFS;
6531da177e4SLinus Torvalds 			break;
6541da177e4SLinus Torvalds 		}
6558b3a7005SKris Katterjohn 		msf = kmalloc(optlen, GFP_KERNEL);
6561da177e4SLinus Torvalds 		if (msf == 0) {
6571da177e4SLinus Torvalds 			err = -ENOBUFS;
6581da177e4SLinus Torvalds 			break;
6591da177e4SLinus Torvalds 		}
6601da177e4SLinus Torvalds 		err = -EFAULT;
6611da177e4SLinus Torvalds 		if (copy_from_user(msf, optval, optlen)) {
6621da177e4SLinus Torvalds 			kfree(msf);
6631da177e4SLinus Torvalds 			break;
6641da177e4SLinus Torvalds 		}
6651da177e4SLinus Torvalds 		/* numsrc >= (1G-4) overflow in 32 bits */
6661da177e4SLinus Torvalds 		if (msf->imsf_numsrc >= 0x3ffffffcU ||
6671da177e4SLinus Torvalds 		    msf->imsf_numsrc > sysctl_igmp_max_msf) {
6681da177e4SLinus Torvalds 			kfree(msf);
6691da177e4SLinus Torvalds 			err = -ENOBUFS;
6701da177e4SLinus Torvalds 			break;
6711da177e4SLinus Torvalds 		}
6721da177e4SLinus Torvalds 		if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
6731da177e4SLinus Torvalds 			kfree(msf);
6741da177e4SLinus Torvalds 			err = -EINVAL;
6751da177e4SLinus Torvalds 			break;
6761da177e4SLinus Torvalds 		}
6771da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, 0);
6781da177e4SLinus Torvalds 		kfree(msf);
6791da177e4SLinus Torvalds 		break;
6801da177e4SLinus Torvalds 	}
6811da177e4SLinus Torvalds 	case IP_BLOCK_SOURCE:
6821da177e4SLinus Torvalds 	case IP_UNBLOCK_SOURCE:
6831da177e4SLinus Torvalds 	case IP_ADD_SOURCE_MEMBERSHIP:
6841da177e4SLinus Torvalds 	case IP_DROP_SOURCE_MEMBERSHIP:
6851da177e4SLinus Torvalds 	{
6861da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
6871da177e4SLinus Torvalds 		int omode, add;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 		if (optlen != sizeof(struct ip_mreq_source))
6901da177e4SLinus Torvalds 			goto e_inval;
6911da177e4SLinus Torvalds 		if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
6921da177e4SLinus Torvalds 			err = -EFAULT;
6931da177e4SLinus Torvalds 			break;
6941da177e4SLinus Torvalds 		}
6951da177e4SLinus Torvalds 		if (optname == IP_BLOCK_SOURCE) {
6961da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
6971da177e4SLinus Torvalds 			add = 1;
6981da177e4SLinus Torvalds 		} else if (optname == IP_UNBLOCK_SOURCE) {
6991da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7001da177e4SLinus Torvalds 			add = 0;
7011da177e4SLinus Torvalds 		} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
7021da177e4SLinus Torvalds 			struct ip_mreqn mreq;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 			mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
7051da177e4SLinus Torvalds 			mreq.imr_address.s_addr = mreqs.imr_interface;
7061da177e4SLinus Torvalds 			mreq.imr_ifindex = 0;
7071da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7088cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
7091da177e4SLinus Torvalds 				break;
7101da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7111da177e4SLinus Torvalds 			add = 1;
7121da177e4SLinus Torvalds 		} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
7131da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7141da177e4SLinus Torvalds 			add = 0;
7151da177e4SLinus Torvalds 		}
7161da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs, 0);
7171da177e4SLinus Torvalds 		break;
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 	case MCAST_JOIN_GROUP:
7201da177e4SLinus Torvalds 	case MCAST_LEAVE_GROUP:
7211da177e4SLinus Torvalds 	{
7221da177e4SLinus Torvalds 		struct group_req greq;
7231da177e4SLinus Torvalds 		struct sockaddr_in *psin;
7241da177e4SLinus Torvalds 		struct ip_mreqn mreq;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		if (optlen < sizeof(struct group_req))
7271da177e4SLinus Torvalds 			goto e_inval;
7281da177e4SLinus Torvalds 		err = -EFAULT;
7291da177e4SLinus Torvalds 		if (copy_from_user(&greq, optval, sizeof(greq)))
7301da177e4SLinus Torvalds 			break;
7311da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greq.gr_group;
7321da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET)
7331da177e4SLinus Torvalds 			goto e_inval;
7341da177e4SLinus Torvalds 		memset(&mreq, 0, sizeof(mreq));
7351da177e4SLinus Torvalds 		mreq.imr_multiaddr = psin->sin_addr;
7361da177e4SLinus Torvalds 		mreq.imr_ifindex = greq.gr_interface;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 		if (optname == MCAST_JOIN_GROUP)
7391da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7401da177e4SLinus Torvalds 		else
7411da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
7421da177e4SLinus Torvalds 		break;
7431da177e4SLinus Torvalds 	}
7441da177e4SLinus Torvalds 	case MCAST_JOIN_SOURCE_GROUP:
7451da177e4SLinus Torvalds 	case MCAST_LEAVE_SOURCE_GROUP:
7461da177e4SLinus Torvalds 	case MCAST_BLOCK_SOURCE:
7471da177e4SLinus Torvalds 	case MCAST_UNBLOCK_SOURCE:
7481da177e4SLinus Torvalds 	{
7491da177e4SLinus Torvalds 		struct group_source_req greqs;
7501da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
7511da177e4SLinus Torvalds 		struct sockaddr_in *psin;
7521da177e4SLinus Torvalds 		int omode, add;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 		if (optlen != sizeof(struct group_source_req))
7551da177e4SLinus Torvalds 			goto e_inval;
7561da177e4SLinus Torvalds 		if (copy_from_user(&greqs, optval, sizeof(greqs))) {
7571da177e4SLinus Torvalds 			err = -EFAULT;
7581da177e4SLinus Torvalds 			break;
7591da177e4SLinus Torvalds 		}
7601da177e4SLinus Torvalds 		if (greqs.gsr_group.ss_family != AF_INET ||
7611da177e4SLinus Torvalds 		    greqs.gsr_source.ss_family != AF_INET) {
7621da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
7631da177e4SLinus Torvalds 			break;
7641da177e4SLinus Torvalds 		}
7651da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_group;
7661da177e4SLinus Torvalds 		mreqs.imr_multiaddr = psin->sin_addr.s_addr;
7671da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_source;
7681da177e4SLinus Torvalds 		mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
7691da177e4SLinus Torvalds 		mreqs.imr_interface = 0; /* use index for mc_source */
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 		if (optname == MCAST_BLOCK_SOURCE) {
7721da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7731da177e4SLinus Torvalds 			add = 1;
7741da177e4SLinus Torvalds 		} else if (optname == MCAST_UNBLOCK_SOURCE) {
7751da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7761da177e4SLinus Torvalds 			add = 0;
7771da177e4SLinus Torvalds 		} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
7781da177e4SLinus Torvalds 			struct ip_mreqn mreq;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&greqs.gsr_group;
7811da177e4SLinus Torvalds 			mreq.imr_multiaddr = psin->sin_addr;
7821da177e4SLinus Torvalds 			mreq.imr_address.s_addr = 0;
7831da177e4SLinus Torvalds 			mreq.imr_ifindex = greqs.gsr_interface;
7841da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7858cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
7861da177e4SLinus Torvalds 				break;
7871da177e4SLinus Torvalds 			greqs.gsr_interface = mreq.imr_ifindex;
7881da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7891da177e4SLinus Torvalds 			add = 1;
7901da177e4SLinus Torvalds 		} else /* MCAST_LEAVE_SOURCE_GROUP */ {
7911da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7921da177e4SLinus Torvalds 			add = 0;
7931da177e4SLinus Torvalds 		}
7941da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs,
7951da177e4SLinus Torvalds 				   greqs.gsr_interface);
7961da177e4SLinus Torvalds 		break;
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 	case MCAST_MSFILTER:
7991da177e4SLinus Torvalds 	{
8001da177e4SLinus Torvalds 		extern int sysctl_igmp_max_msf;
8011da177e4SLinus Torvalds 		struct sockaddr_in *psin;
8021da177e4SLinus Torvalds 		struct ip_msfilter *msf = NULL;
8031da177e4SLinus Torvalds 		struct group_filter *gsf = NULL;
8041da177e4SLinus Torvalds 		int msize, i, ifindex;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 		if (optlen < GROUP_FILTER_SIZE(0))
8071da177e4SLinus Torvalds 			goto e_inval;
8081da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
8091da177e4SLinus Torvalds 			err = -ENOBUFS;
8101da177e4SLinus Torvalds 			break;
8111da177e4SLinus Torvalds 		}
8128b3a7005SKris Katterjohn 		gsf = kmalloc(optlen,GFP_KERNEL);
8131da177e4SLinus Torvalds 		if (gsf == 0) {
8141da177e4SLinus Torvalds 			err = -ENOBUFS;
8151da177e4SLinus Torvalds 			break;
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 		err = -EFAULT;
8181da177e4SLinus Torvalds 		if (copy_from_user(gsf, optval, optlen)) {
8191da177e4SLinus Torvalds 			goto mc_msf_out;
8201da177e4SLinus Torvalds 		}
8211da177e4SLinus Torvalds 		/* numsrc >= (4G-140)/128 overflow in 32 bits */
8221da177e4SLinus Torvalds 		if (gsf->gf_numsrc >= 0x1ffffff ||
8231da177e4SLinus Torvalds 		    gsf->gf_numsrc > sysctl_igmp_max_msf) {
8241da177e4SLinus Torvalds 			err = -ENOBUFS;
8251da177e4SLinus Torvalds 			goto mc_msf_out;
8261da177e4SLinus Torvalds 		}
8271da177e4SLinus Torvalds 		if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
8281da177e4SLinus Torvalds 			err = -EINVAL;
8291da177e4SLinus Torvalds 			goto mc_msf_out;
8301da177e4SLinus Torvalds 		}
8311da177e4SLinus Torvalds 		msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
8328b3a7005SKris Katterjohn 		msf = kmalloc(msize,GFP_KERNEL);
8331da177e4SLinus Torvalds 		if (msf == 0) {
8341da177e4SLinus Torvalds 			err = -ENOBUFS;
8351da177e4SLinus Torvalds 			goto mc_msf_out;
8361da177e4SLinus Torvalds 		}
8371da177e4SLinus Torvalds 		ifindex = gsf->gf_interface;
8381da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&gsf->gf_group;
8391da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET) {
8401da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
8411da177e4SLinus Torvalds 			goto mc_msf_out;
8421da177e4SLinus Torvalds 		}
8431da177e4SLinus Torvalds 		msf->imsf_multiaddr = psin->sin_addr.s_addr;
8441da177e4SLinus Torvalds 		msf->imsf_interface = 0;
8451da177e4SLinus Torvalds 		msf->imsf_fmode = gsf->gf_fmode;
8461da177e4SLinus Torvalds 		msf->imsf_numsrc = gsf->gf_numsrc;
8471da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
8481da177e4SLinus Torvalds 		for (i=0; i<gsf->gf_numsrc; ++i) {
8491da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&gsf->gf_slist[i];
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 			if (psin->sin_family != AF_INET)
8521da177e4SLinus Torvalds 				goto mc_msf_out;
8531da177e4SLinus Torvalds 			msf->imsf_slist[i] = psin->sin_addr.s_addr;
8541da177e4SLinus Torvalds 		}
8551da177e4SLinus Torvalds 		kfree(gsf);
8561da177e4SLinus Torvalds 		gsf = NULL;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, ifindex);
8591da177e4SLinus Torvalds 	mc_msf_out:
8601da177e4SLinus Torvalds 		kfree(msf);
8611da177e4SLinus Torvalds 		kfree(gsf);
8621da177e4SLinus Torvalds 		break;
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 	case IP_ROUTER_ALERT:
8651da177e4SLinus Torvalds 		err = ip_ra_control(sk, val ? 1 : 0, NULL);
8661da177e4SLinus Torvalds 		break;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	case IP_FREEBIND:
8691da177e4SLinus Torvalds 		if (optlen<1)
8701da177e4SLinus Torvalds 			goto e_inval;
8711da177e4SLinus Torvalds 		inet->freebind = !!val;
8721da177e4SLinus Torvalds 		break;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	case IP_IPSEC_POLICY:
8751da177e4SLinus Torvalds 	case IP_XFRM_POLICY:
8766fc0b4a7SHerbert Xu 		err = -EPERM;
8776fc0b4a7SHerbert Xu 		if (!capable(CAP_NET_ADMIN))
8786fc0b4a7SHerbert Xu 			break;
8791da177e4SLinus Torvalds 		err = xfrm_user_policy(sk, optname, optval, optlen);
8801da177e4SLinus Torvalds 		break;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	default:
8831da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
8841da177e4SLinus Torvalds 		break;
8851da177e4SLinus Torvalds 	}
8861da177e4SLinus Torvalds 	release_sock(sk);
8871da177e4SLinus Torvalds 	return err;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds e_inval:
8901da177e4SLinus Torvalds 	release_sock(sk);
8911da177e4SLinus Torvalds 	return -EINVAL;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
8943fdadf7dSDmitry Mishin int ip_setsockopt(struct sock *sk, int level,
8953fdadf7dSDmitry Mishin 		int optname, char __user *optval, int optlen)
8963fdadf7dSDmitry Mishin {
8973fdadf7dSDmitry Mishin 	int err;
8983fdadf7dSDmitry Mishin 
8993fdadf7dSDmitry Mishin 	if (level != SOL_IP)
9003fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
9013fdadf7dSDmitry Mishin 
9023fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
9033fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
9043fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
9053fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
9063fdadf7dSDmitry Mishin 		optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
9073fdadf7dSDmitry Mishin #ifdef CONFIG_IP_MROUTE
9083fdadf7dSDmitry Mishin 		&& (optname < MRT_BASE || optname > (MRT_BASE + 10))
9093fdadf7dSDmitry Mishin #endif
9103fdadf7dSDmitry Mishin 	   ) {
9113fdadf7dSDmitry Mishin 		lock_sock(sk);
9123fdadf7dSDmitry Mishin 		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
9133fdadf7dSDmitry Mishin 		release_sock(sk);
9143fdadf7dSDmitry Mishin 	}
9153fdadf7dSDmitry Mishin #endif
9163fdadf7dSDmitry Mishin 	return err;
9173fdadf7dSDmitry Mishin }
9183fdadf7dSDmitry Mishin 
9193fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
920543d9cfeSArnaldo Carvalho de Melo int compat_ip_setsockopt(struct sock *sk, int level, int optname,
921543d9cfeSArnaldo Carvalho de Melo 			 char __user *optval, int optlen)
9223fdadf7dSDmitry Mishin {
9233fdadf7dSDmitry Mishin 	int err;
9243fdadf7dSDmitry Mishin 
9253fdadf7dSDmitry Mishin 	if (level != SOL_IP)
9263fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
9273fdadf7dSDmitry Mishin 
9283fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
9293fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
9303fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
9313fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
9323fdadf7dSDmitry Mishin 	    optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
9333fdadf7dSDmitry Mishin #ifdef CONFIG_IP_MROUTE
9343fdadf7dSDmitry Mishin 	    && (optname < MRT_BASE || optname > (MRT_BASE + 10))
9353fdadf7dSDmitry Mishin #endif
9363fdadf7dSDmitry Mishin 	   ) {
9373fdadf7dSDmitry Mishin 		lock_sock(sk);
938543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_setsockopt(sk, PF_INET, optname,
939543d9cfeSArnaldo Carvalho de Melo 					   optval, optlen);
9403fdadf7dSDmitry Mishin 		release_sock(sk);
9413fdadf7dSDmitry Mishin 	}
9423fdadf7dSDmitry Mishin #endif
9433fdadf7dSDmitry Mishin 	return err;
9443fdadf7dSDmitry Mishin }
945543d9cfeSArnaldo Carvalho de Melo 
946543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_setsockopt);
9473fdadf7dSDmitry Mishin #endif
9483fdadf7dSDmitry Mishin 
9491da177e4SLinus Torvalds /*
9501da177e4SLinus Torvalds  *	Get the options. Note for future reference. The GET of IP options gets the
9511da177e4SLinus Torvalds  *	_received_ ones. The set sets the _sent_ ones.
9521da177e4SLinus Torvalds  */
9531da177e4SLinus Torvalds 
9543fdadf7dSDmitry Mishin static int do_ip_getsockopt(struct sock *sk, int level, int optname,
9553fdadf7dSDmitry Mishin 			    char __user *optval, int __user *optlen)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
9581da177e4SLinus Torvalds 	int val;
9591da177e4SLinus Torvalds 	int len;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	if (level != SOL_IP)
9621da177e4SLinus Torvalds 		return -EOPNOTSUPP;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
965132adf54SStephen Hemminger 	if (optname >= MRT_BASE && optname <= MRT_BASE+10) {
9661da177e4SLinus Torvalds 		return ip_mroute_getsockopt(sk,optname,optval,optlen);
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds #endif
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	if (get_user(len,optlen))
9711da177e4SLinus Torvalds 		return -EFAULT;
9721da177e4SLinus Torvalds 	if (len < 0)
9731da177e4SLinus Torvalds 		return -EINVAL;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	lock_sock(sk);
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	switch (optname) {
9781da177e4SLinus Torvalds 	case IP_OPTIONS:
9791da177e4SLinus Torvalds 	{
9801da177e4SLinus Torvalds 		unsigned char optbuf[sizeof(struct ip_options)+40];
9811da177e4SLinus Torvalds 		struct ip_options * opt = (struct ip_options*)optbuf;
9821da177e4SLinus Torvalds 		opt->optlen = 0;
9831da177e4SLinus Torvalds 		if (inet->opt)
9841da177e4SLinus Torvalds 			memcpy(optbuf, inet->opt,
9851da177e4SLinus Torvalds 			       sizeof(struct ip_options)+
9861da177e4SLinus Torvalds 			       inet->opt->optlen);
9871da177e4SLinus Torvalds 		release_sock(sk);
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 		if (opt->optlen == 0)
9901da177e4SLinus Torvalds 			return put_user(0, optlen);
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 		ip_options_undo(opt);
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 		len = min_t(unsigned int, len, opt->optlen);
9951da177e4SLinus Torvalds 		if (put_user(len, optlen))
9961da177e4SLinus Torvalds 			return -EFAULT;
9971da177e4SLinus Torvalds 		if (copy_to_user(optval, opt->__data, len))
9981da177e4SLinus Torvalds 			return -EFAULT;
9991da177e4SLinus Torvalds 		return 0;
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 	case IP_PKTINFO:
10021da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
10031da177e4SLinus Torvalds 		break;
10041da177e4SLinus Torvalds 	case IP_RECVTTL:
10051da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
10061da177e4SLinus Torvalds 		break;
10071da177e4SLinus Torvalds 	case IP_RECVTOS:
10081da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
10091da177e4SLinus Torvalds 		break;
10101da177e4SLinus Torvalds 	case IP_RECVOPTS:
10111da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
10121da177e4SLinus Torvalds 		break;
10131da177e4SLinus Torvalds 	case IP_RETOPTS:
10141da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
10151da177e4SLinus Torvalds 		break;
10162c7946a7SCatherine Zhang 	case IP_PASSSEC:
10172c7946a7SCatherine Zhang 		val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
10182c7946a7SCatherine Zhang 		break;
10191da177e4SLinus Torvalds 	case IP_TOS:
10201da177e4SLinus Torvalds 		val = inet->tos;
10211da177e4SLinus Torvalds 		break;
10221da177e4SLinus Torvalds 	case IP_TTL:
10231da177e4SLinus Torvalds 		val = (inet->uc_ttl == -1 ?
10241da177e4SLinus Torvalds 		       sysctl_ip_default_ttl :
10251da177e4SLinus Torvalds 		       inet->uc_ttl);
10261da177e4SLinus Torvalds 		break;
10271da177e4SLinus Torvalds 	case IP_HDRINCL:
10281da177e4SLinus Torvalds 		val = inet->hdrincl;
10291da177e4SLinus Torvalds 		break;
10301da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
10311da177e4SLinus Torvalds 		val = inet->pmtudisc;
10321da177e4SLinus Torvalds 		break;
10331da177e4SLinus Torvalds 	case IP_MTU:
10341da177e4SLinus Torvalds 	{
10351da177e4SLinus Torvalds 		struct dst_entry *dst;
10361da177e4SLinus Torvalds 		val = 0;
10371da177e4SLinus Torvalds 		dst = sk_dst_get(sk);
10381da177e4SLinus Torvalds 		if (dst) {
10391da177e4SLinus Torvalds 			val = dst_mtu(dst);
10401da177e4SLinus Torvalds 			dst_release(dst);
10411da177e4SLinus Torvalds 		}
10421da177e4SLinus Torvalds 		if (!val) {
10431da177e4SLinus Torvalds 			release_sock(sk);
10441da177e4SLinus Torvalds 			return -ENOTCONN;
10451da177e4SLinus Torvalds 		}
10461da177e4SLinus Torvalds 		break;
10471da177e4SLinus Torvalds 	}
10481da177e4SLinus Torvalds 	case IP_RECVERR:
10491da177e4SLinus Torvalds 		val = inet->recverr;
10501da177e4SLinus Torvalds 		break;
10511da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
10521da177e4SLinus Torvalds 		val = inet->mc_ttl;
10531da177e4SLinus Torvalds 		break;
10541da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
10551da177e4SLinus Torvalds 		val = inet->mc_loop;
10561da177e4SLinus Torvalds 		break;
10571da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
10581da177e4SLinus Torvalds 	{
10591da177e4SLinus Torvalds 		struct in_addr addr;
10601da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(struct in_addr));
10611da177e4SLinus Torvalds 		addr.s_addr = inet->mc_addr;
10621da177e4SLinus Torvalds 		release_sock(sk);
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 		if (put_user(len, optlen))
10651da177e4SLinus Torvalds 			return -EFAULT;
10661da177e4SLinus Torvalds 		if (copy_to_user(optval, &addr, len))
10671da177e4SLinus Torvalds 			return -EFAULT;
10681da177e4SLinus Torvalds 		return 0;
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 	case IP_MSFILTER:
10711da177e4SLinus Torvalds 	{
10721da177e4SLinus Torvalds 		struct ip_msfilter msf;
10731da177e4SLinus Torvalds 		int err;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 		if (len < IP_MSFILTER_SIZE(0)) {
10761da177e4SLinus Torvalds 			release_sock(sk);
10771da177e4SLinus Torvalds 			return -EINVAL;
10781da177e4SLinus Torvalds 		}
10791da177e4SLinus Torvalds 		if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
10801da177e4SLinus Torvalds 			release_sock(sk);
10811da177e4SLinus Torvalds 			return -EFAULT;
10821da177e4SLinus Torvalds 		}
10831da177e4SLinus Torvalds 		err = ip_mc_msfget(sk, &msf,
10841da177e4SLinus Torvalds 				   (struct ip_msfilter __user *)optval, optlen);
10851da177e4SLinus Torvalds 		release_sock(sk);
10861da177e4SLinus Torvalds 		return err;
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 	case MCAST_MSFILTER:
10891da177e4SLinus Torvalds 	{
10901da177e4SLinus Torvalds 		struct group_filter gsf;
10911da177e4SLinus Torvalds 		int err;
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 		if (len < GROUP_FILTER_SIZE(0)) {
10941da177e4SLinus Torvalds 			release_sock(sk);
10951da177e4SLinus Torvalds 			return -EINVAL;
10961da177e4SLinus Torvalds 		}
10971da177e4SLinus Torvalds 		if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
10981da177e4SLinus Torvalds 			release_sock(sk);
10991da177e4SLinus Torvalds 			return -EFAULT;
11001da177e4SLinus Torvalds 		}
11011da177e4SLinus Torvalds 		err = ip_mc_gsfget(sk, &gsf,
11021da177e4SLinus Torvalds 				   (struct group_filter __user *)optval, optlen);
11031da177e4SLinus Torvalds 		release_sock(sk);
11041da177e4SLinus Torvalds 		return err;
11051da177e4SLinus Torvalds 	}
11061da177e4SLinus Torvalds 	case IP_PKTOPTIONS:
11071da177e4SLinus Torvalds 	{
11081da177e4SLinus Torvalds 		struct msghdr msg;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 		release_sock(sk);
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_STREAM)
11131da177e4SLinus Torvalds 			return -ENOPROTOOPT;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 		msg.msg_control = optval;
11161da177e4SLinus Torvalds 		msg.msg_controllen = len;
11171da177e4SLinus Torvalds 		msg.msg_flags = 0;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
11201da177e4SLinus Torvalds 			struct in_pktinfo info;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 			info.ipi_addr.s_addr = inet->rcv_saddr;
11231da177e4SLinus Torvalds 			info.ipi_spec_dst.s_addr = inet->rcv_saddr;
11241da177e4SLinus Torvalds 			info.ipi_ifindex = inet->mc_index;
11251da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
11261da177e4SLinus Torvalds 		}
11271da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_TTL) {
11281da177e4SLinus Torvalds 			int hlim = inet->mc_ttl;
11291da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
11301da177e4SLinus Torvalds 		}
11311da177e4SLinus Torvalds 		len -= msg.msg_controllen;
11321da177e4SLinus Torvalds 		return put_user(len, optlen);
11331da177e4SLinus Torvalds 	}
11341da177e4SLinus Torvalds 	case IP_FREEBIND:
11351da177e4SLinus Torvalds 		val = inet->freebind;
11361da177e4SLinus Torvalds 		break;
11371da177e4SLinus Torvalds 	default:
11381da177e4SLinus Torvalds 		release_sock(sk);
11391da177e4SLinus Torvalds 		return -ENOPROTOOPT;
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 	release_sock(sk);
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
11441da177e4SLinus Torvalds 		unsigned char ucval = (unsigned char)val;
11451da177e4SLinus Torvalds 		len = 1;
11461da177e4SLinus Torvalds 		if (put_user(len, optlen))
11471da177e4SLinus Torvalds 			return -EFAULT;
11481da177e4SLinus Torvalds 		if (copy_to_user(optval,&ucval,1))
11491da177e4SLinus Torvalds 			return -EFAULT;
11501da177e4SLinus Torvalds 	} else {
11511da177e4SLinus Torvalds 		len = min_t(unsigned int, sizeof(int), len);
11521da177e4SLinus Torvalds 		if (put_user(len, optlen))
11531da177e4SLinus Torvalds 			return -EFAULT;
11541da177e4SLinus Torvalds 		if (copy_to_user(optval,&val,len))
11551da177e4SLinus Torvalds 			return -EFAULT;
11561da177e4SLinus Torvalds 	}
11571da177e4SLinus Torvalds 	return 0;
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
11603fdadf7dSDmitry Mishin int ip_getsockopt(struct sock *sk, int level,
11613fdadf7dSDmitry Mishin 		  int optname, char __user *optval, int __user *optlen)
11623fdadf7dSDmitry Mishin {
11633fdadf7dSDmitry Mishin 	int err;
11643fdadf7dSDmitry Mishin 
11653fdadf7dSDmitry Mishin 	err = do_ip_getsockopt(sk, level, optname, optval, optlen);
11663fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
11673fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
11683fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
11693fdadf7dSDmitry Mishin #ifdef CONFIG_IP_MROUTE
11703fdadf7dSDmitry Mishin 		&& (optname < MRT_BASE || optname > MRT_BASE+10)
11713fdadf7dSDmitry Mishin #endif
11723fdadf7dSDmitry Mishin 	   ) {
11733fdadf7dSDmitry Mishin 		int len;
11743fdadf7dSDmitry Mishin 
11753fdadf7dSDmitry Mishin 		if (get_user(len,optlen))
11763fdadf7dSDmitry Mishin 			return -EFAULT;
11773fdadf7dSDmitry Mishin 
11783fdadf7dSDmitry Mishin 		lock_sock(sk);
11793fdadf7dSDmitry Mishin 		err = nf_getsockopt(sk, PF_INET, optname, optval,
11803fdadf7dSDmitry Mishin 				&len);
11813fdadf7dSDmitry Mishin 		release_sock(sk);
11823fdadf7dSDmitry Mishin 		if (err >= 0)
11833fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
11843fdadf7dSDmitry Mishin 		return err;
11853fdadf7dSDmitry Mishin 	}
11863fdadf7dSDmitry Mishin #endif
11873fdadf7dSDmitry Mishin 	return err;
11883fdadf7dSDmitry Mishin }
11893fdadf7dSDmitry Mishin 
11903fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
1191543d9cfeSArnaldo Carvalho de Melo int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1192543d9cfeSArnaldo Carvalho de Melo 			 char __user *optval, int __user *optlen)
11933fdadf7dSDmitry Mishin {
1194543d9cfeSArnaldo Carvalho de Melo 	int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
11953fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
11963fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
11973fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
11983fdadf7dSDmitry Mishin #ifdef CONFIG_IP_MROUTE
11993fdadf7dSDmitry Mishin 	    && (optname < MRT_BASE || optname > MRT_BASE+10)
12003fdadf7dSDmitry Mishin #endif
12013fdadf7dSDmitry Mishin 	   ) {
12023fdadf7dSDmitry Mishin 		int len;
12033fdadf7dSDmitry Mishin 
12043fdadf7dSDmitry Mishin 		if (get_user(len, optlen))
12053fdadf7dSDmitry Mishin 			return -EFAULT;
12063fdadf7dSDmitry Mishin 
12073fdadf7dSDmitry Mishin 		lock_sock(sk);
1208543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
12093fdadf7dSDmitry Mishin 		release_sock(sk);
12103fdadf7dSDmitry Mishin 		if (err >= 0)
12113fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
12123fdadf7dSDmitry Mishin 		return err;
12133fdadf7dSDmitry Mishin 	}
12143fdadf7dSDmitry Mishin #endif
12153fdadf7dSDmitry Mishin 	return err;
12163fdadf7dSDmitry Mishin }
1217543d9cfeSArnaldo Carvalho de Melo 
1218543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_getsockopt);
12193fdadf7dSDmitry Mishin #endif
12203fdadf7dSDmitry Mishin 
12211da177e4SLinus Torvalds EXPORT_SYMBOL(ip_cmsg_recv);
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds EXPORT_SYMBOL(ip_getsockopt);
12241da177e4SLinus Torvalds EXPORT_SYMBOL(ip_setsockopt);
1225