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>
26 #include <linux/slab.h>
30 #include <net/tcp_states.h>
31 #include <linux/udp.h>
32 #include <linux/igmp.h>
33 #include <linux/netfilter.h>
34 #include <linux/route.h>
35 #include <linux/mroute.h>
36 #include <net/route.h>
38 #include <net/compat.h>
39 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
40 #include <net/transp_v6.h>
43 #include <linux/errqueue.h>
44 #include <asm/uaccess.h>
46 #define IP_CMSG_PKTINFO 1
49 #define IP_CMSG_RECVOPTS 8
50 #define IP_CMSG_RETOPTS 16
51 #define IP_CMSG_PASSSEC 32
52 #define IP_CMSG_ORIGDSTADDR 64
55 * SOL_IP control messages.
58 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
60 struct in_pktinfo info
;
61 struct rtable
*rt
= skb_rtable(skb
);
63 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
65 info
.ipi_ifindex
= rt
->rt_iif
;
66 info
.ipi_spec_dst
.s_addr
= rt
->rt_spec_dst
;
69 info
.ipi_spec_dst
.s_addr
= 0;
72 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
75 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
77 int ttl
= ip_hdr(skb
)->ttl
;
78 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
81 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
83 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
86 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
88 if (IPCB(skb
)->opt
.optlen
== 0)
91 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
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(NULL
, skb
, &secid
);
123 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
127 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
128 security_release_secctx(secdata
, seclen
);
131 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
133 struct sockaddr_in sin
;
134 const struct iphdr
*iph
= ip_hdr(skb
);
135 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
137 if (skb_transport_offset(skb
) + 4 > skb
->len
)
140 /* All current transport protocols have the port numbers in the
141 * first four bytes of the transport header and this function is
142 * written with this assumption in mind.
145 sin
.sin_family
= AF_INET
;
146 sin
.sin_addr
.s_addr
= iph
->daddr
;
147 sin
.sin_port
= ports
[1];
148 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
150 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
153 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
155 struct inet_sock
*inet
= inet_sk(skb
->sk
);
156 unsigned flags
= inet
->cmsg_flags
;
158 /* Ordered by supposed usage frequency */
160 ip_cmsg_recv_pktinfo(msg
, skb
);
161 if ((flags
>>= 1) == 0)
165 ip_cmsg_recv_ttl(msg
, skb
);
166 if ((flags
>>= 1) == 0)
170 ip_cmsg_recv_tos(msg
, skb
);
171 if ((flags
>>= 1) == 0)
175 ip_cmsg_recv_opts(msg
, skb
);
176 if ((flags
>>= 1) == 0)
180 ip_cmsg_recv_retopts(msg
, skb
);
181 if ((flags
>>= 1) == 0)
185 ip_cmsg_recv_security(msg
, skb
);
187 if ((flags
>>= 1) == 0)
190 ip_cmsg_recv_dstaddr(msg
, skb
);
193 EXPORT_SYMBOL(ip_cmsg_recv
);
195 int ip_cmsg_send(struct net
*net
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
198 struct cmsghdr
*cmsg
;
200 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
201 if (!CMSG_OK(msg
, cmsg
))
203 if (cmsg
->cmsg_level
!= SOL_IP
)
205 switch (cmsg
->cmsg_type
) {
207 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
208 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
209 err
< 40 ? err
: 40);
215 struct in_pktinfo
*info
;
216 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
218 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
219 ipc
->oif
= info
->ipi_ifindex
;
220 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
231 /* Special input handler for packets caught by router alert option.
232 They are selected only by protocol field, and then processed likely
233 local ones; but only if someone wants them! Otherwise, router
234 not running rsvpd will kill RSVP.
236 It is user level problem, what it will make with them.
237 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
238 but receiver should be enough clever f.e. to forward mtrace requests,
239 sent to multicast group to reach destination designated router.
241 struct ip_ra_chain __rcu
*ip_ra_chain
;
242 static DEFINE_SPINLOCK(ip_ra_lock
);
245 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
247 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
249 sock_put(ra
->saved_sk
);
253 int ip_ra_control(struct sock
*sk
, unsigned char on
,
254 void (*destructor
)(struct sock
*))
256 struct ip_ra_chain
*ra
, *new_ra
;
257 struct ip_ra_chain __rcu
**rap
;
259 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
262 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
264 spin_lock_bh(&ip_ra_lock
);
265 for (rap
= &ip_ra_chain
;
266 (ra
= rcu_dereference_protected(*rap
,
267 lockdep_is_held(&ip_ra_lock
))) != NULL
;
271 spin_unlock_bh(&ip_ra_lock
);
275 /* dont let ip_call_ra_chain() use sk again */
277 rcu_assign_pointer(*rap
, ra
->next
);
278 spin_unlock_bh(&ip_ra_lock
);
283 * Delay sock_put(sk) and kfree(ra) after one rcu grace
284 * period. This guarantee ip_call_ra_chain() dont need
285 * to mess with socket refcounts.
288 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
292 if (new_ra
== NULL
) {
293 spin_unlock_bh(&ip_ra_lock
);
297 new_ra
->destructor
= destructor
;
300 rcu_assign_pointer(*rap
, new_ra
);
302 spin_unlock_bh(&ip_ra_lock
);
307 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
308 __be16 port
, u32 info
, u8
*payload
)
310 struct sock_exterr_skb
*serr
;
312 skb
= skb_clone(skb
, GFP_ATOMIC
);
316 serr
= SKB_EXT_ERR(skb
);
317 serr
->ee
.ee_errno
= err
;
318 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
319 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
320 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
322 serr
->ee
.ee_info
= info
;
323 serr
->ee
.ee_data
= 0;
324 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
325 skb_network_header(skb
);
328 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
329 skb_reset_transport_header(skb
);
330 if (sock_queue_err_skb(sk
, skb
) == 0)
336 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
338 struct inet_sock
*inet
= inet_sk(sk
);
339 struct sock_exterr_skb
*serr
;
346 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
350 skb_put(skb
, sizeof(struct iphdr
));
351 skb_reset_network_header(skb
);
355 serr
= SKB_EXT_ERR(skb
);
356 serr
->ee
.ee_errno
= err
;
357 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
358 serr
->ee
.ee_type
= 0;
359 serr
->ee
.ee_code
= 0;
361 serr
->ee
.ee_info
= info
;
362 serr
->ee
.ee_data
= 0;
363 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
366 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
367 skb_reset_transport_header(skb
);
369 if (sock_queue_err_skb(sk
, skb
))
374 * Handle MSG_ERRQUEUE
376 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
378 struct sock_exterr_skb
*serr
;
379 struct sk_buff
*skb
, *skb2
;
380 struct sockaddr_in
*sin
;
382 struct sock_extended_err ee
;
383 struct sockaddr_in offender
;
389 skb
= skb_dequeue(&sk
->sk_error_queue
);
395 msg
->msg_flags
|= MSG_TRUNC
;
398 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
402 sock_recv_timestamp(msg
, sk
, skb
);
404 serr
= SKB_EXT_ERR(skb
);
406 sin
= (struct sockaddr_in
*)msg
->msg_name
;
408 sin
->sin_family
= AF_INET
;
409 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
411 sin
->sin_port
= serr
->port
;
412 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
415 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
416 sin
= &errhdr
.offender
;
417 sin
->sin_family
= AF_UNSPEC
;
418 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
419 struct inet_sock
*inet
= inet_sk(sk
);
421 sin
->sin_family
= AF_INET
;
422 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
424 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
425 if (inet
->cmsg_flags
)
426 ip_cmsg_recv(msg
, skb
);
429 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
431 /* Now we could try to dump offended packet options */
433 msg
->msg_flags
|= MSG_ERRQUEUE
;
436 /* Reset and regenerate socket error */
437 spin_lock_bh(&sk
->sk_error_queue
.lock
);
439 skb2
= skb_peek(&sk
->sk_error_queue
);
441 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
442 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
443 sk
->sk_error_report(sk
);
445 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
454 static void opt_kfree_rcu(struct rcu_head
*head
)
456 kfree(container_of(head
, struct ip_options_rcu
, rcu
));
460 * Socket option code for IP. This is the end of the line after any
461 * TCP,UDP etc options on an IP socket.
464 static int do_ip_setsockopt(struct sock
*sk
, int level
,
465 int optname
, char __user
*optval
, unsigned int optlen
)
467 struct inet_sock
*inet
= inet_sk(sk
);
470 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
471 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
472 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
473 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
474 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
475 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
476 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
) |
477 (1<<IP_MINTTL
) | (1<<IP_NODEFRAG
))) ||
478 optname
== IP_MULTICAST_TTL
||
479 optname
== IP_MULTICAST_ALL
||
480 optname
== IP_MULTICAST_LOOP
||
481 optname
== IP_RECVORIGDSTADDR
) {
482 if (optlen
>= sizeof(int)) {
483 if (get_user(val
, (int __user
*) optval
))
485 } else if (optlen
>= sizeof(char)) {
488 if (get_user(ucval
, (unsigned char __user
*) optval
))
494 /* If optlen==0, it is equivalent to val == 0 */
496 if (ip_mroute_opt(optname
))
497 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
505 struct ip_options_rcu
*old
, *opt
= NULL
;
509 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
513 old
= rcu_dereference_protected(inet
->inet_opt
,
514 sock_owned_by_user(sk
));
516 struct inet_connection_sock
*icsk
= inet_csk(sk
);
517 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
518 if (sk
->sk_family
== PF_INET
||
519 (!((1 << sk
->sk_state
) &
520 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
521 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
524 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
526 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
527 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
528 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
532 rcu_assign_pointer(inet
->inet_opt
, opt
);
534 call_rcu(&old
->rcu
, opt_kfree_rcu
);
539 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
541 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
545 inet
->cmsg_flags
|= IP_CMSG_TTL
;
547 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
551 inet
->cmsg_flags
|= IP_CMSG_TOS
;
553 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
557 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
559 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
563 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
565 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
569 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
571 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
573 case IP_RECVORIGDSTADDR
:
575 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
577 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
579 case IP_TOS
: /* This sets both TOS and Precedence */
580 if (sk
->sk_type
== SOCK_STREAM
) {
582 val
|= inet
->tos
& 3;
584 if (inet
->tos
!= val
) {
586 sk
->sk_priority
= rt_tos2priority(val
);
593 if (val
!= -1 && (val
< 0 || val
> 255))
598 if (sk
->sk_type
!= SOCK_RAW
) {
602 inet
->hdrincl
= val
? 1 : 0;
605 if (sk
->sk_type
!= SOCK_RAW
) {
609 inet
->nodefrag
= val
? 1 : 0;
611 case IP_MTU_DISCOVER
:
612 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
614 inet
->pmtudisc
= val
;
617 inet
->recverr
= !!val
;
619 skb_queue_purge(&sk
->sk_error_queue
);
621 case IP_MULTICAST_TTL
:
622 if (sk
->sk_type
== SOCK_STREAM
)
628 if (val
< 0 || val
> 255)
632 case IP_MULTICAST_LOOP
:
635 inet
->mc_loop
= !!val
;
637 case IP_MULTICAST_IF
:
639 struct ip_mreqn mreq
;
640 struct net_device
*dev
= NULL
;
642 if (sk
->sk_type
== SOCK_STREAM
)
645 * Check the arguments are allowable
648 if (optlen
< sizeof(struct in_addr
))
652 if (optlen
>= sizeof(struct ip_mreqn
)) {
653 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
656 memset(&mreq
, 0, sizeof(mreq
));
657 if (optlen
>= sizeof(struct in_addr
) &&
658 copy_from_user(&mreq
.imr_address
, optval
,
659 sizeof(struct in_addr
)))
663 if (!mreq
.imr_ifindex
) {
664 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
670 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
672 mreq
.imr_ifindex
= dev
->ifindex
;
674 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
677 err
= -EADDRNOTAVAIL
;
683 if (sk
->sk_bound_dev_if
&&
684 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
687 inet
->mc_index
= mreq
.imr_ifindex
;
688 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
693 case IP_ADD_MEMBERSHIP
:
694 case IP_DROP_MEMBERSHIP
:
696 struct ip_mreqn mreq
;
699 if (inet_sk(sk
)->is_icsk
)
702 if (optlen
< sizeof(struct ip_mreq
))
705 if (optlen
>= sizeof(struct ip_mreqn
)) {
706 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
709 memset(&mreq
, 0, sizeof(mreq
));
710 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
714 if (optname
== IP_ADD_MEMBERSHIP
)
715 err
= ip_mc_join_group(sk
, &mreq
);
717 err
= ip_mc_leave_group(sk
, &mreq
);
722 struct ip_msfilter
*msf
;
724 if (optlen
< IP_MSFILTER_SIZE(0))
726 if (optlen
> sysctl_optmem_max
) {
730 msf
= kmalloc(optlen
, GFP_KERNEL
);
736 if (copy_from_user(msf
, optval
, optlen
)) {
740 /* numsrc >= (1G-4) overflow in 32 bits */
741 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
742 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
747 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
752 err
= ip_mc_msfilter(sk
, msf
, 0);
756 case IP_BLOCK_SOURCE
:
757 case IP_UNBLOCK_SOURCE
:
758 case IP_ADD_SOURCE_MEMBERSHIP
:
759 case IP_DROP_SOURCE_MEMBERSHIP
:
761 struct ip_mreq_source mreqs
;
764 if (optlen
!= sizeof(struct ip_mreq_source
))
766 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
770 if (optname
== IP_BLOCK_SOURCE
) {
771 omode
= MCAST_EXCLUDE
;
773 } else if (optname
== IP_UNBLOCK_SOURCE
) {
774 omode
= MCAST_EXCLUDE
;
776 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
777 struct ip_mreqn mreq
;
779 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
780 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
781 mreq
.imr_ifindex
= 0;
782 err
= ip_mc_join_group(sk
, &mreq
);
783 if (err
&& err
!= -EADDRINUSE
)
785 omode
= MCAST_INCLUDE
;
787 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
788 omode
= MCAST_INCLUDE
;
791 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
794 case MCAST_JOIN_GROUP
:
795 case MCAST_LEAVE_GROUP
:
797 struct group_req greq
;
798 struct sockaddr_in
*psin
;
799 struct ip_mreqn mreq
;
801 if (optlen
< sizeof(struct group_req
))
804 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
806 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
807 if (psin
->sin_family
!= AF_INET
)
809 memset(&mreq
, 0, sizeof(mreq
));
810 mreq
.imr_multiaddr
= psin
->sin_addr
;
811 mreq
.imr_ifindex
= greq
.gr_interface
;
813 if (optname
== MCAST_JOIN_GROUP
)
814 err
= ip_mc_join_group(sk
, &mreq
);
816 err
= ip_mc_leave_group(sk
, &mreq
);
819 case MCAST_JOIN_SOURCE_GROUP
:
820 case MCAST_LEAVE_SOURCE_GROUP
:
821 case MCAST_BLOCK_SOURCE
:
822 case MCAST_UNBLOCK_SOURCE
:
824 struct group_source_req greqs
;
825 struct ip_mreq_source mreqs
;
826 struct sockaddr_in
*psin
;
829 if (optlen
!= sizeof(struct group_source_req
))
831 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
835 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
836 greqs
.gsr_source
.ss_family
!= AF_INET
) {
837 err
= -EADDRNOTAVAIL
;
840 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
841 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
842 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
843 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
844 mreqs
.imr_interface
= 0; /* use index for mc_source */
846 if (optname
== MCAST_BLOCK_SOURCE
) {
847 omode
= MCAST_EXCLUDE
;
849 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
850 omode
= MCAST_EXCLUDE
;
852 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
853 struct ip_mreqn mreq
;
855 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
856 mreq
.imr_multiaddr
= psin
->sin_addr
;
857 mreq
.imr_address
.s_addr
= 0;
858 mreq
.imr_ifindex
= greqs
.gsr_interface
;
859 err
= ip_mc_join_group(sk
, &mreq
);
860 if (err
&& err
!= -EADDRINUSE
)
862 greqs
.gsr_interface
= mreq
.imr_ifindex
;
863 omode
= MCAST_INCLUDE
;
865 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
866 omode
= MCAST_INCLUDE
;
869 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
870 greqs
.gsr_interface
);
875 struct sockaddr_in
*psin
;
876 struct ip_msfilter
*msf
= NULL
;
877 struct group_filter
*gsf
= NULL
;
878 int msize
, i
, ifindex
;
880 if (optlen
< GROUP_FILTER_SIZE(0))
882 if (optlen
> sysctl_optmem_max
) {
886 gsf
= kmalloc(optlen
, GFP_KERNEL
);
892 if (copy_from_user(gsf
, optval
, optlen
))
895 /* numsrc >= (4G-140)/128 overflow in 32 bits */
896 if (gsf
->gf_numsrc
>= 0x1ffffff ||
897 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
901 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
905 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
906 msf
= kmalloc(msize
, GFP_KERNEL
);
911 ifindex
= gsf
->gf_interface
;
912 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
913 if (psin
->sin_family
!= AF_INET
) {
914 err
= -EADDRNOTAVAIL
;
917 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
918 msf
->imsf_interface
= 0;
919 msf
->imsf_fmode
= gsf
->gf_fmode
;
920 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
921 err
= -EADDRNOTAVAIL
;
922 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
923 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
925 if (psin
->sin_family
!= AF_INET
)
927 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
932 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
938 case IP_MULTICAST_ALL
:
941 if (val
!= 0 && val
!= 1)
945 case IP_ROUTER_ALERT
:
946 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
952 inet
->freebind
= !!val
;
955 case IP_IPSEC_POLICY
:
958 if (!capable(CAP_NET_ADMIN
))
960 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
964 if (!capable(CAP_NET_ADMIN
)) {
970 inet
->transparent
= !!val
;
976 if (val
< 0 || val
> 255)
994 * ip_queue_rcv_skb - Queue an skb into sock receive queue
998 * Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option
999 * is not set, we drop skb dst entry now, while dst cache line is hot.
1001 int ip_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
1003 if (!(inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
))
1005 return sock_queue_rcv_skb(sk
, skb
);
1007 EXPORT_SYMBOL(ip_queue_rcv_skb
);
1009 int ip_setsockopt(struct sock
*sk
, int level
,
1010 int optname
, char __user
*optval
, unsigned int optlen
)
1014 if (level
!= SOL_IP
)
1015 return -ENOPROTOOPT
;
1017 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1018 #ifdef CONFIG_NETFILTER
1019 /* we need to exclude all possible ENOPROTOOPTs except default case */
1020 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1021 optname
!= IP_IPSEC_POLICY
&&
1022 optname
!= IP_XFRM_POLICY
&&
1023 !ip_mroute_opt(optname
)) {
1025 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1031 EXPORT_SYMBOL(ip_setsockopt
);
1033 #ifdef CONFIG_COMPAT
1034 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1035 char __user
*optval
, unsigned int optlen
)
1039 if (level
!= SOL_IP
)
1040 return -ENOPROTOOPT
;
1042 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1043 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1046 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1047 #ifdef CONFIG_NETFILTER
1048 /* we need to exclude all possible ENOPROTOOPTs except default case */
1049 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1050 optname
!= IP_IPSEC_POLICY
&&
1051 optname
!= IP_XFRM_POLICY
&&
1052 !ip_mroute_opt(optname
)) {
1054 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1061 EXPORT_SYMBOL(compat_ip_setsockopt
);
1065 * Get the options. Note for future reference. The GET of IP options gets
1066 * the _received_ ones. The set sets the _sent_ ones.
1069 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1070 char __user
*optval
, int __user
*optlen
, unsigned flags
)
1072 struct inet_sock
*inet
= inet_sk(sk
);
1076 if (level
!= SOL_IP
)
1079 if (ip_mroute_opt(optname
))
1080 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1082 if (get_user(len
, optlen
))
1092 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1093 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1094 struct ip_options_rcu
*inet_opt
;
1096 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1097 sock_owned_by_user(sk
));
1100 memcpy(optbuf
, &inet_opt
->opt
,
1101 sizeof(struct ip_options
) +
1102 inet_opt
->opt
.optlen
);
1105 if (opt
->optlen
== 0)
1106 return put_user(0, optlen
);
1108 ip_options_undo(opt
);
1110 len
= min_t(unsigned int, len
, opt
->optlen
);
1111 if (put_user(len
, optlen
))
1113 if (copy_to_user(optval
, opt
->__data
, len
))
1118 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1121 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1124 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1127 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1130 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1133 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1135 case IP_RECVORIGDSTADDR
:
1136 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1142 val
= (inet
->uc_ttl
== -1 ?
1143 sysctl_ip_default_ttl
:
1147 val
= inet
->hdrincl
;
1150 val
= inet
->nodefrag
;
1152 case IP_MTU_DISCOVER
:
1153 val
= inet
->pmtudisc
;
1157 struct dst_entry
*dst
;
1159 dst
= sk_dst_get(sk
);
1171 val
= inet
->recverr
;
1173 case IP_MULTICAST_TTL
:
1176 case IP_MULTICAST_LOOP
:
1177 val
= inet
->mc_loop
;
1179 case IP_MULTICAST_IF
:
1181 struct in_addr addr
;
1182 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1183 addr
.s_addr
= inet
->mc_addr
;
1186 if (put_user(len
, optlen
))
1188 if (copy_to_user(optval
, &addr
, len
))
1194 struct ip_msfilter msf
;
1197 if (len
< IP_MSFILTER_SIZE(0)) {
1201 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1205 err
= ip_mc_msfget(sk
, &msf
,
1206 (struct ip_msfilter __user
*)optval
, optlen
);
1210 case MCAST_MSFILTER
:
1212 struct group_filter gsf
;
1215 if (len
< GROUP_FILTER_SIZE(0)) {
1219 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1223 err
= ip_mc_gsfget(sk
, &gsf
,
1224 (struct group_filter __user
*)optval
,
1229 case IP_MULTICAST_ALL
:
1238 if (sk
->sk_type
!= SOCK_STREAM
)
1239 return -ENOPROTOOPT
;
1241 msg
.msg_control
= optval
;
1242 msg
.msg_controllen
= len
;
1243 msg
.msg_flags
= flags
;
1245 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1246 struct in_pktinfo info
;
1248 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1249 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1250 info
.ipi_ifindex
= inet
->mc_index
;
1251 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1253 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1254 int hlim
= inet
->mc_ttl
;
1255 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1257 len
-= msg
.msg_controllen
;
1258 return put_user(len
, optlen
);
1261 val
= inet
->freebind
;
1263 case IP_TRANSPARENT
:
1264 val
= inet
->transparent
;
1267 val
= inet
->min_ttl
;
1271 return -ENOPROTOOPT
;
1275 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1276 unsigned char ucval
= (unsigned char)val
;
1278 if (put_user(len
, optlen
))
1280 if (copy_to_user(optval
, &ucval
, 1))
1283 len
= min_t(unsigned int, sizeof(int), len
);
1284 if (put_user(len
, optlen
))
1286 if (copy_to_user(optval
, &val
, len
))
1292 int ip_getsockopt(struct sock
*sk
, int level
,
1293 int optname
, char __user
*optval
, int __user
*optlen
)
1297 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1298 #ifdef CONFIG_NETFILTER
1299 /* we need to exclude all possible ENOPROTOOPTs except default case */
1300 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1301 !ip_mroute_opt(optname
)) {
1304 if (get_user(len
, optlen
))
1308 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1312 err
= put_user(len
, optlen
);
1318 EXPORT_SYMBOL(ip_getsockopt
);
1320 #ifdef CONFIG_COMPAT
1321 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1322 char __user
*optval
, int __user
*optlen
)
1326 if (optname
== MCAST_MSFILTER
)
1327 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1330 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1333 #ifdef CONFIG_NETFILTER
1334 /* we need to exclude all possible ENOPROTOOPTs except default case */
1335 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1336 !ip_mroute_opt(optname
)) {
1339 if (get_user(len
, optlen
))
1343 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1346 err
= put_user(len
, optlen
);
1352 EXPORT_SYMBOL(compat_ip_getsockopt
);