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