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 __rcu
*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
;
257 struct ip_ra_chain __rcu
**rap
;
259 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
262 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
264 spin_lock_bh(&ip_ra_lock
);
265 for (rap
= &ip_ra_chain
;
266 (ra
= rcu_dereference_protected(*rap
,
267 lockdep_is_held(&ip_ra_lock
))) != NULL
;
271 spin_unlock_bh(&ip_ra_lock
);
275 /* dont let ip_call_ra_chain() use sk again */
277 rcu_assign_pointer(*rap
, ra
->next
);
278 spin_unlock_bh(&ip_ra_lock
);
283 * Delay sock_put(sk) and kfree(ra) after one rcu grace
284 * period. This guarantee ip_call_ra_chain() dont need
285 * to mess with socket refcounts.
288 call_rcu(&ra
->rcu
, ip_ra_destroy_rcu
);
292 if (new_ra
== NULL
) {
293 spin_unlock_bh(&ip_ra_lock
);
297 new_ra
->destructor
= destructor
;
300 rcu_assign_pointer(*rap
, new_ra
);
302 spin_unlock_bh(&ip_ra_lock
);
307 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
308 __be16 port
, u32 info
, u8
*payload
)
310 struct sock_exterr_skb
*serr
;
312 skb
= skb_clone(skb
, GFP_ATOMIC
);
316 serr
= SKB_EXT_ERR(skb
);
317 serr
->ee
.ee_errno
= err
;
318 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
319 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
320 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
322 serr
->ee
.ee_info
= info
;
323 serr
->ee
.ee_data
= 0;
324 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
325 skb_network_header(skb
);
328 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
329 skb_reset_transport_header(skb
);
330 if (sock_queue_err_skb(sk
, skb
) == 0)
336 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
338 struct inet_sock
*inet
= inet_sk(sk
);
339 struct sock_exterr_skb
*serr
;
346 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
350 skb_put(skb
, sizeof(struct iphdr
));
351 skb_reset_network_header(skb
);
355 serr
= SKB_EXT_ERR(skb
);
356 serr
->ee
.ee_errno
= err
;
357 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
358 serr
->ee
.ee_type
= 0;
359 serr
->ee
.ee_code
= 0;
361 serr
->ee
.ee_info
= info
;
362 serr
->ee
.ee_data
= 0;
363 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
366 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
367 skb_reset_transport_header(skb
);
369 if (sock_queue_err_skb(sk
, skb
))
374 * Handle MSG_ERRQUEUE
376 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
378 struct sock_exterr_skb
*serr
;
379 struct sk_buff
*skb
, *skb2
;
380 struct sockaddr_in
*sin
;
382 struct sock_extended_err ee
;
383 struct sockaddr_in offender
;
389 skb
= skb_dequeue(&sk
->sk_error_queue
);
395 msg
->msg_flags
|= MSG_TRUNC
;
398 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
402 sock_recv_timestamp(msg
, sk
, skb
);
404 serr
= SKB_EXT_ERR(skb
);
406 sin
= (struct sockaddr_in
*)msg
->msg_name
;
408 sin
->sin_family
= AF_INET
;
409 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
411 sin
->sin_port
= serr
->port
;
412 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
415 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
416 sin
= &errhdr
.offender
;
417 sin
->sin_family
= AF_UNSPEC
;
418 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
419 struct inet_sock
*inet
= inet_sk(sk
);
421 sin
->sin_family
= AF_INET
;
422 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
424 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
425 if (inet
->cmsg_flags
)
426 ip_cmsg_recv(msg
, skb
);
429 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
431 /* Now we could try to dump offended packet options */
433 msg
->msg_flags
|= MSG_ERRQUEUE
;
436 /* Reset and regenerate socket error */
437 spin_lock_bh(&sk
->sk_error_queue
.lock
);
439 skb2
= skb_peek(&sk
->sk_error_queue
);
441 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
442 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
443 sk
->sk_error_report(sk
);
445 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
455 * Socket option code for IP. This is the end of the line after any
456 * TCP,UDP etc options on an IP socket.
459 static int do_ip_setsockopt(struct sock
*sk
, int level
,
460 int optname
, char __user
*optval
, unsigned int optlen
)
462 struct inet_sock
*inet
= inet_sk(sk
);
465 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
466 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
467 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
468 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
469 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
470 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
471 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
) |
472 (1<<IP_MINTTL
) | (1<<IP_NODEFRAG
))) ||
473 optname
== IP_MULTICAST_TTL
||
474 optname
== IP_MULTICAST_ALL
||
475 optname
== IP_MULTICAST_LOOP
||
476 optname
== IP_RECVORIGDSTADDR
) {
477 if (optlen
>= sizeof(int)) {
478 if (get_user(val
, (int __user
*) optval
))
480 } else if (optlen
>= sizeof(char)) {
483 if (get_user(ucval
, (unsigned char __user
*) optval
))
489 /* If optlen==0, it is equivalent to val == 0 */
491 if (ip_mroute_opt(optname
))
492 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
500 struct ip_options
*opt
= NULL
;
503 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
508 struct inet_connection_sock
*icsk
= inet_csk(sk
);
509 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
510 if (sk
->sk_family
== PF_INET
||
511 (!((1 << sk
->sk_state
) &
512 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
513 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
516 icsk
->icsk_ext_hdr_len
-= inet
->opt
->optlen
;
518 icsk
->icsk_ext_hdr_len
+= opt
->optlen
;
519 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
520 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
524 opt
= xchg(&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
) {
573 val
|= inet
->tos
& 3;
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
;
628 case IP_MULTICAST_IF
:
630 struct ip_mreqn mreq
;
631 struct net_device
*dev
= NULL
;
633 if (sk
->sk_type
== SOCK_STREAM
)
636 * Check the arguments are allowable
639 if (optlen
< sizeof(struct in_addr
))
643 if (optlen
>= sizeof(struct ip_mreqn
)) {
644 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
647 memset(&mreq
, 0, sizeof(mreq
));
648 if (optlen
>= sizeof(struct in_addr
) &&
649 copy_from_user(&mreq
.imr_address
, optval
,
650 sizeof(struct in_addr
)))
654 if (!mreq
.imr_ifindex
) {
655 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
661 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
663 mreq
.imr_ifindex
= dev
->ifindex
;
665 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
668 err
= -EADDRNOTAVAIL
;
674 if (sk
->sk_bound_dev_if
&&
675 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
678 inet
->mc_index
= mreq
.imr_ifindex
;
679 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
684 case IP_ADD_MEMBERSHIP
:
685 case IP_DROP_MEMBERSHIP
:
687 struct ip_mreqn mreq
;
690 if (inet_sk(sk
)->is_icsk
)
693 if (optlen
< sizeof(struct ip_mreq
))
696 if (optlen
>= sizeof(struct ip_mreqn
)) {
697 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
700 memset(&mreq
, 0, sizeof(mreq
));
701 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
705 if (optname
== IP_ADD_MEMBERSHIP
)
706 err
= ip_mc_join_group(sk
, &mreq
);
708 err
= ip_mc_leave_group(sk
, &mreq
);
713 struct ip_msfilter
*msf
;
715 if (optlen
< IP_MSFILTER_SIZE(0))
717 if (optlen
> sysctl_optmem_max
) {
721 msf
= kmalloc(optlen
, GFP_KERNEL
);
727 if (copy_from_user(msf
, optval
, optlen
)) {
731 /* numsrc >= (1G-4) overflow in 32 bits */
732 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
733 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
738 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
743 err
= ip_mc_msfilter(sk
, msf
, 0);
747 case IP_BLOCK_SOURCE
:
748 case IP_UNBLOCK_SOURCE
:
749 case IP_ADD_SOURCE_MEMBERSHIP
:
750 case IP_DROP_SOURCE_MEMBERSHIP
:
752 struct ip_mreq_source mreqs
;
755 if (optlen
!= sizeof(struct ip_mreq_source
))
757 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
761 if (optname
== IP_BLOCK_SOURCE
) {
762 omode
= MCAST_EXCLUDE
;
764 } else if (optname
== IP_UNBLOCK_SOURCE
) {
765 omode
= MCAST_EXCLUDE
;
767 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
768 struct ip_mreqn mreq
;
770 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
771 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
772 mreq
.imr_ifindex
= 0;
773 err
= ip_mc_join_group(sk
, &mreq
);
774 if (err
&& err
!= -EADDRINUSE
)
776 omode
= MCAST_INCLUDE
;
778 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
779 omode
= MCAST_INCLUDE
;
782 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
785 case MCAST_JOIN_GROUP
:
786 case MCAST_LEAVE_GROUP
:
788 struct group_req greq
;
789 struct sockaddr_in
*psin
;
790 struct ip_mreqn mreq
;
792 if (optlen
< sizeof(struct group_req
))
795 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
797 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
798 if (psin
->sin_family
!= AF_INET
)
800 memset(&mreq
, 0, sizeof(mreq
));
801 mreq
.imr_multiaddr
= psin
->sin_addr
;
802 mreq
.imr_ifindex
= greq
.gr_interface
;
804 if (optname
== MCAST_JOIN_GROUP
)
805 err
= ip_mc_join_group(sk
, &mreq
);
807 err
= ip_mc_leave_group(sk
, &mreq
);
810 case MCAST_JOIN_SOURCE_GROUP
:
811 case MCAST_LEAVE_SOURCE_GROUP
:
812 case MCAST_BLOCK_SOURCE
:
813 case MCAST_UNBLOCK_SOURCE
:
815 struct group_source_req greqs
;
816 struct ip_mreq_source mreqs
;
817 struct sockaddr_in
*psin
;
820 if (optlen
!= sizeof(struct group_source_req
))
822 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
826 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
827 greqs
.gsr_source
.ss_family
!= AF_INET
) {
828 err
= -EADDRNOTAVAIL
;
831 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
832 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
833 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
834 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
835 mreqs
.imr_interface
= 0; /* use index for mc_source */
837 if (optname
== MCAST_BLOCK_SOURCE
) {
838 omode
= MCAST_EXCLUDE
;
840 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
841 omode
= MCAST_EXCLUDE
;
843 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
844 struct ip_mreqn mreq
;
846 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
847 mreq
.imr_multiaddr
= psin
->sin_addr
;
848 mreq
.imr_address
.s_addr
= 0;
849 mreq
.imr_ifindex
= greqs
.gsr_interface
;
850 err
= ip_mc_join_group(sk
, &mreq
);
851 if (err
&& err
!= -EADDRINUSE
)
853 greqs
.gsr_interface
= mreq
.imr_ifindex
;
854 omode
= MCAST_INCLUDE
;
856 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
857 omode
= MCAST_INCLUDE
;
860 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
861 greqs
.gsr_interface
);
866 struct sockaddr_in
*psin
;
867 struct ip_msfilter
*msf
= NULL
;
868 struct group_filter
*gsf
= NULL
;
869 int msize
, i
, ifindex
;
871 if (optlen
< GROUP_FILTER_SIZE(0))
873 if (optlen
> sysctl_optmem_max
) {
877 gsf
= kmalloc(optlen
, GFP_KERNEL
);
883 if (copy_from_user(gsf
, optval
, optlen
))
886 /* numsrc >= (4G-140)/128 overflow in 32 bits */
887 if (gsf
->gf_numsrc
>= 0x1ffffff ||
888 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
892 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
896 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
897 msf
= kmalloc(msize
, GFP_KERNEL
);
902 ifindex
= gsf
->gf_interface
;
903 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
904 if (psin
->sin_family
!= AF_INET
) {
905 err
= -EADDRNOTAVAIL
;
908 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
909 msf
->imsf_interface
= 0;
910 msf
->imsf_fmode
= gsf
->gf_fmode
;
911 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
912 err
= -EADDRNOTAVAIL
;
913 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
914 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
916 if (psin
->sin_family
!= AF_INET
)
918 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
923 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
929 case IP_MULTICAST_ALL
:
932 if (val
!= 0 && val
!= 1)
936 case IP_ROUTER_ALERT
:
937 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
943 inet
->freebind
= !!val
;
946 case IP_IPSEC_POLICY
:
949 if (!capable(CAP_NET_ADMIN
))
951 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
955 if (!capable(CAP_NET_ADMIN
)) {
961 inet
->transparent
= !!val
;
967 if (val
< 0 || val
> 255)
985 * ip_queue_rcv_skb - Queue an skb into sock receive queue
989 * Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option
990 * is not set, we drop skb dst entry now, while dst cache line is hot.
992 int ip_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
994 if (!(inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
))
996 return sock_queue_rcv_skb(sk
, skb
);
998 EXPORT_SYMBOL(ip_queue_rcv_skb
);
1000 int ip_setsockopt(struct sock
*sk
, int level
,
1001 int optname
, char __user
*optval
, unsigned int optlen
)
1005 if (level
!= SOL_IP
)
1006 return -ENOPROTOOPT
;
1008 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1009 #ifdef CONFIG_NETFILTER
1010 /* we need to exclude all possible ENOPROTOOPTs except default case */
1011 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1012 optname
!= IP_IPSEC_POLICY
&&
1013 optname
!= IP_XFRM_POLICY
&&
1014 !ip_mroute_opt(optname
)) {
1016 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
1022 EXPORT_SYMBOL(ip_setsockopt
);
1024 #ifdef CONFIG_COMPAT
1025 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
1026 char __user
*optval
, unsigned int optlen
)
1030 if (level
!= SOL_IP
)
1031 return -ENOPROTOOPT
;
1033 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1034 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1037 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1038 #ifdef CONFIG_NETFILTER
1039 /* we need to exclude all possible ENOPROTOOPTs except default case */
1040 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1041 optname
!= IP_IPSEC_POLICY
&&
1042 optname
!= IP_XFRM_POLICY
&&
1043 !ip_mroute_opt(optname
)) {
1045 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1052 EXPORT_SYMBOL(compat_ip_setsockopt
);
1056 * Get the options. Note for future reference. The GET of IP options gets
1057 * the _received_ ones. The set sets the _sent_ ones.
1060 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1061 char __user
*optval
, int __user
*optlen
)
1063 struct inet_sock
*inet
= inet_sk(sk
);
1067 if (level
!= SOL_IP
)
1070 if (ip_mroute_opt(optname
))
1071 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1073 if (get_user(len
, optlen
))
1083 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1084 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
1087 memcpy(optbuf
, inet
->opt
,
1088 sizeof(struct ip_options
)+
1092 if (opt
->optlen
== 0)
1093 return put_user(0, optlen
);
1095 ip_options_undo(opt
);
1097 len
= min_t(unsigned int, len
, opt
->optlen
);
1098 if (put_user(len
, optlen
))
1100 if (copy_to_user(optval
, opt
->__data
, len
))
1105 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1108 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1111 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1114 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1117 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1120 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1122 case IP_RECVORIGDSTADDR
:
1123 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1129 val
= (inet
->uc_ttl
== -1 ?
1130 sysctl_ip_default_ttl
:
1134 val
= inet
->hdrincl
;
1137 val
= inet
->nodefrag
;
1139 case IP_MTU_DISCOVER
:
1140 val
= inet
->pmtudisc
;
1144 struct dst_entry
*dst
;
1146 dst
= sk_dst_get(sk
);
1158 val
= inet
->recverr
;
1160 case IP_MULTICAST_TTL
:
1163 case IP_MULTICAST_LOOP
:
1164 val
= inet
->mc_loop
;
1166 case IP_MULTICAST_IF
:
1168 struct in_addr addr
;
1169 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1170 addr
.s_addr
= inet
->mc_addr
;
1173 if (put_user(len
, optlen
))
1175 if (copy_to_user(optval
, &addr
, len
))
1181 struct ip_msfilter msf
;
1184 if (len
< IP_MSFILTER_SIZE(0)) {
1188 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1192 err
= ip_mc_msfget(sk
, &msf
,
1193 (struct ip_msfilter __user
*)optval
, optlen
);
1197 case MCAST_MSFILTER
:
1199 struct group_filter gsf
;
1202 if (len
< GROUP_FILTER_SIZE(0)) {
1206 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1210 err
= ip_mc_gsfget(sk
, &gsf
,
1211 (struct group_filter __user
*)optval
,
1216 case IP_MULTICAST_ALL
:
1225 if (sk
->sk_type
!= SOCK_STREAM
)
1226 return -ENOPROTOOPT
;
1228 msg
.msg_control
= optval
;
1229 msg
.msg_controllen
= len
;
1232 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1233 struct in_pktinfo info
;
1235 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1236 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1237 info
.ipi_ifindex
= inet
->mc_index
;
1238 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1240 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1241 int hlim
= inet
->mc_ttl
;
1242 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1244 len
-= msg
.msg_controllen
;
1245 return put_user(len
, optlen
);
1248 val
= inet
->freebind
;
1250 case IP_TRANSPARENT
:
1251 val
= inet
->transparent
;
1254 val
= inet
->min_ttl
;
1258 return -ENOPROTOOPT
;
1262 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1263 unsigned char ucval
= (unsigned char)val
;
1265 if (put_user(len
, optlen
))
1267 if (copy_to_user(optval
, &ucval
, 1))
1270 len
= min_t(unsigned int, sizeof(int), len
);
1271 if (put_user(len
, optlen
))
1273 if (copy_to_user(optval
, &val
, len
))
1279 int ip_getsockopt(struct sock
*sk
, int level
,
1280 int optname
, char __user
*optval
, int __user
*optlen
)
1284 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1285 #ifdef CONFIG_NETFILTER
1286 /* we need to exclude all possible ENOPROTOOPTs except default case */
1287 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1288 !ip_mroute_opt(optname
)) {
1291 if (get_user(len
, optlen
))
1295 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1299 err
= put_user(len
, optlen
);
1305 EXPORT_SYMBOL(ip_getsockopt
);
1307 #ifdef CONFIG_COMPAT
1308 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1309 char __user
*optval
, int __user
*optlen
)
1313 if (optname
== MCAST_MSFILTER
)
1314 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1317 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1319 #ifdef CONFIG_NETFILTER
1320 /* we need to exclude all possible ENOPROTOOPTs except default case */
1321 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1322 !ip_mroute_opt(optname
)) {
1325 if (get_user(len
, optlen
))
1329 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1332 err
= put_user(len
, optlen
);
1338 EXPORT_SYMBOL(compat_ip_getsockopt
);