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>
32 #include <net/tcp_states.h>
34 #include <linux/errqueue.h>
35 #include <asm/uaccess.h>
37 int ip6_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
39 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
40 struct inet_sock
*inet
= inet_sk(sk
);
41 struct ipv6_pinfo
*np
= inet6_sk(sk
);
42 struct in6_addr
*daddr
, *final_p
= NULL
, final
;
43 struct dst_entry
*dst
;
45 struct ip6_flowlabel
*flowlabel
= NULL
;
49 if (usin
->sin6_family
== AF_INET
) {
50 if (__ipv6_only_sock(sk
))
52 err
= ip4_datagram_connect(sk
, uaddr
, addr_len
);
56 if (addr_len
< SIN6_LEN_RFC2133
)
59 if (usin
->sin6_family
!= AF_INET6
)
62 memset(&fl
, 0, sizeof(fl
));
64 fl
.fl6_flowlabel
= usin
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
65 if (fl
.fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) {
66 flowlabel
= fl6_sock_lookup(sk
, fl
.fl6_flowlabel
);
67 if (flowlabel
== NULL
)
69 ipv6_addr_copy(&usin
->sin6_addr
, &flowlabel
->dst
);
73 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
75 if (addr_type
== IPV6_ADDR_ANY
) {
79 usin
->sin6_addr
.s6_addr
[15] = 0x01;
82 daddr
= &usin
->sin6_addr
;
84 if (addr_type
== IPV6_ADDR_MAPPED
) {
85 struct sockaddr_in sin
;
87 if (__ipv6_only_sock(sk
)) {
91 sin
.sin_family
= AF_INET
;
92 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
93 sin
.sin_port
= usin
->sin6_port
;
95 err
= ip4_datagram_connect(sk
,
96 (struct sockaddr
*) &sin
,
103 ipv6_addr_set(&np
->daddr
, 0, 0, htonl(0x0000ffff), inet
->daddr
);
105 if (ipv6_addr_any(&np
->saddr
)) {
106 ipv6_addr_set(&np
->saddr
, 0, 0, htonl(0x0000ffff),
110 if (ipv6_addr_any(&np
->rcv_saddr
)) {
111 ipv6_addr_set(&np
->rcv_saddr
, 0, 0, htonl(0x0000ffff),
117 if (addr_type
&IPV6_ADDR_LINKLOCAL
) {
118 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
119 usin
->sin6_scope_id
) {
120 if (sk
->sk_bound_dev_if
&&
121 sk
->sk_bound_dev_if
!= usin
->sin6_scope_id
) {
125 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
126 if (!sk
->sk_bound_dev_if
&&
127 (addr_type
& IPV6_ADDR_MULTICAST
))
128 fl
.oif
= np
->mcast_oif
;
131 /* Connect to link-local address requires an interface */
132 if (!sk
->sk_bound_dev_if
) {
138 ipv6_addr_copy(&np
->daddr
, daddr
);
139 np
->flow_label
= fl
.fl6_flowlabel
;
141 inet
->dport
= usin
->sin6_port
;
144 * Check for a route to destination an obtain the
145 * destination cache for it.
148 fl
.proto
= sk
->sk_protocol
;
149 ipv6_addr_copy(&fl
.fl6_dst
, &np
->daddr
);
150 ipv6_addr_copy(&fl
.fl6_src
, &np
->saddr
);
151 fl
.oif
= sk
->sk_bound_dev_if
;
152 fl
.fl_ip_dport
= inet
->dport
;
153 fl
.fl_ip_sport
= inet
->sport
;
155 if (!fl
.oif
&& (addr_type
&IPV6_ADDR_MULTICAST
))
156 fl
.oif
= np
->mcast_oif
;
159 if (flowlabel
->opt
&& flowlabel
->opt
->srcrt
) {
160 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) flowlabel
->opt
->srcrt
;
161 ipv6_addr_copy(&final
, &fl
.fl6_dst
);
162 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
165 } else if (np
->opt
&& np
->opt
->srcrt
) {
166 struct rt0_hdr
*rt0
= (struct rt0_hdr
*)np
->opt
->srcrt
;
167 ipv6_addr_copy(&final
, &fl
.fl6_dst
);
168 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
172 err
= ip6_dst_lookup(sk
, &dst
, &fl
);
176 ipv6_addr_copy(&fl
.fl6_dst
, final_p
);
178 if ((err
= xfrm_lookup(&dst
, &fl
, sk
, 0)) < 0) {
183 /* source address lookup done in ip6_dst_lookup */
185 if (ipv6_addr_any(&np
->saddr
))
186 ipv6_addr_copy(&np
->saddr
, &fl
.fl6_src
);
188 if (ipv6_addr_any(&np
->rcv_saddr
)) {
189 ipv6_addr_copy(&np
->rcv_saddr
, &fl
.fl6_src
);
190 inet
->rcv_saddr
= LOOPBACK4_IPV6
;
193 ip6_dst_store(sk
, dst
,
194 ipv6_addr_equal(&fl
.fl6_dst
, &np
->daddr
) ?
197 sk
->sk_state
= TCP_ESTABLISHED
;
199 fl6_sock_release(flowlabel
);
203 void ipv6_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
204 u16 port
, u32 info
, u8
*payload
)
206 struct ipv6_pinfo
*np
= inet6_sk(sk
);
207 struct icmp6hdr
*icmph
= (struct icmp6hdr
*)skb
->h
.raw
;
208 struct sock_exterr_skb
*serr
;
213 skb
= skb_clone(skb
, GFP_ATOMIC
);
217 serr
= SKB_EXT_ERR(skb
);
218 serr
->ee
.ee_errno
= err
;
219 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP6
;
220 serr
->ee
.ee_type
= icmph
->icmp6_type
;
221 serr
->ee
.ee_code
= icmph
->icmp6_code
;
223 serr
->ee
.ee_info
= info
;
224 serr
->ee
.ee_data
= 0;
225 serr
->addr_offset
= (u8
*)&(((struct ipv6hdr
*)(icmph
+1))->daddr
) - skb
->nh
.raw
;
228 skb
->h
.raw
= payload
;
229 __skb_pull(skb
, payload
- skb
->data
);
231 if (sock_queue_err_skb(sk
, skb
))
235 void ipv6_local_error(struct sock
*sk
, int err
, struct flowi
*fl
, u32 info
)
237 struct ipv6_pinfo
*np
= inet6_sk(sk
);
238 struct sock_exterr_skb
*serr
;
245 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
249 iph
= (struct ipv6hdr
*)skb_put(skb
, sizeof(struct ipv6hdr
));
251 ipv6_addr_copy(&iph
->daddr
, &fl
->fl6_dst
);
253 serr
= SKB_EXT_ERR(skb
);
254 serr
->ee
.ee_errno
= err
;
255 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
256 serr
->ee
.ee_type
= 0;
257 serr
->ee
.ee_code
= 0;
259 serr
->ee
.ee_info
= info
;
260 serr
->ee
.ee_data
= 0;
261 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb
->nh
.raw
;
262 serr
->port
= fl
->fl_ip_dport
;
264 skb
->h
.raw
= skb
->tail
;
265 __skb_pull(skb
, skb
->tail
- skb
->data
);
267 if (sock_queue_err_skb(sk
, skb
))
272 * Handle MSG_ERRQUEUE
274 int ipv6_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
276 struct ipv6_pinfo
*np
= inet6_sk(sk
);
277 struct sock_exterr_skb
*serr
;
278 struct sk_buff
*skb
, *skb2
;
279 struct sockaddr_in6
*sin
;
281 struct sock_extended_err ee
;
282 struct sockaddr_in6 offender
;
288 skb
= skb_dequeue(&sk
->sk_error_queue
);
294 msg
->msg_flags
|= MSG_TRUNC
;
297 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
301 sock_recv_timestamp(msg
, sk
, skb
);
303 serr
= SKB_EXT_ERR(skb
);
305 sin
= (struct sockaddr_in6
*)msg
->msg_name
;
307 sin
->sin6_family
= AF_INET6
;
308 sin
->sin6_flowinfo
= 0;
309 sin
->sin6_port
= serr
->port
;
310 sin
->sin6_scope_id
= 0;
311 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
) {
312 ipv6_addr_copy(&sin
->sin6_addr
,
313 (struct in6_addr
*)(skb
->nh
.raw
+ serr
->addr_offset
));
315 sin
->sin6_flowinfo
= *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
- 24) & IPV6_FLOWINFO_MASK
;
316 if (ipv6_addr_type(&sin
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
317 sin
->sin6_scope_id
= IP6CB(skb
)->iif
;
319 ipv6_addr_set(&sin
->sin6_addr
, 0, 0,
321 *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
));
325 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
326 sin
= &errhdr
.offender
;
327 sin
->sin6_family
= AF_UNSPEC
;
328 if (serr
->ee
.ee_origin
!= SO_EE_ORIGIN_LOCAL
) {
329 sin
->sin6_family
= AF_INET6
;
330 sin
->sin6_flowinfo
= 0;
331 sin
->sin6_scope_id
= 0;
332 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
) {
333 ipv6_addr_copy(&sin
->sin6_addr
, &skb
->nh
.ipv6h
->saddr
);
335 datagram_recv_ctl(sk
, msg
, skb
);
336 if (ipv6_addr_type(&sin
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
337 sin
->sin6_scope_id
= IP6CB(skb
)->iif
;
339 struct inet_sock
*inet
= inet_sk(sk
);
341 ipv6_addr_set(&sin
->sin6_addr
, 0, 0,
344 if (inet
->cmsg_flags
)
345 ip_cmsg_recv(msg
, skb
);
349 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVERR
, sizeof(errhdr
), &errhdr
);
351 /* Now we could try to dump offended packet options */
353 msg
->msg_flags
|= MSG_ERRQUEUE
;
356 /* Reset and regenerate socket error */
357 spin_lock_bh(&sk
->sk_error_queue
.lock
);
359 if ((skb2
= skb_peek(&sk
->sk_error_queue
)) != NULL
) {
360 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
361 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
362 sk
->sk_error_report(sk
);
364 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
375 int datagram_recv_ctl(struct sock
*sk
, struct msghdr
*msg
, struct sk_buff
*skb
)
377 struct ipv6_pinfo
*np
= inet6_sk(sk
);
378 struct inet6_skb_parm
*opt
= IP6CB(skb
);
380 if (np
->rxopt
.bits
.rxinfo
) {
381 struct in6_pktinfo src_info
;
383 src_info
.ipi6_ifindex
= opt
->iif
;
384 ipv6_addr_copy(&src_info
.ipi6_addr
, &skb
->nh
.ipv6h
->daddr
);
385 put_cmsg(msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
388 if (np
->rxopt
.bits
.rxhlim
) {
389 int hlim
= skb
->nh
.ipv6h
->hop_limit
;
390 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
393 if (np
->rxopt
.bits
.rxflow
&& (*(u32
*)skb
->nh
.raw
& IPV6_FLOWINFO_MASK
)) {
394 u32 flowinfo
= *(u32
*)skb
->nh
.raw
& IPV6_FLOWINFO_MASK
;
395 put_cmsg(msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
397 if (np
->rxopt
.bits
.hopopts
&& opt
->hop
) {
398 u8
*ptr
= skb
->nh
.raw
+ opt
->hop
;
399 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
401 if (np
->rxopt
.bits
.dstopts
&& opt
->dst0
) {
402 u8
*ptr
= skb
->nh
.raw
+ opt
->dst0
;
403 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
405 if (np
->rxopt
.bits
.srcrt
&& opt
->srcrt
) {
406 struct ipv6_rt_hdr
*rthdr
= (struct ipv6_rt_hdr
*)(skb
->nh
.raw
+ opt
->srcrt
);
407 put_cmsg(msg
, SOL_IPV6
, IPV6_RTHDR
, (rthdr
->hdrlen
+1) << 3, rthdr
);
409 if (np
->rxopt
.bits
.dstopts
&& opt
->dst1
) {
410 u8
*ptr
= skb
->nh
.raw
+ opt
->dst1
;
411 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
416 int datagram_send_ctl(struct msghdr
*msg
, struct flowi
*fl
,
417 struct ipv6_txoptions
*opt
,
420 struct in6_pktinfo
*src_info
;
421 struct cmsghdr
*cmsg
;
422 struct ipv6_rt_hdr
*rthdr
;
423 struct ipv6_opt_hdr
*hdr
;
427 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
429 struct net_device
*dev
= NULL
;
431 if (!CMSG_OK(msg
, cmsg
)) {
436 if (cmsg
->cmsg_level
!= SOL_IPV6
)
439 switch (cmsg
->cmsg_type
) {
441 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct in6_pktinfo
))) {
446 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
448 if (src_info
->ipi6_ifindex
) {
449 if (fl
->oif
&& src_info
->ipi6_ifindex
!= fl
->oif
)
451 fl
->oif
= src_info
->ipi6_ifindex
;
454 addr_type
= ipv6_addr_type(&src_info
->ipi6_addr
);
456 if (addr_type
== IPV6_ADDR_ANY
)
459 if (addr_type
& IPV6_ADDR_LINKLOCAL
) {
460 if (!src_info
->ipi6_ifindex
)
463 dev
= dev_get_by_index(src_info
->ipi6_ifindex
);
468 if (!ipv6_chk_addr(&src_info
->ipi6_addr
, dev
, 0)) {
477 ipv6_addr_copy(&fl
->fl6_src
, &src_info
->ipi6_addr
);
481 if (cmsg
->cmsg_len
< CMSG_LEN(4)) {
486 if (fl
->fl6_flowlabel
&IPV6_FLOWINFO_MASK
) {
487 if ((fl
->fl6_flowlabel
^*(u32
*)CMSG_DATA(cmsg
))&~IPV6_FLOWINFO_MASK
) {
492 fl
->fl6_flowlabel
= IPV6_FLOWINFO_MASK
& *(u32
*)CMSG_DATA(cmsg
);
496 if (opt
->hopopt
|| cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
501 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
502 len
= ((hdr
->hdrlen
+ 1) << 3);
503 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
507 if (!capable(CAP_NET_RAW
)) {
511 opt
->opt_nflen
+= len
;
516 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
521 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
522 len
= ((hdr
->hdrlen
+ 1) << 3);
523 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
527 if (!capable(CAP_NET_RAW
)) {
535 opt
->opt_flen
+= len
;
540 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_rt_hdr
))) {
545 rthdr
= (struct ipv6_rt_hdr
*)CMSG_DATA(cmsg
);
555 len
= ((rthdr
->hdrlen
+ 1) << 3);
557 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
562 /* segments left must also match */
563 if ((rthdr
->hdrlen
>> 1) != rthdr
->segments_left
) {
568 opt
->opt_nflen
+= len
;
572 int dsthdrlen
= ((opt
->dst1opt
->hdrlen
+1)<<3);
574 opt
->opt_nflen
+= dsthdrlen
;
575 opt
->dst0opt
= opt
->dst1opt
;
577 opt
->opt_flen
-= dsthdrlen
;
583 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
588 *hlimit
= *(int *)CMSG_DATA(cmsg
);
592 LIMIT_NETDEBUG(KERN_DEBUG
"invalid cmsg type: %d\n",