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