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