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.
59 #define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb))
61 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
63 struct in_pktinfo info
= *PKTINFO_SKB_CB(skb
);
65 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
67 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
70 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
72 int ttl
= ip_hdr(skb
)->ttl
;
73 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
76 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
78 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
81 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
83 if (IPCB(skb
)->opt
.optlen
== 0)
86 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
91 static void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
93 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
94 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
96 if (IPCB(skb
)->opt
.optlen
== 0)
99 if (ip_options_echo(opt
, skb
)) {
100 msg
->msg_flags
|= MSG_CTRUNC
;
103 ip_options_undo(opt
);
105 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
108 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
114 err
= security_socket_getpeersec_dgram(NULL
, skb
, &secid
);
118 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
122 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
123 security_release_secctx(secdata
, seclen
);
126 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
128 struct sockaddr_in sin
;
129 const struct iphdr
*iph
= ip_hdr(skb
);
130 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
132 if (skb_transport_offset(skb
) + 4 > skb
->len
)
135 /* All current transport protocols have the port numbers in the
136 * first four bytes of the transport header and this function is
137 * written with this assumption in mind.
140 sin
.sin_family
= AF_INET
;
141 sin
.sin_addr
.s_addr
= iph
->daddr
;
142 sin
.sin_port
= ports
[1];
143 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
145 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
148 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
150 struct inet_sock
*inet
= inet_sk(skb
->sk
);
151 unsigned int flags
= inet
->cmsg_flags
;
153 /* Ordered by supposed usage frequency */
155 ip_cmsg_recv_pktinfo(msg
, skb
);
156 if ((flags
>>= 1) == 0)
160 ip_cmsg_recv_ttl(msg
, skb
);
161 if ((flags
>>= 1) == 0)
165 ip_cmsg_recv_tos(msg
, skb
);
166 if ((flags
>>= 1) == 0)
170 ip_cmsg_recv_opts(msg
, skb
);
171 if ((flags
>>= 1) == 0)
175 ip_cmsg_recv_retopts(msg
, skb
);
176 if ((flags
>>= 1) == 0)
180 ip_cmsg_recv_security(msg
, skb
);
182 if ((flags
>>= 1) == 0)
185 ip_cmsg_recv_dstaddr(msg
, skb
);
188 EXPORT_SYMBOL(ip_cmsg_recv
);
190 int ip_cmsg_send(struct net
*net
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
193 struct cmsghdr
*cmsg
;
195 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
196 if (!CMSG_OK(msg
, cmsg
))
198 if (cmsg
->cmsg_level
!= SOL_IP
)
200 switch (cmsg
->cmsg_type
) {
202 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
204 /* Our caller is responsible for freeing ipc->opt */
205 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
206 err
< 40 ? err
: 40);
212 struct in_pktinfo
*info
;
213 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
215 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
216 ipc
->oif
= info
->ipi_ifindex
;
217 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
228 /* Special input handler for packets caught by router alert option.
229 They are selected only by protocol field, and then processed likely
230 local ones; but only if someone wants them! Otherwise, router
231 not running rsvpd will kill RSVP.
233 It is user level problem, what it will make with them.
234 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
235 but receiver should be enough clever f.e. to forward mtrace requests,
236 sent to multicast group to reach destination designated router.
238 struct ip_ra_chain __rcu
*ip_ra_chain
;
239 static DEFINE_SPINLOCK(ip_ra_lock
);
242 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
244 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
246 sock_put(ra
->saved_sk
);
250 int ip_ra_control(struct sock
*sk
, unsigned char on
,
251 void (*destructor
)(struct sock
*))
253 struct ip_ra_chain
*ra
, *new_ra
;
254 struct ip_ra_chain __rcu
**rap
;
256 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
259 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
261 spin_lock_bh(&ip_ra_lock
);
262 for (rap
= &ip_ra_chain
;
263 (ra
= rcu_dereference_protected(*rap
,
264 lockdep_is_held(&ip_ra_lock
))) != NULL
;
268 spin_unlock_bh(&ip_ra_lock
);
272 /* dont let ip_call_ra_chain() use sk again */
274 rcu_assign_pointer(*rap
, ra
->next
);
275 spin_unlock_bh(&ip_ra_lock
);
280 * Delay sock_put(sk) and kfree(ra) after one rcu grace
281 * period. This guarantee ip_call_ra_chain() dont need
282 * to mess with socket refcounts.
285 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
289 if (new_ra
== NULL
) {
290 spin_unlock_bh(&ip_ra_lock
);
294 new_ra
->destructor
= destructor
;
297 rcu_assign_pointer(*rap
, new_ra
);
299 spin_unlock_bh(&ip_ra_lock
);
304 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
305 __be16 port
, u32 info
, u8
*payload
)
307 struct sock_exterr_skb
*serr
;
309 skb
= skb_clone(skb
, GFP_ATOMIC
);
313 serr
= SKB_EXT_ERR(skb
);
314 serr
->ee
.ee_errno
= err
;
315 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
316 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
317 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
319 serr
->ee
.ee_info
= info
;
320 serr
->ee
.ee_data
= 0;
321 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
322 skb_network_header(skb
);
325 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
326 skb_reset_transport_header(skb
);
327 if (sock_queue_err_skb(sk
, skb
) == 0)
333 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
335 struct inet_sock
*inet
= inet_sk(sk
);
336 struct sock_exterr_skb
*serr
;
343 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
347 skb_put(skb
, sizeof(struct iphdr
));
348 skb_reset_network_header(skb
);
352 serr
= SKB_EXT_ERR(skb
);
353 serr
->ee
.ee_errno
= err
;
354 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
355 serr
->ee
.ee_type
= 0;
356 serr
->ee
.ee_code
= 0;
358 serr
->ee
.ee_info
= info
;
359 serr
->ee
.ee_data
= 0;
360 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
363 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
364 skb_reset_transport_header(skb
);
366 if (sock_queue_err_skb(sk
, skb
))
371 * Handle MSG_ERRQUEUE
373 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
, int *addr_len
)
375 struct sock_exterr_skb
*serr
;
376 struct sk_buff
*skb
, *skb2
;
377 struct sockaddr_in
*sin
;
379 struct sock_extended_err ee
;
380 struct sockaddr_in offender
;
386 skb
= skb_dequeue(&sk
->sk_error_queue
);
392 msg
->msg_flags
|= MSG_TRUNC
;
395 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
399 sock_recv_timestamp(msg
, sk
, skb
);
401 serr
= SKB_EXT_ERR(skb
);
403 sin
= (struct sockaddr_in
*)msg
->msg_name
;
405 sin
->sin_family
= AF_INET
;
406 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
408 sin
->sin_port
= serr
->port
;
409 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
410 *addr_len
= sizeof(*sin
);
413 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
414 sin
= &errhdr
.offender
;
415 memset(sin
, 0, sizeof(*sin
));
416 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
417 sin
->sin_family
= AF_INET
;
418 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
419 if (inet_sk(sk
)->cmsg_flags
)
420 ip_cmsg_recv(msg
, skb
);
423 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
425 /* Now we could try to dump offended packet options */
427 msg
->msg_flags
|= MSG_ERRQUEUE
;
430 /* Reset and regenerate socket error */
431 spin_lock_bh(&sk
->sk_error_queue
.lock
);
433 skb2
= skb_peek(&sk
->sk_error_queue
);
435 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
436 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
437 sk
->sk_error_report(sk
);
439 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
449 * Socket option code for IP. This is the end of the line after any
450 * TCP,UDP etc options on an IP socket.
453 static int do_ip_setsockopt(struct sock
*sk
, int level
,
454 int optname
, char __user
*optval
, unsigned int optlen
)
456 struct inet_sock
*inet
= inet_sk(sk
);
468 case IP_MTU_DISCOVER
:
470 case IP_ROUTER_ALERT
:
477 case IP_MULTICAST_TTL
:
478 case IP_MULTICAST_ALL
:
479 case IP_MULTICAST_LOOP
:
480 case IP_RECVORIGDSTADDR
:
481 if (optlen
>= sizeof(int)) {
482 if (get_user(val
, (int __user
*) optval
))
484 } else if (optlen
>= sizeof(char)) {
487 if (get_user(ucval
, (unsigned char __user
*) optval
))
493 /* If optlen==0, it is equivalent to val == 0 */
495 if (ip_mroute_opt(optname
))
496 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
504 struct ip_options_rcu
*old
, *opt
= NULL
;
508 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
512 old
= rcu_dereference_protected(inet
->inet_opt
,
513 sock_owned_by_user(sk
));
515 struct inet_connection_sock
*icsk
= inet_csk(sk
);
516 #if IS_ENABLED(CONFIG_IPV6)
517 if (sk
->sk_family
== PF_INET
||
518 (!((1 << sk
->sk_state
) &
519 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
520 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
523 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
525 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
526 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
527 #if IS_ENABLED(CONFIG_IPV6)
531 rcu_assign_pointer(inet
->inet_opt
, opt
);
538 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
540 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
544 inet
->cmsg_flags
|= IP_CMSG_TTL
;
546 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
550 inet
->cmsg_flags
|= IP_CMSG_TOS
;
552 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
556 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
558 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
562 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
564 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
568 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
570 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
572 case IP_RECVORIGDSTADDR
:
574 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
576 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
578 case IP_TOS
: /* This sets both TOS and Precedence */
579 if (sk
->sk_type
== SOCK_STREAM
) {
580 val
&= ~INET_ECN_MASK
;
581 val
|= inet
->tos
& INET_ECN_MASK
;
583 if (inet
->tos
!= val
) {
585 sk
->sk_priority
= rt_tos2priority(val
);
592 if (val
!= -1 && (val
< 1 || val
> 255))
597 if (sk
->sk_type
!= SOCK_RAW
) {
601 inet
->hdrincl
= val
? 1 : 0;
604 if (sk
->sk_type
!= SOCK_RAW
) {
608 inet
->nodefrag
= val
? 1 : 0;
610 case IP_MTU_DISCOVER
:
611 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
613 inet
->pmtudisc
= val
;
616 inet
->recverr
= !!val
;
618 skb_queue_purge(&sk
->sk_error_queue
);
620 case IP_MULTICAST_TTL
:
621 if (sk
->sk_type
== SOCK_STREAM
)
627 if (val
< 0 || val
> 255)
631 case IP_MULTICAST_LOOP
:
634 inet
->mc_loop
= !!val
;
638 struct net_device
*dev
= NULL
;
641 if (optlen
!= sizeof(int))
644 ifindex
= (__force
int)ntohl((__force __be32
)val
);
651 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
652 err
= -EADDRNOTAVAIL
;
658 if (sk
->sk_bound_dev_if
)
661 inet
->uc_index
= ifindex
;
665 case IP_MULTICAST_IF
:
667 struct ip_mreqn mreq
;
668 struct net_device
*dev
= NULL
;
670 if (sk
->sk_type
== SOCK_STREAM
)
673 * Check the arguments are allowable
676 if (optlen
< sizeof(struct in_addr
))
680 if (optlen
>= sizeof(struct ip_mreqn
)) {
681 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
684 memset(&mreq
, 0, sizeof(mreq
));
685 if (optlen
>= sizeof(struct ip_mreq
)) {
686 if (copy_from_user(&mreq
, optval
,
687 sizeof(struct ip_mreq
)))
689 } else if (optlen
>= sizeof(struct in_addr
)) {
690 if (copy_from_user(&mreq
.imr_address
, optval
,
691 sizeof(struct in_addr
)))
696 if (!mreq
.imr_ifindex
) {
697 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
703 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
705 mreq
.imr_ifindex
= dev
->ifindex
;
707 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
710 err
= -EADDRNOTAVAIL
;
716 if (sk
->sk_bound_dev_if
&&
717 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
720 inet
->mc_index
= mreq
.imr_ifindex
;
721 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
726 case IP_ADD_MEMBERSHIP
:
727 case IP_DROP_MEMBERSHIP
:
729 struct ip_mreqn mreq
;
732 if (inet_sk(sk
)->is_icsk
)
735 if (optlen
< sizeof(struct ip_mreq
))
738 if (optlen
>= sizeof(struct ip_mreqn
)) {
739 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
742 memset(&mreq
, 0, sizeof(mreq
));
743 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
747 if (optname
== IP_ADD_MEMBERSHIP
)
748 err
= ip_mc_join_group(sk
, &mreq
);
750 err
= ip_mc_leave_group(sk
, &mreq
);
755 struct ip_msfilter
*msf
;
757 if (optlen
< IP_MSFILTER_SIZE(0))
759 if (optlen
> sysctl_optmem_max
) {
763 msf
= kmalloc(optlen
, GFP_KERNEL
);
769 if (copy_from_user(msf
, optval
, optlen
)) {
773 /* numsrc >= (1G-4) overflow in 32 bits */
774 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
775 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
780 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
785 err
= ip_mc_msfilter(sk
, msf
, 0);
789 case IP_BLOCK_SOURCE
:
790 case IP_UNBLOCK_SOURCE
:
791 case IP_ADD_SOURCE_MEMBERSHIP
:
792 case IP_DROP_SOURCE_MEMBERSHIP
:
794 struct ip_mreq_source mreqs
;
797 if (optlen
!= sizeof(struct ip_mreq_source
))
799 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
803 if (optname
== IP_BLOCK_SOURCE
) {
804 omode
= MCAST_EXCLUDE
;
806 } else if (optname
== IP_UNBLOCK_SOURCE
) {
807 omode
= MCAST_EXCLUDE
;
809 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
810 struct ip_mreqn mreq
;
812 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
813 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
814 mreq
.imr_ifindex
= 0;
815 err
= ip_mc_join_group(sk
, &mreq
);
816 if (err
&& err
!= -EADDRINUSE
)
818 omode
= MCAST_INCLUDE
;
820 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
821 omode
= MCAST_INCLUDE
;
824 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
827 case MCAST_JOIN_GROUP
:
828 case MCAST_LEAVE_GROUP
:
830 struct group_req greq
;
831 struct sockaddr_in
*psin
;
832 struct ip_mreqn mreq
;
834 if (optlen
< sizeof(struct group_req
))
837 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
839 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
840 if (psin
->sin_family
!= AF_INET
)
842 memset(&mreq
, 0, sizeof(mreq
));
843 mreq
.imr_multiaddr
= psin
->sin_addr
;
844 mreq
.imr_ifindex
= greq
.gr_interface
;
846 if (optname
== MCAST_JOIN_GROUP
)
847 err
= ip_mc_join_group(sk
, &mreq
);
849 err
= ip_mc_leave_group(sk
, &mreq
);
852 case MCAST_JOIN_SOURCE_GROUP
:
853 case MCAST_LEAVE_SOURCE_GROUP
:
854 case MCAST_BLOCK_SOURCE
:
855 case MCAST_UNBLOCK_SOURCE
:
857 struct group_source_req greqs
;
858 struct ip_mreq_source mreqs
;
859 struct sockaddr_in
*psin
;
862 if (optlen
!= sizeof(struct group_source_req
))
864 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
868 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
869 greqs
.gsr_source
.ss_family
!= AF_INET
) {
870 err
= -EADDRNOTAVAIL
;
873 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
874 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
875 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
876 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
877 mreqs
.imr_interface
= 0; /* use index for mc_source */
879 if (optname
== MCAST_BLOCK_SOURCE
) {
880 omode
= MCAST_EXCLUDE
;
882 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
883 omode
= MCAST_EXCLUDE
;
885 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
886 struct ip_mreqn mreq
;
888 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
889 mreq
.imr_multiaddr
= psin
->sin_addr
;
890 mreq
.imr_address
.s_addr
= 0;
891 mreq
.imr_ifindex
= greqs
.gsr_interface
;
892 err
= ip_mc_join_group(sk
, &mreq
);
893 if (err
&& err
!= -EADDRINUSE
)
895 greqs
.gsr_interface
= mreq
.imr_ifindex
;
896 omode
= MCAST_INCLUDE
;
898 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
899 omode
= MCAST_INCLUDE
;
902 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
903 greqs
.gsr_interface
);
908 struct sockaddr_in
*psin
;
909 struct ip_msfilter
*msf
= NULL
;
910 struct group_filter
*gsf
= NULL
;
911 int msize
, i
, ifindex
;
913 if (optlen
< GROUP_FILTER_SIZE(0))
915 if (optlen
> sysctl_optmem_max
) {
919 gsf
= kmalloc(optlen
, GFP_KERNEL
);
925 if (copy_from_user(gsf
, optval
, optlen
))
928 /* numsrc >= (4G-140)/128 overflow in 32 bits */
929 if (gsf
->gf_numsrc
>= 0x1ffffff ||
930 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
934 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
938 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
939 msf
= kmalloc(msize
, GFP_KERNEL
);
944 ifindex
= gsf
->gf_interface
;
945 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
946 if (psin
->sin_family
!= AF_INET
) {
947 err
= -EADDRNOTAVAIL
;
950 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
951 msf
->imsf_interface
= 0;
952 msf
->imsf_fmode
= gsf
->gf_fmode
;
953 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
954 err
= -EADDRNOTAVAIL
;
955 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
956 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
958 if (psin
->sin_family
!= AF_INET
)
960 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
965 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
971 case IP_MULTICAST_ALL
:
974 if (val
!= 0 && val
!= 1)
978 case IP_ROUTER_ALERT
:
979 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
985 inet
->freebind
= !!val
;
988 case IP_IPSEC_POLICY
:
991 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
993 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
997 if (!!val
&& !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_RAW
) &&
998 !ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
)) {
1004 inet
->transparent
= !!val
;
1010 if (val
< 0 || val
> 255)
1012 inet
->min_ttl
= val
;
1028 * ipv4_pktinfo_prepare - transfert some info from rtable to skb
1032 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1033 * destination in skb->cb[] before dst drop.
1034 * This way, receiver doesnt make cache line misses to read rtable.
1036 void ipv4_pktinfo_prepare(struct sk_buff
*skb
)
1038 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1040 if (skb_rtable(skb
)) {
1041 pktinfo
->ipi_ifindex
= inet_iif(skb
);
1042 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1044 pktinfo
->ipi_ifindex
= 0;
1045 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1050 int ip_setsockopt(struct sock
*sk
, int level
,
1051 int optname
, char __user
*optval
, unsigned int optlen
)
1055 if (level
!= SOL_IP
)
1056 return -ENOPROTOOPT
;
1058 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1059 #ifdef CONFIG_NETFILTER
1060 /* we need to exclude all possible ENOPROTOOPTs except default case */
1061 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1062 optname
!= IP_IPSEC_POLICY
&&
1063 optname
!= IP_XFRM_POLICY
&&
1064 !ip_mroute_opt(optname
)) {
1066 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1072 EXPORT_SYMBOL(ip_setsockopt
);
1074 #ifdef CONFIG_COMPAT
1075 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1076 char __user
*optval
, unsigned int optlen
)
1080 if (level
!= SOL_IP
)
1081 return -ENOPROTOOPT
;
1083 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1084 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1087 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1088 #ifdef CONFIG_NETFILTER
1089 /* we need to exclude all possible ENOPROTOOPTs except default case */
1090 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1091 optname
!= IP_IPSEC_POLICY
&&
1092 optname
!= IP_XFRM_POLICY
&&
1093 !ip_mroute_opt(optname
)) {
1095 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1102 EXPORT_SYMBOL(compat_ip_setsockopt
);
1106 * Get the options. Note for future reference. The GET of IP options gets
1107 * the _received_ ones. The set sets the _sent_ ones.
1110 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1111 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1113 struct inet_sock
*inet
= inet_sk(sk
);
1117 if (level
!= SOL_IP
)
1120 if (ip_mroute_opt(optname
))
1121 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1123 if (get_user(len
, optlen
))
1133 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1134 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1135 struct ip_options_rcu
*inet_opt
;
1137 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1138 sock_owned_by_user(sk
));
1141 memcpy(optbuf
, &inet_opt
->opt
,
1142 sizeof(struct ip_options
) +
1143 inet_opt
->opt
.optlen
);
1146 if (opt
->optlen
== 0)
1147 return put_user(0, optlen
);
1149 ip_options_undo(opt
);
1151 len
= min_t(unsigned int, len
, opt
->optlen
);
1152 if (put_user(len
, optlen
))
1154 if (copy_to_user(optval
, opt
->__data
, len
))
1159 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1162 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1165 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1168 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1171 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1174 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1176 case IP_RECVORIGDSTADDR
:
1177 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1183 val
= (inet
->uc_ttl
== -1 ?
1184 sysctl_ip_default_ttl
:
1188 val
= inet
->hdrincl
;
1191 val
= inet
->nodefrag
;
1193 case IP_MTU_DISCOVER
:
1194 val
= inet
->pmtudisc
;
1198 struct dst_entry
*dst
;
1200 dst
= sk_dst_get(sk
);
1212 val
= inet
->recverr
;
1214 case IP_MULTICAST_TTL
:
1217 case IP_MULTICAST_LOOP
:
1218 val
= inet
->mc_loop
;
1221 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1223 case IP_MULTICAST_IF
:
1225 struct in_addr addr
;
1226 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1227 addr
.s_addr
= inet
->mc_addr
;
1230 if (put_user(len
, optlen
))
1232 if (copy_to_user(optval
, &addr
, len
))
1238 struct ip_msfilter msf
;
1241 if (len
< IP_MSFILTER_SIZE(0)) {
1245 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1249 err
= ip_mc_msfget(sk
, &msf
,
1250 (struct ip_msfilter __user
*)optval
, optlen
);
1254 case MCAST_MSFILTER
:
1256 struct group_filter gsf
;
1259 if (len
< GROUP_FILTER_SIZE(0)) {
1263 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1267 err
= ip_mc_gsfget(sk
, &gsf
,
1268 (struct group_filter __user
*)optval
,
1273 case IP_MULTICAST_ALL
:
1282 if (sk
->sk_type
!= SOCK_STREAM
)
1283 return -ENOPROTOOPT
;
1285 msg
.msg_control
= optval
;
1286 msg
.msg_controllen
= len
;
1287 msg
.msg_flags
= flags
;
1289 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1290 struct in_pktinfo info
;
1292 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1293 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1294 info
.ipi_ifindex
= inet
->mc_index
;
1295 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1297 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1298 int hlim
= inet
->mc_ttl
;
1299 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1301 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1302 int tos
= inet
->rcv_tos
;
1303 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1305 len
-= msg
.msg_controllen
;
1306 return put_user(len
, optlen
);
1309 val
= inet
->freebind
;
1311 case IP_TRANSPARENT
:
1312 val
= inet
->transparent
;
1315 val
= inet
->min_ttl
;
1319 return -ENOPROTOOPT
;
1323 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1324 unsigned char ucval
= (unsigned char)val
;
1326 if (put_user(len
, optlen
))
1328 if (copy_to_user(optval
, &ucval
, 1))
1331 len
= min_t(unsigned int, sizeof(int), len
);
1332 if (put_user(len
, optlen
))
1334 if (copy_to_user(optval
, &val
, len
))
1340 int ip_getsockopt(struct sock
*sk
, int level
,
1341 int optname
, char __user
*optval
, int __user
*optlen
)
1345 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1346 #ifdef CONFIG_NETFILTER
1347 /* we need to exclude all possible ENOPROTOOPTs except default case */
1348 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1349 !ip_mroute_opt(optname
)) {
1352 if (get_user(len
, optlen
))
1356 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1360 err
= put_user(len
, optlen
);
1366 EXPORT_SYMBOL(ip_getsockopt
);
1368 #ifdef CONFIG_COMPAT
1369 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1370 char __user
*optval
, int __user
*optlen
)
1374 if (optname
== MCAST_MSFILTER
)
1375 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1378 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1381 #ifdef CONFIG_NETFILTER
1382 /* we need to exclude all possible ENOPROTOOPTs except default case */
1383 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1384 !ip_mroute_opt(optname
)) {
1387 if (get_user(len
, optlen
))
1391 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1394 err
= put_user(len
, optlen
);
1400 EXPORT_SYMBOL(compat_ip_getsockopt
);