ip_sockglue.c 32.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
 * Authors:	see ip.c
 *
 * Fixes:
 *		Many		:	Split from ip.c , see ip.c for history.
 *		Martin Mares	:	TOS setting fixed.
13
 *		Alan Cox	:	Fixed a couple of oopses in Martin's
L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23
 *					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>
24
#include <linux/inetdevice.h>
L
Linus Torvalds 已提交
25
#include <linux/netdevice.h>
26
#include <linux/slab.h>
L
Linus Torvalds 已提交
27 28 29
#include <net/sock.h>
#include <net/ip.h>
#include <net/icmp.h>
30
#include <net/tcp_states.h>
L
Linus Torvalds 已提交
31 32 33 34 35
#include <linux/udp.h>
#include <linux/igmp.h>
#include <linux/netfilter.h>
#include <linux/route.h>
#include <linux/mroute.h>
36
#include <net/inet_ecn.h>
L
Linus Torvalds 已提交
37 38
#include <net/route.h>
#include <net/xfrm.h>
39
#include <net/compat.h>
E
Eric Dumazet 已提交
40
#if IS_ENABLED(CONFIG_IPV6)
L
Linus Torvalds 已提交
41 42
#include <net/transp_v6.h>
#endif
43
#include <net/ip_fib.h>
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52

#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 已提交
53
#define IP_CMSG_PASSSEC		32
54
#define IP_CMSG_ORIGDSTADDR     64
L
Linus Torvalds 已提交
55 56 57 58 59 60 61

/*
 *	SOL_IP control messages.
 */

static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
{
62
	struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
L
Linus Torvalds 已提交
63

64
	info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
L
Linus Torvalds 已提交
65 66 67 68 69 70

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

static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
{
71
	int ttl = ip_hdr(skb)->ttl;
L
Linus Torvalds 已提交
72 73 74 75 76
	put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
}

static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
{
77
	put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
L
Linus Torvalds 已提交
78 79 80 81 82 83 84
}

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

85 86
	put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
		 ip_hdr(skb) + 1);
L
Linus Torvalds 已提交
87 88 89 90 91 92
}


static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
{
	unsigned char optbuf[sizeof(struct ip_options) + 40];
D
Daniel Baluta 已提交
93
	struct ip_options *opt = (struct ip_options *)optbuf;
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106

	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 已提交
107 108 109
static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
{
	char *secdata;
110
	u32 seclen, secid;
C
Catherine Zhang 已提交
111 112
	int err;

113 114 115 116 117
	err = security_socket_getpeersec_dgram(NULL, skb, &secid);
	if (err)
		return;

	err = security_secid_to_secctx(secid, &secdata, &seclen);
C
Catherine Zhang 已提交
118 119 120 121
	if (err)
		return;

	put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
122
	security_release_secctx(secdata, seclen);
C
Catherine Zhang 已提交
123 124
}

125
static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
126 127
{
	struct sockaddr_in sin;
128
	const struct iphdr *iph = ip_hdr(skb);
129
	__be16 *ports = (__be16 *)skb_transport_header(skb);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

	if (skb_transport_offset(skb) + 4 > skb->len)
		return;

	/* All current transport protocols have the port numbers in the
	 * first four bytes of the transport header and this function is
	 * written with this assumption in mind.
	 */

	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = iph->daddr;
	sin.sin_port = ports[1];
	memset(sin.sin_zero, 0, sizeof(sin.sin_zero));

	put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
}
L
Linus Torvalds 已提交
146 147 148 149

void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
{
	struct inet_sock *inet = inet_sk(skb->sk);
150
	unsigned int flags = inet->cmsg_flags;
L
Linus Torvalds 已提交
151 152 153 154

	/* Ordered by supposed usage frequency */
	if (flags & 1)
		ip_cmsg_recv_pktinfo(msg, skb);
E
Eric Dumazet 已提交
155
	if ((flags >>= 1) == 0)
L
Linus Torvalds 已提交
156 157 158 159
		return;

	if (flags & 1)
		ip_cmsg_recv_ttl(msg, skb);
E
Eric Dumazet 已提交
160
	if ((flags >>= 1) == 0)
L
Linus Torvalds 已提交
161 162 163 164
		return;

	if (flags & 1)
		ip_cmsg_recv_tos(msg, skb);
E
Eric Dumazet 已提交
165
	if ((flags >>= 1) == 0)
L
Linus Torvalds 已提交
166 167 168 169
		return;

	if (flags & 1)
		ip_cmsg_recv_opts(msg, skb);
E
Eric Dumazet 已提交
170
	if ((flags >>= 1) == 0)
L
Linus Torvalds 已提交
171 172 173 174
		return;

	if (flags & 1)
		ip_cmsg_recv_retopts(msg, skb);
E
Eric Dumazet 已提交
175
	if ((flags >>= 1) == 0)
C
Catherine Zhang 已提交
176 177 178 179
		return;

	if (flags & 1)
		ip_cmsg_recv_security(msg, skb);
180

E
Eric Dumazet 已提交
181
	if ((flags >>= 1) == 0)
182 183 184 185
		return;
	if (flags & 1)
		ip_cmsg_recv_dstaddr(msg, skb);

L
Linus Torvalds 已提交
186
}
E
Eric Dumazet 已提交
187
EXPORT_SYMBOL(ip_cmsg_recv);
L
Linus Torvalds 已提交
188

189 190
int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc,
		 bool allow_ipv6)
L
Linus Torvalds 已提交
191
{
192
	int err, val;
L
Linus Torvalds 已提交
193 194 195 196 197
	struct cmsghdr *cmsg;

	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
		if (!CMSG_OK(msg, cmsg))
			return -EINVAL;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#if defined(CONFIG_IPV6)
		if (allow_ipv6 &&
		    cmsg->cmsg_level == SOL_IPV6 &&
		    cmsg->cmsg_type == IPV6_PKTINFO) {
			struct in6_pktinfo *src_info;

			if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
				return -EINVAL;
			src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
			if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
				return -EINVAL;
			ipc->oif = src_info->ipi6_ifindex;
			ipc->addr = src_info->ipi6_addr.s6_addr32[3];
			continue;
		}
#endif
L
Linus Torvalds 已提交
214 215 216 217 218
		if (cmsg->cmsg_level != SOL_IP)
			continue;
		switch (cmsg->cmsg_type) {
		case IP_RETOPTS:
			err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
E
Eric Dumazet 已提交
219 220
			err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
					     err < 40 ? err : 40);
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233
			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;
		}
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		case IP_TTL:
			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
				return -EINVAL;
			val = *(int *)CMSG_DATA(cmsg);
			if (val < 1 || val > 255)
				return -EINVAL;
			ipc->ttl = val;
			break;
		case IP_TOS:
			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
				return -EINVAL;
			val = *(int *)CMSG_DATA(cmsg);
			if (val < 0 || val > 255)
				return -EINVAL;
			ipc->tos = val;
			ipc->priority = rt_tos2priority(ipc->tos);
			break;

L
Linus Torvalds 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
		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.
 */
270
struct ip_ra_chain __rcu *ip_ra_chain;
E
Eric Dumazet 已提交
271 272
static DEFINE_SPINLOCK(ip_ra_lock);

E
Eric Dumazet 已提交
273 274

static void ip_ra_destroy_rcu(struct rcu_head *head)
E
Eric Dumazet 已提交
275
{
E
Eric Dumazet 已提交
276 277 278 279
	struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);

	sock_put(ra->saved_sk);
	kfree(ra);
E
Eric Dumazet 已提交
280
}
L
Linus Torvalds 已提交
281

E
Eric Dumazet 已提交
282 283
int ip_ra_control(struct sock *sk, unsigned char on,
		  void (*destructor)(struct sock *))
L
Linus Torvalds 已提交
284
{
285 286
	struct ip_ra_chain *ra, *new_ra;
	struct ip_ra_chain __rcu **rap;
L
Linus Torvalds 已提交
287

E
Eric Dumazet 已提交
288
	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
L
Linus Torvalds 已提交
289 290 291 292
		return -EINVAL;

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

E
Eric Dumazet 已提交
293
	spin_lock_bh(&ip_ra_lock);
294 295 296 297
	for (rap = &ip_ra_chain;
	     (ra = rcu_dereference_protected(*rap,
			lockdep_is_held(&ip_ra_lock))) != NULL;
	     rap = &ra->next) {
L
Linus Torvalds 已提交
298 299
		if (ra->sk == sk) {
			if (on) {
E
Eric Dumazet 已提交
300
				spin_unlock_bh(&ip_ra_lock);
J
Jesper Juhl 已提交
301
				kfree(new_ra);
L
Linus Torvalds 已提交
302 303
				return -EADDRINUSE;
			}
E
Eric Dumazet 已提交
304 305
			/* dont let ip_call_ra_chain() use sk again */
			ra->sk = NULL;
306
			RCU_INIT_POINTER(*rap, ra->next);
E
Eric Dumazet 已提交
307
			spin_unlock_bh(&ip_ra_lock);
L
Linus Torvalds 已提交
308 309 310

			if (ra->destructor)
				ra->destructor(sk);
E
Eric Dumazet 已提交
311 312 313 314 315 316 317
			/*
			 * Delay sock_put(sk) and kfree(ra) after one rcu grace
			 * period. This guarantee ip_call_ra_chain() dont need
			 * to mess with socket refcounts.
			 */
			ra->saved_sk = sk;
			call_rcu(&ra->rcu, ip_ra_destroy_rcu);
L
Linus Torvalds 已提交
318 319 320 321
			return 0;
		}
	}
	if (new_ra == NULL) {
E
Eric Dumazet 已提交
322
		spin_unlock_bh(&ip_ra_lock);
L
Linus Torvalds 已提交
323 324 325 326 327
		return -ENOBUFS;
	}
	new_ra->sk = sk;
	new_ra->destructor = destructor;

328
	RCU_INIT_POINTER(new_ra->next, ra);
E
Eric Dumazet 已提交
329
	rcu_assign_pointer(*rap, new_ra);
L
Linus Torvalds 已提交
330
	sock_hold(sk);
E
Eric Dumazet 已提交
331
	spin_unlock_bh(&ip_ra_lock);
L
Linus Torvalds 已提交
332 333 334 335

	return 0;
}

336
void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
A
Al Viro 已提交
337
		   __be16 port, u32 info, u8 *payload)
L
Linus Torvalds 已提交
338 339 340 341 342 343 344
{
	struct sock_exterr_skb *serr;

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

345
	serr = SKB_EXT_ERR(skb);
L
Linus Torvalds 已提交
346 347
	serr->ee.ee_errno = err;
	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
348 349
	serr->ee.ee_type = icmp_hdr(skb)->type;
	serr->ee.ee_code = icmp_hdr(skb)->code;
L
Linus Torvalds 已提交
350 351 352
	serr->ee.ee_pad = 0;
	serr->ee.ee_info = info;
	serr->ee.ee_data = 0;
353
	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
354
				   skb_network_header(skb);
L
Linus Torvalds 已提交
355 356
	serr->port = port;

357 358 359 360 361 362
	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 已提交
363 364
}

A
Al Viro 已提交
365
void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
L
Linus Torvalds 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378
{
	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;

379 380
	skb_put(skb, sizeof(struct iphdr));
	skb_reset_network_header(skb);
381
	iph = ip_hdr(skb);
L
Linus Torvalds 已提交
382 383
	iph->daddr = daddr;

384
	serr = SKB_EXT_ERR(skb);
L
Linus Torvalds 已提交
385 386
	serr->ee.ee_errno = err;
	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
387
	serr->ee.ee_type = 0;
L
Linus Torvalds 已提交
388 389 390 391
	serr->ee.ee_code = 0;
	serr->ee.ee_pad = 0;
	serr->ee.ee_info = info;
	serr->ee.ee_data = 0;
392
	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
L
Linus Torvalds 已提交
393 394
	serr->port = port;

395
	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
396
	skb_reset_transport_header(skb);
L
Linus Torvalds 已提交
397 398 399 400 401

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

402
/*
L
Linus Torvalds 已提交
403 404
 *	Handle MSG_ERRQUEUE
 */
405
int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
L
Linus Torvalds 已提交
406 407
{
	struct sock_exterr_skb *serr;
408
	struct sk_buff *skb;
409
	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417
	struct {
		struct sock_extended_err ee;
		struct sockaddr_in	 offender;
	} errhdr;
	int err;
	int copied;

	err = -EAGAIN;
418
	skb = sock_dequeue_err_skb(sk);
L
Linus Torvalds 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	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);

	if (sin) {
		sin->sin_family = AF_INET;
437 438
		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
						   serr->addr_offset);
L
Linus Torvalds 已提交
439 440
		sin->sin_port = serr->port;
		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
441
		*addr_len = sizeof(*sin);
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449 450
	}

	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;
451
		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464
		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;

465
out_free_skb:
L
Linus Torvalds 已提交
466 467 468 469 470 471 472
	kfree_skb(skb);
out:
	return err;
}


/*
E
Eric Dumazet 已提交
473 474
 *	Socket option code for IP. This is the end of the line after any
 *	TCP,UDP etc options on an IP socket.
L
Linus Torvalds 已提交
475 476
 */

477
static int do_ip_setsockopt(struct sock *sk, int level,
478
			    int optname, char __user *optval, unsigned int optlen)
L
Linus Torvalds 已提交
479 480
{
	struct inet_sock *inet = inet_sk(sk);
481
	int val = 0, err;
L
Linus Torvalds 已提交
482

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
	switch (optname) {
	case IP_PKTINFO:
	case IP_RECVTTL:
	case IP_RECVOPTS:
	case IP_RECVTOS:
	case IP_RETOPTS:
	case IP_TOS:
	case IP_TTL:
	case IP_HDRINCL:
	case IP_MTU_DISCOVER:
	case IP_RECVERR:
	case IP_ROUTER_ALERT:
	case IP_FREEBIND:
	case IP_PASSSEC:
	case IP_TRANSPARENT:
	case IP_MINTTL:
	case IP_NODEFRAG:
	case IP_UNICAST_IF:
	case IP_MULTICAST_TTL:
	case IP_MULTICAST_ALL:
	case IP_MULTICAST_LOOP:
	case IP_RECVORIGDSTADDR:
L
Linus Torvalds 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518
		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 */

519
	if (ip_mroute_opt(optname))
520
		return ip_mroute_setsockopt(sk, optname, optval, optlen);
L
Linus Torvalds 已提交
521 522 523 524 525

	err = 0;
	lock_sock(sk);

	switch (optname) {
S
Stephen Hemminger 已提交
526 527
	case IP_OPTIONS:
	{
528 529
		struct ip_options_rcu *old, *opt = NULL;

530
		if (optlen > 40)
S
Stephen Hemminger 已提交
531
			goto e_inval;
532
		err = ip_options_get_from_user(sock_net(sk), &opt,
533
					       optval, optlen);
S
Stephen Hemminger 已提交
534 535
		if (err)
			break;
536 537
		old = rcu_dereference_protected(inet->inet_opt,
						sock_owned_by_user(sk));
S
Stephen Hemminger 已提交
538 539
		if (inet->is_icsk) {
			struct inet_connection_sock *icsk = inet_csk(sk);
E
Eric Dumazet 已提交
540
#if IS_ENABLED(CONFIG_IPV6)
S
Stephen Hemminger 已提交
541 542 543
			if (sk->sk_family == PF_INET ||
			    (!((1 << sk->sk_state) &
			       (TCPF_LISTEN | TCPF_CLOSE)) &&
E
Eric Dumazet 已提交
544
			     inet->inet_daddr != LOOPBACK4_IPV6)) {
L
Linus Torvalds 已提交
545
#endif
546 547
				if (old)
					icsk->icsk_ext_hdr_len -= old->opt.optlen;
S
Stephen Hemminger 已提交
548
				if (opt)
549
					icsk->icsk_ext_hdr_len += opt->opt.optlen;
S
Stephen Hemminger 已提交
550
				icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
E
Eric Dumazet 已提交
551
#if IS_ENABLED(CONFIG_IPV6)
L
Linus Torvalds 已提交
552
			}
S
Stephen Hemminger 已提交
553
#endif
L
Linus Torvalds 已提交
554
		}
555 556
		rcu_assign_pointer(inet->inet_opt, opt);
		if (old)
557
			kfree_rcu(old, rcu);
S
Stephen Hemminger 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
		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;
596 597 598 599 600 601
	case IP_RECVORIGDSTADDR:
		if (val)
			inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
		else
			inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
		break;
S
Stephen Hemminger 已提交
602 603
	case IP_TOS:	/* This sets both TOS and Precedence */
		if (sk->sk_type == SOCK_STREAM) {
604 605
			val &= ~INET_ECN_MASK;
			val |= inet->tos & INET_ECN_MASK;
S
Stephen Hemminger 已提交
606 607 608 609 610 611 612 613
		}
		if (inet->tos != val) {
			inet->tos = val;
			sk->sk_priority = rt_tos2priority(val);
			sk_dst_reset(sk);
		}
		break;
	case IP_TTL:
E
Eric Dumazet 已提交
614
		if (optlen < 1)
S
Stephen Hemminger 已提交
615
			goto e_inval;
616
		if (val != -1 && (val < 1 || val > 255))
S
Stephen Hemminger 已提交
617 618 619 620 621 622
			goto e_inval;
		inet->uc_ttl = val;
		break;
	case IP_HDRINCL:
		if (sk->sk_type != SOCK_RAW) {
			err = -ENOPROTOOPT;
C
Catherine Zhang 已提交
623
			break;
S
Stephen Hemminger 已提交
624 625 626
		}
		inet->hdrincl = val ? 1 : 0;
		break;
627 628 629 630 631 632 633
	case IP_NODEFRAG:
		if (sk->sk_type != SOCK_RAW) {
			err = -ENOPROTOOPT;
			break;
		}
		inet->nodefrag = val ? 1 : 0;
		break;
S
Stephen Hemminger 已提交
634
	case IP_MTU_DISCOVER:
635
		if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
S
Stephen Hemminger 已提交
636 637 638 639 640 641 642 643 644 645 646
			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;
E
Eric Dumazet 已提交
647
		if (optlen < 1)
S
Stephen Hemminger 已提交
648
			goto e_inval;
649
		if (val == -1)
S
Stephen Hemminger 已提交
650 651 652 653 654 655
			val = 1;
		if (val < 0 || val > 255)
			goto e_inval;
		inet->mc_ttl = val;
		break;
	case IP_MULTICAST_LOOP:
E
Eric Dumazet 已提交
656
		if (optlen < 1)
S
Stephen Hemminger 已提交
657 658 659
			goto e_inval;
		inet->mc_loop = !!val;
		break;
660 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
	case IP_UNICAST_IF:
	{
		struct net_device *dev = NULL;
		int ifindex;

		if (optlen != sizeof(int))
			goto e_inval;

		ifindex = (__force int)ntohl((__force __be32)val);
		if (ifindex == 0) {
			inet->uc_index = 0;
			err = 0;
			break;
		}

		dev = dev_get_by_index(sock_net(sk), ifindex);
		err = -EADDRNOTAVAIL;
		if (!dev)
			break;
		dev_put(dev);

		err = -EINVAL;
		if (sk->sk_bound_dev_if)
			break;

		inet->uc_index = ifindex;
		err = 0;
		break;
	}
S
Stephen Hemminger 已提交
689 690 691 692 693 694 695 696 697 698 699
	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
		 */

700 701 702
		if (optlen < sizeof(struct in_addr))
			goto e_inval;

S
Stephen Hemminger 已提交
703 704
		err = -EFAULT;
		if (optlen >= sizeof(struct ip_mreqn)) {
705
			if (copy_from_user(&mreq, optval, sizeof(mreq)))
L
Linus Torvalds 已提交
706
				break;
S
Stephen Hemminger 已提交
707 708
		} else {
			memset(&mreq, 0, sizeof(mreq));
709 710 711 712 713 714 715 716 717
			if (optlen >= sizeof(struct ip_mreq)) {
				if (copy_from_user(&mreq, optval,
						   sizeof(struct ip_mreq)))
					break;
			} else if (optlen >= sizeof(struct in_addr)) {
				if (copy_from_user(&mreq.imr_address, optval,
						   sizeof(struct in_addr)))
					break;
			}
S
Stephen Hemminger 已提交
718 719 720
		}

		if (!mreq.imr_ifindex) {
A
Al Viro 已提交
721
			if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
S
Stephen Hemminger 已提交
722 723 724
				inet->mc_index = 0;
				inet->mc_addr  = 0;
				err = 0;
L
Linus Torvalds 已提交
725 726
				break;
			}
727
			dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
E
Eric Dumazet 已提交
728
			if (dev)
S
Stephen Hemminger 已提交
729 730
				mreq.imr_ifindex = dev->ifindex;
		} else
E
Eric Dumazet 已提交
731
			dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
L
Linus Torvalds 已提交
732 733


S
Stephen Hemminger 已提交
734 735 736
		err = -EADDRNOTAVAIL;
		if (!dev)
			break;
E
Eric Dumazet 已提交
737
		dev_put(dev);
S
Stephen Hemminger 已提交
738 739 740 741 742

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

S
Stephen Hemminger 已提交
744 745 746 747 748
		inet->mc_index = mreq.imr_ifindex;
		inet->mc_addr  = mreq.imr_address.s_addr;
		err = 0;
		break;
	}
L
Linus Torvalds 已提交
749

S
Stephen Hemminger 已提交
750 751 752 753
	case IP_ADD_MEMBERSHIP:
	case IP_DROP_MEMBERSHIP:
	{
		struct ip_mreqn mreq;
L
Linus Torvalds 已提交
754

755 756 757 758
		err = -EPROTO;
		if (inet_sk(sk)->is_icsk)
			break;

S
Stephen Hemminger 已提交
759 760 761 762
		if (optlen < sizeof(struct ip_mreq))
			goto e_inval;
		err = -EFAULT;
		if (optlen >= sizeof(struct ip_mreqn)) {
763
			if (copy_from_user(&mreq, optval, sizeof(mreq)))
L
Linus Torvalds 已提交
764
				break;
S
Stephen Hemminger 已提交
765 766
		} else {
			memset(&mreq, 0, sizeof(mreq));
767
			if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
L
Linus Torvalds 已提交
768
				break;
S
Stephen Hemminger 已提交
769
		}
L
Linus Torvalds 已提交
770

S
Stephen Hemminger 已提交
771 772 773 774 775 776 777 778 779 780 781 782 783 784
		if (optname == IP_ADD_MEMBERSHIP)
			err = ip_mc_join_group(sk, &mreq);
		else
			err = ip_mc_leave_group(sk, &mreq);
		break;
	}
	case IP_MSFILTER:
	{
		struct ip_msfilter *msf;

		if (optlen < IP_MSFILTER_SIZE(0))
			goto e_inval;
		if (optlen > sysctl_optmem_max) {
			err = -ENOBUFS;
L
Linus Torvalds 已提交
785 786
			break;
		}
S
Stephen Hemminger 已提交
787
		msf = kmalloc(optlen, GFP_KERNEL);
S
Stephen Hemminger 已提交
788
		if (!msf) {
S
Stephen Hemminger 已提交
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 814 815 816 817 818 819
			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 已提交
820

S
Stephen Hemminger 已提交
821 822 823
		if (optlen != sizeof(struct ip_mreq_source))
			goto e_inval;
		if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
L
Linus Torvalds 已提交
824 825 826
			err = -EFAULT;
			break;
		}
S
Stephen Hemminger 已提交
827 828 829 830 831 832 833 834
		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 已提交
835

S
Stephen Hemminger 已提交
836 837 838 839 840
			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 已提交
841
				break;
S
Stephen Hemminger 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 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 882 883 884 885 886 887 888
			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 已提交
889 890 891
			err = -EFAULT;
			break;
		}
S
Stephen Hemminger 已提交
892 893 894
		if (greqs.gsr_group.ss_family != AF_INET ||
		    greqs.gsr_source.ss_family != AF_INET) {
			err = -EADDRNOTAVAIL;
L
Linus Torvalds 已提交
895 896
			break;
		}
S
Stephen Hemminger 已提交
897 898 899 900 901 902 903 904 905 906 907 908 909
		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 已提交
910 911
			struct ip_mreqn mreq;

S
Stephen Hemminger 已提交
912
			psin = (struct sockaddr_in *)&greqs.gsr_group;
L
Linus Torvalds 已提交
913
			mreq.imr_multiaddr = psin->sin_addr;
S
Stephen Hemminger 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
			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:
	{
		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 已提交
941 942
			break;
		}
943
		gsf = kmalloc(optlen, GFP_KERNEL);
S
Stephen Hemminger 已提交
944
		if (!gsf) {
S
Stephen Hemminger 已提交
945
			err = -ENOBUFS;
L
Linus Torvalds 已提交
946 947
			break;
		}
S
Stephen Hemminger 已提交
948
		err = -EFAULT;
E
Eric Dumazet 已提交
949
		if (copy_from_user(gsf, optval, optlen))
S
Stephen Hemminger 已提交
950
			goto mc_msf_out;
E
Eric Dumazet 已提交
951

S
Stephen Hemminger 已提交
952 953 954 955 956 957 958 959 960 961 962
		/* 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);
963
		msf = kmalloc(msize, GFP_KERNEL);
S
Stephen Hemminger 已提交
964
		if (!msf) {
S
Stephen Hemminger 已提交
965 966 967 968 969 970
			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 已提交
971
			err = -EADDRNOTAVAIL;
S
Stephen Hemminger 已提交
972
			goto mc_msf_out;
L
Linus Torvalds 已提交
973
		}
S
Stephen Hemminger 已提交
974 975 976 977 978
		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;
E
Eric Dumazet 已提交
979
		for (i = 0; i < gsf->gf_numsrc; ++i) {
S
Stephen Hemminger 已提交
980
			psin = (struct sockaddr_in *)&gsf->gf_slist[i];
981

S
Stephen Hemminger 已提交
982 983 984 985 986 987 988 989
			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);
E
Eric Dumazet 已提交
990
mc_msf_out:
S
Stephen Hemminger 已提交
991 992 993 994
		kfree(msf);
		kfree(gsf);
		break;
	}
995 996 997 998 999 1000 1001
	case IP_MULTICAST_ALL:
		if (optlen < 1)
			goto e_inval;
		if (val != 0 && val != 1)
			goto e_inval;
		inet->mc_all = val;
		break;
S
Stephen Hemminger 已提交
1002 1003 1004 1005 1006
	case IP_ROUTER_ALERT:
		err = ip_ra_control(sk, val ? 1 : 0, NULL);
		break;

	case IP_FREEBIND:
E
Eric Dumazet 已提交
1007
		if (optlen < 1)
S
Stephen Hemminger 已提交
1008 1009 1010 1011 1012 1013 1014
			goto e_inval;
		inet->freebind = !!val;
		break;

	case IP_IPSEC_POLICY:
	case IP_XFRM_POLICY:
		err = -EPERM;
1015
		if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
L
Linus Torvalds 已提交
1016
			break;
S
Stephen Hemminger 已提交
1017 1018
		err = xfrm_user_policy(sk, optname, optval, optlen);
		break;
L
Linus Torvalds 已提交
1019

1020
	case IP_TRANSPARENT:
1021 1022
		if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
		    !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1023 1024 1025 1026 1027 1028 1029 1030
			err = -EPERM;
			break;
		}
		if (optlen < 1)
			goto e_inval;
		inet->transparent = !!val;
		break;

1031 1032 1033 1034 1035 1036 1037 1038
	case IP_MINTTL:
		if (optlen < 1)
			goto e_inval;
		if (val < 0 || val > 255)
			goto e_inval;
		inet->min_ttl = val;
		break;

S
Stephen Hemminger 已提交
1039 1040 1041
	default:
		err = -ENOPROTOOPT;
		break;
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050
	}
	release_sock(sk);
	return err;

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

E
Eric Dumazet 已提交
1051
/**
1052
 * ipv4_pktinfo_prepare - transfert some info from rtable to skb
E
Eric Dumazet 已提交
1053 1054 1055
 * @sk: socket
 * @skb: buffer
 *
1056 1057
 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
 * destination in skb->cb[] before dst drop.
S
stephen hemminger 已提交
1058
 * This way, receiver doesn't make cache line misses to read rtable.
E
Eric Dumazet 已提交
1059
 */
1060
void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
E
Eric Dumazet 已提交
1061
{
1062
	struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1063 1064
	bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
		       ipv6_sk_rxinfo(sk);
1065

1066
	if (prepare && skb_rtable(skb)) {
1067
		pktinfo->ipi_ifindex = inet_iif(skb);
1068
		pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1069 1070 1071 1072 1073
	} else {
		pktinfo->ipi_ifindex = 0;
		pktinfo->ipi_spec_dst.s_addr = 0;
	}
	skb_dst_drop(skb);
E
Eric Dumazet 已提交
1074 1075
}

1076
int ip_setsockopt(struct sock *sk, int level,
1077
		int optname, char __user *optval, unsigned int optlen)
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
{
	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 &&
1088 1089 1090
			optname != IP_IPSEC_POLICY &&
			optname != IP_XFRM_POLICY &&
			!ip_mroute_opt(optname)) {
1091 1092 1093 1094 1095 1096 1097
		lock_sock(sk);
		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
		release_sock(sk);
	}
#endif
	return err;
}
E
Eric Dumazet 已提交
1098
EXPORT_SYMBOL(ip_setsockopt);
1099 1100

#ifdef CONFIG_COMPAT
1101
int compat_ip_setsockopt(struct sock *sk, int level, int optname,
1102
			 char __user *optval, unsigned int optlen)
1103 1104 1105 1106 1107 1108
{
	int err;

	if (level != SOL_IP)
		return -ENOPROTOOPT;

1109 1110 1111 1112
	if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
		return compat_mc_setsockopt(sk, level, optname, optval, optlen,
			ip_setsockopt);

1113 1114 1115 1116
	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 &&
1117 1118 1119
			optname != IP_IPSEC_POLICY &&
			optname != IP_XFRM_POLICY &&
			!ip_mroute_opt(optname)) {
1120
		lock_sock(sk);
1121 1122
		err = compat_nf_setsockopt(sk, PF_INET, optname,
					   optval, optlen);
1123 1124 1125 1126 1127
		release_sock(sk);
	}
#endif
	return err;
}
1128
EXPORT_SYMBOL(compat_ip_setsockopt);
1129 1130
#endif

L
Linus Torvalds 已提交
1131
/*
E
Eric Dumazet 已提交
1132 1133
 *	Get the options. Note for future reference. The GET of IP options gets
 *	the _received_ ones. The set sets the _sent_ ones.
L
Linus Torvalds 已提交
1134 1135
 */

1136
static int do_ip_getsockopt(struct sock *sk, int level, int optname,
1137
			    char __user *optval, int __user *optlen, unsigned int flags)
L
Linus Torvalds 已提交
1138 1139 1140 1141
{
	struct inet_sock *inet = inet_sk(sk);
	int val;
	int len;
1142

S
Stephen Hemminger 已提交
1143
	if (level != SOL_IP)
L
Linus Torvalds 已提交
1144 1145
		return -EOPNOTSUPP;

1146
	if (ip_mroute_opt(optname))
1147
		return ip_mroute_getsockopt(sk, optname, optval, optlen);
L
Linus Torvalds 已提交
1148

1149
	if (get_user(len, optlen))
L
Linus Torvalds 已提交
1150
		return -EFAULT;
S
Stephen Hemminger 已提交
1151
	if (len < 0)
L
Linus Torvalds 已提交
1152
		return -EINVAL;
1153

L
Linus Torvalds 已提交
1154 1155
	lock_sock(sk);

S
Stephen Hemminger 已提交
1156 1157 1158 1159
	switch (optname) {
	case IP_OPTIONS:
	{
		unsigned char optbuf[sizeof(struct ip_options)+40];
1160 1161 1162 1163 1164
		struct ip_options *opt = (struct ip_options *)optbuf;
		struct ip_options_rcu *inet_opt;

		inet_opt = rcu_dereference_protected(inet->inet_opt,
						     sock_owned_by_user(sk));
S
Stephen Hemminger 已提交
1165
		opt->optlen = 0;
1166 1167 1168 1169
		if (inet_opt)
			memcpy(optbuf, &inet_opt->opt,
			       sizeof(struct ip_options) +
			       inet_opt->opt.optlen);
S
Stephen Hemminger 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
		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;
1202 1203 1204
	case IP_RECVORIGDSTADDR:
		val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
		break;
S
Stephen Hemminger 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
	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;
1216 1217 1218
	case IP_NODEFRAG:
		val = inet->nodefrag;
		break;
S
Stephen Hemminger 已提交
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
	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 已提交
1230
		}
S
Stephen Hemminger 已提交
1231
		if (!val) {
L
Linus Torvalds 已提交
1232
			release_sock(sk);
S
Stephen Hemminger 已提交
1233
			return -ENOTCONN;
L
Linus Torvalds 已提交
1234
		}
S
Stephen Hemminger 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
		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;
1246 1247 1248
	case IP_UNICAST_IF:
		val = (__force int)htonl((__u32) inet->uc_index);
		break;
S
Stephen Hemminger 已提交
1249 1250 1251 1252 1253 1254
	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 已提交
1255

S
Stephen Hemminger 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
		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 已提交
1268
			release_sock(sk);
S
Stephen Hemminger 已提交
1269
			return -EINVAL;
L
Linus Torvalds 已提交
1270
		}
S
Stephen Hemminger 已提交
1271
		if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
L
Linus Torvalds 已提交
1272
			release_sock(sk);
S
Stephen Hemminger 已提交
1273
			return -EFAULT;
L
Linus Torvalds 已提交
1274
		}
S
Stephen Hemminger 已提交
1275 1276 1277 1278 1279 1280 1281 1282 1283
		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 已提交
1284

S
Stephen Hemminger 已提交
1285
		if (len < GROUP_FILTER_SIZE(0)) {
L
Linus Torvalds 已提交
1286
			release_sock(sk);
S
Stephen Hemminger 已提交
1287 1288 1289 1290 1291 1292 1293
			return -EINVAL;
		}
		if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
			release_sock(sk);
			return -EFAULT;
		}
		err = ip_mc_gsfget(sk, &gsf,
E
Eric Dumazet 已提交
1294 1295
				   (struct group_filter __user *)optval,
				   optlen);
S
Stephen Hemminger 已提交
1296 1297 1298
		release_sock(sk);
		return err;
	}
1299 1300 1301
	case IP_MULTICAST_ALL:
		val = inet->mc_all;
		break;
S
Stephen Hemminger 已提交
1302 1303 1304
	case IP_PKTOPTIONS:
	{
		struct msghdr msg;
L
Linus Torvalds 已提交
1305

S
Stephen Hemminger 已提交
1306
		release_sock(sk);
L
Linus Torvalds 已提交
1307

S
Stephen Hemminger 已提交
1308 1309
		if (sk->sk_type != SOCK_STREAM)
			return -ENOPROTOOPT;
L
Linus Torvalds 已提交
1310

1311
		msg.msg_control = (__force void *) optval;
S
Stephen Hemminger 已提交
1312
		msg.msg_controllen = len;
1313
		msg.msg_flags = flags;
L
Linus Torvalds 已提交
1314

S
Stephen Hemminger 已提交
1315 1316 1317
		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
			struct in_pktinfo info;

E
Eric Dumazet 已提交
1318 1319
			info.ipi_addr.s_addr = inet->inet_rcv_saddr;
			info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
S
Stephen Hemminger 已提交
1320 1321
			info.ipi_ifindex = inet->mc_index;
			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
L
Linus Torvalds 已提交
1322
		}
S
Stephen Hemminger 已提交
1323 1324 1325 1326
		if (inet->cmsg_flags & IP_CMSG_TTL) {
			int hlim = inet->mc_ttl;
			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
		}
1327 1328 1329 1330
		if (inet->cmsg_flags & IP_CMSG_TOS) {
			int tos = inet->rcv_tos;
			put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
		}
S
Stephen Hemminger 已提交
1331 1332 1333 1334 1335 1336
		len -= msg.msg_controllen;
		return put_user(len, optlen);
	}
	case IP_FREEBIND:
		val = inet->freebind;
		break;
1337 1338 1339
	case IP_TRANSPARENT:
		val = inet->transparent;
		break;
1340 1341 1342
	case IP_MINTTL:
		val = inet->min_ttl;
		break;
S
Stephen Hemminger 已提交
1343 1344 1345
	default:
		release_sock(sk);
		return -ENOPROTOOPT;
L
Linus Torvalds 已提交
1346 1347
	}
	release_sock(sk);
1348

E
Eric Dumazet 已提交
1349
	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
L
Linus Torvalds 已提交
1350 1351
		unsigned char ucval = (unsigned char)val;
		len = 1;
S
Stephen Hemminger 已提交
1352
		if (put_user(len, optlen))
L
Linus Torvalds 已提交
1353
			return -EFAULT;
1354
		if (copy_to_user(optval, &ucval, 1))
L
Linus Torvalds 已提交
1355 1356 1357
			return -EFAULT;
	} else {
		len = min_t(unsigned int, sizeof(int), len);
S
Stephen Hemminger 已提交
1358
		if (put_user(len, optlen))
L
Linus Torvalds 已提交
1359
			return -EFAULT;
1360
		if (copy_to_user(optval, &val, len))
L
Linus Torvalds 已提交
1361 1362 1363 1364 1365
			return -EFAULT;
	}
	return 0;
}

1366
int ip_getsockopt(struct sock *sk, int level,
S
Stephen Hemminger 已提交
1367
		  int optname, char __user *optval, int __user *optlen)
1368 1369 1370
{
	int err;

1371
	err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
1372 1373
#ifdef CONFIG_NETFILTER
	/* we need to exclude all possible ENOPROTOOPTs except default case */
1374 1375
	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
			!ip_mroute_opt(optname)) {
1376
		int len;
1377

1378
		if (get_user(len, optlen))
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
			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;
}
E
Eric Dumazet 已提交
1392
EXPORT_SYMBOL(ip_getsockopt);
1393 1394

#ifdef CONFIG_COMPAT
1395 1396
int compat_ip_getsockopt(struct sock *sk, int level, int optname,
			 char __user *optval, int __user *optlen)
1397
{
1398 1399 1400 1401 1402 1403
	int err;

	if (optname == MCAST_MSFILTER)
		return compat_mc_getsockopt(sk, level, optname, optval, optlen,
			ip_getsockopt);

1404 1405
	err = do_ip_getsockopt(sk, level, optname, optval, optlen,
		MSG_CMSG_COMPAT);
1406

1407 1408
#ifdef CONFIG_NETFILTER
	/* we need to exclude all possible ENOPROTOOPTs except default case */
1409 1410
	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
			!ip_mroute_opt(optname)) {
1411
		int len;
1412

1413
		if (get_user(len, optlen))
1414 1415 1416
			return -EFAULT;

		lock_sock(sk);
1417
		err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1418 1419 1420 1421 1422 1423 1424 1425
		release_sock(sk);
		if (err >= 0)
			err = put_user(len, optlen);
		return err;
	}
#endif
	return err;
}
1426
EXPORT_SYMBOL(compat_ip_getsockopt);
1427
#endif