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, 0);
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_irq(&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_irq(&sk
->sk_error_queue
.lock
);
368 sk
->sk_error_report(sk
);
370 spin_unlock_irq(&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(&opt
, optval
, optlen
, 1);
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_optmem_max
;
618 extern int sysctl_igmp_max_msf
;
619 struct ip_msfilter
*msf
;
621 if (optlen
< IP_MSFILTER_SIZE(0))
623 if (optlen
> sysctl_optmem_max
) {
627 msf
= (struct ip_msfilter
*)kmalloc(optlen
, GFP_KERNEL
);
633 if (copy_from_user(msf
, optval
, optlen
)) {
637 /* numsrc >= (1G-4) overflow in 32 bits */
638 if (msf
->imsf_numsrc
>= 0x3ffffffcU
||
639 msf
->imsf_numsrc
> sysctl_igmp_max_msf
) {
644 if (IP_MSFILTER_SIZE(msf
->imsf_numsrc
) > optlen
) {
649 err
= ip_mc_msfilter(sk
, msf
, 0);
653 case IP_BLOCK_SOURCE
:
654 case IP_UNBLOCK_SOURCE
:
655 case IP_ADD_SOURCE_MEMBERSHIP
:
656 case IP_DROP_SOURCE_MEMBERSHIP
:
658 struct ip_mreq_source mreqs
;
661 if (optlen
!= sizeof(struct ip_mreq_source
))
663 if (copy_from_user(&mreqs
, optval
, sizeof(mreqs
))) {
667 if (optname
== IP_BLOCK_SOURCE
) {
668 omode
= MCAST_EXCLUDE
;
670 } else if (optname
== IP_UNBLOCK_SOURCE
) {
671 omode
= MCAST_EXCLUDE
;
673 } else if (optname
== IP_ADD_SOURCE_MEMBERSHIP
) {
674 struct ip_mreqn mreq
;
676 mreq
.imr_multiaddr
.s_addr
= mreqs
.imr_multiaddr
;
677 mreq
.imr_address
.s_addr
= mreqs
.imr_interface
;
678 mreq
.imr_ifindex
= 0;
679 err
= ip_mc_join_group(sk
, &mreq
);
682 omode
= MCAST_INCLUDE
;
684 } else /*IP_DROP_SOURCE_MEMBERSHIP */ {
685 omode
= MCAST_INCLUDE
;
688 err
= ip_mc_source(add
, omode
, sk
, &mreqs
, 0);
691 case MCAST_JOIN_GROUP
:
692 case MCAST_LEAVE_GROUP
:
694 struct group_req greq
;
695 struct sockaddr_in
*psin
;
696 struct ip_mreqn mreq
;
698 if (optlen
< sizeof(struct group_req
))
701 if(copy_from_user(&greq
, optval
, sizeof(greq
)))
703 psin
= (struct sockaddr_in
*)&greq
.gr_group
;
704 if (psin
->sin_family
!= AF_INET
)
706 memset(&mreq
, 0, sizeof(mreq
));
707 mreq
.imr_multiaddr
= psin
->sin_addr
;
708 mreq
.imr_ifindex
= greq
.gr_interface
;
710 if (optname
== MCAST_JOIN_GROUP
)
711 err
= ip_mc_join_group(sk
, &mreq
);
713 err
= ip_mc_leave_group(sk
, &mreq
);
716 case MCAST_JOIN_SOURCE_GROUP
:
717 case MCAST_LEAVE_SOURCE_GROUP
:
718 case MCAST_BLOCK_SOURCE
:
719 case MCAST_UNBLOCK_SOURCE
:
721 struct group_source_req greqs
;
722 struct ip_mreq_source mreqs
;
723 struct sockaddr_in
*psin
;
726 if (optlen
!= sizeof(struct group_source_req
))
728 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
732 if (greqs
.gsr_group
.ss_family
!= AF_INET
||
733 greqs
.gsr_source
.ss_family
!= AF_INET
) {
734 err
= -EADDRNOTAVAIL
;
737 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
738 mreqs
.imr_multiaddr
= psin
->sin_addr
.s_addr
;
739 psin
= (struct sockaddr_in
*)&greqs
.gsr_source
;
740 mreqs
.imr_sourceaddr
= psin
->sin_addr
.s_addr
;
741 mreqs
.imr_interface
= 0; /* use index for mc_source */
743 if (optname
== MCAST_BLOCK_SOURCE
) {
744 omode
= MCAST_EXCLUDE
;
746 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
747 omode
= MCAST_EXCLUDE
;
749 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
750 struct ip_mreqn mreq
;
752 psin
= (struct sockaddr_in
*)&greqs
.gsr_group
;
753 mreq
.imr_multiaddr
= psin
->sin_addr
;
754 mreq
.imr_address
.s_addr
= 0;
755 mreq
.imr_ifindex
= greqs
.gsr_interface
;
756 err
= ip_mc_join_group(sk
, &mreq
);
759 greqs
.gsr_interface
= mreq
.imr_ifindex
;
760 omode
= MCAST_INCLUDE
;
762 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
763 omode
= MCAST_INCLUDE
;
766 err
= ip_mc_source(add
, omode
, sk
, &mreqs
,
767 greqs
.gsr_interface
);
772 extern int sysctl_optmem_max
;
773 extern int sysctl_igmp_max_msf
;
774 struct sockaddr_in
*psin
;
775 struct ip_msfilter
*msf
= NULL
;
776 struct group_filter
*gsf
= NULL
;
777 int msize
, i
, ifindex
;
779 if (optlen
< GROUP_FILTER_SIZE(0))
781 if (optlen
> sysctl_optmem_max
) {
785 gsf
= (struct group_filter
*)kmalloc(optlen
,GFP_KERNEL
);
791 if (copy_from_user(gsf
, optval
, optlen
)) {
794 /* numsrc >= (4G-140)/128 overflow in 32 bits */
795 if (gsf
->gf_numsrc
>= 0x1ffffff ||
796 gsf
->gf_numsrc
> sysctl_igmp_max_msf
) {
800 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
804 msize
= IP_MSFILTER_SIZE(gsf
->gf_numsrc
);
805 msf
= (struct ip_msfilter
*)kmalloc(msize
,GFP_KERNEL
);
810 ifindex
= gsf
->gf_interface
;
811 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
812 if (psin
->sin_family
!= AF_INET
) {
813 err
= -EADDRNOTAVAIL
;
816 msf
->imsf_multiaddr
= psin
->sin_addr
.s_addr
;
817 msf
->imsf_interface
= 0;
818 msf
->imsf_fmode
= gsf
->gf_fmode
;
819 msf
->imsf_numsrc
= gsf
->gf_numsrc
;
820 err
= -EADDRNOTAVAIL
;
821 for (i
=0; i
<gsf
->gf_numsrc
; ++i
) {
822 psin
= (struct sockaddr_in
*)&gsf
->gf_slist
[i
];
824 if (psin
->sin_family
!= AF_INET
)
826 msf
->imsf_slist
[i
] = psin
->sin_addr
.s_addr
;
831 err
= ip_mc_msfilter(sk
, msf
, ifindex
);
839 case IP_ROUTER_ALERT
:
840 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
846 inet
->freebind
= !!val
;
849 case IP_IPSEC_POLICY
:
851 err
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
855 #ifdef CONFIG_NETFILTER
856 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
,
872 * Get the options. Note for future reference. The GET of IP options gets the
873 * _received_ ones. The set sets the _sent_ ones.
876 int ip_getsockopt(struct sock
*sk
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
878 struct inet_sock
*inet
= inet_sk(sk
);
885 #ifdef CONFIG_IP_MROUTE
886 if(optname
>=MRT_BASE
&& optname
<=MRT_BASE
+10)
888 return ip_mroute_getsockopt(sk
,optname
,optval
,optlen
);
892 if(get_user(len
,optlen
))
902 unsigned char optbuf
[sizeof(struct ip_options
)+40];
903 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
906 memcpy(optbuf
, inet
->opt
,
907 sizeof(struct ip_options
)+
911 if (opt
->optlen
== 0)
912 return put_user(0, optlen
);
914 ip_options_undo(opt
);
916 len
= min_t(unsigned int, len
, opt
->optlen
);
917 if(put_user(len
, optlen
))
919 if(copy_to_user(optval
, opt
->__data
, len
))
924 val
= (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
927 val
= (inet
->cmsg_flags
& IP_CMSG_TTL
) != 0;
930 val
= (inet
->cmsg_flags
& IP_CMSG_TOS
) != 0;
933 val
= (inet
->cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
936 val
= (inet
->cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
942 val
= (inet
->uc_ttl
== -1 ?
943 sysctl_ip_default_ttl
:
949 case IP_MTU_DISCOVER
:
950 val
= inet
->pmtudisc
;
954 struct dst_entry
*dst
;
956 dst
= sk_dst_get(sk
);
970 case IP_MULTICAST_TTL
:
973 case IP_MULTICAST_LOOP
:
976 case IP_MULTICAST_IF
:
979 len
= min_t(unsigned int, len
, sizeof(struct in_addr
));
980 addr
.s_addr
= inet
->mc_addr
;
983 if(put_user(len
, optlen
))
985 if(copy_to_user(optval
, &addr
, len
))
991 struct ip_msfilter msf
;
994 if (len
< IP_MSFILTER_SIZE(0)) {
998 if (copy_from_user(&msf
, optval
, IP_MSFILTER_SIZE(0))) {
1002 err
= ip_mc_msfget(sk
, &msf
,
1003 (struct ip_msfilter __user
*)optval
, optlen
);
1007 case MCAST_MSFILTER
:
1009 struct group_filter gsf
;
1012 if (len
< GROUP_FILTER_SIZE(0)) {
1016 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0))) {
1020 err
= ip_mc_gsfget(sk
, &gsf
,
1021 (struct group_filter __user
*)optval
, optlen
);
1031 if (sk
->sk_type
!= SOCK_STREAM
)
1032 return -ENOPROTOOPT
;
1034 msg
.msg_control
= optval
;
1035 msg
.msg_controllen
= len
;
1038 if (inet
->cmsg_flags
& IP_CMSG_PKTINFO
) {
1039 struct in_pktinfo info
;
1041 info
.ipi_addr
.s_addr
= inet
->rcv_saddr
;
1042 info
.ipi_spec_dst
.s_addr
= inet
->rcv_saddr
;
1043 info
.ipi_ifindex
= inet
->mc_index
;
1044 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
1046 if (inet
->cmsg_flags
& IP_CMSG_TTL
) {
1047 int hlim
= inet
->mc_ttl
;
1048 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
1050 len
-= msg
.msg_controllen
;
1051 return put_user(len
, optlen
);
1054 val
= inet
->freebind
;
1057 #ifdef CONFIG_NETFILTER
1058 val
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
1062 val
= put_user(len
, optlen
);
1066 return -ENOPROTOOPT
;
1071 if (len
< sizeof(int) && len
> 0 && val
>=0 && val
<255) {
1072 unsigned char ucval
= (unsigned char)val
;
1074 if(put_user(len
, optlen
))
1076 if(copy_to_user(optval
,&ucval
,1))
1079 len
= min_t(unsigned int, sizeof(int), len
);
1080 if(put_user(len
, optlen
))
1082 if(copy_to_user(optval
,&val
,len
))
1088 EXPORT_SYMBOL(ip_cmsg_recv
);
1090 #ifdef CONFIG_IP_SCTP_MODULE
1091 EXPORT_SYMBOL(ip_getsockopt
);
1092 EXPORT_SYMBOL(ip_setsockopt
);