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