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/route.h>
38 #include <net/compat.h>
39 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
40 #include <net/transp_v6.h>
43 #include <linux/errqueue.h>
44 #include <asm/uaccess.h>
46 #define IP_CMSG_PKTINFO 1
49 #define IP_CMSG_RECVOPTS 8
50 #define IP_CMSG_RETOPTS 16
51 #define IP_CMSG_PASSSEC 32
52 #define IP_CMSG_ORIGDSTADDR 64
55 * SOL_IP control messages.
58 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
60 struct in_pktinfo info
;
61 struct rtable
*rt
= skb_rtable(skb
);
63 info
.ipi_addr
.s_addr
= ip_hdr(skb
)->daddr
;
65 info
.ipi_ifindex
= rt
->rt_iif
;
66 info
.ipi_spec_dst
.s_addr
= rt
->rt_spec_dst
;
69 info
.ipi_spec_dst
.s_addr
= 0;
72 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
75 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
77 int ttl
= ip_hdr(skb
)->ttl
;
78 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
81 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
83 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &ip_hdr(skb
)->tos
);
86 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
88 if (IPCB(skb
)->opt
.optlen
== 0)
91 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
,
96 static void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
98 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
99 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
101 if (IPCB(skb
)->opt
.optlen
== 0)
104 if (ip_options_echo(opt
, skb
)) {
105 msg
->msg_flags
|= MSG_CTRUNC
;
108 ip_options_undo(opt
);
110 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
113 static void ip_cmsg_recv_security(struct msghdr
*msg
, struct sk_buff
*skb
)
119 err
= security_socket_getpeersec_dgram(NULL
, skb
, &secid
);
123 err
= security_secid_to_secctx(secid
, &secdata
, &seclen
);
127 put_cmsg(msg
, SOL_IP
, SCM_SECURITY
, seclen
, secdata
);
128 security_release_secctx(secdata
, seclen
);
131 static void ip_cmsg_recv_dstaddr(struct msghdr
*msg
, struct sk_buff
*skb
)
133 struct sockaddr_in sin
;
134 struct iphdr
*iph
= ip_hdr(skb
);
135 __be16
*ports
= (__be16
*)skb_transport_header(skb
);
137 if (skb_transport_offset(skb
) + 4 > skb
->len
)
140 /* All current transport protocols have the port numbers in the
141 * first four bytes of the transport header and this function is
142 * written with this assumption in mind.
145 sin
.sin_family
= AF_INET
;
146 sin
.sin_addr
.s_addr
= iph
->daddr
;
147 sin
.sin_port
= ports
[1];
148 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
150 put_cmsg(msg
, SOL_IP
, IP_ORIGDSTADDR
, sizeof(sin
), &sin
);
153 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
155 struct inet_sock
*inet
= inet_sk(skb
->sk
);
156 unsigned flags
= inet
->cmsg_flags
;
158 /* Ordered by supposed usage frequency */
160 ip_cmsg_recv_pktinfo(msg
, skb
);
161 if ((flags
>>= 1) == 0)
165 ip_cmsg_recv_ttl(msg
, skb
);
166 if ((flags
>>= 1) == 0)
170 ip_cmsg_recv_tos(msg
, skb
);
171 if ((flags
>>= 1) == 0)
175 ip_cmsg_recv_opts(msg
, skb
);
176 if ((flags
>>= 1) == 0)
180 ip_cmsg_recv_retopts(msg
, skb
);
181 if ((flags
>>= 1) == 0)
185 ip_cmsg_recv_security(msg
, skb
);
187 if ((flags
>>= 1) == 0)
190 ip_cmsg_recv_dstaddr(msg
, skb
);
193 EXPORT_SYMBOL(ip_cmsg_recv
);
195 int ip_cmsg_send(struct net
*net
, struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
198 struct cmsghdr
*cmsg
;
200 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
201 if (!CMSG_OK(msg
, cmsg
))
203 if (cmsg
->cmsg_level
!= SOL_IP
)
205 switch (cmsg
->cmsg_type
) {
207 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
208 err
= ip_options_get(net
, &ipc
->opt
, CMSG_DATA(cmsg
),
209 err
< 40 ? err
: 40);
215 struct in_pktinfo
*info
;
216 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
218 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
219 ipc
->oif
= info
->ipi_ifindex
;
220 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
231 /* Special input handler for packets caught by router alert option.
232 They are selected only by protocol field, and then processed likely
233 local ones; but only if someone wants them! Otherwise, router
234 not running rsvpd will kill RSVP.
236 It is user level problem, what it will make with them.
237 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
238 but receiver should be enough clever f.e. to forward mtrace requests,
239 sent to multicast group to reach destination designated router.
241 struct ip_ra_chain
*ip_ra_chain
;
242 static DEFINE_SPINLOCK(ip_ra_lock
);
245 static void ip_ra_destroy_rcu(struct rcu_head
*head
)
247 struct ip_ra_chain
*ra
= container_of(head
, struct ip_ra_chain
, rcu
);
249 sock_put(ra
->saved_sk
);
253 int ip_ra_control(struct sock
*sk
, unsigned char on
,
254 void (*destructor
)(struct sock
*))
256 struct ip_ra_chain
*ra
, *new_ra
, **rap
;
258 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
261 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
263 spin_lock_bh(&ip_ra_lock
);
264 for (rap
= &ip_ra_chain
; (ra
= *rap
) != NULL
; rap
= &ra
->next
) {
267 spin_unlock_bh(&ip_ra_lock
);
271 /* dont let ip_call_ra_chain() use sk again */
273 rcu_assign_pointer(*rap
, ra
->next
);
274 spin_unlock_bh(&ip_ra_lock
);
279 * Delay sock_put(sk) and kfree(ra) after one rcu grace
280 * period. This guarantee ip_call_ra_chain() dont need
281 * to mess with socket refcounts.
284 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
288 if (new_ra
== NULL
) {
289 spin_unlock_bh(&ip_ra_lock
);
293 new_ra
->destructor
= destructor
;
296 rcu_assign_pointer(*rap
, new_ra
);
298 spin_unlock_bh(&ip_ra_lock
);
303 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
304 __be16 port
, u32 info
, u8
*payload
)
306 struct sock_exterr_skb
*serr
;
308 skb
= skb_clone(skb
, GFP_ATOMIC
);
312 serr
= SKB_EXT_ERR(skb
);
313 serr
->ee
.ee_errno
= err
;
314 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
315 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
316 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
318 serr
->ee
.ee_info
= info
;
319 serr
->ee
.ee_data
= 0;
320 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
321 skb_network_header(skb
);
324 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
325 skb_reset_transport_header(skb
);
326 if (sock_queue_err_skb(sk
, skb
) == 0)
332 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
334 struct inet_sock
*inet
= inet_sk(sk
);
335 struct sock_exterr_skb
*serr
;
342 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
346 skb_put(skb
, sizeof(struct iphdr
));
347 skb_reset_network_header(skb
);
351 serr
= SKB_EXT_ERR(skb
);
352 serr
->ee
.ee_errno
= err
;
353 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
354 serr
->ee
.ee_type
= 0;
355 serr
->ee
.ee_code
= 0;
357 serr
->ee
.ee_info
= info
;
358 serr
->ee
.ee_data
= 0;
359 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
362 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
363 skb_reset_transport_header(skb
);
365 if (sock_queue_err_skb(sk
, skb
))
370 * Handle MSG_ERRQUEUE
372 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
374 struct sock_exterr_skb
*serr
;
375 struct sk_buff
*skb
, *skb2
;
376 struct sockaddr_in
*sin
;
378 struct sock_extended_err ee
;
379 struct sockaddr_in offender
;
385 skb
= skb_dequeue(&sk
->sk_error_queue
);
391 msg
->msg_flags
|= MSG_TRUNC
;
394 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
398 sock_recv_timestamp(msg
, sk
, skb
);
400 serr
= SKB_EXT_ERR(skb
);
402 sin
= (struct sockaddr_in
*)msg
->msg_name
;
404 sin
->sin_family
= AF_INET
;
405 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
407 sin
->sin_port
= serr
->port
;
408 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
411 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
412 sin
= &errhdr
.offender
;
413 sin
->sin_family
= AF_UNSPEC
;
414 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
415 struct inet_sock
*inet
= inet_sk(sk
);
417 sin
->sin_family
= AF_INET
;
418 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
420 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
421 if (inet
->cmsg_flags
)
422 ip_cmsg_recv(msg
, skb
);
425 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
427 /* Now we could try to dump offended packet options */
429 msg
->msg_flags
|= MSG_ERRQUEUE
;
432 /* Reset and regenerate socket error */
433 spin_lock_bh(&sk
->sk_error_queue
.lock
);
435 skb2
= skb_peek(&sk
->sk_error_queue
);
437 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
438 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
439 sk
->sk_error_report(sk
);
441 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
451 * Socket option code for IP. This is the end of the line after any
452 * TCP,UDP etc options on an IP socket.
455 static int do_ip_setsockopt(struct sock
*sk
, int level
,
456 int optname
, char __user
*optval
, unsigned int optlen
)
458 struct inet_sock
*inet
= inet_sk(sk
);
461 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
462 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
463 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
464 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
465 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
466 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
467 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
) |
468 (1<<IP_MINTTL
) | (1<<IP_NODEFRAG
))) ||
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
*opt
= NULL
;
499 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
504 struct inet_connection_sock
*icsk
= inet_csk(sk
);
505 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
506 if (sk
->sk_family
== PF_INET
||
507 (!((1 << sk
->sk_state
) &
508 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
509 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
512 icsk
->icsk_ext_hdr_len
-= inet
->opt
->optlen
;
514 icsk
->icsk_ext_hdr_len
+= opt
->optlen
;
515 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
516 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
520 opt
= xchg(&inet
->opt
, opt
);
526 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
528 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
532 inet
->cmsg_flags
|= IP_CMSG_TTL
;
534 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
538 inet
->cmsg_flags
|= IP_CMSG_TOS
;
540 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
544 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
546 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
550 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
552 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
556 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
558 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
560 case IP_RECVORIGDSTADDR
:
562 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
564 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
566 case IP_TOS
: /* This sets both TOS and Precedence */
567 if (sk
->sk_type
== SOCK_STREAM
) {
569 val
|= inet
->tos
& 3;
571 if (inet
->tos
!= val
) {
573 sk
->sk_priority
= rt_tos2priority(val
);
580 if (val
!= -1 && (val
< 0 || val
> 255))
585 if (sk
->sk_type
!= SOCK_RAW
) {
589 inet
->hdrincl
= val
? 1 : 0;
592 if (sk
->sk_type
!= SOCK_RAW
) {
596 inet
->nodefrag
= val
? 1 : 0;
598 case IP_MTU_DISCOVER
:
599 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
601 inet
->pmtudisc
= val
;
604 inet
->recverr
= !!val
;
606 skb_queue_purge(&sk
->sk_error_queue
);
608 case IP_MULTICAST_TTL
:
609 if (sk
->sk_type
== SOCK_STREAM
)
615 if (val
< 0 || val
> 255)
619 case IP_MULTICAST_LOOP
:
622 inet
->mc_loop
= !!val
;
624 case IP_MULTICAST_IF
:
626 struct ip_mreqn mreq
;
627 struct net_device
*dev
= NULL
;
629 if (sk
->sk_type
== SOCK_STREAM
)
632 * Check the arguments are allowable
635 if (optlen
< sizeof(struct in_addr
))
639 if (optlen
>= sizeof(struct ip_mreqn
)) {
640 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
643 memset(&mreq
, 0, sizeof(mreq
));
644 if (optlen
>= sizeof(struct in_addr
) &&
645 copy_from_user(&mreq
.imr_address
, optval
,
646 sizeof(struct in_addr
)))
650 if (!mreq
.imr_ifindex
) {
651 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
657 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
659 mreq
.imr_ifindex
= dev
->ifindex
;
661 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
664 err
= -EADDRNOTAVAIL
;
670 if (sk
->sk_bound_dev_if
&&
671 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
674 inet
->mc_index
= mreq
.imr_ifindex
;
675 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
680 case IP_ADD_MEMBERSHIP
:
681 case IP_DROP_MEMBERSHIP
:
683 struct ip_mreqn mreq
;
686 if (inet_sk(sk
)->is_icsk
)
689 if (optlen
< sizeof(struct ip_mreq
))
692 if (optlen
>= sizeof(struct ip_mreqn
)) {
693 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
696 memset(&mreq
, 0, sizeof(mreq
));
697 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
701 if (optname
== IP_ADD_MEMBERSHIP
)
702 err
= ip_mc_join_group(sk
, &mreq
);
704 err
= ip_mc_leave_group(sk
, &mreq
);
709 struct ip_msfilter
*msf
;
711 if (optlen
< IP_MSFILTER_SIZE(0))
713 if (optlen
> sysctl_optmem_max
) {
717 msf
= kmalloc(optlen
, GFP_KERNEL
);
723 if (copy_from_user(msf
, optval
, optlen
)) {
727 /* numsrc >= (1G-4) overflow in 32 bits */
728 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
729 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
734 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
739 err
= ip_mc_msfilter(sk
, msf
, 0);
743 case IP_BLOCK_SOURCE
:
744 case IP_UNBLOCK_SOURCE
:
745 case IP_ADD_SOURCE_MEMBERSHIP
:
746 case IP_DROP_SOURCE_MEMBERSHIP
:
748 struct ip_mreq_source mreqs
;
751 if (optlen
!= sizeof(struct ip_mreq_source
))
753 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
757 if (optname
== IP_BLOCK_SOURCE
) {
758 omode
= MCAST_EXCLUDE
;
760 } else if (optname
== IP_UNBLOCK_SOURCE
) {
761 omode
= MCAST_EXCLUDE
;
763 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
764 struct ip_mreqn mreq
;
766 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
767 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
768 mreq
.imr_ifindex
= 0;
769 err
= ip_mc_join_group(sk
, &mreq
);
770 if (err
&& err
!= -EADDRINUSE
)
772 omode
= MCAST_INCLUDE
;
774 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
775 omode
= MCAST_INCLUDE
;
778 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
781 case MCAST_JOIN_GROUP
:
782 case MCAST_LEAVE_GROUP
:
784 struct group_req greq
;
785 struct sockaddr_in
*psin
;
786 struct ip_mreqn mreq
;
788 if (optlen
< sizeof(struct group_req
))
791 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
793 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
794 if (psin
->sin_family
!= AF_INET
)
796 memset(&mreq
, 0, sizeof(mreq
));
797 mreq
.imr_multiaddr
= psin
->sin_addr
;
798 mreq
.imr_ifindex
= greq
.gr_interface
;
800 if (optname
== MCAST_JOIN_GROUP
)
801 err
= ip_mc_join_group(sk
, &mreq
);
803 err
= ip_mc_leave_group(sk
, &mreq
);
806 case MCAST_JOIN_SOURCE_GROUP
:
807 case MCAST_LEAVE_SOURCE_GROUP
:
808 case MCAST_BLOCK_SOURCE
:
809 case MCAST_UNBLOCK_SOURCE
:
811 struct group_source_req greqs
;
812 struct ip_mreq_source mreqs
;
813 struct sockaddr_in
*psin
;
816 if (optlen
!= sizeof(struct group_source_req
))
818 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
822 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
823 greqs
.gsr_source
.ss_family
!= AF_INET
) {
824 err
= -EADDRNOTAVAIL
;
827 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
828 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
829 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
830 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
831 mreqs
.imr_interface
= 0; /* use index for mc_source */
833 if (optname
== MCAST_BLOCK_SOURCE
) {
834 omode
= MCAST_EXCLUDE
;
836 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
837 omode
= MCAST_EXCLUDE
;
839 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
840 struct ip_mreqn mreq
;
842 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
843 mreq
.imr_multiaddr
= psin
->sin_addr
;
844 mreq
.imr_address
.s_addr
= 0;
845 mreq
.imr_ifindex
= greqs
.gsr_interface
;
846 err
= ip_mc_join_group(sk
, &mreq
);
847 if (err
&& err
!= -EADDRINUSE
)
849 greqs
.gsr_interface
= mreq
.imr_ifindex
;
850 omode
= MCAST_INCLUDE
;
852 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
853 omode
= MCAST_INCLUDE
;
856 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
857 greqs
.gsr_interface
);
862 struct sockaddr_in
*psin
;
863 struct ip_msfilter
*msf
= NULL
;
864 struct group_filter
*gsf
= NULL
;
865 int msize
, i
, ifindex
;
867 if (optlen
< GROUP_FILTER_SIZE(0))
869 if (optlen
> sysctl_optmem_max
) {
873 gsf
= kmalloc(optlen
, GFP_KERNEL
);
879 if (copy_from_user(gsf
, optval
, optlen
))
882 /* numsrc >= (4G-140)/128 overflow in 32 bits */
883 if (gsf
->gf_numsrc
>= 0x1ffffff ||
884 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
888 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
892 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
893 msf
= kmalloc(msize
, GFP_KERNEL
);
898 ifindex
= gsf
->gf_interface
;
899 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
900 if (psin
->sin_family
!= AF_INET
) {
901 err
= -EADDRNOTAVAIL
;
904 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
905 msf
->imsf_interface
= 0;
906 msf
->imsf_fmode
= gsf
->gf_fmode
;
907 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
908 err
= -EADDRNOTAVAIL
;
909 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
910 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
912 if (psin
->sin_family
!= AF_INET
)
914 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
919 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
925 case IP_MULTICAST_ALL
:
928 if (val
!= 0 && val
!= 1)
932 case IP_ROUTER_ALERT
:
933 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
939 inet
->freebind
= !!val
;
942 case IP_IPSEC_POLICY
:
945 if (!capable(CAP_NET_ADMIN
))
947 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
951 if (!capable(CAP_NET_ADMIN
)) {
957 inet
->transparent
= !!val
;
963 if (val
< 0 || val
> 255)
981 * ip_queue_rcv_skb - Queue an skb into sock receive queue
985 * Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option
986 * is not set, we drop skb dst entry now, while dst cache line is hot.
988 int ip_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
990 if (!(inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
))
992 return sock_queue_rcv_skb(sk
, skb
);
994 EXPORT_SYMBOL(ip_queue_rcv_skb
);
996 int ip_setsockopt(struct sock
*sk
, int level
,
997 int optname
, char __user
*optval
, unsigned int optlen
)
1001 if (level
!= SOL_IP
)
1002 return -ENOPROTOOPT
;
1004 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1005 #ifdef CONFIG_NETFILTER
1006 /* we need to exclude all possible ENOPROTOOPTs except default case */
1007 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1008 optname
!= IP_IPSEC_POLICY
&&
1009 optname
!= IP_XFRM_POLICY
&&
1010 !ip_mroute_opt(optname
)) {
1012 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1018 EXPORT_SYMBOL(ip_setsockopt
);
1020 #ifdef CONFIG_COMPAT
1021 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1022 char __user
*optval
, unsigned int optlen
)
1026 if (level
!= SOL_IP
)
1027 return -ENOPROTOOPT
;
1029 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1030 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1033 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1034 #ifdef CONFIG_NETFILTER
1035 /* we need to exclude all possible ENOPROTOOPTs except default case */
1036 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1037 optname
!= IP_IPSEC_POLICY
&&
1038 optname
!= IP_XFRM_POLICY
&&
1039 !ip_mroute_opt(optname
)) {
1041 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1048 EXPORT_SYMBOL(compat_ip_setsockopt
);
1052 * Get the options. Note for future reference. The GET of IP options gets
1053 * the _received_ ones. The set sets the _sent_ ones.
1056 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1057 char __user
*optval
, int __user
*optlen
)
1059 struct inet_sock
*inet
= inet_sk(sk
);
1063 if (level
!= SOL_IP
)
1066 if (ip_mroute_opt(optname
))
1067 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1069 if (get_user(len
, optlen
))
1079 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1080 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
1083 memcpy(optbuf
, inet
->opt
,
1084 sizeof(struct ip_options
)+
1088 if (opt
->optlen
== 0)
1089 return put_user(0, optlen
);
1091 ip_options_undo(opt
);
1093 len
= min_t(unsigned int, len
, opt
->optlen
);
1094 if (put_user(len
, optlen
))
1096 if (copy_to_user(optval
, opt
->__data
, len
))
1101 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1104 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1107 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1110 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1113 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1116 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1118 case IP_RECVORIGDSTADDR
:
1119 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1125 val
= (inet
->uc_ttl
== -1 ?
1126 sysctl_ip_default_ttl
:
1130 val
= inet
->hdrincl
;
1133 val
= inet
->nodefrag
;
1135 case IP_MTU_DISCOVER
:
1136 val
= inet
->pmtudisc
;
1140 struct dst_entry
*dst
;
1142 dst
= sk_dst_get(sk
);
1154 val
= inet
->recverr
;
1156 case IP_MULTICAST_TTL
:
1159 case IP_MULTICAST_LOOP
:
1160 val
= inet
->mc_loop
;
1162 case IP_MULTICAST_IF
:
1164 struct in_addr addr
;
1165 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1166 addr
.s_addr
= inet
->mc_addr
;
1169 if (put_user(len
, optlen
))
1171 if (copy_to_user(optval
, &addr
, len
))
1177 struct ip_msfilter msf
;
1180 if (len
< IP_MSFILTER_SIZE(0)) {
1184 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1188 err
= ip_mc_msfget(sk
, &msf
,
1189 (struct ip_msfilter __user
*)optval
, optlen
);
1193 case MCAST_MSFILTER
:
1195 struct group_filter gsf
;
1198 if (len
< GROUP_FILTER_SIZE(0)) {
1202 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1206 err
= ip_mc_gsfget(sk
, &gsf
,
1207 (struct group_filter __user
*)optval
,
1212 case IP_MULTICAST_ALL
:
1221 if (sk
->sk_type
!= SOCK_STREAM
)
1222 return -ENOPROTOOPT
;
1224 msg
.msg_control
= optval
;
1225 msg
.msg_controllen
= len
;
1228 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1229 struct in_pktinfo info
;
1231 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1232 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1233 info
.ipi_ifindex
= inet
->mc_index
;
1234 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1236 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1237 int hlim
= inet
->mc_ttl
;
1238 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1240 len
-= msg
.msg_controllen
;
1241 return put_user(len
, optlen
);
1244 val
= inet
->freebind
;
1246 case IP_TRANSPARENT
:
1247 val
= inet
->transparent
;
1250 val
= inet
->min_ttl
;
1254 return -ENOPROTOOPT
;
1258 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1259 unsigned char ucval
= (unsigned char)val
;
1261 if (put_user(len
, optlen
))
1263 if (copy_to_user(optval
, &ucval
, 1))
1266 len
= min_t(unsigned int, sizeof(int), len
);
1267 if (put_user(len
, optlen
))
1269 if (copy_to_user(optval
, &val
, len
))
1275 int ip_getsockopt(struct sock
*sk
, int level
,
1276 int optname
, char __user
*optval
, int __user
*optlen
)
1280 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1281 #ifdef CONFIG_NETFILTER
1282 /* we need to exclude all possible ENOPROTOOPTs except default case */
1283 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1284 !ip_mroute_opt(optname
)) {
1287 if (get_user(len
, optlen
))
1291 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1295 err
= put_user(len
, optlen
);
1301 EXPORT_SYMBOL(ip_getsockopt
);
1303 #ifdef CONFIG_COMPAT
1304 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1305 char __user
*optval
, int __user
*optlen
)
1309 if (optname
== MCAST_MSFILTER
)
1310 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1313 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1315 #ifdef CONFIG_NETFILTER
1316 /* we need to exclude all possible ENOPROTOOPTs except default case */
1317 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1318 !ip_mroute_opt(optname
)) {
1321 if (get_user(len
, optlen
))
1325 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1328 err
= put_user(len
, optlen
);
1334 EXPORT_SYMBOL(compat_ip_getsockopt
);