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

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

CVSweb <webmaster@jp.NetBSD.org>