2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
11 * Many : Split from ip.c , see ip.c for history.
12 * Martin Mares : TOS setting fixed.
13 * Alan Cox : Fixed a couple of oopses in Martin's
15 * Mike McLagan : Routing by source
18 #include <linux/module.h>
19 #include <linux/types.h>
21 #include <linux/skbuff.h>
23 #include <linux/icmp.h>
24 #include <linux/inetdevice.h>
25 #include <linux/netdevice.h>
26 #include <linux/slab.h>
30 #include <net/tcp_states.h>
31 #include <linux/udp.h>
32 #include <linux/igmp.h>
33 #include <linux/netfilter.h>
34 #include <linux/route.h>
35 #include <linux/mroute.h>
36 #include <net/inet_ecn.h>
37 #include <net/route.h>
39 #include <net/compat.h>
40 #include <net/checksum.h>
41 #if IS_ENABLED(CONFIG_IPV6)
42 #include <net/transp_v6.h>
44 #include <net/ip_fib.h>
46 #include <linux/errqueue.h>
47 #include <linux/uaccess.h>
50 * SOL_IP control messages.
53 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
55 struct in_pktinfo info
= *PKTINFO_SKB_CB(skb
);
57 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
59 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
62 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
64 int ttl
= ip_hdr(skb
)->ttl
;
65 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
68 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
70 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
73 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
75 if (IPCB(skb
)->opt
.optlen
== 0)
78 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
83 static void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
85 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
86 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
88 if (IPCB(skb
)->opt
.optlen
== 0)
91 if (ip_options_echo(opt
, skb
)) {
92 msg
->msg_flags
|= MSG_CTRUNC
;
97 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
100 static void ip_cmsg_recv_fragsize(struct msghdr
*msg
, struct sk_buff
*skb
)
104 if (IPCB(skb
)->frag_max_size
== 0)
107 val
= IPCB(skb
)->frag_max_size
;
108 put_cmsg(msg
, SOL_IP
, IP_RECVFRAGSIZE
, sizeof(val
), &val
);
111 static void ip_cmsg_recv_checksum(struct msghdr
*msg
, struct sk_buff
*skb
,
112 int tlen
, int offset
)
114 __wsum csum
= skb
->csum
;
116 if (skb
->ip_summed
!= CHECKSUM_COMPLETE
)
120 int tend_off
= skb_transport_offset(skb
) + tlen
;
121 csum
= csum_sub(csum
, skb_checksum(skb
, tend_off
, offset
, 0));
124 put_cmsg(msg
, SOL_IP
, IP_CHECKSUM
, sizeof(__wsum
), &csum
);
127 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
133 err
= security_socket_getpeersec_dgram(NULL
, skb
, &secid
);
137 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
141 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
142 security_release_secctx(secdata
, seclen
);
145 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
147 struct sockaddr_in sin
;
148 const struct iphdr
*iph
= ip_hdr(skb
);
149 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
151 if (skb_transport_offset(skb
) + 4 > (int)skb
->len
)
154 /* All current transport protocols have the port numbers in the
155 * first four bytes of the transport header and this function is
156 * written with this assumption in mind.
159 sin
.sin_family
= AF_INET
;
160 sin
.sin_addr
.s_addr
= iph
->daddr
;
161 sin
.sin_port
= ports
[1];
162 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
164 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
167 void ip_cmsg_recv_offset(struct msghdr
*msg
, struct sock
*sk
,
168 struct sk_buff
*skb
, int tlen
, int offset
)
170 struct inet_sock
*inet
= inet_sk(sk
);
171 unsigned int flags
= inet
->cmsg_flags
;
173 /* Ordered by supposed usage frequency */
174 if (flags
& IP_CMSG_PKTINFO
) {
175 ip_cmsg_recv_pktinfo(msg
, skb
);
177 flags
&= ~IP_CMSG_PKTINFO
;
182 if (flags
& IP_CMSG_TTL
) {
183 ip_cmsg_recv_ttl(msg
, skb
);
185 flags
&= ~IP_CMSG_TTL
;
190 if (flags
& IP_CMSG_TOS
) {
191 ip_cmsg_recv_tos(msg
, skb
);
193 flags
&= ~IP_CMSG_TOS
;
198 if (flags
& IP_CMSG_RECVOPTS
) {
199 ip_cmsg_recv_opts(msg
, skb
);
201 flags
&= ~IP_CMSG_RECVOPTS
;
206 if (flags
& IP_CMSG_RETOPTS
) {
207 ip_cmsg_recv_retopts(msg
, skb
);
209 flags
&= ~IP_CMSG_RETOPTS
;
214 if (flags
& IP_CMSG_PASSSEC
) {
215 ip_cmsg_recv_security(msg
, skb
);
217 flags
&= ~IP_CMSG_PASSSEC
;
222 if (flags
& IP_CMSG_ORIGDSTADDR
) {
223 ip_cmsg_recv_dstaddr(msg
, skb
);
225 flags
&= ~IP_CMSG_ORIGDSTADDR
;
230 if (flags
& IP_CMSG_CHECKSUM
)
231 ip_cmsg_recv_checksum(msg
, skb
, tlen
, offset
);
233 if (flags
& IP_CMSG_RECVFRAGSIZE
)
234 ip_cmsg_recv_fragsize(msg
, skb
);
236 EXPORT_SYMBOL(ip_cmsg_recv_offset
);
238 int ip_cmsg_send(struct sock
*sk
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
,
242 struct cmsghdr
*cmsg
;
243 struct net
*net
= sock_net(sk
);
245 for_each_cmsghdr(cmsg
, msg
) {
246 if (!CMSG_OK(msg
, cmsg
))
248 #if IS_ENABLED(CONFIG_IPV6)
250 cmsg
->cmsg_level
== SOL_IPV6
&&
251 cmsg
->cmsg_type
== IPV6_PKTINFO
) {
252 struct in6_pktinfo
*src_info
;
254 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*src_info
)))
256 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
257 if (!ipv6_addr_v4mapped(&src_info
->ipi6_addr
))
259 ipc
->oif
= src_info
->ipi6_ifindex
;
260 ipc
->addr
= src_info
->ipi6_addr
.s6_addr32
[3];
264 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
265 err
= __sock_cmsg_send(sk
, msg
, cmsg
, &ipc
->sockc
);
271 if (cmsg
->cmsg_level
!= SOL_IP
)
273 switch (cmsg
->cmsg_type
) {
275 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
277 /* Our caller is responsible for freeing ipc->opt */
278 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
279 err
< 40 ? err
: 40);
285 struct in_pktinfo
*info
;
286 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
288 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
289 ipc
->oif
= info
->ipi_ifindex
;
290 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
294 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
296 val
= *(int *)CMSG_DATA(cmsg
);
297 if (val
< 1 || val
> 255)
302 if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)))
303 val
= *(int *)CMSG_DATA(cmsg
);
304 else if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(u8
)))
305 val
= *(u8
*)CMSG_DATA(cmsg
);
308 if (val
< 0 || val
> 255)
311 ipc
->priority
= rt_tos2priority(ipc
->tos
);
322 /* Special input handler for packets caught by router alert option.
323 They are selected only by protocol field, and then processed likely
324 local ones; but only if someone wants them! Otherwise, router
325 not running rsvpd will kill RSVP.
327 It is user level problem, what it will make with them.
328 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
329 but receiver should be enough clever f.e. to forward mtrace requests,
330 sent to multicast group to reach destination designated router.
332 struct ip_ra_chain __rcu
*ip_ra_chain
;
333 static DEFINE_SPINLOCK(ip_ra_lock
);
336 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
338 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
340 sock_put(ra
->saved_sk
);
344 int ip_ra_control(struct sock
*sk
, unsigned char on
,
345 void (*destructor
)(struct sock
*))
347 struct ip_ra_chain
*ra
, *new_ra
;
348 struct ip_ra_chain __rcu
**rap
;
350 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
353 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
355 spin_lock_bh(&ip_ra_lock
);
356 for (rap
= &ip_ra_chain
;
357 (ra
= rcu_dereference_protected(*rap
,
358 lockdep_is_held(&ip_ra_lock
))) != NULL
;
362 spin_unlock_bh(&ip_ra_lock
);
366 /* dont let ip_call_ra_chain() use sk again */
368 RCU_INIT_POINTER(*rap
, ra
->next
);
369 spin_unlock_bh(&ip_ra_lock
);
374 * Delay sock_put(sk) and kfree(ra) after one rcu grace
375 * period. This guarantee ip_call_ra_chain() dont need
376 * to mess with socket refcounts.
379 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
384 spin_unlock_bh(&ip_ra_lock
);
388 new_ra
->destructor
= destructor
;
390 RCU_INIT_POINTER(new_ra
->next
, ra
);
391 rcu_assign_pointer(*rap
, new_ra
);
393 spin_unlock_bh(&ip_ra_lock
);
398 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
399 __be16 port
, u32 info
, u8
*payload
)
401 struct sock_exterr_skb
*serr
;
403 skb
= skb_clone(skb
, GFP_ATOMIC
);
407 serr
= SKB_EXT_ERR(skb
);
408 serr
->ee
.ee_errno
= err
;
409 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
410 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
411 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
413 serr
->ee
.ee_info
= info
;
414 serr
->ee
.ee_data
= 0;
415 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
416 skb_network_header(skb
);
419 if (skb_pull(skb
, payload
- skb
->data
)) {
420 skb_reset_transport_header(skb
);
421 if (sock_queue_err_skb(sk
, skb
) == 0)
427 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
429 struct inet_sock
*inet
= inet_sk(sk
);
430 struct sock_exterr_skb
*serr
;
437 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
441 skb_put(skb
, sizeof(struct iphdr
));
442 skb_reset_network_header(skb
);
446 serr
= SKB_EXT_ERR(skb
);
447 serr
->ee
.ee_errno
= err
;
448 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
449 serr
->ee
.ee_type
= 0;
450 serr
->ee
.ee_code
= 0;
452 serr
->ee
.ee_info
= info
;
453 serr
->ee
.ee_data
= 0;
454 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
457 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
458 skb_reset_transport_header(skb
);
460 if (sock_queue_err_skb(sk
, skb
))
464 /* For some errors we have valid addr_offset even with zero payload and
465 * zero port. Also, addr_offset should be supported if port is set.
467 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb
*serr
)
469 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
470 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
473 /* IPv4 supports cmsg on all imcp errors and some timestamps
475 * Timestamp code paths do not initialize the fields expected by cmsg:
476 * the PKTINFO fields in skb->cb[]. Fill those in here.
478 static bool ipv4_datagram_support_cmsg(const struct sock
*sk
,
482 struct in_pktinfo
*info
;
484 if (ee_origin
== SO_EE_ORIGIN_ICMP
)
487 if (ee_origin
== SO_EE_ORIGIN_LOCAL
)
490 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
491 * timestamp with egress dev. Not possible for packets without dev
492 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
494 if ((!(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_CMSG
)) ||
498 info
= PKTINFO_SKB_CB(skb
);
499 info
->ipi_spec_dst
.s_addr
= ip_hdr(skb
)->saddr
;
500 info
->ipi_ifindex
= skb
->dev
->ifindex
;
505 * Handle MSG_ERRQUEUE
507 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
509 struct sock_exterr_skb
*serr
;
511 DECLARE_SOCKADDR(struct sockaddr_in
*, sin
, msg
->msg_name
);
513 struct sock_extended_err ee
;
514 struct sockaddr_in offender
;
519 WARN_ON_ONCE(sk
->sk_family
== AF_INET6
);
522 skb
= sock_dequeue_err_skb(sk
);
528 msg
->msg_flags
|= MSG_TRUNC
;
531 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
536 sock_recv_timestamp(msg
, sk
, skb
);
538 serr
= SKB_EXT_ERR(skb
);
540 if (sin
&& ipv4_datagram_support_addr(serr
)) {
541 sin
->sin_family
= AF_INET
;
542 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
544 sin
->sin_port
= serr
->port
;
545 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
546 *addr_len
= sizeof(*sin
);
549 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
550 sin
= &errhdr
.offender
;
551 memset(sin
, 0, sizeof(*sin
));
553 if (ipv4_datagram_support_cmsg(sk
, skb
, serr
->ee
.ee_origin
)) {
554 sin
->sin_family
= AF_INET
;
555 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
556 if (inet_sk(sk
)->cmsg_flags
)
557 ip_cmsg_recv(msg
, skb
);
560 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
562 /* Now we could try to dump offended packet options */
564 msg
->msg_flags
|= MSG_ERRQUEUE
;
574 * Socket option code for IP. This is the end of the line after any
575 * TCP,UDP etc options on an IP socket.
577 static bool setsockopt_needs_rtnl(int optname
)
580 case IP_ADD_MEMBERSHIP
:
581 case IP_ADD_SOURCE_MEMBERSHIP
:
582 case IP_BLOCK_SOURCE
:
583 case IP_DROP_MEMBERSHIP
:
584 case IP_DROP_SOURCE_MEMBERSHIP
:
586 case IP_UNBLOCK_SOURCE
:
587 case MCAST_BLOCK_SOURCE
:
589 case MCAST_JOIN_GROUP
:
590 case MCAST_JOIN_SOURCE_GROUP
:
591 case MCAST_LEAVE_GROUP
:
592 case MCAST_LEAVE_SOURCE_GROUP
:
593 case MCAST_UNBLOCK_SOURCE
:
599 static int do_ip_setsockopt(struct sock
*sk
, int level
,
600 int optname
, char __user
*optval
, unsigned int optlen
)
602 struct inet_sock
*inet
= inet_sk(sk
);
603 struct net
*net
= sock_net(sk
);
605 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
616 case IP_MTU_DISCOVER
:
618 case IP_ROUTER_ALERT
:
624 case IP_BIND_ADDRESS_NO_PORT
:
626 case IP_MULTICAST_TTL
:
627 case IP_MULTICAST_ALL
:
628 case IP_MULTICAST_LOOP
:
629 case IP_RECVORIGDSTADDR
:
631 case IP_RECVFRAGSIZE
:
632 if (optlen
>= sizeof(int)) {
633 if (get_user(val
, (int __user
*) optval
))
635 } else if (optlen
>= sizeof(char)) {
638 if (get_user(ucval
, (unsigned char __user
*) optval
))
644 /* If optlen==0, it is equivalent to val == 0 */
646 if (ip_mroute_opt(optname
))
647 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
657 struct ip_options_rcu
*old
, *opt
= NULL
;
661 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
665 old
= rcu_dereference_protected(inet
->inet_opt
,
666 lockdep_sock_is_held(sk
));
668 struct inet_connection_sock
*icsk
= inet_csk(sk
);
669 #if IS_ENABLED(CONFIG_IPV6)
670 if (sk
->sk_family
== PF_INET
||
671 (!((1 << sk
->sk_state
) &
672 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
673 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
676 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
678 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
679 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
680 #if IS_ENABLED(CONFIG_IPV6)
684 rcu_assign_pointer(inet
->inet_opt
, opt
);
691 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
693 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
697 inet
->cmsg_flags
|= IP_CMSG_TTL
;
699 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
703 inet
->cmsg_flags
|= IP_CMSG_TOS
;
705 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
709 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
711 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
715 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
717 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
721 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
723 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
725 case IP_RECVORIGDSTADDR
:
727 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
729 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
733 if (!(inet
->cmsg_flags
& IP_CMSG_CHECKSUM
)) {
734 inet_inc_convert_csum(sk
);
735 inet
->cmsg_flags
|= IP_CMSG_CHECKSUM
;
738 if (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) {
739 inet_dec_convert_csum(sk
);
740 inet
->cmsg_flags
&= ~IP_CMSG_CHECKSUM
;
744 case IP_RECVFRAGSIZE
:
745 if (sk
->sk_type
!= SOCK_RAW
&& sk
->sk_type
!= SOCK_DGRAM
)
748 inet
->cmsg_flags
|= IP_CMSG_RECVFRAGSIZE
;
750 inet
->cmsg_flags
&= ~IP_CMSG_RECVFRAGSIZE
;
752 case IP_TOS
: /* This sets both TOS and Precedence */
753 if (sk
->sk_type
== SOCK_STREAM
) {
754 val
&= ~INET_ECN_MASK
;
755 val
|= inet
->tos
& INET_ECN_MASK
;
757 if (inet
->tos
!= val
) {
759 sk
->sk_priority
= rt_tos2priority(val
);
766 if (val
!= -1 && (val
< 1 || val
> 255))
771 if (sk
->sk_type
!= SOCK_RAW
) {
775 inet
->hdrincl
= val
? 1 : 0;
778 if (sk
->sk_type
!= SOCK_RAW
) {
782 inet
->nodefrag
= val
? 1 : 0;
784 case IP_BIND_ADDRESS_NO_PORT
:
785 inet
->bind_address_no_port
= val
? 1 : 0;
787 case IP_MTU_DISCOVER
:
788 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_OMIT
)
790 inet
->pmtudisc
= val
;
793 inet
->recverr
= !!val
;
795 skb_queue_purge(&sk
->sk_error_queue
);
797 case IP_MULTICAST_TTL
:
798 if (sk
->sk_type
== SOCK_STREAM
)
804 if (val
< 0 || val
> 255)
808 case IP_MULTICAST_LOOP
:
811 inet
->mc_loop
= !!val
;
815 struct net_device
*dev
= NULL
;
818 if (optlen
!= sizeof(int))
821 ifindex
= (__force
int)ntohl((__force __be32
)val
);
828 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
829 err
= -EADDRNOTAVAIL
;
835 if (sk
->sk_bound_dev_if
)
838 inet
->uc_index
= ifindex
;
842 case IP_MULTICAST_IF
:
844 struct ip_mreqn mreq
;
845 struct net_device
*dev
= NULL
;
847 if (sk
->sk_type
== SOCK_STREAM
)
850 * Check the arguments are allowable
853 if (optlen
< sizeof(struct in_addr
))
857 if (optlen
>= sizeof(struct ip_mreqn
)) {
858 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
861 memset(&mreq
, 0, sizeof(mreq
));
862 if (optlen
>= sizeof(struct ip_mreq
)) {
863 if (copy_from_user(&mreq
, optval
,
864 sizeof(struct ip_mreq
)))
866 } else if (optlen
>= sizeof(struct in_addr
)) {
867 if (copy_from_user(&mreq
.imr_address
, optval
,
868 sizeof(struct in_addr
)))
873 if (!mreq
.imr_ifindex
) {
874 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
880 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
882 mreq
.imr_ifindex
= dev
->ifindex
;
884 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
887 err
= -EADDRNOTAVAIL
;
893 if (sk
->sk_bound_dev_if
&&
894 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
897 inet
->mc_index
= mreq
.imr_ifindex
;
898 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
903 case IP_ADD_MEMBERSHIP
:
904 case IP_DROP_MEMBERSHIP
:
906 struct ip_mreqn mreq
;
909 if (inet_sk(sk
)->is_icsk
)
912 if (optlen
< sizeof(struct ip_mreq
))
915 if (optlen
>= sizeof(struct ip_mreqn
)) {
916 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
919 memset(&mreq
, 0, sizeof(mreq
));
920 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
924 if (optname
== IP_ADD_MEMBERSHIP
)
925 err
= ip_mc_join_group(sk
, &mreq
);
927 err
= ip_mc_leave_group(sk
, &mreq
);
932 struct ip_msfilter
*msf
;
934 if (optlen
< IP_MSFILTER_SIZE(0))
936 if (optlen
> sysctl_optmem_max
) {
940 msf
= kmalloc(optlen
, GFP_KERNEL
);
946 if (copy_from_user(msf
, optval
, optlen
)) {
950 /* numsrc >= (1G-4) overflow in 32 bits */
951 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
952 msf
->imsf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
957 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
962 err
= ip_mc_msfilter(sk
, msf
, 0);
966 case IP_BLOCK_SOURCE
:
967 case IP_UNBLOCK_SOURCE
:
968 case IP_ADD_SOURCE_MEMBERSHIP
:
969 case IP_DROP_SOURCE_MEMBERSHIP
:
971 struct ip_mreq_source mreqs
;
974 if (optlen
!= sizeof(struct ip_mreq_source
))
976 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
980 if (optname
== IP_BLOCK_SOURCE
) {
981 omode
= MCAST_EXCLUDE
;
983 } else if (optname
== IP_UNBLOCK_SOURCE
) {
984 omode
= MCAST_EXCLUDE
;
986 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
987 struct ip_mreqn mreq
;
989 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
990 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
991 mreq
.imr_ifindex
= 0;
992 err
= ip_mc_join_group(sk
, &mreq
);
993 if (err
&& err
!= -EADDRINUSE
)
995 omode
= MCAST_INCLUDE
;
997 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
998 omode
= MCAST_INCLUDE
;
1001 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
1004 case MCAST_JOIN_GROUP
:
1005 case MCAST_LEAVE_GROUP
:
1007 struct group_req greq
;
1008 struct sockaddr_in
*psin
;
1009 struct ip_mreqn mreq
;
1011 if (optlen
< sizeof(struct group_req
))
1014 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
1016 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
1017 if (psin
->sin_family
!= AF_INET
)
1019 memset(&mreq
, 0, sizeof(mreq
));
1020 mreq
.imr_multiaddr
= psin
->sin_addr
;
1021 mreq
.imr_ifindex
= greq
.gr_interface
;
1023 if (optname
== MCAST_JOIN_GROUP
)
1024 err
= ip_mc_join_group(sk
, &mreq
);
1026 err
= ip_mc_leave_group(sk
, &mreq
);
1029 case MCAST_JOIN_SOURCE_GROUP
:
1030 case MCAST_LEAVE_SOURCE_GROUP
:
1031 case MCAST_BLOCK_SOURCE
:
1032 case MCAST_UNBLOCK_SOURCE
:
1034 struct group_source_req greqs
;
1035 struct ip_mreq_source mreqs
;
1036 struct sockaddr_in
*psin
;
1039 if (optlen
!= sizeof(struct group_source_req
))
1041 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
1045 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
1046 greqs
.gsr_source
.ss_family
!= AF_INET
) {
1047 err
= -EADDRNOTAVAIL
;
1050 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1051 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
1052 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
1053 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
1054 mreqs
.imr_interface
= 0; /* use index for mc_source */
1056 if (optname
== MCAST_BLOCK_SOURCE
) {
1057 omode
= MCAST_EXCLUDE
;
1059 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
1060 omode
= MCAST_EXCLUDE
;
1062 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
1063 struct ip_mreqn mreq
;
1065 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1066 mreq
.imr_multiaddr
= psin
->sin_addr
;
1067 mreq
.imr_address
.s_addr
= 0;
1068 mreq
.imr_ifindex
= greqs
.gsr_interface
;
1069 err
= ip_mc_join_group(sk
, &mreq
);
1070 if (err
&& err
!= -EADDRINUSE
)
1072 greqs
.gsr_interface
= mreq
.imr_ifindex
;
1073 omode
= MCAST_INCLUDE
;
1075 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1076 omode
= MCAST_INCLUDE
;
1079 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
1080 greqs
.gsr_interface
);
1083 case MCAST_MSFILTER
:
1085 struct sockaddr_in
*psin
;
1086 struct ip_msfilter
*msf
= NULL
;
1087 struct group_filter
*gsf
= NULL
;
1088 int msize
, i
, ifindex
;
1090 if (optlen
< GROUP_FILTER_SIZE(0))
1092 if (optlen
> sysctl_optmem_max
) {
1096 gsf
= kmalloc(optlen
, GFP_KERNEL
);
1102 if (copy_from_user(gsf
, optval
, optlen
))
1105 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1106 if (gsf
->gf_numsrc
>= 0x1ffffff ||
1107 gsf
->gf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
1111 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
1115 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
1116 msf
= kmalloc(msize
, GFP_KERNEL
);
1121 ifindex
= gsf
->gf_interface
;
1122 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
1123 if (psin
->sin_family
!= AF_INET
) {
1124 err
= -EADDRNOTAVAIL
;
1127 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
1128 msf
->imsf_interface
= 0;
1129 msf
->imsf_fmode
= gsf
->gf_fmode
;
1130 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
1131 err
= -EADDRNOTAVAIL
;
1132 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
1133 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
1135 if (psin
->sin_family
!= AF_INET
)
1137 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
1142 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
1148 case IP_MULTICAST_ALL
:
1151 if (val
!= 0 && val
!= 1)
1155 case IP_ROUTER_ALERT
:
1156 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
1162 inet
->freebind
= !!val
;
1165 case IP_IPSEC_POLICY
:
1166 case IP_XFRM_POLICY
:
1168 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1170 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
1173 case IP_TRANSPARENT
:
1174 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
1175 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1181 inet
->transparent
= !!val
;
1187 if (val
< 0 || val
> 255)
1189 inet
->min_ttl
= val
;
1209 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1213 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1214 * destination in skb->cb[] before dst drop.
1215 * This way, receiver doesn't make cache line misses to read rtable.
1217 void ipv4_pktinfo_prepare(const struct sock
*sk
, struct sk_buff
*skb
)
1219 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1220 bool prepare
= (inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
) ||
1223 if (prepare
&& skb_rtable(skb
)) {
1224 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1225 * which has interface index (iif) as the first member of the
1226 * underlying inet{6}_skb_parm struct. This code then overlays
1227 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1228 * element so the iif is picked up from the prior IPCB. If iif
1229 * is the loopback interface, then return the sending interface
1230 * (e.g., process binds socket to eth0 for Tx which is
1231 * redirected to loopback in the rtable/dst).
1233 if (pktinfo
->ipi_ifindex
== LOOPBACK_IFINDEX
)
1234 pktinfo
->ipi_ifindex
= inet_iif(skb
);
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;
1241 /* We need to keep the dst for __ip_options_echo()
1242 * We could restrict the test to opt.ts_needtime || opt.srr,
1243 * but the following is good enough as IP options are not often used.
1245 if (unlikely(IPCB(skb
)->opt
.optlen
))
1251 int ip_setsockopt(struct sock
*sk
, int level
,
1252 int optname
, char __user
*optval
, unsigned int optlen
)
1256 if (level
!= SOL_IP
)
1257 return -ENOPROTOOPT
;
1259 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1260 #ifdef CONFIG_NETFILTER
1261 /* we need to exclude all possible ENOPROTOOPTs except default case */
1262 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1263 optname
!= IP_IPSEC_POLICY
&&
1264 optname
!= IP_XFRM_POLICY
&&
1265 !ip_mroute_opt(optname
)) {
1267 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1273 EXPORT_SYMBOL(ip_setsockopt
);
1275 #ifdef CONFIG_COMPAT
1276 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1277 char __user
*optval
, unsigned int optlen
)
1281 if (level
!= SOL_IP
)
1282 return -ENOPROTOOPT
;
1284 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1285 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1288 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1289 #ifdef CONFIG_NETFILTER
1290 /* we need to exclude all possible ENOPROTOOPTs except default case */
1291 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1292 optname
!= IP_IPSEC_POLICY
&&
1293 optname
!= IP_XFRM_POLICY
&&
1294 !ip_mroute_opt(optname
)) {
1296 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1303 EXPORT_SYMBOL(compat_ip_setsockopt
);
1307 * Get the options. Note for future reference. The GET of IP options gets
1308 * the _received_ ones. The set sets the _sent_ ones.
1311 static bool getsockopt_needs_rtnl(int optname
)
1315 case MCAST_MSFILTER
:
1321 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1322 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1324 struct inet_sock
*inet
= inet_sk(sk
);
1325 bool needs_rtnl
= getsockopt_needs_rtnl(optname
);
1329 if (level
!= SOL_IP
)
1332 if (ip_mroute_opt(optname
))
1333 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1335 if (get_user(len
, optlen
))
1347 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1348 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1349 struct ip_options_rcu
*inet_opt
;
1351 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1352 lockdep_sock_is_held(sk
));
1355 memcpy(optbuf
, &inet_opt
->opt
,
1356 sizeof(struct ip_options
) +
1357 inet_opt
->opt
.optlen
);
1360 if (opt
->optlen
== 0)
1361 return put_user(0, optlen
);
1363 ip_options_undo(opt
);
1365 len
= min_t(unsigned int, len
, opt
->optlen
);
1366 if (put_user(len
, optlen
))
1368 if (copy_to_user(optval
, opt
->__data
, len
))
1373 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1376 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1379 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1382 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1385 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1388 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1390 case IP_RECVORIGDSTADDR
:
1391 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1394 val
= (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) != 0;
1396 case IP_RECVFRAGSIZE
:
1397 val
= (inet
->cmsg_flags
& IP_CMSG_RECVFRAGSIZE
) != 0;
1404 struct net
*net
= sock_net(sk
);
1405 val
= (inet
->uc_ttl
== -1 ?
1406 net
->ipv4
.sysctl_ip_default_ttl
:
1411 val
= inet
->hdrincl
;
1414 val
= inet
->nodefrag
;
1416 case IP_BIND_ADDRESS_NO_PORT
:
1417 val
= inet
->bind_address_no_port
;
1419 case IP_MTU_DISCOVER
:
1420 val
= inet
->pmtudisc
;
1424 struct dst_entry
*dst
;
1426 dst
= sk_dst_get(sk
);
1438 val
= inet
->recverr
;
1440 case IP_MULTICAST_TTL
:
1443 case IP_MULTICAST_LOOP
:
1444 val
= inet
->mc_loop
;
1447 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1449 case IP_MULTICAST_IF
:
1451 struct in_addr addr
;
1452 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1453 addr
.s_addr
= inet
->mc_addr
;
1456 if (put_user(len
, optlen
))
1458 if (copy_to_user(optval
, &addr
, len
))
1464 struct ip_msfilter msf
;
1466 if (len
< IP_MSFILTER_SIZE(0)) {
1470 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1474 err
= ip_mc_msfget(sk
, &msf
,
1475 (struct ip_msfilter __user
*)optval
, optlen
);
1478 case MCAST_MSFILTER
:
1480 struct group_filter gsf
;
1482 if (len
< GROUP_FILTER_SIZE(0)) {
1486 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1490 err
= ip_mc_gsfget(sk
, &gsf
,
1491 (struct group_filter __user
*)optval
,
1495 case IP_MULTICAST_ALL
:
1504 if (sk
->sk_type
!= SOCK_STREAM
)
1505 return -ENOPROTOOPT
;
1507 msg
.msg_control
= (__force
void *) optval
;
1508 msg
.msg_controllen
= len
;
1509 msg
.msg_flags
= flags
;
1511 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1512 struct in_pktinfo info
;
1514 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1515 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1516 info
.ipi_ifindex
= inet
->mc_index
;
1517 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1519 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1520 int hlim
= inet
->mc_ttl
;
1521 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1523 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1524 int tos
= inet
->rcv_tos
;
1525 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1527 len
-= msg
.msg_controllen
;
1528 return put_user(len
, optlen
);
1531 val
= inet
->freebind
;
1533 case IP_TRANSPARENT
:
1534 val
= inet
->transparent
;
1537 val
= inet
->min_ttl
;
1541 return -ENOPROTOOPT
;
1545 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1546 unsigned char ucval
= (unsigned char)val
;
1548 if (put_user(len
, optlen
))
1550 if (copy_to_user(optval
, &ucval
, 1))
1553 len
= min_t(unsigned int, sizeof(int), len
);
1554 if (put_user(len
, optlen
))
1556 if (copy_to_user(optval
, &val
, len
))
1568 int ip_getsockopt(struct sock
*sk
, int level
,
1569 int optname
, char __user
*optval
, int __user
*optlen
)
1573 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1574 #ifdef CONFIG_NETFILTER
1575 /* we need to exclude all possible ENOPROTOOPTs except default case */
1576 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1577 !ip_mroute_opt(optname
)) {
1580 if (get_user(len
, optlen
))
1584 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1588 err
= put_user(len
, optlen
);
1594 EXPORT_SYMBOL(ip_getsockopt
);
1596 #ifdef CONFIG_COMPAT
1597 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1598 char __user
*optval
, int __user
*optlen
)
1602 if (optname
== MCAST_MSFILTER
)
1603 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1606 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1609 #ifdef CONFIG_NETFILTER
1610 /* we need to exclude all possible ENOPROTOOPTs except default case */
1611 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1612 !ip_mroute_opt(optname
)) {
1615 if (get_user(len
, optlen
))
1619 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1622 err
= put_user(len
, optlen
);
1628 EXPORT_SYMBOL(compat_ip_getsockopt
);