37#include "opt_inet6.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/malloc.h> 43#include <sys/mbuf.h> 44#include <sys/domain.h> 45#include <sys/proc.h> 46#include <sys/protosw.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/sysctl.h> 50#include <sys/syslog.h> 51 52#include <vm/vm_zone.h> 53 54#include <net/if.h> 55#include <net/route.h> 56 57#include <netinet/in.h> 58#include <netinet/in_systm.h> 59#include <netinet/ip.h> 60#ifdef INET6 61#include <netinet/ip6.h> 62#endif 63#include <netinet/in_pcb.h> 64#include <netinet/in_var.h> 65#include <netinet/ip_var.h> 66#ifdef INET6 67#include <netinet6/ip6_var.h> 68#endif 69#include <netinet/ip_icmp.h> 70#include <netinet/icmp_var.h> 71#include <netinet/udp.h> 72#include <netinet/udp_var.h> 73 74#ifdef IPSEC 75#include <netinet6/ipsec.h> 76#endif /*IPSEC*/ 77 78/* 79 * UDP protocol implementation. 80 * Per RFC 768, August, 1980. 81 */ 82#ifndef COMPAT_42 83static int udpcksum = 1; 84#else 85static int udpcksum = 0; /* XXX */ 86#endif 87SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 88 &udpcksum, 0, ""); 89 90int log_in_vain = 0; 91SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 92 &log_in_vain, 0, "Log all incoming UDP packets"); 93 94static int blackhole = 0; 95SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 96 &blackhole, 0, "Do not send port unreachables for refused connects"); 97 98struct inpcbhead udb; /* from udp_var.h */ 99#define udb6 udb /* for KAME src sync over BSD*'s */ 100struct inpcbinfo udbinfo; 101 102#ifndef UDBHASHSIZE 103#define UDBHASHSIZE 16 104#endif 105 106struct udpstat udpstat; /* from udp_var.h */ 107SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, 108 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 109 110static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 111#ifdef INET6 112struct udp_in6 { 113 struct sockaddr_in6 uin6_sin; 114 u_char uin6_init_done : 1; 115} udp_in6 = { 116 { sizeof(udp_in6.uin6_sin), AF_INET6 }, 117 0 118}; 119struct udp_ip6 { 120 struct ip6_hdr uip6_ip6; 121 u_char uip6_init_done : 1; 122} udp_ip6; 123#endif /* INET6 */ 124 125static void udp_append __P((struct inpcb *last, struct ip *ip, 126 struct mbuf *n, int off)); 127#ifdef INET6 128static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip)); 129#endif 130 131static int udp_detach __P((struct socket *so)); 132static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *, 133 struct mbuf *, struct proc *)); 134 135void 136udp_init() 137{ 138 LIST_INIT(&udb); 139 udbinfo.listhead = &udb; 140 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 141 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 142 &udbinfo.porthashmask); 143 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, 144 ZONE_INTERRUPT, 0); 145} 146 147void 148udp_input(m, off, proto) 149 register struct mbuf *m; 150 int off, proto; 151{ 152 int iphlen = off; 153 register struct ip *ip; 154 register struct udphdr *uh; 155 register struct inpcb *inp; 156 struct mbuf *opts = 0; 157 int len; 158 struct ip save_ip; 159 struct sockaddr *append_sa; 160 161 udpstat.udps_ipackets++; 162 163 /* 164 * Strip IP options, if any; should skip this, 165 * make available to user, and use on returned packets, 166 * but we don't yet have a way to check the checksum 167 * with options still present. 168 */ 169 if (iphlen > sizeof (struct ip)) { 170 ip_stripoptions(m, (struct mbuf *)0); 171 iphlen = sizeof(struct ip); 172 } 173 174 /* 175 * Get IP and UDP header together in first mbuf. 176 */ 177 ip = mtod(m, struct ip *); 178 if (m->m_len < iphlen + sizeof(struct udphdr)) { 179 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 180 udpstat.udps_hdrops++; 181 return; 182 } 183 ip = mtod(m, struct ip *); 184 } 185 uh = (struct udphdr *)((caddr_t)ip + iphlen); 186 187 /* 188 * Make mbuf data length reflect UDP length. 189 * If not enough data to reflect UDP length, drop. 190 */ 191 len = ntohs((u_short)uh->uh_ulen); 192 if (ip->ip_len != len) { 193 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 194 udpstat.udps_badlen++; 195 goto bad; 196 } 197 m_adj(m, len - ip->ip_len); 198 /* ip->ip_len = len; */ 199 } 200 /* 201 * Save a copy of the IP header in case we want restore it 202 * for sending an ICMP error message in response. 203 */ 204 save_ip = *ip; 205 206 /* 207 * Checksum extended UDP header and data. 208 */ 209 if (uh->uh_sum) { 210 bzero(((struct ipovly *)ip)->ih_x1, 9); 211 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 212 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 213 if (uh->uh_sum) { 214 udpstat.udps_badsum++; 215 m_freem(m); 216 return; 217 } 218 } 219 220 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 221 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 222 struct inpcb *last; 223 /* 224 * Deliver a multicast or broadcast datagram to *all* sockets 225 * for which the local and remote addresses and ports match 226 * those of the incoming datagram. This allows more than 227 * one process to receive multi/broadcasts on the same port. 228 * (This really ought to be done for unicast datagrams as 229 * well, but that would cause problems with existing 230 * applications that open both address-specific sockets and 231 * a wildcard socket listening to the same port -- they would 232 * end up receiving duplicates of every unicast datagram. 233 * Those applications open the multiple sockets to overcome an 234 * inadequacy of the UDP socket interface, but for backwards 235 * compatibility we avoid the problem here rather than 236 * fixing the interface. Maybe 4.5BSD will remedy this?) 237 */ 238 239 /* 240 * Construct sockaddr format source address. 241 */ 242 udp_in.sin_port = uh->uh_sport; 243 udp_in.sin_addr = ip->ip_src; 244 /* 245 * Locate pcb(s) for datagram. 246 * (Algorithm copied from raw_intr().) 247 */ 248 last = NULL; 249#ifdef INET6 250 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; 251#endif 252 LIST_FOREACH(inp, &udb, inp_list) { 253#ifdef INET6 254 if ((inp->inp_vflag & INP_IPV4) == 0) 255 continue; 256#endif 257 if (inp->inp_lport != uh->uh_dport) 258 continue; 259 if (inp->inp_laddr.s_addr != INADDR_ANY) { 260 if (inp->inp_laddr.s_addr != 261 ip->ip_dst.s_addr) 262 continue; 263 } 264 if (inp->inp_faddr.s_addr != INADDR_ANY) { 265 if (inp->inp_faddr.s_addr != 266 ip->ip_src.s_addr || 267 inp->inp_fport != uh->uh_sport) 268 continue; 269 } 270 271 if (last != NULL) { 272 struct mbuf *n; 273 274#ifdef IPSEC 275 /* check AH/ESP integrity. */ 276 if (ipsec4_in_reject_so(m, last->inp_socket)) 277 ipsecstat.in_polvio++; 278 /* do not inject data to pcb */ 279 else 280#endif /*IPSEC*/ 281 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) 282 udp_append(last, ip, n, 283 iphlen + 284 sizeof(struct udphdr)); 285 } 286 last = inp; 287 /* 288 * Don't look for additional matches if this one does 289 * not have either the SO_REUSEPORT or SO_REUSEADDR 290 * socket options set. This heuristic avoids searching 291 * through all pcbs in the common case of a non-shared 292 * port. It * assumes that an application will never 293 * clear these options after setting them. 294 */ 295 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 296 break; 297 } 298 299 if (last == NULL) { 300 /* 301 * No matching pcb found; discard datagram. 302 * (No need to send an ICMP Port Unreachable 303 * for a broadcast or multicast datgram.) 304 */ 305 udpstat.udps_noportbcast++; 306 goto bad; 307 } 308#ifdef IPSEC 309 /* check AH/ESP integrity. */ 310 if (ipsec4_in_reject_so(m, last->inp_socket)) { 311 ipsecstat.in_polvio++; 312 goto bad; 313 } 314#endif /*IPSEC*/ 315 udp_append(last, ip, m, iphlen + sizeof(struct udphdr)); 316 return; 317 } 318 /* 319 * Locate pcb for datagram. 320 */ 321 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 322 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 323 if (inp == NULL) { 324 if (log_in_vain) { 325 char buf[4*sizeof "123"]; 326 327 strcpy(buf, inet_ntoa(ip->ip_dst)); 328 log(LOG_INFO, 329 "Connection attempt to UDP %s:%d from %s:%d\n", 330 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 331 ntohs(uh->uh_sport)); 332 } 333 udpstat.udps_noport++; 334 if (m->m_flags & (M_BCAST | M_MCAST)) { 335 udpstat.udps_noportbcast++; 336 goto bad; 337 } 338 *ip = save_ip; 339#ifdef ICMP_BANDLIM 340 if (badport_bandlim(0) < 0) 341 goto bad; 342#endif 343 if (!blackhole) 344 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 345 else 346 goto bad; 347 return; 348 } 349#ifdef IPSEC 350 if (ipsec4_in_reject_so(m, inp->inp_socket)) { 351 ipsecstat.in_polvio++; 352 goto bad; 353 } 354#endif /*IPSEC*/ 355 356 /* 357 * Construct sockaddr format source address. 358 * Stuff source address and datagram in user buffer. 359 */ 360 udp_in.sin_port = uh->uh_sport; 361 udp_in.sin_addr = ip->ip_src; 362 if (inp->inp_flags & INP_CONTROLOPTS 363 || inp->inp_socket->so_options & SO_TIMESTAMP) { 364#ifdef INET6 365 if (inp->inp_vflag & INP_IPV6) { 366 int savedflags; 367 368 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 369 savedflags = inp->inp_flags; 370 inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 371 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); 372 inp->inp_flags = savedflags; 373 } else 374#endif 375 ip_savecontrol(inp, &opts, ip, m); 376 } 377 iphlen += sizeof(struct udphdr); 378 m_adj(m, iphlen); 379#ifdef INET6 380 if (inp->inp_vflag & INP_IPV6) { 381 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 382 append_sa = (struct sockaddr *)&udp_in6; 383 } else 384#endif 385 append_sa = (struct sockaddr *)&udp_in; 386 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) { 387 udpstat.udps_fullsock++; 388 goto bad; 389 } 390 sorwakeup(inp->inp_socket); 391 return; 392bad: 393 m_freem(m); 394 if (opts) 395 m_freem(opts); 396 return; 397} 398 399#if defined(INET6) 400static void 401ip_2_ip6_hdr(ip6, ip) 402 struct ip6_hdr *ip6; 403 struct ip *ip; 404{ 405 bzero(ip6, sizeof(*ip6)); 406 407 ip6->ip6_vfc = IPV6_VERSION; 408 ip6->ip6_plen = ip->ip_len; 409 ip6->ip6_nxt = ip->ip_p; 410 ip6->ip6_hlim = ip->ip_ttl; 411 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 412 IPV6_ADDR_INT32_SMP; 413 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 414 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 415} 416#endif 417 418/* 419 * subroutine of udp_input(), mainly for source code readability. 420 * caller must properly init udp_ip6 and udp_in6 beforehand. 421 */ 422static void 423udp_append(last, ip, n, off) 424 struct inpcb *last; 425 struct ip *ip; 426 struct mbuf *n; 427 int off; 428{ 429 struct sockaddr *append_sa; 430 struct mbuf *opts = 0; 431 432 if (last->inp_flags & INP_CONTROLOPTS || 433 last->inp_socket->so_options & SO_TIMESTAMP) { 434#ifdef INET6 435 if (last->inp_vflag & INP_IPV6) { 436 int savedflags; 437 438 if (udp_ip6.uip6_init_done == 0) { 439 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 440 udp_ip6.uip6_init_done = 1; 441 } 442 savedflags = last->inp_flags; 443 last->inp_flags &= ~INP_UNMAPPABLEOPTS; 444 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n); 445 last->inp_flags = savedflags; 446 } else 447#endif 448 ip_savecontrol(last, &opts, ip, n); 449 } 450#ifdef INET6 451 if (last->inp_vflag & INP_IPV6) { 452 if (udp_in6.uin6_init_done == 0) { 453 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 454 udp_in6.uin6_init_done = 1; 455 } 456 append_sa = (struct sockaddr *)&udp_in6.uin6_sin; 457 } else 458#endif 459 append_sa = (struct sockaddr *)&udp_in; 460 m_adj(n, off); 461 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) { 462 m_freem(n); 463 if (opts) 464 m_freem(opts); 465 udpstat.udps_fullsock++; 466 } else 467 sorwakeup(last->inp_socket); 468} 469 470/* 471 * Notify a udp user of an asynchronous error; 472 * just wake up so that he can collect error status. 473 */ 474void 475udp_notify(inp, errno) 476 register struct inpcb *inp; 477 int errno; 478{ 479 inp->inp_socket->so_error = errno; 480 sorwakeup(inp->inp_socket); 481 sowwakeup(inp->inp_socket); 482} 483 484void 485udp_ctlinput(cmd, sa, vip) 486 int cmd; 487 struct sockaddr *sa; 488 void *vip; 489{ 490 register struct ip *ip = vip; 491 register struct udphdr *uh; 492 493 if (!PRC_IS_REDIRECT(cmd) && 494 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 495 return; 496 if (ip) { 497 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 498 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 499 cmd, udp_notify); 500 } else 501 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 502} 503 504static int 505udp_pcblist SYSCTL_HANDLER_ARGS 506{ 507 int error, i, n, s; 508 struct inpcb *inp, **inp_list; 509 inp_gen_t gencnt; 510 struct xinpgen xig; 511 512 /* 513 * The process of preparing the TCB list is too time-consuming and 514 * resource-intensive to repeat twice on every request. 515 */ 516 if (req->oldptr == 0) { 517 n = udbinfo.ipi_count; 518 req->oldidx = 2 * (sizeof xig) 519 + (n + n/8) * sizeof(struct xinpcb); 520 return 0; 521 } 522 523 if (req->newptr != 0) 524 return EPERM; 525 526 /* 527 * OK, now we're committed to doing something. 528 */ 529 s = splnet(); 530 gencnt = udbinfo.ipi_gencnt; 531 n = udbinfo.ipi_count; 532 splx(s); 533 534 xig.xig_len = sizeof xig; 535 xig.xig_count = n; 536 xig.xig_gen = gencnt; 537 xig.xig_sogen = so_gencnt; 538 error = SYSCTL_OUT(req, &xig, sizeof xig); 539 if (error) 540 return error; 541 542 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 543 if (inp_list == 0) 544 return ENOMEM; 545 546 s = splnet(); 547 for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n; 548 inp = inp->inp_list.le_next) { 549 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp)) 550 inp_list[i++] = inp; 551 } 552 splx(s); 553 n = i; 554 555 error = 0; 556 for (i = 0; i < n; i++) { 557 inp = inp_list[i]; 558 if (inp->inp_gencnt <= gencnt) { 559 struct xinpcb xi; 560 xi.xi_len = sizeof xi; 561 /* XXX should avoid extra copy */ 562 bcopy(inp, &xi.xi_inp, sizeof *inp); 563 if (inp->inp_socket) 564 sotoxsocket(inp->inp_socket, &xi.xi_socket); 565 error = SYSCTL_OUT(req, &xi, sizeof xi); 566 } 567 } 568 if (!error) { 569 /* 570 * Give the user an updated idea of our state. 571 * If the generation differs from what we told 572 * her before, she knows that something happened 573 * while we were processing this request, and it 574 * might be necessary to retry. 575 */ 576 s = splnet(); 577 xig.xig_gen = udbinfo.ipi_gencnt; 578 xig.xig_sogen = so_gencnt; 579 xig.xig_count = udbinfo.ipi_count; 580 splx(s); 581 error = SYSCTL_OUT(req, &xig, sizeof xig); 582 } 583 free(inp_list, M_TEMP); 584 return error; 585} 586 587SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 588 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 589 590static int 591udp_getcred SYSCTL_HANDLER_ARGS 592{ 593 struct sockaddr_in addrs[2]; 594 struct inpcb *inp; 595 int error, s; 596 597 error = suser(req->p); 598 if (error) 599 return (error); 600 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 601 if (error) 602 return (error); 603 s = splnet(); 604 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 605 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 606 if (inp == NULL || inp->inp_socket == NULL) { 607 error = ENOENT; 608 goto out; 609 } 610 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred)); 611out: 612 splx(s); 613 return (error); 614} 615 616SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 617 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection"); 618 619static int 620udp_output(inp, m, addr, control, p) 621 register struct inpcb *inp; 622 struct mbuf *m; 623 struct sockaddr *addr; 624 struct mbuf *control; 625 struct proc *p; 626{ 627 register struct udpiphdr *ui; 628 register int len = m->m_pkthdr.len; 629 struct in_addr laddr; 630 struct sockaddr_in *sin; 631 int s = 0, error = 0; 632 633 if (control) 634 m_freem(control); /* XXX */ 635 636 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 637 error = EMSGSIZE; 638 goto release; 639 } 640 641 if (addr) { 642 sin = (struct sockaddr_in *)addr; 643 prison_remote_ip(p, 0, &sin->sin_addr.s_addr); 644 laddr = inp->inp_laddr; 645 if (inp->inp_faddr.s_addr != INADDR_ANY) { 646 error = EISCONN; 647 goto release; 648 } 649 /* 650 * Must block input while temporarily connected. 651 */ 652 s = splnet(); 653 error = in_pcbconnect(inp, addr, p); 654 if (error) { 655 splx(s); 656 goto release; 657 } 658 } else { 659 if (inp->inp_faddr.s_addr == INADDR_ANY) { 660 error = ENOTCONN; 661 goto release; 662 } 663 } 664 /* 665 * Calculate data length and get a mbuf 666 * for UDP and IP headers. 667 */ 668 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 669 if (m == 0) { 670 error = ENOBUFS; 671 if (addr) 672 splx(s); 673 goto release; 674 } 675 676 /* 677 * Fill in mbuf with extended UDP header 678 * and addresses and length put into network format. 679 */ 680 ui = mtod(m, struct udpiphdr *); 681 bzero(ui->ui_x1, sizeof(ui->ui_x1)); 682 ui->ui_pr = IPPROTO_UDP; 683 ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); 684 ui->ui_src = inp->inp_laddr; 685 ui->ui_dst = inp->inp_faddr; 686 ui->ui_sport = inp->inp_lport; 687 ui->ui_dport = inp->inp_fport; 688 ui->ui_ulen = ui->ui_len; 689 690 /* 691 * Stuff checksum and output datagram. 692 */ 693 ui->ui_sum = 0; 694 if (udpcksum) { 695 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 696 ui->ui_sum = 0xffff; 697 } 698 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 699 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 700 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 701 udpstat.udps_opackets++; 702 703#ifdef IPSEC 704 m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket; 705#endif /*IPSEC*/ 706 707 error = ip_output(m, inp->inp_options, &inp->inp_route,
| 38#include "opt_inet6.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/malloc.h> 44#include <sys/mbuf.h> 45#include <sys/domain.h> 46#include <sys/proc.h> 47#include <sys/protosw.h> 48#include <sys/socket.h> 49#include <sys/socketvar.h> 50#include <sys/sysctl.h> 51#include <sys/syslog.h> 52 53#include <vm/vm_zone.h> 54 55#include <net/if.h> 56#include <net/route.h> 57 58#include <netinet/in.h> 59#include <netinet/in_systm.h> 60#include <netinet/ip.h> 61#ifdef INET6 62#include <netinet/ip6.h> 63#endif 64#include <netinet/in_pcb.h> 65#include <netinet/in_var.h> 66#include <netinet/ip_var.h> 67#ifdef INET6 68#include <netinet6/ip6_var.h> 69#endif 70#include <netinet/ip_icmp.h> 71#include <netinet/icmp_var.h> 72#include <netinet/udp.h> 73#include <netinet/udp_var.h> 74 75#ifdef IPSEC 76#include <netinet6/ipsec.h> 77#endif /*IPSEC*/ 78 79/* 80 * UDP protocol implementation. 81 * Per RFC 768, August, 1980. 82 */ 83#ifndef COMPAT_42 84static int udpcksum = 1; 85#else 86static int udpcksum = 0; /* XXX */ 87#endif 88SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 89 &udpcksum, 0, ""); 90 91int log_in_vain = 0; 92SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 93 &log_in_vain, 0, "Log all incoming UDP packets"); 94 95static int blackhole = 0; 96SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 97 &blackhole, 0, "Do not send port unreachables for refused connects"); 98 99struct inpcbhead udb; /* from udp_var.h */ 100#define udb6 udb /* for KAME src sync over BSD*'s */ 101struct inpcbinfo udbinfo; 102 103#ifndef UDBHASHSIZE 104#define UDBHASHSIZE 16 105#endif 106 107struct udpstat udpstat; /* from udp_var.h */ 108SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, 109 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 110 111static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 112#ifdef INET6 113struct udp_in6 { 114 struct sockaddr_in6 uin6_sin; 115 u_char uin6_init_done : 1; 116} udp_in6 = { 117 { sizeof(udp_in6.uin6_sin), AF_INET6 }, 118 0 119}; 120struct udp_ip6 { 121 struct ip6_hdr uip6_ip6; 122 u_char uip6_init_done : 1; 123} udp_ip6; 124#endif /* INET6 */ 125 126static void udp_append __P((struct inpcb *last, struct ip *ip, 127 struct mbuf *n, int off)); 128#ifdef INET6 129static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip)); 130#endif 131 132static int udp_detach __P((struct socket *so)); 133static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *, 134 struct mbuf *, struct proc *)); 135 136void 137udp_init() 138{ 139 LIST_INIT(&udb); 140 udbinfo.listhead = &udb; 141 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 142 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 143 &udbinfo.porthashmask); 144 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, 145 ZONE_INTERRUPT, 0); 146} 147 148void 149udp_input(m, off, proto) 150 register struct mbuf *m; 151 int off, proto; 152{ 153 int iphlen = off; 154 register struct ip *ip; 155 register struct udphdr *uh; 156 register struct inpcb *inp; 157 struct mbuf *opts = 0; 158 int len; 159 struct ip save_ip; 160 struct sockaddr *append_sa; 161 162 udpstat.udps_ipackets++; 163 164 /* 165 * Strip IP options, if any; should skip this, 166 * make available to user, and use on returned packets, 167 * but we don't yet have a way to check the checksum 168 * with options still present. 169 */ 170 if (iphlen > sizeof (struct ip)) { 171 ip_stripoptions(m, (struct mbuf *)0); 172 iphlen = sizeof(struct ip); 173 } 174 175 /* 176 * Get IP and UDP header together in first mbuf. 177 */ 178 ip = mtod(m, struct ip *); 179 if (m->m_len < iphlen + sizeof(struct udphdr)) { 180 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 181 udpstat.udps_hdrops++; 182 return; 183 } 184 ip = mtod(m, struct ip *); 185 } 186 uh = (struct udphdr *)((caddr_t)ip + iphlen); 187 188 /* 189 * Make mbuf data length reflect UDP length. 190 * If not enough data to reflect UDP length, drop. 191 */ 192 len = ntohs((u_short)uh->uh_ulen); 193 if (ip->ip_len != len) { 194 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 195 udpstat.udps_badlen++; 196 goto bad; 197 } 198 m_adj(m, len - ip->ip_len); 199 /* ip->ip_len = len; */ 200 } 201 /* 202 * Save a copy of the IP header in case we want restore it 203 * for sending an ICMP error message in response. 204 */ 205 save_ip = *ip; 206 207 /* 208 * Checksum extended UDP header and data. 209 */ 210 if (uh->uh_sum) { 211 bzero(((struct ipovly *)ip)->ih_x1, 9); 212 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 213 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 214 if (uh->uh_sum) { 215 udpstat.udps_badsum++; 216 m_freem(m); 217 return; 218 } 219 } 220 221 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 222 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 223 struct inpcb *last; 224 /* 225 * Deliver a multicast or broadcast datagram to *all* sockets 226 * for which the local and remote addresses and ports match 227 * those of the incoming datagram. This allows more than 228 * one process to receive multi/broadcasts on the same port. 229 * (This really ought to be done for unicast datagrams as 230 * well, but that would cause problems with existing 231 * applications that open both address-specific sockets and 232 * a wildcard socket listening to the same port -- they would 233 * end up receiving duplicates of every unicast datagram. 234 * Those applications open the multiple sockets to overcome an 235 * inadequacy of the UDP socket interface, but for backwards 236 * compatibility we avoid the problem here rather than 237 * fixing the interface. Maybe 4.5BSD will remedy this?) 238 */ 239 240 /* 241 * Construct sockaddr format source address. 242 */ 243 udp_in.sin_port = uh->uh_sport; 244 udp_in.sin_addr = ip->ip_src; 245 /* 246 * Locate pcb(s) for datagram. 247 * (Algorithm copied from raw_intr().) 248 */ 249 last = NULL; 250#ifdef INET6 251 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; 252#endif 253 LIST_FOREACH(inp, &udb, inp_list) { 254#ifdef INET6 255 if ((inp->inp_vflag & INP_IPV4) == 0) 256 continue; 257#endif 258 if (inp->inp_lport != uh->uh_dport) 259 continue; 260 if (inp->inp_laddr.s_addr != INADDR_ANY) { 261 if (inp->inp_laddr.s_addr != 262 ip->ip_dst.s_addr) 263 continue; 264 } 265 if (inp->inp_faddr.s_addr != INADDR_ANY) { 266 if (inp->inp_faddr.s_addr != 267 ip->ip_src.s_addr || 268 inp->inp_fport != uh->uh_sport) 269 continue; 270 } 271 272 if (last != NULL) { 273 struct mbuf *n; 274 275#ifdef IPSEC 276 /* check AH/ESP integrity. */ 277 if (ipsec4_in_reject_so(m, last->inp_socket)) 278 ipsecstat.in_polvio++; 279 /* do not inject data to pcb */ 280 else 281#endif /*IPSEC*/ 282 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) 283 udp_append(last, ip, n, 284 iphlen + 285 sizeof(struct udphdr)); 286 } 287 last = inp; 288 /* 289 * Don't look for additional matches if this one does 290 * not have either the SO_REUSEPORT or SO_REUSEADDR 291 * socket options set. This heuristic avoids searching 292 * through all pcbs in the common case of a non-shared 293 * port. It * assumes that an application will never 294 * clear these options after setting them. 295 */ 296 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 297 break; 298 } 299 300 if (last == NULL) { 301 /* 302 * No matching pcb found; discard datagram. 303 * (No need to send an ICMP Port Unreachable 304 * for a broadcast or multicast datgram.) 305 */ 306 udpstat.udps_noportbcast++; 307 goto bad; 308 } 309#ifdef IPSEC 310 /* check AH/ESP integrity. */ 311 if (ipsec4_in_reject_so(m, last->inp_socket)) { 312 ipsecstat.in_polvio++; 313 goto bad; 314 } 315#endif /*IPSEC*/ 316 udp_append(last, ip, m, iphlen + sizeof(struct udphdr)); 317 return; 318 } 319 /* 320 * Locate pcb for datagram. 321 */ 322 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 323 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 324 if (inp == NULL) { 325 if (log_in_vain) { 326 char buf[4*sizeof "123"]; 327 328 strcpy(buf, inet_ntoa(ip->ip_dst)); 329 log(LOG_INFO, 330 "Connection attempt to UDP %s:%d from %s:%d\n", 331 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 332 ntohs(uh->uh_sport)); 333 } 334 udpstat.udps_noport++; 335 if (m->m_flags & (M_BCAST | M_MCAST)) { 336 udpstat.udps_noportbcast++; 337 goto bad; 338 } 339 *ip = save_ip; 340#ifdef ICMP_BANDLIM 341 if (badport_bandlim(0) < 0) 342 goto bad; 343#endif 344 if (!blackhole) 345 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 346 else 347 goto bad; 348 return; 349 } 350#ifdef IPSEC 351 if (ipsec4_in_reject_so(m, inp->inp_socket)) { 352 ipsecstat.in_polvio++; 353 goto bad; 354 } 355#endif /*IPSEC*/ 356 357 /* 358 * Construct sockaddr format source address. 359 * Stuff source address and datagram in user buffer. 360 */ 361 udp_in.sin_port = uh->uh_sport; 362 udp_in.sin_addr = ip->ip_src; 363 if (inp->inp_flags & INP_CONTROLOPTS 364 || inp->inp_socket->so_options & SO_TIMESTAMP) { 365#ifdef INET6 366 if (inp->inp_vflag & INP_IPV6) { 367 int savedflags; 368 369 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 370 savedflags = inp->inp_flags; 371 inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 372 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); 373 inp->inp_flags = savedflags; 374 } else 375#endif 376 ip_savecontrol(inp, &opts, ip, m); 377 } 378 iphlen += sizeof(struct udphdr); 379 m_adj(m, iphlen); 380#ifdef INET6 381 if (inp->inp_vflag & INP_IPV6) { 382 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 383 append_sa = (struct sockaddr *)&udp_in6; 384 } else 385#endif 386 append_sa = (struct sockaddr *)&udp_in; 387 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) { 388 udpstat.udps_fullsock++; 389 goto bad; 390 } 391 sorwakeup(inp->inp_socket); 392 return; 393bad: 394 m_freem(m); 395 if (opts) 396 m_freem(opts); 397 return; 398} 399 400#if defined(INET6) 401static void 402ip_2_ip6_hdr(ip6, ip) 403 struct ip6_hdr *ip6; 404 struct ip *ip; 405{ 406 bzero(ip6, sizeof(*ip6)); 407 408 ip6->ip6_vfc = IPV6_VERSION; 409 ip6->ip6_plen = ip->ip_len; 410 ip6->ip6_nxt = ip->ip_p; 411 ip6->ip6_hlim = ip->ip_ttl; 412 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 413 IPV6_ADDR_INT32_SMP; 414 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 415 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 416} 417#endif 418 419/* 420 * subroutine of udp_input(), mainly for source code readability. 421 * caller must properly init udp_ip6 and udp_in6 beforehand. 422 */ 423static void 424udp_append(last, ip, n, off) 425 struct inpcb *last; 426 struct ip *ip; 427 struct mbuf *n; 428 int off; 429{ 430 struct sockaddr *append_sa; 431 struct mbuf *opts = 0; 432 433 if (last->inp_flags & INP_CONTROLOPTS || 434 last->inp_socket->so_options & SO_TIMESTAMP) { 435#ifdef INET6 436 if (last->inp_vflag & INP_IPV6) { 437 int savedflags; 438 439 if (udp_ip6.uip6_init_done == 0) { 440 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 441 udp_ip6.uip6_init_done = 1; 442 } 443 savedflags = last->inp_flags; 444 last->inp_flags &= ~INP_UNMAPPABLEOPTS; 445 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n); 446 last->inp_flags = savedflags; 447 } else 448#endif 449 ip_savecontrol(last, &opts, ip, n); 450 } 451#ifdef INET6 452 if (last->inp_vflag & INP_IPV6) { 453 if (udp_in6.uin6_init_done == 0) { 454 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 455 udp_in6.uin6_init_done = 1; 456 } 457 append_sa = (struct sockaddr *)&udp_in6.uin6_sin; 458 } else 459#endif 460 append_sa = (struct sockaddr *)&udp_in; 461 m_adj(n, off); 462 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) { 463 m_freem(n); 464 if (opts) 465 m_freem(opts); 466 udpstat.udps_fullsock++; 467 } else 468 sorwakeup(last->inp_socket); 469} 470 471/* 472 * Notify a udp user of an asynchronous error; 473 * just wake up so that he can collect error status. 474 */ 475void 476udp_notify(inp, errno) 477 register struct inpcb *inp; 478 int errno; 479{ 480 inp->inp_socket->so_error = errno; 481 sorwakeup(inp->inp_socket); 482 sowwakeup(inp->inp_socket); 483} 484 485void 486udp_ctlinput(cmd, sa, vip) 487 int cmd; 488 struct sockaddr *sa; 489 void *vip; 490{ 491 register struct ip *ip = vip; 492 register struct udphdr *uh; 493 494 if (!PRC_IS_REDIRECT(cmd) && 495 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 496 return; 497 if (ip) { 498 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 499 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 500 cmd, udp_notify); 501 } else 502 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 503} 504 505static int 506udp_pcblist SYSCTL_HANDLER_ARGS 507{ 508 int error, i, n, s; 509 struct inpcb *inp, **inp_list; 510 inp_gen_t gencnt; 511 struct xinpgen xig; 512 513 /* 514 * The process of preparing the TCB list is too time-consuming and 515 * resource-intensive to repeat twice on every request. 516 */ 517 if (req->oldptr == 0) { 518 n = udbinfo.ipi_count; 519 req->oldidx = 2 * (sizeof xig) 520 + (n + n/8) * sizeof(struct xinpcb); 521 return 0; 522 } 523 524 if (req->newptr != 0) 525 return EPERM; 526 527 /* 528 * OK, now we're committed to doing something. 529 */ 530 s = splnet(); 531 gencnt = udbinfo.ipi_gencnt; 532 n = udbinfo.ipi_count; 533 splx(s); 534 535 xig.xig_len = sizeof xig; 536 xig.xig_count = n; 537 xig.xig_gen = gencnt; 538 xig.xig_sogen = so_gencnt; 539 error = SYSCTL_OUT(req, &xig, sizeof xig); 540 if (error) 541 return error; 542 543 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 544 if (inp_list == 0) 545 return ENOMEM; 546 547 s = splnet(); 548 for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n; 549 inp = inp->inp_list.le_next) { 550 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp)) 551 inp_list[i++] = inp; 552 } 553 splx(s); 554 n = i; 555 556 error = 0; 557 for (i = 0; i < n; i++) { 558 inp = inp_list[i]; 559 if (inp->inp_gencnt <= gencnt) { 560 struct xinpcb xi; 561 xi.xi_len = sizeof xi; 562 /* XXX should avoid extra copy */ 563 bcopy(inp, &xi.xi_inp, sizeof *inp); 564 if (inp->inp_socket) 565 sotoxsocket(inp->inp_socket, &xi.xi_socket); 566 error = SYSCTL_OUT(req, &xi, sizeof xi); 567 } 568 } 569 if (!error) { 570 /* 571 * Give the user an updated idea of our state. 572 * If the generation differs from what we told 573 * her before, she knows that something happened 574 * while we were processing this request, and it 575 * might be necessary to retry. 576 */ 577 s = splnet(); 578 xig.xig_gen = udbinfo.ipi_gencnt; 579 xig.xig_sogen = so_gencnt; 580 xig.xig_count = udbinfo.ipi_count; 581 splx(s); 582 error = SYSCTL_OUT(req, &xig, sizeof xig); 583 } 584 free(inp_list, M_TEMP); 585 return error; 586} 587 588SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 589 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 590 591static int 592udp_getcred SYSCTL_HANDLER_ARGS 593{ 594 struct sockaddr_in addrs[2]; 595 struct inpcb *inp; 596 int error, s; 597 598 error = suser(req->p); 599 if (error) 600 return (error); 601 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 602 if (error) 603 return (error); 604 s = splnet(); 605 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 606 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 607 if (inp == NULL || inp->inp_socket == NULL) { 608 error = ENOENT; 609 goto out; 610 } 611 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred)); 612out: 613 splx(s); 614 return (error); 615} 616 617SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 618 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection"); 619 620static int 621udp_output(inp, m, addr, control, p) 622 register struct inpcb *inp; 623 struct mbuf *m; 624 struct sockaddr *addr; 625 struct mbuf *control; 626 struct proc *p; 627{ 628 register struct udpiphdr *ui; 629 register int len = m->m_pkthdr.len; 630 struct in_addr laddr; 631 struct sockaddr_in *sin; 632 int s = 0, error = 0; 633 634 if (control) 635 m_freem(control); /* XXX */ 636 637 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 638 error = EMSGSIZE; 639 goto release; 640 } 641 642 if (addr) { 643 sin = (struct sockaddr_in *)addr; 644 prison_remote_ip(p, 0, &sin->sin_addr.s_addr); 645 laddr = inp->inp_laddr; 646 if (inp->inp_faddr.s_addr != INADDR_ANY) { 647 error = EISCONN; 648 goto release; 649 } 650 /* 651 * Must block input while temporarily connected. 652 */ 653 s = splnet(); 654 error = in_pcbconnect(inp, addr, p); 655 if (error) { 656 splx(s); 657 goto release; 658 } 659 } else { 660 if (inp->inp_faddr.s_addr == INADDR_ANY) { 661 error = ENOTCONN; 662 goto release; 663 } 664 } 665 /* 666 * Calculate data length and get a mbuf 667 * for UDP and IP headers. 668 */ 669 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 670 if (m == 0) { 671 error = ENOBUFS; 672 if (addr) 673 splx(s); 674 goto release; 675 } 676 677 /* 678 * Fill in mbuf with extended UDP header 679 * and addresses and length put into network format. 680 */ 681 ui = mtod(m, struct udpiphdr *); 682 bzero(ui->ui_x1, sizeof(ui->ui_x1)); 683 ui->ui_pr = IPPROTO_UDP; 684 ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); 685 ui->ui_src = inp->inp_laddr; 686 ui->ui_dst = inp->inp_faddr; 687 ui->ui_sport = inp->inp_lport; 688 ui->ui_dport = inp->inp_fport; 689 ui->ui_ulen = ui->ui_len; 690 691 /* 692 * Stuff checksum and output datagram. 693 */ 694 ui->ui_sum = 0; 695 if (udpcksum) { 696 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 697 ui->ui_sum = 0xffff; 698 } 699 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 700 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 701 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 702 udpstat.udps_opackets++; 703 704#ifdef IPSEC 705 m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket; 706#endif /*IPSEC*/ 707 708 error = ip_output(m, inp->inp_options, &inp->inp_route,
|
709 inp->inp_moptions); 710 711 if (addr) { 712 in_pcbdisconnect(inp); 713 inp->inp_laddr = laddr; /* XXX rehash? */ 714 splx(s); 715 } 716 return (error); 717 718release: 719 m_freem(m); 720 return (error); 721} 722 723u_long udp_sendspace = 9216; /* really max datagram size */ 724 /* 40 1K datagrams */ 725SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 726 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 727 728u_long udp_recvspace = 40 * (1024 + 729#ifdef INET6 730 sizeof(struct sockaddr_in6) 731#else 732 sizeof(struct sockaddr_in) 733#endif 734 ); 735SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 736 &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 737 738static int 739udp_abort(struct socket *so) 740{ 741 struct inpcb *inp; 742 int s; 743 744 inp = sotoinpcb(so); 745 if (inp == 0) 746 return EINVAL; /* ??? possible? panic instead? */ 747 soisdisconnected(so); 748 s = splnet(); 749 in_pcbdetach(inp); 750 splx(s); 751 return 0; 752} 753 754static int 755udp_attach(struct socket *so, int proto, struct proc *p) 756{ 757 struct inpcb *inp; 758 int s, error; 759 760 inp = sotoinpcb(so); 761 if (inp != 0) 762 return EINVAL; 763 764 error = soreserve(so, udp_sendspace, udp_recvspace); 765 if (error) 766 return error; 767 s = splnet(); 768 error = in_pcballoc(so, &udbinfo, p); 769 splx(s); 770 if (error) 771 return error; 772 773 inp = (struct inpcb *)so->so_pcb; 774 inp->inp_vflag |= INP_IPV4; 775 inp->inp_ip_ttl = ip_defttl; 776#ifdef IPSEC 777 error = ipsec_init_policy(so, &inp->inp_sp); 778 if (error != 0) { 779 in_pcbdetach(inp); 780 return error; 781 } 782#endif /*IPSEC*/ 783 return 0; 784} 785 786static int 787udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 788{ 789 struct inpcb *inp; 790 int s, error; 791 792 inp = sotoinpcb(so); 793 if (inp == 0) 794 return EINVAL; 795 s = splnet(); 796 error = in_pcbbind(inp, nam, p); 797 splx(s); 798 return error; 799} 800 801static int 802udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 803{ 804 struct inpcb *inp; 805 int s, error; 806 struct sockaddr_in *sin; 807 808 inp = sotoinpcb(so); 809 if (inp == 0) 810 return EINVAL; 811 if (inp->inp_faddr.s_addr != INADDR_ANY) 812 return EISCONN; 813 s = splnet(); 814 sin = (struct sockaddr_in *)nam; 815 prison_remote_ip(p, 0, &sin->sin_addr.s_addr); 816 error = in_pcbconnect(inp, nam, p); 817 splx(s); 818 if (error == 0) 819 soisconnected(so); 820 return error; 821} 822 823static int 824udp_detach(struct socket *so) 825{ 826 struct inpcb *inp; 827 int s; 828 829 inp = sotoinpcb(so); 830 if (inp == 0) 831 return EINVAL; 832 s = splnet(); 833 in_pcbdetach(inp); 834 splx(s); 835 return 0; 836} 837 838static int 839udp_disconnect(struct socket *so) 840{ 841 struct inpcb *inp; 842 int s; 843 844 inp = sotoinpcb(so); 845 if (inp == 0) 846 return EINVAL; 847 if (inp->inp_faddr.s_addr == INADDR_ANY) 848 return ENOTCONN; 849 850 s = splnet(); 851 in_pcbdisconnect(inp); 852 inp->inp_laddr.s_addr = INADDR_ANY; 853 splx(s); 854 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 855 return 0; 856} 857 858static int 859udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 860 struct mbuf *control, struct proc *p) 861{ 862 struct inpcb *inp; 863 864 inp = sotoinpcb(so); 865 if (inp == 0) { 866 m_freem(m); 867 return EINVAL; 868 } 869 return udp_output(inp, m, addr, control, p); 870} 871 872int 873udp_shutdown(struct socket *so) 874{ 875 struct inpcb *inp; 876 877 inp = sotoinpcb(so); 878 if (inp == 0) 879 return EINVAL; 880 socantsendmore(so); 881 return 0; 882} 883 884struct pr_usrreqs udp_usrreqs = { 885 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 886 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 887 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 888 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 889 in_setsockaddr, sosend, soreceive, sopoll 890}; 891
| 711 inp->inp_moptions); 712 713 if (addr) { 714 in_pcbdisconnect(inp); 715 inp->inp_laddr = laddr; /* XXX rehash? */ 716 splx(s); 717 } 718 return (error); 719 720release: 721 m_freem(m); 722 return (error); 723} 724 725u_long udp_sendspace = 9216; /* really max datagram size */ 726 /* 40 1K datagrams */ 727SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 728 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 729 730u_long udp_recvspace = 40 * (1024 + 731#ifdef INET6 732 sizeof(struct sockaddr_in6) 733#else 734 sizeof(struct sockaddr_in) 735#endif 736 ); 737SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 738 &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 739 740static int 741udp_abort(struct socket *so) 742{ 743 struct inpcb *inp; 744 int s; 745 746 inp = sotoinpcb(so); 747 if (inp == 0) 748 return EINVAL; /* ??? possible? panic instead? */ 749 soisdisconnected(so); 750 s = splnet(); 751 in_pcbdetach(inp); 752 splx(s); 753 return 0; 754} 755 756static int 757udp_attach(struct socket *so, int proto, struct proc *p) 758{ 759 struct inpcb *inp; 760 int s, error; 761 762 inp = sotoinpcb(so); 763 if (inp != 0) 764 return EINVAL; 765 766 error = soreserve(so, udp_sendspace, udp_recvspace); 767 if (error) 768 return error; 769 s = splnet(); 770 error = in_pcballoc(so, &udbinfo, p); 771 splx(s); 772 if (error) 773 return error; 774 775 inp = (struct inpcb *)so->so_pcb; 776 inp->inp_vflag |= INP_IPV4; 777 inp->inp_ip_ttl = ip_defttl; 778#ifdef IPSEC 779 error = ipsec_init_policy(so, &inp->inp_sp); 780 if (error != 0) { 781 in_pcbdetach(inp); 782 return error; 783 } 784#endif /*IPSEC*/ 785 return 0; 786} 787 788static int 789udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 790{ 791 struct inpcb *inp; 792 int s, error; 793 794 inp = sotoinpcb(so); 795 if (inp == 0) 796 return EINVAL; 797 s = splnet(); 798 error = in_pcbbind(inp, nam, p); 799 splx(s); 800 return error; 801} 802 803static int 804udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 805{ 806 struct inpcb *inp; 807 int s, error; 808 struct sockaddr_in *sin; 809 810 inp = sotoinpcb(so); 811 if (inp == 0) 812 return EINVAL; 813 if (inp->inp_faddr.s_addr != INADDR_ANY) 814 return EISCONN; 815 s = splnet(); 816 sin = (struct sockaddr_in *)nam; 817 prison_remote_ip(p, 0, &sin->sin_addr.s_addr); 818 error = in_pcbconnect(inp, nam, p); 819 splx(s); 820 if (error == 0) 821 soisconnected(so); 822 return error; 823} 824 825static int 826udp_detach(struct socket *so) 827{ 828 struct inpcb *inp; 829 int s; 830 831 inp = sotoinpcb(so); 832 if (inp == 0) 833 return EINVAL; 834 s = splnet(); 835 in_pcbdetach(inp); 836 splx(s); 837 return 0; 838} 839 840static int 841udp_disconnect(struct socket *so) 842{ 843 struct inpcb *inp; 844 int s; 845 846 inp = sotoinpcb(so); 847 if (inp == 0) 848 return EINVAL; 849 if (inp->inp_faddr.s_addr == INADDR_ANY) 850 return ENOTCONN; 851 852 s = splnet(); 853 in_pcbdisconnect(inp); 854 inp->inp_laddr.s_addr = INADDR_ANY; 855 splx(s); 856 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 857 return 0; 858} 859 860static int 861udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 862 struct mbuf *control, struct proc *p) 863{ 864 struct inpcb *inp; 865 866 inp = sotoinpcb(so); 867 if (inp == 0) { 868 m_freem(m); 869 return EINVAL; 870 } 871 return udp_output(inp, m, addr, control, p); 872} 873 874int 875udp_shutdown(struct socket *so) 876{ 877 struct inpcb *inp; 878 879 inp = sotoinpcb(so); 880 if (inp == 0) 881 return EINVAL; 882 socantsendmore(so); 883 return 0; 884} 885 886struct pr_usrreqs udp_usrreqs = { 887 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 888 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 889 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 890 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 891 in_setsockaddr, sosend, soreceive, sopoll 892}; 893
|