3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: datagram.c,v 1.18 1999/08/20 11:06:17 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>
32 #include <linux/errqueue.h>
33 #include <asm/uaccess.h>
35 void ipv6_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
36 u16 port
, u32 info
, u8
*payload
)
38 struct icmp6hdr
*icmph
= (struct icmp6hdr
*)skb
->h
.raw
;
39 struct sock_exterr_skb
*serr
;
41 if (!sk
->net_pinfo
.af_inet6
.recverr
)
44 skb
= skb_clone(skb
, GFP_ATOMIC
);
48 serr
= SKB_EXT_ERR(skb
);
49 serr
->ee
.ee_errno
= err
;
50 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP6
;
51 serr
->ee
.ee_type
= icmph
->icmp6_type
;
52 serr
->ee
.ee_code
= icmph
->icmp6_code
;
54 serr
->ee
.ee_info
= info
;
56 serr
->addr_offset
= (u8
*)&(((struct ipv6hdr
*)(icmph
+1))->daddr
) - skb
->nh
.raw
;
60 skb_pull(skb
, payload
- skb
->data
);
62 if (sock_queue_err_skb(sk
, skb
))
66 void ipv6_local_error(struct sock
*sk
, int err
, struct flowi
*fl
, u32 info
)
68 struct sock_exterr_skb
*serr
;
72 if (!sk
->net_pinfo
.af_inet6
.recverr
)
75 skb
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
79 iph
= (struct ipv6hdr
*)skb_put(skb
, sizeof(struct ipv6hdr
));
81 memcpy(&iph
->daddr
, fl
->fl6_dst
, 16);
83 serr
= SKB_EXT_ERR(skb
);
84 serr
->ee
.ee_errno
= err
;
85 serr
->ee
.ee_origin
= SO_EE_ORIGIN_LOCAL
;
89 serr
->ee
.ee_info
= info
;
91 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb
->nh
.raw
;
92 serr
->port
= fl
->uli_u
.ports
.dport
;
94 skb
->h
.raw
= skb
->tail
;
95 skb_pull(skb
, skb
->tail
- skb
->data
);
97 if (sock_queue_err_skb(sk
, skb
))
102 * Handle MSG_ERRQUEUE
104 int ipv6_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
106 struct sock_exterr_skb
*serr
;
107 struct sk_buff
*skb
, *skb2
;
108 struct sockaddr_in6
*sin
;
110 struct sock_extended_err ee
;
111 struct sockaddr_in6 offender
;
117 skb
= skb_dequeue(&sk
->error_queue
);
123 msg
->msg_flags
|= MSG_TRUNC
;
126 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, copied
);
130 serr
= SKB_EXT_ERR(skb
);
132 sin
= (struct sockaddr_in6
*)msg
->msg_name
;
134 sin
->sin6_family
= AF_INET6
;
135 sin
->sin6_flowinfo
= 0;
136 sin
->sin6_port
= serr
->port
;
137 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
) {
138 memcpy(&sin
->sin6_addr
, skb
->nh
.raw
+ serr
->addr_offset
, 16);
139 if (sk
->net_pinfo
.af_inet6
.sndflow
)
140 sin
->sin6_flowinfo
= *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
- 24) & IPV6_FLOWINFO_MASK
;
142 ipv6_addr_set(&sin
->sin6_addr
, 0, 0,
143 __constant_htonl(0xffff),
144 *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
));
147 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
148 sin
= &errhdr
.offender
;
149 sin
->sin6_family
= AF_UNSPEC
;
150 if (serr
->ee
.ee_origin
!= SO_EE_ORIGIN_LOCAL
) {
151 sin
->sin6_family
= AF_INET6
;
152 sin
->sin6_flowinfo
= 0;
153 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
) {
154 memcpy(&sin
->sin6_addr
, &skb
->nh
.ipv6h
->saddr
, 16);
155 if (sk
->net_pinfo
.af_inet6
.rxopt
.all
)
156 datagram_recv_ctl(sk
, msg
, skb
);
158 ipv6_addr_set(&sin
->sin6_addr
, 0, 0,
159 __constant_htonl(0xffff),
161 if (sk
->protinfo
.af_inet
.cmsg_flags
)
162 ip_cmsg_recv(msg
, skb
);
166 put_cmsg(msg
, SOL_IPV6
, IPV6_RECVERR
, sizeof(errhdr
), &errhdr
);
168 /* Now we could try to dump offended packet options */
170 msg
->msg_flags
|= MSG_ERRQUEUE
;
173 /* Reset and regenerate socket error */
175 if ((skb2
= skb_peek(&sk
->error_queue
)) != NULL
) {
176 sk
->err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
177 sk
->error_report(sk
);
188 int datagram_recv_ctl(struct sock
*sk
, struct msghdr
*msg
, struct sk_buff
*skb
)
190 struct ipv6_pinfo
*np
= &sk
->net_pinfo
.af_inet6
;
191 struct inet6_skb_parm
*opt
= (struct inet6_skb_parm
*) skb
->cb
;
193 if (np
->rxopt
.bits
.rxinfo
) {
194 struct in6_pktinfo src_info
;
196 src_info
.ipi6_ifindex
= opt
->iif
;
197 ipv6_addr_copy(&src_info
.ipi6_addr
, &skb
->nh
.ipv6h
->daddr
);
198 put_cmsg(msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
201 if (np
->rxopt
.bits
.rxhlim
) {
202 int hlim
= skb
->nh
.ipv6h
->hop_limit
;
203 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
206 if (np
->rxopt
.bits
.rxflow
&& (*(u32
*)skb
->nh
.raw
& IPV6_FLOWINFO_MASK
)) {
207 u32 flowinfo
= *(u32
*)skb
->nh
.raw
& IPV6_FLOWINFO_MASK
;
208 put_cmsg(msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
210 if (np
->rxopt
.bits
.hopopts
&& opt
->hop
) {
211 u8
*ptr
= skb
->nh
.raw
+ opt
->hop
;
212 put_cmsg(msg
, SOL_IPV6
, IPV6_HOPOPTS
, (ptr
[1]+1)<<3, ptr
);
214 if (np
->rxopt
.bits
.dstopts
&& opt
->dst0
) {
215 u8
*ptr
= skb
->nh
.raw
+ opt
->dst0
;
216 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
218 if (np
->rxopt
.bits
.srcrt
&& opt
->srcrt
) {
219 struct ipv6_rt_hdr
*rthdr
= (struct ipv6_rt_hdr
*)(skb
->nh
.raw
+ opt
->srcrt
);
220 put_cmsg(msg
, SOL_IPV6
, IPV6_RTHDR
, (rthdr
->hdrlen
+1) << 3, rthdr
);
222 if (np
->rxopt
.bits
.authhdr
&& opt
->auth
) {
223 u8
*ptr
= skb
->nh
.raw
+ opt
->auth
;
224 put_cmsg(msg
, SOL_IPV6
, IPV6_AUTHHDR
, (ptr
[1]+1)<<2, ptr
);
226 if (np
->rxopt
.bits
.dstopts
&& opt
->dst1
) {
227 u8
*ptr
= skb
->nh
.raw
+ opt
->dst1
;
228 put_cmsg(msg
, SOL_IPV6
, IPV6_DSTOPTS
, (ptr
[1]+1)<<3, ptr
);
233 int datagram_send_ctl(struct msghdr
*msg
, struct flowi
*fl
,
234 struct ipv6_txoptions
*opt
,
237 struct in6_pktinfo
*src_info
;
238 struct cmsghdr
*cmsg
;
239 struct ipv6_rt_hdr
*rthdr
;
240 struct ipv6_opt_hdr
*hdr
;
244 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
246 if (cmsg
->cmsg_len
< sizeof(struct cmsghdr
) ||
247 (unsigned long)(((char*)cmsg
- (char*)msg
->msg_control
)
248 + cmsg
->cmsg_len
) > msg
->msg_controllen
) {
253 if (cmsg
->cmsg_level
!= SOL_IPV6
)
256 switch (cmsg
->cmsg_type
) {
258 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct in6_pktinfo
))) {
263 src_info
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
265 if (src_info
->ipi6_ifindex
) {
266 if (fl
->oif
&& src_info
->ipi6_ifindex
!= fl
->oif
)
268 fl
->oif
= src_info
->ipi6_ifindex
;
271 if (!ipv6_addr_any(&src_info
->ipi6_addr
)) {
272 if (!ipv6_chk_addr(&src_info
->ipi6_addr
, NULL
)) {
277 fl
->fl6_src
= &src_info
->ipi6_addr
;
283 if (cmsg
->cmsg_len
< CMSG_LEN(4)) {
288 if (fl
->fl6_flowlabel
&IPV6_FLOWINFO_MASK
) {
289 if ((fl
->fl6_flowlabel
^*(u32
*)CMSG_DATA(cmsg
))&~IPV6_FLOWINFO_MASK
) {
294 fl
->fl6_flowlabel
= IPV6_FLOWINFO_MASK
& *(u32
*)CMSG_DATA(cmsg
);
298 if (opt
->hopopt
|| cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
303 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
304 len
= ((hdr
->hdrlen
+ 1) << 3);
305 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
309 if (!capable(CAP_NET_RAW
)) {
313 opt
->opt_nflen
+= len
;
318 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
323 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
324 len
= ((hdr
->hdrlen
+ 1) << 3);
325 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
329 if (!capable(CAP_NET_RAW
)) {
337 opt
->opt_flen
+= len
;
342 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_opt_hdr
))) {
347 hdr
= (struct ipv6_opt_hdr
*)CMSG_DATA(cmsg
);
348 len
= ((hdr
->hdrlen
+ 2) << 2);
349 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
357 opt
->opt_flen
+= len
;
362 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct ipv6_rt_hdr
))) {
367 rthdr
= (struct ipv6_rt_hdr
*)CMSG_DATA(cmsg
);
377 len
= ((rthdr
->hdrlen
+ 1) << 3);
379 if (cmsg
->cmsg_len
< CMSG_LEN(len
)) {
384 /* segments left must also match */
385 if ((rthdr
->hdrlen
>> 1) != rthdr
->segments_left
) {
390 opt
->opt_nflen
+= len
;
394 int dsthdrlen
= ((opt
->dst1opt
->hdrlen
+1)<<3);
396 opt
->opt_nflen
+= dsthdrlen
;
397 opt
->dst0opt
= opt
->dst1opt
;
399 opt
->opt_flen
-= dsthdrlen
;
405 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
410 *hlimit
= *(int *)CMSG_DATA(cmsg
);
414 printk(KERN_DEBUG
"invalid cmsg type: %d\n", cmsg
->cmsg_type
);