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