xref: /linux/net/ipv4/ip_sockglue.c (revision ee6b967301b4aa5d4a4b61e2f682f086266db9fb)
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;
60*ee6b9673SEric Dumazet 	struct rtable *rt = skb->rtable;
611da177e4SLinus Torvalds 
62eddc9ec5SArnaldo 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 {
76eddc9ec5SArnaldo 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 {
82eddc9ec5SArnaldo 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 
90eddc9ec5SArnaldo Carvalho de Melo 	put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
91eddc9ec5SArnaldo 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;
27288c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_type = icmp_hdr(skb)->type;
27388c7664fSArnaldo Carvalho de Melo 	serr->ee.ee_code = icmp_hdr(skb)->code;
2741da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
2751da177e4SLinus Torvalds 	serr->ee.ee_info = info;
2761da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
27788c7664fSArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
278d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb);
2791da177e4SLinus Torvalds 	serr->port = port;
2801da177e4SLinus Torvalds 
281bd82393cSArnaldo Carvalho de Melo 	if (skb_pull(skb, payload - skb->data) != NULL) {
282bd82393cSArnaldo Carvalho de Melo 		skb_reset_transport_header(skb);
283bd82393cSArnaldo Carvalho de Melo 		if (sock_queue_err_skb(sk, skb) == 0)
284bd82393cSArnaldo Carvalho de Melo 			return;
285bd82393cSArnaldo Carvalho de Melo 	}
2861da177e4SLinus Torvalds 	kfree_skb(skb);
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2890579016eSAl Viro void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2921da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
2931da177e4SLinus Torvalds 	struct iphdr *iph;
2941da177e4SLinus Torvalds 	struct sk_buff *skb;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if (!inet->recverr)
2971da177e4SLinus Torvalds 		return;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
3001da177e4SLinus Torvalds 	if (!skb)
3011da177e4SLinus Torvalds 		return;
3021da177e4SLinus Torvalds 
3032ca9e6f2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr));
3042ca9e6f2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
305eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
3061da177e4SLinus Torvalds 	iph->daddr = daddr;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3091da177e4SLinus Torvalds 	serr->ee.ee_errno = err;
3101da177e4SLinus Torvalds 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
3111da177e4SLinus Torvalds 	serr->ee.ee_type = 0;
3121da177e4SLinus Torvalds 	serr->ee.ee_code = 0;
3131da177e4SLinus Torvalds 	serr->ee.ee_pad = 0;
3141da177e4SLinus Torvalds 	serr->ee.ee_info = info;
3151da177e4SLinus Torvalds 	serr->ee.ee_data = 0;
316d56f90a7SArnaldo Carvalho de Melo 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
3171da177e4SLinus Torvalds 	serr->port = port;
3181da177e4SLinus Torvalds 
31927a884dcSArnaldo Carvalho de Melo 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
320bd82393cSArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	if (sock_queue_err_skb(sk, skb))
3231da177e4SLinus Torvalds 		kfree_skb(skb);
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds /*
3271da177e4SLinus Torvalds  *	Handle MSG_ERRQUEUE
3281da177e4SLinus Torvalds  */
3291da177e4SLinus Torvalds int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	struct sock_exterr_skb *serr;
3321da177e4SLinus Torvalds 	struct sk_buff *skb, *skb2;
3331da177e4SLinus Torvalds 	struct sockaddr_in *sin;
3341da177e4SLinus Torvalds 	struct {
3351da177e4SLinus Torvalds 		struct sock_extended_err ee;
3361da177e4SLinus Torvalds 		struct sockaddr_in	 offender;
3371da177e4SLinus Torvalds 	} errhdr;
3381da177e4SLinus Torvalds 	int err;
3391da177e4SLinus Torvalds 	int copied;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	err = -EAGAIN;
3421da177e4SLinus Torvalds 	skb = skb_dequeue(&sk->sk_error_queue);
3431da177e4SLinus Torvalds 	if (skb == NULL)
3441da177e4SLinus Torvalds 		goto out;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	copied = skb->len;
3471da177e4SLinus Torvalds 	if (copied > len) {
3481da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
3491da177e4SLinus Torvalds 		copied = len;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3521da177e4SLinus Torvalds 	if (err)
3531da177e4SLinus Torvalds 		goto out_free_skb;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	sock_recv_timestamp(msg, sk, skb);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	serr = SKB_EXT_ERR(skb);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	sin = (struct sockaddr_in *)msg->msg_name;
3601da177e4SLinus Torvalds 	if (sin) {
3611da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
362d56f90a7SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
363d56f90a7SArnaldo Carvalho de Melo 						   serr->addr_offset);
3641da177e4SLinus Torvalds 		sin->sin_port = serr->port;
3651da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
3691da177e4SLinus Torvalds 	sin = &errhdr.offender;
3701da177e4SLinus Torvalds 	sin->sin_family = AF_UNSPEC;
3711da177e4SLinus Torvalds 	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
3721da177e4SLinus Torvalds 		struct inet_sock *inet = inet_sk(sk);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
375eddc9ec5SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
3761da177e4SLinus Torvalds 		sin->sin_port = 0;
3771da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3781da177e4SLinus Torvalds 		if (inet->cmsg_flags)
3791da177e4SLinus Torvalds 			ip_cmsg_recv(msg, skb);
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* Now we could try to dump offended packet options */
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	msg->msg_flags |= MSG_ERRQUEUE;
3871da177e4SLinus Torvalds 	err = copied;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/* Reset and regenerate socket error */
390e0f9f858SHerbert Xu 	spin_lock_bh(&sk->sk_error_queue.lock);
3911da177e4SLinus Torvalds 	sk->sk_err = 0;
3921da177e4SLinus Torvalds 	if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
3931da177e4SLinus Torvalds 		sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
394e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
3951da177e4SLinus Torvalds 		sk->sk_error_report(sk);
3961da177e4SLinus Torvalds 	} else
397e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_error_queue.lock);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds out_free_skb:
4001da177e4SLinus Torvalds 	kfree_skb(skb);
4011da177e4SLinus Torvalds out:
4021da177e4SLinus Torvalds 	return err;
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds /*
4071da177e4SLinus Torvalds  *	Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
4081da177e4SLinus Torvalds  *	an IP socket.
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds 
4113fdadf7dSDmitry Mishin static int do_ip_setsockopt(struct sock *sk, int level,
4123fdadf7dSDmitry Mishin 			    int optname, char __user *optval, int optlen)
4131da177e4SLinus Torvalds {
4141da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4151da177e4SLinus Torvalds 	int val=0,err;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
4181da177e4SLinus Torvalds 			     (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
4191da177e4SLinus Torvalds 			     (1<<IP_RETOPTS) | (1<<IP_TOS) |
4201da177e4SLinus Torvalds 			     (1<<IP_TTL) | (1<<IP_HDRINCL) |
4211da177e4SLinus Torvalds 			     (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
4222c7946a7SCatherine Zhang 			     (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
4232c7946a7SCatherine Zhang 			     (1<<IP_PASSSEC))) ||
4241da177e4SLinus Torvalds 	    optname == IP_MULTICAST_TTL ||
4251da177e4SLinus Torvalds 	    optname == IP_MULTICAST_LOOP) {
4261da177e4SLinus Torvalds 		if (optlen >= sizeof(int)) {
4271da177e4SLinus Torvalds 			if (get_user(val, (int __user *) optval))
4281da177e4SLinus Torvalds 				return -EFAULT;
4291da177e4SLinus Torvalds 		} else if (optlen >= sizeof(char)) {
4301da177e4SLinus Torvalds 			unsigned char ucval;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 			if (get_user(ucval, (unsigned char __user *) optval))
4331da177e4SLinus Torvalds 				return -EFAULT;
4341da177e4SLinus Torvalds 			val = (int) ucval;
4351da177e4SLinus Torvalds 		}
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	/* If optlen==0, it is equivalent to val == 0 */
4391da177e4SLinus Torvalds 
4406a9fb947SPavel Emelyanov 	if (ip_mroute_opt(optname))
4411da177e4SLinus Torvalds 		return ip_mroute_setsockopt(sk,optname,optval,optlen);
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 (inet->tos != val) {
5181da177e4SLinus Torvalds 			inet->tos = val;
5191da177e4SLinus Torvalds 			sk->sk_priority = rt_tos2priority(val);
5201da177e4SLinus Torvalds 			sk_dst_reset(sk);
5211da177e4SLinus Torvalds 		}
5221da177e4SLinus Torvalds 		break;
5231da177e4SLinus Torvalds 	case IP_TTL:
5241da177e4SLinus Torvalds 		if (optlen<1)
5251da177e4SLinus Torvalds 			goto e_inval;
5261da177e4SLinus Torvalds 		if (val != -1 && (val < 1 || val>255))
5271da177e4SLinus Torvalds 			goto e_inval;
5281da177e4SLinus Torvalds 		inet->uc_ttl = val;
5291da177e4SLinus Torvalds 		break;
5301da177e4SLinus Torvalds 	case IP_HDRINCL:
5311da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_RAW) {
5321da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
5331da177e4SLinus Torvalds 			break;
5341da177e4SLinus Torvalds 		}
5351da177e4SLinus Torvalds 		inet->hdrincl = val ? 1 : 0;
5361da177e4SLinus Torvalds 		break;
5371da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
538628a5c56SJohn Heffner 		if (val<0 || val>3)
5391da177e4SLinus Torvalds 			goto e_inval;
5401da177e4SLinus Torvalds 		inet->pmtudisc = val;
5411da177e4SLinus Torvalds 		break;
5421da177e4SLinus Torvalds 	case IP_RECVERR:
5431da177e4SLinus Torvalds 		inet->recverr = !!val;
5441da177e4SLinus Torvalds 		if (!val)
5451da177e4SLinus Torvalds 			skb_queue_purge(&sk->sk_error_queue);
5461da177e4SLinus Torvalds 		break;
5471da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
5481da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
5491da177e4SLinus Torvalds 			goto e_inval;
5501da177e4SLinus Torvalds 		if (optlen<1)
5511da177e4SLinus Torvalds 			goto e_inval;
5521da177e4SLinus Torvalds 		if (val==-1)
5531da177e4SLinus Torvalds 			val = 1;
5541da177e4SLinus Torvalds 		if (val < 0 || val > 255)
5551da177e4SLinus Torvalds 			goto e_inval;
5561da177e4SLinus Torvalds 		inet->mc_ttl = val;
5571da177e4SLinus Torvalds 		break;
5581da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
5591da177e4SLinus Torvalds 		if (optlen<1)
5601da177e4SLinus Torvalds 			goto e_inval;
5611da177e4SLinus Torvalds 		inet->mc_loop = !!val;
5621da177e4SLinus Torvalds 		break;
5631da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
5641da177e4SLinus Torvalds 	{
5651da177e4SLinus Torvalds 		struct ip_mreqn mreq;
5661da177e4SLinus Torvalds 		struct net_device *dev = NULL;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 		if (sk->sk_type == SOCK_STREAM)
5691da177e4SLinus Torvalds 			goto e_inval;
5701da177e4SLinus Torvalds 		/*
5711da177e4SLinus Torvalds 		 *	Check the arguments are allowable
5721da177e4SLinus Torvalds 		 */
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 		err = -EFAULT;
5751da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
5761da177e4SLinus Torvalds 			if (copy_from_user(&mreq,optval,sizeof(mreq)))
5771da177e4SLinus Torvalds 				break;
5781da177e4SLinus Torvalds 		} else {
5791da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
5801da177e4SLinus Torvalds 			if (optlen >= sizeof(struct in_addr) &&
5811da177e4SLinus Torvalds 			    copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
5821da177e4SLinus Torvalds 				break;
5831da177e4SLinus Torvalds 		}
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 		if (!mreq.imr_ifindex) {
5861da177e4SLinus Torvalds 			if (mreq.imr_address.s_addr == INADDR_ANY) {
5871da177e4SLinus Torvalds 				inet->mc_index = 0;
5881da177e4SLinus Torvalds 				inet->mc_addr  = 0;
5891da177e4SLinus Torvalds 				err = 0;
5901da177e4SLinus Torvalds 				break;
5911da177e4SLinus Torvalds 			}
5921ab35276SDenis V. Lunev 			dev = ip_dev_find(&init_net, mreq.imr_address.s_addr);
5931da177e4SLinus Torvalds 			if (dev) {
5941da177e4SLinus Torvalds 				mreq.imr_ifindex = dev->ifindex;
5951da177e4SLinus Torvalds 				dev_put(dev);
5961da177e4SLinus Torvalds 			}
5971da177e4SLinus Torvalds 		} else
598881d966bSEric W. Biederman 			dev = __dev_get_by_index(&init_net, mreq.imr_ifindex);
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
6021da177e4SLinus Torvalds 		if (!dev)
6031da177e4SLinus Torvalds 			break;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 		err = -EINVAL;
6061da177e4SLinus Torvalds 		if (sk->sk_bound_dev_if &&
6071da177e4SLinus Torvalds 		    mreq.imr_ifindex != sk->sk_bound_dev_if)
6081da177e4SLinus Torvalds 			break;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 		inet->mc_index = mreq.imr_ifindex;
6111da177e4SLinus Torvalds 		inet->mc_addr  = mreq.imr_address.s_addr;
6121da177e4SLinus Torvalds 		err = 0;
6131da177e4SLinus Torvalds 		break;
6141da177e4SLinus Torvalds 	}
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	case IP_ADD_MEMBERSHIP:
6171da177e4SLinus Torvalds 	case IP_DROP_MEMBERSHIP:
6181da177e4SLinus Torvalds 	{
6191da177e4SLinus Torvalds 		struct ip_mreqn mreq;
6201da177e4SLinus Torvalds 
621a96fb49bSFlavio Leitner 		err = -EPROTO;
622a96fb49bSFlavio Leitner 		if (inet_sk(sk)->is_icsk)
623a96fb49bSFlavio Leitner 			break;
624a96fb49bSFlavio Leitner 
6251da177e4SLinus Torvalds 		if (optlen < sizeof(struct ip_mreq))
6261da177e4SLinus Torvalds 			goto e_inval;
6271da177e4SLinus Torvalds 		err = -EFAULT;
6281da177e4SLinus Torvalds 		if (optlen >= sizeof(struct ip_mreqn)) {
6291da177e4SLinus Torvalds 			if (copy_from_user(&mreq,optval,sizeof(mreq)))
6301da177e4SLinus Torvalds 				break;
6311da177e4SLinus Torvalds 		} else {
6321da177e4SLinus Torvalds 			memset(&mreq, 0, sizeof(mreq));
6331da177e4SLinus Torvalds 			if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
6341da177e4SLinus Torvalds 				break;
6351da177e4SLinus Torvalds 		}
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 		if (optname == IP_ADD_MEMBERSHIP)
6381da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
6391da177e4SLinus Torvalds 		else
6401da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
6411da177e4SLinus Torvalds 		break;
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 	case IP_MSFILTER:
6441da177e4SLinus Torvalds 	{
6451da177e4SLinus Torvalds 		extern int sysctl_igmp_max_msf;
6461da177e4SLinus Torvalds 		struct ip_msfilter *msf;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 		if (optlen < IP_MSFILTER_SIZE(0))
6491da177e4SLinus Torvalds 			goto e_inval;
6501da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
6511da177e4SLinus Torvalds 			err = -ENOBUFS;
6521da177e4SLinus Torvalds 			break;
6531da177e4SLinus Torvalds 		}
6548b3a7005SKris Katterjohn 		msf = kmalloc(optlen, GFP_KERNEL);
655cfcabdccSStephen Hemminger 		if (!msf) {
6561da177e4SLinus Torvalds 			err = -ENOBUFS;
6571da177e4SLinus Torvalds 			break;
6581da177e4SLinus Torvalds 		}
6591da177e4SLinus Torvalds 		err = -EFAULT;
6601da177e4SLinus Torvalds 		if (copy_from_user(msf, optval, optlen)) {
6611da177e4SLinus Torvalds 			kfree(msf);
6621da177e4SLinus Torvalds 			break;
6631da177e4SLinus Torvalds 		}
6641da177e4SLinus Torvalds 		/* numsrc >= (1G-4) overflow in 32 bits */
6651da177e4SLinus Torvalds 		if (msf->imsf_numsrc >= 0x3ffffffcU ||
6661da177e4SLinus Torvalds 		    msf->imsf_numsrc > sysctl_igmp_max_msf) {
6671da177e4SLinus Torvalds 			kfree(msf);
6681da177e4SLinus Torvalds 			err = -ENOBUFS;
6691da177e4SLinus Torvalds 			break;
6701da177e4SLinus Torvalds 		}
6711da177e4SLinus Torvalds 		if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
6721da177e4SLinus Torvalds 			kfree(msf);
6731da177e4SLinus Torvalds 			err = -EINVAL;
6741da177e4SLinus Torvalds 			break;
6751da177e4SLinus Torvalds 		}
6761da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, 0);
6771da177e4SLinus Torvalds 		kfree(msf);
6781da177e4SLinus Torvalds 		break;
6791da177e4SLinus Torvalds 	}
6801da177e4SLinus Torvalds 	case IP_BLOCK_SOURCE:
6811da177e4SLinus Torvalds 	case IP_UNBLOCK_SOURCE:
6821da177e4SLinus Torvalds 	case IP_ADD_SOURCE_MEMBERSHIP:
6831da177e4SLinus Torvalds 	case IP_DROP_SOURCE_MEMBERSHIP:
6841da177e4SLinus Torvalds 	{
6851da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
6861da177e4SLinus Torvalds 		int omode, add;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 		if (optlen != sizeof(struct ip_mreq_source))
6891da177e4SLinus Torvalds 			goto e_inval;
6901da177e4SLinus Torvalds 		if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
6911da177e4SLinus Torvalds 			err = -EFAULT;
6921da177e4SLinus Torvalds 			break;
6931da177e4SLinus Torvalds 		}
6941da177e4SLinus Torvalds 		if (optname == IP_BLOCK_SOURCE) {
6951da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
6961da177e4SLinus Torvalds 			add = 1;
6971da177e4SLinus Torvalds 		} else if (optname == IP_UNBLOCK_SOURCE) {
6981da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
6991da177e4SLinus Torvalds 			add = 0;
7001da177e4SLinus Torvalds 		} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
7011da177e4SLinus Torvalds 			struct ip_mreqn mreq;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 			mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
7041da177e4SLinus Torvalds 			mreq.imr_address.s_addr = mreqs.imr_interface;
7051da177e4SLinus Torvalds 			mreq.imr_ifindex = 0;
7061da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7078cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
7081da177e4SLinus Torvalds 				break;
7091da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7101da177e4SLinus Torvalds 			add = 1;
7111da177e4SLinus Torvalds 		} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
7121da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7131da177e4SLinus Torvalds 			add = 0;
7141da177e4SLinus Torvalds 		}
7151da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs, 0);
7161da177e4SLinus Torvalds 		break;
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 	case MCAST_JOIN_GROUP:
7191da177e4SLinus Torvalds 	case MCAST_LEAVE_GROUP:
7201da177e4SLinus Torvalds 	{
7211da177e4SLinus Torvalds 		struct group_req greq;
7221da177e4SLinus Torvalds 		struct sockaddr_in *psin;
7231da177e4SLinus Torvalds 		struct ip_mreqn mreq;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 		if (optlen < sizeof(struct group_req))
7261da177e4SLinus Torvalds 			goto e_inval;
7271da177e4SLinus Torvalds 		err = -EFAULT;
7281da177e4SLinus Torvalds 		if (copy_from_user(&greq, optval, sizeof(greq)))
7291da177e4SLinus Torvalds 			break;
7301da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greq.gr_group;
7311da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET)
7321da177e4SLinus Torvalds 			goto e_inval;
7331da177e4SLinus Torvalds 		memset(&mreq, 0, sizeof(mreq));
7341da177e4SLinus Torvalds 		mreq.imr_multiaddr = psin->sin_addr;
7351da177e4SLinus Torvalds 		mreq.imr_ifindex = greq.gr_interface;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 		if (optname == MCAST_JOIN_GROUP)
7381da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7391da177e4SLinus Torvalds 		else
7401da177e4SLinus Torvalds 			err = ip_mc_leave_group(sk, &mreq);
7411da177e4SLinus Torvalds 		break;
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds 	case MCAST_JOIN_SOURCE_GROUP:
7441da177e4SLinus Torvalds 	case MCAST_LEAVE_SOURCE_GROUP:
7451da177e4SLinus Torvalds 	case MCAST_BLOCK_SOURCE:
7461da177e4SLinus Torvalds 	case MCAST_UNBLOCK_SOURCE:
7471da177e4SLinus Torvalds 	{
7481da177e4SLinus Torvalds 		struct group_source_req greqs;
7491da177e4SLinus Torvalds 		struct ip_mreq_source mreqs;
7501da177e4SLinus Torvalds 		struct sockaddr_in *psin;
7511da177e4SLinus Torvalds 		int omode, add;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 		if (optlen != sizeof(struct group_source_req))
7541da177e4SLinus Torvalds 			goto e_inval;
7551da177e4SLinus Torvalds 		if (copy_from_user(&greqs, optval, sizeof(greqs))) {
7561da177e4SLinus Torvalds 			err = -EFAULT;
7571da177e4SLinus Torvalds 			break;
7581da177e4SLinus Torvalds 		}
7591da177e4SLinus Torvalds 		if (greqs.gsr_group.ss_family != AF_INET ||
7601da177e4SLinus Torvalds 		    greqs.gsr_source.ss_family != AF_INET) {
7611da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
7621da177e4SLinus Torvalds 			break;
7631da177e4SLinus Torvalds 		}
7641da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_group;
7651da177e4SLinus Torvalds 		mreqs.imr_multiaddr = psin->sin_addr.s_addr;
7661da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&greqs.gsr_source;
7671da177e4SLinus Torvalds 		mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
7681da177e4SLinus Torvalds 		mreqs.imr_interface = 0; /* use index for mc_source */
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 		if (optname == MCAST_BLOCK_SOURCE) {
7711da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7721da177e4SLinus Torvalds 			add = 1;
7731da177e4SLinus Torvalds 		} else if (optname == MCAST_UNBLOCK_SOURCE) {
7741da177e4SLinus Torvalds 			omode = MCAST_EXCLUDE;
7751da177e4SLinus Torvalds 			add = 0;
7761da177e4SLinus Torvalds 		} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
7771da177e4SLinus Torvalds 			struct ip_mreqn mreq;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&greqs.gsr_group;
7801da177e4SLinus Torvalds 			mreq.imr_multiaddr = psin->sin_addr;
7811da177e4SLinus Torvalds 			mreq.imr_address.s_addr = 0;
7821da177e4SLinus Torvalds 			mreq.imr_ifindex = greqs.gsr_interface;
7831da177e4SLinus Torvalds 			err = ip_mc_join_group(sk, &mreq);
7848cdaaa15SDavid L Stevens 			if (err && err != -EADDRINUSE)
7851da177e4SLinus Torvalds 				break;
7861da177e4SLinus Torvalds 			greqs.gsr_interface = mreq.imr_ifindex;
7871da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7881da177e4SLinus Torvalds 			add = 1;
7891da177e4SLinus Torvalds 		} else /* MCAST_LEAVE_SOURCE_GROUP */ {
7901da177e4SLinus Torvalds 			omode = MCAST_INCLUDE;
7911da177e4SLinus Torvalds 			add = 0;
7921da177e4SLinus Torvalds 		}
7931da177e4SLinus Torvalds 		err = ip_mc_source(add, omode, sk, &mreqs,
7941da177e4SLinus Torvalds 				   greqs.gsr_interface);
7951da177e4SLinus Torvalds 		break;
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 	case MCAST_MSFILTER:
7981da177e4SLinus Torvalds 	{
7991da177e4SLinus Torvalds 		extern int sysctl_igmp_max_msf;
8001da177e4SLinus Torvalds 		struct sockaddr_in *psin;
8011da177e4SLinus Torvalds 		struct ip_msfilter *msf = NULL;
8021da177e4SLinus Torvalds 		struct group_filter *gsf = NULL;
8031da177e4SLinus Torvalds 		int msize, i, ifindex;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 		if (optlen < GROUP_FILTER_SIZE(0))
8061da177e4SLinus Torvalds 			goto e_inval;
8071da177e4SLinus Torvalds 		if (optlen > sysctl_optmem_max) {
8081da177e4SLinus Torvalds 			err = -ENOBUFS;
8091da177e4SLinus Torvalds 			break;
8101da177e4SLinus Torvalds 		}
8118b3a7005SKris Katterjohn 		gsf = kmalloc(optlen,GFP_KERNEL);
812cfcabdccSStephen Hemminger 		if (!gsf) {
8131da177e4SLinus Torvalds 			err = -ENOBUFS;
8141da177e4SLinus Torvalds 			break;
8151da177e4SLinus Torvalds 		}
8161da177e4SLinus Torvalds 		err = -EFAULT;
8171da177e4SLinus Torvalds 		if (copy_from_user(gsf, optval, optlen)) {
8181da177e4SLinus Torvalds 			goto mc_msf_out;
8191da177e4SLinus Torvalds 		}
8201da177e4SLinus Torvalds 		/* numsrc >= (4G-140)/128 overflow in 32 bits */
8211da177e4SLinus Torvalds 		if (gsf->gf_numsrc >= 0x1ffffff ||
8221da177e4SLinus Torvalds 		    gsf->gf_numsrc > sysctl_igmp_max_msf) {
8231da177e4SLinus Torvalds 			err = -ENOBUFS;
8241da177e4SLinus Torvalds 			goto mc_msf_out;
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 		if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
8271da177e4SLinus Torvalds 			err = -EINVAL;
8281da177e4SLinus Torvalds 			goto mc_msf_out;
8291da177e4SLinus Torvalds 		}
8301da177e4SLinus Torvalds 		msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
8318b3a7005SKris Katterjohn 		msf = kmalloc(msize,GFP_KERNEL);
832cfcabdccSStephen Hemminger 		if (!msf) {
8331da177e4SLinus Torvalds 			err = -ENOBUFS;
8341da177e4SLinus Torvalds 			goto mc_msf_out;
8351da177e4SLinus Torvalds 		}
8361da177e4SLinus Torvalds 		ifindex = gsf->gf_interface;
8371da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&gsf->gf_group;
8381da177e4SLinus Torvalds 		if (psin->sin_family != AF_INET) {
8391da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
8401da177e4SLinus Torvalds 			goto mc_msf_out;
8411da177e4SLinus Torvalds 		}
8421da177e4SLinus Torvalds 		msf->imsf_multiaddr = psin->sin_addr.s_addr;
8431da177e4SLinus Torvalds 		msf->imsf_interface = 0;
8441da177e4SLinus Torvalds 		msf->imsf_fmode = gsf->gf_fmode;
8451da177e4SLinus Torvalds 		msf->imsf_numsrc = gsf->gf_numsrc;
8461da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
8471da177e4SLinus Torvalds 		for (i=0; i<gsf->gf_numsrc; ++i) {
8481da177e4SLinus Torvalds 			psin = (struct sockaddr_in *)&gsf->gf_slist[i];
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 			if (psin->sin_family != AF_INET)
8511da177e4SLinus Torvalds 				goto mc_msf_out;
8521da177e4SLinus Torvalds 			msf->imsf_slist[i] = psin->sin_addr.s_addr;
8531da177e4SLinus Torvalds 		}
8541da177e4SLinus Torvalds 		kfree(gsf);
8551da177e4SLinus Torvalds 		gsf = NULL;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 		err = ip_mc_msfilter(sk, msf, ifindex);
8581da177e4SLinus Torvalds 	mc_msf_out:
8591da177e4SLinus Torvalds 		kfree(msf);
8601da177e4SLinus Torvalds 		kfree(gsf);
8611da177e4SLinus Torvalds 		break;
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 	case IP_ROUTER_ALERT:
8641da177e4SLinus Torvalds 		err = ip_ra_control(sk, val ? 1 : 0, NULL);
8651da177e4SLinus Torvalds 		break;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	case IP_FREEBIND:
8681da177e4SLinus Torvalds 		if (optlen<1)
8691da177e4SLinus Torvalds 			goto e_inval;
8701da177e4SLinus Torvalds 		inet->freebind = !!val;
8711da177e4SLinus Torvalds 		break;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	case IP_IPSEC_POLICY:
8741da177e4SLinus Torvalds 	case IP_XFRM_POLICY:
8756fc0b4a7SHerbert Xu 		err = -EPERM;
8766fc0b4a7SHerbert Xu 		if (!capable(CAP_NET_ADMIN))
8776fc0b4a7SHerbert Xu 			break;
8781da177e4SLinus Torvalds 		err = xfrm_user_policy(sk, optname, optval, optlen);
8791da177e4SLinus Torvalds 		break;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	default:
8821da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
8831da177e4SLinus Torvalds 		break;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 	release_sock(sk);
8861da177e4SLinus Torvalds 	return err;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds e_inval:
8891da177e4SLinus Torvalds 	release_sock(sk);
8901da177e4SLinus Torvalds 	return -EINVAL;
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
8933fdadf7dSDmitry Mishin int ip_setsockopt(struct sock *sk, int level,
8943fdadf7dSDmitry Mishin 		int optname, char __user *optval, int optlen)
8953fdadf7dSDmitry Mishin {
8963fdadf7dSDmitry Mishin 	int err;
8973fdadf7dSDmitry Mishin 
8983fdadf7dSDmitry Mishin 	if (level != SOL_IP)
8993fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
9003fdadf7dSDmitry Mishin 
9013fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
9023fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
9033fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
9043fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
9056a9fb947SPavel Emelyanov 			optname != IP_IPSEC_POLICY &&
9066a9fb947SPavel Emelyanov 			optname != IP_XFRM_POLICY &&
9076a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
9083fdadf7dSDmitry Mishin 		lock_sock(sk);
9093fdadf7dSDmitry Mishin 		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
9103fdadf7dSDmitry Mishin 		release_sock(sk);
9113fdadf7dSDmitry Mishin 	}
9123fdadf7dSDmitry Mishin #endif
9133fdadf7dSDmitry Mishin 	return err;
9143fdadf7dSDmitry Mishin }
9153fdadf7dSDmitry Mishin 
9163fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
917543d9cfeSArnaldo Carvalho de Melo int compat_ip_setsockopt(struct sock *sk, int level, int optname,
918543d9cfeSArnaldo Carvalho de Melo 			 char __user *optval, int optlen)
9193fdadf7dSDmitry Mishin {
9203fdadf7dSDmitry Mishin 	int err;
9213fdadf7dSDmitry Mishin 
9223fdadf7dSDmitry Mishin 	if (level != SOL_IP)
9233fdadf7dSDmitry Mishin 		return -ENOPROTOOPT;
9243fdadf7dSDmitry Mishin 
9253fdadf7dSDmitry Mishin 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
9263fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
9273fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
9283fdadf7dSDmitry Mishin 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
9296a9fb947SPavel Emelyanov 			optname != IP_IPSEC_POLICY &&
9306a9fb947SPavel Emelyanov 			optname != IP_XFRM_POLICY &&
9316a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
9323fdadf7dSDmitry Mishin 		lock_sock(sk);
933543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_setsockopt(sk, PF_INET, optname,
934543d9cfeSArnaldo Carvalho de Melo 					   optval, optlen);
9353fdadf7dSDmitry Mishin 		release_sock(sk);
9363fdadf7dSDmitry Mishin 	}
9373fdadf7dSDmitry Mishin #endif
9383fdadf7dSDmitry Mishin 	return err;
9393fdadf7dSDmitry Mishin }
940543d9cfeSArnaldo Carvalho de Melo 
941543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_setsockopt);
9423fdadf7dSDmitry Mishin #endif
9433fdadf7dSDmitry Mishin 
9441da177e4SLinus Torvalds /*
9451da177e4SLinus Torvalds  *	Get the options. Note for future reference. The GET of IP options gets the
9461da177e4SLinus Torvalds  *	_received_ ones. The set sets the _sent_ ones.
9471da177e4SLinus Torvalds  */
9481da177e4SLinus Torvalds 
9493fdadf7dSDmitry Mishin static int do_ip_getsockopt(struct sock *sk, int level, int optname,
9503fdadf7dSDmitry Mishin 			    char __user *optval, int __user *optlen)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
9531da177e4SLinus Torvalds 	int val;
9541da177e4SLinus Torvalds 	int len;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	if (level != SOL_IP)
9571da177e4SLinus Torvalds 		return -EOPNOTSUPP;
9581da177e4SLinus Torvalds 
9596a9fb947SPavel Emelyanov 	if (ip_mroute_opt(optname))
9601da177e4SLinus Torvalds 		return ip_mroute_getsockopt(sk,optname,optval,optlen);
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	if (get_user(len,optlen))
9631da177e4SLinus Torvalds 		return -EFAULT;
9641da177e4SLinus Torvalds 	if (len < 0)
9651da177e4SLinus Torvalds 		return -EINVAL;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	lock_sock(sk);
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	switch (optname) {
9701da177e4SLinus Torvalds 	case IP_OPTIONS:
9711da177e4SLinus Torvalds 	{
9721da177e4SLinus Torvalds 		unsigned char optbuf[sizeof(struct ip_options)+40];
9731da177e4SLinus Torvalds 		struct ip_options * opt = (struct ip_options*)optbuf;
9741da177e4SLinus Torvalds 		opt->optlen = 0;
9751da177e4SLinus Torvalds 		if (inet->opt)
9761da177e4SLinus Torvalds 			memcpy(optbuf, inet->opt,
9771da177e4SLinus Torvalds 			       sizeof(struct ip_options)+
9781da177e4SLinus Torvalds 			       inet->opt->optlen);
9791da177e4SLinus Torvalds 		release_sock(sk);
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 		if (opt->optlen == 0)
9821da177e4SLinus Torvalds 			return put_user(0, optlen);
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 		ip_options_undo(opt);
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 		len = min_t(unsigned int, len, opt->optlen);
9871da177e4SLinus Torvalds 		if (put_user(len, optlen))
9881da177e4SLinus Torvalds 			return -EFAULT;
9891da177e4SLinus Torvalds 		if (copy_to_user(optval, opt->__data, len))
9901da177e4SLinus Torvalds 			return -EFAULT;
9911da177e4SLinus Torvalds 		return 0;
9921da177e4SLinus Torvalds 	}
9931da177e4SLinus Torvalds 	case IP_PKTINFO:
9941da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
9951da177e4SLinus Torvalds 		break;
9961da177e4SLinus Torvalds 	case IP_RECVTTL:
9971da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
9981da177e4SLinus Torvalds 		break;
9991da177e4SLinus Torvalds 	case IP_RECVTOS:
10001da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
10011da177e4SLinus Torvalds 		break;
10021da177e4SLinus Torvalds 	case IP_RECVOPTS:
10031da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
10041da177e4SLinus Torvalds 		break;
10051da177e4SLinus Torvalds 	case IP_RETOPTS:
10061da177e4SLinus Torvalds 		val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
10071da177e4SLinus Torvalds 		break;
10082c7946a7SCatherine Zhang 	case IP_PASSSEC:
10092c7946a7SCatherine Zhang 		val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
10102c7946a7SCatherine Zhang 		break;
10111da177e4SLinus Torvalds 	case IP_TOS:
10121da177e4SLinus Torvalds 		val = inet->tos;
10131da177e4SLinus Torvalds 		break;
10141da177e4SLinus Torvalds 	case IP_TTL:
10151da177e4SLinus Torvalds 		val = (inet->uc_ttl == -1 ?
10161da177e4SLinus Torvalds 		       sysctl_ip_default_ttl :
10171da177e4SLinus Torvalds 		       inet->uc_ttl);
10181da177e4SLinus Torvalds 		break;
10191da177e4SLinus Torvalds 	case IP_HDRINCL:
10201da177e4SLinus Torvalds 		val = inet->hdrincl;
10211da177e4SLinus Torvalds 		break;
10221da177e4SLinus Torvalds 	case IP_MTU_DISCOVER:
10231da177e4SLinus Torvalds 		val = inet->pmtudisc;
10241da177e4SLinus Torvalds 		break;
10251da177e4SLinus Torvalds 	case IP_MTU:
10261da177e4SLinus Torvalds 	{
10271da177e4SLinus Torvalds 		struct dst_entry *dst;
10281da177e4SLinus Torvalds 		val = 0;
10291da177e4SLinus Torvalds 		dst = sk_dst_get(sk);
10301da177e4SLinus Torvalds 		if (dst) {
10311da177e4SLinus Torvalds 			val = dst_mtu(dst);
10321da177e4SLinus Torvalds 			dst_release(dst);
10331da177e4SLinus Torvalds 		}
10341da177e4SLinus Torvalds 		if (!val) {
10351da177e4SLinus Torvalds 			release_sock(sk);
10361da177e4SLinus Torvalds 			return -ENOTCONN;
10371da177e4SLinus Torvalds 		}
10381da177e4SLinus Torvalds 		break;
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 	case IP_RECVERR:
10411da177e4SLinus Torvalds 		val = inet->recverr;
10421da177e4SLinus Torvalds 		break;
10431da177e4SLinus Torvalds 	case IP_MULTICAST_TTL:
10441da177e4SLinus Torvalds 		val = inet->mc_ttl;
10451da177e4SLinus Torvalds 		break;
10461da177e4SLinus Torvalds 	case IP_MULTICAST_LOOP:
10471da177e4SLinus Torvalds 		val = inet->mc_loop;
10481da177e4SLinus Torvalds 		break;
10491da177e4SLinus Torvalds 	case IP_MULTICAST_IF:
10501da177e4SLinus Torvalds 	{
10511da177e4SLinus Torvalds 		struct in_addr addr;
10521da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(struct in_addr));
10531da177e4SLinus Torvalds 		addr.s_addr = inet->mc_addr;
10541da177e4SLinus Torvalds 		release_sock(sk);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 		if (put_user(len, optlen))
10571da177e4SLinus Torvalds 			return -EFAULT;
10581da177e4SLinus Torvalds 		if (copy_to_user(optval, &addr, len))
10591da177e4SLinus Torvalds 			return -EFAULT;
10601da177e4SLinus Torvalds 		return 0;
10611da177e4SLinus Torvalds 	}
10621da177e4SLinus Torvalds 	case IP_MSFILTER:
10631da177e4SLinus Torvalds 	{
10641da177e4SLinus Torvalds 		struct ip_msfilter msf;
10651da177e4SLinus Torvalds 		int err;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 		if (len < IP_MSFILTER_SIZE(0)) {
10681da177e4SLinus Torvalds 			release_sock(sk);
10691da177e4SLinus Torvalds 			return -EINVAL;
10701da177e4SLinus Torvalds 		}
10711da177e4SLinus Torvalds 		if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
10721da177e4SLinus Torvalds 			release_sock(sk);
10731da177e4SLinus Torvalds 			return -EFAULT;
10741da177e4SLinus Torvalds 		}
10751da177e4SLinus Torvalds 		err = ip_mc_msfget(sk, &msf,
10761da177e4SLinus Torvalds 				   (struct ip_msfilter __user *)optval, optlen);
10771da177e4SLinus Torvalds 		release_sock(sk);
10781da177e4SLinus Torvalds 		return err;
10791da177e4SLinus Torvalds 	}
10801da177e4SLinus Torvalds 	case MCAST_MSFILTER:
10811da177e4SLinus Torvalds 	{
10821da177e4SLinus Torvalds 		struct group_filter gsf;
10831da177e4SLinus Torvalds 		int err;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 		if (len < GROUP_FILTER_SIZE(0)) {
10861da177e4SLinus Torvalds 			release_sock(sk);
10871da177e4SLinus Torvalds 			return -EINVAL;
10881da177e4SLinus Torvalds 		}
10891da177e4SLinus Torvalds 		if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
10901da177e4SLinus Torvalds 			release_sock(sk);
10911da177e4SLinus Torvalds 			return -EFAULT;
10921da177e4SLinus Torvalds 		}
10931da177e4SLinus Torvalds 		err = ip_mc_gsfget(sk, &gsf,
10941da177e4SLinus Torvalds 				   (struct group_filter __user *)optval, optlen);
10951da177e4SLinus Torvalds 		release_sock(sk);
10961da177e4SLinus Torvalds 		return err;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 	case IP_PKTOPTIONS:
10991da177e4SLinus Torvalds 	{
11001da177e4SLinus Torvalds 		struct msghdr msg;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 		release_sock(sk);
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_STREAM)
11051da177e4SLinus Torvalds 			return -ENOPROTOOPT;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 		msg.msg_control = optval;
11081da177e4SLinus Torvalds 		msg.msg_controllen = len;
11091da177e4SLinus Torvalds 		msg.msg_flags = 0;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
11121da177e4SLinus Torvalds 			struct in_pktinfo info;
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 			info.ipi_addr.s_addr = inet->rcv_saddr;
11151da177e4SLinus Torvalds 			info.ipi_spec_dst.s_addr = inet->rcv_saddr;
11161da177e4SLinus Torvalds 			info.ipi_ifindex = inet->mc_index;
11171da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
11181da177e4SLinus Torvalds 		}
11191da177e4SLinus Torvalds 		if (inet->cmsg_flags & IP_CMSG_TTL) {
11201da177e4SLinus Torvalds 			int hlim = inet->mc_ttl;
11211da177e4SLinus Torvalds 			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
11221da177e4SLinus Torvalds 		}
11231da177e4SLinus Torvalds 		len -= msg.msg_controllen;
11241da177e4SLinus Torvalds 		return put_user(len, optlen);
11251da177e4SLinus Torvalds 	}
11261da177e4SLinus Torvalds 	case IP_FREEBIND:
11271da177e4SLinus Torvalds 		val = inet->freebind;
11281da177e4SLinus Torvalds 		break;
11291da177e4SLinus Torvalds 	default:
11301da177e4SLinus Torvalds 		release_sock(sk);
11311da177e4SLinus Torvalds 		return -ENOPROTOOPT;
11321da177e4SLinus Torvalds 	}
11331da177e4SLinus Torvalds 	release_sock(sk);
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
11361da177e4SLinus Torvalds 		unsigned char ucval = (unsigned char)val;
11371da177e4SLinus Torvalds 		len = 1;
11381da177e4SLinus Torvalds 		if (put_user(len, optlen))
11391da177e4SLinus Torvalds 			return -EFAULT;
11401da177e4SLinus Torvalds 		if (copy_to_user(optval,&ucval,1))
11411da177e4SLinus Torvalds 			return -EFAULT;
11421da177e4SLinus Torvalds 	} else {
11431da177e4SLinus Torvalds 		len = min_t(unsigned int, sizeof(int), len);
11441da177e4SLinus Torvalds 		if (put_user(len, optlen))
11451da177e4SLinus Torvalds 			return -EFAULT;
11461da177e4SLinus Torvalds 		if (copy_to_user(optval,&val,len))
11471da177e4SLinus Torvalds 			return -EFAULT;
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds 	return 0;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
11523fdadf7dSDmitry Mishin int ip_getsockopt(struct sock *sk, int level,
11533fdadf7dSDmitry Mishin 		  int optname, char __user *optval, int __user *optlen)
11543fdadf7dSDmitry Mishin {
11553fdadf7dSDmitry Mishin 	int err;
11563fdadf7dSDmitry Mishin 
11573fdadf7dSDmitry Mishin 	err = do_ip_getsockopt(sk, level, optname, optval, optlen);
11583fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
11593fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
11606a9fb947SPavel Emelyanov 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
11616a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
11623fdadf7dSDmitry Mishin 		int len;
11633fdadf7dSDmitry Mishin 
11643fdadf7dSDmitry Mishin 		if (get_user(len,optlen))
11653fdadf7dSDmitry Mishin 			return -EFAULT;
11663fdadf7dSDmitry Mishin 
11673fdadf7dSDmitry Mishin 		lock_sock(sk);
11683fdadf7dSDmitry Mishin 		err = nf_getsockopt(sk, PF_INET, optname, optval,
11693fdadf7dSDmitry Mishin 				&len);
11703fdadf7dSDmitry Mishin 		release_sock(sk);
11713fdadf7dSDmitry Mishin 		if (err >= 0)
11723fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
11733fdadf7dSDmitry Mishin 		return err;
11743fdadf7dSDmitry Mishin 	}
11753fdadf7dSDmitry Mishin #endif
11763fdadf7dSDmitry Mishin 	return err;
11773fdadf7dSDmitry Mishin }
11783fdadf7dSDmitry Mishin 
11793fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
1180543d9cfeSArnaldo Carvalho de Melo int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1181543d9cfeSArnaldo Carvalho de Melo 			 char __user *optval, int __user *optlen)
11823fdadf7dSDmitry Mishin {
1183543d9cfeSArnaldo Carvalho de Melo 	int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
11843fdadf7dSDmitry Mishin #ifdef CONFIG_NETFILTER
11853fdadf7dSDmitry Mishin 	/* we need to exclude all possible ENOPROTOOPTs except default case */
11866a9fb947SPavel Emelyanov 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
11876a9fb947SPavel Emelyanov 			!ip_mroute_opt(optname)) {
11883fdadf7dSDmitry Mishin 		int len;
11893fdadf7dSDmitry Mishin 
11903fdadf7dSDmitry Mishin 		if (get_user(len, optlen))
11913fdadf7dSDmitry Mishin 			return -EFAULT;
11923fdadf7dSDmitry Mishin 
11933fdadf7dSDmitry Mishin 		lock_sock(sk);
1194543d9cfeSArnaldo Carvalho de Melo 		err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
11953fdadf7dSDmitry Mishin 		release_sock(sk);
11963fdadf7dSDmitry Mishin 		if (err >= 0)
11973fdadf7dSDmitry Mishin 			err = put_user(len, optlen);
11983fdadf7dSDmitry Mishin 		return err;
11993fdadf7dSDmitry Mishin 	}
12003fdadf7dSDmitry Mishin #endif
12013fdadf7dSDmitry Mishin 	return err;
12023fdadf7dSDmitry Mishin }
1203543d9cfeSArnaldo Carvalho de Melo 
1204543d9cfeSArnaldo Carvalho de Melo EXPORT_SYMBOL(compat_ip_getsockopt);
12053fdadf7dSDmitry Mishin #endif
12063fdadf7dSDmitry Mishin 
12071da177e4SLinus Torvalds EXPORT_SYMBOL(ip_cmsg_recv);
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds EXPORT_SYMBOL(ip_getsockopt);
12101da177e4SLinus Torvalds EXPORT_SYMBOL(ip_setsockopt);
1211