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