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
));
203 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
204 err
< 40 ? err
: 40);
210 struct in_pktinfo
*info
;
211 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
213 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
214 ipc
->oif
= info
->ipi_ifindex
;
215 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
226 /* Special input handler for packets caught by router alert option.
227 They are selected only by protocol field, and then processed likely
228 local ones; but only if someone wants them! Otherwise, router
229 not running rsvpd will kill RSVP.
231 It is user level problem, what it will make with them.
232 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
233 but receiver should be enough clever f.e. to forward mtrace requests,
234 sent to multicast group to reach destination designated router.
236 struct ip_ra_chain __rcu
*ip_ra_chain
;
237 static DEFINE_SPINLOCK(ip_ra_lock
);
240 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
242 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
244 sock_put(ra
->saved_sk
);
248 int ip_ra_control(struct sock
*sk
, unsigned char on
,
249 void (*destructor
)(struct sock
*))
251 struct ip_ra_chain
*ra
, *new_ra
;
252 struct ip_ra_chain __rcu
**rap
;
254 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
257 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
259 spin_lock_bh(&ip_ra_lock
);
260 for (rap
= &ip_ra_chain
;
261 (ra
= rcu_dereference_protected(*rap
,
262 lockdep_is_held(&ip_ra_lock
))) != NULL
;
266 spin_unlock_bh(&ip_ra_lock
);
270 /* dont let ip_call_ra_chain() use sk again */
272 rcu_assign_pointer(*rap
, ra
->next
);
273 spin_unlock_bh(&ip_ra_lock
);
278 * Delay sock_put(sk) and kfree(ra) after one rcu grace
279 * period. This guarantee ip_call_ra_chain() dont need
280 * to mess with socket refcounts.
283 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
287 if (new_ra
== NULL
) {
288 spin_unlock_bh(&ip_ra_lock
);
292 new_ra
->destructor
= destructor
;
295 rcu_assign_pointer(*rap
, new_ra
);
297 spin_unlock_bh(&ip_ra_lock
);
302 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
303 __be16 port
, u32 info
, u8
*payload
)
305 struct sock_exterr_skb
*serr
;
307 skb
= skb_clone(skb
, GFP_ATOMIC
);
311 serr
= SKB_EXT_ERR(skb
);
312 serr
->ee
.ee_errno
= err
;
313 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
314 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
315 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
317 serr
->ee
.ee_info
= info
;
318 serr
->ee
.ee_data
= 0;
319 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
320 skb_network_header(skb
);
323 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
324 skb_reset_transport_header(skb
);
325 if (sock_queue_err_skb(sk
, skb
) == 0)
331 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
333 struct inet_sock
*inet
= inet_sk(sk
);
334 struct sock_exterr_skb
*serr
;
341 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
345 skb_put(skb
, sizeof(struct iphdr
));
346 skb_reset_network_header(skb
);
350 serr
= SKB_EXT_ERR(skb
);
351 serr
->ee
.ee_errno
= err
;
352 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
353 serr
->ee
.ee_type
= 0;
354 serr
->ee
.ee_code
= 0;
356 serr
->ee
.ee_info
= info
;
357 serr
->ee
.ee_data
= 0;
358 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
361 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
362 skb_reset_transport_header(skb
);
364 if (sock_queue_err_skb(sk
, skb
))
369 * Handle MSG_ERRQUEUE
371 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
373 struct sock_exterr_skb
*serr
;
374 struct sk_buff
*skb
, *skb2
;
375 struct sockaddr_in
*sin
;
377 struct sock_extended_err ee
;
378 struct sockaddr_in offender
;
384 skb
= skb_dequeue(&sk
->sk_error_queue
);
390 msg
->msg_flags
|= MSG_TRUNC
;
393 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
397 sock_recv_timestamp(msg
, sk
, skb
);
399 serr
= SKB_EXT_ERR(skb
);
401 sin
= (struct sockaddr_in
*)msg
->msg_name
;
403 sin
->sin_family
= AF_INET
;
404 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
406 sin
->sin_port
= serr
->port
;
407 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
410 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
411 sin
= &errhdr
.offender
;
412 sin
->sin_family
= AF_UNSPEC
;
413 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
414 struct inet_sock
*inet
= inet_sk(sk
);
416 sin
->sin_family
= AF_INET
;
417 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
419 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
420 if (inet
->cmsg_flags
)
421 ip_cmsg_recv(msg
, skb
);
424 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
426 /* Now we could try to dump offended packet options */
428 msg
->msg_flags
|= MSG_ERRQUEUE
;
431 /* Reset and regenerate socket error */
432 spin_lock_bh(&sk
->sk_error_queue
.lock
);
434 skb2
= skb_peek(&sk
->sk_error_queue
);
436 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
437 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
438 sk
->sk_error_report(sk
);
440 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
450 * Socket option code for IP. This is the end of the line after any
451 * TCP,UDP etc options on an IP socket.
454 static int do_ip_setsockopt(struct sock
*sk
, int level
,
455 int optname
, char __user
*optval
, unsigned int optlen
)
457 struct inet_sock
*inet
= inet_sk(sk
);
460 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
461 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
462 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
463 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
464 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
465 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
466 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
) |
467 (1<<IP_MINTTL
) | (1<<IP_NODEFRAG
))) ||
468 optname
== IP_UNICAST_IF
||
469 optname
== IP_MULTICAST_TTL
||
470 optname
== IP_MULTICAST_ALL
||
471 optname
== IP_MULTICAST_LOOP
||
472 optname
== IP_RECVORIGDSTADDR
) {
473 if (optlen
>= sizeof(int)) {
474 if (get_user(val
, (int __user
*) optval
))
476 } else if (optlen
>= sizeof(char)) {
479 if (get_user(ucval
, (unsigned char __user
*) optval
))
485 /* If optlen==0, it is equivalent to val == 0 */
487 if (ip_mroute_opt(optname
))
488 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
496 struct ip_options_rcu
*old
, *opt
= NULL
;
500 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
504 old
= rcu_dereference_protected(inet
->inet_opt
,
505 sock_owned_by_user(sk
));
507 struct inet_connection_sock
*icsk
= inet_csk(sk
);
508 #if IS_ENABLED(CONFIG_IPV6)
509 if (sk
->sk_family
== PF_INET
||
510 (!((1 << sk
->sk_state
) &
511 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
512 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
515 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
517 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
518 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
519 #if IS_ENABLED(CONFIG_IPV6)
523 rcu_assign_pointer(inet
->inet_opt
, opt
);
530 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
532 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
536 inet
->cmsg_flags
|= IP_CMSG_TTL
;
538 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
542 inet
->cmsg_flags
|= IP_CMSG_TOS
;
544 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
548 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
550 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
554 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
556 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
560 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
562 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
564 case IP_RECVORIGDSTADDR
:
566 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
568 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
570 case IP_TOS
: /* This sets both TOS and Precedence */
571 if (sk
->sk_type
== SOCK_STREAM
) {
572 val
&= ~INET_ECN_MASK
;
573 val
|= inet
->tos
& INET_ECN_MASK
;
575 if (inet
->tos
!= val
) {
577 sk
->sk_priority
= rt_tos2priority(val
);
584 if (val
!= -1 && (val
< 0 || val
> 255))
589 if (sk
->sk_type
!= SOCK_RAW
) {
593 inet
->hdrincl
= val
? 1 : 0;
596 if (sk
->sk_type
!= SOCK_RAW
) {
600 inet
->nodefrag
= val
? 1 : 0;
602 case IP_MTU_DISCOVER
:
603 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
605 inet
->pmtudisc
= val
;
608 inet
->recverr
= !!val
;
610 skb_queue_purge(&sk
->sk_error_queue
);
612 case IP_MULTICAST_TTL
:
613 if (sk
->sk_type
== SOCK_STREAM
)
619 if (val
< 0 || val
> 255)
623 case IP_MULTICAST_LOOP
:
626 inet
->mc_loop
= !!val
;
630 struct net_device
*dev
= NULL
;
633 if (optlen
!= sizeof(int))
636 ifindex
= (__force
int)ntohl((__force __be32
)val
);
643 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
644 err
= -EADDRNOTAVAIL
;
650 if (sk
->sk_bound_dev_if
)
653 inet
->uc_index
= ifindex
;
657 case IP_MULTICAST_IF
:
659 struct ip_mreqn mreq
;
660 struct net_device
*dev
= NULL
;
662 if (sk
->sk_type
== SOCK_STREAM
)
665 * Check the arguments are allowable
668 if (optlen
< sizeof(struct in_addr
))
672 if (optlen
>= sizeof(struct ip_mreqn
)) {
673 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
676 memset(&mreq
, 0, sizeof(mreq
));
677 if (optlen
>= sizeof(struct ip_mreq
)) {
678 if (copy_from_user(&mreq
, optval
,
679 sizeof(struct ip_mreq
)))
681 } else if (optlen
>= sizeof(struct in_addr
)) {
682 if (copy_from_user(&mreq
.imr_address
, optval
,
683 sizeof(struct in_addr
)))
688 if (!mreq
.imr_ifindex
) {
689 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
695 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
697 mreq
.imr_ifindex
= dev
->ifindex
;
699 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
702 err
= -EADDRNOTAVAIL
;
708 if (sk
->sk_bound_dev_if
&&
709 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
712 inet
->mc_index
= mreq
.imr_ifindex
;
713 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
718 case IP_ADD_MEMBERSHIP
:
719 case IP_DROP_MEMBERSHIP
:
721 struct ip_mreqn mreq
;
724 if (inet_sk(sk
)->is_icsk
)
727 if (optlen
< sizeof(struct ip_mreq
))
730 if (optlen
>= sizeof(struct ip_mreqn
)) {
731 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
734 memset(&mreq
, 0, sizeof(mreq
));
735 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
739 if (optname
== IP_ADD_MEMBERSHIP
)
740 err
= ip_mc_join_group(sk
, &mreq
);
742 err
= ip_mc_leave_group(sk
, &mreq
);
747 struct ip_msfilter
*msf
;
749 if (optlen
< IP_MSFILTER_SIZE(0))
751 if (optlen
> sysctl_optmem_max
) {
755 msf
= kmalloc(optlen
, GFP_KERNEL
);
761 if (copy_from_user(msf
, optval
, optlen
)) {
765 /* numsrc >= (1G-4) overflow in 32 bits */
766 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
767 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
772 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
777 err
= ip_mc_msfilter(sk
, msf
, 0);
781 case IP_BLOCK_SOURCE
:
782 case IP_UNBLOCK_SOURCE
:
783 case IP_ADD_SOURCE_MEMBERSHIP
:
784 case IP_DROP_SOURCE_MEMBERSHIP
:
786 struct ip_mreq_source mreqs
;
789 if (optlen
!= sizeof(struct ip_mreq_source
))
791 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
795 if (optname
== IP_BLOCK_SOURCE
) {
796 omode
= MCAST_EXCLUDE
;
798 } else if (optname
== IP_UNBLOCK_SOURCE
) {
799 omode
= MCAST_EXCLUDE
;
801 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
802 struct ip_mreqn mreq
;
804 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
805 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
806 mreq
.imr_ifindex
= 0;
807 err
= ip_mc_join_group(sk
, &mreq
);
808 if (err
&& err
!= -EADDRINUSE
)
810 omode
= MCAST_INCLUDE
;
812 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
813 omode
= MCAST_INCLUDE
;
816 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
819 case MCAST_JOIN_GROUP
:
820 case MCAST_LEAVE_GROUP
:
822 struct group_req greq
;
823 struct sockaddr_in
*psin
;
824 struct ip_mreqn mreq
;
826 if (optlen
< sizeof(struct group_req
))
829 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
831 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
832 if (psin
->sin_family
!= AF_INET
)
834 memset(&mreq
, 0, sizeof(mreq
));
835 mreq
.imr_multiaddr
= psin
->sin_addr
;
836 mreq
.imr_ifindex
= greq
.gr_interface
;
838 if (optname
== MCAST_JOIN_GROUP
)
839 err
= ip_mc_join_group(sk
, &mreq
);
841 err
= ip_mc_leave_group(sk
, &mreq
);
844 case MCAST_JOIN_SOURCE_GROUP
:
845 case MCAST_LEAVE_SOURCE_GROUP
:
846 case MCAST_BLOCK_SOURCE
:
847 case MCAST_UNBLOCK_SOURCE
:
849 struct group_source_req greqs
;
850 struct ip_mreq_source mreqs
;
851 struct sockaddr_in
*psin
;
854 if (optlen
!= sizeof(struct group_source_req
))
856 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
860 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
861 greqs
.gsr_source
.ss_family
!= AF_INET
) {
862 err
= -EADDRNOTAVAIL
;
865 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
866 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
867 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
868 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
869 mreqs
.imr_interface
= 0; /* use index for mc_source */
871 if (optname
== MCAST_BLOCK_SOURCE
) {
872 omode
= MCAST_EXCLUDE
;
874 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
875 omode
= MCAST_EXCLUDE
;
877 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
878 struct ip_mreqn mreq
;
880 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
881 mreq
.imr_multiaddr
= psin
->sin_addr
;
882 mreq
.imr_address
.s_addr
= 0;
883 mreq
.imr_ifindex
= greqs
.gsr_interface
;
884 err
= ip_mc_join_group(sk
, &mreq
);
885 if (err
&& err
!= -EADDRINUSE
)
887 greqs
.gsr_interface
= mreq
.imr_ifindex
;
888 omode
= MCAST_INCLUDE
;
890 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
891 omode
= MCAST_INCLUDE
;
894 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
895 greqs
.gsr_interface
);
900 struct sockaddr_in
*psin
;
901 struct ip_msfilter
*msf
= NULL
;
902 struct group_filter
*gsf
= NULL
;
903 int msize
, i
, ifindex
;
905 if (optlen
< GROUP_FILTER_SIZE(0))
907 if (optlen
> sysctl_optmem_max
) {
911 gsf
= kmalloc(optlen
, GFP_KERNEL
);
917 if (copy_from_user(gsf
, optval
, optlen
))
920 /* numsrc >= (4G-140)/128 overflow in 32 bits */
921 if (gsf
->gf_numsrc
>= 0x1ffffff ||
922 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
926 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
930 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
931 msf
= kmalloc(msize
, GFP_KERNEL
);
936 ifindex
= gsf
->gf_interface
;
937 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
938 if (psin
->sin_family
!= AF_INET
) {
939 err
= -EADDRNOTAVAIL
;
942 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
943 msf
->imsf_interface
= 0;
944 msf
->imsf_fmode
= gsf
->gf_fmode
;
945 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
946 err
= -EADDRNOTAVAIL
;
947 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
948 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
950 if (psin
->sin_family
!= AF_INET
)
952 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
957 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
963 case IP_MULTICAST_ALL
:
966 if (val
!= 0 && val
!= 1)
970 case IP_ROUTER_ALERT
:
971 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
977 inet
->freebind
= !!val
;
980 case IP_IPSEC_POLICY
:
983 if (!capable(CAP_NET_ADMIN
))
985 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
989 if (!!val
&& !capable(CAP_NET_RAW
) && !capable(CAP_NET_ADMIN
)) {
995 inet
->transparent
= !!val
;
1001 if (val
< 0 || val
> 255)
1003 inet
->min_ttl
= val
;
1019 * ipv4_pktinfo_prepare - transfert some info from rtable to skb
1023 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1024 * destination in skb->cb[] before dst drop.
1025 * This way, receiver doesnt make cache line misses to read rtable.
1027 void ipv4_pktinfo_prepare(struct sk_buff
*skb
)
1029 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
1031 if (skb_rtable(skb
)) {
1032 pktinfo
->ipi_ifindex
= inet_iif(skb
);
1033 pktinfo
->ipi_spec_dst
.s_addr
= fib_compute_spec_dst(skb
);
1035 pktinfo
->ipi_ifindex
= 0;
1036 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1041 int ip_setsockopt(struct sock
*sk
, int level
,
1042 int optname
, char __user
*optval
, unsigned int optlen
)
1046 if (level
!= SOL_IP
)
1047 return -ENOPROTOOPT
;
1049 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1050 #ifdef CONFIG_NETFILTER
1051 /* we need to exclude all possible ENOPROTOOPTs except default case */
1052 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1053 optname
!= IP_IPSEC_POLICY
&&
1054 optname
!= IP_XFRM_POLICY
&&
1055 !ip_mroute_opt(optname
)) {
1057 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1063 EXPORT_SYMBOL(ip_setsockopt
);
1065 #ifdef CONFIG_COMPAT
1066 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1067 char __user
*optval
, unsigned int optlen
)
1071 if (level
!= SOL_IP
)
1072 return -ENOPROTOOPT
;
1074 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1075 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1078 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1079 #ifdef CONFIG_NETFILTER
1080 /* we need to exclude all possible ENOPROTOOPTs except default case */
1081 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1082 optname
!= IP_IPSEC_POLICY
&&
1083 optname
!= IP_XFRM_POLICY
&&
1084 !ip_mroute_opt(optname
)) {
1086 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1093 EXPORT_SYMBOL(compat_ip_setsockopt
);
1097 * Get the options. Note for future reference. The GET of IP options gets
1098 * the _received_ ones. The set sets the _sent_ ones.
1101 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1102 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1104 struct inet_sock
*inet
= inet_sk(sk
);
1108 if (level
!= SOL_IP
)
1111 if (ip_mroute_opt(optname
))
1112 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1114 if (get_user(len
, optlen
))
1124 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1125 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1126 struct ip_options_rcu
*inet_opt
;
1128 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1129 sock_owned_by_user(sk
));
1132 memcpy(optbuf
, &inet_opt
->opt
,
1133 sizeof(struct ip_options
) +
1134 inet_opt
->opt
.optlen
);
1137 if (opt
->optlen
== 0)
1138 return put_user(0, optlen
);
1140 ip_options_undo(opt
);
1142 len
= min_t(unsigned int, len
, opt
->optlen
);
1143 if (put_user(len
, optlen
))
1145 if (copy_to_user(optval
, opt
->__data
, len
))
1150 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1153 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1156 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1159 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1162 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1165 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1167 case IP_RECVORIGDSTADDR
:
1168 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1174 val
= (inet
->uc_ttl
== -1 ?
1175 sysctl_ip_default_ttl
:
1179 val
= inet
->hdrincl
;
1182 val
= inet
->nodefrag
;
1184 case IP_MTU_DISCOVER
:
1185 val
= inet
->pmtudisc
;
1189 struct dst_entry
*dst
;
1191 dst
= sk_dst_get(sk
);
1203 val
= inet
->recverr
;
1205 case IP_MULTICAST_TTL
:
1208 case IP_MULTICAST_LOOP
:
1209 val
= inet
->mc_loop
;
1212 val
= (__force
int)htonl((__u32
) inet
->uc_index
);
1214 case IP_MULTICAST_IF
:
1216 struct in_addr addr
;
1217 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1218 addr
.s_addr
= inet
->mc_addr
;
1221 if (put_user(len
, optlen
))
1223 if (copy_to_user(optval
, &addr
, len
))
1229 struct ip_msfilter msf
;
1232 if (len
< IP_MSFILTER_SIZE(0)) {
1236 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1240 err
= ip_mc_msfget(sk
, &msf
,
1241 (struct ip_msfilter __user
*)optval
, optlen
);
1245 case MCAST_MSFILTER
:
1247 struct group_filter gsf
;
1250 if (len
< GROUP_FILTER_SIZE(0)) {
1254 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1258 err
= ip_mc_gsfget(sk
, &gsf
,
1259 (struct group_filter __user
*)optval
,
1264 case IP_MULTICAST_ALL
:
1273 if (sk
->sk_type
!= SOCK_STREAM
)
1274 return -ENOPROTOOPT
;
1276 msg
.msg_control
= optval
;
1277 msg
.msg_controllen
= len
;
1278 msg
.msg_flags
= flags
;
1280 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1281 struct in_pktinfo info
;
1283 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1284 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1285 info
.ipi_ifindex
= inet
->mc_index
;
1286 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1288 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1289 int hlim
= inet
->mc_ttl
;
1290 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1292 if (inet
->cmsg_flags
& IP_CMSG_TOS
) {
1293 int tos
= inet
->rcv_tos
;
1294 put_cmsg(&msg
, SOL_IP
, IP_TOS
, sizeof(tos
), &tos
);
1296 len
-= msg
.msg_controllen
;
1297 return put_user(len
, optlen
);
1300 val
= inet
->freebind
;
1302 case IP_TRANSPARENT
:
1303 val
= inet
->transparent
;
1306 val
= inet
->min_ttl
;
1310 return -ENOPROTOOPT
;
1314 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1315 unsigned char ucval
= (unsigned char)val
;
1317 if (put_user(len
, optlen
))
1319 if (copy_to_user(optval
, &ucval
, 1))
1322 len
= min_t(unsigned int, sizeof(int), len
);
1323 if (put_user(len
, optlen
))
1325 if (copy_to_user(optval
, &val
, len
))
1331 int ip_getsockopt(struct sock
*sk
, int level
,
1332 int optname
, char __user
*optval
, int __user
*optlen
)
1336 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1337 #ifdef CONFIG_NETFILTER
1338 /* we need to exclude all possible ENOPROTOOPTs except default case */
1339 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1340 !ip_mroute_opt(optname
)) {
1343 if (get_user(len
, optlen
))
1347 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1351 err
= put_user(len
, optlen
);
1357 EXPORT_SYMBOL(ip_getsockopt
);
1359 #ifdef CONFIG_COMPAT
1360 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1361 char __user
*optval
, int __user
*optlen
)
1365 if (optname
== MCAST_MSFILTER
)
1366 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1369 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1372 #ifdef CONFIG_NETFILTER
1373 /* we need to exclude all possible ENOPROTOOPTs except default case */
1374 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1375 !ip_mroute_opt(optname
)) {
1378 if (get_user(len
, optlen
))
1382 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1385 err
= put_user(len
, optlen
);
1391 EXPORT_SYMBOL(compat_ip_getsockopt
);