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