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 struct sockaddr_in sin
;
152 const struct iphdr
*iph
= ip_hdr(skb
);
156 end
= skb_transport_offset(skb
) + 4;
157 if (end
> 0 && !pskb_may_pull(skb
, end
))
160 /* All current transport protocols have the port numbers in the
161 * first four bytes of the transport header and this function is
162 * written with this assumption in mind.
164 ports
= (__be16
*)skb_transport_header(skb
);
166 sin
.sin_family
= AF_INET
;
167 sin
.sin_addr
.s_addr
= iph
->daddr
;
168 sin
.sin_port
= ports
[1];
169 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
171 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
174 void ip_cmsg_recv_offset(struct msghdr
*msg
, struct sock
*sk
,
175 struct sk_buff
*skb
, int tlen
, int offset
)
177 struct inet_sock
*inet
= inet_sk(sk
);
178 unsigned int flags
= inet
->cmsg_flags
;
180 /* Ordered by supposed usage frequency */
181 if (flags
& IP_CMSG_PKTINFO
) {
182 ip_cmsg_recv_pktinfo(msg
, skb
);
184 flags
&= ~IP_CMSG_PKTINFO
;
189 if (flags
& IP_CMSG_TTL
) {
190 ip_cmsg_recv_ttl(msg
, skb
);
192 flags
&= ~IP_CMSG_TTL
;
197 if (flags
& IP_CMSG_TOS
) {
198 ip_cmsg_recv_tos(msg
, skb
);
200 flags
&= ~IP_CMSG_TOS
;
205 if (flags
& IP_CMSG_RECVOPTS
) {
206 ip_cmsg_recv_opts(msg
, skb
);
208 flags
&= ~IP_CMSG_RECVOPTS
;
213 if (flags
& IP_CMSG_RETOPTS
) {
214 ip_cmsg_recv_retopts(sock_net(sk
), msg
, skb
);
216 flags
&= ~IP_CMSG_RETOPTS
;
221 if (flags
& IP_CMSG_PASSSEC
) {
222 ip_cmsg_recv_security(msg
, skb
);
224 flags
&= ~IP_CMSG_PASSSEC
;
229 if (flags
& IP_CMSG_ORIGDSTADDR
) {
230 ip_cmsg_recv_dstaddr(msg
, skb
);
232 flags
&= ~IP_CMSG_ORIGDSTADDR
;
237 if (flags
& IP_CMSG_CHECKSUM
)
238 ip_cmsg_recv_checksum(msg
, skb
, tlen
, offset
);
240 if (flags
& IP_CMSG_RECVFRAGSIZE
)
241 ip_cmsg_recv_fragsize(msg
, skb
);
243 EXPORT_SYMBOL(ip_cmsg_recv_offset
);
245 int ip_cmsg_send(struct sock
*sk
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
,
249 struct cmsghdr
*cmsg
;
250 struct net
*net
= sock_net(sk
);
252 for_each_cmsghdr(cmsg
, msg
) {
253 if (!CMSG_OK(msg
, cmsg
))
255 #if IS_ENABLED(CONFIG_IPV6)
257 cmsg
->cmsg_level
== SOL_IPV6
&&
258 cmsg
->cmsg_type
== IPV6_PKTINFO
) {
259 struct in6_pktinfo
*src_info
;
261 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*src_info
)))
263 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
264 if (!ipv6_addr_v4mapped(&src_info
->ipi6_addr
))
266 if (src_info
->ipi6_ifindex
)
267 ipc
->oif
= src_info
->ipi6_ifindex
;
268 ipc
->addr
= src_info
->ipi6_addr
.s6_addr32
[3];
272 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
273 err
= __sock_cmsg_send(sk
, msg
, cmsg
, &ipc
->sockc
);
279 if (cmsg
->cmsg_level
!= SOL_IP
)
281 switch (cmsg
->cmsg_type
) {
283 err
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
285 /* Our caller is responsible for freeing ipc->opt */
286 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
287 err
< 40 ? err
: 40);
293 struct in_pktinfo
*info
;
294 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
296 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
297 if (info
->ipi_ifindex
)
298 ipc
->oif
= info
->ipi_ifindex
;
299 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
303 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
305 val
= *(int *)CMSG_DATA(cmsg
);
306 if (val
< 1 || val
> 255)
311 if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)))
312 val
= *(int *)CMSG_DATA(cmsg
);
313 else if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(u8
)))
314 val
= *(u8
*)CMSG_DATA(cmsg
);
317 if (val
< 0 || val
> 255)
320 ipc
->priority
= rt_tos2priority(ipc
->tos
);
330 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
332 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
334 sock_put(ra
->saved_sk
);
338 int ip_ra_control(struct sock
*sk
, unsigned char on
,
339 void (*destructor
)(struct sock
*))
341 struct ip_ra_chain
*ra
, *new_ra
;
342 struct ip_ra_chain __rcu
**rap
;
343 struct net
*net
= sock_net(sk
);
345 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
348 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
350 mutex_lock(&net
->ipv4
.ra_mutex
);
351 for (rap
= &net
->ipv4
.ra_chain
;
352 (ra
= rcu_dereference_protected(*rap
,
353 lockdep_is_held(&net
->ipv4
.ra_mutex
))) != NULL
;
357 mutex_unlock(&net
->ipv4
.ra_mutex
);
361 /* dont let ip_call_ra_chain() use sk again */
363 RCU_INIT_POINTER(*rap
, ra
->next
);
364 mutex_unlock(&net
->ipv4
.ra_mutex
);
369 * Delay sock_put(sk) and kfree(ra) after one rcu grace
370 * period. This guarantee ip_call_ra_chain() dont need
371 * to mess with socket refcounts.
374 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
379 mutex_unlock(&net
->ipv4
.ra_mutex
);
383 new_ra
->destructor
= destructor
;
385 RCU_INIT_POINTER(new_ra
->next
, ra
);
386 rcu_assign_pointer(*rap
, new_ra
);
388 mutex_unlock(&net
->ipv4
.ra_mutex
);
393 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
394 __be16 port
, u32 info
, u8
*payload
)
396 struct sock_exterr_skb
*serr
;
398 skb
= skb_clone(skb
, GFP_ATOMIC
);
402 serr
= SKB_EXT_ERR(skb
);
403 serr
->ee
.ee_errno
= err
;
404 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
405 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
406 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
408 serr
->ee
.ee_info
= info
;
409 serr
->ee
.ee_data
= 0;
410 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
411 skb_network_header(skb
);
414 if (skb_pull(skb
, payload
- skb
->data
)) {
415 skb_reset_transport_header(skb
);
416 if (sock_queue_err_skb(sk
, skb
) == 0)
422 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
424 struct inet_sock
*inet
= inet_sk(sk
);
425 struct sock_exterr_skb
*serr
;
432 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
436 skb_put(skb
, sizeof(struct iphdr
));
437 skb_reset_network_header(skb
);
441 serr
= SKB_EXT_ERR(skb
);
442 serr
->ee
.ee_errno
= err
;
443 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
444 serr
->ee
.ee_type
= 0;
445 serr
->ee
.ee_code
= 0;
447 serr
->ee
.ee_info
= info
;
448 serr
->ee
.ee_data
= 0;
449 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
452 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
453 skb_reset_transport_header(skb
);
455 if (sock_queue_err_skb(sk
, skb
))
459 /* For some errors we have valid addr_offset even with zero payload and
460 * zero port. Also, addr_offset should be supported if port is set.
462 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb
*serr
)
464 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
465 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
468 /* IPv4 supports cmsg on all imcp errors and some timestamps
470 * Timestamp code paths do not initialize the fields expected by cmsg:
471 * the PKTINFO fields in skb->cb[]. Fill those in here.
473 static bool ipv4_datagram_support_cmsg(const struct sock
*sk
,
477 struct in_pktinfo
*info
;
479 if (ee_origin
== SO_EE_ORIGIN_ICMP
)
482 if (ee_origin
== SO_EE_ORIGIN_LOCAL
)
485 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
486 * timestamp with egress dev. Not possible for packets without iif
487 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
489 info
= PKTINFO_SKB_CB(skb
);
490 if (!(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_CMSG
) ||
494 info
->ipi_spec_dst
.s_addr
= ip_hdr(skb
)->saddr
;
499 * Handle MSG_ERRQUEUE
501 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
503 struct sock_exterr_skb
*serr
;
505 DECLARE_SOCKADDR(struct sockaddr_in
*, sin
, msg
->msg_name
);
507 struct sock_extended_err ee
;
508 struct sockaddr_in offender
;
514 skb
= sock_dequeue_err_skb(sk
);
520 msg
->msg_flags
|= MSG_TRUNC
;
523 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
528 sock_recv_timestamp(msg
, sk
, skb
);
530 serr
= SKB_EXT_ERR(skb
);
532 if (sin
&& ipv4_datagram_support_addr(serr
)) {
533 sin
->sin_family
= AF_INET
;
534 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
536 sin
->sin_port
= serr
->port
;
537 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
538 *addr_len
= sizeof(*sin
);
541 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
542 sin
= &errhdr
.offender
;
543 memset(sin
, 0, sizeof(*sin
));
545 if (ipv4_datagram_support_cmsg(sk
, skb
, serr
->ee
.ee_origin
)) {
546 sin
->sin_family
= AF_INET
;
547 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
548 if (inet_sk(sk
)->cmsg_flags
)
549 ip_cmsg_recv(msg
, skb
);
552 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
554 /* Now we could try to dump offended packet options */
556 msg
->msg_flags
|= MSG_ERRQUEUE
;
566 * Socket option code for IP. This is the end of the line after any
567 * TCP,UDP etc options on an IP socket.
569 static bool setsockopt_needs_rtnl(int optname
)
572 case IP_ADD_MEMBERSHIP
:
573 case IP_ADD_SOURCE_MEMBERSHIP
:
574 case IP_BLOCK_SOURCE
:
575 case IP_DROP_MEMBERSHIP
:
576 case IP_DROP_SOURCE_MEMBERSHIP
:
578 case IP_UNBLOCK_SOURCE
:
579 case MCAST_BLOCK_SOURCE
:
581 case MCAST_JOIN_GROUP
:
582 case MCAST_JOIN_SOURCE_GROUP
:
583 case MCAST_LEAVE_GROUP
:
584 case MCAST_LEAVE_SOURCE_GROUP
:
585 case MCAST_UNBLOCK_SOURCE
:
591 static int do_ip_setsockopt(struct sock
*sk
, int level
,
592 int optname
, char __user
*optval
, unsigned int optlen
)
594 struct inet_sock
*inet
= inet_sk(sk
);
595 struct net
*net
= sock_net(sk
);
597 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
608 case IP_MTU_DISCOVER
:
610 case IP_ROUTER_ALERT
:
616 case IP_BIND_ADDRESS_NO_PORT
:
618 case IP_MULTICAST_TTL
:
619 case IP_MULTICAST_ALL
:
620 case IP_MULTICAST_LOOP
:
621 case IP_RECVORIGDSTADDR
:
623 case IP_RECVFRAGSIZE
:
624 if (optlen
>= sizeof(int)) {
625 if (get_user(val
, (int __user
*) optval
))
627 } else if (optlen
>= sizeof(char)) {
630 if (get_user(ucval
, (unsigned char __user
*) optval
))
636 /* If optlen==0, it is equivalent to val == 0 */
638 if (optname
== IP_ROUTER_ALERT
)
639 return ip_ra_control(sk
, val
? 1 : 0, NULL
);
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_ssm(sk
, &mreq
, MCAST_INCLUDE
);
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_ssm(sk
, &mreq
, MCAST_INCLUDE
);
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)
1154 inet
->freebind
= !!val
;
1157 case IP_IPSEC_POLICY
:
1158 case IP_XFRM_POLICY
:
1160 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1162 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
1165 case IP_TRANSPARENT
:
1166 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
1167 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1173 inet
->transparent
= !!val
;
1179 if (val
< 0 || val
> 255)
1181 inet
->min_ttl
= val
;
1201 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1205 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1206 * destination in skb->cb[] before dst drop.
1207 * This way, receiver doesn't make cache line misses to read rtable.
1209 void ipv4_pktinfo_prepare(const struct sock
*sk
, struct sk_buff
*skb
)
1211 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1212 bool prepare
= (inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
) ||
1215 if (prepare
&& skb_rtable(skb
)) {
1216 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1217 * which has interface index (iif) as the first member of the
1218 * underlying inet{6}_skb_parm struct. This code then overlays
1219 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1220 * element so the iif is picked up from the prior IPCB. If iif
1221 * is the loopback interface, then return the sending interface
1222 * (e.g., process binds socket to eth0 for Tx which is
1223 * redirected to loopback in the rtable/dst).
1225 struct rtable
*rt
= skb_rtable(skb
);
1226 bool l3slave
= ipv4_l3mdev_skb(IPCB(skb
)->flags
);
1228 if (pktinfo
->ipi_ifindex
== LOOPBACK_IFINDEX
)
1229 pktinfo
->ipi_ifindex
= inet_iif(skb
);
1230 else if (l3slave
&& rt
&& rt
->rt_iif
)
1231 pktinfo
->ipi_ifindex
= rt
->rt_iif
;
1233 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1235 pktinfo
->ipi_ifindex
= 0;
1236 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1241 int ip_setsockopt(struct sock
*sk
, int level
,
1242 int optname
, char __user
*optval
, unsigned int optlen
)
1246 if (level
!= SOL_IP
)
1247 return -ENOPROTOOPT
;
1249 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1250 #ifdef CONFIG_BPFILTER
1251 if (optname
>= BPFILTER_IPT_SO_SET_REPLACE
&&
1252 optname
< BPFILTER_IPT_SET_MAX
)
1253 err
= bpfilter_ip_set_sockopt(sk
, optname
, optval
, optlen
);
1255 #ifdef CONFIG_NETFILTER
1256 /* we need to exclude all possible ENOPROTOOPTs except default case */
1257 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1258 optname
!= IP_IPSEC_POLICY
&&
1259 optname
!= IP_XFRM_POLICY
&&
1260 !ip_mroute_opt(optname
))
1261 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1265 EXPORT_SYMBOL(ip_setsockopt
);
1267 #ifdef CONFIG_COMPAT
1268 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1269 char __user
*optval
, unsigned int optlen
)
1273 if (level
!= SOL_IP
)
1274 return -ENOPROTOOPT
;
1276 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1277 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1280 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1281 #ifdef CONFIG_NETFILTER
1282 /* we need to exclude all possible ENOPROTOOPTs except default case */
1283 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1284 optname
!= IP_IPSEC_POLICY
&&
1285 optname
!= IP_XFRM_POLICY
&&
1286 !ip_mroute_opt(optname
))
1287 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
, optval
,
1292 EXPORT_SYMBOL(compat_ip_setsockopt
);
1296 * Get the options. Note for future reference. The GET of IP options gets
1297 * the _received_ ones. The set sets the _sent_ ones.
1300 static bool getsockopt_needs_rtnl(int optname
)
1304 case MCAST_MSFILTER
:
1310 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1311 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1313 struct inet_sock
*inet
= inet_sk(sk
);
1314 bool needs_rtnl
= getsockopt_needs_rtnl(optname
);
1318 if (level
!= SOL_IP
)
1321 if (ip_mroute_opt(optname
))
1322 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1324 if (get_user(len
, optlen
))
1336 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1337 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1338 struct ip_options_rcu
*inet_opt
;
1340 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1341 lockdep_sock_is_held(sk
));
1344 memcpy(optbuf
, &inet_opt
->opt
,
1345 sizeof(struct ip_options
) +
1346 inet_opt
->opt
.optlen
);
1349 if (opt
->optlen
== 0)
1350 return put_user(0, optlen
);
1352 ip_options_undo(opt
);
1354 len
= min_t(unsigned int, len
, opt
->optlen
);
1355 if (put_user(len
, optlen
))
1357 if (copy_to_user(optval
, opt
->__data
, len
))
1362 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1365 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1368 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1371 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1374 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1377 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1379 case IP_RECVORIGDSTADDR
:
1380 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1383 val
= (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) != 0;
1385 case IP_RECVFRAGSIZE
:
1386 val
= (inet
->cmsg_flags
& IP_CMSG_RECVFRAGSIZE
) != 0;
1393 struct net
*net
= sock_net(sk
);
1394 val
= (inet
->uc_ttl
== -1 ?
1395 net
->ipv4
.sysctl_ip_default_ttl
:
1400 val
= inet
->hdrincl
;
1403 val
= inet
->nodefrag
;
1405 case IP_BIND_ADDRESS_NO_PORT
:
1406 val
= inet
->bind_address_no_port
;
1408 case IP_MTU_DISCOVER
:
1409 val
= inet
->pmtudisc
;
1413 struct dst_entry
*dst
;
1415 dst
= sk_dst_get(sk
);
1427 val
= inet
->recverr
;
1429 case IP_MULTICAST_TTL
:
1432 case IP_MULTICAST_LOOP
:
1433 val
= inet
->mc_loop
;
1436 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1438 case IP_MULTICAST_IF
:
1440 struct in_addr addr
;
1441 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1442 addr
.s_addr
= inet
->mc_addr
;
1445 if (put_user(len
, optlen
))
1447 if (copy_to_user(optval
, &addr
, len
))
1453 struct ip_msfilter msf
;
1455 if (len
< IP_MSFILTER_SIZE(0)) {
1459 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1463 err
= ip_mc_msfget(sk
, &msf
,
1464 (struct ip_msfilter __user
*)optval
, optlen
);
1467 case MCAST_MSFILTER
:
1469 struct group_filter gsf
;
1471 if (len
< GROUP_FILTER_SIZE(0)) {
1475 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1479 err
= ip_mc_gsfget(sk
, &gsf
,
1480 (struct group_filter __user
*)optval
,
1484 case IP_MULTICAST_ALL
:
1493 if (sk
->sk_type
!= SOCK_STREAM
)
1494 return -ENOPROTOOPT
;
1496 msg
.msg_control
= (__force
void *) optval
;
1497 msg
.msg_controllen
= len
;
1498 msg
.msg_flags
= flags
;
1500 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1501 struct in_pktinfo info
;
1503 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1504 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1505 info
.ipi_ifindex
= inet
->mc_index
;
1506 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1508 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1509 int hlim
= inet
->mc_ttl
;
1510 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1512 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1513 int tos
= inet
->rcv_tos
;
1514 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1516 len
-= msg
.msg_controllen
;
1517 return put_user(len
, optlen
);
1520 val
= inet
->freebind
;
1522 case IP_TRANSPARENT
:
1523 val
= inet
->transparent
;
1526 val
= inet
->min_ttl
;
1530 return -ENOPROTOOPT
;
1534 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1535 unsigned char ucval
= (unsigned char)val
;
1537 if (put_user(len
, optlen
))
1539 if (copy_to_user(optval
, &ucval
, 1))
1542 len
= min_t(unsigned int, sizeof(int), len
);
1543 if (put_user(len
, optlen
))
1545 if (copy_to_user(optval
, &val
, len
))
1557 int ip_getsockopt(struct sock
*sk
, int level
,
1558 int optname
, char __user
*optval
, int __user
*optlen
)
1562 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1563 #ifdef CONFIG_BPFILTER
1564 if (optname
>= BPFILTER_IPT_SO_GET_INFO
&&
1565 optname
< BPFILTER_IPT_GET_MAX
)
1566 err
= bpfilter_ip_get_sockopt(sk
, optname
, optval
, optlen
);
1568 #ifdef CONFIG_NETFILTER
1569 /* we need to exclude all possible ENOPROTOOPTs except default case */
1570 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1571 !ip_mroute_opt(optname
)) {
1574 if (get_user(len
, optlen
))
1577 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1579 err
= put_user(len
, optlen
);
1585 EXPORT_SYMBOL(ip_getsockopt
);
1587 #ifdef CONFIG_COMPAT
1588 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1589 char __user
*optval
, int __user
*optlen
)
1593 if (optname
== MCAST_MSFILTER
)
1594 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1597 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1600 #ifdef CONFIG_BPFILTER
1601 if (optname
>= BPFILTER_IPT_SO_GET_INFO
&&
1602 optname
< BPFILTER_IPT_GET_MAX
)
1603 err
= bpfilter_ip_get_sockopt(sk
, optname
, optval
, optlen
);
1605 #ifdef CONFIG_NETFILTER
1606 /* we need to exclude all possible ENOPROTOOPTs except default case */
1607 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1608 !ip_mroute_opt(optname
)) {
1611 if (get_user(len
, optlen
))
1614 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1616 err
= put_user(len
, optlen
);
1622 EXPORT_SYMBOL(compat_ip_getsockopt
);