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> |
a495f836 | 25 | #include <linux/export.h> |
1da177e4 LT |
26 | |
27 | #include <net/ipv6.h> | |
28 | #include <net/ndisc.h> | |
29 | #include <net/addrconf.h> | |
30 | #include <net/transp_v6.h> | |
31 | #include <net/ip6_route.h> | |
c752f073 | 32 | #include <net/tcp_states.h> |
e7219858 | 33 | #include <net/dsfield.h> |
1da177e4 LT |
34 | |
35 | #include <linux/errqueue.h> | |
7c0f6ba6 | 36 | #include <linux/uaccess.h> |
1da177e4 | 37 | |
a50feda5 | 38 | static bool ipv6_mapped_addr_any(const struct in6_addr *a) |
c15fea2d | 39 | { |
a50feda5 | 40 | return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0); |
c15fea2d MM |
41 | } |
42 | ||
80fbdb20 MKL |
43 | static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk) |
44 | { | |
45 | struct inet_sock *inet = inet_sk(sk); | |
46 | struct ipv6_pinfo *np = inet6_sk(sk); | |
47 | ||
48 | memset(fl6, 0, sizeof(*fl6)); | |
49 | fl6->flowi6_proto = sk->sk_protocol; | |
50 | fl6->daddr = sk->sk_v6_daddr; | |
51 | fl6->saddr = np->saddr; | |
52 | fl6->flowi6_oif = sk->sk_bound_dev_if; | |
53 | fl6->flowi6_mark = sk->sk_mark; | |
54 | fl6->fl6_dport = inet->inet_dport; | |
55 | fl6->fl6_sport = inet->inet_sport; | |
56 | fl6->flowlabel = np->flow_label; | |
e2d118a1 | 57 | fl6->flowi6_uid = sk->sk_uid; |
80fbdb20 MKL |
58 | |
59 | if (!fl6->flowi6_oif) | |
60 | fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; | |
61 | ||
62 | if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) | |
63 | fl6->flowi6_oif = np->mcast_oif; | |
64 | ||
65 | security_sk_classify_flow(sk, flowi6_to_flowi(fl6)); | |
66 | } | |
67 | ||
33c162a9 | 68 | int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr) |
7e2040db MKL |
69 | { |
70 | struct ip6_flowlabel *flowlabel = NULL; | |
71 | struct in6_addr *final_p, final; | |
72 | struct ipv6_txoptions *opt; | |
73 | struct dst_entry *dst; | |
74 | struct inet_sock *inet = inet_sk(sk); | |
75 | struct ipv6_pinfo *np = inet6_sk(sk); | |
76 | struct flowi6 fl6; | |
77 | int err = 0; | |
78 | ||
79 | if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) { | |
80 | flowlabel = fl6_sock_lookup(sk, np->flow_label); | |
81 | if (!flowlabel) | |
82 | return -EINVAL; | |
83 | } | |
84 | ip6_datagram_flow_key_init(&fl6, sk); | |
85 | ||
86 | rcu_read_lock(); | |
87 | opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt); | |
88 | final_p = fl6_update_dst(&fl6, opt, &final); | |
89 | rcu_read_unlock(); | |
90 | ||
91 | dst = ip6_dst_lookup_flow(sk, &fl6, final_p); | |
92 | if (IS_ERR(dst)) { | |
93 | err = PTR_ERR(dst); | |
94 | goto out; | |
95 | } | |
96 | ||
33c162a9 MKL |
97 | if (fix_sk_saddr) { |
98 | if (ipv6_addr_any(&np->saddr)) | |
99 | np->saddr = fl6.saddr; | |
7e2040db | 100 | |
33c162a9 MKL |
101 | if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) { |
102 | sk->sk_v6_rcv_saddr = fl6.saddr; | |
103 | inet->inet_rcv_saddr = LOOPBACK4_IPV6; | |
104 | if (sk->sk_prot->rehash) | |
105 | sk->sk_prot->rehash(sk); | |
106 | } | |
7e2040db MKL |
107 | } |
108 | ||
7d6850f7 | 109 | ip6_sk_dst_store_flow(sk, dst, &fl6); |
7e2040db MKL |
110 | |
111 | out: | |
112 | fl6_sock_release(flowlabel); | |
113 | return err; | |
114 | } | |
115 | ||
e646b657 MKL |
116 | void ip6_datagram_release_cb(struct sock *sk) |
117 | { | |
118 | struct dst_entry *dst; | |
119 | ||
120 | if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) | |
121 | return; | |
122 | ||
123 | rcu_read_lock(); | |
124 | dst = __sk_dst_get(sk); | |
125 | if (!dst || !dst->obsolete || | |
126 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) { | |
127 | rcu_read_unlock(); | |
128 | return; | |
129 | } | |
130 | rcu_read_unlock(); | |
131 | ||
132 | ip6_datagram_dst_update(sk, false); | |
133 | } | |
134 | EXPORT_SYMBOL_GPL(ip6_datagram_release_cb); | |
135 | ||
0382a25a GN |
136 | int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, |
137 | int addr_len) | |
1da177e4 LT |
138 | { |
139 | struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr; | |
67ba4152 IM |
140 | struct inet_sock *inet = inet_sk(sk); |
141 | struct ipv6_pinfo *np = inet6_sk(sk); | |
2f987a76 PA |
142 | struct in6_addr *daddr, old_daddr; |
143 | __be32 fl6_flowlabel = 0; | |
144 | __be32 old_fl6_flowlabel; | |
5f2fb802 | 145 | __be16 old_dport; |
1da177e4 LT |
146 | int addr_type; |
147 | int err; | |
148 | ||
149 | if (usin->sin6_family == AF_INET) { | |
150 | if (__ipv6_only_sock(sk)) | |
151 | return -EAFNOSUPPORT; | |
03645a11 | 152 | err = __ip4_datagram_connect(sk, uaddr, addr_len); |
1da177e4 LT |
153 | goto ipv4_connected; |
154 | } | |
155 | ||
156 | if (addr_len < SIN6_LEN_RFC2133) | |
1ab1457c | 157 | return -EINVAL; |
1da177e4 | 158 | |
1ab1457c YH |
159 | if (usin->sin6_family != AF_INET6) |
160 | return -EAFNOSUPPORT; | |
1da177e4 | 161 | |
7e2040db | 162 | if (np->sndflow) |
80fbdb20 | 163 | fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK; |
1da177e4 | 164 | |
052d2369 | 165 | if (ipv6_addr_any(&usin->sin6_addr)) { |
1da177e4 LT |
166 | /* |
167 | * connect to self | |
168 | */ | |
052d2369 JL |
169 | if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) |
170 | ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK), | |
171 | &usin->sin6_addr); | |
172 | else | |
173 | usin->sin6_addr = in6addr_loopback; | |
1da177e4 LT |
174 | } |
175 | ||
052d2369 JL |
176 | addr_type = ipv6_addr_type(&usin->sin6_addr); |
177 | ||
1da177e4 LT |
178 | daddr = &usin->sin6_addr; |
179 | ||
052d2369 | 180 | if (addr_type & IPV6_ADDR_MAPPED) { |
1da177e4 LT |
181 | struct sockaddr_in sin; |
182 | ||
183 | if (__ipv6_only_sock(sk)) { | |
184 | err = -ENETUNREACH; | |
185 | goto out; | |
186 | } | |
187 | sin.sin_family = AF_INET; | |
188 | sin.sin_addr.s_addr = daddr->s6_addr32[3]; | |
189 | sin.sin_port = usin->sin6_port; | |
190 | ||
03645a11 ED |
191 | err = __ip4_datagram_connect(sk, |
192 | (struct sockaddr *) &sin, | |
193 | sizeof(sin)); | |
1da177e4 LT |
194 | |
195 | ipv4_connected: | |
196 | if (err) | |
197 | goto out; | |
1ab1457c | 198 | |
efe4208f | 199 | ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr); |
1da177e4 | 200 | |
c15fea2d MM |
201 | if (ipv6_addr_any(&np->saddr) || |
202 | ipv6_mapped_addr_any(&np->saddr)) | |
c720c7e8 | 203 | ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); |
b301e82c | 204 | |
efe4208f ED |
205 | if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) || |
206 | ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) { | |
c720c7e8 | 207 | ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, |
efe4208f | 208 | &sk->sk_v6_rcv_saddr); |
719f8358 ED |
209 | if (sk->sk_prot->rehash) |
210 | sk->sk_prot->rehash(sk); | |
211 | } | |
1da177e4 | 212 | |
1da177e4 LT |
213 | goto out; |
214 | } | |
215 | ||
842df073 | 216 | if (__ipv6_addr_needs_scope_id(addr_type)) { |
1da177e4 LT |
217 | if (addr_len >= sizeof(struct sockaddr_in6) && |
218 | usin->sin6_scope_id) { | |
54dc3e33 | 219 | if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id)) { |
1da177e4 LT |
220 | err = -EINVAL; |
221 | goto out; | |
222 | } | |
223 | sk->sk_bound_dev_if = usin->sin6_scope_id; | |
1da177e4 LT |
224 | } |
225 | ||
1ac4f008 BH |
226 | if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST)) |
227 | sk->sk_bound_dev_if = np->mcast_oif; | |
228 | ||
1da177e4 LT |
229 | /* Connect to link-local address requires an interface */ |
230 | if (!sk->sk_bound_dev_if) { | |
231 | err = -EINVAL; | |
232 | goto out; | |
233 | } | |
234 | } | |
235 | ||
2f987a76 PA |
236 | /* save the current peer information before updating it */ |
237 | old_daddr = sk->sk_v6_daddr; | |
238 | old_fl6_flowlabel = np->flow_label; | |
239 | old_dport = inet->inet_dport; | |
240 | ||
efe4208f | 241 | sk->sk_v6_daddr = *daddr; |
80fbdb20 | 242 | np->flow_label = fl6_flowlabel; |
c720c7e8 | 243 | inet->inet_dport = usin->sin6_port; |
1da177e4 LT |
244 | |
245 | /* | |
246 | * Check for a route to destination an obtain the | |
247 | * destination cache for it. | |
248 | */ | |
249 | ||
33c162a9 | 250 | err = ip6_datagram_dst_update(sk, true); |
85cb73ff | 251 | if (err) { |
2f987a76 PA |
252 | /* Restore the socket peer info, to keep it consistent with |
253 | * the old socket state | |
85cb73ff | 254 | */ |
2f987a76 PA |
255 | sk->sk_v6_daddr = old_daddr; |
256 | np->flow_label = old_fl6_flowlabel; | |
257 | inet->inet_dport = old_dport; | |
1da177e4 | 258 | goto out; |
85cb73ff | 259 | } |
1da177e4 LT |
260 | |
261 | sk->sk_state = TCP_ESTABLISHED; | |
877d1f62 | 262 | sk_set_txhash(sk); |
1da177e4 | 263 | out: |
1da177e4 LT |
264 | return err; |
265 | } | |
0382a25a | 266 | EXPORT_SYMBOL_GPL(__ip6_datagram_connect); |
03645a11 ED |
267 | |
268 | int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) | |
269 | { | |
270 | int res; | |
271 | ||
272 | lock_sock(sk); | |
273 | res = __ip6_datagram_connect(sk, uaddr, addr_len); | |
274 | release_sock(sk); | |
275 | return res; | |
276 | } | |
a495f836 | 277 | EXPORT_SYMBOL_GPL(ip6_datagram_connect); |
1da177e4 | 278 | |
82b276cd HFS |
279 | int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr, |
280 | int addr_len) | |
281 | { | |
282 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr); | |
283 | if (sin6->sin6_family != AF_INET6) | |
284 | return -EAFNOSUPPORT; | |
285 | return ip6_datagram_connect(sk, uaddr, addr_len); | |
286 | } | |
287 | EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only); | |
288 | ||
1ab1457c | 289 | void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
e69a4adc | 290 | __be16 port, u32 info, u8 *payload) |
1da177e4 LT |
291 | { |
292 | struct ipv6_pinfo *np = inet6_sk(sk); | |
cc70ab26 | 293 | struct icmp6hdr *icmph = icmp6_hdr(skb); |
1da177e4 LT |
294 | struct sock_exterr_skb *serr; |
295 | ||
296 | if (!np->recverr) | |
297 | return; | |
298 | ||
299 | skb = skb_clone(skb, GFP_ATOMIC); | |
300 | if (!skb) | |
301 | return; | |
302 | ||
d40a4de0 BH |
303 | skb->protocol = htons(ETH_P_IPV6); |
304 | ||
1da177e4 LT |
305 | serr = SKB_EXT_ERR(skb); |
306 | serr->ee.ee_errno = err; | |
307 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6; | |
1ab1457c | 308 | serr->ee.ee_type = icmph->icmp6_type; |
1da177e4 LT |
309 | serr->ee.ee_code = icmph->icmp6_code; |
310 | serr->ee.ee_pad = 0; | |
311 | serr->ee.ee_info = info; | |
312 | serr->ee.ee_data = 0; | |
d56f90a7 ACM |
313 | serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) - |
314 | skb_network_header(skb); | |
1da177e4 LT |
315 | serr->port = port; |
316 | ||
1da177e4 | 317 | __skb_pull(skb, payload - skb->data); |
bd82393c | 318 | skb_reset_transport_header(skb); |
1da177e4 LT |
319 | |
320 | if (sock_queue_err_skb(sk, skb)) | |
321 | kfree_skb(skb); | |
322 | } | |
323 | ||
4c9483b2 | 324 | void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info) |
1da177e4 | 325 | { |
1c1e9d2b | 326 | const struct ipv6_pinfo *np = inet6_sk(sk); |
1da177e4 LT |
327 | struct sock_exterr_skb *serr; |
328 | struct ipv6hdr *iph; | |
329 | struct sk_buff *skb; | |
330 | ||
331 | if (!np->recverr) | |
332 | return; | |
333 | ||
334 | skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); | |
335 | if (!skb) | |
336 | return; | |
337 | ||
d40a4de0 BH |
338 | skb->protocol = htons(ETH_P_IPV6); |
339 | ||
1ced98e8 ACM |
340 | skb_put(skb, sizeof(struct ipv6hdr)); |
341 | skb_reset_network_header(skb); | |
0660e03f | 342 | iph = ipv6_hdr(skb); |
4e3fd7a0 | 343 | iph->daddr = fl6->daddr; |
1da177e4 LT |
344 | |
345 | serr = SKB_EXT_ERR(skb); | |
346 | serr->ee.ee_errno = err; | |
347 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; | |
1ab1457c | 348 | serr->ee.ee_type = 0; |
1da177e4 LT |
349 | serr->ee.ee_code = 0; |
350 | serr->ee.ee_pad = 0; | |
351 | serr->ee.ee_info = info; | |
352 | serr->ee.ee_data = 0; | |
d56f90a7 | 353 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
1958b856 | 354 | serr->port = fl6->fl6_dport; |
1da177e4 | 355 | |
27a884dc | 356 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
bd82393c | 357 | skb_reset_transport_header(skb); |
1da177e4 LT |
358 | |
359 | if (sock_queue_err_skb(sk, skb)) | |
360 | kfree_skb(skb); | |
361 | } | |
362 | ||
4c9483b2 | 363 | void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu) |
4b340ae2 BH |
364 | { |
365 | struct ipv6_pinfo *np = inet6_sk(sk); | |
366 | struct ipv6hdr *iph; | |
367 | struct sk_buff *skb; | |
368 | struct ip6_mtuinfo *mtu_info; | |
369 | ||
370 | if (!np->rxopt.bits.rxpmtu) | |
371 | return; | |
372 | ||
373 | skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); | |
374 | if (!skb) | |
375 | return; | |
376 | ||
377 | skb_put(skb, sizeof(struct ipv6hdr)); | |
378 | skb_reset_network_header(skb); | |
379 | iph = ipv6_hdr(skb); | |
4e3fd7a0 | 380 | iph->daddr = fl6->daddr; |
4b340ae2 BH |
381 | |
382 | mtu_info = IP6CBMTU(skb); | |
4b340ae2 BH |
383 | |
384 | mtu_info->ip6m_mtu = mtu; | |
385 | mtu_info->ip6m_addr.sin6_family = AF_INET6; | |
386 | mtu_info->ip6m_addr.sin6_port = 0; | |
387 | mtu_info->ip6m_addr.sin6_flowinfo = 0; | |
4c9483b2 | 388 | mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif; |
4e3fd7a0 | 389 | mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr; |
4b340ae2 BH |
390 | |
391 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); | |
392 | skb_reset_transport_header(skb); | |
393 | ||
394 | skb = xchg(&np->rxpmtu, skb); | |
395 | kfree_skb(skb); | |
396 | } | |
397 | ||
34b99df4 JA |
398 | /* For some errors we have valid addr_offset even with zero payload and |
399 | * zero port. Also, addr_offset should be supported if port is set. | |
400 | */ | |
401 | static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr) | |
402 | { | |
403 | return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 || | |
404 | serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || | |
405 | serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port; | |
406 | } | |
407 | ||
c247f053 WB |
408 | /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL. |
409 | * | |
410 | * At one point, excluding local errors was a quick test to identify icmp/icmp6 | |
411 | * errors. This is no longer true, but the test remained, so the v6 stack, | |
412 | * unlike v4, also honors cmsg requests on all wifi and timestamp errors. | |
c247f053 WB |
413 | */ |
414 | static bool ip6_datagram_support_cmsg(struct sk_buff *skb, | |
415 | struct sock_exterr_skb *serr) | |
829ae9d6 | 416 | { |
c247f053 WB |
417 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || |
418 | serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) | |
419 | return true; | |
420 | ||
421 | if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL) | |
422 | return false; | |
423 | ||
1862d620 | 424 | if (!IP6CB(skb)->iif) |
c247f053 | 425 | return false; |
829ae9d6 | 426 | |
c247f053 | 427 | return true; |
829ae9d6 WB |
428 | } |
429 | ||
1ab1457c | 430 | /* |
1da177e4 LT |
431 | * Handle MSG_ERRQUEUE |
432 | */ | |
85fbaa75 | 433 | int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) |
1da177e4 LT |
434 | { |
435 | struct ipv6_pinfo *np = inet6_sk(sk); | |
436 | struct sock_exterr_skb *serr; | |
364a9e93 | 437 | struct sk_buff *skb; |
342dfc30 | 438 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name); |
1da177e4 LT |
439 | struct { |
440 | struct sock_extended_err ee; | |
441 | struct sockaddr_in6 offender; | |
442 | } errhdr; | |
443 | int err; | |
444 | int copied; | |
445 | ||
446 | err = -EAGAIN; | |
364a9e93 | 447 | skb = sock_dequeue_err_skb(sk); |
63159f29 | 448 | if (!skb) |
1da177e4 LT |
449 | goto out; |
450 | ||
451 | copied = skb->len; | |
452 | if (copied > len) { | |
453 | msg->msg_flags |= MSG_TRUNC; | |
454 | copied = len; | |
455 | } | |
51f3d02b | 456 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
960a2628 ED |
457 | if (unlikely(err)) { |
458 | kfree_skb(skb); | |
459 | return err; | |
460 | } | |
1da177e4 LT |
461 | sock_recv_timestamp(msg, sk, skb); |
462 | ||
463 | serr = SKB_EXT_ERR(skb); | |
464 | ||
34b99df4 | 465 | if (sin && ipv6_datagram_support_addr(serr)) { |
d56f90a7 | 466 | const unsigned char *nh = skb_network_header(skb); |
1da177e4 LT |
467 | sin->sin6_family = AF_INET6; |
468 | sin->sin6_flowinfo = 0; | |
1ab1457c | 469 | sin->sin6_port = serr->port; |
d40a4de0 | 470 | if (skb->protocol == htons(ETH_P_IPV6)) { |
6c40d100 YH |
471 | const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset), |
472 | struct ipv6hdr, daddr); | |
473 | sin->sin6_addr = ip6h->daddr; | |
1da177e4 | 474 | if (np->sndflow) |
6502ca52 | 475 | sin->sin6_flowinfo = ip6_flowinfo(ip6h); |
842df073 HFS |
476 | sin->sin6_scope_id = |
477 | ipv6_iface_scope_id(&sin->sin6_addr, | |
478 | IP6CB(skb)->iif); | |
1da177e4 | 479 | } else { |
b301e82c BH |
480 | ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset), |
481 | &sin->sin6_addr); | |
842df073 | 482 | sin->sin6_scope_id = 0; |
1da177e4 | 483 | } |
85fbaa75 | 484 | *addr_len = sizeof(*sin); |
1da177e4 LT |
485 | } |
486 | ||
487 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); | |
488 | sin = &errhdr.offender; | |
f812116b | 489 | memset(sin, 0, sizeof(*sin)); |
c247f053 WB |
490 | |
491 | if (ip6_datagram_support_cmsg(skb, serr)) { | |
1da177e4 | 492 | sin->sin6_family = AF_INET6; |
c247f053 | 493 | if (np->rxopt.all) |
4b261c75 | 494 | ip6_datagram_recv_common_ctl(sk, msg, skb); |
d40a4de0 | 495 | if (skb->protocol == htons(ETH_P_IPV6)) { |
4e3fd7a0 | 496 | sin->sin6_addr = ipv6_hdr(skb)->saddr; |
1da177e4 | 497 | if (np->rxopt.all) |
4b261c75 | 498 | ip6_datagram_recv_specific_ctl(sk, msg, skb); |
842df073 HFS |
499 | sin->sin6_scope_id = |
500 | ipv6_iface_scope_id(&sin->sin6_addr, | |
501 | IP6CB(skb)->iif); | |
1da177e4 | 502 | } else { |
b301e82c BH |
503 | ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, |
504 | &sin->sin6_addr); | |
f812116b | 505 | if (inet_sk(sk)->cmsg_flags) |
1da177e4 LT |
506 | ip_cmsg_recv(msg, skb); |
507 | } | |
508 | } | |
509 | ||
510 | put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr); | |
511 | ||
512 | /* Now we could try to dump offended packet options */ | |
513 | ||
514 | msg->msg_flags |= MSG_ERRQUEUE; | |
515 | err = copied; | |
516 | ||
960a2628 | 517 | consume_skb(skb); |
1da177e4 LT |
518 | out: |
519 | return err; | |
520 | } | |
a495f836 | 521 | EXPORT_SYMBOL_GPL(ipv6_recv_error); |
1da177e4 | 522 | |
4b340ae2 BH |
523 | /* |
524 | * Handle IPV6_RECVPATHMTU | |
525 | */ | |
85fbaa75 HFS |
526 | int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len, |
527 | int *addr_len) | |
4b340ae2 BH |
528 | { |
529 | struct ipv6_pinfo *np = inet6_sk(sk); | |
530 | struct sk_buff *skb; | |
4b340ae2 | 531 | struct ip6_mtuinfo mtu_info; |
342dfc30 | 532 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name); |
4b340ae2 BH |
533 | int err; |
534 | int copied; | |
535 | ||
536 | err = -EAGAIN; | |
537 | skb = xchg(&np->rxpmtu, NULL); | |
63159f29 | 538 | if (!skb) |
4b340ae2 BH |
539 | goto out; |
540 | ||
541 | copied = skb->len; | |
542 | if (copied > len) { | |
543 | msg->msg_flags |= MSG_TRUNC; | |
544 | copied = len; | |
545 | } | |
51f3d02b | 546 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
4b340ae2 BH |
547 | if (err) |
548 | goto out_free_skb; | |
549 | ||
550 | sock_recv_timestamp(msg, sk, skb); | |
551 | ||
552 | memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info)); | |
553 | ||
4b340ae2 BH |
554 | if (sin) { |
555 | sin->sin6_family = AF_INET6; | |
556 | sin->sin6_flowinfo = 0; | |
557 | sin->sin6_port = 0; | |
558 | sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id; | |
4e3fd7a0 | 559 | sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr; |
85fbaa75 | 560 | *addr_len = sizeof(*sin); |
4b340ae2 BH |
561 | } |
562 | ||
563 | put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info); | |
564 | ||
565 | err = copied; | |
566 | ||
567 | out_free_skb: | |
568 | kfree_skb(skb); | |
569 | out: | |
570 | return err; | |
571 | } | |
1da177e4 LT |
572 | |
573 | ||
4b261c75 HFS |
574 | void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg, |
575 | struct sk_buff *skb) | |
1da177e4 LT |
576 | { |
577 | struct ipv6_pinfo *np = inet6_sk(sk); | |
4b261c75 | 578 | bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6); |
1da177e4 LT |
579 | |
580 | if (np->rxopt.bits.rxinfo) { | |
581 | struct in6_pktinfo src_info; | |
582 | ||
4b261c75 HFS |
583 | if (is_ipv6) { |
584 | src_info.ipi6_ifindex = IP6CB(skb)->iif; | |
585 | src_info.ipi6_addr = ipv6_hdr(skb)->daddr; | |
586 | } else { | |
587 | src_info.ipi6_ifindex = | |
588 | PKTINFO_SKB_CB(skb)->ipi_ifindex; | |
589 | ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr, | |
590 | &src_info.ipi6_addr); | |
591 | } | |
829ae9d6 WB |
592 | |
593 | if (src_info.ipi6_ifindex >= 0) | |
594 | put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, | |
595 | sizeof(src_info), &src_info); | |
1da177e4 | 596 | } |
4b261c75 HFS |
597 | } |
598 | ||
599 | void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg, | |
600 | struct sk_buff *skb) | |
601 | { | |
602 | struct ipv6_pinfo *np = inet6_sk(sk); | |
603 | struct inet6_skb_parm *opt = IP6CB(skb); | |
604 | unsigned char *nh = skb_network_header(skb); | |
1da177e4 LT |
605 | |
606 | if (np->rxopt.bits.rxhlim) { | |
0660e03f | 607 | int hlim = ipv6_hdr(skb)->hop_limit; |
1da177e4 LT |
608 | put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); |
609 | } | |
610 | ||
41a1f8ea | 611 | if (np->rxopt.bits.rxtclass) { |
e7219858 | 612 | int tclass = ipv6_get_dsfield(ipv6_hdr(skb)); |
41a1f8ea YH |
613 | put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); |
614 | } | |
615 | ||
6502ca52 YH |
616 | if (np->rxopt.bits.rxflow) { |
617 | __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh); | |
618 | if (flowinfo) | |
619 | put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); | |
1da177e4 | 620 | } |
333fad53 YH |
621 | |
622 | /* HbH is allowed only once */ | |
8b58a398 FW |
623 | if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) { |
624 | u8 *ptr = nh + sizeof(struct ipv6hdr); | |
1da177e4 LT |
625 | put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr); |
626 | } | |
333fad53 YH |
627 | |
628 | if (opt->lastopt && | |
629 | (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) { | |
630 | /* | |
631 | * Silly enough, but we need to reparse in order to | |
632 | * report extension headers (except for HbH) | |
633 | * in order. | |
634 | * | |
1ab1457c | 635 | * Also note that IPV6_RECVRTHDRDSTOPTS is NOT |
333fad53 YH |
636 | * (and WILL NOT be) defined because |
637 | * IPV6_RECVDSTOPTS is more generic. --yoshfuji | |
638 | */ | |
639 | unsigned int off = sizeof(struct ipv6hdr); | |
0660e03f | 640 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; |
333fad53 YH |
641 | |
642 | while (off <= opt->lastopt) { | |
95c96174 | 643 | unsigned int len; |
d56f90a7 | 644 | u8 *ptr = nh + off; |
333fad53 | 645 | |
b5a4257c | 646 | switch (nexthdr) { |
333fad53 YH |
647 | case IPPROTO_DSTOPTS: |
648 | nexthdr = ptr[0]; | |
649 | len = (ptr[1] + 1) << 3; | |
650 | if (np->rxopt.bits.dstopts) | |
651 | put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr); | |
652 | break; | |
653 | case IPPROTO_ROUTING: | |
654 | nexthdr = ptr[0]; | |
655 | len = (ptr[1] + 1) << 3; | |
656 | if (np->rxopt.bits.srcrt) | |
657 | put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr); | |
658 | break; | |
659 | case IPPROTO_AH: | |
660 | nexthdr = ptr[0]; | |
a3059893 | 661 | len = (ptr[1] + 2) << 2; |
333fad53 YH |
662 | break; |
663 | default: | |
664 | nexthdr = ptr[0]; | |
665 | len = (ptr[1] + 1) << 3; | |
666 | break; | |
667 | } | |
668 | ||
669 | off += len; | |
670 | } | |
671 | } | |
672 | ||
673 | /* socket options in old style */ | |
674 | if (np->rxopt.bits.rxoinfo) { | |
675 | struct in6_pktinfo src_info; | |
676 | ||
677 | src_info.ipi6_ifindex = opt->iif; | |
4e3fd7a0 | 678 | src_info.ipi6_addr = ipv6_hdr(skb)->daddr; |
333fad53 YH |
679 | put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
680 | } | |
681 | if (np->rxopt.bits.rxohlim) { | |
0660e03f | 682 | int hlim = ipv6_hdr(skb)->hop_limit; |
333fad53 YH |
683 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); |
684 | } | |
8b58a398 FW |
685 | if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) { |
686 | u8 *ptr = nh + sizeof(struct ipv6hdr); | |
333fad53 YH |
687 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr); |
688 | } | |
689 | if (np->rxopt.bits.odstopts && opt->dst0) { | |
d56f90a7 | 690 | u8 *ptr = nh + opt->dst0; |
333fad53 | 691 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
1da177e4 | 692 | } |
333fad53 | 693 | if (np->rxopt.bits.osrcrt && opt->srcrt) { |
d56f90a7 | 694 | struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt); |
333fad53 | 695 | put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr); |
1da177e4 | 696 | } |
333fad53 | 697 | if (np->rxopt.bits.odstopts && opt->dst1) { |
d56f90a7 | 698 | u8 *ptr = nh + opt->dst1; |
333fad53 | 699 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
1da177e4 | 700 | } |
6c468622 BS |
701 | if (np->rxopt.bits.rxorigdstaddr) { |
702 | struct sockaddr_in6 sin6; | |
2efd4fca WB |
703 | __be16 *ports; |
704 | int end; | |
6c468622 | 705 | |
2efd4fca WB |
706 | end = skb_transport_offset(skb) + 4; |
707 | if (end <= 0 || pskb_may_pull(skb, end)) { | |
6c468622 BS |
708 | /* All current transport protocols have the port numbers in the |
709 | * first four bytes of the transport header and this function is | |
710 | * written with this assumption in mind. | |
711 | */ | |
2efd4fca | 712 | ports = (__be16 *)skb_transport_header(skb); |
6c468622 BS |
713 | |
714 | sin6.sin6_family = AF_INET6; | |
4e3fd7a0 | 715 | sin6.sin6_addr = ipv6_hdr(skb)->daddr; |
6c468622 BS |
716 | sin6.sin6_port = ports[1]; |
717 | sin6.sin6_flowinfo = 0; | |
3868b7aa HFS |
718 | sin6.sin6_scope_id = |
719 | ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr, | |
720 | opt->iif); | |
6c468622 BS |
721 | |
722 | put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6); | |
723 | } | |
724 | } | |
0cc0aa61 WB |
725 | if (np->rxopt.bits.recvfragsize && opt->frag_max_size) { |
726 | int val = opt->frag_max_size; | |
727 | ||
728 | put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val); | |
729 | } | |
4b261c75 HFS |
730 | } |
731 | ||
732 | void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg, | |
733 | struct sk_buff *skb) | |
734 | { | |
735 | ip6_datagram_recv_common_ctl(sk, msg, skb); | |
736 | ip6_datagram_recv_specific_ctl(sk, msg, skb); | |
1da177e4 | 737 | } |
8e72d37e | 738 | EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl); |
1da177e4 | 739 | |
73df66f8 TP |
740 | int ip6_datagram_send_ctl(struct net *net, struct sock *sk, |
741 | struct msghdr *msg, struct flowi6 *fl6, | |
5fdaa88d | 742 | struct ipcm6_cookie *ipc6) |
1da177e4 LT |
743 | { |
744 | struct in6_pktinfo *src_info; | |
745 | struct cmsghdr *cmsg; | |
746 | struct ipv6_rt_hdr *rthdr; | |
747 | struct ipv6_opt_hdr *hdr; | |
26879da5 | 748 | struct ipv6_txoptions *opt = ipc6->opt; |
1da177e4 LT |
749 | int len; |
750 | int err = 0; | |
751 | ||
f95b414e | 752 | for_each_cmsghdr(cmsg, msg) { |
1da177e4 | 753 | int addr_type; |
1da177e4 LT |
754 | |
755 | if (!CMSG_OK(msg, cmsg)) { | |
756 | err = -EINVAL; | |
757 | goto exit_f; | |
758 | } | |
759 | ||
ad1e46a8 | 760 | if (cmsg->cmsg_level == SOL_SOCKET) { |
5fdaa88d | 761 | err = __sock_cmsg_send(sk, msg, cmsg, &ipc6->sockc); |
2632616b ED |
762 | if (err) |
763 | return err; | |
ad1e46a8 SHY |
764 | continue; |
765 | } | |
766 | ||
1da177e4 LT |
767 | if (cmsg->cmsg_level != SOL_IPV6) |
768 | continue; | |
769 | ||
770 | switch (cmsg->cmsg_type) { | |
1ab1457c YH |
771 | case IPV6_PKTINFO: |
772 | case IPV6_2292PKTINFO: | |
187e3838 YH |
773 | { |
774 | struct net_device *dev = NULL; | |
775 | ||
1ab1457c | 776 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) { |
1da177e4 LT |
777 | err = -EINVAL; |
778 | goto exit_f; | |
779 | } | |
780 | ||
781 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); | |
1ab1457c | 782 | |
1da177e4 | 783 | if (src_info->ipi6_ifindex) { |
4c9483b2 DM |
784 | if (fl6->flowi6_oif && |
785 | src_info->ipi6_ifindex != fl6->flowi6_oif) | |
1da177e4 | 786 | return -EINVAL; |
4c9483b2 | 787 | fl6->flowi6_oif = src_info->ipi6_ifindex; |
1da177e4 LT |
788 | } |
789 | ||
187e3838 | 790 | addr_type = __ipv6_addr_type(&src_info->ipi6_addr); |
1da177e4 | 791 | |
536b2e92 | 792 | rcu_read_lock(); |
4c9483b2 DM |
793 | if (fl6->flowi6_oif) { |
794 | dev = dev_get_by_index_rcu(net, fl6->flowi6_oif); | |
536b2e92 ED |
795 | if (!dev) { |
796 | rcu_read_unlock(); | |
187e3838 | 797 | return -ENODEV; |
536b2e92 ED |
798 | } |
799 | } else if (addr_type & IPV6_ADDR_LINKLOCAL) { | |
800 | rcu_read_unlock(); | |
187e3838 | 801 | return -EINVAL; |
536b2e92 | 802 | } |
1ab1457c | 803 | |
187e3838 YH |
804 | if (addr_type != IPV6_ADDR_ANY) { |
805 | int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL; | |
83ba4645 | 806 | if (!ipv6_can_nonlocal_bind(net, inet_sk(sk)) && |
232378e8 DA |
807 | !ipv6_chk_addr_and_flags(net, &src_info->ipi6_addr, |
808 | dev, !strict, 0, | |
809 | IFA_F_TENTATIVE) && | |
7c90cc2d FLB |
810 | !ipv6_chk_acast_addr_src(net, dev, |
811 | &src_info->ipi6_addr)) | |
187e3838 YH |
812 | err = -EINVAL; |
813 | else | |
4e3fd7a0 | 814 | fl6->saddr = src_info->ipi6_addr; |
1da177e4 | 815 | } |
187e3838 | 816 | |
536b2e92 | 817 | rcu_read_unlock(); |
1da177e4 | 818 | |
187e3838 YH |
819 | if (err) |
820 | goto exit_f; | |
821 | ||
1da177e4 | 822 | break; |
187e3838 | 823 | } |
1da177e4 LT |
824 | |
825 | case IPV6_FLOWINFO: | |
1ab1457c | 826 | if (cmsg->cmsg_len < CMSG_LEN(4)) { |
1da177e4 LT |
827 | err = -EINVAL; |
828 | goto exit_f; | |
829 | } | |
830 | ||
4c9483b2 DM |
831 | if (fl6->flowlabel&IPV6_FLOWINFO_MASK) { |
832 | if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) { | |
1da177e4 LT |
833 | err = -EINVAL; |
834 | goto exit_f; | |
835 | } | |
836 | } | |
4c9483b2 | 837 | fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg); |
1da177e4 LT |
838 | break; |
839 | ||
333fad53 | 840 | case IPV6_2292HOPOPTS: |
1da177e4 | 841 | case IPV6_HOPOPTS: |
1ab1457c | 842 | if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
1da177e4 LT |
843 | err = -EINVAL; |
844 | goto exit_f; | |
845 | } | |
846 | ||
847 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
848 | len = ((hdr->hdrlen + 1) << 3); | |
849 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
850 | err = -EINVAL; | |
851 | goto exit_f; | |
852 | } | |
af31f412 | 853 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) { |
1da177e4 LT |
854 | err = -EPERM; |
855 | goto exit_f; | |
856 | } | |
857 | opt->opt_nflen += len; | |
858 | opt->hopopt = hdr; | |
859 | break; | |
860 | ||
333fad53 | 861 | case IPV6_2292DSTOPTS: |
1ab1457c | 862 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
1da177e4 LT |
863 | err = -EINVAL; |
864 | goto exit_f; | |
865 | } | |
866 | ||
867 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
868 | len = ((hdr->hdrlen + 1) << 3); | |
869 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
870 | err = -EINVAL; | |
871 | goto exit_f; | |
872 | } | |
af31f412 | 873 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) { |
1da177e4 LT |
874 | err = -EPERM; |
875 | goto exit_f; | |
876 | } | |
877 | if (opt->dst1opt) { | |
878 | err = -EINVAL; | |
879 | goto exit_f; | |
880 | } | |
881 | opt->opt_flen += len; | |
882 | opt->dst1opt = hdr; | |
883 | break; | |
884 | ||
333fad53 YH |
885 | case IPV6_DSTOPTS: |
886 | case IPV6_RTHDRDSTOPTS: | |
887 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { | |
888 | err = -EINVAL; | |
889 | goto exit_f; | |
890 | } | |
891 | ||
892 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); | |
893 | len = ((hdr->hdrlen + 1) << 3); | |
894 | if (cmsg->cmsg_len < CMSG_LEN(len)) { | |
895 | err = -EINVAL; | |
896 | goto exit_f; | |
897 | } | |
af31f412 | 898 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) { |
333fad53 YH |
899 | err = -EPERM; |
900 | goto exit_f; | |
901 | } | |
902 | if (cmsg->cmsg_type == IPV6_DSTOPTS) { | |
903 | opt->opt_flen += len; | |
904 | opt->dst1opt = hdr; | |
905 | } else { | |
906 | opt->opt_nflen += len; | |
907 | opt->dst0opt = hdr; | |
908 | } | |
909 | break; | |
910 | ||
911 | case IPV6_2292RTHDR: | |
1da177e4 | 912 | case IPV6_RTHDR: |
1ab1457c | 913 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) { |
1da177e4 LT |
914 | err = -EINVAL; |
915 | goto exit_f; | |
916 | } | |
917 | ||
918 | rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg); | |
919 | ||
280a9d34 | 920 | switch (rthdr->type) { |
07a93626 | 921 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
280a9d34 | 922 | case IPV6_SRCRT_TYPE_2: |
6e093d9d BH |
923 | if (rthdr->hdrlen != 2 || |
924 | rthdr->segments_left != 1) { | |
925 | err = -EINVAL; | |
926 | goto exit_f; | |
927 | } | |
280a9d34 | 928 | break; |
bb4dbf9e | 929 | #endif |
280a9d34 | 930 | default: |
1da177e4 LT |
931 | err = -EINVAL; |
932 | goto exit_f; | |
933 | } | |
934 | ||
935 | len = ((rthdr->hdrlen + 1) << 3); | |
936 | ||
1ab1457c | 937 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
1da177e4 LT |
938 | err = -EINVAL; |
939 | goto exit_f; | |
940 | } | |
941 | ||
942 | /* segments left must also match */ | |
943 | if ((rthdr->hdrlen >> 1) != rthdr->segments_left) { | |
944 | err = -EINVAL; | |
945 | goto exit_f; | |
946 | } | |
947 | ||
948 | opt->opt_nflen += len; | |
949 | opt->srcrt = rthdr; | |
950 | ||
333fad53 | 951 | if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) { |
1da177e4 LT |
952 | int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3); |
953 | ||
954 | opt->opt_nflen += dsthdrlen; | |
955 | opt->dst0opt = opt->dst1opt; | |
956 | opt->dst1opt = NULL; | |
957 | opt->opt_flen -= dsthdrlen; | |
958 | } | |
959 | ||
960 | break; | |
961 | ||
333fad53 | 962 | case IPV6_2292HOPLIMIT: |
1da177e4 LT |
963 | case IPV6_HOPLIMIT: |
964 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { | |
965 | err = -EINVAL; | |
966 | goto exit_f; | |
967 | } | |
968 | ||
26879da5 WW |
969 | ipc6->hlimit = *(int *)CMSG_DATA(cmsg); |
970 | if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) { | |
e8766fc8 SW |
971 | err = -EINVAL; |
972 | goto exit_f; | |
973 | } | |
974 | ||
1da177e4 LT |
975 | break; |
976 | ||
41a1f8ea YH |
977 | case IPV6_TCLASS: |
978 | { | |
979 | int tc; | |
980 | ||
981 | err = -EINVAL; | |
b5a4257c | 982 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) |
41a1f8ea | 983 | goto exit_f; |
41a1f8ea YH |
984 | |
985 | tc = *(int *)CMSG_DATA(cmsg); | |
d0ee011f | 986 | if (tc < -1 || tc > 0xff) |
41a1f8ea YH |
987 | goto exit_f; |
988 | ||
989 | err = 0; | |
26879da5 | 990 | ipc6->tclass = tc; |
41a1f8ea | 991 | |
13b52cd4 BH |
992 | break; |
993 | } | |
994 | ||
995 | case IPV6_DONTFRAG: | |
996 | { | |
997 | int df; | |
998 | ||
999 | err = -EINVAL; | |
b5a4257c | 1000 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) |
13b52cd4 | 1001 | goto exit_f; |
13b52cd4 BH |
1002 | |
1003 | df = *(int *)CMSG_DATA(cmsg); | |
1004 | if (df < 0 || df > 1) | |
1005 | goto exit_f; | |
1006 | ||
1007 | err = 0; | |
26879da5 | 1008 | ipc6->dontfrag = df; |
13b52cd4 | 1009 | |
41a1f8ea YH |
1010 | break; |
1011 | } | |
1da177e4 | 1012 | default: |
ba7a46f1 JP |
1013 | net_dbg_ratelimited("invalid cmsg type: %d\n", |
1014 | cmsg->cmsg_type); | |
1da177e4 | 1015 | err = -EINVAL; |
4a36702e | 1016 | goto exit_f; |
3ff50b79 | 1017 | } |
1da177e4 LT |
1018 | } |
1019 | ||
1020 | exit_f: | |
1021 | return err; | |
1022 | } | |
73df66f8 | 1023 | EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl); |
17ef66af | 1024 | |
6c206b20 PA |
1025 | void __ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp, |
1026 | __u16 srcp, __u16 destp, int rqueue, int bucket) | |
17ef66af | 1027 | { |
17ef66af LC |
1028 | const struct in6_addr *dest, *src; |
1029 | ||
efe4208f ED |
1030 | dest = &sp->sk_v6_daddr; |
1031 | src = &sp->sk_v6_rcv_saddr; | |
17ef66af LC |
1032 | seq_printf(seq, |
1033 | "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " | |
d14c5ab6 | 1034 | "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n", |
17ef66af LC |
1035 | bucket, |
1036 | src->s6_addr32[0], src->s6_addr32[1], | |
1037 | src->s6_addr32[2], src->s6_addr32[3], srcp, | |
1038 | dest->s6_addr32[0], dest->s6_addr32[1], | |
1039 | dest->s6_addr32[2], dest->s6_addr32[3], destp, | |
1040 | sp->sk_state, | |
1041 | sk_wmem_alloc_get(sp), | |
6c206b20 | 1042 | rqueue, |
17ef66af LC |
1043 | 0, 0L, 0, |
1044 | from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)), | |
1045 | 0, | |
1046 | sock_i_ino(sp), | |
41c6d650 | 1047 | refcount_read(&sp->sk_refcnt), sp, |
17ef66af LC |
1048 | atomic_read(&sp->sk_drops)); |
1049 | } |