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

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

CVSweb <webmaster@jp.NetBSD.org>