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