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