[BACK]Return to udp_usrreq.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet

Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.190.2.5

1.190.2.3  rmind       1: /*     $NetBSD$        */
1.48      itojun      2:
                      3: /*
                      4:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      5:  * All rights reserved.
1.94      itojun      6:  *
1.48      itojun      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.
1.94      itojun     18:  *
1.48      itojun     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.
1.104     agc        44:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
1.44      thorpej    60:  *     @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
1.1       cgd        61:  */
1.91      lukem      62:
1.190.2.1  rmind      63: /*
                     64:  * UDP protocol implementation.
                     65:  * Per RFC 768, August, 1980.
                     66:  */
                     67:
1.91      lukem      68: #include <sys/cdefs.h>
1.190.2.3  rmind      69: __KERNEL_RCSID(0, "$NetBSD$");
1.50      thorpej    70:
1.77      soda       71: #include "opt_inet.h"
1.174     christos   72: #include "opt_compat_netbsd.h"
1.50      thorpej    73: #include "opt_ipsec.h"
1.78      thorpej    74: #include "opt_inet_csum.h"
1.64      ws         75: #include "opt_ipkdb.h"
1.101     martin     76: #include "opt_mbuftrace.h"
1.1       cgd        77:
1.5       mycroft    78: #include <sys/param.h>
                     79: #include <sys/mbuf.h>
1.190.2.5! rmind      80: #include <sys/once.h>
1.5       mycroft    81: #include <sys/protosw.h>
                     82: #include <sys/socket.h>
                     83: #include <sys/socketvar.h>
1.27      christos   84: #include <sys/systm.h>
1.190.2.1  rmind      85: #include <sys/kmem.h>
1.53      itojun     86: #include <sys/domain.h>
1.27      christos   87: #include <sys/sysctl.h>
1.1       cgd        88:
1.5       mycroft    89: #include <net/if.h>
                     90: #include <net/route.h>
1.1       cgd        91:
1.5       mycroft    92: #include <netinet/in.h>
                     93: #include <netinet/in_systm.h>
1.15      cgd        94: #include <netinet/in_var.h>
1.5       mycroft    95: #include <netinet/ip.h>
                     96: #include <netinet/in_pcb.h>
                     97: #include <netinet/ip_var.h>
                     98: #include <netinet/ip_icmp.h>
                     99: #include <netinet/udp.h>
                    100: #include <netinet/udp_var.h>
1.166     thorpej   101: #include <netinet/udp_private.h>
1.1       cgd       102:
1.53      itojun    103: #ifdef INET6
                    104: #include <netinet/ip6.h>
                    105: #include <netinet/icmp6.h>
                    106: #include <netinet6/ip6_var.h>
1.167     thorpej   107: #include <netinet6/ip6_private.h>
1.53      itojun    108: #include <netinet6/in6_pcb.h>
                    109: #include <netinet6/udp6_var.h>
1.168     thorpej   110: #include <netinet6/udp6_private.h>
1.53      itojun    111: #endif
                    112:
                    113: #ifndef INET6
                    114: /* always need ip6.h for IP6_EXTHDR_GET */
                    115: #include <netinet/ip6.h>
                    116: #endif
                    117:
1.190     christos  118: #ifdef IPSEC
1.105     jonathan  119: #include <netipsec/ipsec.h>
1.169     thorpej   120: #include <netipsec/ipsec_var.h>
                    121: #include <netipsec/ipsec_private.h>
1.147     christos  122: #include <netipsec/esp.h>
1.105     jonathan  123: #ifdef INET6
                    124: #include <netipsec/ipsec6.h>
                    125: #endif
1.190     christos  126: #endif /* IPSEC */
1.105     jonathan  127:
1.174     christos  128: #ifdef COMPAT_50
                    129: #include <compat/sys/socket.h>
                    130: #endif
                    131:
1.64      ws        132: #ifdef IPKDB
                    133: #include <ipkdb/ipkdb.h>
                    134: #endif
                    135:
1.8       mycroft   136: int    udpcksum = 1;
1.141     yamt      137: int    udp_do_loopback_cksum = 0;
1.93      matt      138:
1.190.2.1  rmind     139: inpcbtable_t *         udbtable __read_mostly;
                    140: percpu_t *             udpstat_percpu;
1.8       mycroft   141:
1.72      itojun    142: #ifdef INET
1.190     christos  143: #ifdef IPSEC
1.144     manu      144: static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
1.130     manu      145:        struct socket *);
1.189     christos  146: #endif
1.119     matt      147: static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
                    148:        struct socket *);
                    149: static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
1.144     manu      150:        struct mbuf **, int);
1.129     yamt      151: static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.72      itojun    152: #endif
1.7       mycroft   153:
1.26      mycroft   154: #ifndef UDBHASHSIZE
                    155: #define        UDBHASHSIZE     128
                    156: #endif
                    157: int    udbhashsize = UDBHASHSIZE;
                    158:
1.190.2.1  rmind     159: static int     udp_sendspace = 9216;
                    160: static int     udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
                    161:
1.98      matt      162: #ifdef MBUFTRACE
1.150     dogcow    163: struct mowner udp_mowner = MOWNER_INIT("udp", "");
                    164: struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
                    165: struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
1.98      matt      166: #endif
                    167:
1.78      thorpej   168: #ifdef UDP_CSUM_COUNTERS
                    169: #include <sys/device.h>
                    170:
1.140     yamt      171: #if defined(INET)
1.78      thorpej   172: struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    173:     NULL, "udp", "hwcsum bad");
                    174: struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    175:     NULL, "udp", "hwcsum ok");
                    176: struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    177:     NULL, "udp", "hwcsum data");
                    178: struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    179:     NULL, "udp", "swcsum");
                    180:
1.120     matt      181: EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
                    182: EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
                    183: EVCNT_ATTACH_STATIC(udp_hwcsum_data);
                    184: EVCNT_ATTACH_STATIC(udp_swcsum);
1.140     yamt      185: #endif /* defined(INET) */
                    186:
                    187: #define        UDP_CSUM_COUNTER_INCR(ev)       (ev)->ev_count++
1.78      thorpej   188: #else
                    189: #define        UDP_CSUM_COUNTER_INCR(ev)       /* nothing */
                    190: #endif /* UDP_CSUM_COUNTERS */
                    191:
1.179     pooka     192: static void sysctl_net_inet_udp_setup(struct sysctllog **);
                    193:
1.190.2.5! rmind     194: static int
        !           195: do_udpinit(void)
1.1       cgd       196: {
1.190.2.1  rmind     197:        udbtable = inpcb_init(udbhashsize, udbhashsize, 0);
1.190.2.5! rmind     198:        udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
1.179     pooka     199:
1.98      matt      200:        MOWNER_ATTACH(&udp_tx_mowner);
                    201:        MOWNER_ATTACH(&udp_rx_mowner);
                    202:        MOWNER_ATTACH(&udp_mowner);
1.166     thorpej   203:
1.190.2.5! rmind     204:        return 0;
        !           205: }
        !           206:
        !           207: void
        !           208: udp_init_common(void)
        !           209: {
        !           210:        static ONCE_DECL(doudpinit);
        !           211:
        !           212:        RUN_ONCE(&doudpinit, do_udpinit);
        !           213: }
        !           214:
        !           215: void
        !           216: udp_init(void)
        !           217: {
        !           218:
        !           219:        sysctl_net_inet_udp_setup(NULL);
        !           220:
        !           221:        udp_init_common();
1.1       cgd       222: }
                    223:
1.129     yamt      224: /*
                    225:  * Checksum extended UDP header and data.
                    226:  */
                    227:
                    228: int
                    229: udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
                    230:     int iphlen, int len)
                    231: {
                    232:        switch (af) {
1.72      itojun    233: #ifdef INET
1.129     yamt      234:        case AF_INET:
                    235:                return udp4_input_checksum(m, uh, iphlen, len);
                    236: #endif
                    237: #ifdef INET6
                    238:        case AF_INET6:
                    239:                return udp6_input_checksum(m, uh, iphlen, len);
                    240: #endif
1.190.2.1  rmind     241:        default:
                    242:                KASSERT(false);
1.129     yamt      243:        }
                    244:        return -1;
                    245: }
                    246:
                    247: #ifdef INET
                    248:
                    249: /*
                    250:  * Checksum extended UDP header and data.
                    251:  */
                    252:
                    253: static int
                    254: udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
                    255:     int iphlen, int len)
                    256: {
                    257:
                    258:        /*
                    259:         * XXX it's better to record and check if this mbuf is
                    260:         * already checked.
                    261:         */
                    262:
                    263:        if (uh->uh_sum == 0)
                    264:                return 0;
                    265:
                    266:        switch (m->m_pkthdr.csum_flags &
                    267:            ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
                    268:            M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
                    269:        case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
                    270:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
                    271:                goto badcsum;
                    272:
                    273:        case M_CSUM_UDPv4|M_CSUM_DATA: {
                    274:                u_int32_t hw_csum = m->m_pkthdr.csum_data;
                    275:
                    276:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
                    277:                if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
                    278:                        const struct ip *ip =
                    279:                            mtod(m, const struct ip *);
                    280:
                    281:                        hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
                    282:                            ip->ip_dst.s_addr,
                    283:                            htons(hw_csum + len + IPPROTO_UDP));
                    284:                }
                    285:                if ((hw_csum ^ 0xffff) != 0)
                    286:                        goto badcsum;
                    287:                break;
                    288:        }
                    289:
                    290:        case M_CSUM_UDPv4:
                    291:                /* Checksum was okay. */
                    292:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
                    293:                break;
                    294:
                    295:        default:
                    296:                /*
                    297:                 * Need to compute it ourselves.  Maybe skip checksum
                    298:                 * on loopback interfaces.
                    299:                 */
                    300:                if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
                    301:                                     IFF_LOOPBACK) ||
                    302:                                   udp_do_loopback_cksum)) {
                    303:                        UDP_CSUM_COUNTER_INCR(&udp_swcsum);
                    304:                        if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
                    305:                                goto badcsum;
                    306:                }
                    307:                break;
                    308:        }
                    309:
                    310:        return 0;
                    311:
                    312: badcsum:
1.166     thorpej   313:        UDP_STATINC(UDP_STAT_BADSUM);
1.129     yamt      314:        return -1;
                    315: }
                    316:
1.7       mycroft   317: void
1.27      christos  318: udp_input(struct mbuf *m, ...)
1.1       cgd       319: {
1.53      itojun    320:        va_list ap;
                    321:        struct sockaddr_in src, dst;
                    322:        struct ip *ip;
                    323:        struct udphdr *uh;
1.190.2.4  rmind     324:        int iphlen, len, n;
                    325:        uint16_t ip_len;
1.53      itojun    326:
                    327:        va_start(ap, m);
                    328:        iphlen = va_arg(ap, int);
1.97      simonb    329:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.53      itojun    330:        va_end(ap);
                    331:
1.98      matt      332:        MCLAIM(m, &udp_rx_mowner);
1.166     thorpej   333:        UDP_STATINC(UDP_STAT_IPACKETS);
1.53      itojun    334:
                    335:        /*
                    336:         * Get IP and UDP header together in first mbuf.
                    337:         */
                    338:        ip = mtod(m, struct ip *);
                    339:        IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
                    340:        if (uh == NULL) {
1.166     thorpej   341:                UDP_STATINC(UDP_STAT_HDROPS);
1.53      itojun    342:                return;
                    343:        }
1.95      thorpej   344:        KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53      itojun    345:
1.190.2.4  rmind     346:        /* Destination port of 0 is illegal, based on RFC 768. */
1.57      itojun    347:        if (uh->uh_dport == 0)
                    348:                goto bad;
                    349:
1.53      itojun    350:        /*
                    351:         * Make mbuf data length reflect UDP length.
                    352:         * If not enough data to reflect UDP length, drop.
                    353:         */
1.96      itojun    354:        ip_len = ntohs(ip->ip_len);
1.190.2.4  rmind     355:        len = ntohs((uint16_t)uh->uh_ulen);
1.96      itojun    356:        if (ip_len != iphlen + len) {
                    357:                if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.166     thorpej   358:                        UDP_STATINC(UDP_STAT_BADLEN);
1.53      itojun    359:                        goto bad;
                    360:                }
1.96      itojun    361:                m_adj(m, iphlen + len - ip_len);
1.53      itojun    362:        }
                    363:
                    364:        /*
                    365:         * Checksum extended UDP header and data.
                    366:         */
1.129     yamt      367:        if (udp4_input_checksum(m, uh, iphlen, len))
1.190.2.4  rmind     368:                goto bad;
1.53      itojun    369:
                    370:        /* construct source and dst sockaddrs. */
1.159     dyoung    371:        sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
                    372:        sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
1.53      itojun    373:
1.144     manu      374:        if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
1.166     thorpej   375:                UDP_STATINC(UDP_STAT_HDROPS);
1.144     manu      376:                return;
                    377:        }
1.188     christos  378:        if (m == NULL) {
                    379:                /*
                    380:                 * packet has been processed by ESP stuff -
                    381:                 * e.g. dropped NAT-T-keep-alive-packet ...
                    382:                 */
                    383:                return;
                    384:        }
                    385:        ip = mtod(m, struct ip *);
1.53      itojun    386: #ifdef INET6
                    387:        if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
                    388:                struct sockaddr_in6 src6, dst6;
                    389:
1.175     cegger    390:                memset(&src6, 0, sizeof(src6));
1.53      itojun    391:                src6.sin6_family = AF_INET6;
                    392:                src6.sin6_len = sizeof(struct sockaddr_in6);
                    393:                src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
1.177     tsutsui   394:                memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
1.53      itojun    395:                        sizeof(ip->ip_src));
                    396:                src6.sin6_port = uh->uh_sport;
1.175     cegger    397:                memset(&dst6, 0, sizeof(dst6));
1.53      itojun    398:                dst6.sin6_family = AF_INET6;
                    399:                dst6.sin6_len = sizeof(struct sockaddr_in6);
                    400:                dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
1.177     tsutsui   401:                memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
1.53      itojun    402:                        sizeof(ip->ip_dst));
                    403:                dst6.sin6_port = uh->uh_dport;
                    404:
                    405:                n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
                    406:        }
                    407: #endif
                    408:
                    409:        if (n == 0) {
                    410:                if (m->m_flags & (M_BCAST | M_MCAST)) {
1.166     thorpej   411:                        UDP_STATINC(UDP_STAT_NOPORTBCAST);
1.53      itojun    412:                        goto bad;
                    413:                }
1.166     thorpej   414:                UDP_STATINC(UDP_STAT_NOPORT);
1.64      ws        415: #ifdef IPKDB
1.53      itojun    416:                if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
                    417:                                m, iphlen + sizeof(struct udphdr),
                    418:                                m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
                    419:                        /*
                    420:                         * It was a debugger connect packet,
                    421:                         * just drop it now
                    422:                         */
                    423:                        goto bad;
                    424:                }
                    425: #endif
                    426:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
                    427:                m = NULL;
                    428:        }
                    429: bad:
1.190.2.4  rmind     430:        if (m) {
1.53      itojun    431:                m_freem(m);
1.190.2.4  rmind     432:        }
1.78      thorpej   433:        return;
1.53      itojun    434: }
                    435:
                    436: static void
1.119     matt      437: udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
                    438:        struct sockaddr *src, struct socket *so)
1.53      itojun    439: {
                    440:        struct mbuf *opts = NULL;
                    441:        struct mbuf *n;
1.190.2.1  rmind     442:        inpcb_t *inp = NULL;
1.53      itojun    443:
                    444:        if (!so)
                    445:                return;
                    446:        switch (so->so_proto->pr_domain->dom_family) {
                    447:        case AF_INET:
                    448:                inp = sotoinpcb(so);
                    449:                break;
                    450: #ifdef INET6
                    451:        case AF_INET6:
                    452:                break;
                    453: #endif
                    454:        default:
                    455:                return;
                    456:        }
                    457:
1.190     christos  458: #if defined(IPSEC)
1.53      itojun    459:        /* check AH/ESP integrity. */
                    460:        if (so != NULL && ipsec4_in_reject_so(m, so)) {
1.169     thorpej   461:                IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.162     dyoung    462:                if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
1.110     itojun    463:                        icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
                    464:                            0, 0);
1.53      itojun    465:                return;
                    466:        }
                    467: #endif /*IPSEC*/
                    468:
1.162     dyoung    469:        if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.190.2.1  rmind     470:                if (inp && ((inpcb_get_flags(inp) & INP_CONTROLOPTS) != 0
1.174     christos  471: #ifdef SO_OTIMESTAMP
                    472:                         || so->so_options & SO_OTIMESTAMP
                    473: #endif
1.53      itojun    474:                         || so->so_options & SO_TIMESTAMP)) {
                    475:                        struct ip *ip = mtod(n, struct ip *);
                    476:                        ip_savecontrol(inp, &opts, ip, n);
                    477:                }
                    478:
                    479:                m_adj(n, off);
                    480:                if (sbappendaddr(&so->so_rcv, src, n,
                    481:                                opts) == 0) {
                    482:                        m_freem(n);
                    483:                        if (opts)
                    484:                                m_freem(opts);
1.124     darrenr   485:                        so->so_rcv.sb_overflowed++;
1.166     thorpej   486:                        UDP_STATINC(UDP_STAT_FULLSOCK);
1.53      itojun    487:                } else
                    488:                        sorwakeup(so);
                    489:        }
                    490: }
                    491:
1.190.2.1  rmind     492: struct udp_pcb_ctx {
                    493:        struct mbuf *           mbuf;
                    494:        struct sockaddr_in *    src;
                    495:        struct sockaddr_in *    dst;
                    496:        int                     off;
                    497:        int                     rcvcnt;
                    498: };
1.53      itojun    499:
1.190.2.1  rmind     500: static int
                    501: udp4_pcb_process(inpcb_t *inp, void *arg)
                    502: {
                    503:        struct udp_pcb_ctx *uctx = arg;
                    504:        struct in_addr dst4 = uctx->dst->sin_addr;
                    505:        in_port_t dport = uctx->dst->sin_port;
                    506:        struct in_addr laddr, faddr;
                    507:        in_port_t lport, fport;
                    508:        struct socket *so;
1.53      itojun    509:
1.190.2.1  rmind     510:        inpcb_get_ports(inp, &lport, &fport);
                    511:        if (lport != dport) {
                    512:                return 0;
1.53      itojun    513:        }
1.190.2.1  rmind     514:        inpcb_get_addrs(inp, &laddr, &faddr);
                    515:        if (!in_nullhost(laddr) && !in_hosteq(laddr, dst4)) {
                    516:                return 0;
                    517:        }
                    518:        if (!in_nullhost(faddr)) {
                    519:                struct in_addr src4 = uctx->src->sin_addr;
                    520:                in_port_t sport = uctx->src->sin_port;
1.53      itojun    521:
1.190.2.1  rmind     522:                if (!in_hosteq(faddr, src4) || fport != sport) {
                    523:                        return 0;
1.53      itojun    524:                }
1.190.2.1  rmind     525:        }
1.53      itojun    526:
1.190.2.1  rmind     527:        so = inpcb_get_socket(inp);
                    528:        udp4_sendup(uctx->mbuf, uctx->off, (struct sockaddr *)uctx->src, so);
                    529:        uctx->rcvcnt++;
                    530:
                    531:        /*
                    532:         * Do not look for additional matches if this one does not have
                    533:         * either the SO_REUSEPORT or SO_REUSEADDR socket options set.
                    534:         * This heuristic avoids searching through all PCBs in the common
                    535:         * case of a non-shared port.  It assumes that an application will
                    536:         * never clear these options after setting them.
                    537:         */
                    538:        if ((so->so_options & (SO_REUSEPORT|SO_REUSEADDR)) == 0) {
                    539:                return EJUSTRETURN;
1.53      itojun    540:        }
1.190.2.1  rmind     541:        return 0;
1.53      itojun    542: }
                    543:
                    544: static int
1.119     matt      545: udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
1.190.2.1  rmind     546:     struct mbuf **mp, int off /* offset of udphdr */)
1.53      itojun    547: {
1.190.2.1  rmind     548:        in_port_t *sport, *dport;
1.53      itojun    549:        struct in_addr *src4, *dst4;
1.190.2.1  rmind     550:        inpcb_t *inp;
1.144     manu      551:        struct mbuf *m = *mp;
1.190.2.1  rmind     552:        int rcvcnt;
1.53      itojun    553:
                    554:        rcvcnt = 0;
                    555:        off += sizeof(struct udphdr);   /* now, offset of payload */
                    556:
                    557:        if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
                    558:                goto bad;
                    559:
                    560:        src4 = &src->sin_addr;
                    561:        sport = &src->sin_port;
                    562:        dst4 = &dst->sin_addr;
                    563:        dport = &dst->sin_port;
                    564:
1.73      itojun    565:        if (IN_MULTICAST(dst4->s_addr) ||
1.53      itojun    566:            in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
1.190.2.1  rmind     567:                struct udp_pcb_ctx uctx = {
                    568:                        .mbuf = m, .src = src, .dst = dst,
                    569:                        .off = off, .rcvcnt = 0
                    570:                };
                    571:                int error;
                    572:
1.53      itojun    573:                /*
                    574:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    575:                 * for which the local and remote addresses and ports match
                    576:                 * those of the incoming datagram.  This allows more than
                    577:                 * one process to receive multi/broadcasts on the same port.
                    578:                 */
1.190.2.1  rmind     579:                error = inpcb_foreach(udbtable, AF_INET,
                    580:                    udp4_pcb_process, &uctx);
                    581:                KASSERT(error == 0 || error == EJUSTRETURN);
                    582:                rcvcnt = uctx.rcvcnt;
1.53      itojun    583:        } else {
                    584:                /*
1.190.2.1  rmind     585:                 * Locate PCB for datagram.
1.53      itojun    586:                 */
1.190.2.1  rmind     587:                struct socket *so;
                    588:
1.190.2.3  rmind     589:                inp = inpcb_lookup(udbtable, *src4, *sport, *dst4, *dport, NULL);
1.190.2.1  rmind     590:                if (inp == NULL) {
1.166     thorpej   591:                        UDP_STATINC(UDP_STAT_PCBHASHMISS);
1.190.2.3  rmind     592:                        inp = inpcb_lookup_bound(udbtable, *dst4, *dport);
1.190.2.1  rmind     593:                        if (inp == NULL)
1.53      itojun    594:                                return rcvcnt;
                    595:                }
1.190.2.1  rmind     596:                so = inpcb_get_socket(inp);
1.53      itojun    597:
1.190     christos  598: #ifdef IPSEC
1.130     manu      599:                /* Handle ESP over UDP */
1.190.2.1  rmind     600:                if (inpcb_get_flags(inp) & INP_ESPINUDP_ALL) {
1.130     manu      601:                        struct sockaddr *sa = (struct sockaddr *)src;
                    602:
1.190.2.1  rmind     603:                        switch (udp4_espinudp(mp, off, sa, so)) {
1.144     manu      604:                        case -1:        /* Error, m was freeed */
                    605:                                rcvcnt = -1;
                    606:                                goto bad;
                    607:                                break;
                    608:
                    609:                        case 1:         /* ESP over UDP */
1.130     manu      610:                                rcvcnt++;
                    611:                                goto bad;
1.144     manu      612:                                break;
                    613:
                    614:                        case 0:         /* plain UDP */
                    615:                        default:        /* Unexpected */
                    616:                                /*
                    617:                                 * Normal UDP processing will take place
                    618:                                 * m may have changed.
                    619:                                 */
                    620:                                m = *mp;
                    621:                                break;
1.130     manu      622:                        }
                    623:                }
1.189     christos  624: #endif
1.130     manu      625:
1.178     minskim   626:                /*
                    627:                 * Check the minimum TTL for socket.
                    628:                 */
1.190.2.1  rmind     629:                if (mtod(m, struct ip *)->ip_ttl < inpcb_get_minttl(inp)) {
1.178     minskim   630:                        goto bad;
1.53      itojun    631:                }
1.190.2.1  rmind     632:                udp4_sendup(m, off, (struct sockaddr *)src, so);
1.53      itojun    633:                rcvcnt++;
                    634:        }
                    635:
                    636: bad:
                    637:        return rcvcnt;
                    638: }
                    639: #endif
1.1       cgd       640:
1.72      itojun    641: #ifdef INET
1.1       cgd       642: /*
1.190.2.1  rmind     643:  * Notify a UDP user of an asynchronous error;
1.1       cgd       644:  * just wake up so that he can collect error status.
                    645:  */
1.7       mycroft   646: static void
1.190.2.1  rmind     647: udp_notify(inpcb_t *inp, int errno)
1.1       cgd       648: {
1.190.2.1  rmind     649:        struct socket *so = inpcb_get_socket(inp);
                    650:
                    651:        so->so_error = errno;
                    652:        sorwakeup(so);
                    653:        sowwakeup(so);
1.1       cgd       654: }
                    655:
1.27      christos  656: void *
1.157     dyoung    657: udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.1       cgd       658: {
1.66      augustss  659:        struct ip *ip = v;
                    660:        struct udphdr *uh;
1.21      mycroft   661:        int errno;
1.190.2.1  rmind     662:        bool rdr;
1.1       cgd       663:
1.190.2.1  rmind     664:        if (sa->sa_family != AF_INET ||
                    665:            sa->sa_len != sizeof(struct sockaddr_in))
1.51      itojun    666:                return NULL;
1.20      mycroft   667:        if ((unsigned)cmd >= PRC_NCMDS)
1.27      christos  668:                return NULL;
1.20      mycroft   669:        errno = inetctlerrmap[cmd];
1.190.2.1  rmind     670:
                    671:        rdr = PRC_IS_REDIRECT(cmd);
                    672:        if (rdr || cmd == PRC_HOSTDEAD || ip == NULL) {
                    673:                inpcb_notifyall(udbtable, satocsin(sa)->sin_addr,
                    674:                    errno, rdr ? inpcb_rtchange : udp_notify);
1.27      christos  675:                return NULL;
1.190.2.1  rmind     676:        } else if (errno == 0) {
                    677:                return NULL;
                    678:        }
1.53      itojun    679:
1.190.2.1  rmind     680:        /* Note: mapped address case */
                    681:        uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
                    682:        inpcb_notify(udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
                    683:            ip->ip_src, uh->uh_sport, errno, udp_notify);
1.27      christos  684:        return NULL;
1.1       cgd       685: }
                    686:
1.7       mycroft   687: int
1.173     plunky    688: udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.130     manu      689: {
1.190.2.4  rmind     690:        int family, optval, inpflags, error = 0;
1.190.2.1  rmind     691:        inpcb_t *inp;
1.130     manu      692:
1.190.2.4  rmind     693:        KASSERT(solocked(so));
                    694:
1.130     manu      695:        family = so->so_proto->pr_domain->dom_family;
                    696:
                    697:        switch (family) {
                    698: #ifdef INET
                    699:        case PF_INET:
1.173     plunky    700:                if (sopt->sopt_level != IPPROTO_UDP) {
1.190.2.4  rmind     701:                        return ip_ctloutput(op, so, sopt);
1.130     manu      702:                }
                    703:                break;
                    704: #endif
                    705: #ifdef INET6
                    706:        case PF_INET6:
1.173     plunky    707:                if (sopt->sopt_level != IPPROTO_UDP) {
1.190.2.4  rmind     708:                        return ip6_ctloutput(op, so, sopt);
1.130     manu      709:                }
                    710:                break;
                    711: #endif
                    712:        default:
1.190.2.4  rmind     713:                return EAFNOSUPPORT;
1.130     manu      714:        }
                    715:
                    716:        switch (op) {
                    717:        case PRCO_SETOPT:
                    718:                inp = sotoinpcb(so);
                    719:
1.173     plunky    720:                switch (sopt->sopt_name) {
1.130     manu      721:                case UDP_ENCAP:
1.173     plunky    722:                        error = sockopt_getint(sopt, &optval);
                    723:                        if (error)
1.153     yamt      724:                                break;
1.131     perry     725:
1.190.2.1  rmind     726:                        inpflags = inpcb_get_flags(inp);
1.173     plunky    727:                        switch(optval) {
1.130     manu      728:                        case 0:
1.190.2.1  rmind     729:                                inpflags &= ~INP_ESPINUDP_ALL;
1.130     manu      730:                                break;
                    731:
                    732:                        case UDP_ENCAP_ESPINUDP:
1.190.2.1  rmind     733:                                inpflags &= ~INP_ESPINUDP_ALL;
                    734:                                inpflags |= INP_ESPINUDP;
1.130     manu      735:                                break;
1.131     perry     736:
1.130     manu      737:                        case UDP_ENCAP_ESPINUDP_NON_IKE:
1.190.2.1  rmind     738:                                inpflags &= ~INP_ESPINUDP_ALL;
                    739:                                inpflags |= INP_ESPINUDP_NON_IKE;
1.130     manu      740:                                break;
                    741:                        default:
                    742:                                error = EINVAL;
                    743:                                break;
                    744:                        }
1.190.2.1  rmind     745:                        inpcb_set_flags(inp, inpflags);
1.130     manu      746:                        break;
1.190.2.1  rmind     747:
1.130     manu      748:                default:
                    749:                        error = ENOPROTOOPT;
                    750:                        break;
                    751:                }
                    752:                break;
                    753:
                    754:        default:
                    755:                error = EINVAL;
                    756:                break;
1.131     perry     757:        }
                    758:
1.130     manu      759:        return error;
                    760: }
1.131     perry     761:
1.190.2.3  rmind     762: static int
                    763: udp_output(struct mbuf *m, inpcb_t *inp)
1.27      christos  764: {
1.190.2.1  rmind     765:        struct socket *so;
1.66      augustss  766:        struct udpiphdr *ui;
1.125     thorpej   767:        struct route *ro;
1.66      augustss  768:        int len = m->m_pkthdr.len;
1.31      mycroft   769:        int error = 0;
1.27      christos  770:
1.98      matt      771:        MCLAIM(m, &udp_tx_mowner);
1.190.2.1  rmind     772:        so = inpcb_get_socket(inp);
                    773:        KASSERT(solocked(so));
                    774:
1.1       cgd       775:        /*
1.190.2.3  rmind     776:         * Calculate data length and get a mbuf for UDP and IP headers.
1.1       cgd       777:         */
1.13      mycroft   778:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1.190.2.3  rmind     779:        if (m == NULL) {
1.13      mycroft   780:                error = ENOBUFS;
1.39      thorpej   781:                goto release;
                    782:        }
                    783:
                    784:        /*
                    785:         * Compute the packet length of the IP header, and
                    786:         * punt if the length looks bogus.
                    787:         */
1.96      itojun    788:        if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39      thorpej   789:                error = EMSGSIZE;
1.13      mycroft   790:                goto release;
                    791:        }
1.1       cgd       792:
                    793:        /*
1.190.2.3  rmind     794:         * Fill in mbuf with extended UDP header.
1.1       cgd       795:         */
                    796:        ui = mtod(m, struct udpiphdr *);
                    797:        ui->ui_pr = IPPROTO_UDP;
1.190.2.1  rmind     798:        inpcb_get_addrs(inp, &ui->ui_src, &ui->ui_dst);
                    799:        inpcb_get_ports(inp, &ui->ui_sport, &ui->ui_dport);
1.190.2.3  rmind     800:        ui->ui_ulen = htons((uint16_t)len + sizeof(struct udphdr));
1.1       cgd       801:
                    802:        /*
1.78      thorpej   803:         * Set up checksum and output datagram.
1.1       cgd       804:         */
                    805:        if (udpcksum) {
1.78      thorpej   806:                /*
                    807:                 * XXX Cache pseudo-header checksum part for
                    808:                 * XXX "connected" UDP sockets.
                    809:                 */
                    810:                ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
                    811:                    ui->ui_dst.s_addr, htons((u_int16_t)len +
                    812:                    sizeof(struct udphdr) + IPPROTO_UDP));
1.135     yamt      813:                m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1.78      thorpej   814:                m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
                    815:        } else
                    816:                ui->ui_sum = 0;
1.190.2.1  rmind     817:
1.190.2.3  rmind     818:        struct ip *ui_ip = (struct ip *)ui;
                    819:        ui_ip->ip_len = htons(sizeof (struct udpiphdr) + len);
1.190.2.1  rmind     820:
                    821:        struct ip *inp_ip = in_getiphdr(inp);
1.190.2.3  rmind     822:        ui_ip->ip_ttl = inp_ip->ip_ttl; /* XXX */
                    823:        ui_ip->ip_tos = inp_ip->ip_tos; /* XXX */
1.166     thorpej   824:        UDP_STATINC(UDP_STAT_OPACKETS);
1.48      itojun    825:
1.190.2.4  rmind     826:        ro = inpcb_get_route(inp);
                    827:        return ip_output(m, inpcb_get_options(inp), ro,
1.190.2.1  rmind     828:            so->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.190.2.4  rmind     829:            inpcb_get_moptions(inp), so);
1.1       cgd       830:
                    831: release:
                    832:        m_freem(m);
1.190.2.1  rmind     833:        return error;
1.1       cgd       834: }
                    835:
1.190.2.2  rmind     836: static int
                    837: udp_attach(struct socket *so, int proto)
                    838: {
                    839:        inpcb_t *inp;
                    840:        struct ip *ip;
1.190.2.4  rmind     841:        int error;
1.190.2.2  rmind     842:
                    843:        KASSERT(sotoinpcb(so) == NULL);
                    844:
1.190.2.4  rmind     845:        /* Assign the lock (must happen if we will error out). */
1.190.2.2  rmind     846:        sosetlock(so);
                    847:
                    848: #ifdef MBUFTRACE
                    849:        so->so_mowner = &udp_mowner;
                    850:        so->so_rcv.sb_mowner = &udp_rx_mowner;
                    851:        so->so_snd.sb_mowner = &udp_tx_mowner;
                    852: #endif
                    853:        if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    854:                error = soreserve(so, udp_sendspace, udp_recvspace);
                    855:                if (error) {
1.190.2.4  rmind     856:                        return error;
1.190.2.2  rmind     857:                }
                    858:        }
                    859:
                    860:        error = inpcb_create(so, udbtable);
                    861:        if (error) {
1.190.2.4  rmind     862:                return error;
1.190.2.2  rmind     863:        }
                    864:        inp = sotoinpcb(so);
                    865:        ip = in_getiphdr(inp);
                    866:        ip->ip_ttl = ip_defttl;
                    867:        return error;
                    868: }
                    869:
                    870: static void
                    871: udp_detach(struct socket *so)
                    872: {
                    873:        inpcb_t *inp;
                    874:
                    875:        KASSERT(solocked(so));
                    876:        inp = sotoinpcb(so);
                    877:        KASSERT(inp != NULL);
                    878:        inpcb_destroy(inp);
                    879: }
                    880:
                    881: static int
1.190.2.3  rmind     882: udp_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
                    883:     struct mbuf *control)
                    884: {
                    885:        inpcb_t *inp;
                    886:        struct in_addr laddr;
                    887:        int error;
                    888:
                    889:        KASSERT(solocked(so));
                    890:        inp = sotoinpcb(so);
                    891:
                    892:        if (control && control->m_len) {
                    893:                m_freem(control);
                    894:                m_freem(m);
                    895:                return EINVAL;
                    896:        }
                    897:
                    898:        if ((so->so_state & SS_ISCONNECTED) != 0) {
                    899:                m_freem(m);
                    900:                return nam ? EISCONN : ENOTCONN;
                    901:        }
                    902:
                    903:        if (nam) {
                    904:                /*
                    905:                 * XXX: sendto() case - temporarily connect the socket
                    906:                 * to the destination, send and then disconnect.  Also,
                    907:                 * preserve the local address as it may be changed.
                    908:                 */
                    909:                inpcb_get_addrs(inp, &laddr, NULL);
                    910:                if ((error = inpcb_connect(inp, nam, curlwp)) != 0) {
                    911:                        m_freem(m);
                    912:                        return error;
                    913:                }
                    914:        }
                    915:        error = udp_output(m, inp);
                    916:        if (nam) {
                    917:                inpcb_disconnect(inp);
                    918:                inpcb_set_addrs(inp, &laddr, NULL);
                    919:                inpcb_set_state(inp, INP_BOUND);
                    920:        }
                    921:        return error;
                    922: }
                    923:
                    924: static int
1.119     matt      925: udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1.190.2.1  rmind     926:     struct mbuf *control, struct lwp *l)
1.1       cgd       927: {
1.190.2.1  rmind     928:        inpcb_t *inp;
1.190.2.4  rmind     929:        int error = 0;
1.59      thorpej   930:
1.190.2.2  rmind     931:        KASSERT(req != PRU_ATTACH);
                    932:        KASSERT(req != PRU_DETACH);
                    933:
1.190.2.1  rmind     934:        if (req == PRU_CONTROL) {
1.190.2.4  rmind     935:                KERNEL_LOCK(1, NULL);
                    936:                error = in_control(so, (long)m, nam, (ifnet_t *)control, l);
                    937:                KERNEL_UNLOCK_ONE(NULL);
                    938:                return error;
1.190.2.1  rmind     939:        }
1.60      thorpej   940:        if (req == PRU_PURGEIF) {
1.190.2.4  rmind     941:                KERNEL_LOCK(1, NULL);
1.170     ad        942:                mutex_enter(softnet_lock);
1.190.2.1  rmind     943:                inpcb_purgeif0(udbtable, (ifnet_t *)control);
                    944:                in_purgeif((ifnet_t *)control);
                    945:                inpcb_purgeif(udbtable, (ifnet_t *)control);
1.170     ad        946:                mutex_exit(softnet_lock);
1.190.2.4  rmind     947:                KERNEL_UNLOCK_ONE(NULL);
                    948:                return 0;
1.59      thorpej   949:        }
1.31      mycroft   950:
1.190.2.2  rmind     951:        KASSERT(solocked(so));
1.31      mycroft   952:        inp = sotoinpcb(so);
1.190.2.1  rmind     953:
                    954:        KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
1.190.2.2  rmind     955:        if (inp == NULL) {
1.190.2.4  rmind     956:                return EINVAL;
1.31      mycroft   957:        }
                    958:
1.1       cgd       959:        switch (req) {
                    960:        case PRU_BIND:
1.190.2.1  rmind     961:                error = inpcb_bind(inp, nam, l);
1.1       cgd       962:                break;
                    963:
                    964:        case PRU_LISTEN:
                    965:                error = EOPNOTSUPP;
                    966:                break;
                    967:
                    968:        case PRU_CONNECT:
1.190.2.1  rmind     969:                error = inpcb_connect(inp, nam, l);
1.31      mycroft   970:                if (error)
1.1       cgd       971:                        break;
1.31      mycroft   972:                soisconnected(so);
1.1       cgd       973:                break;
                    974:
                    975:        case PRU_CONNECT2:
                    976:                error = EOPNOTSUPP;
                    977:                break;
                    978:
                    979:        case PRU_DISCONNECT:
1.31      mycroft   980:                /*soisdisconnected(so);*/
1.190.2.1  rmind     981:                so->so_state &= ~SS_ISCONNECTED;                /* XXX */
                    982:                inpcb_disconnect(inp);
1.1       cgd       983:                break;
                    984:
                    985:        case PRU_SHUTDOWN:
                    986:                socantsendmore(so);
                    987:                break;
                    988:
1.31      mycroft   989:        case PRU_RCVD:
                    990:                error = EOPNOTSUPP;
1.1       cgd       991:                break;
                    992:
1.31      mycroft   993:        case PRU_SEND:
1.190.2.3  rmind     994:                error = udp_send(so, m, nam, control);
1.1       cgd       995:                break;
                    996:
                    997:        case PRU_SENSE:
                    998:                /*
                    999:                 * stat: don't bother with a blocksize.
                   1000:                 */
1.190.2.4  rmind    1001:                return 0;
1.1       cgd      1002:
1.31      mycroft  1003:        case PRU_RCVOOB:
                   1004:                error =  EOPNOTSUPP;
                   1005:                break;
                   1006:
1.1       cgd      1007:        case PRU_SENDOOB:
1.32      mycroft  1008:                m_freem(control);
1.31      mycroft  1009:                m_freem(m);
1.1       cgd      1010:                error =  EOPNOTSUPP;
                   1011:                break;
                   1012:
1.31      mycroft  1013:        case PRU_SOCKADDR:
1.190.2.1  rmind    1014:                inpcb_fetch_sockaddr(inp, nam);
1.31      mycroft  1015:                break;
                   1016:
                   1017:        case PRU_PEERADDR:
1.190.2.1  rmind    1018:                inpcb_fetch_peeraddr(inp, nam);
1.31      mycroft  1019:                break;
1.1       cgd      1020:
                   1021:        default:
                   1022:                panic("udp_usrreq");
                   1023:        }
                   1024:
1.190.2.4  rmind    1025:        return error;
1.13      mycroft  1026: }
                   1027:
1.166     thorpej  1028: static int
                   1029: sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
                   1030: {
                   1031:
1.172     thorpej  1032:        return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1.166     thorpej  1033: }
                   1034:
1.13      mycroft  1035: /*
                   1036:  * Sysctl for udp variables.
                   1037:  */
1.179     pooka    1038: static void
                   1039: sysctl_net_inet_udp_setup(struct sysctllog **clog)
1.13      mycroft  1040: {
1.190.2.5! rmind    1041:
1.116     atatat   1042:        sysctl_createv(clog, 0, NULL, NULL,
                   1043:                       CTLFLAG_PERMANENT,
1.114     atatat   1044:                       CTLTYPE_NODE, "inet", NULL,
                   1045:                       NULL, 0, NULL, 0,
                   1046:                       CTL_NET, PF_INET, CTL_EOL);
1.116     atatat   1047:        sysctl_createv(clog, 0, NULL, NULL,
                   1048:                       CTLFLAG_PERMANENT,
1.122     atatat   1049:                       CTLTYPE_NODE, "udp",
                   1050:                       SYSCTL_DESCR("UDPv4 related settings"),
1.114     atatat   1051:                       NULL, 0, NULL, 0,
                   1052:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
                   1053:
1.116     atatat   1054:        sysctl_createv(clog, 0, NULL, NULL,
                   1055:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1056:                       CTLTYPE_INT, "checksum",
1.123     heas     1057:                       SYSCTL_DESCR("Compute UDP checksums"),
1.114     atatat   1058:                       NULL, 0, &udpcksum, 0,
                   1059:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
                   1060:                       CTL_EOL);
1.116     atatat   1061:        sysctl_createv(clog, 0, NULL, NULL,
                   1062:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1063:                       CTLTYPE_INT, "sendspace",
                   1064:                       SYSCTL_DESCR("Default UDP send buffer size"),
1.114     atatat   1065:                       NULL, 0, &udp_sendspace, 0,
                   1066:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
                   1067:                       CTL_EOL);
1.116     atatat   1068:        sysctl_createv(clog, 0, NULL, NULL,
                   1069:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1070:                       CTLTYPE_INT, "recvspace",
                   1071:                       SYSCTL_DESCR("Default UDP receive buffer size"),
1.114     atatat   1072:                       NULL, 0, &udp_recvspace, 0,
                   1073:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
                   1074:                       CTL_EOL);
1.125     thorpej  1075:        sysctl_createv(clog, 0, NULL, NULL,
                   1076:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1077:                       CTLTYPE_INT, "do_loopback_cksum",
                   1078:                       SYSCTL_DESCR("Perform UDP checksum on loopback"),
                   1079:                       NULL, 0, &udp_do_loopback_cksum, 0,
                   1080:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
                   1081:                       CTL_EOL);
1.132     atatat   1082:        sysctl_createv(clog, 0, NULL, NULL,
                   1083:                       CTLFLAG_PERMANENT,
1.134     atatat   1084:                       CTLTYPE_STRUCT, "pcblist",
1.132     atatat   1085:                       SYSCTL_DESCR("UDP protocol control block list"),
1.190.2.1  rmind    1086:                       sysctl_inpcblist, 0, udbtable, 0,
1.132     atatat   1087:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
                   1088:                       CTL_EOL);
1.139     elad     1089:        sysctl_createv(clog, 0, NULL, NULL,
                   1090:                       CTLFLAG_PERMANENT,
                   1091:                       CTLTYPE_STRUCT, "stats",
                   1092:                       SYSCTL_DESCR("UDP statistics"),
1.166     thorpej  1093:                       sysctl_net_inet_udp_stats, 0, NULL, 0,
1.139     elad     1094:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
                   1095:                       CTL_EOL);
1.1       cgd      1096: }
1.72      itojun   1097: #endif
1.130     manu     1098:
1.166     thorpej  1099: void
                   1100: udp_statinc(u_int stat)
                   1101: {
                   1102:
                   1103:        KASSERT(stat < UDP_NSTATS);
                   1104:        UDP_STATINC(stat);
                   1105: }
                   1106:
1.190     christos 1107: #if defined(INET) && defined(IPSEC)
1.130     manu     1108: /*
                   1109:  * Returns:
                   1110:  * 1 if the packet was processed
                   1111:  * 0 if normal UDP processing should take place
1.144     manu     1112:  * -1 if an error occurent and m was freed
1.130     manu     1113:  */
                   1114: static int
1.154     yamt     1115: udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1.151     christos 1116:     struct socket *so)
1.130     manu     1117: {
                   1118:        size_t len;
1.158     christos 1119:        void *data;
1.190.2.1  rmind    1120:        inpcb_t *inp;
1.130     manu     1121:        size_t skip = 0;
                   1122:        size_t minlen;
                   1123:        size_t iphdrlen;
                   1124:        struct ip *ip;
1.136     manu     1125:        struct m_tag *tag;
                   1126:        struct udphdr *udphdr;
1.190.2.1  rmind    1127:        in_port_t sport, dport;
1.144     manu     1128:        struct mbuf *m = *mp;
1.190.2.1  rmind    1129:        int inpflags;
1.130     manu     1130:
1.131     perry    1131:        /*
1.130     manu     1132:         * Collapse the mbuf chain if the first mbuf is too short
                   1133:         * The longest case is: UDP + non ESP marker + ESP
                   1134:         */
                   1135:        minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
                   1136:        if (minlen > m->m_pkthdr.len)
                   1137:                minlen = m->m_pkthdr.len;
                   1138:
                   1139:        if (m->m_len < minlen) {
1.144     manu     1140:                if ((*mp = m_pullup(m, minlen)) == NULL) {
1.130     manu     1141:                        printf("udp4_espinudp: m_pullup failed\n");
1.144     manu     1142:                        return -1;
1.130     manu     1143:                }
1.144     manu     1144:                m = *mp;
1.130     manu     1145:        }
                   1146:
1.131     perry    1147:        len = m->m_len - off;
1.158     christos 1148:        data = mtod(m, char *) + off;
1.130     manu     1149:        inp = sotoinpcb(so);
                   1150:
                   1151:        /* Ignore keepalive packets */
1.158     christos 1152:        if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1.188     christos 1153:                m_free(m);
                   1154:                *mp = NULL; /* avoid any further processiong by caller ... */
1.130     manu     1155:                return 1;
                   1156:        }
1.190.2.1  rmind    1157:        inpflags = inpcb_get_flags(inp);
1.130     manu     1158:
1.131     perry    1159:        /*
                   1160:         * Check that the payload is long enough to hold
1.130     manu     1161:         * an ESP header and compute the length of encapsulation
1.131     perry    1162:         * header to remove
1.130     manu     1163:         */
1.190.2.1  rmind    1164:        if (inpflags & INP_ESPINUDP) {
1.130     manu     1165:                u_int32_t *st = (u_int32_t *)data;
                   1166:
                   1167:                if ((len <= sizeof(struct esp)) || (*st == 0))
                   1168:                        return 0; /* Normal UDP processing */
                   1169:
                   1170:                skip = sizeof(struct udphdr);
                   1171:        }
                   1172:
1.190.2.1  rmind    1173:        if (inpflags & INP_ESPINUDP_NON_IKE) {
1.142     kleink   1174:                u_int32_t *st = (u_int32_t *)data;
1.130     manu     1175:
                   1176:                if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1.142     kleink   1177:                    || ((st[0] | st[1]) != 0))
1.130     manu     1178:                        return 0; /* Normal UDP processing */
1.131     perry    1179:
1.130     manu     1180:                skip = sizeof(struct udphdr) + sizeof(u_int64_t);
                   1181:        }
                   1182:
                   1183:        /*
1.136     manu     1184:         * Get the UDP ports. They are handled in network
                   1185:         * order everywhere in IPSEC_NAT_T code.
                   1186:         */
1.158     christos 1187:        udphdr = (struct udphdr *)((char *)data - skip);
1.136     manu     1188:        sport = udphdr->uh_sport;
                   1189:        dport = udphdr->uh_dport;
                   1190:
                   1191:        /*
1.130     manu     1192:         * Remove the UDP header (and possibly the non ESP marker)
                   1193:         * IP header lendth is iphdrlen
1.131     perry    1194:         * Before:
1.130     manu     1195:         *   <--- off --->
                   1196:         *   +----+------+-----+
                   1197:         *   | IP |  UDP | ESP |
                   1198:         *   +----+------+-----+
                   1199:         *        <-skip->
                   1200:         * After:
                   1201:         *          +----+-----+
                   1202:         *          | IP | ESP |
                   1203:         *          +----+-----+
                   1204:         *   <-skip->
                   1205:         */
                   1206:        iphdrlen = off - sizeof(struct udphdr);
1.158     christos 1207:        memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1.130     manu     1208:        m_adj(m, skip);
                   1209:
                   1210:        ip = mtod(m, struct ip *);
                   1211:        ip->ip_len = htons(ntohs(ip->ip_len) - skip);
                   1212:        ip->ip_p = IPPROTO_ESP;
                   1213:
                   1214:        /*
1.188     christos 1215:         * We have modified the packet - it is now ESP, so we should not
                   1216:         * return to UDP processing ...
                   1217:         *
1.136     manu     1218:         * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
                   1219:         * the source UDP port. This is required if we want
                   1220:         * to select the right SPD for multiple hosts behind
                   1221:         * same NAT
                   1222:         */
1.137     manu     1223:        if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
                   1224:            sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
                   1225:                printf("udp4_espinudp: m_tag_get failed\n");
1.188     christos 1226:                m_freem(m);
                   1227:                return -1;
1.137     manu     1228:        }
1.136     manu     1229:        ((u_int16_t *)(tag + 1))[0] = sport;
                   1230:        ((u_int16_t *)(tag + 1))[1] = dport;
1.188     christos 1231:        m_tag_prepend(m, tag);
1.136     manu     1232:
1.190     christos 1233: #ifdef IPSEC
1.188     christos 1234:        ipsec4_common_input(m, iphdrlen, IPPROTO_ESP);
1.147     christos 1235: #else
1.188     christos 1236:        esp4_input(m, iphdrlen);
1.147     christos 1237: #endif
1.130     manu     1238:
1.185     liamjfoy 1239:        /* We handled it, it shouldn't be handled by UDP */
1.188     christos 1240:        *mp = NULL; /* avoid free by caller ... */
1.130     manu     1241:        return 1;
                   1242: }
                   1243: #endif
1.190.2.2  rmind    1244:
                   1245: const struct pr_usrreqs udp_usrreqs = {
                   1246:        .pr_attach      = udp_attach,
                   1247:        .pr_detach      = udp_detach,
                   1248:        .pr_generic     = udp_usrreq,
                   1249: };

CVSweb <webmaster@jp.NetBSD.org>