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