Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.47.6.3
1.47.6.3! itojun 1: /* $NetBSD: udp_usrreq.c,v 1.47 1999/01/19 23:03:22 mycroft Exp $ */
1.47.6.2 itojun 2:
3: /*
4: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. Neither the name of the project nor the names of its contributors
16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
1.14 cgd 31:
1.1 cgd 32: /*
1.44 thorpej 33: * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
1.13 mycroft 34: * The Regents of the University of California. All rights reserved.
1.1 cgd 35: *
36: * Redistribution and use in source and binary forms, with or without
37: * modification, are permitted provided that the following conditions
38: * are met:
39: * 1. Redistributions of source code must retain the above copyright
40: * notice, this list of conditions and the following disclaimer.
41: * 2. Redistributions in binary form must reproduce the above copyright
42: * notice, this list of conditions and the following disclaimer in the
43: * documentation and/or other materials provided with the distribution.
44: * 3. All advertising materials mentioning features or use of this software
45: * must display the following acknowledgement:
46: * This product includes software developed by the University of
47: * California, Berkeley and its contributors.
48: * 4. Neither the name of the University nor the names of its contributors
49: * may be used to endorse or promote products derived from this software
50: * without specific prior written permission.
51: *
52: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62: * SUCH DAMAGE.
63: *
1.44 thorpej 64: * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
1.1 cgd 65: */
1.47.6.2 itojun 66:
1.47.6.3! itojun 67: #include "opt_ipsec.h"
1.47.6.2 itojun 68:
1.38 ws 69: #include "ipkdb.h"
1.1 cgd 70:
1.5 mycroft 71: #include <sys/param.h>
72: #include <sys/malloc.h>
73: #include <sys/mbuf.h>
74: #include <sys/protosw.h>
75: #include <sys/socket.h>
76: #include <sys/socketvar.h>
1.13 mycroft 77: #include <sys/errno.h>
1.5 mycroft 78: #include <sys/stat.h>
1.27 christos 79: #include <sys/systm.h>
80: #include <sys/proc.h>
1.47.6.3! itojun 81: #include <sys/domain.h>
1.27 christos 82:
83: #include <vm/vm.h>
84: #include <sys/sysctl.h>
1.1 cgd 85:
1.5 mycroft 86: #include <net/if.h>
87: #include <net/route.h>
1.1 cgd 88:
1.5 mycroft 89: #include <netinet/in.h>
90: #include <netinet/in_systm.h>
1.15 cgd 91: #include <netinet/in_var.h>
1.5 mycroft 92: #include <netinet/ip.h>
93: #include <netinet/in_pcb.h>
94: #include <netinet/ip_var.h>
95: #include <netinet/ip_icmp.h>
96: #include <netinet/udp.h>
97: #include <netinet/udp_var.h>
1.1 cgd 98:
1.47.6.3! itojun 99: #ifdef INET6
! 100: #include <netinet/ip6.h>
! 101: #include <netinet/icmp6.h>
! 102: #include <netinet6/ip6_var.h>
! 103: #include <netinet6/in6_pcb.h>
! 104: #include <netinet6/udp6_var.h>
! 105: #endif
! 106:
! 107: #ifdef PULLDOWN_TEST
! 108: #ifndef INET6
! 109: /* always need ip6.h for IP6_EXTHDR_GET */
! 110: #include <netinet/ip6.h>
! 111: #endif
! 112: #endif
! 113:
1.27 christos 114: #include <machine/stdarg.h>
115:
1.47.6.1 itojun 116: #ifdef IPSEC
117: #include <netinet6/ipsec.h>
118: #include <netkey/key.h>
119: #include <netkey/key_debug.h>
120: #endif /*IPSEC*/
121:
1.8 mycroft 122: /*
123: * UDP protocol implementation.
124: * Per RFC 768, August, 1980.
125: */
126: #ifndef COMPAT_42
127: int udpcksum = 1;
128: #else
129: int udpcksum = 0; /* XXX */
130: #endif
131:
1.47.6.3! itojun 132: static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *,
! 133: struct socket *));
! 134: static int udp4_realinput __P((struct sockaddr_in *, struct sockaddr_in *,
! 135: struct mbuf *, int));
! 136: #ifdef INET6
! 137: static void udp6_sendup __P((struct mbuf *, int, struct sockaddr *,
! 138: struct socket *));
! 139: static int in6_mcmatch __P((struct in6pcb *, struct in6_addr *,
! 140: struct ifnet *));
! 141: static int udp6_realinput __P((int, struct sockaddr_in6 *,
! 142: struct sockaddr_in6 *, struct mbuf *, int));
! 143: #endif
1.13 mycroft 144: static void udp_notify __P((struct inpcb *, int));
1.7 mycroft 145:
1.26 mycroft 146: #ifndef UDBHASHSIZE
147: #define UDBHASHSIZE 128
148: #endif
149: int udbhashsize = UDBHASHSIZE;
150:
1.7 mycroft 151: void
1.1 cgd 152: udp_init()
153: {
1.18 mycroft 154:
1.35 mycroft 155: in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.1 cgd 156: }
157:
1.7 mycroft 158: void
1.27 christos 159: #if __STDC__
160: udp_input(struct mbuf *m, ...)
161: #else
162: udp_input(m, va_alist)
163: struct mbuf *m;
164: va_dcl
165: #endif
1.1 cgd 166: {
1.47.6.3! itojun 167: va_list ap;
! 168: struct sockaddr_in src, dst;
! 169: struct ip *ip;
! 170: struct udphdr *uh;
! 171: int iphlen, proto;
! 172: int len;
! 173: int n;
! 174:
! 175: va_start(ap, m);
! 176: iphlen = va_arg(ap, int);
! 177: proto = va_arg(ap, int);
! 178: va_end(ap);
! 179:
! 180: udpstat.udps_ipackets++;
! 181:
! 182: #ifndef PULLDOWN_TEST
! 183: /*
! 184: * Strip IP options, if any; should skip this,
! 185: * make available to user, and use on returned packets,
! 186: * but we don't yet have a way to check the checksum
! 187: * with options still present.
! 188: */
! 189: if (iphlen > sizeof (struct ip)) {
! 190: ip_stripoptions(m, (struct mbuf *)0);
! 191: iphlen = sizeof(struct ip);
! 192: }
! 193: #else
! 194: /*
! 195: * we may enable the above code if we save and pass IPv4 options
! 196: * to the userland.
! 197: */
! 198: #endif
! 199:
! 200: /*
! 201: * Get IP and UDP header together in first mbuf.
! 202: */
! 203: ip = mtod(m, struct ip *);
! 204: #ifndef PULLDOWN_TEST
! 205: if (m->m_len < iphlen + sizeof(struct udphdr)) {
! 206: if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
! 207: udpstat.udps_hdrops++;
! 208: return;
! 209: }
! 210: ip = mtod(m, struct ip *);
! 211: }
! 212: uh = (struct udphdr *)((caddr_t)ip + iphlen);
! 213: #else
! 214: IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
! 215: if (uh == NULL) {
! 216: udpstat.udps_hdrops++;
! 217: return;
! 218: }
! 219: #endif
! 220:
! 221: /*
! 222: * Make mbuf data length reflect UDP length.
! 223: * If not enough data to reflect UDP length, drop.
! 224: */
! 225: len = ntohs((u_int16_t)uh->uh_ulen);
! 226: if (ip->ip_len != iphlen + len) {
! 227: if (ip->ip_len < iphlen + len) {
! 228: udpstat.udps_badlen++;
! 229: goto bad;
! 230: }
! 231: m_adj(m, iphlen + len - ip->ip_len);
! 232: }
! 233:
! 234: /*
! 235: * Checksum extended UDP header and data.
! 236: */
! 237: if (uh->uh_sum) {
! 238: #ifndef PULLDOWN_TEST
! 239: struct ip save_ip;
! 240:
! 241: /*
! 242: * Save a copy of the IP header in case we want restore it
! 243: * for sending an ICMP error message in response.
! 244: */
! 245: save_ip = *ip;
! 246:
! 247: bzero(((struct ipovly *)ip)->ih_x1,
! 248: sizeof ((struct ipovly *)ip)->ih_x1);
! 249: ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
! 250: if (in_cksum(m, len + sizeof (struct ip)) != 0) {
! 251: udpstat.udps_badsum++;
! 252: m_freem(m);
! 253: return;
! 254: }
! 255:
! 256: *ip = save_ip;
! 257: #else
! 258: if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0) {
! 259: udpstat.udps_badsum++;
! 260: m_freem(m);
! 261: return;
! 262: }
! 263: #endif
! 264: }
! 265:
! 266: /* construct source and dst sockaddrs. */
! 267: bzero(&src, sizeof(src));
! 268: src.sin_family = AF_INET;
! 269: src.sin_len = sizeof(struct sockaddr_in);
! 270: bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
! 271: src.sin_port = uh->uh_sport;
! 272: bzero(&dst, sizeof(dst));
! 273: dst.sin_family = AF_INET;
! 274: dst.sin_len = sizeof(struct sockaddr_in);
! 275: bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
! 276: dst.sin_port = uh->uh_dport;
! 277:
! 278: n = udp4_realinput(&src, &dst, m, iphlen);
! 279: #ifdef INET6
! 280: if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
! 281: struct sockaddr_in6 src6, dst6;
! 282:
! 283: bzero(&src6, sizeof(src6));
! 284: src6.sin6_family = AF_INET6;
! 285: src6.sin6_len = sizeof(struct sockaddr_in6);
! 286: src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
! 287: bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
! 288: sizeof(ip->ip_src));
! 289: src6.sin6_port = uh->uh_sport;
! 290: bzero(&dst6, sizeof(dst6));
! 291: dst6.sin6_family = AF_INET6;
! 292: dst6.sin6_len = sizeof(struct sockaddr_in6);
! 293: dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
! 294: bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
! 295: sizeof(ip->ip_dst));
! 296: dst6.sin6_port = uh->uh_dport;
! 297:
! 298: n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
! 299: }
! 300: #endif
! 301:
! 302: if (n == 0) {
! 303: udpstat.udps_noport++;
! 304: if (m->m_flags & (M_BCAST | M_MCAST)) {
! 305: udpstat.udps_noportbcast++;
! 306: goto bad;
! 307: }
! 308: #if NIPKDB > 0
! 309: if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
! 310: m, iphlen + sizeof(struct udphdr),
! 311: m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
! 312: /*
! 313: * It was a debugger connect packet,
! 314: * just drop it now
! 315: */
! 316: goto bad;
! 317: }
! 318: #endif
! 319: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
! 320: m = NULL;
! 321: }
! 322:
! 323: bad:
! 324: if (m)
! 325: m_freem(m);
! 326: }
! 327:
! 328: #ifdef INET6
! 329: int
! 330: udp6_input(mp, offp, proto)
! 331: struct mbuf **mp;
! 332: int *offp, proto;
! 333: {
! 334: struct mbuf *m = *mp;
! 335: int off = *offp;
! 336: struct sockaddr_in6 src, dst;
! 337: struct ip6_hdr *ip6;
! 338: struct udphdr *uh;
! 339: u_int32_t plen, ulen;
! 340:
! 341: #if defined(NFAITH) && 0 < NFAITH
! 342: if (m->m_pkthdr.rcvif) {
! 343: if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
! 344: /* send icmp6 host unreach? */
! 345: m_freem(m);
! 346: return IPPROTO_DONE;
! 347: }
! 348: }
! 349: #endif
! 350:
! 351: udp6stat.udp6s_ipackets++;
! 352:
! 353: #ifndef PULLDOWN_TEST
! 354: IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
! 355: #endif
! 356:
! 357: ip6 = mtod(m, struct ip6_hdr *);
! 358: /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
! 359: plen = m->m_pkthdr.len - off;
! 360: #ifndef PULLDOWN_TEST
! 361: uh = (struct udphdr *)((caddr_t)ip6 + off);
! 362: #else
! 363: IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
! 364: if (uh == NULL) {
! 365: ip6stat.ip6s_tooshort++;
! 366: return IPPROTO_DONE;
! 367: }
! 368: #endif
! 369: ulen = ntohs((u_short)uh->uh_ulen);
! 370: if (ulen == 0 && plen > 0xffff)
! 371: ulen = plen;
! 372:
! 373: if (plen != ulen) {
! 374: udp6stat.udp6s_badlen++;
! 375: goto bad;
! 376: }
! 377:
! 378: /*
! 379: * Checksum extended UDP header and data.
! 380: */
! 381: if (uh->uh_sum == 0)
! 382: udp6stat.udp6s_nosum++;
! 383: else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
! 384: udp6stat.udp6s_badsum++;
! 385: goto bad;
! 386: }
! 387:
! 388: /*
! 389: * Construct source and dst sockaddrs.
! 390: * Note that ifindex (s6_addr16[1]) is already filled.
! 391: */
! 392: bzero(&src, sizeof(src));
! 393: src.sin6_family = AF_INET6;
! 394: src.sin6_len = sizeof(struct sockaddr_in6);
! 395: bcopy(&ip6->ip6_src, &src.sin6_addr, sizeof(src.sin6_addr));
! 396: if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6_addr))
! 397: src.sin6_addr.s6_addr16[1] = 0;
! 398: if (m->m_pkthdr.rcvif) {
! 399: if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6_addr))
! 400: src.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
! 401: else
! 402: src.sin6_scope_id = 0;
! 403: }
! 404: src.sin6_port = uh->uh_sport;
! 405: bzero(&dst, sizeof(dst));
! 406: dst.sin6_family = AF_INET6;
! 407: dst.sin6_len = sizeof(struct sockaddr_in6);
! 408: bcopy(&ip6->ip6_dst, &dst.sin6_addr, sizeof(dst.sin6_addr));
! 409: if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr))
! 410: dst.sin6_addr.s6_addr16[1] = 0;
! 411: if (m->m_pkthdr.rcvif) {
! 412: if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr))
! 413: dst.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
! 414: else
! 415: dst.sin6_scope_id = 0;
! 416: }
! 417: dst.sin6_port = uh->uh_dport;
! 418:
! 419: if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
! 420: udp6stat.udp6s_noport++;
! 421: if (m->m_flags & M_MCAST) {
! 422: udp6stat.udp6s_noportmcast++;
! 423: goto bad;
! 424: }
! 425: icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
! 426: m = NULL;
! 427: }
! 428:
! 429: bad:
! 430: if (m)
! 431: m_freem(m);
! 432: return IPPROTO_DONE;
! 433: }
! 434: #endif
! 435:
! 436: static void
! 437: udp4_sendup(m, off, src, so)
! 438: struct mbuf *m;
! 439: int off; /* offset of data portion */
! 440: struct sockaddr *src;
! 441: struct socket *so;
! 442: {
! 443: struct mbuf *opts = NULL;
! 444: struct mbuf *n;
! 445: struct inpcb *inp = NULL;
! 446: #ifdef INET6
! 447: struct in6pcb *in6p = NULL;
! 448: #endif
! 449:
! 450: if (!so)
! 451: return;
! 452: switch (so->so_proto->pr_domain->dom_family) {
! 453: case AF_INET:
! 454: inp = sotoinpcb(so);
! 455: break;
! 456: #ifdef INET6
! 457: case AF_INET6:
! 458: in6p = sotoin6pcb(so);
! 459: break;
! 460: #endif
! 461: default:
! 462: return;
! 463: }
! 464:
! 465: #ifdef IPSEC
! 466: /* check AH/ESP integrity. */
! 467: if (so != NULL && ipsec4_in_reject_so(m, so)) {
! 468: ipsecstat.in_polvio++;
! 469: return;
! 470: }
! 471: #endif /*IPSEC*/
! 472:
! 473: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
! 474: if (inp && (inp->inp_flags & INP_CONTROLOPTS
! 475: || so->so_options & SO_TIMESTAMP)) {
! 476: struct ip *ip = mtod(n, struct ip *);
! 477: ip_savecontrol(inp, &opts, ip, n);
! 478: }
! 479:
! 480: m_adj(n, off);
! 481: if (sbappendaddr(&so->so_rcv, src, n,
! 482: opts) == 0) {
! 483: m_freem(n);
! 484: if (opts)
! 485: m_freem(opts);
! 486: } else
! 487: sorwakeup(so);
! 488: }
! 489: }
! 490:
! 491: #ifdef INET6
! 492: static void
! 493: udp6_sendup(m, off, src, so)
! 494: struct mbuf *m;
! 495: int off; /* offset of data portion */
! 496: struct sockaddr *src;
! 497: struct socket *so;
! 498: {
! 499: struct mbuf *opts = NULL;
! 500: struct mbuf *n;
! 501: struct in6pcb *in6p = NULL;
! 502:
! 503: if (!so)
! 504: return;
! 505: if (so->so_proto->pr_domain->dom_family != AF_INET6)
! 506: return;
! 507: in6p = sotoin6pcb(so);
! 508:
! 509: #ifdef IPSEC
! 510: /* check AH/ESP integrity. */
! 511: if (so != NULL && ipsec6_in_reject_so(m, so)) {
! 512: ipsec6stat.in_polvio++;
! 513: return;
! 514: }
! 515: #endif /*IPSEC*/
! 516:
! 517: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
! 518: if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
! 519: || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
! 520: struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
! 521: ip6_savecontrol(in6p, &opts, ip6, n);
! 522: }
! 523:
! 524: m_adj(n, off);
! 525: if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
! 526: m_freem(n);
! 527: if (opts)
! 528: m_freem(opts);
! 529: udp6stat.udp6s_fullsock++;
! 530: } else
! 531: sorwakeup(so);
! 532: }
! 533: }
! 534: #endif
! 535:
! 536: static int
! 537: udp4_realinput(src, dst, m, off)
! 538: struct sockaddr_in *src;
! 539: struct sockaddr_in *dst;
! 540: struct mbuf *m;
! 541: int off; /* offset of udphdr */
! 542: {
! 543: u_int16_t *sport, *dport;
! 544: int rcvcnt;
! 545: struct in_addr *src4, *dst4;
! 546: struct inpcb *inp;
! 547:
! 548: rcvcnt = 0;
! 549: off += sizeof(struct udphdr); /* now, offset of payload */
! 550:
! 551: if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
! 552: goto bad;
! 553:
! 554: src4 = &src->sin_addr;
! 555: sport = &src->sin_port;
! 556: dst4 = &dst->sin_addr;
! 557: dport = &dst->sin_port;
! 558:
! 559: if (IN_MULTICAST(src4->s_addr) ||
! 560: in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
! 561: struct inpcb *last;
! 562: /*
! 563: * Deliver a multicast or broadcast datagram to *all* sockets
! 564: * for which the local and remote addresses and ports match
! 565: * those of the incoming datagram. This allows more than
! 566: * one process to receive multi/broadcasts on the same port.
! 567: * (This really ought to be done for unicast datagrams as
! 568: * well, but that would cause problems with existing
! 569: * applications that open both address-specific sockets and
! 570: * a wildcard socket listening to the same port -- they would
! 571: * end up receiving duplicates of every unicast datagram.
! 572: * Those applications open the multiple sockets to overcome an
! 573: * inadequacy of the UDP socket interface, but for backwards
! 574: * compatibility we avoid the problem here rather than
! 575: * fixing the interface. Maybe 4.5BSD will remedy this?)
! 576: */
! 577:
! 578: /*
! 579: * KAME note: usually we drop udpiphdr from mbuf here.
! 580: * we need udpiphdr for iPsec processing so we do that later.
! 581: */
! 582: /*
! 583: * Locate pcb(s) for datagram.
! 584: */
! 585: for (inp = udbtable.inpt_queue.cqh_first;
! 586: inp != (struct inpcb *)&udbtable.inpt_queue;
! 587: inp = inp->inp_queue.cqe_next) {
! 588: if (inp->inp_lport != *dport)
! 589: continue;
! 590: if (!in_nullhost(inp->inp_laddr)) {
! 591: if (!in_hosteq(inp->inp_laddr, *dst4))
! 592: continue;
! 593: }
! 594: if (!in_nullhost(inp->inp_faddr)) {
! 595: if (!in_hosteq(inp->inp_faddr, *src4) ||
! 596: inp->inp_fport != *sport)
! 597: continue;
! 598: }
! 599:
! 600: last = inp;
! 601: udp4_sendup(m, off, (struct sockaddr *)src,
! 602: inp->inp_socket);
! 603: rcvcnt++;
! 604:
! 605: /*
! 606: * Don't look for additional matches if this one does
! 607: * not have either the SO_REUSEPORT or SO_REUSEADDR
! 608: * socket options set. This heuristic avoids searching
! 609: * through all pcbs in the common case of a non-shared
! 610: * port. It assumes that an application will never
! 611: * clear these options after setting them.
! 612: */
! 613: if ((inp->inp_socket->so_options &
! 614: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
! 615: break;
! 616: }
! 617:
! 618: #if 0
! 619: if (last == NULL) {
! 620: /*
! 621: * No matching pcb found; discard datagram.
! 622: * (No need to send an ICMP Port Unreachable
! 623: * for a broadcast or multicast datgram.)
! 624: */
! 625: udpstat.udps_noport++;
! 626: udpstat.udps_noportbcast++;
! 627: goto bad;
! 628: }
! 629: #endif
! 630: } else {
! 631: /*
! 632: * Locate pcb for datagram.
! 633: */
! 634: inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
! 635: if (inp == 0) {
! 636: ++udpstat.udps_pcbhashmiss;
! 637: inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
! 638: if (inp == 0) {
! 639: #if 0
! 640: struct mbuf *n;
! 641:
! 642: udpstat.udps_noport++;
! 643: if (m->m_flags & (M_BCAST | M_MCAST)) {
! 644: udpstat.udps_noportbcast++;
! 645: goto bad;
! 646: }
! 647: #if NIPKDB > 0
! 648: if (checkipkdb(src4, *sport, *dport, m, off,
! 649: m->m_pkthdr.len - off)) {
! 650: /*
! 651: * It was a debugger connect packet,
! 652: * just drop it now
! 653: */
! 654: goto bad;
! 655: }
! 656: #endif
! 657: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
! 658: icmp_error(n, ICMP_UNREACH,
! 659: ICMP_UNREACH_PORT, 0, 0);
! 660: }
! 661: #endif
! 662: return rcvcnt;
! 663: }
! 664: }
! 665:
! 666: udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
! 667: rcvcnt++;
! 668: }
! 669:
! 670: bad:
! 671: return rcvcnt;
! 672: }
! 673:
! 674: #ifdef INET6
! 675: static int
! 676: in6_mcmatch(in6p, ia6, ifp)
! 677: struct in6pcb *in6p;
! 678: register struct in6_addr *ia6;
! 679: struct ifnet *ifp;
! 680: {
! 681: struct ip6_moptions *im6o = in6p->in6p_moptions;
! 682: struct in6_multi_mship *imm;
! 683:
! 684: if (im6o == NULL)
! 685: return 0;
! 686:
! 687: for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
! 688: imm = imm->i6mm_chain.le_next) {
! 689: if ((ifp == NULL ||
! 690: imm->i6mm_maddr->in6m_ifp == ifp) &&
! 691: IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
! 692: ia6))
! 693: return 1;
! 694: }
! 695: return 0;
! 696: }
! 697:
! 698: static int
! 699: udp6_realinput(af, src, dst, m, off)
! 700: int af; /* af on packet */
! 701: struct sockaddr_in6 *src;
! 702: struct sockaddr_in6 *dst;
! 703: struct mbuf *m;
! 704: int off; /* offset of udphdr */
! 705: {
! 706: u_int16_t *sport, *dport;
! 707: int rcvcnt;
! 708: struct in6_addr *src6, *dst6;
! 709: struct in_addr *src4;
! 710: struct in6pcb *in6p;
! 711:
! 712: rcvcnt = 0;
! 713: off += sizeof(struct udphdr); /* now, offset of payload */
! 714:
! 715: if (af != AF_INET && af != AF_INET6)
! 716: goto bad;
! 717: if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
! 718: goto bad;
! 719:
! 720: src6 = &src->sin6_addr;
! 721: sport = &src->sin6_port;
! 722: dst6 = &dst->sin6_addr;
! 723: dport = &dst->sin6_port;
! 724: src4 = (struct in_addr *)&src->sin6_addr.s6_addr32[12];
! 725:
! 726: if (IN6_IS_ADDR_MULTICAST(dst6)
! 727: || (af == AF_INET && IN_MULTICAST(src4->s_addr))) {
! 728: struct in6pcb *last;
! 729: /*
! 730: * Deliver a multicast or broadcast datagram to *all* sockets
! 731: * for which the local and remote addresses and ports match
! 732: * those of the incoming datagram. This allows more than
! 733: * one process to receive multi/broadcasts on the same port.
! 734: * (This really ought to be done for unicast datagrams as
! 735: * well, but that would cause problems with existing
! 736: * applications that open both address-specific sockets and
! 737: * a wildcard socket listening to the same port -- they would
! 738: * end up receiving duplicates of every unicast datagram.
! 739: * Those applications open the multiple sockets to overcome an
! 740: * inadequacy of the UDP socket interface, but for backwards
! 741: * compatibility we avoid the problem here rather than
! 742: * fixing the interface. Maybe 4.5BSD will remedy this?)
! 743: */
! 744:
! 745: /*
! 746: * KAME note: usually we drop udpiphdr from mbuf here.
! 747: * we need udpiphdr for iPsec processing so we do that later.
! 748: */
! 749: /*
! 750: * Locate pcb(s) for datagram.
! 751: */
! 752: for (in6p = udb6.in6p_next; in6p != &udb6;
! 753: in6p = in6p->in6p_next) {
! 754: if (in6p->in6p_lport != *dport)
! 755: continue;
! 756: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
! 757: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, dst6)
! 758: && !in6_mcmatch(in6p, dst6, m->m_pkthdr.rcvif))
! 759: continue;
! 760: }
! 761: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
! 762: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, src6)
! 763: || in6p->in6p_fport != *sport)
! 764: continue;
! 765: }
! 766:
! 767: last = in6p;
! 768: udp6_sendup(m, off, (struct sockaddr *)src,
! 769: in6p->in6p_socket);
! 770: rcvcnt++;
! 771:
! 772: /*
! 773: * Don't look for additional matches if this one does
! 774: * not have either the SO_REUSEPORT or SO_REUSEADDR
! 775: * socket options set. This heuristic avoids searching
! 776: * through all pcbs in the common case of a non-shared
! 777: * port. It assumes that an application will never
! 778: * clear these options after setting them.
! 779: */
! 780: if ((in6p->in6p_socket->so_options &
! 781: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
! 782: break;
! 783: }
! 784:
! 785: #if 0
! 786: if (last == NULL) {
! 787: /*
! 788: * No matching pcb found; discard datagram.
! 789: * (No need to send an ICMP Port Unreachable
! 790: * for a broadcast or multicast datgram.)
! 791: */
! 792: switch (af) {
! 793: case AF_INET:
! 794: udpstat.udps_noport++;
! 795: udpstat.udps_noportbcast++;
! 796: break;
! 797: case AF_INET6:
! 798: udp6stat.udp6s_noport++;
! 799: udp6stat.udp6s_noportmcast++;
! 800: break;
! 801: }
! 802: goto bad;
! 803: }
! 804: #endif
! 805: } else {
! 806: /*
! 807: * Locate pcb for datagram.
! 808: */
! 809: in6p = in6_pcblookup_connect(&udb6, src6, *sport,
! 810: dst6, *dport, 0);
! 811: if (in6p == 0) {
! 812: ++udpstat.udps_pcbhashmiss;
! 813: in6p = in6_pcblookup_bind(&udb6, dst6, *dport, 0);
! 814: if (in6p == 0) {
! 815: #if 0
! 816: struct mbuf *n;
! 817: n = m_copy(m, 0, M_COPYALL);
! 818: switch (af) {
! 819: case AF_INET:
! 820: udpstat.udps_noport++;
! 821: if (m->m_flags & (M_BCAST | M_MCAST)) {
! 822: udpstat.udps_noportbcast++;
! 823: goto bad;
! 824: }
! 825: if (n != NULL)
! 826: icmp_error(n, ICMP_UNREACH,
! 827: ICMP_UNREACH_PORT, 0, 0);
! 828: break;
! 829: case AF_INET6:
! 830: udp6stat.udp6s_noport++;
! 831: if (m->m_flags & M_MCAST) {
! 832: udp6stat.udp6s_noportmcast++;
! 833: goto bad;
! 834: }
! 835: if (n != NULL)
! 836: icmp6_error(n, ICMP6_DST_UNREACH,
! 837: ICMP6_DST_UNREACH_NOPORT, 0);
! 838: break;
! 839: }
! 840: #endif
! 841:
! 842: return rcvcnt;
! 843: }
! 844: }
! 845:
! 846: udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
! 847: rcvcnt++;
! 848: }
! 849:
! 850: bad:
! 851: return rcvcnt;
! 852: }
! 853: #endif
! 854:
! 855: #if 0
! 856: void
! 857: #if __STDC__
! 858: udp_input(struct mbuf *m, ...)
! 859: #else
! 860: udp_input(m, va_alist)
! 861: struct mbuf *m;
! 862: va_dcl
! 863: #endif
! 864: {
1.47.6.1 itojun 865: int proto;
1.1 cgd 866: register struct ip *ip;
867: register struct udphdr *uh;
868: register struct inpcb *inp;
869: struct mbuf *opts = 0;
870: int len;
871: struct ip save_ip;
1.27 christos 872: int iphlen;
873: va_list ap;
1.34 mycroft 874: struct sockaddr_in udpsrc;
1.47.6.1 itojun 875: struct sockaddr *sa;
1.27 christos 876:
877: va_start(ap, m);
878: iphlen = va_arg(ap, int);
1.47.6.1 itojun 879: proto = va_arg(ap, int);
1.27 christos 880: va_end(ap);
1.1 cgd 881:
882: udpstat.udps_ipackets++;
883:
884: /*
885: * Strip IP options, if any; should skip this,
886: * make available to user, and use on returned packets,
887: * but we don't yet have a way to check the checksum
888: * with options still present.
889: */
890: if (iphlen > sizeof (struct ip)) {
891: ip_stripoptions(m, (struct mbuf *)0);
892: iphlen = sizeof(struct ip);
893: }
894:
895: /*
896: * Get IP and UDP header together in first mbuf.
897: */
898: ip = mtod(m, struct ip *);
899: if (m->m_len < iphlen + sizeof(struct udphdr)) {
900: if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
901: udpstat.udps_hdrops++;
902: return;
903: }
904: ip = mtod(m, struct ip *);
905: }
906: uh = (struct udphdr *)((caddr_t)ip + iphlen);
907:
908: /*
909: * Make mbuf data length reflect UDP length.
910: * If not enough data to reflect UDP length, drop.
911: */
1.15 cgd 912: len = ntohs((u_int16_t)uh->uh_ulen);
1.47 mycroft 913: if (ip->ip_len != iphlen + len) {
914: if (ip->ip_len < iphlen + len) {
1.1 cgd 915: udpstat.udps_badlen++;
916: goto bad;
917: }
1.47 mycroft 918: m_adj(m, iphlen + len - ip->ip_len);
1.1 cgd 919: }
920: /*
921: * Save a copy of the IP header in case we want restore it
922: * for sending an ICMP error message in response.
923: */
924: save_ip = *ip;
925:
926: /*
927: * Checksum extended UDP header and data.
928: */
1.29 mrg 929: if (uh->uh_sum) {
1.25 cgd 930: bzero(((struct ipovly *)ip)->ih_x1,
931: sizeof ((struct ipovly *)ip)->ih_x1);
1.1 cgd 932: ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
1.46 mycroft 933: if (in_cksum(m, len + sizeof (struct ip)) != 0) {
1.1 cgd 934: udpstat.udps_badsum++;
935: m_freem(m);
936: return;
937: }
938: }
1.13 mycroft 939:
1.47.6.1 itojun 940: /*
941: * Construct sockaddr format source address.
942: */
943: udpsrc.sin_family = AF_INET;
944: udpsrc.sin_len = sizeof(struct sockaddr_in);
945: udpsrc.sin_addr = ip->ip_src;
946: udpsrc.sin_port = uh->uh_sport;
947: bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero));
948:
1.16 mycroft 949: if (IN_MULTICAST(ip->ip_dst.s_addr) ||
1.13 mycroft 950: in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1.40 thorpej 951: struct inpcb *last;
1.4 hpeyerl 952: /*
953: * Deliver a multicast or broadcast datagram to *all* sockets
954: * for which the local and remote addresses and ports match
955: * those of the incoming datagram. This allows more than
956: * one process to receive multi/broadcasts on the same port.
957: * (This really ought to be done for unicast datagrams as
958: * well, but that would cause problems with existing
959: * applications that open both address-specific sockets and
960: * a wildcard socket listening to the same port -- they would
961: * end up receiving duplicates of every unicast datagram.
962: * Those applications open the multiple sockets to overcome an
963: * inadequacy of the UDP socket interface, but for backwards
964: * compatibility we avoid the problem here rather than
1.13 mycroft 965: * fixing the interface. Maybe 4.5BSD will remedy this?)
1.4 hpeyerl 966: */
1.13 mycroft 967:
1.47.6.1 itojun 968: iphlen += sizeof(struct udphdr);
1.4 hpeyerl 969: /*
1.47.6.1 itojun 970: * KAME note: usually we drop udpiphdr from mbuf here.
971: * we need udpiphdr for iPsec processing so we do that later.
1.4 hpeyerl 972: */
973: /*
974: * Locate pcb(s) for datagram.
975: * (Algorithm copied from raw_intr().)
976: */
977: last = NULL;
1.22 cgd 978: for (inp = udbtable.inpt_queue.cqh_first;
979: inp != (struct inpcb *)&udbtable.inpt_queue;
980: inp = inp->inp_queue.cqe_next) {
1.4 hpeyerl 981: if (inp->inp_lport != uh->uh_dport)
982: continue;
1.34 mycroft 983: if (!in_nullhost(inp->inp_laddr)) {
984: if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
1.4 hpeyerl 985: continue;
986: }
1.34 mycroft 987: if (!in_nullhost(inp->inp_faddr)) {
988: if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
1.4 hpeyerl 989: inp->inp_fport != uh->uh_sport)
990: continue;
991: }
992:
993: if (last != NULL) {
994: struct mbuf *n;
995:
1.47.6.1 itojun 996: #ifdef IPSEC
997: /* check AH/ESP integrity. */
998: if (last != NULL && ipsec4_in_reject(m, last)) {
999: ipsecstat.in_polvio++;
1000: /* do not inject data to pcb */
1001: } else
1002: #endif /*IPSEC*/
1.4 hpeyerl 1003: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
1.40 thorpej 1004: if (last->inp_flags & INP_CONTROLOPTS
1005: || last->inp_socket->so_options &
1006: SO_TIMESTAMP) {
1007: ip_savecontrol(last, &opts,
1008: ip, n);
1009: }
1.47.6.3! itojun 1010: m_adj(n, iphlen);
1.47.6.1 itojun 1011: sa = (struct sockaddr *)&udpsrc;
1.40 thorpej 1012: if (sbappendaddr(
1013: &last->inp_socket->so_rcv,
1.47.6.1 itojun 1014: sa, n, opts) == 0) {
1.4 hpeyerl 1015: m_freem(n);
1.40 thorpej 1016: if (opts)
1017: m_freem(opts);
1.13 mycroft 1018: } else
1.40 thorpej 1019: sorwakeup(last->inp_socket);
1020: opts = 0;
1.4 hpeyerl 1021: }
1022: }
1.40 thorpej 1023: last = inp;
1.4 hpeyerl 1024: /*
1.13 mycroft 1025: * Don't look for additional matches if this one does
1026: * not have either the SO_REUSEPORT or SO_REUSEADDR
1027: * socket options set. This heuristic avoids searching
1028: * through all pcbs in the common case of a non-shared
1029: * port. It * assumes that an application will never
1030: * clear these options after setting them.
1.4 hpeyerl 1031: */
1.40 thorpej 1032: if ((last->inp_socket->so_options &
1033: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
1.4 hpeyerl 1034: break;
1035: }
1.6 mycroft 1036:
1.4 hpeyerl 1037: if (last == NULL) {
1038: /*
1039: * No matching pcb found; discard datagram.
1040: * (No need to send an ICMP Port Unreachable
1041: * for a broadcast or multicast datgram.)
1042: */
1.47.6.1 itojun 1043: udpstat.udps_noport++;
1.13 mycroft 1044: udpstat.udps_noportbcast++;
1.4 hpeyerl 1045: goto bad;
1046: }
1.47.6.1 itojun 1047: #ifdef IPSEC
1048: /* check AH/ESP integrity. */
1049: if (last != NULL && ipsec4_in_reject(m, last)) {
1050: ipsecstat.in_polvio++;
1051: goto bad;
1052: }
1053: #endif /*IPSEC*/
1.40 thorpej 1054: if (last->inp_flags & INP_CONTROLOPTS ||
1055: last->inp_socket->so_options & SO_TIMESTAMP)
1056: ip_savecontrol(last, &opts, ip, m);
1.47.6.3! itojun 1057: m->m_len -= iphlen;
! 1058: m->m_pkthdr.len -= iphlen;
! 1059: m->m_data += iphlen;
1.47.6.1 itojun 1060: sa = (struct sockaddr *)&udpsrc;
1061: if (sbappendaddr(&last->inp_socket->so_rcv, sa, m, opts) == 0) {
1.13 mycroft 1062: udpstat.udps_fullsock++;
1.4 hpeyerl 1063: goto bad;
1.13 mycroft 1064: }
1.40 thorpej 1065: sorwakeup(last->inp_socket);
1.4 hpeyerl 1066: return;
1067: }
1.1 cgd 1068: /*
1069: * Locate pcb for datagram.
1070: */
1.35 mycroft 1071: inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport,
1.26 mycroft 1072: ip->ip_dst, uh->uh_dport);
1073: if (inp == 0) {
1074: ++udpstat.udps_pcbhashmiss;
1.35 mycroft 1075: inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport);
1.18 mycroft 1076: if (inp == 0) {
1077: udpstat.udps_noport++;
1078: if (m->m_flags & (M_BCAST | M_MCAST)) {
1079: udpstat.udps_noportbcast++;
1080: goto bad;
1081: }
1082: *ip = save_ip;
1.38 ws 1083: #if NIPKDB > 0
1084: if (checkipkdb(&ip->ip_src,
1085: uh->uh_sport,
1086: uh->uh_dport,
1087: m,
1088: iphlen + sizeof(struct udphdr),
1089: len - sizeof(struct udphdr)))
1.37 ws 1090: /* It was a debugger connect packet, just drop it now */
1091: goto bad;
1092: #endif
1.18 mycroft 1093: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
1094: return;
1.13 mycroft 1095: }
1.1 cgd 1096: }
1.47.6.1 itojun 1097: #ifdef IPSEC
1098: if (inp != NULL && ipsec4_in_reject(m, inp)) {
1099: ipsecstat.in_polvio++;
1100: goto bad;
1101: }
1102: #endif /*IPSEC*/
1.1 cgd 1103:
1104: /*
1105: * Stuff source address and datagram in user buffer.
1106: */
1.40 thorpej 1107: if (inp->inp_flags & INP_CONTROLOPTS ||
1108: inp->inp_socket->so_options & SO_TIMESTAMP)
1109: ip_savecontrol(inp, &opts, ip, m);
1.1 cgd 1110: iphlen += sizeof(struct udphdr);
1111: m->m_len -= iphlen;
1112: m->m_pkthdr.len -= iphlen;
1113: m->m_data += iphlen;
1.47.6.1 itojun 1114: sa = (struct sockaddr *)&udpsrc;
1115: if (sbappendaddr(&inp->inp_socket->so_rcv, sa, m, opts) == 0) {
1.1 cgd 1116: udpstat.udps_fullsock++;
1117: goto bad;
1118: }
1119: sorwakeup(inp->inp_socket);
1120: return;
1121: bad:
1122: m_freem(m);
1123: if (opts)
1124: m_freem(opts);
1125: }
1.47.6.3! itojun 1126: #endif
1.1 cgd 1127:
1128: /*
1129: * Notify a udp user of an asynchronous error;
1130: * just wake up so that he can collect error status.
1131: */
1.7 mycroft 1132: static void
1.1 cgd 1133: udp_notify(inp, errno)
1134: register struct inpcb *inp;
1.7 mycroft 1135: int errno;
1.1 cgd 1136: {
1.34 mycroft 1137:
1.1 cgd 1138: inp->inp_socket->so_error = errno;
1139: sorwakeup(inp->inp_socket);
1140: sowwakeup(inp->inp_socket);
1141: }
1142:
1.27 christos 1143: void *
1144: udp_ctlinput(cmd, sa, v)
1.1 cgd 1145: int cmd;
1146: struct sockaddr *sa;
1.27 christos 1147: void *v;
1.1 cgd 1148: {
1.27 christos 1149: register struct ip *ip = v;
1.1 cgd 1150: register struct udphdr *uh;
1.21 mycroft 1151: extern int inetctlerrmap[];
1.18 mycroft 1152: void (*notify) __P((struct inpcb *, int)) = udp_notify;
1.21 mycroft 1153: int errno;
1.1 cgd 1154:
1.47.6.3! itojun 1155: if (sa->sa_family != AF_INET
! 1156: || sa->sa_len != sizeof(struct sockaddr_in))
! 1157: return NULL;
1.20 mycroft 1158: if ((unsigned)cmd >= PRC_NCMDS)
1.27 christos 1159: return NULL;
1.20 mycroft 1160: errno = inetctlerrmap[cmd];
1.18 mycroft 1161: if (PRC_IS_REDIRECT(cmd))
1.19 mycroft 1162: notify = in_rtchange, ip = 0;
1.18 mycroft 1163: else if (cmd == PRC_HOSTDEAD)
1.19 mycroft 1164: ip = 0;
1.23 cgd 1165: else if (errno == 0)
1.27 christos 1166: return NULL;
1.19 mycroft 1167: if (ip) {
1.1 cgd 1168: uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34 mycroft 1169: in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
1170: ip->ip_src, uh->uh_sport, errno, notify);
1.47.6.3! itojun 1171:
! 1172: /* XXX mapped address case */
1.19 mycroft 1173: } else
1.34 mycroft 1174: in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
1175: notify);
1.27 christos 1176: return NULL;
1.1 cgd 1177: }
1178:
1.7 mycroft 1179: int
1.27 christos 1180: #if __STDC__
1181: udp_output(struct mbuf *m, ...)
1182: #else
1183: udp_output(m, va_alist)
1184: struct mbuf *m;
1185: va_dcl
1186: #endif
1187: {
1.1 cgd 1188: register struct inpcb *inp;
1189: register struct udpiphdr *ui;
1190: register int len = m->m_pkthdr.len;
1.31 mycroft 1191: int error = 0;
1.27 christos 1192: va_list ap;
1193:
1194: va_start(ap, m);
1195: inp = va_arg(ap, struct inpcb *);
1196: va_end(ap);
1.1 cgd 1197:
1198: /*
1199: * Calculate data length and get a mbuf
1200: * for UDP and IP headers.
1201: */
1.13 mycroft 1202: M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1203: if (m == 0) {
1204: error = ENOBUFS;
1.39 thorpej 1205: goto release;
1206: }
1207:
1208: /*
1209: * Compute the packet length of the IP header, and
1210: * punt if the length looks bogus.
1211: */
1212: if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
1213: error = EMSGSIZE;
1.13 mycroft 1214: goto release;
1215: }
1.1 cgd 1216:
1217: /*
1218: * Fill in mbuf with extended UDP header
1219: * and addresses and length put into network format.
1220: */
1221: ui = mtod(m, struct udpiphdr *);
1.25 cgd 1222: bzero(ui->ui_x1, sizeof ui->ui_x1);
1.1 cgd 1223: ui->ui_pr = IPPROTO_UDP;
1.15 cgd 1224: ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1.1 cgd 1225: ui->ui_src = inp->inp_laddr;
1226: ui->ui_dst = inp->inp_faddr;
1227: ui->ui_sport = inp->inp_lport;
1228: ui->ui_dport = inp->inp_fport;
1229: ui->ui_ulen = ui->ui_len;
1230:
1231: /*
1232: * Stuff checksum and output datagram.
1233: */
1234: ui->ui_sum = 0;
1235: if (udpcksum) {
1236: if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
1237: ui->ui_sum = 0xffff;
1238: }
1239: ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
1240: ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
1241: ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1242: udpstat.udps_opackets++;
1.47.6.1 itojun 1243:
1244: #ifdef IPSEC
1245: m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
1246: #endif /*IPSEC*/
1247:
1.31 mycroft 1248: return (ip_output(m, inp->inp_options, &inp->inp_route,
1.12 mycroft 1249: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.31 mycroft 1250: inp->inp_moptions));
1.1 cgd 1251:
1252: release:
1253: m_freem(m);
1254: return (error);
1255: }
1256:
1.42 thorpej 1257: int udp_sendspace = 9216; /* really max datagram size */
1258: int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1 cgd 1259: /* 40 1K datagrams */
1260:
1261: /*ARGSUSED*/
1.7 mycroft 1262: int
1.31 mycroft 1263: udp_usrreq(so, req, m, nam, control, p)
1.1 cgd 1264: struct socket *so;
1265: int req;
1.31 mycroft 1266: struct mbuf *m, *nam, *control;
1.30 mycroft 1267: struct proc *p;
1.1 cgd 1268: {
1.31 mycroft 1269: register struct inpcb *inp;
1.1 cgd 1270: int s;
1.31 mycroft 1271: register int error = 0;
1.1 cgd 1272:
1273: if (req == PRU_CONTROL)
1.31 mycroft 1274: return (in_control(so, (long)m, (caddr_t)nam,
1.30 mycroft 1275: (struct ifnet *)control, p));
1.31 mycroft 1276:
1277: s = splsoftnet();
1278: inp = sotoinpcb(so);
1.32 mycroft 1279: #ifdef DIAGNOSTIC
1280: if (req != PRU_SEND && req != PRU_SENDOOB && control)
1281: panic("udp_usrreq: unexpected control mbuf");
1282: #endif
1.31 mycroft 1283: if (inp == 0 && req != PRU_ATTACH) {
1284: error = EINVAL;
1285: goto release;
1286: }
1287:
1.1 cgd 1288: /*
1289: * Note: need to block udp_input while changing
1290: * the udp pcb queue and/or pcb addresses.
1291: */
1292: switch (req) {
1293:
1294: case PRU_ATTACH:
1.31 mycroft 1295: if (inp != 0) {
1296: error = EISCONN;
1.1 cgd 1297: break;
1298: }
1.31 mycroft 1299: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1300: error = soreserve(so, udp_sendspace, udp_recvspace);
1301: if (error)
1302: break;
1303: }
1.18 mycroft 1304: error = in_pcballoc(so, &udbtable);
1.1 cgd 1305: if (error)
1306: break;
1.31 mycroft 1307: inp = sotoinpcb(so);
1308: inp->inp_ip.ip_ttl = ip_defttl;
1.47.6.1 itojun 1309: #ifdef IPSEC
1.47.6.3! itojun 1310: error = ipsec_init_policy(so, &inp->inp_sp);
! 1311: if (error != 0) {
! 1312: in_pcbdetach(inp);
! 1313: break;
! 1314: }
1.47.6.1 itojun 1315: #endif /*IPSEC*/
1.1 cgd 1316: break;
1317:
1318: case PRU_DETACH:
1.31 mycroft 1319: in_pcbdetach(inp);
1.1 cgd 1320: break;
1321:
1322: case PRU_BIND:
1.31 mycroft 1323: error = in_pcbbind(inp, nam, p);
1.1 cgd 1324: break;
1325:
1326: case PRU_LISTEN:
1327: error = EOPNOTSUPP;
1328: break;
1329:
1330: case PRU_CONNECT:
1.31 mycroft 1331: error = in_pcbconnect(inp, nam);
1332: if (error)
1.1 cgd 1333: break;
1.31 mycroft 1334: soisconnected(so);
1.1 cgd 1335: break;
1336:
1337: case PRU_CONNECT2:
1338: error = EOPNOTSUPP;
1339: break;
1340:
1341: case PRU_DISCONNECT:
1.31 mycroft 1342: /*soisdisconnected(so);*/
1343: so->so_state &= ~SS_ISCONNECTED; /* XXX */
1.1 cgd 1344: in_pcbdisconnect(inp);
1.34 mycroft 1345: inp->inp_laddr = zeroin_addr; /* XXX */
1.35 mycroft 1346: in_pcbstate(inp, INP_BOUND); /* XXX */
1.1 cgd 1347: break;
1348:
1349: case PRU_SHUTDOWN:
1350: socantsendmore(so);
1351: break;
1352:
1.31 mycroft 1353: case PRU_RCVD:
1354: error = EOPNOTSUPP;
1.1 cgd 1355: break;
1356:
1.31 mycroft 1357: case PRU_SEND:
1.32 mycroft 1358: if (control && control->m_len) {
1359: m_freem(control);
1360: m_freem(m);
1361: error = EINVAL;
1362: break;
1363: }
1.31 mycroft 1364: {
1.35 mycroft 1365: struct in_addr laddr; /* XXX */
1.1 cgd 1366:
1.31 mycroft 1367: if (nam) {
1.35 mycroft 1368: laddr = inp->inp_laddr; /* XXX */
1.31 mycroft 1369: if ((so->so_state & SS_ISCONNECTED) != 0) {
1370: error = EISCONN;
1.32 mycroft 1371: goto die;
1.31 mycroft 1372: }
1373: error = in_pcbconnect(inp, nam);
1.32 mycroft 1374: if (error) {
1375: die:
1376: m_freem(m);
1.31 mycroft 1377: break;
1.32 mycroft 1378: }
1.31 mycroft 1379: } else {
1380: if ((so->so_state & SS_ISCONNECTED) == 0) {
1381: error = ENOTCONN;
1.32 mycroft 1382: goto die;
1.31 mycroft 1383: }
1384: }
1.33 mycroft 1385: error = udp_output(m, inp);
1.31 mycroft 1386: if (nam) {
1387: in_pcbdisconnect(inp);
1.35 mycroft 1388: inp->inp_laddr = laddr; /* XXX */
1389: in_pcbstate(inp, INP_BOUND); /* XXX */
1.31 mycroft 1390: }
1391: }
1.1 cgd 1392: break;
1393:
1394: case PRU_SENSE:
1395: /*
1396: * stat: don't bother with a blocksize.
1397: */
1.31 mycroft 1398: splx(s);
1.1 cgd 1399: return (0);
1400:
1.31 mycroft 1401: case PRU_RCVOOB:
1402: error = EOPNOTSUPP;
1403: break;
1404:
1.1 cgd 1405: case PRU_SENDOOB:
1.32 mycroft 1406: m_freem(control);
1.31 mycroft 1407: m_freem(m);
1.1 cgd 1408: error = EOPNOTSUPP;
1409: break;
1410:
1.31 mycroft 1411: case PRU_SOCKADDR:
1412: in_setsockaddr(inp, nam);
1413: break;
1414:
1415: case PRU_PEERADDR:
1416: in_setpeeraddr(inp, nam);
1417: break;
1.1 cgd 1418:
1419: default:
1420: panic("udp_usrreq");
1421: }
1422:
1423: release:
1.31 mycroft 1424: splx(s);
1.1 cgd 1425: return (error);
1.13 mycroft 1426: }
1427:
1428: /*
1429: * Sysctl for udp variables.
1430: */
1.27 christos 1431: int
1.13 mycroft 1432: udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1433: int *name;
1434: u_int namelen;
1435: void *oldp;
1436: size_t *oldlenp;
1437: void *newp;
1438: size_t newlen;
1439: {
1440: /* All sysctl names at this level are terminal. */
1441: if (namelen != 1)
1442: return (ENOTDIR);
1443:
1444: switch (name[0]) {
1445: case UDPCTL_CHECKSUM:
1446: return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1.42 thorpej 1447: case UDPCTL_SENDSPACE:
1448: return (sysctl_int(oldp, oldlenp, newp, newlen,
1449: &udp_sendspace));
1450: case UDPCTL_RECVSPACE:
1451: return (sysctl_int(oldp, oldlenp, newp, newlen,
1452: &udp_recvspace));
1.13 mycroft 1453: default:
1454: return (ENOPROTOOPT);
1455: }
1456: /* NOTREACHED */
1.1 cgd 1457: }
CVSweb <webmaster@jp.NetBSD.org>