* better
[mascara-docs.git] / i386 / linux-2.3.21 / net / ipv6 / datagram.c
blobc561d318d063f442b8f54b293fe9455ceaab9385
1 /*
2 * common UDP/RAW code
3 * Linux INET6 implementation
5 * Authors:
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>
27 #include <net/ipv6.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)
42 return;
44 skb = skb_clone(skb, GFP_ATOMIC);
45 if (!skb)
46 return;
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;
53 serr->ee.ee_pad = 0;
54 serr->ee.ee_info = info;
55 serr->ee.ee_data = 0;
56 serr->addr_offset = (u8*)&(((struct ipv6hdr*)(icmph+1))->daddr) - skb->nh.raw;
57 serr->port = port;
59 skb->h.raw = payload;
60 skb_pull(skb, payload - skb->data);
62 if (sock_queue_err_skb(sk, skb))
63 kfree_skb(skb);
66 void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info)
68 struct sock_exterr_skb *serr;
69 struct ipv6hdr *iph;
70 struct sk_buff *skb;
72 if (!sk->net_pinfo.af_inet6.recverr)
73 return;
75 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
76 if (!skb)
77 return;
79 iph = (struct ipv6hdr*)skb_put(skb, sizeof(struct ipv6hdr));
80 skb->nh.ipv6h = iph;
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;
86 serr->ee.ee_type = 0;
87 serr->ee.ee_code = 0;
88 serr->ee.ee_pad = 0;
89 serr->ee.ee_info = info;
90 serr->ee.ee_data = 0;
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))
98 kfree_skb(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;
109 struct {
110 struct sock_extended_err ee;
111 struct sockaddr_in6 offender;
112 } errhdr;
113 int err;
114 int copied;
116 err = -EAGAIN;
117 skb = skb_dequeue(&sk->error_queue);
118 if (skb == NULL)
119 goto out;
121 copied = skb->len;
122 if (copied > len) {
123 msg->msg_flags |= MSG_TRUNC;
124 copied = len;
126 err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
127 if (err)
128 goto out_free_skb;
130 serr = SKB_EXT_ERR(skb);
132 sin = (struct sockaddr_in6 *)msg->msg_name;
133 if (sin) {
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;
141 } else
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);
157 } else {
158 ipv6_addr_set(&sin->sin6_addr, 0, 0,
159 __constant_htonl(0xffff),
160 skb->nh.iph->saddr);
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;
171 err = copied;
173 /* Reset and regenerate socket error */
174 sk->err = 0;
175 if ((skb2 = skb_peek(&sk->error_queue)) != NULL) {
176 sk->err = SKB_EXT_ERR(skb2)->ee.ee_errno;
177 sk->error_report(sk);
180 out_free_skb:
181 kfree_skb(skb);
182 out:
183 return err;
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);
230 return 0;
233 int datagram_send_ctl(struct msghdr *msg, struct flowi *fl,
234 struct ipv6_txoptions *opt,
235 int *hlimit)
237 struct in6_pktinfo *src_info;
238 struct cmsghdr *cmsg;
239 struct ipv6_rt_hdr *rthdr;
240 struct ipv6_opt_hdr *hdr;
241 int len;
242 int err = 0;
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) {
249 err = -EINVAL;
250 goto exit_f;
253 if (cmsg->cmsg_level != SOL_IPV6)
254 continue;
256 switch (cmsg->cmsg_type) {
257 case IPV6_PKTINFO:
258 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
259 err = -EINVAL;
260 goto exit_f;
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)
267 return -EINVAL;
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)) {
273 err = -EINVAL;
274 goto exit_f;
277 fl->fl6_src = &src_info->ipi6_addr;
280 break;
282 case IPV6_FLOWINFO:
283 if (cmsg->cmsg_len < CMSG_LEN(4)) {
284 err = -EINVAL;
285 goto exit_f;
288 if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) {
289 if ((fl->fl6_flowlabel^*(u32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
290 err = -EINVAL;
291 goto exit_f;
294 fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(u32 *)CMSG_DATA(cmsg);
295 break;
297 case IPV6_HOPOPTS:
298 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
299 err = -EINVAL;
300 goto exit_f;
303 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
304 len = ((hdr->hdrlen + 1) << 3);
305 if (cmsg->cmsg_len < CMSG_LEN(len)) {
306 err = -EINVAL;
307 goto exit_f;
309 if (!capable(CAP_NET_RAW)) {
310 err = -EPERM;
311 goto exit_f;
313 opt->opt_nflen += len;
314 opt->hopopt = hdr;
315 break;
317 case IPV6_DSTOPTS:
318 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
319 err = -EINVAL;
320 goto exit_f;
323 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
324 len = ((hdr->hdrlen + 1) << 3);
325 if (cmsg->cmsg_len < CMSG_LEN(len)) {
326 err = -EINVAL;
327 goto exit_f;
329 if (!capable(CAP_NET_RAW)) {
330 err = -EPERM;
331 goto exit_f;
333 if (opt->dst1opt) {
334 err = -EINVAL;
335 goto exit_f;
337 opt->opt_flen += len;
338 opt->dst1opt = hdr;
339 break;
341 case IPV6_AUTHHDR:
342 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
343 err = -EINVAL;
344 goto exit_f;
347 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
348 len = ((hdr->hdrlen + 2) << 2);
349 if (cmsg->cmsg_len < CMSG_LEN(len)) {
350 err = -EINVAL;
351 goto exit_f;
353 if (len & ~7) {
354 err = -EINVAL;
355 goto exit_f;
357 opt->opt_flen += len;
358 opt->auth = hdr;
359 break;
361 case IPV6_RTHDR:
362 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
363 err = -EINVAL;
364 goto exit_f;
367 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
370 * TYPE 0
372 if (rthdr->type) {
373 err = -EINVAL;
374 goto exit_f;
377 len = ((rthdr->hdrlen + 1) << 3);
379 if (cmsg->cmsg_len < CMSG_LEN(len)) {
380 err = -EINVAL;
381 goto exit_f;
384 /* segments left must also match */
385 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
386 err = -EINVAL;
387 goto exit_f;
390 opt->opt_nflen += len;
391 opt->srcrt = rthdr;
393 if (opt->dst1opt) {
394 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
396 opt->opt_nflen += dsthdrlen;
397 opt->dst0opt = opt->dst1opt;
398 opt->dst1opt = NULL;
399 opt->opt_flen -= dsthdrlen;
402 break;
404 case IPV6_HOPLIMIT:
405 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
406 err = -EINVAL;
407 goto exit_f;
410 *hlimit = *(int *)CMSG_DATA(cmsg);
411 break;
413 default:
414 printk(KERN_DEBUG "invalid cmsg type: %d\n", cmsg->cmsg_type);
415 err = -EINVAL;
416 break;
420 exit_f:
421 return err;