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

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

CVSweb <webmaster@jp.NetBSD.org>