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/errno.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/interrupt.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/in6.h>
24 #include <linux/ipv6.h>
25 #include <linux/route.h>
28 #include <net/ndisc.h>
29 #include <net/addrconf.h>
30 #include <net/transp_v6.h>
31 #include <net/ip6_route.h>
33 #include <linux/errqueue.h>
34 #include <asm/uaccess.h>
36 int ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
38 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
39 struct inet_sock
*inet
= inet_sk(sk
);
40 struct ipv6_pinfo
*np
= inet6_sk(sk
);
41 struct in6_addr
*daddr
, *final_p
= NULL
, final
;
42 struct dst_entry
*dst
;
44 struct ip6_flowlabel
*flowlabel
= NULL
;
48 if (usin
->sin6_family
== AF_INET
) {
49 if (__ipv6_only_sock(sk
))
51 err
= ip4_datagram_connect(sk
, uaddr
, addr_len
);
55 if (addr_len
< SIN6_LEN_RFC2133
)
58 if (usin
->sin6_family
!= AF_INET6
)
61 memset(&fl
, 0, sizeof(fl
));
63 fl
.fl6_flowlabel
= usin
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
64 if (fl
.fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) {
65 flowlabel
= fl6_sock_lookup(sk
, fl
.fl6_flowlabel
);
66 if (flowlabel
== NULL
)
68 ipv6_addr_copy(&usin
->sin6_addr
, &flowlabel
->dst
);
72 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
74 if (addr_type
== IPV6_ADDR_ANY
) {
78 usin
->sin6_addr
.s6_addr
[15] = 0x01;
81 daddr
= &usin
->sin6_addr
;
83 if (addr_type
== IPV6_ADDR_MAPPED
) {
84 struct sockaddr_in sin
;
86 if (__ipv6_only_sock(sk
)) {
90 sin
.sin_family
= AF_INET
;
91 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
92 sin
.sin_port
= usin
->sin6_port
;
94 err
= ip4_datagram_connect(sk
,
95 (struct sockaddr
*) &sin
,
102 ipv6_addr_set(&np
->daddr
, 0, 0, htonl(0x0000ffff), inet
->daddr
);
104 if (ipv6_addr_any(&np
->saddr
)) {
105 ipv6_addr_set(&np
->saddr
, 0, 0, htonl(0x0000ffff),
109 if (ipv6_addr_any(&np
->rcv_saddr
)) {
110 ipv6_addr_set(&np
->rcv_saddr
, 0, 0, htonl(0x0000ffff),
116 if (addr_type
&IPV6_ADDR_LINKLOCAL
) {
117 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
118 usin
->sin6_scope_id
) {
119 if (sk
->sk_bound_dev_if
&&
120 sk
->sk_bound_dev_if
!= usin
->sin6_scope_id
) {
124 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
125 if (!sk
->sk_bound_dev_if
&&
126 (addr_type
& IPV6_ADDR_MULTICAST
))
127 fl
.oif
= np
->mcast_oif
;
130 /* Connect to link-local address requires an interface */
131 if (!sk
->sk_bound_dev_if
) {
137 ipv6_addr_copy(&np
->daddr
, daddr
);
138 np
->flow_label
= fl
.fl6_flowlabel
;
140 inet
->dport
= usin
->sin6_port
;
143 * Check for a route to destination an obtain the
144 * destination cache for it.
147 fl
.proto
= sk
->sk_protocol
;
148 ipv6_addr_copy(&fl
.fl6_dst
, &np
->daddr
);
149 ipv6_addr_copy(&fl
.fl6_src
, &np
->saddr
);
150 fl
.oif
= sk
->sk_bound_dev_if
;
151 fl
.fl_ip_dport
= inet
->dport
;
152 fl
.fl_ip_sport
= inet
->sport
;
154 if (!fl
.oif
&& (addr_type
&IPV6_ADDR_MULTICAST
))
155 fl
.oif
= np
->mcast_oif
;
158 if (flowlabel
->opt
&& flowlabel
->opt
->srcrt
) {
159 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) flowlabel
->opt
->srcrt
;
160 ipv6_addr_copy(&final
, &fl
.fl6_dst
);
161 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
164 } else if (np
->opt
&& np
->opt
->srcrt
) {
165 struct rt0_hdr
*rt0
= (struct rt0_hdr
*)np
->opt
->srcrt
;
166 ipv6_addr_copy(&final
, &fl
.fl6_dst
);
167 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
171 err
= ip6_dst_lookup(sk
, &dst
, &fl
);
175 ipv6_addr_copy(&fl
.fl6_dst
, final_p
);
177 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_irq(&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_irq(&sk
->sk_error_queue
.lock
);
361 sk
->sk_error_report(sk
);
363 spin_unlock_irq(&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
.rxflow
&& (*(u32
*)skb
->nh
.raw
& IPV6_FLOWINFO_MASK
)) {
393 u32 flowinfo
= *(u32
*)skb
->nh
.raw
& IPV6_FLOWINFO_MASK
;
394 put_cmsg(msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
396 if (np
->rxopt
.bits
.hopopts
&& opt
->hop
) {
397 u8
*ptr
= skb
->nh
.raw
+ opt
->hop
;
398 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
400 if (np
->rxopt
.bits
.dstopts
&& opt
->dst0
) {
401 u8
*ptr
= skb
->nh
.raw
+ opt
->dst0
;
402 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
404 if (np
->rxopt
.bits
.srcrt
&& opt
->srcrt
) {
405 struct ipv6_rt_hdr
*rthdr
= (struct ipv6_rt_hdr
*)(skb
->nh
.raw
+ opt
->srcrt
);
406 put_cmsg(msg
, SOL_IPV6
, IPV6_RTHDR
, (rthdr
->hdrlen
+1) << 3, rthdr
);
408 if (np
->rxopt
.bits
.dstopts
&& opt
->dst1
) {
409 u8
*ptr
= skb
->nh
.raw
+ opt
->dst1
;
410 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
415 int datagram_send_ctl(struct msghdr
*msg
, struct flowi
*fl
,
416 struct ipv6_txoptions
*opt
,
419 struct in6_pktinfo
*src_info
;
420 struct cmsghdr
*cmsg
;
421 struct ipv6_rt_hdr
*rthdr
;
422 struct ipv6_opt_hdr
*hdr
;
426 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
428 struct net_device
*dev
= NULL
;
430 if (!CMSG_OK(msg
, cmsg
)) {
435 if (cmsg
->cmsg_level
!= SOL_IPV6
)
438 switch (cmsg
->cmsg_type
) {
440 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct in6_pktinfo
))) {
445 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
447 if (src_info
->ipi6_ifindex
) {
448 if (fl
->oif
&& src_info
->ipi6_ifindex
!= fl
->oif
)
450 fl
->oif
= src_info
->ipi6_ifindex
;
453 addr_type
= ipv6_addr_type(&src_info
->ipi6_addr
);
455 if (addr_type
== IPV6_ADDR_ANY
)
458 if (addr_type
& IPV6_ADDR_LINKLOCAL
) {
459 if (!src_info
->ipi6_ifindex
)
462 dev
= dev_get_by_index(src_info
->ipi6_ifindex
);
467 if (!ipv6_chk_addr(&src_info
->ipi6_addr
, dev
, 0)) {
476 ipv6_addr_copy(&fl
->fl6_src
, &src_info
->ipi6_addr
);
480 if (cmsg
->cmsg_len
< CMSG_LEN(4)) {
485 if (fl
->fl6_flowlabel
&IPV6_FLOWINFO_MASK
) {
486 if ((fl
->fl6_flowlabel
^*(u32
*)CMSG_DATA(cmsg
))&~IPV6_FLOWINFO_MASK
) {
491 fl
->fl6_flowlabel
= IPV6_FLOWINFO_MASK
& *(u32
*)CMSG_DATA(cmsg
);
495 if (opt
->hopopt
|| cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
500 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
501 len
= ((hdr
->hdrlen
+ 1) << 3);
502 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
506 if (!capable(CAP_NET_RAW
)) {
510 opt
->opt_nflen
+= len
;
515 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
520 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
521 len
= ((hdr
->hdrlen
+ 1) << 3);
522 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
526 if (!capable(CAP_NET_RAW
)) {
534 opt
->opt_flen
+= len
;
539 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_rt_hdr
))) {
544 rthdr
= (struct ipv6_rt_hdr
*)CMSG_DATA(cmsg
);
554 len
= ((rthdr
->hdrlen
+ 1) << 3);
556 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
561 /* segments left must also match */
562 if ((rthdr
->hdrlen
>> 1) != rthdr
->segments_left
) {
567 opt
->opt_nflen
+= len
;
571 int dsthdrlen
= ((opt
->dst1opt
->hdrlen
+1)<<3);
573 opt
->opt_nflen
+= dsthdrlen
;
574 opt
->dst0opt
= opt
->dst1opt
;
576 opt
->opt_flen
-= dsthdrlen
;
582 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
587 *hlimit
= *(int *)CMSG_DATA(cmsg
);
592 printk(KERN_DEBUG
"invalid cmsg type: %d\n", cmsg
->cmsg_type
));