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