Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * RAW sockets for IPv6 | |
1ab1457c | 4 | * Linux INET6 implementation |
1da177e4 LT |
5 | * |
6 | * Authors: | |
1ab1457c | 7 | * Pedro Roque <roque@di.fc.ul.pt> |
1da177e4 LT |
8 | * |
9 | * Adapted from linux/net/ipv4/raw.c | |
10 | * | |
1da177e4 LT |
11 | * Fixes: |
12 | * Hideaki YOSHIFUJI : sin6_scope_id support | |
1ab1457c | 13 | * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance) |
1da177e4 | 14 | * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data |
1da177e4 LT |
15 | */ |
16 | ||
17 | #include <linux/errno.h> | |
18 | #include <linux/types.h> | |
19 | #include <linux/socket.h> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
1da177e4 | 21 | #include <linux/sockios.h> |
1da177e4 LT |
22 | #include <linux/net.h> |
23 | #include <linux/in6.h> | |
24 | #include <linux/netdevice.h> | |
25 | #include <linux/if_arp.h> | |
26 | #include <linux/icmpv6.h> | |
27 | #include <linux/netfilter.h> | |
28 | #include <linux/netfilter_ipv6.h> | |
3305b80c | 29 | #include <linux/skbuff.h> |
e2d57766 | 30 | #include <linux/compat.h> |
29778bec | 31 | #include <linux/uaccess.h> |
1da177e4 LT |
32 | #include <asm/ioctls.h> |
33 | ||
457c4cbc | 34 | #include <net/net_namespace.h> |
1da177e4 LT |
35 | #include <net/ip.h> |
36 | #include <net/sock.h> | |
37 | #include <net/snmp.h> | |
38 | ||
39 | #include <net/ipv6.h> | |
40 | #include <net/ndisc.h> | |
41 | #include <net/protocol.h> | |
42 | #include <net/ip6_route.h> | |
43 | #include <net/ip6_checksum.h> | |
44 | #include <net/addrconf.h> | |
45 | #include <net/transp_v6.h> | |
46 | #include <net/udp.h> | |
47 | #include <net/inet_common.h> | |
c752f073 | 48 | #include <net/tcp_states.h> |
07a93626 | 49 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
7be96f76 MN |
50 | #include <net/mip6.h> |
51 | #endif | |
7bc570c8 | 52 | #include <linux/mroute6.h> |
1da177e4 | 53 | |
b673e4df | 54 | #include <net/raw.h> |
1da177e4 LT |
55 | #include <net/rawv6.h> |
56 | #include <net/xfrm.h> | |
57 | ||
58 | #include <linux/proc_fs.h> | |
59 | #include <linux/seq_file.h> | |
bc3b2d7f | 60 | #include <linux/export.h> |
1da177e4 | 61 | |
d1c53c8e WA |
62 | #define ICMPV6_HDRLEN 4 /* ICMPv6 header, RFC 4443 Section 2.1 */ |
63 | ||
0daf07e5 | 64 | struct raw_hashinfo raw_v6_hashinfo; |
432490f9 | 65 | EXPORT_SYMBOL_GPL(raw_v6_hashinfo); |
1da177e4 | 66 | |
db6af4fd | 67 | bool raw_v6_match(struct net *net, const struct sock *sk, unsigned short num, |
ba44f818 ED |
68 | const struct in6_addr *loc_addr, |
69 | const struct in6_addr *rmt_addr, int dif, int sdif) | |
1da177e4 | 70 | { |
ba44f818 ED |
71 | if (inet_sk(sk)->inet_num != num || |
72 | !net_eq(sock_net(sk), net) || | |
73 | (!ipv6_addr_any(&sk->sk_v6_daddr) && | |
74 | !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || | |
75 | !raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, | |
76 | dif, sdif)) | |
77 | return false; | |
78 | ||
79 | if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) || | |
80 | ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr) || | |
81 | (ipv6_addr_is_multicast(loc_addr) && | |
82 | inet6_mc_check(sk, loc_addr, rmt_addr))) | |
83 | return true; | |
84 | ||
85 | return false; | |
1da177e4 | 86 | } |
ba44f818 | 87 | EXPORT_SYMBOL_GPL(raw_v6_match); |
1da177e4 LT |
88 | |
89 | /* | |
90 | * 0 - deliver | |
91 | * 1 - block | |
92 | */ | |
1b05c4b5 | 93 | static int icmpv6_filter(const struct sock *sk, const struct sk_buff *skb) |
1da177e4 | 94 | { |
9cc08af3 | 95 | struct icmp6hdr _hdr; |
1b05c4b5 | 96 | const struct icmp6hdr *hdr; |
1da177e4 | 97 | |
d1c53c8e WA |
98 | /* We require only the four bytes of the ICMPv6 header, not any |
99 | * additional bytes of message body in "struct icmp6hdr". | |
100 | */ | |
1b05c4b5 | 101 | hdr = skb_header_pointer(skb, skb_transport_offset(skb), |
d1c53c8e | 102 | ICMPV6_HDRLEN, &_hdr); |
1b05c4b5 ED |
103 | if (hdr) { |
104 | const __u32 *data = &raw6_sk(sk)->filter.data[0]; | |
105 | unsigned int type = hdr->icmp6_type; | |
1da177e4 | 106 | |
1b05c4b5 | 107 | return (data[type >> 5] & (1U << (type & 31))) != 0; |
1da177e4 | 108 | } |
1b05c4b5 | 109 | return 1; |
1da177e4 LT |
110 | } |
111 | ||
07a93626 | 112 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
f2eda47d | 113 | typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb); |
59fbb3a6 | 114 | |
f2eda47d ED |
115 | static mh_filter_t __rcu *mh_filter __read_mostly; |
116 | ||
117 | int rawv6_mh_filter_register(mh_filter_t filter) | |
59fbb3a6 | 118 | { |
cf778b00 | 119 | rcu_assign_pointer(mh_filter, filter); |
59fbb3a6 MN |
120 | return 0; |
121 | } | |
122 | EXPORT_SYMBOL(rawv6_mh_filter_register); | |
123 | ||
f2eda47d | 124 | int rawv6_mh_filter_unregister(mh_filter_t filter) |
59fbb3a6 | 125 | { |
a9b3cd7f | 126 | RCU_INIT_POINTER(mh_filter, NULL); |
59fbb3a6 MN |
127 | synchronize_rcu(); |
128 | return 0; | |
129 | } | |
130 | EXPORT_SYMBOL(rawv6_mh_filter_unregister); | |
131 | ||
132 | #endif | |
133 | ||
1da177e4 LT |
134 | /* |
135 | * demultiplex raw sockets. | |
136 | * (should consider queueing the skb in the sock receive_queue | |
137 | * without calling rawv6.c) | |
138 | * | |
139 | * Caller owns SKB so we must make clones. | |
140 | */ | |
a50feda5 | 141 | static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr) |
1da177e4 | 142 | { |
ba44f818 | 143 | struct net *net = dev_net(skb->dev); |
b71d1d42 ED |
144 | const struct in6_addr *saddr; |
145 | const struct in6_addr *daddr; | |
0a78cf72 | 146 | struct hlist_head *hlist; |
1da177e4 | 147 | struct sock *sk; |
a50feda5 | 148 | bool delivered = false; |
1da177e4 LT |
149 | __u8 hash; |
150 | ||
0660e03f | 151 | saddr = &ipv6_hdr(skb)->saddr; |
1da177e4 LT |
152 | daddr = saddr + 1; |
153 | ||
6579f5ba | 154 | hash = raw_hashfunc(net, nexthdr); |
0daf07e5 ED |
155 | hlist = &raw_v6_hashinfo.ht[hash]; |
156 | rcu_read_lock(); | |
0a78cf72 | 157 | sk_for_each_rcu(sk, hlist) { |
7be96f76 MN |
158 | int filtered; |
159 | ||
ba44f818 ED |
160 | if (!raw_v6_match(net, sk, nexthdr, daddr, saddr, |
161 | inet6_iif(skb), inet6_sdif(skb))) | |
162 | continue; | |
026763ec ED |
163 | |
164 | if (atomic_read(&sk->sk_rmem_alloc) >= | |
165 | READ_ONCE(sk->sk_rcvbuf)) { | |
166 | atomic_inc(&sk->sk_drops); | |
167 | continue; | |
168 | } | |
169 | ||
a50feda5 | 170 | delivered = true; |
7be96f76 MN |
171 | switch (nexthdr) { |
172 | case IPPROTO_ICMPV6: | |
173 | filtered = icmpv6_filter(sk, skb); | |
174 | break; | |
59fbb3a6 | 175 | |
07a93626 | 176 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
7be96f76 | 177 | case IPPROTO_MH: |
59fbb3a6 | 178 | { |
7be96f76 MN |
179 | /* XXX: To validate MH only once for each packet, |
180 | * this is placed here. It should be after checking | |
181 | * xfrm policy, however it doesn't. The checking xfrm | |
182 | * policy is placed in rawv6_rcv() because it is | |
183 | * required for each socket. | |
184 | */ | |
f2eda47d | 185 | mh_filter_t *filter; |
59fbb3a6 MN |
186 | |
187 | filter = rcu_dereference(mh_filter); | |
f2eda47d | 188 | filtered = filter ? (*filter)(sk, skb) : 0; |
7be96f76 | 189 | break; |
59fbb3a6 | 190 | } |
7be96f76 MN |
191 | #endif |
192 | default: | |
193 | filtered = 0; | |
194 | break; | |
195 | } | |
196 | ||
197 | if (filtered < 0) | |
198 | break; | |
199 | if (filtered == 0) { | |
1da177e4 LT |
200 | struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); |
201 | ||
202 | /* Not releasing hash table! */ | |
b0e214d2 | 203 | if (clone) |
1da177e4 LT |
204 | rawv6_rcv(sk, clone); |
205 | } | |
1da177e4 | 206 | } |
0daf07e5 | 207 | rcu_read_unlock(); |
d13964f4 | 208 | return delivered; |
1da177e4 LT |
209 | } |
210 | ||
a50feda5 | 211 | bool raw6_local_deliver(struct sk_buff *skb, int nexthdr) |
69d6da0b | 212 | { |
ba44f818 | 213 | return ipv6_raw_deliver(skb, nexthdr); |
69d6da0b PE |
214 | } |
215 | ||
1da177e4 LT |
216 | /* This cleans up af_inet6 a bit. -DaveM */ |
217 | static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) | |
218 | { | |
219 | struct inet_sock *inet = inet_sk(sk); | |
220 | struct ipv6_pinfo *np = inet6_sk(sk); | |
221 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr; | |
e69a4adc | 222 | __be32 v4addr = 0; |
1da177e4 LT |
223 | int addr_type; |
224 | int err; | |
225 | ||
226 | if (addr_len < SIN6_LEN_RFC2133) | |
227 | return -EINVAL; | |
82b276cd HFS |
228 | |
229 | if (addr->sin6_family != AF_INET6) | |
230 | return -EINVAL; | |
231 | ||
1da177e4 LT |
232 | addr_type = ipv6_addr_type(&addr->sin6_addr); |
233 | ||
234 | /* Raw sockets are IPv6 only */ | |
235 | if (addr_type == IPV6_ADDR_MAPPED) | |
fd5c0027 | 236 | return -EADDRNOTAVAIL; |
1da177e4 LT |
237 | |
238 | lock_sock(sk); | |
239 | ||
240 | err = -EINVAL; | |
241 | if (sk->sk_state != TCP_CLOSE) | |
242 | goto out; | |
243 | ||
fd5c0027 | 244 | rcu_read_lock(); |
1da177e4 LT |
245 | /* Check if the address belongs to the host. */ |
246 | if (addr_type != IPV6_ADDR_ANY) { | |
247 | struct net_device *dev = NULL; | |
248 | ||
842df073 | 249 | if (__ipv6_addr_needs_scope_id(addr_type)) { |
1da177e4 LT |
250 | if (addr_len >= sizeof(struct sockaddr_in6) && |
251 | addr->sin6_scope_id) { | |
252 | /* Override any existing binding, if another | |
253 | * one is supplied by user. | |
254 | */ | |
255 | sk->sk_bound_dev_if = addr->sin6_scope_id; | |
256 | } | |
1ab1457c | 257 | |
1da177e4 LT |
258 | /* Binding to link-local address requires an interface */ |
259 | if (!sk->sk_bound_dev_if) | |
fd5c0027 | 260 | goto out_unlock; |
72f7cfab | 261 | } |
1da177e4 | 262 | |
72f7cfab | 263 | if (sk->sk_bound_dev_if) { |
fd5c0027 ED |
264 | err = -ENODEV; |
265 | dev = dev_get_by_index_rcu(sock_net(sk), | |
266 | sk->sk_bound_dev_if); | |
267 | if (!dev) | |
268 | goto out_unlock; | |
1da177e4 | 269 | } |
1ab1457c | 270 | |
1da177e4 LT |
271 | /* ipv4 addr of the socket is invalid. Only the |
272 | * unspecified and mapped address have a v4 equivalent. | |
273 | */ | |
274 | v4addr = LOOPBACK4_IPV6; | |
35a256fe | 275 | if (!(addr_type & IPV6_ADDR_MULTICAST) && |
630e4576 | 276 | !ipv6_can_nonlocal_bind(sock_net(sk), inet)) { |
1da177e4 | 277 | err = -EADDRNOTAVAIL; |
3b1e0a65 | 278 | if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr, |
bfeade08 | 279 | dev, 0)) { |
fd5c0027 | 280 | goto out_unlock; |
1da177e4 LT |
281 | } |
282 | } | |
1da177e4 LT |
283 | } |
284 | ||
c720c7e8 | 285 | inet->inet_rcv_saddr = inet->inet_saddr = v4addr; |
efe4208f | 286 | sk->sk_v6_rcv_saddr = addr->sin6_addr; |
1da177e4 | 287 | if (!(addr_type & IPV6_ADDR_MULTICAST)) |
4e3fd7a0 | 288 | np->saddr = addr->sin6_addr; |
1da177e4 | 289 | err = 0; |
fd5c0027 ED |
290 | out_unlock: |
291 | rcu_read_unlock(); | |
1da177e4 LT |
292 | out: |
293 | release_sock(sk); | |
294 | return err; | |
295 | } | |
296 | ||
69d6da0b | 297 | static void rawv6_err(struct sock *sk, struct sk_buff *skb, |
d75fe63a | 298 | u8 type, u8 code, int offset, __be32 info) |
1da177e4 | 299 | { |
3fa29971 | 300 | bool recverr = inet6_test_bit(RECVERR6, sk); |
1da177e4 LT |
301 | struct ipv6_pinfo *np = inet6_sk(sk); |
302 | int err; | |
303 | int harderr; | |
304 | ||
305 | /* Report error on raw socket, if: | |
306 | 1. User requested recverr. | |
307 | 2. Socket is connected (otherwise the error indication | |
308 | is useless without recverr and error is hard. | |
309 | */ | |
3fa29971 | 310 | if (!recverr && sk->sk_state != TCP_ESTABLISHED) |
1da177e4 LT |
311 | return; |
312 | ||
313 | harderr = icmpv6_err_convert(type, code, &err); | |
81aded24 DM |
314 | if (type == ICMPV6_PKT_TOOBIG) { |
315 | ip6_sk_update_pmtu(skb, sk, info); | |
6b724bc4 | 316 | harderr = (READ_ONCE(np->pmtudisc) == IPV6_PMTUDISC_DO); |
81aded24 | 317 | } |
8d65b119 | 318 | if (type == NDISC_REDIRECT) { |
ec18d9a2 | 319 | ip6_sk_redirect(skb, sk); |
8d65b119 DJ |
320 | return; |
321 | } | |
3fa29971 | 322 | if (recverr) { |
1da177e4 | 323 | u8 *payload = skb->data; |
cafbe182 | 324 | if (!inet_test_bit(HDRINCL, sk)) |
1da177e4 LT |
325 | payload += offset; |
326 | ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload); | |
327 | } | |
328 | ||
3fa29971 | 329 | if (recverr || harderr) { |
1da177e4 | 330 | sk->sk_err = err; |
e3ae2365 | 331 | sk_error_report(sk); |
1da177e4 LT |
332 | } |
333 | } | |
334 | ||
69d6da0b | 335 | void raw6_icmp_error(struct sk_buff *skb, int nexthdr, |
d5fdd6ba | 336 | u8 type, u8 code, int inner_offset, __be32 info) |
69d6da0b | 337 | { |
ba44f818 | 338 | struct net *net = dev_net(skb->dev); |
0a78cf72 | 339 | struct hlist_head *hlist; |
69d6da0b PE |
340 | struct sock *sk; |
341 | int hash; | |
69d6da0b | 342 | |
6579f5ba | 343 | hash = raw_hashfunc(net, nexthdr); |
0daf07e5 ED |
344 | hlist = &raw_v6_hashinfo.ht[hash]; |
345 | rcu_read_lock(); | |
0a78cf72 | 346 | sk_for_each_rcu(sk, hlist) { |
05f175cd | 347 | /* Note: ipv6_hdr(skb) != skb->data */ |
b71d1d42 | 348 | const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data; |
69d6da0b | 349 | |
ba44f818 ED |
350 | if (!raw_v6_match(net, sk, nexthdr, &ip6h->saddr, &ip6h->daddr, |
351 | inet6_iif(skb), inet6_iif(skb))) | |
352 | continue; | |
d75fe63a | 353 | rawv6_err(sk, skb, type, code, inner_offset, info); |
69d6da0b | 354 | } |
0daf07e5 | 355 | rcu_read_unlock(); |
69d6da0b PE |
356 | } |
357 | ||
33d480ce | 358 | static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb) |
1da177e4 | 359 | { |
8d8ebd77 ED |
360 | enum skb_drop_reason reason; |
361 | ||
33d480ce | 362 | if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) && |
fb286bb2 | 363 | skb_checksum_complete(skb)) { |
a92aa318 | 364 | atomic_inc(&sk->sk_drops); |
ce9a2424 | 365 | sk_skb_reason_drop(sk, skb, SKB_DROP_REASON_SKB_CSUM); |
3cc76caa | 366 | return NET_RX_DROP; |
1da177e4 LT |
367 | } |
368 | ||
369 | /* Charge it to the socket. */ | |
d826eb14 | 370 | skb_dst_drop(skb); |
8d8ebd77 | 371 | if (sock_queue_rcv_skb_reason(sk, skb, &reason) < 0) { |
ce9a2424 | 372 | sk_skb_reason_drop(sk, skb, reason); |
3cc76caa | 373 | return NET_RX_DROP; |
1da177e4 LT |
374 | } |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
379 | /* | |
1ab1457c | 380 | * This is next to useless... |
1da177e4 | 381 | * if we demultiplex in network layer we don't need the extra call |
1ab1457c YH |
382 | * just to queue the skb... |
383 | * maybe we could have the network decide upon a hint if it | |
1da177e4 LT |
384 | * should call raw_rcv for demultiplexing |
385 | */ | |
386 | int rawv6_rcv(struct sock *sk, struct sk_buff *skb) | |
387 | { | |
388 | struct inet_sock *inet = inet_sk(sk); | |
389 | struct raw6_sock *rp = raw6_sk(sk); | |
390 | ||
1ab1457c | 391 | if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { |
a92aa318 | 392 | atomic_inc(&sk->sk_drops); |
ce9a2424 | 393 | sk_skb_reason_drop(sk, skb, SKB_DROP_REASON_XFRM_POLICY); |
1ab1457c YH |
394 | return NET_RX_DROP; |
395 | } | |
b0e214d2 | 396 | nf_reset_ct(skb); |
1da177e4 LT |
397 | |
398 | if (!rp->checksum) | |
399 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
400 | ||
84fa7933 | 401 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
d56f90a7 | 402 | skb_postpull_rcsum(skb, skb_network_header(skb), |
cfe1fc77 | 403 | skb_network_header_len(skb)); |
0660e03f ACM |
404 | if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
405 | &ipv6_hdr(skb)->daddr, | |
c720c7e8 | 406 | skb->len, inet->inet_num, skb->csum)) |
1da177e4 | 407 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
1da177e4 | 408 | } |
60476372 | 409 | if (!skb_csum_unnecessary(skb)) |
0660e03f ACM |
410 | skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
411 | &ipv6_hdr(skb)->daddr, | |
412 | skb->len, | |
c720c7e8 | 413 | inet->inet_num, 0)); |
1da177e4 | 414 | |
cafbe182 | 415 | if (inet_test_bit(HDRINCL, sk)) { |
fb286bb2 | 416 | if (skb_checksum_complete(skb)) { |
a92aa318 | 417 | atomic_inc(&sk->sk_drops); |
ce9a2424 | 418 | sk_skb_reason_drop(sk, skb, SKB_DROP_REASON_SKB_CSUM); |
3cc76caa | 419 | return NET_RX_DROP; |
1da177e4 | 420 | } |
1da177e4 LT |
421 | } |
422 | ||
423 | rawv6_rcv_skb(sk, skb); | |
424 | return 0; | |
425 | } | |
426 | ||
427 | ||
428 | /* | |
429 | * This should be easy, if there is something there | |
430 | * we return it, otherwise we block. | |
431 | */ | |
432 | ||
1b784140 | 433 | static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
ec095263 | 434 | int flags, int *addr_len) |
1da177e4 LT |
435 | { |
436 | struct ipv6_pinfo *np = inet6_sk(sk); | |
342dfc30 | 437 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); |
1da177e4 LT |
438 | struct sk_buff *skb; |
439 | size_t copied; | |
440 | int err; | |
441 | ||
442 | if (flags & MSG_OOB) | |
443 | return -EOPNOTSUPP; | |
1ab1457c | 444 | |
1da177e4 | 445 | if (flags & MSG_ERRQUEUE) |
85fbaa75 | 446 | return ipv6_recv_error(sk, msg, len, addr_len); |
1da177e4 | 447 | |
4b340ae2 | 448 | if (np->rxpmtu && np->rxopt.bits.rxpmtu) |
85fbaa75 | 449 | return ipv6_recv_rxpmtu(sk, msg, len, addr_len); |
4b340ae2 | 450 | |
f4b41f06 | 451 | skb = skb_recv_datagram(sk, flags, &err); |
1da177e4 LT |
452 | if (!skb) |
453 | goto out; | |
454 | ||
455 | copied = skb->len; | |
1ab1457c YH |
456 | if (copied > len) { |
457 | copied = len; | |
458 | msg->msg_flags |= MSG_TRUNC; | |
459 | } | |
1da177e4 | 460 | |
60476372 | 461 | if (skb_csum_unnecessary(skb)) { |
51f3d02b | 462 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
1da177e4 | 463 | } else if (msg->msg_flags&MSG_TRUNC) { |
fb286bb2 | 464 | if (__skb_checksum_complete(skb)) |
1da177e4 | 465 | goto csum_copy_err; |
51f3d02b | 466 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
1da177e4 | 467 | } else { |
227158db | 468 | err = skb_copy_and_csum_datagram_msg(skb, 0, msg); |
1da177e4 LT |
469 | if (err == -EINVAL) |
470 | goto csum_copy_err; | |
471 | } | |
472 | if (err) | |
473 | goto out_free; | |
474 | ||
475 | /* Copy the address. */ | |
476 | if (sin6) { | |
477 | sin6->sin6_family = AF_INET6; | |
f59fc7f3 | 478 | sin6->sin6_port = 0; |
4e3fd7a0 | 479 | sin6->sin6_addr = ipv6_hdr(skb)->saddr; |
1da177e4 | 480 | sin6->sin6_flowinfo = 0; |
842df073 | 481 | sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr, |
4330487a | 482 | inet6_iif(skb)); |
bceaa902 | 483 | *addr_len = sizeof(*sin6); |
1da177e4 LT |
484 | } |
485 | ||
6fd1d51c | 486 | sock_recv_cmsgs(msg, sk, skb); |
1da177e4 LT |
487 | |
488 | if (np->rxopt.all) | |
73df66f8 | 489 | ip6_datagram_recv_ctl(sk, msg, skb); |
1da177e4 LT |
490 | |
491 | err = copied; | |
492 | if (flags & MSG_TRUNC) | |
493 | err = skb->len; | |
494 | ||
495 | out_free: | |
496 | skb_free_datagram(sk, skb); | |
497 | out: | |
498 | return err; | |
499 | ||
500 | csum_copy_err: | |
3305b80c | 501 | skb_kill_datagram(sk, skb, flags); |
1da177e4 LT |
502 | |
503 | /* Error for blocking case is chosen to masquerade | |
504 | as some normal condition. | |
505 | */ | |
506 | err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH; | |
3305b80c | 507 | goto out; |
1da177e4 LT |
508 | } |
509 | ||
4c9483b2 | 510 | static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, |
357b40a1 | 511 | struct raw6_sock *rp) |
1da177e4 | 512 | { |
cb3e9864 | 513 | struct ipv6_txoptions *opt; |
1da177e4 LT |
514 | struct sk_buff *skb; |
515 | int err = 0; | |
357b40a1 HX |
516 | int offset; |
517 | int len; | |
679a8738 | 518 | int total_len; |
868c86bc AV |
519 | __wsum tmp_csum; |
520 | __sum16 csum; | |
1da177e4 LT |
521 | |
522 | if (!rp->checksum) | |
523 | goto send; | |
524 | ||
43728fa5 FF |
525 | skb = skb_peek(&sk->sk_write_queue); |
526 | if (!skb) | |
1da177e4 LT |
527 | goto out; |
528 | ||
357b40a1 | 529 | offset = rp->offset; |
299b0767 | 530 | total_len = inet_sk(sk)->cork.base.length; |
cb3e9864 HX |
531 | opt = inet6_sk(sk)->cork.opt; |
532 | total_len -= opt ? opt->opt_flen : 0; | |
533 | ||
679a8738 | 534 | if (offset >= total_len - 1) { |
1da177e4 | 535 | err = -EINVAL; |
357b40a1 | 536 | ip6_flush_pending_frames(sk); |
1da177e4 LT |
537 | goto out; |
538 | } | |
539 | ||
540 | /* should be check HW csum miyazawa */ | |
541 | if (skb_queue_len(&sk->sk_write_queue) == 1) { | |
542 | /* | |
543 | * Only one fragment on the socket. | |
544 | */ | |
545 | tmp_csum = skb->csum; | |
546 | } else { | |
357b40a1 | 547 | struct sk_buff *csum_skb = NULL; |
1da177e4 LT |
548 | tmp_csum = 0; |
549 | ||
550 | skb_queue_walk(&sk->sk_write_queue, skb) { | |
551 | tmp_csum = csum_add(tmp_csum, skb->csum); | |
357b40a1 HX |
552 | |
553 | if (csum_skb) | |
554 | continue; | |
555 | ||
ea2ae17d | 556 | len = skb->len - skb_transport_offset(skb); |
357b40a1 HX |
557 | if (offset >= len) { |
558 | offset -= len; | |
559 | continue; | |
560 | } | |
561 | ||
562 | csum_skb = skb; | |
1da177e4 | 563 | } |
357b40a1 HX |
564 | |
565 | skb = csum_skb; | |
1da177e4 LT |
566 | } |
567 | ||
ea2ae17d | 568 | offset += skb_transport_offset(skb); |
a98f9175 DJ |
569 | err = skb_copy_bits(skb, offset, &csum, 2); |
570 | if (err < 0) { | |
571 | ip6_flush_pending_frames(sk); | |
572 | goto out; | |
573 | } | |
357b40a1 | 574 | |
1da177e4 | 575 | /* in case cksum was not initialized */ |
357b40a1 | 576 | if (unlikely(csum)) |
5f92a738 | 577 | tmp_csum = csum_sub(tmp_csum, csum_unfold(csum)); |
357b40a1 | 578 | |
4c9483b2 DM |
579 | csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, |
580 | total_len, fl6->flowi6_proto, tmp_csum); | |
357b40a1 | 581 | |
4c9483b2 | 582 | if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP) |
f6ab0288 | 583 | csum = CSUM_MANGLED_0; |
1da177e4 | 584 | |
8242fc33 | 585 | BUG_ON(skb_store_bits(skb, offset, &csum, 2)); |
1da177e4 | 586 | |
1da177e4 LT |
587 | send: |
588 | err = ip6_push_pending_frames(sk); | |
589 | out: | |
590 | return err; | |
591 | } | |
592 | ||
c3c1a7db | 593 | static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length, |
4c9483b2 | 594 | struct flowi6 *fl6, struct dst_entry **dstp, |
a818f75e | 595 | unsigned int flags, const struct sockcm_cookie *sockc) |
1da177e4 | 596 | { |
adb28c9d | 597 | struct net *net = sock_net(sk); |
1da177e4 LT |
598 | struct ipv6hdr *iph; |
599 | struct sk_buff *skb; | |
1da177e4 | 600 | int err; |
e8dfd42c | 601 | struct rt6_info *rt = dst_rt6_info(*dstp); |
a7ae1992 HX |
602 | int hlen = LL_RESERVED_SPACE(rt->dst.dev); |
603 | int tlen = rt->dst.dev->needed_tailroom; | |
1da177e4 | 604 | |
d8d1f30b | 605 | if (length > rt->dst.dev->mtu) { |
4c9483b2 | 606 | ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu); |
1da177e4 LT |
607 | return -EMSGSIZE; |
608 | } | |
86f4c90a AP |
609 | if (length < sizeof(struct ipv6hdr)) |
610 | return -EINVAL; | |
1da177e4 LT |
611 | if (flags&MSG_PROBE) |
612 | goto out; | |
613 | ||
f5184d26 | 614 | skb = sock_alloc_send_skb(sk, |
a7ae1992 | 615 | length + hlen + tlen + 15, |
f5184d26 | 616 | flags & MSG_DONTWAIT, &err); |
63159f29 | 617 | if (!skb) |
1ab1457c | 618 | goto error; |
a7ae1992 | 619 | skb_reserve(skb, hlen); |
1da177e4 | 620 | |
9c9c9ad5 | 621 | skb->protocol = htons(ETH_P_IPV6); |
a32f3e9d | 622 | skb->priority = sockc->priority; |
c6af0c22 | 623 | skb->mark = sockc->mark; |
1693c5db | 624 | skb_set_delivery_type_by_clockid(skb, sockc->transmit_time, sk->sk_clockid); |
35c3e279 | 625 | |
1ced98e8 ACM |
626 | skb_put(skb, length); |
627 | skb_reset_network_header(skb); | |
0660e03f | 628 | iph = ipv6_hdr(skb); |
1da177e4 LT |
629 | |
630 | skb->ip_summed = CHECKSUM_NONE; | |
631 | ||
822b5bc6 | 632 | skb_setup_tx_timestamp(skb, sockc); |
fbfb2321 | 633 | |
0dec879f JA |
634 | if (flags & MSG_CONFIRM) |
635 | skb_set_dst_pending_confirm(skb, 1); | |
636 | ||
b0e380b1 | 637 | skb->transport_header = skb->network_header; |
21226abb | 638 | err = memcpy_from_msg(iph, msg, length); |
a688caa3 WW |
639 | if (err) { |
640 | err = -EFAULT; | |
641 | kfree_skb(skb); | |
642 | goto error; | |
643 | } | |
644 | ||
645 | skb_dst_set(skb, &rt->dst); | |
646 | *dstp = NULL; | |
1da177e4 | 647 | |
a8e3e1a9 DA |
648 | /* if egress device is enslaved to an L3 master device pass the |
649 | * skb to its handler for processing | |
650 | */ | |
651 | skb = l3mdev_ip6_out(sk, skb); | |
652 | if (unlikely(!skb)) | |
653 | return 0; | |
654 | ||
a688caa3 WW |
655 | /* Acquire rcu_read_lock() in case we need to use rt->rt6i_idev |
656 | * in the error path. Since skb has been freed, the dst could | |
657 | * have been queued for deletion. | |
658 | */ | |
659 | rcu_read_lock(); | |
b4a11b20 | 660 | IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS); |
29a26a56 | 661 | err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb, |
13206b6b | 662 | NULL, rt->dst.dev, dst_output); |
1da177e4 | 663 | if (err > 0) |
6ce9e7b5 | 664 | err = net_xmit_errno(err); |
a688caa3 WW |
665 | if (err) { |
666 | IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); | |
667 | rcu_read_unlock(); | |
668 | goto error_check; | |
669 | } | |
670 | rcu_read_unlock(); | |
1da177e4 LT |
671 | out: |
672 | return 0; | |
673 | ||
1da177e4 | 674 | error: |
adb28c9d | 675 | IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); |
a688caa3 | 676 | error_check: |
3fa29971 | 677 | if (err == -ENOBUFS && !inet6_test_bit(RECVERR6, sk)) |
6ce9e7b5 | 678 | err = 0; |
1ab1457c | 679 | return err; |
1da177e4 LT |
680 | } |
681 | ||
19e3c66b AV |
682 | struct raw6_frag_vec { |
683 | struct msghdr *msg; | |
684 | int hlen; | |
685 | char c[4]; | |
686 | }; | |
687 | ||
688 | static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6) | |
1da177e4 | 689 | { |
19e3c66b AV |
690 | int err = 0; |
691 | switch (fl6->flowi6_proto) { | |
692 | case IPPROTO_ICMPV6: | |
693 | rfv->hlen = 2; | |
694 | err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); | |
695 | if (!err) { | |
696 | fl6->fl6_icmp_type = rfv->c[0]; | |
697 | fl6->fl6_icmp_code = rfv->c[1]; | |
698 | } | |
699 | break; | |
700 | case IPPROTO_MH: | |
701 | rfv->hlen = 4; | |
702 | err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); | |
703 | if (!err) | |
704 | fl6->fl6_mh_type = rfv->c[2]; | |
705 | } | |
706 | return err; | |
707 | } | |
1da177e4 | 708 | |
19e3c66b AV |
709 | static int raw6_getfrag(void *from, char *to, int offset, int len, int odd, |
710 | struct sk_buff *skb) | |
711 | { | |
712 | struct raw6_frag_vec *rfv = from; | |
713 | ||
714 | if (offset < rfv->hlen) { | |
715 | int copy = min(rfv->hlen - offset, len); | |
716 | ||
717 | if (skb->ip_summed == CHECKSUM_PARTIAL) | |
718 | memcpy(to, rfv->c + offset, copy); | |
719 | else | |
720 | skb->csum = csum_block_add( | |
721 | skb->csum, | |
722 | csum_partial_copy_nocheck(rfv->c + offset, | |
cc44c17b | 723 | to, copy), |
19e3c66b AV |
724 | odd); |
725 | ||
726 | odd = 0; | |
727 | offset += copy; | |
728 | to += copy; | |
729 | len -= copy; | |
730 | ||
731 | if (!len) | |
732 | return 0; | |
733 | } | |
1da177e4 | 734 | |
19e3c66b | 735 | offset -= rfv->hlen; |
6e8f4d48 | 736 | |
f69e6d13 | 737 | return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); |
1da177e4 LT |
738 | } |
739 | ||
1b784140 | 740 | static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) |
1da177e4 | 741 | { |
45f6fad8 | 742 | struct ipv6_txoptions *opt_to_free = NULL; |
1da177e4 | 743 | struct ipv6_txoptions opt_space; |
342dfc30 | 744 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); |
20c59de2 | 745 | struct in6_addr *daddr, *final_p, final; |
1da177e4 LT |
746 | struct inet_sock *inet = inet_sk(sk); |
747 | struct ipv6_pinfo *np = inet6_sk(sk); | |
748 | struct raw6_sock *rp = raw6_sk(sk); | |
749 | struct ipv6_txoptions *opt = NULL; | |
750 | struct ip6_flowlabel *flowlabel = NULL; | |
751 | struct dst_entry *dst = NULL; | |
19e3c66b | 752 | struct raw6_frag_vec rfv; |
4c9483b2 | 753 | struct flowi6 fl6; |
26879da5 | 754 | struct ipcm6_cookie ipc6; |
1da177e4 | 755 | int addr_len = msg->msg_namelen; |
59e3e4b5 | 756 | int hdrincl; |
1da177e4 LT |
757 | u16 proto; |
758 | int err; | |
759 | ||
760 | /* Rough check on arithmetic overflow, | |
b59e139b | 761 | better check is made in ip6_append_data(). |
1da177e4 | 762 | */ |
b59e139b | 763 | if (len > INT_MAX) |
1da177e4 LT |
764 | return -EMSGSIZE; |
765 | ||
766 | /* Mirror BSD error message compatibility */ | |
1ab1457c | 767 | if (msg->msg_flags & MSG_OOB) |
1da177e4 LT |
768 | return -EOPNOTSUPP; |
769 | ||
cafbe182 | 770 | hdrincl = inet_test_bit(HDRINCL, sk); |
59e3e4b5 | 771 | |
5cd2f788 WB |
772 | ipcm6_init_sk(&ipc6, sk); |
773 | ||
1da177e4 | 774 | /* |
1ab1457c | 775 | * Get and verify the address. |
1da177e4 | 776 | */ |
4c9483b2 | 777 | memset(&fl6, 0, sizeof(fl6)); |
1da177e4 | 778 | |
5cd2f788 | 779 | fl6.flowi6_mark = ipc6.sockc.mark; |
e2d118a1 | 780 | fl6.flowi6_uid = sk->sk_uid; |
4a19ec58 | 781 | |
1da177e4 | 782 | if (sin6) { |
1ab1457c | 783 | if (addr_len < SIN6_LEN_RFC2133) |
1da177e4 LT |
784 | return -EINVAL; |
785 | ||
1ab1457c | 786 | if (sin6->sin6_family && sin6->sin6_family != AF_INET6) |
a02cec21 | 787 | return -EAFNOSUPPORT; |
1da177e4 LT |
788 | |
789 | /* port is the proto value [0..255] carried in nexthdr */ | |
790 | proto = ntohs(sin6->sin6_port); | |
791 | ||
792 | if (!proto) | |
c720c7e8 | 793 | proto = inet->inet_num; |
3632679d ND |
794 | else if (proto != inet->inet_num && |
795 | inet->inet_num != IPPROTO_RAW) | |
a02cec21 | 796 | return -EINVAL; |
1da177e4 LT |
797 | |
798 | if (proto > 255) | |
a02cec21 | 799 | return -EINVAL; |
1da177e4 LT |
800 | |
801 | daddr = &sin6->sin6_addr; | |
859f8b26 | 802 | if (inet6_test_bit(SNDFLOW, sk)) { |
4c9483b2 DM |
803 | fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; |
804 | if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { | |
805 | flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); | |
59c820b2 | 806 | if (IS_ERR(flowlabel)) |
1da177e4 | 807 | return -EINVAL; |
1da177e4 LT |
808 | } |
809 | } | |
810 | ||
811 | /* | |
812 | * Otherwise it will be difficult to maintain | |
813 | * sk->sk_dst_cache. | |
814 | */ | |
815 | if (sk->sk_state == TCP_ESTABLISHED && | |
efe4208f ED |
816 | ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) |
817 | daddr = &sk->sk_v6_daddr; | |
1da177e4 LT |
818 | |
819 | if (addr_len >= sizeof(struct sockaddr_in6) && | |
820 | sin6->sin6_scope_id && | |
842df073 | 821 | __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) |
4c9483b2 | 822 | fl6.flowi6_oif = sin6->sin6_scope_id; |
1da177e4 | 823 | } else { |
1ab1457c | 824 | if (sk->sk_state != TCP_ESTABLISHED) |
1da177e4 | 825 | return -EDESTADDRREQ; |
1ab1457c | 826 | |
c720c7e8 | 827 | proto = inet->inet_num; |
efe4208f | 828 | daddr = &sk->sk_v6_daddr; |
4c9483b2 | 829 | fl6.flowlabel = np->flow_label; |
1da177e4 LT |
830 | } |
831 | ||
4c9483b2 DM |
832 | if (fl6.flowi6_oif == 0) |
833 | fl6.flowi6_oif = sk->sk_bound_dev_if; | |
1da177e4 LT |
834 | |
835 | if (msg->msg_controllen) { | |
836 | opt = &opt_space; | |
837 | memset(opt, 0, sizeof(struct ipv6_txoptions)); | |
838 | opt->tot_len = sizeof(struct ipv6_txoptions); | |
26879da5 | 839 | ipc6.opt = opt; |
1da177e4 | 840 | |
5fdaa88d | 841 | err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6); |
1da177e4 LT |
842 | if (err < 0) { |
843 | fl6_sock_release(flowlabel); | |
844 | return err; | |
845 | } | |
4c9483b2 DM |
846 | if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { |
847 | flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); | |
59c820b2 | 848 | if (IS_ERR(flowlabel)) |
1da177e4 LT |
849 | return -EINVAL; |
850 | } | |
851 | if (!(opt->opt_nflen|opt->opt_flen)) | |
852 | opt = NULL; | |
853 | } | |
45f6fad8 ED |
854 | if (!opt) { |
855 | opt = txopt_get(np); | |
856 | opt_to_free = opt; | |
26879da5 | 857 | } |
df9890c3 YH |
858 | if (flowlabel) |
859 | opt = fl6_merge_options(&opt_space, flowlabel, opt); | |
860 | opt = ipv6_fixup_options(&opt_space, opt); | |
1da177e4 | 861 | |
4c9483b2 | 862 | fl6.flowi6_proto = proto; |
c6af0c22 | 863 | fl6.flowi6_mark = ipc6.sockc.mark; |
b9aa52c4 OM |
864 | |
865 | if (!hdrincl) { | |
866 | rfv.msg = msg; | |
867 | rfv.hlen = 0; | |
868 | err = rawv6_probe_proto_opt(&rfv, &fl6); | |
869 | if (err) | |
870 | goto out; | |
871 | } | |
1ab1457c | 872 | |
876c7f41 | 873 | if (!ipv6_addr_any(daddr)) |
4e3fd7a0 | 874 | fl6.daddr = *daddr; |
876c7f41 | 875 | else |
4c9483b2 DM |
876 | fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ |
877 | if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) | |
4e3fd7a0 | 878 | fl6.saddr = np->saddr; |
1da177e4 | 879 | |
4c9483b2 | 880 | final_p = fl6_update_dst(&fl6, opt, &final); |
1da177e4 | 881 | |
4c9483b2 | 882 | if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) |
d2f011a0 | 883 | fl6.flowi6_oif = READ_ONCE(np->mcast_oif); |
c4062dfc | 884 | else if (!fl6.flowi6_oif) |
1ac13efd | 885 | fl6.flowi6_oif = READ_ONCE(np->ucast_oif); |
3df98d79 | 886 | security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6)); |
1da177e4 | 887 | |
59e3e4b5 | 888 | if (hdrincl) |
48e8aa6e MKL |
889 | fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH; |
890 | ||
38b7097b HFS |
891 | fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); |
892 | ||
c4e85f73 | 893 | dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p); |
68d0c6d3 DM |
894 | if (IS_ERR(dst)) { |
895 | err = PTR_ERR(dst); | |
1da177e4 | 896 | goto out; |
14e50e57 | 897 | } |
26879da5 WW |
898 | if (ipc6.hlimit < 0) |
899 | ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); | |
1da177e4 LT |
900 | |
901 | if (msg->msg_flags&MSG_CONFIRM) | |
902 | goto do_confirm; | |
903 | ||
904 | back_from_confirm: | |
59e3e4b5 | 905 | if (hdrincl) |
a818f75e | 906 | err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst, |
5fdaa88d | 907 | msg->msg_flags, &ipc6.sockc); |
1789a640 | 908 | else { |
26879da5 | 909 | ipc6.opt = opt; |
1da177e4 | 910 | lock_sock(sk); |
19e3c66b | 911 | err = ip6_append_data(sk, raw6_getfrag, &rfv, |
e8dfd42c | 912 | len, 0, &ipc6, &fl6, dst_rt6_info(dst), |
5fdaa88d | 913 | msg->msg_flags); |
1da177e4 LT |
914 | |
915 | if (err) | |
916 | ip6_flush_pending_frames(sk); | |
917 | else if (!(msg->msg_flags & MSG_MORE)) | |
4c9483b2 | 918 | err = rawv6_push_pending_frames(sk, &fl6, rp); |
3ef9d943 | 919 | release_sock(sk); |
1da177e4 LT |
920 | } |
921 | done: | |
6d3e85ec | 922 | dst_release(dst); |
1ab1457c | 923 | out: |
1da177e4 | 924 | fl6_sock_release(flowlabel); |
45f6fad8 | 925 | txopt_put(opt_to_free); |
67ba4152 | 926 | return err < 0 ? err : len; |
1da177e4 | 927 | do_confirm: |
0dec879f JA |
928 | if (msg->msg_flags & MSG_PROBE) |
929 | dst_confirm_neigh(dst, &fl6.daddr); | |
1da177e4 LT |
930 | if (!(msg->msg_flags & MSG_PROBE) || len) |
931 | goto back_from_confirm; | |
932 | err = 0; | |
933 | goto done; | |
934 | } | |
935 | ||
2e26b6df | 936 | static int rawv6_seticmpfilter(struct sock *sk, int optname, |
a7b75c5a | 937 | sockptr_t optval, int optlen) |
1da177e4 LT |
938 | { |
939 | switch (optname) { | |
940 | case ICMPV6_FILTER: | |
941 | if (optlen > sizeof(struct icmp6_filter)) | |
942 | optlen = sizeof(struct icmp6_filter); | |
a7b75c5a | 943 | if (copy_from_sockptr(&raw6_sk(sk)->filter, optval, optlen)) |
1da177e4 LT |
944 | return -EFAULT; |
945 | return 0; | |
946 | default: | |
947 | return -ENOPROTOOPT; | |
3ff50b79 | 948 | } |
1da177e4 LT |
949 | |
950 | return 0; | |
951 | } | |
952 | ||
2e26b6df | 953 | static int rawv6_geticmpfilter(struct sock *sk, int optname, |
1da177e4 LT |
954 | char __user *optval, int __user *optlen) |
955 | { | |
956 | int len; | |
957 | ||
958 | switch (optname) { | |
959 | case ICMPV6_FILTER: | |
960 | if (get_user(len, optlen)) | |
961 | return -EFAULT; | |
962 | if (len < 0) | |
963 | return -EINVAL; | |
964 | if (len > sizeof(struct icmp6_filter)) | |
965 | len = sizeof(struct icmp6_filter); | |
966 | if (put_user(len, optlen)) | |
967 | return -EFAULT; | |
968 | if (copy_to_user(optval, &raw6_sk(sk)->filter, len)) | |
969 | return -EFAULT; | |
970 | return 0; | |
971 | default: | |
972 | return -ENOPROTOOPT; | |
3ff50b79 | 973 | } |
1da177e4 LT |
974 | |
975 | return 0; | |
976 | } | |
977 | ||
978 | ||
3fdadf7d | 979 | static int do_rawv6_setsockopt(struct sock *sk, int level, int optname, |
a7b75c5a | 980 | sockptr_t optval, unsigned int optlen) |
1da177e4 LT |
981 | { |
982 | struct raw6_sock *rp = raw6_sk(sk); | |
983 | int val; | |
984 | ||
fb7bc920 TD |
985 | if (optlen < sizeof(val)) |
986 | return -EINVAL; | |
987 | ||
a7b75c5a | 988 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
1da177e4 LT |
989 | return -EFAULT; |
990 | ||
991 | switch (optname) { | |
715f504b HFS |
992 | case IPV6_HDRINCL: |
993 | if (sk->sk_type != SOCK_RAW) | |
994 | return -EINVAL; | |
cafbe182 | 995 | inet_assign_bit(HDRINCL, sk, val); |
715f504b | 996 | return 0; |
207ec0ab JP |
997 | case IPV6_CHECKSUM: |
998 | if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 && | |
999 | level == IPPROTO_IPV6) { | |
1000 | /* | |
1001 | * RFC3542 tells that IPV6_CHECKSUM socket | |
1002 | * option in the IPPROTO_IPV6 level is not | |
1003 | * allowed on ICMPv6 sockets. | |
1004 | * If you want to set it, use IPPROTO_RAW | |
1005 | * level IPV6_CHECKSUM socket option | |
1006 | * (Linux extension). | |
1007 | */ | |
1008 | return -EINVAL; | |
1009 | } | |
1a98d05f | 1010 | |
207ec0ab JP |
1011 | /* You may get strange result with a positive odd offset; |
1012 | RFC2292bis agrees with me. */ | |
1013 | if (val > 0 && (val&1)) | |
1014 | return -EINVAL; | |
1015 | if (val < 0) { | |
1016 | rp->checksum = 0; | |
1017 | } else { | |
1018 | rp->checksum = 1; | |
1019 | rp->offset = val; | |
1020 | } | |
1da177e4 | 1021 | |
207ec0ab | 1022 | return 0; |
1da177e4 | 1023 | |
207ec0ab JP |
1024 | default: |
1025 | return -ENOPROTOOPT; | |
1da177e4 LT |
1026 | } |
1027 | } | |
1028 | ||
3fdadf7d | 1029 | static int rawv6_setsockopt(struct sock *sk, int level, int optname, |
a7b75c5a | 1030 | sockptr_t optval, unsigned int optlen) |
1da177e4 | 1031 | { |
207ec0ab JP |
1032 | switch (level) { |
1033 | case SOL_RAW: | |
1034 | break; | |
1da177e4 | 1035 | |
207ec0ab JP |
1036 | case SOL_ICMPV6: |
1037 | if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) | |
1038 | return -EOPNOTSUPP; | |
2e26b6df | 1039 | return rawv6_seticmpfilter(sk, optname, optval, optlen); |
207ec0ab | 1040 | case SOL_IPV6: |
715f504b HFS |
1041 | if (optname == IPV6_CHECKSUM || |
1042 | optname == IPV6_HDRINCL) | |
207ec0ab | 1043 | break; |
a8eceea8 | 1044 | fallthrough; |
207ec0ab JP |
1045 | default: |
1046 | return ipv6_setsockopt(sk, level, optname, optval, optlen); | |
3ff50b79 SH |
1047 | } |
1048 | ||
3fdadf7d DM |
1049 | return do_rawv6_setsockopt(sk, level, optname, optval, optlen); |
1050 | } | |
1051 | ||
3fdadf7d DM |
1052 | static int do_rawv6_getsockopt(struct sock *sk, int level, int optname, |
1053 | char __user *optval, int __user *optlen) | |
1054 | { | |
1055 | struct raw6_sock *rp = raw6_sk(sk); | |
1056 | int val, len; | |
1da177e4 | 1057 | |
67ba4152 | 1058 | if (get_user(len, optlen)) |
1da177e4 LT |
1059 | return -EFAULT; |
1060 | ||
1061 | switch (optname) { | |
715f504b | 1062 | case IPV6_HDRINCL: |
cafbe182 | 1063 | val = inet_test_bit(HDRINCL, sk); |
715f504b | 1064 | break; |
1da177e4 | 1065 | case IPV6_CHECKSUM: |
1a98d05f YH |
1066 | /* |
1067 | * We allow getsockopt() for IPPROTO_IPV6-level | |
1068 | * IPV6_CHECKSUM socket option on ICMPv6 sockets | |
1069 | * since RFC3542 is silent about it. | |
1070 | */ | |
1da177e4 LT |
1071 | if (rp->checksum == 0) |
1072 | val = -1; | |
1073 | else | |
1074 | val = rp->offset; | |
1075 | break; | |
1076 | ||
1077 | default: | |
1078 | return -ENOPROTOOPT; | |
1079 | } | |
1080 | ||
1081 | len = min_t(unsigned int, sizeof(int), len); | |
1082 | ||
1083 | if (put_user(len, optlen)) | |
1084 | return -EFAULT; | |
67ba4152 | 1085 | if (copy_to_user(optval, &val, len)) |
1da177e4 LT |
1086 | return -EFAULT; |
1087 | return 0; | |
1088 | } | |
1089 | ||
3fdadf7d DM |
1090 | static int rawv6_getsockopt(struct sock *sk, int level, int optname, |
1091 | char __user *optval, int __user *optlen) | |
1092 | { | |
207ec0ab JP |
1093 | switch (level) { |
1094 | case SOL_RAW: | |
1095 | break; | |
3fdadf7d | 1096 | |
207ec0ab JP |
1097 | case SOL_ICMPV6: |
1098 | if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) | |
1099 | return -EOPNOTSUPP; | |
2e26b6df | 1100 | return rawv6_geticmpfilter(sk, optname, optval, optlen); |
207ec0ab | 1101 | case SOL_IPV6: |
715f504b HFS |
1102 | if (optname == IPV6_CHECKSUM || |
1103 | optname == IPV6_HDRINCL) | |
207ec0ab | 1104 | break; |
a8eceea8 | 1105 | fallthrough; |
207ec0ab JP |
1106 | default: |
1107 | return ipv6_getsockopt(sk, level, optname, optval, optlen); | |
3ff50b79 SH |
1108 | } |
1109 | ||
3fdadf7d DM |
1110 | return do_rawv6_getsockopt(sk, level, optname, optval, optlen); |
1111 | } | |
1112 | ||
e1d001fa | 1113 | static int rawv6_ioctl(struct sock *sk, int cmd, int *karg) |
1da177e4 | 1114 | { |
207ec0ab JP |
1115 | switch (cmd) { |
1116 | case SIOCOUTQ: { | |
e1d001fa BL |
1117 | *karg = sk_wmem_alloc_get(sk); |
1118 | return 0; | |
207ec0ab JP |
1119 | } |
1120 | case SIOCINQ: { | |
1121 | struct sk_buff *skb; | |
207ec0ab JP |
1122 | |
1123 | spin_lock_bh(&sk->sk_receive_queue.lock); | |
1124 | skb = skb_peek(&sk->sk_receive_queue); | |
53b24b8f | 1125 | if (skb) |
e1d001fa BL |
1126 | *karg = skb->len; |
1127 | else | |
1128 | *karg = 0; | |
207ec0ab | 1129 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
e1d001fa | 1130 | return 0; |
207ec0ab | 1131 | } |
1da177e4 | 1132 | |
207ec0ab | 1133 | default: |
7bc570c8 | 1134 | #ifdef CONFIG_IPV6_MROUTE |
e1d001fa | 1135 | return ip6mr_ioctl(sk, cmd, karg); |
7bc570c8 | 1136 | #else |
207ec0ab | 1137 | return -ENOIOCTLCMD; |
7bc570c8 | 1138 | #endif |
1da177e4 LT |
1139 | } |
1140 | } | |
1141 | ||
e2d57766 DM |
1142 | #ifdef CONFIG_COMPAT |
1143 | static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg) | |
1144 | { | |
1145 | switch (cmd) { | |
1146 | case SIOCOUTQ: | |
1147 | case SIOCINQ: | |
1148 | return -ENOIOCTLCMD; | |
1149 | default: | |
1150 | #ifdef CONFIG_IPV6_MROUTE | |
1151 | return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg)); | |
1152 | #else | |
1153 | return -ENOIOCTLCMD; | |
1154 | #endif | |
1155 | } | |
1156 | } | |
1157 | #endif | |
1158 | ||
1da177e4 LT |
1159 | static void rawv6_close(struct sock *sk, long timeout) |
1160 | { | |
c720c7e8 | 1161 | if (inet_sk(sk)->inet_num == IPPROTO_RAW) |
725a8ff0 | 1162 | ip6_ra_control(sk, -1); |
7bc570c8 | 1163 | ip6mr_sk_done(sk); |
1da177e4 LT |
1164 | sk_common_release(sk); |
1165 | } | |
1166 | ||
7d06b2e0 | 1167 | static void raw6_destroy(struct sock *sk) |
22dd4850 DL |
1168 | { |
1169 | lock_sock(sk); | |
1170 | ip6_flush_pending_frames(sk); | |
1171 | release_sock(sk); | |
22dd4850 DL |
1172 | } |
1173 | ||
1da177e4 LT |
1174 | static int rawv6_init_sk(struct sock *sk) |
1175 | { | |
f48d5ff1 MN |
1176 | struct raw6_sock *rp = raw6_sk(sk); |
1177 | ||
c720c7e8 | 1178 | switch (inet_sk(sk)->inet_num) { |
f48d5ff1 | 1179 | case IPPROTO_ICMPV6: |
1da177e4 LT |
1180 | rp->checksum = 1; |
1181 | rp->offset = 2; | |
f48d5ff1 MN |
1182 | break; |
1183 | case IPPROTO_MH: | |
1184 | rp->checksum = 1; | |
1185 | rp->offset = 4; | |
1186 | break; | |
1187 | default: | |
1188 | break; | |
1da177e4 | 1189 | } |
a02cec21 | 1190 | return 0; |
1da177e4 LT |
1191 | } |
1192 | ||
1193 | struct proto rawv6_prot = { | |
543d9cfe ACM |
1194 | .name = "RAWv6", |
1195 | .owner = THIS_MODULE, | |
1196 | .close = rawv6_close, | |
22dd4850 | 1197 | .destroy = raw6_destroy, |
82b276cd | 1198 | .connect = ip6_datagram_connect_v6_only, |
286c72de | 1199 | .disconnect = __udp_disconnect, |
543d9cfe ACM |
1200 | .ioctl = rawv6_ioctl, |
1201 | .init = rawv6_init_sk, | |
543d9cfe ACM |
1202 | .setsockopt = rawv6_setsockopt, |
1203 | .getsockopt = rawv6_getsockopt, | |
1204 | .sendmsg = rawv6_sendmsg, | |
1205 | .recvmsg = rawv6_recvmsg, | |
1206 | .bind = rawv6_bind, | |
1207 | .backlog_rcv = rawv6_rcv_skb, | |
fc8717ba PE |
1208 | .hash = raw_hash_sk, |
1209 | .unhash = raw_unhash_sk, | |
543d9cfe | 1210 | .obj_size = sizeof(struct raw6_sock), |
f5f80e32 | 1211 | .ipv6_pinfo_offset = offsetof(struct raw6_sock, inet6), |
8c2bc895 DW |
1212 | .useroffset = offsetof(struct raw6_sock, filter), |
1213 | .usersize = sizeof_field(struct raw6_sock, filter), | |
fc8717ba | 1214 | .h.raw_hash = &raw_v6_hashinfo, |
3fdadf7d | 1215 | #ifdef CONFIG_COMPAT |
e2d57766 | 1216 | .compat_ioctl = compat_rawv6_ioctl, |
3fdadf7d | 1217 | #endif |
432490f9 | 1218 | .diag_destroy = raw_abort, |
1da177e4 LT |
1219 | }; |
1220 | ||
1221 | #ifdef CONFIG_PROC_FS | |
1da177e4 LT |
1222 | static int raw6_seq_show(struct seq_file *seq, void *v) |
1223 | { | |
17ef66af LC |
1224 | if (v == SEQ_START_TOKEN) { |
1225 | seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); | |
1226 | } else { | |
1227 | struct sock *sp = v; | |
1228 | __u16 srcp = inet_sk(sp)->inet_num; | |
1229 | ip6_dgram_sock_seq_show(seq, v, srcp, 0, | |
1230 | raw_seq_private(seq)->bucket); | |
1231 | } | |
1da177e4 LT |
1232 | return 0; |
1233 | } | |
1234 | ||
56b3d975 | 1235 | static const struct seq_operations raw6_seq_ops = { |
42a73808 PE |
1236 | .start = raw_seq_start, |
1237 | .next = raw_seq_next, | |
1238 | .stop = raw_seq_stop, | |
1da177e4 LT |
1239 | .show = raw6_seq_show, |
1240 | }; | |
1241 | ||
2c8c1e72 | 1242 | static int __net_init raw6_init_net(struct net *net) |
1da177e4 | 1243 | { |
c3506372 CH |
1244 | if (!proc_create_net_data("raw6", 0444, net->proc_net, &raw6_seq_ops, |
1245 | sizeof(struct raw_iter_state), &raw_v6_hashinfo)) | |
1da177e4 | 1246 | return -ENOMEM; |
a308da16 | 1247 | |
1da177e4 LT |
1248 | return 0; |
1249 | } | |
1250 | ||
2c8c1e72 | 1251 | static void __net_exit raw6_exit_net(struct net *net) |
a308da16 | 1252 | { |
ece31ffd | 1253 | remove_proc_entry("raw6", net->proc_net); |
a308da16 PE |
1254 | } |
1255 | ||
1256 | static struct pernet_operations raw6_net_ops = { | |
1257 | .init = raw6_init_net, | |
1258 | .exit = raw6_exit_net, | |
1259 | }; | |
1260 | ||
1261 | int __init raw6_proc_init(void) | |
1262 | { | |
1263 | return register_pernet_subsys(&raw6_net_ops); | |
1264 | } | |
1265 | ||
1da177e4 LT |
1266 | void raw6_proc_exit(void) |
1267 | { | |
a308da16 | 1268 | unregister_pernet_subsys(&raw6_net_ops); |
1da177e4 LT |
1269 | } |
1270 | #endif /* CONFIG_PROC_FS */ | |
7f4e4868 | 1271 | |
a11e1d43 | 1272 | /* Same as inet6_dgram_ops, sans udp_poll. */ |
77d4b1d3 | 1273 | const struct proto_ops inet6_sockraw_ops = { |
7f4e4868 DL |
1274 | .family = PF_INET6, |
1275 | .owner = THIS_MODULE, | |
1276 | .release = inet6_release, | |
1277 | .bind = inet6_bind, | |
1278 | .connect = inet_dgram_connect, /* ok */ | |
1279 | .socketpair = sock_no_socketpair, /* a do nothing */ | |
1280 | .accept = sock_no_accept, /* a do nothing */ | |
1281 | .getname = inet6_getname, | |
a11e1d43 | 1282 | .poll = datagram_poll, /* ok */ |
7f4e4868 | 1283 | .ioctl = inet6_ioctl, /* must change */ |
c7cbdbf2 | 1284 | .gettstamp = sock_gettstamp, |
7f4e4868 DL |
1285 | .listen = sock_no_listen, /* ok */ |
1286 | .shutdown = inet_shutdown, /* ok */ | |
1287 | .setsockopt = sock_common_setsockopt, /* ok */ | |
1288 | .getsockopt = sock_common_getsockopt, /* ok */ | |
1289 | .sendmsg = inet_sendmsg, /* ok */ | |
1290 | .recvmsg = sock_common_recvmsg, /* ok */ | |
1291 | .mmap = sock_no_mmap, | |
7f4e4868 | 1292 | #ifdef CONFIG_COMPAT |
3986912f | 1293 | .compat_ioctl = inet6_compat_ioctl, |
7f4e4868 DL |
1294 | #endif |
1295 | }; | |
1296 | ||
1297 | static struct inet_protosw rawv6_protosw = { | |
1298 | .type = SOCK_RAW, | |
1299 | .protocol = IPPROTO_IP, /* wild card */ | |
1300 | .prot = &rawv6_prot, | |
1301 | .ops = &inet6_sockraw_ops, | |
7f4e4868 DL |
1302 | .flags = INET_PROTOSW_REUSE, |
1303 | }; | |
1304 | ||
1305 | int __init rawv6_init(void) | |
1306 | { | |
3d2f6d41 | 1307 | return inet6_register_protosw(&rawv6_protosw); |
7f4e4868 DL |
1308 | } |
1309 | ||
09f7709f | 1310 | void rawv6_exit(void) |
7f4e4868 DL |
1311 | { |
1312 | inet6_unregister_protosw(&rawv6_protosw); | |
1313 | } |