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.
8 * Version: $Id: ip_sockglue.c,v 1.62 2002/02/01 22:01:04 davem Exp $
13 * Many : Split from ip.c , see ip.c for history.
14 * Martin Mares : TOS setting fixed.
15 * Alan Cox : Fixed a couple of oopses in Martin's
17 * Mike McLagan : Routing by source
20 #include <linux/module.h>
21 #include <linux/types.h>
23 #include <linux/skbuff.h>
25 #include <linux/icmp.h>
26 #include <linux/inetdevice.h>
27 #include <linux/netdevice.h>
31 #include <net/tcp_states.h>
32 #include <linux/udp.h>
33 #include <linux/igmp.h>
34 #include <linux/netfilter.h>
35 #include <linux/route.h>
36 #include <linux/mroute.h>
37 #include <net/route.h>
39 #include <net/compat.h>
40 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
41 #include <net/transp_v6.h>
44 #include <linux/errqueue.h>
45 #include <asm/uaccess.h>
47 #define IP_CMSG_PKTINFO 1
50 #define IP_CMSG_RECVOPTS 8
51 #define IP_CMSG_RETOPTS 16
52 #define IP_CMSG_PASSSEC 32
55 * SOL_IP control messages.
58 static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
60 struct in_pktinfo info;
61 struct rtable *rt = skb->rtable;
63 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
65 info.ipi_ifindex = rt->rt_iif;
66 info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
69 info.ipi_spec_dst.s_addr = 0;
72 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
75 static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
77 int ttl = ip_hdr(skb)->ttl;
78 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
81 static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
83 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
86 static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
88 if (IPCB(skb)->opt.optlen == 0)
91 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
96 static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
98 unsigned char optbuf[sizeof(struct ip_options) + 40];
99 struct ip_options * opt = (struct ip_options*)optbuf;
101 if (IPCB(skb)->opt.optlen == 0)
104 if (ip_options_echo(opt, skb)) {
105 msg->msg_flags |= MSG_CTRUNC;
108 ip_options_undo(opt);
110 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
113 static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
119 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
123 err = security_secid_to_secctx(secid, &secdata, &seclen);
127 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
128 security_release_secctx(secdata, seclen);
132 void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
134 struct inet_sock *inet = inet_sk(skb->sk);
135 unsigned flags = inet->cmsg_flags;
137 /* Ordered by supposed usage frequency */
139 ip_cmsg_recv_pktinfo(msg, skb);
140 if ((flags>>=1) == 0)
144 ip_cmsg_recv_ttl(msg, skb);
145 if ((flags>>=1) == 0)
149 ip_cmsg_recv_tos(msg, skb);
150 if ((flags>>=1) == 0)
154 ip_cmsg_recv_opts(msg, skb);
155 if ((flags>>=1) == 0)
159 ip_cmsg_recv_retopts(msg, skb);
160 if ((flags>>=1) == 0)
164 ip_cmsg_recv_security(msg, skb);
167 int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
170 struct cmsghdr *cmsg;
172 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
173 if (!CMSG_OK(msg, cmsg))
175 if (cmsg->cmsg_level != SOL_IP)
177 switch (cmsg->cmsg_type) {
179 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
180 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg), err < 40 ? err : 40);
186 struct in_pktinfo *info;
187 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
189 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
190 ipc->oif = info->ipi_ifindex;
191 ipc->addr = info->ipi_spec_dst.s_addr;
202 /* Special input handler for packets caught by router alert option.
203 They are selected only by protocol field, and then processed likely
204 local ones; but only if someone wants them! Otherwise, router
205 not running rsvpd will kill RSVP.
207 It is user level problem, what it will make with them.
208 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
209 but receiver should be enough clever f.e. to forward mtrace requests,
210 sent to multicast group to reach destination designated router.
212 struct ip_ra_chain *ip_ra_chain;
213 DEFINE_RWLOCK(ip_ra_lock);
215 int ip_ra_control(struct sock *sk, unsigned char on, void (*destructor)(struct sock *))
217 struct ip_ra_chain *ra, *new_ra, **rap;
219 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
222 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
224 write_lock_bh(&ip_ra_lock);
225 for (rap = &ip_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
228 write_unlock_bh(&ip_ra_lock);
233 write_unlock_bh(&ip_ra_lock);
242 if (new_ra == NULL) {
243 write_unlock_bh(&ip_ra_lock);
247 new_ra->destructor = destructor;
252 write_unlock_bh(&ip_ra_lock);
257 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
258 __be16 port, u32 info, u8 *payload)
260 struct inet_sock *inet = inet_sk(sk);
261 struct sock_exterr_skb *serr;
266 skb = skb_clone(skb, GFP_ATOMIC);
270 serr = SKB_EXT_ERR(skb);
271 serr->ee.ee_errno = err;
272 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
273 serr->ee.ee_type = icmp_hdr(skb)->type;
274 serr->ee.ee_code = icmp_hdr(skb)->code;
276 serr->ee.ee_info = info;
277 serr->ee.ee_data = 0;
278 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
279 skb_network_header(skb);
282 if (skb_pull(skb, payload - skb->data) != NULL) {
283 skb_reset_transport_header(skb);
284 if (sock_queue_err_skb(sk, skb) == 0)
290 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
292 struct inet_sock *inet = inet_sk(sk);
293 struct sock_exterr_skb *serr;
300 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
304 skb_put(skb, sizeof(struct iphdr));
305 skb_reset_network_header(skb);
309 serr = SKB_EXT_ERR(skb);
310 serr->ee.ee_errno = err;
311 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
312 serr->ee.ee_type = 0;
313 serr->ee.ee_code = 0;
315 serr->ee.ee_info = info;
316 serr->ee.ee_data = 0;
317 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
320 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
321 skb_reset_transport_header(skb);
323 if (sock_queue_err_skb(sk, skb))
328 * Handle MSG_ERRQUEUE
330 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
332 struct sock_exterr_skb *serr;
333 struct sk_buff *skb, *skb2;
334 struct sockaddr_in *sin;
336 struct sock_extended_err ee;
337 struct sockaddr_in offender;
343 skb = skb_dequeue(&sk->sk_error_queue);
349 msg->msg_flags |= MSG_TRUNC;
352 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
356 sock_recv_timestamp(msg, sk, skb);
358 serr = SKB_EXT_ERR(skb);
360 sin = (struct sockaddr_in *)msg->msg_name;
362 sin->sin_family = AF_INET;
363 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
365 sin->sin_port = serr->port;
366 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
369 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
370 sin = &errhdr.offender;
371 sin->sin_family = AF_UNSPEC;
372 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
373 struct inet_sock *inet = inet_sk(sk);
375 sin->sin_family = AF_INET;
376 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
378 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
379 if (inet->cmsg_flags)
380 ip_cmsg_recv(msg, skb);
383 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
385 /* Now we could try to dump offended packet options */
387 msg->msg_flags |= MSG_ERRQUEUE;
390 /* Reset and regenerate socket error */
391 spin_lock_bh(&sk->sk_error_queue.lock);
393 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
394 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
395 spin_unlock_bh(&sk->sk_error_queue.lock);
396 sk->sk_error_report(sk);
398 spin_unlock_bh(&sk->sk_error_queue.lock);
408 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
412 static int do_ip_setsockopt(struct sock *sk, int level,
413 int optname, char __user *optval, int optlen)
415 struct inet_sock *inet = inet_sk(sk);
418 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
419 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
420 (1<<IP_RETOPTS) | (1<<IP_TOS) |
421 (1<<IP_TTL) | (1<<IP_HDRINCL) |
422 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
423 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
425 optname == IP_MULTICAST_TTL ||
426 optname == IP_MULTICAST_LOOP) {
427 if (optlen >= sizeof(int)) {
428 if (get_user(val, (int __user *) optval))
430 } else if (optlen >= sizeof(char)) {
433 if (get_user(ucval, (unsigned char __user *) optval))
439 /* If optlen==0, it is equivalent to val == 0 */
441 if (ip_mroute_opt(optname))
442 return ip_mroute_setsockopt(sk,optname,optval,optlen);
450 struct ip_options * opt = NULL;
451 if (optlen > 40 || optlen < 0)
453 err = ip_options_get_from_user(sock_net(sk), &opt,
458 struct inet_connection_sock *icsk = inet_csk(sk);
459 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
460 if (sk->sk_family == PF_INET ||
461 (!((1 << sk->sk_state) &
462 (TCPF_LISTEN | TCPF_CLOSE)) &&
463 inet->daddr != LOOPBACK4_IPV6)) {
466 icsk->icsk_ext_hdr_len -= inet->opt->optlen;
468 icsk->icsk_ext_hdr_len += opt->optlen;
469 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
470 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
474 opt = xchg(&inet->opt, opt);
480 inet->cmsg_flags |= IP_CMSG_PKTINFO;
482 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
486 inet->cmsg_flags |= IP_CMSG_TTL;
488 inet->cmsg_flags &= ~IP_CMSG_TTL;
492 inet->cmsg_flags |= IP_CMSG_TOS;
494 inet->cmsg_flags &= ~IP_CMSG_TOS;
498 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
500 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
504 inet->cmsg_flags |= IP_CMSG_RETOPTS;
506 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
510 inet->cmsg_flags |= IP_CMSG_PASSSEC;
512 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
514 case IP_TOS: /* This sets both TOS and Precedence */
515 if (sk->sk_type == SOCK_STREAM) {
517 val |= inet->tos & 3;
519 if (inet->tos != val) {
521 sk->sk_priority = rt_tos2priority(val);
528 if (val != -1 && (val < 1 || val>255))
533 if (sk->sk_type != SOCK_RAW) {
537 inet->hdrincl = val ? 1 : 0;
539 case IP_MTU_DISCOVER:
542 inet->pmtudisc = val;
545 inet->recverr = !!val;
547 skb_queue_purge(&sk->sk_error_queue);
549 case IP_MULTICAST_TTL:
550 if (sk->sk_type == SOCK_STREAM)
556 if (val < 0 || val > 255)
560 case IP_MULTICAST_LOOP:
563 inet->mc_loop = !!val;
565 case IP_MULTICAST_IF:
567 struct ip_mreqn mreq;
568 struct net_device *dev = NULL;
570 if (sk->sk_type == SOCK_STREAM)
573 * Check the arguments are allowable
577 if (optlen >= sizeof(struct ip_mreqn)) {
578 if (copy_from_user(&mreq,optval,sizeof(mreq)))
581 memset(&mreq, 0, sizeof(mreq));
582 if (optlen >= sizeof(struct in_addr) &&
583 copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
587 if (!mreq.imr_ifindex) {
588 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
594 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
596 mreq.imr_ifindex = dev->ifindex;
600 dev = __dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
603 err = -EADDRNOTAVAIL;
608 if (sk->sk_bound_dev_if &&
609 mreq.imr_ifindex != sk->sk_bound_dev_if)
612 inet->mc_index = mreq.imr_ifindex;
613 inet->mc_addr = mreq.imr_address.s_addr;
618 case IP_ADD_MEMBERSHIP:
619 case IP_DROP_MEMBERSHIP:
621 struct ip_mreqn mreq;
624 if (inet_sk(sk)->is_icsk)
627 if (optlen < sizeof(struct ip_mreq))
630 if (optlen >= sizeof(struct ip_mreqn)) {
631 if (copy_from_user(&mreq,optval,sizeof(mreq)))
634 memset(&mreq, 0, sizeof(mreq));
635 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
639 if (optname == IP_ADD_MEMBERSHIP)
640 err = ip_mc_join_group(sk, &mreq);
642 err = ip_mc_leave_group(sk, &mreq);
647 extern int sysctl_igmp_max_msf;
648 struct ip_msfilter *msf;
650 if (optlen < IP_MSFILTER_SIZE(0))
652 if (optlen > sysctl_optmem_max) {
656 msf = kmalloc(optlen, GFP_KERNEL);
662 if (copy_from_user(msf, optval, optlen)) {
666 /* numsrc >= (1G-4) overflow in 32 bits */
667 if (msf->imsf_numsrc >= 0x3ffffffcU ||
668 msf->imsf_numsrc > sysctl_igmp_max_msf) {
673 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
678 err = ip_mc_msfilter(sk, msf, 0);
682 case IP_BLOCK_SOURCE:
683 case IP_UNBLOCK_SOURCE:
684 case IP_ADD_SOURCE_MEMBERSHIP:
685 case IP_DROP_SOURCE_MEMBERSHIP:
687 struct ip_mreq_source mreqs;
690 if (optlen != sizeof(struct ip_mreq_source))
692 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
696 if (optname == IP_BLOCK_SOURCE) {
697 omode = MCAST_EXCLUDE;
699 } else if (optname == IP_UNBLOCK_SOURCE) {
700 omode = MCAST_EXCLUDE;
702 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
703 struct ip_mreqn mreq;
705 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
706 mreq.imr_address.s_addr = mreqs.imr_interface;
707 mreq.imr_ifindex = 0;
708 err = ip_mc_join_group(sk, &mreq);
709 if (err && err != -EADDRINUSE)
711 omode = MCAST_INCLUDE;
713 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
714 omode = MCAST_INCLUDE;
717 err = ip_mc_source(add, omode, sk, &mreqs, 0);
720 case MCAST_JOIN_GROUP:
721 case MCAST_LEAVE_GROUP:
723 struct group_req greq;
724 struct sockaddr_in *psin;
725 struct ip_mreqn mreq;
727 if (optlen < sizeof(struct group_req))
730 if (copy_from_user(&greq, optval, sizeof(greq)))
732 psin = (struct sockaddr_in *)&greq.gr_group;
733 if (psin->sin_family != AF_INET)
735 memset(&mreq, 0, sizeof(mreq));
736 mreq.imr_multiaddr = psin->sin_addr;
737 mreq.imr_ifindex = greq.gr_interface;
739 if (optname == MCAST_JOIN_GROUP)
740 err = ip_mc_join_group(sk, &mreq);
742 err = ip_mc_leave_group(sk, &mreq);
745 case MCAST_JOIN_SOURCE_GROUP:
746 case MCAST_LEAVE_SOURCE_GROUP:
747 case MCAST_BLOCK_SOURCE:
748 case MCAST_UNBLOCK_SOURCE:
750 struct group_source_req greqs;
751 struct ip_mreq_source mreqs;
752 struct sockaddr_in *psin;
755 if (optlen != sizeof(struct group_source_req))
757 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
761 if (greqs.gsr_group.ss_family != AF_INET ||
762 greqs.gsr_source.ss_family != AF_INET) {
763 err = -EADDRNOTAVAIL;
766 psin = (struct sockaddr_in *)&greqs.gsr_group;
767 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
768 psin = (struct sockaddr_in *)&greqs.gsr_source;
769 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
770 mreqs.imr_interface = 0; /* use index for mc_source */
772 if (optname == MCAST_BLOCK_SOURCE) {
773 omode = MCAST_EXCLUDE;
775 } else if (optname == MCAST_UNBLOCK_SOURCE) {
776 omode = MCAST_EXCLUDE;
778 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
779 struct ip_mreqn mreq;
781 psin = (struct sockaddr_in *)&greqs.gsr_group;
782 mreq.imr_multiaddr = psin->sin_addr;
783 mreq.imr_address.s_addr = 0;
784 mreq.imr_ifindex = greqs.gsr_interface;
785 err = ip_mc_join_group(sk, &mreq);
786 if (err && err != -EADDRINUSE)
788 greqs.gsr_interface = mreq.imr_ifindex;
789 omode = MCAST_INCLUDE;
791 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
792 omode = MCAST_INCLUDE;
795 err = ip_mc_source(add, omode, sk, &mreqs,
796 greqs.gsr_interface);
801 extern int sysctl_igmp_max_msf;
802 struct sockaddr_in *psin;
803 struct ip_msfilter *msf = NULL;
804 struct group_filter *gsf = NULL;
805 int msize, i, ifindex;
807 if (optlen < GROUP_FILTER_SIZE(0))
809 if (optlen > sysctl_optmem_max) {
813 gsf = kmalloc(optlen,GFP_KERNEL);
819 if (copy_from_user(gsf, optval, optlen)) {
822 /* numsrc >= (4G-140)/128 overflow in 32 bits */
823 if (gsf->gf_numsrc >= 0x1ffffff ||
824 gsf->gf_numsrc > sysctl_igmp_max_msf) {
828 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
832 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
833 msf = kmalloc(msize,GFP_KERNEL);
838 ifindex = gsf->gf_interface;
839 psin = (struct sockaddr_in *)&gsf->gf_group;
840 if (psin->sin_family != AF_INET) {
841 err = -EADDRNOTAVAIL;
844 msf->imsf_multiaddr = psin->sin_addr.s_addr;
845 msf->imsf_interface = 0;
846 msf->imsf_fmode = gsf->gf_fmode;
847 msf->imsf_numsrc = gsf->gf_numsrc;
848 err = -EADDRNOTAVAIL;
849 for (i=0; i<gsf->gf_numsrc; ++i) {
850 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
852 if (psin->sin_family != AF_INET)
854 msf->imsf_slist[i] = psin->sin_addr.s_addr;
859 err = ip_mc_msfilter(sk, msf, ifindex);
865 case IP_ROUTER_ALERT:
866 err = ip_ra_control(sk, val ? 1 : 0, NULL);
872 inet->freebind = !!val;
875 case IP_IPSEC_POLICY:
878 if (!capable(CAP_NET_ADMIN))
880 err = xfrm_user_policy(sk, optname, optval, optlen);
895 int ip_setsockopt(struct sock *sk, int level,
896 int optname, char __user *optval, int optlen)
903 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
904 #ifdef CONFIG_NETFILTER
905 /* we need to exclude all possible ENOPROTOOPTs except default case */
906 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
907 optname != IP_IPSEC_POLICY &&
908 optname != IP_XFRM_POLICY &&
909 !ip_mroute_opt(optname)) {
911 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
919 int compat_ip_setsockopt(struct sock *sk, int level, int optname,
920 char __user *optval, int optlen)
927 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
928 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
931 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
932 #ifdef CONFIG_NETFILTER
933 /* we need to exclude all possible ENOPROTOOPTs except default case */
934 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
935 optname != IP_IPSEC_POLICY &&
936 optname != IP_XFRM_POLICY &&
937 !ip_mroute_opt(optname)) {
939 err = compat_nf_setsockopt(sk, PF_INET, optname,
947 EXPORT_SYMBOL(compat_ip_setsockopt);
951 * Get the options. Note for future reference. The GET of IP options gets the
952 * _received_ ones. The set sets the _sent_ ones.
955 static int do_ip_getsockopt(struct sock *sk, int level, int optname,
956 char __user *optval, int __user *optlen)
958 struct inet_sock *inet = inet_sk(sk);
965 if (ip_mroute_opt(optname))
966 return ip_mroute_getsockopt(sk,optname,optval,optlen);
968 if (get_user(len,optlen))
978 unsigned char optbuf[sizeof(struct ip_options)+40];
979 struct ip_options * opt = (struct ip_options*)optbuf;
982 memcpy(optbuf, inet->opt,
983 sizeof(struct ip_options)+
987 if (opt->optlen == 0)
988 return put_user(0, optlen);
990 ip_options_undo(opt);
992 len = min_t(unsigned int, len, opt->optlen);
993 if (put_user(len, optlen))
995 if (copy_to_user(optval, opt->__data, len))
1000 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1003 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1006 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1009 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1012 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1015 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1021 val = (inet->uc_ttl == -1 ?
1022 sysctl_ip_default_ttl :
1026 val = inet->hdrincl;
1028 case IP_MTU_DISCOVER:
1029 val = inet->pmtudisc;
1033 struct dst_entry *dst;
1035 dst = sk_dst_get(sk);
1047 val = inet->recverr;
1049 case IP_MULTICAST_TTL:
1052 case IP_MULTICAST_LOOP:
1053 val = inet->mc_loop;
1055 case IP_MULTICAST_IF:
1057 struct in_addr addr;
1058 len = min_t(unsigned int, len, sizeof(struct in_addr));
1059 addr.s_addr = inet->mc_addr;
1062 if (put_user(len, optlen))
1064 if (copy_to_user(optval, &addr, len))
1070 struct ip_msfilter msf;
1073 if (len < IP_MSFILTER_SIZE(0)) {
1077 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1081 err = ip_mc_msfget(sk, &msf,
1082 (struct ip_msfilter __user *)optval, optlen);
1086 case MCAST_MSFILTER:
1088 struct group_filter gsf;
1091 if (len < GROUP_FILTER_SIZE(0)) {
1095 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
1099 err = ip_mc_gsfget(sk, &gsf,
1100 (struct group_filter __user *)optval, optlen);
1110 if (sk->sk_type != SOCK_STREAM)
1111 return -ENOPROTOOPT;
1113 msg.msg_control = optval;
1114 msg.msg_controllen = len;
1117 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1118 struct in_pktinfo info;
1120 info.ipi_addr.s_addr = inet->rcv_saddr;
1121 info.ipi_spec_dst.s_addr = inet->rcv_saddr;
1122 info.ipi_ifindex = inet->mc_index;
1123 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1125 if (inet->cmsg_flags & IP_CMSG_TTL) {
1126 int hlim = inet->mc_ttl;
1127 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1129 len -= msg.msg_controllen;
1130 return put_user(len, optlen);
1133 val = inet->freebind;
1137 return -ENOPROTOOPT;
1141 if (len < sizeof(int) && len > 0 && val>=0 && val<=255) {
1142 unsigned char ucval = (unsigned char)val;
1144 if (put_user(len, optlen))
1146 if (copy_to_user(optval,&ucval,1))
1149 len = min_t(unsigned int, sizeof(int), len);
1150 if (put_user(len, optlen))
1152 if (copy_to_user(optval,&val,len))
1158 int ip_getsockopt(struct sock *sk, int level,
1159 int optname, char __user *optval, int __user *optlen)
1163 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1164 #ifdef CONFIG_NETFILTER
1165 /* we need to exclude all possible ENOPROTOOPTs except default case */
1166 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1167 !ip_mroute_opt(optname)) {
1170 if (get_user(len,optlen))
1174 err = nf_getsockopt(sk, PF_INET, optname, optval,
1178 err = put_user(len, optlen);
1185 #ifdef CONFIG_COMPAT
1186 int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1187 char __user *optval, int __user *optlen)
1191 if (optname == MCAST_MSFILTER)
1192 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1195 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1197 #ifdef CONFIG_NETFILTER
1198 /* we need to exclude all possible ENOPROTOOPTs except default case */
1199 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1200 !ip_mroute_opt(optname)) {
1203 if (get_user(len, optlen))
1207 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1210 err = put_user(len, optlen);
1217 EXPORT_SYMBOL(compat_ip_getsockopt);
1220 EXPORT_SYMBOL(ip_cmsg_recv);
1222 EXPORT_SYMBOL(ip_getsockopt);
1223 EXPORT_SYMBOL(ip_setsockopt);