Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * common UDP/RAW code | |
1ab1457c | 3 | * Linux INET6 implementation |
1da177e4 LT |
4 | * |
5 | * Authors: | |
1ab1457c | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
1da177e4 LT |
7 | * |
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 | ||
4fc268d2 | 16 | #include <linux/capability.h> |
1da177e4 LT |
17 | #include <linux/errno.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/kernel.h> | |
1da177e4 LT |
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> | |
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> | |
c752f073 | 32 | #include <net/tcp_states.h> |
1da177e4 LT |
33 | |
34 | #include <linux/errqueue.h> | |
35 | #include <asm/uaccess.h> | |
36 | ||
37 | int 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) | |
1ab1457c | 57 | return -EINVAL; |
1da177e4 | 58 | |
1ab1457c YH |
59 | if (usin->sin6_family != AF_INET6) |
60 | return -EAFNOSUPPORT; | |
1da177e4 LT |
61 | |
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 | ||
1ab1457c YH |
95 | err = ip4_datagram_connect(sk, |
96 | (struct sockaddr*) &sin, | |
1da177e4 LT |
97 | sizeof(sin)); |
98 | ||
99 | ipv4_connected: | |
100 | if (err) | |
101 | goto out; | |
1ab1457c | 102 | |
1da177e4 LT |
103 | 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 | ||
beb8d13b VY |
158 | security_sk_classify_flow(sk, &fl); |
159 | ||
1da177e4 LT |
160 | 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 | ||
8eb9086f | 180 | if ((err = xfrm_lookup(&dst, &fl, sk, 1)) < 0) |
1da177e4 | 181 | goto out; |
1da177e4 LT |
182 | |
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) ? | |
8e1ef0a9 YH |
195 | &np->daddr : NULL, |
196 | #ifdef CONFIG_IPV6_SUBTREES | |
197 | ipv6_addr_equal(&fl.fl6_src, &np->saddr) ? | |
198 | &np->saddr : | |
199 | #endif | |
200 | NULL); | |
1da177e4 LT |
201 | |
202 | sk->sk_state = TCP_ESTABLISHED; | |
203 | out: | |
204 | fl6_sock_release(flowlabel); | |
205 | return err; | |
206 | } | |
207 | ||
1ab1457c | 208 | void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
e69a4adc | 209 | __be16 port, u32 info, u8 *payload) |
1da177e4 LT |
210 | { |
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; | |
1ab1457c | 225 | serr->ee.ee_type = icmph->icmp6_type; |
1da177e4 LT |
226 | 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; | |
230 | serr->addr_offset = (u8*)&(((struct ipv6hdr*)(icmph+1))->daddr) - skb->nh.raw; | |
231 | serr->port = port; | |
232 | ||
233 | skb->h.raw = payload; | |
234 | __skb_pull(skb, payload - skb->data); | |
235 | ||
236 | if (sock_queue_err_skb(sk, skb)) | |
237 | kfree_skb(skb); | |
238 | } | |
239 | ||
240 | void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info) | |
241 | { | |
242 | struct ipv6_pinfo *np = inet6_sk(sk); | |
243 | struct sock_exterr_skb *serr; | |
244 | struct ipv6hdr *iph; | |
245 | struct sk_buff *skb; | |
246 | ||
247 | if (!np->recverr) | |
248 | return; | |
249 | ||
250 | skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); | |
251 | if (!skb) | |
252 | return; | |
253 | ||
1ced98e8 ACM |
254 | skb_put(skb, sizeof(struct ipv6hdr)); |
255 | skb_reset_network_header(skb); | |
256 | iph = skb->nh.ipv6h; | |
1da177e4 LT |
257 | ipv6_addr_copy(&iph->daddr, &fl->fl6_dst); |
258 | ||
259 | serr = SKB_EXT_ERR(skb); | |
260 | serr->ee.ee_errno = err; | |
261 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; | |
1ab1457c | 262 | serr->ee.ee_type = 0; |
1da177e4 LT |
263 | serr->ee.ee_code = 0; |
264 | serr->ee.ee_pad = 0; | |
265 | serr->ee.ee_info = info; | |
266 | serr->ee.ee_data = 0; | |
267 | serr->addr_offset = (u8*)&iph->daddr - skb->nh.raw; | |
268 | serr->port = fl->fl_ip_dport; | |
269 | ||
270 | skb->h.raw = skb->tail; | |
271 | __skb_pull(skb, skb->tail - skb->data); | |
272 | ||
273 | if (sock_queue_err_skb(sk, skb)) | |
274 | kfree_skb(skb); | |
275 | } | |
276 | ||
1ab1457c | 277 | /* |
1da177e4 LT |
278 | * Handle MSG_ERRQUEUE |
279 | */ | |
280 | int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len) | |
281 | { | |
282 | struct ipv6_pinfo *np = inet6_sk(sk); | |
283 | struct sock_exterr_skb *serr; | |
284 | struct sk_buff *skb, *skb2; | |
285 | struct sockaddr_in6 *sin; | |
286 | struct { | |
287 | struct sock_extended_err ee; | |
288 | struct sockaddr_in6 offender; | |
289 | } errhdr; | |
290 | int err; | |
291 | int copied; | |
292 | ||
293 | err = -EAGAIN; | |
294 | skb = skb_dequeue(&sk->sk_error_queue); | |
295 | if (skb == NULL) | |
296 | goto out; | |
297 | ||
298 | copied = skb->len; | |
299 | if (copied > len) { | |
300 | msg->msg_flags |= MSG_TRUNC; | |
301 | copied = len; | |
302 | } | |
303 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | |
304 | if (err) | |
305 | goto out_free_skb; | |
306 | ||
307 | sock_recv_timestamp(msg, sk, skb); | |
308 | ||
309 | serr = SKB_EXT_ERR(skb); | |
310 | ||
311 | sin = (struct sockaddr_in6 *)msg->msg_name; | |
312 | if (sin) { | |
313 | sin->sin6_family = AF_INET6; | |
314 | sin->sin6_flowinfo = 0; | |
1ab1457c | 315 | sin->sin6_port = serr->port; |
1da177e4 LT |
316 | sin->sin6_scope_id = 0; |
317 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) { | |
318 | ipv6_addr_copy(&sin->sin6_addr, | |
319 | (struct in6_addr *)(skb->nh.raw + serr->addr_offset)); | |
320 | if (np->sndflow) | |
90bcaf7b | 321 | sin->sin6_flowinfo = *(__be32*)(skb->nh.raw + serr->addr_offset - 24) & IPV6_FLOWINFO_MASK; |
1da177e4 LT |
322 | if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL) |
323 | sin->sin6_scope_id = IP6CB(skb)->iif; | |
324 | } else { | |
325 | ipv6_addr_set(&sin->sin6_addr, 0, 0, | |
326 | htonl(0xffff), | |
e69a4adc | 327 | *(__be32*)(skb->nh.raw + serr->addr_offset)); |
1da177e4 LT |
328 | } |
329 | } | |
330 | ||
331 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); | |
332 | sin = &errhdr.offender; | |
333 | sin->sin6_family = AF_UNSPEC; | |
334 | if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) { | |
335 | sin->sin6_family = AF_INET6; | |
336 | sin->sin6_flowinfo = 0; | |
337 | sin->sin6_scope_id = 0; | |
338 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) { | |
339 | ipv6_addr_copy(&sin->sin6_addr, &skb->nh.ipv6h->saddr); | |
340 | if (np->rxopt.all) | |
341 | datagram_recv_ctl(sk, msg, skb); | |
342 | if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL) | |
343 | sin->sin6_scope_id = IP6CB(skb)->iif; | |
344 | } else { | |
345 | struct inet_sock *inet = inet_sk(sk); | |
346 | ||
347 | ipv6_addr_set(&sin->sin6_addr, 0, 0, | |
348 | htonl(0xffff), | |
349 | skb->nh.iph->saddr); | |
350 | if (inet->cmsg_flags) | |
351 | ip_cmsg_recv(msg, skb); | |
352 | } | |
353 | } | |
354 | ||
355 | put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr); | |
356 | ||
357 | /* Now we could try to dump offended packet options */ | |
358 | ||
359 | msg->msg_flags |= MSG_ERRQUEUE; | |
360 | err = copied; | |
361 | ||
362 | /* Reset and regenerate socket error */ | |
e0f9f858 | 363 | spin_lock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
364 | sk->sk_err = 0; |
365 | if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) { | |
366 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; | |
e0f9f858 | 367 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
368 | sk->sk_error_report(sk); |
369 | } else { | |
e0f9f858 | 370 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
371 | } |
372 | ||
1ab1457c | 373 | out_free_skb: |
1da177e4 LT |
374 | kfree_skb(skb); |
375 | out: | |
376 | return err; | |
377 | } | |
378 | ||
379 | ||
380 | ||
381 | int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb) | |
382 | { | |
383 | struct ipv6_pinfo *np = inet6_sk(sk); | |
384 | struct inet6_skb_parm *opt = IP6CB(skb); | |
385 | ||
386 | if (np->rxopt.bits.rxinfo) { | |
387 | struct in6_pktinfo src_info; | |
388 | ||
389 | src_info.ipi6_ifindex = opt->iif; | |
390 | ipv6_addr_copy(&src_info.ipi6_addr, &skb->nh.ipv6h->daddr); | |
391 | put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); | |
392 | } | |
393 | ||
394 | if (np->rxopt.bits.rxhlim) { | |
395 | int hlim = skb->nh.ipv6h->hop_limit; | |
396 | put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); | |
397 | } | |
398 | ||
41a1f8ea | 399 | if (np->rxopt.bits.rxtclass) { |
e69a4adc | 400 | int tclass = (ntohl(*(__be32 *)skb->nh.ipv6h) >> 20) & 0xff; |
41a1f8ea YH |
401 | put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); |
402 | } | |
403 | ||
90bcaf7b AV |
404 | if (np->rxopt.bits.rxflow && (*(__be32*)skb->nh.raw & IPV6_FLOWINFO_MASK)) { |
405 | __be32 flowinfo = *(__be32*)skb->nh.raw & IPV6_FLOWINFO_MASK; | |
1da177e4 LT |
406 | put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); |
407 | } | |
333fad53 YH |
408 | |
409 | /* HbH is allowed only once */ | |
1da177e4 LT |
410 | if (np->rxopt.bits.hopopts && opt->hop) { |
411 | u8 *ptr = skb->nh.raw + opt->hop; | |
412 | put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr); | |
413 | } | |
333fad53 YH |
414 | |
415 | if (opt->lastopt && | |
416 | (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) { | |
417 | /* | |
418 | * Silly enough, but we need to reparse in order to | |
419 | * report extension headers (except for HbH) | |
420 | * in order. | |
421 | * | |
1ab1457c | 422 | * Also note that IPV6_RECVRTHDRDSTOPTS is NOT |
333fad53 YH |
423 | * (and WILL NOT be) defined because |
424 | * IPV6_RECVDSTOPTS is more generic. --yoshfuji | |
425 | */ | |
426 | unsigned int off = sizeof(struct ipv6hdr); | |
427 | u8 nexthdr = skb->nh.ipv6h->nexthdr; | |
428 | ||
429 | while (off <= opt->lastopt) { | |
430 | unsigned len; | |
431 | u8 *ptr = skb->nh.raw + off; | |
432 | ||
433 | switch(nexthdr) { | |
434 | case IPPROTO_DSTOPTS: | |
435 | nexthdr = ptr[0]; | |
436 | len = (ptr[1] + 1) << 3; | |
437 | if (np->rxopt.bits.dstopts) | |
438 | put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr); | |
439 | break; | |
440 | case IPPROTO_ROUTING: | |
441 | nexthdr = ptr[0]; | |
442 | len = (ptr[1] + 1) << 3; | |
443 | if (np->rxopt.bits.srcrt) | |
444 | put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr); | |
445 | break; | |
446 | case IPPROTO_AH: | |
447 | nexthdr = ptr[0]; | |
a3059893 | 448 | len = (ptr[1] + 2) << 2; |
333fad53 YH |
449 | break; |
450 | default: | |
451 | nexthdr = ptr[0]; | |
452 | len = (ptr[1] + 1) << 3; | |
453 | break; | |
454 | } | |
455 | ||
456 | off += len; | |
457 | } | |
458 | } | |
459 | ||
460 | /* socket options in old style */ | |
461 | if (np->rxopt.bits.rxoinfo) { | |
462 | struct in6_pktinfo src_info; | |
463 | ||
464 | src_info.ipi6_ifindex = opt->iif; | |
465 | ipv6_addr_copy(&src_info.ipi6_addr, &skb->nh.ipv6h->daddr); | |
466 | put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); | |
467 | } | |
468 | if (np->rxopt.bits.rxohlim) { | |
469 | int hlim = skb->nh.ipv6h->hop_limit; | |
470 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); | |
471 | } | |
472 | if (np->rxopt.bits.ohopopts && opt->hop) { | |
473 | u8 *ptr = skb->nh.raw + opt->hop; | |
474 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr); | |
475 | } | |
476 | if (np->rxopt.bits.odstopts && opt->dst0) { | |
1da177e4 | 477 | u8 *ptr = skb->nh.raw + opt->dst0; |
333fad53 | 478 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
1da177e4 | 479 | } |
333fad53 | 480 | if (np->rxopt.bits.osrcrt && opt->srcrt) { |
1da177e4 | 481 | struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(skb->nh.raw + opt->srcrt); |
333fad53 | 482 | put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr); |
1da177e4 | 483 | } |
333fad53 | 484 | if (np->rxopt.bits.odstopts && opt->dst1) { |
1da177e4 | 485 | u8 *ptr = skb->nh.raw + opt->dst1; |
333fad53 | 486 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
1da177e4 LT |
487 | } |
488 | return 0; | |
489 | } | |
490 | ||
491 | int datagram_send_ctl(struct msghdr *msg, struct flowi *fl, | |
492 | struct ipv6_txoptions *opt, | |
41a1f8ea | 493 | int *hlimit, int *tclass) |
1da177e4 LT |
494 | { |
495 | struct in6_pktinfo *src_info; | |
496 | struct cmsghdr *cmsg; | |
497 | struct ipv6_rt_hdr *rthdr; | |
498 | struct ipv6_opt_hdr *hdr; | |
499 | int len; | |
500 | int err = 0; | |
501 | ||
502 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { | |
503 | int addr_type; | |
504 | struct net_device *dev = NULL; | |
505 | ||
506 | if (!CMSG_OK(msg, cmsg)) { | |
507 | err = -EINVAL; | |
508 | goto exit_f; | |
509 | } | |
510 | ||
511 | if (cmsg->cmsg_level != SOL_IPV6) | |
512 | continue; | |
513 | ||
514 | switch (cmsg->cmsg_type) { | |
1ab1457c YH |
515 | case IPV6_PKTINFO: |
516 | case IPV6_2292PKTINFO: | |
517 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) { | |
1da177e4 LT |
518 | err = -EINVAL; |
519 | goto exit_f; | |
520 | } | |
521 | ||
522 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); | |
1ab1457c | 523 | |
1da177e4 LT |
524 | if (src_info->ipi6_ifindex) { |
525 | if (fl->oif && src_info->ipi6_ifindex != fl->oif) | |
526 | return -EINVAL; | |
527 | fl->oif = src_info->ipi6_ifindex; | |
528 | } | |
529 | ||
530 | addr_type = ipv6_addr_type(&src_info->ipi6_addr); | |
531 | ||
532 | if (addr_type == IPV6_ADDR_ANY) | |
533 | break; | |
1ab1457c | 534 | |
1da177e4 LT |
535 | if (addr_type & IPV6_ADDR_LINKLOCAL) { |
536 | if (!src_info->ipi6_ifindex) | |
537 | return -EINVAL; | |
538 | else { | |
539 | dev = dev_get_by_index(src_info->ipi6_ifindex); | |
540 | if (!dev) | |
541 | return -ENODEV; | |
542 | } | |
543 | } | |
544 | if (!ipv6_chk_addr(&src_info->ipi6_addr, dev, 0)) { | |
545 | if (dev) | |
546 | dev_put(dev); | |
547 | err = -EINVAL; | |
548 | goto exit_f; | |
549 | } | |
550 | if (dev) | |
551 | dev_put(dev); | |
552 | ||
553 | ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr); | |
554 | break; | |
555 | ||
556 | case IPV6_FLOWINFO: | |
1ab1457c | 557 | if (cmsg->cmsg_len < CMSG_LEN(4)) { |
1da177e4 LT |
558 | err = -EINVAL; |
559 | goto exit_f; | |
560 | } | |
561 | ||
562 | if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) { | |
90bcaf7b | 563 | if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) { |
1da177e4 LT |
564 | err = -EINVAL; |
565 | goto exit_f; | |
566 | } | |
567 | } | |
90bcaf7b | 568 | fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg); |
1da177e4 LT |
569 | break; |
570 | ||
333fad53 | 571 | case IPV6_2292HOPOPTS: |
1da177e4 | 572 | case IPV6_HOPOPTS: |
1ab1457c | 573 | if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
1da177e4 LT |
574 | err = -EINVAL; |
575 | goto exit_f; | |
576 | } | |
577 | ||
578 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
579 | len = ((hdr->hdrlen + 1) << 3); | |
580 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
581 | err = -EINVAL; | |
582 | goto exit_f; | |
583 | } | |
584 | if (!capable(CAP_NET_RAW)) { | |
585 | err = -EPERM; | |
586 | goto exit_f; | |
587 | } | |
588 | opt->opt_nflen += len; | |
589 | opt->hopopt = hdr; | |
590 | break; | |
591 | ||
333fad53 | 592 | case IPV6_2292DSTOPTS: |
1ab1457c | 593 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
1da177e4 LT |
594 | err = -EINVAL; |
595 | goto exit_f; | |
596 | } | |
597 | ||
598 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
599 | len = ((hdr->hdrlen + 1) << 3); | |
600 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
601 | err = -EINVAL; | |
602 | goto exit_f; | |
603 | } | |
604 | if (!capable(CAP_NET_RAW)) { | |
605 | err = -EPERM; | |
606 | goto exit_f; | |
607 | } | |
608 | if (opt->dst1opt) { | |
609 | err = -EINVAL; | |
610 | goto exit_f; | |
611 | } | |
612 | opt->opt_flen += len; | |
613 | opt->dst1opt = hdr; | |
614 | break; | |
615 | ||
333fad53 YH |
616 | case IPV6_DSTOPTS: |
617 | case IPV6_RTHDRDSTOPTS: | |
618 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { | |
619 | err = -EINVAL; | |
620 | goto exit_f; | |
621 | } | |
622 | ||
623 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
624 | len = ((hdr->hdrlen + 1) << 3); | |
625 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
626 | err = -EINVAL; | |
627 | goto exit_f; | |
628 | } | |
629 | if (!capable(CAP_NET_RAW)) { | |
630 | err = -EPERM; | |
631 | goto exit_f; | |
632 | } | |
633 | if (cmsg->cmsg_type == IPV6_DSTOPTS) { | |
634 | opt->opt_flen += len; | |
635 | opt->dst1opt = hdr; | |
636 | } else { | |
637 | opt->opt_nflen += len; | |
638 | opt->dst0opt = hdr; | |
639 | } | |
640 | break; | |
641 | ||
642 | case IPV6_2292RTHDR: | |
1da177e4 | 643 | case IPV6_RTHDR: |
1ab1457c | 644 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) { |
1da177e4 LT |
645 | err = -EINVAL; |
646 | goto exit_f; | |
647 | } | |
648 | ||
649 | rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg); | |
650 | ||
280a9d34 MN |
651 | switch (rthdr->type) { |
652 | case IPV6_SRCRT_TYPE_0: | |
653 | #ifdef CONFIG_IPV6_MIP6 | |
654 | case IPV6_SRCRT_TYPE_2: | |
655 | #endif | |
656 | break; | |
657 | default: | |
1da177e4 LT |
658 | err = -EINVAL; |
659 | goto exit_f; | |
660 | } | |
661 | ||
662 | len = ((rthdr->hdrlen + 1) << 3); | |
663 | ||
1ab1457c | 664 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
1da177e4 LT |
665 | err = -EINVAL; |
666 | goto exit_f; | |
667 | } | |
668 | ||
669 | /* segments left must also match */ | |
670 | if ((rthdr->hdrlen >> 1) != rthdr->segments_left) { | |
671 | err = -EINVAL; | |
672 | goto exit_f; | |
673 | } | |
674 | ||
675 | opt->opt_nflen += len; | |
676 | opt->srcrt = rthdr; | |
677 | ||
333fad53 | 678 | if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) { |
1da177e4 LT |
679 | int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3); |
680 | ||
681 | opt->opt_nflen += dsthdrlen; | |
682 | opt->dst0opt = opt->dst1opt; | |
683 | opt->dst1opt = NULL; | |
684 | opt->opt_flen -= dsthdrlen; | |
685 | } | |
686 | ||
687 | break; | |
688 | ||
333fad53 | 689 | case IPV6_2292HOPLIMIT: |
1da177e4 LT |
690 | case IPV6_HOPLIMIT: |
691 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { | |
692 | err = -EINVAL; | |
693 | goto exit_f; | |
694 | } | |
695 | ||
696 | *hlimit = *(int *)CMSG_DATA(cmsg); | |
697 | break; | |
698 | ||
41a1f8ea YH |
699 | case IPV6_TCLASS: |
700 | { | |
701 | int tc; | |
702 | ||
703 | err = -EINVAL; | |
704 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { | |
705 | goto exit_f; | |
706 | } | |
707 | ||
708 | tc = *(int *)CMSG_DATA(cmsg); | |
d0ee011f | 709 | if (tc < -1 || tc > 0xff) |
41a1f8ea YH |
710 | goto exit_f; |
711 | ||
712 | err = 0; | |
713 | *tclass = tc; | |
714 | ||
715 | break; | |
716 | } | |
1da177e4 | 717 | default: |
64ce2073 | 718 | LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n", |
1ab1457c | 719 | cmsg->cmsg_type); |
1da177e4 LT |
720 | err = -EINVAL; |
721 | break; | |
722 | }; | |
723 | } | |
724 | ||
725 | exit_f: | |
726 | return err; | |
727 | } |