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 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
40 #include <net/transp_v6.h>
43 #include <linux/errqueue.h>
44 #include <asm/uaccess.h>
46 #define IP_CMSG_PKTINFO 1
49 #define IP_CMSG_RECVOPTS 8
50 #define IP_CMSG_RETOPTS 16
51 #define IP_CMSG_PASSSEC 32
54 * SOL_IP control messages.
57 static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
59 struct in_pktinfo info;
60 struct rtable *rt = (struct rtable *)skb->dst;
62 info.ipi_addr.s_addr = skb->nh.iph->daddr;
64 info.ipi_ifindex = rt->rt_iif;
65 info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
68 info.ipi_spec_dst.s_addr = 0;
71 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
74 static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
76 int ttl = skb->nh.iph->ttl;
77 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
80 static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
82 put_cmsg(msg, SOL_IP, IP_TOS, 1, &skb->nh.iph->tos);
85 static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
87 if (IPCB(skb)->opt.optlen == 0)
90 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, skb->nh.iph+1);
94 static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
96 unsigned char optbuf[sizeof(struct ip_options) + 40];
97 struct ip_options * opt = (struct ip_options*)optbuf;
99 if (IPCB(skb)->opt.optlen == 0)
102 if (ip_options_echo(opt, skb)) {
103 msg->msg_flags |= MSG_CTRUNC;
106 ip_options_undo(opt);
108 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
111 static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
117 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
121 err = security_secid_to_secctx(secid, &secdata, &seclen);
125 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
126 security_release_secctx(secdata, seclen);
130 void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
132 struct inet_sock *inet = inet_sk(skb->sk);
133 unsigned flags = inet->cmsg_flags;
135 /* Ordered by supposed usage frequency */
137 ip_cmsg_recv_pktinfo(msg, skb);
138 if ((flags>>=1) == 0)
142 ip_cmsg_recv_ttl(msg, skb);
143 if ((flags>>=1) == 0)
147 ip_cmsg_recv_tos(msg, skb);
148 if ((flags>>=1) == 0)
152 ip_cmsg_recv_opts(msg, skb);
153 if ((flags>>=1) == 0)
157 ip_cmsg_recv_retopts(msg, skb);
158 if ((flags>>=1) == 0)
162 ip_cmsg_recv_security(msg, skb);
165 int ip_cmsg_send(struct msghdr *msg, struct ipcm_cookie *ipc)
168 struct cmsghdr *cmsg;
170 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
171 if (!CMSG_OK(msg, cmsg))
173 if (cmsg->cmsg_level != SOL_IP)
175 switch (cmsg->cmsg_type) {
177 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
178 err = ip_options_get(&ipc->opt, CMSG_DATA(cmsg), err < 40 ? err : 40);
184 struct in_pktinfo *info;
185 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
187 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
188 ipc->oif = info->ipi_ifindex;
189 ipc->addr = info->ipi_spec_dst.s_addr;
200 /* Special input handler for packets caught by router alert option.
201 They are selected only by protocol field, and then processed likely
202 local ones; but only if someone wants them! Otherwise, router
203 not running rsvpd will kill RSVP.
205 It is user level problem, what it will make with them.
206 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
207 but receiver should be enough clever f.e. to forward mtrace requests,
208 sent to multicast group to reach destination designated router.
210 struct ip_ra_chain *ip_ra_chain;
211 DEFINE_RWLOCK(ip_ra_lock);
213 int ip_ra_control(struct sock *sk, unsigned char on, void (*destructor)(struct sock *))
215 struct ip_ra_chain *ra, *new_ra, **rap;
217 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
220 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
222 write_lock_bh(&ip_ra_lock);
223 for (rap = &ip_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
226 write_unlock_bh(&ip_ra_lock);
231 write_unlock_bh(&ip_ra_lock);
240 if (new_ra == NULL) {
241 write_unlock_bh(&ip_ra_lock);
245 new_ra->destructor = destructor;
250 write_unlock_bh(&ip_ra_lock);
255 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
256 __be16 port, u32 info, u8 *payload)
258 struct inet_sock *inet = inet_sk(sk);
259 struct sock_exterr_skb *serr;
264 skb = skb_clone(skb, GFP_ATOMIC);
268 serr = SKB_EXT_ERR(skb);
269 serr->ee.ee_errno = err;
270 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
271 serr->ee.ee_type = skb->h.icmph->type;
272 serr->ee.ee_code = skb->h.icmph->code;
274 serr->ee.ee_info = info;
275 serr->ee.ee_data = 0;
276 serr->addr_offset = (u8*)&(((struct iphdr*)(skb->h.icmph+1))->daddr) - skb->nh.raw;
279 skb->h.raw = payload;
280 if (!skb_pull(skb, payload - skb->data) ||
281 sock_queue_err_skb(sk, skb))
285 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
287 struct inet_sock *inet = inet_sk(sk);
288 struct sock_exterr_skb *serr;
295 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
299 iph = (struct iphdr*)skb_put(skb, sizeof(struct iphdr));
303 serr = SKB_EXT_ERR(skb);
304 serr->ee.ee_errno = err;
305 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
306 serr->ee.ee_type = 0;
307 serr->ee.ee_code = 0;
309 serr->ee.ee_info = info;
310 serr->ee.ee_data = 0;
311 serr->addr_offset = (u8*)&iph->daddr - skb->nh.raw;
314 skb->h.raw = skb->tail;
315 __skb_pull(skb, skb->tail - skb->data);
317 if (sock_queue_err_skb(sk, skb))
322 * Handle MSG_ERRQUEUE
324 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
326 struct sock_exterr_skb *serr;
327 struct sk_buff *skb, *skb2;
328 struct sockaddr_in *sin;
330 struct sock_extended_err ee;
331 struct sockaddr_in offender;
337 skb = skb_dequeue(&sk->sk_error_queue);
343 msg->msg_flags |= MSG_TRUNC;
346 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
350 sock_recv_timestamp(msg, sk, skb);
352 serr = SKB_EXT_ERR(skb);
354 sin = (struct sockaddr_in *)msg->msg_name;
356 sin->sin_family = AF_INET;
357 sin->sin_addr.s_addr = *(__be32*)(skb->nh.raw + serr->addr_offset);
358 sin->sin_port = serr->port;
359 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
362 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
363 sin = &errhdr.offender;
364 sin->sin_family = AF_UNSPEC;
365 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
366 struct inet_sock *inet = inet_sk(sk);
368 sin->sin_family = AF_INET;
369 sin->sin_addr.s_addr = skb->nh.iph->saddr;
371 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
372 if (inet->cmsg_flags)
373 ip_cmsg_recv(msg, skb);
376 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
378 /* Now we could try to dump offended packet options */
380 msg->msg_flags |= MSG_ERRQUEUE;
383 /* Reset and regenerate socket error */
384 spin_lock_bh(&sk->sk_error_queue.lock);
386 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
387 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
388 spin_unlock_bh(&sk->sk_error_queue.lock);
389 sk->sk_error_report(sk);
391 spin_unlock_bh(&sk->sk_error_queue.lock);
401 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
405 static int do_ip_setsockopt(struct sock *sk, int level,
406 int optname, char __user *optval, int optlen)
408 struct inet_sock *inet = inet_sk(sk);
411 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
412 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
413 (1<<IP_RETOPTS) | (1<<IP_TOS) |
414 (1<<IP_TTL) | (1<<IP_HDRINCL) |
415 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
416 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
418 optname == IP_MULTICAST_TTL ||
419 optname == IP_MULTICAST_LOOP) {
420 if (optlen >= sizeof(int)) {
421 if (get_user(val, (int __user *) optval))
423 } else if (optlen >= sizeof(char)) {
426 if (get_user(ucval, (unsigned char __user *) optval))
432 /* If optlen==0, it is equivalent to val == 0 */
434 #ifdef CONFIG_IP_MROUTE
435 if (optname >= MRT_BASE && optname <= (MRT_BASE + 10))
436 return ip_mroute_setsockopt(sk,optname,optval,optlen);
445 struct ip_options * opt = NULL;
446 if (optlen > 40 || optlen < 0)
448 err = ip_options_get_from_user(&opt, optval, optlen);
452 struct inet_connection_sock *icsk = inet_csk(sk);
453 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
454 if (sk->sk_family == PF_INET ||
455 (!((1 << sk->sk_state) &
456 (TCPF_LISTEN | TCPF_CLOSE)) &&
457 inet->daddr != LOOPBACK4_IPV6)) {
460 icsk->icsk_ext_hdr_len -= inet->opt->optlen;
462 icsk->icsk_ext_hdr_len += opt->optlen;
463 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
464 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
468 opt = xchg(&inet->opt, opt);
474 inet->cmsg_flags |= IP_CMSG_PKTINFO;
476 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
480 inet->cmsg_flags |= IP_CMSG_TTL;
482 inet->cmsg_flags &= ~IP_CMSG_TTL;
486 inet->cmsg_flags |= IP_CMSG_TOS;
488 inet->cmsg_flags &= ~IP_CMSG_TOS;
492 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
494 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
498 inet->cmsg_flags |= IP_CMSG_RETOPTS;
500 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
504 inet->cmsg_flags |= IP_CMSG_PASSSEC;
506 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
508 case IP_TOS: /* This sets both TOS and Precedence */
509 if (sk->sk_type == SOCK_STREAM) {
511 val |= inet->tos & 3;
513 if (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
514 !capable(CAP_NET_ADMIN)) {
518 if (inet->tos != val) {
520 sk->sk_priority = rt_tos2priority(val);
527 if (val != -1 && (val < 1 || val>255))
532 if (sk->sk_type != SOCK_RAW) {
536 inet->hdrincl = val ? 1 : 0;
538 case IP_MTU_DISCOVER:
541 inet->pmtudisc = val;
544 inet->recverr = !!val;
546 skb_queue_purge(&sk->sk_error_queue);
548 case IP_MULTICAST_TTL:
549 if (sk->sk_type == SOCK_STREAM)
555 if (val < 0 || val > 255)
559 case IP_MULTICAST_LOOP:
562 inet->mc_loop = !!val;
564 case IP_MULTICAST_IF:
566 struct ip_mreqn mreq;
567 struct net_device *dev = NULL;
569 if (sk->sk_type == SOCK_STREAM)
572 * Check the arguments are allowable
576 if (optlen >= sizeof(struct ip_mreqn)) {
577 if (copy_from_user(&mreq,optval,sizeof(mreq)))
580 memset(&mreq, 0, sizeof(mreq));
581 if (optlen >= sizeof(struct in_addr) &&
582 copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
586 if (!mreq.imr_ifindex) {
587 if (mreq.imr_address.s_addr == INADDR_ANY) {
593 dev = ip_dev_find(mreq.imr_address.s_addr);
595 mreq.imr_ifindex = dev->ifindex;
599 dev = __dev_get_by_index(mreq.imr_ifindex);
602 err = -EADDRNOTAVAIL;
607 if (sk->sk_bound_dev_if &&
608 mreq.imr_ifindex != sk->sk_bound_dev_if)
611 inet->mc_index = mreq.imr_ifindex;
612 inet->mc_addr = mreq.imr_address.s_addr;
617 case IP_ADD_MEMBERSHIP:
618 case IP_DROP_MEMBERSHIP:
620 struct ip_mreqn mreq;
622 if (optlen < sizeof(struct ip_mreq))
625 if (optlen >= sizeof(struct ip_mreqn)) {
626 if (copy_from_user(&mreq,optval,sizeof(mreq)))
629 memset(&mreq, 0, sizeof(mreq));
630 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
634 if (optname == IP_ADD_MEMBERSHIP)
635 err = ip_mc_join_group(sk, &mreq);
637 err = ip_mc_leave_group(sk, &mreq);
642 extern int sysctl_igmp_max_msf;
643 struct ip_msfilter *msf;
645 if (optlen < IP_MSFILTER_SIZE(0))
647 if (optlen > sysctl_optmem_max) {
651 msf = kmalloc(optlen, GFP_KERNEL);
657 if (copy_from_user(msf, optval, optlen)) {
661 /* numsrc >= (1G-4) overflow in 32 bits */
662 if (msf->imsf_numsrc >= 0x3ffffffcU ||
663 msf->imsf_numsrc > sysctl_igmp_max_msf) {
668 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
673 err = ip_mc_msfilter(sk, msf, 0);
677 case IP_BLOCK_SOURCE:
678 case IP_UNBLOCK_SOURCE:
679 case IP_ADD_SOURCE_MEMBERSHIP:
680 case IP_DROP_SOURCE_MEMBERSHIP:
682 struct ip_mreq_source mreqs;
685 if (optlen != sizeof(struct ip_mreq_source))
687 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
691 if (optname == IP_BLOCK_SOURCE) {
692 omode = MCAST_EXCLUDE;
694 } else if (optname == IP_UNBLOCK_SOURCE) {
695 omode = MCAST_EXCLUDE;
697 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
698 struct ip_mreqn mreq;
700 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
701 mreq.imr_address.s_addr = mreqs.imr_interface;
702 mreq.imr_ifindex = 0;
703 err = ip_mc_join_group(sk, &mreq);
704 if (err && err != -EADDRINUSE)
706 omode = MCAST_INCLUDE;
708 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
709 omode = MCAST_INCLUDE;
712 err = ip_mc_source(add, omode, sk, &mreqs, 0);
715 case MCAST_JOIN_GROUP:
716 case MCAST_LEAVE_GROUP:
718 struct group_req greq;
719 struct sockaddr_in *psin;
720 struct ip_mreqn mreq;
722 if (optlen < sizeof(struct group_req))
725 if (copy_from_user(&greq, optval, sizeof(greq)))
727 psin = (struct sockaddr_in *)&greq.gr_group;
728 if (psin->sin_family != AF_INET)
730 memset(&mreq, 0, sizeof(mreq));
731 mreq.imr_multiaddr = psin->sin_addr;
732 mreq.imr_ifindex = greq.gr_interface;
734 if (optname == MCAST_JOIN_GROUP)
735 err = ip_mc_join_group(sk, &mreq);
737 err = ip_mc_leave_group(sk, &mreq);
740 case MCAST_JOIN_SOURCE_GROUP:
741 case MCAST_LEAVE_SOURCE_GROUP:
742 case MCAST_BLOCK_SOURCE:
743 case MCAST_UNBLOCK_SOURCE:
745 struct group_source_req greqs;
746 struct ip_mreq_source mreqs;
747 struct sockaddr_in *psin;
750 if (optlen != sizeof(struct group_source_req))
752 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
756 if (greqs.gsr_group.ss_family != AF_INET ||
757 greqs.gsr_source.ss_family != AF_INET) {
758 err = -EADDRNOTAVAIL;
761 psin = (struct sockaddr_in *)&greqs.gsr_group;
762 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
763 psin = (struct sockaddr_in *)&greqs.gsr_source;
764 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
765 mreqs.imr_interface = 0; /* use index for mc_source */
767 if (optname == MCAST_BLOCK_SOURCE) {
768 omode = MCAST_EXCLUDE;
770 } else if (optname == MCAST_UNBLOCK_SOURCE) {
771 omode = MCAST_EXCLUDE;
773 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
774 struct ip_mreqn mreq;
776 psin = (struct sockaddr_in *)&greqs.gsr_group;
777 mreq.imr_multiaddr = psin->sin_addr;
778 mreq.imr_address.s_addr = 0;
779 mreq.imr_ifindex = greqs.gsr_interface;
780 err = ip_mc_join_group(sk, &mreq);
781 if (err && err != -EADDRINUSE)
783 greqs.gsr_interface = mreq.imr_ifindex;
784 omode = MCAST_INCLUDE;
786 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
787 omode = MCAST_INCLUDE;
790 err = ip_mc_source(add, omode, sk, &mreqs,
791 greqs.gsr_interface);
796 extern int sysctl_igmp_max_msf;
797 struct sockaddr_in *psin;
798 struct ip_msfilter *msf = NULL;
799 struct group_filter *gsf = NULL;
800 int msize, i, ifindex;
802 if (optlen < GROUP_FILTER_SIZE(0))
804 if (optlen > sysctl_optmem_max) {
808 gsf = kmalloc(optlen,GFP_KERNEL);
814 if (copy_from_user(gsf, optval, optlen)) {
817 /* numsrc >= (4G-140)/128 overflow in 32 bits */
818 if (gsf->gf_numsrc >= 0x1ffffff ||
819 gsf->gf_numsrc > sysctl_igmp_max_msf) {
823 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
827 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
828 msf = kmalloc(msize,GFP_KERNEL);
833 ifindex = gsf->gf_interface;
834 psin = (struct sockaddr_in *)&gsf->gf_group;
835 if (psin->sin_family != AF_INET) {
836 err = -EADDRNOTAVAIL;
839 msf->imsf_multiaddr = psin->sin_addr.s_addr;
840 msf->imsf_interface = 0;
841 msf->imsf_fmode = gsf->gf_fmode;
842 msf->imsf_numsrc = gsf->gf_numsrc;
843 err = -EADDRNOTAVAIL;
844 for (i=0; i<gsf->gf_numsrc; ++i) {
845 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
847 if (psin->sin_family != AF_INET)
849 msf->imsf_slist[i] = psin->sin_addr.s_addr;
854 err = ip_mc_msfilter(sk, msf, ifindex);
860 case IP_ROUTER_ALERT:
861 err = ip_ra_control(sk, val ? 1 : 0, NULL);
867 inet->freebind = !!val;
870 case IP_IPSEC_POLICY:
873 if (!capable(CAP_NET_ADMIN))
875 err = xfrm_user_policy(sk, optname, optval, optlen);
890 int ip_setsockopt(struct sock *sk, int level,
891 int optname, char __user *optval, int optlen)
898 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
899 #ifdef CONFIG_NETFILTER
900 /* we need to exclude all possible ENOPROTOOPTs except default case */
901 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
902 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
903 #ifdef CONFIG_IP_MROUTE
904 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
908 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
916 int compat_ip_setsockopt(struct sock *sk, int level, int optname,
917 char __user *optval, int optlen)
924 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
925 #ifdef CONFIG_NETFILTER
926 /* we need to exclude all possible ENOPROTOOPTs except default case */
927 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
928 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
929 #ifdef CONFIG_IP_MROUTE
930 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
934 err = compat_nf_setsockopt(sk, PF_INET, optname,
942 EXPORT_SYMBOL(compat_ip_setsockopt);
946 * Get the options. Note for future reference. The GET of IP options gets the
947 * _received_ ones. The set sets the _sent_ ones.
950 static int do_ip_getsockopt(struct sock *sk, int level, int optname,
951 char __user *optval, int __user *optlen)
953 struct inet_sock *inet = inet_sk(sk);
960 #ifdef CONFIG_IP_MROUTE
961 if (optname >= MRT_BASE && optname <= MRT_BASE+10) {
962 return ip_mroute_getsockopt(sk,optname,optval,optlen);
966 if (get_user(len,optlen))
976 unsigned char optbuf[sizeof(struct ip_options)+40];
977 struct ip_options * opt = (struct ip_options*)optbuf;
980 memcpy(optbuf, inet->opt,
981 sizeof(struct ip_options)+
985 if (opt->optlen == 0)
986 return put_user(0, optlen);
988 ip_options_undo(opt);
990 len = min_t(unsigned int, len, opt->optlen);
991 if (put_user(len, optlen))
993 if (copy_to_user(optval, opt->__data, len))
998 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1001 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1004 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1007 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1010 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1013 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1019 val = (inet->uc_ttl == -1 ?
1020 sysctl_ip_default_ttl :
1024 val = inet->hdrincl;
1026 case IP_MTU_DISCOVER:
1027 val = inet->pmtudisc;
1031 struct dst_entry *dst;
1033 dst = sk_dst_get(sk);
1045 val = inet->recverr;
1047 case IP_MULTICAST_TTL:
1050 case IP_MULTICAST_LOOP:
1051 val = inet->mc_loop;
1053 case IP_MULTICAST_IF:
1055 struct in_addr addr;
1056 len = min_t(unsigned int, len, sizeof(struct in_addr));
1057 addr.s_addr = inet->mc_addr;
1060 if (put_user(len, optlen))
1062 if (copy_to_user(optval, &addr, len))
1068 struct ip_msfilter msf;
1071 if (len < IP_MSFILTER_SIZE(0)) {
1075 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1079 err = ip_mc_msfget(sk, &msf,
1080 (struct ip_msfilter __user *)optval, optlen);
1084 case MCAST_MSFILTER:
1086 struct group_filter gsf;
1089 if (len < GROUP_FILTER_SIZE(0)) {
1093 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
1097 err = ip_mc_gsfget(sk, &gsf,
1098 (struct group_filter __user *)optval, optlen);
1108 if (sk->sk_type != SOCK_STREAM)
1109 return -ENOPROTOOPT;
1111 msg.msg_control = optval;
1112 msg.msg_controllen = len;
1115 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1116 struct in_pktinfo info;
1118 info.ipi_addr.s_addr = inet->rcv_saddr;
1119 info.ipi_spec_dst.s_addr = inet->rcv_saddr;
1120 info.ipi_ifindex = inet->mc_index;
1121 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1123 if (inet->cmsg_flags & IP_CMSG_TTL) {
1124 int hlim = inet->mc_ttl;
1125 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1127 len -= msg.msg_controllen;
1128 return put_user(len, optlen);
1131 val = inet->freebind;
1135 return -ENOPROTOOPT;
1139 if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
1140 unsigned char ucval = (unsigned char)val;
1142 if (put_user(len, optlen))
1144 if (copy_to_user(optval,&ucval,1))
1147 len = min_t(unsigned int, sizeof(int), len);
1148 if (put_user(len, optlen))
1150 if (copy_to_user(optval,&val,len))
1156 int ip_getsockopt(struct sock *sk, int level,
1157 int optname, char __user *optval, int __user *optlen)
1161 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1162 #ifdef CONFIG_NETFILTER
1163 /* we need to exclude all possible ENOPROTOOPTs except default case */
1164 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1165 #ifdef CONFIG_IP_MROUTE
1166 && (optname < MRT_BASE || optname > MRT_BASE+10)
1171 if (get_user(len,optlen))
1175 err = nf_getsockopt(sk, PF_INET, optname, optval,
1179 err = put_user(len, optlen);
1186 #ifdef CONFIG_COMPAT
1187 int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1188 char __user *optval, int __user *optlen)
1190 int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1191 #ifdef CONFIG_NETFILTER
1192 /* we need to exclude all possible ENOPROTOOPTs except default case */
1193 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1194 #ifdef CONFIG_IP_MROUTE
1195 && (optname < MRT_BASE || optname > MRT_BASE+10)
1200 if (get_user(len, optlen))
1204 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1207 err = put_user(len, optlen);
1214 EXPORT_SYMBOL(compat_ip_getsockopt);
1217 EXPORT_SYMBOL(ip_cmsg_recv);
1219 EXPORT_SYMBOL(ip_getsockopt);
1220 EXPORT_SYMBOL(ip_setsockopt);