1 /* 2 * UDP over IPv6 3 * Linux INET6 implementation 4 * 5 * Authors: 6 * Pedro Roque <roque@di.fc.ul.pt> 7 * 8 * Based on linux/ipv4/udp.c 9 * 10 * Fixes: 11 * Hideaki YOSHIFUJI : sin6_scope_id support 12 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 13 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind 14 * a single port at the same time. 15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data 16 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file. 17 * 18 * This program is free software; you can redistribute it and/or 19 * modify it under the terms of the GNU General Public License 20 * as published by the Free Software Foundation; either version 21 * 2 of the License, or (at your option) any later version. 22 */ 23 24 #include <linux/errno.h> 25 #include <linux/types.h> 26 #include <linux/socket.h> 27 #include <linux/sockios.h> 28 #include <linux/net.h> 29 #include <linux/in6.h> 30 #include <linux/netdevice.h> 31 #include <linux/if_arp.h> 32 #include <linux/ipv6.h> 33 #include <linux/icmpv6.h> 34 #include <linux/init.h> 35 #include <linux/module.h> 36 #include <linux/skbuff.h> 37 #include <asm/uaccess.h> 38 39 #include <net/ndisc.h> 40 #include <net/protocol.h> 41 #include <net/transp_v6.h> 42 #include <net/ip6_route.h> 43 #include <net/raw.h> 44 #include <net/tcp_states.h> 45 #include <net/ip6_checksum.h> 46 #include <net/xfrm.h> 47 48 #include <linux/proc_fs.h> 49 #include <linux/seq_file.h> 50 #include "udp_impl.h" 51 52 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2) 53 { 54 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr; 55 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2); 56 int sk_ipv6only = ipv6_only_sock(sk); 57 int sk2_ipv6only = inet_v6_ipv6only(sk2); 58 int addr_type = ipv6_addr_type(sk_rcv_saddr6); 59 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED; 60 61 /* if both are mapped, treat as IPv4 */ 62 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED) 63 return ipv4_rcv_saddr_equal(sk, sk2); 64 65 if (addr_type2 == IPV6_ADDR_ANY && 66 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED)) 67 return 1; 68 69 if (addr_type == IPV6_ADDR_ANY && 70 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED)) 71 return 1; 72 73 if (sk2_rcv_saddr6 && 74 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6)) 75 return 1; 76 77 return 0; 78 } 79 80 int udp_v6_get_port(struct sock *sk, unsigned short snum) 81 { 82 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal); 83 } 84 85 static inline int compute_score(struct sock *sk, struct net *net, 86 unsigned short hnum, 87 struct in6_addr *saddr, __be16 sport, 88 struct in6_addr *daddr, __be16 dport, 89 int dif) 90 { 91 int score = -1; 92 93 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && 94 sk->sk_family == PF_INET6) { 95 struct ipv6_pinfo *np = inet6_sk(sk); 96 struct inet_sock *inet = inet_sk(sk); 97 98 score = 0; 99 if (inet->dport) { 100 if (inet->dport != sport) 101 return -1; 102 score++; 103 } 104 if (!ipv6_addr_any(&np->rcv_saddr)) { 105 if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) 106 return -1; 107 score++; 108 } 109 if (!ipv6_addr_any(&np->daddr)) { 110 if (!ipv6_addr_equal(&np->daddr, saddr)) 111 return -1; 112 score++; 113 } 114 if (sk->sk_bound_dev_if) { 115 if (sk->sk_bound_dev_if != dif) 116 return -1; 117 score++; 118 } 119 } 120 return score; 121 } 122 123 static struct sock *__udp6_lib_lookup(struct net *net, 124 struct in6_addr *saddr, __be16 sport, 125 struct in6_addr *daddr, __be16 dport, 126 int dif, struct udp_table *udptable) 127 { 128 struct sock *sk, *result; 129 struct hlist_nulls_node *node; 130 unsigned short hnum = ntohs(dport); 131 unsigned int hash = udp_hashfn(net, hnum); 132 struct udp_hslot *hslot = &udptable->hash[hash]; 133 int score, badness; 134 135 rcu_read_lock(); 136 begin: 137 result = NULL; 138 badness = -1; 139 sk_nulls_for_each_rcu(sk, node, &hslot->head) { 140 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif); 141 if (score > badness) { 142 result = sk; 143 badness = score; 144 } 145 } 146 /* 147 * if the nulls value we got at the end of this lookup is 148 * not the expected one, we must restart lookup. 149 * We probably met an item that was moved to another chain. 150 */ 151 if (get_nulls_value(node) != hash) 152 goto begin; 153 154 if (result) { 155 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt))) 156 result = NULL; 157 else if (unlikely(compute_score(result, net, hnum, saddr, sport, 158 daddr, dport, dif) < badness)) { 159 sock_put(result); 160 goto begin; 161 } 162 } 163 rcu_read_unlock(); 164 return result; 165 } 166 167 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 168 __be16 sport, __be16 dport, 169 struct udp_table *udptable) 170 { 171 struct sock *sk; 172 struct ipv6hdr *iph = ipv6_hdr(skb); 173 174 if (unlikely(sk = skb_steal_sock(skb))) 175 return sk; 176 else 177 return __udp6_lib_lookup(dev_net(skb->dst->dev), &iph->saddr, sport, 178 &iph->daddr, dport, inet6_iif(skb), 179 udptable); 180 } 181 182 /* 183 * This should be easy, if there is something there we 184 * return it, otherwise we block. 185 */ 186 187 int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, 188 struct msghdr *msg, size_t len, 189 int noblock, int flags, int *addr_len) 190 { 191 struct ipv6_pinfo *np = inet6_sk(sk); 192 struct inet_sock *inet = inet_sk(sk); 193 struct sk_buff *skb; 194 unsigned int ulen, copied; 195 int peeked; 196 int err; 197 int is_udplite = IS_UDPLITE(sk); 198 int is_udp4; 199 200 if (addr_len) 201 *addr_len=sizeof(struct sockaddr_in6); 202 203 if (flags & MSG_ERRQUEUE) 204 return ipv6_recv_error(sk, msg, len); 205 206 try_again: 207 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 208 &peeked, &err); 209 if (!skb) 210 goto out; 211 212 ulen = skb->len - sizeof(struct udphdr); 213 copied = len; 214 if (copied > ulen) 215 copied = ulen; 216 else if (copied < ulen) 217 msg->msg_flags |= MSG_TRUNC; 218 219 is_udp4 = (skb->protocol == htons(ETH_P_IP)); 220 221 /* 222 * If checksum is needed at all, try to do it while copying the 223 * data. If the data is truncated, or if we only want a partial 224 * coverage checksum (UDP-Lite), do it before the copy. 225 */ 226 227 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 228 if (udp_lib_checksum_complete(skb)) 229 goto csum_copy_err; 230 } 231 232 if (skb_csum_unnecessary(skb)) 233 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 234 msg->msg_iov, copied ); 235 else { 236 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 237 if (err == -EINVAL) 238 goto csum_copy_err; 239 } 240 if (err) 241 goto out_free; 242 243 if (!peeked) { 244 if (is_udp4) 245 UDP_INC_STATS_USER(sock_net(sk), 246 UDP_MIB_INDATAGRAMS, is_udplite); 247 else 248 UDP6_INC_STATS_USER(sock_net(sk), 249 UDP_MIB_INDATAGRAMS, is_udplite); 250 } 251 252 sock_recv_timestamp(msg, sk, skb); 253 254 /* Copy the address. */ 255 if (msg->msg_name) { 256 struct sockaddr_in6 *sin6; 257 258 sin6 = (struct sockaddr_in6 *) msg->msg_name; 259 sin6->sin6_family = AF_INET6; 260 sin6->sin6_port = udp_hdr(skb)->source; 261 sin6->sin6_flowinfo = 0; 262 sin6->sin6_scope_id = 0; 263 264 if (is_udp4) 265 ipv6_addr_set(&sin6->sin6_addr, 0, 0, 266 htonl(0xffff), ip_hdr(skb)->saddr); 267 else { 268 ipv6_addr_copy(&sin6->sin6_addr, 269 &ipv6_hdr(skb)->saddr); 270 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) 271 sin6->sin6_scope_id = IP6CB(skb)->iif; 272 } 273 274 } 275 if (is_udp4) { 276 if (inet->cmsg_flags) 277 ip_cmsg_recv(msg, skb); 278 } else { 279 if (np->rxopt.all) 280 datagram_recv_ctl(sk, msg, skb); 281 } 282 283 err = copied; 284 if (flags & MSG_TRUNC) 285 err = ulen; 286 287 out_free: 288 lock_sock(sk); 289 skb_free_datagram(sk, skb); 290 release_sock(sk); 291 out: 292 return err; 293 294 csum_copy_err: 295 lock_sock(sk); 296 if (!skb_kill_datagram(sk, skb, flags)) { 297 if (is_udp4) 298 UDP_INC_STATS_USER(sock_net(sk), 299 UDP_MIB_INERRORS, is_udplite); 300 else 301 UDP6_INC_STATS_USER(sock_net(sk), 302 UDP_MIB_INERRORS, is_udplite); 303 } 304 release_sock(sk); 305 306 if (flags & MSG_DONTWAIT) 307 return -EAGAIN; 308 goto try_again; 309 } 310 311 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 312 int type, int code, int offset, __be32 info, 313 struct udp_table *udptable) 314 { 315 struct ipv6_pinfo *np; 316 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data; 317 struct in6_addr *saddr = &hdr->saddr; 318 struct in6_addr *daddr = &hdr->daddr; 319 struct udphdr *uh = (struct udphdr*)(skb->data+offset); 320 struct sock *sk; 321 int err; 322 323 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest, 324 saddr, uh->source, inet6_iif(skb), udptable); 325 if (sk == NULL) 326 return; 327 328 np = inet6_sk(sk); 329 330 if (!icmpv6_err_convert(type, code, &err) && !np->recverr) 331 goto out; 332 333 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr) 334 goto out; 335 336 if (np->recverr) 337 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 338 339 sk->sk_err = err; 340 sk->sk_error_report(sk); 341 out: 342 sock_put(sk); 343 } 344 345 static __inline__ void udpv6_err(struct sk_buff *skb, 346 struct inet6_skb_parm *opt, int type, 347 int code, int offset, __be32 info ) 348 { 349 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); 350 } 351 352 int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 353 { 354 struct udp_sock *up = udp_sk(sk); 355 int rc; 356 int is_udplite = IS_UDPLITE(sk); 357 358 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) 359 goto drop; 360 361 /* 362 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 363 */ 364 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 365 366 if (up->pcrlen == 0) { /* full coverage was set */ 367 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage" 368 " %d while full coverage %d requested\n", 369 UDP_SKB_CB(skb)->cscov, skb->len); 370 goto drop; 371 } 372 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 373 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d " 374 "too small, need min %d\n", 375 UDP_SKB_CB(skb)->cscov, up->pcrlen); 376 goto drop; 377 } 378 } 379 380 if (sk->sk_filter) { 381 if (udp_lib_checksum_complete(skb)) 382 goto drop; 383 } 384 385 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { 386 /* Note that an ENOMEM error is charged twice */ 387 if (rc == -ENOMEM) { 388 UDP6_INC_STATS_BH(sock_net(sk), 389 UDP_MIB_RCVBUFERRORS, is_udplite); 390 atomic_inc(&sk->sk_drops); 391 } 392 goto drop; 393 } 394 395 return 0; 396 drop: 397 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 398 kfree_skb(skb); 399 return -1; 400 } 401 402 static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk, 403 __be16 loc_port, struct in6_addr *loc_addr, 404 __be16 rmt_port, struct in6_addr *rmt_addr, 405 int dif) 406 { 407 struct hlist_nulls_node *node; 408 struct sock *s = sk; 409 unsigned short num = ntohs(loc_port); 410 411 sk_nulls_for_each_from(s, node) { 412 struct inet_sock *inet = inet_sk(s); 413 414 if (!net_eq(sock_net(s), net)) 415 continue; 416 417 if (s->sk_hash == num && s->sk_family == PF_INET6) { 418 struct ipv6_pinfo *np = inet6_sk(s); 419 if (inet->dport) { 420 if (inet->dport != rmt_port) 421 continue; 422 } 423 if (!ipv6_addr_any(&np->daddr) && 424 !ipv6_addr_equal(&np->daddr, rmt_addr)) 425 continue; 426 427 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif) 428 continue; 429 430 if (!ipv6_addr_any(&np->rcv_saddr)) { 431 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr)) 432 continue; 433 } 434 if (!inet6_mc_check(s, loc_addr, rmt_addr)) 435 continue; 436 return s; 437 } 438 } 439 return NULL; 440 } 441 442 /* 443 * Note: called only from the BH handler context, 444 * so we don't need to lock the hashes. 445 */ 446 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 447 struct in6_addr *saddr, struct in6_addr *daddr, 448 struct udp_table *udptable) 449 { 450 struct sock *sk, *sk2; 451 const struct udphdr *uh = udp_hdr(skb); 452 struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))]; 453 int dif; 454 455 spin_lock(&hslot->lock); 456 sk = sk_nulls_head(&hslot->head); 457 dif = inet6_iif(skb); 458 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); 459 if (!sk) { 460 kfree_skb(skb); 461 goto out; 462 } 463 464 sk2 = sk; 465 while ((sk2 = udp_v6_mcast_next(net, sk_nulls_next(sk2), uh->dest, daddr, 466 uh->source, saddr, dif))) { 467 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC); 468 if (buff) { 469 bh_lock_sock(sk2); 470 if (!sock_owned_by_user(sk2)) 471 udpv6_queue_rcv_skb(sk2, buff); 472 else 473 sk_add_backlog(sk2, buff); 474 bh_unlock_sock(sk2); 475 } 476 } 477 bh_lock_sock(sk); 478 if (!sock_owned_by_user(sk)) 479 udpv6_queue_rcv_skb(sk, skb); 480 else 481 sk_add_backlog(sk, skb); 482 bh_unlock_sock(sk); 483 out: 484 spin_unlock(&hslot->lock); 485 return 0; 486 } 487 488 static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, 489 int proto) 490 { 491 int err; 492 493 UDP_SKB_CB(skb)->partial_cov = 0; 494 UDP_SKB_CB(skb)->cscov = skb->len; 495 496 if (proto == IPPROTO_UDPLITE) { 497 err = udplite_checksum_init(skb, uh); 498 if (err) 499 return err; 500 } 501 502 if (uh->check == 0) { 503 /* RFC 2460 section 8.1 says that we SHOULD log 504 this error. Well, it is reasonable. 505 */ 506 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n"); 507 return 1; 508 } 509 if (skb->ip_summed == CHECKSUM_COMPLETE && 510 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, 511 skb->len, proto, skb->csum)) 512 skb->ip_summed = CHECKSUM_UNNECESSARY; 513 514 if (!skb_csum_unnecessary(skb)) 515 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 516 &ipv6_hdr(skb)->daddr, 517 skb->len, proto, 0)); 518 519 return 0; 520 } 521 522 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 523 int proto) 524 { 525 struct sock *sk; 526 struct udphdr *uh; 527 struct net_device *dev = skb->dev; 528 struct in6_addr *saddr, *daddr; 529 u32 ulen = 0; 530 struct net *net = dev_net(skb->dev); 531 532 if (!pskb_may_pull(skb, sizeof(struct udphdr))) 533 goto short_packet; 534 535 saddr = &ipv6_hdr(skb)->saddr; 536 daddr = &ipv6_hdr(skb)->daddr; 537 uh = udp_hdr(skb); 538 539 ulen = ntohs(uh->len); 540 if (ulen > skb->len) 541 goto short_packet; 542 543 if (proto == IPPROTO_UDP) { 544 /* UDP validates ulen. */ 545 546 /* Check for jumbo payload */ 547 if (ulen == 0) 548 ulen = skb->len; 549 550 if (ulen < sizeof(*uh)) 551 goto short_packet; 552 553 if (ulen < skb->len) { 554 if (pskb_trim_rcsum(skb, ulen)) 555 goto short_packet; 556 saddr = &ipv6_hdr(skb)->saddr; 557 daddr = &ipv6_hdr(skb)->daddr; 558 uh = udp_hdr(skb); 559 } 560 } 561 562 if (udp6_csum_init(skb, uh, proto)) 563 goto discard; 564 565 /* 566 * Multicast receive code 567 */ 568 if (ipv6_addr_is_multicast(daddr)) 569 return __udp6_lib_mcast_deliver(net, skb, 570 saddr, daddr, udptable); 571 572 /* Unicast */ 573 574 /* 575 * check socket cache ... must talk to Alan about his plans 576 * for sock caches... i'll skip this for now. 577 */ 578 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 579 580 if (sk == NULL) { 581 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 582 goto discard; 583 584 if (udp_lib_checksum_complete(skb)) 585 goto discard; 586 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, 587 proto == IPPROTO_UDPLITE); 588 589 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev); 590 591 kfree_skb(skb); 592 return 0; 593 } 594 595 /* deliver */ 596 597 bh_lock_sock(sk); 598 if (!sock_owned_by_user(sk)) 599 udpv6_queue_rcv_skb(sk, skb); 600 else 601 sk_add_backlog(sk, skb); 602 bh_unlock_sock(sk); 603 sock_put(sk); 604 return 0; 605 606 short_packet: 607 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n", 608 proto == IPPROTO_UDPLITE ? "-Lite" : "", 609 ulen, skb->len); 610 611 discard: 612 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 613 kfree_skb(skb); 614 return 0; 615 } 616 617 static __inline__ int udpv6_rcv(struct sk_buff *skb) 618 { 619 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); 620 } 621 622 /* 623 * Throw away all pending data and cancel the corking. Socket is locked. 624 */ 625 static void udp_v6_flush_pending_frames(struct sock *sk) 626 { 627 struct udp_sock *up = udp_sk(sk); 628 629 if (up->pending == AF_INET) 630 udp_flush_pending_frames(sk); 631 else if (up->pending) { 632 up->len = 0; 633 up->pending = 0; 634 ip6_flush_pending_frames(sk); 635 } 636 } 637 638 /* 639 * Sending 640 */ 641 642 static int udp_v6_push_pending_frames(struct sock *sk) 643 { 644 struct sk_buff *skb; 645 struct udphdr *uh; 646 struct udp_sock *up = udp_sk(sk); 647 struct inet_sock *inet = inet_sk(sk); 648 struct flowi *fl = &inet->cork.fl; 649 int err = 0; 650 int is_udplite = IS_UDPLITE(sk); 651 __wsum csum = 0; 652 653 /* Grab the skbuff where UDP header space exists. */ 654 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 655 goto out; 656 657 /* 658 * Create a UDP header 659 */ 660 uh = udp_hdr(skb); 661 uh->source = fl->fl_ip_sport; 662 uh->dest = fl->fl_ip_dport; 663 uh->len = htons(up->len); 664 uh->check = 0; 665 666 if (is_udplite) 667 csum = udplite_csum_outgoing(sk, skb); 668 else 669 csum = udp_csum_outgoing(sk, skb); 670 671 /* add protocol-dependent pseudo-header */ 672 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst, 673 up->len, fl->proto, csum ); 674 if (uh->check == 0) 675 uh->check = CSUM_MANGLED_0; 676 677 err = ip6_push_pending_frames(sk); 678 out: 679 up->len = 0; 680 up->pending = 0; 681 if (!err) 682 UDP6_INC_STATS_USER(sock_net(sk), 683 UDP_MIB_OUTDATAGRAMS, is_udplite); 684 return err; 685 } 686 687 int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, 688 struct msghdr *msg, size_t len) 689 { 690 struct ipv6_txoptions opt_space; 691 struct udp_sock *up = udp_sk(sk); 692 struct inet_sock *inet = inet_sk(sk); 693 struct ipv6_pinfo *np = inet6_sk(sk); 694 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name; 695 struct in6_addr *daddr, *final_p = NULL, final; 696 struct ipv6_txoptions *opt = NULL; 697 struct ip6_flowlabel *flowlabel = NULL; 698 struct flowi fl; 699 struct dst_entry *dst; 700 int addr_len = msg->msg_namelen; 701 int ulen = len; 702 int hlimit = -1; 703 int tclass = -1; 704 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 705 int err; 706 int connected = 0; 707 int is_udplite = IS_UDPLITE(sk); 708 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 709 710 /* destination address check */ 711 if (sin6) { 712 if (addr_len < offsetof(struct sockaddr, sa_data)) 713 return -EINVAL; 714 715 switch (sin6->sin6_family) { 716 case AF_INET6: 717 if (addr_len < SIN6_LEN_RFC2133) 718 return -EINVAL; 719 daddr = &sin6->sin6_addr; 720 break; 721 case AF_INET: 722 goto do_udp_sendmsg; 723 case AF_UNSPEC: 724 msg->msg_name = sin6 = NULL; 725 msg->msg_namelen = addr_len = 0; 726 daddr = NULL; 727 break; 728 default: 729 return -EINVAL; 730 } 731 } else if (!up->pending) { 732 if (sk->sk_state != TCP_ESTABLISHED) 733 return -EDESTADDRREQ; 734 daddr = &np->daddr; 735 } else 736 daddr = NULL; 737 738 if (daddr) { 739 if (ipv6_addr_v4mapped(daddr)) { 740 struct sockaddr_in sin; 741 sin.sin_family = AF_INET; 742 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport; 743 sin.sin_addr.s_addr = daddr->s6_addr32[3]; 744 msg->msg_name = &sin; 745 msg->msg_namelen = sizeof(sin); 746 do_udp_sendmsg: 747 if (__ipv6_only_sock(sk)) 748 return -ENETUNREACH; 749 return udp_sendmsg(iocb, sk, msg, len); 750 } 751 } 752 753 if (up->pending == AF_INET) 754 return udp_sendmsg(iocb, sk, msg, len); 755 756 /* Rough check on arithmetic overflow, 757 better check is made in ip6_append_data(). 758 */ 759 if (len > INT_MAX - sizeof(struct udphdr)) 760 return -EMSGSIZE; 761 762 if (up->pending) { 763 /* 764 * There are pending frames. 765 * The socket lock must be held while it's corked. 766 */ 767 lock_sock(sk); 768 if (likely(up->pending)) { 769 if (unlikely(up->pending != AF_INET6)) { 770 release_sock(sk); 771 return -EAFNOSUPPORT; 772 } 773 dst = NULL; 774 goto do_append_data; 775 } 776 release_sock(sk); 777 } 778 ulen += sizeof(struct udphdr); 779 780 memset(&fl, 0, sizeof(fl)); 781 782 if (sin6) { 783 if (sin6->sin6_port == 0) 784 return -EINVAL; 785 786 fl.fl_ip_dport = sin6->sin6_port; 787 daddr = &sin6->sin6_addr; 788 789 if (np->sndflow) { 790 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 791 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) { 792 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); 793 if (flowlabel == NULL) 794 return -EINVAL; 795 daddr = &flowlabel->dst; 796 } 797 } 798 799 /* 800 * Otherwise it will be difficult to maintain 801 * sk->sk_dst_cache. 802 */ 803 if (sk->sk_state == TCP_ESTABLISHED && 804 ipv6_addr_equal(daddr, &np->daddr)) 805 daddr = &np->daddr; 806 807 if (addr_len >= sizeof(struct sockaddr_in6) && 808 sin6->sin6_scope_id && 809 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL) 810 fl.oif = sin6->sin6_scope_id; 811 } else { 812 if (sk->sk_state != TCP_ESTABLISHED) 813 return -EDESTADDRREQ; 814 815 fl.fl_ip_dport = inet->dport; 816 daddr = &np->daddr; 817 fl.fl6_flowlabel = np->flow_label; 818 connected = 1; 819 } 820 821 if (!fl.oif) 822 fl.oif = sk->sk_bound_dev_if; 823 824 if (!fl.oif) 825 fl.oif = np->sticky_pktinfo.ipi6_ifindex; 826 827 if (msg->msg_controllen) { 828 opt = &opt_space; 829 memset(opt, 0, sizeof(struct ipv6_txoptions)); 830 opt->tot_len = sizeof(*opt); 831 832 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass); 833 if (err < 0) { 834 fl6_sock_release(flowlabel); 835 return err; 836 } 837 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 838 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); 839 if (flowlabel == NULL) 840 return -EINVAL; 841 } 842 if (!(opt->opt_nflen|opt->opt_flen)) 843 opt = NULL; 844 connected = 0; 845 } 846 if (opt == NULL) 847 opt = np->opt; 848 if (flowlabel) 849 opt = fl6_merge_options(&opt_space, flowlabel, opt); 850 opt = ipv6_fixup_options(&opt_space, opt); 851 852 fl.proto = sk->sk_protocol; 853 if (!ipv6_addr_any(daddr)) 854 ipv6_addr_copy(&fl.fl6_dst, daddr); 855 else 856 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 857 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr)) 858 ipv6_addr_copy(&fl.fl6_src, &np->saddr); 859 fl.fl_ip_sport = inet->sport; 860 861 /* merge ip6_build_xmit from ip6_output */ 862 if (opt && opt->srcrt) { 863 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt; 864 ipv6_addr_copy(&final, &fl.fl6_dst); 865 ipv6_addr_copy(&fl.fl6_dst, rt0->addr); 866 final_p = &final; 867 connected = 0; 868 } 869 870 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) { 871 fl.oif = np->mcast_oif; 872 connected = 0; 873 } 874 875 security_sk_classify_flow(sk, &fl); 876 877 err = ip6_sk_dst_lookup(sk, &dst, &fl); 878 if (err) 879 goto out; 880 if (final_p) 881 ipv6_addr_copy(&fl.fl6_dst, final_p); 882 883 err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT); 884 if (err < 0) { 885 if (err == -EREMOTE) 886 err = ip6_dst_blackhole(sk, &dst, &fl); 887 if (err < 0) 888 goto out; 889 } 890 891 if (hlimit < 0) { 892 if (ipv6_addr_is_multicast(&fl.fl6_dst)) 893 hlimit = np->mcast_hops; 894 else 895 hlimit = np->hop_limit; 896 if (hlimit < 0) 897 hlimit = ip6_dst_hoplimit(dst); 898 } 899 900 if (tclass < 0) { 901 tclass = np->tclass; 902 if (tclass < 0) 903 tclass = 0; 904 } 905 906 if (msg->msg_flags&MSG_CONFIRM) 907 goto do_confirm; 908 back_from_confirm: 909 910 lock_sock(sk); 911 if (unlikely(up->pending)) { 912 /* The socket is already corked while preparing it. */ 913 /* ... which is an evident application bug. --ANK */ 914 release_sock(sk); 915 916 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 917 err = -EINVAL; 918 goto out; 919 } 920 921 up->pending = AF_INET6; 922 923 do_append_data: 924 up->len += ulen; 925 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 926 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen, 927 sizeof(struct udphdr), hlimit, tclass, opt, &fl, 928 (struct rt6_info*)dst, 929 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 930 if (err) 931 udp_v6_flush_pending_frames(sk); 932 else if (!corkreq) 933 err = udp_v6_push_pending_frames(sk); 934 else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 935 up->pending = 0; 936 937 if (dst) { 938 if (connected) { 939 ip6_dst_store(sk, dst, 940 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ? 941 &np->daddr : NULL, 942 #ifdef CONFIG_IPV6_SUBTREES 943 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ? 944 &np->saddr : 945 #endif 946 NULL); 947 } else { 948 dst_release(dst); 949 } 950 dst = NULL; 951 } 952 953 if (err > 0) 954 err = np->recverr ? net_xmit_errno(err) : 0; 955 release_sock(sk); 956 out: 957 dst_release(dst); 958 fl6_sock_release(flowlabel); 959 if (!err) 960 return len; 961 /* 962 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 963 * ENOBUFS might not be good (it's not tunable per se), but otherwise 964 * we don't have a good statistic (IpOutDiscards but it can be too many 965 * things). We could add another new stat but at least for now that 966 * seems like overkill. 967 */ 968 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 969 UDP6_INC_STATS_USER(sock_net(sk), 970 UDP_MIB_SNDBUFERRORS, is_udplite); 971 } 972 return err; 973 974 do_confirm: 975 dst_confirm(dst); 976 if (!(msg->msg_flags&MSG_PROBE) || len) 977 goto back_from_confirm; 978 err = 0; 979 goto out; 980 } 981 982 void udpv6_destroy_sock(struct sock *sk) 983 { 984 lock_sock(sk); 985 udp_v6_flush_pending_frames(sk); 986 release_sock(sk); 987 988 inet6_destroy_sock(sk); 989 } 990 991 /* 992 * Socket option code for UDP 993 */ 994 int udpv6_setsockopt(struct sock *sk, int level, int optname, 995 char __user *optval, int optlen) 996 { 997 if (level == SOL_UDP || level == SOL_UDPLITE) 998 return udp_lib_setsockopt(sk, level, optname, optval, optlen, 999 udp_v6_push_pending_frames); 1000 return ipv6_setsockopt(sk, level, optname, optval, optlen); 1001 } 1002 1003 #ifdef CONFIG_COMPAT 1004 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, 1005 char __user *optval, int optlen) 1006 { 1007 if (level == SOL_UDP || level == SOL_UDPLITE) 1008 return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1009 udp_v6_push_pending_frames); 1010 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen); 1011 } 1012 #endif 1013 1014 int udpv6_getsockopt(struct sock *sk, int level, int optname, 1015 char __user *optval, int __user *optlen) 1016 { 1017 if (level == SOL_UDP || level == SOL_UDPLITE) 1018 return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1019 return ipv6_getsockopt(sk, level, optname, optval, optlen); 1020 } 1021 1022 #ifdef CONFIG_COMPAT 1023 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, 1024 char __user *optval, int __user *optlen) 1025 { 1026 if (level == SOL_UDP || level == SOL_UDPLITE) 1027 return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1028 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen); 1029 } 1030 #endif 1031 1032 static struct inet6_protocol udpv6_protocol = { 1033 .handler = udpv6_rcv, 1034 .err_handler = udpv6_err, 1035 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1036 }; 1037 1038 /* ------------------------------------------------------------------------ */ 1039 #ifdef CONFIG_PROC_FS 1040 1041 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket) 1042 { 1043 struct inet_sock *inet = inet_sk(sp); 1044 struct ipv6_pinfo *np = inet6_sk(sp); 1045 struct in6_addr *dest, *src; 1046 __u16 destp, srcp; 1047 1048 dest = &np->daddr; 1049 src = &np->rcv_saddr; 1050 destp = ntohs(inet->dport); 1051 srcp = ntohs(inet->sport); 1052 seq_printf(seq, 1053 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " 1054 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n", 1055 bucket, 1056 src->s6_addr32[0], src->s6_addr32[1], 1057 src->s6_addr32[2], src->s6_addr32[3], srcp, 1058 dest->s6_addr32[0], dest->s6_addr32[1], 1059 dest->s6_addr32[2], dest->s6_addr32[3], destp, 1060 sp->sk_state, 1061 atomic_read(&sp->sk_wmem_alloc), 1062 atomic_read(&sp->sk_rmem_alloc), 1063 0, 0L, 0, 1064 sock_i_uid(sp), 0, 1065 sock_i_ino(sp), 1066 atomic_read(&sp->sk_refcnt), sp, 1067 atomic_read(&sp->sk_drops)); 1068 } 1069 1070 int udp6_seq_show(struct seq_file *seq, void *v) 1071 { 1072 if (v == SEQ_START_TOKEN) 1073 seq_printf(seq, 1074 " sl " 1075 "local_address " 1076 "remote_address " 1077 "st tx_queue rx_queue tr tm->when retrnsmt" 1078 " uid timeout inode ref pointer drops\n"); 1079 else 1080 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket); 1081 return 0; 1082 } 1083 1084 static struct udp_seq_afinfo udp6_seq_afinfo = { 1085 .name = "udp6", 1086 .family = AF_INET6, 1087 .udp_table = &udp_table, 1088 .seq_fops = { 1089 .owner = THIS_MODULE, 1090 }, 1091 .seq_ops = { 1092 .show = udp6_seq_show, 1093 }, 1094 }; 1095 1096 int udp6_proc_init(struct net *net) 1097 { 1098 return udp_proc_register(net, &udp6_seq_afinfo); 1099 } 1100 1101 void udp6_proc_exit(struct net *net) { 1102 udp_proc_unregister(net, &udp6_seq_afinfo); 1103 } 1104 #endif /* CONFIG_PROC_FS */ 1105 1106 /* ------------------------------------------------------------------------ */ 1107 1108 struct proto udpv6_prot = { 1109 .name = "UDPv6", 1110 .owner = THIS_MODULE, 1111 .close = udp_lib_close, 1112 .connect = ip6_datagram_connect, 1113 .disconnect = udp_disconnect, 1114 .ioctl = udp_ioctl, 1115 .destroy = udpv6_destroy_sock, 1116 .setsockopt = udpv6_setsockopt, 1117 .getsockopt = udpv6_getsockopt, 1118 .sendmsg = udpv6_sendmsg, 1119 .recvmsg = udpv6_recvmsg, 1120 .backlog_rcv = udpv6_queue_rcv_skb, 1121 .hash = udp_lib_hash, 1122 .unhash = udp_lib_unhash, 1123 .get_port = udp_v6_get_port, 1124 .memory_allocated = &udp_memory_allocated, 1125 .sysctl_mem = sysctl_udp_mem, 1126 .sysctl_wmem = &sysctl_udp_wmem_min, 1127 .sysctl_rmem = &sysctl_udp_rmem_min, 1128 .obj_size = sizeof(struct udp6_sock), 1129 .slab_flags = SLAB_DESTROY_BY_RCU, 1130 .h.udp_table = &udp_table, 1131 #ifdef CONFIG_COMPAT 1132 .compat_setsockopt = compat_udpv6_setsockopt, 1133 .compat_getsockopt = compat_udpv6_getsockopt, 1134 #endif 1135 }; 1136 1137 static struct inet_protosw udpv6_protosw = { 1138 .type = SOCK_DGRAM, 1139 .protocol = IPPROTO_UDP, 1140 .prot = &udpv6_prot, 1141 .ops = &inet6_dgram_ops, 1142 .capability =-1, 1143 .no_check = UDP_CSUM_DEFAULT, 1144 .flags = INET_PROTOSW_PERMANENT, 1145 }; 1146 1147 1148 int __init udpv6_init(void) 1149 { 1150 int ret; 1151 1152 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 1153 if (ret) 1154 goto out; 1155 1156 ret = inet6_register_protosw(&udpv6_protosw); 1157 if (ret) 1158 goto out_udpv6_protocol; 1159 out: 1160 return ret; 1161 1162 out_udpv6_protocol: 1163 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1164 goto out; 1165 } 1166 1167 void udpv6_exit(void) 1168 { 1169 inet6_unregister_protosw(&udpv6_protosw); 1170 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1171 } 1172