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