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

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

CVSweb <webmaster@jp.NetBSD.org>