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
53 * SOL_IP control messages.
56 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
58 struct in_pktinfo info
;
59 struct rtable
*rt
= skb
->rtable
;
61 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
63 info
.ipi_ifindex
= rt
->rt_iif
;
64 info
.ipi_spec_dst
.s_addr
= rt
->rt_spec_dst
;
67 info
.ipi_spec_dst
.s_addr
= 0;
70 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
73 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
75 int ttl
= ip_hdr(skb
)->ttl
;
76 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
79 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
81 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
84 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
86 if (IPCB(skb
)->opt
.optlen
== 0)
89 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
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 net
*net
, 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(net
, &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
= icmp_hdr(skb
)->type
;
272 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
274 serr
->ee
.ee_info
= info
;
275 serr
->ee
.ee_data
= 0;
276 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
277 skb_network_header(skb
);
280 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
281 skb_reset_transport_header(skb
);
282 if (sock_queue_err_skb(sk
, skb
) == 0)
288 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
290 struct inet_sock
*inet
= inet_sk(sk
);
291 struct sock_exterr_skb
*serr
;
298 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
302 skb_put(skb
, sizeof(struct iphdr
));
303 skb_reset_network_header(skb
);
307 serr
= SKB_EXT_ERR(skb
);
308 serr
->ee
.ee_errno
= err
;
309 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
310 serr
->ee
.ee_type
= 0;
311 serr
->ee
.ee_code
= 0;
313 serr
->ee
.ee_info
= info
;
314 serr
->ee
.ee_data
= 0;
315 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
318 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
319 skb_reset_transport_header(skb
);
321 if (sock_queue_err_skb(sk
, skb
))
326 * Handle MSG_ERRQUEUE
328 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
330 struct sock_exterr_skb
*serr
;
331 struct sk_buff
*skb
, *skb2
;
332 struct sockaddr_in
*sin
;
334 struct sock_extended_err ee
;
335 struct sockaddr_in offender
;
341 skb
= skb_dequeue(&sk
->sk_error_queue
);
347 msg
->msg_flags
|= MSG_TRUNC
;
350 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
354 sock_recv_timestamp(msg
, sk
, skb
);
356 serr
= SKB_EXT_ERR(skb
);
358 sin
= (struct sockaddr_in
*)msg
->msg_name
;
360 sin
->sin_family
= AF_INET
;
361 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
363 sin
->sin_port
= serr
->port
;
364 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
367 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
368 sin
= &errhdr
.offender
;
369 sin
->sin_family
= AF_UNSPEC
;
370 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
371 struct inet_sock
*inet
= inet_sk(sk
);
373 sin
->sin_family
= AF_INET
;
374 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
376 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
377 if (inet
->cmsg_flags
)
378 ip_cmsg_recv(msg
, skb
);
381 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
383 /* Now we could try to dump offended packet options */
385 msg
->msg_flags
|= MSG_ERRQUEUE
;
388 /* Reset and regenerate socket error */
389 spin_lock_bh(&sk
->sk_error_queue
.lock
);
391 if ((skb2
= skb_peek(&sk
->sk_error_queue
)) != NULL
) {
392 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
393 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
394 sk
->sk_error_report(sk
);
396 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
406 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
410 static int do_ip_setsockopt(struct sock
*sk
, int level
,
411 int optname
, char __user
*optval
, int optlen
)
413 struct inet_sock
*inet
= inet_sk(sk
);
416 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
417 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
418 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
419 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
420 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
421 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
422 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
))) ||
423 optname
== IP_MULTICAST_TTL
||
424 optname
== IP_MULTICAST_LOOP
) {
425 if (optlen
>= sizeof(int)) {
426 if (get_user(val
, (int __user
*) optval
))
428 } else if (optlen
>= sizeof(char)) {
431 if (get_user(ucval
, (unsigned char __user
*) optval
))
437 /* If optlen==0, it is equivalent to val == 0 */
439 if (ip_mroute_opt(optname
))
440 return ip_mroute_setsockopt(sk
,optname
,optval
,optlen
);
448 struct ip_options
* opt
= NULL
;
449 if (optlen
> 40 || optlen
< 0)
451 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
456 struct inet_connection_sock
*icsk
= inet_csk(sk
);
457 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
458 if (sk
->sk_family
== PF_INET
||
459 (!((1 << sk
->sk_state
) &
460 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
461 inet
->daddr
!= LOOPBACK4_IPV6
)) {
464 icsk
->icsk_ext_hdr_len
-= inet
->opt
->optlen
;
466 icsk
->icsk_ext_hdr_len
+= opt
->optlen
;
467 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
468 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
472 opt
= xchg(&inet
->opt
, opt
);
478 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
480 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
484 inet
->cmsg_flags
|= IP_CMSG_TTL
;
486 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
490 inet
->cmsg_flags
|= IP_CMSG_TOS
;
492 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
496 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
498 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
502 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
504 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
508 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
510 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
512 case IP_TOS
: /* This sets both TOS and Precedence */
513 if (sk
->sk_type
== SOCK_STREAM
) {
515 val
|= inet
->tos
& 3;
517 if (inet
->tos
!= val
) {
519 sk
->sk_priority
= rt_tos2priority(val
);
526 if (val
!= -1 && (val
< 1 || val
>255))
531 if (sk
->sk_type
!= SOCK_RAW
) {
535 inet
->hdrincl
= val
? 1 : 0;
537 case IP_MTU_DISCOVER
:
540 inet
->pmtudisc
= val
;
543 inet
->recverr
= !!val
;
545 skb_queue_purge(&sk
->sk_error_queue
);
547 case IP_MULTICAST_TTL
:
548 if (sk
->sk_type
== SOCK_STREAM
)
554 if (val
< 0 || val
> 255)
558 case IP_MULTICAST_LOOP
:
561 inet
->mc_loop
= !!val
;
563 case IP_MULTICAST_IF
:
565 struct ip_mreqn mreq
;
566 struct net_device
*dev
= NULL
;
568 if (sk
->sk_type
== SOCK_STREAM
)
571 * Check the arguments are allowable
575 if (optlen
>= sizeof(struct ip_mreqn
)) {
576 if (copy_from_user(&mreq
,optval
,sizeof(mreq
)))
579 memset(&mreq
, 0, sizeof(mreq
));
580 if (optlen
>= sizeof(struct in_addr
) &&
581 copy_from_user(&mreq
.imr_address
,optval
,sizeof(struct in_addr
)))
585 if (!mreq
.imr_ifindex
) {
586 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
592 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
594 mreq
.imr_ifindex
= dev
->ifindex
;
598 dev
= __dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
601 err
= -EADDRNOTAVAIL
;
606 if (sk
->sk_bound_dev_if
&&
607 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
610 inet
->mc_index
= mreq
.imr_ifindex
;
611 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
616 case IP_ADD_MEMBERSHIP
:
617 case IP_DROP_MEMBERSHIP
:
619 struct ip_mreqn mreq
;
622 if (inet_sk(sk
)->is_icsk
)
625 if (optlen
< sizeof(struct ip_mreq
))
628 if (optlen
>= sizeof(struct ip_mreqn
)) {
629 if (copy_from_user(&mreq
,optval
,sizeof(mreq
)))
632 memset(&mreq
, 0, sizeof(mreq
));
633 if (copy_from_user(&mreq
,optval
,sizeof(struct ip_mreq
)))
637 if (optname
== IP_ADD_MEMBERSHIP
)
638 err
= ip_mc_join_group(sk
, &mreq
);
640 err
= ip_mc_leave_group(sk
, &mreq
);
645 extern int sysctl_igmp_max_msf
;
646 struct ip_msfilter
*msf
;
648 if (optlen
< IP_MSFILTER_SIZE(0))
650 if (optlen
> sysctl_optmem_max
) {
654 msf
= kmalloc(optlen
, GFP_KERNEL
);
660 if (copy_from_user(msf
, optval
, optlen
)) {
664 /* numsrc >= (1G-4) overflow in 32 bits */
665 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
666 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
671 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
676 err
= ip_mc_msfilter(sk
, msf
, 0);
680 case IP_BLOCK_SOURCE
:
681 case IP_UNBLOCK_SOURCE
:
682 case IP_ADD_SOURCE_MEMBERSHIP
:
683 case IP_DROP_SOURCE_MEMBERSHIP
:
685 struct ip_mreq_source mreqs
;
688 if (optlen
!= sizeof(struct ip_mreq_source
))
690 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
694 if (optname
== IP_BLOCK_SOURCE
) {
695 omode
= MCAST_EXCLUDE
;
697 } else if (optname
== IP_UNBLOCK_SOURCE
) {
698 omode
= MCAST_EXCLUDE
;
700 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
701 struct ip_mreqn mreq
;
703 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
704 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
705 mreq
.imr_ifindex
= 0;
706 err
= ip_mc_join_group(sk
, &mreq
);
707 if (err
&& err
!= -EADDRINUSE
)
709 omode
= MCAST_INCLUDE
;
711 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
712 omode
= MCAST_INCLUDE
;
715 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
718 case MCAST_JOIN_GROUP
:
719 case MCAST_LEAVE_GROUP
:
721 struct group_req greq
;
722 struct sockaddr_in
*psin
;
723 struct ip_mreqn mreq
;
725 if (optlen
< sizeof(struct group_req
))
728 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
730 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
731 if (psin
->sin_family
!= AF_INET
)
733 memset(&mreq
, 0, sizeof(mreq
));
734 mreq
.imr_multiaddr
= psin
->sin_addr
;
735 mreq
.imr_ifindex
= greq
.gr_interface
;
737 if (optname
== MCAST_JOIN_GROUP
)
738 err
= ip_mc_join_group(sk
, &mreq
);
740 err
= ip_mc_leave_group(sk
, &mreq
);
743 case MCAST_JOIN_SOURCE_GROUP
:
744 case MCAST_LEAVE_SOURCE_GROUP
:
745 case MCAST_BLOCK_SOURCE
:
746 case MCAST_UNBLOCK_SOURCE
:
748 struct group_source_req greqs
;
749 struct ip_mreq_source mreqs
;
750 struct sockaddr_in
*psin
;
753 if (optlen
!= sizeof(struct group_source_req
))
755 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
759 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
760 greqs
.gsr_source
.ss_family
!= AF_INET
) {
761 err
= -EADDRNOTAVAIL
;
764 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
765 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
766 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
767 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
768 mreqs
.imr_interface
= 0; /* use index for mc_source */
770 if (optname
== MCAST_BLOCK_SOURCE
) {
771 omode
= MCAST_EXCLUDE
;
773 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
774 omode
= MCAST_EXCLUDE
;
776 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
777 struct ip_mreqn mreq
;
779 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
780 mreq
.imr_multiaddr
= psin
->sin_addr
;
781 mreq
.imr_address
.s_addr
= 0;
782 mreq
.imr_ifindex
= greqs
.gsr_interface
;
783 err
= ip_mc_join_group(sk
, &mreq
);
784 if (err
&& err
!= -EADDRINUSE
)
786 greqs
.gsr_interface
= mreq
.imr_ifindex
;
787 omode
= MCAST_INCLUDE
;
789 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
790 omode
= MCAST_INCLUDE
;
793 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
794 greqs
.gsr_interface
);
799 extern int sysctl_igmp_max_msf
;
800 struct sockaddr_in
*psin
;
801 struct ip_msfilter
*msf
= NULL
;
802 struct group_filter
*gsf
= NULL
;
803 int msize
, i
, ifindex
;
805 if (optlen
< GROUP_FILTER_SIZE(0))
807 if (optlen
> sysctl_optmem_max
) {
811 gsf
= kmalloc(optlen
,GFP_KERNEL
);
817 if (copy_from_user(gsf
, optval
, optlen
)) {
820 /* numsrc >= (4G-140)/128 overflow in 32 bits */
821 if (gsf
->gf_numsrc
>= 0x1ffffff ||
822 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
826 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
830 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
831 msf
= kmalloc(msize
,GFP_KERNEL
);
836 ifindex
= gsf
->gf_interface
;
837 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
838 if (psin
->sin_family
!= AF_INET
) {
839 err
= -EADDRNOTAVAIL
;
842 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
843 msf
->imsf_interface
= 0;
844 msf
->imsf_fmode
= gsf
->gf_fmode
;
845 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
846 err
= -EADDRNOTAVAIL
;
847 for (i
=0; i
<gsf
->gf_numsrc
; ++i
) {
848 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
850 if (psin
->sin_family
!= AF_INET
)
852 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
857 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
863 case IP_ROUTER_ALERT
:
864 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
870 inet
->freebind
= !!val
;
873 case IP_IPSEC_POLICY
:
876 if (!capable(CAP_NET_ADMIN
))
878 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
882 if (!capable(CAP_NET_ADMIN
)) {
888 inet
->transparent
= !!val
;
903 int ip_setsockopt(struct sock
*sk
, int level
,
904 int optname
, char __user
*optval
, int optlen
)
911 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
912 #ifdef CONFIG_NETFILTER
913 /* we need to exclude all possible ENOPROTOOPTs except default case */
914 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
915 optname
!= IP_IPSEC_POLICY
&&
916 optname
!= IP_XFRM_POLICY
&&
917 !ip_mroute_opt(optname
)) {
919 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
927 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
928 char __user
*optval
, int optlen
)
935 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
936 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
939 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
940 #ifdef CONFIG_NETFILTER
941 /* we need to exclude all possible ENOPROTOOPTs except default case */
942 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
943 optname
!= IP_IPSEC_POLICY
&&
944 optname
!= IP_XFRM_POLICY
&&
945 !ip_mroute_opt(optname
)) {
947 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
955 EXPORT_SYMBOL(compat_ip_setsockopt
);
959 * Get the options. Note for future reference. The GET of IP options gets the
960 * _received_ ones. The set sets the _sent_ ones.
963 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
964 char __user
*optval
, int __user
*optlen
)
966 struct inet_sock
*inet
= inet_sk(sk
);
973 if (ip_mroute_opt(optname
))
974 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
;
1143 case IP_TRANSPARENT
:
1144 val
= inet
->transparent
;
1148 return -ENOPROTOOPT
;
1152 if (len
< sizeof(int) && len
> 0 && val
>=0 && val
<=255) {
1153 unsigned char ucval
= (unsigned char)val
;
1155 if (put_user(len
, optlen
))
1157 if (copy_to_user(optval
,&ucval
,1))
1160 len
= min_t(unsigned int, sizeof(int), len
);
1161 if (put_user(len
, optlen
))
1163 if (copy_to_user(optval
,&val
,len
))
1169 int ip_getsockopt(struct sock
*sk
, int level
,
1170 int optname
, char __user
*optval
, int __user
*optlen
)
1174 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1175 #ifdef CONFIG_NETFILTER
1176 /* we need to exclude all possible ENOPROTOOPTs except default case */
1177 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1178 !ip_mroute_opt(optname
)) {
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
)
1202 if (optname
== MCAST_MSFILTER
)
1203 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1206 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1208 #ifdef CONFIG_NETFILTER
1209 /* we need to exclude all possible ENOPROTOOPTs except default case */
1210 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1211 !ip_mroute_opt(optname
)) {
1214 if (get_user(len
, optlen
))
1218 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1221 err
= put_user(len
, optlen
);
1228 EXPORT_SYMBOL(compat_ip_getsockopt
);
1231 EXPORT_SYMBOL(ip_cmsg_recv
);
1233 EXPORT_SYMBOL(ip_getsockopt
);
1234 EXPORT_SYMBOL(ip_setsockopt
);