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.
11 * Many : Split from ip.c , see ip.c for history.
12 * Martin Mares : TOS setting fixed.
13 * Alan Cox : Fixed a couple of oopses in Martin's
15 * Mike McLagan : Routing by source
18 #include <linux/module.h>
19 #include <linux/types.h>
21 #include <linux/skbuff.h>
23 #include <linux/icmp.h>
24 #include <linux/inetdevice.h>
25 #include <linux/netdevice.h>
29 #include <net/tcp_states.h>
30 #include <linux/udp.h>
31 #include <linux/igmp.h>
32 #include <linux/netfilter.h>
33 #include <linux/route.h>
34 #include <linux/mroute.h>
35 #include <net/route.h>
37 #include <net/compat.h>
38 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
39 #include <net/transp_v6.h>
42 #include <linux/errqueue.h>
43 #include <asm/uaccess.h>
45 #define IP_CMSG_PKTINFO 1
48 #define IP_CMSG_RECVOPTS 8
49 #define IP_CMSG_RETOPTS 16
50 #define IP_CMSG_PASSSEC 32
51 #define IP_CMSG_ORIGDSTADDR 64
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
);
130 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
132 struct sockaddr_in sin
;
133 struct iphdr
*iph
= ip_hdr(skb
);
134 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
136 if (skb_transport_offset(skb
) + 4 > skb
->len
)
139 /* All current transport protocols have the port numbers in the
140 * first four bytes of the transport header and this function is
141 * written with this assumption in mind.
144 sin
.sin_family
= AF_INET
;
145 sin
.sin_addr
.s_addr
= iph
->daddr
;
146 sin
.sin_port
= ports
[1];
147 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
149 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
152 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
154 struct inet_sock
*inet
= inet_sk(skb
->sk
);
155 unsigned flags
= inet
->cmsg_flags
;
157 /* Ordered by supposed usage frequency */
159 ip_cmsg_recv_pktinfo(msg
, skb
);
160 if ((flags
>>=1) == 0)
164 ip_cmsg_recv_ttl(msg
, skb
);
165 if ((flags
>>=1) == 0)
169 ip_cmsg_recv_tos(msg
, skb
);
170 if ((flags
>>=1) == 0)
174 ip_cmsg_recv_opts(msg
, skb
);
175 if ((flags
>>=1) == 0)
179 ip_cmsg_recv_retopts(msg
, skb
);
180 if ((flags
>>=1) == 0)
184 ip_cmsg_recv_security(msg
, skb
);
186 if ((flags
>>=1) == 0)
189 ip_cmsg_recv_dstaddr(msg
, skb
);
193 int ip_cmsg_send(struct net
*net
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
196 struct cmsghdr
*cmsg
;
198 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
199 if (!CMSG_OK(msg
, cmsg
))
201 if (cmsg
->cmsg_level
!= SOL_IP
)
203 switch (cmsg
->cmsg_type
) {
205 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
206 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
), err
< 40 ? err
: 40);
212 struct in_pktinfo
*info
;
213 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
215 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
216 ipc
->oif
= info
->ipi_ifindex
;
217 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
228 /* Special input handler for packets caught by router alert option.
229 They are selected only by protocol field, and then processed likely
230 local ones; but only if someone wants them! Otherwise, router
231 not running rsvpd will kill RSVP.
233 It is user level problem, what it will make with them.
234 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
235 but receiver should be enough clever f.e. to forward mtrace requests,
236 sent to multicast group to reach destination designated router.
238 struct ip_ra_chain
*ip_ra_chain
;
239 DEFINE_RWLOCK(ip_ra_lock
);
241 int ip_ra_control(struct sock
*sk
, unsigned char on
, void (*destructor
)(struct sock
*))
243 struct ip_ra_chain
*ra
, *new_ra
, **rap
;
245 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->num
== IPPROTO_RAW
)
248 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
250 write_lock_bh(&ip_ra_lock
);
251 for (rap
= &ip_ra_chain
; (ra
=*rap
) != NULL
; rap
= &ra
->next
) {
254 write_unlock_bh(&ip_ra_lock
);
259 write_unlock_bh(&ip_ra_lock
);
268 if (new_ra
== NULL
) {
269 write_unlock_bh(&ip_ra_lock
);
273 new_ra
->destructor
= destructor
;
278 write_unlock_bh(&ip_ra_lock
);
283 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
284 __be16 port
, u32 info
, u8
*payload
)
286 struct inet_sock
*inet
= inet_sk(sk
);
287 struct sock_exterr_skb
*serr
;
292 skb
= skb_clone(skb
, GFP_ATOMIC
);
296 serr
= SKB_EXT_ERR(skb
);
297 serr
->ee
.ee_errno
= err
;
298 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
299 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
300 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
302 serr
->ee
.ee_info
= info
;
303 serr
->ee
.ee_data
= 0;
304 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
305 skb_network_header(skb
);
308 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
309 skb_reset_transport_header(skb
);
310 if (sock_queue_err_skb(sk
, skb
) == 0)
316 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
318 struct inet_sock
*inet
= inet_sk(sk
);
319 struct sock_exterr_skb
*serr
;
326 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
330 skb_put(skb
, sizeof(struct iphdr
));
331 skb_reset_network_header(skb
);
335 serr
= SKB_EXT_ERR(skb
);
336 serr
->ee
.ee_errno
= err
;
337 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
338 serr
->ee
.ee_type
= 0;
339 serr
->ee
.ee_code
= 0;
341 serr
->ee
.ee_info
= info
;
342 serr
->ee
.ee_data
= 0;
343 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
346 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
347 skb_reset_transport_header(skb
);
349 if (sock_queue_err_skb(sk
, skb
))
354 * Handle MSG_ERRQUEUE
356 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
358 struct sock_exterr_skb
*serr
;
359 struct sk_buff
*skb
, *skb2
;
360 struct sockaddr_in
*sin
;
362 struct sock_extended_err ee
;
363 struct sockaddr_in offender
;
369 skb
= skb_dequeue(&sk
->sk_error_queue
);
375 msg
->msg_flags
|= MSG_TRUNC
;
378 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
382 sock_recv_timestamp(msg
, sk
, skb
);
384 serr
= SKB_EXT_ERR(skb
);
386 sin
= (struct sockaddr_in
*)msg
->msg_name
;
388 sin
->sin_family
= AF_INET
;
389 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
391 sin
->sin_port
= serr
->port
;
392 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
395 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
396 sin
= &errhdr
.offender
;
397 sin
->sin_family
= AF_UNSPEC
;
398 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
399 struct inet_sock
*inet
= inet_sk(sk
);
401 sin
->sin_family
= AF_INET
;
402 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
404 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
405 if (inet
->cmsg_flags
)
406 ip_cmsg_recv(msg
, skb
);
409 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
411 /* Now we could try to dump offended packet options */
413 msg
->msg_flags
|= MSG_ERRQUEUE
;
416 /* Reset and regenerate socket error */
417 spin_lock_bh(&sk
->sk_error_queue
.lock
);
419 if ((skb2
= skb_peek(&sk
->sk_error_queue
)) != NULL
) {
420 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
421 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
422 sk
->sk_error_report(sk
);
424 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
434 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
438 static int do_ip_setsockopt(struct sock
*sk
, int level
,
439 int optname
, char __user
*optval
, int optlen
)
441 struct inet_sock
*inet
= inet_sk(sk
);
444 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
445 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
446 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
447 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
448 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
449 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
450 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
))) ||
451 optname
== IP_MULTICAST_TTL
||
452 optname
== IP_MULTICAST_LOOP
||
453 optname
== IP_RECVORIGDSTADDR
) {
454 if (optlen
>= sizeof(int)) {
455 if (get_user(val
, (int __user
*) optval
))
457 } else if (optlen
>= sizeof(char)) {
460 if (get_user(ucval
, (unsigned char __user
*) optval
))
466 /* If optlen==0, it is equivalent to val == 0 */
468 if (ip_mroute_opt(optname
))
469 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
477 struct ip_options
* opt
= NULL
;
478 if (optlen
> 40 || optlen
< 0)
480 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
485 struct inet_connection_sock
*icsk
= inet_csk(sk
);
486 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
487 if (sk
->sk_family
== PF_INET
||
488 (!((1 << sk
->sk_state
) &
489 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
490 inet
->daddr
!= LOOPBACK4_IPV6
)) {
493 icsk
->icsk_ext_hdr_len
-= inet
->opt
->optlen
;
495 icsk
->icsk_ext_hdr_len
+= opt
->optlen
;
496 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
497 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
501 opt
= xchg(&inet
->opt
, opt
);
507 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
509 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
513 inet
->cmsg_flags
|= IP_CMSG_TTL
;
515 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
519 inet
->cmsg_flags
|= IP_CMSG_TOS
;
521 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
525 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
527 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
531 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
533 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
537 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
539 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
541 case IP_RECVORIGDSTADDR
:
543 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
545 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
547 case IP_TOS
: /* This sets both TOS and Precedence */
548 if (sk
->sk_type
== SOCK_STREAM
) {
550 val
|= inet
->tos
& 3;
552 if (inet
->tos
!= val
) {
554 sk
->sk_priority
= rt_tos2priority(val
);
561 if (val
!= -1 && (val
< 1 || val
>255))
566 if (sk
->sk_type
!= SOCK_RAW
) {
570 inet
->hdrincl
= val
? 1 : 0;
572 case IP_MTU_DISCOVER
:
575 inet
->pmtudisc
= val
;
578 inet
->recverr
= !!val
;
580 skb_queue_purge(&sk
->sk_error_queue
);
582 case IP_MULTICAST_TTL
:
583 if (sk
->sk_type
== SOCK_STREAM
)
589 if (val
< 0 || val
> 255)
593 case IP_MULTICAST_LOOP
:
596 inet
->mc_loop
= !!val
;
598 case IP_MULTICAST_IF
:
600 struct ip_mreqn mreq
;
601 struct net_device
*dev
= NULL
;
603 if (sk
->sk_type
== SOCK_STREAM
)
606 * Check the arguments are allowable
610 if (optlen
>= sizeof(struct ip_mreqn
)) {
611 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
614 memset(&mreq
, 0, sizeof(mreq
));
615 if (optlen
>= sizeof(struct in_addr
) &&
616 copy_from_user(&mreq
.imr_address
, optval
, sizeof(struct in_addr
)))
620 if (!mreq
.imr_ifindex
) {
621 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
627 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
629 mreq
.imr_ifindex
= dev
->ifindex
;
633 dev
= __dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
636 err
= -EADDRNOTAVAIL
;
641 if (sk
->sk_bound_dev_if
&&
642 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
645 inet
->mc_index
= mreq
.imr_ifindex
;
646 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
651 case IP_ADD_MEMBERSHIP
:
652 case IP_DROP_MEMBERSHIP
:
654 struct ip_mreqn mreq
;
657 if (inet_sk(sk
)->is_icsk
)
660 if (optlen
< sizeof(struct ip_mreq
))
663 if (optlen
>= sizeof(struct ip_mreqn
)) {
664 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
667 memset(&mreq
, 0, sizeof(mreq
));
668 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
672 if (optname
== IP_ADD_MEMBERSHIP
)
673 err
= ip_mc_join_group(sk
, &mreq
);
675 err
= ip_mc_leave_group(sk
, &mreq
);
680 extern int sysctl_igmp_max_msf
;
681 struct ip_msfilter
*msf
;
683 if (optlen
< IP_MSFILTER_SIZE(0))
685 if (optlen
> sysctl_optmem_max
) {
689 msf
= kmalloc(optlen
, GFP_KERNEL
);
695 if (copy_from_user(msf
, optval
, optlen
)) {
699 /* numsrc >= (1G-4) overflow in 32 bits */
700 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
701 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
706 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
711 err
= ip_mc_msfilter(sk
, msf
, 0);
715 case IP_BLOCK_SOURCE
:
716 case IP_UNBLOCK_SOURCE
:
717 case IP_ADD_SOURCE_MEMBERSHIP
:
718 case IP_DROP_SOURCE_MEMBERSHIP
:
720 struct ip_mreq_source mreqs
;
723 if (optlen
!= sizeof(struct ip_mreq_source
))
725 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
729 if (optname
== IP_BLOCK_SOURCE
) {
730 omode
= MCAST_EXCLUDE
;
732 } else if (optname
== IP_UNBLOCK_SOURCE
) {
733 omode
= MCAST_EXCLUDE
;
735 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
736 struct ip_mreqn mreq
;
738 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
739 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
740 mreq
.imr_ifindex
= 0;
741 err
= ip_mc_join_group(sk
, &mreq
);
742 if (err
&& err
!= -EADDRINUSE
)
744 omode
= MCAST_INCLUDE
;
746 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
747 omode
= MCAST_INCLUDE
;
750 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
753 case MCAST_JOIN_GROUP
:
754 case MCAST_LEAVE_GROUP
:
756 struct group_req greq
;
757 struct sockaddr_in
*psin
;
758 struct ip_mreqn mreq
;
760 if (optlen
< sizeof(struct group_req
))
763 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
765 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
766 if (psin
->sin_family
!= AF_INET
)
768 memset(&mreq
, 0, sizeof(mreq
));
769 mreq
.imr_multiaddr
= psin
->sin_addr
;
770 mreq
.imr_ifindex
= greq
.gr_interface
;
772 if (optname
== MCAST_JOIN_GROUP
)
773 err
= ip_mc_join_group(sk
, &mreq
);
775 err
= ip_mc_leave_group(sk
, &mreq
);
778 case MCAST_JOIN_SOURCE_GROUP
:
779 case MCAST_LEAVE_SOURCE_GROUP
:
780 case MCAST_BLOCK_SOURCE
:
781 case MCAST_UNBLOCK_SOURCE
:
783 struct group_source_req greqs
;
784 struct ip_mreq_source mreqs
;
785 struct sockaddr_in
*psin
;
788 if (optlen
!= sizeof(struct group_source_req
))
790 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
794 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
795 greqs
.gsr_source
.ss_family
!= AF_INET
) {
796 err
= -EADDRNOTAVAIL
;
799 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
800 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
801 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
802 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
803 mreqs
.imr_interface
= 0; /* use index for mc_source */
805 if (optname
== MCAST_BLOCK_SOURCE
) {
806 omode
= MCAST_EXCLUDE
;
808 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
809 omode
= MCAST_EXCLUDE
;
811 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
812 struct ip_mreqn mreq
;
814 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
815 mreq
.imr_multiaddr
= psin
->sin_addr
;
816 mreq
.imr_address
.s_addr
= 0;
817 mreq
.imr_ifindex
= greqs
.gsr_interface
;
818 err
= ip_mc_join_group(sk
, &mreq
);
819 if (err
&& err
!= -EADDRINUSE
)
821 greqs
.gsr_interface
= mreq
.imr_ifindex
;
822 omode
= MCAST_INCLUDE
;
824 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
825 omode
= MCAST_INCLUDE
;
828 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
829 greqs
.gsr_interface
);
834 extern int sysctl_igmp_max_msf
;
835 struct sockaddr_in
*psin
;
836 struct ip_msfilter
*msf
= NULL
;
837 struct group_filter
*gsf
= NULL
;
838 int msize
, i
, ifindex
;
840 if (optlen
< GROUP_FILTER_SIZE(0))
842 if (optlen
> sysctl_optmem_max
) {
846 gsf
= kmalloc(optlen
, GFP_KERNEL
);
852 if (copy_from_user(gsf
, optval
, optlen
)) {
855 /* numsrc >= (4G-140)/128 overflow in 32 bits */
856 if (gsf
->gf_numsrc
>= 0x1ffffff ||
857 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
861 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
865 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
866 msf
= kmalloc(msize
, GFP_KERNEL
);
871 ifindex
= gsf
->gf_interface
;
872 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
873 if (psin
->sin_family
!= AF_INET
) {
874 err
= -EADDRNOTAVAIL
;
877 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
878 msf
->imsf_interface
= 0;
879 msf
->imsf_fmode
= gsf
->gf_fmode
;
880 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
881 err
= -EADDRNOTAVAIL
;
882 for (i
=0; i
<gsf
->gf_numsrc
; ++i
) {
883 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
885 if (psin
->sin_family
!= AF_INET
)
887 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
892 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
898 case IP_ROUTER_ALERT
:
899 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
905 inet
->freebind
= !!val
;
908 case IP_IPSEC_POLICY
:
911 if (!capable(CAP_NET_ADMIN
))
913 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
917 if (!capable(CAP_NET_ADMIN
)) {
923 inet
->transparent
= !!val
;
938 int ip_setsockopt(struct sock
*sk
, int level
,
939 int optname
, char __user
*optval
, int optlen
)
946 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
947 #ifdef CONFIG_NETFILTER
948 /* we need to exclude all possible ENOPROTOOPTs except default case */
949 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
950 optname
!= IP_IPSEC_POLICY
&&
951 optname
!= IP_XFRM_POLICY
&&
952 !ip_mroute_opt(optname
)) {
954 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
962 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
963 char __user
*optval
, int optlen
)
970 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
971 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
974 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
975 #ifdef CONFIG_NETFILTER
976 /* we need to exclude all possible ENOPROTOOPTs except default case */
977 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
978 optname
!= IP_IPSEC_POLICY
&&
979 optname
!= IP_XFRM_POLICY
&&
980 !ip_mroute_opt(optname
)) {
982 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
990 EXPORT_SYMBOL(compat_ip_setsockopt
);
994 * Get the options. Note for future reference. The GET of IP options gets the
995 * _received_ ones. The set sets the _sent_ ones.
998 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
999 char __user
*optval
, int __user
*optlen
)
1001 struct inet_sock
*inet
= inet_sk(sk
);
1005 if (level
!= SOL_IP
)
1008 if (ip_mroute_opt(optname
))
1009 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1011 if (get_user(len
, optlen
))
1021 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1022 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
1025 memcpy(optbuf
, inet
->opt
,
1026 sizeof(struct ip_options
)+
1030 if (opt
->optlen
== 0)
1031 return put_user(0, optlen
);
1033 ip_options_undo(opt
);
1035 len
= min_t(unsigned int, len
, opt
->optlen
);
1036 if (put_user(len
, optlen
))
1038 if (copy_to_user(optval
, opt
->__data
, len
))
1043 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1046 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1049 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1052 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1055 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1058 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1060 case IP_RECVORIGDSTADDR
:
1061 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1067 val
= (inet
->uc_ttl
== -1 ?
1068 sysctl_ip_default_ttl
:
1072 val
= inet
->hdrincl
;
1074 case IP_MTU_DISCOVER
:
1075 val
= inet
->pmtudisc
;
1079 struct dst_entry
*dst
;
1081 dst
= sk_dst_get(sk
);
1093 val
= inet
->recverr
;
1095 case IP_MULTICAST_TTL
:
1098 case IP_MULTICAST_LOOP
:
1099 val
= inet
->mc_loop
;
1101 case IP_MULTICAST_IF
:
1103 struct in_addr addr
;
1104 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1105 addr
.s_addr
= inet
->mc_addr
;
1108 if (put_user(len
, optlen
))
1110 if (copy_to_user(optval
, &addr
, len
))
1116 struct ip_msfilter msf
;
1119 if (len
< IP_MSFILTER_SIZE(0)) {
1123 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1127 err
= ip_mc_msfget(sk
, &msf
,
1128 (struct ip_msfilter __user
*)optval
, optlen
);
1132 case MCAST_MSFILTER
:
1134 struct group_filter gsf
;
1137 if (len
< GROUP_FILTER_SIZE(0)) {
1141 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1145 err
= ip_mc_gsfget(sk
, &gsf
,
1146 (struct group_filter __user
*)optval
, optlen
);
1156 if (sk
->sk_type
!= SOCK_STREAM
)
1157 return -ENOPROTOOPT
;
1159 msg
.msg_control
= optval
;
1160 msg
.msg_controllen
= len
;
1163 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1164 struct in_pktinfo info
;
1166 info
.ipi_addr
.s_addr
= inet
->rcv_saddr
;
1167 info
.ipi_spec_dst
.s_addr
= inet
->rcv_saddr
;
1168 info
.ipi_ifindex
= inet
->mc_index
;
1169 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1171 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1172 int hlim
= inet
->mc_ttl
;
1173 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1175 len
-= msg
.msg_controllen
;
1176 return put_user(len
, optlen
);
1179 val
= inet
->freebind
;
1181 case IP_TRANSPARENT
:
1182 val
= inet
->transparent
;
1186 return -ENOPROTOOPT
;
1190 if (len
< sizeof(int) && len
> 0 && val
>=0 && val
<=255) {
1191 unsigned char ucval
= (unsigned char)val
;
1193 if (put_user(len
, optlen
))
1195 if (copy_to_user(optval
, &ucval
, 1))
1198 len
= min_t(unsigned int, sizeof(int), len
);
1199 if (put_user(len
, optlen
))
1201 if (copy_to_user(optval
, &val
, len
))
1207 int ip_getsockopt(struct sock
*sk
, int level
,
1208 int optname
, char __user
*optval
, int __user
*optlen
)
1212 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1213 #ifdef CONFIG_NETFILTER
1214 /* we need to exclude all possible ENOPROTOOPTs except default case */
1215 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1216 !ip_mroute_opt(optname
)) {
1219 if (get_user(len
, optlen
))
1223 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1227 err
= put_user(len
, optlen
);
1234 #ifdef CONFIG_COMPAT
1235 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1236 char __user
*optval
, int __user
*optlen
)
1240 if (optname
== MCAST_MSFILTER
)
1241 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1244 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1246 #ifdef CONFIG_NETFILTER
1247 /* we need to exclude all possible ENOPROTOOPTs except default case */
1248 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1249 !ip_mroute_opt(optname
)) {
1252 if (get_user(len
, optlen
))
1256 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1259 err
= put_user(len
, optlen
);
1266 EXPORT_SYMBOL(compat_ip_getsockopt
);
1269 EXPORT_SYMBOL(ip_cmsg_recv
);
1271 EXPORT_SYMBOL(ip_getsockopt
);
1272 EXPORT_SYMBOL(ip_setsockopt
);