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

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

CVSweb <webmaster@jp.NetBSD.org>