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

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

CVSweb <webmaster@jp.NetBSD.org>