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/config.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
24 #include <linux/sched.h>
25 #include <linux/skbuff.h>
27 #include <linux/icmp.h>
28 #include <linux/inetdevice.h>
29 #include <linux/netdevice.h>
33 #include <net/tcp_states.h>
34 #include <linux/udp.h>
35 #include <linux/igmp.h>
36 #include <linux/netfilter.h>
37 #include <linux/route.h>
38 #include <linux/mroute.h>
39 #include <net/route.h>
41 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
42 #include <net/transp_v6.h>
45 #include <linux/errqueue.h>
46 #include <asm/uaccess.h>
48 #define IP_CMSG_PKTINFO 1
51 #define IP_CMSG_RECVOPTS 8
52 #define IP_CMSG_RETOPTS 16
53 #define IP_CMSG_PASSSEC 32
56 * SOL_IP control messages.
59 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
61 struct in_pktinfo info
;
62 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
64 info
.ipi_addr
.s_addr
= skb
->nh
.iph
->daddr
;
66 info
.ipi_ifindex
= rt
->rt_iif
;
67 info
.ipi_spec_dst
.s_addr
= rt
->rt_spec_dst
;
70 info
.ipi_spec_dst
.s_addr
= 0;
73 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
76 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
78 int ttl
= skb
->nh
.iph
->ttl
;
79 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
82 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
84 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &skb
->nh
.iph
->tos
);
87 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
89 if (IPCB(skb
)->opt
.optlen
== 0)
92 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
, skb
->nh
.iph
+1);
96 static void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
98 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
99 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
101 if (IPCB(skb
)->opt
.optlen
== 0)
104 if (ip_options_echo(opt
, skb
)) {
105 msg
->msg_flags
|= MSG_CTRUNC
;
108 ip_options_undo(opt
);
110 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
113 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
119 err
= security_socket_getpeersec_dgram(skb
, &secdata
, &seclen
);
123 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
127 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
129 struct inet_sock
*inet
= inet_sk(skb
->sk
);
130 unsigned flags
= inet
->cmsg_flags
;
132 /* Ordered by supposed usage frequency */
134 ip_cmsg_recv_pktinfo(msg
, skb
);
135 if ((flags
>>=1) == 0)
139 ip_cmsg_recv_ttl(msg
, skb
);
140 if ((flags
>>=1) == 0)
144 ip_cmsg_recv_tos(msg
, skb
);
145 if ((flags
>>=1) == 0)
149 ip_cmsg_recv_opts(msg
, skb
);
150 if ((flags
>>=1) == 0)
154 ip_cmsg_recv_retopts(msg
, skb
);
155 if ((flags
>>=1) == 0)
159 ip_cmsg_recv_security(msg
, skb
);
162 int ip_cmsg_send(struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
165 struct cmsghdr
*cmsg
;
167 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
168 if (!CMSG_OK(msg
, cmsg
))
170 if (cmsg
->cmsg_level
!= SOL_IP
)
172 switch (cmsg
->cmsg_type
) {
174 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
175 err
= ip_options_get(&ipc
->opt
, CMSG_DATA(cmsg
), err
< 40 ? err
: 40);
181 struct in_pktinfo
*info
;
182 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
184 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
185 ipc
->oif
= info
->ipi_ifindex
;
186 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
197 /* Special input handler for packets caught by router alert option.
198 They are selected only by protocol field, and then processed likely
199 local ones; but only if someone wants them! Otherwise, router
200 not running rsvpd will kill RSVP.
202 It is user level problem, what it will make with them.
203 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
204 but receiver should be enough clever f.e. to forward mtrace requests,
205 sent to multicast group to reach destination designated router.
207 struct ip_ra_chain
*ip_ra_chain
;
208 DEFINE_RWLOCK(ip_ra_lock
);
210 int ip_ra_control(struct sock
*sk
, unsigned char on
, void (*destructor
)(struct sock
*))
212 struct ip_ra_chain
*ra
, *new_ra
, **rap
;
214 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->num
== IPPROTO_RAW
)
217 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
219 write_lock_bh(&ip_ra_lock
);
220 for (rap
= &ip_ra_chain
; (ra
=*rap
) != NULL
; rap
= &ra
->next
) {
223 write_unlock_bh(&ip_ra_lock
);
228 write_unlock_bh(&ip_ra_lock
);
237 if (new_ra
== NULL
) {
238 write_unlock_bh(&ip_ra_lock
);
242 new_ra
->destructor
= destructor
;
247 write_unlock_bh(&ip_ra_lock
);
252 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
253 u16 port
, u32 info
, u8
*payload
)
255 struct inet_sock
*inet
= inet_sk(sk
);
256 struct sock_exterr_skb
*serr
;
261 skb
= skb_clone(skb
, GFP_ATOMIC
);
265 serr
= SKB_EXT_ERR(skb
);
266 serr
->ee
.ee_errno
= err
;
267 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
268 serr
->ee
.ee_type
= skb
->h
.icmph
->type
;
269 serr
->ee
.ee_code
= skb
->h
.icmph
->code
;
271 serr
->ee
.ee_info
= info
;
272 serr
->ee
.ee_data
= 0;
273 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(skb
->h
.icmph
+1))->daddr
) - skb
->nh
.raw
;
276 skb
->h
.raw
= payload
;
277 if (!skb_pull(skb
, payload
- skb
->data
) ||
278 sock_queue_err_skb(sk
, skb
))
282 void ip_local_error(struct sock
*sk
, int err
, u32 daddr
, u16 port
, u32 info
)
284 struct inet_sock
*inet
= inet_sk(sk
);
285 struct sock_exterr_skb
*serr
;
292 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
296 iph
= (struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
));
300 serr
= SKB_EXT_ERR(skb
);
301 serr
->ee
.ee_errno
= err
;
302 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
303 serr
->ee
.ee_type
= 0;
304 serr
->ee
.ee_code
= 0;
306 serr
->ee
.ee_info
= info
;
307 serr
->ee
.ee_data
= 0;
308 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb
->nh
.raw
;
311 skb
->h
.raw
= skb
->tail
;
312 __skb_pull(skb
, skb
->tail
- skb
->data
);
314 if (sock_queue_err_skb(sk
, skb
))
319 * Handle MSG_ERRQUEUE
321 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
323 struct sock_exterr_skb
*serr
;
324 struct sk_buff
*skb
, *skb2
;
325 struct sockaddr_in
*sin
;
327 struct sock_extended_err ee
;
328 struct sockaddr_in offender
;
334 skb
= skb_dequeue(&sk
->sk_error_queue
);
340 msg
->msg_flags
|= MSG_TRUNC
;
343 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
347 sock_recv_timestamp(msg
, sk
, skb
);
349 serr
= SKB_EXT_ERR(skb
);
351 sin
= (struct sockaddr_in
*)msg
->msg_name
;
353 sin
->sin_family
= AF_INET
;
354 sin
->sin_addr
.s_addr
= *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
);
355 sin
->sin_port
= serr
->port
;
356 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
359 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
360 sin
= &errhdr
.offender
;
361 sin
->sin_family
= AF_UNSPEC
;
362 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
363 struct inet_sock
*inet
= inet_sk(sk
);
365 sin
->sin_family
= AF_INET
;
366 sin
->sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
368 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
369 if (inet
->cmsg_flags
)
370 ip_cmsg_recv(msg
, skb
);
373 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
375 /* Now we could try to dump offended packet options */
377 msg
->msg_flags
|= MSG_ERRQUEUE
;
380 /* Reset and regenerate socket error */
381 spin_lock_bh(&sk
->sk_error_queue
.lock
);
383 if ((skb2
= skb_peek(&sk
->sk_error_queue
)) != NULL
) {
384 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
385 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
386 sk
->sk_error_report(sk
);
388 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
398 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
402 static int do_ip_setsockopt(struct sock
*sk
, int level
,
403 int optname
, char __user
*optval
, int optlen
)
405 struct inet_sock
*inet
= inet_sk(sk
);
408 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
409 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
410 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
411 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
412 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
413 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
415 optname
== IP_MULTICAST_TTL
||
416 optname
== IP_MULTICAST_LOOP
) {
417 if (optlen
>= sizeof(int)) {
418 if (get_user(val
, (int __user
*) optval
))
420 } else if (optlen
>= sizeof(char)) {
423 if (get_user(ucval
, (unsigned char __user
*) optval
))
429 /* If optlen==0, it is equivalent to val == 0 */
431 #ifdef CONFIG_IP_MROUTE
432 if (optname
>= MRT_BASE
&& optname
<= (MRT_BASE
+ 10))
433 return ip_mroute_setsockopt(sk
,optname
,optval
,optlen
);
442 struct ip_options
* opt
= NULL
;
443 if (optlen
> 40 || optlen
< 0)
445 err
= ip_options_get_from_user(&opt
, optval
, optlen
);
449 struct inet_connection_sock
*icsk
= inet_csk(sk
);
450 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
451 if (sk
->sk_family
== PF_INET
||
452 (!((1 << sk
->sk_state
) &
453 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
454 inet
->daddr
!= LOOPBACK4_IPV6
)) {
457 icsk
->icsk_ext_hdr_len
-= inet
->opt
->optlen
;
459 icsk
->icsk_ext_hdr_len
+= opt
->optlen
;
460 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
461 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
465 opt
= xchg(&inet
->opt
, opt
);
471 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
473 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
477 inet
->cmsg_flags
|= IP_CMSG_TTL
;
479 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
483 inet
->cmsg_flags
|= IP_CMSG_TOS
;
485 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
489 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
491 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
495 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
497 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
501 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
503 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
505 case IP_TOS
: /* This sets both TOS and Precedence */
506 if (sk
->sk_type
== SOCK_STREAM
) {
508 val
|= inet
->tos
& 3;
510 if (IPTOS_PREC(val
) >= IPTOS_PREC_CRITIC_ECP
&&
511 !capable(CAP_NET_ADMIN
)) {
515 if (inet
->tos
!= val
) {
517 sk
->sk_priority
= rt_tos2priority(val
);
524 if (val
!= -1 && (val
< 1 || val
>255))
529 if (sk
->sk_type
!= SOCK_RAW
) {
533 inet
->hdrincl
= val
? 1 : 0;
535 case IP_MTU_DISCOVER
:
538 inet
->pmtudisc
= val
;
541 inet
->recverr
= !!val
;
543 skb_queue_purge(&sk
->sk_error_queue
);
545 case IP_MULTICAST_TTL
:
546 if (sk
->sk_type
== SOCK_STREAM
)
552 if (val
< 0 || val
> 255)
556 case IP_MULTICAST_LOOP
:
559 inet
->mc_loop
= !!val
;
561 case IP_MULTICAST_IF
:
563 struct ip_mreqn mreq
;
564 struct net_device
*dev
= NULL
;
566 if (sk
->sk_type
== SOCK_STREAM
)
569 * Check the arguments are allowable
573 if (optlen
>= sizeof(struct ip_mreqn
)) {
574 if (copy_from_user(&mreq
,optval
,sizeof(mreq
)))
577 memset(&mreq
, 0, sizeof(mreq
));
578 if (optlen
>= sizeof(struct in_addr
) &&
579 copy_from_user(&mreq
.imr_address
,optval
,sizeof(struct in_addr
)))
583 if (!mreq
.imr_ifindex
) {
584 if (mreq
.imr_address
.s_addr
== INADDR_ANY
) {
590 dev
= ip_dev_find(mreq
.imr_address
.s_addr
);
592 mreq
.imr_ifindex
= dev
->ifindex
;
596 dev
= __dev_get_by_index(mreq
.imr_ifindex
);
599 err
= -EADDRNOTAVAIL
;
604 if (sk
->sk_bound_dev_if
&&
605 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
608 inet
->mc_index
= mreq
.imr_ifindex
;
609 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
614 case IP_ADD_MEMBERSHIP
:
615 case IP_DROP_MEMBERSHIP
:
617 struct ip_mreqn mreq
;
619 if (optlen
< sizeof(struct ip_mreq
))
622 if (optlen
>= sizeof(struct ip_mreqn
)) {
623 if(copy_from_user(&mreq
,optval
,sizeof(mreq
)))
626 memset(&mreq
, 0, sizeof(mreq
));
627 if (copy_from_user(&mreq
,optval
,sizeof(struct ip_mreq
)))
631 if (optname
== IP_ADD_MEMBERSHIP
)
632 err
= ip_mc_join_group(sk
, &mreq
);
634 err
= ip_mc_leave_group(sk
, &mreq
);
639 extern int sysctl_igmp_max_msf
;
640 struct ip_msfilter
*msf
;
642 if (optlen
< IP_MSFILTER_SIZE(0))
644 if (optlen
> sysctl_optmem_max
) {
648 msf
= kmalloc(optlen
, GFP_KERNEL
);
654 if (copy_from_user(msf
, optval
, optlen
)) {
658 /* numsrc >= (1G-4) overflow in 32 bits */
659 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
660 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
665 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
670 err
= ip_mc_msfilter(sk
, msf
, 0);
674 case IP_BLOCK_SOURCE
:
675 case IP_UNBLOCK_SOURCE
:
676 case IP_ADD_SOURCE_MEMBERSHIP
:
677 case IP_DROP_SOURCE_MEMBERSHIP
:
679 struct ip_mreq_source mreqs
;
682 if (optlen
!= sizeof(struct ip_mreq_source
))
684 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
688 if (optname
== IP_BLOCK_SOURCE
) {
689 omode
= MCAST_EXCLUDE
;
691 } else if (optname
== IP_UNBLOCK_SOURCE
) {
692 omode
= MCAST_EXCLUDE
;
694 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
695 struct ip_mreqn mreq
;
697 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
698 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
699 mreq
.imr_ifindex
= 0;
700 err
= ip_mc_join_group(sk
, &mreq
);
701 if (err
&& err
!= -EADDRINUSE
)
703 omode
= MCAST_INCLUDE
;
705 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
706 omode
= MCAST_INCLUDE
;
709 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
712 case MCAST_JOIN_GROUP
:
713 case MCAST_LEAVE_GROUP
:
715 struct group_req greq
;
716 struct sockaddr_in
*psin
;
717 struct ip_mreqn mreq
;
719 if (optlen
< sizeof(struct group_req
))
722 if(copy_from_user(&greq
, optval
, sizeof(greq
)))
724 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
725 if (psin
->sin_family
!= AF_INET
)
727 memset(&mreq
, 0, sizeof(mreq
));
728 mreq
.imr_multiaddr
= psin
->sin_addr
;
729 mreq
.imr_ifindex
= greq
.gr_interface
;
731 if (optname
== MCAST_JOIN_GROUP
)
732 err
= ip_mc_join_group(sk
, &mreq
);
734 err
= ip_mc_leave_group(sk
, &mreq
);
737 case MCAST_JOIN_SOURCE_GROUP
:
738 case MCAST_LEAVE_SOURCE_GROUP
:
739 case MCAST_BLOCK_SOURCE
:
740 case MCAST_UNBLOCK_SOURCE
:
742 struct group_source_req greqs
;
743 struct ip_mreq_source mreqs
;
744 struct sockaddr_in
*psin
;
747 if (optlen
!= sizeof(struct group_source_req
))
749 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
753 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
754 greqs
.gsr_source
.ss_family
!= AF_INET
) {
755 err
= -EADDRNOTAVAIL
;
758 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
759 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
760 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
761 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
762 mreqs
.imr_interface
= 0; /* use index for mc_source */
764 if (optname
== MCAST_BLOCK_SOURCE
) {
765 omode
= MCAST_EXCLUDE
;
767 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
768 omode
= MCAST_EXCLUDE
;
770 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
771 struct ip_mreqn mreq
;
773 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
774 mreq
.imr_multiaddr
= psin
->sin_addr
;
775 mreq
.imr_address
.s_addr
= 0;
776 mreq
.imr_ifindex
= greqs
.gsr_interface
;
777 err
= ip_mc_join_group(sk
, &mreq
);
778 if (err
&& err
!= -EADDRINUSE
)
780 greqs
.gsr_interface
= mreq
.imr_ifindex
;
781 omode
= MCAST_INCLUDE
;
783 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
784 omode
= MCAST_INCLUDE
;
787 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
788 greqs
.gsr_interface
);
793 extern int sysctl_igmp_max_msf
;
794 struct sockaddr_in
*psin
;
795 struct ip_msfilter
*msf
= NULL
;
796 struct group_filter
*gsf
= NULL
;
797 int msize
, i
, ifindex
;
799 if (optlen
< GROUP_FILTER_SIZE(0))
801 if (optlen
> sysctl_optmem_max
) {
805 gsf
= kmalloc(optlen
,GFP_KERNEL
);
811 if (copy_from_user(gsf
, optval
, optlen
)) {
814 /* numsrc >= (4G-140)/128 overflow in 32 bits */
815 if (gsf
->gf_numsrc
>= 0x1ffffff ||
816 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
820 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
824 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
825 msf
= kmalloc(msize
,GFP_KERNEL
);
830 ifindex
= gsf
->gf_interface
;
831 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
832 if (psin
->sin_family
!= AF_INET
) {
833 err
= -EADDRNOTAVAIL
;
836 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
837 msf
->imsf_interface
= 0;
838 msf
->imsf_fmode
= gsf
->gf_fmode
;
839 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
840 err
= -EADDRNOTAVAIL
;
841 for (i
=0; i
<gsf
->gf_numsrc
; ++i
) {
842 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
844 if (psin
->sin_family
!= AF_INET
)
846 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
851 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
857 case IP_ROUTER_ALERT
:
858 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
864 inet
->freebind
= !!val
;
867 case IP_IPSEC_POLICY
:
870 if (!capable(CAP_NET_ADMIN
))
872 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
887 int ip_setsockopt(struct sock
*sk
, int level
,
888 int optname
, char __user
*optval
, int optlen
)
895 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
896 #ifdef CONFIG_NETFILTER
897 /* we need to exclude all possible ENOPROTOOPTs except default case */
898 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
899 optname
!= IP_IPSEC_POLICY
&& optname
!= IP_XFRM_POLICY
900 #ifdef CONFIG_IP_MROUTE
901 && (optname
< MRT_BASE
|| optname
> (MRT_BASE
+ 10))
905 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
913 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
914 char __user
*optval
, int optlen
)
921 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
922 #ifdef CONFIG_NETFILTER
923 /* we need to exclude all possible ENOPROTOOPTs except default case */
924 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
925 optname
!= IP_IPSEC_POLICY
&& optname
!= IP_XFRM_POLICY
926 #ifdef CONFIG_IP_MROUTE
927 && (optname
< MRT_BASE
|| optname
> (MRT_BASE
+ 10))
931 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
939 EXPORT_SYMBOL(compat_ip_setsockopt
);
943 * Get the options. Note for future reference. The GET of IP options gets the
944 * _received_ ones. The set sets the _sent_ ones.
947 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
948 char __user
*optval
, int __user
*optlen
)
950 struct inet_sock
*inet
= inet_sk(sk
);
957 #ifdef CONFIG_IP_MROUTE
958 if(optname
>=MRT_BASE
&& optname
<=MRT_BASE
+10)
960 return ip_mroute_getsockopt(sk
,optname
,optval
,optlen
);
964 if(get_user(len
,optlen
))
974 unsigned char optbuf
[sizeof(struct ip_options
)+40];
975 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
978 memcpy(optbuf
, inet
->opt
,
979 sizeof(struct ip_options
)+
983 if (opt
->optlen
== 0)
984 return put_user(0, optlen
);
986 ip_options_undo(opt
);
988 len
= min_t(unsigned int, len
, opt
->optlen
);
989 if(put_user(len
, optlen
))
991 if(copy_to_user(optval
, opt
->__data
, len
))
996 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
999 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1002 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1005 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1008 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1011 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1017 val
= (inet
->uc_ttl
== -1 ?
1018 sysctl_ip_default_ttl
:
1022 val
= inet
->hdrincl
;
1024 case IP_MTU_DISCOVER
:
1025 val
= inet
->pmtudisc
;
1029 struct dst_entry
*dst
;
1031 dst
= sk_dst_get(sk
);
1043 val
= inet
->recverr
;
1045 case IP_MULTICAST_TTL
:
1048 case IP_MULTICAST_LOOP
:
1049 val
= inet
->mc_loop
;
1051 case IP_MULTICAST_IF
:
1053 struct in_addr addr
;
1054 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1055 addr
.s_addr
= inet
->mc_addr
;
1058 if(put_user(len
, optlen
))
1060 if(copy_to_user(optval
, &addr
, len
))
1066 struct ip_msfilter msf
;
1069 if (len
< IP_MSFILTER_SIZE(0)) {
1073 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1077 err
= ip_mc_msfget(sk
, &msf
,
1078 (struct ip_msfilter __user
*)optval
, optlen
);
1082 case MCAST_MSFILTER
:
1084 struct group_filter gsf
;
1087 if (len
< GROUP_FILTER_SIZE(0)) {
1091 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1095 err
= ip_mc_gsfget(sk
, &gsf
,
1096 (struct group_filter __user
*)optval
, optlen
);
1106 if (sk
->sk_type
!= SOCK_STREAM
)
1107 return -ENOPROTOOPT
;
1109 msg
.msg_control
= optval
;
1110 msg
.msg_controllen
= len
;
1113 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1114 struct in_pktinfo info
;
1116 info
.ipi_addr
.s_addr
= inet
->rcv_saddr
;
1117 info
.ipi_spec_dst
.s_addr
= inet
->rcv_saddr
;
1118 info
.ipi_ifindex
= inet
->mc_index
;
1119 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1121 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1122 int hlim
= inet
->mc_ttl
;
1123 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1125 len
-= msg
.msg_controllen
;
1126 return put_user(len
, optlen
);
1129 val
= inet
->freebind
;
1133 return -ENOPROTOOPT
;
1137 if (len
< sizeof(int) && len
> 0 && val
>=0 && val
<255) {
1138 unsigned char ucval
= (unsigned char)val
;
1140 if(put_user(len
, optlen
))
1142 if(copy_to_user(optval
,&ucval
,1))
1145 len
= min_t(unsigned int, sizeof(int), len
);
1146 if(put_user(len
, optlen
))
1148 if(copy_to_user(optval
,&val
,len
))
1154 int ip_getsockopt(struct sock
*sk
, int level
,
1155 int optname
, char __user
*optval
, int __user
*optlen
)
1159 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1160 #ifdef CONFIG_NETFILTER
1161 /* we need to exclude all possible ENOPROTOOPTs except default case */
1162 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
1163 #ifdef CONFIG_IP_MROUTE
1164 && (optname
< MRT_BASE
|| optname
> MRT_BASE
+10)
1169 if(get_user(len
,optlen
))
1173 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1177 err
= put_user(len
, optlen
);
1184 #ifdef CONFIG_COMPAT
1185 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1186 char __user
*optval
, int __user
*optlen
)
1188 int err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1189 #ifdef CONFIG_NETFILTER
1190 /* we need to exclude all possible ENOPROTOOPTs except default case */
1191 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
1192 #ifdef CONFIG_IP_MROUTE
1193 && (optname
< MRT_BASE
|| optname
> MRT_BASE
+10)
1198 if (get_user(len
, optlen
))
1202 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1205 err
= put_user(len
, optlen
);
1212 EXPORT_SYMBOL(compat_ip_getsockopt
);
1215 EXPORT_SYMBOL(ip_cmsg_recv
);
1217 EXPORT_SYMBOL(ip_getsockopt
);
1218 EXPORT_SYMBOL(ip_setsockopt
);