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(skb, &secdata, &seclen);
122 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
126 void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
128 struct inet_sock *inet = inet_sk(skb->sk);
129 unsigned flags = inet->cmsg_flags;
131 /* Ordered by supposed usage frequency */
133 ip_cmsg_recv_pktinfo(msg, skb);
134 if ((flags>>=1) == 0)
138 ip_cmsg_recv_ttl(msg, skb);
139 if ((flags>>=1) == 0)
143 ip_cmsg_recv_tos(msg, skb);
144 if ((flags>>=1) == 0)
148 ip_cmsg_recv_opts(msg, skb);
149 if ((flags>>=1) == 0)
153 ip_cmsg_recv_retopts(msg, skb);
154 if ((flags>>=1) == 0)
158 ip_cmsg_recv_security(msg, skb);
161 int ip_cmsg_send(struct msghdr *msg, struct ipcm_cookie *ipc)
164 struct cmsghdr *cmsg;
166 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
167 if (!CMSG_OK(msg, cmsg))
169 if (cmsg->cmsg_level != SOL_IP)
171 switch (cmsg->cmsg_type) {
173 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
174 err = ip_options_get(&ipc->opt, CMSG_DATA(cmsg), err < 40 ? err : 40);
180 struct in_pktinfo *info;
181 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
183 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
184 ipc->oif = info->ipi_ifindex;
185 ipc->addr = info->ipi_spec_dst.s_addr;
196 /* Special input handler for packets caught by router alert option.
197 They are selected only by protocol field, and then processed likely
198 local ones; but only if someone wants them! Otherwise, router
199 not running rsvpd will kill RSVP.
201 It is user level problem, what it will make with them.
202 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
203 but receiver should be enough clever f.e. to forward mtrace requests,
204 sent to multicast group to reach destination designated router.
206 struct ip_ra_chain *ip_ra_chain;
207 DEFINE_RWLOCK(ip_ra_lock);
209 int ip_ra_control(struct sock *sk, unsigned char on, void (*destructor)(struct sock *))
211 struct ip_ra_chain *ra, *new_ra, **rap;
213 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
216 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
218 write_lock_bh(&ip_ra_lock);
219 for (rap = &ip_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
222 write_unlock_bh(&ip_ra_lock);
227 write_unlock_bh(&ip_ra_lock);
236 if (new_ra == NULL) {
237 write_unlock_bh(&ip_ra_lock);
241 new_ra->destructor = destructor;
246 write_unlock_bh(&ip_ra_lock);
251 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
252 u16 port, u32 info, u8 *payload)
254 struct inet_sock *inet = inet_sk(sk);
255 struct sock_exterr_skb *serr;
260 skb = skb_clone(skb, GFP_ATOMIC);
264 serr = SKB_EXT_ERR(skb);
265 serr->ee.ee_errno = err;
266 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
267 serr->ee.ee_type = skb->h.icmph->type;
268 serr->ee.ee_code = skb->h.icmph->code;
270 serr->ee.ee_info = info;
271 serr->ee.ee_data = 0;
272 serr->addr_offset = (u8*)&(((struct iphdr*)(skb->h.icmph+1))->daddr) - skb->nh.raw;
275 skb->h.raw = payload;
276 if (!skb_pull(skb, payload - skb->data) ||
277 sock_queue_err_skb(sk, skb))
281 void ip_local_error(struct sock *sk, int err, u32 daddr, u16 port, u32 info)
283 struct inet_sock *inet = inet_sk(sk);
284 struct sock_exterr_skb *serr;
291 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
295 iph = (struct iphdr*)skb_put(skb, sizeof(struct iphdr));
299 serr = SKB_EXT_ERR(skb);
300 serr->ee.ee_errno = err;
301 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
302 serr->ee.ee_type = 0;
303 serr->ee.ee_code = 0;
305 serr->ee.ee_info = info;
306 serr->ee.ee_data = 0;
307 serr->addr_offset = (u8*)&iph->daddr - skb->nh.raw;
310 skb->h.raw = skb->tail;
311 __skb_pull(skb, skb->tail - skb->data);
313 if (sock_queue_err_skb(sk, skb))
318 * Handle MSG_ERRQUEUE
320 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
322 struct sock_exterr_skb *serr;
323 struct sk_buff *skb, *skb2;
324 struct sockaddr_in *sin;
326 struct sock_extended_err ee;
327 struct sockaddr_in offender;
333 skb = skb_dequeue(&sk->sk_error_queue);
339 msg->msg_flags |= MSG_TRUNC;
342 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
346 sock_recv_timestamp(msg, sk, skb);
348 serr = SKB_EXT_ERR(skb);
350 sin = (struct sockaddr_in *)msg->msg_name;
352 sin->sin_family = AF_INET;
353 sin->sin_addr.s_addr = *(u32*)(skb->nh.raw + serr->addr_offset);
354 sin->sin_port = serr->port;
355 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
358 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
359 sin = &errhdr.offender;
360 sin->sin_family = AF_UNSPEC;
361 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
362 struct inet_sock *inet = inet_sk(sk);
364 sin->sin_family = AF_INET;
365 sin->sin_addr.s_addr = skb->nh.iph->saddr;
367 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
368 if (inet->cmsg_flags)
369 ip_cmsg_recv(msg, skb);
372 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
374 /* Now we could try to dump offended packet options */
376 msg->msg_flags |= MSG_ERRQUEUE;
379 /* Reset and regenerate socket error */
380 spin_lock_bh(&sk->sk_error_queue.lock);
382 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
383 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
384 spin_unlock_bh(&sk->sk_error_queue.lock);
385 sk->sk_error_report(sk);
387 spin_unlock_bh(&sk->sk_error_queue.lock);
397 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
401 static int do_ip_setsockopt(struct sock *sk, int level,
402 int optname, char __user *optval, int optlen)
404 struct inet_sock *inet = inet_sk(sk);
407 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
408 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
409 (1<<IP_RETOPTS) | (1<<IP_TOS) |
410 (1<<IP_TTL) | (1<<IP_HDRINCL) |
411 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
412 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
414 optname == IP_MULTICAST_TTL ||
415 optname == IP_MULTICAST_LOOP) {
416 if (optlen >= sizeof(int)) {
417 if (get_user(val, (int __user *) optval))
419 } else if (optlen >= sizeof(char)) {
422 if (get_user(ucval, (unsigned char __user *) optval))
428 /* If optlen==0, it is equivalent to val == 0 */
430 #ifdef CONFIG_IP_MROUTE
431 if (optname >= MRT_BASE && optname <= (MRT_BASE + 10))
432 return ip_mroute_setsockopt(sk,optname,optval,optlen);
441 struct ip_options * opt = NULL;
442 if (optlen > 40 || optlen < 0)
444 err = ip_options_get_from_user(&opt, optval, optlen);
448 struct inet_connection_sock *icsk = inet_csk(sk);
449 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
450 if (sk->sk_family == PF_INET ||
451 (!((1 << sk->sk_state) &
452 (TCPF_LISTEN | TCPF_CLOSE)) &&
453 inet->daddr != LOOPBACK4_IPV6)) {
456 icsk->icsk_ext_hdr_len -= inet->opt->optlen;
458 icsk->icsk_ext_hdr_len += opt->optlen;
459 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
460 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
464 opt = xchg(&inet->opt, opt);
470 inet->cmsg_flags |= IP_CMSG_PKTINFO;
472 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
476 inet->cmsg_flags |= IP_CMSG_TTL;
478 inet->cmsg_flags &= ~IP_CMSG_TTL;
482 inet->cmsg_flags |= IP_CMSG_TOS;
484 inet->cmsg_flags &= ~IP_CMSG_TOS;
488 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
490 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
494 inet->cmsg_flags |= IP_CMSG_RETOPTS;
496 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
500 inet->cmsg_flags |= IP_CMSG_PASSSEC;
502 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
504 case IP_TOS: /* This sets both TOS and Precedence */
505 if (sk->sk_type == SOCK_STREAM) {
507 val |= inet->tos & 3;
509 if (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
510 !capable(CAP_NET_ADMIN)) {
514 if (inet->tos != val) {
516 sk->sk_priority = rt_tos2priority(val);
523 if (val != -1 && (val < 1 || val>255))
528 if (sk->sk_type != SOCK_RAW) {
532 inet->hdrincl = val ? 1 : 0;
534 case IP_MTU_DISCOVER:
537 inet->pmtudisc = val;
540 inet->recverr = !!val;
542 skb_queue_purge(&sk->sk_error_queue);
544 case IP_MULTICAST_TTL:
545 if (sk->sk_type == SOCK_STREAM)
551 if (val < 0 || val > 255)
555 case IP_MULTICAST_LOOP:
558 inet->mc_loop = !!val;
560 case IP_MULTICAST_IF:
562 struct ip_mreqn mreq;
563 struct net_device *dev = NULL;
565 if (sk->sk_type == SOCK_STREAM)
568 * Check the arguments are allowable
572 if (optlen >= sizeof(struct ip_mreqn)) {
573 if (copy_from_user(&mreq,optval,sizeof(mreq)))
576 memset(&mreq, 0, sizeof(mreq));
577 if (optlen >= sizeof(struct in_addr) &&
578 copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
582 if (!mreq.imr_ifindex) {
583 if (mreq.imr_address.s_addr == INADDR_ANY) {
589 dev = ip_dev_find(mreq.imr_address.s_addr);
591 mreq.imr_ifindex = dev->ifindex;
595 dev = __dev_get_by_index(mreq.imr_ifindex);
598 err = -EADDRNOTAVAIL;
603 if (sk->sk_bound_dev_if &&
604 mreq.imr_ifindex != sk->sk_bound_dev_if)
607 inet->mc_index = mreq.imr_ifindex;
608 inet->mc_addr = mreq.imr_address.s_addr;
613 case IP_ADD_MEMBERSHIP:
614 case IP_DROP_MEMBERSHIP:
616 struct ip_mreqn mreq;
618 if (optlen < sizeof(struct ip_mreq))
621 if (optlen >= sizeof(struct ip_mreqn)) {
622 if(copy_from_user(&mreq,optval,sizeof(mreq)))
625 memset(&mreq, 0, sizeof(mreq));
626 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
630 if (optname == IP_ADD_MEMBERSHIP)
631 err = ip_mc_join_group(sk, &mreq);
633 err = ip_mc_leave_group(sk, &mreq);
638 extern int sysctl_igmp_max_msf;
639 struct ip_msfilter *msf;
641 if (optlen < IP_MSFILTER_SIZE(0))
643 if (optlen > sysctl_optmem_max) {
647 msf = kmalloc(optlen, GFP_KERNEL);
653 if (copy_from_user(msf, optval, optlen)) {
657 /* numsrc >= (1G-4) overflow in 32 bits */
658 if (msf->imsf_numsrc >= 0x3ffffffcU ||
659 msf->imsf_numsrc > sysctl_igmp_max_msf) {
664 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
669 err = ip_mc_msfilter(sk, msf, 0);
673 case IP_BLOCK_SOURCE:
674 case IP_UNBLOCK_SOURCE:
675 case IP_ADD_SOURCE_MEMBERSHIP:
676 case IP_DROP_SOURCE_MEMBERSHIP:
678 struct ip_mreq_source mreqs;
681 if (optlen != sizeof(struct ip_mreq_source))
683 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
687 if (optname == IP_BLOCK_SOURCE) {
688 omode = MCAST_EXCLUDE;
690 } else if (optname == IP_UNBLOCK_SOURCE) {
691 omode = MCAST_EXCLUDE;
693 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
694 struct ip_mreqn mreq;
696 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
697 mreq.imr_address.s_addr = mreqs.imr_interface;
698 mreq.imr_ifindex = 0;
699 err = ip_mc_join_group(sk, &mreq);
700 if (err && err != -EADDRINUSE)
702 omode = MCAST_INCLUDE;
704 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
705 omode = MCAST_INCLUDE;
708 err = ip_mc_source(add, omode, sk, &mreqs, 0);
711 case MCAST_JOIN_GROUP:
712 case MCAST_LEAVE_GROUP:
714 struct group_req greq;
715 struct sockaddr_in *psin;
716 struct ip_mreqn mreq;
718 if (optlen < sizeof(struct group_req))
721 if(copy_from_user(&greq, optval, sizeof(greq)))
723 psin = (struct sockaddr_in *)&greq.gr_group;
724 if (psin->sin_family != AF_INET)
726 memset(&mreq, 0, sizeof(mreq));
727 mreq.imr_multiaddr = psin->sin_addr;
728 mreq.imr_ifindex = greq.gr_interface;
730 if (optname == MCAST_JOIN_GROUP)
731 err = ip_mc_join_group(sk, &mreq);
733 err = ip_mc_leave_group(sk, &mreq);
736 case MCAST_JOIN_SOURCE_GROUP:
737 case MCAST_LEAVE_SOURCE_GROUP:
738 case MCAST_BLOCK_SOURCE:
739 case MCAST_UNBLOCK_SOURCE:
741 struct group_source_req greqs;
742 struct ip_mreq_source mreqs;
743 struct sockaddr_in *psin;
746 if (optlen != sizeof(struct group_source_req))
748 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
752 if (greqs.gsr_group.ss_family != AF_INET ||
753 greqs.gsr_source.ss_family != AF_INET) {
754 err = -EADDRNOTAVAIL;
757 psin = (struct sockaddr_in *)&greqs.gsr_group;
758 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
759 psin = (struct sockaddr_in *)&greqs.gsr_source;
760 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
761 mreqs.imr_interface = 0; /* use index for mc_source */
763 if (optname == MCAST_BLOCK_SOURCE) {
764 omode = MCAST_EXCLUDE;
766 } else if (optname == MCAST_UNBLOCK_SOURCE) {
767 omode = MCAST_EXCLUDE;
769 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
770 struct ip_mreqn mreq;
772 psin = (struct sockaddr_in *)&greqs.gsr_group;
773 mreq.imr_multiaddr = psin->sin_addr;
774 mreq.imr_address.s_addr = 0;
775 mreq.imr_ifindex = greqs.gsr_interface;
776 err = ip_mc_join_group(sk, &mreq);
777 if (err && err != -EADDRINUSE)
779 greqs.gsr_interface = mreq.imr_ifindex;
780 omode = MCAST_INCLUDE;
782 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
783 omode = MCAST_INCLUDE;
786 err = ip_mc_source(add, omode, sk, &mreqs,
787 greqs.gsr_interface);
792 extern int sysctl_igmp_max_msf;
793 struct sockaddr_in *psin;
794 struct ip_msfilter *msf = NULL;
795 struct group_filter *gsf = NULL;
796 int msize, i, ifindex;
798 if (optlen < GROUP_FILTER_SIZE(0))
800 if (optlen > sysctl_optmem_max) {
804 gsf = kmalloc(optlen,GFP_KERNEL);
810 if (copy_from_user(gsf, optval, optlen)) {
813 /* numsrc >= (4G-140)/128 overflow in 32 bits */
814 if (gsf->gf_numsrc >= 0x1ffffff ||
815 gsf->gf_numsrc > sysctl_igmp_max_msf) {
819 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
823 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
824 msf = kmalloc(msize,GFP_KERNEL);
829 ifindex = gsf->gf_interface;
830 psin = (struct sockaddr_in *)&gsf->gf_group;
831 if (psin->sin_family != AF_INET) {
832 err = -EADDRNOTAVAIL;
835 msf->imsf_multiaddr = psin->sin_addr.s_addr;
836 msf->imsf_interface = 0;
837 msf->imsf_fmode = gsf->gf_fmode;
838 msf->imsf_numsrc = gsf->gf_numsrc;
839 err = -EADDRNOTAVAIL;
840 for (i=0; i<gsf->gf_numsrc; ++i) {
841 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
843 if (psin->sin_family != AF_INET)
845 msf->imsf_slist[i] = psin->sin_addr.s_addr;
850 err = ip_mc_msfilter(sk, msf, ifindex);
856 case IP_ROUTER_ALERT:
857 err = ip_ra_control(sk, val ? 1 : 0, NULL);
863 inet->freebind = !!val;
866 case IP_IPSEC_POLICY:
869 if (!capable(CAP_NET_ADMIN))
871 err = xfrm_user_policy(sk, optname, optval, optlen);
886 int ip_setsockopt(struct sock *sk, int level,
887 int optname, char __user *optval, int optlen)
894 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
895 #ifdef CONFIG_NETFILTER
896 /* we need to exclude all possible ENOPROTOOPTs except default case */
897 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
898 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
899 #ifdef CONFIG_IP_MROUTE
900 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
904 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
912 int compat_ip_setsockopt(struct sock *sk, int level, int optname,
913 char __user *optval, int optlen)
920 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
921 #ifdef CONFIG_NETFILTER
922 /* we need to exclude all possible ENOPROTOOPTs except default case */
923 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
924 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
925 #ifdef CONFIG_IP_MROUTE
926 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
930 err = compat_nf_setsockopt(sk, PF_INET, optname,
938 EXPORT_SYMBOL(compat_ip_setsockopt);
942 * Get the options. Note for future reference. The GET of IP options gets the
943 * _received_ ones. The set sets the _sent_ ones.
946 static int do_ip_getsockopt(struct sock *sk, int level, int optname,
947 char __user *optval, int __user *optlen)
949 struct inet_sock *inet = inet_sk(sk);
956 #ifdef CONFIG_IP_MROUTE
957 if(optname>=MRT_BASE && optname <=MRT_BASE+10)
959 return ip_mroute_getsockopt(sk,optname,optval,optlen);
963 if(get_user(len,optlen))
973 unsigned char optbuf[sizeof(struct ip_options)+40];
974 struct ip_options * opt = (struct ip_options*)optbuf;
977 memcpy(optbuf, inet->opt,
978 sizeof(struct ip_options)+
982 if (opt->optlen == 0)
983 return put_user(0, optlen);
985 ip_options_undo(opt);
987 len = min_t(unsigned int, len, opt->optlen);
988 if(put_user(len, optlen))
990 if(copy_to_user(optval, opt->__data, len))
995 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
998 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1001 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1004 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1007 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1010 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1016 val = (inet->uc_ttl == -1 ?
1017 sysctl_ip_default_ttl :
1021 val = inet->hdrincl;
1023 case IP_MTU_DISCOVER:
1024 val = inet->pmtudisc;
1028 struct dst_entry *dst;
1030 dst = sk_dst_get(sk);
1042 val = inet->recverr;
1044 case IP_MULTICAST_TTL:
1047 case IP_MULTICAST_LOOP:
1048 val = inet->mc_loop;
1050 case IP_MULTICAST_IF:
1052 struct in_addr addr;
1053 len = min_t(unsigned int, len, sizeof(struct in_addr));
1054 addr.s_addr = inet->mc_addr;
1057 if(put_user(len, optlen))
1059 if(copy_to_user(optval, &addr, len))
1065 struct ip_msfilter msf;
1068 if (len < IP_MSFILTER_SIZE(0)) {
1072 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1076 err = ip_mc_msfget(sk, &msf,
1077 (struct ip_msfilter __user *)optval, optlen);
1081 case MCAST_MSFILTER:
1083 struct group_filter gsf;
1086 if (len < GROUP_FILTER_SIZE(0)) {
1090 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
1094 err = ip_mc_gsfget(sk, &gsf,
1095 (struct group_filter __user *)optval, optlen);
1105 if (sk->sk_type != SOCK_STREAM)
1106 return -ENOPROTOOPT;
1108 msg.msg_control = optval;
1109 msg.msg_controllen = len;
1112 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1113 struct in_pktinfo info;
1115 info.ipi_addr.s_addr = inet->rcv_saddr;
1116 info.ipi_spec_dst.s_addr = inet->rcv_saddr;
1117 info.ipi_ifindex = inet->mc_index;
1118 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1120 if (inet->cmsg_flags & IP_CMSG_TTL) {
1121 int hlim = inet->mc_ttl;
1122 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1124 len -= msg.msg_controllen;
1125 return put_user(len, optlen);
1128 val = inet->freebind;
1132 return -ENOPROTOOPT;
1136 if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
1137 unsigned char ucval = (unsigned char)val;
1139 if(put_user(len, optlen))
1141 if(copy_to_user(optval,&ucval,1))
1144 len = min_t(unsigned int, sizeof(int), len);
1145 if(put_user(len, optlen))
1147 if(copy_to_user(optval,&val,len))
1153 int ip_getsockopt(struct sock *sk, int level,
1154 int optname, char __user *optval, int __user *optlen)
1158 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1159 #ifdef CONFIG_NETFILTER
1160 /* we need to exclude all possible ENOPROTOOPTs except default case */
1161 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1162 #ifdef CONFIG_IP_MROUTE
1163 && (optname < MRT_BASE || optname > MRT_BASE+10)
1168 if(get_user(len,optlen))
1172 err = nf_getsockopt(sk, PF_INET, optname, optval,
1176 err = put_user(len, optlen);
1183 #ifdef CONFIG_COMPAT
1184 int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1185 char __user *optval, int __user *optlen)
1187 int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1188 #ifdef CONFIG_NETFILTER
1189 /* we need to exclude all possible ENOPROTOOPTs except default case */
1190 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1191 #ifdef CONFIG_IP_MROUTE
1192 && (optname < MRT_BASE || optname > MRT_BASE+10)
1197 if (get_user(len, optlen))
1201 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1204 err = put_user(len, optlen);
1211 EXPORT_SYMBOL(compat_ip_getsockopt);
1214 EXPORT_SYMBOL(ip_cmsg_recv);
1216 EXPORT_SYMBOL(ip_getsockopt);
1217 EXPORT_SYMBOL(ip_setsockopt);