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)))
288 val
= *(int *)CMSG_DATA(cmsg
);
289 else if (cmsg
->cmsg_len
== CMSG_LEN(sizeof(u8
)))
290 val
= *(u8
*)CMSG_DATA(cmsg
);
293 if (val
< 0 || val
> 255)
296 ipc
->priority
= rt_tos2priority(ipc
->tos
);
307 /* Special input handler for packets caught by router alert option.
308 They are selected only by protocol field, and then processed likely
309 local ones; but only if someone wants them! Otherwise, router
310 not running rsvpd will kill RSVP.
312 It is user level problem, what it will make with them.
313 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
314 but receiver should be enough clever f.e. to forward mtrace requests,
315 sent to multicast group to reach destination designated router.
317 struct ip_ra_chain __rcu
*ip_ra_chain
;
318 static DEFINE_SPINLOCK(ip_ra_lock
);
321 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
323 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
325 sock_put(ra
->saved_sk
);
329 int ip_ra_control(struct sock
*sk
, unsigned char on
,
330 void (*destructor
)(struct sock
*))
332 struct ip_ra_chain
*ra
, *new_ra
;
333 struct ip_ra_chain __rcu
**rap
;
335 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
338 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
340 spin_lock_bh(&ip_ra_lock
);
341 for (rap
= &ip_ra_chain
;
342 (ra
= rcu_dereference_protected(*rap
,
343 lockdep_is_held(&ip_ra_lock
))) != NULL
;
347 spin_unlock_bh(&ip_ra_lock
);
351 /* dont let ip_call_ra_chain() use sk again */
353 RCU_INIT_POINTER(*rap
, ra
->next
);
354 spin_unlock_bh(&ip_ra_lock
);
359 * Delay sock_put(sk) and kfree(ra) after one rcu grace
360 * period. This guarantee ip_call_ra_chain() dont need
361 * to mess with socket refcounts.
364 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
369 spin_unlock_bh(&ip_ra_lock
);
373 new_ra
->destructor
= destructor
;
375 RCU_INIT_POINTER(new_ra
->next
, ra
);
376 rcu_assign_pointer(*rap
, new_ra
);
378 spin_unlock_bh(&ip_ra_lock
);
383 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
384 __be16 port
, u32 info
, u8
*payload
)
386 struct sock_exterr_skb
*serr
;
388 skb
= skb_clone(skb
, GFP_ATOMIC
);
392 serr
= SKB_EXT_ERR(skb
);
393 serr
->ee
.ee_errno
= err
;
394 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
395 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
396 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
398 serr
->ee
.ee_info
= info
;
399 serr
->ee
.ee_data
= 0;
400 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
401 skb_network_header(skb
);
404 if (skb_pull(skb
, payload
- skb
->data
)) {
405 skb_reset_transport_header(skb
);
406 if (sock_queue_err_skb(sk
, skb
) == 0)
412 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
414 struct inet_sock
*inet
= inet_sk(sk
);
415 struct sock_exterr_skb
*serr
;
422 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
426 skb_put(skb
, sizeof(struct iphdr
));
427 skb_reset_network_header(skb
);
431 serr
= SKB_EXT_ERR(skb
);
432 serr
->ee
.ee_errno
= err
;
433 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
434 serr
->ee
.ee_type
= 0;
435 serr
->ee
.ee_code
= 0;
437 serr
->ee
.ee_info
= info
;
438 serr
->ee
.ee_data
= 0;
439 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
442 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
443 skb_reset_transport_header(skb
);
445 if (sock_queue_err_skb(sk
, skb
))
449 /* For some errors we have valid addr_offset even with zero payload and
450 * zero port. Also, addr_offset should be supported if port is set.
452 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb
*serr
)
454 return serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
455 serr
->ee
.ee_origin
== SO_EE_ORIGIN_LOCAL
|| serr
->port
;
458 /* IPv4 supports cmsg on all imcp errors and some timestamps
460 * Timestamp code paths do not initialize the fields expected by cmsg:
461 * the PKTINFO fields in skb->cb[]. Fill those in here.
463 static bool ipv4_datagram_support_cmsg(const struct sock
*sk
,
467 struct in_pktinfo
*info
;
469 if (ee_origin
== SO_EE_ORIGIN_ICMP
)
472 if (ee_origin
== SO_EE_ORIGIN_LOCAL
)
475 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
476 * timestamp with egress dev. Not possible for packets without dev
477 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
479 if ((!(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_CMSG
)) ||
483 info
= PKTINFO_SKB_CB(skb
);
484 info
->ipi_spec_dst
.s_addr
= ip_hdr(skb
)->saddr
;
485 info
->ipi_ifindex
= skb
->dev
->ifindex
;
490 * Handle MSG_ERRQUEUE
492 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
494 struct sock_exterr_skb
*serr
;
496 DECLARE_SOCKADDR(struct sockaddr_in
*, sin
, msg
->msg_name
);
498 struct sock_extended_err ee
;
499 struct sockaddr_in offender
;
504 WARN_ON_ONCE(sk
->sk_family
== AF_INET6
);
507 skb
= sock_dequeue_err_skb(sk
);
513 msg
->msg_flags
|= MSG_TRUNC
;
516 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
521 sock_recv_timestamp(msg
, sk
, skb
);
523 serr
= SKB_EXT_ERR(skb
);
525 if (sin
&& ipv4_datagram_support_addr(serr
)) {
526 sin
->sin_family
= AF_INET
;
527 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
529 sin
->sin_port
= serr
->port
;
530 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
531 *addr_len
= sizeof(*sin
);
534 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
535 sin
= &errhdr
.offender
;
536 memset(sin
, 0, sizeof(*sin
));
538 if (ipv4_datagram_support_cmsg(sk
, skb
, serr
->ee
.ee_origin
)) {
539 sin
->sin_family
= AF_INET
;
540 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
541 if (inet_sk(sk
)->cmsg_flags
)
542 ip_cmsg_recv(msg
, skb
);
545 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
547 /* Now we could try to dump offended packet options */
549 msg
->msg_flags
|= MSG_ERRQUEUE
;
559 * Socket option code for IP. This is the end of the line after any
560 * TCP,UDP etc options on an IP socket.
562 static bool setsockopt_needs_rtnl(int optname
)
565 case IP_ADD_MEMBERSHIP
:
566 case IP_ADD_SOURCE_MEMBERSHIP
:
567 case IP_BLOCK_SOURCE
:
568 case IP_DROP_MEMBERSHIP
:
569 case IP_DROP_SOURCE_MEMBERSHIP
:
571 case IP_UNBLOCK_SOURCE
:
572 case MCAST_BLOCK_SOURCE
:
574 case MCAST_JOIN_GROUP
:
575 case MCAST_JOIN_SOURCE_GROUP
:
576 case MCAST_LEAVE_GROUP
:
577 case MCAST_LEAVE_SOURCE_GROUP
:
578 case MCAST_UNBLOCK_SOURCE
:
584 static int do_ip_setsockopt(struct sock
*sk
, int level
,
585 int optname
, char __user
*optval
, unsigned int optlen
)
587 struct inet_sock
*inet
= inet_sk(sk
);
588 struct net
*net
= sock_net(sk
);
590 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
601 case IP_MTU_DISCOVER
:
603 case IP_ROUTER_ALERT
:
609 case IP_BIND_ADDRESS_NO_PORT
:
611 case IP_MULTICAST_TTL
:
612 case IP_MULTICAST_ALL
:
613 case IP_MULTICAST_LOOP
:
614 case IP_RECVORIGDSTADDR
:
616 if (optlen
>= sizeof(int)) {
617 if (get_user(val
, (int __user
*) optval
))
619 } else if (optlen
>= sizeof(char)) {
622 if (get_user(ucval
, (unsigned char __user
*) optval
))
628 /* If optlen==0, it is equivalent to val == 0 */
630 if (ip_mroute_opt(optname
))
631 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
641 struct ip_options_rcu
*old
, *opt
= NULL
;
645 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
649 old
= rcu_dereference_protected(inet
->inet_opt
,
650 lockdep_sock_is_held(sk
));
652 struct inet_connection_sock
*icsk
= inet_csk(sk
);
653 #if IS_ENABLED(CONFIG_IPV6)
654 if (sk
->sk_family
== PF_INET
||
655 (!((1 << sk
->sk_state
) &
656 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
657 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
660 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
662 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
663 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
664 #if IS_ENABLED(CONFIG_IPV6)
668 rcu_assign_pointer(inet
->inet_opt
, opt
);
675 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
677 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
681 inet
->cmsg_flags
|= IP_CMSG_TTL
;
683 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
687 inet
->cmsg_flags
|= IP_CMSG_TOS
;
689 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
693 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
695 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
699 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
701 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
705 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
707 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
709 case IP_RECVORIGDSTADDR
:
711 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
713 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
717 if (!(inet
->cmsg_flags
& IP_CMSG_CHECKSUM
)) {
718 inet_inc_convert_csum(sk
);
719 inet
->cmsg_flags
|= IP_CMSG_CHECKSUM
;
722 if (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) {
723 inet_dec_convert_csum(sk
);
724 inet
->cmsg_flags
&= ~IP_CMSG_CHECKSUM
;
728 case IP_TOS
: /* This sets both TOS and Precedence */
729 if (sk
->sk_type
== SOCK_STREAM
) {
730 val
&= ~INET_ECN_MASK
;
731 val
|= inet
->tos
& INET_ECN_MASK
;
733 if (inet
->tos
!= val
) {
735 sk
->sk_priority
= rt_tos2priority(val
);
742 if (val
!= -1 && (val
< 1 || val
> 255))
747 if (sk
->sk_type
!= SOCK_RAW
) {
751 inet
->hdrincl
= val
? 1 : 0;
754 if (sk
->sk_type
!= SOCK_RAW
) {
758 inet
->nodefrag
= val
? 1 : 0;
760 case IP_BIND_ADDRESS_NO_PORT
:
761 inet
->bind_address_no_port
= val
? 1 : 0;
763 case IP_MTU_DISCOVER
:
764 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_OMIT
)
766 inet
->pmtudisc
= val
;
769 inet
->recverr
= !!val
;
771 skb_queue_purge(&sk
->sk_error_queue
);
773 case IP_MULTICAST_TTL
:
774 if (sk
->sk_type
== SOCK_STREAM
)
780 if (val
< 0 || val
> 255)
784 case IP_MULTICAST_LOOP
:
787 inet
->mc_loop
= !!val
;
791 struct net_device
*dev
= NULL
;
794 if (optlen
!= sizeof(int))
797 ifindex
= (__force
int)ntohl((__force __be32
)val
);
804 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
805 err
= -EADDRNOTAVAIL
;
811 if (sk
->sk_bound_dev_if
)
814 inet
->uc_index
= ifindex
;
818 case IP_MULTICAST_IF
:
820 struct ip_mreqn mreq
;
821 struct net_device
*dev
= NULL
;
823 if (sk
->sk_type
== SOCK_STREAM
)
826 * Check the arguments are allowable
829 if (optlen
< sizeof(struct in_addr
))
833 if (optlen
>= sizeof(struct ip_mreqn
)) {
834 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
837 memset(&mreq
, 0, sizeof(mreq
));
838 if (optlen
>= sizeof(struct ip_mreq
)) {
839 if (copy_from_user(&mreq
, optval
,
840 sizeof(struct ip_mreq
)))
842 } else if (optlen
>= sizeof(struct in_addr
)) {
843 if (copy_from_user(&mreq
.imr_address
, optval
,
844 sizeof(struct in_addr
)))
849 if (!mreq
.imr_ifindex
) {
850 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
856 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
858 mreq
.imr_ifindex
= dev
->ifindex
;
860 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
863 err
= -EADDRNOTAVAIL
;
869 if (sk
->sk_bound_dev_if
&&
870 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
873 inet
->mc_index
= mreq
.imr_ifindex
;
874 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
879 case IP_ADD_MEMBERSHIP
:
880 case IP_DROP_MEMBERSHIP
:
882 struct ip_mreqn mreq
;
885 if (inet_sk(sk
)->is_icsk
)
888 if (optlen
< sizeof(struct ip_mreq
))
891 if (optlen
>= sizeof(struct ip_mreqn
)) {
892 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
895 memset(&mreq
, 0, sizeof(mreq
));
896 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
900 if (optname
== IP_ADD_MEMBERSHIP
)
901 err
= ip_mc_join_group(sk
, &mreq
);
903 err
= ip_mc_leave_group(sk
, &mreq
);
908 struct ip_msfilter
*msf
;
910 if (optlen
< IP_MSFILTER_SIZE(0))
912 if (optlen
> sysctl_optmem_max
) {
916 msf
= kmalloc(optlen
, GFP_KERNEL
);
922 if (copy_from_user(msf
, optval
, optlen
)) {
926 /* numsrc >= (1G-4) overflow in 32 bits */
927 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
928 msf
->imsf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
933 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
938 err
= ip_mc_msfilter(sk
, msf
, 0);
942 case IP_BLOCK_SOURCE
:
943 case IP_UNBLOCK_SOURCE
:
944 case IP_ADD_SOURCE_MEMBERSHIP
:
945 case IP_DROP_SOURCE_MEMBERSHIP
:
947 struct ip_mreq_source mreqs
;
950 if (optlen
!= sizeof(struct ip_mreq_source
))
952 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
956 if (optname
== IP_BLOCK_SOURCE
) {
957 omode
= MCAST_EXCLUDE
;
959 } else if (optname
== IP_UNBLOCK_SOURCE
) {
960 omode
= MCAST_EXCLUDE
;
962 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
963 struct ip_mreqn mreq
;
965 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
966 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
967 mreq
.imr_ifindex
= 0;
968 err
= ip_mc_join_group(sk
, &mreq
);
969 if (err
&& err
!= -EADDRINUSE
)
971 omode
= MCAST_INCLUDE
;
973 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
974 omode
= MCAST_INCLUDE
;
977 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
980 case MCAST_JOIN_GROUP
:
981 case MCAST_LEAVE_GROUP
:
983 struct group_req greq
;
984 struct sockaddr_in
*psin
;
985 struct ip_mreqn mreq
;
987 if (optlen
< sizeof(struct group_req
))
990 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
992 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
993 if (psin
->sin_family
!= AF_INET
)
995 memset(&mreq
, 0, sizeof(mreq
));
996 mreq
.imr_multiaddr
= psin
->sin_addr
;
997 mreq
.imr_ifindex
= greq
.gr_interface
;
999 if (optname
== MCAST_JOIN_GROUP
)
1000 err
= ip_mc_join_group(sk
, &mreq
);
1002 err
= ip_mc_leave_group(sk
, &mreq
);
1005 case MCAST_JOIN_SOURCE_GROUP
:
1006 case MCAST_LEAVE_SOURCE_GROUP
:
1007 case MCAST_BLOCK_SOURCE
:
1008 case MCAST_UNBLOCK_SOURCE
:
1010 struct group_source_req greqs
;
1011 struct ip_mreq_source mreqs
;
1012 struct sockaddr_in
*psin
;
1015 if (optlen
!= sizeof(struct group_source_req
))
1017 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
1021 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
1022 greqs
.gsr_source
.ss_family
!= AF_INET
) {
1023 err
= -EADDRNOTAVAIL
;
1026 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1027 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
1028 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
1029 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
1030 mreqs
.imr_interface
= 0; /* use index for mc_source */
1032 if (optname
== MCAST_BLOCK_SOURCE
) {
1033 omode
= MCAST_EXCLUDE
;
1035 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
1036 omode
= MCAST_EXCLUDE
;
1038 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
1039 struct ip_mreqn mreq
;
1041 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
1042 mreq
.imr_multiaddr
= psin
->sin_addr
;
1043 mreq
.imr_address
.s_addr
= 0;
1044 mreq
.imr_ifindex
= greqs
.gsr_interface
;
1045 err
= ip_mc_join_group(sk
, &mreq
);
1046 if (err
&& err
!= -EADDRINUSE
)
1048 greqs
.gsr_interface
= mreq
.imr_ifindex
;
1049 omode
= MCAST_INCLUDE
;
1051 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1052 omode
= MCAST_INCLUDE
;
1055 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
1056 greqs
.gsr_interface
);
1059 case MCAST_MSFILTER
:
1061 struct sockaddr_in
*psin
;
1062 struct ip_msfilter
*msf
= NULL
;
1063 struct group_filter
*gsf
= NULL
;
1064 int msize
, i
, ifindex
;
1066 if (optlen
< GROUP_FILTER_SIZE(0))
1068 if (optlen
> sysctl_optmem_max
) {
1072 gsf
= kmalloc(optlen
, GFP_KERNEL
);
1078 if (copy_from_user(gsf
, optval
, optlen
))
1081 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1082 if (gsf
->gf_numsrc
>= 0x1ffffff ||
1083 gsf
->gf_numsrc
> net
->ipv4
.sysctl_igmp_max_msf
) {
1087 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
1091 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
1092 msf
= kmalloc(msize
, GFP_KERNEL
);
1097 ifindex
= gsf
->gf_interface
;
1098 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
1099 if (psin
->sin_family
!= AF_INET
) {
1100 err
= -EADDRNOTAVAIL
;
1103 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
1104 msf
->imsf_interface
= 0;
1105 msf
->imsf_fmode
= gsf
->gf_fmode
;
1106 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
1107 err
= -EADDRNOTAVAIL
;
1108 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
1109 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
1111 if (psin
->sin_family
!= AF_INET
)
1113 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
1118 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
1124 case IP_MULTICAST_ALL
:
1127 if (val
!= 0 && val
!= 1)
1131 case IP_ROUTER_ALERT
:
1132 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
1138 inet
->freebind
= !!val
;
1141 case IP_IPSEC_POLICY
:
1142 case IP_XFRM_POLICY
:
1144 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1146 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
1149 case IP_TRANSPARENT
:
1150 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
1151 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1157 inet
->transparent
= !!val
;
1163 if (val
< 0 || val
> 255)
1165 inet
->min_ttl
= val
;
1185 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1189 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1190 * destination in skb->cb[] before dst drop.
1191 * This way, receiver doesn't make cache line misses to read rtable.
1193 void ipv4_pktinfo_prepare(const struct sock
*sk
, struct sk_buff
*skb
)
1195 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1196 bool prepare
= (inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
) ||
1199 if (prepare
&& skb_rtable(skb
)) {
1200 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1201 * which has interface index (iif) as the first member of the
1202 * underlying inet{6}_skb_parm struct. This code then overlays
1203 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1204 * element so the iif is picked up from the prior IPCB
1206 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1208 pktinfo
->ipi_ifindex
= 0;
1209 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1214 int ip_setsockopt(struct sock
*sk
, int level
,
1215 int optname
, char __user
*optval
, unsigned int optlen
)
1219 if (level
!= SOL_IP
)
1220 return -ENOPROTOOPT
;
1222 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1223 #ifdef CONFIG_NETFILTER
1224 /* we need to exclude all possible ENOPROTOOPTs except default case */
1225 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1226 optname
!= IP_IPSEC_POLICY
&&
1227 optname
!= IP_XFRM_POLICY
&&
1228 !ip_mroute_opt(optname
)) {
1230 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1236 EXPORT_SYMBOL(ip_setsockopt
);
1238 #ifdef CONFIG_COMPAT
1239 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1240 char __user
*optval
, unsigned int optlen
)
1244 if (level
!= SOL_IP
)
1245 return -ENOPROTOOPT
;
1247 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1248 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1251 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1252 #ifdef CONFIG_NETFILTER
1253 /* we need to exclude all possible ENOPROTOOPTs except default case */
1254 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1255 optname
!= IP_IPSEC_POLICY
&&
1256 optname
!= IP_XFRM_POLICY
&&
1257 !ip_mroute_opt(optname
)) {
1259 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1266 EXPORT_SYMBOL(compat_ip_setsockopt
);
1270 * Get the options. Note for future reference. The GET of IP options gets
1271 * the _received_ ones. The set sets the _sent_ ones.
1274 static bool getsockopt_needs_rtnl(int optname
)
1278 case MCAST_MSFILTER
:
1284 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1285 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1287 struct inet_sock
*inet
= inet_sk(sk
);
1288 bool needs_rtnl
= getsockopt_needs_rtnl(optname
);
1292 if (level
!= SOL_IP
)
1295 if (ip_mroute_opt(optname
))
1296 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1298 if (get_user(len
, optlen
))
1310 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1311 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1312 struct ip_options_rcu
*inet_opt
;
1314 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1315 lockdep_sock_is_held(sk
));
1318 memcpy(optbuf
, &inet_opt
->opt
,
1319 sizeof(struct ip_options
) +
1320 inet_opt
->opt
.optlen
);
1323 if (opt
->optlen
== 0)
1324 return put_user(0, optlen
);
1326 ip_options_undo(opt
);
1328 len
= min_t(unsigned int, len
, opt
->optlen
);
1329 if (put_user(len
, optlen
))
1331 if (copy_to_user(optval
, opt
->__data
, len
))
1336 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1339 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1342 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1345 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1348 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1351 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1353 case IP_RECVORIGDSTADDR
:
1354 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1357 val
= (inet
->cmsg_flags
& IP_CMSG_CHECKSUM
) != 0;
1364 struct net
*net
= sock_net(sk
);
1365 val
= (inet
->uc_ttl
== -1 ?
1366 net
->ipv4
.sysctl_ip_default_ttl
:
1371 val
= inet
->hdrincl
;
1374 val
= inet
->nodefrag
;
1376 case IP_BIND_ADDRESS_NO_PORT
:
1377 val
= inet
->bind_address_no_port
;
1379 case IP_MTU_DISCOVER
:
1380 val
= inet
->pmtudisc
;
1384 struct dst_entry
*dst
;
1386 dst
= sk_dst_get(sk
);
1398 val
= inet
->recverr
;
1400 case IP_MULTICAST_TTL
:
1403 case IP_MULTICAST_LOOP
:
1404 val
= inet
->mc_loop
;
1407 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1409 case IP_MULTICAST_IF
:
1411 struct in_addr addr
;
1412 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1413 addr
.s_addr
= inet
->mc_addr
;
1416 if (put_user(len
, optlen
))
1418 if (copy_to_user(optval
, &addr
, len
))
1424 struct ip_msfilter msf
;
1426 if (len
< IP_MSFILTER_SIZE(0)) {
1430 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1434 err
= ip_mc_msfget(sk
, &msf
,
1435 (struct ip_msfilter __user
*)optval
, optlen
);
1438 case MCAST_MSFILTER
:
1440 struct group_filter gsf
;
1442 if (len
< GROUP_FILTER_SIZE(0)) {
1446 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1450 err
= ip_mc_gsfget(sk
, &gsf
,
1451 (struct group_filter __user
*)optval
,
1455 case IP_MULTICAST_ALL
:
1464 if (sk
->sk_type
!= SOCK_STREAM
)
1465 return -ENOPROTOOPT
;
1467 msg
.msg_control
= (__force
void *) optval
;
1468 msg
.msg_controllen
= len
;
1469 msg
.msg_flags
= flags
;
1471 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1472 struct in_pktinfo info
;
1474 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1475 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1476 info
.ipi_ifindex
= inet
->mc_index
;
1477 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1479 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1480 int hlim
= inet
->mc_ttl
;
1481 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1483 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1484 int tos
= inet
->rcv_tos
;
1485 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1487 len
-= msg
.msg_controllen
;
1488 return put_user(len
, optlen
);
1491 val
= inet
->freebind
;
1493 case IP_TRANSPARENT
:
1494 val
= inet
->transparent
;
1497 val
= inet
->min_ttl
;
1501 return -ENOPROTOOPT
;
1505 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1506 unsigned char ucval
= (unsigned char)val
;
1508 if (put_user(len
, optlen
))
1510 if (copy_to_user(optval
, &ucval
, 1))
1513 len
= min_t(unsigned int, sizeof(int), len
);
1514 if (put_user(len
, optlen
))
1516 if (copy_to_user(optval
, &val
, len
))
1528 int ip_getsockopt(struct sock
*sk
, int level
,
1529 int optname
, char __user
*optval
, int __user
*optlen
)
1533 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1534 #ifdef CONFIG_NETFILTER
1535 /* we need to exclude all possible ENOPROTOOPTs except default case */
1536 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1537 !ip_mroute_opt(optname
)) {
1540 if (get_user(len
, optlen
))
1544 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1548 err
= put_user(len
, optlen
);
1554 EXPORT_SYMBOL(ip_getsockopt
);
1556 #ifdef CONFIG_COMPAT
1557 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1558 char __user
*optval
, int __user
*optlen
)
1562 if (optname
== MCAST_MSFILTER
)
1563 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1566 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1569 #ifdef CONFIG_NETFILTER
1570 /* we need to exclude all possible ENOPROTOOPTs except default case */
1571 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1572 !ip_mroute_opt(optname
)) {
1575 if (get_user(len
, optlen
))
1579 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1582 err
= put_user(len
, optlen
);
1588 EXPORT_SYMBOL(compat_ip_getsockopt
);