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>
50 #include <linux/bpfilter.h>
53 * SOL_IP control messages.
56 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
58 struct in_pktinfo info
= *PKTINFO_SKB_CB(skb
);
60 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
62 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
65 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
67 int ttl
= ip_hdr(skb
)->ttl
;
68 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
71 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
73 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
76 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
78 if (IPCB(skb
)->opt
.optlen
== 0)
81 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
86 static void ip_cmsg_recv_retopts(struct net
*net
, struct msghdr
*msg
,
89 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
90 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
92 if (IPCB(skb
)->opt
.optlen
== 0)
95 if (ip_options_echo(net
, opt
, skb
)) {
96 msg
->msg_flags
|= MSG_CTRUNC
;
101 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
104 static void ip_cmsg_recv_fragsize(struct msghdr
*msg
, struct sk_buff
*skb
)
108 if (IPCB(skb
)->frag_max_size
== 0)
111 val
= IPCB(skb
)->frag_max_size
;
112 put_cmsg(msg
, SOL_IP
, IP_RECVFRAGSIZE
, sizeof(val
), &val
);
115 static void ip_cmsg_recv_checksum(struct msghdr
*msg
, struct sk_buff
*skb
,
116 int tlen
, int offset
)
118 __wsum csum
= skb
->csum
;
120 if (skb
->ip_summed
!= CHECKSUM_COMPLETE
)
124 int tend_off
= skb_transport_offset(skb
) + tlen
;
125 csum
= csum_sub(csum
, skb_checksum(skb
, tend_off
, offset
, 0));
128 put_cmsg(msg
, SOL_IP
, IP_CHECKSUM
, sizeof(__wsum
), &csum
);
131 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
137 err
= security_socket_getpeersec_dgram(NULL
, skb
, &secid
);
141 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
145 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
146 security_release_secctx(secdata
, seclen
);
149 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
151 __be16 _ports
[2], *ports
;
152 struct sockaddr_in sin
;
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.
158 ports
= skb_header_pointer(skb
, skb_transport_offset(skb
),
159 sizeof(_ports
), &_ports
);
163 sin
.sin_family
= AF_INET
;
164 sin
.sin_addr
.s_addr
= ip_hdr(skb
)->daddr
;
165 sin
.sin_port
= ports
[1];
166 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
168 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
171 void ip_cmsg_recv_offset(struct msghdr
*msg
, struct sock
*sk
,
172 struct sk_buff
*skb
, int tlen
, int offset
)
174 struct inet_sock
*inet
= inet_sk(sk
);
175 unsigned int flags
= inet
->cmsg_flags
;
177 /* Ordered by supposed usage frequency */
178 if (flags
& IP_CMSG_PKTINFO
) {
179 ip_cmsg_recv_pktinfo(msg
, skb
);
181 flags
&= ~IP_CMSG_PKTINFO
;
186 if (flags
& IP_CMSG_TTL
) {
187 ip_cmsg_recv_ttl(msg
, skb
);
189 flags
&= ~IP_CMSG_TTL
;
194 if (flags
& IP_CMSG_TOS
) {
195 ip_cmsg_recv_tos(msg
, skb
);
197 flags
&= ~IP_CMSG_TOS
;
202 if (flags
& IP_CMSG_RECVOPTS
) {
203 ip_cmsg_recv_opts(msg
, skb
);
205 flags
&= ~IP_CMSG_RECVOPTS
;
210 if (flags
& IP_CMSG_RETOPTS
) {
211 ip_cmsg_recv_retopts(sock_net(sk
), msg
, skb
);
213 flags
&= ~IP_CMSG_RETOPTS
;
218 if (flags
& IP_CMSG_PASSSEC
) {
219 ip_cmsg_recv_security(msg
, skb
);
221 flags
&= ~IP_CMSG_PASSSEC
;
226 if (flags
& IP_CMSG_ORIGDSTADDR
) {
227 ip_cmsg_recv_dstaddr(msg
, skb
);
229 flags
&= ~IP_CMSG_ORIGDSTADDR
;
234 if (flags
& IP_CMSG_CHECKSUM
)
235 ip_cmsg_recv_checksum(msg
, skb
, tlen
, offset
);
237 if (flags
& IP_CMSG_RECVFRAGSIZE
)
238 ip_cmsg_recv_fragsize(msg
, skb
);
240 EXPORT_SYMBOL(ip_cmsg_recv_offset
);
242 int ip_cmsg_send(struct sock
*sk
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
,
246 struct cmsghdr
*cmsg
;
247 struct net
*net
= sock_net(sk
);
249 for_each_cmsghdr(cmsg
, msg
) {
250 if (!CMSG_OK(msg
, cmsg
))
252 #if IS_ENABLED(CONFIG_IPV6)
254 cmsg
->cmsg_level
== SOL_IPV6
&&
255 cmsg
->cmsg_type
== IPV6_PKTINFO
) {
256 struct in6_pktinfo
*src_info
;
258 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*src_info
)))
260 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
261 if (!ipv6_addr_v4mapped(&src_info
->ipi6_addr
))
263 if (src_info
->ipi6_ifindex
)
264 ipc
->oif
= src_info
->ipi6_ifindex
;
265 ipc
->addr
= src_info
->ipi6_addr
.s6_addr32
[3];
269 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
270 err
= __sock_cmsg_send(sk
, msg
, cmsg
, &ipc
->sockc
);
276 if (cmsg
->cmsg_level
!= SOL_IP
)
278 switch (cmsg
->cmsg_type
) {
280 err
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
282 /* Our caller is responsible for freeing ipc->opt */
283 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
284 err
< 40 ? err
: 40);
290 struct in_pktinfo
*info
;
291 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
293 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
294 if (info
->ipi_ifindex
)
295 ipc
->oif
= info
->ipi_ifindex
;
296 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
300 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
302 val
= *(int *)CMSG_DATA(cmsg
);
303 if (val
< 1 || val
> 255)
308 if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)))
309 val
= *(int *)CMSG_DATA(cmsg
);
310 else if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(u8
)))
311 val
= *(u8
*)CMSG_DATA(cmsg
);
314 if (val
< 0 || val
> 255)
317 ipc
->priority
= rt_tos2priority(ipc
->tos
);
327 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
329 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
331 sock_put(ra
->saved_sk
);
335 int ip_ra_control(struct sock
*sk
, unsigned char on
,
336 void (*destructor
)(struct sock
*))
338 struct ip_ra_chain
*ra
, *new_ra
;
339 struct ip_ra_chain __rcu
**rap
;
340 struct net
*net
= sock_net(sk
);
342 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
345 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
349 mutex_lock(&net
->ipv4
.ra_mutex
);
350 for (rap
= &net
->ipv4
.ra_chain
;
351 (ra
= rcu_dereference_protected(*rap
,
352 lockdep_is_held(&net
->ipv4
.ra_mutex
))) != NULL
;
356 mutex_unlock(&net
->ipv4
.ra_mutex
);
360 /* dont let ip_call_ra_chain() use sk again */
362 RCU_INIT_POINTER(*rap
, ra
->next
);
363 mutex_unlock(&net
->ipv4
.ra_mutex
);
368 * Delay sock_put(sk) and kfree(ra) after one rcu grace
369 * period. This guarantee ip_call_ra_chain() dont need
370 * to mess with socket refcounts.
373 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
378 mutex_unlock(&net
->ipv4
.ra_mutex
);
382 new_ra
->destructor
= destructor
;
384 RCU_INIT_POINTER(new_ra
->next
, ra
);
385 rcu_assign_pointer(*rap
, new_ra
);
387 mutex_unlock(&net
->ipv4
.ra_mutex
);
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
;
513 skb
= sock_dequeue_err_skb(sk
);
519 msg
->msg_flags
|= MSG_TRUNC
;
522 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
527 sock_recv_timestamp(msg
, sk
, skb
);
529 serr
= SKB_EXT_ERR(skb
);
531 if (sin
&& ipv4_datagram_support_addr(serr
)) {
532 sin
->sin_family
= AF_INET
;
533 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
535 sin
->sin_port
= serr
->port
;
536 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
537 *addr_len
= sizeof(*sin
);
540 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
541 sin
= &errhdr
.offender
;
542 memset(sin
, 0, sizeof(*sin
));
544 if (ipv4_datagram_support_cmsg(sk
, skb
, serr
->ee
.ee_origin
)) {
545 sin
->sin_family
= AF_INET
;
546 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
547 if (inet_sk(sk
)->cmsg_flags
)
548 ip_cmsg_recv(msg
, skb
);
551 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
553 /* Now we could try to dump offended packet options */
555 msg
->msg_flags
|= MSG_ERRQUEUE
;
565 * Socket option code for IP. This is the end of the line after any
566 * TCP,UDP etc options on an IP socket.
568 static bool setsockopt_needs_rtnl(int optname
)
571 case IP_ADD_MEMBERSHIP
:
572 case IP_ADD_SOURCE_MEMBERSHIP
:
573 case IP_BLOCK_SOURCE
:
574 case IP_DROP_MEMBERSHIP
:
575 case IP_DROP_SOURCE_MEMBERSHIP
:
577 case IP_UNBLOCK_SOURCE
:
578 case MCAST_BLOCK_SOURCE
:
580 case MCAST_JOIN_GROUP
:
581 case MCAST_JOIN_SOURCE_GROUP
:
582 case MCAST_LEAVE_GROUP
:
583 case MCAST_LEAVE_SOURCE_GROUP
:
584 case MCAST_UNBLOCK_SOURCE
:
590 static int do_ip_setsockopt(struct sock
*sk
, int level
,
591 int optname
, char __user
*optval
, unsigned int optlen
)
593 struct inet_sock
*inet
= inet_sk(sk
);
594 struct net
*net
= sock_net(sk
);
596 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
607 case IP_MTU_DISCOVER
:
609 case IP_ROUTER_ALERT
:
615 case IP_BIND_ADDRESS_NO_PORT
:
617 case IP_MULTICAST_TTL
:
618 case IP_MULTICAST_ALL
:
619 case IP_MULTICAST_LOOP
:
620 case IP_RECVORIGDSTADDR
:
622 case IP_RECVFRAGSIZE
:
623 if (optlen
>= sizeof(int)) {
624 if (get_user(val
, (int __user
*) optval
))
626 } else if (optlen
>= sizeof(char)) {
629 if (get_user(ucval
, (unsigned char __user
*) optval
))
635 /* If optlen==0, it is equivalent to val == 0 */
637 if (optname
== IP_ROUTER_ALERT
)
638 return ip_ra_control(sk
, val
? 1 : 0, NULL
);
639 if (ip_mroute_opt(optname
))
640 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
650 struct ip_options_rcu
*old
, *opt
= NULL
;
654 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
658 old
= rcu_dereference_protected(inet
->inet_opt
,
659 lockdep_sock_is_held(sk
));
661 struct inet_connection_sock
*icsk
= inet_csk(sk
);
662 #if IS_ENABLED(CONFIG_IPV6)
663 if (sk
->sk_family
== PF_INET
||
664 (!((1 << sk
->sk_state
) &
665 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
666 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
669 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
671 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
672 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
673 #if IS_ENABLED(CONFIG_IPV6)
677 rcu_assign_pointer(inet
->inet_opt
, opt
);
684 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
686 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
690 inet
->cmsg_flags
|= IP_CMSG_TTL
;
692 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
696 inet
->cmsg_flags
|= IP_CMSG_TOS
;
698 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
702 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
704 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
708 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
710 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
714 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
716 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
718 case IP_RECVORIGDSTADDR
:
720 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
722 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
726 if (!(inet
->cmsg_flags
& IP_CMSG_CHECKSUM
)) {
727 inet_inc_convert_csum(sk
);
728 inet
->cmsg_flags
|= IP_CMSG_CHECKSUM
;
731 if (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) {
732 inet_dec_convert_csum(sk
);
733 inet
->cmsg_flags
&= ~IP_CMSG_CHECKSUM
;
737 case IP_RECVFRAGSIZE
:
738 if (sk
->sk_type
!= SOCK_RAW
&& sk
->sk_type
!= SOCK_DGRAM
)
741 inet
->cmsg_flags
|= IP_CMSG_RECVFRAGSIZE
;
743 inet
->cmsg_flags
&= ~IP_CMSG_RECVFRAGSIZE
;
745 case IP_TOS
: /* This sets both TOS and Precedence */
746 if (sk
->sk_type
== SOCK_STREAM
) {
747 val
&= ~INET_ECN_MASK
;
748 val
|= inet
->tos
& INET_ECN_MASK
;
750 if (inet
->tos
!= val
) {
752 sk
->sk_priority
= rt_tos2priority(val
);
759 if (val
!= -1 && (val
< 1 || val
> 255))
764 if (sk
->sk_type
!= SOCK_RAW
) {
768 inet
->hdrincl
= val
? 1 : 0;
771 if (sk
->sk_type
!= SOCK_RAW
) {
775 inet
->nodefrag
= val
? 1 : 0;
777 case IP_BIND_ADDRESS_NO_PORT
:
778 inet
->bind_address_no_port
= val
? 1 : 0;
780 case IP_MTU_DISCOVER
:
781 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_OMIT
)
783 inet
->pmtudisc
= val
;
786 inet
->recverr
= !!val
;
788 skb_queue_purge(&sk
->sk_error_queue
);
790 case IP_MULTICAST_TTL
:
791 if (sk
->sk_type
== SOCK_STREAM
)
797 if (val
< 0 || val
> 255)
801 case IP_MULTICAST_LOOP
:
804 inet
->mc_loop
= !!val
;
808 struct net_device
*dev
= NULL
;
812 if (optlen
!= sizeof(int))
815 ifindex
= (__force
int)ntohl((__force __be32
)val
);
822 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
823 err
= -EADDRNOTAVAIL
;
827 midx
= l3mdev_master_ifindex(dev
);
831 if (sk
->sk_bound_dev_if
&&
832 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
835 inet
->uc_index
= ifindex
;
839 case IP_MULTICAST_IF
:
841 struct ip_mreqn mreq
;
842 struct net_device
*dev
= NULL
;
845 if (sk
->sk_type
== SOCK_STREAM
)
848 * Check the arguments are allowable
851 if (optlen
< sizeof(struct in_addr
))
855 if (optlen
>= sizeof(struct ip_mreqn
)) {
856 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
859 memset(&mreq
, 0, sizeof(mreq
));
860 if (optlen
>= sizeof(struct ip_mreq
)) {
861 if (copy_from_user(&mreq
, optval
,
862 sizeof(struct ip_mreq
)))
864 } else if (optlen
>= sizeof(struct in_addr
)) {
865 if (copy_from_user(&mreq
.imr_address
, optval
,
866 sizeof(struct in_addr
)))
871 if (!mreq
.imr_ifindex
) {
872 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
878 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
880 mreq
.imr_ifindex
= dev
->ifindex
;
882 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
885 err
= -EADDRNOTAVAIL
;
889 midx
= l3mdev_master_ifindex(dev
);
894 if (sk
->sk_bound_dev_if
&&
895 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
&&
896 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
899 inet
->mc_index
= mreq
.imr_ifindex
;
900 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
905 case IP_ADD_MEMBERSHIP
:
906 case IP_DROP_MEMBERSHIP
:
908 struct ip_mreqn mreq
;
911 if (inet_sk(sk
)->is_icsk
)
914 if (optlen
< sizeof(struct ip_mreq
))
917 if (optlen
>= sizeof(struct ip_mreqn
)) {
918 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
921 memset(&mreq
, 0, sizeof(mreq
));
922 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
926 if (optname
== IP_ADD_MEMBERSHIP
)
927 err
= ip_mc_join_group(sk
, &mreq
);
929 err
= ip_mc_leave_group(sk
, &mreq
);
934 struct ip_msfilter
*msf
;
936 if (optlen
< IP_MSFILTER_SIZE(0))
938 if (optlen
> sysctl_optmem_max
) {
942 msf
= memdup_user(optval
, optlen
);
947 /* numsrc >= (1G-4) overflow in 32 bits */
948 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
949 msf
->imsf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
954 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
959 err
= ip_mc_msfilter(sk
, msf
, 0);
963 case IP_BLOCK_SOURCE
:
964 case IP_UNBLOCK_SOURCE
:
965 case IP_ADD_SOURCE_MEMBERSHIP
:
966 case IP_DROP_SOURCE_MEMBERSHIP
:
968 struct ip_mreq_source mreqs
;
971 if (optlen
!= sizeof(struct ip_mreq_source
))
973 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
977 if (optname
== IP_BLOCK_SOURCE
) {
978 omode
= MCAST_EXCLUDE
;
980 } else if (optname
== IP_UNBLOCK_SOURCE
) {
981 omode
= MCAST_EXCLUDE
;
983 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
984 struct ip_mreqn mreq
;
986 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
987 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
988 mreq
.imr_ifindex
= 0;
989 err
= ip_mc_join_group_ssm(sk
, &mreq
, MCAST_INCLUDE
);
990 if (err
&& err
!= -EADDRINUSE
)
992 omode
= MCAST_INCLUDE
;
994 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
995 omode
= MCAST_INCLUDE
;
998 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
1001 case MCAST_JOIN_GROUP
:
1002 case MCAST_LEAVE_GROUP
:
1004 struct group_req greq
;
1005 struct sockaddr_in
*psin
;
1006 struct ip_mreqn mreq
;
1008 if (optlen
< sizeof(struct group_req
))
1011 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
1013 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
1014 if (psin
->sin_family
!= AF_INET
)
1016 memset(&mreq
, 0, sizeof(mreq
));
1017 mreq
.imr_multiaddr
= psin
->sin_addr
;
1018 mreq
.imr_ifindex
= greq
.gr_interface
;
1020 if (optname
== MCAST_JOIN_GROUP
)
1021 err
= ip_mc_join_group(sk
, &mreq
);
1023 err
= ip_mc_leave_group(sk
, &mreq
);
1026 case MCAST_JOIN_SOURCE_GROUP
:
1027 case MCAST_LEAVE_SOURCE_GROUP
:
1028 case MCAST_BLOCK_SOURCE
:
1029 case MCAST_UNBLOCK_SOURCE
:
1031 struct group_source_req greqs
;
1032 struct ip_mreq_source mreqs
;
1033 struct sockaddr_in
*psin
;
1036 if (optlen
!= sizeof(struct group_source_req
))
1038 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
1042 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
1043 greqs
.gsr_source
.ss_family
!= AF_INET
) {
1044 err
= -EADDRNOTAVAIL
;
1047 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1048 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
1049 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
1050 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
1051 mreqs
.imr_interface
= 0; /* use index for mc_source */
1053 if (optname
== MCAST_BLOCK_SOURCE
) {
1054 omode
= MCAST_EXCLUDE
;
1056 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
1057 omode
= MCAST_EXCLUDE
;
1059 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
1060 struct ip_mreqn mreq
;
1062 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1063 mreq
.imr_multiaddr
= psin
->sin_addr
;
1064 mreq
.imr_address
.s_addr
= 0;
1065 mreq
.imr_ifindex
= greqs
.gsr_interface
;
1066 err
= ip_mc_join_group_ssm(sk
, &mreq
, MCAST_INCLUDE
);
1067 if (err
&& err
!= -EADDRINUSE
)
1069 greqs
.gsr_interface
= mreq
.imr_ifindex
;
1070 omode
= MCAST_INCLUDE
;
1072 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1073 omode
= MCAST_INCLUDE
;
1076 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
1077 greqs
.gsr_interface
);
1080 case MCAST_MSFILTER
:
1082 struct sockaddr_in
*psin
;
1083 struct ip_msfilter
*msf
= NULL
;
1084 struct group_filter
*gsf
= NULL
;
1085 int msize
, i
, ifindex
;
1087 if (optlen
< GROUP_FILTER_SIZE(0))
1089 if (optlen
> sysctl_optmem_max
) {
1093 gsf
= memdup_user(optval
, optlen
);
1099 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1100 if (gsf
->gf_numsrc
>= 0x1ffffff ||
1101 gsf
->gf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
1105 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
1109 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
1110 msf
= kmalloc(msize
, GFP_KERNEL
);
1115 ifindex
= gsf
->gf_interface
;
1116 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
1117 if (psin
->sin_family
!= AF_INET
) {
1118 err
= -EADDRNOTAVAIL
;
1121 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
1122 msf
->imsf_interface
= 0;
1123 msf
->imsf_fmode
= gsf
->gf_fmode
;
1124 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
1125 err
= -EADDRNOTAVAIL
;
1126 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
1127 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
1129 if (psin
->sin_family
!= AF_INET
)
1131 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
1136 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
1142 case IP_MULTICAST_ALL
:
1145 if (val
!= 0 && val
!= 1)
1153 inet
->freebind
= !!val
;
1156 case IP_IPSEC_POLICY
:
1157 case IP_XFRM_POLICY
:
1159 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1161 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
1164 case IP_TRANSPARENT
:
1165 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
1166 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1172 inet
->transparent
= !!val
;
1178 if (val
< 0 || val
> 255)
1180 inet
->min_ttl
= val
;
1200 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1204 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1205 * destination in skb->cb[] before dst drop.
1206 * This way, receiver doesn't make cache line misses to read rtable.
1208 void ipv4_pktinfo_prepare(const struct sock
*sk
, struct sk_buff
*skb
)
1210 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1211 bool prepare
= (inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
) ||
1214 if (prepare
&& skb_rtable(skb
)) {
1215 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1216 * which has interface index (iif) as the first member of the
1217 * underlying inet{6}_skb_parm struct. This code then overlays
1218 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1219 * element so the iif is picked up from the prior IPCB. If iif
1220 * is the loopback interface, then return the sending interface
1221 * (e.g., process binds socket to eth0 for Tx which is
1222 * redirected to loopback in the rtable/dst).
1224 struct rtable
*rt
= skb_rtable(skb
);
1225 bool l3slave
= ipv4_l3mdev_skb(IPCB(skb
)->flags
);
1227 if (pktinfo
->ipi_ifindex
== LOOPBACK_IFINDEX
)
1228 pktinfo
->ipi_ifindex
= inet_iif(skb
);
1229 else if (l3slave
&& rt
&& rt
->rt_iif
)
1230 pktinfo
->ipi_ifindex
= rt
->rt_iif
;
1232 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1234 pktinfo
->ipi_ifindex
= 0;
1235 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1240 int ip_setsockopt(struct sock
*sk
, int level
,
1241 int optname
, char __user
*optval
, unsigned int optlen
)
1245 if (level
!= SOL_IP
)
1246 return -ENOPROTOOPT
;
1248 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1249 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1250 if (optname
>= BPFILTER_IPT_SO_SET_REPLACE
&&
1251 optname
< BPFILTER_IPT_SET_MAX
)
1252 err
= bpfilter_ip_set_sockopt(sk
, optname
, optval
, optlen
);
1254 #ifdef CONFIG_NETFILTER
1255 /* we need to exclude all possible ENOPROTOOPTs except default case */
1256 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1257 optname
!= IP_IPSEC_POLICY
&&
1258 optname
!= IP_XFRM_POLICY
&&
1259 !ip_mroute_opt(optname
))
1260 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1264 EXPORT_SYMBOL(ip_setsockopt
);
1266 #ifdef CONFIG_COMPAT
1267 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1268 char __user
*optval
, unsigned int optlen
)
1272 if (level
!= SOL_IP
)
1273 return -ENOPROTOOPT
;
1275 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1276 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1279 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1280 #ifdef CONFIG_NETFILTER
1281 /* we need to exclude all possible ENOPROTOOPTs except default case */
1282 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1283 optname
!= IP_IPSEC_POLICY
&&
1284 optname
!= IP_XFRM_POLICY
&&
1285 !ip_mroute_opt(optname
))
1286 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
, optval
,
1291 EXPORT_SYMBOL(compat_ip_setsockopt
);
1295 * Get the options. Note for future reference. The GET of IP options gets
1296 * the _received_ ones. The set sets the _sent_ ones.
1299 static bool getsockopt_needs_rtnl(int optname
)
1303 case MCAST_MSFILTER
:
1309 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1310 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1312 struct inet_sock
*inet
= inet_sk(sk
);
1313 bool needs_rtnl
= getsockopt_needs_rtnl(optname
);
1317 if (level
!= SOL_IP
)
1320 if (ip_mroute_opt(optname
))
1321 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1323 if (get_user(len
, optlen
))
1335 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1336 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1337 struct ip_options_rcu
*inet_opt
;
1339 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1340 lockdep_sock_is_held(sk
));
1343 memcpy(optbuf
, &inet_opt
->opt
,
1344 sizeof(struct ip_options
) +
1345 inet_opt
->opt
.optlen
);
1348 if (opt
->optlen
== 0)
1349 return put_user(0, optlen
);
1351 ip_options_undo(opt
);
1353 len
= min_t(unsigned int, len
, opt
->optlen
);
1354 if (put_user(len
, optlen
))
1356 if (copy_to_user(optval
, opt
->__data
, len
))
1361 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1364 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1367 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1370 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1373 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1376 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1378 case IP_RECVORIGDSTADDR
:
1379 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1382 val
= (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) != 0;
1384 case IP_RECVFRAGSIZE
:
1385 val
= (inet
->cmsg_flags
& IP_CMSG_RECVFRAGSIZE
) != 0;
1392 struct net
*net
= sock_net(sk
);
1393 val
= (inet
->uc_ttl
== -1 ?
1394 net
->ipv4
.sysctl_ip_default_ttl
:
1399 val
= inet
->hdrincl
;
1402 val
= inet
->nodefrag
;
1404 case IP_BIND_ADDRESS_NO_PORT
:
1405 val
= inet
->bind_address_no_port
;
1407 case IP_MTU_DISCOVER
:
1408 val
= inet
->pmtudisc
;
1412 struct dst_entry
*dst
;
1414 dst
= sk_dst_get(sk
);
1426 val
= inet
->recverr
;
1428 case IP_MULTICAST_TTL
:
1431 case IP_MULTICAST_LOOP
:
1432 val
= inet
->mc_loop
;
1435 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1437 case IP_MULTICAST_IF
:
1439 struct in_addr addr
;
1440 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1441 addr
.s_addr
= inet
->mc_addr
;
1444 if (put_user(len
, optlen
))
1446 if (copy_to_user(optval
, &addr
, len
))
1452 struct ip_msfilter msf
;
1454 if (len
< IP_MSFILTER_SIZE(0)) {
1458 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1462 err
= ip_mc_msfget(sk
, &msf
,
1463 (struct ip_msfilter __user
*)optval
, optlen
);
1466 case MCAST_MSFILTER
:
1468 struct group_filter gsf
;
1470 if (len
< GROUP_FILTER_SIZE(0)) {
1474 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1478 err
= ip_mc_gsfget(sk
, &gsf
,
1479 (struct group_filter __user
*)optval
,
1483 case IP_MULTICAST_ALL
:
1492 if (sk
->sk_type
!= SOCK_STREAM
)
1493 return -ENOPROTOOPT
;
1495 msg
.msg_control
= (__force
void *) optval
;
1496 msg
.msg_controllen
= len
;
1497 msg
.msg_flags
= flags
;
1499 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1500 struct in_pktinfo info
;
1502 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1503 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1504 info
.ipi_ifindex
= inet
->mc_index
;
1505 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1507 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1508 int hlim
= inet
->mc_ttl
;
1509 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1511 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1512 int tos
= inet
->rcv_tos
;
1513 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1515 len
-= msg
.msg_controllen
;
1516 return put_user(len
, optlen
);
1519 val
= inet
->freebind
;
1521 case IP_TRANSPARENT
:
1522 val
= inet
->transparent
;
1525 val
= inet
->min_ttl
;
1529 return -ENOPROTOOPT
;
1533 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1534 unsigned char ucval
= (unsigned char)val
;
1536 if (put_user(len
, optlen
))
1538 if (copy_to_user(optval
, &ucval
, 1))
1541 len
= min_t(unsigned int, sizeof(int), len
);
1542 if (put_user(len
, optlen
))
1544 if (copy_to_user(optval
, &val
, len
))
1556 int ip_getsockopt(struct sock
*sk
, int level
,
1557 int optname
, char __user
*optval
, int __user
*optlen
)
1561 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1562 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1563 if (optname
>= BPFILTER_IPT_SO_GET_INFO
&&
1564 optname
< BPFILTER_IPT_GET_MAX
)
1565 err
= bpfilter_ip_get_sockopt(sk
, optname
, optval
, optlen
);
1567 #ifdef CONFIG_NETFILTER
1568 /* we need to exclude all possible ENOPROTOOPTs except default case */
1569 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1570 !ip_mroute_opt(optname
)) {
1573 if (get_user(len
, optlen
))
1576 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1578 err
= put_user(len
, optlen
);
1584 EXPORT_SYMBOL(ip_getsockopt
);
1586 #ifdef CONFIG_COMPAT
1587 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1588 char __user
*optval
, int __user
*optlen
)
1592 if (optname
== MCAST_MSFILTER
)
1593 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1596 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1599 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1600 if (optname
>= BPFILTER_IPT_SO_GET_INFO
&&
1601 optname
< BPFILTER_IPT_GET_MAX
)
1602 err
= bpfilter_ip_get_sockopt(sk
, optname
, optval
, optlen
);
1604 #ifdef CONFIG_NETFILTER
1605 /* we need to exclude all possible ENOPROTOOPTs except default case */
1606 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1607 !ip_mroute_opt(optname
)) {
1610 if (get_user(len
, optlen
))
1613 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1615 err
= put_user(len
, optlen
);
1621 EXPORT_SYMBOL(compat_ip_getsockopt
);