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