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