| 1 | |
| 2 | |
| 3 | * UDP over IPv6
|
| 4 | * Linux INET6 implementation
|
| 5 | *
|
| 6 | * Authors:
|
| 7 | * Pedro Roque <[email protected]>
|
| 8 | *
|
| 9 | * Based on linux/ipv4/udp.c
|
| 10 | *
|
| 11 | * Fixes:
|
| 12 | * Hideaki YOSHIFUJI : sin6_scope_id support
|
| 13 | * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
|
| 14 | * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
|
| 15 | * a single port at the same time.
|
| 16 | * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
|
| 17 | * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
|
| 18 |
|
| 19 |
|
| 20 | #include <linux/bpf-cgroup.h>
|
| 21 | #include <linux/errno.h>
|
| 22 | #include <linux/types.h>
|
| 23 | #include <linux/socket.h>
|
| 24 | #include <linux/sockios.h>
|
| 25 | #include <linux/net.h>
|
| 26 | #include <linux/in6.h>
|
| 27 | #include <linux/netdevice.h>
|
| 28 | #include <linux/if_arp.h>
|
| 29 | #include <linux/ipv6.h>
|
| 30 | #include <linux/icmpv6.h>
|
| 31 | #include <linux/init.h>
|
| 32 | #include <linux/module.h>
|
| 33 | #include <linux/skbuff.h>
|
| 34 | #include <linux/slab.h>
|
| 35 | #include <linux/uaccess.h>
|
| 36 | #include <linux/indirect_call_wrapper.h>
|
| 37 | #include <trace/events/udp.h>
|
| 38 |
|
| 39 | #include <net/addrconf.h>
|
| 40 | #include <net/aligned_data.h>
|
| 41 | #include <net/ndisc.h>
|
| 42 | #include <net/protocol.h>
|
| 43 | #include <net/transp_v6.h>
|
| 44 | #include <net/ip6_route.h>
|
| 45 | #include <net/raw.h>
|
| 46 | #include <net/seg6.h>
|
| 47 | #include <net/tcp_states.h>
|
| 48 | #include <net/ip6_checksum.h>
|
| 49 | #include <net/ip6_tunnel.h>
|
| 50 | #include <net/udp_tunnel.h>
|
| 51 | #include <net/xfrm.h>
|
| 52 | #include <net/inet_hashtables.h>
|
| 53 | #include <net/inet6_hashtables.h>
|
| 54 | #include <net/busy_poll.h>
|
| 55 | #include <net/sock_reuseport.h>
|
| 56 | #include <net/gro.h>
|
| 57 |
|
| 58 | #include <linux/proc_fs.h>
|
| 59 | #include <linux/seq_file.h>
|
| 60 | #include <trace/events/skb.h>
|
| 61 |
|
| 62 | static void udpv6_destruct_sock(struct sock *sk)
|
| 63 | {
|
| 64 | udp_destruct_common(sk);
|
| 65 | inet6_sock_destruct(sk);
|
| 66 | }
|
| 67 |
|
| 68 | static int udpv6_init_sock(struct sock *sk)
|
| 69 | {
|
| 70 | int res = udp_lib_init_sock(sk);
|
| 71 |
|
| 72 | sk->sk_destruct = udpv6_destruct_sock;
|
| 73 | set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);
|
| 74 | return res;
|
| 75 | }
|
| 76 |
|
| 77 | INDIRECT_CALLABLE_SCOPE
|
| 78 | u32 udp6_ehashfn(const struct net *net,
|
| 79 | const struct in6_addr *laddr,
|
| 80 | const u16 lport,
|
| 81 | const struct in6_addr *faddr,
|
| 82 | const __be16 fport)
|
| 83 | {
|
| 84 | u32 lhash, fhash;
|
| 85 |
|
| 86 | net_get_random_once(&udp6_ehash_secret,
|
| 87 | sizeof(udp6_ehash_secret));
|
| 88 | net_get_random_once(&udp_ipv6_hash_secret,
|
| 89 | sizeof(udp_ipv6_hash_secret));
|
| 90 |
|
| 91 | lhash = (__force u32)laddr->s6_addr32[3];
|
| 92 | fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
|
| 93 |
|
| 94 | return __inet6_ehashfn(lhash, lport, fhash, fport,
|
| 95 | udp6_ehash_secret + net_hash_mix(net));
|
| 96 | }
|
| 97 |
|
| 98 | static int udp_v6_get_port(struct sock *sk, unsigned short snum)
|
| 99 | {
|
| 100 | unsigned int hash2_nulladdr =
|
| 101 | ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
|
| 102 | unsigned int hash2_partial =
|
| 103 | ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
|
| 104 |
|
| 105 |
|
| 106 | udp_sk(sk)->udp_portaddr_hash = hash2_partial;
|
| 107 | return udp_lib_get_port(sk, snum, hash2_nulladdr);
|
| 108 | }
|
| 109 |
|
| 110 | static void udp_v6_rehash(struct sock *sk)
|
| 111 | {
|
| 112 | u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
|
| 113 | &sk->sk_v6_rcv_saddr,
|
| 114 | inet_sk(sk)->inet_num);
|
| 115 | u16 new_hash4;
|
| 116 |
|
| 117 | if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) {
|
| 118 | new_hash4 = udp_ehashfn(sock_net(sk),
|
| 119 | sk->sk_rcv_saddr, sk->sk_num,
|
| 120 | sk->sk_daddr, sk->sk_dport);
|
| 121 | } else {
|
| 122 | new_hash4 = udp6_ehashfn(sock_net(sk),
|
| 123 | &sk->sk_v6_rcv_saddr, sk->sk_num,
|
| 124 | &sk->sk_v6_daddr, sk->sk_dport);
|
| 125 | }
|
| 126 |
|
| 127 | udp_lib_rehash(sk, new_hash, new_hash4);
|
| 128 | }
|
| 129 |
|
| 130 | static __always_inline int
|
| 131 | compute_score(struct sock *sk, const struct net *net,
|
| 132 | const struct in6_addr *saddr, __be16 sport,
|
| 133 | const struct in6_addr *daddr, unsigned short hnum,
|
| 134 | int dif, int sdif)
|
| 135 | {
|
| 136 | int bound_dev_if, score;
|
| 137 | struct inet_sock *inet;
|
| 138 | bool dev_match;
|
| 139 |
|
| 140 | if (!net_eq(sock_net(sk), net) ||
|
| 141 | udp_sk(sk)->udp_port_hash != hnum ||
|
| 142 | sk->sk_family != PF_INET6)
|
| 143 | return -1;
|
| 144 |
|
| 145 | if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
|
| 146 | return -1;
|
| 147 |
|
| 148 | score = 0;
|
| 149 | inet = inet_sk(sk);
|
| 150 |
|
| 151 | if (inet->inet_dport) {
|
| 152 | if (inet->inet_dport != sport)
|
| 153 | return -1;
|
| 154 | score++;
|
| 155 | }
|
| 156 |
|
| 157 | if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
|
| 158 | if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
|
| 159 | return -1;
|
| 160 | score++;
|
| 161 | }
|
| 162 |
|
| 163 | bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
|
| 164 | dev_match = udp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif);
|
| 165 | if (!dev_match)
|
| 166 | return -1;
|
| 167 | if (bound_dev_if)
|
| 168 | score++;
|
| 169 |
|
| 170 | if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
|
| 171 | score++;
|
| 172 |
|
| 173 | return score;
|
| 174 | }
|
| 175 |
|
| 176 | |
| 177 | * udp6_lib_lookup1() - Simplified lookup using primary hash (destination port)
|
| 178 | * @net: Network namespace
|
| 179 | * @saddr: Source address, network order
|
| 180 | * @sport: Source port, network order
|
| 181 | * @daddr: Destination address, network order
|
| 182 | * @hnum: Destination port, host order
|
| 183 | * @dif: Destination interface index
|
| 184 | * @sdif: Destination bridge port index, if relevant
|
| 185 | * @udptable: Set of UDP hash tables
|
| 186 | *
|
| 187 | * Simplified lookup to be used as fallback if no sockets are found due to a
|
| 188 | * potential race between (receive) address change, and lookup happening before
|
| 189 | * the rehash operation. This function ignores SO_REUSEPORT groups while scoring
|
| 190 | * result sockets, because if we have one, we don't need the fallback at all.
|
| 191 | *
|
| 192 | * Called under rcu_read_lock().
|
| 193 | *
|
| 194 | * Return: socket with highest matching score if any, NULL if none
|
| 195 |
|
| 196 | static struct sock *udp6_lib_lookup1(const struct net *net,
|
| 197 | const struct in6_addr *saddr, __be16 sport,
|
| 198 | const struct in6_addr *daddr,
|
| 199 | unsigned int hnum, int dif, int sdif,
|
| 200 | const struct udp_table *udptable)
|
| 201 | {
|
| 202 | unsigned int slot = udp_hashfn(net, hnum, udptable->mask);
|
| 203 | struct udp_hslot *hslot = &udptable->hash[slot];
|
| 204 | struct sock *sk, *result = NULL;
|
| 205 | int score, badness = 0;
|
| 206 |
|
| 207 | sk_for_each_rcu(sk, &hslot->head) {
|
| 208 | score = compute_score(sk, net,
|
| 209 | saddr, sport, daddr, hnum, dif, sdif);
|
| 210 | if (score > badness) {
|
| 211 | result = sk;
|
| 212 | badness = score;
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | return result;
|
| 217 | }
|
| 218 |
|
| 219 |
|
| 220 | static struct sock *udp6_lib_lookup2(const struct net *net,
|
| 221 | const struct in6_addr *saddr, __be16 sport,
|
| 222 | const struct in6_addr *daddr, unsigned int hnum,
|
| 223 | int dif, int sdif, struct udp_hslot *hslot2,
|
| 224 | struct sk_buff *skb)
|
| 225 | {
|
| 226 | struct sock *sk, *result;
|
| 227 | int score, badness;
|
| 228 | bool need_rescore;
|
| 229 |
|
| 230 | result = NULL;
|
| 231 | badness = -1;
|
| 232 | udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
|
| 233 | need_rescore = false;
|
| 234 | rescore:
|
| 235 | score = compute_score(need_rescore ? result : sk, net, saddr,
|
| 236 | sport, daddr, hnum, dif, sdif);
|
| 237 | if (score > badness) {
|
| 238 | badness = score;
|
| 239 |
|
| 240 | if (need_rescore)
|
| 241 | continue;
|
| 242 |
|
| 243 | if (sk->sk_state == TCP_ESTABLISHED) {
|
| 244 | result = sk;
|
| 245 | continue;
|
| 246 | }
|
| 247 |
|
| 248 | result = inet6_lookup_reuseport(net, sk, skb, sizeof(struct udphdr),
|
| 249 | saddr, sport, daddr, hnum, udp6_ehashfn);
|
| 250 | if (!result) {
|
| 251 | result = sk;
|
| 252 | continue;
|
| 253 | }
|
| 254 |
|
| 255 |
|
| 256 | if (!reuseport_has_conns(sk))
|
| 257 | return result;
|
| 258 |
|
| 259 |
|
| 260 | if (IS_ERR(result))
|
| 261 | continue;
|
| 262 |
|
| 263 | |
| 264 | * inlined twice here, and calling it uninlined
|
| 265 | * here yields measurable overhead for some
|
| 266 | * workloads. Work around it by jumping
|
| 267 | * backwards to rescore 'result'.
|
| 268 |
|
| 269 | need_rescore = true;
|
| 270 | goto rescore;
|
| 271 | }
|
| 272 | }
|
| 273 | return result;
|
| 274 | }
|
| 275 |
|
| 276 | #if IS_ENABLED(CONFIG_BASE_SMALL)
|
| 277 | static struct sock *udp6_lib_lookup4(const struct net *net,
|
| 278 | const struct in6_addr *saddr, __be16 sport,
|
| 279 | const struct in6_addr *daddr,
|
| 280 | unsigned int hnum, int dif, int sdif,
|
| 281 | struct udp_table *udptable)
|
| 282 | {
|
| 283 | return NULL;
|
| 284 | }
|
| 285 |
|
| 286 | static void udp6_hash4(struct sock *sk)
|
| 287 | {
|
| 288 | }
|
| 289 | #else
|
| 290 | static struct sock *udp6_lib_lookup4(const struct net *net,
|
| 291 | const struct in6_addr *saddr, __be16 sport,
|
| 292 | const struct in6_addr *daddr,
|
| 293 | unsigned int hnum, int dif, int sdif,
|
| 294 | struct udp_table *udptable)
|
| 295 | {
|
| 296 | const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
|
| 297 | const struct hlist_nulls_node *node;
|
| 298 | struct udp_hslot *hslot4;
|
| 299 | unsigned int hash4, slot;
|
| 300 | struct udp_sock *up;
|
| 301 | struct sock *sk;
|
| 302 |
|
| 303 | hash4 = udp6_ehashfn(net, daddr, hnum, saddr, sport);
|
| 304 | slot = hash4 & udptable->mask;
|
| 305 | hslot4 = &udptable->hash4[slot];
|
| 306 |
|
| 307 | begin:
|
| 308 | udp_lrpa_for_each_entry_rcu(up, node, &hslot4->nulls_head) {
|
| 309 | sk = (struct sock *)up;
|
| 310 | if (inet6_match(net, sk, saddr, daddr, ports, dif, sdif))
|
| 311 | return sk;
|
| 312 | }
|
| 313 |
|
| 314 | |
| 315 | * expected one, we must restart lookup. We probably met an item that
|
| 316 | * was moved to another chain due to rehash.
|
| 317 |
|
| 318 | if (get_nulls_value(node) != slot)
|
| 319 | goto begin;
|
| 320 |
|
| 321 | return NULL;
|
| 322 | }
|
| 323 |
|
| 324 | static void udp6_hash4(struct sock *sk)
|
| 325 | {
|
| 326 | struct net *net = sock_net(sk);
|
| 327 | unsigned int hash;
|
| 328 |
|
| 329 | if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) {
|
| 330 | udp4_hash4(sk);
|
| 331 | return;
|
| 332 | }
|
| 333 |
|
| 334 | if (sk_unhashed(sk) || ipv6_addr_any(&sk->sk_v6_rcv_saddr))
|
| 335 | return;
|
| 336 |
|
| 337 | hash = udp6_ehashfn(net, &sk->sk_v6_rcv_saddr, sk->sk_num,
|
| 338 | &sk->sk_v6_daddr, sk->sk_dport);
|
| 339 |
|
| 340 | udp_lib_hash4(sk, hash);
|
| 341 | }
|
| 342 | #endif
|
| 343 |
|
| 344 |
|
| 345 | struct sock *__udp6_lib_lookup(const struct net *net,
|
| 346 | const struct in6_addr *saddr, __be16 sport,
|
| 347 | const struct in6_addr *daddr, __be16 dport,
|
| 348 | int dif, int sdif, struct sk_buff *skb)
|
| 349 | {
|
| 350 | struct udp_table *udptable = net->ipv4.udp_table;
|
| 351 | unsigned short hnum = ntohs(dport);
|
| 352 | struct udp_hslot *hslot2;
|
| 353 | struct sock *result, *sk;
|
| 354 | unsigned int hash2;
|
| 355 |
|
| 356 | hash2 = ipv6_portaddr_hash(net, daddr, hnum);
|
| 357 | hslot2 = udp_hashslot2(udptable, hash2);
|
| 358 |
|
| 359 | if (udp_has_hash4(hslot2)) {
|
| 360 | result = udp6_lib_lookup4(net, saddr, sport, daddr, hnum,
|
| 361 | dif, sdif, udptable);
|
| 362 | if (result)
|
| 363 | return result;
|
| 364 | }
|
| 365 |
|
| 366 |
|
| 367 | result = udp6_lib_lookup2(net, saddr, sport,
|
| 368 | daddr, hnum, dif, sdif,
|
| 369 | hslot2, skb);
|
| 370 | if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
|
| 371 | goto done;
|
| 372 |
|
| 373 |
|
| 374 | if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
|
| 375 | sk = inet6_lookup_run_sk_lookup(net, IPPROTO_UDP, skb, sizeof(struct udphdr),
|
| 376 | saddr, sport, daddr, hnum, dif,
|
| 377 | udp6_ehashfn);
|
| 378 | if (sk) {
|
| 379 | result = sk;
|
| 380 | goto done;
|
| 381 | }
|
| 382 | }
|
| 383 |
|
| 384 |
|
| 385 | if (result)
|
| 386 | goto done;
|
| 387 |
|
| 388 |
|
| 389 | hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
|
| 390 | hslot2 = udp_hashslot2(udptable, hash2);
|
| 391 |
|
| 392 | result = udp6_lib_lookup2(net, saddr, sport,
|
| 393 | &in6addr_any, hnum, dif, sdif,
|
| 394 | hslot2, skb);
|
| 395 | if (!IS_ERR_OR_NULL(result))
|
| 396 | goto done;
|
| 397 |
|
| 398 |
|
| 399 | result = udp6_lib_lookup1(net, saddr, sport, daddr, hnum, dif, sdif,
|
| 400 | udptable);
|
| 401 |
|
| 402 | done:
|
| 403 | if (IS_ERR(result))
|
| 404 | return NULL;
|
| 405 | return result;
|
| 406 | }
|
| 407 | EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
|
| 408 |
|
| 409 | static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
|
| 410 | __be16 sport, __be16 dport)
|
| 411 | {
|
| 412 | const struct ipv6hdr *iph = ipv6_hdr(skb);
|
| 413 |
|
| 414 | return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
|
| 415 | &iph->daddr, dport, inet6_iif(skb),
|
| 416 | inet6_sdif(skb), skb);
|
| 417 | }
|
| 418 |
|
| 419 | struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
|
| 420 | __be16 sport, __be16 dport)
|
| 421 | {
|
| 422 | const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
|
| 423 | const struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + offset);
|
| 424 | int iif, sdif;
|
| 425 |
|
| 426 | inet6_get_iif_sdif(skb, &iif, &sdif);
|
| 427 |
|
| 428 | return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
|
| 429 | &iph->daddr, dport, iif, sdif, NULL);
|
| 430 | }
|
| 431 |
|
| 432 | |
| 433 | * Does increment socket refcount.
|
| 434 |
|
| 435 | #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
|
| 436 | struct sock *udp6_lib_lookup(const struct net *net, const struct in6_addr *saddr, __be16 sport,
|
| 437 | const struct in6_addr *daddr, __be16 dport, int dif)
|
| 438 | {
|
| 439 | struct sock *sk;
|
| 440 |
|
| 441 | sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, 0, NULL);
|
| 442 | if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
|
| 443 | sk = NULL;
|
| 444 | return sk;
|
| 445 | }
|
| 446 | EXPORT_SYMBOL_GPL(udp6_lib_lookup);
|
| 447 | #endif
|
| 448 |
|
| 449 | |
| 450 | * scratch area space; note that the IP6CB flags is still in the first
|
| 451 | * cacheline, so checking for jumbograms is cheap
|
| 452 |
|
| 453 | static int udp6_skb_len(struct sk_buff *skb)
|
| 454 | {
|
| 455 | return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
|
| 456 | }
|
| 457 |
|
| 458 | |
| 459 | * This should be easy, if there is something there we
|
| 460 | * return it, otherwise we block.
|
| 461 |
|
| 462 |
|
| 463 | INDIRECT_CALLABLE_SCOPE
|
| 464 | int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
|
| 465 | int flags)
|
| 466 | {
|
| 467 | int off, is_udp4, err, peeking = flags & MSG_PEEK;
|
| 468 | struct ipv6_pinfo *np = inet6_sk(sk);
|
| 469 | struct inet_sock *inet = inet_sk(sk);
|
| 470 | struct udp_mib __percpu *mib;
|
| 471 | bool checksum_valid = false;
|
| 472 | unsigned int ulen, copied;
|
| 473 | struct sk_buff *skb;
|
| 474 |
|
| 475 | if (flags & MSG_ERRQUEUE)
|
| 476 | return ipv6_recv_error(sk, msg, len);
|
| 477 |
|
| 478 | if (np->rxopt.bits.rxpmtu && READ_ONCE(np->rxpmtu))
|
| 479 | return ipv6_recv_rxpmtu(sk, msg, len);
|
| 480 |
|
| 481 | try_again:
|
| 482 | off = sk_peek_offset(sk, flags);
|
| 483 | skb = __skb_recv_udp(sk, flags, &off, &err);
|
| 484 | if (!skb)
|
| 485 | return err;
|
| 486 |
|
| 487 | ulen = udp6_skb_len(skb);
|
| 488 | copied = len;
|
| 489 | if (copied > ulen - off)
|
| 490 | copied = ulen - off;
|
| 491 | else if (copied < ulen)
|
| 492 | msg->msg_flags |= MSG_TRUNC;
|
| 493 |
|
| 494 | is_udp4 = (skb->protocol == htons(ETH_P_IP));
|
| 495 | mib = __UDPX_MIB(sk, is_udp4);
|
| 496 |
|
| 497 | |
| 498 | * data. If the data is truncated, do it before the copy.
|
| 499 |
|
| 500 | if (copied < ulen || peeking) {
|
| 501 | checksum_valid = udp_skb_csum_unnecessary(skb) ||
|
| 502 | !__udp_lib_checksum_complete(skb);
|
| 503 | if (!checksum_valid)
|
| 504 | goto csum_copy_err;
|
| 505 | }
|
| 506 |
|
| 507 | if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
|
| 508 | if (udp_skb_is_linear(skb))
|
| 509 | err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
|
| 510 | else
|
| 511 | err = skb_copy_datagram_msg(skb, off, msg, copied);
|
| 512 | } else {
|
| 513 | err = skb_copy_and_csum_datagram_msg(skb, off, msg);
|
| 514 | if (err == -EINVAL)
|
| 515 | goto csum_copy_err;
|
| 516 | }
|
| 517 | if (unlikely(err)) {
|
| 518 | if (!peeking) {
|
| 519 | udp_drops_inc(sk);
|
| 520 | SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
|
| 521 | }
|
| 522 | kfree_skb(skb);
|
| 523 | return err;
|
| 524 | }
|
| 525 | if (!peeking)
|
| 526 | SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
|
| 527 |
|
| 528 | sock_recv_cmsgs(msg, sk, skb);
|
| 529 |
|
| 530 |
|
| 531 | if (msg->msg_name) {
|
| 532 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
|
| 533 | sin6->sin6_family = AF_INET6;
|
| 534 | sin6->sin6_port = udp_hdr(skb)->source;
|
| 535 | sin6->sin6_flowinfo = 0;
|
| 536 |
|
| 537 | if (is_udp4) {
|
| 538 | ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
|
| 539 | &sin6->sin6_addr);
|
| 540 | sin6->sin6_scope_id = 0;
|
| 541 | } else {
|
| 542 | sin6->sin6_addr = ipv6_hdr(skb)->saddr;
|
| 543 | sin6->sin6_scope_id =
|
| 544 | ipv6_iface_scope_id(&sin6->sin6_addr,
|
| 545 | inet6_iif(skb));
|
| 546 | }
|
| 547 | msg->msg_namelen = sizeof(*sin6);
|
| 548 |
|
| 549 | BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
|
| 550 | (struct sockaddr *)sin6,
|
| 551 | &msg->msg_namelen);
|
| 552 | }
|
| 553 |
|
| 554 | if (udp_test_bit(GRO_ENABLED, sk))
|
| 555 | udp_cmsg_recv(msg, sk, skb);
|
| 556 |
|
| 557 | if (np->rxopt.all)
|
| 558 | ip6_datagram_recv_common_ctl(sk, msg, skb);
|
| 559 |
|
| 560 | if (is_udp4) {
|
| 561 | if (inet_cmsg_flags(inet))
|
| 562 | ip_cmsg_recv_offset(msg, sk, skb,
|
| 563 | sizeof(struct udphdr), off);
|
| 564 | } else {
|
| 565 | if (np->rxopt.all)
|
| 566 | ip6_datagram_recv_specific_ctl(sk, msg, skb);
|
| 567 | }
|
| 568 |
|
| 569 | err = copied;
|
| 570 | if (flags & MSG_TRUNC)
|
| 571 | err = ulen;
|
| 572 |
|
| 573 | skb_consume_udp(sk, skb, peeking ? -err : err);
|
| 574 | return err;
|
| 575 |
|
| 576 | csum_copy_err:
|
| 577 | if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
|
| 578 | udp_skb_destructor)) {
|
| 579 | SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
|
| 580 | SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
|
| 581 | }
|
| 582 | kfree_skb_reason(skb, SKB_DROP_REASON_UDP_CSUM);
|
| 583 |
|
| 584 |
|
| 585 | cond_resched();
|
| 586 | msg->msg_flags &= ~MSG_TRUNC;
|
| 587 | goto try_again;
|
| 588 | }
|
| 589 |
|
| 590 | DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
|
| 591 | void udpv6_encap_enable(void)
|
| 592 | {
|
| 593 | static_branch_inc(&udpv6_encap_needed_key);
|
| 594 | }
|
| 595 | EXPORT_SYMBOL(udpv6_encap_enable);
|
| 596 |
|
| 597 | |
| 598 | * through error handlers in encapsulations looking for a match.
|
| 599 |
|
| 600 | static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
|
| 601 | struct inet6_skb_parm *opt,
|
| 602 | u8 type, u8 code, int offset, __be32 info)
|
| 603 | {
|
| 604 | int i;
|
| 605 |
|
| 606 | for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
|
| 607 | int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
|
| 608 | u8 type, u8 code, int offset, __be32 info);
|
| 609 | const struct ip6_tnl_encap_ops *encap;
|
| 610 |
|
| 611 | encap = rcu_dereference(ip6tun_encaps[i]);
|
| 612 | if (!encap)
|
| 613 | continue;
|
| 614 | handler = encap->err_handler;
|
| 615 | if (handler && !handler(skb, opt, type, code, offset, info))
|
| 616 | return 0;
|
| 617 | }
|
| 618 |
|
| 619 | return -ENOENT;
|
| 620 | }
|
| 621 |
|
| 622 | |
| 623 | * reversing source and destination port: this will match tunnels that force the
|
| 624 | * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
|
| 625 | * lwtunnels might actually break this assumption by being configured with
|
| 626 | * different destination ports on endpoints, in this case we won't be able to
|
| 627 | * trace ICMP messages back to them.
|
| 628 | *
|
| 629 | * If this doesn't match any socket, probe tunnels with arbitrary destination
|
| 630 | * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
|
| 631 | * we've sent packets to won't necessarily match the local destination port.
|
| 632 | *
|
| 633 | * Then ask the tunnel implementation to match the error against a valid
|
| 634 | * association.
|
| 635 | *
|
| 636 | * Return an error if we can't find a match, the socket if we need further
|
| 637 | * processing, zero otherwise.
|
| 638 |
|
| 639 | static struct sock *__udp6_lib_err_encap(struct net *net,
|
| 640 | const struct ipv6hdr *hdr, int offset,
|
| 641 | struct udphdr *uh,
|
| 642 | struct sock *sk,
|
| 643 | struct sk_buff *skb,
|
| 644 | struct inet6_skb_parm *opt,
|
| 645 | u8 type, u8 code, __be32 info)
|
| 646 | {
|
| 647 | int (*lookup)(struct sock *sk, struct sk_buff *skb);
|
| 648 | int network_offset, transport_offset;
|
| 649 | struct udp_sock *up;
|
| 650 |
|
| 651 | network_offset = skb_network_offset(skb);
|
| 652 | transport_offset = skb_transport_offset(skb);
|
| 653 |
|
| 654 |
|
| 655 | skb_reset_network_header(skb);
|
| 656 |
|
| 657 |
|
| 658 | skb_set_transport_header(skb, offset);
|
| 659 |
|
| 660 | if (sk) {
|
| 661 | up = udp_sk(sk);
|
| 662 |
|
| 663 | lookup = READ_ONCE(up->encap_err_lookup);
|
| 664 | if (lookup && lookup(sk, skb))
|
| 665 | sk = NULL;
|
| 666 |
|
| 667 | goto out;
|
| 668 | }
|
| 669 |
|
| 670 | sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
|
| 671 | &hdr->saddr, uh->dest,
|
| 672 | inet6_iif(skb), 0, skb);
|
| 673 | if (sk) {
|
| 674 | up = udp_sk(sk);
|
| 675 |
|
| 676 | lookup = READ_ONCE(up->encap_err_lookup);
|
| 677 | if (!lookup || lookup(sk, skb))
|
| 678 | sk = NULL;
|
| 679 | }
|
| 680 |
|
| 681 | out:
|
| 682 | if (!sk) {
|
| 683 | sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
|
| 684 | offset, info));
|
| 685 | }
|
| 686 |
|
| 687 | skb_set_transport_header(skb, transport_offset);
|
| 688 | skb_set_network_header(skb, network_offset);
|
| 689 |
|
| 690 | return sk;
|
| 691 | }
|
| 692 |
|
| 693 | static void udpv6_err_update_exception(struct net *net, struct sk_buff *skb,
|
| 694 | u8 type, __be32 info)
|
| 695 | {
|
| 696 | if (type == ICMPV6_PKT_TOOBIG)
|
| 697 | ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 0,
|
| 698 | sock_net_uid(net, NULL));
|
| 699 | else if (type == NDISC_REDIRECT)
|
| 700 | ip6_redirect(skb, net, skb->dev->ifindex, 0,
|
| 701 | sock_net_uid(net, NULL));
|
| 702 | }
|
| 703 |
|
| 704 | static int udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
|
| 705 | u8 type, u8 code, int offset, __be32 info)
|
| 706 | {
|
| 707 | const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
|
| 708 | struct udphdr *uh = (struct udphdr *)(skb->data + offset);
|
| 709 | const struct in6_addr *saddr, *daddr;
|
| 710 | struct net *net = dev_net(skb->dev);
|
| 711 | struct ipv6_pinfo *np;
|
| 712 | bool tunnel = false;
|
| 713 | struct sock *sk;
|
| 714 | int harderr;
|
| 715 | int err;
|
| 716 |
|
| 717 | udpv6_err_update_exception(net, skb, type, info);
|
| 718 |
|
| 719 | daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr;
|
| 720 | saddr = &hdr->saddr;
|
| 721 | sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
|
| 722 | inet6_iif(skb), inet6_sdif(skb), NULL);
|
| 723 |
|
| 724 | if (!sk || READ_ONCE(udp_sk(sk)->encap_type)) {
|
| 725 |
|
| 726 | if (static_branch_unlikely(&udpv6_encap_needed_key)) {
|
| 727 | sk = __udp6_lib_err_encap(net, hdr, offset, uh, sk, skb,
|
| 728 | opt, type, code, info);
|
| 729 | if (!sk)
|
| 730 | return 0;
|
| 731 | } else
|
| 732 | sk = ERR_PTR(-ENOENT);
|
| 733 |
|
| 734 | if (IS_ERR(sk)) {
|
| 735 | __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
|
| 736 | ICMP6_MIB_INERRORS);
|
| 737 | return PTR_ERR(sk);
|
| 738 | }
|
| 739 |
|
| 740 | tunnel = true;
|
| 741 | }
|
| 742 |
|
| 743 | harderr = icmpv6_err_convert(type, code, &err);
|
| 744 | np = inet6_sk(sk);
|
| 745 |
|
| 746 | if (type == ICMPV6_PKT_TOOBIG) {
|
| 747 | if (!ip6_sk_accept_pmtu(sk))
|
| 748 | goto out;
|
| 749 | ip6_sk_update_pmtu(skb, sk, info);
|
| 750 | if (READ_ONCE(np->pmtudisc) != IPV6_PMTUDISC_DONT)
|
| 751 | harderr = 1;
|
| 752 | }
|
| 753 | if (type == NDISC_REDIRECT) {
|
| 754 | if (tunnel) {
|
| 755 | ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
|
| 756 | READ_ONCE(sk->sk_mark),
|
| 757 | sk_uid(sk));
|
| 758 | } else {
|
| 759 | ip6_sk_redirect(skb, sk);
|
| 760 | }
|
| 761 | goto out;
|
| 762 | }
|
| 763 |
|
| 764 |
|
| 765 | if (tunnel) {
|
| 766 | if (udp_sk(sk)->encap_err_rcv)
|
| 767 | udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest,
|
| 768 | ntohl(info), (u8 *)(uh+1));
|
| 769 | goto out;
|
| 770 | }
|
| 771 |
|
| 772 | if (!inet6_test_bit(RECVERR6, sk)) {
|
| 773 | if (!harderr || sk->sk_state != TCP_ESTABLISHED)
|
| 774 | goto out;
|
| 775 | } else {
|
| 776 | ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
|
| 777 | }
|
| 778 |
|
| 779 | sk->sk_err = err;
|
| 780 | sk_error_report(sk);
|
| 781 | out:
|
| 782 | return 0;
|
| 783 | }
|
| 784 |
|
| 785 | static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
|
| 786 | {
|
| 787 | int rc;
|
| 788 |
|
| 789 | if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
|
| 790 | sock_rps_save_rxhash(sk, skb);
|
| 791 | sk_mark_napi_id(sk, skb);
|
| 792 | sk_incoming_cpu_update(sk);
|
| 793 | } else {
|
| 794 | sk_mark_napi_id_once(sk, skb);
|
| 795 | }
|
| 796 |
|
| 797 | rc = __udp_enqueue_schedule_skb(sk, skb);
|
| 798 | if (rc < 0) {
|
| 799 | enum skb_drop_reason drop_reason;
|
| 800 | struct net *net = sock_net(sk);
|
| 801 |
|
| 802 |
|
| 803 | if (rc == -ENOMEM) {
|
| 804 | UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS);
|
| 805 | drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF;
|
| 806 | } else {
|
| 807 | UDP6_INC_STATS(net, UDP_MIB_MEMERRORS);
|
| 808 | drop_reason = SKB_DROP_REASON_PROTO_MEM;
|
| 809 | }
|
| 810 | UDP6_INC_STATS(net, UDP_MIB_INERRORS);
|
| 811 | trace_udp_fail_queue_rcv_skb(rc, sk, skb);
|
| 812 | sk_skb_reason_drop(sk, skb, drop_reason);
|
| 813 | return -1;
|
| 814 | }
|
| 815 |
|
| 816 | return 0;
|
| 817 | }
|
| 818 |
|
| 819 | static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
|
| 820 | {
|
| 821 | enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
|
| 822 | struct udp_sock *up = udp_sk(sk);
|
| 823 | struct net *net = sock_net(sk);
|
| 824 |
|
| 825 | if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
|
| 826 | drop_reason = SKB_DROP_REASON_XFRM_POLICY;
|
| 827 | goto drop;
|
| 828 | }
|
| 829 | nf_reset_ct(skb);
|
| 830 |
|
| 831 | if (static_branch_unlikely(&udpv6_encap_needed_key) &&
|
| 832 | READ_ONCE(up->encap_type)) {
|
| 833 | int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
|
| 834 |
|
| 835 | |
| 836 | * This is an encapsulation socket so pass the skb to
|
| 837 | * the socket's udp_encap_rcv() hook. Otherwise, just
|
| 838 | * fall through and pass this up the UDP socket.
|
| 839 | * up->encap_rcv() returns the following value:
|
| 840 | * =0 if skb was successfully passed to the encap
|
| 841 | * handler or was discarded by it.
|
| 842 | * >0 if skb should be passed on to UDP.
|
| 843 | * <0 if skb should be resubmitted as proto -N
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 | encap_rcv = READ_ONCE(up->encap_rcv);
|
| 848 | if (encap_rcv) {
|
| 849 | int ret;
|
| 850 |
|
| 851 |
|
| 852 | if (udp_lib_checksum_complete(skb))
|
| 853 | goto csum_error;
|
| 854 |
|
| 855 | ret = encap_rcv(sk, skb);
|
| 856 | if (ret <= 0) {
|
| 857 | __UDP6_INC_STATS(net, UDP_MIB_INDATAGRAMS);
|
| 858 | return -ret;
|
| 859 | }
|
| 860 | }
|
| 861 |
|
| 862 |
|
| 863 | }
|
| 864 |
|
| 865 | prefetch(&sk->sk_rmem_alloc);
|
| 866 | if (rcu_access_pointer(sk->sk_filter) &&
|
| 867 | udp_lib_checksum_complete(skb))
|
| 868 | goto csum_error;
|
| 869 |
|
| 870 | drop_reason = sk_filter_trim_cap(sk, skb, sizeof(struct udphdr));
|
| 871 | if (drop_reason)
|
| 872 | goto drop;
|
| 873 |
|
| 874 | udp_csum_pull_header(skb);
|
| 875 |
|
| 876 | skb_dst_drop(skb);
|
| 877 |
|
| 878 | return __udpv6_queue_rcv_skb(sk, skb);
|
| 879 |
|
| 880 | csum_error:
|
| 881 | drop_reason = SKB_DROP_REASON_UDP_CSUM;
|
| 882 | __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS);
|
| 883 | drop:
|
| 884 | __UDP6_INC_STATS(net, UDP_MIB_INERRORS);
|
| 885 | udp_drops_inc(sk);
|
| 886 | sk_skb_reason_drop(sk, skb, drop_reason);
|
| 887 | return -1;
|
| 888 | }
|
| 889 |
|
| 890 | static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
|
| 891 | {
|
| 892 | struct sk_buff *next, *segs;
|
| 893 | int ret;
|
| 894 |
|
| 895 | if (likely(!udp_unexpected_gso(sk, skb)))
|
| 896 | return udpv6_queue_rcv_one_skb(sk, skb);
|
| 897 |
|
| 898 | __skb_push(skb, -skb_mac_offset(skb));
|
| 899 | segs = udp_rcv_segment(sk, skb, false);
|
| 900 | skb_list_walk_safe(segs, skb, next) {
|
| 901 | __skb_pull(skb, skb_transport_offset(skb));
|
| 902 |
|
| 903 | udp_post_segment_fix_csum(skb);
|
| 904 | ret = udpv6_queue_rcv_one_skb(sk, skb);
|
| 905 | if (ret > 0)
|
| 906 | ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
|
| 907 | true);
|
| 908 | }
|
| 909 | return 0;
|
| 910 | }
|
| 911 |
|
| 912 | static bool __udp_v6_is_mcast_sock(struct net *net, const struct sock *sk,
|
| 913 | __be16 loc_port, const struct in6_addr *loc_addr,
|
| 914 | __be16 rmt_port, const struct in6_addr *rmt_addr,
|
| 915 | int dif, int sdif, unsigned short hnum)
|
| 916 | {
|
| 917 | const struct inet_sock *inet = inet_sk(sk);
|
| 918 |
|
| 919 | if (!net_eq(sock_net(sk), net))
|
| 920 | return false;
|
| 921 |
|
| 922 | if (udp_sk(sk)->udp_port_hash != hnum ||
|
| 923 | sk->sk_family != PF_INET6 ||
|
| 924 | (inet->inet_dport && inet->inet_dport != rmt_port) ||
|
| 925 | (!ipv6_addr_any(&sk->sk_v6_daddr) &&
|
| 926 | !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
|
| 927 | !udp_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif, sdif) ||
|
| 928 | (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
|
| 929 | !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
|
| 930 | return false;
|
| 931 | if (!inet6_mc_check(sk, loc_addr, rmt_addr))
|
| 932 | return false;
|
| 933 | return true;
|
| 934 | }
|
| 935 |
|
| 936 | static void udp6_csum_zero_error(struct sk_buff *skb)
|
| 937 | {
|
| 938 | |
| 939 | * this error. Well, it is reasonable.
|
| 940 |
|
| 941 | net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
|
| 942 | &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
|
| 943 | &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
|
| 944 | }
|
| 945 |
|
| 946 | |
| 947 | * Note: called only from the BH handler context,
|
| 948 | * so we don't need to lock the hashes.
|
| 949 |
|
| 950 | static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
|
| 951 | const struct in6_addr *saddr,
|
| 952 | const struct in6_addr *daddr)
|
| 953 | {
|
| 954 | struct udp_table *udptable = net->ipv4.udp_table;
|
| 955 | const struct udphdr *uh = udp_hdr(skb);
|
| 956 | unsigned int hash2, hash2_any, offset;
|
| 957 | unsigned short hnum = ntohs(uh->dest);
|
| 958 | struct sock *sk, *first = NULL;
|
| 959 | int sdif = inet6_sdif(skb);
|
| 960 | int dif = inet6_iif(skb);
|
| 961 | struct hlist_node *node;
|
| 962 | struct udp_hslot *hslot;
|
| 963 | struct sk_buff *nskb;
|
| 964 | bool use_hash2;
|
| 965 | int ret;
|
| 966 |
|
| 967 | hash2_any = 0;
|
| 968 | hash2 = 0;
|
| 969 | hslot = udp_hashslot(udptable, net, hnum);
|
| 970 | use_hash2 = hslot->count > 10;
|
| 971 | offset = offsetof(typeof(*sk), sk_node);
|
| 972 |
|
| 973 | if (use_hash2) {
|
| 974 | hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
|
| 975 | udptable->mask;
|
| 976 | hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
|
| 977 | start_lookup:
|
| 978 | hslot = &udptable->hash2[hash2].hslot;
|
| 979 | offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
|
| 980 | }
|
| 981 |
|
| 982 | sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
|
| 983 | if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
|
| 984 | uh->source, saddr, dif, sdif,
|
| 985 | hnum))
|
| 986 | continue;
|
| 987 | |
| 988 | * the socket then skip it.
|
| 989 |
|
| 990 | if (!uh->check && !udp_get_no_check6_rx(sk))
|
| 991 | continue;
|
| 992 | if (!first) {
|
| 993 | first = sk;
|
| 994 | continue;
|
| 995 | }
|
| 996 | nskb = skb_clone(skb, GFP_ATOMIC);
|
| 997 | if (unlikely(!nskb)) {
|
| 998 | udp_drops_inc(sk);
|
| 999 | __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS);
|
| 1000 | __UDP6_INC_STATS(net, UDP_MIB_INERRORS);
|
| 1001 | continue;
|
| 1002 | }
|
| 1003 |
|
| 1004 | if (udpv6_queue_rcv_skb(sk, nskb) > 0)
|
| 1005 | consume_skb(nskb);
|
| 1006 | }
|
| 1007 |
|
| 1008 |
|
| 1009 | if (use_hash2 && hash2 != hash2_any) {
|
| 1010 | hash2 = hash2_any;
|
| 1011 | goto start_lookup;
|
| 1012 | }
|
| 1013 |
|
| 1014 | if (first) {
|
| 1015 | ret = udpv6_queue_rcv_skb(first, skb);
|
| 1016 | if (ret > 0)
|
| 1017 | return ret;
|
| 1018 | } else {
|
| 1019 | kfree_skb(skb);
|
| 1020 | __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI);
|
| 1021 | }
|
| 1022 | return 0;
|
| 1023 | }
|
| 1024 |
|
| 1025 | static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
|
| 1026 | {
|
| 1027 | if (udp_sk_rx_dst_set(sk, dst))
|
| 1028 | sk->sk_rx_dst_cookie = rt6_get_cookie(dst_rt6_info(dst));
|
| 1029 | }
|
| 1030 |
|
| 1031 | |
| 1032 | * return code conversion for ip layer consumption
|
| 1033 |
|
| 1034 | static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
|
| 1035 | struct udphdr *uh)
|
| 1036 | {
|
| 1037 | int ret;
|
| 1038 |
|
| 1039 | if (inet_get_convert_csum(sk) && uh->check)
|
| 1040 | skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
|
| 1041 |
|
| 1042 | ret = udpv6_queue_rcv_skb(sk, skb);
|
| 1043 |
|
| 1044 |
|
| 1045 | if (ret > 0)
|
| 1046 | return ret;
|
| 1047 | return 0;
|
| 1048 | }
|
| 1049 |
|
| 1050 | static int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh)
|
| 1051 | {
|
| 1052 | int err;
|
| 1053 |
|
| 1054 | |
| 1055 | * we accept a checksum of zero here. When we find the socket
|
| 1056 | * for the UDP packet we'll check if that socket allows zero checksum
|
| 1057 | * for IPv6 (set by socket option).
|
| 1058 | *
|
| 1059 | * Note, we are only interested in != 0 or == 0, thus the
|
| 1060 | * force to int.
|
| 1061 |
|
| 1062 | err = (__force int)skb_checksum_init_zero_check(skb, IPPROTO_UDP, uh->check,
|
| 1063 | ip6_compute_pseudo);
|
| 1064 | if (err)
|
| 1065 | return err;
|
| 1066 |
|
| 1067 | if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) {
|
| 1068 |
|
| 1069 | if (skb->csum_complete_sw)
|
| 1070 | return 1;
|
| 1071 |
|
| 1072 | |
| 1073 | * skb->csum is no longer the full packet checksum,
|
| 1074 | * so don't treat is as such.
|
| 1075 |
|
| 1076 | skb_checksum_complete_unset(skb);
|
| 1077 | }
|
| 1078 |
|
| 1079 | return 0;
|
| 1080 | }
|
| 1081 |
|
| 1082 | INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
|
| 1083 | {
|
| 1084 | enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
|
| 1085 | const struct in6_addr *saddr, *daddr;
|
| 1086 | struct net *net = dev_net(skb->dev);
|
| 1087 | struct sock *sk = NULL;
|
| 1088 | struct udphdr *uh;
|
| 1089 | bool refcounted;
|
| 1090 | u32 ulen = 0;
|
| 1091 |
|
| 1092 | if (!pskb_may_pull(skb, sizeof(struct udphdr)))
|
| 1093 | goto discard;
|
| 1094 |
|
| 1095 | saddr = &ipv6_hdr(skb)->saddr;
|
| 1096 | daddr = &ipv6_hdr(skb)->daddr;
|
| 1097 | uh = udp_hdr(skb);
|
| 1098 |
|
| 1099 | ulen = udp_get_len(skb, uh, 0);
|
| 1100 | if (ulen > skb->len)
|
| 1101 | goto short_packet;
|
| 1102 |
|
| 1103 |
|
| 1104 | if (ulen == 0 && inet6_is_jumbogram(skb))
|
| 1105 | ulen = skb->len;
|
| 1106 |
|
| 1107 | if (ulen < sizeof(*uh))
|
| 1108 | goto short_packet;
|
| 1109 |
|
| 1110 | if (ulen < skb->len) {
|
| 1111 | if (pskb_trim_rcsum(skb, ulen))
|
| 1112 | goto short_packet;
|
| 1113 |
|
| 1114 | saddr = &ipv6_hdr(skb)->saddr;
|
| 1115 | daddr = &ipv6_hdr(skb)->daddr;
|
| 1116 | uh = udp_hdr(skb);
|
| 1117 | }
|
| 1118 |
|
| 1119 | if (udp6_csum_init(skb, uh))
|
| 1120 | goto csum_error;
|
| 1121 |
|
| 1122 |
|
| 1123 | sk = inet6_steal_sock(net, skb, sizeof(struct udphdr), saddr, uh->source, daddr, uh->dest,
|
| 1124 | &refcounted, udp6_ehashfn);
|
| 1125 | if (IS_ERR(sk))
|
| 1126 | goto no_sk;
|
| 1127 |
|
| 1128 | if (sk) {
|
| 1129 | struct dst_entry *dst = skb_dst(skb);
|
| 1130 | int ret;
|
| 1131 |
|
| 1132 | if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst))
|
| 1133 | udp6_sk_rx_dst_set(sk, dst);
|
| 1134 |
|
| 1135 | if (!uh->check && !udp_get_no_check6_rx(sk)) {
|
| 1136 | if (refcounted)
|
| 1137 | sock_put(sk);
|
| 1138 | goto report_csum_error;
|
| 1139 | }
|
| 1140 |
|
| 1141 | ret = udp6_unicast_rcv_skb(sk, skb, uh);
|
| 1142 | if (refcounted)
|
| 1143 | sock_put(sk);
|
| 1144 | return ret;
|
| 1145 | }
|
| 1146 |
|
| 1147 | |
| 1148 | * Multicast receive code
|
| 1149 |
|
| 1150 | if (ipv6_addr_is_multicast(daddr))
|
| 1151 | return __udp6_lib_mcast_deliver(net, skb, saddr, daddr);
|
| 1152 |
|
| 1153 |
|
| 1154 | sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest);
|
| 1155 | if (sk) {
|
| 1156 | if (!uh->check && !udp_get_no_check6_rx(sk))
|
| 1157 | goto report_csum_error;
|
| 1158 | return udp6_unicast_rcv_skb(sk, skb, uh);
|
| 1159 | }
|
| 1160 | no_sk:
|
| 1161 | reason = SKB_DROP_REASON_NO_SOCKET;
|
| 1162 |
|
| 1163 | if (!uh->check)
|
| 1164 | goto report_csum_error;
|
| 1165 |
|
| 1166 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
|
| 1167 | goto discard;
|
| 1168 | nf_reset_ct(skb);
|
| 1169 |
|
| 1170 | if (udp_lib_checksum_complete(skb))
|
| 1171 | goto csum_error;
|
| 1172 |
|
| 1173 | __UDP6_INC_STATS(net, UDP_MIB_NOPORTS);
|
| 1174 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
|
| 1175 |
|
| 1176 | sk_skb_reason_drop(sk, skb, reason);
|
| 1177 | return 0;
|
| 1178 |
|
| 1179 | short_packet:
|
| 1180 | if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
|
| 1181 | reason = SKB_DROP_REASON_PKT_TOO_SMALL;
|
| 1182 | net_dbg_ratelimited("UDPv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
|
| 1183 | saddr, ntohs(uh->source),
|
| 1184 | ulen, skb->len,
|
| 1185 | daddr, ntohs(uh->dest));
|
| 1186 | goto discard;
|
| 1187 |
|
| 1188 | report_csum_error:
|
| 1189 | udp6_csum_zero_error(skb);
|
| 1190 | csum_error:
|
| 1191 | if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
|
| 1192 | reason = SKB_DROP_REASON_UDP_CSUM;
|
| 1193 | __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS);
|
| 1194 | discard:
|
| 1195 | __UDP6_INC_STATS(net, UDP_MIB_INERRORS);
|
| 1196 | sk_skb_reason_drop(sk, skb, reason);
|
| 1197 | return 0;
|
| 1198 | }
|
| 1199 |
|
| 1200 |
|
| 1201 | static struct sock *__udp6_lib_demux_lookup(struct net *net,
|
| 1202 | __be16 loc_port, const struct in6_addr *loc_addr,
|
| 1203 | __be16 rmt_port, const struct in6_addr *rmt_addr,
|
| 1204 | int dif, int sdif)
|
| 1205 | {
|
| 1206 | struct udp_table *udptable = net->ipv4.udp_table;
|
| 1207 | unsigned short hnum = ntohs(loc_port);
|
| 1208 | struct udp_hslot *hslot2;
|
| 1209 | unsigned int hash2;
|
| 1210 | __portpair ports;
|
| 1211 | struct sock *sk;
|
| 1212 |
|
| 1213 | hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
|
| 1214 | hslot2 = udp_hashslot2(udptable, hash2);
|
| 1215 | ports = INET_COMBINED_PORTS(rmt_port, hnum);
|
| 1216 |
|
| 1217 | udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
|
| 1218 | if (sk->sk_state == TCP_ESTABLISHED &&
|
| 1219 | inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif))
|
| 1220 | return sk;
|
| 1221 |
|
| 1222 | break;
|
| 1223 | }
|
| 1224 | return NULL;
|
| 1225 | }
|
| 1226 |
|
| 1227 | void udp_v6_early_demux(struct sk_buff *skb)
|
| 1228 | {
|
| 1229 | struct net *net = dev_net(skb->dev);
|
| 1230 | const struct udphdr *uh;
|
| 1231 | struct sock *sk;
|
| 1232 | struct dst_entry *dst;
|
| 1233 | int dif = skb->dev->ifindex;
|
| 1234 | int sdif = inet6_sdif(skb);
|
| 1235 |
|
| 1236 | if (!pskb_may_pull(skb, skb_transport_offset(skb) +
|
| 1237 | sizeof(struct udphdr)))
|
| 1238 | return;
|
| 1239 |
|
| 1240 | uh = udp_hdr(skb);
|
| 1241 |
|
| 1242 | if (skb->pkt_type == PACKET_HOST)
|
| 1243 | sk = __udp6_lib_demux_lookup(net, uh->dest,
|
| 1244 | &ipv6_hdr(skb)->daddr,
|
| 1245 | uh->source, &ipv6_hdr(skb)->saddr,
|
| 1246 | dif, sdif);
|
| 1247 | else
|
| 1248 | return;
|
| 1249 |
|
| 1250 | if (!sk)
|
| 1251 | return;
|
| 1252 |
|
| 1253 | skb->sk = sk;
|
| 1254 | DEBUG_NET_WARN_ON_ONCE(sk_is_refcounted(sk));
|
| 1255 | skb->destructor = sock_pfree;
|
| 1256 | dst = rcu_dereference(sk->sk_rx_dst);
|
| 1257 |
|
| 1258 | if (dst)
|
| 1259 | dst = dst_check(dst, sk->sk_rx_dst_cookie);
|
| 1260 | if (dst) {
|
| 1261 | |
| 1262 | * any place which wants to hold dst has to call
|
| 1263 | * dst_hold_safe()
|
| 1264 |
|
| 1265 | skb_dst_set_noref(skb, dst);
|
| 1266 | }
|
| 1267 | }
|
| 1268 |
|
| 1269 | |
| 1270 | * Throw away all pending data and cancel the corking. Socket is locked.
|
| 1271 |
|
| 1272 | static void udp_v6_flush_pending_frames(struct sock *sk)
|
| 1273 | {
|
| 1274 | struct udp_sock *up = udp_sk(sk);
|
| 1275 |
|
| 1276 | if (up->pending == AF_INET)
|
| 1277 | udp_flush_pending_frames(sk);
|
| 1278 | else if (up->pending) {
|
| 1279 | up->len = 0;
|
| 1280 | WRITE_ONCE(up->pending, 0);
|
| 1281 | ip6_flush_pending_frames(sk);
|
| 1282 | }
|
| 1283 | }
|
| 1284 |
|
| 1285 | static int udpv6_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
|
| 1286 | int addr_len)
|
| 1287 | {
|
| 1288 | if (addr_len < offsetofend(struct sockaddr, sa_family))
|
| 1289 | return -EINVAL;
|
| 1290 | |
| 1291 | * and intended to prevent BPF program called below from accessing
|
| 1292 | * bytes that are out of the bound specified by user in addr_len.
|
| 1293 |
|
| 1294 | if (uaddr->sa_family == AF_INET) {
|
| 1295 | if (ipv6_only_sock(sk))
|
| 1296 | return -EAFNOSUPPORT;
|
| 1297 | return udp_pre_connect(sk, uaddr, addr_len);
|
| 1298 | }
|
| 1299 |
|
| 1300 | if (addr_len < SIN6_LEN_RFC2133)
|
| 1301 | return -EINVAL;
|
| 1302 |
|
| 1303 | return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr, &addr_len);
|
| 1304 | }
|
| 1305 |
|
| 1306 | static int udpv6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
|
| 1307 | int addr_len)
|
| 1308 | {
|
| 1309 | int res;
|
| 1310 |
|
| 1311 | lock_sock(sk);
|
| 1312 | res = __ip6_datagram_connect(sk, uaddr, addr_len);
|
| 1313 | if (!res)
|
| 1314 | udp6_hash4(sk);
|
| 1315 | release_sock(sk);
|
| 1316 | return res;
|
| 1317 | }
|
| 1318 |
|
| 1319 | |
| 1320 | * udp6_hwcsum_outgoing - handle outgoing HW checksumming
|
| 1321 | * @sk: socket we are sending on
|
| 1322 | * @skb: sk_buff containing the filled-in UDP header
|
| 1323 | * (checksum field must be zeroed out)
|
| 1324 | * @saddr: source address
|
| 1325 | * @daddr: destination address
|
| 1326 | * @len: length of packet
|
| 1327 |
|
| 1328 | static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
|
| 1329 | const struct in6_addr *saddr,
|
| 1330 | const struct in6_addr *daddr, int len)
|
| 1331 | {
|
| 1332 | unsigned int offset;
|
| 1333 | struct udphdr *uh = udp_hdr(skb);
|
| 1334 | struct sk_buff *frags = skb_shinfo(skb)->frag_list;
|
| 1335 | __wsum csum = 0;
|
| 1336 |
|
| 1337 | if (!frags) {
|
| 1338 |
|
| 1339 | skb->csum_start = skb_transport_header(skb) - skb->head;
|
| 1340 | skb->csum_offset = offsetof(struct udphdr, check);
|
| 1341 | uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
|
| 1342 | } else {
|
| 1343 | |
| 1344 | * HW-checksum won't work as there are two or more
|
| 1345 | * fragments on the socket so that all csums of sk_buffs
|
| 1346 | * should be together
|
| 1347 |
|
| 1348 | offset = skb_transport_offset(skb);
|
| 1349 | skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
|
| 1350 | csum = skb->csum;
|
| 1351 |
|
| 1352 | skb->ip_summed = CHECKSUM_NONE;
|
| 1353 |
|
| 1354 | do {
|
| 1355 | csum = csum_add(csum, frags->csum);
|
| 1356 | } while ((frags = frags->next));
|
| 1357 |
|
| 1358 | uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
|
| 1359 | csum);
|
| 1360 | if (uh->check == 0)
|
| 1361 | uh->check = CSUM_MANGLED_0;
|
| 1362 | }
|
| 1363 | }
|
| 1364 |
|
| 1365 | |
| 1366 | * Sending
|
| 1367 |
|
| 1368 |
|
| 1369 | static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
|
| 1370 | struct inet_cork *cork)
|
| 1371 | {
|
| 1372 | struct sock *sk = skb->sk;
|
| 1373 | int offset, len, datalen;
|
| 1374 | struct udphdr *uh;
|
| 1375 | int err = 0;
|
| 1376 |
|
| 1377 | offset = skb_transport_offset(skb);
|
| 1378 | len = skb->len - offset;
|
| 1379 | datalen = len - sizeof(*uh);
|
| 1380 |
|
| 1381 | |
| 1382 | * Create a UDP header
|
| 1383 |
|
| 1384 | uh = udp_hdr(skb);
|
| 1385 | uh->source = fl6->fl6_sport;
|
| 1386 | uh->dest = fl6->fl6_dport;
|
| 1387 |
|
| 1388 | udp_set_len_short(uh, len);
|
| 1389 | uh->check = 0;
|
| 1390 |
|
| 1391 | if (cork->gso_size) {
|
| 1392 | const int hlen = skb_network_header_len(skb) +
|
| 1393 | sizeof(struct udphdr);
|
| 1394 |
|
| 1395 | if (hlen + min(datalen, cork->gso_size) > cork->fragsize) {
|
| 1396 | kfree_skb(skb);
|
| 1397 | return -EMSGSIZE;
|
| 1398 | }
|
| 1399 | if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) {
|
| 1400 | kfree_skb(skb);
|
| 1401 | return -EINVAL;
|
| 1402 | }
|
| 1403 | if (udp_get_no_check6_tx(sk)) {
|
| 1404 | kfree_skb(skb);
|
| 1405 | return -EINVAL;
|
| 1406 | }
|
| 1407 | if (dst_xfrm(skb_dst(skb))) {
|
| 1408 | kfree_skb(skb);
|
| 1409 | return -EIO;
|
| 1410 | }
|
| 1411 |
|
| 1412 | if (datalen > cork->gso_size) {
|
| 1413 | skb_shinfo(skb)->gso_size = cork->gso_size;
|
| 1414 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
|
| 1415 | skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
|
| 1416 | cork->gso_size);
|
| 1417 |
|
| 1418 |
|
| 1419 | goto csum_partial;
|
| 1420 | }
|
| 1421 | }
|
| 1422 |
|
| 1423 | if (udp_get_no_check6_tx(sk)) {
|
| 1424 | skb->ip_summed = CHECKSUM_NONE;
|
| 1425 | goto send;
|
| 1426 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
|
| 1427 | csum_partial:
|
| 1428 | udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
|
| 1429 | goto send;
|
| 1430 | }
|
| 1431 |
|
| 1432 |
|
| 1433 | uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
|
| 1434 | len, IPPROTO_UDP, udp_csum(skb));
|
| 1435 | if (uh->check == 0)
|
| 1436 | uh->check = CSUM_MANGLED_0;
|
| 1437 |
|
| 1438 | send:
|
| 1439 | err = ip6_send_skb(skb);
|
| 1440 | if (unlikely(err)) {
|
| 1441 | if (err == -ENOBUFS && !inet6_test_bit(RECVERR6, sk)) {
|
| 1442 | UDP6_INC_STATS(sock_net(sk), UDP_MIB_SNDBUFERRORS);
|
| 1443 | err = 0;
|
| 1444 | }
|
| 1445 | } else {
|
| 1446 | UDP6_INC_STATS(sock_net(sk), UDP_MIB_OUTDATAGRAMS);
|
| 1447 | }
|
| 1448 | return err;
|
| 1449 | }
|
| 1450 |
|
| 1451 | static int udp_v6_push_pending_frames(struct sock *sk)
|
| 1452 | {
|
| 1453 | struct sk_buff *skb;
|
| 1454 | struct udp_sock *up = udp_sk(sk);
|
| 1455 | int err = 0;
|
| 1456 |
|
| 1457 | if (up->pending == AF_INET)
|
| 1458 | return udp_push_pending_frames(sk);
|
| 1459 |
|
| 1460 | skb = ip6_finish_skb(sk);
|
| 1461 | if (!skb)
|
| 1462 | goto out;
|
| 1463 |
|
| 1464 | err = udp_v6_send_skb(skb, &inet_sk(sk)->cork.fl.u.ip6,
|
| 1465 | &inet_sk(sk)->cork.base);
|
| 1466 | out:
|
| 1467 | up->len = 0;
|
| 1468 | WRITE_ONCE(up->pending, 0);
|
| 1469 | return err;
|
| 1470 | }
|
| 1471 |
|
| 1472 | int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
|
| 1473 | {
|
| 1474 | int corkreq = udp_test_bit(CORK, sk) || msg->msg_flags & MSG_MORE;
|
| 1475 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
|
| 1476 | struct ipv6_txoptions *opt_to_free = NULL;
|
| 1477 | struct in6_addr *daddr, *final_p, final;
|
| 1478 | struct ip6_flowlabel *flowlabel = NULL;
|
| 1479 | struct inet_sock *inet = inet_sk(sk);
|
| 1480 | struct ipv6_pinfo *np = inet6_sk(sk);
|
| 1481 | struct ipv6_txoptions *opt = NULL;
|
| 1482 | struct udp_sock *up = udp_sk(sk);
|
| 1483 | struct ipv6_txoptions opt_space;
|
| 1484 | int addr_len = msg->msg_namelen;
|
| 1485 | struct inet_cork_full cork;
|
| 1486 | struct ipcm6_cookie ipc6;
|
| 1487 | bool connected = false;
|
| 1488 | struct dst_entry *dst;
|
| 1489 | struct flowi6 *fl6;
|
| 1490 | int ulen = len;
|
| 1491 | int err;
|
| 1492 |
|
| 1493 | fl6 = &cork.fl.u.ip6;
|
| 1494 | ipcm6_init_sk(&ipc6, sk);
|
| 1495 | ipc6.gso_size = READ_ONCE(up->gso_size);
|
| 1496 |
|
| 1497 |
|
| 1498 | if (sin6) {
|
| 1499 | if (addr_len < offsetof(struct sockaddr, sa_data))
|
| 1500 | return -EINVAL;
|
| 1501 |
|
| 1502 | switch (sin6->sin6_family) {
|
| 1503 | case AF_INET6:
|
| 1504 | if (addr_len < SIN6_LEN_RFC2133)
|
| 1505 | return -EINVAL;
|
| 1506 | daddr = &sin6->sin6_addr;
|
| 1507 | if (ipv6_addr_any(daddr) &&
|
| 1508 | ipv6_addr_v4mapped(&np->saddr))
|
| 1509 | ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
|
| 1510 | daddr);
|
| 1511 | break;
|
| 1512 | case AF_INET:
|
| 1513 | goto do_udp_sendmsg;
|
| 1514 | case AF_UNSPEC:
|
| 1515 | msg->msg_name = sin6 = NULL;
|
| 1516 | msg->msg_namelen = addr_len = 0;
|
| 1517 | daddr = NULL;
|
| 1518 | break;
|
| 1519 | default:
|
| 1520 | return -EINVAL;
|
| 1521 | }
|
| 1522 | } else if (!READ_ONCE(up->pending)) {
|
| 1523 | if (sk->sk_state != TCP_ESTABLISHED)
|
| 1524 | return -EDESTADDRREQ;
|
| 1525 | daddr = &sk->sk_v6_daddr;
|
| 1526 | } else
|
| 1527 | daddr = NULL;
|
| 1528 |
|
| 1529 | if (daddr) {
|
| 1530 | if (ipv6_addr_v4mapped(daddr)) {
|
| 1531 | struct sockaddr_in sin;
|
| 1532 | sin.sin_family = AF_INET;
|
| 1533 | sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
|
| 1534 | sin.sin_addr.s_addr = daddr->s6_addr32[3];
|
| 1535 | msg->msg_name = &sin;
|
| 1536 | msg->msg_namelen = sizeof(sin);
|
| 1537 | do_udp_sendmsg:
|
| 1538 | err = ipv6_only_sock(sk) ?
|
| 1539 | -ENETUNREACH : udp_sendmsg(sk, msg, len);
|
| 1540 | msg->msg_name = sin6;
|
| 1541 | msg->msg_namelen = addr_len;
|
| 1542 | return err;
|
| 1543 | }
|
| 1544 | }
|
| 1545 |
|
| 1546 | |
| 1547 | better check is made in ip6_append_data().
|
| 1548 |
|
| 1549 | if (len > INT_MAX - sizeof(struct udphdr))
|
| 1550 | return -EMSGSIZE;
|
| 1551 |
|
| 1552 | if (READ_ONCE(up->pending)) {
|
| 1553 | if (READ_ONCE(up->pending) == AF_INET)
|
| 1554 | return udp_sendmsg(sk, msg, len);
|
| 1555 | |
| 1556 | * There are pending frames.
|
| 1557 | * The socket lock must be held while it's corked.
|
| 1558 |
|
| 1559 | lock_sock(sk);
|
| 1560 | if (likely(up->pending)) {
|
| 1561 | if (unlikely(up->pending != AF_INET6)) {
|
| 1562 | release_sock(sk);
|
| 1563 | return -EAFNOSUPPORT;
|
| 1564 | }
|
| 1565 | dst = NULL;
|
| 1566 | goto do_append_data;
|
| 1567 | }
|
| 1568 | release_sock(sk);
|
| 1569 | }
|
| 1570 | ulen += sizeof(struct udphdr);
|
| 1571 |
|
| 1572 | memset(fl6, 0, sizeof(*fl6));
|
| 1573 |
|
| 1574 | if (sin6) {
|
| 1575 | if (sin6->sin6_port == 0)
|
| 1576 | return -EINVAL;
|
| 1577 |
|
| 1578 | fl6->fl6_dport = sin6->sin6_port;
|
| 1579 | daddr = &sin6->sin6_addr;
|
| 1580 |
|
| 1581 | if (inet6_test_bit(SNDFLOW, sk)) {
|
| 1582 | fl6->flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
|
| 1583 | if (fl6->flowlabel & IPV6_FLOWLABEL_MASK) {
|
| 1584 | flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
|
| 1585 | if (IS_ERR(flowlabel))
|
| 1586 | return -EINVAL;
|
| 1587 | }
|
| 1588 | }
|
| 1589 |
|
| 1590 | |
| 1591 | * Otherwise it will be difficult to maintain
|
| 1592 | * sk->sk_dst_cache.
|
| 1593 |
|
| 1594 | if (sk->sk_state == TCP_ESTABLISHED &&
|
| 1595 | ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
|
| 1596 | daddr = &sk->sk_v6_daddr;
|
| 1597 |
|
| 1598 | if (addr_len >= sizeof(struct sockaddr_in6) &&
|
| 1599 | sin6->sin6_scope_id &&
|
| 1600 | __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
|
| 1601 | fl6->flowi6_oif = sin6->sin6_scope_id;
|
| 1602 | } else {
|
| 1603 | if (sk->sk_state != TCP_ESTABLISHED)
|
| 1604 | return -EDESTADDRREQ;
|
| 1605 |
|
| 1606 | fl6->fl6_dport = inet->inet_dport;
|
| 1607 | daddr = &sk->sk_v6_daddr;
|
| 1608 | fl6->flowlabel = np->flow_label;
|
| 1609 | connected = true;
|
| 1610 | }
|
| 1611 |
|
| 1612 | if (!fl6->flowi6_oif)
|
| 1613 | fl6->flowi6_oif = READ_ONCE(sk->sk_bound_dev_if);
|
| 1614 |
|
| 1615 | if (!fl6->flowi6_oif)
|
| 1616 | fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
|
| 1617 |
|
| 1618 | fl6->flowi6_uid = sk_uid(sk);
|
| 1619 |
|
| 1620 | if (msg->msg_controllen) {
|
| 1621 | opt = &opt_space;
|
| 1622 | memset(opt, 0, sizeof(struct ipv6_txoptions));
|
| 1623 | opt->tot_len = sizeof(*opt);
|
| 1624 | ipc6.opt = opt;
|
| 1625 |
|
| 1626 | err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
|
| 1627 | if (err > 0) {
|
| 1628 | err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, fl6,
|
| 1629 | &ipc6);
|
| 1630 | connected = false;
|
| 1631 | }
|
| 1632 | if (err < 0) {
|
| 1633 | fl6_sock_release(flowlabel);
|
| 1634 | return err;
|
| 1635 | }
|
| 1636 | if ((fl6->flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
|
| 1637 | flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
|
| 1638 | if (IS_ERR(flowlabel))
|
| 1639 | return -EINVAL;
|
| 1640 | }
|
| 1641 | if (!(opt->opt_nflen|opt->opt_flen))
|
| 1642 | opt = NULL;
|
| 1643 | }
|
| 1644 | if (!opt) {
|
| 1645 | opt = txopt_get(np);
|
| 1646 | opt_to_free = opt;
|
| 1647 | }
|
| 1648 | if (flowlabel)
|
| 1649 | opt = fl6_merge_options(&opt_space, flowlabel, opt);
|
| 1650 | opt = ipv6_fixup_options(&opt_space, opt);
|
| 1651 | ipc6.opt = opt;
|
| 1652 |
|
| 1653 | fl6->flowi6_proto = IPPROTO_UDP;
|
| 1654 | fl6->flowi6_mark = ipc6.sockc.mark;
|
| 1655 | fl6->daddr = *daddr;
|
| 1656 | if (ipv6_addr_any(&fl6->saddr) && !ipv6_addr_any(&np->saddr))
|
| 1657 | fl6->saddr = np->saddr;
|
| 1658 | fl6->fl6_sport = inet->inet_sport;
|
| 1659 |
|
| 1660 | if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) {
|
| 1661 | err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
|
| 1662 | (struct sockaddr *)sin6,
|
| 1663 | &addr_len,
|
| 1664 | &fl6->saddr);
|
| 1665 | if (err)
|
| 1666 | goto out_no_dst;
|
| 1667 | if (sin6) {
|
| 1668 | if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
|
| 1669 | |
| 1670 | * IPv6. It's currently unsupported.
|
| 1671 |
|
| 1672 | err = -ENOTSUPP;
|
| 1673 | goto out_no_dst;
|
| 1674 | }
|
| 1675 | if (sin6->sin6_port == 0) {
|
| 1676 |
|
| 1677 | err = -EINVAL;
|
| 1678 | goto out_no_dst;
|
| 1679 | }
|
| 1680 | fl6->fl6_dport = sin6->sin6_port;
|
| 1681 | fl6->daddr = sin6->sin6_addr;
|
| 1682 | }
|
| 1683 | }
|
| 1684 |
|
| 1685 | if (ipv6_addr_any(&fl6->daddr))
|
| 1686 | fl6->daddr.s6_addr[15] = 0x1;
|
| 1687 |
|
| 1688 | final_p = fl6_update_dst(fl6, opt, &final);
|
| 1689 | if (final_p)
|
| 1690 | connected = false;
|
| 1691 |
|
| 1692 | if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) {
|
| 1693 | fl6->flowi6_oif = READ_ONCE(np->mcast_oif);
|
| 1694 | connected = false;
|
| 1695 | } else if (!fl6->flowi6_oif)
|
| 1696 | fl6->flowi6_oif = READ_ONCE(np->ucast_oif);
|
| 1697 |
|
| 1698 | security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
|
| 1699 |
|
| 1700 | fl6->flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6->flowlabel);
|
| 1701 |
|
| 1702 | dst = ip6_sk_dst_lookup_flow(sk, fl6, final_p, connected);
|
| 1703 | if (IS_ERR(dst)) {
|
| 1704 | err = PTR_ERR(dst);
|
| 1705 | dst = NULL;
|
| 1706 | goto out;
|
| 1707 | }
|
| 1708 |
|
| 1709 | if (ipc6.hlimit < 0)
|
| 1710 | ipc6.hlimit = ip6_sk_dst_hoplimit(np, fl6, dst);
|
| 1711 |
|
| 1712 | if (msg->msg_flags&MSG_CONFIRM)
|
| 1713 | goto do_confirm;
|
| 1714 | back_from_confirm:
|
| 1715 |
|
| 1716 |
|
| 1717 | if (!corkreq) {
|
| 1718 | struct sk_buff *skb;
|
| 1719 |
|
| 1720 | skb = ip6_make_skb(sk, ip_generic_getfrag, msg, ulen,
|
| 1721 | sizeof(struct udphdr), &ipc6,
|
| 1722 | dst_rt6_info(dst),
|
| 1723 | msg->msg_flags, &cork);
|
| 1724 | err = PTR_ERR(skb);
|
| 1725 | if (!IS_ERR_OR_NULL(skb))
|
| 1726 | err = udp_v6_send_skb(skb, fl6, &cork.base);
|
| 1727 |
|
| 1728 | goto out_no_dst;
|
| 1729 | }
|
| 1730 |
|
| 1731 | lock_sock(sk);
|
| 1732 | if (unlikely(up->pending)) {
|
| 1733 |
|
| 1734 |
|
| 1735 | release_sock(sk);
|
| 1736 |
|
| 1737 | net_dbg_ratelimited("udp cork app bug 2\n");
|
| 1738 | err = -EINVAL;
|
| 1739 | goto out;
|
| 1740 | }
|
| 1741 |
|
| 1742 | WRITE_ONCE(up->pending, AF_INET6);
|
| 1743 |
|
| 1744 | do_append_data:
|
| 1745 | up->len += ulen;
|
| 1746 | err = ip6_append_data(sk, ip_generic_getfrag, msg, ulen,
|
| 1747 | sizeof(struct udphdr), &ipc6, fl6,
|
| 1748 | dst_rt6_info(dst),
|
| 1749 | corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
|
| 1750 | if (err)
|
| 1751 | udp_v6_flush_pending_frames(sk);
|
| 1752 | else if (!corkreq)
|
| 1753 | err = udp_v6_push_pending_frames(sk);
|
| 1754 | else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
|
| 1755 | WRITE_ONCE(up->pending, 0);
|
| 1756 |
|
| 1757 | if (err > 0)
|
| 1758 | err = inet6_test_bit(RECVERR6, sk) ? net_xmit_errno(err) : 0;
|
| 1759 | release_sock(sk);
|
| 1760 |
|
| 1761 | out:
|
| 1762 | dst_release(dst);
|
| 1763 | out_no_dst:
|
| 1764 | fl6_sock_release(flowlabel);
|
| 1765 | txopt_put(opt_to_free);
|
| 1766 | if (!err)
|
| 1767 | return len;
|
| 1768 | |
| 1769 | * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
|
| 1770 | * ENOBUFS might not be good (it's not tunable per se), but otherwise
|
| 1771 | * we don't have a good statistic (IpOutDiscards but it can be too many
|
| 1772 | * things). We could add another new stat but at least for now that
|
| 1773 | * seems like overkill.
|
| 1774 |
|
| 1775 | if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
|
| 1776 | UDP6_INC_STATS(sock_net(sk), UDP_MIB_SNDBUFERRORS);
|
| 1777 |
|
| 1778 | return err;
|
| 1779 |
|
| 1780 | do_confirm:
|
| 1781 | if (msg->msg_flags & MSG_PROBE)
|
| 1782 | dst_confirm_neigh(dst, &fl6->daddr);
|
| 1783 | if (!(msg->msg_flags&MSG_PROBE) || len)
|
| 1784 | goto back_from_confirm;
|
| 1785 | err = 0;
|
| 1786 | goto out;
|
| 1787 | }
|
| 1788 | EXPORT_SYMBOL(udpv6_sendmsg);
|
| 1789 |
|
| 1790 | static void udpv6_splice_eof(struct socket *sock)
|
| 1791 | {
|
| 1792 | struct sock *sk = sock->sk;
|
| 1793 | struct udp_sock *up = udp_sk(sk);
|
| 1794 |
|
| 1795 | if (!READ_ONCE(up->pending) || udp_test_bit(CORK, sk))
|
| 1796 | return;
|
| 1797 |
|
| 1798 | lock_sock(sk);
|
| 1799 | if (up->pending && !udp_test_bit(CORK, sk))
|
| 1800 | udp_v6_push_pending_frames(sk);
|
| 1801 | release_sock(sk);
|
| 1802 | }
|
| 1803 |
|
| 1804 | static void udpv6_destroy_sock(struct sock *sk)
|
| 1805 | {
|
| 1806 | struct udp_sock *up = udp_sk(sk);
|
| 1807 | lock_sock(sk);
|
| 1808 |
|
| 1809 |
|
| 1810 | sock_set_flag(sk, SOCK_DEAD);
|
| 1811 | udp_v6_flush_pending_frames(sk);
|
| 1812 | release_sock(sk);
|
| 1813 |
|
| 1814 | if (static_branch_unlikely(&udpv6_encap_needed_key)) {
|
| 1815 | if (up->encap_type) {
|
| 1816 | void (*encap_destroy)(struct sock *sk);
|
| 1817 | encap_destroy = READ_ONCE(up->encap_destroy);
|
| 1818 | if (encap_destroy)
|
| 1819 | encap_destroy(sk);
|
| 1820 | }
|
| 1821 | if (udp_test_bit(ENCAP_ENABLED, sk)) {
|
| 1822 | static_branch_dec(&udpv6_encap_needed_key);
|
| 1823 | udp_encap_disable();
|
| 1824 | udp_tunnel_cleanup_gro(sk);
|
| 1825 | }
|
| 1826 | }
|
| 1827 | }
|
| 1828 |
|
| 1829 | |
| 1830 | * Socket option code for UDP
|
| 1831 |
|
| 1832 | static int udpv6_setsockopt(struct sock *sk, int level, int optname,
|
| 1833 | sockptr_t optval, unsigned int optlen)
|
| 1834 | {
|
| 1835 | if (level == SOL_UDP || level == SOL_SOCKET)
|
| 1836 | return udp_lib_setsockopt(sk, level, optname,
|
| 1837 | optval, optlen,
|
| 1838 | udp_v6_push_pending_frames);
|
| 1839 | return ipv6_setsockopt(sk, level, optname, optval, optlen);
|
| 1840 | }
|
| 1841 |
|
| 1842 | static int udpv6_getsockopt(struct sock *sk, int level, int optname,
|
| 1843 | char __user *optval, int __user *optlen)
|
| 1844 | {
|
| 1845 | sockopt_t opt;
|
| 1846 | int err;
|
| 1847 |
|
| 1848 | if (level != SOL_UDP)
|
| 1849 | return ipv6_getsockopt(sk, level, optname, optval, optlen);
|
| 1850 |
|
| 1851 | err = sockopt_init_user(&opt, optval, optlen);
|
| 1852 | if (err)
|
| 1853 | return err;
|
| 1854 |
|
| 1855 | err = udp_lib_getsockopt(sk, level, optname, &opt);
|
| 1856 | if (err)
|
| 1857 | return err;
|
| 1858 | if (put_user(opt.optlen, optlen))
|
| 1859 | return -EFAULT;
|
| 1860 | return 0;
|
| 1861 | }
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 | #ifdef CONFIG_PROC_FS
|
| 1866 | static int udp6_seq_show(struct seq_file *seq, void *v)
|
| 1867 | {
|
| 1868 | if (v == SEQ_START_TOKEN) {
|
| 1869 | seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
|
| 1870 | } else {
|
| 1871 | int bucket = ((struct udp_iter_state *)seq->private)->bucket;
|
| 1872 | const struct inet_sock *inet = inet_sk((const struct sock *)v);
|
| 1873 | __u16 srcp = ntohs(inet->inet_sport);
|
| 1874 | __u16 destp = ntohs(inet->inet_dport);
|
| 1875 | __ip6_dgram_sock_seq_show(seq, v, srcp, destp,
|
| 1876 | udp_rqueue_get(v), bucket);
|
| 1877 | }
|
| 1878 | return 0;
|
| 1879 | }
|
| 1880 |
|
| 1881 | static const struct seq_operations udp6_seq_ops = {
|
| 1882 | .start = udp_seq_start,
|
| 1883 | .next = udp_seq_next,
|
| 1884 | .stop = udp_seq_stop,
|
| 1885 | .show = udp6_seq_show,
|
| 1886 | };
|
| 1887 |
|
| 1888 | static struct udp_seq_afinfo udp6_seq_afinfo = {
|
| 1889 | .family = AF_INET6,
|
| 1890 | };
|
| 1891 |
|
| 1892 | int __net_init udp6_proc_init(struct net *net)
|
| 1893 | {
|
| 1894 | if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
|
| 1895 | sizeof(struct udp_iter_state), &udp6_seq_afinfo))
|
| 1896 | return -ENOMEM;
|
| 1897 | return 0;
|
| 1898 | }
|
| 1899 |
|
| 1900 | void udp6_proc_exit(struct net *net)
|
| 1901 | {
|
| 1902 | remove_proc_entry("udp6", net->proc_net);
|
| 1903 | }
|
| 1904 | #endif
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 | struct proto udpv6_prot = {
|
| 1909 | .name = "UDPv6",
|
| 1910 | .owner = THIS_MODULE,
|
| 1911 | .close = udp_lib_close,
|
| 1912 | .pre_connect = udpv6_pre_connect,
|
| 1913 | .connect = udpv6_connect,
|
| 1914 | .disconnect = udp_disconnect,
|
| 1915 | .ioctl = udp_ioctl,
|
| 1916 | .init = udpv6_init_sock,
|
| 1917 | .destroy = udpv6_destroy_sock,
|
| 1918 | .setsockopt = udpv6_setsockopt,
|
| 1919 | .getsockopt = udpv6_getsockopt,
|
| 1920 | .sendmsg = udpv6_sendmsg,
|
| 1921 | .recvmsg = udpv6_recvmsg,
|
| 1922 | .splice_eof = udpv6_splice_eof,
|
| 1923 | .release_cb = ip6_datagram_release_cb,
|
| 1924 | .hash = udp_lib_hash,
|
| 1925 | .unhash = udp_lib_unhash,
|
| 1926 | .rehash = udp_v6_rehash,
|
| 1927 | .get_port = udp_v6_get_port,
|
| 1928 | .put_port = udp_lib_unhash,
|
| 1929 | #ifdef CONFIG_BPF_SYSCALL
|
| 1930 | .psock_update_sk_prot = udp_bpf_update_proto,
|
| 1931 | #endif
|
| 1932 |
|
| 1933 | .memory_allocated = &net_aligned_data.udp_memory_allocated,
|
| 1934 | .per_cpu_fw_alloc = &udp_memory_per_cpu_fw_alloc,
|
| 1935 |
|
| 1936 | .sysctl_mem = sysctl_udp_mem,
|
| 1937 | .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
|
| 1938 | .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
|
| 1939 | .obj_size = sizeof(struct udp6_sock),
|
| 1940 | .ipv6_pinfo_offset = offsetof(struct udp6_sock, inet6),
|
| 1941 | .diag_destroy = udp_abort,
|
| 1942 | };
|
| 1943 |
|
| 1944 | static struct inet_protosw udpv6_protosw = {
|
| 1945 | .type = SOCK_DGRAM,
|
| 1946 | .protocol = IPPROTO_UDP,
|
| 1947 | .prot = &udpv6_prot,
|
| 1948 | .ops = &inet6_dgram_ops,
|
| 1949 | .flags = INET_PROTOSW_PERMANENT,
|
| 1950 | };
|
| 1951 |
|
| 1952 | int __init udpv6_init(void)
|
| 1953 | {
|
| 1954 | int ret;
|
| 1955 |
|
| 1956 | net_hotdata.udpv6_protocol = (struct inet6_protocol) {
|
| 1957 | .handler = udpv6_rcv,
|
| 1958 | .err_handler = udpv6_err,
|
| 1959 | .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
|
| 1960 | };
|
| 1961 | ret = inet6_add_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
|
| 1962 | if (ret)
|
| 1963 | goto out;
|
| 1964 |
|
| 1965 | ret = inet6_register_protosw(&udpv6_protosw);
|
| 1966 | if (ret)
|
| 1967 | goto out_udpv6_protocol;
|
| 1968 | out:
|
| 1969 | return ret;
|
| 1970 |
|
| 1971 | out_udpv6_protocol:
|
| 1972 | inet6_del_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
|
| 1973 | goto out;
|
| 1974 | }
|
| 1975 |
|
| 1976 | void udpv6_exit(void)
|
| 1977 | {
|
| 1978 | inet6_unregister_protosw(&udpv6_protosw);
|
| 1979 | inet6_del_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
|
| 1980 | }
|