]>
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 | |
c720c7e8 | 101 | ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr); |
1da177e4 | 102 | |
b301e82c | 103 | if (ipv6_addr_any(&np->saddr)) |
c720c7e8 | 104 | ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); |
b301e82c BH |
105 | |
106 | if (ipv6_addr_any(&np->rcv_saddr)) | |
c720c7e8 ED |
107 | ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, |
108 | &np->rcv_saddr); | |
1da177e4 | 109 | |
1da177e4 LT |
110 | goto out; |
111 | } | |
112 | ||
113 | if (addr_type&IPV6_ADDR_LINKLOCAL) { | |
114 | if (addr_len >= sizeof(struct sockaddr_in6) && | |
115 | usin->sin6_scope_id) { | |
116 | if (sk->sk_bound_dev_if && | |
117 | sk->sk_bound_dev_if != usin->sin6_scope_id) { | |
118 | err = -EINVAL; | |
119 | goto out; | |
120 | } | |
121 | sk->sk_bound_dev_if = usin->sin6_scope_id; | |
1da177e4 LT |
122 | } |
123 | ||
1ac4f008 BH |
124 | if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST)) |
125 | sk->sk_bound_dev_if = np->mcast_oif; | |
126 | ||
1da177e4 LT |
127 | /* Connect to link-local address requires an interface */ |
128 | if (!sk->sk_bound_dev_if) { | |
129 | err = -EINVAL; | |
130 | goto out; | |
131 | } | |
132 | } | |
133 | ||
134 | ipv6_addr_copy(&np->daddr, daddr); | |
135 | np->flow_label = fl.fl6_flowlabel; | |
136 | ||
c720c7e8 | 137 | inet->inet_dport = usin->sin6_port; |
1da177e4 LT |
138 | |
139 | /* | |
140 | * Check for a route to destination an obtain the | |
141 | * destination cache for it. | |
142 | */ | |
143 | ||
144 | fl.proto = sk->sk_protocol; | |
145 | ipv6_addr_copy(&fl.fl6_dst, &np->daddr); | |
146 | ipv6_addr_copy(&fl.fl6_src, &np->saddr); | |
147 | fl.oif = sk->sk_bound_dev_if; | |
51953d5b | 148 | fl.mark = sk->sk_mark; |
c720c7e8 ED |
149 | fl.fl_ip_dport = inet->inet_dport; |
150 | fl.fl_ip_sport = inet->inet_sport; | |
1da177e4 LT |
151 | |
152 | if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST)) | |
153 | fl.oif = np->mcast_oif; | |
154 | ||
beb8d13b VY |
155 | security_sk_classify_flow(sk, &fl); |
156 | ||
1da177e4 LT |
157 | if (flowlabel) { |
158 | if (flowlabel->opt && flowlabel->opt->srcrt) { | |
159 | struct rt0_hdr *rt0 = (struct rt0_hdr *) flowlabel->opt->srcrt; | |
160 | ipv6_addr_copy(&final, &fl.fl6_dst); | |
161 | ipv6_addr_copy(&fl.fl6_dst, rt0->addr); | |
162 | final_p = &final; | |
163 | } | |
164 | } else if (np->opt && np->opt->srcrt) { | |
165 | struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt; | |
166 | ipv6_addr_copy(&final, &fl.fl6_dst); | |
167 | ipv6_addr_copy(&fl.fl6_dst, rt0->addr); | |
168 | final_p = &final; | |
169 | } | |
170 | ||
171 | err = ip6_dst_lookup(sk, &dst, &fl); | |
172 | if (err) | |
173 | goto out; | |
174 | if (final_p) | |
175 | ipv6_addr_copy(&fl.fl6_dst, final_p); | |
176 | ||
52479b62 AD |
177 | err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT); |
178 | if (err < 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); | |
c720c7e8 | 192 | inet->inet_rcv_saddr = LOOPBACK4_IPV6; |
1da177e4 LT |
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 { | |
b301e82c BH |
331 | ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset), |
332 | &sin->sin6_addr); | |
1da177e4 LT |
333 | } |
334 | } | |
335 | ||
336 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); | |
337 | sin = &errhdr.offender; | |
338 | sin->sin6_family = AF_UNSPEC; | |
339 | if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) { | |
340 | sin->sin6_family = AF_INET6; | |
341 | sin->sin6_flowinfo = 0; | |
342 | sin->sin6_scope_id = 0; | |
343 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) { | |
0660e03f | 344 | ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr); |
1da177e4 LT |
345 | if (np->rxopt.all) |
346 | datagram_recv_ctl(sk, msg, skb); | |
347 | if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL) | |
348 | sin->sin6_scope_id = IP6CB(skb)->iif; | |
349 | } else { | |
350 | struct inet_sock *inet = inet_sk(sk); | |
351 | ||
b301e82c BH |
352 | ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, |
353 | &sin->sin6_addr); | |
1da177e4 LT |
354 | if (inet->cmsg_flags) |
355 | ip_cmsg_recv(msg, skb); | |
356 | } | |
357 | } | |
358 | ||
359 | put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr); | |
360 | ||
361 | /* Now we could try to dump offended packet options */ | |
362 | ||
363 | msg->msg_flags |= MSG_ERRQUEUE; | |
364 | err = copied; | |
365 | ||
366 | /* Reset and regenerate socket error */ | |
e0f9f858 | 367 | spin_lock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
368 | sk->sk_err = 0; |
369 | if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) { | |
370 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; | |
e0f9f858 | 371 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
372 | sk->sk_error_report(sk); |
373 | } else { | |
e0f9f858 | 374 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
375 | } |
376 | ||
1ab1457c | 377 | out_free_skb: |
1da177e4 LT |
378 | kfree_skb(skb); |
379 | out: | |
380 | return err; | |
381 | } | |
382 | ||
383 | ||
384 | ||
385 | int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb) | |
386 | { | |
387 | struct ipv6_pinfo *np = inet6_sk(sk); | |
388 | struct inet6_skb_parm *opt = IP6CB(skb); | |
d56f90a7 | 389 | unsigned char *nh = skb_network_header(skb); |
1da177e4 LT |
390 | |
391 | if (np->rxopt.bits.rxinfo) { | |
392 | struct in6_pktinfo src_info; | |
393 | ||
394 | src_info.ipi6_ifindex = opt->iif; | |
0660e03f | 395 | ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr); |
1da177e4 LT |
396 | put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); |
397 | } | |
398 | ||
399 | if (np->rxopt.bits.rxhlim) { | |
0660e03f | 400 | int hlim = ipv6_hdr(skb)->hop_limit; |
1da177e4 LT |
401 | put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); |
402 | } | |
403 | ||
41a1f8ea | 404 | if (np->rxopt.bits.rxtclass) { |
0660e03f | 405 | int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff; |
41a1f8ea YH |
406 | put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); |
407 | } | |
408 | ||
d56f90a7 ACM |
409 | if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) { |
410 | __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK; | |
1da177e4 LT |
411 | put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); |
412 | } | |
333fad53 YH |
413 | |
414 | /* HbH is allowed only once */ | |
1da177e4 | 415 | if (np->rxopt.bits.hopopts && opt->hop) { |
d56f90a7 | 416 | u8 *ptr = nh + opt->hop; |
1da177e4 LT |
417 | put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr); |
418 | } | |
333fad53 YH |
419 | |
420 | if (opt->lastopt && | |
421 | (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) { | |
422 | /* | |
423 | * Silly enough, but we need to reparse in order to | |
424 | * report extension headers (except for HbH) | |
425 | * in order. | |
426 | * | |
1ab1457c | 427 | * Also note that IPV6_RECVRTHDRDSTOPTS is NOT |
333fad53 YH |
428 | * (and WILL NOT be) defined because |
429 | * IPV6_RECVDSTOPTS is more generic. --yoshfuji | |
430 | */ | |
431 | unsigned int off = sizeof(struct ipv6hdr); | |
0660e03f | 432 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; |
333fad53 YH |
433 | |
434 | while (off <= opt->lastopt) { | |
435 | unsigned len; | |
d56f90a7 | 436 | u8 *ptr = nh + off; |
333fad53 YH |
437 | |
438 | switch(nexthdr) { | |
439 | case IPPROTO_DSTOPTS: | |
440 | nexthdr = ptr[0]; | |
441 | len = (ptr[1] + 1) << 3; | |
442 | if (np->rxopt.bits.dstopts) | |
443 | put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr); | |
444 | break; | |
445 | case IPPROTO_ROUTING: | |
446 | nexthdr = ptr[0]; | |
447 | len = (ptr[1] + 1) << 3; | |
448 | if (np->rxopt.bits.srcrt) | |
449 | put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr); | |
450 | break; | |
451 | case IPPROTO_AH: | |
452 | nexthdr = ptr[0]; | |
a3059893 | 453 | len = (ptr[1] + 2) << 2; |
333fad53 YH |
454 | break; |
455 | default: | |
456 | nexthdr = ptr[0]; | |
457 | len = (ptr[1] + 1) << 3; | |
458 | break; | |
459 | } | |
460 | ||
461 | off += len; | |
462 | } | |
463 | } | |
464 | ||
465 | /* socket options in old style */ | |
466 | if (np->rxopt.bits.rxoinfo) { | |
467 | struct in6_pktinfo src_info; | |
468 | ||
469 | src_info.ipi6_ifindex = opt->iif; | |
0660e03f | 470 | ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr); |
333fad53 YH |
471 | put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
472 | } | |
473 | if (np->rxopt.bits.rxohlim) { | |
0660e03f | 474 | int hlim = ipv6_hdr(skb)->hop_limit; |
333fad53 YH |
475 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); |
476 | } | |
477 | if (np->rxopt.bits.ohopopts && opt->hop) { | |
d56f90a7 | 478 | u8 *ptr = nh + opt->hop; |
333fad53 YH |
479 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr); |
480 | } | |
481 | if (np->rxopt.bits.odstopts && opt->dst0) { | |
d56f90a7 | 482 | u8 *ptr = nh + opt->dst0; |
333fad53 | 483 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
1da177e4 | 484 | } |
333fad53 | 485 | if (np->rxopt.bits.osrcrt && opt->srcrt) { |
d56f90a7 | 486 | struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt); |
333fad53 | 487 | put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr); |
1da177e4 | 488 | } |
333fad53 | 489 | if (np->rxopt.bits.odstopts && opt->dst1) { |
d56f90a7 | 490 | u8 *ptr = nh + opt->dst1; |
333fad53 | 491 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
1da177e4 LT |
492 | } |
493 | return 0; | |
494 | } | |
495 | ||
91e1908f YH |
496 | int datagram_send_ctl(struct net *net, |
497 | struct msghdr *msg, struct flowi *fl, | |
1da177e4 | 498 | struct ipv6_txoptions *opt, |
41a1f8ea | 499 | int *hlimit, int *tclass) |
1da177e4 LT |
500 | { |
501 | struct in6_pktinfo *src_info; | |
502 | struct cmsghdr *cmsg; | |
503 | struct ipv6_rt_hdr *rthdr; | |
504 | struct ipv6_opt_hdr *hdr; | |
505 | int len; | |
506 | int err = 0; | |
507 | ||
508 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { | |
509 | int addr_type; | |
1da177e4 LT |
510 | |
511 | if (!CMSG_OK(msg, cmsg)) { | |
512 | err = -EINVAL; | |
513 | goto exit_f; | |
514 | } | |
515 | ||
516 | if (cmsg->cmsg_level != SOL_IPV6) | |
517 | continue; | |
518 | ||
519 | switch (cmsg->cmsg_type) { | |
1ab1457c YH |
520 | case IPV6_PKTINFO: |
521 | case IPV6_2292PKTINFO: | |
187e3838 YH |
522 | { |
523 | struct net_device *dev = NULL; | |
524 | ||
1ab1457c | 525 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) { |
1da177e4 LT |
526 | err = -EINVAL; |
527 | goto exit_f; | |
528 | } | |
529 | ||
530 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); | |
1ab1457c | 531 | |
1da177e4 LT |
532 | if (src_info->ipi6_ifindex) { |
533 | if (fl->oif && src_info->ipi6_ifindex != fl->oif) | |
534 | return -EINVAL; | |
535 | fl->oif = src_info->ipi6_ifindex; | |
536 | } | |
537 | ||
187e3838 | 538 | addr_type = __ipv6_addr_type(&src_info->ipi6_addr); |
1da177e4 | 539 | |
187e3838 | 540 | if (fl->oif) { |
91e1908f | 541 | dev = dev_get_by_index(net, fl->oif); |
187e3838 YH |
542 | if (!dev) |
543 | return -ENODEV; | |
544 | } else if (addr_type & IPV6_ADDR_LINKLOCAL) | |
545 | return -EINVAL; | |
1ab1457c | 546 | |
187e3838 YH |
547 | if (addr_type != IPV6_ADDR_ANY) { |
548 | int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL; | |
91e1908f | 549 | if (!ipv6_chk_addr(net, &src_info->ipi6_addr, |
187e3838 YH |
550 | strict ? dev : NULL, 0)) |
551 | err = -EINVAL; | |
552 | else | |
553 | ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr); | |
1da177e4 | 554 | } |
187e3838 | 555 | |
1da177e4 LT |
556 | if (dev) |
557 | dev_put(dev); | |
558 | ||
187e3838 YH |
559 | if (err) |
560 | goto exit_f; | |
561 | ||
1da177e4 | 562 | break; |
187e3838 | 563 | } |
1da177e4 LT |
564 | |
565 | case IPV6_FLOWINFO: | |
1ab1457c | 566 | if (cmsg->cmsg_len < CMSG_LEN(4)) { |
1da177e4 LT |
567 | err = -EINVAL; |
568 | goto exit_f; | |
569 | } | |
570 | ||
571 | if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) { | |
90bcaf7b | 572 | if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) { |
1da177e4 LT |
573 | err = -EINVAL; |
574 | goto exit_f; | |
575 | } | |
576 | } | |
90bcaf7b | 577 | fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg); |
1da177e4 LT |
578 | break; |
579 | ||
333fad53 | 580 | case IPV6_2292HOPOPTS: |
1da177e4 | 581 | case IPV6_HOPOPTS: |
1ab1457c | 582 | if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
1da177e4 LT |
583 | err = -EINVAL; |
584 | goto exit_f; | |
585 | } | |
586 | ||
587 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
588 | len = ((hdr->hdrlen + 1) << 3); | |
589 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
590 | err = -EINVAL; | |
591 | goto exit_f; | |
592 | } | |
593 | if (!capable(CAP_NET_RAW)) { | |
594 | err = -EPERM; | |
595 | goto exit_f; | |
596 | } | |
597 | opt->opt_nflen += len; | |
598 | opt->hopopt = hdr; | |
599 | break; | |
600 | ||
333fad53 | 601 | case IPV6_2292DSTOPTS: |
1ab1457c | 602 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
1da177e4 LT |
603 | err = -EINVAL; |
604 | goto exit_f; | |
605 | } | |
606 | ||
607 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
608 | len = ((hdr->hdrlen + 1) << 3); | |
609 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
610 | err = -EINVAL; | |
611 | goto exit_f; | |
612 | } | |
613 | if (!capable(CAP_NET_RAW)) { | |
614 | err = -EPERM; | |
615 | goto exit_f; | |
616 | } | |
617 | if (opt->dst1opt) { | |
618 | err = -EINVAL; | |
619 | goto exit_f; | |
620 | } | |
621 | opt->opt_flen += len; | |
622 | opt->dst1opt = hdr; | |
623 | break; | |
624 | ||
333fad53 YH |
625 | case IPV6_DSTOPTS: |
626 | case IPV6_RTHDRDSTOPTS: | |
627 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { | |
628 | err = -EINVAL; | |
629 | goto exit_f; | |
630 | } | |
631 | ||
632 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
633 | len = ((hdr->hdrlen + 1) << 3); | |
634 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
635 | err = -EINVAL; | |
636 | goto exit_f; | |
637 | } | |
638 | if (!capable(CAP_NET_RAW)) { | |
639 | err = -EPERM; | |
640 | goto exit_f; | |
641 | } | |
642 | if (cmsg->cmsg_type == IPV6_DSTOPTS) { | |
643 | opt->opt_flen += len; | |
644 | opt->dst1opt = hdr; | |
645 | } else { | |
646 | opt->opt_nflen += len; | |
647 | opt->dst0opt = hdr; | |
648 | } | |
649 | break; | |
650 | ||
651 | case IPV6_2292RTHDR: | |
1da177e4 | 652 | case IPV6_RTHDR: |
1ab1457c | 653 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) { |
1da177e4 LT |
654 | err = -EINVAL; |
655 | goto exit_f; | |
656 | } | |
657 | ||
658 | rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg); | |
659 | ||
280a9d34 | 660 | switch (rthdr->type) { |
59fbb3a6 | 661 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
280a9d34 | 662 | case IPV6_SRCRT_TYPE_2: |
6e093d9d BH |
663 | if (rthdr->hdrlen != 2 || |
664 | rthdr->segments_left != 1) { | |
665 | err = -EINVAL; | |
666 | goto exit_f; | |
667 | } | |
280a9d34 | 668 | break; |
bb4dbf9e | 669 | #endif |
280a9d34 | 670 | default: |
1da177e4 LT |
671 | err = -EINVAL; |
672 | goto exit_f; | |
673 | } | |
674 | ||
675 | len = ((rthdr->hdrlen + 1) << 3); | |
676 | ||
1ab1457c | 677 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
1da177e4 LT |
678 | err = -EINVAL; |
679 | goto exit_f; | |
680 | } | |
681 | ||
682 | /* segments left must also match */ | |
683 | if ((rthdr->hdrlen >> 1) != rthdr->segments_left) { | |
684 | err = -EINVAL; | |
685 | goto exit_f; | |
686 | } | |
687 | ||
688 | opt->opt_nflen += len; | |
689 | opt->srcrt = rthdr; | |
690 | ||
333fad53 | 691 | if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) { |
1da177e4 LT |
692 | int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3); |
693 | ||
694 | opt->opt_nflen += dsthdrlen; | |
695 | opt->dst0opt = opt->dst1opt; | |
696 | opt->dst1opt = NULL; | |
697 | opt->opt_flen -= dsthdrlen; | |
698 | } | |
699 | ||
700 | break; | |
701 | ||
333fad53 | 702 | case IPV6_2292HOPLIMIT: |
1da177e4 LT |
703 | case IPV6_HOPLIMIT: |
704 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { | |
705 | err = -EINVAL; | |
706 | goto exit_f; | |
707 | } | |
708 | ||
709 | *hlimit = *(int *)CMSG_DATA(cmsg); | |
e8766fc8 SW |
710 | if (*hlimit < -1 || *hlimit > 0xff) { |
711 | err = -EINVAL; | |
712 | goto exit_f; | |
713 | } | |
714 | ||
1da177e4 LT |
715 | break; |
716 | ||
41a1f8ea YH |
717 | case IPV6_TCLASS: |
718 | { | |
719 | int tc; | |
720 | ||
721 | err = -EINVAL; | |
722 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { | |
723 | goto exit_f; | |
724 | } | |
725 | ||
726 | tc = *(int *)CMSG_DATA(cmsg); | |
d0ee011f | 727 | if (tc < -1 || tc > 0xff) |
41a1f8ea YH |
728 | goto exit_f; |
729 | ||
730 | err = 0; | |
731 | *tclass = tc; | |
732 | ||
733 | break; | |
734 | } | |
1da177e4 | 735 | default: |
64ce2073 | 736 | LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n", |
1ab1457c | 737 | cmsg->cmsg_type); |
1da177e4 | 738 | err = -EINVAL; |
4a36702e | 739 | goto exit_f; |
3ff50b79 | 740 | } |
1da177e4 LT |
741 | } |
742 | ||
743 | exit_f: | |
744 | return err; | |
745 | } |