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.54 2000/11/28 13:34:56 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/types.h>
23 #include <linux/sched.h>
24 #include <linux/skbuff.h>
26 #include <linux/icmp.h>
27 #include <linux/netdevice.h>
32 #include <linux/tcp.h>
33 #include <linux/udp.h>
34 #include <linux/igmp.h>
35 #include <linux/netfilter.h>
36 #include <linux/route.h>
37 #include <linux/mroute.h>
38 #include <net/route.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 MAX(a,b) ((a)>(b)?(a):(b))
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 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 unsigned flags
= skb
->sk
->protinfo
.af_inet
.cmsg_flags
;
117 /* Ordered by supposed usage frequency */
119 ip_cmsg_recv_pktinfo(msg
, skb
);
120 if ((flags
>>=1) == 0)
124 ip_cmsg_recv_ttl(msg
, skb
);
125 if ((flags
>>=1) == 0)
129 ip_cmsg_recv_tos(msg
, skb
);
130 if ((flags
>>=1) == 0)
134 ip_cmsg_recv_opts(msg
, skb
);
135 if ((flags
>>=1) == 0)
139 ip_cmsg_recv_retopts(msg
, skb
);
142 int ip_cmsg_send(struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
145 struct cmsghdr
*cmsg
;
147 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
148 if (cmsg
->cmsg_len
< sizeof(struct cmsghdr
) ||
149 (unsigned long)(((char*)cmsg
- (char*)msg
->msg_control
)
150 + cmsg
->cmsg_len
) > msg
->msg_controllen
) {
153 if (cmsg
->cmsg_level
!= SOL_IP
)
155 switch (cmsg
->cmsg_type
) {
157 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
158 err
= ip_options_get(&ipc
->opt
, CMSG_DATA(cmsg
), err
< 40 ? err
: 40, 0);
164 struct in_pktinfo
*info
;
165 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
167 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
168 ipc
->oif
= info
->ipi_ifindex
;
169 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
180 /* Special input handler for packets catched by router alert option.
181 They are selected only by protocol field, and then processed likely
182 local ones; but only if someone wants them! Otherwise, router
183 not running rsvpd will kill RSVP.
185 It is user level problem, what it will make with them.
186 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
187 but receiver should be enough clever f.e. to forward mtrace requests,
188 sent to multicast group to reach destination designated router.
190 struct ip_ra_chain
*ip_ra_chain
;
191 rwlock_t ip_ra_lock
= RW_LOCK_UNLOCKED
;
193 int ip_ra_control(struct sock
*sk
, unsigned char on
, void (*destructor
)(struct sock
*))
195 struct ip_ra_chain
*ra
, *new_ra
, **rap
;
197 if (sk
->type
!= SOCK_RAW
|| sk
->num
== IPPROTO_RAW
)
200 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
202 write_lock_bh(&ip_ra_lock
);
203 for (rap
= &ip_ra_chain
; (ra
=*rap
) != NULL
; rap
= &ra
->next
) {
206 write_unlock_bh(&ip_ra_lock
);
212 write_unlock_bh(&ip_ra_lock
);
221 if (new_ra
== NULL
) {
222 write_unlock_bh(&ip_ra_lock
);
226 new_ra
->destructor
= destructor
;
231 write_unlock_bh(&ip_ra_lock
);
236 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
237 u16 port
, u32 info
, u8
*payload
)
239 struct sock_exterr_skb
*serr
;
241 if (!sk
->protinfo
.af_inet
.recverr
)
244 skb
= skb_clone(skb
, GFP_ATOMIC
);
248 serr
= SKB_EXT_ERR(skb
);
249 serr
->ee
.ee_errno
= err
;
250 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
251 serr
->ee
.ee_type
= skb
->h
.icmph
->type
;
252 serr
->ee
.ee_code
= skb
->h
.icmph
->code
;
254 serr
->ee
.ee_info
= info
;
255 serr
->ee
.ee_data
= 0;
256 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(skb
->h
.icmph
+1))->daddr
) - skb
->nh
.raw
;
259 skb
->h
.raw
= payload
;
260 skb_pull(skb
, payload
- skb
->data
);
262 if (sock_queue_err_skb(sk
, skb
))
266 void ip_local_error(struct sock
*sk
, int err
, u32 daddr
, u16 port
, u32 info
)
268 struct sock_exterr_skb
*serr
;
272 if (!sk
->protinfo
.af_inet
.recverr
)
275 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
279 iph
= (struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
));
283 serr
= SKB_EXT_ERR(skb
);
284 serr
->ee
.ee_errno
= err
;
285 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
286 serr
->ee
.ee_type
= 0;
287 serr
->ee
.ee_code
= 0;
289 serr
->ee
.ee_info
= info
;
290 serr
->ee
.ee_data
= 0;
291 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb
->nh
.raw
;
294 skb
->h
.raw
= skb
->tail
;
295 skb_pull(skb
, skb
->tail
- skb
->data
);
297 if (sock_queue_err_skb(sk
, skb
))
302 * Handle MSG_ERRQUEUE
304 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
306 struct sock_exterr_skb
*serr
;
307 struct sk_buff
*skb
, *skb2
;
308 struct sockaddr_in
*sin
;
310 struct sock_extended_err ee
;
311 struct sockaddr_in offender
;
317 skb
= skb_dequeue(&sk
->error_queue
);
323 msg
->msg_flags
|= MSG_TRUNC
;
326 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, copied
);
330 sock_recv_timestamp(msg
, sk
, skb
);
332 serr
= SKB_EXT_ERR(skb
);
334 sin
= (struct sockaddr_in
*)msg
->msg_name
;
336 sin
->sin_family
= AF_INET
;
337 sin
->sin_addr
.s_addr
= *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
);
338 sin
->sin_port
= serr
->port
;
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 sin
->sin_family
= AF_INET
;
346 sin
->sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
347 if (sk
->protinfo
.af_inet
.cmsg_flags
)
348 ip_cmsg_recv(msg
, skb
);
351 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
353 /* Now we could try to dump offended packet options */
355 msg
->msg_flags
|= MSG_ERRQUEUE
;
358 /* Reset and regenerate socket error */
359 spin_lock_irq(&sk
->error_queue
.lock
);
361 if ((skb2
= skb_peek(&sk
->error_queue
)) != NULL
) {
362 sk
->err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
363 spin_unlock_irq(&sk
->error_queue
.lock
);
364 sk
->error_report(sk
);
366 spin_unlock_irq(&sk
->error_queue
.lock
);
377 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
381 int ip_setsockopt(struct sock
*sk
, int level
, int optname
, char *optval
, int optlen
)
388 if (((1<<optname
) & ((1<<IP_PKTINFO
) | (1<<IP_RECVTTL
) |
389 (1<<IP_RECVOPTS
) | (1<<IP_RECVTOS
) |
390 (1<<IP_RETOPTS
) | (1<<IP_TOS
) |
391 (1<<IP_TTL
) | (1<<IP_HDRINCL
) |
392 (1<<IP_MTU_DISCOVER
) | (1<<IP_RECVERR
) |
393 (1<<IP_ROUTER_ALERT
) | (1<<IP_FREEBIND
))) ||
394 optname
== IP_MULTICAST_TTL
||
395 optname
== IP_MULTICAST_LOOP
) {
396 if (optlen
>= sizeof(int)) {
397 if (get_user(val
, (int *) optval
))
399 } else if (optlen
>= sizeof(char)) {
402 if (get_user(ucval
, (unsigned char *) optval
))
408 /* If optlen==0, it is equivalent to val == 0 */
410 #ifdef CONFIG_IP_MROUTE
411 if (optname
>= MRT_BASE
&& optname
<= (MRT_BASE
+ 10))
412 return ip_mroute_setsockopt(sk
,optname
,optval
,optlen
);
421 struct ip_options
* opt
= NULL
;
422 if (optlen
> 40 || optlen
< 0)
424 err
= ip_options_get(&opt
, optval
, optlen
, 1);
427 if (sk
->type
== SOCK_STREAM
) {
428 struct tcp_opt
*tp
= &sk
->tp_pinfo
.af_tcp
;
429 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
430 if (sk
->family
== PF_INET
||
431 (!((1<<sk
->state
)&(TCPF_LISTEN
|TCPF_CLOSE
))
432 && sk
->daddr
!= LOOPBACK4_IPV6
)) {
435 tp
->ext_header_len
= opt
->optlen
;
436 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
437 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
441 opt
= xchg(&sk
->protinfo
.af_inet
.opt
, opt
);
448 sk
->protinfo
.af_inet
.cmsg_flags
|= IP_CMSG_PKTINFO
;
450 sk
->protinfo
.af_inet
.cmsg_flags
&= ~IP_CMSG_PKTINFO
;
454 sk
->protinfo
.af_inet
.cmsg_flags
|= IP_CMSG_TTL
;
456 sk
->protinfo
.af_inet
.cmsg_flags
&= ~IP_CMSG_TTL
;
460 sk
->protinfo
.af_inet
.cmsg_flags
|= IP_CMSG_TOS
;
462 sk
->protinfo
.af_inet
.cmsg_flags
&= ~IP_CMSG_TOS
;
466 sk
->protinfo
.af_inet
.cmsg_flags
|= IP_CMSG_RECVOPTS
;
468 sk
->protinfo
.af_inet
.cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
472 sk
->protinfo
.af_inet
.cmsg_flags
|= IP_CMSG_RETOPTS
;
474 sk
->protinfo
.af_inet
.cmsg_flags
&= ~IP_CMSG_RETOPTS
;
476 case IP_TOS
: /* This sets both TOS and Precedence */
477 /* Reject setting of unused bits */
478 #ifndef CONFIG_INET_ECN
479 if (val
& ~(IPTOS_TOS_MASK
|IPTOS_PREC_MASK
))
482 if (sk
->type
== SOCK_STREAM
) {
484 val
|= sk
->protinfo
.af_inet
.tos
& 3;
487 if (IPTOS_PREC(val
) >= IPTOS_PREC_CRITIC_ECP
&&
488 !capable(CAP_NET_ADMIN
)) {
492 if (sk
->protinfo
.af_inet
.tos
!= val
) {
493 sk
->protinfo
.af_inet
.tos
=val
;
494 sk
->priority
= rt_tos2priority(val
);
502 val
= sysctl_ip_default_ttl
;
505 sk
->protinfo
.af_inet
.ttl
=val
;
508 if(sk
->type
!=SOCK_RAW
) {
512 sk
->protinfo
.af_inet
.hdrincl
=val
?1:0;
514 case IP_MTU_DISCOVER
:
517 sk
->protinfo
.af_inet
.pmtudisc
= val
;
520 sk
->protinfo
.af_inet
.recverr
= !!val
;
522 skb_queue_purge(&sk
->error_queue
);
524 case IP_MULTICAST_TTL
:
525 if (sk
->type
== SOCK_STREAM
)
531 if (val
< 0 || val
> 255)
533 sk
->protinfo
.af_inet
.mc_ttl
=val
;
535 case IP_MULTICAST_LOOP
:
538 sk
->protinfo
.af_inet
.mc_loop
= val
? 1 : 0;
540 case IP_MULTICAST_IF
:
542 struct ip_mreqn mreq
;
543 struct net_device
*dev
= NULL
;
545 if (sk
->type
== SOCK_STREAM
)
548 * Check the arguments are allowable
552 if (optlen
>= sizeof(struct ip_mreqn
)) {
553 if (copy_from_user(&mreq
,optval
,sizeof(mreq
)))
556 memset(&mreq
, 0, sizeof(mreq
));
557 if (optlen
>= sizeof(struct in_addr
) &&
558 copy_from_user(&mreq
.imr_address
,optval
,sizeof(struct in_addr
)))
562 if (!mreq
.imr_ifindex
) {
563 if (mreq
.imr_address
.s_addr
== INADDR_ANY
) {
564 sk
->protinfo
.af_inet
.mc_index
= 0;
565 sk
->protinfo
.af_inet
.mc_addr
= 0;
569 dev
= ip_dev_find(mreq
.imr_address
.s_addr
);
571 mreq
.imr_ifindex
= dev
->ifindex
;
575 dev
= __dev_get_by_index(mreq
.imr_ifindex
);
578 err
= -EADDRNOTAVAIL
;
583 if (sk
->bound_dev_if
&& mreq
.imr_ifindex
!= sk
->bound_dev_if
)
586 sk
->protinfo
.af_inet
.mc_index
= mreq
.imr_ifindex
;
587 sk
->protinfo
.af_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
);
615 case IP_ROUTER_ALERT
:
616 err
= ip_ra_control(sk
, val
? 1 : 0, NULL
);
622 sk
->protinfo
.af_inet
.freebind
= !!val
;
626 #ifdef CONFIG_NETFILTER
627 err
= nf_setsockopt(sk
, PF_INET
, optname
, optval
,
643 * Get the options. Note for future reference. The GET of IP options gets the
644 * _received_ ones. The set sets the _sent_ ones.
647 int ip_getsockopt(struct sock
*sk
, int level
, int optname
, char *optval
, int *optlen
)
655 #ifdef CONFIG_IP_MROUTE
656 if(optname
>=MRT_BASE
&& optname
<=MRT_BASE
+10)
658 return ip_mroute_getsockopt(sk
,optname
,optval
,optlen
);
662 if(get_user(len
,optlen
))
670 unsigned char optbuf
[sizeof(struct ip_options
)+40];
671 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
673 if (sk
->protinfo
.af_inet
.opt
)
674 memcpy(optbuf
, sk
->protinfo
.af_inet
.opt
,
675 sizeof(struct ip_options
)+
676 sk
->protinfo
.af_inet
.opt
->optlen
);
679 if (opt
->optlen
== 0)
680 return put_user(0, optlen
);
682 ip_options_undo(opt
);
684 len
=min(len
, opt
->optlen
);
685 if(put_user(len
, optlen
))
687 if(copy_to_user(optval
, opt
->__data
, len
))
692 val
= (sk
->protinfo
.af_inet
.cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
695 val
= (sk
->protinfo
.af_inet
.cmsg_flags
& IP_CMSG_TTL
) != 0;
698 val
= (sk
->protinfo
.af_inet
.cmsg_flags
& IP_CMSG_TOS
) != 0;
701 val
= (sk
->protinfo
.af_inet
.cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
704 val
= (sk
->protinfo
.af_inet
.cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
707 val
=sk
->protinfo
.af_inet
.tos
;
710 val
=sk
->protinfo
.af_inet
.ttl
;
713 val
=sk
->protinfo
.af_inet
.hdrincl
;
715 case IP_MTU_DISCOVER
:
716 val
=sk
->protinfo
.af_inet
.pmtudisc
;
720 struct dst_entry
*dst
;
722 dst
= sk_dst_get(sk
);
734 val
=sk
->protinfo
.af_inet
.recverr
;
736 case IP_MULTICAST_TTL
:
737 val
=sk
->protinfo
.af_inet
.mc_ttl
;
739 case IP_MULTICAST_LOOP
:
740 val
=sk
->protinfo
.af_inet
.mc_loop
;
742 case IP_MULTICAST_IF
:
745 len
= min(len
,sizeof(struct in_addr
));
746 addr
.s_addr
= sk
->protinfo
.af_inet
.mc_addr
;
749 if(put_user(len
, optlen
))
751 if(copy_to_user((void *)optval
, &addr
, len
))
761 if (sk
->type
!= SOCK_STREAM
)
764 msg
.msg_control
= optval
;
765 msg
.msg_controllen
= len
;
768 if (sk
->protinfo
.af_inet
.cmsg_flags
&IP_CMSG_PKTINFO
) {
769 struct in_pktinfo info
;
771 info
.ipi_addr
.s_addr
= sk
->rcv_saddr
;
772 info
.ipi_spec_dst
.s_addr
= sk
->rcv_saddr
;
773 info
.ipi_ifindex
= sk
->protinfo
.af_inet
.mc_index
;
774 put_cmsg(&msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
776 if (sk
->protinfo
.af_inet
.cmsg_flags
&IP_CMSG_TTL
) {
777 int hlim
= sk
->protinfo
.af_inet
.mc_ttl
;
778 put_cmsg(&msg
, SOL_IP
, IP_TTL
, sizeof(hlim
), &hlim
);
780 len
-= msg
.msg_controllen
;
781 return put_user(len
, optlen
);
784 val
= sk
->protinfo
.af_inet
.freebind
;
787 #ifdef CONFIG_NETFILTER
788 val
= nf_getsockopt(sk
, PF_INET
, optname
, optval
,
792 val
= put_user(len
, optlen
);
801 if (len
< sizeof(int) && len
> 0 && val
>=0 && val
<255) {
802 unsigned char ucval
= (unsigned char)val
;
804 if(put_user(len
, optlen
))
806 if(copy_to_user(optval
,&ucval
,1))
809 len
=min(sizeof(int),len
);
810 if(put_user(len
, optlen
))
812 if(copy_to_user(optval
,&val
,len
))