xref: /openbmc/linux/net/ipv4/ip_sockglue.c (revision c819e2cf)
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		The IP to API glue.
7  *
8  * Authors:	see ip.c
9  *
10  * Fixes:
11  *		Many		:	Split from ip.c , see ip.c for history.
12  *		Martin Mares	:	TOS setting fixed.
13  *		Alan Cox	:	Fixed a couple of oopses in Martin's
14  *					TOS tweaks.
15  *		Mike McLagan	:	Routing by source
16  */
17 
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/mm.h>
21 #include <linux/skbuff.h>
22 #include <linux/ip.h>
23 #include <linux/icmp.h>
24 #include <linux/inetdevice.h>
25 #include <linux/netdevice.h>
26 #include <linux/slab.h>
27 #include <net/sock.h>
28 #include <net/ip.h>
29 #include <net/icmp.h>
30 #include <net/tcp_states.h>
31 #include <linux/udp.h>
32 #include <linux/igmp.h>
33 #include <linux/netfilter.h>
34 #include <linux/route.h>
35 #include <linux/mroute.h>
36 #include <net/inet_ecn.h>
37 #include <net/route.h>
38 #include <net/xfrm.h>
39 #include <net/compat.h>
40 #if IS_ENABLED(CONFIG_IPV6)
41 #include <net/transp_v6.h>
42 #endif
43 #include <net/ip_fib.h>
44 
45 #include <linux/errqueue.h>
46 #include <asm/uaccess.h>
47 
48 #define IP_CMSG_PKTINFO		1
49 #define IP_CMSG_TTL		2
50 #define IP_CMSG_TOS		4
51 #define IP_CMSG_RECVOPTS	8
52 #define IP_CMSG_RETOPTS		16
53 #define IP_CMSG_PASSSEC		32
54 #define IP_CMSG_ORIGDSTADDR     64
55 
56 /*
57  *	SOL_IP control messages.
58  */
59 
60 static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
61 {
62 	struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
63 
64 	info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
65 
66 	put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
67 }
68 
69 static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
70 {
71 	int ttl = ip_hdr(skb)->ttl;
72 	put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
73 }
74 
75 static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
76 {
77 	put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
78 }
79 
80 static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
81 {
82 	if (IPCB(skb)->opt.optlen == 0)
83 		return;
84 
85 	put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
86 		 ip_hdr(skb) + 1);
87 }
88 
89 
90 static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
91 {
92 	unsigned char optbuf[sizeof(struct ip_options) + 40];
93 	struct ip_options *opt = (struct ip_options *)optbuf;
94 
95 	if (IPCB(skb)->opt.optlen == 0)
96 		return;
97 
98 	if (ip_options_echo(opt, skb)) {
99 		msg->msg_flags |= MSG_CTRUNC;
100 		return;
101 	}
102 	ip_options_undo(opt);
103 
104 	put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
105 }
106 
107 static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
108 {
109 	char *secdata;
110 	u32 seclen, secid;
111 	int err;
112 
113 	err = security_socket_getpeersec_dgram(NULL, skb, &secid);
114 	if (err)
115 		return;
116 
117 	err = security_secid_to_secctx(secid, &secdata, &seclen);
118 	if (err)
119 		return;
120 
121 	put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
122 	security_release_secctx(secdata, seclen);
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 	if (skb_transport_offset(skb) + 4 > skb->len)
132 		return;
133 
134 	/* All current transport protocols have the port numbers in the
135 	 * first four bytes of the transport header and this function is
136 	 * written with this assumption in mind.
137 	 */
138 
139 	sin.sin_family = AF_INET;
140 	sin.sin_addr.s_addr = iph->daddr;
141 	sin.sin_port = ports[1];
142 	memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
143 
144 	put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
145 }
146 
147 void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
148 {
149 	struct inet_sock *inet = inet_sk(skb->sk);
150 	unsigned int flags = inet->cmsg_flags;
151 
152 	/* Ordered by supposed usage frequency */
153 	if (flags & 1)
154 		ip_cmsg_recv_pktinfo(msg, skb);
155 	if ((flags >>= 1) == 0)
156 		return;
157 
158 	if (flags & 1)
159 		ip_cmsg_recv_ttl(msg, skb);
160 	if ((flags >>= 1) == 0)
161 		return;
162 
163 	if (flags & 1)
164 		ip_cmsg_recv_tos(msg, skb);
165 	if ((flags >>= 1) == 0)
166 		return;
167 
168 	if (flags & 1)
169 		ip_cmsg_recv_opts(msg, skb);
170 	if ((flags >>= 1) == 0)
171 		return;
172 
173 	if (flags & 1)
174 		ip_cmsg_recv_retopts(msg, skb);
175 	if ((flags >>= 1) == 0)
176 		return;
177 
178 	if (flags & 1)
179 		ip_cmsg_recv_security(msg, skb);
180 
181 	if ((flags >>= 1) == 0)
182 		return;
183 	if (flags & 1)
184 		ip_cmsg_recv_dstaddr(msg, skb);
185 
186 }
187 EXPORT_SYMBOL(ip_cmsg_recv);
188 
189 int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc,
190 		 bool allow_ipv6)
191 {
192 	int err, val;
193 	struct cmsghdr *cmsg;
194 
195 	for_each_cmsghdr(cmsg, msg) {
196 		if (!CMSG_OK(msg, cmsg))
197 			return -EINVAL;
198 #if IS_ENABLED(CONFIG_IPV6)
199 		if (allow_ipv6 &&
200 		    cmsg->cmsg_level == SOL_IPV6 &&
201 		    cmsg->cmsg_type == IPV6_PKTINFO) {
202 			struct in6_pktinfo *src_info;
203 
204 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
205 				return -EINVAL;
206 			src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
207 			if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
208 				return -EINVAL;
209 			ipc->oif = src_info->ipi6_ifindex;
210 			ipc->addr = src_info->ipi6_addr.s6_addr32[3];
211 			continue;
212 		}
213 #endif
214 		if (cmsg->cmsg_level != SOL_IP)
215 			continue;
216 		switch (cmsg->cmsg_type) {
217 		case IP_RETOPTS:
218 			err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
219 			err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
220 					     err < 40 ? err : 40);
221 			if (err)
222 				return err;
223 			break;
224 		case IP_PKTINFO:
225 		{
226 			struct in_pktinfo *info;
227 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
228 				return -EINVAL;
229 			info = (struct in_pktinfo *)CMSG_DATA(cmsg);
230 			ipc->oif = info->ipi_ifindex;
231 			ipc->addr = info->ipi_spec_dst.s_addr;
232 			break;
233 		}
234 		case IP_TTL:
235 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
236 				return -EINVAL;
237 			val = *(int *)CMSG_DATA(cmsg);
238 			if (val < 1 || val > 255)
239 				return -EINVAL;
240 			ipc->ttl = val;
241 			break;
242 		case IP_TOS:
243 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
244 				return -EINVAL;
245 			val = *(int *)CMSG_DATA(cmsg);
246 			if (val < 0 || val > 255)
247 				return -EINVAL;
248 			ipc->tos = val;
249 			ipc->priority = rt_tos2priority(ipc->tos);
250 			break;
251 
252 		default:
253 			return -EINVAL;
254 		}
255 	}
256 	return 0;
257 }
258 
259 
260 /* Special input handler for packets caught by router alert option.
261    They are selected only by protocol field, and then processed likely
262    local ones; but only if someone wants them! Otherwise, router
263    not running rsvpd will kill RSVP.
264 
265    It is user level problem, what it will make with them.
266    I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
267    but receiver should be enough clever f.e. to forward mtrace requests,
268    sent to multicast group to reach destination designated router.
269  */
270 struct ip_ra_chain __rcu *ip_ra_chain;
271 static DEFINE_SPINLOCK(ip_ra_lock);
272 
273 
274 static void ip_ra_destroy_rcu(struct rcu_head *head)
275 {
276 	struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
277 
278 	sock_put(ra->saved_sk);
279 	kfree(ra);
280 }
281 
282 int ip_ra_control(struct sock *sk, unsigned char on,
283 		  void (*destructor)(struct sock *))
284 {
285 	struct ip_ra_chain *ra, *new_ra;
286 	struct ip_ra_chain __rcu **rap;
287 
288 	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
289 		return -EINVAL;
290 
291 	new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
292 
293 	spin_lock_bh(&ip_ra_lock);
294 	for (rap = &ip_ra_chain;
295 	     (ra = rcu_dereference_protected(*rap,
296 			lockdep_is_held(&ip_ra_lock))) != NULL;
297 	     rap = &ra->next) {
298 		if (ra->sk == sk) {
299 			if (on) {
300 				spin_unlock_bh(&ip_ra_lock);
301 				kfree(new_ra);
302 				return -EADDRINUSE;
303 			}
304 			/* dont let ip_call_ra_chain() use sk again */
305 			ra->sk = NULL;
306 			RCU_INIT_POINTER(*rap, ra->next);
307 			spin_unlock_bh(&ip_ra_lock);
308 
309 			if (ra->destructor)
310 				ra->destructor(sk);
311 			/*
312 			 * Delay sock_put(sk) and kfree(ra) after one rcu grace
313 			 * period. This guarantee ip_call_ra_chain() dont need
314 			 * to mess with socket refcounts.
315 			 */
316 			ra->saved_sk = sk;
317 			call_rcu(&ra->rcu, ip_ra_destroy_rcu);
318 			return 0;
319 		}
320 	}
321 	if (new_ra == NULL) {
322 		spin_unlock_bh(&ip_ra_lock);
323 		return -ENOBUFS;
324 	}
325 	new_ra->sk = sk;
326 	new_ra->destructor = destructor;
327 
328 	RCU_INIT_POINTER(new_ra->next, ra);
329 	rcu_assign_pointer(*rap, new_ra);
330 	sock_hold(sk);
331 	spin_unlock_bh(&ip_ra_lock);
332 
333 	return 0;
334 }
335 
336 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
337 		   __be16 port, u32 info, u8 *payload)
338 {
339 	struct sock_exterr_skb *serr;
340 
341 	skb = skb_clone(skb, GFP_ATOMIC);
342 	if (!skb)
343 		return;
344 
345 	serr = SKB_EXT_ERR(skb);
346 	serr->ee.ee_errno = err;
347 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
348 	serr->ee.ee_type = icmp_hdr(skb)->type;
349 	serr->ee.ee_code = icmp_hdr(skb)->code;
350 	serr->ee.ee_pad = 0;
351 	serr->ee.ee_info = info;
352 	serr->ee.ee_data = 0;
353 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
354 				   skb_network_header(skb);
355 	serr->port = port;
356 
357 	if (skb_pull(skb, payload - skb->data) != NULL) {
358 		skb_reset_transport_header(skb);
359 		if (sock_queue_err_skb(sk, skb) == 0)
360 			return;
361 	}
362 	kfree_skb(skb);
363 }
364 
365 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
366 {
367 	struct inet_sock *inet = inet_sk(sk);
368 	struct sock_exterr_skb *serr;
369 	struct iphdr *iph;
370 	struct sk_buff *skb;
371 
372 	if (!inet->recverr)
373 		return;
374 
375 	skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
376 	if (!skb)
377 		return;
378 
379 	skb_put(skb, sizeof(struct iphdr));
380 	skb_reset_network_header(skb);
381 	iph = ip_hdr(skb);
382 	iph->daddr = daddr;
383 
384 	serr = SKB_EXT_ERR(skb);
385 	serr->ee.ee_errno = err;
386 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
387 	serr->ee.ee_type = 0;
388 	serr->ee.ee_code = 0;
389 	serr->ee.ee_pad = 0;
390 	serr->ee.ee_info = info;
391 	serr->ee.ee_data = 0;
392 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
393 	serr->port = port;
394 
395 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
396 	skb_reset_transport_header(skb);
397 
398 	if (sock_queue_err_skb(sk, skb))
399 		kfree_skb(skb);
400 }
401 
402 static bool ipv4_pktinfo_prepare_errqueue(const struct sock *sk,
403 					  const struct sk_buff *skb,
404 					  int ee_origin)
405 {
406 	struct in_pktinfo *info = PKTINFO_SKB_CB(skb);
407 
408 	if ((ee_origin != SO_EE_ORIGIN_TIMESTAMPING) ||
409 	    (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG)) ||
410 	    (!skb->dev))
411 		return false;
412 
413 	info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
414 	info->ipi_ifindex = skb->dev->ifindex;
415 	return true;
416 }
417 
418 /*
419  *	Handle MSG_ERRQUEUE
420  */
421 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
422 {
423 	struct sock_exterr_skb *serr;
424 	struct sk_buff *skb;
425 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
426 	struct {
427 		struct sock_extended_err ee;
428 		struct sockaddr_in	 offender;
429 	} errhdr;
430 	int err;
431 	int copied;
432 
433 	WARN_ON_ONCE(sk->sk_family == AF_INET6);
434 
435 	err = -EAGAIN;
436 	skb = sock_dequeue_err_skb(sk);
437 	if (skb == NULL)
438 		goto out;
439 
440 	copied = skb->len;
441 	if (copied > len) {
442 		msg->msg_flags |= MSG_TRUNC;
443 		copied = len;
444 	}
445 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
446 	if (err)
447 		goto out_free_skb;
448 
449 	sock_recv_timestamp(msg, sk, skb);
450 
451 	serr = SKB_EXT_ERR(skb);
452 
453 	if (sin) {
454 		sin->sin_family = AF_INET;
455 		sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
456 						   serr->addr_offset);
457 		sin->sin_port = serr->port;
458 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
459 		*addr_len = sizeof(*sin);
460 	}
461 
462 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
463 	sin = &errhdr.offender;
464 	memset(sin, 0, sizeof(*sin));
465 
466 	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
467 	    ipv4_pktinfo_prepare_errqueue(sk, skb, serr->ee.ee_origin)) {
468 		sin->sin_family = AF_INET;
469 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
470 		if (inet_sk(sk)->cmsg_flags)
471 			ip_cmsg_recv(msg, skb);
472 	}
473 
474 	put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
475 
476 	/* Now we could try to dump offended packet options */
477 
478 	msg->msg_flags |= MSG_ERRQUEUE;
479 	err = copied;
480 
481 out_free_skb:
482 	kfree_skb(skb);
483 out:
484 	return err;
485 }
486 
487 
488 /*
489  *	Socket option code for IP. This is the end of the line after any
490  *	TCP,UDP etc options on an IP socket.
491  */
492 
493 static int do_ip_setsockopt(struct sock *sk, int level,
494 			    int optname, char __user *optval, unsigned int optlen)
495 {
496 	struct inet_sock *inet = inet_sk(sk);
497 	int val = 0, err;
498 
499 	switch (optname) {
500 	case IP_PKTINFO:
501 	case IP_RECVTTL:
502 	case IP_RECVOPTS:
503 	case IP_RECVTOS:
504 	case IP_RETOPTS:
505 	case IP_TOS:
506 	case IP_TTL:
507 	case IP_HDRINCL:
508 	case IP_MTU_DISCOVER:
509 	case IP_RECVERR:
510 	case IP_ROUTER_ALERT:
511 	case IP_FREEBIND:
512 	case IP_PASSSEC:
513 	case IP_TRANSPARENT:
514 	case IP_MINTTL:
515 	case IP_NODEFRAG:
516 	case IP_UNICAST_IF:
517 	case IP_MULTICAST_TTL:
518 	case IP_MULTICAST_ALL:
519 	case IP_MULTICAST_LOOP:
520 	case IP_RECVORIGDSTADDR:
521 		if (optlen >= sizeof(int)) {
522 			if (get_user(val, (int __user *) optval))
523 				return -EFAULT;
524 		} else if (optlen >= sizeof(char)) {
525 			unsigned char ucval;
526 
527 			if (get_user(ucval, (unsigned char __user *) optval))
528 				return -EFAULT;
529 			val = (int) ucval;
530 		}
531 	}
532 
533 	/* If optlen==0, it is equivalent to val == 0 */
534 
535 	if (ip_mroute_opt(optname))
536 		return ip_mroute_setsockopt(sk, optname, optval, optlen);
537 
538 	err = 0;
539 	lock_sock(sk);
540 
541 	switch (optname) {
542 	case IP_OPTIONS:
543 	{
544 		struct ip_options_rcu *old, *opt = NULL;
545 
546 		if (optlen > 40)
547 			goto e_inval;
548 		err = ip_options_get_from_user(sock_net(sk), &opt,
549 					       optval, optlen);
550 		if (err)
551 			break;
552 		old = rcu_dereference_protected(inet->inet_opt,
553 						sock_owned_by_user(sk));
554 		if (inet->is_icsk) {
555 			struct inet_connection_sock *icsk = inet_csk(sk);
556 #if IS_ENABLED(CONFIG_IPV6)
557 			if (sk->sk_family == PF_INET ||
558 			    (!((1 << sk->sk_state) &
559 			       (TCPF_LISTEN | TCPF_CLOSE)) &&
560 			     inet->inet_daddr != LOOPBACK4_IPV6)) {
561 #endif
562 				if (old)
563 					icsk->icsk_ext_hdr_len -= old->opt.optlen;
564 				if (opt)
565 					icsk->icsk_ext_hdr_len += opt->opt.optlen;
566 				icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
567 #if IS_ENABLED(CONFIG_IPV6)
568 			}
569 #endif
570 		}
571 		rcu_assign_pointer(inet->inet_opt, opt);
572 		if (old)
573 			kfree_rcu(old, rcu);
574 		break;
575 	}
576 	case IP_PKTINFO:
577 		if (val)
578 			inet->cmsg_flags |= IP_CMSG_PKTINFO;
579 		else
580 			inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
581 		break;
582 	case IP_RECVTTL:
583 		if (val)
584 			inet->cmsg_flags |=  IP_CMSG_TTL;
585 		else
586 			inet->cmsg_flags &= ~IP_CMSG_TTL;
587 		break;
588 	case IP_RECVTOS:
589 		if (val)
590 			inet->cmsg_flags |=  IP_CMSG_TOS;
591 		else
592 			inet->cmsg_flags &= ~IP_CMSG_TOS;
593 		break;
594 	case IP_RECVOPTS:
595 		if (val)
596 			inet->cmsg_flags |=  IP_CMSG_RECVOPTS;
597 		else
598 			inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
599 		break;
600 	case IP_RETOPTS:
601 		if (val)
602 			inet->cmsg_flags |= IP_CMSG_RETOPTS;
603 		else
604 			inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
605 		break;
606 	case IP_PASSSEC:
607 		if (val)
608 			inet->cmsg_flags |= IP_CMSG_PASSSEC;
609 		else
610 			inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
611 		break;
612 	case IP_RECVORIGDSTADDR:
613 		if (val)
614 			inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
615 		else
616 			inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
617 		break;
618 	case IP_TOS:	/* This sets both TOS and Precedence */
619 		if (sk->sk_type == SOCK_STREAM) {
620 			val &= ~INET_ECN_MASK;
621 			val |= inet->tos & INET_ECN_MASK;
622 		}
623 		if (inet->tos != val) {
624 			inet->tos = val;
625 			sk->sk_priority = rt_tos2priority(val);
626 			sk_dst_reset(sk);
627 		}
628 		break;
629 	case IP_TTL:
630 		if (optlen < 1)
631 			goto e_inval;
632 		if (val != -1 && (val < 1 || val > 255))
633 			goto e_inval;
634 		inet->uc_ttl = val;
635 		break;
636 	case IP_HDRINCL:
637 		if (sk->sk_type != SOCK_RAW) {
638 			err = -ENOPROTOOPT;
639 			break;
640 		}
641 		inet->hdrincl = val ? 1 : 0;
642 		break;
643 	case IP_NODEFRAG:
644 		if (sk->sk_type != SOCK_RAW) {
645 			err = -ENOPROTOOPT;
646 			break;
647 		}
648 		inet->nodefrag = val ? 1 : 0;
649 		break;
650 	case IP_MTU_DISCOVER:
651 		if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
652 			goto e_inval;
653 		inet->pmtudisc = val;
654 		break;
655 	case IP_RECVERR:
656 		inet->recverr = !!val;
657 		if (!val)
658 			skb_queue_purge(&sk->sk_error_queue);
659 		break;
660 	case IP_MULTICAST_TTL:
661 		if (sk->sk_type == SOCK_STREAM)
662 			goto e_inval;
663 		if (optlen < 1)
664 			goto e_inval;
665 		if (val == -1)
666 			val = 1;
667 		if (val < 0 || val > 255)
668 			goto e_inval;
669 		inet->mc_ttl = val;
670 		break;
671 	case IP_MULTICAST_LOOP:
672 		if (optlen < 1)
673 			goto e_inval;
674 		inet->mc_loop = !!val;
675 		break;
676 	case IP_UNICAST_IF:
677 	{
678 		struct net_device *dev = NULL;
679 		int ifindex;
680 
681 		if (optlen != sizeof(int))
682 			goto e_inval;
683 
684 		ifindex = (__force int)ntohl((__force __be32)val);
685 		if (ifindex == 0) {
686 			inet->uc_index = 0;
687 			err = 0;
688 			break;
689 		}
690 
691 		dev = dev_get_by_index(sock_net(sk), ifindex);
692 		err = -EADDRNOTAVAIL;
693 		if (!dev)
694 			break;
695 		dev_put(dev);
696 
697 		err = -EINVAL;
698 		if (sk->sk_bound_dev_if)
699 			break;
700 
701 		inet->uc_index = ifindex;
702 		err = 0;
703 		break;
704 	}
705 	case IP_MULTICAST_IF:
706 	{
707 		struct ip_mreqn mreq;
708 		struct net_device *dev = NULL;
709 
710 		if (sk->sk_type == SOCK_STREAM)
711 			goto e_inval;
712 		/*
713 		 *	Check the arguments are allowable
714 		 */
715 
716 		if (optlen < sizeof(struct in_addr))
717 			goto e_inval;
718 
719 		err = -EFAULT;
720 		if (optlen >= sizeof(struct ip_mreqn)) {
721 			if (copy_from_user(&mreq, optval, sizeof(mreq)))
722 				break;
723 		} else {
724 			memset(&mreq, 0, sizeof(mreq));
725 			if (optlen >= sizeof(struct ip_mreq)) {
726 				if (copy_from_user(&mreq, optval,
727 						   sizeof(struct ip_mreq)))
728 					break;
729 			} else if (optlen >= sizeof(struct in_addr)) {
730 				if (copy_from_user(&mreq.imr_address, optval,
731 						   sizeof(struct in_addr)))
732 					break;
733 			}
734 		}
735 
736 		if (!mreq.imr_ifindex) {
737 			if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
738 				inet->mc_index = 0;
739 				inet->mc_addr  = 0;
740 				err = 0;
741 				break;
742 			}
743 			dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
744 			if (dev)
745 				mreq.imr_ifindex = dev->ifindex;
746 		} else
747 			dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
748 
749 
750 		err = -EADDRNOTAVAIL;
751 		if (!dev)
752 			break;
753 		dev_put(dev);
754 
755 		err = -EINVAL;
756 		if (sk->sk_bound_dev_if &&
757 		    mreq.imr_ifindex != sk->sk_bound_dev_if)
758 			break;
759 
760 		inet->mc_index = mreq.imr_ifindex;
761 		inet->mc_addr  = mreq.imr_address.s_addr;
762 		err = 0;
763 		break;
764 	}
765 
766 	case IP_ADD_MEMBERSHIP:
767 	case IP_DROP_MEMBERSHIP:
768 	{
769 		struct ip_mreqn mreq;
770 
771 		err = -EPROTO;
772 		if (inet_sk(sk)->is_icsk)
773 			break;
774 
775 		if (optlen < sizeof(struct ip_mreq))
776 			goto e_inval;
777 		err = -EFAULT;
778 		if (optlen >= sizeof(struct ip_mreqn)) {
779 			if (copy_from_user(&mreq, optval, sizeof(mreq)))
780 				break;
781 		} else {
782 			memset(&mreq, 0, sizeof(mreq));
783 			if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
784 				break;
785 		}
786 
787 		if (optname == IP_ADD_MEMBERSHIP)
788 			err = ip_mc_join_group(sk, &mreq);
789 		else
790 			err = ip_mc_leave_group(sk, &mreq);
791 		break;
792 	}
793 	case IP_MSFILTER:
794 	{
795 		struct ip_msfilter *msf;
796 
797 		if (optlen < IP_MSFILTER_SIZE(0))
798 			goto e_inval;
799 		if (optlen > sysctl_optmem_max) {
800 			err = -ENOBUFS;
801 			break;
802 		}
803 		msf = kmalloc(optlen, GFP_KERNEL);
804 		if (!msf) {
805 			err = -ENOBUFS;
806 			break;
807 		}
808 		err = -EFAULT;
809 		if (copy_from_user(msf, optval, optlen)) {
810 			kfree(msf);
811 			break;
812 		}
813 		/* numsrc >= (1G-4) overflow in 32 bits */
814 		if (msf->imsf_numsrc >= 0x3ffffffcU ||
815 		    msf->imsf_numsrc > sysctl_igmp_max_msf) {
816 			kfree(msf);
817 			err = -ENOBUFS;
818 			break;
819 		}
820 		if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
821 			kfree(msf);
822 			err = -EINVAL;
823 			break;
824 		}
825 		err = ip_mc_msfilter(sk, msf, 0);
826 		kfree(msf);
827 		break;
828 	}
829 	case IP_BLOCK_SOURCE:
830 	case IP_UNBLOCK_SOURCE:
831 	case IP_ADD_SOURCE_MEMBERSHIP:
832 	case IP_DROP_SOURCE_MEMBERSHIP:
833 	{
834 		struct ip_mreq_source mreqs;
835 		int omode, add;
836 
837 		if (optlen != sizeof(struct ip_mreq_source))
838 			goto e_inval;
839 		if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
840 			err = -EFAULT;
841 			break;
842 		}
843 		if (optname == IP_BLOCK_SOURCE) {
844 			omode = MCAST_EXCLUDE;
845 			add = 1;
846 		} else if (optname == IP_UNBLOCK_SOURCE) {
847 			omode = MCAST_EXCLUDE;
848 			add = 0;
849 		} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
850 			struct ip_mreqn mreq;
851 
852 			mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
853 			mreq.imr_address.s_addr = mreqs.imr_interface;
854 			mreq.imr_ifindex = 0;
855 			err = ip_mc_join_group(sk, &mreq);
856 			if (err && err != -EADDRINUSE)
857 				break;
858 			omode = MCAST_INCLUDE;
859 			add = 1;
860 		} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
861 			omode = MCAST_INCLUDE;
862 			add = 0;
863 		}
864 		err = ip_mc_source(add, omode, sk, &mreqs, 0);
865 		break;
866 	}
867 	case MCAST_JOIN_GROUP:
868 	case MCAST_LEAVE_GROUP:
869 	{
870 		struct group_req greq;
871 		struct sockaddr_in *psin;
872 		struct ip_mreqn mreq;
873 
874 		if (optlen < sizeof(struct group_req))
875 			goto e_inval;
876 		err = -EFAULT;
877 		if (copy_from_user(&greq, optval, sizeof(greq)))
878 			break;
879 		psin = (struct sockaddr_in *)&greq.gr_group;
880 		if (psin->sin_family != AF_INET)
881 			goto e_inval;
882 		memset(&mreq, 0, sizeof(mreq));
883 		mreq.imr_multiaddr = psin->sin_addr;
884 		mreq.imr_ifindex = greq.gr_interface;
885 
886 		if (optname == MCAST_JOIN_GROUP)
887 			err = ip_mc_join_group(sk, &mreq);
888 		else
889 			err = ip_mc_leave_group(sk, &mreq);
890 		break;
891 	}
892 	case MCAST_JOIN_SOURCE_GROUP:
893 	case MCAST_LEAVE_SOURCE_GROUP:
894 	case MCAST_BLOCK_SOURCE:
895 	case MCAST_UNBLOCK_SOURCE:
896 	{
897 		struct group_source_req greqs;
898 		struct ip_mreq_source mreqs;
899 		struct sockaddr_in *psin;
900 		int omode, add;
901 
902 		if (optlen != sizeof(struct group_source_req))
903 			goto e_inval;
904 		if (copy_from_user(&greqs, optval, sizeof(greqs))) {
905 			err = -EFAULT;
906 			break;
907 		}
908 		if (greqs.gsr_group.ss_family != AF_INET ||
909 		    greqs.gsr_source.ss_family != AF_INET) {
910 			err = -EADDRNOTAVAIL;
911 			break;
912 		}
913 		psin = (struct sockaddr_in *)&greqs.gsr_group;
914 		mreqs.imr_multiaddr = psin->sin_addr.s_addr;
915 		psin = (struct sockaddr_in *)&greqs.gsr_source;
916 		mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
917 		mreqs.imr_interface = 0; /* use index for mc_source */
918 
919 		if (optname == MCAST_BLOCK_SOURCE) {
920 			omode = MCAST_EXCLUDE;
921 			add = 1;
922 		} else if (optname == MCAST_UNBLOCK_SOURCE) {
923 			omode = MCAST_EXCLUDE;
924 			add = 0;
925 		} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
926 			struct ip_mreqn mreq;
927 
928 			psin = (struct sockaddr_in *)&greqs.gsr_group;
929 			mreq.imr_multiaddr = psin->sin_addr;
930 			mreq.imr_address.s_addr = 0;
931 			mreq.imr_ifindex = greqs.gsr_interface;
932 			err = ip_mc_join_group(sk, &mreq);
933 			if (err && err != -EADDRINUSE)
934 				break;
935 			greqs.gsr_interface = mreq.imr_ifindex;
936 			omode = MCAST_INCLUDE;
937 			add = 1;
938 		} else /* MCAST_LEAVE_SOURCE_GROUP */ {
939 			omode = MCAST_INCLUDE;
940 			add = 0;
941 		}
942 		err = ip_mc_source(add, omode, sk, &mreqs,
943 				   greqs.gsr_interface);
944 		break;
945 	}
946 	case MCAST_MSFILTER:
947 	{
948 		struct sockaddr_in *psin;
949 		struct ip_msfilter *msf = NULL;
950 		struct group_filter *gsf = NULL;
951 		int msize, i, ifindex;
952 
953 		if (optlen < GROUP_FILTER_SIZE(0))
954 			goto e_inval;
955 		if (optlen > sysctl_optmem_max) {
956 			err = -ENOBUFS;
957 			break;
958 		}
959 		gsf = kmalloc(optlen, GFP_KERNEL);
960 		if (!gsf) {
961 			err = -ENOBUFS;
962 			break;
963 		}
964 		err = -EFAULT;
965 		if (copy_from_user(gsf, optval, optlen))
966 			goto mc_msf_out;
967 
968 		/* numsrc >= (4G-140)/128 overflow in 32 bits */
969 		if (gsf->gf_numsrc >= 0x1ffffff ||
970 		    gsf->gf_numsrc > sysctl_igmp_max_msf) {
971 			err = -ENOBUFS;
972 			goto mc_msf_out;
973 		}
974 		if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
975 			err = -EINVAL;
976 			goto mc_msf_out;
977 		}
978 		msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
979 		msf = kmalloc(msize, GFP_KERNEL);
980 		if (!msf) {
981 			err = -ENOBUFS;
982 			goto mc_msf_out;
983 		}
984 		ifindex = gsf->gf_interface;
985 		psin = (struct sockaddr_in *)&gsf->gf_group;
986 		if (psin->sin_family != AF_INET) {
987 			err = -EADDRNOTAVAIL;
988 			goto mc_msf_out;
989 		}
990 		msf->imsf_multiaddr = psin->sin_addr.s_addr;
991 		msf->imsf_interface = 0;
992 		msf->imsf_fmode = gsf->gf_fmode;
993 		msf->imsf_numsrc = gsf->gf_numsrc;
994 		err = -EADDRNOTAVAIL;
995 		for (i = 0; i < gsf->gf_numsrc; ++i) {
996 			psin = (struct sockaddr_in *)&gsf->gf_slist[i];
997 
998 			if (psin->sin_family != AF_INET)
999 				goto mc_msf_out;
1000 			msf->imsf_slist[i] = psin->sin_addr.s_addr;
1001 		}
1002 		kfree(gsf);
1003 		gsf = NULL;
1004 
1005 		err = ip_mc_msfilter(sk, msf, ifindex);
1006 mc_msf_out:
1007 		kfree(msf);
1008 		kfree(gsf);
1009 		break;
1010 	}
1011 	case IP_MULTICAST_ALL:
1012 		if (optlen < 1)
1013 			goto e_inval;
1014 		if (val != 0 && val != 1)
1015 			goto e_inval;
1016 		inet->mc_all = val;
1017 		break;
1018 	case IP_ROUTER_ALERT:
1019 		err = ip_ra_control(sk, val ? 1 : 0, NULL);
1020 		break;
1021 
1022 	case IP_FREEBIND:
1023 		if (optlen < 1)
1024 			goto e_inval;
1025 		inet->freebind = !!val;
1026 		break;
1027 
1028 	case IP_IPSEC_POLICY:
1029 	case IP_XFRM_POLICY:
1030 		err = -EPERM;
1031 		if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1032 			break;
1033 		err = xfrm_user_policy(sk, optname, optval, optlen);
1034 		break;
1035 
1036 	case IP_TRANSPARENT:
1037 		if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1038 		    !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1039 			err = -EPERM;
1040 			break;
1041 		}
1042 		if (optlen < 1)
1043 			goto e_inval;
1044 		inet->transparent = !!val;
1045 		break;
1046 
1047 	case IP_MINTTL:
1048 		if (optlen < 1)
1049 			goto e_inval;
1050 		if (val < 0 || val > 255)
1051 			goto e_inval;
1052 		inet->min_ttl = val;
1053 		break;
1054 
1055 	default:
1056 		err = -ENOPROTOOPT;
1057 		break;
1058 	}
1059 	release_sock(sk);
1060 	return err;
1061 
1062 e_inval:
1063 	release_sock(sk);
1064 	return -EINVAL;
1065 }
1066 
1067 /**
1068  * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1069  * @sk: socket
1070  * @skb: buffer
1071  *
1072  * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1073  * destination in skb->cb[] before dst drop.
1074  * This way, receiver doesn't make cache line misses to read rtable.
1075  */
1076 void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
1077 {
1078 	struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1079 	bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1080 		       ipv6_sk_rxinfo(sk);
1081 
1082 	if (prepare && skb_rtable(skb)) {
1083 		pktinfo->ipi_ifindex = inet_iif(skb);
1084 		pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1085 	} else {
1086 		pktinfo->ipi_ifindex = 0;
1087 		pktinfo->ipi_spec_dst.s_addr = 0;
1088 	}
1089 	skb_dst_drop(skb);
1090 }
1091 
1092 int ip_setsockopt(struct sock *sk, int level,
1093 		int optname, char __user *optval, unsigned int optlen)
1094 {
1095 	int err;
1096 
1097 	if (level != SOL_IP)
1098 		return -ENOPROTOOPT;
1099 
1100 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1101 #ifdef CONFIG_NETFILTER
1102 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1103 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
1104 			optname != IP_IPSEC_POLICY &&
1105 			optname != IP_XFRM_POLICY &&
1106 			!ip_mroute_opt(optname)) {
1107 		lock_sock(sk);
1108 		err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1109 		release_sock(sk);
1110 	}
1111 #endif
1112 	return err;
1113 }
1114 EXPORT_SYMBOL(ip_setsockopt);
1115 
1116 #ifdef CONFIG_COMPAT
1117 int compat_ip_setsockopt(struct sock *sk, int level, int optname,
1118 			 char __user *optval, unsigned int optlen)
1119 {
1120 	int err;
1121 
1122 	if (level != SOL_IP)
1123 		return -ENOPROTOOPT;
1124 
1125 	if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1126 		return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1127 			ip_setsockopt);
1128 
1129 	err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1130 #ifdef CONFIG_NETFILTER
1131 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1132 	if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
1133 			optname != IP_IPSEC_POLICY &&
1134 			optname != IP_XFRM_POLICY &&
1135 			!ip_mroute_opt(optname)) {
1136 		lock_sock(sk);
1137 		err = compat_nf_setsockopt(sk, PF_INET, optname,
1138 					   optval, optlen);
1139 		release_sock(sk);
1140 	}
1141 #endif
1142 	return err;
1143 }
1144 EXPORT_SYMBOL(compat_ip_setsockopt);
1145 #endif
1146 
1147 /*
1148  *	Get the options. Note for future reference. The GET of IP options gets
1149  *	the _received_ ones. The set sets the _sent_ ones.
1150  */
1151 
1152 static int do_ip_getsockopt(struct sock *sk, int level, int optname,
1153 			    char __user *optval, int __user *optlen, unsigned int flags)
1154 {
1155 	struct inet_sock *inet = inet_sk(sk);
1156 	int val;
1157 	int len;
1158 
1159 	if (level != SOL_IP)
1160 		return -EOPNOTSUPP;
1161 
1162 	if (ip_mroute_opt(optname))
1163 		return ip_mroute_getsockopt(sk, optname, optval, optlen);
1164 
1165 	if (get_user(len, optlen))
1166 		return -EFAULT;
1167 	if (len < 0)
1168 		return -EINVAL;
1169 
1170 	lock_sock(sk);
1171 
1172 	switch (optname) {
1173 	case IP_OPTIONS:
1174 	{
1175 		unsigned char optbuf[sizeof(struct ip_options)+40];
1176 		struct ip_options *opt = (struct ip_options *)optbuf;
1177 		struct ip_options_rcu *inet_opt;
1178 
1179 		inet_opt = rcu_dereference_protected(inet->inet_opt,
1180 						     sock_owned_by_user(sk));
1181 		opt->optlen = 0;
1182 		if (inet_opt)
1183 			memcpy(optbuf, &inet_opt->opt,
1184 			       sizeof(struct ip_options) +
1185 			       inet_opt->opt.optlen);
1186 		release_sock(sk);
1187 
1188 		if (opt->optlen == 0)
1189 			return put_user(0, optlen);
1190 
1191 		ip_options_undo(opt);
1192 
1193 		len = min_t(unsigned int, len, opt->optlen);
1194 		if (put_user(len, optlen))
1195 			return -EFAULT;
1196 		if (copy_to_user(optval, opt->__data, len))
1197 			return -EFAULT;
1198 		return 0;
1199 	}
1200 	case IP_PKTINFO:
1201 		val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1202 		break;
1203 	case IP_RECVTTL:
1204 		val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1205 		break;
1206 	case IP_RECVTOS:
1207 		val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1208 		break;
1209 	case IP_RECVOPTS:
1210 		val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1211 		break;
1212 	case IP_RETOPTS:
1213 		val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1214 		break;
1215 	case IP_PASSSEC:
1216 		val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1217 		break;
1218 	case IP_RECVORIGDSTADDR:
1219 		val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1220 		break;
1221 	case IP_TOS:
1222 		val = inet->tos;
1223 		break;
1224 	case IP_TTL:
1225 		val = (inet->uc_ttl == -1 ?
1226 		       sysctl_ip_default_ttl :
1227 		       inet->uc_ttl);
1228 		break;
1229 	case IP_HDRINCL:
1230 		val = inet->hdrincl;
1231 		break;
1232 	case IP_NODEFRAG:
1233 		val = inet->nodefrag;
1234 		break;
1235 	case IP_MTU_DISCOVER:
1236 		val = inet->pmtudisc;
1237 		break;
1238 	case IP_MTU:
1239 	{
1240 		struct dst_entry *dst;
1241 		val = 0;
1242 		dst = sk_dst_get(sk);
1243 		if (dst) {
1244 			val = dst_mtu(dst);
1245 			dst_release(dst);
1246 		}
1247 		if (!val) {
1248 			release_sock(sk);
1249 			return -ENOTCONN;
1250 		}
1251 		break;
1252 	}
1253 	case IP_RECVERR:
1254 		val = inet->recverr;
1255 		break;
1256 	case IP_MULTICAST_TTL:
1257 		val = inet->mc_ttl;
1258 		break;
1259 	case IP_MULTICAST_LOOP:
1260 		val = inet->mc_loop;
1261 		break;
1262 	case IP_UNICAST_IF:
1263 		val = (__force int)htonl((__u32) inet->uc_index);
1264 		break;
1265 	case IP_MULTICAST_IF:
1266 	{
1267 		struct in_addr addr;
1268 		len = min_t(unsigned int, len, sizeof(struct in_addr));
1269 		addr.s_addr = inet->mc_addr;
1270 		release_sock(sk);
1271 
1272 		if (put_user(len, optlen))
1273 			return -EFAULT;
1274 		if (copy_to_user(optval, &addr, len))
1275 			return -EFAULT;
1276 		return 0;
1277 	}
1278 	case IP_MSFILTER:
1279 	{
1280 		struct ip_msfilter msf;
1281 		int err;
1282 
1283 		if (len < IP_MSFILTER_SIZE(0)) {
1284 			release_sock(sk);
1285 			return -EINVAL;
1286 		}
1287 		if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1288 			release_sock(sk);
1289 			return -EFAULT;
1290 		}
1291 		err = ip_mc_msfget(sk, &msf,
1292 				   (struct ip_msfilter __user *)optval, optlen);
1293 		release_sock(sk);
1294 		return err;
1295 	}
1296 	case MCAST_MSFILTER:
1297 	{
1298 		struct group_filter gsf;
1299 		int err;
1300 
1301 		if (len < GROUP_FILTER_SIZE(0)) {
1302 			release_sock(sk);
1303 			return -EINVAL;
1304 		}
1305 		if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
1306 			release_sock(sk);
1307 			return -EFAULT;
1308 		}
1309 		err = ip_mc_gsfget(sk, &gsf,
1310 				   (struct group_filter __user *)optval,
1311 				   optlen);
1312 		release_sock(sk);
1313 		return err;
1314 	}
1315 	case IP_MULTICAST_ALL:
1316 		val = inet->mc_all;
1317 		break;
1318 	case IP_PKTOPTIONS:
1319 	{
1320 		struct msghdr msg;
1321 
1322 		release_sock(sk);
1323 
1324 		if (sk->sk_type != SOCK_STREAM)
1325 			return -ENOPROTOOPT;
1326 
1327 		msg.msg_control = (__force void *) optval;
1328 		msg.msg_controllen = len;
1329 		msg.msg_flags = flags;
1330 
1331 		if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1332 			struct in_pktinfo info;
1333 
1334 			info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1335 			info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
1336 			info.ipi_ifindex = inet->mc_index;
1337 			put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1338 		}
1339 		if (inet->cmsg_flags & IP_CMSG_TTL) {
1340 			int hlim = inet->mc_ttl;
1341 			put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1342 		}
1343 		if (inet->cmsg_flags & IP_CMSG_TOS) {
1344 			int tos = inet->rcv_tos;
1345 			put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1346 		}
1347 		len -= msg.msg_controllen;
1348 		return put_user(len, optlen);
1349 	}
1350 	case IP_FREEBIND:
1351 		val = inet->freebind;
1352 		break;
1353 	case IP_TRANSPARENT:
1354 		val = inet->transparent;
1355 		break;
1356 	case IP_MINTTL:
1357 		val = inet->min_ttl;
1358 		break;
1359 	default:
1360 		release_sock(sk);
1361 		return -ENOPROTOOPT;
1362 	}
1363 	release_sock(sk);
1364 
1365 	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1366 		unsigned char ucval = (unsigned char)val;
1367 		len = 1;
1368 		if (put_user(len, optlen))
1369 			return -EFAULT;
1370 		if (copy_to_user(optval, &ucval, 1))
1371 			return -EFAULT;
1372 	} else {
1373 		len = min_t(unsigned int, sizeof(int), len);
1374 		if (put_user(len, optlen))
1375 			return -EFAULT;
1376 		if (copy_to_user(optval, &val, len))
1377 			return -EFAULT;
1378 	}
1379 	return 0;
1380 }
1381 
1382 int ip_getsockopt(struct sock *sk, int level,
1383 		  int optname, char __user *optval, int __user *optlen)
1384 {
1385 	int err;
1386 
1387 	err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
1388 #ifdef CONFIG_NETFILTER
1389 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1390 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1391 			!ip_mroute_opt(optname)) {
1392 		int len;
1393 
1394 		if (get_user(len, optlen))
1395 			return -EFAULT;
1396 
1397 		lock_sock(sk);
1398 		err = nf_getsockopt(sk, PF_INET, optname, optval,
1399 				&len);
1400 		release_sock(sk);
1401 		if (err >= 0)
1402 			err = put_user(len, optlen);
1403 		return err;
1404 	}
1405 #endif
1406 	return err;
1407 }
1408 EXPORT_SYMBOL(ip_getsockopt);
1409 
1410 #ifdef CONFIG_COMPAT
1411 int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1412 			 char __user *optval, int __user *optlen)
1413 {
1414 	int err;
1415 
1416 	if (optname == MCAST_MSFILTER)
1417 		return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1418 			ip_getsockopt);
1419 
1420 	err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1421 		MSG_CMSG_COMPAT);
1422 
1423 #ifdef CONFIG_NETFILTER
1424 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1425 	if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1426 			!ip_mroute_opt(optname)) {
1427 		int len;
1428 
1429 		if (get_user(len, optlen))
1430 			return -EFAULT;
1431 
1432 		lock_sock(sk);
1433 		err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1434 		release_sock(sk);
1435 		if (err >= 0)
1436 			err = put_user(len, optlen);
1437 		return err;
1438 	}
1439 #endif
1440 	return err;
1441 }
1442 EXPORT_SYMBOL(compat_ip_getsockopt);
1443 #endif
1444