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 #include <net/checksum.h>
41 #if IS_ENABLED(CONFIG_IPV6)
42 #include <net/transp_v6.h>
44 #include <net/ip_fib.h>
46 #include <linux/errqueue.h>
47 #include <asm/uaccess.h>
50 * SOL_IP control messages.
53 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
55 struct in_pktinfo info
= *PKTINFO_SKB_CB(skb
);
57 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
59 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
62 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
64 int ttl
= ip_hdr(skb
)->ttl
;
65 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
68 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
70 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
73 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
75 if (IPCB(skb
)->opt
.optlen
== 0)
78 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
83 static void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
85 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
86 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
88 if (IPCB(skb
)->opt
.optlen
== 0)
91 if (ip_options_echo(opt
, skb
)) {
92 msg
->msg_flags
|= MSG_CTRUNC
;
97 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
100 static void ip_cmsg_recv_checksum(struct msghdr
*msg
, struct sk_buff
*skb
,
103 __wsum csum
= skb
->csum
;
105 if (skb
->ip_summed
!= CHECKSUM_COMPLETE
)
109 csum
= csum_sub(csum
, csum_partial(skb
->data
, offset
, 0));
111 put_cmsg(msg
, SOL_IP
, IP_CHECKSUM
, sizeof(__wsum
), &csum
);
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_offset(struct msghdr
*msg
, struct sk_buff
*skb
,
157 struct inet_sock
*inet
= inet_sk(skb
->sk
);
158 unsigned int flags
= inet
->cmsg_flags
;
160 /* Ordered by supposed usage frequency */
161 if (flags
& IP_CMSG_PKTINFO
) {
162 ip_cmsg_recv_pktinfo(msg
, skb
);
164 flags
&= ~IP_CMSG_PKTINFO
;
169 if (flags
& IP_CMSG_TTL
) {
170 ip_cmsg_recv_ttl(msg
, skb
);
172 flags
&= ~IP_CMSG_TTL
;
177 if (flags
& IP_CMSG_TOS
) {
178 ip_cmsg_recv_tos(msg
, skb
);
180 flags
&= ~IP_CMSG_TOS
;
185 if (flags
& IP_CMSG_RECVOPTS
) {
186 ip_cmsg_recv_opts(msg
, skb
);
188 flags
&= ~IP_CMSG_RECVOPTS
;
193 if (flags
& IP_CMSG_RETOPTS
) {
194 ip_cmsg_recv_retopts(msg
, skb
);
196 flags
&= ~IP_CMSG_RETOPTS
;
201 if (flags
& IP_CMSG_PASSSEC
) {
202 ip_cmsg_recv_security(msg
, skb
);
204 flags
&= ~IP_CMSG_PASSSEC
;
209 if (flags
& IP_CMSG_ORIGDSTADDR
) {
210 ip_cmsg_recv_dstaddr(msg
, skb
);
212 flags
&= ~IP_CMSG_ORIGDSTADDR
;
217 if (flags
& IP_CMSG_CHECKSUM
)
218 ip_cmsg_recv_checksum(msg
, skb
, offset
);
220 EXPORT_SYMBOL(ip_cmsg_recv_offset
);
222 int ip_cmsg_send(struct net
*net
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
,
226 struct cmsghdr
*cmsg
;
228 for_each_cmsghdr(cmsg
, msg
) {
229 if (!CMSG_OK(msg
, cmsg
))
231 #if IS_ENABLED(CONFIG_IPV6)
233 cmsg
->cmsg_level
== SOL_IPV6
&&
234 cmsg
->cmsg_type
== IPV6_PKTINFO
) {
235 struct in6_pktinfo
*src_info
;
237 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*src_info
)))
239 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
240 if (!ipv6_addr_v4mapped(&src_info
->ipi6_addr
))
242 ipc
->oif
= src_info
->ipi6_ifindex
;
243 ipc
->addr
= src_info
->ipi6_addr
.s6_addr32
[3];
247 if (cmsg
->cmsg_level
!= SOL_IP
)
249 switch (cmsg
->cmsg_type
) {
251 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
252 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
253 err
< 40 ? err
: 40);
259 struct in_pktinfo
*info
;
260 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
262 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
263 ipc
->oif
= info
->ipi_ifindex
;
264 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
268 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
270 val
= *(int *)CMSG_DATA(cmsg
);
271 if (val
< 1 || val
> 255)
276 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
278 val
= *(int *)CMSG_DATA(cmsg
);
279 if (val
< 0 || val
> 255)
282 ipc
->priority
= rt_tos2priority(ipc
->tos
);
293 /* Special input handler for packets caught by router alert option.
294 They are selected only by protocol field, and then processed likely
295 local ones; but only if someone wants them! Otherwise, router
296 not running rsvpd will kill RSVP.
298 It is user level problem, what it will make with them.
299 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
300 but receiver should be enough clever f.e. to forward mtrace requests,
301 sent to multicast group to reach destination designated router.
303 struct ip_ra_chain __rcu
*ip_ra_chain
;
304 static DEFINE_SPINLOCK(ip_ra_lock
);
307 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
309 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
311 sock_put(ra
->saved_sk
);
315 int ip_ra_control(struct sock
*sk
, unsigned char on
,
316 void (*destructor
)(struct sock
*))
318 struct ip_ra_chain
*ra
, *new_ra
;
319 struct ip_ra_chain __rcu
**rap
;
321 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
324 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
326 spin_lock_bh(&ip_ra_lock
);
327 for (rap
= &ip_ra_chain
;
328 (ra
= rcu_dereference_protected(*rap
,
329 lockdep_is_held(&ip_ra_lock
))) != NULL
;
333 spin_unlock_bh(&ip_ra_lock
);
337 /* dont let ip_call_ra_chain() use sk again */
339 RCU_INIT_POINTER(*rap
, ra
->next
);
340 spin_unlock_bh(&ip_ra_lock
);
345 * Delay sock_put(sk) and kfree(ra) after one rcu grace
346 * period. This guarantee ip_call_ra_chain() dont need
347 * to mess with socket refcounts.
350 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
355 spin_unlock_bh(&ip_ra_lock
);
359 new_ra
->destructor
= destructor
;
361 RCU_INIT_POINTER(new_ra
->next
, ra
);
362 rcu_assign_pointer(*rap
, new_ra
);
364 spin_unlock_bh(&ip_ra_lock
);
369 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
370 __be16 port
, u32 info
, u8
*payload
)
372 struct sock_exterr_skb
*serr
;
374 skb
= skb_clone(skb
, GFP_ATOMIC
);
378 serr
= SKB_EXT_ERR(skb
);
379 serr
->ee
.ee_errno
= err
;
380 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
381 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
382 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
384 serr
->ee
.ee_info
= info
;
385 serr
->ee
.ee_data
= 0;
386 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
387 skb_network_header(skb
);
390 if (skb_pull(skb
, payload
- skb
->data
)) {
391 skb_reset_transport_header(skb
);
392 if (sock_queue_err_skb(sk
, skb
) == 0)
398 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
400 struct inet_sock
*inet
= inet_sk(sk
);
401 struct sock_exterr_skb
*serr
;
408 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
412 skb_put(skb
, sizeof(struct iphdr
));
413 skb_reset_network_header(skb
);
417 serr
= SKB_EXT_ERR(skb
);
418 serr
->ee
.ee_errno
= err
;
419 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
420 serr
->ee
.ee_type
= 0;
421 serr
->ee
.ee_code
= 0;
423 serr
->ee
.ee_info
= info
;
424 serr
->ee
.ee_data
= 0;
425 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
428 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
429 skb_reset_transport_header(skb
);
431 if (sock_queue_err_skb(sk
, skb
))
435 /* For some errors we have valid addr_offset even with zero payload and
436 * zero port. Also, addr_offset should be supported if port is set.
438 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb
*serr
)
440 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
441 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
444 /* IPv4 supports cmsg on all imcp errors and some timestamps
446 * Timestamp code paths do not initialize the fields expected by cmsg:
447 * the PKTINFO fields in skb->cb[]. Fill those in here.
449 static bool ipv4_datagram_support_cmsg(const struct sock
*sk
,
453 struct in_pktinfo
*info
;
455 if (ee_origin
== SO_EE_ORIGIN_ICMP
)
458 if (ee_origin
== SO_EE_ORIGIN_LOCAL
)
461 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
462 * timestamp with egress dev. Not possible for packets without dev
463 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
465 if ((!(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_CMSG
)) ||
469 info
= PKTINFO_SKB_CB(skb
);
470 info
->ipi_spec_dst
.s_addr
= ip_hdr(skb
)->saddr
;
471 info
->ipi_ifindex
= skb
->dev
->ifindex
;
476 * Handle MSG_ERRQUEUE
478 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
480 struct sock_exterr_skb
*serr
;
482 DECLARE_SOCKADDR(struct sockaddr_in
*, sin
, msg
->msg_name
);
484 struct sock_extended_err ee
;
485 struct sockaddr_in offender
;
490 WARN_ON_ONCE(sk
->sk_family
== AF_INET6
);
493 skb
= sock_dequeue_err_skb(sk
);
499 msg
->msg_flags
|= MSG_TRUNC
;
502 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
506 sock_recv_timestamp(msg
, sk
, skb
);
508 serr
= SKB_EXT_ERR(skb
);
510 if (sin
&& ipv4_datagram_support_addr(serr
)) {
511 sin
->sin_family
= AF_INET
;
512 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
514 sin
->sin_port
= serr
->port
;
515 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
516 *addr_len
= sizeof(*sin
);
519 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
520 sin
= &errhdr
.offender
;
521 memset(sin
, 0, sizeof(*sin
));
523 if (ipv4_datagram_support_cmsg(sk
, skb
, serr
->ee
.ee_origin
)) {
524 sin
->sin_family
= AF_INET
;
525 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
526 if (inet_sk(sk
)->cmsg_flags
)
527 ip_cmsg_recv(msg
, skb
);
530 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
532 /* Now we could try to dump offended packet options */
534 msg
->msg_flags
|= MSG_ERRQUEUE
;
545 * Socket option code for IP. This is the end of the line after any
546 * TCP,UDP etc options on an IP socket.
548 static bool setsockopt_needs_rtnl(int optname
)
551 case IP_ADD_MEMBERSHIP
:
552 case IP_ADD_SOURCE_MEMBERSHIP
:
553 case IP_BLOCK_SOURCE
:
554 case IP_DROP_MEMBERSHIP
:
555 case IP_DROP_SOURCE_MEMBERSHIP
:
557 case IP_UNBLOCK_SOURCE
:
558 case MCAST_BLOCK_SOURCE
:
560 case MCAST_JOIN_GROUP
:
561 case MCAST_JOIN_SOURCE_GROUP
:
562 case MCAST_LEAVE_GROUP
:
563 case MCAST_LEAVE_SOURCE_GROUP
:
564 case MCAST_UNBLOCK_SOURCE
:
570 static int do_ip_setsockopt(struct sock
*sk
, int level
,
571 int optname
, char __user
*optval
, unsigned int optlen
)
573 struct inet_sock
*inet
= inet_sk(sk
);
575 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
586 case IP_MTU_DISCOVER
:
588 case IP_ROUTER_ALERT
:
594 case IP_BIND_ADDRESS_NO_PORT
:
596 case IP_MULTICAST_TTL
:
597 case IP_MULTICAST_ALL
:
598 case IP_MULTICAST_LOOP
:
599 case IP_RECVORIGDSTADDR
:
601 if (optlen
>= sizeof(int)) {
602 if (get_user(val
, (int __user
*) optval
))
604 } else if (optlen
>= sizeof(char)) {
607 if (get_user(ucval
, (unsigned char __user
*) optval
))
613 /* If optlen==0, it is equivalent to val == 0 */
615 if (ip_mroute_opt(optname
))
616 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
626 struct ip_options_rcu
*old
, *opt
= NULL
;
630 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
634 old
= rcu_dereference_protected(inet
->inet_opt
,
635 sock_owned_by_user(sk
));
637 struct inet_connection_sock
*icsk
= inet_csk(sk
);
638 #if IS_ENABLED(CONFIG_IPV6)
639 if (sk
->sk_family
== PF_INET
||
640 (!((1 << sk
->sk_state
) &
641 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
642 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
645 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
647 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
648 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
649 #if IS_ENABLED(CONFIG_IPV6)
653 rcu_assign_pointer(inet
->inet_opt
, opt
);
660 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
662 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
666 inet
->cmsg_flags
|= IP_CMSG_TTL
;
668 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
672 inet
->cmsg_flags
|= IP_CMSG_TOS
;
674 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
678 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
680 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
684 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
686 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
690 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
692 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
694 case IP_RECVORIGDSTADDR
:
696 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
698 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
702 if (!(inet
->cmsg_flags
& IP_CMSG_CHECKSUM
)) {
703 inet_inc_convert_csum(sk
);
704 inet
->cmsg_flags
|= IP_CMSG_CHECKSUM
;
707 if (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) {
708 inet_dec_convert_csum(sk
);
709 inet
->cmsg_flags
&= ~IP_CMSG_CHECKSUM
;
713 case IP_TOS
: /* This sets both TOS and Precedence */
714 if (sk
->sk_type
== SOCK_STREAM
) {
715 val
&= ~INET_ECN_MASK
;
716 val
|= inet
->tos
& INET_ECN_MASK
;
718 if (inet
->tos
!= val
) {
720 sk
->sk_priority
= rt_tos2priority(val
);
727 if (val
!= -1 && (val
< 1 || val
> 255))
732 if (sk
->sk_type
!= SOCK_RAW
) {
736 inet
->hdrincl
= val
? 1 : 0;
739 if (sk
->sk_type
!= SOCK_RAW
) {
743 inet
->nodefrag
= val
? 1 : 0;
745 case IP_BIND_ADDRESS_NO_PORT
:
746 inet
->bind_address_no_port
= val
? 1 : 0;
748 case IP_MTU_DISCOVER
:
749 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_OMIT
)
751 inet
->pmtudisc
= val
;
754 inet
->recverr
= !!val
;
756 skb_queue_purge(&sk
->sk_error_queue
);
758 case IP_MULTICAST_TTL
:
759 if (sk
->sk_type
== SOCK_STREAM
)
765 if (val
< 0 || val
> 255)
769 case IP_MULTICAST_LOOP
:
772 inet
->mc_loop
= !!val
;
776 struct net_device
*dev
= NULL
;
779 if (optlen
!= sizeof(int))
782 ifindex
= (__force
int)ntohl((__force __be32
)val
);
789 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
790 err
= -EADDRNOTAVAIL
;
796 if (sk
->sk_bound_dev_if
)
799 inet
->uc_index
= ifindex
;
803 case IP_MULTICAST_IF
:
805 struct ip_mreqn mreq
;
806 struct net_device
*dev
= NULL
;
808 if (sk
->sk_type
== SOCK_STREAM
)
811 * Check the arguments are allowable
814 if (optlen
< sizeof(struct in_addr
))
818 if (optlen
>= sizeof(struct ip_mreqn
)) {
819 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
822 memset(&mreq
, 0, sizeof(mreq
));
823 if (optlen
>= sizeof(struct ip_mreq
)) {
824 if (copy_from_user(&mreq
, optval
,
825 sizeof(struct ip_mreq
)))
827 } else if (optlen
>= sizeof(struct in_addr
)) {
828 if (copy_from_user(&mreq
.imr_address
, optval
,
829 sizeof(struct in_addr
)))
834 if (!mreq
.imr_ifindex
) {
835 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
841 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
843 mreq
.imr_ifindex
= dev
->ifindex
;
845 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
848 err
= -EADDRNOTAVAIL
;
854 if (sk
->sk_bound_dev_if
&&
855 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
858 inet
->mc_index
= mreq
.imr_ifindex
;
859 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
864 case IP_ADD_MEMBERSHIP
:
865 case IP_DROP_MEMBERSHIP
:
867 struct ip_mreqn mreq
;
870 if (inet_sk(sk
)->is_icsk
)
873 if (optlen
< sizeof(struct ip_mreq
))
876 if (optlen
>= sizeof(struct ip_mreqn
)) {
877 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
880 memset(&mreq
, 0, sizeof(mreq
));
881 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
885 if (optname
== IP_ADD_MEMBERSHIP
)
886 err
= ip_mc_join_group(sk
, &mreq
);
888 err
= ip_mc_leave_group(sk
, &mreq
);
893 struct ip_msfilter
*msf
;
895 if (optlen
< IP_MSFILTER_SIZE(0))
897 if (optlen
> sysctl_optmem_max
) {
901 msf
= kmalloc(optlen
, GFP_KERNEL
);
907 if (copy_from_user(msf
, optval
, optlen
)) {
911 /* numsrc >= (1G-4) overflow in 32 bits */
912 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
913 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
918 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
923 err
= ip_mc_msfilter(sk
, msf
, 0);
927 case IP_BLOCK_SOURCE
:
928 case IP_UNBLOCK_SOURCE
:
929 case IP_ADD_SOURCE_MEMBERSHIP
:
930 case IP_DROP_SOURCE_MEMBERSHIP
:
932 struct ip_mreq_source mreqs
;
935 if (optlen
!= sizeof(struct ip_mreq_source
))
937 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
941 if (optname
== IP_BLOCK_SOURCE
) {
942 omode
= MCAST_EXCLUDE
;
944 } else if (optname
== IP_UNBLOCK_SOURCE
) {
945 omode
= MCAST_EXCLUDE
;
947 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
948 struct ip_mreqn mreq
;
950 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
951 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
952 mreq
.imr_ifindex
= 0;
953 err
= ip_mc_join_group(sk
, &mreq
);
954 if (err
&& err
!= -EADDRINUSE
)
956 omode
= MCAST_INCLUDE
;
958 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
959 omode
= MCAST_INCLUDE
;
962 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
965 case MCAST_JOIN_GROUP
:
966 case MCAST_LEAVE_GROUP
:
968 struct group_req greq
;
969 struct sockaddr_in
*psin
;
970 struct ip_mreqn mreq
;
972 if (optlen
< sizeof(struct group_req
))
975 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
977 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
978 if (psin
->sin_family
!= AF_INET
)
980 memset(&mreq
, 0, sizeof(mreq
));
981 mreq
.imr_multiaddr
= psin
->sin_addr
;
982 mreq
.imr_ifindex
= greq
.gr_interface
;
984 if (optname
== MCAST_JOIN_GROUP
)
985 err
= ip_mc_join_group(sk
, &mreq
);
987 err
= ip_mc_leave_group(sk
, &mreq
);
990 case MCAST_JOIN_SOURCE_GROUP
:
991 case MCAST_LEAVE_SOURCE_GROUP
:
992 case MCAST_BLOCK_SOURCE
:
993 case MCAST_UNBLOCK_SOURCE
:
995 struct group_source_req greqs
;
996 struct ip_mreq_source mreqs
;
997 struct sockaddr_in
*psin
;
1000 if (optlen
!= sizeof(struct group_source_req
))
1002 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
1006 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
1007 greqs
.gsr_source
.ss_family
!= AF_INET
) {
1008 err
= -EADDRNOTAVAIL
;
1011 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1012 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
1013 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
1014 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
1015 mreqs
.imr_interface
= 0; /* use index for mc_source */
1017 if (optname
== MCAST_BLOCK_SOURCE
) {
1018 omode
= MCAST_EXCLUDE
;
1020 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
1021 omode
= MCAST_EXCLUDE
;
1023 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
1024 struct ip_mreqn mreq
;
1026 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1027 mreq
.imr_multiaddr
= psin
->sin_addr
;
1028 mreq
.imr_address
.s_addr
= 0;
1029 mreq
.imr_ifindex
= greqs
.gsr_interface
;
1030 err
= ip_mc_join_group(sk
, &mreq
);
1031 if (err
&& err
!= -EADDRINUSE
)
1033 greqs
.gsr_interface
= mreq
.imr_ifindex
;
1034 omode
= MCAST_INCLUDE
;
1036 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1037 omode
= MCAST_INCLUDE
;
1040 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
1041 greqs
.gsr_interface
);
1044 case MCAST_MSFILTER
:
1046 struct sockaddr_in
*psin
;
1047 struct ip_msfilter
*msf
= NULL
;
1048 struct group_filter
*gsf
= NULL
;
1049 int msize
, i
, ifindex
;
1051 if (optlen
< GROUP_FILTER_SIZE(0))
1053 if (optlen
> sysctl_optmem_max
) {
1057 gsf
= kmalloc(optlen
, GFP_KERNEL
);
1063 if (copy_from_user(gsf
, optval
, optlen
))
1066 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1067 if (gsf
->gf_numsrc
>= 0x1ffffff ||
1068 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
1072 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
1076 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
1077 msf
= kmalloc(msize
, GFP_KERNEL
);
1082 ifindex
= gsf
->gf_interface
;
1083 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
1084 if (psin
->sin_family
!= AF_INET
) {
1085 err
= -EADDRNOTAVAIL
;
1088 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
1089 msf
->imsf_interface
= 0;
1090 msf
->imsf_fmode
= gsf
->gf_fmode
;
1091 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
1092 err
= -EADDRNOTAVAIL
;
1093 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
1094 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
1096 if (psin
->sin_family
!= AF_INET
)
1098 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
1103 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
1109 case IP_MULTICAST_ALL
:
1112 if (val
!= 0 && val
!= 1)
1116 case IP_ROUTER_ALERT
:
1117 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
1123 inet
->freebind
= !!val
;
1126 case IP_IPSEC_POLICY
:
1127 case IP_XFRM_POLICY
:
1129 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1131 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
1134 case IP_TRANSPARENT
:
1135 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
1136 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1142 inet
->transparent
= !!val
;
1148 if (val
< 0 || val
> 255)
1150 inet
->min_ttl
= val
;
1170 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1174 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1175 * destination in skb->cb[] before dst drop.
1176 * This way, receiver doesn't make cache line misses to read rtable.
1178 void ipv4_pktinfo_prepare(const struct sock
*sk
, struct sk_buff
*skb
)
1180 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1181 bool prepare
= (inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
) ||
1184 if (prepare
&& skb_rtable(skb
)) {
1185 pktinfo
->ipi_ifindex
= inet_iif(skb
);
1186 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1188 pktinfo
->ipi_ifindex
= 0;
1189 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1194 int ip_setsockopt(struct sock
*sk
, int level
,
1195 int optname
, char __user
*optval
, unsigned int optlen
)
1199 if (level
!= SOL_IP
)
1200 return -ENOPROTOOPT
;
1202 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1203 #ifdef CONFIG_NETFILTER
1204 /* we need to exclude all possible ENOPROTOOPTs except default case */
1205 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1206 optname
!= IP_IPSEC_POLICY
&&
1207 optname
!= IP_XFRM_POLICY
&&
1208 !ip_mroute_opt(optname
)) {
1210 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1216 EXPORT_SYMBOL(ip_setsockopt
);
1218 #ifdef CONFIG_COMPAT
1219 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1220 char __user
*optval
, unsigned int optlen
)
1224 if (level
!= SOL_IP
)
1225 return -ENOPROTOOPT
;
1227 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1228 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1231 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1232 #ifdef CONFIG_NETFILTER
1233 /* we need to exclude all possible ENOPROTOOPTs except default case */
1234 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1235 optname
!= IP_IPSEC_POLICY
&&
1236 optname
!= IP_XFRM_POLICY
&&
1237 !ip_mroute_opt(optname
)) {
1239 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1246 EXPORT_SYMBOL(compat_ip_setsockopt
);
1250 * Get the options. Note for future reference. The GET of IP options gets
1251 * the _received_ ones. The set sets the _sent_ ones.
1254 static bool getsockopt_needs_rtnl(int optname
)
1258 case MCAST_MSFILTER
:
1264 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1265 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1267 struct inet_sock
*inet
= inet_sk(sk
);
1268 bool needs_rtnl
= getsockopt_needs_rtnl(optname
);
1272 if (level
!= SOL_IP
)
1275 if (ip_mroute_opt(optname
))
1276 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1278 if (get_user(len
, optlen
))
1290 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1291 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1292 struct ip_options_rcu
*inet_opt
;
1294 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1295 sock_owned_by_user(sk
));
1298 memcpy(optbuf
, &inet_opt
->opt
,
1299 sizeof(struct ip_options
) +
1300 inet_opt
->opt
.optlen
);
1303 if (opt
->optlen
== 0)
1304 return put_user(0, optlen
);
1306 ip_options_undo(opt
);
1308 len
= min_t(unsigned int, len
, opt
->optlen
);
1309 if (put_user(len
, optlen
))
1311 if (copy_to_user(optval
, opt
->__data
, len
))
1316 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1319 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1322 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1325 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1328 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1331 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1333 case IP_RECVORIGDSTADDR
:
1334 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1337 val
= (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) != 0;
1343 val
= (inet
->uc_ttl
== -1 ?
1344 sysctl_ip_default_ttl
:
1348 val
= inet
->hdrincl
;
1351 val
= inet
->nodefrag
;
1353 case IP_BIND_ADDRESS_NO_PORT
:
1354 val
= inet
->bind_address_no_port
;
1356 case IP_MTU_DISCOVER
:
1357 val
= inet
->pmtudisc
;
1361 struct dst_entry
*dst
;
1363 dst
= sk_dst_get(sk
);
1375 val
= inet
->recverr
;
1377 case IP_MULTICAST_TTL
:
1380 case IP_MULTICAST_LOOP
:
1381 val
= inet
->mc_loop
;
1384 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1386 case IP_MULTICAST_IF
:
1388 struct in_addr addr
;
1389 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1390 addr
.s_addr
= inet
->mc_addr
;
1393 if (put_user(len
, optlen
))
1395 if (copy_to_user(optval
, &addr
, len
))
1401 struct ip_msfilter msf
;
1403 if (len
< IP_MSFILTER_SIZE(0)) {
1407 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1411 err
= ip_mc_msfget(sk
, &msf
,
1412 (struct ip_msfilter __user
*)optval
, optlen
);
1415 case MCAST_MSFILTER
:
1417 struct group_filter gsf
;
1419 if (len
< GROUP_FILTER_SIZE(0)) {
1423 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1427 err
= ip_mc_gsfget(sk
, &gsf
,
1428 (struct group_filter __user
*)optval
,
1432 case IP_MULTICAST_ALL
:
1441 if (sk
->sk_type
!= SOCK_STREAM
)
1442 return -ENOPROTOOPT
;
1444 msg
.msg_control
= (__force
void *) optval
;
1445 msg
.msg_controllen
= len
;
1446 msg
.msg_flags
= flags
;
1448 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1449 struct in_pktinfo info
;
1451 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1452 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1453 info
.ipi_ifindex
= inet
->mc_index
;
1454 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1456 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1457 int hlim
= inet
->mc_ttl
;
1458 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1460 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1461 int tos
= inet
->rcv_tos
;
1462 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1464 len
-= msg
.msg_controllen
;
1465 return put_user(len
, optlen
);
1468 val
= inet
->freebind
;
1470 case IP_TRANSPARENT
:
1471 val
= inet
->transparent
;
1474 val
= inet
->min_ttl
;
1478 return -ENOPROTOOPT
;
1482 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1483 unsigned char ucval
= (unsigned char)val
;
1485 if (put_user(len
, optlen
))
1487 if (copy_to_user(optval
, &ucval
, 1))
1490 len
= min_t(unsigned int, sizeof(int), len
);
1491 if (put_user(len
, optlen
))
1493 if (copy_to_user(optval
, &val
, len
))
1505 int ip_getsockopt(struct sock
*sk
, int level
,
1506 int optname
, char __user
*optval
, int __user
*optlen
)
1510 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1511 #ifdef CONFIG_NETFILTER
1512 /* we need to exclude all possible ENOPROTOOPTs except default case */
1513 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1514 !ip_mroute_opt(optname
)) {
1517 if (get_user(len
, optlen
))
1521 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1525 err
= put_user(len
, optlen
);
1531 EXPORT_SYMBOL(ip_getsockopt
);
1533 #ifdef CONFIG_COMPAT
1534 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1535 char __user
*optval
, int __user
*optlen
)
1539 if (optname
== MCAST_MSFILTER
)
1540 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1543 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1546 #ifdef CONFIG_NETFILTER
1547 /* we need to exclude all possible ENOPROTOOPTs except default case */
1548 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1549 !ip_mroute_opt(optname
)) {
1552 if (get_user(len
, optlen
))
1556 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1559 err
= put_user(len
, optlen
);
1565 EXPORT_SYMBOL(compat_ip_getsockopt
);