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>
44 #include <linux/errqueue.h>
45 #include <asm/uaccess.h>
47 #define IP_CMSG_PKTINFO 1
50 #define IP_CMSG_RECVOPTS 8
51 #define IP_CMSG_RETOPTS 16
52 #define IP_CMSG_PASSSEC 32
53 #define IP_CMSG_ORIGDSTADDR 64
56 * SOL_IP control messages.
58 #define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb))
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 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
)
192 struct cmsghdr
*cmsg
;
194 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
195 if (!CMSG_OK(msg
, cmsg
))
197 if (cmsg
->cmsg_level
!= SOL_IP
)
199 switch (cmsg
->cmsg_type
) {
201 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
202 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
203 err
< 40 ? err
: 40);
209 struct in_pktinfo
*info
;
210 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
212 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
213 ipc
->oif
= info
->ipi_ifindex
;
214 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
225 /* Special input handler for packets caught by router alert option.
226 They are selected only by protocol field, and then processed likely
227 local ones; but only if someone wants them! Otherwise, router
228 not running rsvpd will kill RSVP.
230 It is user level problem, what it will make with them.
231 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
232 but receiver should be enough clever f.e. to forward mtrace requests,
233 sent to multicast group to reach destination designated router.
235 struct ip_ra_chain __rcu
*ip_ra_chain
;
236 static DEFINE_SPINLOCK(ip_ra_lock
);
239 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
241 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
243 sock_put(ra
->saved_sk
);
247 int ip_ra_control(struct sock
*sk
, unsigned char on
,
248 void (*destructor
)(struct sock
*))
250 struct ip_ra_chain
*ra
, *new_ra
;
251 struct ip_ra_chain __rcu
**rap
;
253 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
256 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
258 spin_lock_bh(&ip_ra_lock
);
259 for (rap
= &ip_ra_chain
;
260 (ra
= rcu_dereference_protected(*rap
,
261 lockdep_is_held(&ip_ra_lock
))) != NULL
;
265 spin_unlock_bh(&ip_ra_lock
);
269 /* dont let ip_call_ra_chain() use sk again */
271 rcu_assign_pointer(*rap
, ra
->next
);
272 spin_unlock_bh(&ip_ra_lock
);
277 * Delay sock_put(sk) and kfree(ra) after one rcu grace
278 * period. This guarantee ip_call_ra_chain() dont need
279 * to mess with socket refcounts.
282 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
286 if (new_ra
== NULL
) {
287 spin_unlock_bh(&ip_ra_lock
);
291 new_ra
->destructor
= destructor
;
294 rcu_assign_pointer(*rap
, new_ra
);
296 spin_unlock_bh(&ip_ra_lock
);
301 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
302 __be16 port
, u32 info
, u8
*payload
)
304 struct sock_exterr_skb
*serr
;
306 skb
= skb_clone(skb
, GFP_ATOMIC
);
310 serr
= SKB_EXT_ERR(skb
);
311 serr
->ee
.ee_errno
= err
;
312 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
313 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
314 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
316 serr
->ee
.ee_info
= info
;
317 serr
->ee
.ee_data
= 0;
318 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
319 skb_network_header(skb
);
322 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
323 skb_reset_transport_header(skb
);
324 if (sock_queue_err_skb(sk
, skb
) == 0)
330 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
332 struct inet_sock
*inet
= inet_sk(sk
);
333 struct sock_exterr_skb
*serr
;
340 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
344 skb_put(skb
, sizeof(struct iphdr
));
345 skb_reset_network_header(skb
);
349 serr
= SKB_EXT_ERR(skb
);
350 serr
->ee
.ee_errno
= err
;
351 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
352 serr
->ee
.ee_type
= 0;
353 serr
->ee
.ee_code
= 0;
355 serr
->ee
.ee_info
= info
;
356 serr
->ee
.ee_data
= 0;
357 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
360 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
361 skb_reset_transport_header(skb
);
363 if (sock_queue_err_skb(sk
, skb
))
368 * Handle MSG_ERRQUEUE
370 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
372 struct sock_exterr_skb
*serr
;
373 struct sk_buff
*skb
, *skb2
;
374 struct sockaddr_in
*sin
;
376 struct sock_extended_err ee
;
377 struct sockaddr_in offender
;
383 skb
= skb_dequeue(&sk
->sk_error_queue
);
389 msg
->msg_flags
|= MSG_TRUNC
;
392 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
396 sock_recv_timestamp(msg
, sk
, skb
);
398 serr
= SKB_EXT_ERR(skb
);
400 sin
= (struct sockaddr_in
*)msg
->msg_name
;
402 sin
->sin_family
= AF_INET
;
403 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
405 sin
->sin_port
= serr
->port
;
406 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
409 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
410 sin
= &errhdr
.offender
;
411 sin
->sin_family
= AF_UNSPEC
;
412 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
413 struct inet_sock
*inet
= inet_sk(sk
);
415 sin
->sin_family
= AF_INET
;
416 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
418 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
419 if (inet
->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
);
448 static void opt_kfree_rcu(struct rcu_head
*head
)
450 kfree(container_of(head
, struct ip_options_rcu
, rcu
));
454 * Socket option code for IP. This is the end of the line after any
455 * TCP,UDP etc options on an IP socket.
458 static int do_ip_setsockopt(struct sock
*sk
, int level
,
459 int optname
, char __user
*optval
, unsigned int optlen
)
461 struct inet_sock
*inet
= inet_sk(sk
);
464 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
465 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
466 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
467 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
468 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
469 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
470 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
) |
471 (1<<IP_MINTTL
) | (1<<IP_NODEFRAG
))) ||
472 optname
== IP_MULTICAST_TTL
||
473 optname
== IP_MULTICAST_ALL
||
474 optname
== IP_MULTICAST_LOOP
||
475 optname
== IP_RECVORIGDSTADDR
) {
476 if (optlen
>= sizeof(int)) {
477 if (get_user(val
, (int __user
*) optval
))
479 } else if (optlen
>= sizeof(char)) {
482 if (get_user(ucval
, (unsigned char __user
*) optval
))
488 /* If optlen==0, it is equivalent to val == 0 */
490 if (ip_mroute_opt(optname
))
491 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
499 struct ip_options_rcu
*old
, *opt
= NULL
;
503 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
507 old
= rcu_dereference_protected(inet
->inet_opt
,
508 sock_owned_by_user(sk
));
510 struct inet_connection_sock
*icsk
= inet_csk(sk
);
511 #if IS_ENABLED(CONFIG_IPV6)
512 if (sk
->sk_family
== PF_INET
||
513 (!((1 << sk
->sk_state
) &
514 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
515 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
518 icsk
->icsk_ext_hdr_len
-= old
->opt
.optlen
;
520 icsk
->icsk_ext_hdr_len
+= opt
->opt
.optlen
;
521 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
522 #if IS_ENABLED(CONFIG_IPV6)
526 rcu_assign_pointer(inet
->inet_opt
, opt
);
528 call_rcu(&old
->rcu
, opt_kfree_rcu
);
533 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
535 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
539 inet
->cmsg_flags
|= IP_CMSG_TTL
;
541 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
545 inet
->cmsg_flags
|= IP_CMSG_TOS
;
547 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
551 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
553 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
557 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
559 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
563 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
565 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
567 case IP_RECVORIGDSTADDR
:
569 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
571 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
573 case IP_TOS
: /* This sets both TOS and Precedence */
574 if (sk
->sk_type
== SOCK_STREAM
) {
575 val
&= ~INET_ECN_MASK
;
576 val
|= inet
->tos
& INET_ECN_MASK
;
578 if (inet
->tos
!= val
) {
580 sk
->sk_priority
= rt_tos2priority(val
);
587 if (val
!= -1 && (val
< 0 || val
> 255))
592 if (sk
->sk_type
!= SOCK_RAW
) {
596 inet
->hdrincl
= val
? 1 : 0;
599 if (sk
->sk_type
!= SOCK_RAW
) {
603 inet
->nodefrag
= val
? 1 : 0;
605 case IP_MTU_DISCOVER
:
606 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
608 inet
->pmtudisc
= val
;
611 inet
->recverr
= !!val
;
613 skb_queue_purge(&sk
->sk_error_queue
);
615 case IP_MULTICAST_TTL
:
616 if (sk
->sk_type
== SOCK_STREAM
)
622 if (val
< 0 || val
> 255)
626 case IP_MULTICAST_LOOP
:
629 inet
->mc_loop
= !!val
;
631 case IP_MULTICAST_IF
:
633 struct ip_mreqn mreq
;
634 struct net_device
*dev
= NULL
;
636 if (sk
->sk_type
== SOCK_STREAM
)
639 * Check the arguments are allowable
642 if (optlen
< sizeof(struct in_addr
))
646 if (optlen
>= sizeof(struct ip_mreqn
)) {
647 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
650 memset(&mreq
, 0, sizeof(mreq
));
651 if (optlen
>= sizeof(struct in_addr
) &&
652 copy_from_user(&mreq
.imr_address
, optval
,
653 sizeof(struct in_addr
)))
657 if (!mreq
.imr_ifindex
) {
658 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
664 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
666 mreq
.imr_ifindex
= dev
->ifindex
;
668 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
671 err
= -EADDRNOTAVAIL
;
677 if (sk
->sk_bound_dev_if
&&
678 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
681 inet
->mc_index
= mreq
.imr_ifindex
;
682 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
687 case IP_ADD_MEMBERSHIP
:
688 case IP_DROP_MEMBERSHIP
:
690 struct ip_mreqn mreq
;
693 if (inet_sk(sk
)->is_icsk
)
696 if (optlen
< sizeof(struct ip_mreq
))
699 if (optlen
>= sizeof(struct ip_mreqn
)) {
700 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
703 memset(&mreq
, 0, sizeof(mreq
));
704 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
708 if (optname
== IP_ADD_MEMBERSHIP
)
709 err
= ip_mc_join_group(sk
, &mreq
);
711 err
= ip_mc_leave_group(sk
, &mreq
);
716 struct ip_msfilter
*msf
;
718 if (optlen
< IP_MSFILTER_SIZE(0))
720 if (optlen
> sysctl_optmem_max
) {
724 msf
= kmalloc(optlen
, GFP_KERNEL
);
730 if (copy_from_user(msf
, optval
, optlen
)) {
734 /* numsrc >= (1G-4) overflow in 32 bits */
735 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
736 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
741 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
746 err
= ip_mc_msfilter(sk
, msf
, 0);
750 case IP_BLOCK_SOURCE
:
751 case IP_UNBLOCK_SOURCE
:
752 case IP_ADD_SOURCE_MEMBERSHIP
:
753 case IP_DROP_SOURCE_MEMBERSHIP
:
755 struct ip_mreq_source mreqs
;
758 if (optlen
!= sizeof(struct ip_mreq_source
))
760 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
764 if (optname
== IP_BLOCK_SOURCE
) {
765 omode
= MCAST_EXCLUDE
;
767 } else if (optname
== IP_UNBLOCK_SOURCE
) {
768 omode
= MCAST_EXCLUDE
;
770 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
771 struct ip_mreqn mreq
;
773 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
774 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
775 mreq
.imr_ifindex
= 0;
776 err
= ip_mc_join_group(sk
, &mreq
);
777 if (err
&& err
!= -EADDRINUSE
)
779 omode
= MCAST_INCLUDE
;
781 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
782 omode
= MCAST_INCLUDE
;
785 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
788 case MCAST_JOIN_GROUP
:
789 case MCAST_LEAVE_GROUP
:
791 struct group_req greq
;
792 struct sockaddr_in
*psin
;
793 struct ip_mreqn mreq
;
795 if (optlen
< sizeof(struct group_req
))
798 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
800 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
801 if (psin
->sin_family
!= AF_INET
)
803 memset(&mreq
, 0, sizeof(mreq
));
804 mreq
.imr_multiaddr
= psin
->sin_addr
;
805 mreq
.imr_ifindex
= greq
.gr_interface
;
807 if (optname
== MCAST_JOIN_GROUP
)
808 err
= ip_mc_join_group(sk
, &mreq
);
810 err
= ip_mc_leave_group(sk
, &mreq
);
813 case MCAST_JOIN_SOURCE_GROUP
:
814 case MCAST_LEAVE_SOURCE_GROUP
:
815 case MCAST_BLOCK_SOURCE
:
816 case MCAST_UNBLOCK_SOURCE
:
818 struct group_source_req greqs
;
819 struct ip_mreq_source mreqs
;
820 struct sockaddr_in
*psin
;
823 if (optlen
!= sizeof(struct group_source_req
))
825 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
829 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
830 greqs
.gsr_source
.ss_family
!= AF_INET
) {
831 err
= -EADDRNOTAVAIL
;
834 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
835 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
836 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
837 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
838 mreqs
.imr_interface
= 0; /* use index for mc_source */
840 if (optname
== MCAST_BLOCK_SOURCE
) {
841 omode
= MCAST_EXCLUDE
;
843 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
844 omode
= MCAST_EXCLUDE
;
846 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
847 struct ip_mreqn mreq
;
849 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
850 mreq
.imr_multiaddr
= psin
->sin_addr
;
851 mreq
.imr_address
.s_addr
= 0;
852 mreq
.imr_ifindex
= greqs
.gsr_interface
;
853 err
= ip_mc_join_group(sk
, &mreq
);
854 if (err
&& err
!= -EADDRINUSE
)
856 greqs
.gsr_interface
= mreq
.imr_ifindex
;
857 omode
= MCAST_INCLUDE
;
859 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
860 omode
= MCAST_INCLUDE
;
863 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
864 greqs
.gsr_interface
);
869 struct sockaddr_in
*psin
;
870 struct ip_msfilter
*msf
= NULL
;
871 struct group_filter
*gsf
= NULL
;
872 int msize
, i
, ifindex
;
874 if (optlen
< GROUP_FILTER_SIZE(0))
876 if (optlen
> sysctl_optmem_max
) {
880 gsf
= kmalloc(optlen
, GFP_KERNEL
);
886 if (copy_from_user(gsf
, optval
, optlen
))
889 /* numsrc >= (4G-140)/128 overflow in 32 bits */
890 if (gsf
->gf_numsrc
>= 0x1ffffff ||
891 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
895 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
899 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
900 msf
= kmalloc(msize
, GFP_KERNEL
);
905 ifindex
= gsf
->gf_interface
;
906 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
907 if (psin
->sin_family
!= AF_INET
) {
908 err
= -EADDRNOTAVAIL
;
911 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
912 msf
->imsf_interface
= 0;
913 msf
->imsf_fmode
= gsf
->gf_fmode
;
914 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
915 err
= -EADDRNOTAVAIL
;
916 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
917 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
919 if (psin
->sin_family
!= AF_INET
)
921 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
926 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
932 case IP_MULTICAST_ALL
:
935 if (val
!= 0 && val
!= 1)
939 case IP_ROUTER_ALERT
:
940 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
946 inet
->freebind
= !!val
;
949 case IP_IPSEC_POLICY
:
952 if (!capable(CAP_NET_ADMIN
))
954 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
958 if (!!val
&& !capable(CAP_NET_RAW
) && !capable(CAP_NET_ADMIN
)) {
964 inet
->transparent
= !!val
;
970 if (val
< 0 || val
> 255)
988 * ipv4_pktinfo_prepare - transfert some info from rtable to skb
992 * To support IP_CMSG_PKTINFO option, we store rt_iif and rt_spec_dst
993 * in skb->cb[] before dst drop.
994 * This way, receiver doesnt make cache line misses to read rtable.
996 void ipv4_pktinfo_prepare(struct sk_buff
*skb
)
998 struct in_pktinfo
*pktinfo
= PKTINFO_SKB_CB(skb
);
999 const struct rtable
*rt
= skb_rtable(skb
);
1002 pktinfo
->ipi_ifindex
= rt
->rt_iif
;
1003 pktinfo
->ipi_spec_dst
.s_addr
= rt
->rt_spec_dst
;
1005 pktinfo
->ipi_ifindex
= 0;
1006 pktinfo
->ipi_spec_dst
.s_addr
= 0;
1011 int ip_setsockopt(struct sock
*sk
, int level
,
1012 int optname
, char __user
*optval
, unsigned int optlen
)
1016 if (level
!= SOL_IP
)
1017 return -ENOPROTOOPT
;
1019 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1020 #ifdef CONFIG_NETFILTER
1021 /* we need to exclude all possible ENOPROTOOPTs except default case */
1022 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1023 optname
!= IP_IPSEC_POLICY
&&
1024 optname
!= IP_XFRM_POLICY
&&
1025 !ip_mroute_opt(optname
)) {
1027 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1033 EXPORT_SYMBOL(ip_setsockopt
);
1035 #ifdef CONFIG_COMPAT
1036 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1037 char __user
*optval
, unsigned int optlen
)
1041 if (level
!= SOL_IP
)
1042 return -ENOPROTOOPT
;
1044 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1045 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1048 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1049 #ifdef CONFIG_NETFILTER
1050 /* we need to exclude all possible ENOPROTOOPTs except default case */
1051 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1052 optname
!= IP_IPSEC_POLICY
&&
1053 optname
!= IP_XFRM_POLICY
&&
1054 !ip_mroute_opt(optname
)) {
1056 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1063 EXPORT_SYMBOL(compat_ip_setsockopt
);
1067 * Get the options. Note for future reference. The GET of IP options gets
1068 * the _received_ ones. The set sets the _sent_ ones.
1071 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1072 char __user
*optval
, int __user
*optlen
, unsigned flags
)
1074 struct inet_sock
*inet
= inet_sk(sk
);
1078 if (level
!= SOL_IP
)
1081 if (ip_mroute_opt(optname
))
1082 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1084 if (get_user(len
, optlen
))
1094 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1095 struct ip_options
*opt
= (struct ip_options
*)optbuf
;
1096 struct ip_options_rcu
*inet_opt
;
1098 inet_opt
= rcu_dereference_protected(inet
->inet_opt
,
1099 sock_owned_by_user(sk
));
1102 memcpy(optbuf
, &inet_opt
->opt
,
1103 sizeof(struct ip_options
) +
1104 inet_opt
->opt
.optlen
);
1107 if (opt
->optlen
== 0)
1108 return put_user(0, optlen
);
1110 ip_options_undo(opt
);
1112 len
= min_t(unsigned int, len
, opt
->optlen
);
1113 if (put_user(len
, optlen
))
1115 if (copy_to_user(optval
, opt
->__data
, len
))
1120 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1123 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1126 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1129 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1132 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1135 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1137 case IP_RECVORIGDSTADDR
:
1138 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1144 val
= (inet
->uc_ttl
== -1 ?
1145 sysctl_ip_default_ttl
:
1149 val
= inet
->hdrincl
;
1152 val
= inet
->nodefrag
;
1154 case IP_MTU_DISCOVER
:
1155 val
= inet
->pmtudisc
;
1159 struct dst_entry
*dst
;
1161 dst
= sk_dst_get(sk
);
1173 val
= inet
->recverr
;
1175 case IP_MULTICAST_TTL
:
1178 case IP_MULTICAST_LOOP
:
1179 val
= inet
->mc_loop
;
1181 case IP_MULTICAST_IF
:
1183 struct in_addr addr
;
1184 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1185 addr
.s_addr
= inet
->mc_addr
;
1188 if (put_user(len
, optlen
))
1190 if (copy_to_user(optval
, &addr
, len
))
1196 struct ip_msfilter msf
;
1199 if (len
< IP_MSFILTER_SIZE(0)) {
1203 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1207 err
= ip_mc_msfget(sk
, &msf
,
1208 (struct ip_msfilter __user
*)optval
, optlen
);
1212 case MCAST_MSFILTER
:
1214 struct group_filter gsf
;
1217 if (len
< GROUP_FILTER_SIZE(0)) {
1221 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1225 err
= ip_mc_gsfget(sk
, &gsf
,
1226 (struct group_filter __user
*)optval
,
1231 case IP_MULTICAST_ALL
:
1240 if (sk
->sk_type
!= SOCK_STREAM
)
1241 return -ENOPROTOOPT
;
1243 msg
.msg_control
= optval
;
1244 msg
.msg_controllen
= len
;
1245 msg
.msg_flags
= flags
;
1247 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1248 struct in_pktinfo info
;
1250 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1251 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1252 info
.ipi_ifindex
= inet
->mc_index
;
1253 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1255 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1256 int hlim
= inet
->mc_ttl
;
1257 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1259 len
-= msg
.msg_controllen
;
1260 return put_user(len
, optlen
);
1263 val
= inet
->freebind
;
1265 case IP_TRANSPARENT
:
1266 val
= inet
->transparent
;
1269 val
= inet
->min_ttl
;
1273 return -ENOPROTOOPT
;
1277 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1278 unsigned char ucval
= (unsigned char)val
;
1280 if (put_user(len
, optlen
))
1282 if (copy_to_user(optval
, &ucval
, 1))
1285 len
= min_t(unsigned int, sizeof(int), len
);
1286 if (put_user(len
, optlen
))
1288 if (copy_to_user(optval
, &val
, len
))
1294 int ip_getsockopt(struct sock
*sk
, int level
,
1295 int optname
, char __user
*optval
, int __user
*optlen
)
1299 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1300 #ifdef CONFIG_NETFILTER
1301 /* we need to exclude all possible ENOPROTOOPTs except default case */
1302 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1303 !ip_mroute_opt(optname
)) {
1306 if (get_user(len
, optlen
))
1310 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1314 err
= put_user(len
, optlen
);
1320 EXPORT_SYMBOL(ip_getsockopt
);
1322 #ifdef CONFIG_COMPAT
1323 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1324 char __user
*optval
, int __user
*optlen
)
1328 if (optname
== MCAST_MSFILTER
)
1329 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1332 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
,
1335 #ifdef CONFIG_NETFILTER
1336 /* we need to exclude all possible ENOPROTOOPTs except default case */
1337 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1338 !ip_mroute_opt(optname
)) {
1341 if (get_user(len
, optlen
))
1345 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1348 err
= put_user(len
, optlen
);
1354 EXPORT_SYMBOL(compat_ip_getsockopt
);