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.
8 * Version: $Id: ip_sockglue.c,v 1.62 2002/02/01 22:01:04 davem Exp $
13 * Many : Split from ip.c , see ip.c for history.
14 * Martin Mares : TOS setting fixed.
15 * Alan Cox : Fixed a couple of oopses in Martin's
17 * Mike McLagan : Routing by source
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
24 #include <linux/sched.h>
25 #include <linux/skbuff.h>
27 #include <linux/icmp.h>
28 #include <linux/netdevice.h>
33 #include <linux/tcp.h>
34 #include <linux/udp.h>
35 #include <linux/igmp.h>
36 #include <linux/netfilter.h>
37 #include <linux/route.h>
38 #include <linux/mroute.h>
39 #include <net/route.h>
41 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
42 #include <net/transp_v6.h>
45 #include <linux/errqueue.h>
46 #include <asm/uaccess.h>
48 #define IP_CMSG_PKTINFO 1
51 #define IP_CMSG_RECVOPTS 8
52 #define IP_CMSG_RETOPTS 16
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
= (struct rtable
*)skb
->dst
;
63 info
.ipi_addr
.s_addr
= skb
->nh
.iph
->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
= skb
->nh
.iph
->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, &skb
->nh
.iph
->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
, skb
->nh
.iph
+1);
95 static void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
97 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
98 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
100 if (IPCB(skb
)->opt
.optlen
== 0)
103 if (ip_options_echo(opt
, skb
)) {
104 msg
->msg_flags
|= MSG_CTRUNC
;
107 ip_options_undo(opt
);
109 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
113 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
115 struct inet_sock
*inet
= inet_sk(skb
->sk
);
116 unsigned flags
= inet
->cmsg_flags
;
118 /* Ordered by supposed usage frequency */
120 ip_cmsg_recv_pktinfo(msg
, skb
);
121 if ((flags
>>=1) == 0)
125 ip_cmsg_recv_ttl(msg
, skb
);
126 if ((flags
>>=1) == 0)
130 ip_cmsg_recv_tos(msg
, skb
);
131 if ((flags
>>=1) == 0)
135 ip_cmsg_recv_opts(msg
, skb
);
136 if ((flags
>>=1) == 0)
140 ip_cmsg_recv_retopts(msg
, skb
);
143 int ip_cmsg_send(struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
146 struct cmsghdr
*cmsg
;
148 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
149 if (!CMSG_OK(msg
, cmsg
))
151 if (cmsg
->cmsg_level
!= SOL_IP
)
153 switch (cmsg
->cmsg_type
) {
155 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
156 err
= ip_options_get(&ipc
->opt
, CMSG_DATA(cmsg
), err
< 40 ? err
: 40);
162 struct in_pktinfo
*info
;
163 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
165 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
166 ipc
->oif
= info
->ipi_ifindex
;
167 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
178 /* Special input handler for packets caught by router alert option.
179 They are selected only by protocol field, and then processed likely
180 local ones; but only if someone wants them! Otherwise, router
181 not running rsvpd will kill RSVP.
183 It is user level problem, what it will make with them.
184 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
185 but receiver should be enough clever f.e. to forward mtrace requests,
186 sent to multicast group to reach destination designated router.
188 struct ip_ra_chain
*ip_ra_chain
;
189 DEFINE_RWLOCK(ip_ra_lock
);
191 int ip_ra_control(struct sock
*sk
, unsigned char on
, void (*destructor
)(struct sock
*))
193 struct ip_ra_chain
*ra
, *new_ra
, **rap
;
195 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->num
== IPPROTO_RAW
)
198 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
200 write_lock_bh(&ip_ra_lock
);
201 for (rap
= &ip_ra_chain
; (ra
=*rap
) != NULL
; rap
= &ra
->next
) {
204 write_unlock_bh(&ip_ra_lock
);
210 write_unlock_bh(&ip_ra_lock
);
219 if (new_ra
== NULL
) {
220 write_unlock_bh(&ip_ra_lock
);
224 new_ra
->destructor
= destructor
;
229 write_unlock_bh(&ip_ra_lock
);
234 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
235 u16 port
, u32 info
, u8
*payload
)
237 struct inet_sock
*inet
= inet_sk(sk
);
238 struct sock_exterr_skb
*serr
;
243 skb
= skb_clone(skb
, GFP_ATOMIC
);
247 serr
= SKB_EXT_ERR(skb
);
248 serr
->ee
.ee_errno
= err
;
249 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
250 serr
->ee
.ee_type
= skb
->h
.icmph
->type
;
251 serr
->ee
.ee_code
= skb
->h
.icmph
->code
;
253 serr
->ee
.ee_info
= info
;
254 serr
->ee
.ee_data
= 0;
255 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(skb
->h
.icmph
+1))->daddr
) - skb
->nh
.raw
;
258 skb
->h
.raw
= payload
;
259 if (!skb_pull(skb
, payload
- skb
->data
) ||
260 sock_queue_err_skb(sk
, skb
))
264 void ip_local_error(struct sock
*sk
, int err
, u32 daddr
, u16 port
, u32 info
)
266 struct inet_sock
*inet
= inet_sk(sk
);
267 struct sock_exterr_skb
*serr
;
274 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
278 iph
= (struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
));
282 serr
= SKB_EXT_ERR(skb
);
283 serr
->ee
.ee_errno
= err
;
284 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
285 serr
->ee
.ee_type
= 0;
286 serr
->ee
.ee_code
= 0;
288 serr
->ee
.ee_info
= info
;
289 serr
->ee
.ee_data
= 0;
290 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb
->nh
.raw
;
293 skb
->h
.raw
= skb
->tail
;
294 __skb_pull(skb
, skb
->tail
- skb
->data
);
296 if (sock_queue_err_skb(sk
, skb
))
301 * Handle MSG_ERRQUEUE
303 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
305 struct sock_exterr_skb
*serr
;
306 struct sk_buff
*skb
, *skb2
;
307 struct sockaddr_in
*sin
;
309 struct sock_extended_err ee
;
310 struct sockaddr_in offender
;
316 skb
= skb_dequeue(&sk
->sk_error_queue
);
322 msg
->msg_flags
|= MSG_TRUNC
;
325 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
329 sock_recv_timestamp(msg
, sk
, skb
);
331 serr
= SKB_EXT_ERR(skb
);
333 sin
= (struct sockaddr_in
*)msg
->msg_name
;
335 sin
->sin_family
= AF_INET
;
336 sin
->sin_addr
.s_addr
= *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
);
337 sin
->sin_port
= serr
->port
;
338 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
341 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
342 sin
= &errhdr
.offender
;
343 sin
->sin_family
= AF_UNSPEC
;
344 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
345 struct inet_sock
*inet
= inet_sk(sk
);
347 sin
->sin_family
= AF_INET
;
348 sin
->sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
350 memset(&sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
351 if (inet
->cmsg_flags
)
352 ip_cmsg_recv(msg
, skb
);
355 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
357 /* Now we could try to dump offended packet options */
359 msg
->msg_flags
|= MSG_ERRQUEUE
;
362 /* Reset and regenerate socket error */
363 spin_lock_bh(&sk
->sk_error_queue
.lock
);
365 if ((skb2
= skb_peek(&sk
->sk_error_queue
)) != NULL
) {
366 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
367 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
368 sk
->sk_error_report(sk
);
370 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
380 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
384 int ip_setsockopt(struct sock
*sk
, int level
, int optname
, char __user
*optval
, int optlen
)
386 struct inet_sock
*inet
= inet_sk(sk
);
392 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
393 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
394 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
395 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
396 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
397 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
))) ||
398 optname
== IP_MULTICAST_TTL
||
399 optname
== IP_MULTICAST_LOOP
) {
400 if (optlen
>= sizeof(int)) {
401 if (get_user(val
, (int __user
*) optval
))
403 } else if (optlen
>= sizeof(char)) {
406 if (get_user(ucval
, (unsigned char __user
*) optval
))
412 /* If optlen==0, it is equivalent to val == 0 */
414 #ifdef CONFIG_IP_MROUTE
415 if (optname
>= MRT_BASE
&& optname
<= (MRT_BASE
+ 10))
416 return ip_mroute_setsockopt(sk
,optname
,optval
,optlen
);
425 struct ip_options
* opt
= NULL
;
426 if (optlen
> 40 || optlen
< 0)
428 err
= ip_options_get_from_user(&opt
, optval
, optlen
);
431 if (sk
->sk_type
== SOCK_STREAM
) {
432 struct tcp_sock
*tp
= tcp_sk(sk
);
433 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
434 if (sk
->sk_family
== PF_INET
||
435 (!((1 << sk
->sk_state
) &
436 (TCPF_LISTEN
| TCPF_CLOSE
)) &&
437 inet
->daddr
!= LOOPBACK4_IPV6
)) {
440 tp
->ext_header_len
-= inet
->opt
->optlen
;
442 tp
->ext_header_len
+= opt
->optlen
;
443 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
444 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
448 opt
= xchg(&inet
->opt
, opt
);
455 inet
->cmsg_flags
|= IP_CMSG_PKTINFO
;
457 inet
->cmsg_flags
&= ~IP_CMSG_PKTINFO
;
461 inet
->cmsg_flags
|= IP_CMSG_TTL
;
463 inet
->cmsg_flags
&= ~IP_CMSG_TTL
;
467 inet
->cmsg_flags
|= IP_CMSG_TOS
;
469 inet
->cmsg_flags
&= ~IP_CMSG_TOS
;
473 inet
->cmsg_flags
|= IP_CMSG_RECVOPTS
;
475 inet
->cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
479 inet
->cmsg_flags
|= IP_CMSG_RETOPTS
;
481 inet
->cmsg_flags
&= ~IP_CMSG_RETOPTS
;
483 case IP_TOS
: /* This sets both TOS and Precedence */
484 if (sk
->sk_type
== SOCK_STREAM
) {
486 val
|= inet
->tos
& 3;
488 if (IPTOS_PREC(val
) >= IPTOS_PREC_CRITIC_ECP
&&
489 !capable(CAP_NET_ADMIN
)) {
493 if (inet
->tos
!= val
) {
495 sk
->sk_priority
= rt_tos2priority(val
);
502 if (val
!= -1 && (val
< 1 || val
>255))
507 if (sk
->sk_type
!= SOCK_RAW
) {
511 inet
->hdrincl
= val
? 1 : 0;
513 case IP_MTU_DISCOVER
:
516 inet
->pmtudisc
= val
;
519 inet
->recverr
= !!val
;
521 skb_queue_purge(&sk
->sk_error_queue
);
523 case IP_MULTICAST_TTL
:
524 if (sk
->sk_type
== SOCK_STREAM
)
530 if (val
< 0 || val
> 255)
534 case IP_MULTICAST_LOOP
:
537 inet
->mc_loop
= !!val
;
539 case IP_MULTICAST_IF
:
541 struct ip_mreqn mreq
;
542 struct net_device
*dev
= NULL
;
544 if (sk
->sk_type
== SOCK_STREAM
)
547 * Check the arguments are allowable
551 if (optlen
>= sizeof(struct ip_mreqn
)) {
552 if (copy_from_user(&mreq
,optval
,sizeof(mreq
)))
555 memset(&mreq
, 0, sizeof(mreq
));
556 if (optlen
>= sizeof(struct in_addr
) &&
557 copy_from_user(&mreq
.imr_address
,optval
,sizeof(struct in_addr
)))
561 if (!mreq
.imr_ifindex
) {
562 if (mreq
.imr_address
.s_addr
== INADDR_ANY
) {
568 dev
= ip_dev_find(mreq
.imr_address
.s_addr
);
570 mreq
.imr_ifindex
= dev
->ifindex
;
574 dev
= __dev_get_by_index(mreq
.imr_ifindex
);
577 err
= -EADDRNOTAVAIL
;
582 if (sk
->sk_bound_dev_if
&&
583 mreq
.imr_ifindex
!= sk
->sk_bound_dev_if
)
586 inet
->mc_index
= mreq
.imr_ifindex
;
587 inet
->mc_addr
= mreq
.imr_address
.s_addr
;
592 case IP_ADD_MEMBERSHIP
:
593 case IP_DROP_MEMBERSHIP
:
595 struct ip_mreqn mreq
;
597 if (optlen
< sizeof(struct ip_mreq
))
600 if (optlen
>= sizeof(struct ip_mreqn
)) {
601 if(copy_from_user(&mreq
,optval
,sizeof(mreq
)))
604 memset(&mreq
, 0, sizeof(mreq
));
605 if (copy_from_user(&mreq
,optval
,sizeof(struct ip_mreq
)))
609 if (optname
== IP_ADD_MEMBERSHIP
)
610 err
= ip_mc_join_group(sk
, &mreq
);
612 err
= ip_mc_leave_group(sk
, &mreq
);
617 extern int sysctl_igmp_max_msf
;
618 struct ip_msfilter
*msf
;
620 if (optlen
< IP_MSFILTER_SIZE(0))
622 if (optlen
> sysctl_optmem_max
) {
626 msf
= (struct ip_msfilter
*)kmalloc(optlen
, GFP_KERNEL
);
632 if (copy_from_user(msf
, optval
, optlen
)) {
636 /* numsrc >= (1G-4) overflow in 32 bits */
637 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
638 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
643 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
648 err
= ip_mc_msfilter(sk
, msf
, 0);
652 case IP_BLOCK_SOURCE
:
653 case IP_UNBLOCK_SOURCE
:
654 case IP_ADD_SOURCE_MEMBERSHIP
:
655 case IP_DROP_SOURCE_MEMBERSHIP
:
657 struct ip_mreq_source mreqs
;
660 if (optlen
!= sizeof(struct ip_mreq_source
))
662 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
666 if (optname
== IP_BLOCK_SOURCE
) {
667 omode
= MCAST_EXCLUDE
;
669 } else if (optname
== IP_UNBLOCK_SOURCE
) {
670 omode
= MCAST_EXCLUDE
;
672 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
673 struct ip_mreqn mreq
;
675 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
676 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
677 mreq
.imr_ifindex
= 0;
678 err
= ip_mc_join_group(sk
, &mreq
);
679 if (err
&& err
!= -EADDRINUSE
)
681 omode
= MCAST_INCLUDE
;
683 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
684 omode
= MCAST_INCLUDE
;
687 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
690 case MCAST_JOIN_GROUP
:
691 case MCAST_LEAVE_GROUP
:
693 struct group_req greq
;
694 struct sockaddr_in
*psin
;
695 struct ip_mreqn mreq
;
697 if (optlen
< sizeof(struct group_req
))
700 if(copy_from_user(&greq
, optval
, sizeof(greq
)))
702 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
703 if (psin
->sin_family
!= AF_INET
)
705 memset(&mreq
, 0, sizeof(mreq
));
706 mreq
.imr_multiaddr
= psin
->sin_addr
;
707 mreq
.imr_ifindex
= greq
.gr_interface
;
709 if (optname
== MCAST_JOIN_GROUP
)
710 err
= ip_mc_join_group(sk
, &mreq
);
712 err
= ip_mc_leave_group(sk
, &mreq
);
715 case MCAST_JOIN_SOURCE_GROUP
:
716 case MCAST_LEAVE_SOURCE_GROUP
:
717 case MCAST_BLOCK_SOURCE
:
718 case MCAST_UNBLOCK_SOURCE
:
720 struct group_source_req greqs
;
721 struct ip_mreq_source mreqs
;
722 struct sockaddr_in
*psin
;
725 if (optlen
!= sizeof(struct group_source_req
))
727 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
731 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
732 greqs
.gsr_source
.ss_family
!= AF_INET
) {
733 err
= -EADDRNOTAVAIL
;
736 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
737 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
738 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
739 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
740 mreqs
.imr_interface
= 0; /* use index for mc_source */
742 if (optname
== MCAST_BLOCK_SOURCE
) {
743 omode
= MCAST_EXCLUDE
;
745 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
746 omode
= MCAST_EXCLUDE
;
748 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
749 struct ip_mreqn mreq
;
751 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
752 mreq
.imr_multiaddr
= psin
->sin_addr
;
753 mreq
.imr_address
.s_addr
= 0;
754 mreq
.imr_ifindex
= greqs
.gsr_interface
;
755 err
= ip_mc_join_group(sk
, &mreq
);
756 if (err
&& err
!= -EADDRINUSE
)
758 greqs
.gsr_interface
= mreq
.imr_ifindex
;
759 omode
= MCAST_INCLUDE
;
761 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
762 omode
= MCAST_INCLUDE
;
765 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
766 greqs
.gsr_interface
);
771 extern int sysctl_igmp_max_msf
;
772 struct sockaddr_in
*psin
;
773 struct ip_msfilter
*msf
= NULL
;
774 struct group_filter
*gsf
= NULL
;
775 int msize
, i
, ifindex
;
777 if (optlen
< GROUP_FILTER_SIZE(0))
779 if (optlen
> sysctl_optmem_max
) {
783 gsf
= (struct group_filter
*)kmalloc(optlen
,GFP_KERNEL
);
789 if (copy_from_user(gsf
, optval
, optlen
)) {
792 /* numsrc >= (4G-140)/128 overflow in 32 bits */
793 if (gsf
->gf_numsrc
>= 0x1ffffff ||
794 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
798 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
802 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
803 msf
= (struct ip_msfilter
*)kmalloc(msize
,GFP_KERNEL
);
808 ifindex
= gsf
->gf_interface
;
809 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
810 if (psin
->sin_family
!= AF_INET
) {
811 err
= -EADDRNOTAVAIL
;
814 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
815 msf
->imsf_interface
= 0;
816 msf
->imsf_fmode
= gsf
->gf_fmode
;
817 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
818 err
= -EADDRNOTAVAIL
;
819 for (i
=0; i
<gsf
->gf_numsrc
; ++i
) {
820 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
822 if (psin
->sin_family
!= AF_INET
)
824 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
829 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
837 case IP_ROUTER_ALERT
:
838 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
844 inet
->freebind
= !!val
;
847 case IP_IPSEC_POLICY
:
850 if (!capable(CAP_NET_ADMIN
))
852 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
856 #ifdef CONFIG_NETFILTER
857 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
,
873 * Get the options. Note for future reference. The GET of IP options gets the
874 * _received_ ones. The set sets the _sent_ ones.
877 int ip_getsockopt(struct sock
*sk
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
879 struct inet_sock
*inet
= inet_sk(sk
);
886 #ifdef CONFIG_IP_MROUTE
887 if(optname
>=MRT_BASE
&& optname
<=MRT_BASE
+10)
889 return ip_mroute_getsockopt(sk
,optname
,optval
,optlen
);
893 if(get_user(len
,optlen
))
903 unsigned char optbuf
[sizeof(struct ip_options
)+40];
904 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
907 memcpy(optbuf
, inet
->opt
,
908 sizeof(struct ip_options
)+
912 if (opt
->optlen
== 0)
913 return put_user(0, optlen
);
915 ip_options_undo(opt
);
917 len
= min_t(unsigned int, len
, opt
->optlen
);
918 if(put_user(len
, optlen
))
920 if(copy_to_user(optval
, opt
->__data
, len
))
925 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
928 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
931 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
934 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
937 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
943 val
= (inet
->uc_ttl
== -1 ?
944 sysctl_ip_default_ttl
:
950 case IP_MTU_DISCOVER
:
951 val
= inet
->pmtudisc
;
955 struct dst_entry
*dst
;
957 dst
= sk_dst_get(sk
);
971 case IP_MULTICAST_TTL
:
974 case IP_MULTICAST_LOOP
:
977 case IP_MULTICAST_IF
:
980 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
981 addr
.s_addr
= inet
->mc_addr
;
984 if(put_user(len
, optlen
))
986 if(copy_to_user(optval
, &addr
, len
))
992 struct ip_msfilter msf
;
995 if (len
< IP_MSFILTER_SIZE(0)) {
999 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1003 err
= ip_mc_msfget(sk
, &msf
,
1004 (struct ip_msfilter __user
*)optval
, optlen
);
1008 case MCAST_MSFILTER
:
1010 struct group_filter gsf
;
1013 if (len
< GROUP_FILTER_SIZE(0)) {
1017 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1021 err
= ip_mc_gsfget(sk
, &gsf
,
1022 (struct group_filter __user
*)optval
, optlen
);
1032 if (sk
->sk_type
!= SOCK_STREAM
)
1033 return -ENOPROTOOPT
;
1035 msg
.msg_control
= optval
;
1036 msg
.msg_controllen
= len
;
1039 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1040 struct in_pktinfo info
;
1042 info
.ipi_addr
.s_addr
= inet
->rcv_saddr
;
1043 info
.ipi_spec_dst
.s_addr
= inet
->rcv_saddr
;
1044 info
.ipi_ifindex
= inet
->mc_index
;
1045 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1047 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1048 int hlim
= inet
->mc_ttl
;
1049 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1051 len
-= msg
.msg_controllen
;
1052 return put_user(len
, optlen
);
1055 val
= inet
->freebind
;
1058 #ifdef CONFIG_NETFILTER
1059 val
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1063 val
= put_user(len
, optlen
);
1067 return -ENOPROTOOPT
;
1072 if (len
< sizeof(int) && len
> 0 && val
>=0 && val
<255) {
1073 unsigned char ucval
= (unsigned char)val
;
1075 if(put_user(len
, optlen
))
1077 if(copy_to_user(optval
,&ucval
,1))
1080 len
= min_t(unsigned int, sizeof(int), len
);
1081 if(put_user(len
, optlen
))
1083 if(copy_to_user(optval
,&val
,len
))
1089 EXPORT_SYMBOL(ip_cmsg_recv
);
1091 EXPORT_SYMBOL(ip_getsockopt
);
1092 EXPORT_SYMBOL(ip_setsockopt
);