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 = skb->rtable;
62 info.ipi_addr.s_addr = ip_hdr(skb)->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 = ip_hdr(skb)->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, &ip_hdr(skb)->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,
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 net *net, 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(net, &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 = icmp_hdr(skb)->type;
273 serr->ee.ee_code = icmp_hdr(skb)->code;
275 serr->ee.ee_info = info;
276 serr->ee.ee_data = 0;
277 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
278 skb_network_header(skb);
281 if (skb_pull(skb, payload - skb->data) != NULL) {
282 skb_reset_transport_header(skb);
283 if (sock_queue_err_skb(sk, skb) == 0)
289 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
291 struct inet_sock *inet = inet_sk(sk);
292 struct sock_exterr_skb *serr;
299 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
303 skb_put(skb, sizeof(struct iphdr));
304 skb_reset_network_header(skb);
308 serr = SKB_EXT_ERR(skb);
309 serr->ee.ee_errno = err;
310 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
311 serr->ee.ee_type = 0;
312 serr->ee.ee_code = 0;
314 serr->ee.ee_info = info;
315 serr->ee.ee_data = 0;
316 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
319 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
320 skb_reset_transport_header(skb);
322 if (sock_queue_err_skb(sk, skb))
327 * Handle MSG_ERRQUEUE
329 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
331 struct sock_exterr_skb *serr;
332 struct sk_buff *skb, *skb2;
333 struct sockaddr_in *sin;
335 struct sock_extended_err ee;
336 struct sockaddr_in offender;
342 skb = skb_dequeue(&sk->sk_error_queue);
348 msg->msg_flags |= MSG_TRUNC;
351 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
355 sock_recv_timestamp(msg, sk, skb);
357 serr = SKB_EXT_ERR(skb);
359 sin = (struct sockaddr_in *)msg->msg_name;
361 sin->sin_family = AF_INET;
362 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
364 sin->sin_port = serr->port;
365 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
368 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
369 sin = &errhdr.offender;
370 sin->sin_family = AF_UNSPEC;
371 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
372 struct inet_sock *inet = inet_sk(sk);
374 sin->sin_family = AF_INET;
375 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
377 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
378 if (inet->cmsg_flags)
379 ip_cmsg_recv(msg, skb);
382 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
384 /* Now we could try to dump offended packet options */
386 msg->msg_flags |= MSG_ERRQUEUE;
389 /* Reset and regenerate socket error */
390 spin_lock_bh(&sk->sk_error_queue.lock);
392 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
393 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
394 spin_unlock_bh(&sk->sk_error_queue.lock);
395 sk->sk_error_report(sk);
397 spin_unlock_bh(&sk->sk_error_queue.lock);
407 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
411 static int do_ip_setsockopt(struct sock *sk, int level,
412 int optname, char __user *optval, int optlen)
414 struct inet_sock *inet = inet_sk(sk);
417 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
418 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
419 (1<<IP_RETOPTS) | (1<<IP_TOS) |
420 (1<<IP_TTL) | (1<<IP_HDRINCL) |
421 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
422 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
424 optname == IP_MULTICAST_TTL ||
425 optname == IP_MULTICAST_LOOP) {
426 if (optlen >= sizeof(int)) {
427 if (get_user(val, (int __user *) optval))
429 } else if (optlen >= sizeof(char)) {
432 if (get_user(ucval, (unsigned char __user *) optval))
438 /* If optlen==0, it is equivalent to val == 0 */
440 if (ip_mroute_opt(optname))
441 return ip_mroute_setsockopt(sk,optname,optval,optlen);
449 struct ip_options * opt = NULL;
450 if (optlen > 40 || optlen < 0)
452 err = ip_options_get_from_user(sock_net(sk), &opt,
457 struct inet_connection_sock *icsk = inet_csk(sk);
458 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
459 if (sk->sk_family == PF_INET ||
460 (!((1 << sk->sk_state) &
461 (TCPF_LISTEN | TCPF_CLOSE)) &&
462 inet->daddr != LOOPBACK4_IPV6)) {
465 icsk->icsk_ext_hdr_len -= inet->opt->optlen;
467 icsk->icsk_ext_hdr_len += opt->optlen;
468 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
469 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
473 opt = xchg(&inet->opt, opt);
479 inet->cmsg_flags |= IP_CMSG_PKTINFO;
481 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
485 inet->cmsg_flags |= IP_CMSG_TTL;
487 inet->cmsg_flags &= ~IP_CMSG_TTL;
491 inet->cmsg_flags |= IP_CMSG_TOS;
493 inet->cmsg_flags &= ~IP_CMSG_TOS;
497 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
499 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
503 inet->cmsg_flags |= IP_CMSG_RETOPTS;
505 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
509 inet->cmsg_flags |= IP_CMSG_PASSSEC;
511 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
513 case IP_TOS: /* This sets both TOS and Precedence */
514 if (sk->sk_type == SOCK_STREAM) {
516 val |= inet->tos & 3;
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 == htonl(INADDR_ANY)) {
593 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
595 mreq.imr_ifindex = dev->ifindex;
599 dev = __dev_get_by_index(sock_net(sk), 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;
623 if (inet_sk(sk)->is_icsk)
626 if (optlen < sizeof(struct ip_mreq))
629 if (optlen >= sizeof(struct ip_mreqn)) {
630 if (copy_from_user(&mreq,optval,sizeof(mreq)))
633 memset(&mreq, 0, sizeof(mreq));
634 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
638 if (optname == IP_ADD_MEMBERSHIP)
639 err = ip_mc_join_group(sk, &mreq);
641 err = ip_mc_leave_group(sk, &mreq);
646 extern int sysctl_igmp_max_msf;
647 struct ip_msfilter *msf;
649 if (optlen < IP_MSFILTER_SIZE(0))
651 if (optlen > sysctl_optmem_max) {
655 msf = kmalloc(optlen, GFP_KERNEL);
661 if (copy_from_user(msf, optval, optlen)) {
665 /* numsrc >= (1G-4) overflow in 32 bits */
666 if (msf->imsf_numsrc >= 0x3ffffffcU ||
667 msf->imsf_numsrc > sysctl_igmp_max_msf) {
672 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
677 err = ip_mc_msfilter(sk, msf, 0);
681 case IP_BLOCK_SOURCE:
682 case IP_UNBLOCK_SOURCE:
683 case IP_ADD_SOURCE_MEMBERSHIP:
684 case IP_DROP_SOURCE_MEMBERSHIP:
686 struct ip_mreq_source mreqs;
689 if (optlen != sizeof(struct ip_mreq_source))
691 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
695 if (optname == IP_BLOCK_SOURCE) {
696 omode = MCAST_EXCLUDE;
698 } else if (optname == IP_UNBLOCK_SOURCE) {
699 omode = MCAST_EXCLUDE;
701 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
702 struct ip_mreqn mreq;
704 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
705 mreq.imr_address.s_addr = mreqs.imr_interface;
706 mreq.imr_ifindex = 0;
707 err = ip_mc_join_group(sk, &mreq);
708 if (err && err != -EADDRINUSE)
710 omode = MCAST_INCLUDE;
712 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
713 omode = MCAST_INCLUDE;
716 err = ip_mc_source(add, omode, sk, &mreqs, 0);
719 case MCAST_JOIN_GROUP:
720 case MCAST_LEAVE_GROUP:
722 struct group_req greq;
723 struct sockaddr_in *psin;
724 struct ip_mreqn mreq;
726 if (optlen < sizeof(struct group_req))
729 if (copy_from_user(&greq, optval, sizeof(greq)))
731 psin = (struct sockaddr_in *)&greq.gr_group;
732 if (psin->sin_family != AF_INET)
734 memset(&mreq, 0, sizeof(mreq));
735 mreq.imr_multiaddr = psin->sin_addr;
736 mreq.imr_ifindex = greq.gr_interface;
738 if (optname == MCAST_JOIN_GROUP)
739 err = ip_mc_join_group(sk, &mreq);
741 err = ip_mc_leave_group(sk, &mreq);
744 case MCAST_JOIN_SOURCE_GROUP:
745 case MCAST_LEAVE_SOURCE_GROUP:
746 case MCAST_BLOCK_SOURCE:
747 case MCAST_UNBLOCK_SOURCE:
749 struct group_source_req greqs;
750 struct ip_mreq_source mreqs;
751 struct sockaddr_in *psin;
754 if (optlen != sizeof(struct group_source_req))
756 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
760 if (greqs.gsr_group.ss_family != AF_INET ||
761 greqs.gsr_source.ss_family != AF_INET) {
762 err = -EADDRNOTAVAIL;
765 psin = (struct sockaddr_in *)&greqs.gsr_group;
766 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
767 psin = (struct sockaddr_in *)&greqs.gsr_source;
768 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
769 mreqs.imr_interface = 0; /* use index for mc_source */
771 if (optname == MCAST_BLOCK_SOURCE) {
772 omode = MCAST_EXCLUDE;
774 } else if (optname == MCAST_UNBLOCK_SOURCE) {
775 omode = MCAST_EXCLUDE;
777 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
778 struct ip_mreqn mreq;
780 psin = (struct sockaddr_in *)&greqs.gsr_group;
781 mreq.imr_multiaddr = psin->sin_addr;
782 mreq.imr_address.s_addr = 0;
783 mreq.imr_ifindex = greqs.gsr_interface;
784 err = ip_mc_join_group(sk, &mreq);
785 if (err && err != -EADDRINUSE)
787 greqs.gsr_interface = mreq.imr_ifindex;
788 omode = MCAST_INCLUDE;
790 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
791 omode = MCAST_INCLUDE;
794 err = ip_mc_source(add, omode, sk, &mreqs,
795 greqs.gsr_interface);
800 extern int sysctl_igmp_max_msf;
801 struct sockaddr_in *psin;
802 struct ip_msfilter *msf = NULL;
803 struct group_filter *gsf = NULL;
804 int msize, i, ifindex;
806 if (optlen < GROUP_FILTER_SIZE(0))
808 if (optlen > sysctl_optmem_max) {
812 gsf = kmalloc(optlen,GFP_KERNEL);
818 if (copy_from_user(gsf, optval, optlen)) {
821 /* numsrc >= (4G-140)/128 overflow in 32 bits */
822 if (gsf->gf_numsrc >= 0x1ffffff ||
823 gsf->gf_numsrc > sysctl_igmp_max_msf) {
827 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
831 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
832 msf = kmalloc(msize,GFP_KERNEL);
837 ifindex = gsf->gf_interface;
838 psin = (struct sockaddr_in *)&gsf->gf_group;
839 if (psin->sin_family != AF_INET) {
840 err = -EADDRNOTAVAIL;
843 msf->imsf_multiaddr = psin->sin_addr.s_addr;
844 msf->imsf_interface = 0;
845 msf->imsf_fmode = gsf->gf_fmode;
846 msf->imsf_numsrc = gsf->gf_numsrc;
847 err = -EADDRNOTAVAIL;
848 for (i=0; i<gsf->gf_numsrc; ++i) {
849 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
851 if (psin->sin_family != AF_INET)
853 msf->imsf_slist[i] = psin->sin_addr.s_addr;
858 err = ip_mc_msfilter(sk, msf, ifindex);
864 case IP_ROUTER_ALERT:
865 err = ip_ra_control(sk, val ? 1 : 0, NULL);
871 inet->freebind = !!val;
874 case IP_IPSEC_POLICY:
877 if (!capable(CAP_NET_ADMIN))
879 err = xfrm_user_policy(sk, optname, optval, optlen);
894 int ip_setsockopt(struct sock *sk, int level,
895 int optname, char __user *optval, int optlen)
902 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
903 #ifdef CONFIG_NETFILTER
904 /* we need to exclude all possible ENOPROTOOPTs except default case */
905 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
906 optname != IP_IPSEC_POLICY &&
907 optname != IP_XFRM_POLICY &&
908 !ip_mroute_opt(optname)) {
910 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
918 int compat_ip_setsockopt(struct sock *sk, int level, int optname,
919 char __user *optval, int optlen)
926 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
927 #ifdef CONFIG_NETFILTER
928 /* we need to exclude all possible ENOPROTOOPTs except default case */
929 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
930 optname != IP_IPSEC_POLICY &&
931 optname != IP_XFRM_POLICY &&
932 !ip_mroute_opt(optname)) {
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 if (ip_mroute_opt(optname))
961 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 !ip_mroute_opt(optname)) {
1165 if (get_user(len,optlen))
1169 err = nf_getsockopt(sk, PF_INET, optname, optval,
1173 err = put_user(len, optlen);
1180 #ifdef CONFIG_COMPAT
1181 int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1182 char __user *optval, int __user *optlen)
1184 int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1185 #ifdef CONFIG_NETFILTER
1186 /* we need to exclude all possible ENOPROTOOPTs except default case */
1187 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1188 !ip_mroute_opt(optname)) {
1191 if (get_user(len, optlen))
1195 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1198 err = put_user(len, optlen);
1205 EXPORT_SYMBOL(compat_ip_getsockopt);
1208 EXPORT_SYMBOL(ip_cmsg_recv);
1210 EXPORT_SYMBOL(ip_getsockopt);
1211 EXPORT_SYMBOL(ip_setsockopt);