1 // SPDX-License-Identifier: GPL-2.0
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
12 * Many : Split from ip.c , see ip.c for history.
13 * Martin Mares : TOS setting fixed.
14 * Alan Cox : Fixed a couple of oopses in Martin's
16 * Mike McLagan : Routing by source
19 #include <linux/module.h>
20 #include <linux/types.h>
22 #include <linux/skbuff.h>
24 #include <linux/icmp.h>
25 #include <linux/inetdevice.h>
26 #include <linux/netdevice.h>
27 #include <linux/slab.h>
31 #include <net/tcp_states.h>
32 #include <linux/udp.h>
33 #include <linux/igmp.h>
34 #include <linux/netfilter.h>
35 #include <linux/route.h>
36 #include <linux/mroute.h>
37 #include <net/inet_ecn.h>
38 #include <net/route.h>
40 #include <net/compat.h>
41 #include <net/checksum.h>
42 #if IS_ENABLED(CONFIG_IPV6)
43 #include <net/transp_v6.h>
45 #include <net/ip_fib.h>
47 #include <linux/errqueue.h>
48 #include <linux/uaccess.h>
51 * SOL_IP control messages.
54 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
56 struct in_pktinfo info
= *PKTINFO_SKB_CB(skb
);
58 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
60 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
63 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
65 int ttl
= ip_hdr(skb
)->ttl
;
66 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
69 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
71 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
74 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
76 if (IPCB(skb
)->opt
.optlen
== 0)
79 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
84 static void ip_cmsg_recv_retopts(struct net
*net
, struct msghdr
*msg
,
87 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
88 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
90 if (IPCB(skb
)->opt
.optlen
== 0)
93 if (ip_options_echo(net
, opt
, skb
)) {
94 msg
->msg_flags
|= MSG_CTRUNC
;
99 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
102 static void ip_cmsg_recv_fragsize(struct msghdr
*msg
, struct sk_buff
*skb
)
106 if (IPCB(skb
)->frag_max_size
== 0)
109 val
= IPCB(skb
)->frag_max_size
;
110 put_cmsg(msg
, SOL_IP
, IP_RECVFRAGSIZE
, sizeof(val
), &val
);
113 static void ip_cmsg_recv_checksum(struct msghdr
*msg
, struct sk_buff
*skb
,
114 int tlen
, int offset
)
116 __wsum csum
= skb
->csum
;
118 if (skb
->ip_summed
!= CHECKSUM_COMPLETE
)
122 int tend_off
= skb_transport_offset(skb
) + tlen
;
123 csum
= csum_sub(csum
, skb_checksum(skb
, tend_off
, offset
, 0));
126 put_cmsg(msg
, SOL_IP
, IP_CHECKSUM
, sizeof(__wsum
), &csum
);
129 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
135 err
= security_socket_getpeersec_dgram(NULL
, skb
, &secid
);
139 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
143 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
144 security_release_secctx(secdata
, seclen
);
147 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
149 struct sockaddr_in sin
;
150 const struct iphdr
*iph
= ip_hdr(skb
);
151 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
153 if (skb_transport_offset(skb
) + 4 > (int)skb
->len
)
156 /* All current transport protocols have the port numbers in the
157 * first four bytes of the transport header and this function is
158 * written with this assumption in mind.
161 sin
.sin_family
= AF_INET
;
162 sin
.sin_addr
.s_addr
= iph
->daddr
;
163 sin
.sin_port
= ports
[1];
164 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
166 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
169 void ip_cmsg_recv_offset(struct msghdr
*msg
, struct sock
*sk
,
170 struct sk_buff
*skb
, int tlen
, int offset
)
172 struct inet_sock
*inet
= inet_sk(sk
);
173 unsigned int flags
= inet
->cmsg_flags
;
175 /* Ordered by supposed usage frequency */
176 if (flags
& IP_CMSG_PKTINFO
) {
177 ip_cmsg_recv_pktinfo(msg
, skb
);
179 flags
&= ~IP_CMSG_PKTINFO
;
184 if (flags
& IP_CMSG_TTL
) {
185 ip_cmsg_recv_ttl(msg
, skb
);
187 flags
&= ~IP_CMSG_TTL
;
192 if (flags
& IP_CMSG_TOS
) {
193 ip_cmsg_recv_tos(msg
, skb
);
195 flags
&= ~IP_CMSG_TOS
;
200 if (flags
& IP_CMSG_RECVOPTS
) {
201 ip_cmsg_recv_opts(msg
, skb
);
203 flags
&= ~IP_CMSG_RECVOPTS
;
208 if (flags
& IP_CMSG_RETOPTS
) {
209 ip_cmsg_recv_retopts(sock_net(sk
), msg
, skb
);
211 flags
&= ~IP_CMSG_RETOPTS
;
216 if (flags
& IP_CMSG_PASSSEC
) {
217 ip_cmsg_recv_security(msg
, skb
);
219 flags
&= ~IP_CMSG_PASSSEC
;
224 if (flags
& IP_CMSG_ORIGDSTADDR
) {
225 ip_cmsg_recv_dstaddr(msg
, skb
);
227 flags
&= ~IP_CMSG_ORIGDSTADDR
;
232 if (flags
& IP_CMSG_CHECKSUM
)
233 ip_cmsg_recv_checksum(msg
, skb
, tlen
, offset
);
235 if (flags
& IP_CMSG_RECVFRAGSIZE
)
236 ip_cmsg_recv_fragsize(msg
, skb
);
238 EXPORT_SYMBOL(ip_cmsg_recv_offset
);
240 int ip_cmsg_send(struct sock
*sk
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
,
244 struct cmsghdr
*cmsg
;
245 struct net
*net
= sock_net(sk
);
247 for_each_cmsghdr(cmsg
, msg
) {
248 if (!CMSG_OK(msg
, cmsg
))
250 #if IS_ENABLED(CONFIG_IPV6)
252 cmsg
->cmsg_level
== SOL_IPV6
&&
253 cmsg
->cmsg_type
== IPV6_PKTINFO
) {
254 struct in6_pktinfo
*src_info
;
256 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*src_info
)))
258 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
259 if (!ipv6_addr_v4mapped(&src_info
->ipi6_addr
))
261 ipc
->oif
= src_info
->ipi6_ifindex
;
262 ipc
->addr
= src_info
->ipi6_addr
.s6_addr32
[3];
266 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
267 err
= __sock_cmsg_send(sk
, msg
, cmsg
, &ipc
->sockc
);
273 if (cmsg
->cmsg_level
!= SOL_IP
)
275 switch (cmsg
->cmsg_type
) {
277 err
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
279 /* Our caller is responsible for freeing ipc->opt */
280 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
281 err
< 40 ? err
: 40);
287 struct in_pktinfo
*info
;
288 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
290 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
291 ipc
->oif
= info
->ipi_ifindex
;
292 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
296 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
298 val
= *(int *)CMSG_DATA(cmsg
);
299 if (val
< 1 || val
> 255)
304 if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)))
305 val
= *(int *)CMSG_DATA(cmsg
);
306 else if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(u8
)))
307 val
= *(u8
*)CMSG_DATA(cmsg
);
310 if (val
< 0 || val
> 255)
313 ipc
->priority
= rt_tos2priority(ipc
->tos
);
324 /* Special input handler for packets caught by router alert option.
325 They are selected only by protocol field, and then processed likely
326 local ones; but only if someone wants them! Otherwise, router
327 not running rsvpd will kill RSVP.
329 It is user level problem, what it will make with them.
330 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
331 but receiver should be enough clever f.e. to forward mtrace requests,
332 sent to multicast group to reach destination designated router.
334 struct ip_ra_chain __rcu
*ip_ra_chain
;
337 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
339 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
341 sock_put(ra
->saved_sk
);
345 int ip_ra_control(struct sock
*sk
, unsigned char on
,
346 void (*destructor
)(struct sock
*))
348 struct ip_ra_chain
*ra
, *new_ra
;
349 struct ip_ra_chain __rcu
**rap
;
351 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
354 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
356 for (rap
= &ip_ra_chain
;
357 (ra
= rtnl_dereference(*rap
)) != NULL
;
364 /* dont let ip_call_ra_chain() use sk again */
366 RCU_INIT_POINTER(*rap
, ra
->next
);
371 * Delay sock_put(sk) and kfree(ra) after one rcu grace
372 * period. This guarantee ip_call_ra_chain() dont need
373 * to mess with socket refcounts.
376 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
383 new_ra
->destructor
= destructor
;
385 RCU_INIT_POINTER(new_ra
->next
, ra
);
386 rcu_assign_pointer(*rap
, new_ra
);
392 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
393 __be16 port
, u32 info
, u8
*payload
)
395 struct sock_exterr_skb
*serr
;
397 skb
= skb_clone(skb
, GFP_ATOMIC
);
401 serr
= SKB_EXT_ERR(skb
);
402 serr
->ee
.ee_errno
= err
;
403 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
404 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
405 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
407 serr
->ee
.ee_info
= info
;
408 serr
->ee
.ee_data
= 0;
409 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
410 skb_network_header(skb
);
413 if (skb_pull(skb
, payload
- skb
->data
)) {
414 skb_reset_transport_header(skb
);
415 if (sock_queue_err_skb(sk
, skb
) == 0)
421 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
423 struct inet_sock
*inet
= inet_sk(sk
);
424 struct sock_exterr_skb
*serr
;
431 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
435 skb_put(skb
, sizeof(struct iphdr
));
436 skb_reset_network_header(skb
);
440 serr
= SKB_EXT_ERR(skb
);
441 serr
->ee
.ee_errno
= err
;
442 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
443 serr
->ee
.ee_type
= 0;
444 serr
->ee
.ee_code
= 0;
446 serr
->ee
.ee_info
= info
;
447 serr
->ee
.ee_data
= 0;
448 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
451 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
452 skb_reset_transport_header(skb
);
454 if (sock_queue_err_skb(sk
, skb
))
458 /* For some errors we have valid addr_offset even with zero payload and
459 * zero port. Also, addr_offset should be supported if port is set.
461 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb
*serr
)
463 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
464 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
467 /* IPv4 supports cmsg on all imcp errors and some timestamps
469 * Timestamp code paths do not initialize the fields expected by cmsg:
470 * the PKTINFO fields in skb->cb[]. Fill those in here.
472 static bool ipv4_datagram_support_cmsg(const struct sock
*sk
,
476 struct in_pktinfo
*info
;
478 if (ee_origin
== SO_EE_ORIGIN_ICMP
)
481 if (ee_origin
== SO_EE_ORIGIN_LOCAL
)
484 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
485 * timestamp with egress dev. Not possible for packets without iif
486 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
488 info
= PKTINFO_SKB_CB(skb
);
489 if (!(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_CMSG
) ||
493 info
->ipi_spec_dst
.s_addr
= ip_hdr(skb
)->saddr
;
498 * Handle MSG_ERRQUEUE
500 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
502 struct sock_exterr_skb
*serr
;
504 DECLARE_SOCKADDR(struct sockaddr_in
*, sin
, msg
->msg_name
);
506 struct sock_extended_err ee
;
507 struct sockaddr_in offender
;
512 WARN_ON_ONCE(sk
->sk_family
== AF_INET6
);
515 skb
= sock_dequeue_err_skb(sk
);
521 msg
->msg_flags
|= MSG_TRUNC
;
524 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
529 sock_recv_timestamp(msg
, sk
, skb
);
531 serr
= SKB_EXT_ERR(skb
);
533 if (sin
&& ipv4_datagram_support_addr(serr
)) {
534 sin
->sin_family
= AF_INET
;
535 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
537 sin
->sin_port
= serr
->port
;
538 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
539 *addr_len
= sizeof(*sin
);
542 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
543 sin
= &errhdr
.offender
;
544 memset(sin
, 0, sizeof(*sin
));
546 if (ipv4_datagram_support_cmsg(sk
, skb
, serr
->ee
.ee_origin
)) {
547 sin
->sin_family
= AF_INET
;
548 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
549 if (inet_sk(sk
)->cmsg_flags
)
550 ip_cmsg_recv(msg
, skb
);
553 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
555 /* Now we could try to dump offended packet options */
557 msg
->msg_flags
|= MSG_ERRQUEUE
;
567 * Socket option code for IP. This is the end of the line after any
568 * TCP,UDP etc options on an IP socket.
570 static bool setsockopt_needs_rtnl(int optname
)
573 case IP_ADD_MEMBERSHIP
:
574 case IP_ADD_SOURCE_MEMBERSHIP
:
575 case IP_BLOCK_SOURCE
:
576 case IP_DROP_MEMBERSHIP
:
577 case IP_DROP_SOURCE_MEMBERSHIP
:
579 case IP_UNBLOCK_SOURCE
:
580 case MCAST_BLOCK_SOURCE
:
582 case MCAST_JOIN_GROUP
:
583 case MCAST_JOIN_SOURCE_GROUP
:
584 case MCAST_LEAVE_GROUP
:
585 case MCAST_LEAVE_SOURCE_GROUP
:
586 case MCAST_UNBLOCK_SOURCE
:
587 case IP_ROUTER_ALERT
:
593 static int do_ip_setsockopt(struct sock
*sk
, int level
,
594 int optname
, char __user
*optval
, unsigned int optlen
)
596 struct inet_sock
*inet
= inet_sk(sk
);
597 struct net
*net
= sock_net(sk
);
599 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
610 case IP_MTU_DISCOVER
:
612 case IP_ROUTER_ALERT
:
618 case IP_BIND_ADDRESS_NO_PORT
:
620 case IP_MULTICAST_TTL
:
621 case IP_MULTICAST_ALL
:
622 case IP_MULTICAST_LOOP
:
623 case IP_RECVORIGDSTADDR
:
625 case IP_RECVFRAGSIZE
:
626 if (optlen
>= sizeof(int)) {
627 if (get_user(val
, (int __user
*) optval
))
629 } else if (optlen
>= sizeof(char)) {
632 if (get_user(ucval
, (unsigned char __user
*) optval
))
638 /* If optlen==0, it is equivalent to val == 0 */
640 if (ip_mroute_opt(optname
))
641 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
651 struct ip_options_rcu
*old
, *opt
= NULL
;
655 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
659 old
= rcu_dereference_protected(inet
->inet_opt
,
660 lockdep_sock_is_held(sk
));
662 struct inet_connection_sock
*icsk
= inet_csk(sk
);
663 #if IS_ENABLED(CONFIG_IPV6)
664 if (sk
->sk_family
== PF_INET
||
665 (!((1 << sk
->sk_state
) &
666 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
667 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
670 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
672 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
673 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
674 #if IS_ENABLED(CONFIG_IPV6)
678 rcu_assign_pointer(inet
->inet_opt
, opt
);
685 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
687 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
691 inet
->cmsg_flags
|= IP_CMSG_TTL
;
693 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
697 inet
->cmsg_flags
|= IP_CMSG_TOS
;
699 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
703 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
705 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
709 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
711 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
715 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
717 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
719 case IP_RECVORIGDSTADDR
:
721 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
723 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
727 if (!(inet
->cmsg_flags
& IP_CMSG_CHECKSUM
)) {
728 inet_inc_convert_csum(sk
);
729 inet
->cmsg_flags
|= IP_CMSG_CHECKSUM
;
732 if (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) {
733 inet_dec_convert_csum(sk
);
734 inet
->cmsg_flags
&= ~IP_CMSG_CHECKSUM
;
738 case IP_RECVFRAGSIZE
:
739 if (sk
->sk_type
!= SOCK_RAW
&& sk
->sk_type
!= SOCK_DGRAM
)
742 inet
->cmsg_flags
|= IP_CMSG_RECVFRAGSIZE
;
744 inet
->cmsg_flags
&= ~IP_CMSG_RECVFRAGSIZE
;
746 case IP_TOS
: /* This sets both TOS and Precedence */
747 if (sk
->sk_type
== SOCK_STREAM
) {
748 val
&= ~INET_ECN_MASK
;
749 val
|= inet
->tos
& INET_ECN_MASK
;
751 if (inet
->tos
!= val
) {
753 sk
->sk_priority
= rt_tos2priority(val
);
760 if (val
!= -1 && (val
< 1 || val
> 255))
765 if (sk
->sk_type
!= SOCK_RAW
) {
769 inet
->hdrincl
= val
? 1 : 0;
772 if (sk
->sk_type
!= SOCK_RAW
) {
776 inet
->nodefrag
= val
? 1 : 0;
778 case IP_BIND_ADDRESS_NO_PORT
:
779 inet
->bind_address_no_port
= val
? 1 : 0;
781 case IP_MTU_DISCOVER
:
782 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_OMIT
)
784 inet
->pmtudisc
= val
;
787 inet
->recverr
= !!val
;
789 skb_queue_purge(&sk
->sk_error_queue
);
791 case IP_MULTICAST_TTL
:
792 if (sk
->sk_type
== SOCK_STREAM
)
798 if (val
< 0 || val
> 255)
802 case IP_MULTICAST_LOOP
:
805 inet
->mc_loop
= !!val
;
809 struct net_device
*dev
= NULL
;
813 if (optlen
!= sizeof(int))
816 ifindex
= (__force
int)ntohl((__force __be32
)val
);
823 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
824 err
= -EADDRNOTAVAIL
;
828 midx
= l3mdev_master_ifindex(dev
);
832 if (sk
->sk_bound_dev_if
&&
833 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
836 inet
->uc_index
= ifindex
;
840 case IP_MULTICAST_IF
:
842 struct ip_mreqn mreq
;
843 struct net_device
*dev
= NULL
;
846 if (sk
->sk_type
== SOCK_STREAM
)
849 * Check the arguments are allowable
852 if (optlen
< sizeof(struct in_addr
))
856 if (optlen
>= sizeof(struct ip_mreqn
)) {
857 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
860 memset(&mreq
, 0, sizeof(mreq
));
861 if (optlen
>= sizeof(struct ip_mreq
)) {
862 if (copy_from_user(&mreq
, optval
,
863 sizeof(struct ip_mreq
)))
865 } else if (optlen
>= sizeof(struct in_addr
)) {
866 if (copy_from_user(&mreq
.imr_address
, optval
,
867 sizeof(struct in_addr
)))
872 if (!mreq
.imr_ifindex
) {
873 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
879 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
881 mreq
.imr_ifindex
= dev
->ifindex
;
883 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
886 err
= -EADDRNOTAVAIL
;
890 midx
= l3mdev_master_ifindex(dev
);
895 if (sk
->sk_bound_dev_if
&&
896 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
&&
897 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
900 inet
->mc_index
= mreq
.imr_ifindex
;
901 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
906 case IP_ADD_MEMBERSHIP
:
907 case IP_DROP_MEMBERSHIP
:
909 struct ip_mreqn mreq
;
912 if (inet_sk(sk
)->is_icsk
)
915 if (optlen
< sizeof(struct ip_mreq
))
918 if (optlen
>= sizeof(struct ip_mreqn
)) {
919 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
922 memset(&mreq
, 0, sizeof(mreq
));
923 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
927 if (optname
== IP_ADD_MEMBERSHIP
)
928 err
= ip_mc_join_group(sk
, &mreq
);
930 err
= ip_mc_leave_group(sk
, &mreq
);
935 struct ip_msfilter
*msf
;
937 if (optlen
< IP_MSFILTER_SIZE(0))
939 if (optlen
> sysctl_optmem_max
) {
943 msf
= memdup_user(optval
, optlen
);
948 /* numsrc >= (1G-4) overflow in 32 bits */
949 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
950 msf
->imsf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
955 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
960 err
= ip_mc_msfilter(sk
, msf
, 0);
964 case IP_BLOCK_SOURCE
:
965 case IP_UNBLOCK_SOURCE
:
966 case IP_ADD_SOURCE_MEMBERSHIP
:
967 case IP_DROP_SOURCE_MEMBERSHIP
:
969 struct ip_mreq_source mreqs
;
972 if (optlen
!= sizeof(struct ip_mreq_source
))
974 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
978 if (optname
== IP_BLOCK_SOURCE
) {
979 omode
= MCAST_EXCLUDE
;
981 } else if (optname
== IP_UNBLOCK_SOURCE
) {
982 omode
= MCAST_EXCLUDE
;
984 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
985 struct ip_mreqn mreq
;
987 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
988 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
989 mreq
.imr_ifindex
= 0;
990 err
= ip_mc_join_group(sk
, &mreq
);
991 if (err
&& err
!= -EADDRINUSE
)
993 omode
= MCAST_INCLUDE
;
995 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
996 omode
= MCAST_INCLUDE
;
999 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
1002 case MCAST_JOIN_GROUP
:
1003 case MCAST_LEAVE_GROUP
:
1005 struct group_req greq
;
1006 struct sockaddr_in
*psin
;
1007 struct ip_mreqn mreq
;
1009 if (optlen
< sizeof(struct group_req
))
1012 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
1014 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
1015 if (psin
->sin_family
!= AF_INET
)
1017 memset(&mreq
, 0, sizeof(mreq
));
1018 mreq
.imr_multiaddr
= psin
->sin_addr
;
1019 mreq
.imr_ifindex
= greq
.gr_interface
;
1021 if (optname
== MCAST_JOIN_GROUP
)
1022 err
= ip_mc_join_group(sk
, &mreq
);
1024 err
= ip_mc_leave_group(sk
, &mreq
);
1027 case MCAST_JOIN_SOURCE_GROUP
:
1028 case MCAST_LEAVE_SOURCE_GROUP
:
1029 case MCAST_BLOCK_SOURCE
:
1030 case MCAST_UNBLOCK_SOURCE
:
1032 struct group_source_req greqs
;
1033 struct ip_mreq_source mreqs
;
1034 struct sockaddr_in
*psin
;
1037 if (optlen
!= sizeof(struct group_source_req
))
1039 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
1043 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
1044 greqs
.gsr_source
.ss_family
!= AF_INET
) {
1045 err
= -EADDRNOTAVAIL
;
1048 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1049 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
1050 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
1051 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
1052 mreqs
.imr_interface
= 0; /* use index for mc_source */
1054 if (optname
== MCAST_BLOCK_SOURCE
) {
1055 omode
= MCAST_EXCLUDE
;
1057 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
1058 omode
= MCAST_EXCLUDE
;
1060 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
1061 struct ip_mreqn mreq
;
1063 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1064 mreq
.imr_multiaddr
= psin
->sin_addr
;
1065 mreq
.imr_address
.s_addr
= 0;
1066 mreq
.imr_ifindex
= greqs
.gsr_interface
;
1067 err
= ip_mc_join_group(sk
, &mreq
);
1068 if (err
&& err
!= -EADDRINUSE
)
1070 greqs
.gsr_interface
= mreq
.imr_ifindex
;
1071 omode
= MCAST_INCLUDE
;
1073 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1074 omode
= MCAST_INCLUDE
;
1077 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
1078 greqs
.gsr_interface
);
1081 case MCAST_MSFILTER
:
1083 struct sockaddr_in
*psin
;
1084 struct ip_msfilter
*msf
= NULL
;
1085 struct group_filter
*gsf
= NULL
;
1086 int msize
, i
, ifindex
;
1088 if (optlen
< GROUP_FILTER_SIZE(0))
1090 if (optlen
> sysctl_optmem_max
) {
1094 gsf
= memdup_user(optval
, optlen
);
1100 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1101 if (gsf
->gf_numsrc
>= 0x1ffffff ||
1102 gsf
->gf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
1106 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
1110 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
1111 msf
= kmalloc(msize
, GFP_KERNEL
);
1116 ifindex
= gsf
->gf_interface
;
1117 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
1118 if (psin
->sin_family
!= AF_INET
) {
1119 err
= -EADDRNOTAVAIL
;
1122 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
1123 msf
->imsf_interface
= 0;
1124 msf
->imsf_fmode
= gsf
->gf_fmode
;
1125 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
1126 err
= -EADDRNOTAVAIL
;
1127 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
1128 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
1130 if (psin
->sin_family
!= AF_INET
)
1132 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
1137 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
1143 case IP_MULTICAST_ALL
:
1146 if (val
!= 0 && val
!= 1)
1150 case IP_ROUTER_ALERT
:
1151 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
1157 inet
->freebind
= !!val
;
1160 case IP_IPSEC_POLICY
:
1161 case IP_XFRM_POLICY
:
1163 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1165 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
1168 case IP_TRANSPARENT
:
1169 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
1170 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1176 inet
->transparent
= !!val
;
1182 if (val
< 0 || val
> 255)
1184 inet
->min_ttl
= val
;
1204 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1208 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1209 * destination in skb->cb[] before dst drop.
1210 * This way, receiver doesn't make cache line misses to read rtable.
1212 void ipv4_pktinfo_prepare(const struct sock
*sk
, struct sk_buff
*skb
)
1214 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1215 bool prepare
= (inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
) ||
1218 if (prepare
&& skb_rtable(skb
)) {
1219 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1220 * which has interface index (iif) as the first member of the
1221 * underlying inet{6}_skb_parm struct. This code then overlays
1222 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1223 * element so the iif is picked up from the prior IPCB. If iif
1224 * is the loopback interface, then return the sending interface
1225 * (e.g., process binds socket to eth0 for Tx which is
1226 * redirected to loopback in the rtable/dst).
1228 struct rtable
*rt
= skb_rtable(skb
);
1229 bool l3slave
= ipv4_l3mdev_skb(IPCB(skb
)->flags
);
1231 if (pktinfo
->ipi_ifindex
== LOOPBACK_IFINDEX
)
1232 pktinfo
->ipi_ifindex
= inet_iif(skb
);
1233 else if (l3slave
&& rt
&& rt
->rt_iif
)
1234 pktinfo
->ipi_ifindex
= rt
->rt_iif
;
1236 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1238 pktinfo
->ipi_ifindex
= 0;
1239 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1244 int ip_setsockopt(struct sock
*sk
, int level
,
1245 int optname
, char __user
*optval
, unsigned int optlen
)
1249 if (level
!= SOL_IP
)
1250 return -ENOPROTOOPT
;
1252 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1253 #ifdef CONFIG_NETFILTER
1254 /* we need to exclude all possible ENOPROTOOPTs except default case */
1255 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1256 optname
!= IP_IPSEC_POLICY
&&
1257 optname
!= IP_XFRM_POLICY
&&
1258 !ip_mroute_opt(optname
))
1259 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1263 EXPORT_SYMBOL(ip_setsockopt
);
1265 #ifdef CONFIG_COMPAT
1266 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1267 char __user
*optval
, unsigned int optlen
)
1271 if (level
!= SOL_IP
)
1272 return -ENOPROTOOPT
;
1274 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1275 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1278 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1279 #ifdef CONFIG_NETFILTER
1280 /* we need to exclude all possible ENOPROTOOPTs except default case */
1281 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1282 optname
!= IP_IPSEC_POLICY
&&
1283 optname
!= IP_XFRM_POLICY
&&
1284 !ip_mroute_opt(optname
))
1285 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
, optval
,
1290 EXPORT_SYMBOL(compat_ip_setsockopt
);
1294 * Get the options. Note for future reference. The GET of IP options gets
1295 * the _received_ ones. The set sets the _sent_ ones.
1298 static bool getsockopt_needs_rtnl(int optname
)
1302 case MCAST_MSFILTER
:
1308 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1309 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1311 struct inet_sock
*inet
= inet_sk(sk
);
1312 bool needs_rtnl
= getsockopt_needs_rtnl(optname
);
1316 if (level
!= SOL_IP
)
1319 if (ip_mroute_opt(optname
))
1320 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1322 if (get_user(len
, optlen
))
1334 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1335 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1336 struct ip_options_rcu
*inet_opt
;
1338 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1339 lockdep_sock_is_held(sk
));
1342 memcpy(optbuf
, &inet_opt
->opt
,
1343 sizeof(struct ip_options
) +
1344 inet_opt
->opt
.optlen
);
1347 if (opt
->optlen
== 0)
1348 return put_user(0, optlen
);
1350 ip_options_undo(opt
);
1352 len
= min_t(unsigned int, len
, opt
->optlen
);
1353 if (put_user(len
, optlen
))
1355 if (copy_to_user(optval
, opt
->__data
, len
))
1360 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1363 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1366 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1369 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1372 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1375 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1377 case IP_RECVORIGDSTADDR
:
1378 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1381 val
= (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) != 0;
1383 case IP_RECVFRAGSIZE
:
1384 val
= (inet
->cmsg_flags
& IP_CMSG_RECVFRAGSIZE
) != 0;
1391 struct net
*net
= sock_net(sk
);
1392 val
= (inet
->uc_ttl
== -1 ?
1393 net
->ipv4
.sysctl_ip_default_ttl
:
1398 val
= inet
->hdrincl
;
1401 val
= inet
->nodefrag
;
1403 case IP_BIND_ADDRESS_NO_PORT
:
1404 val
= inet
->bind_address_no_port
;
1406 case IP_MTU_DISCOVER
:
1407 val
= inet
->pmtudisc
;
1411 struct dst_entry
*dst
;
1413 dst
= sk_dst_get(sk
);
1425 val
= inet
->recverr
;
1427 case IP_MULTICAST_TTL
:
1430 case IP_MULTICAST_LOOP
:
1431 val
= inet
->mc_loop
;
1434 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1436 case IP_MULTICAST_IF
:
1438 struct in_addr addr
;
1439 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1440 addr
.s_addr
= inet
->mc_addr
;
1443 if (put_user(len
, optlen
))
1445 if (copy_to_user(optval
, &addr
, len
))
1451 struct ip_msfilter msf
;
1453 if (len
< IP_MSFILTER_SIZE(0)) {
1457 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1461 err
= ip_mc_msfget(sk
, &msf
,
1462 (struct ip_msfilter __user
*)optval
, optlen
);
1465 case MCAST_MSFILTER
:
1467 struct group_filter gsf
;
1469 if (len
< GROUP_FILTER_SIZE(0)) {
1473 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1477 err
= ip_mc_gsfget(sk
, &gsf
,
1478 (struct group_filter __user
*)optval
,
1482 case IP_MULTICAST_ALL
:
1491 if (sk
->sk_type
!= SOCK_STREAM
)
1492 return -ENOPROTOOPT
;
1494 msg
.msg_control
= (__force
void *) optval
;
1495 msg
.msg_controllen
= len
;
1496 msg
.msg_flags
= flags
;
1498 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1499 struct in_pktinfo info
;
1501 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1502 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1503 info
.ipi_ifindex
= inet
->mc_index
;
1504 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1506 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1507 int hlim
= inet
->mc_ttl
;
1508 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1510 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1511 int tos
= inet
->rcv_tos
;
1512 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1514 len
-= msg
.msg_controllen
;
1515 return put_user(len
, optlen
);
1518 val
= inet
->freebind
;
1520 case IP_TRANSPARENT
:
1521 val
= inet
->transparent
;
1524 val
= inet
->min_ttl
;
1528 return -ENOPROTOOPT
;
1532 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1533 unsigned char ucval
= (unsigned char)val
;
1535 if (put_user(len
, optlen
))
1537 if (copy_to_user(optval
, &ucval
, 1))
1540 len
= min_t(unsigned int, sizeof(int), len
);
1541 if (put_user(len
, optlen
))
1543 if (copy_to_user(optval
, &val
, len
))
1555 int ip_getsockopt(struct sock
*sk
, int level
,
1556 int optname
, char __user
*optval
, int __user
*optlen
)
1560 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1561 #ifdef CONFIG_NETFILTER
1562 /* we need to exclude all possible ENOPROTOOPTs except default case */
1563 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1564 !ip_mroute_opt(optname
)) {
1567 if (get_user(len
, optlen
))
1570 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1572 err
= put_user(len
, optlen
);
1578 EXPORT_SYMBOL(ip_getsockopt
);
1580 #ifdef CONFIG_COMPAT
1581 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1582 char __user
*optval
, int __user
*optlen
)
1586 if (optname
== MCAST_MSFILTER
)
1587 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1590 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1593 #ifdef CONFIG_NETFILTER
1594 /* we need to exclude all possible ENOPROTOOPTs except default case */
1595 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1596 !ip_mroute_opt(optname
)) {
1599 if (get_user(len
, optlen
))
1602 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1604 err
= put_user(len
, optlen
);
1610 EXPORT_SYMBOL(compat_ip_getsockopt
);