blob: 7a86db6163ee02c2fc3bee1713e8252af9d1acb5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * common UDP/RAW code
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * $Id: datagram.c,v 1.24 2002/02/01 22:01:04 davem Exp $
9 *
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.
14 */
15
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
26
27#include <net/ipv6.h>
28#include <net/ndisc.h>
29#include <net/addrconf.h>
30#include <net/transp_v6.h>
31#include <net/ip6_route.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070032#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/errqueue.h>
35#include <asm/uaccess.h>
36
37int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
38{
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;
44 struct flowi fl;
45 struct ip6_flowlabel *flowlabel = NULL;
46 int addr_type;
47 int err;
48
49 if (usin->sin6_family == AF_INET) {
50 if (__ipv6_only_sock(sk))
51 return -EAFNOSUPPORT;
52 err = ip4_datagram_connect(sk, uaddr, addr_len);
53 goto ipv4_connected;
54 }
55
56 if (addr_len < SIN6_LEN_RFC2133)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090057 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090059 if (usin->sin6_family != AF_INET6)
60 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 memset(&fl, 0, sizeof(fl));
63 if (np->sndflow) {
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)
68 return -EINVAL;
69 ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
70 }
71 }
72
73 addr_type = ipv6_addr_type(&usin->sin6_addr);
74
75 if (addr_type == IPV6_ADDR_ANY) {
76 /*
77 * connect to self
78 */
79 usin->sin6_addr.s6_addr[15] = 0x01;
80 }
81
82 daddr = &usin->sin6_addr;
83
84 if (addr_type == IPV6_ADDR_MAPPED) {
85 struct sockaddr_in sin;
86
87 if (__ipv6_only_sock(sk)) {
88 err = -ENETUNREACH;
89 goto out;
90 }
91 sin.sin_family = AF_INET;
92 sin.sin_addr.s_addr = daddr->s6_addr32[3];
93 sin.sin_port = usin->sin6_port;
94
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090095 err = ip4_datagram_connect(sk,
96 (struct sockaddr*) &sin,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 sizeof(sin));
98
99ipv4_connected:
100 if (err)
101 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ipv6_addr_set(&np->daddr, 0, 0, htonl(0x0000ffff), inet->daddr);
104
105 if (ipv6_addr_any(&np->saddr)) {
106 ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000ffff),
107 inet->saddr);
108 }
109
110 if (ipv6_addr_any(&np->rcv_saddr)) {
111 ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000ffff),
112 inet->rcv_saddr);
113 }
114 goto out;
115 }
116
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) {
122 err = -EINVAL;
123 goto out;
124 }
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;
129 }
130
131 /* Connect to link-local address requires an interface */
132 if (!sk->sk_bound_dev_if) {
133 err = -EINVAL;
134 goto out;
135 }
136 }
137
138 ipv6_addr_copy(&np->daddr, daddr);
139 np->flow_label = fl.fl6_flowlabel;
140
141 inet->dport = usin->sin6_port;
142
143 /*
144 * Check for a route to destination an obtain the
145 * destination cache for it.
146 */
147
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;
154
155 if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST))
156 fl.oif = np->mcast_oif;
157
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700158 security_sk_classify_flow(sk, &fl);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (flowlabel) {
161 if (flowlabel->opt && flowlabel->opt->srcrt) {
162 struct rt0_hdr *rt0 = (struct rt0_hdr *) flowlabel->opt->srcrt;
163 ipv6_addr_copy(&final, &fl.fl6_dst);
164 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
165 final_p = &final;
166 }
167 } else if (np->opt && np->opt->srcrt) {
168 struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
169 ipv6_addr_copy(&final, &fl.fl6_dst);
170 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
171 final_p = &final;
172 }
173
174 err = ip6_dst_lookup(sk, &dst, &fl);
175 if (err)
176 goto out;
177 if (final_p)
178 ipv6_addr_copy(&fl.fl6_dst, final_p);
179
David S. Miller8eb90862007-02-08 02:09:21 -0800180 if ((err = xfrm_lookup(&dst, &fl, sk, 1)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* source address lookup done in ip6_dst_lookup */
184
185 if (ipv6_addr_any(&np->saddr))
186 ipv6_addr_copy(&np->saddr, &fl.fl6_src);
187
188 if (ipv6_addr_any(&np->rcv_saddr)) {
189 ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src);
190 inet->rcv_saddr = LOOPBACK4_IPV6;
191 }
192
193 ip6_dst_store(sk, dst,
194 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700195 &np->daddr : NULL,
196#ifdef CONFIG_IPV6_SUBTREES
197 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
198 &np->saddr :
199#endif
200 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 sk->sk_state = TCP_ESTABLISHED;
203out:
204 fl6_sock_release(flowlabel);
205 return err;
206}
207
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900208void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4ad2006-11-14 20:56:00 -0800209 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct ipv6_pinfo *np = inet6_sk(sk);
212 struct icmp6hdr *icmph = (struct icmp6hdr *)skb->h.raw;
213 struct sock_exterr_skb *serr;
214
215 if (!np->recverr)
216 return;
217
218 skb = skb_clone(skb, GFP_ATOMIC);
219 if (!skb)
220 return;
221
222 serr = SKB_EXT_ERR(skb);
223 serr->ee.ee_errno = err;
224 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900225 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 serr->ee.ee_code = icmph->icmp6_code;
227 serr->ee.ee_pad = 0;
228 serr->ee.ee_info = info;
229 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700230 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
231 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 serr->port = port;
233
234 skb->h.raw = payload;
235 __skb_pull(skb, payload - skb->data);
236
237 if (sock_queue_err_skb(sk, skb))
238 kfree_skb(skb);
239}
240
241void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info)
242{
243 struct ipv6_pinfo *np = inet6_sk(sk);
244 struct sock_exterr_skb *serr;
245 struct ipv6hdr *iph;
246 struct sk_buff *skb;
247
248 if (!np->recverr)
249 return;
250
251 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
252 if (!skb)
253 return;
254
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300255 skb_put(skb, sizeof(struct ipv6hdr));
256 skb_reset_network_header(skb);
257 iph = skb->nh.ipv6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
259
260 serr = SKB_EXT_ERR(skb);
261 serr->ee.ee_errno = err;
262 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900263 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 serr->ee.ee_code = 0;
265 serr->ee.ee_pad = 0;
266 serr->ee.ee_info = info;
267 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700268 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 serr->port = fl->fl_ip_dport;
270
271 skb->h.raw = skb->tail;
272 __skb_pull(skb, skb->tail - skb->data);
273
274 if (sock_queue_err_skb(sk, skb))
275 kfree_skb(skb);
276}
277
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900278/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * Handle MSG_ERRQUEUE
280 */
281int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
282{
283 struct ipv6_pinfo *np = inet6_sk(sk);
284 struct sock_exterr_skb *serr;
285 struct sk_buff *skb, *skb2;
286 struct sockaddr_in6 *sin;
287 struct {
288 struct sock_extended_err ee;
289 struct sockaddr_in6 offender;
290 } errhdr;
291 int err;
292 int copied;
293
294 err = -EAGAIN;
295 skb = skb_dequeue(&sk->sk_error_queue);
296 if (skb == NULL)
297 goto out;
298
299 copied = skb->len;
300 if (copied > len) {
301 msg->msg_flags |= MSG_TRUNC;
302 copied = len;
303 }
304 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
305 if (err)
306 goto out_free_skb;
307
308 sock_recv_timestamp(msg, sk, skb);
309
310 serr = SKB_EXT_ERR(skb);
311
312 sin = (struct sockaddr_in6 *)msg->msg_name;
313 if (sin) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700314 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 sin->sin6_family = AF_INET6;
316 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900317 sin->sin6_port = serr->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 sin->sin6_scope_id = 0;
319 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
320 ipv6_addr_copy(&sin->sin6_addr,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700321 (struct in6_addr *)(nh + serr->addr_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (np->sndflow)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700323 sin->sin6_flowinfo =
324 (*(__be32 *)(nh + serr->addr_offset - 24) &
325 IPV6_FLOWINFO_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
327 sin->sin6_scope_id = IP6CB(skb)->iif;
328 } else {
329 ipv6_addr_set(&sin->sin6_addr, 0, 0,
330 htonl(0xffff),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700331 *(__be32 *)(nh + serr->addr_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 }
334
335 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
336 sin = &errhdr.offender;
337 sin->sin6_family = AF_UNSPEC;
338 if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
339 sin->sin6_family = AF_INET6;
340 sin->sin6_flowinfo = 0;
341 sin->sin6_scope_id = 0;
342 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
343 ipv6_addr_copy(&sin->sin6_addr, &skb->nh.ipv6h->saddr);
344 if (np->rxopt.all)
345 datagram_recv_ctl(sk, msg, skb);
346 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
347 sin->sin6_scope_id = IP6CB(skb)->iif;
348 } else {
349 struct inet_sock *inet = inet_sk(sk);
350
351 ipv6_addr_set(&sin->sin6_addr, 0, 0,
352 htonl(0xffff),
353 skb->nh.iph->saddr);
354 if (inet->cmsg_flags)
355 ip_cmsg_recv(msg, skb);
356 }
357 }
358
359 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
360
361 /* Now we could try to dump offended packet options */
362
363 msg->msg_flags |= MSG_ERRQUEUE;
364 err = copied;
365
366 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700367 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 sk->sk_err = 0;
369 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
370 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700371 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 sk->sk_error_report(sk);
373 } else {
Herbert Xue0f9f852005-06-18 22:56:18 -0700374 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900377out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 kfree_skb(skb);
379out:
380 return err;
381}
382
383
384
385int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
386{
387 struct ipv6_pinfo *np = inet6_sk(sk);
388 struct inet6_skb_parm *opt = IP6CB(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700389 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (np->rxopt.bits.rxinfo) {
392 struct in6_pktinfo src_info;
393
394 src_info.ipi6_ifindex = opt->iif;
395 ipv6_addr_copy(&src_info.ipi6_addr, &skb->nh.ipv6h->daddr);
396 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
397 }
398
399 if (np->rxopt.bits.rxhlim) {
400 int hlim = skb->nh.ipv6h->hop_limit;
401 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
402 }
403
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900404 if (np->rxopt.bits.rxtclass) {
Al Viroe69a4ad2006-11-14 20:56:00 -0800405 int tclass = (ntohl(*(__be32 *)skb->nh.ipv6h) >> 20) & 0xff;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900406 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
407 }
408
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700409 if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
410 __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
412 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900413
414 /* HbH is allowed only once */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (np->rxopt.bits.hopopts && opt->hop) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700416 u8 *ptr = nh + opt->hop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
418 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900419
420 if (opt->lastopt &&
421 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
422 /*
423 * Silly enough, but we need to reparse in order to
424 * report extension headers (except for HbH)
425 * in order.
426 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900427 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900428 * (and WILL NOT be) defined because
429 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
430 */
431 unsigned int off = sizeof(struct ipv6hdr);
432 u8 nexthdr = skb->nh.ipv6h->nexthdr;
433
434 while (off <= opt->lastopt) {
435 unsigned len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700436 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900437
438 switch(nexthdr) {
439 case IPPROTO_DSTOPTS:
440 nexthdr = ptr[0];
441 len = (ptr[1] + 1) << 3;
442 if (np->rxopt.bits.dstopts)
443 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
444 break;
445 case IPPROTO_ROUTING:
446 nexthdr = ptr[0];
447 len = (ptr[1] + 1) << 3;
448 if (np->rxopt.bits.srcrt)
449 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
450 break;
451 case IPPROTO_AH:
452 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900453 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900454 break;
455 default:
456 nexthdr = ptr[0];
457 len = (ptr[1] + 1) << 3;
458 break;
459 }
460
461 off += len;
462 }
463 }
464
465 /* socket options in old style */
466 if (np->rxopt.bits.rxoinfo) {
467 struct in6_pktinfo src_info;
468
469 src_info.ipi6_ifindex = opt->iif;
470 ipv6_addr_copy(&src_info.ipi6_addr, &skb->nh.ipv6h->daddr);
471 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
472 }
473 if (np->rxopt.bits.rxohlim) {
474 int hlim = skb->nh.ipv6h->hop_limit;
475 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
476 }
477 if (np->rxopt.bits.ohopopts && opt->hop) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700478 u8 *ptr = nh + opt->hop;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900479 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
480 }
481 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700482 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900483 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900485 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700486 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900487 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900489 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700490 u8 *ptr = nh + opt->dst1;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900491 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 return 0;
494}
495
496int datagram_send_ctl(struct msghdr *msg, struct flowi *fl,
497 struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900498 int *hlimit, int *tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct in6_pktinfo *src_info;
501 struct cmsghdr *cmsg;
502 struct ipv6_rt_hdr *rthdr;
503 struct ipv6_opt_hdr *hdr;
504 int len;
505 int err = 0;
506
507 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
508 int addr_type;
509 struct net_device *dev = NULL;
510
511 if (!CMSG_OK(msg, cmsg)) {
512 err = -EINVAL;
513 goto exit_f;
514 }
515
516 if (cmsg->cmsg_level != SOL_IPV6)
517 continue;
518
519 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900520 case IPV6_PKTINFO:
521 case IPV6_2292PKTINFO:
522 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 err = -EINVAL;
524 goto exit_f;
525 }
526
527 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (src_info->ipi6_ifindex) {
530 if (fl->oif && src_info->ipi6_ifindex != fl->oif)
531 return -EINVAL;
532 fl->oif = src_info->ipi6_ifindex;
533 }
534
535 addr_type = ipv6_addr_type(&src_info->ipi6_addr);
536
537 if (addr_type == IPV6_ADDR_ANY)
538 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (addr_type & IPV6_ADDR_LINKLOCAL) {
541 if (!src_info->ipi6_ifindex)
542 return -EINVAL;
543 else {
544 dev = dev_get_by_index(src_info->ipi6_ifindex);
545 if (!dev)
546 return -ENODEV;
547 }
548 }
549 if (!ipv6_chk_addr(&src_info->ipi6_addr, dev, 0)) {
550 if (dev)
551 dev_put(dev);
552 err = -EINVAL;
553 goto exit_f;
554 }
555 if (dev)
556 dev_put(dev);
557
558 ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr);
559 break;
560
561 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900562 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 err = -EINVAL;
564 goto exit_f;
565 }
566
567 if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) {
Al Viro90bcaf72006-11-08 00:25:17 -0800568 if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 err = -EINVAL;
570 goto exit_f;
571 }
572 }
Al Viro90bcaf72006-11-08 00:25:17 -0800573 fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
575
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900576 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900578 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 err = -EINVAL;
580 goto exit_f;
581 }
582
583 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
584 len = ((hdr->hdrlen + 1) << 3);
585 if (cmsg->cmsg_len < CMSG_LEN(len)) {
586 err = -EINVAL;
587 goto exit_f;
588 }
589 if (!capable(CAP_NET_RAW)) {
590 err = -EPERM;
591 goto exit_f;
592 }
593 opt->opt_nflen += len;
594 opt->hopopt = hdr;
595 break;
596
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900597 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900598 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 err = -EINVAL;
600 goto exit_f;
601 }
602
603 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
604 len = ((hdr->hdrlen + 1) << 3);
605 if (cmsg->cmsg_len < CMSG_LEN(len)) {
606 err = -EINVAL;
607 goto exit_f;
608 }
609 if (!capable(CAP_NET_RAW)) {
610 err = -EPERM;
611 goto exit_f;
612 }
613 if (opt->dst1opt) {
614 err = -EINVAL;
615 goto exit_f;
616 }
617 opt->opt_flen += len;
618 opt->dst1opt = hdr;
619 break;
620
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900621 case IPV6_DSTOPTS:
622 case IPV6_RTHDRDSTOPTS:
623 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
624 err = -EINVAL;
625 goto exit_f;
626 }
627
628 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
629 len = ((hdr->hdrlen + 1) << 3);
630 if (cmsg->cmsg_len < CMSG_LEN(len)) {
631 err = -EINVAL;
632 goto exit_f;
633 }
634 if (!capable(CAP_NET_RAW)) {
635 err = -EPERM;
636 goto exit_f;
637 }
638 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
639 opt->opt_flen += len;
640 opt->dst1opt = hdr;
641 } else {
642 opt->opt_nflen += len;
643 opt->dst0opt = hdr;
644 }
645 break;
646
647 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900649 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 err = -EINVAL;
651 goto exit_f;
652 }
653
654 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
655
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700656 switch (rthdr->type) {
657 case IPV6_SRCRT_TYPE_0:
658#ifdef CONFIG_IPV6_MIP6
659 case IPV6_SRCRT_TYPE_2:
660#endif
661 break;
662 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 err = -EINVAL;
664 goto exit_f;
665 }
666
667 len = ((rthdr->hdrlen + 1) << 3);
668
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900669 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 err = -EINVAL;
671 goto exit_f;
672 }
673
674 /* segments left must also match */
675 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
676 err = -EINVAL;
677 goto exit_f;
678 }
679
680 opt->opt_nflen += len;
681 opt->srcrt = rthdr;
682
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900683 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
685
686 opt->opt_nflen += dsthdrlen;
687 opt->dst0opt = opt->dst1opt;
688 opt->dst1opt = NULL;
689 opt->opt_flen -= dsthdrlen;
690 }
691
692 break;
693
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900694 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 case IPV6_HOPLIMIT:
696 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
697 err = -EINVAL;
698 goto exit_f;
699 }
700
701 *hlimit = *(int *)CMSG_DATA(cmsg);
702 break;
703
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900704 case IPV6_TCLASS:
705 {
706 int tc;
707
708 err = -EINVAL;
709 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
710 goto exit_f;
711 }
712
713 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700714 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900715 goto exit_f;
716
717 err = 0;
718 *tclass = tc;
719
720 break;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 default:
Patrick McHardy64ce2072005-08-09 20:50:53 -0700723 LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900724 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 err = -EINVAL;
726 break;
727 };
728 }
729
730exit_f:
731 return err;
732}