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.42 1999/04/22 10:07:34 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/firewall.h>
36 #include <linux/ip_fw.h>
37 #include <linux/route.h>
38 #include <linux/mroute.h>
39 #include <net/route.h>
40 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
41 #include <net/transp_v6.h>
44 #ifdef CONFIG_IP_MASQUERADE
45 #include <linux/ip_masq.h>
48 #include <linux/errqueue.h>
49 #include <asm/uaccess.h>
51 #define MAX(a,b) ((a)>(b)?(a):(b))
53 #define IP_CMSG_PKTINFO 1
56 #define IP_CMSG_RECVOPTS 8
57 #define IP_CMSG_RETOPTS 16
60 * SOL_IP control messages.
63 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
65 struct in_pktinfo info
;
66 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
68 info
.ipi_addr
.s_addr
= skb
->nh
.iph
->daddr
;
70 info
.ipi_ifindex
= rt
->rt_iif
;
71 info
.ipi_spec_dst
.s_addr
= rt
->rt_spec_dst
;
74 info
.ipi_spec_dst
.s_addr
= 0;
77 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
80 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
82 int ttl
= skb
->nh
.iph
->ttl
;
83 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
86 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
88 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &skb
->nh
.iph
->tos
);
91 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
93 if (IPCB(skb
)->opt
.optlen
== 0)
96 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
, skb
->nh
.iph
+1);
100 void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
102 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
103 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
105 if (IPCB(skb
)->opt
.optlen
== 0)
108 if (ip_options_echo(opt
, skb
)) {
109 msg
->msg_flags
|= MSG_CTRUNC
;
112 ip_options_undo(opt
);
114 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
118 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
120 unsigned flags
= skb
->sk
->ip_cmsg_flags
;
122 /* Ordered by supposed usage frequency */
124 ip_cmsg_recv_pktinfo(msg
, skb
);
125 if ((flags
>>=1) == 0)
129 ip_cmsg_recv_ttl(msg
, skb
);
130 if ((flags
>>=1) == 0)
134 ip_cmsg_recv_tos(msg
, skb
);
135 if ((flags
>>=1) == 0)
139 ip_cmsg_recv_opts(msg
, skb
);
140 if ((flags
>>=1) == 0)
144 ip_cmsg_recv_retopts(msg
, skb
);
147 int ip_cmsg_send(struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
150 struct cmsghdr
*cmsg
;
152 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
153 if (cmsg
->cmsg_len
< sizeof(struct cmsghdr
) ||
154 (unsigned long)(((char*)cmsg
- (char*)msg
->msg_control
)
155 + cmsg
->cmsg_len
) > msg
->msg_controllen
) {
158 if (cmsg
->cmsg_level
!= SOL_IP
)
160 switch (cmsg
->cmsg_type
) {
162 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
163 err
= ip_options_get(&ipc
->opt
, CMSG_DATA(cmsg
), err
< 40 ? err
: 40, 0);
169 struct in_pktinfo
*info
;
170 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
172 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
173 ipc
->oif
= info
->ipi_ifindex
;
174 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
185 /* Special input handler for packets catched by router alert option.
186 They are selected only by protocol field, and then processed likely
187 local ones; but only if someone wants them! Otherwise, router
188 not running rsvpd will kill RSVP.
190 It is user level problem, what it will make with them.
191 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
192 but receiver should be enough clever f.e. to forward mtrace requests,
193 sent to multicast group to reach destination designated router.
195 struct ip_ra_chain
*ip_ra_chain
;
197 int ip_ra_control(struct sock
*sk
, unsigned char on
, void (*destructor
)(struct sock
*))
199 struct ip_ra_chain
*ra
, *new_ra
, **rap
;
201 if (sk
->type
!= SOCK_RAW
|| sk
->num
== IPPROTO_RAW
)
204 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
206 for (rap
= &ip_ra_chain
; (ra
=*rap
) != NULL
; rap
= &ra
->next
) {
225 new_ra
->destructor
= destructor
;
234 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
235 u16 port
, u32 info
, u8
*payload
)
237 struct sock_exterr_skb
*serr
;
242 skb
= skb_clone(skb
, GFP_ATOMIC
);
246 serr
= SKB_EXT_ERR(skb
);
247 serr
->ee
.ee_errno
= err
;
248 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
249 serr
->ee
.ee_type
= skb
->h
.icmph
->type
;
250 serr
->ee
.ee_code
= skb
->h
.icmph
->code
;
252 serr
->ee
.ee_info
= info
;
253 serr
->ee
.ee_data
= 0;
254 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(skb
->h
.icmph
+1))->daddr
) - skb
->nh
.raw
;
257 skb
->h
.raw
= payload
;
258 skb_pull(skb
, payload
- skb
->data
);
260 if (sock_queue_err_skb(sk
, skb
))
264 void ip_local_error(struct sock
*sk
, int err
, u32 daddr
, u16 port
, u32 info
)
266 struct sock_exterr_skb
*serr
;
273 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
277 iph
= (struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
));
281 serr
= SKB_EXT_ERR(skb
);
282 serr
->ee
.ee_errno
= err
;
283 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
284 serr
->ee
.ee_type
= 0;
285 serr
->ee
.ee_code
= 0;
287 serr
->ee
.ee_info
= info
;
288 serr
->ee
.ee_data
= 0;
289 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb
->nh
.raw
;
292 skb
->h
.raw
= skb
->tail
;
293 skb_pull(skb
, skb
->tail
- skb
->data
);
295 if (sock_queue_err_skb(sk
, skb
))
300 * Handle MSG_ERRQUEUE
302 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
304 struct sock_exterr_skb
*serr
;
305 struct sk_buff
*skb
, *skb2
;
306 struct sockaddr_in
*sin
;
308 struct sock_extended_err ee
;
309 struct sockaddr_in offender
;
315 skb
= skb_dequeue(&sk
->error_queue
);
321 msg
->msg_flags
|= MSG_TRUNC
;
324 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, copied
);
328 serr
= SKB_EXT_ERR(skb
);
330 sin
= (struct sockaddr_in
*)msg
->msg_name
;
332 sin
->sin_family
= AF_INET
;
333 sin
->sin_addr
.s_addr
= *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
);
334 sin
->sin_port
= serr
->port
;
337 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
338 sin
= &errhdr
.offender
;
339 sin
->sin_family
= AF_UNSPEC
;
340 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
341 sin
->sin_family
= AF_INET
;
342 sin
->sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
343 if (sk
->ip_cmsg_flags
)
344 ip_cmsg_recv(msg
, skb
);
347 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
349 /* Now we could try to dump offended packet options */
351 msg
->msg_flags
|= MSG_ERRQUEUE
;
354 /* Reset and regenerate socket error */
356 if ((skb2
= skb_peek(&sk
->error_queue
)) != NULL
) {
357 sk
->err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
358 sk
->error_report(sk
);
369 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
372 * We implement IP_TOS (type of service), IP_TTL (time to live).
375 int ip_setsockopt(struct sock
*sk
, int level
, int optname
, char *optval
, int optlen
)
378 #if defined(CONFIG_IP_FIREWALL)
379 char tmp_fw
[MAX(sizeof(struct ip_fwtest
),sizeof(struct ip_fwnew
))];
381 if(optlen
>=sizeof(int)) {
382 if(get_user(val
, (int *) optval
))
384 } else if(optlen
>=sizeof(char)) {
386 if(get_user(ucval
, (unsigned char *) optval
))
390 /* If optlen==0, it is equivalent to val == 0 */
394 #ifdef CONFIG_IP_MROUTE
395 if(optname
>=MRT_BASE
&& optname
<=MRT_BASE
+10)
397 return ip_mroute_setsockopt(sk
,optname
,optval
,optlen
);
405 struct ip_options
* opt
= NULL
;
406 if (optlen
> 40 || optlen
< 0)
408 err
= ip_options_get(&opt
, optval
, optlen
, 1);
412 if (sk
->type
== SOCK_STREAM
) {
413 struct tcp_opt
*tp
= &sk
->tp_pinfo
.af_tcp
;
414 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
415 if (sk
->family
== PF_INET
||
416 ((tcp_connected(sk
->state
) || sk
->state
== TCP_SYN_SENT
)
417 && sk
->daddr
!= LOOPBACK4_IPV6
)) {
420 tp
->ext_header_len
= opt
->optlen
;
421 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
422 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
426 opt
= xchg(&sk
->opt
, opt
);
429 kfree_s(opt
, sizeof(struct ip_options
) + opt
->optlen
);
434 sk
->ip_cmsg_flags
|= IP_CMSG_PKTINFO
;
436 sk
->ip_cmsg_flags
&= ~IP_CMSG_PKTINFO
;
440 sk
->ip_cmsg_flags
|= IP_CMSG_TTL
;
442 sk
->ip_cmsg_flags
&= ~IP_CMSG_TTL
;
446 sk
->ip_cmsg_flags
|= IP_CMSG_TOS
;
448 sk
->ip_cmsg_flags
&= ~IP_CMSG_TOS
;
452 sk
->ip_cmsg_flags
|= IP_CMSG_RECVOPTS
;
454 sk
->ip_cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
458 sk
->ip_cmsg_flags
|= IP_CMSG_RETOPTS
;
460 sk
->ip_cmsg_flags
&= ~IP_CMSG_RETOPTS
;
462 case IP_TOS
: /* This sets both TOS and Precedence */
463 /* Reject setting of unused bits */
464 if (val
& ~(IPTOS_TOS_MASK
|IPTOS_PREC_MASK
))
466 if (IPTOS_PREC(val
) >= IPTOS_PREC_CRITIC_ECP
&&
467 !capable(CAP_NET_ADMIN
))
469 if (sk
->ip_tos
!= val
) {
472 sk
->priority
= rt_tos2priority(val
);
473 dst_release(xchg(&sk
->dst_cache
, NULL
));
481 val
= ip_statistics
.IpDefaultTTL
;
487 if(sk
->type
!=SOCK_RAW
)
489 sk
->ip_hdrincl
=val
?1:0;
491 case IP_MTU_DISCOVER
:
494 sk
->ip_pmtudisc
= val
;
497 sk
->ip_recverr
= !!val
;
499 skb_queue_purge(&sk
->error_queue
);
501 case IP_MULTICAST_TTL
:
506 if (val
< 0 || val
> 255)
510 case IP_MULTICAST_LOOP
:
513 sk
->ip_mc_loop
= val
? 1 : 0;
515 case IP_MULTICAST_IF
:
517 struct ip_mreqn mreq
;
518 struct device
*dev
= NULL
;
521 * Check the arguments are allowable
524 if (optlen
>= sizeof(struct ip_mreqn
)) {
525 if (copy_from_user(&mreq
,optval
,sizeof(mreq
)))
528 memset(&mreq
, 0, sizeof(mreq
));
529 if (optlen
>= sizeof(struct in_addr
) &&
530 copy_from_user(&mreq
.imr_address
,optval
,sizeof(struct in_addr
)))
534 if (!mreq
.imr_ifindex
) {
535 if (mreq
.imr_address
.s_addr
== INADDR_ANY
) {
540 dev
= ip_dev_find(mreq
.imr_address
.s_addr
);
542 dev
= dev_get_by_index(mreq
.imr_ifindex
);
545 return -EADDRNOTAVAIL
;
547 if (sk
->bound_dev_if
&& dev
->ifindex
!= sk
->bound_dev_if
)
550 sk
->ip_mc_index
= mreq
.imr_ifindex
;
551 sk
->ip_mc_addr
= mreq
.imr_address
.s_addr
;
555 case IP_ADD_MEMBERSHIP
:
556 case IP_DROP_MEMBERSHIP
:
558 struct ip_mreqn mreq
;
560 if (optlen
< sizeof(struct ip_mreq
))
562 if (optlen
>= sizeof(struct ip_mreqn
)) {
563 if(copy_from_user(&mreq
,optval
,sizeof(mreq
)))
566 memset(&mreq
, 0, sizeof(mreq
));
567 if (copy_from_user(&mreq
,optval
,sizeof(struct ip_mreq
)))
571 if (optname
== IP_ADD_MEMBERSHIP
)
572 return ip_mc_join_group(sk
,&mreq
);
574 return ip_mc_leave_group(sk
,&mreq
);
576 case IP_ROUTER_ALERT
:
577 return ip_ra_control(sk
, val
? 1 : 0, NULL
);
579 #ifdef CONFIG_IP_FIREWALL
580 case IP_FW_MASQ_TIMEOUTS
:
584 case IP_FW_DELETE_NUM
:
589 case IP_FW_CREATECHAIN
:
590 case IP_FW_DELETECHAIN
:
592 if(!capable(CAP_NET_ADMIN
))
594 if(optlen
>sizeof(tmp_fw
) || optlen
<1)
596 if(copy_from_user(&tmp_fw
,optval
,optlen
))
598 err
=ip_fw_ctl(optname
, &tmp_fw
,optlen
);
599 return -err
; /* -0 is 0 after all */
600 #endif /* CONFIG_IP_FIREWALL */
601 #ifdef CONFIG_IP_MASQUERADE
603 if(!capable(CAP_NET_ADMIN
))
607 err
=ip_masq_uctl(optname
, optval
,optlen
);
612 return(-ENOPROTOOPT
);
617 * Get the options. Note for future reference. The GET of IP options gets the
618 * _received_ ones. The set sets the _sent_ ones.
621 int ip_getsockopt(struct sock
*sk
, int level
, int optname
, char *optval
, int *optlen
)
629 #ifdef CONFIG_IP_MROUTE
630 if(optname
>=MRT_BASE
&& optname
<=MRT_BASE
+10)
632 return ip_mroute_getsockopt(sk
,optname
,optval
,optlen
);
636 if(get_user(len
,optlen
))
643 unsigned char optbuf
[sizeof(struct ip_options
)+40];
644 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
648 memcpy(optbuf
, sk
->opt
, sizeof(struct ip_options
)+sk
->opt
->optlen
);
650 if (opt
->optlen
== 0)
651 return put_user(0, optlen
);
653 ip_options_undo(opt
);
655 len
=min(len
, opt
->optlen
);
656 if(put_user(len
, optlen
))
658 if(copy_to_user(optval
, opt
->__data
, len
))
663 val
= (sk
->ip_cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
666 val
= (sk
->ip_cmsg_flags
& IP_CMSG_TTL
) != 0;
669 val
= (sk
->ip_cmsg_flags
& IP_CMSG_TOS
) != 0;
672 val
= (sk
->ip_cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
675 val
= (sk
->ip_cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
686 case IP_MTU_DISCOVER
:
693 val
= sk
->dst_cache
->pmtu
;
701 case IP_MULTICAST_TTL
:
704 case IP_MULTICAST_LOOP
:
707 case IP_MULTICAST_IF
:
709 struct ip_mreqn mreq
;
710 len
= min(len
,sizeof(struct ip_mreqn
));
711 if(put_user(len
, optlen
))
713 mreq
.imr_ifindex
= sk
->ip_mc_index
;
714 mreq
.imr_address
.s_addr
= sk
->ip_mc_addr
;
715 mreq
.imr_multiaddr
.s_addr
= 0;
716 if(copy_to_user((void *)optval
, &mreq
, len
))
721 return(-ENOPROTOOPT
);
724 if (len
< sizeof(int) && len
> 0 && val
>=0 && val
<255) {
725 unsigned char ucval
= (unsigned char)val
;
727 if(put_user(len
, optlen
))
729 if(copy_to_user(optval
,&ucval
,1))
732 len
=min(sizeof(int),len
);
733 if(put_user(len
, optlen
))
735 if(copy_to_user(optval
,&val
,len
))