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