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 | 7 | * |
1da177e4 LT |
8 | * This program is free software; you can redistribute it and/or |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; either version | |
11 | * 2 of the License, or (at your option) any later version. | |
12 | */ | |
13 | ||
4fc268d2 | 14 | #include <linux/capability.h> |
1da177e4 LT |
15 | #include <linux/errno.h> |
16 | #include <linux/types.h> | |
17 | #include <linux/kernel.h> | |
1da177e4 LT |
18 | #include <linux/interrupt.h> |
19 | #include <linux/socket.h> | |
20 | #include <linux/sockios.h> | |
21 | #include <linux/in6.h> | |
22 | #include <linux/ipv6.h> | |
23 | #include <linux/route.h> | |
24 | ||
25 | #include <net/ipv6.h> | |
26 | #include <net/ndisc.h> | |
27 | #include <net/addrconf.h> | |
28 | #include <net/transp_v6.h> | |
29 | #include <net/ip6_route.h> | |
c752f073 | 30 | #include <net/tcp_states.h> |
1da177e4 LT |
31 | |
32 | #include <linux/errqueue.h> | |
33 | #include <asm/uaccess.h> | |
34 | ||
35 | int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) | |
36 | { | |
37 | struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr; | |
38 | struct inet_sock *inet = inet_sk(sk); | |
39 | struct ipv6_pinfo *np = inet6_sk(sk); | |
40 | struct in6_addr *daddr, *final_p = NULL, final; | |
41 | struct dst_entry *dst; | |
42 | struct flowi fl; | |
43 | struct ip6_flowlabel *flowlabel = NULL; | |
44 | int addr_type; | |
45 | int err; | |
46 | ||
47 | if (usin->sin6_family == AF_INET) { | |
48 | if (__ipv6_only_sock(sk)) | |
49 | return -EAFNOSUPPORT; | |
50 | err = ip4_datagram_connect(sk, uaddr, addr_len); | |
51 | goto ipv4_connected; | |
52 | } | |
53 | ||
54 | if (addr_len < SIN6_LEN_RFC2133) | |
1ab1457c | 55 | return -EINVAL; |
1da177e4 | 56 | |
1ab1457c YH |
57 | if (usin->sin6_family != AF_INET6) |
58 | return -EAFNOSUPPORT; | |
1da177e4 LT |
59 | |
60 | memset(&fl, 0, sizeof(fl)); | |
61 | if (np->sndflow) { | |
62 | fl.fl6_flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK; | |
63 | if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) { | |
64 | flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); | |
65 | if (flowlabel == NULL) | |
66 | return -EINVAL; | |
67 | ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst); | |
68 | } | |
69 | } | |
70 | ||
71 | addr_type = ipv6_addr_type(&usin->sin6_addr); | |
72 | ||
73 | if (addr_type == IPV6_ADDR_ANY) { | |
74 | /* | |
75 | * connect to self | |
76 | */ | |
77 | usin->sin6_addr.s6_addr[15] = 0x01; | |
78 | } | |
79 | ||
80 | daddr = &usin->sin6_addr; | |
81 | ||
82 | if (addr_type == IPV6_ADDR_MAPPED) { | |
83 | struct sockaddr_in sin; | |
84 | ||
85 | if (__ipv6_only_sock(sk)) { | |
86 | err = -ENETUNREACH; | |
87 | goto out; | |
88 | } | |
89 | sin.sin_family = AF_INET; | |
90 | sin.sin_addr.s_addr = daddr->s6_addr32[3]; | |
91 | sin.sin_port = usin->sin6_port; | |
92 | ||
1ab1457c YH |
93 | err = ip4_datagram_connect(sk, |
94 | (struct sockaddr*) &sin, | |
1da177e4 LT |
95 | sizeof(sin)); |
96 | ||
97 | ipv4_connected: | |
98 | if (err) | |
99 | goto out; | |
1ab1457c | 100 | |
1da177e4 LT |
101 | ipv6_addr_set(&np->daddr, 0, 0, htonl(0x0000ffff), inet->daddr); |
102 | ||
103 | if (ipv6_addr_any(&np->saddr)) { | |
104 | ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000ffff), | |
105 | inet->saddr); | |
106 | } | |
107 | ||
108 | if (ipv6_addr_any(&np->rcv_saddr)) { | |
109 | ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000ffff), | |
110 | inet->rcv_saddr); | |
111 | } | |
112 | goto out; | |
113 | } | |
114 | ||
115 | if (addr_type&IPV6_ADDR_LINKLOCAL) { | |
116 | if (addr_len >= sizeof(struct sockaddr_in6) && | |
117 | usin->sin6_scope_id) { | |
118 | if (sk->sk_bound_dev_if && | |
119 | sk->sk_bound_dev_if != usin->sin6_scope_id) { | |
120 | err = -EINVAL; | |
121 | goto out; | |
122 | } | |
123 | sk->sk_bound_dev_if = usin->sin6_scope_id; | |
1da177e4 LT |
124 | } |
125 | ||
1ac4f008 BH |
126 | if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST)) |
127 | sk->sk_bound_dev_if = np->mcast_oif; | |
128 | ||
1da177e4 LT |
129 | /* Connect to link-local address requires an interface */ |
130 | if (!sk->sk_bound_dev_if) { | |
131 | err = -EINVAL; | |
132 | goto out; | |
133 | } | |
134 | } | |
135 | ||
136 | ipv6_addr_copy(&np->daddr, daddr); | |
137 | np->flow_label = fl.fl6_flowlabel; | |
138 | ||
139 | inet->dport = usin->sin6_port; | |
140 | ||
141 | /* | |
142 | * Check for a route to destination an obtain the | |
143 | * destination cache for it. | |
144 | */ | |
145 | ||
146 | fl.proto = sk->sk_protocol; | |
147 | ipv6_addr_copy(&fl.fl6_dst, &np->daddr); | |
148 | ipv6_addr_copy(&fl.fl6_src, &np->saddr); | |
149 | fl.oif = sk->sk_bound_dev_if; | |
150 | fl.fl_ip_dport = inet->dport; | |
151 | fl.fl_ip_sport = inet->sport; | |
152 | ||
153 | if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST)) | |
154 | fl.oif = np->mcast_oif; | |
155 | ||
beb8d13b VY |
156 | security_sk_classify_flow(sk, &fl); |
157 | ||
1da177e4 LT |
158 | if (flowlabel) { |
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); | |
163 | final_p = &final; | |
164 | } | |
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); | |
169 | final_p = &final; | |
170 | } | |
171 | ||
172 | err = ip6_dst_lookup(sk, &dst, &fl); | |
173 | if (err) | |
174 | goto out; | |
175 | if (final_p) | |
176 | ipv6_addr_copy(&fl.fl6_dst, final_p); | |
177 | ||
bb72845e | 178 | if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) { |
14e50e57 DM |
179 | if (err == -EREMOTE) |
180 | err = ip6_dst_blackhole(sk, &dst, &fl); | |
181 | if (err < 0) | |
182 | goto out; | |
183 | } | |
1da177e4 LT |
184 | |
185 | /* source address lookup done in ip6_dst_lookup */ | |
186 | ||
187 | if (ipv6_addr_any(&np->saddr)) | |
188 | ipv6_addr_copy(&np->saddr, &fl.fl6_src); | |
189 | ||
190 | if (ipv6_addr_any(&np->rcv_saddr)) { | |
191 | ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src); | |
192 | inet->rcv_saddr = LOOPBACK4_IPV6; | |
193 | } | |
194 | ||
195 | ip6_dst_store(sk, dst, | |
196 | ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ? | |
8e1ef0a9 YH |
197 | &np->daddr : NULL, |
198 | #ifdef CONFIG_IPV6_SUBTREES | |
199 | ipv6_addr_equal(&fl.fl6_src, &np->saddr) ? | |
200 | &np->saddr : | |
201 | #endif | |
202 | NULL); | |
1da177e4 LT |
203 | |
204 | sk->sk_state = TCP_ESTABLISHED; | |
205 | out: | |
206 | fl6_sock_release(flowlabel); | |
207 | return err; | |
208 | } | |
209 | ||
1ab1457c | 210 | void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
e69a4adc | 211 | __be16 port, u32 info, u8 *payload) |
1da177e4 LT |
212 | { |
213 | struct ipv6_pinfo *np = inet6_sk(sk); | |
cc70ab26 | 214 | struct icmp6hdr *icmph = icmp6_hdr(skb); |
1da177e4 LT |
215 | struct sock_exterr_skb *serr; |
216 | ||
217 | if (!np->recverr) | |
218 | return; | |
219 | ||
220 | skb = skb_clone(skb, GFP_ATOMIC); | |
221 | if (!skb) | |
222 | return; | |
223 | ||
224 | serr = SKB_EXT_ERR(skb); | |
225 | serr->ee.ee_errno = err; | |
226 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6; | |
1ab1457c | 227 | serr->ee.ee_type = icmph->icmp6_type; |
1da177e4 LT |
228 | serr->ee.ee_code = icmph->icmp6_code; |
229 | serr->ee.ee_pad = 0; | |
230 | serr->ee.ee_info = info; | |
231 | serr->ee.ee_data = 0; | |
d56f90a7 ACM |
232 | serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) - |
233 | skb_network_header(skb); | |
1da177e4 LT |
234 | serr->port = port; |
235 | ||
1da177e4 | 236 | __skb_pull(skb, payload - skb->data); |
bd82393c | 237 | skb_reset_transport_header(skb); |
1da177e4 LT |
238 | |
239 | if (sock_queue_err_skb(sk, skb)) | |
240 | kfree_skb(skb); | |
241 | } | |
242 | ||
243 | void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info) | |
244 | { | |
245 | struct ipv6_pinfo *np = inet6_sk(sk); | |
246 | struct sock_exterr_skb *serr; | |
247 | struct ipv6hdr *iph; | |
248 | struct sk_buff *skb; | |
249 | ||
250 | if (!np->recverr) | |
251 | return; | |
252 | ||
253 | skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); | |
254 | if (!skb) | |
255 | return; | |
256 | ||
1ced98e8 ACM |
257 | skb_put(skb, sizeof(struct ipv6hdr)); |
258 | skb_reset_network_header(skb); | |
0660e03f | 259 | iph = ipv6_hdr(skb); |
1da177e4 LT |
260 | ipv6_addr_copy(&iph->daddr, &fl->fl6_dst); |
261 | ||
262 | serr = SKB_EXT_ERR(skb); | |
263 | serr->ee.ee_errno = err; | |
264 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; | |
1ab1457c | 265 | serr->ee.ee_type = 0; |
1da177e4 LT |
266 | serr->ee.ee_code = 0; |
267 | serr->ee.ee_pad = 0; | |
268 | serr->ee.ee_info = info; | |
269 | serr->ee.ee_data = 0; | |
d56f90a7 | 270 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
1da177e4 LT |
271 | serr->port = fl->fl_ip_dport; |
272 | ||
27a884dc | 273 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
bd82393c | 274 | skb_reset_transport_header(skb); |
1da177e4 LT |
275 | |
276 | if (sock_queue_err_skb(sk, skb)) | |
277 | kfree_skb(skb); | |
278 | } | |
279 | ||
1ab1457c | 280 | /* |
1da177e4 LT |
281 | * Handle MSG_ERRQUEUE |
282 | */ | |
283 | int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len) | |
284 | { | |
285 | struct ipv6_pinfo *np = inet6_sk(sk); | |
286 | struct sock_exterr_skb *serr; | |
287 | struct sk_buff *skb, *skb2; | |
288 | struct sockaddr_in6 *sin; | |
289 | struct { | |
290 | struct sock_extended_err ee; | |
291 | struct sockaddr_in6 offender; | |
292 | } errhdr; | |
293 | int err; | |
294 | int copied; | |
295 | ||
296 | err = -EAGAIN; | |
297 | skb = skb_dequeue(&sk->sk_error_queue); | |
298 | if (skb == NULL) | |
299 | goto out; | |
300 | ||
301 | copied = skb->len; | |
302 | if (copied > len) { | |
303 | msg->msg_flags |= MSG_TRUNC; | |
304 | copied = len; | |
305 | } | |
306 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | |
307 | if (err) | |
308 | goto out_free_skb; | |
309 | ||
310 | sock_recv_timestamp(msg, sk, skb); | |
311 | ||
312 | serr = SKB_EXT_ERR(skb); | |
313 | ||
314 | sin = (struct sockaddr_in6 *)msg->msg_name; | |
315 | if (sin) { | |
d56f90a7 | 316 | const unsigned char *nh = skb_network_header(skb); |
1da177e4 LT |
317 | sin->sin6_family = AF_INET6; |
318 | sin->sin6_flowinfo = 0; | |
1ab1457c | 319 | sin->sin6_port = serr->port; |
1da177e4 LT |
320 | sin->sin6_scope_id = 0; |
321 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) { | |
322 | ipv6_addr_copy(&sin->sin6_addr, | |
d56f90a7 | 323 | (struct in6_addr *)(nh + serr->addr_offset)); |
1da177e4 | 324 | if (np->sndflow) |
d56f90a7 ACM |
325 | sin->sin6_flowinfo = |
326 | (*(__be32 *)(nh + serr->addr_offset - 24) & | |
327 | IPV6_FLOWINFO_MASK); | |
1da177e4 LT |
328 | if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL) |
329 | sin->sin6_scope_id = IP6CB(skb)->iif; | |
330 | } else { | |
331 | ipv6_addr_set(&sin->sin6_addr, 0, 0, | |
332 | htonl(0xffff), | |
d56f90a7 | 333 | *(__be32 *)(nh + serr->addr_offset)); |
1da177e4 LT |
334 | } |
335 | } | |
336 | ||
337 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); | |
338 | sin = &errhdr.offender; | |
339 | sin->sin6_family = AF_UNSPEC; | |
340 | if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) { | |
341 | sin->sin6_family = AF_INET6; | |
342 | sin->sin6_flowinfo = 0; | |
343 | sin->sin6_scope_id = 0; | |
344 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) { | |
0660e03f | 345 | ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr); |
1da177e4 LT |
346 | if (np->rxopt.all) |
347 | datagram_recv_ctl(sk, msg, skb); | |
348 | if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL) | |
349 | sin->sin6_scope_id = IP6CB(skb)->iif; | |
350 | } else { | |
351 | struct inet_sock *inet = inet_sk(sk); | |
352 | ||
353 | ipv6_addr_set(&sin->sin6_addr, 0, 0, | |
eddc9ec5 | 354 | htonl(0xffff), ip_hdr(skb)->saddr); |
1da177e4 LT |
355 | if (inet->cmsg_flags) |
356 | ip_cmsg_recv(msg, skb); | |
357 | } | |
358 | } | |
359 | ||
360 | put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr); | |
361 | ||
362 | /* Now we could try to dump offended packet options */ | |
363 | ||
364 | msg->msg_flags |= MSG_ERRQUEUE; | |
365 | err = copied; | |
366 | ||
367 | /* Reset and regenerate socket error */ | |
e0f9f858 | 368 | spin_lock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
369 | sk->sk_err = 0; |
370 | if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) { | |
371 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; | |
e0f9f858 | 372 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
373 | sk->sk_error_report(sk); |
374 | } else { | |
e0f9f858 | 375 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
376 | } |
377 | ||
1ab1457c | 378 | out_free_skb: |
1da177e4 LT |
379 | kfree_skb(skb); |
380 | out: | |
381 | return err; | |
382 | } | |
383 | ||
384 | ||
385 | ||
386 | int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb) | |
387 | { | |
388 | struct ipv6_pinfo *np = inet6_sk(sk); | |
389 | struct inet6_skb_parm *opt = IP6CB(skb); | |
d56f90a7 | 390 | unsigned char *nh = skb_network_header(skb); |
1da177e4 LT |
391 | |
392 | if (np->rxopt.bits.rxinfo) { | |
393 | struct in6_pktinfo src_info; | |
394 | ||
395 | src_info.ipi6_ifindex = opt->iif; | |
0660e03f | 396 | ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr); |
1da177e4 LT |
397 | put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); |
398 | } | |
399 | ||
400 | if (np->rxopt.bits.rxhlim) { | |
0660e03f | 401 | int hlim = ipv6_hdr(skb)->hop_limit; |
1da177e4 LT |
402 | put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); |
403 | } | |
404 | ||
41a1f8ea | 405 | if (np->rxopt.bits.rxtclass) { |
0660e03f | 406 | int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff; |
41a1f8ea YH |
407 | put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); |
408 | } | |
409 | ||
d56f90a7 ACM |
410 | if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) { |
411 | __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK; | |
1da177e4 LT |
412 | put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); |
413 | } | |
333fad53 YH |
414 | |
415 | /* HbH is allowed only once */ | |
1da177e4 | 416 | if (np->rxopt.bits.hopopts && opt->hop) { |
d56f90a7 | 417 | u8 *ptr = nh + opt->hop; |
1da177e4 LT |
418 | put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr); |
419 | } | |
333fad53 YH |
420 | |
421 | if (opt->lastopt && | |
422 | (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) { | |
423 | /* | |
424 | * Silly enough, but we need to reparse in order to | |
425 | * report extension headers (except for HbH) | |
426 | * in order. | |
427 | * | |
1ab1457c | 428 | * Also note that IPV6_RECVRTHDRDSTOPTS is NOT |
333fad53 YH |
429 | * (and WILL NOT be) defined because |
430 | * IPV6_RECVDSTOPTS is more generic. --yoshfuji | |
431 | */ | |
432 | unsigned int off = sizeof(struct ipv6hdr); | |
0660e03f | 433 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; |
333fad53 YH |
434 | |
435 | while (off <= opt->lastopt) { | |
436 | unsigned len; | |
d56f90a7 | 437 | u8 *ptr = nh + off; |
333fad53 YH |
438 | |
439 | switch(nexthdr) { | |
440 | case IPPROTO_DSTOPTS: | |
441 | nexthdr = ptr[0]; | |
442 | len = (ptr[1] + 1) << 3; | |
443 | if (np->rxopt.bits.dstopts) | |
444 | put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr); | |
445 | break; | |
446 | case IPPROTO_ROUTING: | |
447 | nexthdr = ptr[0]; | |
448 | len = (ptr[1] + 1) << 3; | |
449 | if (np->rxopt.bits.srcrt) | |
450 | put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr); | |
451 | break; | |
452 | case IPPROTO_AH: | |
453 | nexthdr = ptr[0]; | |
a3059893 | 454 | len = (ptr[1] + 2) << 2; |
333fad53 YH |
455 | break; |
456 | default: | |
457 | nexthdr = ptr[0]; | |
458 | len = (ptr[1] + 1) << 3; | |
459 | break; | |
460 | } | |
461 | ||
462 | off += len; | |
463 | } | |
464 | } | |
465 | ||
466 | /* socket options in old style */ | |
467 | if (np->rxopt.bits.rxoinfo) { | |
468 | struct in6_pktinfo src_info; | |
469 | ||
470 | src_info.ipi6_ifindex = opt->iif; | |
0660e03f | 471 | ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr); |
333fad53 YH |
472 | put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
473 | } | |
474 | if (np->rxopt.bits.rxohlim) { | |
0660e03f | 475 | int hlim = ipv6_hdr(skb)->hop_limit; |
333fad53 YH |
476 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); |
477 | } | |
478 | if (np->rxopt.bits.ohopopts && opt->hop) { | |
d56f90a7 | 479 | u8 *ptr = nh + opt->hop; |
333fad53 YH |
480 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr); |
481 | } | |
482 | if (np->rxopt.bits.odstopts && opt->dst0) { | |
d56f90a7 | 483 | u8 *ptr = nh + opt->dst0; |
333fad53 | 484 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
1da177e4 | 485 | } |
333fad53 | 486 | if (np->rxopt.bits.osrcrt && opt->srcrt) { |
d56f90a7 | 487 | struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt); |
333fad53 | 488 | put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr); |
1da177e4 | 489 | } |
333fad53 | 490 | if (np->rxopt.bits.odstopts && opt->dst1) { |
d56f90a7 | 491 | u8 *ptr = nh + opt->dst1; |
333fad53 | 492 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
1da177e4 LT |
493 | } |
494 | return 0; | |
495 | } | |
496 | ||
91e1908f YH |
497 | int datagram_send_ctl(struct net *net, |
498 | struct msghdr *msg, struct flowi *fl, | |
1da177e4 | 499 | struct ipv6_txoptions *opt, |
41a1f8ea | 500 | int *hlimit, int *tclass) |
1da177e4 LT |
501 | { |
502 | struct in6_pktinfo *src_info; | |
503 | struct cmsghdr *cmsg; | |
504 | struct ipv6_rt_hdr *rthdr; | |
505 | struct ipv6_opt_hdr *hdr; | |
506 | int len; | |
507 | int err = 0; | |
508 | ||
509 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { | |
510 | int addr_type; | |
1da177e4 LT |
511 | |
512 | if (!CMSG_OK(msg, cmsg)) { | |
513 | err = -EINVAL; | |
514 | goto exit_f; | |
515 | } | |
516 | ||
517 | if (cmsg->cmsg_level != SOL_IPV6) | |
518 | continue; | |
519 | ||
520 | switch (cmsg->cmsg_type) { | |
1ab1457c YH |
521 | case IPV6_PKTINFO: |
522 | case IPV6_2292PKTINFO: | |
187e3838 YH |
523 | { |
524 | struct net_device *dev = NULL; | |
525 | ||
1ab1457c | 526 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) { |
1da177e4 LT |
527 | err = -EINVAL; |
528 | goto exit_f; | |
529 | } | |
530 | ||
531 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); | |
1ab1457c | 532 | |
1da177e4 LT |
533 | if (src_info->ipi6_ifindex) { |
534 | if (fl->oif && src_info->ipi6_ifindex != fl->oif) | |
535 | return -EINVAL; | |
536 | fl->oif = src_info->ipi6_ifindex; | |
537 | } | |
538 | ||
187e3838 | 539 | addr_type = __ipv6_addr_type(&src_info->ipi6_addr); |
1da177e4 | 540 | |
187e3838 | 541 | if (fl->oif) { |
91e1908f | 542 | dev = dev_get_by_index(net, fl->oif); |
187e3838 YH |
543 | if (!dev) |
544 | return -ENODEV; | |
545 | } else if (addr_type & IPV6_ADDR_LINKLOCAL) | |
546 | return -EINVAL; | |
1ab1457c | 547 | |
187e3838 YH |
548 | if (addr_type != IPV6_ADDR_ANY) { |
549 | int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL; | |
91e1908f | 550 | if (!ipv6_chk_addr(net, &src_info->ipi6_addr, |
187e3838 YH |
551 | strict ? dev : NULL, 0)) |
552 | err = -EINVAL; | |
553 | else | |
554 | ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr); | |
1da177e4 | 555 | } |
187e3838 | 556 | |
1da177e4 LT |
557 | if (dev) |
558 | dev_put(dev); | |
559 | ||
187e3838 YH |
560 | if (err) |
561 | goto exit_f; | |
562 | ||
1da177e4 | 563 | break; |
187e3838 | 564 | } |
1da177e4 LT |
565 | |
566 | case IPV6_FLOWINFO: | |
1ab1457c | 567 | if (cmsg->cmsg_len < CMSG_LEN(4)) { |
1da177e4 LT |
568 | err = -EINVAL; |
569 | goto exit_f; | |
570 | } | |
571 | ||
572 | if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) { | |
90bcaf7b | 573 | if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) { |
1da177e4 LT |
574 | err = -EINVAL; |
575 | goto exit_f; | |
576 | } | |
577 | } | |
90bcaf7b | 578 | fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg); |
1da177e4 LT |
579 | break; |
580 | ||
333fad53 | 581 | case IPV6_2292HOPOPTS: |
1da177e4 | 582 | case IPV6_HOPOPTS: |
1ab1457c | 583 | if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
1da177e4 LT |
584 | err = -EINVAL; |
585 | goto exit_f; | |
586 | } | |
587 | ||
588 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
589 | len = ((hdr->hdrlen + 1) << 3); | |
590 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
591 | err = -EINVAL; | |
592 | goto exit_f; | |
593 | } | |
594 | if (!capable(CAP_NET_RAW)) { | |
595 | err = -EPERM; | |
596 | goto exit_f; | |
597 | } | |
598 | opt->opt_nflen += len; | |
599 | opt->hopopt = hdr; | |
600 | break; | |
601 | ||
333fad53 | 602 | case IPV6_2292DSTOPTS: |
1ab1457c | 603 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
1da177e4 LT |
604 | err = -EINVAL; |
605 | goto exit_f; | |
606 | } | |
607 | ||
608 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
609 | len = ((hdr->hdrlen + 1) << 3); | |
610 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
611 | err = -EINVAL; | |
612 | goto exit_f; | |
613 | } | |
614 | if (!capable(CAP_NET_RAW)) { | |
615 | err = -EPERM; | |
616 | goto exit_f; | |
617 | } | |
618 | if (opt->dst1opt) { | |
619 | err = -EINVAL; | |
620 | goto exit_f; | |
621 | } | |
622 | opt->opt_flen += len; | |
623 | opt->dst1opt = hdr; | |
624 | break; | |
625 | ||
333fad53 YH |
626 | case IPV6_DSTOPTS: |
627 | case IPV6_RTHDRDSTOPTS: | |
628 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { | |
629 | err = -EINVAL; | |
630 | goto exit_f; | |
631 | } | |
632 | ||
633 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
634 | len = ((hdr->hdrlen + 1) << 3); | |
635 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
636 | err = -EINVAL; | |
637 | goto exit_f; | |
638 | } | |
639 | if (!capable(CAP_NET_RAW)) { | |
640 | err = -EPERM; | |
641 | goto exit_f; | |
642 | } | |
643 | if (cmsg->cmsg_type == IPV6_DSTOPTS) { | |
644 | opt->opt_flen += len; | |
645 | opt->dst1opt = hdr; | |
646 | } else { | |
647 | opt->opt_nflen += len; | |
648 | opt->dst0opt = hdr; | |
649 | } | |
650 | break; | |
651 | ||
652 | case IPV6_2292RTHDR: | |
1da177e4 | 653 | case IPV6_RTHDR: |
1ab1457c | 654 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) { |
1da177e4 LT |
655 | err = -EINVAL; |
656 | goto exit_f; | |
657 | } | |
658 | ||
659 | rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg); | |
660 | ||
280a9d34 | 661 | switch (rthdr->type) { |
59fbb3a6 | 662 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
280a9d34 | 663 | case IPV6_SRCRT_TYPE_2: |
280a9d34 | 664 | break; |
bb4dbf9e | 665 | #endif |
280a9d34 | 666 | default: |
1da177e4 LT |
667 | err = -EINVAL; |
668 | goto exit_f; | |
669 | } | |
670 | ||
671 | len = ((rthdr->hdrlen + 1) << 3); | |
672 | ||
1ab1457c | 673 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
1da177e4 LT |
674 | err = -EINVAL; |
675 | goto exit_f; | |
676 | } | |
677 | ||
678 | /* segments left must also match */ | |
679 | if ((rthdr->hdrlen >> 1) != rthdr->segments_left) { | |
680 | err = -EINVAL; | |
681 | goto exit_f; | |
682 | } | |
683 | ||
684 | opt->opt_nflen += len; | |
685 | opt->srcrt = rthdr; | |
686 | ||
333fad53 | 687 | if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) { |
1da177e4 LT |
688 | int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3); |
689 | ||
690 | opt->opt_nflen += dsthdrlen; | |
691 | opt->dst0opt = opt->dst1opt; | |
692 | opt->dst1opt = NULL; | |
693 | opt->opt_flen -= dsthdrlen; | |
694 | } | |
695 | ||
696 | break; | |
697 | ||
333fad53 | 698 | case IPV6_2292HOPLIMIT: |
1da177e4 LT |
699 | case IPV6_HOPLIMIT: |
700 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { | |
701 | err = -EINVAL; | |
702 | goto exit_f; | |
703 | } | |
704 | ||
705 | *hlimit = *(int *)CMSG_DATA(cmsg); | |
e8766fc8 SW |
706 | if (*hlimit < -1 || *hlimit > 0xff) { |
707 | err = -EINVAL; | |
708 | goto exit_f; | |
709 | } | |
710 | ||
1da177e4 LT |
711 | break; |
712 | ||
41a1f8ea YH |
713 | case IPV6_TCLASS: |
714 | { | |
715 | int tc; | |
716 | ||
717 | err = -EINVAL; | |
718 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { | |
719 | goto exit_f; | |
720 | } | |
721 | ||
722 | tc = *(int *)CMSG_DATA(cmsg); | |
d0ee011f | 723 | if (tc < -1 || tc > 0xff) |
41a1f8ea YH |
724 | goto exit_f; |
725 | ||
726 | err = 0; | |
727 | *tclass = tc; | |
728 | ||
729 | break; | |
730 | } | |
1da177e4 | 731 | default: |
64ce2073 | 732 | LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n", |
1ab1457c | 733 | cmsg->cmsg_type); |
1da177e4 LT |
734 | err = -EINVAL; |
735 | break; | |
3ff50b79 | 736 | } |
1da177e4 LT |
737 | } |
738 | ||
739 | exit_f: | |
740 | return err; | |
741 | } |