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 <asm/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_checksum(struct msghdr
*msg
, struct sk_buff
*skb
,
103 __wsum csum
= skb
->csum
;
105 if (skb
->ip_summed
!= CHECKSUM_COMPLETE
)
109 csum
= csum_sub(csum
, csum_partial(skb_transport_header(skb
),
112 put_cmsg(msg
, SOL_IP
, IP_CHECKSUM
, sizeof(__wsum
), &csum
);
115 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
121 err
= security_socket_getpeersec_dgram(NULL
, skb
, &secid
);
125 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
129 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
130 security_release_secctx(secdata
, seclen
);
133 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
135 struct sockaddr_in sin
;
136 const struct iphdr
*iph
= ip_hdr(skb
);
137 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
139 if (skb_transport_offset(skb
) + 4 > skb
->len
)
142 /* All current transport protocols have the port numbers in the
143 * first four bytes of the transport header and this function is
144 * written with this assumption in mind.
147 sin
.sin_family
= AF_INET
;
148 sin
.sin_addr
.s_addr
= iph
->daddr
;
149 sin
.sin_port
= ports
[1];
150 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
152 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
155 void ip_cmsg_recv_offset(struct msghdr
*msg
, struct sk_buff
*skb
,
158 struct inet_sock
*inet
= inet_sk(skb
->sk
);
159 unsigned int flags
= inet
->cmsg_flags
;
161 /* Ordered by supposed usage frequency */
162 if (flags
& IP_CMSG_PKTINFO
) {
163 ip_cmsg_recv_pktinfo(msg
, skb
);
165 flags
&= ~IP_CMSG_PKTINFO
;
170 if (flags
& IP_CMSG_TTL
) {
171 ip_cmsg_recv_ttl(msg
, skb
);
173 flags
&= ~IP_CMSG_TTL
;
178 if (flags
& IP_CMSG_TOS
) {
179 ip_cmsg_recv_tos(msg
, skb
);
181 flags
&= ~IP_CMSG_TOS
;
186 if (flags
& IP_CMSG_RECVOPTS
) {
187 ip_cmsg_recv_opts(msg
, skb
);
189 flags
&= ~IP_CMSG_RECVOPTS
;
194 if (flags
& IP_CMSG_RETOPTS
) {
195 ip_cmsg_recv_retopts(msg
, skb
);
197 flags
&= ~IP_CMSG_RETOPTS
;
202 if (flags
& IP_CMSG_PASSSEC
) {
203 ip_cmsg_recv_security(msg
, skb
);
205 flags
&= ~IP_CMSG_PASSSEC
;
210 if (flags
& IP_CMSG_ORIGDSTADDR
) {
211 ip_cmsg_recv_dstaddr(msg
, skb
);
213 flags
&= ~IP_CMSG_ORIGDSTADDR
;
218 if (flags
& IP_CMSG_CHECKSUM
)
219 ip_cmsg_recv_checksum(msg
, skb
, offset
);
221 EXPORT_SYMBOL(ip_cmsg_recv_offset
);
223 int ip_cmsg_send(struct sock
*sk
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
,
227 struct cmsghdr
*cmsg
;
228 struct net
*net
= sock_net(sk
);
230 for_each_cmsghdr(cmsg
, msg
) {
231 if (!CMSG_OK(msg
, cmsg
))
233 #if IS_ENABLED(CONFIG_IPV6)
235 cmsg
->cmsg_level
== SOL_IPV6
&&
236 cmsg
->cmsg_type
== IPV6_PKTINFO
) {
237 struct in6_pktinfo
*src_info
;
239 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*src_info
)))
241 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
242 if (!ipv6_addr_v4mapped(&src_info
->ipi6_addr
))
244 ipc
->oif
= src_info
->ipi6_ifindex
;
245 ipc
->addr
= src_info
->ipi6_addr
.s6_addr32
[3];
249 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
250 err
= __sock_cmsg_send(sk
, msg
, cmsg
, &ipc
->sockc
);
256 if (cmsg
->cmsg_level
!= SOL_IP
)
258 switch (cmsg
->cmsg_type
) {
260 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
262 /* Our caller is responsible for freeing ipc->opt */
263 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
264 err
< 40 ? err
: 40);
270 struct in_pktinfo
*info
;
271 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
273 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
274 ipc
->oif
= info
->ipi_ifindex
;
275 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
279 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
281 val
= *(int *)CMSG_DATA(cmsg
);
282 if (val
< 1 || val
> 255)
287 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
289 val
= *(int *)CMSG_DATA(cmsg
);
290 if (val
< 0 || val
> 255)
293 ipc
->priority
= rt_tos2priority(ipc
->tos
);
304 /* Special input handler for packets caught by router alert option.
305 They are selected only by protocol field, and then processed likely
306 local ones; but only if someone wants them! Otherwise, router
307 not running rsvpd will kill RSVP.
309 It is user level problem, what it will make with them.
310 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
311 but receiver should be enough clever f.e. to forward mtrace requests,
312 sent to multicast group to reach destination designated router.
314 struct ip_ra_chain __rcu
*ip_ra_chain
;
315 static DEFINE_SPINLOCK(ip_ra_lock
);
318 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
320 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
322 sock_put(ra
->saved_sk
);
326 int ip_ra_control(struct sock
*sk
, unsigned char on
,
327 void (*destructor
)(struct sock
*))
329 struct ip_ra_chain
*ra
, *new_ra
;
330 struct ip_ra_chain __rcu
**rap
;
332 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
335 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
337 spin_lock_bh(&ip_ra_lock
);
338 for (rap
= &ip_ra_chain
;
339 (ra
= rcu_dereference_protected(*rap
,
340 lockdep_is_held(&ip_ra_lock
))) != NULL
;
344 spin_unlock_bh(&ip_ra_lock
);
348 /* dont let ip_call_ra_chain() use sk again */
350 RCU_INIT_POINTER(*rap
, ra
->next
);
351 spin_unlock_bh(&ip_ra_lock
);
356 * Delay sock_put(sk) and kfree(ra) after one rcu grace
357 * period. This guarantee ip_call_ra_chain() dont need
358 * to mess with socket refcounts.
361 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
366 spin_unlock_bh(&ip_ra_lock
);
370 new_ra
->destructor
= destructor
;
372 RCU_INIT_POINTER(new_ra
->next
, ra
);
373 rcu_assign_pointer(*rap
, new_ra
);
375 spin_unlock_bh(&ip_ra_lock
);
380 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
381 __be16 port
, u32 info
, u8
*payload
)
383 struct sock_exterr_skb
*serr
;
385 skb
= skb_clone(skb
, GFP_ATOMIC
);
389 serr
= SKB_EXT_ERR(skb
);
390 serr
->ee
.ee_errno
= err
;
391 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
392 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
393 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
395 serr
->ee
.ee_info
= info
;
396 serr
->ee
.ee_data
= 0;
397 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
398 skb_network_header(skb
);
401 if (skb_pull(skb
, payload
- skb
->data
)) {
402 skb_reset_transport_header(skb
);
403 if (sock_queue_err_skb(sk
, skb
) == 0)
409 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
411 struct inet_sock
*inet
= inet_sk(sk
);
412 struct sock_exterr_skb
*serr
;
419 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
423 skb_put(skb
, sizeof(struct iphdr
));
424 skb_reset_network_header(skb
);
428 serr
= SKB_EXT_ERR(skb
);
429 serr
->ee
.ee_errno
= err
;
430 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
431 serr
->ee
.ee_type
= 0;
432 serr
->ee
.ee_code
= 0;
434 serr
->ee
.ee_info
= info
;
435 serr
->ee
.ee_data
= 0;
436 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
439 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
440 skb_reset_transport_header(skb
);
442 if (sock_queue_err_skb(sk
, skb
))
446 /* For some errors we have valid addr_offset even with zero payload and
447 * zero port. Also, addr_offset should be supported if port is set.
449 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb
*serr
)
451 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
452 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
455 /* IPv4 supports cmsg on all imcp errors and some timestamps
457 * Timestamp code paths do not initialize the fields expected by cmsg:
458 * the PKTINFO fields in skb->cb[]. Fill those in here.
460 static bool ipv4_datagram_support_cmsg(const struct sock
*sk
,
464 struct in_pktinfo
*info
;
466 if (ee_origin
== SO_EE_ORIGIN_ICMP
)
469 if (ee_origin
== SO_EE_ORIGIN_LOCAL
)
472 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
473 * timestamp with egress dev. Not possible for packets without dev
474 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
476 if ((!(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_CMSG
)) ||
480 info
= PKTINFO_SKB_CB(skb
);
481 info
->ipi_spec_dst
.s_addr
= ip_hdr(skb
)->saddr
;
482 info
->ipi_ifindex
= skb
->dev
->ifindex
;
487 * Handle MSG_ERRQUEUE
489 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
491 struct sock_exterr_skb
*serr
;
493 DECLARE_SOCKADDR(struct sockaddr_in
*, sin
, msg
->msg_name
);
495 struct sock_extended_err ee
;
496 struct sockaddr_in offender
;
501 WARN_ON_ONCE(sk
->sk_family
== AF_INET6
);
504 skb
= sock_dequeue_err_skb(sk
);
510 msg
->msg_flags
|= MSG_TRUNC
;
513 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
518 sock_recv_timestamp(msg
, sk
, skb
);
520 serr
= SKB_EXT_ERR(skb
);
522 if (sin
&& ipv4_datagram_support_addr(serr
)) {
523 sin
->sin_family
= AF_INET
;
524 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
526 sin
->sin_port
= serr
->port
;
527 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
528 *addr_len
= sizeof(*sin
);
531 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
532 sin
= &errhdr
.offender
;
533 memset(sin
, 0, sizeof(*sin
));
535 if (ipv4_datagram_support_cmsg(sk
, skb
, serr
->ee
.ee_origin
)) {
536 sin
->sin_family
= AF_INET
;
537 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
538 if (inet_sk(sk
)->cmsg_flags
)
539 ip_cmsg_recv(msg
, skb
);
542 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
544 /* Now we could try to dump offended packet options */
546 msg
->msg_flags
|= MSG_ERRQUEUE
;
556 * Socket option code for IP. This is the end of the line after any
557 * TCP,UDP etc options on an IP socket.
559 static bool setsockopt_needs_rtnl(int optname
)
562 case IP_ADD_MEMBERSHIP
:
563 case IP_ADD_SOURCE_MEMBERSHIP
:
564 case IP_BLOCK_SOURCE
:
565 case IP_DROP_MEMBERSHIP
:
566 case IP_DROP_SOURCE_MEMBERSHIP
:
568 case IP_UNBLOCK_SOURCE
:
569 case MCAST_BLOCK_SOURCE
:
571 case MCAST_JOIN_GROUP
:
572 case MCAST_JOIN_SOURCE_GROUP
:
573 case MCAST_LEAVE_GROUP
:
574 case MCAST_LEAVE_SOURCE_GROUP
:
575 case MCAST_UNBLOCK_SOURCE
:
581 static int do_ip_setsockopt(struct sock
*sk
, int level
,
582 int optname
, char __user
*optval
, unsigned int optlen
)
584 struct inet_sock
*inet
= inet_sk(sk
);
585 struct net
*net
= sock_net(sk
);
587 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
598 case IP_MTU_DISCOVER
:
600 case IP_ROUTER_ALERT
:
606 case IP_BIND_ADDRESS_NO_PORT
:
608 case IP_MULTICAST_TTL
:
609 case IP_MULTICAST_ALL
:
610 case IP_MULTICAST_LOOP
:
611 case IP_RECVORIGDSTADDR
:
613 if (optlen
>= sizeof(int)) {
614 if (get_user(val
, (int __user
*) optval
))
616 } else if (optlen
>= sizeof(char)) {
619 if (get_user(ucval
, (unsigned char __user
*) optval
))
625 /* If optlen==0, it is equivalent to val == 0 */
627 if (ip_mroute_opt(optname
))
628 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
638 struct ip_options_rcu
*old
, *opt
= NULL
;
642 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
646 old
= rcu_dereference_protected(inet
->inet_opt
,
647 lockdep_sock_is_held(sk
));
649 struct inet_connection_sock
*icsk
= inet_csk(sk
);
650 #if IS_ENABLED(CONFIG_IPV6)
651 if (sk
->sk_family
== PF_INET
||
652 (!((1 << sk
->sk_state
) &
653 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
654 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
657 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
659 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
660 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
661 #if IS_ENABLED(CONFIG_IPV6)
665 rcu_assign_pointer(inet
->inet_opt
, opt
);
672 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
674 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
678 inet
->cmsg_flags
|= IP_CMSG_TTL
;
680 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
684 inet
->cmsg_flags
|= IP_CMSG_TOS
;
686 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
690 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
692 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
696 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
698 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
702 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
704 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
706 case IP_RECVORIGDSTADDR
:
708 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
710 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
714 if (!(inet
->cmsg_flags
& IP_CMSG_CHECKSUM
)) {
715 inet_inc_convert_csum(sk
);
716 inet
->cmsg_flags
|= IP_CMSG_CHECKSUM
;
719 if (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) {
720 inet_dec_convert_csum(sk
);
721 inet
->cmsg_flags
&= ~IP_CMSG_CHECKSUM
;
725 case IP_TOS
: /* This sets both TOS and Precedence */
726 if (sk
->sk_type
== SOCK_STREAM
) {
727 val
&= ~INET_ECN_MASK
;
728 val
|= inet
->tos
& INET_ECN_MASK
;
730 if (inet
->tos
!= val
) {
732 sk
->sk_priority
= rt_tos2priority(val
);
739 if (val
!= -1 && (val
< 1 || val
> 255))
744 if (sk
->sk_type
!= SOCK_RAW
) {
748 inet
->hdrincl
= val
? 1 : 0;
751 if (sk
->sk_type
!= SOCK_RAW
) {
755 inet
->nodefrag
= val
? 1 : 0;
757 case IP_BIND_ADDRESS_NO_PORT
:
758 inet
->bind_address_no_port
= val
? 1 : 0;
760 case IP_MTU_DISCOVER
:
761 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_OMIT
)
763 inet
->pmtudisc
= val
;
766 inet
->recverr
= !!val
;
768 skb_queue_purge(&sk
->sk_error_queue
);
770 case IP_MULTICAST_TTL
:
771 if (sk
->sk_type
== SOCK_STREAM
)
777 if (val
< 0 || val
> 255)
781 case IP_MULTICAST_LOOP
:
784 inet
->mc_loop
= !!val
;
788 struct net_device
*dev
= NULL
;
791 if (optlen
!= sizeof(int))
794 ifindex
= (__force
int)ntohl((__force __be32
)val
);
801 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
802 err
= -EADDRNOTAVAIL
;
808 if (sk
->sk_bound_dev_if
)
811 inet
->uc_index
= ifindex
;
815 case IP_MULTICAST_IF
:
817 struct ip_mreqn mreq
;
818 struct net_device
*dev
= NULL
;
820 if (sk
->sk_type
== SOCK_STREAM
)
823 * Check the arguments are allowable
826 if (optlen
< sizeof(struct in_addr
))
830 if (optlen
>= sizeof(struct ip_mreqn
)) {
831 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
834 memset(&mreq
, 0, sizeof(mreq
));
835 if (optlen
>= sizeof(struct ip_mreq
)) {
836 if (copy_from_user(&mreq
, optval
,
837 sizeof(struct ip_mreq
)))
839 } else if (optlen
>= sizeof(struct in_addr
)) {
840 if (copy_from_user(&mreq
.imr_address
, optval
,
841 sizeof(struct in_addr
)))
846 if (!mreq
.imr_ifindex
) {
847 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
853 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
855 mreq
.imr_ifindex
= dev
->ifindex
;
857 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
860 err
= -EADDRNOTAVAIL
;
866 if (sk
->sk_bound_dev_if
&&
867 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
870 inet
->mc_index
= mreq
.imr_ifindex
;
871 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
876 case IP_ADD_MEMBERSHIP
:
877 case IP_DROP_MEMBERSHIP
:
879 struct ip_mreqn mreq
;
882 if (inet_sk(sk
)->is_icsk
)
885 if (optlen
< sizeof(struct ip_mreq
))
888 if (optlen
>= sizeof(struct ip_mreqn
)) {
889 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
892 memset(&mreq
, 0, sizeof(mreq
));
893 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
897 if (optname
== IP_ADD_MEMBERSHIP
)
898 err
= ip_mc_join_group(sk
, &mreq
);
900 err
= ip_mc_leave_group(sk
, &mreq
);
905 struct ip_msfilter
*msf
;
907 if (optlen
< IP_MSFILTER_SIZE(0))
909 if (optlen
> sysctl_optmem_max
) {
913 msf
= kmalloc(optlen
, GFP_KERNEL
);
919 if (copy_from_user(msf
, optval
, optlen
)) {
923 /* numsrc >= (1G-4) overflow in 32 bits */
924 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
925 msf
->imsf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
930 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
935 err
= ip_mc_msfilter(sk
, msf
, 0);
939 case IP_BLOCK_SOURCE
:
940 case IP_UNBLOCK_SOURCE
:
941 case IP_ADD_SOURCE_MEMBERSHIP
:
942 case IP_DROP_SOURCE_MEMBERSHIP
:
944 struct ip_mreq_source mreqs
;
947 if (optlen
!= sizeof(struct ip_mreq_source
))
949 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
953 if (optname
== IP_BLOCK_SOURCE
) {
954 omode
= MCAST_EXCLUDE
;
956 } else if (optname
== IP_UNBLOCK_SOURCE
) {
957 omode
= MCAST_EXCLUDE
;
959 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
960 struct ip_mreqn mreq
;
962 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
963 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
964 mreq
.imr_ifindex
= 0;
965 err
= ip_mc_join_group(sk
, &mreq
);
966 if (err
&& err
!= -EADDRINUSE
)
968 omode
= MCAST_INCLUDE
;
970 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
971 omode
= MCAST_INCLUDE
;
974 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
977 case MCAST_JOIN_GROUP
:
978 case MCAST_LEAVE_GROUP
:
980 struct group_req greq
;
981 struct sockaddr_in
*psin
;
982 struct ip_mreqn mreq
;
984 if (optlen
< sizeof(struct group_req
))
987 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
989 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
990 if (psin
->sin_family
!= AF_INET
)
992 memset(&mreq
, 0, sizeof(mreq
));
993 mreq
.imr_multiaddr
= psin
->sin_addr
;
994 mreq
.imr_ifindex
= greq
.gr_interface
;
996 if (optname
== MCAST_JOIN_GROUP
)
997 err
= ip_mc_join_group(sk
, &mreq
);
999 err
= ip_mc_leave_group(sk
, &mreq
);
1002 case MCAST_JOIN_SOURCE_GROUP
:
1003 case MCAST_LEAVE_SOURCE_GROUP
:
1004 case MCAST_BLOCK_SOURCE
:
1005 case MCAST_UNBLOCK_SOURCE
:
1007 struct group_source_req greqs
;
1008 struct ip_mreq_source mreqs
;
1009 struct sockaddr_in
*psin
;
1012 if (optlen
!= sizeof(struct group_source_req
))
1014 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
1018 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
1019 greqs
.gsr_source
.ss_family
!= AF_INET
) {
1020 err
= -EADDRNOTAVAIL
;
1023 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1024 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
1025 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
1026 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
1027 mreqs
.imr_interface
= 0; /* use index for mc_source */
1029 if (optname
== MCAST_BLOCK_SOURCE
) {
1030 omode
= MCAST_EXCLUDE
;
1032 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
1033 omode
= MCAST_EXCLUDE
;
1035 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
1036 struct ip_mreqn mreq
;
1038 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1039 mreq
.imr_multiaddr
= psin
->sin_addr
;
1040 mreq
.imr_address
.s_addr
= 0;
1041 mreq
.imr_ifindex
= greqs
.gsr_interface
;
1042 err
= ip_mc_join_group(sk
, &mreq
);
1043 if (err
&& err
!= -EADDRINUSE
)
1045 greqs
.gsr_interface
= mreq
.imr_ifindex
;
1046 omode
= MCAST_INCLUDE
;
1048 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1049 omode
= MCAST_INCLUDE
;
1052 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
1053 greqs
.gsr_interface
);
1056 case MCAST_MSFILTER
:
1058 struct sockaddr_in
*psin
;
1059 struct ip_msfilter
*msf
= NULL
;
1060 struct group_filter
*gsf
= NULL
;
1061 int msize
, i
, ifindex
;
1063 if (optlen
< GROUP_FILTER_SIZE(0))
1065 if (optlen
> sysctl_optmem_max
) {
1069 gsf
= kmalloc(optlen
, GFP_KERNEL
);
1075 if (copy_from_user(gsf
, optval
, optlen
))
1078 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1079 if (gsf
->gf_numsrc
>= 0x1ffffff ||
1080 gsf
->gf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
1084 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
1088 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
1089 msf
= kmalloc(msize
, GFP_KERNEL
);
1094 ifindex
= gsf
->gf_interface
;
1095 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
1096 if (psin
->sin_family
!= AF_INET
) {
1097 err
= -EADDRNOTAVAIL
;
1100 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
1101 msf
->imsf_interface
= 0;
1102 msf
->imsf_fmode
= gsf
->gf_fmode
;
1103 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
1104 err
= -EADDRNOTAVAIL
;
1105 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
1106 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
1108 if (psin
->sin_family
!= AF_INET
)
1110 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
1115 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
1121 case IP_MULTICAST_ALL
:
1124 if (val
!= 0 && val
!= 1)
1128 case IP_ROUTER_ALERT
:
1129 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
1135 inet
->freebind
= !!val
;
1138 case IP_IPSEC_POLICY
:
1139 case IP_XFRM_POLICY
:
1141 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1143 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
1146 case IP_TRANSPARENT
:
1147 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
1148 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1154 inet
->transparent
= !!val
;
1160 if (val
< 0 || val
> 255)
1162 inet
->min_ttl
= val
;
1182 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1186 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1187 * destination in skb->cb[] before dst drop.
1188 * This way, receiver doesn't make cache line misses to read rtable.
1190 void ipv4_pktinfo_prepare(const struct sock
*sk
, struct sk_buff
*skb
)
1192 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1193 bool prepare
= (inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
) ||
1196 if (prepare
&& skb_rtable(skb
)) {
1197 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1198 * which has interface index (iif) as the first member of the
1199 * underlying inet{6}_skb_parm struct. This code then overlays
1200 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1201 * element so the iif is picked up from the prior IPCB
1203 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1205 pktinfo
->ipi_ifindex
= 0;
1206 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1211 int ip_setsockopt(struct sock
*sk
, int level
,
1212 int optname
, char __user
*optval
, unsigned int optlen
)
1216 if (level
!= SOL_IP
)
1217 return -ENOPROTOOPT
;
1219 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1220 #ifdef CONFIG_NETFILTER
1221 /* we need to exclude all possible ENOPROTOOPTs except default case */
1222 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1223 optname
!= IP_IPSEC_POLICY
&&
1224 optname
!= IP_XFRM_POLICY
&&
1225 !ip_mroute_opt(optname
)) {
1227 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1233 EXPORT_SYMBOL(ip_setsockopt
);
1235 #ifdef CONFIG_COMPAT
1236 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1237 char __user
*optval
, unsigned int optlen
)
1241 if (level
!= SOL_IP
)
1242 return -ENOPROTOOPT
;
1244 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1245 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1248 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1249 #ifdef CONFIG_NETFILTER
1250 /* we need to exclude all possible ENOPROTOOPTs except default case */
1251 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1252 optname
!= IP_IPSEC_POLICY
&&
1253 optname
!= IP_XFRM_POLICY
&&
1254 !ip_mroute_opt(optname
)) {
1256 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1263 EXPORT_SYMBOL(compat_ip_setsockopt
);
1267 * Get the options. Note for future reference. The GET of IP options gets
1268 * the _received_ ones. The set sets the _sent_ ones.
1271 static bool getsockopt_needs_rtnl(int optname
)
1275 case MCAST_MSFILTER
:
1281 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1282 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1284 struct inet_sock
*inet
= inet_sk(sk
);
1285 bool needs_rtnl
= getsockopt_needs_rtnl(optname
);
1289 if (level
!= SOL_IP
)
1292 if (ip_mroute_opt(optname
))
1293 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1295 if (get_user(len
, optlen
))
1307 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1308 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1309 struct ip_options_rcu
*inet_opt
;
1311 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1312 lockdep_sock_is_held(sk
));
1315 memcpy(optbuf
, &inet_opt
->opt
,
1316 sizeof(struct ip_options
) +
1317 inet_opt
->opt
.optlen
);
1320 if (opt
->optlen
== 0)
1321 return put_user(0, optlen
);
1323 ip_options_undo(opt
);
1325 len
= min_t(unsigned int, len
, opt
->optlen
);
1326 if (put_user(len
, optlen
))
1328 if (copy_to_user(optval
, opt
->__data
, len
))
1333 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1336 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1339 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1342 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1345 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1348 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1350 case IP_RECVORIGDSTADDR
:
1351 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1354 val
= (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) != 0;
1361 struct net
*net
= sock_net(sk
);
1362 val
= (inet
->uc_ttl
== -1 ?
1363 net
->ipv4
.sysctl_ip_default_ttl
:
1368 val
= inet
->hdrincl
;
1371 val
= inet
->nodefrag
;
1373 case IP_BIND_ADDRESS_NO_PORT
:
1374 val
= inet
->bind_address_no_port
;
1376 case IP_MTU_DISCOVER
:
1377 val
= inet
->pmtudisc
;
1381 struct dst_entry
*dst
;
1383 dst
= sk_dst_get(sk
);
1395 val
= inet
->recverr
;
1397 case IP_MULTICAST_TTL
:
1400 case IP_MULTICAST_LOOP
:
1401 val
= inet
->mc_loop
;
1404 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1406 case IP_MULTICAST_IF
:
1408 struct in_addr addr
;
1409 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1410 addr
.s_addr
= inet
->mc_addr
;
1413 if (put_user(len
, optlen
))
1415 if (copy_to_user(optval
, &addr
, len
))
1421 struct ip_msfilter msf
;
1423 if (len
< IP_MSFILTER_SIZE(0)) {
1427 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1431 err
= ip_mc_msfget(sk
, &msf
,
1432 (struct ip_msfilter __user
*)optval
, optlen
);
1435 case MCAST_MSFILTER
:
1437 struct group_filter gsf
;
1439 if (len
< GROUP_FILTER_SIZE(0)) {
1443 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1447 err
= ip_mc_gsfget(sk
, &gsf
,
1448 (struct group_filter __user
*)optval
,
1452 case IP_MULTICAST_ALL
:
1461 if (sk
->sk_type
!= SOCK_STREAM
)
1462 return -ENOPROTOOPT
;
1464 msg
.msg_control
= (__force
void *) optval
;
1465 msg
.msg_controllen
= len
;
1466 msg
.msg_flags
= flags
;
1468 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1469 struct in_pktinfo info
;
1471 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1472 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1473 info
.ipi_ifindex
= inet
->mc_index
;
1474 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1476 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1477 int hlim
= inet
->mc_ttl
;
1478 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1480 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1481 int tos
= inet
->rcv_tos
;
1482 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1484 len
-= msg
.msg_controllen
;
1485 return put_user(len
, optlen
);
1488 val
= inet
->freebind
;
1490 case IP_TRANSPARENT
:
1491 val
= inet
->transparent
;
1494 val
= inet
->min_ttl
;
1498 return -ENOPROTOOPT
;
1502 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1503 unsigned char ucval
= (unsigned char)val
;
1505 if (put_user(len
, optlen
))
1507 if (copy_to_user(optval
, &ucval
, 1))
1510 len
= min_t(unsigned int, sizeof(int), len
);
1511 if (put_user(len
, optlen
))
1513 if (copy_to_user(optval
, &val
, len
))
1525 int ip_getsockopt(struct sock
*sk
, int level
,
1526 int optname
, char __user
*optval
, int __user
*optlen
)
1530 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1531 #ifdef CONFIG_NETFILTER
1532 /* we need to exclude all possible ENOPROTOOPTs except default case */
1533 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1534 !ip_mroute_opt(optname
)) {
1537 if (get_user(len
, optlen
))
1541 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1545 err
= put_user(len
, optlen
);
1551 EXPORT_SYMBOL(ip_getsockopt
);
1553 #ifdef CONFIG_COMPAT
1554 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1555 char __user
*optval
, int __user
*optlen
)
1559 if (optname
== MCAST_MSFILTER
)
1560 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1563 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1566 #ifdef CONFIG_NETFILTER
1567 /* we need to exclude all possible ENOPROTOOPTs except default case */
1568 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1569 !ip_mroute_opt(optname
)) {
1572 if (get_user(len
, optlen
))
1576 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1579 err
= put_user(len
, optlen
);
1585 EXPORT_SYMBOL(compat_ip_getsockopt
);