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

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

CVSweb <webmaster@jp.NetBSD.org>