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 #if IS_ENABLED(CONFIG_IPV6)
41 #include <net/transp_v6.h>
43 #include <net/ip_fib.h>
45 #include <linux/errqueue.h>
46 #include <asm/uaccess.h>
48 #define IP_CMSG_PKTINFO 1
51 #define IP_CMSG_RECVOPTS 8
52 #define IP_CMSG_RETOPTS 16
53 #define IP_CMSG_PASSSEC 32
54 #define IP_CMSG_ORIGDSTADDR 64
57 * SOL_IP control messages.
60 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
62 struct in_pktinfo info
= *PKTINFO_SKB_CB(skb
);
64 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
66 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
69 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
71 int ttl
= ip_hdr(skb
)->ttl
;
72 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
75 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
77 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
80 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
82 if (IPCB(skb
)->opt
.optlen
== 0)
85 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
90 static void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
92 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
93 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
95 if (IPCB(skb
)->opt
.optlen
== 0)
98 if (ip_options_echo(opt
, skb
)) {
99 msg
->msg_flags
|= MSG_CTRUNC
;
102 ip_options_undo(opt
);
104 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
107 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
113 err
= security_socket_getpeersec_dgram(NULL
, skb
, &secid
);
117 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
121 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
122 security_release_secctx(secdata
, seclen
);
125 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
127 struct sockaddr_in sin
;
128 const struct iphdr
*iph
= ip_hdr(skb
);
129 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
131 if (skb_transport_offset(skb
) + 4 > skb
->len
)
134 /* All current transport protocols have the port numbers in the
135 * first four bytes of the transport header and this function is
136 * written with this assumption in mind.
139 sin
.sin_family
= AF_INET
;
140 sin
.sin_addr
.s_addr
= iph
->daddr
;
141 sin
.sin_port
= ports
[1];
142 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
144 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
147 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
149 struct inet_sock
*inet
= inet_sk(skb
->sk
);
150 unsigned int flags
= inet
->cmsg_flags
;
152 /* Ordered by supposed usage frequency */
154 ip_cmsg_recv_pktinfo(msg
, skb
);
155 if ((flags
>>= 1) == 0)
159 ip_cmsg_recv_ttl(msg
, skb
);
160 if ((flags
>>= 1) == 0)
164 ip_cmsg_recv_tos(msg
, skb
);
165 if ((flags
>>= 1) == 0)
169 ip_cmsg_recv_opts(msg
, skb
);
170 if ((flags
>>= 1) == 0)
174 ip_cmsg_recv_retopts(msg
, skb
);
175 if ((flags
>>= 1) == 0)
179 ip_cmsg_recv_security(msg
, skb
);
181 if ((flags
>>= 1) == 0)
184 ip_cmsg_recv_dstaddr(msg
, skb
);
187 EXPORT_SYMBOL(ip_cmsg_recv
);
189 int ip_cmsg_send(struct net
*net
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
,
193 struct cmsghdr
*cmsg
;
195 for_each_cmsghdr(cmsg
, msg
) {
196 if (!CMSG_OK(msg
, cmsg
))
198 #if IS_ENABLED(CONFIG_IPV6)
200 cmsg
->cmsg_level
== SOL_IPV6
&&
201 cmsg
->cmsg_type
== IPV6_PKTINFO
) {
202 struct in6_pktinfo
*src_info
;
204 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*src_info
)))
206 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
207 if (!ipv6_addr_v4mapped(&src_info
->ipi6_addr
))
209 ipc
->oif
= src_info
->ipi6_ifindex
;
210 ipc
->addr
= src_info
->ipi6_addr
.s6_addr32
[3];
214 if (cmsg
->cmsg_level
!= SOL_IP
)
216 switch (cmsg
->cmsg_type
) {
218 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
219 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
220 err
< 40 ? err
: 40);
226 struct in_pktinfo
*info
;
227 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
229 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
230 ipc
->oif
= info
->ipi_ifindex
;
231 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
235 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
237 val
= *(int *)CMSG_DATA(cmsg
);
238 if (val
< 1 || val
> 255)
243 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)))
245 val
= *(int *)CMSG_DATA(cmsg
);
246 if (val
< 0 || val
> 255)
249 ipc
->priority
= rt_tos2priority(ipc
->tos
);
260 /* Special input handler for packets caught by router alert option.
261 They are selected only by protocol field, and then processed likely
262 local ones; but only if someone wants them! Otherwise, router
263 not running rsvpd will kill RSVP.
265 It is user level problem, what it will make with them.
266 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
267 but receiver should be enough clever f.e. to forward mtrace requests,
268 sent to multicast group to reach destination designated router.
270 struct ip_ra_chain __rcu
*ip_ra_chain
;
271 static DEFINE_SPINLOCK(ip_ra_lock
);
274 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
276 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
278 sock_put(ra
->saved_sk
);
282 int ip_ra_control(struct sock
*sk
, unsigned char on
,
283 void (*destructor
)(struct sock
*))
285 struct ip_ra_chain
*ra
, *new_ra
;
286 struct ip_ra_chain __rcu
**rap
;
288 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
291 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
293 spin_lock_bh(&ip_ra_lock
);
294 for (rap
= &ip_ra_chain
;
295 (ra
= rcu_dereference_protected(*rap
,
296 lockdep_is_held(&ip_ra_lock
))) != NULL
;
300 spin_unlock_bh(&ip_ra_lock
);
304 /* dont let ip_call_ra_chain() use sk again */
306 RCU_INIT_POINTER(*rap
, ra
->next
);
307 spin_unlock_bh(&ip_ra_lock
);
312 * Delay sock_put(sk) and kfree(ra) after one rcu grace
313 * period. This guarantee ip_call_ra_chain() dont need
314 * to mess with socket refcounts.
317 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
321 if (new_ra
== NULL
) {
322 spin_unlock_bh(&ip_ra_lock
);
326 new_ra
->destructor
= destructor
;
328 RCU_INIT_POINTER(new_ra
->next
, ra
);
329 rcu_assign_pointer(*rap
, new_ra
);
331 spin_unlock_bh(&ip_ra_lock
);
336 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
337 __be16 port
, u32 info
, u8
*payload
)
339 struct sock_exterr_skb
*serr
;
341 skb
= skb_clone(skb
, GFP_ATOMIC
);
345 serr
= SKB_EXT_ERR(skb
);
346 serr
->ee
.ee_errno
= err
;
347 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
348 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
349 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
351 serr
->ee
.ee_info
= info
;
352 serr
->ee
.ee_data
= 0;
353 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
354 skb_network_header(skb
);
357 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
358 skb_reset_transport_header(skb
);
359 if (sock_queue_err_skb(sk
, skb
) == 0)
365 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
367 struct inet_sock
*inet
= inet_sk(sk
);
368 struct sock_exterr_skb
*serr
;
375 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
379 skb_put(skb
, sizeof(struct iphdr
));
380 skb_reset_network_header(skb
);
384 serr
= SKB_EXT_ERR(skb
);
385 serr
->ee
.ee_errno
= err
;
386 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
387 serr
->ee
.ee_type
= 0;
388 serr
->ee
.ee_code
= 0;
390 serr
->ee
.ee_info
= info
;
391 serr
->ee
.ee_data
= 0;
392 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
395 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
396 skb_reset_transport_header(skb
);
398 if (sock_queue_err_skb(sk
, skb
))
402 static bool ipv4_pktinfo_prepare_errqueue(const struct sock
*sk
,
403 const struct sk_buff
*skb
,
406 struct in_pktinfo
*info
= PKTINFO_SKB_CB(skb
);
408 if ((ee_origin
!= SO_EE_ORIGIN_TIMESTAMPING
) ||
409 (!(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_CMSG
)) ||
413 info
->ipi_spec_dst
.s_addr
= ip_hdr(skb
)->saddr
;
414 info
->ipi_ifindex
= skb
->dev
->ifindex
;
419 * Handle MSG_ERRQUEUE
421 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
423 struct sock_exterr_skb
*serr
;
425 DECLARE_SOCKADDR(struct sockaddr_in
*, sin
, msg
->msg_name
);
427 struct sock_extended_err ee
;
428 struct sockaddr_in offender
;
433 WARN_ON_ONCE(sk
->sk_family
== AF_INET6
);
436 skb
= sock_dequeue_err_skb(sk
);
442 msg
->msg_flags
|= MSG_TRUNC
;
445 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
449 sock_recv_timestamp(msg
, sk
, skb
);
451 serr
= SKB_EXT_ERR(skb
);
454 sin
->sin_family
= AF_INET
;
455 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
457 sin
->sin_port
= serr
->port
;
458 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
459 *addr_len
= sizeof(*sin
);
462 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
463 sin
= &errhdr
.offender
;
464 sin
->sin_family
= AF_UNSPEC
;
466 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
467 ipv4_pktinfo_prepare_errqueue(sk
, skb
, serr
->ee
.ee_origin
)) {
468 struct inet_sock
*inet
= inet_sk(sk
);
470 sin
->sin_family
= AF_INET
;
471 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
473 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
474 if (inet
->cmsg_flags
)
475 ip_cmsg_recv(msg
, skb
);
478 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
480 /* Now we could try to dump offended packet options */
482 msg
->msg_flags
|= MSG_ERRQUEUE
;
493 * Socket option code for IP. This is the end of the line after any
494 * TCP,UDP etc options on an IP socket.
497 static int do_ip_setsockopt(struct sock
*sk
, int level
,
498 int optname
, char __user
*optval
, unsigned int optlen
)
500 struct inet_sock
*inet
= inet_sk(sk
);
512 case IP_MTU_DISCOVER
:
514 case IP_ROUTER_ALERT
:
521 case IP_MULTICAST_TTL
:
522 case IP_MULTICAST_ALL
:
523 case IP_MULTICAST_LOOP
:
524 case IP_RECVORIGDSTADDR
:
525 if (optlen
>= sizeof(int)) {
526 if (get_user(val
, (int __user
*) optval
))
528 } else if (optlen
>= sizeof(char)) {
531 if (get_user(ucval
, (unsigned char __user
*) optval
))
537 /* If optlen==0, it is equivalent to val == 0 */
539 if (ip_mroute_opt(optname
))
540 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
548 struct ip_options_rcu
*old
, *opt
= NULL
;
552 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
556 old
= rcu_dereference_protected(inet
->inet_opt
,
557 sock_owned_by_user(sk
));
559 struct inet_connection_sock
*icsk
= inet_csk(sk
);
560 #if IS_ENABLED(CONFIG_IPV6)
561 if (sk
->sk_family
== PF_INET
||
562 (!((1 << sk
->sk_state
) &
563 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
564 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
567 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
569 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
570 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
571 #if IS_ENABLED(CONFIG_IPV6)
575 rcu_assign_pointer(inet
->inet_opt
, opt
);
582 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
584 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
588 inet
->cmsg_flags
|= IP_CMSG_TTL
;
590 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
594 inet
->cmsg_flags
|= IP_CMSG_TOS
;
596 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
600 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
602 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
606 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
608 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
612 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
614 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
616 case IP_RECVORIGDSTADDR
:
618 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
620 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
622 case IP_TOS
: /* This sets both TOS and Precedence */
623 if (sk
->sk_type
== SOCK_STREAM
) {
624 val
&= ~INET_ECN_MASK
;
625 val
|= inet
->tos
& INET_ECN_MASK
;
627 if (inet
->tos
!= val
) {
629 sk
->sk_priority
= rt_tos2priority(val
);
636 if (val
!= -1 && (val
< 1 || val
> 255))
641 if (sk
->sk_type
!= SOCK_RAW
) {
645 inet
->hdrincl
= val
? 1 : 0;
648 if (sk
->sk_type
!= SOCK_RAW
) {
652 inet
->nodefrag
= val
? 1 : 0;
654 case IP_MTU_DISCOVER
:
655 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_OMIT
)
657 inet
->pmtudisc
= val
;
660 inet
->recverr
= !!val
;
662 skb_queue_purge(&sk
->sk_error_queue
);
664 case IP_MULTICAST_TTL
:
665 if (sk
->sk_type
== SOCK_STREAM
)
671 if (val
< 0 || val
> 255)
675 case IP_MULTICAST_LOOP
:
678 inet
->mc_loop
= !!val
;
682 struct net_device
*dev
= NULL
;
685 if (optlen
!= sizeof(int))
688 ifindex
= (__force
int)ntohl((__force __be32
)val
);
695 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
696 err
= -EADDRNOTAVAIL
;
702 if (sk
->sk_bound_dev_if
)
705 inet
->uc_index
= ifindex
;
709 case IP_MULTICAST_IF
:
711 struct ip_mreqn mreq
;
712 struct net_device
*dev
= NULL
;
714 if (sk
->sk_type
== SOCK_STREAM
)
717 * Check the arguments are allowable
720 if (optlen
< sizeof(struct in_addr
))
724 if (optlen
>= sizeof(struct ip_mreqn
)) {
725 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
728 memset(&mreq
, 0, sizeof(mreq
));
729 if (optlen
>= sizeof(struct ip_mreq
)) {
730 if (copy_from_user(&mreq
, optval
,
731 sizeof(struct ip_mreq
)))
733 } else if (optlen
>= sizeof(struct in_addr
)) {
734 if (copy_from_user(&mreq
.imr_address
, optval
,
735 sizeof(struct in_addr
)))
740 if (!mreq
.imr_ifindex
) {
741 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
747 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
749 mreq
.imr_ifindex
= dev
->ifindex
;
751 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
754 err
= -EADDRNOTAVAIL
;
760 if (sk
->sk_bound_dev_if
&&
761 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
764 inet
->mc_index
= mreq
.imr_ifindex
;
765 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
770 case IP_ADD_MEMBERSHIP
:
771 case IP_DROP_MEMBERSHIP
:
773 struct ip_mreqn mreq
;
776 if (inet_sk(sk
)->is_icsk
)
779 if (optlen
< sizeof(struct ip_mreq
))
782 if (optlen
>= sizeof(struct ip_mreqn
)) {
783 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
786 memset(&mreq
, 0, sizeof(mreq
));
787 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
791 if (optname
== IP_ADD_MEMBERSHIP
)
792 err
= ip_mc_join_group(sk
, &mreq
);
794 err
= ip_mc_leave_group(sk
, &mreq
);
799 struct ip_msfilter
*msf
;
801 if (optlen
< IP_MSFILTER_SIZE(0))
803 if (optlen
> sysctl_optmem_max
) {
807 msf
= kmalloc(optlen
, GFP_KERNEL
);
813 if (copy_from_user(msf
, optval
, optlen
)) {
817 /* numsrc >= (1G-4) overflow in 32 bits */
818 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
819 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
824 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
829 err
= ip_mc_msfilter(sk
, msf
, 0);
833 case IP_BLOCK_SOURCE
:
834 case IP_UNBLOCK_SOURCE
:
835 case IP_ADD_SOURCE_MEMBERSHIP
:
836 case IP_DROP_SOURCE_MEMBERSHIP
:
838 struct ip_mreq_source mreqs
;
841 if (optlen
!= sizeof(struct ip_mreq_source
))
843 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
847 if (optname
== IP_BLOCK_SOURCE
) {
848 omode
= MCAST_EXCLUDE
;
850 } else if (optname
== IP_UNBLOCK_SOURCE
) {
851 omode
= MCAST_EXCLUDE
;
853 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
854 struct ip_mreqn mreq
;
856 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
857 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
858 mreq
.imr_ifindex
= 0;
859 err
= ip_mc_join_group(sk
, &mreq
);
860 if (err
&& err
!= -EADDRINUSE
)
862 omode
= MCAST_INCLUDE
;
864 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
865 omode
= MCAST_INCLUDE
;
868 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
871 case MCAST_JOIN_GROUP
:
872 case MCAST_LEAVE_GROUP
:
874 struct group_req greq
;
875 struct sockaddr_in
*psin
;
876 struct ip_mreqn mreq
;
878 if (optlen
< sizeof(struct group_req
))
881 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
883 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
884 if (psin
->sin_family
!= AF_INET
)
886 memset(&mreq
, 0, sizeof(mreq
));
887 mreq
.imr_multiaddr
= psin
->sin_addr
;
888 mreq
.imr_ifindex
= greq
.gr_interface
;
890 if (optname
== MCAST_JOIN_GROUP
)
891 err
= ip_mc_join_group(sk
, &mreq
);
893 err
= ip_mc_leave_group(sk
, &mreq
);
896 case MCAST_JOIN_SOURCE_GROUP
:
897 case MCAST_LEAVE_SOURCE_GROUP
:
898 case MCAST_BLOCK_SOURCE
:
899 case MCAST_UNBLOCK_SOURCE
:
901 struct group_source_req greqs
;
902 struct ip_mreq_source mreqs
;
903 struct sockaddr_in
*psin
;
906 if (optlen
!= sizeof(struct group_source_req
))
908 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
912 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
913 greqs
.gsr_source
.ss_family
!= AF_INET
) {
914 err
= -EADDRNOTAVAIL
;
917 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
918 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
919 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
920 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
921 mreqs
.imr_interface
= 0; /* use index for mc_source */
923 if (optname
== MCAST_BLOCK_SOURCE
) {
924 omode
= MCAST_EXCLUDE
;
926 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
927 omode
= MCAST_EXCLUDE
;
929 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
930 struct ip_mreqn mreq
;
932 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
933 mreq
.imr_multiaddr
= psin
->sin_addr
;
934 mreq
.imr_address
.s_addr
= 0;
935 mreq
.imr_ifindex
= greqs
.gsr_interface
;
936 err
= ip_mc_join_group(sk
, &mreq
);
937 if (err
&& err
!= -EADDRINUSE
)
939 greqs
.gsr_interface
= mreq
.imr_ifindex
;
940 omode
= MCAST_INCLUDE
;
942 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
943 omode
= MCAST_INCLUDE
;
946 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
947 greqs
.gsr_interface
);
952 struct sockaddr_in
*psin
;
953 struct ip_msfilter
*msf
= NULL
;
954 struct group_filter
*gsf
= NULL
;
955 int msize
, i
, ifindex
;
957 if (optlen
< GROUP_FILTER_SIZE(0))
959 if (optlen
> sysctl_optmem_max
) {
963 gsf
= kmalloc(optlen
, GFP_KERNEL
);
969 if (copy_from_user(gsf
, optval
, optlen
))
972 /* numsrc >= (4G-140)/128 overflow in 32 bits */
973 if (gsf
->gf_numsrc
>= 0x1ffffff ||
974 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
978 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
982 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
983 msf
= kmalloc(msize
, GFP_KERNEL
);
988 ifindex
= gsf
->gf_interface
;
989 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
990 if (psin
->sin_family
!= AF_INET
) {
991 err
= -EADDRNOTAVAIL
;
994 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
995 msf
->imsf_interface
= 0;
996 msf
->imsf_fmode
= gsf
->gf_fmode
;
997 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
998 err
= -EADDRNOTAVAIL
;
999 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
1000 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
1002 if (psin
->sin_family
!= AF_INET
)
1004 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
1009 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
1015 case IP_MULTICAST_ALL
:
1018 if (val
!= 0 && val
!= 1)
1022 case IP_ROUTER_ALERT
:
1023 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
1029 inet
->freebind
= !!val
;
1032 case IP_IPSEC_POLICY
:
1033 case IP_XFRM_POLICY
:
1035 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1037 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
1040 case IP_TRANSPARENT
:
1041 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
1042 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1048 inet
->transparent
= !!val
;
1054 if (val
< 0 || val
> 255)
1056 inet
->min_ttl
= val
;
1072 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1076 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1077 * destination in skb->cb[] before dst drop.
1078 * This way, receiver doesn't make cache line misses to read rtable.
1080 void ipv4_pktinfo_prepare(const struct sock
*sk
, struct sk_buff
*skb
)
1082 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1083 bool prepare
= (inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
) ||
1086 if (prepare
&& skb_rtable(skb
)) {
1087 pktinfo
->ipi_ifindex
= inet_iif(skb
);
1088 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1090 pktinfo
->ipi_ifindex
= 0;
1091 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1096 int ip_setsockopt(struct sock
*sk
, int level
,
1097 int optname
, char __user
*optval
, unsigned int optlen
)
1101 if (level
!= SOL_IP
)
1102 return -ENOPROTOOPT
;
1104 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1105 #ifdef CONFIG_NETFILTER
1106 /* we need to exclude all possible ENOPROTOOPTs except default case */
1107 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1108 optname
!= IP_IPSEC_POLICY
&&
1109 optname
!= IP_XFRM_POLICY
&&
1110 !ip_mroute_opt(optname
)) {
1112 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1118 EXPORT_SYMBOL(ip_setsockopt
);
1120 #ifdef CONFIG_COMPAT
1121 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1122 char __user
*optval
, unsigned int optlen
)
1126 if (level
!= SOL_IP
)
1127 return -ENOPROTOOPT
;
1129 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1130 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1133 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1134 #ifdef CONFIG_NETFILTER
1135 /* we need to exclude all possible ENOPROTOOPTs except default case */
1136 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1137 optname
!= IP_IPSEC_POLICY
&&
1138 optname
!= IP_XFRM_POLICY
&&
1139 !ip_mroute_opt(optname
)) {
1141 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1148 EXPORT_SYMBOL(compat_ip_setsockopt
);
1152 * Get the options. Note for future reference. The GET of IP options gets
1153 * the _received_ ones. The set sets the _sent_ ones.
1156 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1157 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1159 struct inet_sock
*inet
= inet_sk(sk
);
1163 if (level
!= SOL_IP
)
1166 if (ip_mroute_opt(optname
))
1167 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1169 if (get_user(len
, optlen
))
1179 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1180 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1181 struct ip_options_rcu
*inet_opt
;
1183 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1184 sock_owned_by_user(sk
));
1187 memcpy(optbuf
, &inet_opt
->opt
,
1188 sizeof(struct ip_options
) +
1189 inet_opt
->opt
.optlen
);
1192 if (opt
->optlen
== 0)
1193 return put_user(0, optlen
);
1195 ip_options_undo(opt
);
1197 len
= min_t(unsigned int, len
, opt
->optlen
);
1198 if (put_user(len
, optlen
))
1200 if (copy_to_user(optval
, opt
->__data
, len
))
1205 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1208 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1211 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1214 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1217 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1220 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1222 case IP_RECVORIGDSTADDR
:
1223 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1229 val
= (inet
->uc_ttl
== -1 ?
1230 sysctl_ip_default_ttl
:
1234 val
= inet
->hdrincl
;
1237 val
= inet
->nodefrag
;
1239 case IP_MTU_DISCOVER
:
1240 val
= inet
->pmtudisc
;
1244 struct dst_entry
*dst
;
1246 dst
= sk_dst_get(sk
);
1258 val
= inet
->recverr
;
1260 case IP_MULTICAST_TTL
:
1263 case IP_MULTICAST_LOOP
:
1264 val
= inet
->mc_loop
;
1267 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1269 case IP_MULTICAST_IF
:
1271 struct in_addr addr
;
1272 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1273 addr
.s_addr
= inet
->mc_addr
;
1276 if (put_user(len
, optlen
))
1278 if (copy_to_user(optval
, &addr
, len
))
1284 struct ip_msfilter msf
;
1287 if (len
< IP_MSFILTER_SIZE(0)) {
1291 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1295 err
= ip_mc_msfget(sk
, &msf
,
1296 (struct ip_msfilter __user
*)optval
, optlen
);
1300 case MCAST_MSFILTER
:
1302 struct group_filter gsf
;
1305 if (len
< GROUP_FILTER_SIZE(0)) {
1309 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1313 err
= ip_mc_gsfget(sk
, &gsf
,
1314 (struct group_filter __user
*)optval
,
1319 case IP_MULTICAST_ALL
:
1328 if (sk
->sk_type
!= SOCK_STREAM
)
1329 return -ENOPROTOOPT
;
1331 msg
.msg_control
= (__force
void *) optval
;
1332 msg
.msg_controllen
= len
;
1333 msg
.msg_flags
= flags
;
1335 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1336 struct in_pktinfo info
;
1338 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1339 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1340 info
.ipi_ifindex
= inet
->mc_index
;
1341 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1343 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1344 int hlim
= inet
->mc_ttl
;
1345 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1347 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1348 int tos
= inet
->rcv_tos
;
1349 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1351 len
-= msg
.msg_controllen
;
1352 return put_user(len
, optlen
);
1355 val
= inet
->freebind
;
1357 case IP_TRANSPARENT
:
1358 val
= inet
->transparent
;
1361 val
= inet
->min_ttl
;
1365 return -ENOPROTOOPT
;
1369 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1370 unsigned char ucval
= (unsigned char)val
;
1372 if (put_user(len
, optlen
))
1374 if (copy_to_user(optval
, &ucval
, 1))
1377 len
= min_t(unsigned int, sizeof(int), len
);
1378 if (put_user(len
, optlen
))
1380 if (copy_to_user(optval
, &val
, len
))
1386 int ip_getsockopt(struct sock
*sk
, int level
,
1387 int optname
, char __user
*optval
, int __user
*optlen
)
1391 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1392 #ifdef CONFIG_NETFILTER
1393 /* we need to exclude all possible ENOPROTOOPTs except default case */
1394 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1395 !ip_mroute_opt(optname
)) {
1398 if (get_user(len
, optlen
))
1402 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1406 err
= put_user(len
, optlen
);
1412 EXPORT_SYMBOL(ip_getsockopt
);
1414 #ifdef CONFIG_COMPAT
1415 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1416 char __user
*optval
, int __user
*optlen
)
1420 if (optname
== MCAST_MSFILTER
)
1421 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1424 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1427 #ifdef CONFIG_NETFILTER
1428 /* we need to exclude all possible ENOPROTOOPTs except default case */
1429 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1430 !ip_mroute_opt(optname
)) {
1433 if (get_user(len
, optlen
))
1437 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1440 err
= put_user(len
, optlen
);
1446 EXPORT_SYMBOL(compat_ip_getsockopt
);