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(skb
);
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
);
192 EXPORT_SYMBOL(ip_cmsg_recv
);
194 int ip_cmsg_send(struct net
*net
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
197 struct cmsghdr
*cmsg
;
199 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
200 if (!CMSG_OK(msg
, cmsg
))
202 if (cmsg
->cmsg_level
!= SOL_IP
)
204 switch (cmsg
->cmsg_type
) {
206 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
207 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
208 err
< 40 ? err
: 40);
214 struct in_pktinfo
*info
;
215 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
217 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
218 ipc
->oif
= info
->ipi_ifindex
;
219 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
230 /* Special input handler for packets caught by router alert option.
231 They are selected only by protocol field, and then processed likely
232 local ones; but only if someone wants them! Otherwise, router
233 not running rsvpd will kill RSVP.
235 It is user level problem, what it will make with them.
236 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
237 but receiver should be enough clever f.e. to forward mtrace requests,
238 sent to multicast group to reach destination designated router.
240 struct ip_ra_chain
*ip_ra_chain
;
241 DEFINE_RWLOCK(ip_ra_lock
);
243 int ip_ra_control(struct sock
*sk
, unsigned char on
,
244 void (*destructor
)(struct sock
*))
246 struct ip_ra_chain
*ra
, *new_ra
, **rap
;
248 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->num
== IPPROTO_RAW
)
251 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
253 write_lock_bh(&ip_ra_lock
);
254 for (rap
= &ip_ra_chain
; (ra
= *rap
) != NULL
; rap
= &ra
->next
) {
257 write_unlock_bh(&ip_ra_lock
);
262 write_unlock_bh(&ip_ra_lock
);
271 if (new_ra
== NULL
) {
272 write_unlock_bh(&ip_ra_lock
);
276 new_ra
->destructor
= destructor
;
281 write_unlock_bh(&ip_ra_lock
);
286 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
287 __be16 port
, u32 info
, u8
*payload
)
289 struct inet_sock
*inet
= inet_sk(sk
);
290 struct sock_exterr_skb
*serr
;
295 skb
= skb_clone(skb
, GFP_ATOMIC
);
299 serr
= SKB_EXT_ERR(skb
);
300 serr
->ee
.ee_errno
= err
;
301 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
302 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
303 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
305 serr
->ee
.ee_info
= info
;
306 serr
->ee
.ee_data
= 0;
307 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
308 skb_network_header(skb
);
311 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
312 skb_reset_transport_header(skb
);
313 if (sock_queue_err_skb(sk
, skb
) == 0)
319 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
321 struct inet_sock
*inet
= inet_sk(sk
);
322 struct sock_exterr_skb
*serr
;
329 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
333 skb_put(skb
, sizeof(struct iphdr
));
334 skb_reset_network_header(skb
);
338 serr
= SKB_EXT_ERR(skb
);
339 serr
->ee
.ee_errno
= err
;
340 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
341 serr
->ee
.ee_type
= 0;
342 serr
->ee
.ee_code
= 0;
344 serr
->ee
.ee_info
= info
;
345 serr
->ee
.ee_data
= 0;
346 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
349 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
350 skb_reset_transport_header(skb
);
352 if (sock_queue_err_skb(sk
, skb
))
357 * Handle MSG_ERRQUEUE
359 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
361 struct sock_exterr_skb
*serr
;
362 struct sk_buff
*skb
, *skb2
;
363 struct sockaddr_in
*sin
;
365 struct sock_extended_err ee
;
366 struct sockaddr_in offender
;
372 skb
= skb_dequeue(&sk
->sk_error_queue
);
378 msg
->msg_flags
|= MSG_TRUNC
;
381 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
385 sock_recv_timestamp(msg
, sk
, skb
);
387 serr
= SKB_EXT_ERR(skb
);
389 sin
= (struct sockaddr_in
*)msg
->msg_name
;
391 sin
->sin_family
= AF_INET
;
392 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
394 sin
->sin_port
= serr
->port
;
395 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
398 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
399 sin
= &errhdr
.offender
;
400 sin
->sin_family
= AF_UNSPEC
;
401 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
402 struct inet_sock
*inet
= inet_sk(sk
);
404 sin
->sin_family
= AF_INET
;
405 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
407 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
408 if (inet
->cmsg_flags
)
409 ip_cmsg_recv(msg
, skb
);
412 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
414 /* Now we could try to dump offended packet options */
416 msg
->msg_flags
|= MSG_ERRQUEUE
;
419 /* Reset and regenerate socket error */
420 spin_lock_bh(&sk
->sk_error_queue
.lock
);
422 skb2
= skb_peek(&sk
->sk_error_queue
);
424 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
425 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
426 sk
->sk_error_report(sk
);
428 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
438 * Socket option code for IP. This is the end of the line after any
439 * TCP,UDP etc options on an IP socket.
442 static int do_ip_setsockopt(struct sock
*sk
, int level
,
443 int optname
, char __user
*optval
, unsigned int optlen
)
445 struct inet_sock
*inet
= inet_sk(sk
);
448 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
449 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
450 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
451 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
452 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
453 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
454 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
))) ||
455 optname
== IP_MULTICAST_TTL
||
456 optname
== IP_MULTICAST_ALL
||
457 optname
== IP_MULTICAST_LOOP
||
458 optname
== IP_RECVORIGDSTADDR
) {
459 if (optlen
>= sizeof(int)) {
460 if (get_user(val
, (int __user
*) optval
))
462 } else if (optlen
>= sizeof(char)) {
465 if (get_user(ucval
, (unsigned char __user
*) optval
))
471 /* If optlen==0, it is equivalent to val == 0 */
473 if (ip_mroute_opt(optname
))
474 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
482 struct ip_options
*opt
= NULL
;
483 if (optlen
> 40 || optlen
< 0)
485 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
490 struct inet_connection_sock
*icsk
= inet_csk(sk
);
491 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
492 if (sk
->sk_family
== PF_INET
||
493 (!((1 << sk
->sk_state
) &
494 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
495 inet
->daddr
!= LOOPBACK4_IPV6
)) {
498 icsk
->icsk_ext_hdr_len
-= inet
->opt
->optlen
;
500 icsk
->icsk_ext_hdr_len
+= opt
->optlen
;
501 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
502 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
506 opt
= xchg(&inet
->opt
, opt
);
512 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
514 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
518 inet
->cmsg_flags
|= IP_CMSG_TTL
;
520 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
524 inet
->cmsg_flags
|= IP_CMSG_TOS
;
526 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
530 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
532 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
536 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
538 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
542 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
544 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
546 case IP_RECVORIGDSTADDR
:
548 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
550 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
552 case IP_TOS
: /* This sets both TOS and Precedence */
553 if (sk
->sk_type
== SOCK_STREAM
) {
555 val
|= inet
->tos
& 3;
557 if (inet
->tos
!= val
) {
559 sk
->sk_priority
= rt_tos2priority(val
);
566 if (val
!= -1 && (val
< 0 || val
> 255))
571 if (sk
->sk_type
!= SOCK_RAW
) {
575 inet
->hdrincl
= val
? 1 : 0;
577 case IP_MTU_DISCOVER
:
578 if (val
< 0 || val
> 3)
580 inet
->pmtudisc
= val
;
583 inet
->recverr
= !!val
;
585 skb_queue_purge(&sk
->sk_error_queue
);
587 case IP_MULTICAST_TTL
:
588 if (sk
->sk_type
== SOCK_STREAM
)
594 if (val
< 0 || val
> 255)
598 case IP_MULTICAST_LOOP
:
601 inet
->mc_loop
= !!val
;
603 case IP_MULTICAST_IF
:
605 struct ip_mreqn mreq
;
606 struct net_device
*dev
= NULL
;
608 if (sk
->sk_type
== SOCK_STREAM
)
611 * Check the arguments are allowable
614 if (optlen
< sizeof(struct in_addr
))
618 if (optlen
>= sizeof(struct ip_mreqn
)) {
619 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
622 memset(&mreq
, 0, sizeof(mreq
));
623 if (optlen
>= sizeof(struct in_addr
) &&
624 copy_from_user(&mreq
.imr_address
, optval
,
625 sizeof(struct in_addr
)))
629 if (!mreq
.imr_ifindex
) {
630 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
636 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
638 mreq
.imr_ifindex
= dev
->ifindex
;
642 dev
= __dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
645 err
= -EADDRNOTAVAIL
;
650 if (sk
->sk_bound_dev_if
&&
651 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
654 inet
->mc_index
= mreq
.imr_ifindex
;
655 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
660 case IP_ADD_MEMBERSHIP
:
661 case IP_DROP_MEMBERSHIP
:
663 struct ip_mreqn mreq
;
666 if (inet_sk(sk
)->is_icsk
)
669 if (optlen
< sizeof(struct ip_mreq
))
672 if (optlen
>= sizeof(struct ip_mreqn
)) {
673 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
676 memset(&mreq
, 0, sizeof(mreq
));
677 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
681 if (optname
== IP_ADD_MEMBERSHIP
)
682 err
= ip_mc_join_group(sk
, &mreq
);
684 err
= ip_mc_leave_group(sk
, &mreq
);
689 struct ip_msfilter
*msf
;
691 if (optlen
< IP_MSFILTER_SIZE(0))
693 if (optlen
> sysctl_optmem_max
) {
697 msf
= kmalloc(optlen
, GFP_KERNEL
);
703 if (copy_from_user(msf
, optval
, optlen
)) {
707 /* numsrc >= (1G-4) overflow in 32 bits */
708 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
709 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
714 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
719 err
= ip_mc_msfilter(sk
, msf
, 0);
723 case IP_BLOCK_SOURCE
:
724 case IP_UNBLOCK_SOURCE
:
725 case IP_ADD_SOURCE_MEMBERSHIP
:
726 case IP_DROP_SOURCE_MEMBERSHIP
:
728 struct ip_mreq_source mreqs
;
731 if (optlen
!= sizeof(struct ip_mreq_source
))
733 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
737 if (optname
== IP_BLOCK_SOURCE
) {
738 omode
= MCAST_EXCLUDE
;
740 } else if (optname
== IP_UNBLOCK_SOURCE
) {
741 omode
= MCAST_EXCLUDE
;
743 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
744 struct ip_mreqn mreq
;
746 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
747 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
748 mreq
.imr_ifindex
= 0;
749 err
= ip_mc_join_group(sk
, &mreq
);
750 if (err
&& err
!= -EADDRINUSE
)
752 omode
= MCAST_INCLUDE
;
754 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
755 omode
= MCAST_INCLUDE
;
758 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
761 case MCAST_JOIN_GROUP
:
762 case MCAST_LEAVE_GROUP
:
764 struct group_req greq
;
765 struct sockaddr_in
*psin
;
766 struct ip_mreqn mreq
;
768 if (optlen
< sizeof(struct group_req
))
771 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
773 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
774 if (psin
->sin_family
!= AF_INET
)
776 memset(&mreq
, 0, sizeof(mreq
));
777 mreq
.imr_multiaddr
= psin
->sin_addr
;
778 mreq
.imr_ifindex
= greq
.gr_interface
;
780 if (optname
== MCAST_JOIN_GROUP
)
781 err
= ip_mc_join_group(sk
, &mreq
);
783 err
= ip_mc_leave_group(sk
, &mreq
);
786 case MCAST_JOIN_SOURCE_GROUP
:
787 case MCAST_LEAVE_SOURCE_GROUP
:
788 case MCAST_BLOCK_SOURCE
:
789 case MCAST_UNBLOCK_SOURCE
:
791 struct group_source_req greqs
;
792 struct ip_mreq_source mreqs
;
793 struct sockaddr_in
*psin
;
796 if (optlen
!= sizeof(struct group_source_req
))
798 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
802 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
803 greqs
.gsr_source
.ss_family
!= AF_INET
) {
804 err
= -EADDRNOTAVAIL
;
807 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
808 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
809 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
810 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
811 mreqs
.imr_interface
= 0; /* use index for mc_source */
813 if (optname
== MCAST_BLOCK_SOURCE
) {
814 omode
= MCAST_EXCLUDE
;
816 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
817 omode
= MCAST_EXCLUDE
;
819 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
820 struct ip_mreqn mreq
;
822 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
823 mreq
.imr_multiaddr
= psin
->sin_addr
;
824 mreq
.imr_address
.s_addr
= 0;
825 mreq
.imr_ifindex
= greqs
.gsr_interface
;
826 err
= ip_mc_join_group(sk
, &mreq
);
827 if (err
&& err
!= -EADDRINUSE
)
829 greqs
.gsr_interface
= mreq
.imr_ifindex
;
830 omode
= MCAST_INCLUDE
;
832 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
833 omode
= MCAST_INCLUDE
;
836 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
837 greqs
.gsr_interface
);
842 struct sockaddr_in
*psin
;
843 struct ip_msfilter
*msf
= NULL
;
844 struct group_filter
*gsf
= NULL
;
845 int msize
, i
, ifindex
;
847 if (optlen
< GROUP_FILTER_SIZE(0))
849 if (optlen
> sysctl_optmem_max
) {
853 gsf
= kmalloc(optlen
, GFP_KERNEL
);
859 if (copy_from_user(gsf
, optval
, optlen
))
862 /* numsrc >= (4G-140)/128 overflow in 32 bits */
863 if (gsf
->gf_numsrc
>= 0x1ffffff ||
864 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
868 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
872 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
873 msf
= kmalloc(msize
, GFP_KERNEL
);
878 ifindex
= gsf
->gf_interface
;
879 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
880 if (psin
->sin_family
!= AF_INET
) {
881 err
= -EADDRNOTAVAIL
;
884 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
885 msf
->imsf_interface
= 0;
886 msf
->imsf_fmode
= gsf
->gf_fmode
;
887 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
888 err
= -EADDRNOTAVAIL
;
889 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
890 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
892 if (psin
->sin_family
!= AF_INET
)
894 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
899 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
905 case IP_MULTICAST_ALL
:
908 if (val
!= 0 && val
!= 1)
912 case IP_ROUTER_ALERT
:
913 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
919 inet
->freebind
= !!val
;
922 case IP_IPSEC_POLICY
:
925 if (!capable(CAP_NET_ADMIN
))
927 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
931 if (!capable(CAP_NET_ADMIN
)) {
937 inet
->transparent
= !!val
;
952 int ip_setsockopt(struct sock
*sk
, int level
,
953 int optname
, char __user
*optval
, unsigned int optlen
)
960 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
961 #ifdef CONFIG_NETFILTER
962 /* we need to exclude all possible ENOPROTOOPTs except default case */
963 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
964 optname
!= IP_IPSEC_POLICY
&&
965 optname
!= IP_XFRM_POLICY
&&
966 !ip_mroute_opt(optname
)) {
968 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
974 EXPORT_SYMBOL(ip_setsockopt
);
977 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
978 char __user
*optval
, unsigned int optlen
)
985 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
986 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
989 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
990 #ifdef CONFIG_NETFILTER
991 /* we need to exclude all possible ENOPROTOOPTs except default case */
992 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
993 optname
!= IP_IPSEC_POLICY
&&
994 optname
!= IP_XFRM_POLICY
&&
995 !ip_mroute_opt(optname
)) {
997 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1004 EXPORT_SYMBOL(compat_ip_setsockopt
);
1008 * Get the options. Note for future reference. The GET of IP options gets
1009 * the _received_ ones. The set sets the _sent_ ones.
1012 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1013 char __user
*optval
, int __user
*optlen
)
1015 struct inet_sock
*inet
= inet_sk(sk
);
1019 if (level
!= SOL_IP
)
1022 if (ip_mroute_opt(optname
))
1023 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1025 if (get_user(len
, optlen
))
1035 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1036 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
1039 memcpy(optbuf
, inet
->opt
,
1040 sizeof(struct ip_options
)+
1044 if (opt
->optlen
== 0)
1045 return put_user(0, optlen
);
1047 ip_options_undo(opt
);
1049 len
= min_t(unsigned int, len
, opt
->optlen
);
1050 if (put_user(len
, optlen
))
1052 if (copy_to_user(optval
, opt
->__data
, len
))
1057 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1060 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1063 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1066 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1069 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1072 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1074 case IP_RECVORIGDSTADDR
:
1075 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1081 val
= (inet
->uc_ttl
== -1 ?
1082 sysctl_ip_default_ttl
:
1086 val
= inet
->hdrincl
;
1088 case IP_MTU_DISCOVER
:
1089 val
= inet
->pmtudisc
;
1093 struct dst_entry
*dst
;
1095 dst
= sk_dst_get(sk
);
1107 val
= inet
->recverr
;
1109 case IP_MULTICAST_TTL
:
1112 case IP_MULTICAST_LOOP
:
1113 val
= inet
->mc_loop
;
1115 case IP_MULTICAST_IF
:
1117 struct in_addr addr
;
1118 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1119 addr
.s_addr
= inet
->mc_addr
;
1122 if (put_user(len
, optlen
))
1124 if (copy_to_user(optval
, &addr
, len
))
1130 struct ip_msfilter msf
;
1133 if (len
< IP_MSFILTER_SIZE(0)) {
1137 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1141 err
= ip_mc_msfget(sk
, &msf
,
1142 (struct ip_msfilter __user
*)optval
, optlen
);
1146 case MCAST_MSFILTER
:
1148 struct group_filter gsf
;
1151 if (len
< GROUP_FILTER_SIZE(0)) {
1155 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1159 err
= ip_mc_gsfget(sk
, &gsf
,
1160 (struct group_filter __user
*)optval
,
1165 case IP_MULTICAST_ALL
:
1174 if (sk
->sk_type
!= SOCK_STREAM
)
1175 return -ENOPROTOOPT
;
1177 msg
.msg_control
= optval
;
1178 msg
.msg_controllen
= len
;
1181 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1182 struct in_pktinfo info
;
1184 info
.ipi_addr
.s_addr
= inet
->rcv_saddr
;
1185 info
.ipi_spec_dst
.s_addr
= inet
->rcv_saddr
;
1186 info
.ipi_ifindex
= inet
->mc_index
;
1187 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1189 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1190 int hlim
= inet
->mc_ttl
;
1191 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1193 len
-= msg
.msg_controllen
;
1194 return put_user(len
, optlen
);
1197 val
= inet
->freebind
;
1199 case IP_TRANSPARENT
:
1200 val
= inet
->transparent
;
1204 return -ENOPROTOOPT
;
1208 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1209 unsigned char ucval
= (unsigned char)val
;
1211 if (put_user(len
, optlen
))
1213 if (copy_to_user(optval
, &ucval
, 1))
1216 len
= min_t(unsigned int, sizeof(int), len
);
1217 if (put_user(len
, optlen
))
1219 if (copy_to_user(optval
, &val
, len
))
1225 int ip_getsockopt(struct sock
*sk
, int level
,
1226 int optname
, char __user
*optval
, int __user
*optlen
)
1230 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1231 #ifdef CONFIG_NETFILTER
1232 /* we need to exclude all possible ENOPROTOOPTs except default case */
1233 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1234 !ip_mroute_opt(optname
)) {
1237 if (get_user(len
, optlen
))
1241 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1245 err
= put_user(len
, optlen
);
1251 EXPORT_SYMBOL(ip_getsockopt
);
1253 #ifdef CONFIG_COMPAT
1254 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1255 char __user
*optval
, int __user
*optlen
)
1259 if (optname
== MCAST_MSFILTER
)
1260 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1263 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1265 #ifdef CONFIG_NETFILTER
1266 /* we need to exclude all possible ENOPROTOOPTs except default case */
1267 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1268 !ip_mroute_opt(optname
)) {
1271 if (get_user(len
, optlen
))
1275 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1278 err
= put_user(len
, optlen
);
1284 EXPORT_SYMBOL(compat_ip_getsockopt
);