ip_sockglue.c 27.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		The IP to API glue.
7
 *
L
Linus Torvalds 已提交
8 9 10 11 12 13 14
 * Version:	$Id: ip_sockglue.c,v 1.62 2002/02/01 22:01:04 davem Exp $
 *
 * Authors:	see ip.c
 *
 * Fixes:
 *		Many		:	Split from ip.c , see ip.c for history.
 *		Martin Mares	:	TOS setting fixed.
15
 *		Alan Cox	:	Fixed a couple of oopses in Martin's
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23 24 25
 *					TOS tweaks.
 *		Mike McLagan	:	Routing by source
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/icmp.h>
26
#include <linux/inetdevice.h>
L
Linus Torvalds 已提交
27 28 29 30
#include <linux/netdevice.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/icmp.h>
31
#include <net/tcp_states.h>
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <linux/udp.h>
#include <linux/igmp.h>
#include <linux/netfilter.h>
#include <linux/route.h>
#include <linux/mroute.h>
#include <net/route.h>
#include <net/xfrm.h>
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
#include <net/transp_v6.h>
#endif

#include <linux/errqueue.h>
#include <asm/uaccess.h>

#define IP_CMSG_PKTINFO		1
#define IP_CMSG_TTL		2
#define IP_CMSG_TOS		4
#define IP_CMSG_RECVOPTS	8
#define IP_CMSG_RETOPTS		16
C
Catherine Zhang 已提交
51
#define IP_CMSG_PASSSEC		32
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61

/*
 *	SOL_IP control messages.
 */

static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
{
	struct in_pktinfo info;
	struct rtable *rt = (struct rtable *)skb->dst;

62
	info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
	if (rt) {
		info.ipi_ifindex = rt->rt_iif;
		info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
	} else {
		info.ipi_ifindex = 0;
		info.ipi_spec_dst.s_addr = 0;
	}

	put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
}

static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
{
76
	int ttl = ip_hdr(skb)->ttl;
L
Linus Torvalds 已提交
77 78 79 80 81
	put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
}

static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
{
82
	put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
L
Linus Torvalds 已提交
83 84 85 86 87 88 89
}

static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
{
	if (IPCB(skb)->opt.optlen == 0)
		return;

90 91
	put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
		 ip_hdr(skb) + 1);
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}


static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
{
	unsigned char optbuf[sizeof(struct ip_options) + 40];
	struct ip_options * opt = (struct ip_options*)optbuf;

	if (IPCB(skb)->opt.optlen == 0)
		return;

	if (ip_options_echo(opt, skb)) {
		msg->msg_flags |= MSG_CTRUNC;
		return;
	}
	ip_options_undo(opt);

	put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
}

C
Catherine Zhang 已提交
112 113 114
static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
{
	char *secdata;
115
	u32 seclen, secid;
C
Catherine Zhang 已提交
116 117
	int err;

118 119 120 121 122
	err = security_socket_getpeersec_dgram(NULL, skb, &secid);
	if (err)
		return;

	err = security_secid_to_secctx(secid, &secdata, &seclen);
C
Catherine Zhang 已提交
123 124 125 126
	if (err)
		return;

	put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
127
	security_release_secctx(secdata, seclen);
C
Catherine Zhang 已提交
128 129
}

L
Linus Torvalds 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
{
	struct inet_sock *inet = inet_sk(skb->sk);
	unsigned flags = inet->cmsg_flags;

	/* Ordered by supposed usage frequency */
	if (flags & 1)
		ip_cmsg_recv_pktinfo(msg, skb);
	if ((flags>>=1) == 0)
		return;

	if (flags & 1)
		ip_cmsg_recv_ttl(msg, skb);
	if ((flags>>=1) == 0)
		return;

	if (flags & 1)
		ip_cmsg_recv_tos(msg, skb);
	if ((flags>>=1) == 0)
		return;

	if (flags & 1)
		ip_cmsg_recv_opts(msg, skb);
	if ((flags>>=1) == 0)
		return;

	if (flags & 1)
		ip_cmsg_recv_retopts(msg, skb);
C
Catherine Zhang 已提交
159 160 161 162 163
	if ((flags>>=1) == 0)
		return;

	if (flags & 1)
		ip_cmsg_recv_security(msg, skb);
L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
}

int ip_cmsg_send(struct msghdr *msg, struct ipcm_cookie *ipc)
{
	int err;
	struct cmsghdr *cmsg;

	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
		if (!CMSG_OK(msg, cmsg))
			return -EINVAL;
		if (cmsg->cmsg_level != SOL_IP)
			continue;
		switch (cmsg->cmsg_type) {
		case IP_RETOPTS:
			err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
179
			err = ip_options_get(&ipc->opt, CMSG_DATA(cmsg), err < 40 ? err : 40);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
			if (err)
				return err;
			break;
		case IP_PKTINFO:
		{
			struct in_pktinfo *info;
			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
				return -EINVAL;
			info = (struct in_pktinfo *)CMSG_DATA(cmsg);
			ipc->oif = info->ipi_ifindex;
			ipc->addr = info->ipi_spec_dst.s_addr;
			break;
		}
		default:
			return -EINVAL;
		}
	}
	return 0;
}


/* Special input handler for packets caught by router alert option.
   They are selected only by protocol field, and then processed likely
   local ones; but only if someone wants them! Otherwise, router
   not running rsvpd will kill RSVP.

   It is user level problem, what it will make with them.
   I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
   but receiver should be enough clever f.e. to forward mtrace requests,
   sent to multicast group to reach destination designated router.
 */
struct ip_ra_chain *ip_ra_chain;
DEFINE_RWLOCK(ip_ra_lock);

int ip_ra_control(struct sock *sk, unsigned char on, void (*destructor)(struct sock *))
{
	struct ip_ra_chain *ra, *new_ra, **rap;

	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
		return -EINVAL;

	new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;

	write_lock_bh(&ip_ra_lock);
	for (rap = &ip_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
		if (ra->sk == sk) {
			if (on) {
				write_unlock_bh(&ip_ra_lock);
J
Jesper Juhl 已提交
228
				kfree(new_ra);
L
Linus Torvalds 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
				return -EADDRINUSE;
			}
			*rap = ra->next;
			write_unlock_bh(&ip_ra_lock);

			if (ra->destructor)
				ra->destructor(sk);
			sock_put(sk);
			kfree(ra);
			return 0;
		}
	}
	if (new_ra == NULL) {
		write_unlock_bh(&ip_ra_lock);
		return -ENOBUFS;
	}
	new_ra->sk = sk;
	new_ra->destructor = destructor;

	new_ra->next = ra;
	*rap = new_ra;
	sock_hold(sk);
	write_unlock_bh(&ip_ra_lock);

	return 0;
}

256
void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
A
Al Viro 已提交
257
		   __be16 port, u32 info, u8 *payload)
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268
{
	struct inet_sock *inet = inet_sk(sk);
	struct sock_exterr_skb *serr;

	if (!inet->recverr)
		return;

	skb = skb_clone(skb, GFP_ATOMIC);
	if (!skb)
		return;

269
	serr = SKB_EXT_ERR(skb);
L
Linus Torvalds 已提交
270 271
	serr->ee.ee_errno = err;
	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
272 273
	serr->ee.ee_type = icmp_hdr(skb)->type;
	serr->ee.ee_code = icmp_hdr(skb)->code;
L
Linus Torvalds 已提交
274 275 276
	serr->ee.ee_pad = 0;
	serr->ee.ee_info = info;
	serr->ee.ee_data = 0;
277
	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
278
				   skb_network_header(skb);
L
Linus Torvalds 已提交
279 280
	serr->port = port;

281 282 283 284 285 286
	if (skb_pull(skb, payload - skb->data) != NULL) {
		skb_reset_transport_header(skb);
		if (sock_queue_err_skb(sk, skb) == 0)
			return;
	}
	kfree_skb(skb);
L
Linus Torvalds 已提交
287 288
}

A
Al Viro 已提交
289
void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302
{
	struct inet_sock *inet = inet_sk(sk);
	struct sock_exterr_skb *serr;
	struct iphdr *iph;
	struct sk_buff *skb;

	if (!inet->recverr)
		return;

	skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
	if (!skb)
		return;

303 304
	skb_put(skb, sizeof(struct iphdr));
	skb_reset_network_header(skb);
305
	iph = ip_hdr(skb);
L
Linus Torvalds 已提交
306 307
	iph->daddr = daddr;

308
	serr = SKB_EXT_ERR(skb);
L
Linus Torvalds 已提交
309 310
	serr->ee.ee_errno = err;
	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
311
	serr->ee.ee_type = 0;
L
Linus Torvalds 已提交
312 313 314 315
	serr->ee.ee_code = 0;
	serr->ee.ee_pad = 0;
	serr->ee.ee_info = info;
	serr->ee.ee_data = 0;
316
	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
L
Linus Torvalds 已提交
317 318
	serr->port = port;

319
	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
320
	skb_reset_transport_header(skb);
L
Linus Torvalds 已提交
321 322 323 324 325

	if (sock_queue_err_skb(sk, skb))
		kfree_skb(skb);
}

326
/*
L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
 *	Handle MSG_ERRQUEUE
 */
int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
{
	struct sock_exterr_skb *serr;
	struct sk_buff *skb, *skb2;
	struct sockaddr_in *sin;
	struct {
		struct sock_extended_err ee;
		struct sockaddr_in	 offender;
	} errhdr;
	int err;
	int copied;

	err = -EAGAIN;
	skb = skb_dequeue(&sk->sk_error_queue);
	if (skb == NULL)
		goto out;

	copied = skb->len;
	if (copied > len) {
		msg->msg_flags |= MSG_TRUNC;
		copied = len;
	}
	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
	if (err)
		goto out_free_skb;

	sock_recv_timestamp(msg, sk, skb);

	serr = SKB_EXT_ERR(skb);

	sin = (struct sockaddr_in *)msg->msg_name;
	if (sin) {
		sin->sin_family = AF_INET;
362 363
		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
						   serr->addr_offset);
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372 373 374
		sin->sin_port = serr->port;
		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
	}

	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
	sin = &errhdr.offender;
	sin->sin_family = AF_UNSPEC;
	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
		struct inet_sock *inet = inet_sk(sk);

		sin->sin_family = AF_INET;
375
		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389
		sin->sin_port = 0;
		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
		if (inet->cmsg_flags)
			ip_cmsg_recv(msg, skb);
	}

	put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);

	/* Now we could try to dump offended packet options */

	msg->msg_flags |= MSG_ERRQUEUE;
	err = copied;

	/* Reset and regenerate socket error */
390
	spin_lock_bh(&sk->sk_error_queue.lock);
L
Linus Torvalds 已提交
391 392 393
	sk->sk_err = 0;
	if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
		sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
394
		spin_unlock_bh(&sk->sk_error_queue.lock);
L
Linus Torvalds 已提交
395 396
		sk->sk_error_report(sk);
	} else
397
		spin_unlock_bh(&sk->sk_error_queue.lock);
L
Linus Torvalds 已提交
398

399
out_free_skb:
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410
	kfree_skb(skb);
out:
	return err;
}


/*
 *	Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
 *	an IP socket.
 */

411
static int do_ip_setsockopt(struct sock *sk, int level,
S
Stephen Hemminger 已提交
412
			    int optname, char __user *optval, int optlen)
L
Linus Torvalds 已提交
413 414 415 416
{
	struct inet_sock *inet = inet_sk(sk);
	int val=0,err;

417
	if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
S
Stephen Hemminger 已提交
418 419 420 421 422 423 424 425
			     (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
			     (1<<IP_RETOPTS) | (1<<IP_TOS) |
			     (1<<IP_TTL) | (1<<IP_HDRINCL) |
			     (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
			     (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
			     (1<<IP_PASSSEC))) ||
	    optname == IP_MULTICAST_TTL ||
	    optname == IP_MULTICAST_LOOP) {
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439
		if (optlen >= sizeof(int)) {
			if (get_user(val, (int __user *) optval))
				return -EFAULT;
		} else if (optlen >= sizeof(char)) {
			unsigned char ucval;

			if (get_user(ucval, (unsigned char __user *) optval))
				return -EFAULT;
			val = (int) ucval;
		}
	}

	/* If optlen==0, it is equivalent to val == 0 */

440
	if (ip_mroute_opt(optname))
L
Linus Torvalds 已提交
441 442 443 444 445 446
		return ip_mroute_setsockopt(sk,optname,optval,optlen);

	err = 0;
	lock_sock(sk);

	switch (optname) {
S
Stephen Hemminger 已提交
447 448 449 450 451 452 453 454 455 456
	case IP_OPTIONS:
	{
		struct ip_options * opt = NULL;
		if (optlen > 40 || optlen < 0)
			goto e_inval;
		err = ip_options_get_from_user(&opt, optval, optlen);
		if (err)
			break;
		if (inet->is_icsk) {
			struct inet_connection_sock *icsk = inet_csk(sk);
L
Linus Torvalds 已提交
457
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
S
Stephen Hemminger 已提交
458 459 460 461
			if (sk->sk_family == PF_INET ||
			    (!((1 << sk->sk_state) &
			       (TCPF_LISTEN | TCPF_CLOSE)) &&
			     inet->daddr != LOOPBACK4_IPV6)) {
L
Linus Torvalds 已提交
462
#endif
S
Stephen Hemminger 已提交
463 464 465 466 467
				if (inet->opt)
					icsk->icsk_ext_hdr_len -= inet->opt->optlen;
				if (opt)
					icsk->icsk_ext_hdr_len += opt->optlen;
				icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
L
Linus Torvalds 已提交
468 469
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
			}
S
Stephen Hemminger 已提交
470
#endif
L
Linus Torvalds 已提交
471
		}
S
Stephen Hemminger 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
		opt = xchg(&inet->opt, opt);
		kfree(opt);
		break;
	}
	case IP_PKTINFO:
		if (val)
			inet->cmsg_flags |= IP_CMSG_PKTINFO;
		else
			inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
		break;
	case IP_RECVTTL:
		if (val)
			inet->cmsg_flags |=  IP_CMSG_TTL;
		else
			inet->cmsg_flags &= ~IP_CMSG_TTL;
		break;
	case IP_RECVTOS:
		if (val)
			inet->cmsg_flags |=  IP_CMSG_TOS;
		else
			inet->cmsg_flags &= ~IP_CMSG_TOS;
		break;
	case IP_RECVOPTS:
		if (val)
			inet->cmsg_flags |=  IP_CMSG_RECVOPTS;
		else
			inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
		break;
	case IP_RETOPTS:
		if (val)
			inet->cmsg_flags |= IP_CMSG_RETOPTS;
		else
			inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
		break;
	case IP_PASSSEC:
		if (val)
			inet->cmsg_flags |= IP_CMSG_PASSSEC;
		else
			inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
		break;
	case IP_TOS:	/* This sets both TOS and Precedence */
		if (sk->sk_type == SOCK_STREAM) {
			val &= ~3;
			val |= inet->tos & 3;
		}
		if (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
		    !capable(CAP_NET_ADMIN)) {
			err = -EPERM;
L
Linus Torvalds 已提交
520
			break;
S
Stephen Hemminger 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
		}
		if (inet->tos != val) {
			inet->tos = val;
			sk->sk_priority = rt_tos2priority(val);
			sk_dst_reset(sk);
		}
		break;
	case IP_TTL:
		if (optlen<1)
			goto e_inval;
		if (val != -1 && (val < 1 || val>255))
			goto e_inval;
		inet->uc_ttl = val;
		break;
	case IP_HDRINCL:
		if (sk->sk_type != SOCK_RAW) {
			err = -ENOPROTOOPT;
C
Catherine Zhang 已提交
538
			break;
S
Stephen Hemminger 已提交
539 540 541 542
		}
		inet->hdrincl = val ? 1 : 0;
		break;
	case IP_MTU_DISCOVER:
J
John Heffner 已提交
543
		if (val<0 || val>3)
S
Stephen Hemminger 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
			goto e_inval;
		inet->pmtudisc = val;
		break;
	case IP_RECVERR:
		inet->recverr = !!val;
		if (!val)
			skb_queue_purge(&sk->sk_error_queue);
		break;
	case IP_MULTICAST_TTL:
		if (sk->sk_type == SOCK_STREAM)
			goto e_inval;
		if (optlen<1)
			goto e_inval;
		if (val==-1)
			val = 1;
		if (val < 0 || val > 255)
			goto e_inval;
		inet->mc_ttl = val;
		break;
	case IP_MULTICAST_LOOP:
		if (optlen<1)
			goto e_inval;
		inet->mc_loop = !!val;
		break;
	case IP_MULTICAST_IF:
	{
		struct ip_mreqn mreq;
		struct net_device *dev = NULL;

		if (sk->sk_type == SOCK_STREAM)
			goto e_inval;
		/*
		 *	Check the arguments are allowable
		 */

		err = -EFAULT;
		if (optlen >= sizeof(struct ip_mreqn)) {
			if (copy_from_user(&mreq,optval,sizeof(mreq)))
L
Linus Torvalds 已提交
582
				break;
S
Stephen Hemminger 已提交
583 584 585 586 587 588 589 590 591 592 593 594
		} else {
			memset(&mreq, 0, sizeof(mreq));
			if (optlen >= sizeof(struct in_addr) &&
			    copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
				break;
		}

		if (!mreq.imr_ifindex) {
			if (mreq.imr_address.s_addr == INADDR_ANY) {
				inet->mc_index = 0;
				inet->mc_addr  = 0;
				err = 0;
L
Linus Torvalds 已提交
595 596
				break;
			}
S
Stephen Hemminger 已提交
597 598 599 600 601 602
			dev = ip_dev_find(mreq.imr_address.s_addr);
			if (dev) {
				mreq.imr_ifindex = dev->ifindex;
				dev_put(dev);
			}
		} else
603
			dev = __dev_get_by_index(&init_net, mreq.imr_ifindex);
L
Linus Torvalds 已提交
604 605


S
Stephen Hemminger 已提交
606 607 608 609 610 611 612 613
		err = -EADDRNOTAVAIL;
		if (!dev)
			break;

		err = -EINVAL;
		if (sk->sk_bound_dev_if &&
		    mreq.imr_ifindex != sk->sk_bound_dev_if)
			break;
L
Linus Torvalds 已提交
614

S
Stephen Hemminger 已提交
615 616 617 618 619
		inet->mc_index = mreq.imr_ifindex;
		inet->mc_addr  = mreq.imr_address.s_addr;
		err = 0;
		break;
	}
L
Linus Torvalds 已提交
620

S
Stephen Hemminger 已提交
621 622 623 624
	case IP_ADD_MEMBERSHIP:
	case IP_DROP_MEMBERSHIP:
	{
		struct ip_mreqn mreq;
L
Linus Torvalds 已提交
625

626 627 628 629
		err = -EPROTO;
		if (inet_sk(sk)->is_icsk)
			break;

S
Stephen Hemminger 已提交
630 631 632 633 634
		if (optlen < sizeof(struct ip_mreq))
			goto e_inval;
		err = -EFAULT;
		if (optlen >= sizeof(struct ip_mreqn)) {
			if (copy_from_user(&mreq,optval,sizeof(mreq)))
L
Linus Torvalds 已提交
635
				break;
S
Stephen Hemminger 已提交
636 637 638
		} else {
			memset(&mreq, 0, sizeof(mreq));
			if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
L
Linus Torvalds 已提交
639
				break;
S
Stephen Hemminger 已提交
640
		}
L
Linus Torvalds 已提交
641

S
Stephen Hemminger 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
		if (optname == IP_ADD_MEMBERSHIP)
			err = ip_mc_join_group(sk, &mreq);
		else
			err = ip_mc_leave_group(sk, &mreq);
		break;
	}
	case IP_MSFILTER:
	{
		extern int sysctl_igmp_max_msf;
		struct ip_msfilter *msf;

		if (optlen < IP_MSFILTER_SIZE(0))
			goto e_inval;
		if (optlen > sysctl_optmem_max) {
			err = -ENOBUFS;
L
Linus Torvalds 已提交
657 658
			break;
		}
S
Stephen Hemminger 已提交
659
		msf = kmalloc(optlen, GFP_KERNEL);
S
Stephen Hemminger 已提交
660
		if (!msf) {
S
Stephen Hemminger 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
			err = -ENOBUFS;
			break;
		}
		err = -EFAULT;
		if (copy_from_user(msf, optval, optlen)) {
			kfree(msf);
			break;
		}
		/* numsrc >= (1G-4) overflow in 32 bits */
		if (msf->imsf_numsrc >= 0x3ffffffcU ||
		    msf->imsf_numsrc > sysctl_igmp_max_msf) {
			kfree(msf);
			err = -ENOBUFS;
			break;
		}
		if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
			kfree(msf);
			err = -EINVAL;
			break;
		}
		err = ip_mc_msfilter(sk, msf, 0);
		kfree(msf);
		break;
	}
	case IP_BLOCK_SOURCE:
	case IP_UNBLOCK_SOURCE:
	case IP_ADD_SOURCE_MEMBERSHIP:
	case IP_DROP_SOURCE_MEMBERSHIP:
	{
		struct ip_mreq_source mreqs;
		int omode, add;
L
Linus Torvalds 已提交
692

S
Stephen Hemminger 已提交
693 694 695
		if (optlen != sizeof(struct ip_mreq_source))
			goto e_inval;
		if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
L
Linus Torvalds 已提交
696 697 698
			err = -EFAULT;
			break;
		}
S
Stephen Hemminger 已提交
699 700 701 702 703 704 705 706
		if (optname == IP_BLOCK_SOURCE) {
			omode = MCAST_EXCLUDE;
			add = 1;
		} else if (optname == IP_UNBLOCK_SOURCE) {
			omode = MCAST_EXCLUDE;
			add = 0;
		} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
			struct ip_mreqn mreq;
L
Linus Torvalds 已提交
707

S
Stephen Hemminger 已提交
708 709 710 711 712
			mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
			mreq.imr_address.s_addr = mreqs.imr_interface;
			mreq.imr_ifindex = 0;
			err = ip_mc_join_group(sk, &mreq);
			if (err && err != -EADDRINUSE)
L
Linus Torvalds 已提交
713
				break;
S
Stephen Hemminger 已提交
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
			omode = MCAST_INCLUDE;
			add = 1;
		} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
			omode = MCAST_INCLUDE;
			add = 0;
		}
		err = ip_mc_source(add, omode, sk, &mreqs, 0);
		break;
	}
	case MCAST_JOIN_GROUP:
	case MCAST_LEAVE_GROUP:
	{
		struct group_req greq;
		struct sockaddr_in *psin;
		struct ip_mreqn mreq;

		if (optlen < sizeof(struct group_req))
			goto e_inval;
		err = -EFAULT;
		if (copy_from_user(&greq, optval, sizeof(greq)))
			break;
		psin = (struct sockaddr_in *)&greq.gr_group;
		if (psin->sin_family != AF_INET)
			goto e_inval;
		memset(&mreq, 0, sizeof(mreq));
		mreq.imr_multiaddr = psin->sin_addr;
		mreq.imr_ifindex = greq.gr_interface;

		if (optname == MCAST_JOIN_GROUP)
			err = ip_mc_join_group(sk, &mreq);
		else
			err = ip_mc_leave_group(sk, &mreq);
		break;
	}
	case MCAST_JOIN_SOURCE_GROUP:
	case MCAST_LEAVE_SOURCE_GROUP:
	case MCAST_BLOCK_SOURCE:
	case MCAST_UNBLOCK_SOURCE:
	{
		struct group_source_req greqs;
		struct ip_mreq_source mreqs;
		struct sockaddr_in *psin;
		int omode, add;

		if (optlen != sizeof(struct group_source_req))
			goto e_inval;
		if (copy_from_user(&greqs, optval, sizeof(greqs))) {
L
Linus Torvalds 已提交
761 762 763
			err = -EFAULT;
			break;
		}
S
Stephen Hemminger 已提交
764 765 766
		if (greqs.gsr_group.ss_family != AF_INET ||
		    greqs.gsr_source.ss_family != AF_INET) {
			err = -EADDRNOTAVAIL;
L
Linus Torvalds 已提交
767 768
			break;
		}
S
Stephen Hemminger 已提交
769 770 771 772 773 774 775 776 777 778 779 780 781
		psin = (struct sockaddr_in *)&greqs.gsr_group;
		mreqs.imr_multiaddr = psin->sin_addr.s_addr;
		psin = (struct sockaddr_in *)&greqs.gsr_source;
		mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
		mreqs.imr_interface = 0; /* use index for mc_source */

		if (optname == MCAST_BLOCK_SOURCE) {
			omode = MCAST_EXCLUDE;
			add = 1;
		} else if (optname == MCAST_UNBLOCK_SOURCE) {
			omode = MCAST_EXCLUDE;
			add = 0;
		} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
L
Linus Torvalds 已提交
782 783
			struct ip_mreqn mreq;

S
Stephen Hemminger 已提交
784
			psin = (struct sockaddr_in *)&greqs.gsr_group;
L
Linus Torvalds 已提交
785
			mreq.imr_multiaddr = psin->sin_addr;
S
Stephen Hemminger 已提交
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
			mreq.imr_address.s_addr = 0;
			mreq.imr_ifindex = greqs.gsr_interface;
			err = ip_mc_join_group(sk, &mreq);
			if (err && err != -EADDRINUSE)
				break;
			greqs.gsr_interface = mreq.imr_ifindex;
			omode = MCAST_INCLUDE;
			add = 1;
		} else /* MCAST_LEAVE_SOURCE_GROUP */ {
			omode = MCAST_INCLUDE;
			add = 0;
		}
		err = ip_mc_source(add, omode, sk, &mreqs,
				   greqs.gsr_interface);
		break;
	}
	case MCAST_MSFILTER:
	{
		extern int sysctl_igmp_max_msf;
		struct sockaddr_in *psin;
		struct ip_msfilter *msf = NULL;
		struct group_filter *gsf = NULL;
		int msize, i, ifindex;

		if (optlen < GROUP_FILTER_SIZE(0))
			goto e_inval;
		if (optlen > sysctl_optmem_max) {
			err = -ENOBUFS;
L
Linus Torvalds 已提交
814 815
			break;
		}
S
Stephen Hemminger 已提交
816
		gsf = kmalloc(optlen,GFP_KERNEL);
S
Stephen Hemminger 已提交
817
		if (!gsf) {
S
Stephen Hemminger 已提交
818
			err = -ENOBUFS;
L
Linus Torvalds 已提交
819 820
			break;
		}
S
Stephen Hemminger 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
		err = -EFAULT;
		if (copy_from_user(gsf, optval, optlen)) {
			goto mc_msf_out;
		}
		/* numsrc >= (4G-140)/128 overflow in 32 bits */
		if (gsf->gf_numsrc >= 0x1ffffff ||
		    gsf->gf_numsrc > sysctl_igmp_max_msf) {
			err = -ENOBUFS;
			goto mc_msf_out;
		}
		if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
			err = -EINVAL;
			goto mc_msf_out;
		}
		msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
		msf = kmalloc(msize,GFP_KERNEL);
S
Stephen Hemminger 已提交
837
		if (!msf) {
S
Stephen Hemminger 已提交
838 839 840 841 842 843
			err = -ENOBUFS;
			goto mc_msf_out;
		}
		ifindex = gsf->gf_interface;
		psin = (struct sockaddr_in *)&gsf->gf_group;
		if (psin->sin_family != AF_INET) {
L
Linus Torvalds 已提交
844
			err = -EADDRNOTAVAIL;
S
Stephen Hemminger 已提交
845
			goto mc_msf_out;
L
Linus Torvalds 已提交
846
		}
S
Stephen Hemminger 已提交
847 848 849 850 851 852 853
		msf->imsf_multiaddr = psin->sin_addr.s_addr;
		msf->imsf_interface = 0;
		msf->imsf_fmode = gsf->gf_fmode;
		msf->imsf_numsrc = gsf->gf_numsrc;
		err = -EADDRNOTAVAIL;
		for (i=0; i<gsf->gf_numsrc; ++i) {
			psin = (struct sockaddr_in *)&gsf->gf_slist[i];
854

S
Stephen Hemminger 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
			if (psin->sin_family != AF_INET)
				goto mc_msf_out;
			msf->imsf_slist[i] = psin->sin_addr.s_addr;
		}
		kfree(gsf);
		gsf = NULL;

		err = ip_mc_msfilter(sk, msf, ifindex);
	mc_msf_out:
		kfree(msf);
		kfree(gsf);
		break;
	}
	case IP_ROUTER_ALERT:
		err = ip_ra_control(sk, val ? 1 : 0, NULL);
		break;

	case IP_FREEBIND:
		if (optlen<1)
			goto e_inval;
		inet->freebind = !!val;
		break;

	case IP_IPSEC_POLICY:
	case IP_XFRM_POLICY:
		err = -EPERM;
		if (!capable(CAP_NET_ADMIN))
L
Linus Torvalds 已提交
882
			break;
S
Stephen Hemminger 已提交
883 884
		err = xfrm_user_policy(sk, optname, optval, optlen);
		break;
L
Linus Torvalds 已提交
885

S
Stephen Hemminger 已提交
886 887 888
	default:
		err = -ENOPROTOOPT;
		break;
L
Linus Torvalds 已提交
889 890 891 892 893 894 895 896 897
	}
	release_sock(sk);
	return err;

e_inval:
	release_sock(sk);
	return -EINVAL;
}

898 899 900 901 902 903 904 905 906 907 908 909
int ip_setsockopt(struct sock *sk, int level,
		int optname, char __user *optval, int optlen)
{
	int err;

	if (level != SOL_IP)
		return -ENOPROTOOPT;

	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
#ifdef CONFIG_NETFILTER
	/* we need to exclude all possible ENOPROTOOPTs except default case */
	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
910 911 912
			optname != IP_IPSEC_POLICY &&
			optname != IP_XFRM_POLICY &&
			!ip_mroute_opt(optname)) {
913 914 915 916 917 918 919 920 921
		lock_sock(sk);
		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
		release_sock(sk);
	}
#endif
	return err;
}

#ifdef CONFIG_COMPAT
922 923
int compat_ip_setsockopt(struct sock *sk, int level, int optname,
			 char __user *optval, int optlen)
924 925 926 927 928 929 930 931 932 933
{
	int err;

	if (level != SOL_IP)
		return -ENOPROTOOPT;

	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
#ifdef CONFIG_NETFILTER
	/* we need to exclude all possible ENOPROTOOPTs except default case */
	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
934 935 936
			optname != IP_IPSEC_POLICY &&
			optname != IP_XFRM_POLICY &&
			!ip_mroute_opt(optname)) {
937
		lock_sock(sk);
938 939
		err = compat_nf_setsockopt(sk, PF_INET, optname,
					   optval, optlen);
940 941 942 943 944
		release_sock(sk);
	}
#endif
	return err;
}
945 946

EXPORT_SYMBOL(compat_ip_setsockopt);
947 948
#endif

L
Linus Torvalds 已提交
949 950 951 952 953
/*
 *	Get the options. Note for future reference. The GET of IP options gets the
 *	_received_ ones. The set sets the _sent_ ones.
 */

954
static int do_ip_getsockopt(struct sock *sk, int level, int optname,
S
Stephen Hemminger 已提交
955
			    char __user *optval, int __user *optlen)
L
Linus Torvalds 已提交
956 957 958 959
{
	struct inet_sock *inet = inet_sk(sk);
	int val;
	int len;
960

S
Stephen Hemminger 已提交
961
	if (level != SOL_IP)
L
Linus Torvalds 已提交
962 963
		return -EOPNOTSUPP;

964
	if (ip_mroute_opt(optname))
L
Linus Torvalds 已提交
965 966
		return ip_mroute_getsockopt(sk,optname,optval,optlen);

S
Stephen Hemminger 已提交
967
	if (get_user(len,optlen))
L
Linus Torvalds 已提交
968
		return -EFAULT;
S
Stephen Hemminger 已提交
969
	if (len < 0)
L
Linus Torvalds 已提交
970
		return -EINVAL;
971

L
Linus Torvalds 已提交
972 973
	lock_sock(sk);

S
Stephen Hemminger 已提交
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	switch (optname) {
	case IP_OPTIONS:
	{
		unsigned char optbuf[sizeof(struct ip_options)+40];
		struct ip_options * opt = (struct ip_options*)optbuf;
		opt->optlen = 0;
		if (inet->opt)
			memcpy(optbuf, inet->opt,
			       sizeof(struct ip_options)+
			       inet->opt->optlen);
		release_sock(sk);

		if (opt->optlen == 0)
			return put_user(0, optlen);

		ip_options_undo(opt);

		len = min_t(unsigned int, len, opt->optlen);
		if (put_user(len, optlen))
			return -EFAULT;
		if (copy_to_user(optval, opt->__data, len))
			return -EFAULT;
		return 0;
	}
	case IP_PKTINFO:
		val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
		break;
	case IP_RECVTTL:
		val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
		break;
	case IP_RECVTOS:
		val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
		break;
	case IP_RECVOPTS:
		val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
		break;
	case IP_RETOPTS:
		val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
		break;
	case IP_PASSSEC:
		val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
		break;
	case IP_TOS:
		val = inet->tos;
		break;
	case IP_TTL:
		val = (inet->uc_ttl == -1 ?
		       sysctl_ip_default_ttl :
		       inet->uc_ttl);
		break;
	case IP_HDRINCL:
		val = inet->hdrincl;
		break;
	case IP_MTU_DISCOVER:
		val = inet->pmtudisc;
		break;
	case IP_MTU:
	{
		struct dst_entry *dst;
		val = 0;
		dst = sk_dst_get(sk);
		if (dst) {
			val = dst_mtu(dst);
			dst_release(dst);
L
Linus Torvalds 已提交
1038
		}
S
Stephen Hemminger 已提交
1039
		if (!val) {
L
Linus Torvalds 已提交
1040
			release_sock(sk);
S
Stephen Hemminger 已提交
1041
			return -ENOTCONN;
L
Linus Torvalds 已提交
1042
		}
S
Stephen Hemminger 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
		break;
	}
	case IP_RECVERR:
		val = inet->recverr;
		break;
	case IP_MULTICAST_TTL:
		val = inet->mc_ttl;
		break;
	case IP_MULTICAST_LOOP:
		val = inet->mc_loop;
		break;
	case IP_MULTICAST_IF:
	{
		struct in_addr addr;
		len = min_t(unsigned int, len, sizeof(struct in_addr));
		addr.s_addr = inet->mc_addr;
		release_sock(sk);
L
Linus Torvalds 已提交
1060

S
Stephen Hemminger 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
		if (put_user(len, optlen))
			return -EFAULT;
		if (copy_to_user(optval, &addr, len))
			return -EFAULT;
		return 0;
	}
	case IP_MSFILTER:
	{
		struct ip_msfilter msf;
		int err;

		if (len < IP_MSFILTER_SIZE(0)) {
L
Linus Torvalds 已提交
1073
			release_sock(sk);
S
Stephen Hemminger 已提交
1074
			return -EINVAL;
L
Linus Torvalds 已提交
1075
		}
S
Stephen Hemminger 已提交
1076
		if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
L
Linus Torvalds 已提交
1077
			release_sock(sk);
S
Stephen Hemminger 已提交
1078
			return -EFAULT;
L
Linus Torvalds 已提交
1079
		}
S
Stephen Hemminger 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088
		err = ip_mc_msfget(sk, &msf,
				   (struct ip_msfilter __user *)optval, optlen);
		release_sock(sk);
		return err;
	}
	case MCAST_MSFILTER:
	{
		struct group_filter gsf;
		int err;
L
Linus Torvalds 已提交
1089

S
Stephen Hemminger 已提交
1090
		if (len < GROUP_FILTER_SIZE(0)) {
L
Linus Torvalds 已提交
1091
			release_sock(sk);
S
Stephen Hemminger 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
			return -EINVAL;
		}
		if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
			release_sock(sk);
			return -EFAULT;
		}
		err = ip_mc_gsfget(sk, &gsf,
				   (struct group_filter __user *)optval, optlen);
		release_sock(sk);
		return err;
	}
	case IP_PKTOPTIONS:
	{
		struct msghdr msg;
L
Linus Torvalds 已提交
1106

S
Stephen Hemminger 已提交
1107
		release_sock(sk);
L
Linus Torvalds 已提交
1108

S
Stephen Hemminger 已提交
1109 1110
		if (sk->sk_type != SOCK_STREAM)
			return -ENOPROTOOPT;
L
Linus Torvalds 已提交
1111

S
Stephen Hemminger 已提交
1112 1113 1114
		msg.msg_control = optval;
		msg.msg_controllen = len;
		msg.msg_flags = 0;
L
Linus Torvalds 已提交
1115

S
Stephen Hemminger 已提交
1116 1117 1118 1119 1120 1121 1122
		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
			struct in_pktinfo info;

			info.ipi_addr.s_addr = inet->rcv_saddr;
			info.ipi_spec_dst.s_addr = inet->rcv_saddr;
			info.ipi_ifindex = inet->mc_index;
			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
L
Linus Torvalds 已提交
1123
		}
S
Stephen Hemminger 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
		if (inet->cmsg_flags & IP_CMSG_TTL) {
			int hlim = inet->mc_ttl;
			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
		}
		len -= msg.msg_controllen;
		return put_user(len, optlen);
	}
	case IP_FREEBIND:
		val = inet->freebind;
		break;
	default:
		release_sock(sk);
		return -ENOPROTOOPT;
L
Linus Torvalds 已提交
1137 1138
	}
	release_sock(sk);
1139

L
Linus Torvalds 已提交
1140 1141 1142
	if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
		unsigned char ucval = (unsigned char)val;
		len = 1;
S
Stephen Hemminger 已提交
1143
		if (put_user(len, optlen))
L
Linus Torvalds 已提交
1144
			return -EFAULT;
S
Stephen Hemminger 已提交
1145
		if (copy_to_user(optval,&ucval,1))
L
Linus Torvalds 已提交
1146 1147 1148
			return -EFAULT;
	} else {
		len = min_t(unsigned int, sizeof(int), len);
S
Stephen Hemminger 已提交
1149
		if (put_user(len, optlen))
L
Linus Torvalds 已提交
1150
			return -EFAULT;
S
Stephen Hemminger 已提交
1151
		if (copy_to_user(optval,&val,len))
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156
			return -EFAULT;
	}
	return 0;
}

1157
int ip_getsockopt(struct sock *sk, int level,
S
Stephen Hemminger 已提交
1158
		  int optname, char __user *optval, int __user *optlen)
1159 1160 1161 1162 1163 1164
{
	int err;

	err = do_ip_getsockopt(sk, level, optname, optval, optlen);
#ifdef CONFIG_NETFILTER
	/* we need to exclude all possible ENOPROTOOPTs except default case */
1165 1166
	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
			!ip_mroute_opt(optname)) {
1167
		int len;
1168

S
Stephen Hemminger 已提交
1169
		if (get_user(len,optlen))
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
			return -EFAULT;

		lock_sock(sk);
		err = nf_getsockopt(sk, PF_INET, optname, optval,
				&len);
		release_sock(sk);
		if (err >= 0)
			err = put_user(len, optlen);
		return err;
	}
#endif
	return err;
}

#ifdef CONFIG_COMPAT
1185 1186
int compat_ip_getsockopt(struct sock *sk, int level, int optname,
			 char __user *optval, int __user *optlen)
1187
{
1188
	int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1189 1190
#ifdef CONFIG_NETFILTER
	/* we need to exclude all possible ENOPROTOOPTs except default case */
1191 1192
	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
			!ip_mroute_opt(optname)) {
1193
		int len;
1194

1195
		if (get_user(len, optlen))
1196 1197 1198
			return -EFAULT;

		lock_sock(sk);
1199
		err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1200 1201 1202 1203 1204 1205 1206 1207
		release_sock(sk);
		if (err >= 0)
			err = put_user(len, optlen);
		return err;
	}
#endif
	return err;
}
1208 1209

EXPORT_SYMBOL(compat_ip_getsockopt);
1210 1211
#endif

L
Linus Torvalds 已提交
1212 1213 1214 1215
EXPORT_SYMBOL(ip_cmsg_recv);

EXPORT_SYMBOL(ip_getsockopt);
EXPORT_SYMBOL(ip_setsockopt);