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

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

CVSweb <webmaster@jp.NetBSD.org>