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