[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.179.4.1

1.179.4.1! 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:
                     63: #include <sys/cdefs.h>
1.179.4.1! rmind      64: __KERNEL_RCSID(0, "$NetBSD$");
1.50      thorpej    65:
1.77      soda       66: #include "opt_inet.h"
1.174     christos   67: #include "opt_compat_netbsd.h"
1.50      thorpej    68: #include "opt_ipsec.h"
1.78      thorpej    69: #include "opt_inet_csum.h"
1.64      ws         70: #include "opt_ipkdb.h"
1.101     martin     71: #include "opt_mbuftrace.h"
1.1       cgd        72:
1.5       mycroft    73: #include <sys/param.h>
                     74: #include <sys/malloc.h>
                     75: #include <sys/mbuf.h>
                     76: #include <sys/protosw.h>
                     77: #include <sys/socket.h>
                     78: #include <sys/socketvar.h>
1.13      mycroft    79: #include <sys/errno.h>
1.5       mycroft    80: #include <sys/stat.h>
1.27      christos   81: #include <sys/systm.h>
                     82: #include <sys/proc.h>
1.53      itojun     83: #include <sys/domain.h>
1.27      christos   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.166     thorpej    98: #include <netinet/udp_private.h>
1.1       cgd        99:
1.53      itojun    100: #ifdef INET6
                    101: #include <netinet/ip6.h>
                    102: #include <netinet/icmp6.h>
                    103: #include <netinet6/ip6_var.h>
1.167     thorpej   104: #include <netinet6/ip6_private.h>
1.53      itojun    105: #include <netinet6/in6_pcb.h>
                    106: #include <netinet6/udp6_var.h>
1.168     thorpej   107: #include <netinet6/udp6_private.h>
1.146     rpaulo    108: #include <netinet6/scope6_var.h>
1.53      itojun    109: #endif
                    110:
                    111: #ifndef INET6
                    112: /* always need ip6.h for IP6_EXTHDR_GET */
                    113: #include <netinet/ip6.h>
                    114: #endif
                    115:
1.76      itojun    116: #include "faith.h"
                    117: #if defined(NFAITH) && NFAITH > 0
                    118: #include <net/if_faith.h>
                    119: #endif
                    120:
1.27      christos  121: #include <machine/stdarg.h>
                    122:
1.105     jonathan  123: #ifdef FAST_IPSEC
                    124: #include <netipsec/ipsec.h>
1.169     thorpej   125: #include <netipsec/ipsec_var.h>
                    126: #include <netipsec/ipsec_private.h>
1.147     christos  127: #include <netipsec/esp.h>
1.105     jonathan  128: #ifdef INET6
                    129: #include <netipsec/ipsec6.h>
                    130: #endif
1.169     thorpej   131: #endif /* FAST_IPSEC */
1.105     jonathan  132:
1.48      itojun    133: #ifdef IPSEC
                    134: #include <netinet6/ipsec.h>
1.169     thorpej   135: #include <netinet6/ipsec_private.h>
1.147     christos  136: #include <netinet6/esp.h>
1.48      itojun    137: #include <netkey/key.h>
1.169     thorpej   138: #endif /* IPSEC */
1.48      itojun    139:
1.174     christos  140: #ifdef COMPAT_50
                    141: #include <compat/sys/socket.h>
                    142: #endif
                    143:
1.64      ws        144: #ifdef IPKDB
                    145: #include <ipkdb/ipkdb.h>
                    146: #endif
                    147:
1.8       mycroft   148: /*
                    149:  * UDP protocol implementation.
                    150:  * Per RFC 768, August, 1980.
                    151:  */
                    152: int    udpcksum = 1;
1.141     yamt      153: int    udp_do_loopback_cksum = 0;
1.93      matt      154:
                    155: struct inpcbtable udbtable;
1.164     thorpej   156:
1.166     thorpej   157: percpu_t *udpstat_percpu;
1.8       mycroft   158:
1.72      itojun    159: #ifdef INET
1.130     manu      160: #ifdef IPSEC_NAT_T
1.144     manu      161: static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
1.130     manu      162:        struct socket *);
                    163: #endif
1.119     matt      164: static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
                    165:        struct socket *);
                    166: static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
1.144     manu      167:        struct mbuf **, int);
1.129     yamt      168: static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.72      itojun    169: #endif
1.53      itojun    170: #ifdef INET6
1.119     matt      171: static void udp6_sendup (struct mbuf *, int, struct sockaddr *,
                    172:        struct socket *);
                    173: static int udp6_realinput (int, struct sockaddr_in6 *,
                    174:        struct sockaddr_in6 *, struct mbuf *, int);
1.129     yamt      175: static int udp6_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.53      itojun    176: #endif
1.72      itojun    177: #ifdef INET
1.119     matt      178: static void udp_notify (struct inpcb *, int);
1.72      itojun    179: #endif
1.7       mycroft   180:
1.26      mycroft   181: #ifndef UDBHASHSIZE
                    182: #define        UDBHASHSIZE     128
                    183: #endif
                    184: int    udbhashsize = UDBHASHSIZE;
                    185:
1.98      matt      186: #ifdef MBUFTRACE
1.150     dogcow    187: struct mowner udp_mowner = MOWNER_INIT("udp", "");
                    188: struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
                    189: struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
1.98      matt      190: #endif
                    191:
1.78      thorpej   192: #ifdef UDP_CSUM_COUNTERS
                    193: #include <sys/device.h>
                    194:
1.140     yamt      195: #if defined(INET)
1.78      thorpej   196: struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    197:     NULL, "udp", "hwcsum bad");
                    198: struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    199:     NULL, "udp", "hwcsum ok");
                    200: struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    201:     NULL, "udp", "hwcsum data");
                    202: struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    203:     NULL, "udp", "swcsum");
                    204:
1.120     matt      205: EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
                    206: EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
                    207: EVCNT_ATTACH_STATIC(udp_hwcsum_data);
                    208: EVCNT_ATTACH_STATIC(udp_swcsum);
1.140     yamt      209: #endif /* defined(INET) */
                    210:
                    211: #if defined(INET6)
                    212: struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    213:     NULL, "udp6", "hwcsum bad");
                    214: struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    215:     NULL, "udp6", "hwcsum ok");
                    216: struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    217:     NULL, "udp6", "hwcsum data");
                    218: struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    219:     NULL, "udp6", "swcsum");
                    220:
                    221: EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
                    222: EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
                    223: EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
                    224: EVCNT_ATTACH_STATIC(udp6_swcsum);
                    225: #endif /* defined(INET6) */
                    226:
                    227: #define        UDP_CSUM_COUNTER_INCR(ev)       (ev)->ev_count++
1.120     matt      228:
1.78      thorpej   229: #else
                    230:
                    231: #define        UDP_CSUM_COUNTER_INCR(ev)       /* nothing */
                    232:
                    233: #endif /* UDP_CSUM_COUNTERS */
                    234:
1.179     pooka     235: static void sysctl_net_inet_udp_setup(struct sysctllog **);
                    236:
1.7       mycroft   237: void
1.119     matt      238: udp_init(void)
1.1       cgd       239: {
1.18      mycroft   240:
1.179     pooka     241:        sysctl_net_inet_udp_setup(NULL);
                    242:
1.35      mycroft   243:        in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.78      thorpej   244:
1.98      matt      245:        MOWNER_ATTACH(&udp_tx_mowner);
                    246:        MOWNER_ATTACH(&udp_rx_mowner);
                    247:        MOWNER_ATTACH(&udp_mowner);
1.166     thorpej   248:
1.171     yamt      249: #ifdef INET
1.166     thorpej   250:        udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
1.171     yamt      251: #endif
                    252: #ifdef INET6
                    253:        udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
                    254: #endif
1.1       cgd       255: }
                    256:
1.129     yamt      257: /*
                    258:  * Checksum extended UDP header and data.
                    259:  */
                    260:
                    261: int
                    262: udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
                    263:     int iphlen, int len)
                    264: {
                    265:
                    266:        switch (af) {
1.72      itojun    267: #ifdef INET
1.129     yamt      268:        case AF_INET:
                    269:                return udp4_input_checksum(m, uh, iphlen, len);
                    270: #endif
                    271: #ifdef INET6
                    272:        case AF_INET6:
                    273:                return udp6_input_checksum(m, uh, iphlen, len);
                    274: #endif
                    275:        }
                    276: #ifdef DIAGNOSTIC
                    277:        panic("udp_input_checksum: unknown af %d", af);
                    278: #endif
                    279:        /* NOTREACHED */
                    280:        return -1;
                    281: }
                    282:
                    283: #ifdef INET
                    284:
                    285: /*
                    286:  * Checksum extended UDP header and data.
                    287:  */
                    288:
                    289: static int
                    290: udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
                    291:     int iphlen, int len)
                    292: {
                    293:
                    294:        /*
                    295:         * XXX it's better to record and check if this mbuf is
                    296:         * already checked.
                    297:         */
                    298:
                    299:        if (uh->uh_sum == 0)
                    300:                return 0;
                    301:
                    302:        switch (m->m_pkthdr.csum_flags &
                    303:            ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
                    304:            M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
                    305:        case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
                    306:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
                    307:                goto badcsum;
                    308:
                    309:        case M_CSUM_UDPv4|M_CSUM_DATA: {
                    310:                u_int32_t hw_csum = m->m_pkthdr.csum_data;
                    311:
                    312:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
                    313:                if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
                    314:                        const struct ip *ip =
                    315:                            mtod(m, const struct ip *);
                    316:
                    317:                        hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
                    318:                            ip->ip_dst.s_addr,
                    319:                            htons(hw_csum + len + IPPROTO_UDP));
                    320:                }
                    321:                if ((hw_csum ^ 0xffff) != 0)
                    322:                        goto badcsum;
                    323:                break;
                    324:        }
                    325:
                    326:        case M_CSUM_UDPv4:
                    327:                /* Checksum was okay. */
                    328:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
                    329:                break;
                    330:
                    331:        default:
                    332:                /*
                    333:                 * Need to compute it ourselves.  Maybe skip checksum
                    334:                 * on loopback interfaces.
                    335:                 */
                    336:                if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
                    337:                                     IFF_LOOPBACK) ||
                    338:                                   udp_do_loopback_cksum)) {
                    339:                        UDP_CSUM_COUNTER_INCR(&udp_swcsum);
                    340:                        if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
                    341:                                goto badcsum;
                    342:                }
                    343:                break;
                    344:        }
                    345:
                    346:        return 0;
                    347:
                    348: badcsum:
1.166     thorpej   349:        UDP_STATINC(UDP_STAT_BADSUM);
1.129     yamt      350:        return -1;
                    351: }
                    352:
1.7       mycroft   353: void
1.27      christos  354: udp_input(struct mbuf *m, ...)
1.1       cgd       355: {
1.53      itojun    356:        va_list ap;
                    357:        struct sockaddr_in src, dst;
                    358:        struct ip *ip;
                    359:        struct udphdr *uh;
1.97      simonb    360:        int iphlen;
1.53      itojun    361:        int len;
                    362:        int n;
1.96      itojun    363:        u_int16_t ip_len;
1.53      itojun    364:
                    365:        va_start(ap, m);
                    366:        iphlen = va_arg(ap, int);
1.97      simonb    367:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.53      itojun    368:        va_end(ap);
                    369:
1.98      matt      370:        MCLAIM(m, &udp_rx_mowner);
1.166     thorpej   371:        UDP_STATINC(UDP_STAT_IPACKETS);
1.53      itojun    372:
                    373:        /*
                    374:         * Get IP and UDP header together in first mbuf.
                    375:         */
                    376:        ip = mtod(m, struct ip *);
                    377:        IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
                    378:        if (uh == NULL) {
1.166     thorpej   379:                UDP_STATINC(UDP_STAT_HDROPS);
1.53      itojun    380:                return;
                    381:        }
1.95      thorpej   382:        KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53      itojun    383:
1.57      itojun    384:        /* destination port of 0 is illegal, based on RFC768. */
                    385:        if (uh->uh_dport == 0)
                    386:                goto bad;
                    387:
1.53      itojun    388:        /*
                    389:         * Make mbuf data length reflect UDP length.
                    390:         * If not enough data to reflect UDP length, drop.
                    391:         */
1.96      itojun    392:        ip_len = ntohs(ip->ip_len);
1.53      itojun    393:        len = ntohs((u_int16_t)uh->uh_ulen);
1.96      itojun    394:        if (ip_len != iphlen + len) {
                    395:                if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.166     thorpej   396:                        UDP_STATINC(UDP_STAT_BADLEN);
1.53      itojun    397:                        goto bad;
                    398:                }
1.96      itojun    399:                m_adj(m, iphlen + len - ip_len);
1.53      itojun    400:        }
                    401:
                    402:        /*
                    403:         * Checksum extended UDP header and data.
                    404:         */
1.129     yamt      405:        if (udp4_input_checksum(m, uh, iphlen, len))
                    406:                goto badcsum;
1.53      itojun    407:
                    408:        /* construct source and dst sockaddrs. */
1.159     dyoung    409:        sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
                    410:        sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
1.53      itojun    411:
1.144     manu      412:        if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
1.166     thorpej   413:                UDP_STATINC(UDP_STAT_HDROPS);
1.144     manu      414:                return;
                    415:        }
1.53      itojun    416: #ifdef INET6
                    417:        if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
                    418:                struct sockaddr_in6 src6, dst6;
                    419:
1.175     cegger    420:                memset(&src6, 0, sizeof(src6));
1.53      itojun    421:                src6.sin6_family = AF_INET6;
                    422:                src6.sin6_len = sizeof(struct sockaddr_in6);
                    423:                src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
1.177     tsutsui   424:                memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
1.53      itojun    425:                        sizeof(ip->ip_src));
                    426:                src6.sin6_port = uh->uh_sport;
1.175     cegger    427:                memset(&dst6, 0, sizeof(dst6));
1.53      itojun    428:                dst6.sin6_family = AF_INET6;
                    429:                dst6.sin6_len = sizeof(struct sockaddr_in6);
                    430:                dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
1.177     tsutsui   431:                memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
1.53      itojun    432:                        sizeof(ip->ip_dst));
                    433:                dst6.sin6_port = uh->uh_dport;
                    434:
                    435:                n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
                    436:        }
                    437: #endif
                    438:
                    439:        if (n == 0) {
                    440:                if (m->m_flags & (M_BCAST | M_MCAST)) {
1.166     thorpej   441:                        UDP_STATINC(UDP_STAT_NOPORTBCAST);
1.53      itojun    442:                        goto bad;
                    443:                }
1.166     thorpej   444:                UDP_STATINC(UDP_STAT_NOPORT);
1.64      ws        445: #ifdef IPKDB
1.53      itojun    446:                if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
                    447:                                m, iphlen + sizeof(struct udphdr),
                    448:                                m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
                    449:                        /*
                    450:                         * It was a debugger connect packet,
                    451:                         * just drop it now
                    452:                         */
                    453:                        goto bad;
                    454:                }
                    455: #endif
                    456:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
                    457:                m = NULL;
                    458:        }
                    459:
                    460: bad:
                    461:        if (m)
                    462:                m_freem(m);
1.78      thorpej   463:        return;
                    464:
                    465: badcsum:
                    466:        m_freem(m);
1.53      itojun    467: }
1.72      itojun    468: #endif
1.53      itojun    469:
                    470: #ifdef INET6
1.129     yamt      471: static int
                    472: udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
                    473: {
                    474:
1.140     yamt      475:        /*
                    476:         * XXX it's better to record and check if this mbuf is
                    477:         * already checked.
                    478:         */
                    479:
1.129     yamt      480:        if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
                    481:                goto good;
                    482:        }
                    483:        if (uh->uh_sum == 0) {
1.168     thorpej   484:                UDP6_STATINC(UDP6_STAT_NOSUM);
1.129     yamt      485:                goto bad;
                    486:        }
1.140     yamt      487:
                    488:        switch (m->m_pkthdr.csum_flags &
                    489:            ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv6) |
                    490:            M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
                    491:        case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
                    492:                UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
1.168     thorpej   493:                UDP6_STATINC(UDP6_STAT_BADSUM);
1.129     yamt      494:                goto bad;
1.140     yamt      495:
                    496: #if 0 /* notyet */
                    497:        case M_CSUM_UDPv6|M_CSUM_DATA:
                    498: #endif
                    499:
                    500:        case M_CSUM_UDPv6:
                    501:                /* Checksum was okay. */
                    502:                UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
                    503:                break;
                    504:
                    505:        default:
                    506:                /*
                    507:                 * Need to compute it ourselves.  Maybe skip checksum
                    508:                 * on loopback interfaces.
                    509:                 */
                    510:                UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
                    511:                if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
1.168     thorpej   512:                        UDP6_STATINC(UDP6_STAT_BADSUM);
1.140     yamt      513:                        goto bad;
                    514:                }
1.129     yamt      515:        }
                    516:
                    517: good:
                    518:        return 0;
                    519: bad:
                    520:        return -1;
                    521: }
                    522:
1.53      itojun    523: int
1.154     yamt      524: udp6_input(struct mbuf **mp, int *offp, int proto)
1.53      itojun    525: {
                    526:        struct mbuf *m = *mp;
                    527:        int off = *offp;
                    528:        struct sockaddr_in6 src, dst;
                    529:        struct ip6_hdr *ip6;
                    530:        struct udphdr *uh;
                    531:        u_int32_t plen, ulen;
                    532:
1.76      itojun    533:        ip6 = mtod(m, struct ip6_hdr *);
                    534:
1.53      itojun    535: #if defined(NFAITH) && 0 < NFAITH
1.76      itojun    536:        if (faithprefix(&ip6->ip6_dst)) {
                    537:                /* send icmp6 host unreach? */
                    538:                m_freem(m);
                    539:                return IPPROTO_DONE;
1.53      itojun    540:        }
                    541: #endif
                    542:
1.168     thorpej   543:        UDP6_STATINC(UDP6_STAT_IPACKETS);
1.53      itojun    544:
                    545:        /* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
                    546:        plen = m->m_pkthdr.len - off;
                    547:        IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
                    548:        if (uh == NULL) {
1.167     thorpej   549:                IP6_STATINC(IP6_STAT_TOOSHORT);
1.53      itojun    550:                return IPPROTO_DONE;
                    551:        }
1.95      thorpej   552:        KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53      itojun    553:        ulen = ntohs((u_short)uh->uh_ulen);
1.69      itojun    554:        /*
                    555:         * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
                    556:         * iff payload length > 0xffff.
                    557:         */
1.53      itojun    558:        if (ulen == 0 && plen > 0xffff)
                    559:                ulen = plen;
                    560:
                    561:        if (plen != ulen) {
1.168     thorpej   562:                UDP6_STATINC(UDP6_STAT_BADLEN);
1.54      itojun    563:                goto bad;
                    564:        }
                    565:
1.57      itojun    566:        /* destination port of 0 is illegal, based on RFC768. */
                    567:        if (uh->uh_dport == 0)
                    568:                goto bad;
                    569:
1.54      itojun    570:        /* Be proactive about malicious use of IPv4 mapped address */
                    571:        if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
                    572:            IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
                    573:                /* XXX stat */
1.53      itojun    574:                goto bad;
                    575:        }
                    576:
                    577:        /*
1.125     thorpej   578:         * Checksum extended UDP header and data.  Maybe skip checksum
                    579:         * on loopback interfaces.
1.53      itojun    580:         */
1.129     yamt      581:        if (udp6_input_checksum(m, uh, off, ulen))
                    582:                goto bad;
1.53      itojun    583:
                    584:        /*
                    585:         * Construct source and dst sockaddrs.
                    586:         */
1.175     cegger    587:        memset(&src, 0, sizeof(src));
1.53      itojun    588:        src.sin6_family = AF_INET6;
                    589:        src.sin6_len = sizeof(struct sockaddr_in6);
1.146     rpaulo    590:        src.sin6_addr = ip6->ip6_src;
1.53      itojun    591:        src.sin6_port = uh->uh_sport;
1.175     cegger    592:        memset(&dst, 0, sizeof(dst));
1.53      itojun    593:        dst.sin6_family = AF_INET6;
                    594:        dst.sin6_len = sizeof(struct sockaddr_in6);
1.146     rpaulo    595:        dst.sin6_addr = ip6->ip6_dst;
1.53      itojun    596:        dst.sin6_port = uh->uh_dport;
                    597:
                    598:        if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
                    599:                if (m->m_flags & M_MCAST) {
1.168     thorpej   600:                        UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
1.53      itojun    601:                        goto bad;
                    602:                }
1.168     thorpej   603:                UDP6_STATINC(UDP6_STAT_NOPORT);
1.53      itojun    604:                icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
                    605:                m = NULL;
                    606:        }
                    607:
                    608: bad:
                    609:        if (m)
                    610:                m_freem(m);
                    611:        return IPPROTO_DONE;
                    612: }
                    613: #endif
                    614:
1.72      itojun    615: #ifdef INET
1.53      itojun    616: static void
1.119     matt      617: udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
                    618:        struct sockaddr *src, struct socket *so)
1.53      itojun    619: {
                    620:        struct mbuf *opts = NULL;
                    621:        struct mbuf *n;
                    622:        struct inpcb *inp = NULL;
                    623:
                    624:        if (!so)
                    625:                return;
                    626:        switch (so->so_proto->pr_domain->dom_family) {
                    627:        case AF_INET:
                    628:                inp = sotoinpcb(so);
                    629:                break;
                    630: #ifdef INET6
                    631:        case AF_INET6:
                    632:                break;
                    633: #endif
                    634:        default:
                    635:                return;
                    636:        }
                    637:
1.105     jonathan  638: #if defined(IPSEC) || defined(FAST_IPSEC)
1.53      itojun    639:        /* check AH/ESP integrity. */
                    640:        if (so != NULL && ipsec4_in_reject_so(m, so)) {
1.169     thorpej   641:                IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.162     dyoung    642:                if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
1.110     itojun    643:                        icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
                    644:                            0, 0);
1.53      itojun    645:                return;
                    646:        }
                    647: #endif /*IPSEC*/
                    648:
1.162     dyoung    649:        if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.53      itojun    650:                if (inp && (inp->inp_flags & INP_CONTROLOPTS
1.174     christos  651: #ifdef SO_OTIMESTAMP
                    652:                         || so->so_options & SO_OTIMESTAMP
                    653: #endif
1.53      itojun    654:                         || so->so_options & SO_TIMESTAMP)) {
                    655:                        struct ip *ip = mtod(n, struct ip *);
                    656:                        ip_savecontrol(inp, &opts, ip, n);
                    657:                }
                    658:
                    659:                m_adj(n, off);
                    660:                if (sbappendaddr(&so->so_rcv, src, n,
                    661:                                opts) == 0) {
                    662:                        m_freem(n);
                    663:                        if (opts)
                    664:                                m_freem(opts);
1.124     darrenr   665:                        so->so_rcv.sb_overflowed++;
1.166     thorpej   666:                        UDP_STATINC(UDP_STAT_FULLSOCK);
1.53      itojun    667:                } else
                    668:                        sorwakeup(so);
                    669:        }
                    670: }
1.72      itojun    671: #endif
1.53      itojun    672:
                    673: #ifdef INET6
                    674: static void
1.119     matt      675: udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
                    676:        struct sockaddr *src, struct socket *so)
1.53      itojun    677: {
                    678:        struct mbuf *opts = NULL;
                    679:        struct mbuf *n;
                    680:        struct in6pcb *in6p = NULL;
                    681:
                    682:        if (!so)
                    683:                return;
                    684:        if (so->so_proto->pr_domain->dom_family != AF_INET6)
                    685:                return;
                    686:        in6p = sotoin6pcb(so);
                    687:
1.105     jonathan  688: #if defined(IPSEC) || defined(FAST_IPSEC)
1.53      itojun    689:        /* check AH/ESP integrity. */
                    690:        if (so != NULL && ipsec6_in_reject_so(m, so)) {
1.169     thorpej   691:                IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
1.162     dyoung    692:                if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
1.110     itojun    693:                        icmp6_error(n, ICMP6_DST_UNREACH,
                    694:                            ICMP6_DST_UNREACH_ADMIN, 0);
1.53      itojun    695:                return;
                    696:        }
                    697: #endif /*IPSEC*/
                    698:
1.162     dyoung    699:        if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.53      itojun    700:                if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
1.174     christos  701: #ifdef SO_OTIMESTAMP
                    702:                    || in6p->in6p_socket->so_options & SO_OTIMESTAMP
                    703: #endif
                    704:                    || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
1.53      itojun    705:                        struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
                    706:                        ip6_savecontrol(in6p, &opts, ip6, n);
                    707:                }
                    708:
                    709:                m_adj(n, off);
                    710:                if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
                    711:                        m_freem(n);
                    712:                        if (opts)
                    713:                                m_freem(opts);
1.124     darrenr   714:                        so->so_rcv.sb_overflowed++;
1.168     thorpej   715:                        UDP6_STATINC(UDP6_STAT_FULLSOCK);
1.53      itojun    716:                } else
                    717:                        sorwakeup(so);
                    718:        }
                    719: }
                    720: #endif
                    721:
1.72      itojun    722: #ifdef INET
1.53      itojun    723: static int
1.119     matt      724: udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
1.144     manu      725:        struct mbuf **mp, int off /* offset of udphdr */)
1.53      itojun    726: {
                    727:        u_int16_t *sport, *dport;
                    728:        int rcvcnt;
                    729:        struct in_addr *src4, *dst4;
1.109     itojun    730:        struct inpcb_hdr *inph;
1.53      itojun    731:        struct inpcb *inp;
1.144     manu      732:        struct mbuf *m = *mp;
1.53      itojun    733:
                    734:        rcvcnt = 0;
                    735:        off += sizeof(struct udphdr);   /* now, offset of payload */
                    736:
                    737:        if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
                    738:                goto bad;
                    739:
                    740:        src4 = &src->sin_addr;
                    741:        sport = &src->sin_port;
                    742:        dst4 = &dst->sin_addr;
                    743:        dport = &dst->sin_port;
                    744:
1.73      itojun    745:        if (IN_MULTICAST(dst4->s_addr) ||
1.53      itojun    746:            in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
                    747:                /*
                    748:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    749:                 * for which the local and remote addresses and ports match
                    750:                 * those of the incoming datagram.  This allows more than
                    751:                 * one process to receive multi/broadcasts on the same port.
                    752:                 * (This really ought to be done for unicast datagrams as
                    753:                 * well, but that would cause problems with existing
                    754:                 * applications that open both address-specific sockets and
                    755:                 * a wildcard socket listening to the same port -- they would
                    756:                 * end up receiving duplicates of every unicast datagram.
                    757:                 * Those applications open the multiple sockets to overcome an
                    758:                 * inadequacy of the UDP socket interface, but for backwards
                    759:                 * compatibility we avoid the problem here rather than
                    760:                 * fixing the interface.  Maybe 4.5BSD will remedy this?)
                    761:                 */
                    762:
                    763:                /*
1.92      itojun    764:                 * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71      itojun    765:                 * we need udpiphdr for IPsec processing so we do that later.
1.53      itojun    766:                 */
                    767:                /*
                    768:                 * Locate pcb(s) for datagram.
                    769:                 */
1.109     itojun    770:                CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
                    771:                        inp = (struct inpcb *)inph;
                    772:                        if (inp->inp_af != AF_INET)
                    773:                                continue;
                    774:
1.53      itojun    775:                        if (inp->inp_lport != *dport)
                    776:                                continue;
                    777:                        if (!in_nullhost(inp->inp_laddr)) {
                    778:                                if (!in_hosteq(inp->inp_laddr, *dst4))
                    779:                                        continue;
                    780:                        }
                    781:                        if (!in_nullhost(inp->inp_faddr)) {
                    782:                                if (!in_hosteq(inp->inp_faddr, *src4) ||
                    783:                                    inp->inp_fport != *sport)
                    784:                                        continue;
                    785:                        }
                    786:
                    787:                        udp4_sendup(m, off, (struct sockaddr *)src,
                    788:                                inp->inp_socket);
                    789:                        rcvcnt++;
                    790:
                    791:                        /*
                    792:                         * Don't look for additional matches if this one does
                    793:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    794:                         * socket options set.  This heuristic avoids searching
                    795:                         * through all pcbs in the common case of a non-shared
                    796:                         * port.  It assumes that an application will never
                    797:                         * clear these options after setting them.
                    798:                         */
                    799:                        if ((inp->inp_socket->so_options &
                    800:                            (SO_REUSEPORT|SO_REUSEADDR)) == 0)
                    801:                                break;
                    802:                }
                    803:        } else {
                    804:                /*
                    805:                 * Locate pcb for datagram.
                    806:                 */
1.179.4.1! rmind     807:                inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4,
        !           808:                    *dport, 0);
1.53      itojun    809:                if (inp == 0) {
1.166     thorpej   810:                        UDP_STATINC(UDP_STAT_PCBHASHMISS);
1.53      itojun    811:                        inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
1.82      itojun    812:                        if (inp == 0)
1.53      itojun    813:                                return rcvcnt;
                    814:                }
                    815:
1.130     manu      816: #ifdef IPSEC_NAT_T
                    817:                /* Handle ESP over UDP */
                    818:                if (inp->inp_flags & INP_ESPINUDP_ALL) {
                    819:                        struct sockaddr *sa = (struct sockaddr *)src;
                    820:
1.144     manu      821:                        switch(udp4_espinudp(mp, off, sa, inp->inp_socket)) {
                    822:                        case -1:        /* Error, m was freeed */
                    823:                                rcvcnt = -1;
                    824:                                goto bad;
                    825:                                break;
                    826:
                    827:                        case 1:         /* ESP over UDP */
1.130     manu      828:                                rcvcnt++;
                    829:                                goto bad;
1.144     manu      830:                                break;
                    831:
                    832:                        case 0:         /* plain UDP */
                    833:                        default:        /* Unexpected */
                    834:                                /*
                    835:                                 * Normal UDP processing will take place
                    836:                                 * m may have changed.
                    837:                                 */
                    838:                                m = *mp;
                    839:                                break;
1.130     manu      840:                        }
                    841:                }
                    842: #endif
                    843:
1.178     minskim   844:                /*
                    845:                 * Check the minimum TTL for socket.
                    846:                 */
                    847:                if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl)
                    848:                        goto bad;
                    849:
1.53      itojun    850:                udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
                    851:                rcvcnt++;
                    852:        }
                    853:
                    854: bad:
                    855:        return rcvcnt;
                    856: }
1.72      itojun    857: #endif
1.53      itojun    858:
                    859: #ifdef INET6
                    860: static int
1.119     matt      861: udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
                    862:        struct mbuf *m, int off)
1.53      itojun    863: {
1.79      itojun    864:        u_int16_t sport, dport;
1.53      itojun    865:        int rcvcnt;
1.146     rpaulo    866:        struct in6_addr src6, *dst6;
1.79      itojun    867:        const struct in_addr *dst4;
1.109     itojun    868:        struct inpcb_hdr *inph;
1.53      itojun    869:        struct in6pcb *in6p;
                    870:
                    871:        rcvcnt = 0;
                    872:        off += sizeof(struct udphdr);   /* now, offset of payload */
                    873:
                    874:        if (af != AF_INET && af != AF_INET6)
                    875:                goto bad;
                    876:        if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
                    877:                goto bad;
                    878:
1.146     rpaulo    879:        src6 = src->sin6_addr;
                    880:        if (sa6_recoverscope(src) != 0) {
                    881:                /* XXX: should be impossible. */
                    882:                goto bad;
                    883:        }
1.79      itojun    884:        sport = src->sin6_port;
1.146     rpaulo    885:
1.79      itojun    886:        dport = dst->sin6_port;
1.88      itojun    887:        dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
1.146     rpaulo    888:        dst6 = &dst->sin6_addr;
1.53      itojun    889:
1.146     rpaulo    890:        if (IN6_IS_ADDR_MULTICAST(dst6) ||
1.79      itojun    891:            (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
1.53      itojun    892:                /*
                    893:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    894:                 * for which the local and remote addresses and ports match
                    895:                 * those of the incoming datagram.  This allows more than
                    896:                 * one process to receive multi/broadcasts on the same port.
                    897:                 * (This really ought to be done for unicast datagrams as
                    898:                 * well, but that would cause problems with existing
                    899:                 * applications that open both address-specific sockets and
                    900:                 * a wildcard socket listening to the same port -- they would
                    901:                 * end up receiving duplicates of every unicast datagram.
                    902:                 * Those applications open the multiple sockets to overcome an
                    903:                 * inadequacy of the UDP socket interface, but for backwards
                    904:                 * compatibility we avoid the problem here rather than
                    905:                 * fixing the interface.  Maybe 4.5BSD will remedy this?)
                    906:                 */
                    907:
                    908:                /*
1.92      itojun    909:                 * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71      itojun    910:                 * we need udpiphdr for IPsec processing so we do that later.
1.53      itojun    911:                 */
                    912:                /*
                    913:                 * Locate pcb(s) for datagram.
                    914:                 */
1.109     itojun    915:                CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
                    916:                        in6p = (struct in6pcb *)inph;
                    917:                        if (in6p->in6p_af != AF_INET6)
                    918:                                continue;
                    919:
1.79      itojun    920:                        if (in6p->in6p_lport != dport)
1.53      itojun    921:                                continue;
                    922:                        if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1.146     rpaulo    923:                                if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
                    924:                                    dst6))
1.53      itojun    925:                                        continue;
1.92      itojun    926:                        } else {
1.146     rpaulo    927:                                if (IN6_IS_ADDR_V4MAPPED(dst6) &&
1.85      itojun    928:                                    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55      itojun    929:                                        continue;
                    930:                        }
1.53      itojun    931:                        if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1.79      itojun    932:                                if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
                    933:                                    &src6) || in6p->in6p_fport != sport)
1.53      itojun    934:                                        continue;
1.92      itojun    935:                        } else {
1.79      itojun    936:                                if (IN6_IS_ADDR_V4MAPPED(&src6) &&
1.85      itojun    937:                                    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55      itojun    938:                                        continue;
                    939:                        }
1.53      itojun    940:
                    941:                        udp6_sendup(m, off, (struct sockaddr *)src,
                    942:                                in6p->in6p_socket);
                    943:                        rcvcnt++;
                    944:
                    945:                        /*
                    946:                         * Don't look for additional matches if this one does
                    947:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    948:                         * socket options set.  This heuristic avoids searching
                    949:                         * through all pcbs in the common case of a non-shared
                    950:                         * port.  It assumes that an application will never
                    951:                         * clear these options after setting them.
                    952:                         */
                    953:                        if ((in6p->in6p_socket->so_options &
                    954:                            (SO_REUSEPORT|SO_REUSEADDR)) == 0)
                    955:                                break;
                    956:                }
                    957:        } else {
                    958:                /*
                    959:                 * Locate pcb for datagram.
                    960:                 */
1.146     rpaulo    961:                in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
1.179.4.1! rmind     962:                                             dport, 0, 0);
1.53      itojun    963:                if (in6p == 0) {
1.166     thorpej   964:                        UDP_STATINC(UDP_STAT_PCBHASHMISS);
1.146     rpaulo    965:                        in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
1.82      itojun    966:                        if (in6p == 0)
1.53      itojun    967:                                return rcvcnt;
                    968:                }
                    969:
                    970:                udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
                    971:                rcvcnt++;
                    972:        }
                    973:
                    974: bad:
                    975:        return rcvcnt;
                    976: }
                    977: #endif
1.1       cgd       978:
1.72      itojun    979: #ifdef INET
1.1       cgd       980: /*
                    981:  * Notify a udp user of an asynchronous error;
                    982:  * just wake up so that he can collect error status.
                    983:  */
1.7       mycroft   984: static void
1.119     matt      985: udp_notify(struct inpcb *inp, int errno)
1.1       cgd       986: {
                    987:        inp->inp_socket->so_error = errno;
                    988:        sorwakeup(inp->inp_socket);
                    989:        sowwakeup(inp->inp_socket);
                    990: }
                    991:
1.27      christos  992: void *
1.157     dyoung    993: udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.1       cgd       994: {
1.66      augustss  995:        struct ip *ip = v;
                    996:        struct udphdr *uh;
1.119     matt      997:        void (*notify)(struct inpcb *, int) = udp_notify;
1.21      mycroft   998:        int errno;
1.1       cgd       999:
1.53      itojun   1000:        if (sa->sa_family != AF_INET
                   1001:         || sa->sa_len != sizeof(struct sockaddr_in))
1.51      itojun   1002:                return NULL;
1.20      mycroft  1003:        if ((unsigned)cmd >= PRC_NCMDS)
1.27      christos 1004:                return NULL;
1.20      mycroft  1005:        errno = inetctlerrmap[cmd];
1.18      mycroft  1006:        if (PRC_IS_REDIRECT(cmd))
1.19      mycroft  1007:                notify = in_rtchange, ip = 0;
1.18      mycroft  1008:        else if (cmd == PRC_HOSTDEAD)
1.19      mycroft  1009:                ip = 0;
1.23      cgd      1010:        else if (errno == 0)
1.27      christos 1011:                return NULL;
1.19      mycroft  1012:        if (ip) {
1.158     christos 1013:                uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
1.157     dyoung   1014:                in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
1.34      mycroft  1015:                    ip->ip_src, uh->uh_sport, errno, notify);
1.53      itojun   1016:
                   1017:                /* XXX mapped address case */
1.19      mycroft  1018:        } else
1.157     dyoung   1019:                in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
1.34      mycroft  1020:                    notify);
1.27      christos 1021:        return NULL;
1.1       cgd      1022: }
                   1023:
1.7       mycroft  1024: int
1.173     plunky   1025: udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.130     manu     1026: {
                   1027:        int s;
                   1028:        int error = 0;
                   1029:        struct inpcb *inp;
                   1030:        int family;
1.173     plunky   1031:        int optval;
1.130     manu     1032:
                   1033:        family = so->so_proto->pr_domain->dom_family;
                   1034:
                   1035:        s = splsoftnet();
                   1036:        switch (family) {
                   1037: #ifdef INET
                   1038:        case PF_INET:
1.173     plunky   1039:                if (sopt->sopt_level != IPPROTO_UDP) {
                   1040:                        error = ip_ctloutput(op, so, sopt);
1.130     manu     1041:                        goto end;
                   1042:                }
                   1043:                break;
                   1044: #endif
                   1045: #ifdef INET6
                   1046:        case PF_INET6:
1.173     plunky   1047:                if (sopt->sopt_level != IPPROTO_UDP) {
                   1048:                        error = ip6_ctloutput(op, so, sopt);
1.130     manu     1049:                        goto end;
                   1050:                }
                   1051:                break;
                   1052: #endif
                   1053:        default:
                   1054:                error = EAFNOSUPPORT;
                   1055:                goto end;
                   1056:        }
                   1057:
                   1058:
                   1059:        switch (op) {
                   1060:        case PRCO_SETOPT:
                   1061:                inp = sotoinpcb(so);
                   1062:
1.173     plunky   1063:                switch (sopt->sopt_name) {
1.130     manu     1064:                case UDP_ENCAP:
1.173     plunky   1065:                        error = sockopt_getint(sopt, &optval);
                   1066:                        if (error)
1.153     yamt     1067:                                break;
1.131     perry    1068:
1.173     plunky   1069:                        switch(optval) {
1.130     manu     1070: #ifdef IPSEC_NAT_T
                   1071:                        case 0:
                   1072:                                inp->inp_flags &= ~INP_ESPINUDP_ALL;
                   1073:                                break;
                   1074:
                   1075:                        case UDP_ENCAP_ESPINUDP:
                   1076:                                inp->inp_flags &= ~INP_ESPINUDP_ALL;
                   1077:                                inp->inp_flags |= INP_ESPINUDP;
                   1078:                                break;
1.131     perry    1079:
1.130     manu     1080:                        case UDP_ENCAP_ESPINUDP_NON_IKE:
                   1081:                                inp->inp_flags &= ~INP_ESPINUDP_ALL;
                   1082:                                inp->inp_flags |= INP_ESPINUDP_NON_IKE;
                   1083:                                break;
                   1084: #endif
                   1085:                        default:
                   1086:                                error = EINVAL;
                   1087:                                break;
                   1088:                        }
                   1089:                        break;
                   1090:
                   1091:                default:
                   1092:                        error = ENOPROTOOPT;
                   1093:                        break;
                   1094:                }
                   1095:                break;
                   1096:
                   1097:        default:
                   1098:                error = EINVAL;
                   1099:                break;
1.131     perry    1100:        }
                   1101:
1.130     manu     1102: end:
                   1103:        splx(s);
                   1104:        return error;
                   1105: }
1.131     perry    1106:
1.130     manu     1107:
                   1108: int
1.27      christos 1109: udp_output(struct mbuf *m, ...)
                   1110: {
1.66      augustss 1111:        struct inpcb *inp;
                   1112:        struct udpiphdr *ui;
1.125     thorpej  1113:        struct route *ro;
1.66      augustss 1114:        int len = m->m_pkthdr.len;
1.31      mycroft  1115:        int error = 0;
1.27      christos 1116:        va_list ap;
                   1117:
1.98      matt     1118:        MCLAIM(m, &udp_tx_mowner);
1.27      christos 1119:        va_start(ap, m);
                   1120:        inp = va_arg(ap, struct inpcb *);
                   1121:        va_end(ap);
1.1       cgd      1122:
                   1123:        /*
                   1124:         * Calculate data length and get a mbuf
                   1125:         * for UDP and IP headers.
                   1126:         */
1.13      mycroft  1127:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
                   1128:        if (m == 0) {
                   1129:                error = ENOBUFS;
1.39      thorpej  1130:                goto release;
                   1131:        }
                   1132:
                   1133:        /*
                   1134:         * Compute the packet length of the IP header, and
                   1135:         * punt if the length looks bogus.
                   1136:         */
1.96      itojun   1137:        if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39      thorpej  1138:                error = EMSGSIZE;
1.13      mycroft  1139:                goto release;
                   1140:        }
1.1       cgd      1141:
                   1142:        /*
                   1143:         * Fill in mbuf with extended UDP header
                   1144:         * and addresses and length put into network format.
                   1145:         */
                   1146:        ui = mtod(m, struct udpiphdr *);
                   1147:        ui->ui_pr = IPPROTO_UDP;
                   1148:        ui->ui_src = inp->inp_laddr;
                   1149:        ui->ui_dst = inp->inp_faddr;
                   1150:        ui->ui_sport = inp->inp_lport;
                   1151:        ui->ui_dport = inp->inp_fport;
1.78      thorpej  1152:        ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1       cgd      1153:
1.125     thorpej  1154:        ro = &inp->inp_route;
                   1155:
1.1       cgd      1156:        /*
1.78      thorpej  1157:         * Set up checksum and output datagram.
1.1       cgd      1158:         */
                   1159:        if (udpcksum) {
1.78      thorpej  1160:                /*
                   1161:                 * XXX Cache pseudo-header checksum part for
                   1162:                 * XXX "connected" UDP sockets.
                   1163:                 */
                   1164:                ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
                   1165:                    ui->ui_dst.s_addr, htons((u_int16_t)len +
                   1166:                    sizeof(struct udphdr) + IPPROTO_UDP));
1.135     yamt     1167:                m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1.78      thorpej  1168:                m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
                   1169:        } else
                   1170:                ui->ui_sum = 0;
1.96      itojun   1171:        ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1.1       cgd      1172:        ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
                   1173:        ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1.166     thorpej  1174:        UDP_STATINC(UDP_STAT_OPACKETS);
1.48      itojun   1175:
1.125     thorpej  1176:        return (ip_output(m, inp->inp_options, ro,
1.12      mycroft  1177:            inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.107     itojun   1178:            inp->inp_moptions, inp->inp_socket));
1.1       cgd      1179:
                   1180: release:
                   1181:        m_freem(m);
                   1182:        return (error);
                   1183: }
                   1184:
1.42      thorpej  1185: int    udp_sendspace = 9216;           /* really max datagram size */
                   1186: int    udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1       cgd      1187:                                        /* 40 1K datagrams */
                   1188:
                   1189: /*ARGSUSED*/
1.7       mycroft  1190: int
1.119     matt     1191: udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1.145     christos 1192:        struct mbuf *control, struct lwp *l)
1.1       cgd      1193: {
1.66      augustss 1194:        struct inpcb *inp;
1.1       cgd      1195:        int s;
1.66      augustss 1196:        int error = 0;
1.1       cgd      1197:
                   1198:        if (req == PRU_CONTROL)
1.158     christos 1199:                return (in_control(so, (long)m, (void *)nam,
1.148     ad       1200:                    (struct ifnet *)control, l));
1.59      thorpej  1201:
1.149     tls      1202:        s = splsoftnet();
                   1203:
1.60      thorpej  1204:        if (req == PRU_PURGEIF) {
1.170     ad       1205:                mutex_enter(softnet_lock);
1.80      itojun   1206:                in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
1.60      thorpej  1207:                in_purgeif((struct ifnet *)control);
                   1208:                in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1.170     ad       1209:                mutex_exit(softnet_lock);
1.149     tls      1210:                splx(s);
1.59      thorpej  1211:                return (0);
                   1212:        }
1.31      mycroft  1213:
                   1214:        inp = sotoinpcb(so);
1.32      mycroft  1215: #ifdef DIAGNOSTIC
                   1216:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                   1217:                panic("udp_usrreq: unexpected control mbuf");
                   1218: #endif
1.170     ad       1219:        if (req == PRU_ATTACH) {
                   1220:                sosetlock(so);
                   1221:        } else if (inp == 0) {
1.31      mycroft  1222:                error = EINVAL;
                   1223:                goto release;
                   1224:        }
                   1225:
1.1       cgd      1226:        /*
                   1227:         * Note: need to block udp_input while changing
                   1228:         * the udp pcb queue and/or pcb addresses.
                   1229:         */
                   1230:        switch (req) {
                   1231:
                   1232:        case PRU_ATTACH:
1.31      mycroft  1233:                if (inp != 0) {
                   1234:                        error = EISCONN;
1.1       cgd      1235:                        break;
                   1236:                }
1.98      matt     1237: #ifdef MBUFTRACE
                   1238:                so->so_mowner = &udp_mowner;
                   1239:                so->so_rcv.sb_mowner = &udp_rx_mowner;
                   1240:                so->so_snd.sb_mowner = &udp_tx_mowner;
                   1241: #endif
1.31      mycroft  1242:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                   1243:                        error = soreserve(so, udp_sendspace, udp_recvspace);
                   1244:                        if (error)
                   1245:                                break;
                   1246:                }
1.18      mycroft  1247:                error = in_pcballoc(so, &udbtable);
1.1       cgd      1248:                if (error)
                   1249:                        break;
1.31      mycroft  1250:                inp = sotoinpcb(so);
                   1251:                inp->inp_ip.ip_ttl = ip_defttl;
1.1       cgd      1252:                break;
                   1253:
                   1254:        case PRU_DETACH:
1.31      mycroft  1255:                in_pcbdetach(inp);
1.1       cgd      1256:                break;
                   1257:
                   1258:        case PRU_BIND:
1.148     ad       1259:                error = in_pcbbind(inp, nam, l);
1.1       cgd      1260:                break;
                   1261:
                   1262:        case PRU_LISTEN:
                   1263:                error = EOPNOTSUPP;
                   1264:                break;
                   1265:
                   1266:        case PRU_CONNECT:
1.148     ad       1267:                error = in_pcbconnect(inp, nam, l);
1.31      mycroft  1268:                if (error)
1.1       cgd      1269:                        break;
1.31      mycroft  1270:                soisconnected(so);
1.1       cgd      1271:                break;
                   1272:
                   1273:        case PRU_CONNECT2:
                   1274:                error = EOPNOTSUPP;
                   1275:                break;
                   1276:
                   1277:        case PRU_DISCONNECT:
1.31      mycroft  1278:                /*soisdisconnected(so);*/
                   1279:                so->so_state &= ~SS_ISCONNECTED;        /* XXX */
1.1       cgd      1280:                in_pcbdisconnect(inp);
1.34      mycroft  1281:                inp->inp_laddr = zeroin_addr;           /* XXX */
1.35      mycroft  1282:                in_pcbstate(inp, INP_BOUND);            /* XXX */
1.1       cgd      1283:                break;
                   1284:
                   1285:        case PRU_SHUTDOWN:
                   1286:                socantsendmore(so);
                   1287:                break;
                   1288:
1.31      mycroft  1289:        case PRU_RCVD:
                   1290:                error = EOPNOTSUPP;
1.1       cgd      1291:                break;
                   1292:
1.31      mycroft  1293:        case PRU_SEND:
1.32      mycroft  1294:                if (control && control->m_len) {
                   1295:                        m_freem(control);
                   1296:                        m_freem(m);
                   1297:                        error = EINVAL;
                   1298:                        break;
                   1299:                }
1.31      mycroft  1300:        {
1.35      mycroft  1301:                struct in_addr laddr;                   /* XXX */
1.1       cgd      1302:
1.31      mycroft  1303:                if (nam) {
1.35      mycroft  1304:                        laddr = inp->inp_laddr;         /* XXX */
1.31      mycroft  1305:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
                   1306:                                error = EISCONN;
1.32      mycroft  1307:                                goto die;
1.31      mycroft  1308:                        }
1.148     ad       1309:                        error = in_pcbconnect(inp, nam, l);
1.100     matt     1310:                        if (error)
                   1311:                                goto die;
1.31      mycroft  1312:                } else {
                   1313:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
                   1314:                                error = ENOTCONN;
1.32      mycroft  1315:                                goto die;
1.31      mycroft  1316:                        }
                   1317:                }
1.33      mycroft  1318:                error = udp_output(m, inp);
1.100     matt     1319:                m = NULL;
1.31      mycroft  1320:                if (nam) {
                   1321:                        in_pcbdisconnect(inp);
1.35      mycroft  1322:                        inp->inp_laddr = laddr;         /* XXX */
                   1323:                        in_pcbstate(inp, INP_BOUND);    /* XXX */
1.31      mycroft  1324:                }
1.100     matt     1325:          die:
                   1326:                if (m)
                   1327:                        m_freem(m);
1.31      mycroft  1328:        }
1.1       cgd      1329:                break;
                   1330:
                   1331:        case PRU_SENSE:
                   1332:                /*
                   1333:                 * stat: don't bother with a blocksize.
                   1334:                 */
1.31      mycroft  1335:                splx(s);
1.1       cgd      1336:                return (0);
                   1337:
1.31      mycroft  1338:        case PRU_RCVOOB:
                   1339:                error =  EOPNOTSUPP;
                   1340:                break;
                   1341:
1.1       cgd      1342:        case PRU_SENDOOB:
1.32      mycroft  1343:                m_freem(control);
1.31      mycroft  1344:                m_freem(m);
1.1       cgd      1345:                error =  EOPNOTSUPP;
                   1346:                break;
                   1347:
1.31      mycroft  1348:        case PRU_SOCKADDR:
                   1349:                in_setsockaddr(inp, nam);
                   1350:                break;
                   1351:
                   1352:        case PRU_PEERADDR:
                   1353:                in_setpeeraddr(inp, nam);
                   1354:                break;
1.1       cgd      1355:
                   1356:        default:
                   1357:                panic("udp_usrreq");
                   1358:        }
                   1359:
                   1360: release:
1.31      mycroft  1361:        splx(s);
1.1       cgd      1362:        return (error);
1.13      mycroft  1363: }
                   1364:
1.166     thorpej  1365: static int
                   1366: sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
                   1367: {
                   1368:
1.172     thorpej  1369:        return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1.166     thorpej  1370: }
                   1371:
1.13      mycroft  1372: /*
                   1373:  * Sysctl for udp variables.
                   1374:  */
1.179     pooka    1375: static void
                   1376: sysctl_net_inet_udp_setup(struct sysctllog **clog)
1.13      mycroft  1377: {
1.114     atatat   1378:
1.116     atatat   1379:        sysctl_createv(clog, 0, NULL, NULL,
                   1380:                       CTLFLAG_PERMANENT,
1.114     atatat   1381:                       CTLTYPE_NODE, "net", NULL,
                   1382:                       NULL, 0, NULL, 0,
                   1383:                       CTL_NET, CTL_EOL);
1.116     atatat   1384:        sysctl_createv(clog, 0, NULL, NULL,
                   1385:                       CTLFLAG_PERMANENT,
1.114     atatat   1386:                       CTLTYPE_NODE, "inet", NULL,
                   1387:                       NULL, 0, NULL, 0,
                   1388:                       CTL_NET, PF_INET, CTL_EOL);
1.116     atatat   1389:        sysctl_createv(clog, 0, NULL, NULL,
                   1390:                       CTLFLAG_PERMANENT,
1.122     atatat   1391:                       CTLTYPE_NODE, "udp",
                   1392:                       SYSCTL_DESCR("UDPv4 related settings"),
1.114     atatat   1393:                       NULL, 0, NULL, 0,
                   1394:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
                   1395:
1.116     atatat   1396:        sysctl_createv(clog, 0, NULL, NULL,
                   1397:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1398:                       CTLTYPE_INT, "checksum",
1.123     heas     1399:                       SYSCTL_DESCR("Compute UDP checksums"),
1.114     atatat   1400:                       NULL, 0, &udpcksum, 0,
                   1401:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
                   1402:                       CTL_EOL);
1.116     atatat   1403:        sysctl_createv(clog, 0, NULL, NULL,
                   1404:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1405:                       CTLTYPE_INT, "sendspace",
                   1406:                       SYSCTL_DESCR("Default UDP send buffer size"),
1.114     atatat   1407:                       NULL, 0, &udp_sendspace, 0,
                   1408:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
                   1409:                       CTL_EOL);
1.116     atatat   1410:        sysctl_createv(clog, 0, NULL, NULL,
                   1411:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1412:                       CTLTYPE_INT, "recvspace",
                   1413:                       SYSCTL_DESCR("Default UDP receive buffer size"),
1.114     atatat   1414:                       NULL, 0, &udp_recvspace, 0,
                   1415:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
                   1416:                       CTL_EOL);
1.125     thorpej  1417:        sysctl_createv(clog, 0, NULL, NULL,
                   1418:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1419:                       CTLTYPE_INT, "do_loopback_cksum",
                   1420:                       SYSCTL_DESCR("Perform UDP checksum on loopback"),
                   1421:                       NULL, 0, &udp_do_loopback_cksum, 0,
                   1422:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
                   1423:                       CTL_EOL);
1.132     atatat   1424:        sysctl_createv(clog, 0, NULL, NULL,
                   1425:                       CTLFLAG_PERMANENT,
1.134     atatat   1426:                       CTLTYPE_STRUCT, "pcblist",
1.132     atatat   1427:                       SYSCTL_DESCR("UDP protocol control block list"),
                   1428:                       sysctl_inpcblist, 0, &udbtable, 0,
                   1429:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
                   1430:                       CTL_EOL);
1.139     elad     1431:        sysctl_createv(clog, 0, NULL, NULL,
                   1432:                       CTLFLAG_PERMANENT,
                   1433:                       CTLTYPE_STRUCT, "stats",
                   1434:                       SYSCTL_DESCR("UDP statistics"),
1.166     thorpej  1435:                       sysctl_net_inet_udp_stats, 0, NULL, 0,
1.139     elad     1436:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
                   1437:                       CTL_EOL);
1.1       cgd      1438: }
1.72      itojun   1439: #endif
1.130     manu     1440:
1.166     thorpej  1441: void
                   1442: udp_statinc(u_int stat)
                   1443: {
                   1444:
                   1445:        KASSERT(stat < UDP_NSTATS);
                   1446:        UDP_STATINC(stat);
                   1447: }
                   1448:
1.130     manu     1449: #if (defined INET && defined IPSEC_NAT_T)
                   1450: /*
                   1451:  * Returns:
                   1452:  * 1 if the packet was processed
                   1453:  * 0 if normal UDP processing should take place
1.144     manu     1454:  * -1 if an error occurent and m was freed
1.130     manu     1455:  */
                   1456: static int
1.154     yamt     1457: udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1.151     christos 1458:     struct socket *so)
1.130     manu     1459: {
                   1460:        size_t len;
1.158     christos 1461:        void *data;
1.130     manu     1462:        struct inpcb *inp;
                   1463:        size_t skip = 0;
                   1464:        size_t minlen;
                   1465:        size_t iphdrlen;
                   1466:        struct ip *ip;
                   1467:        struct mbuf *n;
1.136     manu     1468:        struct m_tag *tag;
                   1469:        struct udphdr *udphdr;
                   1470:        u_int16_t sport, dport;
1.144     manu     1471:        struct mbuf *m = *mp;
1.130     manu     1472:
1.131     perry    1473:        /*
1.130     manu     1474:         * Collapse the mbuf chain if the first mbuf is too short
                   1475:         * The longest case is: UDP + non ESP marker + ESP
                   1476:         */
                   1477:        minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
                   1478:        if (minlen > m->m_pkthdr.len)
                   1479:                minlen = m->m_pkthdr.len;
                   1480:
                   1481:        if (m->m_len < minlen) {
1.144     manu     1482:                if ((*mp = m_pullup(m, minlen)) == NULL) {
1.130     manu     1483:                        printf("udp4_espinudp: m_pullup failed\n");
1.144     manu     1484:                        return -1;
1.130     manu     1485:                }
1.144     manu     1486:                m = *mp;
1.130     manu     1487:        }
                   1488:
1.131     perry    1489:        len = m->m_len - off;
1.158     christos 1490:        data = mtod(m, char *) + off;
1.130     manu     1491:        inp = sotoinpcb(so);
                   1492:
                   1493:        /* Ignore keepalive packets */
1.158     christos 1494:        if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1.130     manu     1495:                return 1;
                   1496:        }
                   1497:
1.131     perry    1498:        /*
                   1499:         * Check that the payload is long enough to hold
1.130     manu     1500:         * an ESP header and compute the length of encapsulation
1.131     perry    1501:         * header to remove
1.130     manu     1502:         */
                   1503:        if (inp->inp_flags & INP_ESPINUDP) {
                   1504:                u_int32_t *st = (u_int32_t *)data;
                   1505:
                   1506:                if ((len <= sizeof(struct esp)) || (*st == 0))
                   1507:                        return 0; /* Normal UDP processing */
                   1508:
                   1509:                skip = sizeof(struct udphdr);
                   1510:        }
                   1511:
                   1512:        if (inp->inp_flags & INP_ESPINUDP_NON_IKE) {
1.142     kleink   1513:                u_int32_t *st = (u_int32_t *)data;
1.130     manu     1514:
                   1515:                if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1.142     kleink   1516:                    || ((st[0] | st[1]) != 0))
1.130     manu     1517:                        return 0; /* Normal UDP processing */
1.131     perry    1518:
1.130     manu     1519:                skip = sizeof(struct udphdr) + sizeof(u_int64_t);
                   1520:        }
                   1521:
                   1522:        /*
1.136     manu     1523:         * Get the UDP ports. They are handled in network
                   1524:         * order everywhere in IPSEC_NAT_T code.
                   1525:         */
1.158     christos 1526:        udphdr = (struct udphdr *)((char *)data - skip);
1.136     manu     1527:        sport = udphdr->uh_sport;
                   1528:        dport = udphdr->uh_dport;
                   1529:
                   1530:        /*
1.130     manu     1531:         * Remove the UDP header (and possibly the non ESP marker)
                   1532:         * IP header lendth is iphdrlen
1.131     perry    1533:         * Before:
1.130     manu     1534:         *   <--- off --->
                   1535:         *   +----+------+-----+
                   1536:         *   | IP |  UDP | ESP |
                   1537:         *   +----+------+-----+
                   1538:         *        <-skip->
                   1539:         * After:
                   1540:         *          +----+-----+
                   1541:         *          | IP | ESP |
                   1542:         *          +----+-----+
                   1543:         *   <-skip->
                   1544:         */
                   1545:        iphdrlen = off - sizeof(struct udphdr);
1.158     christos 1546:        memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1.130     manu     1547:        m_adj(m, skip);
                   1548:
                   1549:        ip = mtod(m, struct ip *);
                   1550:        ip->ip_len = htons(ntohs(ip->ip_len) - skip);
                   1551:        ip->ip_p = IPPROTO_ESP;
                   1552:
                   1553:        /*
1.131     perry    1554:         * Copy the mbuf to avoid multiple free, as both
                   1555:         * esp4_input (which we call) and udp_input (which
1.130     manu     1556:         * called us) free the mbuf.
                   1557:         */
                   1558:        if ((n = m_dup(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
                   1559:                printf("udp4_espinudp: m_dup failed\n");
                   1560:                return 0;
                   1561:        }
                   1562:
1.136     manu     1563:        /*
                   1564:         * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
                   1565:         * the source UDP port. This is required if we want
                   1566:         * to select the right SPD for multiple hosts behind
                   1567:         * same NAT
                   1568:         */
1.137     manu     1569:        if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
                   1570:            sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
                   1571:                printf("udp4_espinudp: m_tag_get failed\n");
1.138     manu     1572:                m_freem(n);
1.137     manu     1573:                return 0;
                   1574:        }
1.136     manu     1575:        ((u_int16_t *)(tag + 1))[0] = sport;
                   1576:        ((u_int16_t *)(tag + 1))[1] = dport;
                   1577:        m_tag_prepend(n, tag);
                   1578:
1.147     christos 1579: #ifdef FAST_IPSEC
1.160     degroote 1580:        ipsec4_common_input(n, iphdrlen, IPPROTO_ESP);
1.147     christos 1581: #else
1.130     manu     1582:        esp4_input(n, iphdrlen);
1.147     christos 1583: #endif
1.130     manu     1584:
                   1585:        /* We handled it, it shoudln't be handled by UDP */
                   1586:        return 1;
                   1587: }
                   1588: #endif

CVSweb <webmaster@jp.NetBSD.org>