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/inet_ecn.h>
37 #include <net/route.h>
39 #include <net/compat.h>
40 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
41 #include <net/transp_v6.h>
44 #include <linux/errqueue.h>
45 #include <asm/uaccess.h>
47 #define IP_CMSG_PKTINFO 1
50 #define IP_CMSG_RECVOPTS 8
51 #define IP_CMSG_RETOPTS 16
52 #define IP_CMSG_PASSSEC 32
53 #define IP_CMSG_ORIGDSTADDR 64
56 * SOL_IP control messages.
59 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
61 struct in_pktinfo info
;
62 struct rtable
*rt
= skb_rtable(skb
);
64 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
66 info
.ipi_ifindex
= rt
->rt_iif
;
67 info
.ipi_spec_dst
.s_addr
= rt
->rt_spec_dst
;
70 info
.ipi_spec_dst
.s_addr
= 0;
73 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
76 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
78 int ttl
= ip_hdr(skb
)->ttl
;
79 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
82 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
84 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
87 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
89 if (IPCB(skb
)->opt
.optlen
== 0)
92 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
97 static void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
99 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
100 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
102 if (IPCB(skb
)->opt
.optlen
== 0)
105 if (ip_options_echo(opt
, skb
)) {
106 msg
->msg_flags
|= MSG_CTRUNC
;
109 ip_options_undo(opt
);
111 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
114 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
120 err
= security_socket_getpeersec_dgram(NULL
, skb
, &secid
);
124 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
128 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
129 security_release_secctx(secdata
, seclen
);
132 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
134 struct sockaddr_in sin
;
135 const struct iphdr
*iph
= ip_hdr(skb
);
136 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
138 if (skb_transport_offset(skb
) + 4 > skb
->len
)
141 /* All current transport protocols have the port numbers in the
142 * first four bytes of the transport header and this function is
143 * written with this assumption in mind.
146 sin
.sin_family
= AF_INET
;
147 sin
.sin_addr
.s_addr
= iph
->daddr
;
148 sin
.sin_port
= ports
[1];
149 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
151 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
154 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
156 struct inet_sock
*inet
= inet_sk(skb
->sk
);
157 unsigned flags
= inet
->cmsg_flags
;
159 /* Ordered by supposed usage frequency */
161 ip_cmsg_recv_pktinfo(msg
, skb
);
162 if ((flags
>>= 1) == 0)
166 ip_cmsg_recv_ttl(msg
, skb
);
167 if ((flags
>>= 1) == 0)
171 ip_cmsg_recv_tos(msg
, skb
);
172 if ((flags
>>= 1) == 0)
176 ip_cmsg_recv_opts(msg
, skb
);
177 if ((flags
>>= 1) == 0)
181 ip_cmsg_recv_retopts(msg
, skb
);
182 if ((flags
>>= 1) == 0)
186 ip_cmsg_recv_security(msg
, skb
);
188 if ((flags
>>= 1) == 0)
191 ip_cmsg_recv_dstaddr(msg
, skb
);
194 EXPORT_SYMBOL(ip_cmsg_recv
);
196 int ip_cmsg_send(struct net
*net
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
199 struct cmsghdr
*cmsg
;
201 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
202 if (!CMSG_OK(msg
, cmsg
))
204 if (cmsg
->cmsg_level
!= SOL_IP
)
206 switch (cmsg
->cmsg_type
) {
208 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
209 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
210 err
< 40 ? err
: 40);
216 struct in_pktinfo
*info
;
217 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
219 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
220 ipc
->oif
= info
->ipi_ifindex
;
221 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
232 /* Special input handler for packets caught by router alert option.
233 They are selected only by protocol field, and then processed likely
234 local ones; but only if someone wants them! Otherwise, router
235 not running rsvpd will kill RSVP.
237 It is user level problem, what it will make with them.
238 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
239 but receiver should be enough clever f.e. to forward mtrace requests,
240 sent to multicast group to reach destination designated router.
242 struct ip_ra_chain __rcu
*ip_ra_chain
;
243 static DEFINE_SPINLOCK(ip_ra_lock
);
246 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
248 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
250 sock_put(ra
->saved_sk
);
254 int ip_ra_control(struct sock
*sk
, unsigned char on
,
255 void (*destructor
)(struct sock
*))
257 struct ip_ra_chain
*ra
, *new_ra
;
258 struct ip_ra_chain __rcu
**rap
;
260 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
263 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
265 spin_lock_bh(&ip_ra_lock
);
266 for (rap
= &ip_ra_chain
;
267 (ra
= rcu_dereference_protected(*rap
,
268 lockdep_is_held(&ip_ra_lock
))) != NULL
;
272 spin_unlock_bh(&ip_ra_lock
);
276 /* dont let ip_call_ra_chain() use sk again */
278 rcu_assign_pointer(*rap
, ra
->next
);
279 spin_unlock_bh(&ip_ra_lock
);
284 * Delay sock_put(sk) and kfree(ra) after one rcu grace
285 * period. This guarantee ip_call_ra_chain() dont need
286 * to mess with socket refcounts.
289 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
293 if (new_ra
== NULL
) {
294 spin_unlock_bh(&ip_ra_lock
);
298 new_ra
->destructor
= destructor
;
301 rcu_assign_pointer(*rap
, new_ra
);
303 spin_unlock_bh(&ip_ra_lock
);
308 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
309 __be16 port
, u32 info
, u8
*payload
)
311 struct sock_exterr_skb
*serr
;
313 skb
= skb_clone(skb
, GFP_ATOMIC
);
317 serr
= SKB_EXT_ERR(skb
);
318 serr
->ee
.ee_errno
= err
;
319 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
320 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
321 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
323 serr
->ee
.ee_info
= info
;
324 serr
->ee
.ee_data
= 0;
325 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
326 skb_network_header(skb
);
329 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
330 skb_reset_transport_header(skb
);
331 if (sock_queue_err_skb(sk
, skb
) == 0)
337 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
339 struct inet_sock
*inet
= inet_sk(sk
);
340 struct sock_exterr_skb
*serr
;
347 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
351 skb_put(skb
, sizeof(struct iphdr
));
352 skb_reset_network_header(skb
);
356 serr
= SKB_EXT_ERR(skb
);
357 serr
->ee
.ee_errno
= err
;
358 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
359 serr
->ee
.ee_type
= 0;
360 serr
->ee
.ee_code
= 0;
362 serr
->ee
.ee_info
= info
;
363 serr
->ee
.ee_data
= 0;
364 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
367 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
368 skb_reset_transport_header(skb
);
370 if (sock_queue_err_skb(sk
, skb
))
375 * Handle MSG_ERRQUEUE
377 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
379 struct sock_exterr_skb
*serr
;
380 struct sk_buff
*skb
, *skb2
;
381 struct sockaddr_in
*sin
;
383 struct sock_extended_err ee
;
384 struct sockaddr_in offender
;
390 skb
= skb_dequeue(&sk
->sk_error_queue
);
396 msg
->msg_flags
|= MSG_TRUNC
;
399 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
403 sock_recv_timestamp(msg
, sk
, skb
);
405 serr
= SKB_EXT_ERR(skb
);
407 sin
= (struct sockaddr_in
*)msg
->msg_name
;
409 sin
->sin_family
= AF_INET
;
410 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
412 sin
->sin_port
= serr
->port
;
413 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
416 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
417 sin
= &errhdr
.offender
;
418 sin
->sin_family
= AF_UNSPEC
;
419 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
420 struct inet_sock
*inet
= inet_sk(sk
);
422 sin
->sin_family
= AF_INET
;
423 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
425 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
426 if (inet
->cmsg_flags
)
427 ip_cmsg_recv(msg
, skb
);
430 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
432 /* Now we could try to dump offended packet options */
434 msg
->msg_flags
|= MSG_ERRQUEUE
;
437 /* Reset and regenerate socket error */
438 spin_lock_bh(&sk
->sk_error_queue
.lock
);
440 skb2
= skb_peek(&sk
->sk_error_queue
);
442 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
443 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
444 sk
->sk_error_report(sk
);
446 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
455 static void opt_kfree_rcu(struct rcu_head
*head
)
457 kfree(container_of(head
, struct ip_options_rcu
, rcu
));
461 * Socket option code for IP. This is the end of the line after any
462 * TCP,UDP etc options on an IP socket.
465 static int do_ip_setsockopt(struct sock
*sk
, int level
,
466 int optname
, char __user
*optval
, unsigned int optlen
)
468 struct inet_sock
*inet
= inet_sk(sk
);
471 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
472 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
473 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
474 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
475 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
476 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
477 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
) |
478 (1<<IP_MINTTL
) | (1<<IP_NODEFRAG
))) ||
479 optname
== IP_MULTICAST_TTL
||
480 optname
== IP_MULTICAST_ALL
||
481 optname
== IP_MULTICAST_LOOP
||
482 optname
== IP_RECVORIGDSTADDR
) {
483 if (optlen
>= sizeof(int)) {
484 if (get_user(val
, (int __user
*) optval
))
486 } else if (optlen
>= sizeof(char)) {
489 if (get_user(ucval
, (unsigned char __user
*) optval
))
495 /* If optlen==0, it is equivalent to val == 0 */
497 if (ip_mroute_opt(optname
))
498 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
506 struct ip_options_rcu
*old
, *opt
= NULL
;
510 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
514 old
= rcu_dereference_protected(inet
->inet_opt
,
515 sock_owned_by_user(sk
));
517 struct inet_connection_sock
*icsk
= inet_csk(sk
);
518 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
519 if (sk
->sk_family
== PF_INET
||
520 (!((1 << sk
->sk_state
) &
521 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
522 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
525 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
527 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
528 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
529 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
533 rcu_assign_pointer(inet
->inet_opt
, opt
);
535 call_rcu(&old
->rcu
, opt_kfree_rcu
);
540 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
542 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
546 inet
->cmsg_flags
|= IP_CMSG_TTL
;
548 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
552 inet
->cmsg_flags
|= IP_CMSG_TOS
;
554 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
558 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
560 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
564 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
566 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
570 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
572 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
574 case IP_RECVORIGDSTADDR
:
576 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
578 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
580 case IP_TOS
: /* This sets both TOS and Precedence */
581 if (sk
->sk_type
== SOCK_STREAM
) {
582 val
&= ~INET_ECN_MASK
;
583 val
|= inet
->tos
& INET_ECN_MASK
;
585 if (inet
->tos
!= val
) {
587 sk
->sk_priority
= rt_tos2priority(val
);
594 if (val
!= -1 && (val
< 0 || val
> 255))
599 if (sk
->sk_type
!= SOCK_RAW
) {
603 inet
->hdrincl
= val
? 1 : 0;
606 if (sk
->sk_type
!= SOCK_RAW
) {
610 inet
->nodefrag
= val
? 1 : 0;
612 case IP_MTU_DISCOVER
:
613 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
615 inet
->pmtudisc
= val
;
618 inet
->recverr
= !!val
;
620 skb_queue_purge(&sk
->sk_error_queue
);
622 case IP_MULTICAST_TTL
:
623 if (sk
->sk_type
== SOCK_STREAM
)
629 if (val
< 0 || val
> 255)
633 case IP_MULTICAST_LOOP
:
636 inet
->mc_loop
= !!val
;
638 case IP_MULTICAST_IF
:
640 struct ip_mreqn mreq
;
641 struct net_device
*dev
= NULL
;
643 if (sk
->sk_type
== SOCK_STREAM
)
646 * Check the arguments are allowable
649 if (optlen
< sizeof(struct in_addr
))
653 if (optlen
>= sizeof(struct ip_mreqn
)) {
654 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
657 memset(&mreq
, 0, sizeof(mreq
));
658 if (optlen
>= sizeof(struct in_addr
) &&
659 copy_from_user(&mreq
.imr_address
, optval
,
660 sizeof(struct in_addr
)))
664 if (!mreq
.imr_ifindex
) {
665 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
671 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
673 mreq
.imr_ifindex
= dev
->ifindex
;
675 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
678 err
= -EADDRNOTAVAIL
;
684 if (sk
->sk_bound_dev_if
&&
685 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
688 inet
->mc_index
= mreq
.imr_ifindex
;
689 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
694 case IP_ADD_MEMBERSHIP
:
695 case IP_DROP_MEMBERSHIP
:
697 struct ip_mreqn mreq
;
700 if (inet_sk(sk
)->is_icsk
)
703 if (optlen
< sizeof(struct ip_mreq
))
706 if (optlen
>= sizeof(struct ip_mreqn
)) {
707 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
710 memset(&mreq
, 0, sizeof(mreq
));
711 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
715 if (optname
== IP_ADD_MEMBERSHIP
)
716 err
= ip_mc_join_group(sk
, &mreq
);
718 err
= ip_mc_leave_group(sk
, &mreq
);
723 struct ip_msfilter
*msf
;
725 if (optlen
< IP_MSFILTER_SIZE(0))
727 if (optlen
> sysctl_optmem_max
) {
731 msf
= kmalloc(optlen
, GFP_KERNEL
);
737 if (copy_from_user(msf
, optval
, optlen
)) {
741 /* numsrc >= (1G-4) overflow in 32 bits */
742 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
743 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
748 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
753 err
= ip_mc_msfilter(sk
, msf
, 0);
757 case IP_BLOCK_SOURCE
:
758 case IP_UNBLOCK_SOURCE
:
759 case IP_ADD_SOURCE_MEMBERSHIP
:
760 case IP_DROP_SOURCE_MEMBERSHIP
:
762 struct ip_mreq_source mreqs
;
765 if (optlen
!= sizeof(struct ip_mreq_source
))
767 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
771 if (optname
== IP_BLOCK_SOURCE
) {
772 omode
= MCAST_EXCLUDE
;
774 } else if (optname
== IP_UNBLOCK_SOURCE
) {
775 omode
= MCAST_EXCLUDE
;
777 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
778 struct ip_mreqn mreq
;
780 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
781 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
782 mreq
.imr_ifindex
= 0;
783 err
= ip_mc_join_group(sk
, &mreq
);
784 if (err
&& err
!= -EADDRINUSE
)
786 omode
= MCAST_INCLUDE
;
788 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
789 omode
= MCAST_INCLUDE
;
792 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
795 case MCAST_JOIN_GROUP
:
796 case MCAST_LEAVE_GROUP
:
798 struct group_req greq
;
799 struct sockaddr_in
*psin
;
800 struct ip_mreqn mreq
;
802 if (optlen
< sizeof(struct group_req
))
805 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
807 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
808 if (psin
->sin_family
!= AF_INET
)
810 memset(&mreq
, 0, sizeof(mreq
));
811 mreq
.imr_multiaddr
= psin
->sin_addr
;
812 mreq
.imr_ifindex
= greq
.gr_interface
;
814 if (optname
== MCAST_JOIN_GROUP
)
815 err
= ip_mc_join_group(sk
, &mreq
);
817 err
= ip_mc_leave_group(sk
, &mreq
);
820 case MCAST_JOIN_SOURCE_GROUP
:
821 case MCAST_LEAVE_SOURCE_GROUP
:
822 case MCAST_BLOCK_SOURCE
:
823 case MCAST_UNBLOCK_SOURCE
:
825 struct group_source_req greqs
;
826 struct ip_mreq_source mreqs
;
827 struct sockaddr_in
*psin
;
830 if (optlen
!= sizeof(struct group_source_req
))
832 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
836 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
837 greqs
.gsr_source
.ss_family
!= AF_INET
) {
838 err
= -EADDRNOTAVAIL
;
841 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
842 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
843 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
844 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
845 mreqs
.imr_interface
= 0; /* use index for mc_source */
847 if (optname
== MCAST_BLOCK_SOURCE
) {
848 omode
= MCAST_EXCLUDE
;
850 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
851 omode
= MCAST_EXCLUDE
;
853 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
854 struct ip_mreqn mreq
;
856 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
857 mreq
.imr_multiaddr
= psin
->sin_addr
;
858 mreq
.imr_address
.s_addr
= 0;
859 mreq
.imr_ifindex
= greqs
.gsr_interface
;
860 err
= ip_mc_join_group(sk
, &mreq
);
861 if (err
&& err
!= -EADDRINUSE
)
863 greqs
.gsr_interface
= mreq
.imr_ifindex
;
864 omode
= MCAST_INCLUDE
;
866 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
867 omode
= MCAST_INCLUDE
;
870 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
871 greqs
.gsr_interface
);
876 struct sockaddr_in
*psin
;
877 struct ip_msfilter
*msf
= NULL
;
878 struct group_filter
*gsf
= NULL
;
879 int msize
, i
, ifindex
;
881 if (optlen
< GROUP_FILTER_SIZE(0))
883 if (optlen
> sysctl_optmem_max
) {
887 gsf
= kmalloc(optlen
, GFP_KERNEL
);
893 if (copy_from_user(gsf
, optval
, optlen
))
896 /* numsrc >= (4G-140)/128 overflow in 32 bits */
897 if (gsf
->gf_numsrc
>= 0x1ffffff ||
898 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
902 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
906 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
907 msf
= kmalloc(msize
, GFP_KERNEL
);
912 ifindex
= gsf
->gf_interface
;
913 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
914 if (psin
->sin_family
!= AF_INET
) {
915 err
= -EADDRNOTAVAIL
;
918 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
919 msf
->imsf_interface
= 0;
920 msf
->imsf_fmode
= gsf
->gf_fmode
;
921 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
922 err
= -EADDRNOTAVAIL
;
923 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
924 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
926 if (psin
->sin_family
!= AF_INET
)
928 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
933 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
939 case IP_MULTICAST_ALL
:
942 if (val
!= 0 && val
!= 1)
946 case IP_ROUTER_ALERT
:
947 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
953 inet
->freebind
= !!val
;
956 case IP_IPSEC_POLICY
:
959 if (!capable(CAP_NET_ADMIN
))
961 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
965 if (!!val
&& !capable(CAP_NET_RAW
) && !capable(CAP_NET_ADMIN
)) {
971 inet
->transparent
= !!val
;
977 if (val
< 0 || val
> 255)
995 * ip_queue_rcv_skb - Queue an skb into sock receive queue
999 * Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option
1000 * is not set, we drop skb dst entry now, while dst cache line is hot.
1002 int ip_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
1004 if (!(inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
))
1006 return sock_queue_rcv_skb(sk
, skb
);
1008 EXPORT_SYMBOL(ip_queue_rcv_skb
);
1010 int ip_setsockopt(struct sock
*sk
, int level
,
1011 int optname
, char __user
*optval
, unsigned int optlen
)
1015 if (level
!= SOL_IP
)
1016 return -ENOPROTOOPT
;
1018 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1019 #ifdef CONFIG_NETFILTER
1020 /* we need to exclude all possible ENOPROTOOPTs except default case */
1021 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1022 optname
!= IP_IPSEC_POLICY
&&
1023 optname
!= IP_XFRM_POLICY
&&
1024 !ip_mroute_opt(optname
)) {
1026 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1032 EXPORT_SYMBOL(ip_setsockopt
);
1034 #ifdef CONFIG_COMPAT
1035 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1036 char __user
*optval
, unsigned int optlen
)
1040 if (level
!= SOL_IP
)
1041 return -ENOPROTOOPT
;
1043 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1044 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1047 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1048 #ifdef CONFIG_NETFILTER
1049 /* we need to exclude all possible ENOPROTOOPTs except default case */
1050 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1051 optname
!= IP_IPSEC_POLICY
&&
1052 optname
!= IP_XFRM_POLICY
&&
1053 !ip_mroute_opt(optname
)) {
1055 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1062 EXPORT_SYMBOL(compat_ip_setsockopt
);
1066 * Get the options. Note for future reference. The GET of IP options gets
1067 * the _received_ ones. The set sets the _sent_ ones.
1070 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1071 char __user
*optval
, int __user
*optlen
, unsigned flags
)
1073 struct inet_sock
*inet
= inet_sk(sk
);
1077 if (level
!= SOL_IP
)
1080 if (ip_mroute_opt(optname
))
1081 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1083 if (get_user(len
, optlen
))
1093 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1094 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1095 struct ip_options_rcu
*inet_opt
;
1097 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1098 sock_owned_by_user(sk
));
1101 memcpy(optbuf
, &inet_opt
->opt
,
1102 sizeof(struct ip_options
) +
1103 inet_opt
->opt
.optlen
);
1106 if (opt
->optlen
== 0)
1107 return put_user(0, optlen
);
1109 ip_options_undo(opt
);
1111 len
= min_t(unsigned int, len
, opt
->optlen
);
1112 if (put_user(len
, optlen
))
1114 if (copy_to_user(optval
, opt
->__data
, len
))
1119 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1122 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1125 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1128 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1131 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1134 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1136 case IP_RECVORIGDSTADDR
:
1137 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1143 val
= (inet
->uc_ttl
== -1 ?
1144 sysctl_ip_default_ttl
:
1148 val
= inet
->hdrincl
;
1151 val
= inet
->nodefrag
;
1153 case IP_MTU_DISCOVER
:
1154 val
= inet
->pmtudisc
;
1158 struct dst_entry
*dst
;
1160 dst
= sk_dst_get(sk
);
1172 val
= inet
->recverr
;
1174 case IP_MULTICAST_TTL
:
1177 case IP_MULTICAST_LOOP
:
1178 val
= inet
->mc_loop
;
1180 case IP_MULTICAST_IF
:
1182 struct in_addr addr
;
1183 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1184 addr
.s_addr
= inet
->mc_addr
;
1187 if (put_user(len
, optlen
))
1189 if (copy_to_user(optval
, &addr
, len
))
1195 struct ip_msfilter msf
;
1198 if (len
< IP_MSFILTER_SIZE(0)) {
1202 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1206 err
= ip_mc_msfget(sk
, &msf
,
1207 (struct ip_msfilter __user
*)optval
, optlen
);
1211 case MCAST_MSFILTER
:
1213 struct group_filter gsf
;
1216 if (len
< GROUP_FILTER_SIZE(0)) {
1220 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1224 err
= ip_mc_gsfget(sk
, &gsf
,
1225 (struct group_filter __user
*)optval
,
1230 case IP_MULTICAST_ALL
:
1239 if (sk
->sk_type
!= SOCK_STREAM
)
1240 return -ENOPROTOOPT
;
1242 msg
.msg_control
= optval
;
1243 msg
.msg_controllen
= len
;
1244 msg
.msg_flags
= flags
;
1246 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1247 struct in_pktinfo info
;
1249 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1250 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1251 info
.ipi_ifindex
= inet
->mc_index
;
1252 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1254 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1255 int hlim
= inet
->mc_ttl
;
1256 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1258 len
-= msg
.msg_controllen
;
1259 return put_user(len
, optlen
);
1262 val
= inet
->freebind
;
1264 case IP_TRANSPARENT
:
1265 val
= inet
->transparent
;
1268 val
= inet
->min_ttl
;
1272 return -ENOPROTOOPT
;
1276 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1277 unsigned char ucval
= (unsigned char)val
;
1279 if (put_user(len
, optlen
))
1281 if (copy_to_user(optval
, &ucval
, 1))
1284 len
= min_t(unsigned int, sizeof(int), len
);
1285 if (put_user(len
, optlen
))
1287 if (copy_to_user(optval
, &val
, len
))
1293 int ip_getsockopt(struct sock
*sk
, int level
,
1294 int optname
, char __user
*optval
, int __user
*optlen
)
1298 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1299 #ifdef CONFIG_NETFILTER
1300 /* we need to exclude all possible ENOPROTOOPTs except default case */
1301 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1302 !ip_mroute_opt(optname
)) {
1305 if (get_user(len
, optlen
))
1309 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1313 err
= put_user(len
, optlen
);
1319 EXPORT_SYMBOL(ip_getsockopt
);
1321 #ifdef CONFIG_COMPAT
1322 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1323 char __user
*optval
, int __user
*optlen
)
1327 if (optname
== MCAST_MSFILTER
)
1328 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1331 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1334 #ifdef CONFIG_NETFILTER
1335 /* we need to exclude all possible ENOPROTOOPTs except default case */
1336 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1337 !ip_mroute_opt(optname
)) {
1340 if (get_user(len
, optlen
))
1344 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1347 err
= put_user(len
, optlen
);
1353 EXPORT_SYMBOL(compat_ip_getsockopt
);