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