3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: datagram.c,v 1.24 2002/02/01 22:01:04 davem Exp $
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/interrupt.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/in6.h>
25 #include <linux/ipv6.h>
26 #include <linux/route.h>
29 #include <net/ndisc.h>
30 #include <net/addrconf.h>
31 #include <net/transp_v6.h>
32 #include <net/ip6_route.h>
33 #include <net/tcp_states.h>
35 #include <linux/errqueue.h>
36 #include <asm/uaccess.h>
38 int ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
40 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
41 struct inet_sock
*inet
= inet_sk(sk
);
42 struct ipv6_pinfo
*np
= inet6_sk(sk
);
43 struct in6_addr
*daddr
, *final_p
= NULL
, final
;
44 struct dst_entry
*dst
;
46 struct ip6_flowlabel
*flowlabel
= NULL
;
50 if (usin
->sin6_family
== AF_INET
) {
51 if (__ipv6_only_sock(sk
))
53 err
= ip4_datagram_connect(sk
, uaddr
, addr_len
);
57 if (addr_len
< SIN6_LEN_RFC2133
)
60 if (usin
->sin6_family
!= AF_INET6
)
63 memset(&fl
, 0, sizeof(fl
));
65 fl
.fl6_flowlabel
= usin
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
66 if (fl
.fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) {
67 flowlabel
= fl6_sock_lookup(sk
, fl
.fl6_flowlabel
);
68 if (flowlabel
== NULL
)
70 ipv6_addr_copy(&usin
->sin6_addr
, &flowlabel
->dst
);
74 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
76 if (addr_type
== IPV6_ADDR_ANY
) {
80 usin
->sin6_addr
.s6_addr
[15] = 0x01;
83 daddr
= &usin
->sin6_addr
;
85 if (addr_type
== IPV6_ADDR_MAPPED
) {
86 struct sockaddr_in sin
;
88 if (__ipv6_only_sock(sk
)) {
92 sin
.sin_family
= AF_INET
;
93 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
94 sin
.sin_port
= usin
->sin6_port
;
96 err
= ip4_datagram_connect(sk
,
97 (struct sockaddr
*) &sin
,
104 ipv6_addr_set(&np
->daddr
, 0, 0, htonl(0x0000ffff), inet
->daddr
);
106 if (ipv6_addr_any(&np
->saddr
)) {
107 ipv6_addr_set(&np
->saddr
, 0, 0, htonl(0x0000ffff),
111 if (ipv6_addr_any(&np
->rcv_saddr
)) {
112 ipv6_addr_set(&np
->rcv_saddr
, 0, 0, htonl(0x0000ffff),
118 if (addr_type
&IPV6_ADDR_LINKLOCAL
) {
119 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
120 usin
->sin6_scope_id
) {
121 if (sk
->sk_bound_dev_if
&&
122 sk
->sk_bound_dev_if
!= usin
->sin6_scope_id
) {
126 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
127 if (!sk
->sk_bound_dev_if
&&
128 (addr_type
& IPV6_ADDR_MULTICAST
))
129 fl
.oif
= np
->mcast_oif
;
132 /* Connect to link-local address requires an interface */
133 if (!sk
->sk_bound_dev_if
) {
139 ipv6_addr_copy(&np
->daddr
, daddr
);
140 np
->flow_label
= fl
.fl6_flowlabel
;
142 inet
->dport
= usin
->sin6_port
;
145 * Check for a route to destination an obtain the
146 * destination cache for it.
149 fl
.proto
= sk
->sk_protocol
;
150 ipv6_addr_copy(&fl
.fl6_dst
, &np
->daddr
);
151 ipv6_addr_copy(&fl
.fl6_src
, &np
->saddr
);
152 fl
.oif
= sk
->sk_bound_dev_if
;
153 fl
.fl_ip_dport
= inet
->dport
;
154 fl
.fl_ip_sport
= inet
->sport
;
156 if (!fl
.oif
&& (addr_type
&IPV6_ADDR_MULTICAST
))
157 fl
.oif
= np
->mcast_oif
;
160 if (flowlabel
->opt
&& flowlabel
->opt
->srcrt
) {
161 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) flowlabel
->opt
->srcrt
;
162 ipv6_addr_copy(&final
, &fl
.fl6_dst
);
163 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
166 } else if (np
->opt
&& np
->opt
->srcrt
) {
167 struct rt0_hdr
*rt0
= (struct rt0_hdr
*)np
->opt
->srcrt
;
168 ipv6_addr_copy(&final
, &fl
.fl6_dst
);
169 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
173 err
= ip6_dst_lookup(sk
, &dst
, &fl
);
177 ipv6_addr_copy(&fl
.fl6_dst
, final_p
);
179 if ((err
= xfrm_lookup(&dst
, &fl
, sk
, 0)) < 0)
182 /* source address lookup done in ip6_dst_lookup */
184 if (ipv6_addr_any(&np
->saddr
))
185 ipv6_addr_copy(&np
->saddr
, &fl
.fl6_src
);
187 if (ipv6_addr_any(&np
->rcv_saddr
)) {
188 ipv6_addr_copy(&np
->rcv_saddr
, &fl
.fl6_src
);
189 inet
->rcv_saddr
= LOOPBACK4_IPV6
;
192 ip6_dst_store(sk
, dst
,
193 ipv6_addr_equal(&fl
.fl6_dst
, &np
->daddr
) ?
196 sk
->sk_state
= TCP_ESTABLISHED
;
198 fl6_sock_release(flowlabel
);
202 void ipv6_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
203 u16 port
, u32 info
, u8
*payload
)
205 struct ipv6_pinfo
*np
= inet6_sk(sk
);
206 struct icmp6hdr
*icmph
= (struct icmp6hdr
*)skb
->h
.raw
;
207 struct sock_exterr_skb
*serr
;
212 skb
= skb_clone(skb
, GFP_ATOMIC
);
216 serr
= SKB_EXT_ERR(skb
);
217 serr
->ee
.ee_errno
= err
;
218 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP6
;
219 serr
->ee
.ee_type
= icmph
->icmp6_type
;
220 serr
->ee
.ee_code
= icmph
->icmp6_code
;
222 serr
->ee
.ee_info
= info
;
223 serr
->ee
.ee_data
= 0;
224 serr
->addr_offset
= (u8
*)&(((struct ipv6hdr
*)(icmph
+1))->daddr
) - skb
->nh
.raw
;
227 skb
->h
.raw
= payload
;
228 __skb_pull(skb
, payload
- skb
->data
);
230 if (sock_queue_err_skb(sk
, skb
))
234 void ipv6_local_error(struct sock
*sk
, int err
, struct flowi
*fl
, u32 info
)
236 struct ipv6_pinfo
*np
= inet6_sk(sk
);
237 struct sock_exterr_skb
*serr
;
244 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
248 iph
= (struct ipv6hdr
*)skb_put(skb
, sizeof(struct ipv6hdr
));
250 ipv6_addr_copy(&iph
->daddr
, &fl
->fl6_dst
);
252 serr
= SKB_EXT_ERR(skb
);
253 serr
->ee
.ee_errno
= err
;
254 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
255 serr
->ee
.ee_type
= 0;
256 serr
->ee
.ee_code
= 0;
258 serr
->ee
.ee_info
= info
;
259 serr
->ee
.ee_data
= 0;
260 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb
->nh
.raw
;
261 serr
->port
= fl
->fl_ip_dport
;
263 skb
->h
.raw
= skb
->tail
;
264 __skb_pull(skb
, skb
->tail
- skb
->data
);
266 if (sock_queue_err_skb(sk
, skb
))
271 * Handle MSG_ERRQUEUE
273 int ipv6_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
275 struct ipv6_pinfo
*np
= inet6_sk(sk
);
276 struct sock_exterr_skb
*serr
;
277 struct sk_buff
*skb
, *skb2
;
278 struct sockaddr_in6
*sin
;
280 struct sock_extended_err ee
;
281 struct sockaddr_in6 offender
;
287 skb
= skb_dequeue(&sk
->sk_error_queue
);
293 msg
->msg_flags
|= MSG_TRUNC
;
296 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
300 sock_recv_timestamp(msg
, sk
, skb
);
302 serr
= SKB_EXT_ERR(skb
);
304 sin
= (struct sockaddr_in6
*)msg
->msg_name
;
306 sin
->sin6_family
= AF_INET6
;
307 sin
->sin6_flowinfo
= 0;
308 sin
->sin6_port
= serr
->port
;
309 sin
->sin6_scope_id
= 0;
310 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
) {
311 ipv6_addr_copy(&sin
->sin6_addr
,
312 (struct in6_addr
*)(skb
->nh
.raw
+ serr
->addr_offset
));
314 sin
->sin6_flowinfo
= *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
- 24) & IPV6_FLOWINFO_MASK
;
315 if (ipv6_addr_type(&sin
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
316 sin
->sin6_scope_id
= IP6CB(skb
)->iif
;
318 ipv6_addr_set(&sin
->sin6_addr
, 0, 0,
320 *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
));
324 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
325 sin
= &errhdr
.offender
;
326 sin
->sin6_family
= AF_UNSPEC
;
327 if (serr
->ee
.ee_origin
!= SO_EE_ORIGIN_LOCAL
) {
328 sin
->sin6_family
= AF_INET6
;
329 sin
->sin6_flowinfo
= 0;
330 sin
->sin6_scope_id
= 0;
331 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
) {
332 ipv6_addr_copy(&sin
->sin6_addr
, &skb
->nh
.ipv6h
->saddr
);
334 datagram_recv_ctl(sk
, msg
, skb
);
335 if (ipv6_addr_type(&sin
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
336 sin
->sin6_scope_id
= IP6CB(skb
)->iif
;
338 struct inet_sock
*inet
= inet_sk(sk
);
340 ipv6_addr_set(&sin
->sin6_addr
, 0, 0,
343 if (inet
->cmsg_flags
)
344 ip_cmsg_recv(msg
, skb
);
348 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVERR
, sizeof(errhdr
), &errhdr
);
350 /* Now we could try to dump offended packet options */
352 msg
->msg_flags
|= MSG_ERRQUEUE
;
355 /* Reset and regenerate socket error */
356 spin_lock_bh(&sk
->sk_error_queue
.lock
);
358 if ((skb2
= skb_peek(&sk
->sk_error_queue
)) != NULL
) {
359 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
360 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
361 sk
->sk_error_report(sk
);
363 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
374 int datagram_recv_ctl(struct sock
*sk
, struct msghdr
*msg
, struct sk_buff
*skb
)
376 struct ipv6_pinfo
*np
= inet6_sk(sk
);
377 struct inet6_skb_parm
*opt
= IP6CB(skb
);
379 if (np
->rxopt
.bits
.rxinfo
) {
380 struct in6_pktinfo src_info
;
382 src_info
.ipi6_ifindex
= opt
->iif
;
383 ipv6_addr_copy(&src_info
.ipi6_addr
, &skb
->nh
.ipv6h
->daddr
);
384 put_cmsg(msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
387 if (np
->rxopt
.bits
.rxhlim
) {
388 int hlim
= skb
->nh
.ipv6h
->hop_limit
;
389 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
392 if (np
->rxopt
.bits
.rxtclass
) {
393 int tclass
= (ntohl(*(u32
*)skb
->nh
.ipv6h
) >> 20) & 0xff;
394 put_cmsg(msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
397 if (np
->rxopt
.bits
.rxflow
&& (*(u32
*)skb
->nh
.raw
& IPV6_FLOWINFO_MASK
)) {
398 u32 flowinfo
= *(u32
*)skb
->nh
.raw
& IPV6_FLOWINFO_MASK
;
399 put_cmsg(msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
402 /* HbH is allowed only once */
403 if (np
->rxopt
.bits
.hopopts
&& opt
->hop
) {
404 u8
*ptr
= skb
->nh
.raw
+ opt
->hop
;
405 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
409 (np
->rxopt
.bits
.dstopts
|| np
->rxopt
.bits
.srcrt
)) {
411 * Silly enough, but we need to reparse in order to
412 * report extension headers (except for HbH)
415 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
416 * (and WILL NOT be) defined because
417 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
419 unsigned int off
= sizeof(struct ipv6hdr
);
420 u8 nexthdr
= skb
->nh
.ipv6h
->nexthdr
;
422 while (off
<= opt
->lastopt
) {
424 u8
*ptr
= skb
->nh
.raw
+ off
;
427 case IPPROTO_DSTOPTS
:
429 len
= (ptr
[1] + 1) << 3;
430 if (np
->rxopt
.bits
.dstopts
)
431 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, len
, ptr
);
433 case IPPROTO_ROUTING
:
435 len
= (ptr
[1] + 1) << 3;
436 if (np
->rxopt
.bits
.srcrt
)
437 put_cmsg(msg
, SOL_IPV6
, IPV6_RTHDR
, len
, ptr
);
441 len
= (ptr
[1] + 2) << 2;
445 len
= (ptr
[1] + 1) << 3;
453 /* socket options in old style */
454 if (np
->rxopt
.bits
.rxoinfo
) {
455 struct in6_pktinfo src_info
;
457 src_info
.ipi6_ifindex
= opt
->iif
;
458 ipv6_addr_copy(&src_info
.ipi6_addr
, &skb
->nh
.ipv6h
->daddr
);
459 put_cmsg(msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
461 if (np
->rxopt
.bits
.rxohlim
) {
462 int hlim
= skb
->nh
.ipv6h
->hop_limit
;
463 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
465 if (np
->rxopt
.bits
.ohopopts
&& opt
->hop
) {
466 u8
*ptr
= skb
->nh
.raw
+ opt
->hop
;
467 put_cmsg(msg
, SOL_IPV6
, IPV6_2292HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
469 if (np
->rxopt
.bits
.odstopts
&& opt
->dst0
) {
470 u8
*ptr
= skb
->nh
.raw
+ opt
->dst0
;
471 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
473 if (np
->rxopt
.bits
.osrcrt
&& opt
->srcrt
) {
474 struct ipv6_rt_hdr
*rthdr
= (struct ipv6_rt_hdr
*)(skb
->nh
.raw
+ opt
->srcrt
);
475 put_cmsg(msg
, SOL_IPV6
, IPV6_2292RTHDR
, (rthdr
->hdrlen
+1) << 3, rthdr
);
477 if (np
->rxopt
.bits
.odstopts
&& opt
->dst1
) {
478 u8
*ptr
= skb
->nh
.raw
+ opt
->dst1
;
479 put_cmsg(msg
, SOL_IPV6
, IPV6_2292DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
484 int datagram_send_ctl(struct msghdr
*msg
, struct flowi
*fl
,
485 struct ipv6_txoptions
*opt
,
486 int *hlimit
, int *tclass
)
488 struct in6_pktinfo
*src_info
;
489 struct cmsghdr
*cmsg
;
490 struct ipv6_rt_hdr
*rthdr
;
491 struct ipv6_opt_hdr
*hdr
;
495 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
497 struct net_device
*dev
= NULL
;
499 if (!CMSG_OK(msg
, cmsg
)) {
504 if (cmsg
->cmsg_level
!= SOL_IPV6
)
507 switch (cmsg
->cmsg_type
) {
509 case IPV6_2292PKTINFO
:
510 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct in6_pktinfo
))) {
515 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
517 if (src_info
->ipi6_ifindex
) {
518 if (fl
->oif
&& src_info
->ipi6_ifindex
!= fl
->oif
)
520 fl
->oif
= src_info
->ipi6_ifindex
;
523 addr_type
= ipv6_addr_type(&src_info
->ipi6_addr
);
525 if (addr_type
== IPV6_ADDR_ANY
)
528 if (addr_type
& IPV6_ADDR_LINKLOCAL
) {
529 if (!src_info
->ipi6_ifindex
)
532 dev
= dev_get_by_index(src_info
->ipi6_ifindex
);
537 if (!ipv6_chk_addr(&src_info
->ipi6_addr
, dev
, 0)) {
546 ipv6_addr_copy(&fl
->fl6_src
, &src_info
->ipi6_addr
);
550 if (cmsg
->cmsg_len
< CMSG_LEN(4)) {
555 if (fl
->fl6_flowlabel
&IPV6_FLOWINFO_MASK
) {
556 if ((fl
->fl6_flowlabel
^*(u32
*)CMSG_DATA(cmsg
))&~IPV6_FLOWINFO_MASK
) {
561 fl
->fl6_flowlabel
= IPV6_FLOWINFO_MASK
& *(u32
*)CMSG_DATA(cmsg
);
564 case IPV6_2292HOPOPTS
:
566 if (opt
->hopopt
|| cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
571 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
572 len
= ((hdr
->hdrlen
+ 1) << 3);
573 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
577 if (!capable(CAP_NET_RAW
)) {
581 opt
->opt_nflen
+= len
;
585 case IPV6_2292DSTOPTS
:
586 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
591 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
592 len
= ((hdr
->hdrlen
+ 1) << 3);
593 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
597 if (!capable(CAP_NET_RAW
)) {
605 opt
->opt_flen
+= len
;
610 case IPV6_RTHDRDSTOPTS
:
611 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
616 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
617 len
= ((hdr
->hdrlen
+ 1) << 3);
618 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
622 if (!capable(CAP_NET_RAW
)) {
626 if (cmsg
->cmsg_type
== IPV6_DSTOPTS
) {
627 opt
->opt_flen
+= len
;
630 opt
->opt_nflen
+= len
;
637 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_rt_hdr
))) {
642 rthdr
= (struct ipv6_rt_hdr
*)CMSG_DATA(cmsg
);
652 len
= ((rthdr
->hdrlen
+ 1) << 3);
654 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
659 /* segments left must also match */
660 if ((rthdr
->hdrlen
>> 1) != rthdr
->segments_left
) {
665 opt
->opt_nflen
+= len
;
668 if (cmsg
->cmsg_type
== IPV6_2292RTHDR
&& opt
->dst1opt
) {
669 int dsthdrlen
= ((opt
->dst1opt
->hdrlen
+1)<<3);
671 opt
->opt_nflen
+= dsthdrlen
;
672 opt
->dst0opt
= opt
->dst1opt
;
674 opt
->opt_flen
-= dsthdrlen
;
679 case IPV6_2292HOPLIMIT
:
681 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
686 *hlimit
= *(int *)CMSG_DATA(cmsg
);
694 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
698 tc
= *(int *)CMSG_DATA(cmsg
);
699 if (tc
< 0 || tc
> 0xff)
708 LIMIT_NETDEBUG(KERN_DEBUG
"invalid cmsg type: %d\n",