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

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

CVSweb <webmaster@jp.NetBSD.org>