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/sched.h>
24 #include <linux/skbuff.h>
26 #include <linux/icmp.h>
27 #include <linux/inetdevice.h>
28 #include <linux/netdevice.h>
32 #include <net/tcp_states.h>
33 #include <linux/udp.h>
34 #include <linux/igmp.h>
35 #include <linux/netfilter.h>
36 #include <linux/route.h>
37 #include <linux/mroute.h>
38 #include <net/route.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 = (struct rtable *)skb->dst;
63 info.ipi_addr.s_addr = skb->nh.iph->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 = skb->nh.iph->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, &skb->nh.iph->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, skb->nh.iph+1);
95 static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
97 unsigned char optbuf[sizeof(struct ip_options) + 40];
98 struct ip_options * opt = (struct ip_options*)optbuf;
100 if (IPCB(skb)->opt.optlen == 0)
103 if (ip_options_echo(opt, skb)) {
104 msg->msg_flags |= MSG_CTRUNC;
107 ip_options_undo(opt);
109 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
112 static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
118 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
122 err = security_secid_to_secctx(secid, &secdata, &seclen);
126 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
127 security_release_secctx(secdata, seclen);
131 void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
133 struct inet_sock *inet = inet_sk(skb->sk);
134 unsigned flags = inet->cmsg_flags;
136 /* Ordered by supposed usage frequency */
138 ip_cmsg_recv_pktinfo(msg, skb);
139 if ((flags>>=1) == 0)
143 ip_cmsg_recv_ttl(msg, skb);
144 if ((flags>>=1) == 0)
148 ip_cmsg_recv_tos(msg, skb);
149 if ((flags>>=1) == 0)
153 ip_cmsg_recv_opts(msg, skb);
154 if ((flags>>=1) == 0)
158 ip_cmsg_recv_retopts(msg, skb);
159 if ((flags>>=1) == 0)
163 ip_cmsg_recv_security(msg, skb);
166 int ip_cmsg_send(struct msghdr *msg, struct ipcm_cookie *ipc)
169 struct cmsghdr *cmsg;
171 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
172 if (!CMSG_OK(msg, cmsg))
174 if (cmsg->cmsg_level != SOL_IP)
176 switch (cmsg->cmsg_type) {
178 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
179 err = ip_options_get(&ipc->opt, CMSG_DATA(cmsg), err < 40 ? err : 40);
185 struct in_pktinfo *info;
186 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
188 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
189 ipc->oif = info->ipi_ifindex;
190 ipc->addr = info->ipi_spec_dst.s_addr;
201 /* Special input handler for packets caught by router alert option.
202 They are selected only by protocol field, and then processed likely
203 local ones; but only if someone wants them! Otherwise, router
204 not running rsvpd will kill RSVP.
206 It is user level problem, what it will make with them.
207 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
208 but receiver should be enough clever f.e. to forward mtrace requests,
209 sent to multicast group to reach destination designated router.
211 struct ip_ra_chain *ip_ra_chain;
212 DEFINE_RWLOCK(ip_ra_lock);
214 int ip_ra_control(struct sock *sk, unsigned char on, void (*destructor)(struct sock *))
216 struct ip_ra_chain *ra, *new_ra, **rap;
218 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
221 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
223 write_lock_bh(&ip_ra_lock);
224 for (rap = &ip_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
227 write_unlock_bh(&ip_ra_lock);
232 write_unlock_bh(&ip_ra_lock);
241 if (new_ra == NULL) {
242 write_unlock_bh(&ip_ra_lock);
246 new_ra->destructor = destructor;
251 write_unlock_bh(&ip_ra_lock);
256 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
257 __be16 port, u32 info, u8 *payload)
259 struct inet_sock *inet = inet_sk(sk);
260 struct sock_exterr_skb *serr;
265 skb = skb_clone(skb, GFP_ATOMIC);
269 serr = SKB_EXT_ERR(skb);
270 serr->ee.ee_errno = err;
271 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
272 serr->ee.ee_type = skb->h.icmph->type;
273 serr->ee.ee_code = skb->h.icmph->code;
275 serr->ee.ee_info = info;
276 serr->ee.ee_data = 0;
277 serr->addr_offset = (u8*)&(((struct iphdr*)(skb->h.icmph+1))->daddr) - skb->nh.raw;
280 skb->h.raw = payload;
281 if (!skb_pull(skb, payload - skb->data) ||
282 sock_queue_err_skb(sk, skb))
286 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
288 struct inet_sock *inet = inet_sk(sk);
289 struct sock_exterr_skb *serr;
296 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
300 iph = (struct iphdr*)skb_put(skb, sizeof(struct iphdr));
304 serr = SKB_EXT_ERR(skb);
305 serr->ee.ee_errno = err;
306 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
307 serr->ee.ee_type = 0;
308 serr->ee.ee_code = 0;
310 serr->ee.ee_info = info;
311 serr->ee.ee_data = 0;
312 serr->addr_offset = (u8*)&iph->daddr - skb->nh.raw;
315 skb->h.raw = skb->tail;
316 __skb_pull(skb, skb->tail - skb->data);
318 if (sock_queue_err_skb(sk, skb))
323 * Handle MSG_ERRQUEUE
325 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
327 struct sock_exterr_skb *serr;
328 struct sk_buff *skb, *skb2;
329 struct sockaddr_in *sin;
331 struct sock_extended_err ee;
332 struct sockaddr_in offender;
338 skb = skb_dequeue(&sk->sk_error_queue);
344 msg->msg_flags |= MSG_TRUNC;
347 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
351 sock_recv_timestamp(msg, sk, skb);
353 serr = SKB_EXT_ERR(skb);
355 sin = (struct sockaddr_in *)msg->msg_name;
357 sin->sin_family = AF_INET;
358 sin->sin_addr.s_addr = *(__be32*)(skb->nh.raw + serr->addr_offset);
359 sin->sin_port = serr->port;
360 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
363 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
364 sin = &errhdr.offender;
365 sin->sin_family = AF_UNSPEC;
366 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
367 struct inet_sock *inet = inet_sk(sk);
369 sin->sin_family = AF_INET;
370 sin->sin_addr.s_addr = skb->nh.iph->saddr;
372 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
373 if (inet->cmsg_flags)
374 ip_cmsg_recv(msg, skb);
377 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
379 /* Now we could try to dump offended packet options */
381 msg->msg_flags |= MSG_ERRQUEUE;
384 /* Reset and regenerate socket error */
385 spin_lock_bh(&sk->sk_error_queue.lock);
387 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
388 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
389 spin_unlock_bh(&sk->sk_error_queue.lock);
390 sk->sk_error_report(sk);
392 spin_unlock_bh(&sk->sk_error_queue.lock);
402 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
406 static int do_ip_setsockopt(struct sock *sk, int level,
407 int optname, char __user *optval, int optlen)
409 struct inet_sock *inet = inet_sk(sk);
412 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
413 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
414 (1<<IP_RETOPTS) | (1<<IP_TOS) |
415 (1<<IP_TTL) | (1<<IP_HDRINCL) |
416 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
417 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
419 optname == IP_MULTICAST_TTL ||
420 optname == IP_MULTICAST_LOOP) {
421 if (optlen >= sizeof(int)) {
422 if (get_user(val, (int __user *) optval))
424 } else if (optlen >= sizeof(char)) {
427 if (get_user(ucval, (unsigned char __user *) optval))
433 /* If optlen==0, it is equivalent to val == 0 */
435 #ifdef CONFIG_IP_MROUTE
436 if (optname >= MRT_BASE && optname <= (MRT_BASE + 10))
437 return ip_mroute_setsockopt(sk,optname,optval,optlen);
446 struct ip_options * opt = NULL;
447 if (optlen > 40 || optlen < 0)
449 err = ip_options_get_from_user(&opt, optval, optlen);
453 struct inet_connection_sock *icsk = inet_csk(sk);
454 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
455 if (sk->sk_family == PF_INET ||
456 (!((1 << sk->sk_state) &
457 (TCPF_LISTEN | TCPF_CLOSE)) &&
458 inet->daddr != LOOPBACK4_IPV6)) {
461 icsk->icsk_ext_hdr_len -= inet->opt->optlen;
463 icsk->icsk_ext_hdr_len += opt->optlen;
464 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
465 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
469 opt = xchg(&inet->opt, opt);
475 inet->cmsg_flags |= IP_CMSG_PKTINFO;
477 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
481 inet->cmsg_flags |= IP_CMSG_TTL;
483 inet->cmsg_flags &= ~IP_CMSG_TTL;
487 inet->cmsg_flags |= IP_CMSG_TOS;
489 inet->cmsg_flags &= ~IP_CMSG_TOS;
493 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
495 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
499 inet->cmsg_flags |= IP_CMSG_RETOPTS;
501 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
505 inet->cmsg_flags |= IP_CMSG_PASSSEC;
507 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
509 case IP_TOS: /* This sets both TOS and Precedence */
510 if (sk->sk_type == SOCK_STREAM) {
512 val |= inet->tos & 3;
514 if (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
515 !capable(CAP_NET_ADMIN)) {
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 == INADDR_ANY) {
594 dev = ip_dev_find(mreq.imr_address.s_addr);
596 mreq.imr_ifindex = dev->ifindex;
600 dev = __dev_get_by_index(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;
623 if (optlen < sizeof(struct ip_mreq))
626 if (optlen >= sizeof(struct ip_mreqn)) {
627 if(copy_from_user(&mreq,optval,sizeof(mreq)))
630 memset(&mreq, 0, sizeof(mreq));
631 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
635 if (optname == IP_ADD_MEMBERSHIP)
636 err = ip_mc_join_group(sk, &mreq);
638 err = ip_mc_leave_group(sk, &mreq);
643 extern int sysctl_igmp_max_msf;
644 struct ip_msfilter *msf;
646 if (optlen < IP_MSFILTER_SIZE(0))
648 if (optlen > sysctl_optmem_max) {
652 msf = kmalloc(optlen, GFP_KERNEL);
658 if (copy_from_user(msf, optval, optlen)) {
662 /* numsrc >= (1G-4) overflow in 32 bits */
663 if (msf->imsf_numsrc >= 0x3ffffffcU ||
664 msf->imsf_numsrc > sysctl_igmp_max_msf) {
669 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
674 err = ip_mc_msfilter(sk, msf, 0);
678 case IP_BLOCK_SOURCE:
679 case IP_UNBLOCK_SOURCE:
680 case IP_ADD_SOURCE_MEMBERSHIP:
681 case IP_DROP_SOURCE_MEMBERSHIP:
683 struct ip_mreq_source mreqs;
686 if (optlen != sizeof(struct ip_mreq_source))
688 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
692 if (optname == IP_BLOCK_SOURCE) {
693 omode = MCAST_EXCLUDE;
695 } else if (optname == IP_UNBLOCK_SOURCE) {
696 omode = MCAST_EXCLUDE;
698 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
699 struct ip_mreqn mreq;
701 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
702 mreq.imr_address.s_addr = mreqs.imr_interface;
703 mreq.imr_ifindex = 0;
704 err = ip_mc_join_group(sk, &mreq);
705 if (err && err != -EADDRINUSE)
707 omode = MCAST_INCLUDE;
709 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
710 omode = MCAST_INCLUDE;
713 err = ip_mc_source(add, omode, sk, &mreqs, 0);
716 case MCAST_JOIN_GROUP:
717 case MCAST_LEAVE_GROUP:
719 struct group_req greq;
720 struct sockaddr_in *psin;
721 struct ip_mreqn mreq;
723 if (optlen < sizeof(struct group_req))
726 if(copy_from_user(&greq, optval, sizeof(greq)))
728 psin = (struct sockaddr_in *)&greq.gr_group;
729 if (psin->sin_family != AF_INET)
731 memset(&mreq, 0, sizeof(mreq));
732 mreq.imr_multiaddr = psin->sin_addr;
733 mreq.imr_ifindex = greq.gr_interface;
735 if (optname == MCAST_JOIN_GROUP)
736 err = ip_mc_join_group(sk, &mreq);
738 err = ip_mc_leave_group(sk, &mreq);
741 case MCAST_JOIN_SOURCE_GROUP:
742 case MCAST_LEAVE_SOURCE_GROUP:
743 case MCAST_BLOCK_SOURCE:
744 case MCAST_UNBLOCK_SOURCE:
746 struct group_source_req greqs;
747 struct ip_mreq_source mreqs;
748 struct sockaddr_in *psin;
751 if (optlen != sizeof(struct group_source_req))
753 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
757 if (greqs.gsr_group.ss_family != AF_INET ||
758 greqs.gsr_source.ss_family != AF_INET) {
759 err = -EADDRNOTAVAIL;
762 psin = (struct sockaddr_in *)&greqs.gsr_group;
763 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
764 psin = (struct sockaddr_in *)&greqs.gsr_source;
765 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
766 mreqs.imr_interface = 0; /* use index for mc_source */
768 if (optname == MCAST_BLOCK_SOURCE) {
769 omode = MCAST_EXCLUDE;
771 } else if (optname == MCAST_UNBLOCK_SOURCE) {
772 omode = MCAST_EXCLUDE;
774 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
775 struct ip_mreqn mreq;
777 psin = (struct sockaddr_in *)&greqs.gsr_group;
778 mreq.imr_multiaddr = psin->sin_addr;
779 mreq.imr_address.s_addr = 0;
780 mreq.imr_ifindex = greqs.gsr_interface;
781 err = ip_mc_join_group(sk, &mreq);
782 if (err && err != -EADDRINUSE)
784 greqs.gsr_interface = mreq.imr_ifindex;
785 omode = MCAST_INCLUDE;
787 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
788 omode = MCAST_INCLUDE;
791 err = ip_mc_source(add, omode, sk, &mreqs,
792 greqs.gsr_interface);
797 extern int sysctl_igmp_max_msf;
798 struct sockaddr_in *psin;
799 struct ip_msfilter *msf = NULL;
800 struct group_filter *gsf = NULL;
801 int msize, i, ifindex;
803 if (optlen < GROUP_FILTER_SIZE(0))
805 if (optlen > sysctl_optmem_max) {
809 gsf = kmalloc(optlen,GFP_KERNEL);
815 if (copy_from_user(gsf, optval, optlen)) {
818 /* numsrc >= (4G-140)/128 overflow in 32 bits */
819 if (gsf->gf_numsrc >= 0x1ffffff ||
820 gsf->gf_numsrc > sysctl_igmp_max_msf) {
824 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
828 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
829 msf = kmalloc(msize,GFP_KERNEL);
834 ifindex = gsf->gf_interface;
835 psin = (struct sockaddr_in *)&gsf->gf_group;
836 if (psin->sin_family != AF_INET) {
837 err = -EADDRNOTAVAIL;
840 msf->imsf_multiaddr = psin->sin_addr.s_addr;
841 msf->imsf_interface = 0;
842 msf->imsf_fmode = gsf->gf_fmode;
843 msf->imsf_numsrc = gsf->gf_numsrc;
844 err = -EADDRNOTAVAIL;
845 for (i=0; i<gsf->gf_numsrc; ++i) {
846 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
848 if (psin->sin_family != AF_INET)
850 msf->imsf_slist[i] = psin->sin_addr.s_addr;
855 err = ip_mc_msfilter(sk, msf, ifindex);
861 case IP_ROUTER_ALERT:
862 err = ip_ra_control(sk, val ? 1 : 0, NULL);
868 inet->freebind = !!val;
871 case IP_IPSEC_POLICY:
874 if (!capable(CAP_NET_ADMIN))
876 err = xfrm_user_policy(sk, optname, optval, optlen);
891 int ip_setsockopt(struct sock *sk, int level,
892 int optname, char __user *optval, int optlen)
899 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
900 #ifdef CONFIG_NETFILTER
901 /* we need to exclude all possible ENOPROTOOPTs except default case */
902 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
903 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
904 #ifdef CONFIG_IP_MROUTE
905 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
909 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
917 int compat_ip_setsockopt(struct sock *sk, int level, int optname,
918 char __user *optval, int optlen)
925 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
926 #ifdef CONFIG_NETFILTER
927 /* we need to exclude all possible ENOPROTOOPTs except default case */
928 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
929 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
930 #ifdef CONFIG_IP_MROUTE
931 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
935 err = compat_nf_setsockopt(sk, PF_INET, optname,
943 EXPORT_SYMBOL(compat_ip_setsockopt);
947 * Get the options. Note for future reference. The GET of IP options gets the
948 * _received_ ones. The set sets the _sent_ ones.
951 static int do_ip_getsockopt(struct sock *sk, int level, int optname,
952 char __user *optval, int __user *optlen)
954 struct inet_sock *inet = inet_sk(sk);
961 #ifdef CONFIG_IP_MROUTE
962 if(optname>=MRT_BASE && optname <=MRT_BASE+10)
964 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 #ifdef CONFIG_IP_MROUTE
1168 && (optname < MRT_BASE || optname > MRT_BASE+10)
1173 if(get_user(len,optlen))
1177 err = nf_getsockopt(sk, PF_INET, optname, optval,
1181 err = put_user(len, optlen);
1188 #ifdef CONFIG_COMPAT
1189 int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1190 char __user *optval, int __user *optlen)
1192 int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1193 #ifdef CONFIG_NETFILTER
1194 /* we need to exclude all possible ENOPROTOOPTs except default case */
1195 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1196 #ifdef CONFIG_IP_MROUTE
1197 && (optname < MRT_BASE || optname > MRT_BASE+10)
1202 if (get_user(len, optlen))
1206 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1209 err = put_user(len, optlen);
1216 EXPORT_SYMBOL(compat_ip_getsockopt);
1219 EXPORT_SYMBOL(ip_cmsg_recv);
1221 EXPORT_SYMBOL(ip_getsockopt);
1222 EXPORT_SYMBOL(ip_setsockopt);