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 = 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 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 = 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 #ifdef CONFIG_IP_MROUTE
441 if (optname >= MRT_BASE && optname <= (MRT_BASE + 10))
442 return ip_mroute_setsockopt(sk,optname,optval,optlen);
451 struct ip_options * opt = NULL;
452 if (optlen > 40 || optlen < 0)
454 err = ip_options_get_from_user(&opt, optval, optlen);
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 (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
520 !capable(CAP_NET_ADMIN)) {
524 if (inet->tos != val) {
526 sk->sk_priority = rt_tos2priority(val);
533 if (val != -1 && (val < 1 || val>255))
538 if (sk->sk_type != SOCK_RAW) {
542 inet->hdrincl = val ? 1 : 0;
544 case IP_MTU_DISCOVER:
547 inet->pmtudisc = val;
550 inet->recverr = !!val;
552 skb_queue_purge(&sk->sk_error_queue);
554 case IP_MULTICAST_TTL:
555 if (sk->sk_type == SOCK_STREAM)
561 if (val < 0 || val > 255)
565 case IP_MULTICAST_LOOP:
568 inet->mc_loop = !!val;
570 case IP_MULTICAST_IF:
572 struct ip_mreqn mreq;
573 struct net_device *dev = NULL;
575 if (sk->sk_type == SOCK_STREAM)
578 * Check the arguments are allowable
582 if (optlen >= sizeof(struct ip_mreqn)) {
583 if (copy_from_user(&mreq,optval,sizeof(mreq)))
586 memset(&mreq, 0, sizeof(mreq));
587 if (optlen >= sizeof(struct in_addr) &&
588 copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
592 if (!mreq.imr_ifindex) {
593 if (mreq.imr_address.s_addr == INADDR_ANY) {
599 dev = ip_dev_find(mreq.imr_address.s_addr);
601 mreq.imr_ifindex = dev->ifindex;
605 dev = __dev_get_by_index(&init_net, mreq.imr_ifindex);
608 err = -EADDRNOTAVAIL;
613 if (sk->sk_bound_dev_if &&
614 mreq.imr_ifindex != sk->sk_bound_dev_if)
617 inet->mc_index = mreq.imr_ifindex;
618 inet->mc_addr = mreq.imr_address.s_addr;
623 case IP_ADD_MEMBERSHIP:
624 case IP_DROP_MEMBERSHIP:
626 struct ip_mreqn mreq;
629 if (inet_sk(sk)->is_icsk)
632 if (optlen < sizeof(struct ip_mreq))
635 if (optlen >= sizeof(struct ip_mreqn)) {
636 if (copy_from_user(&mreq,optval,sizeof(mreq)))
639 memset(&mreq, 0, sizeof(mreq));
640 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
644 if (optname == IP_ADD_MEMBERSHIP)
645 err = ip_mc_join_group(sk, &mreq);
647 err = ip_mc_leave_group(sk, &mreq);
652 extern int sysctl_igmp_max_msf;
653 struct ip_msfilter *msf;
655 if (optlen < IP_MSFILTER_SIZE(0))
657 if (optlen > sysctl_optmem_max) {
661 msf = kmalloc(optlen, GFP_KERNEL);
667 if (copy_from_user(msf, optval, optlen)) {
671 /* numsrc >= (1G-4) overflow in 32 bits */
672 if (msf->imsf_numsrc >= 0x3ffffffcU ||
673 msf->imsf_numsrc > sysctl_igmp_max_msf) {
678 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
683 err = ip_mc_msfilter(sk, msf, 0);
687 case IP_BLOCK_SOURCE:
688 case IP_UNBLOCK_SOURCE:
689 case IP_ADD_SOURCE_MEMBERSHIP:
690 case IP_DROP_SOURCE_MEMBERSHIP:
692 struct ip_mreq_source mreqs;
695 if (optlen != sizeof(struct ip_mreq_source))
697 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
701 if (optname == IP_BLOCK_SOURCE) {
702 omode = MCAST_EXCLUDE;
704 } else if (optname == IP_UNBLOCK_SOURCE) {
705 omode = MCAST_EXCLUDE;
707 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
708 struct ip_mreqn mreq;
710 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
711 mreq.imr_address.s_addr = mreqs.imr_interface;
712 mreq.imr_ifindex = 0;
713 err = ip_mc_join_group(sk, &mreq);
714 if (err && err != -EADDRINUSE)
716 omode = MCAST_INCLUDE;
718 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
719 omode = MCAST_INCLUDE;
722 err = ip_mc_source(add, omode, sk, &mreqs, 0);
725 case MCAST_JOIN_GROUP:
726 case MCAST_LEAVE_GROUP:
728 struct group_req greq;
729 struct sockaddr_in *psin;
730 struct ip_mreqn mreq;
732 if (optlen < sizeof(struct group_req))
735 if (copy_from_user(&greq, optval, sizeof(greq)))
737 psin = (struct sockaddr_in *)&greq.gr_group;
738 if (psin->sin_family != AF_INET)
740 memset(&mreq, 0, sizeof(mreq));
741 mreq.imr_multiaddr = psin->sin_addr;
742 mreq.imr_ifindex = greq.gr_interface;
744 if (optname == MCAST_JOIN_GROUP)
745 err = ip_mc_join_group(sk, &mreq);
747 err = ip_mc_leave_group(sk, &mreq);
750 case MCAST_JOIN_SOURCE_GROUP:
751 case MCAST_LEAVE_SOURCE_GROUP:
752 case MCAST_BLOCK_SOURCE:
753 case MCAST_UNBLOCK_SOURCE:
755 struct group_source_req greqs;
756 struct ip_mreq_source mreqs;
757 struct sockaddr_in *psin;
760 if (optlen != sizeof(struct group_source_req))
762 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
766 if (greqs.gsr_group.ss_family != AF_INET ||
767 greqs.gsr_source.ss_family != AF_INET) {
768 err = -EADDRNOTAVAIL;
771 psin = (struct sockaddr_in *)&greqs.gsr_group;
772 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
773 psin = (struct sockaddr_in *)&greqs.gsr_source;
774 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
775 mreqs.imr_interface = 0; /* use index for mc_source */
777 if (optname == MCAST_BLOCK_SOURCE) {
778 omode = MCAST_EXCLUDE;
780 } else if (optname == MCAST_UNBLOCK_SOURCE) {
781 omode = MCAST_EXCLUDE;
783 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
784 struct ip_mreqn mreq;
786 psin = (struct sockaddr_in *)&greqs.gsr_group;
787 mreq.imr_multiaddr = psin->sin_addr;
788 mreq.imr_address.s_addr = 0;
789 mreq.imr_ifindex = greqs.gsr_interface;
790 err = ip_mc_join_group(sk, &mreq);
791 if (err && err != -EADDRINUSE)
793 greqs.gsr_interface = mreq.imr_ifindex;
794 omode = MCAST_INCLUDE;
796 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
797 omode = MCAST_INCLUDE;
800 err = ip_mc_source(add, omode, sk, &mreqs,
801 greqs.gsr_interface);
806 extern int sysctl_igmp_max_msf;
807 struct sockaddr_in *psin;
808 struct ip_msfilter *msf = NULL;
809 struct group_filter *gsf = NULL;
810 int msize, i, ifindex;
812 if (optlen < GROUP_FILTER_SIZE(0))
814 if (optlen > sysctl_optmem_max) {
818 gsf = kmalloc(optlen,GFP_KERNEL);
824 if (copy_from_user(gsf, optval, optlen)) {
827 /* numsrc >= (4G-140)/128 overflow in 32 bits */
828 if (gsf->gf_numsrc >= 0x1ffffff ||
829 gsf->gf_numsrc > sysctl_igmp_max_msf) {
833 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
837 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
838 msf = kmalloc(msize,GFP_KERNEL);
843 ifindex = gsf->gf_interface;
844 psin = (struct sockaddr_in *)&gsf->gf_group;
845 if (psin->sin_family != AF_INET) {
846 err = -EADDRNOTAVAIL;
849 msf->imsf_multiaddr = psin->sin_addr.s_addr;
850 msf->imsf_interface = 0;
851 msf->imsf_fmode = gsf->gf_fmode;
852 msf->imsf_numsrc = gsf->gf_numsrc;
853 err = -EADDRNOTAVAIL;
854 for (i=0; i<gsf->gf_numsrc; ++i) {
855 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
857 if (psin->sin_family != AF_INET)
859 msf->imsf_slist[i] = psin->sin_addr.s_addr;
864 err = ip_mc_msfilter(sk, msf, ifindex);
870 case IP_ROUTER_ALERT:
871 err = ip_ra_control(sk, val ? 1 : 0, NULL);
877 inet->freebind = !!val;
880 case IP_IPSEC_POLICY:
883 if (!capable(CAP_NET_ADMIN))
885 err = xfrm_user_policy(sk, optname, optval, optlen);
900 int ip_setsockopt(struct sock *sk, int level,
901 int optname, char __user *optval, int optlen)
908 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
909 #ifdef CONFIG_NETFILTER
910 /* we need to exclude all possible ENOPROTOOPTs except default case */
911 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
912 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
913 #ifdef CONFIG_IP_MROUTE
914 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
918 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
926 int compat_ip_setsockopt(struct sock *sk, int level, int optname,
927 char __user *optval, int optlen)
934 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
935 #ifdef CONFIG_NETFILTER
936 /* we need to exclude all possible ENOPROTOOPTs except default case */
937 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
938 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
939 #ifdef CONFIG_IP_MROUTE
940 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
944 err = compat_nf_setsockopt(sk, PF_INET, optname,
952 EXPORT_SYMBOL(compat_ip_setsockopt);
956 * Get the options. Note for future reference. The GET of IP options gets the
957 * _received_ ones. The set sets the _sent_ ones.
960 static int do_ip_getsockopt(struct sock *sk, int level, int optname,
961 char __user *optval, int __user *optlen)
963 struct inet_sock *inet = inet_sk(sk);
970 #ifdef CONFIG_IP_MROUTE
971 if (optname >= MRT_BASE && optname <= MRT_BASE+10) {
972 return ip_mroute_getsockopt(sk,optname,optval,optlen);
976 if (get_user(len,optlen))
986 unsigned char optbuf[sizeof(struct ip_options)+40];
987 struct ip_options * opt = (struct ip_options*)optbuf;
990 memcpy(optbuf, inet->opt,
991 sizeof(struct ip_options)+
995 if (opt->optlen == 0)
996 return put_user(0, optlen);
998 ip_options_undo(opt);
1000 len = min_t(unsigned int, len, opt->optlen);
1001 if (put_user(len, optlen))
1003 if (copy_to_user(optval, opt->__data, len))
1008 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1011 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1014 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1017 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1020 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1023 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1029 val = (inet->uc_ttl == -1 ?
1030 sysctl_ip_default_ttl :
1034 val = inet->hdrincl;
1036 case IP_MTU_DISCOVER:
1037 val = inet->pmtudisc;
1041 struct dst_entry *dst;
1043 dst = sk_dst_get(sk);
1055 val = inet->recverr;
1057 case IP_MULTICAST_TTL:
1060 case IP_MULTICAST_LOOP:
1061 val = inet->mc_loop;
1063 case IP_MULTICAST_IF:
1065 struct in_addr addr;
1066 len = min_t(unsigned int, len, sizeof(struct in_addr));
1067 addr.s_addr = inet->mc_addr;
1070 if (put_user(len, optlen))
1072 if (copy_to_user(optval, &addr, len))
1078 struct ip_msfilter msf;
1081 if (len < IP_MSFILTER_SIZE(0)) {
1085 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1089 err = ip_mc_msfget(sk, &msf,
1090 (struct ip_msfilter __user *)optval, optlen);
1094 case MCAST_MSFILTER:
1096 struct group_filter gsf;
1099 if (len < GROUP_FILTER_SIZE(0)) {
1103 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
1107 err = ip_mc_gsfget(sk, &gsf,
1108 (struct group_filter __user *)optval, optlen);
1118 if (sk->sk_type != SOCK_STREAM)
1119 return -ENOPROTOOPT;
1121 msg.msg_control = optval;
1122 msg.msg_controllen = len;
1125 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1126 struct in_pktinfo info;
1128 info.ipi_addr.s_addr = inet->rcv_saddr;
1129 info.ipi_spec_dst.s_addr = inet->rcv_saddr;
1130 info.ipi_ifindex = inet->mc_index;
1131 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1133 if (inet->cmsg_flags & IP_CMSG_TTL) {
1134 int hlim = inet->mc_ttl;
1135 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1137 len -= msg.msg_controllen;
1138 return put_user(len, optlen);
1141 val = inet->freebind;
1145 return -ENOPROTOOPT;
1149 if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
1150 unsigned char ucval = (unsigned char)val;
1152 if (put_user(len, optlen))
1154 if (copy_to_user(optval,&ucval,1))
1157 len = min_t(unsigned int, sizeof(int), len);
1158 if (put_user(len, optlen))
1160 if (copy_to_user(optval,&val,len))
1166 int ip_getsockopt(struct sock *sk, int level,
1167 int optname, char __user *optval, int __user *optlen)
1171 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1172 #ifdef CONFIG_NETFILTER
1173 /* we need to exclude all possible ENOPROTOOPTs except default case */
1174 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1175 #ifdef CONFIG_IP_MROUTE
1176 && (optname < MRT_BASE || optname > MRT_BASE+10)
1181 if (get_user(len,optlen))
1185 err = nf_getsockopt(sk, PF_INET, optname, optval,
1189 err = put_user(len, optlen);
1196 #ifdef CONFIG_COMPAT
1197 int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1198 char __user *optval, int __user *optlen)
1200 int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1201 #ifdef CONFIG_NETFILTER
1202 /* we need to exclude all possible ENOPROTOOPTs except default case */
1203 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1204 #ifdef CONFIG_IP_MROUTE
1205 && (optname < MRT_BASE || optname > MRT_BASE+10)
1210 if (get_user(len, optlen))
1214 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1217 err = put_user(len, optlen);
1224 EXPORT_SYMBOL(compat_ip_getsockopt);
1227 EXPORT_SYMBOL(ip_cmsg_recv);
1229 EXPORT_SYMBOL(ip_getsockopt);
1230 EXPORT_SYMBOL(ip_setsockopt);