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 DEFINE_RWLOCK(ip_ra_lock
);
244 int ip_ra_control(struct sock
*sk
, unsigned char on
,
245 void (*destructor
)(struct sock
*))
247 struct ip_ra_chain
*ra
, *new_ra
, **rap
;
249 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
== IPPROTO_RAW
)
252 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
254 write_lock_bh(&ip_ra_lock
);
255 for (rap
= &ip_ra_chain
; (ra
= *rap
) != NULL
; rap
= &ra
->next
) {
258 write_unlock_bh(&ip_ra_lock
);
263 write_unlock_bh(&ip_ra_lock
);
272 if (new_ra
== NULL
) {
273 write_unlock_bh(&ip_ra_lock
);
277 new_ra
->destructor
= destructor
;
282 write_unlock_bh(&ip_ra_lock
);
287 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
288 __be16 port
, u32 info
, u8
*payload
)
290 struct sock_exterr_skb
*serr
;
292 skb
= skb_clone(skb
, GFP_ATOMIC
);
296 serr
= SKB_EXT_ERR(skb
);
297 serr
->ee
.ee_errno
= err
;
298 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
299 serr
->ee
.ee_type
= icmp_hdr(skb
)->type
;
300 serr
->ee
.ee_code
= icmp_hdr(skb
)->code
;
302 serr
->ee
.ee_info
= info
;
303 serr
->ee
.ee_data
= 0;
304 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(icmp_hdr(skb
) + 1))->daddr
) -
305 skb_network_header(skb
);
308 if (skb_pull(skb
, payload
- skb
->data
) != NULL
) {
309 skb_reset_transport_header(skb
);
310 if (sock_queue_err_skb(sk
, skb
) == 0)
316 void ip_local_error(struct sock
*sk
, int err
, __be32 daddr
, __be16 port
, u32 info
)
318 struct inet_sock
*inet
= inet_sk(sk
);
319 struct sock_exterr_skb
*serr
;
326 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
330 skb_put(skb
, sizeof(struct iphdr
));
331 skb_reset_network_header(skb
);
335 serr
= SKB_EXT_ERR(skb
);
336 serr
->ee
.ee_errno
= err
;
337 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
338 serr
->ee
.ee_type
= 0;
339 serr
->ee
.ee_code
= 0;
341 serr
->ee
.ee_info
= info
;
342 serr
->ee
.ee_data
= 0;
343 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb_network_header(skb
);
346 __skb_pull(skb
, skb_tail_pointer(skb
) - skb
->data
);
347 skb_reset_transport_header(skb
);
349 if (sock_queue_err_skb(sk
, skb
))
354 * Handle MSG_ERRQUEUE
356 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
358 struct sock_exterr_skb
*serr
;
359 struct sk_buff
*skb
, *skb2
;
360 struct sockaddr_in
*sin
;
362 struct sock_extended_err ee
;
363 struct sockaddr_in offender
;
369 skb
= skb_dequeue(&sk
->sk_error_queue
);
375 msg
->msg_flags
|= MSG_TRUNC
;
378 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
382 sock_recv_timestamp(msg
, sk
, skb
);
384 serr
= SKB_EXT_ERR(skb
);
386 sin
= (struct sockaddr_in
*)msg
->msg_name
;
388 sin
->sin_family
= AF_INET
;
389 sin
->sin_addr
.s_addr
= *(__be32
*)(skb_network_header(skb
) +
391 sin
->sin_port
= serr
->port
;
392 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
395 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
396 sin
= &errhdr
.offender
;
397 sin
->sin_family
= AF_UNSPEC
;
398 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
399 struct inet_sock
*inet
= inet_sk(sk
);
401 sin
->sin_family
= AF_INET
;
402 sin
->sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
404 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
405 if (inet
->cmsg_flags
)
406 ip_cmsg_recv(msg
, skb
);
409 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
411 /* Now we could try to dump offended packet options */
413 msg
->msg_flags
|= MSG_ERRQUEUE
;
416 /* Reset and regenerate socket error */
417 spin_lock_bh(&sk
->sk_error_queue
.lock
);
419 skb2
= skb_peek(&sk
->sk_error_queue
);
421 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
422 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
423 sk
->sk_error_report(sk
);
425 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
435 * Socket option code for IP. This is the end of the line after any
436 * TCP,UDP etc options on an IP socket.
439 static int do_ip_setsockopt(struct sock
*sk
, int level
,
440 int optname
, char __user
*optval
, unsigned int optlen
)
442 struct inet_sock
*inet
= inet_sk(sk
);
445 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
446 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
447 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
448 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
449 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
450 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
) |
451 (1<<IP_PASSSEC
) | (1<<IP_TRANSPARENT
) |
453 optname
== IP_MULTICAST_TTL
||
454 optname
== IP_MULTICAST_ALL
||
455 optname
== IP_MULTICAST_LOOP
||
456 optname
== IP_RECVORIGDSTADDR
) {
457 if (optlen
>= sizeof(int)) {
458 if (get_user(val
, (int __user
*) optval
))
460 } else if (optlen
>= sizeof(char)) {
463 if (get_user(ucval
, (unsigned char __user
*) optval
))
469 /* If optlen==0, it is equivalent to val == 0 */
471 if (ip_mroute_opt(optname
))
472 return ip_mroute_setsockopt(sk
, optname
, optval
, optlen
);
480 struct ip_options
*opt
= NULL
;
483 err
= ip_options_get_from_user(sock_net(sk
), &opt
,
488 struct inet_connection_sock
*icsk
= inet_csk(sk
);
489 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
490 if (sk
->sk_family
== PF_INET
||
491 (!((1 << sk
->sk_state
) &
492 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
493 inet
->inet_daddr
!= LOOPBACK4_IPV6
)) {
496 icsk
->icsk_ext_hdr_len
-= inet
->opt
->optlen
;
498 icsk
->icsk_ext_hdr_len
+= opt
->optlen
;
499 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
500 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
504 opt
= xchg(&inet
->opt
, opt
);
510 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
512 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
516 inet
->cmsg_flags
|= IP_CMSG_TTL
;
518 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
522 inet
->cmsg_flags
|= IP_CMSG_TOS
;
524 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
528 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
530 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
534 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
536 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
540 inet
->cmsg_flags
|= IP_CMSG_PASSSEC
;
542 inet
->cmsg_flags
&= ~IP_CMSG_PASSSEC
;
544 case IP_RECVORIGDSTADDR
:
546 inet
->cmsg_flags
|= IP_CMSG_ORIGDSTADDR
;
548 inet
->cmsg_flags
&= ~IP_CMSG_ORIGDSTADDR
;
550 case IP_TOS
: /* This sets both TOS and Precedence */
551 if (sk
->sk_type
== SOCK_STREAM
) {
553 val
|= inet
->tos
& 3;
555 if (inet
->tos
!= val
) {
557 sk
->sk_priority
= rt_tos2priority(val
);
564 if (val
!= -1 && (val
< 0 || val
> 255))
569 if (sk
->sk_type
!= SOCK_RAW
) {
573 inet
->hdrincl
= val
? 1 : 0;
575 case IP_MTU_DISCOVER
:
576 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
578 inet
->pmtudisc
= val
;
581 inet
->recverr
= !!val
;
583 skb_queue_purge(&sk
->sk_error_queue
);
585 case IP_MULTICAST_TTL
:
586 if (sk
->sk_type
== SOCK_STREAM
)
592 if (val
< 0 || val
> 255)
596 case IP_MULTICAST_LOOP
:
599 inet
->mc_loop
= !!val
;
601 case IP_MULTICAST_IF
:
603 struct ip_mreqn mreq
;
604 struct net_device
*dev
= NULL
;
606 if (sk
->sk_type
== SOCK_STREAM
)
609 * Check the arguments are allowable
612 if (optlen
< sizeof(struct in_addr
))
616 if (optlen
>= sizeof(struct ip_mreqn
)) {
617 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
620 memset(&mreq
, 0, sizeof(mreq
));
621 if (optlen
>= sizeof(struct in_addr
) &&
622 copy_from_user(&mreq
.imr_address
, optval
,
623 sizeof(struct in_addr
)))
627 if (!mreq
.imr_ifindex
) {
628 if (mreq
.imr_address
.s_addr
== htonl(INADDR_ANY
)) {
634 dev
= ip_dev_find(sock_net(sk
), mreq
.imr_address
.s_addr
);
636 mreq
.imr_ifindex
= dev
->ifindex
;
638 dev
= dev_get_by_index(sock_net(sk
), mreq
.imr_ifindex
);
641 err
= -EADDRNOTAVAIL
;
647 if (sk
->sk_bound_dev_if
&&
648 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
651 inet
->mc_index
= mreq
.imr_ifindex
;
652 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
657 case IP_ADD_MEMBERSHIP
:
658 case IP_DROP_MEMBERSHIP
:
660 struct ip_mreqn mreq
;
663 if (inet_sk(sk
)->is_icsk
)
666 if (optlen
< sizeof(struct ip_mreq
))
669 if (optlen
>= sizeof(struct ip_mreqn
)) {
670 if (copy_from_user(&mreq
, optval
, sizeof(mreq
)))
673 memset(&mreq
, 0, sizeof(mreq
));
674 if (copy_from_user(&mreq
, optval
, sizeof(struct ip_mreq
)))
678 if (optname
== IP_ADD_MEMBERSHIP
)
679 err
= ip_mc_join_group(sk
, &mreq
);
681 err
= ip_mc_leave_group(sk
, &mreq
);
686 struct ip_msfilter
*msf
;
688 if (optlen
< IP_MSFILTER_SIZE(0))
690 if (optlen
> sysctl_optmem_max
) {
694 msf
= kmalloc(optlen
, GFP_KERNEL
);
700 if (copy_from_user(msf
, optval
, optlen
)) {
704 /* numsrc >= (1G-4) overflow in 32 bits */
705 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
706 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
711 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
716 err
= ip_mc_msfilter(sk
, msf
, 0);
720 case IP_BLOCK_SOURCE
:
721 case IP_UNBLOCK_SOURCE
:
722 case IP_ADD_SOURCE_MEMBERSHIP
:
723 case IP_DROP_SOURCE_MEMBERSHIP
:
725 struct ip_mreq_source mreqs
;
728 if (optlen
!= sizeof(struct ip_mreq_source
))
730 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
734 if (optname
== IP_BLOCK_SOURCE
) {
735 omode
= MCAST_EXCLUDE
;
737 } else if (optname
== IP_UNBLOCK_SOURCE
) {
738 omode
= MCAST_EXCLUDE
;
740 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
741 struct ip_mreqn mreq
;
743 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
744 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
745 mreq
.imr_ifindex
= 0;
746 err
= ip_mc_join_group(sk
, &mreq
);
747 if (err
&& err
!= -EADDRINUSE
)
749 omode
= MCAST_INCLUDE
;
751 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
752 omode
= MCAST_INCLUDE
;
755 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
758 case MCAST_JOIN_GROUP
:
759 case MCAST_LEAVE_GROUP
:
761 struct group_req greq
;
762 struct sockaddr_in
*psin
;
763 struct ip_mreqn mreq
;
765 if (optlen
< sizeof(struct group_req
))
768 if (copy_from_user(&greq
, optval
, sizeof(greq
)))
770 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
771 if (psin
->sin_family
!= AF_INET
)
773 memset(&mreq
, 0, sizeof(mreq
));
774 mreq
.imr_multiaddr
= psin
->sin_addr
;
775 mreq
.imr_ifindex
= greq
.gr_interface
;
777 if (optname
== MCAST_JOIN_GROUP
)
778 err
= ip_mc_join_group(sk
, &mreq
);
780 err
= ip_mc_leave_group(sk
, &mreq
);
783 case MCAST_JOIN_SOURCE_GROUP
:
784 case MCAST_LEAVE_SOURCE_GROUP
:
785 case MCAST_BLOCK_SOURCE
:
786 case MCAST_UNBLOCK_SOURCE
:
788 struct group_source_req greqs
;
789 struct ip_mreq_source mreqs
;
790 struct sockaddr_in
*psin
;
793 if (optlen
!= sizeof(struct group_source_req
))
795 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
799 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
800 greqs
.gsr_source
.ss_family
!= AF_INET
) {
801 err
= -EADDRNOTAVAIL
;
804 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
805 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
806 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
807 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
808 mreqs
.imr_interface
= 0; /* use index for mc_source */
810 if (optname
== MCAST_BLOCK_SOURCE
) {
811 omode
= MCAST_EXCLUDE
;
813 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
814 omode
= MCAST_EXCLUDE
;
816 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
817 struct ip_mreqn mreq
;
819 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
820 mreq
.imr_multiaddr
= psin
->sin_addr
;
821 mreq
.imr_address
.s_addr
= 0;
822 mreq
.imr_ifindex
= greqs
.gsr_interface
;
823 err
= ip_mc_join_group(sk
, &mreq
);
824 if (err
&& err
!= -EADDRINUSE
)
826 greqs
.gsr_interface
= mreq
.imr_ifindex
;
827 omode
= MCAST_INCLUDE
;
829 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
830 omode
= MCAST_INCLUDE
;
833 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
834 greqs
.gsr_interface
);
839 struct sockaddr_in
*psin
;
840 struct ip_msfilter
*msf
= NULL
;
841 struct group_filter
*gsf
= NULL
;
842 int msize
, i
, ifindex
;
844 if (optlen
< GROUP_FILTER_SIZE(0))
846 if (optlen
> sysctl_optmem_max
) {
850 gsf
= kmalloc(optlen
, GFP_KERNEL
);
856 if (copy_from_user(gsf
, optval
, optlen
))
859 /* numsrc >= (4G-140)/128 overflow in 32 bits */
860 if (gsf
->gf_numsrc
>= 0x1ffffff ||
861 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
865 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
869 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
870 msf
= kmalloc(msize
, GFP_KERNEL
);
875 ifindex
= gsf
->gf_interface
;
876 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
877 if (psin
->sin_family
!= AF_INET
) {
878 err
= -EADDRNOTAVAIL
;
881 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
882 msf
->imsf_interface
= 0;
883 msf
->imsf_fmode
= gsf
->gf_fmode
;
884 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
885 err
= -EADDRNOTAVAIL
;
886 for (i
= 0; i
< gsf
->gf_numsrc
; ++i
) {
887 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
889 if (psin
->sin_family
!= AF_INET
)
891 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
896 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
902 case IP_MULTICAST_ALL
:
905 if (val
!= 0 && val
!= 1)
909 case IP_ROUTER_ALERT
:
910 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
916 inet
->freebind
= !!val
;
919 case IP_IPSEC_POLICY
:
922 if (!capable(CAP_NET_ADMIN
))
924 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
928 if (!capable(CAP_NET_ADMIN
)) {
934 inet
->transparent
= !!val
;
940 if (val
< 0 || val
> 255)
958 * ip_queue_rcv_skb - Queue an skb into sock receive queue
962 * Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option
963 * is not set, we drop skb dst entry now, while dst cache line is hot.
965 int ip_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
967 if (!(inet_sk(sk
)->cmsg_flags
& IP_CMSG_PKTINFO
))
969 return sock_queue_rcv_skb(sk
, skb
);
971 EXPORT_SYMBOL(ip_queue_rcv_skb
);
973 int ip_setsockopt(struct sock
*sk
, int level
,
974 int optname
, char __user
*optval
, unsigned int optlen
)
981 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
982 #ifdef CONFIG_NETFILTER
983 /* we need to exclude all possible ENOPROTOOPTs except default case */
984 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
985 optname
!= IP_IPSEC_POLICY
&&
986 optname
!= IP_XFRM_POLICY
&&
987 !ip_mroute_opt(optname
)) {
989 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
, optlen
);
995 EXPORT_SYMBOL(ip_setsockopt
);
998 int compat_ip_setsockopt(struct sock
*sk
, int level
, int optname
,
999 char __user
*optval
, unsigned int optlen
)
1003 if (level
!= SOL_IP
)
1004 return -ENOPROTOOPT
;
1006 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
1007 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
1010 err
= do_ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
1011 #ifdef CONFIG_NETFILTER
1012 /* we need to exclude all possible ENOPROTOOPTs except default case */
1013 if (err
== -ENOPROTOOPT
&& optname
!= IP_HDRINCL
&&
1014 optname
!= IP_IPSEC_POLICY
&&
1015 optname
!= IP_XFRM_POLICY
&&
1016 !ip_mroute_opt(optname
)) {
1018 err
= compat_nf_setsockopt(sk
, PF_INET
, optname
,
1025 EXPORT_SYMBOL(compat_ip_setsockopt
);
1029 * Get the options. Note for future reference. The GET of IP options gets
1030 * the _received_ ones. The set sets the _sent_ ones.
1033 static int do_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1034 char __user
*optval
, int __user
*optlen
)
1036 struct inet_sock
*inet
= inet_sk(sk
);
1040 if (level
!= SOL_IP
)
1043 if (ip_mroute_opt(optname
))
1044 return ip_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1046 if (get_user(len
, optlen
))
1056 unsigned char optbuf
[sizeof(struct ip_options
)+40];
1057 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
1060 memcpy(optbuf
, inet
->opt
,
1061 sizeof(struct ip_options
)+
1065 if (opt
->optlen
== 0)
1066 return put_user(0, optlen
);
1068 ip_options_undo(opt
);
1070 len
= min_t(unsigned int, len
, opt
->optlen
);
1071 if (put_user(len
, optlen
))
1073 if (copy_to_user(optval
, opt
->__data
, len
))
1078 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
1081 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
1084 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
1087 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
1090 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
1093 val
= (inet
->cmsg_flags
& IP_CMSG_PASSSEC
) != 0;
1095 case IP_RECVORIGDSTADDR
:
1096 val
= (inet
->cmsg_flags
& IP_CMSG_ORIGDSTADDR
) != 0;
1102 val
= (inet
->uc_ttl
== -1 ?
1103 sysctl_ip_default_ttl
:
1107 val
= inet
->hdrincl
;
1109 case IP_MTU_DISCOVER
:
1110 val
= inet
->pmtudisc
;
1114 struct dst_entry
*dst
;
1116 dst
= sk_dst_get(sk
);
1128 val
= inet
->recverr
;
1130 case IP_MULTICAST_TTL
:
1133 case IP_MULTICAST_LOOP
:
1134 val
= inet
->mc_loop
;
1136 case IP_MULTICAST_IF
:
1138 struct in_addr addr
;
1139 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
1140 addr
.s_addr
= inet
->mc_addr
;
1143 if (put_user(len
, optlen
))
1145 if (copy_to_user(optval
, &addr
, len
))
1151 struct ip_msfilter msf
;
1154 if (len
< IP_MSFILTER_SIZE(0)) {
1158 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1162 err
= ip_mc_msfget(sk
, &msf
,
1163 (struct ip_msfilter __user
*)optval
, optlen
);
1167 case MCAST_MSFILTER
:
1169 struct group_filter gsf
;
1172 if (len
< GROUP_FILTER_SIZE(0)) {
1176 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1180 err
= ip_mc_gsfget(sk
, &gsf
,
1181 (struct group_filter __user
*)optval
,
1186 case IP_MULTICAST_ALL
:
1195 if (sk
->sk_type
!= SOCK_STREAM
)
1196 return -ENOPROTOOPT
;
1198 msg
.msg_control
= optval
;
1199 msg
.msg_controllen
= len
;
1202 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1203 struct in_pktinfo info
;
1205 info
.ipi_addr
.s_addr
= inet
->inet_rcv_saddr
;
1206 info
.ipi_spec_dst
.s_addr
= inet
->inet_rcv_saddr
;
1207 info
.ipi_ifindex
= inet
->mc_index
;
1208 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1210 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1211 int hlim
= inet
->mc_ttl
;
1212 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1214 len
-= msg
.msg_controllen
;
1215 return put_user(len
, optlen
);
1218 val
= inet
->freebind
;
1220 case IP_TRANSPARENT
:
1221 val
= inet
->transparent
;
1224 val
= inet
->min_ttl
;
1228 return -ENOPROTOOPT
;
1232 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
<= 255) {
1233 unsigned char ucval
= (unsigned char)val
;
1235 if (put_user(len
, optlen
))
1237 if (copy_to_user(optval
, &ucval
, 1))
1240 len
= min_t(unsigned int, sizeof(int), len
);
1241 if (put_user(len
, optlen
))
1243 if (copy_to_user(optval
, &val
, len
))
1249 int ip_getsockopt(struct sock
*sk
, int level
,
1250 int optname
, char __user
*optval
, int __user
*optlen
)
1254 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1255 #ifdef CONFIG_NETFILTER
1256 /* we need to exclude all possible ENOPROTOOPTs except default case */
1257 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1258 !ip_mroute_opt(optname
)) {
1261 if (get_user(len
, optlen
))
1265 err
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1269 err
= put_user(len
, optlen
);
1275 EXPORT_SYMBOL(ip_getsockopt
);
1277 #ifdef CONFIG_COMPAT
1278 int compat_ip_getsockopt(struct sock
*sk
, int level
, int optname
,
1279 char __user
*optval
, int __user
*optlen
)
1283 if (optname
== MCAST_MSFILTER
)
1284 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1287 err
= do_ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1289 #ifdef CONFIG_NETFILTER
1290 /* we need to exclude all possible ENOPROTOOPTs except default case */
1291 if (err
== -ENOPROTOOPT
&& optname
!= IP_PKTOPTIONS
&&
1292 !ip_mroute_opt(optname
)) {
1295 if (get_user(len
, optlen
))
1299 err
= compat_nf_getsockopt(sk
, PF_INET
, optname
, optval
, &len
);
1302 err
= put_user(len
, optlen
);
1308 EXPORT_SYMBOL(compat_ip_getsockopt
);