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