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 <linux/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_fragsize(struct msghdr
*msg
, struct sk_buff
*skb
)
104 if (IPCB(skb
)->frag_max_size
== 0)
107 val
= IPCB(skb
)->frag_max_size
;
108 put_cmsg(msg
, SOL_IP
, IP_RECVFRAGSIZE
, sizeof(val
), &val
);
111 static void ip_cmsg_recv_checksum(struct msghdr
*msg
, struct sk_buff
*skb
,
112 int tlen
, int offset
)
114 __wsum csum
= skb
->csum
;
116 if (skb
->ip_summed
!= CHECKSUM_COMPLETE
)
120 int tend_off
= skb_transport_offset(skb
) + tlen
;
121 csum
= csum_sub(csum
, skb_checksum(skb
, tend_off
, offset
, 0));
124 put_cmsg(msg
, SOL_IP
, IP_CHECKSUM
, sizeof(__wsum
), &csum
);
127 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
133 err
= security_socket_getpeersec_dgram(NULL
, skb
, &secid
);
137 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
141 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
142 security_release_secctx(secdata
, seclen
);
145 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
147 struct sockaddr_in sin
;
148 const struct iphdr
*iph
= ip_hdr(skb
);
149 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
151 if (skb_transport_offset(skb
) + 4 > (int)skb
->len
)
154 /* All current transport protocols have the port numbers in the
155 * first four bytes of the transport header and this function is
156 * written with this assumption in mind.
159 sin
.sin_family
= AF_INET
;
160 sin
.sin_addr
.s_addr
= iph
->daddr
;
161 sin
.sin_port
= ports
[1];
162 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
164 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
167 void ip_cmsg_recv_offset(struct msghdr
*msg
, struct sock
*sk
,
168 struct sk_buff
*skb
, int tlen
, int offset
)
170 struct inet_sock
*inet
= inet_sk(sk
);
171 unsigned int flags
= inet
->cmsg_flags
;
173 /* Ordered by supposed usage frequency */
174 if (flags
& IP_CMSG_PKTINFO
) {
175 ip_cmsg_recv_pktinfo(msg
, skb
);
177 flags
&= ~IP_CMSG_PKTINFO
;
182 if (flags
& IP_CMSG_TTL
) {
183 ip_cmsg_recv_ttl(msg
, skb
);
185 flags
&= ~IP_CMSG_TTL
;
190 if (flags
& IP_CMSG_TOS
) {
191 ip_cmsg_recv_tos(msg
, skb
);
193 flags
&= ~IP_CMSG_TOS
;
198 if (flags
& IP_CMSG_RECVOPTS
) {
199 ip_cmsg_recv_opts(msg
, skb
);
201 flags
&= ~IP_CMSG_RECVOPTS
;
206 if (flags
& IP_CMSG_RETOPTS
) {
207 ip_cmsg_recv_retopts(msg
, skb
);
209 flags
&= ~IP_CMSG_RETOPTS
;
214 if (flags
& IP_CMSG_PASSSEC
) {
215 ip_cmsg_recv_security(msg
, skb
);
217 flags
&= ~IP_CMSG_PASSSEC
;
222 if (flags
& IP_CMSG_ORIGDSTADDR
) {
223 ip_cmsg_recv_dstaddr(msg
, skb
);
225 flags
&= ~IP_CMSG_ORIGDSTADDR
;
230 if (flags
& IP_CMSG_CHECKSUM
)
231 ip_cmsg_recv_checksum(msg
, skb
, tlen
, offset
);
233 if (flags
& IP_CMSG_RECVFRAGSIZE
)
234 ip_cmsg_recv_fragsize(msg
, skb
);
236 EXPORT_SYMBOL(ip_cmsg_recv_offset
);
238 int ip_cmsg_send(struct sock
*sk
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
,
242 struct cmsghdr
*cmsg
;
243 struct net
*net
= sock_net(sk
);
245 for_each_cmsghdr(cmsg
, msg
) {
246 if (!CMSG_OK(msg
, cmsg
))
248 #if IS_ENABLED(CONFIG_IPV6)
250 cmsg
->cmsg_level
== SOL_IPV6
&&
251 cmsg
->cmsg_type
== IPV6_PKTINFO
) {
252 struct in6_pktinfo
*src_info
;
254 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*src_info
)))
256 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
257 if (!ipv6_addr_v4mapped(&src_info
->ipi6_addr
))
259 ipc
->oif
= src_info
->ipi6_ifindex
;
260 ipc
->addr
= src_info
->ipi6_addr
.s6_addr32
[3];
264 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
265 err
= __sock_cmsg_send(sk
, msg
, cmsg
, &ipc
->sockc
);
271 if (cmsg
->cmsg_level
!= SOL_IP
)
273 switch (cmsg
->cmsg_type
) {
275 err
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
277 /* Our caller is responsible for freeing ipc->opt */
278 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
279 err
< 40 ? err
: 40);
285 struct in_pktinfo
*info
;
286 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
288 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
289 ipc
->oif
= info
->ipi_ifindex
;
290 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
294 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
296 val
= *(int *)CMSG_DATA(cmsg
);
297 if (val
< 1 || val
> 255)
302 if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)))
303 val
= *(int *)CMSG_DATA(cmsg
);
304 else if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(u8
)))
305 val
= *(u8
*)CMSG_DATA(cmsg
);
308 if (val
< 0 || val
> 255)
311 ipc
->priority
= rt_tos2priority(ipc
->tos
);
322 /* Special input handler for packets caught by router alert option.
323 They are selected only by protocol field, and then processed likely
324 local ones; but only if someone wants them! Otherwise, router
325 not running rsvpd will kill RSVP.
327 It is user level problem, what it will make with them.
328 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
329 but receiver should be enough clever f.e. to forward mtrace requests,
330 sent to multicast group to reach destination designated router.
332 struct ip_ra_chain __rcu
*ip_ra_chain
;
333 static DEFINE_SPINLOCK(ip_ra_lock
);
336 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
338 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
340 sock_put(ra
->saved_sk
);
344 int ip_ra_control(struct sock
*sk
, unsigned char on
,
345 void (*destructor
)(struct sock
*))
347 struct ip_ra_chain
*ra
, *new_ra
;
348 struct ip_ra_chain __rcu
**rap
;
350 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
353 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
355 spin_lock_bh(&ip_ra_lock
);
356 for (rap
= &ip_ra_chain
;
357 (ra
= rcu_dereference_protected(*rap
,
358 lockdep_is_held(&ip_ra_lock
))) != NULL
;
362 spin_unlock_bh(&ip_ra_lock
);
366 /* dont let ip_call_ra_chain() use sk again */
368 RCU_INIT_POINTER(*rap
, ra
->next
);
369 spin_unlock_bh(&ip_ra_lock
);
374 * Delay sock_put(sk) and kfree(ra) after one rcu grace
375 * period. This guarantee ip_call_ra_chain() dont need
376 * to mess with socket refcounts.
379 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
384 spin_unlock_bh(&ip_ra_lock
);
388 new_ra
->destructor
= destructor
;
390 RCU_INIT_POINTER(new_ra
->next
, ra
);
391 rcu_assign_pointer(*rap
, new_ra
);
393 spin_unlock_bh(&ip_ra_lock
);
398 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
399 __be16 port
, u32 info
, u8
*payload
)
401 struct sock_exterr_skb
*serr
;
403 skb
= skb_clone(skb
, GFP_ATOMIC
);
407 serr
= SKB_EXT_ERR(skb
);
408 serr
->ee
.ee_errno
= err
;
409 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
410 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
411 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
413 serr
->ee
.ee_info
= info
;
414 serr
->ee
.ee_data
= 0;
415 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
416 skb_network_header(skb
);
419 if (skb_pull(skb
, payload
- skb
->data
)) {
420 skb_reset_transport_header(skb
);
421 if (sock_queue_err_skb(sk
, skb
) == 0)
427 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
429 struct inet_sock
*inet
= inet_sk(sk
);
430 struct sock_exterr_skb
*serr
;
437 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
441 skb_put(skb
, sizeof(struct iphdr
));
442 skb_reset_network_header(skb
);
446 serr
= SKB_EXT_ERR(skb
);
447 serr
->ee
.ee_errno
= err
;
448 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
449 serr
->ee
.ee_type
= 0;
450 serr
->ee
.ee_code
= 0;
452 serr
->ee
.ee_info
= info
;
453 serr
->ee
.ee_data
= 0;
454 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
457 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
458 skb_reset_transport_header(skb
);
460 if (sock_queue_err_skb(sk
, skb
))
464 /* For some errors we have valid addr_offset even with zero payload and
465 * zero port. Also, addr_offset should be supported if port is set.
467 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb
*serr
)
469 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
470 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
473 /* IPv4 supports cmsg on all imcp errors and some timestamps
475 * Timestamp code paths do not initialize the fields expected by cmsg:
476 * the PKTINFO fields in skb->cb[]. Fill those in here.
478 static bool ipv4_datagram_support_cmsg(const struct sock
*sk
,
482 struct in_pktinfo
*info
;
484 if (ee_origin
== SO_EE_ORIGIN_ICMP
)
487 if (ee_origin
== SO_EE_ORIGIN_LOCAL
)
490 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
491 * timestamp with egress dev. Not possible for packets without iif
492 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
494 info
= PKTINFO_SKB_CB(skb
);
495 if (!(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_CMSG
) ||
499 info
->ipi_spec_dst
.s_addr
= ip_hdr(skb
)->saddr
;
504 * Handle MSG_ERRQUEUE
506 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
508 struct sock_exterr_skb
*serr
;
510 DECLARE_SOCKADDR(struct sockaddr_in
*, sin
, msg
->msg_name
);
512 struct sock_extended_err ee
;
513 struct sockaddr_in offender
;
518 WARN_ON_ONCE(sk
->sk_family
== AF_INET6
);
521 skb
= sock_dequeue_err_skb(sk
);
527 msg
->msg_flags
|= MSG_TRUNC
;
530 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
535 sock_recv_timestamp(msg
, sk
, skb
);
537 serr
= SKB_EXT_ERR(skb
);
539 if (sin
&& ipv4_datagram_support_addr(serr
)) {
540 sin
->sin_family
= AF_INET
;
541 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
543 sin
->sin_port
= serr
->port
;
544 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
545 *addr_len
= sizeof(*sin
);
548 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
549 sin
= &errhdr
.offender
;
550 memset(sin
, 0, sizeof(*sin
));
552 if (ipv4_datagram_support_cmsg(sk
, skb
, serr
->ee
.ee_origin
)) {
553 sin
->sin_family
= AF_INET
;
554 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
555 if (inet_sk(sk
)->cmsg_flags
)
556 ip_cmsg_recv(msg
, skb
);
559 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
561 /* Now we could try to dump offended packet options */
563 msg
->msg_flags
|= MSG_ERRQUEUE
;
573 * Socket option code for IP. This is the end of the line after any
574 * TCP,UDP etc options on an IP socket.
576 static bool setsockopt_needs_rtnl(int optname
)
579 case IP_ADD_MEMBERSHIP
:
580 case IP_ADD_SOURCE_MEMBERSHIP
:
581 case IP_BLOCK_SOURCE
:
582 case IP_DROP_MEMBERSHIP
:
583 case IP_DROP_SOURCE_MEMBERSHIP
:
585 case IP_UNBLOCK_SOURCE
:
586 case MCAST_BLOCK_SOURCE
:
588 case MCAST_JOIN_GROUP
:
589 case MCAST_JOIN_SOURCE_GROUP
:
590 case MCAST_LEAVE_GROUP
:
591 case MCAST_LEAVE_SOURCE_GROUP
:
592 case MCAST_UNBLOCK_SOURCE
:
593 case IP_ROUTER_ALERT
:
599 static int do_ip_setsockopt(struct sock
*sk
, int level
,
600 int optname
, char __user
*optval
, unsigned int optlen
)
602 struct inet_sock
*inet
= inet_sk(sk
);
603 struct net
*net
= sock_net(sk
);
605 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
616 case IP_MTU_DISCOVER
:
618 case IP_ROUTER_ALERT
:
624 case IP_BIND_ADDRESS_NO_PORT
:
626 case IP_MULTICAST_TTL
:
627 case IP_MULTICAST_ALL
:
628 case IP_MULTICAST_LOOP
:
629 case IP_RECVORIGDSTADDR
:
631 case IP_RECVFRAGSIZE
:
632 if (optlen
>= sizeof(int)) {
633 if (get_user(val
, (int __user
*) optval
))
635 } else if (optlen
>= sizeof(char)) {
638 if (get_user(ucval
, (unsigned char __user
*) optval
))
644 /* If optlen==0, it is equivalent to val == 0 */
646 if (ip_mroute_opt(optname
))
647 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
657 struct ip_options_rcu
*old
, *opt
= NULL
;
661 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
665 old
= rcu_dereference_protected(inet
->inet_opt
,
666 lockdep_sock_is_held(sk
));
668 struct inet_connection_sock
*icsk
= inet_csk(sk
);
669 #if IS_ENABLED(CONFIG_IPV6)
670 if (sk
->sk_family
== PF_INET
||
671 (!((1 << sk
->sk_state
) &
672 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
673 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
676 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
678 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
679 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
680 #if IS_ENABLED(CONFIG_IPV6)
684 rcu_assign_pointer(inet
->inet_opt
, opt
);
691 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
693 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
697 inet
->cmsg_flags
|= IP_CMSG_TTL
;
699 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
703 inet
->cmsg_flags
|= IP_CMSG_TOS
;
705 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
709 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
711 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
715 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
717 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
721 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
723 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
725 case IP_RECVORIGDSTADDR
:
727 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
729 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
733 if (!(inet
->cmsg_flags
& IP_CMSG_CHECKSUM
)) {
734 inet_inc_convert_csum(sk
);
735 inet
->cmsg_flags
|= IP_CMSG_CHECKSUM
;
738 if (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) {
739 inet_dec_convert_csum(sk
);
740 inet
->cmsg_flags
&= ~IP_CMSG_CHECKSUM
;
744 case IP_RECVFRAGSIZE
:
745 if (sk
->sk_type
!= SOCK_RAW
&& sk
->sk_type
!= SOCK_DGRAM
)
748 inet
->cmsg_flags
|= IP_CMSG_RECVFRAGSIZE
;
750 inet
->cmsg_flags
&= ~IP_CMSG_RECVFRAGSIZE
;
752 case IP_TOS
: /* This sets both TOS and Precedence */
753 if (sk
->sk_type
== SOCK_STREAM
) {
754 val
&= ~INET_ECN_MASK
;
755 val
|= inet
->tos
& INET_ECN_MASK
;
757 if (inet
->tos
!= val
) {
759 sk
->sk_priority
= rt_tos2priority(val
);
766 if (val
!= -1 && (val
< 1 || val
> 255))
771 if (sk
->sk_type
!= SOCK_RAW
) {
775 inet
->hdrincl
= val
? 1 : 0;
778 if (sk
->sk_type
!= SOCK_RAW
) {
782 inet
->nodefrag
= val
? 1 : 0;
784 case IP_BIND_ADDRESS_NO_PORT
:
785 inet
->bind_address_no_port
= val
? 1 : 0;
787 case IP_MTU_DISCOVER
:
788 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_OMIT
)
790 inet
->pmtudisc
= val
;
793 inet
->recverr
= !!val
;
795 skb_queue_purge(&sk
->sk_error_queue
);
797 case IP_MULTICAST_TTL
:
798 if (sk
->sk_type
== SOCK_STREAM
)
804 if (val
< 0 || val
> 255)
808 case IP_MULTICAST_LOOP
:
811 inet
->mc_loop
= !!val
;
815 struct net_device
*dev
= NULL
;
818 if (optlen
!= sizeof(int))
821 ifindex
= (__force
int)ntohl((__force __be32
)val
);
828 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
829 err
= -EADDRNOTAVAIL
;
835 if (sk
->sk_bound_dev_if
)
838 inet
->uc_index
= ifindex
;
842 case IP_MULTICAST_IF
:
844 struct ip_mreqn mreq
;
845 struct net_device
*dev
= NULL
;
848 if (sk
->sk_type
== SOCK_STREAM
)
851 * Check the arguments are allowable
854 if (optlen
< sizeof(struct in_addr
))
858 if (optlen
>= sizeof(struct ip_mreqn
)) {
859 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
862 memset(&mreq
, 0, sizeof(mreq
));
863 if (optlen
>= sizeof(struct ip_mreq
)) {
864 if (copy_from_user(&mreq
, optval
,
865 sizeof(struct ip_mreq
)))
867 } else if (optlen
>= sizeof(struct in_addr
)) {
868 if (copy_from_user(&mreq
.imr_address
, optval
,
869 sizeof(struct in_addr
)))
874 if (!mreq
.imr_ifindex
) {
875 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
881 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
883 mreq
.imr_ifindex
= dev
->ifindex
;
885 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
888 err
= -EADDRNOTAVAIL
;
892 midx
= l3mdev_master_ifindex(dev
);
897 if (sk
->sk_bound_dev_if
&&
898 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
&&
899 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
902 inet
->mc_index
= mreq
.imr_ifindex
;
903 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
908 case IP_ADD_MEMBERSHIP
:
909 case IP_DROP_MEMBERSHIP
:
911 struct ip_mreqn mreq
;
914 if (inet_sk(sk
)->is_icsk
)
917 if (optlen
< sizeof(struct ip_mreq
))
920 if (optlen
>= sizeof(struct ip_mreqn
)) {
921 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
924 memset(&mreq
, 0, sizeof(mreq
));
925 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
929 if (optname
== IP_ADD_MEMBERSHIP
)
930 err
= ip_mc_join_group(sk
, &mreq
);
932 err
= ip_mc_leave_group(sk
, &mreq
);
937 struct ip_msfilter
*msf
;
939 if (optlen
< IP_MSFILTER_SIZE(0))
941 if (optlen
> sysctl_optmem_max
) {
945 msf
= kmalloc(optlen
, GFP_KERNEL
);
951 if (copy_from_user(msf
, optval
, optlen
)) {
955 /* numsrc >= (1G-4) overflow in 32 bits */
956 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
957 msf
->imsf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
962 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
967 err
= ip_mc_msfilter(sk
, msf
, 0);
971 case IP_BLOCK_SOURCE
:
972 case IP_UNBLOCK_SOURCE
:
973 case IP_ADD_SOURCE_MEMBERSHIP
:
974 case IP_DROP_SOURCE_MEMBERSHIP
:
976 struct ip_mreq_source mreqs
;
979 if (optlen
!= sizeof(struct ip_mreq_source
))
981 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
985 if (optname
== IP_BLOCK_SOURCE
) {
986 omode
= MCAST_EXCLUDE
;
988 } else if (optname
== IP_UNBLOCK_SOURCE
) {
989 omode
= MCAST_EXCLUDE
;
991 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
992 struct ip_mreqn mreq
;
994 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
995 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
996 mreq
.imr_ifindex
= 0;
997 err
= ip_mc_join_group(sk
, &mreq
);
998 if (err
&& err
!= -EADDRINUSE
)
1000 omode
= MCAST_INCLUDE
;
1002 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1003 omode
= MCAST_INCLUDE
;
1006 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
1009 case MCAST_JOIN_GROUP
:
1010 case MCAST_LEAVE_GROUP
:
1012 struct group_req greq
;
1013 struct sockaddr_in
*psin
;
1014 struct ip_mreqn mreq
;
1016 if (optlen
< sizeof(struct group_req
))
1019 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
1021 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
1022 if (psin
->sin_family
!= AF_INET
)
1024 memset(&mreq
, 0, sizeof(mreq
));
1025 mreq
.imr_multiaddr
= psin
->sin_addr
;
1026 mreq
.imr_ifindex
= greq
.gr_interface
;
1028 if (optname
== MCAST_JOIN_GROUP
)
1029 err
= ip_mc_join_group(sk
, &mreq
);
1031 err
= ip_mc_leave_group(sk
, &mreq
);
1034 case MCAST_JOIN_SOURCE_GROUP
:
1035 case MCAST_LEAVE_SOURCE_GROUP
:
1036 case MCAST_BLOCK_SOURCE
:
1037 case MCAST_UNBLOCK_SOURCE
:
1039 struct group_source_req greqs
;
1040 struct ip_mreq_source mreqs
;
1041 struct sockaddr_in
*psin
;
1044 if (optlen
!= sizeof(struct group_source_req
))
1046 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
1050 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
1051 greqs
.gsr_source
.ss_family
!= AF_INET
) {
1052 err
= -EADDRNOTAVAIL
;
1055 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1056 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
1057 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
1058 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
1059 mreqs
.imr_interface
= 0; /* use index for mc_source */
1061 if (optname
== MCAST_BLOCK_SOURCE
) {
1062 omode
= MCAST_EXCLUDE
;
1064 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
1065 omode
= MCAST_EXCLUDE
;
1067 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
1068 struct ip_mreqn mreq
;
1070 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1071 mreq
.imr_multiaddr
= psin
->sin_addr
;
1072 mreq
.imr_address
.s_addr
= 0;
1073 mreq
.imr_ifindex
= greqs
.gsr_interface
;
1074 err
= ip_mc_join_group(sk
, &mreq
);
1075 if (err
&& err
!= -EADDRINUSE
)
1077 greqs
.gsr_interface
= mreq
.imr_ifindex
;
1078 omode
= MCAST_INCLUDE
;
1080 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1081 omode
= MCAST_INCLUDE
;
1084 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
1085 greqs
.gsr_interface
);
1088 case MCAST_MSFILTER
:
1090 struct sockaddr_in
*psin
;
1091 struct ip_msfilter
*msf
= NULL
;
1092 struct group_filter
*gsf
= NULL
;
1093 int msize
, i
, ifindex
;
1095 if (optlen
< GROUP_FILTER_SIZE(0))
1097 if (optlen
> sysctl_optmem_max
) {
1101 gsf
= kmalloc(optlen
, GFP_KERNEL
);
1107 if (copy_from_user(gsf
, optval
, optlen
))
1110 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1111 if (gsf
->gf_numsrc
>= 0x1ffffff ||
1112 gsf
->gf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
1116 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
1120 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
1121 msf
= kmalloc(msize
, GFP_KERNEL
);
1126 ifindex
= gsf
->gf_interface
;
1127 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
1128 if (psin
->sin_family
!= AF_INET
) {
1129 err
= -EADDRNOTAVAIL
;
1132 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
1133 msf
->imsf_interface
= 0;
1134 msf
->imsf_fmode
= gsf
->gf_fmode
;
1135 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
1136 err
= -EADDRNOTAVAIL
;
1137 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
1138 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
1140 if (psin
->sin_family
!= AF_INET
)
1142 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
1147 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
1153 case IP_MULTICAST_ALL
:
1156 if (val
!= 0 && val
!= 1)
1160 case IP_ROUTER_ALERT
:
1161 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
1167 inet
->freebind
= !!val
;
1170 case IP_IPSEC_POLICY
:
1171 case IP_XFRM_POLICY
:
1173 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1175 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
1178 case IP_TRANSPARENT
:
1179 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
1180 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1186 inet
->transparent
= !!val
;
1192 if (val
< 0 || val
> 255)
1194 inet
->min_ttl
= val
;
1214 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1218 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1219 * destination in skb->cb[] before dst drop.
1220 * This way, receiver doesn't make cache line misses to read rtable.
1222 void ipv4_pktinfo_prepare(const struct sock
*sk
, struct sk_buff
*skb
)
1224 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1225 bool prepare
= (inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
) ||
1228 if (prepare
&& skb_rtable(skb
)) {
1229 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1230 * which has interface index (iif) as the first member of the
1231 * underlying inet{6}_skb_parm struct. This code then overlays
1232 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1233 * element so the iif is picked up from the prior IPCB. If iif
1234 * is the loopback interface, then return the sending interface
1235 * (e.g., process binds socket to eth0 for Tx which is
1236 * redirected to loopback in the rtable/dst).
1238 if (pktinfo
->ipi_ifindex
== LOOPBACK_IFINDEX
)
1239 pktinfo
->ipi_ifindex
= inet_iif(skb
);
1241 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1243 pktinfo
->ipi_ifindex
= 0;
1244 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1246 /* We need to keep the dst for __ip_options_echo()
1247 * We could restrict the test to opt.ts_needtime || opt.srr,
1248 * but the following is good enough as IP options are not often used.
1250 if (unlikely(IPCB(skb
)->opt
.optlen
))
1256 int ip_setsockopt(struct sock
*sk
, int level
,
1257 int optname
, char __user
*optval
, unsigned int optlen
)
1261 if (level
!= SOL_IP
)
1262 return -ENOPROTOOPT
;
1264 err
= do_ip_setsockopt(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_HDRINCL
&&
1268 optname
!= IP_IPSEC_POLICY
&&
1269 optname
!= IP_XFRM_POLICY
&&
1270 !ip_mroute_opt(optname
)) {
1272 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1278 EXPORT_SYMBOL(ip_setsockopt
);
1280 #ifdef CONFIG_COMPAT
1281 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1282 char __user
*optval
, unsigned int optlen
)
1286 if (level
!= SOL_IP
)
1287 return -ENOPROTOOPT
;
1289 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1290 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1293 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1294 #ifdef CONFIG_NETFILTER
1295 /* we need to exclude all possible ENOPROTOOPTs except default case */
1296 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1297 optname
!= IP_IPSEC_POLICY
&&
1298 optname
!= IP_XFRM_POLICY
&&
1299 !ip_mroute_opt(optname
)) {
1301 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1308 EXPORT_SYMBOL(compat_ip_setsockopt
);
1312 * Get the options. Note for future reference. The GET of IP options gets
1313 * the _received_ ones. The set sets the _sent_ ones.
1316 static bool getsockopt_needs_rtnl(int optname
)
1320 case MCAST_MSFILTER
:
1326 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1327 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1329 struct inet_sock
*inet
= inet_sk(sk
);
1330 bool needs_rtnl
= getsockopt_needs_rtnl(optname
);
1334 if (level
!= SOL_IP
)
1337 if (ip_mroute_opt(optname
))
1338 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1340 if (get_user(len
, optlen
))
1352 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1353 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1354 struct ip_options_rcu
*inet_opt
;
1356 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1357 lockdep_sock_is_held(sk
));
1360 memcpy(optbuf
, &inet_opt
->opt
,
1361 sizeof(struct ip_options
) +
1362 inet_opt
->opt
.optlen
);
1365 if (opt
->optlen
== 0)
1366 return put_user(0, optlen
);
1368 ip_options_undo(opt
);
1370 len
= min_t(unsigned int, len
, opt
->optlen
);
1371 if (put_user(len
, optlen
))
1373 if (copy_to_user(optval
, opt
->__data
, len
))
1378 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1381 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1384 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1387 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1390 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1393 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1395 case IP_RECVORIGDSTADDR
:
1396 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1399 val
= (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) != 0;
1401 case IP_RECVFRAGSIZE
:
1402 val
= (inet
->cmsg_flags
& IP_CMSG_RECVFRAGSIZE
) != 0;
1409 struct net
*net
= sock_net(sk
);
1410 val
= (inet
->uc_ttl
== -1 ?
1411 net
->ipv4
.sysctl_ip_default_ttl
:
1416 val
= inet
->hdrincl
;
1419 val
= inet
->nodefrag
;
1421 case IP_BIND_ADDRESS_NO_PORT
:
1422 val
= inet
->bind_address_no_port
;
1424 case IP_MTU_DISCOVER
:
1425 val
= inet
->pmtudisc
;
1429 struct dst_entry
*dst
;
1431 dst
= sk_dst_get(sk
);
1443 val
= inet
->recverr
;
1445 case IP_MULTICAST_TTL
:
1448 case IP_MULTICAST_LOOP
:
1449 val
= inet
->mc_loop
;
1452 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1454 case IP_MULTICAST_IF
:
1456 struct in_addr addr
;
1457 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1458 addr
.s_addr
= inet
->mc_addr
;
1461 if (put_user(len
, optlen
))
1463 if (copy_to_user(optval
, &addr
, len
))
1469 struct ip_msfilter msf
;
1471 if (len
< IP_MSFILTER_SIZE(0)) {
1475 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1479 err
= ip_mc_msfget(sk
, &msf
,
1480 (struct ip_msfilter __user
*)optval
, optlen
);
1483 case MCAST_MSFILTER
:
1485 struct group_filter gsf
;
1487 if (len
< GROUP_FILTER_SIZE(0)) {
1491 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1495 err
= ip_mc_gsfget(sk
, &gsf
,
1496 (struct group_filter __user
*)optval
,
1500 case IP_MULTICAST_ALL
:
1509 if (sk
->sk_type
!= SOCK_STREAM
)
1510 return -ENOPROTOOPT
;
1512 msg
.msg_control
= (__force
void *) optval
;
1513 msg
.msg_controllen
= len
;
1514 msg
.msg_flags
= flags
;
1516 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1517 struct in_pktinfo info
;
1519 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1520 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1521 info
.ipi_ifindex
= inet
->mc_index
;
1522 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1524 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1525 int hlim
= inet
->mc_ttl
;
1526 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1528 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1529 int tos
= inet
->rcv_tos
;
1530 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1532 len
-= msg
.msg_controllen
;
1533 return put_user(len
, optlen
);
1536 val
= inet
->freebind
;
1538 case IP_TRANSPARENT
:
1539 val
= inet
->transparent
;
1542 val
= inet
->min_ttl
;
1546 return -ENOPROTOOPT
;
1550 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1551 unsigned char ucval
= (unsigned char)val
;
1553 if (put_user(len
, optlen
))
1555 if (copy_to_user(optval
, &ucval
, 1))
1558 len
= min_t(unsigned int, sizeof(int), len
);
1559 if (put_user(len
, optlen
))
1561 if (copy_to_user(optval
, &val
, len
))
1573 int ip_getsockopt(struct sock
*sk
, int level
,
1574 int optname
, char __user
*optval
, int __user
*optlen
)
1578 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1579 #ifdef CONFIG_NETFILTER
1580 /* we need to exclude all possible ENOPROTOOPTs except default case */
1581 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1582 !ip_mroute_opt(optname
)) {
1585 if (get_user(len
, optlen
))
1589 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1593 err
= put_user(len
, optlen
);
1599 EXPORT_SYMBOL(ip_getsockopt
);
1601 #ifdef CONFIG_COMPAT
1602 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1603 char __user
*optval
, int __user
*optlen
)
1607 if (optname
== MCAST_MSFILTER
)
1608 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1611 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1614 #ifdef CONFIG_NETFILTER
1615 /* we need to exclude all possible ENOPROTOOPTs except default case */
1616 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1617 !ip_mroute_opt(optname
)) {
1620 if (get_user(len
, optlen
))
1624 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1627 err
= put_user(len
, optlen
);
1633 EXPORT_SYMBOL(compat_ip_getsockopt
);