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

1.234   ! christos    1: /*     $NetBSD: udp_usrreq.c,v 1.233 2017/04/20 08:46:07 ozaki-r 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:
1.198     rmind      63: /*
                     64:  * UDP protocol implementation.
                     65:  * Per RFC 768, August, 1980.
                     66:  */
                     67:
1.91      lukem      68: #include <sys/cdefs.h>
1.234   ! christos   69: __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.233 2017/04/20 08:46:07 ozaki-r Exp $");
1.50      thorpej    70:
1.222     pooka      71: #ifdef _KERNEL_OPT
1.77      soda       72: #include "opt_inet.h"
1.50      thorpej    73: #include "opt_ipsec.h"
1.78      thorpej    74: #include "opt_inet_csum.h"
1.64      ws         75: #include "opt_ipkdb.h"
1.101     martin     76: #include "opt_mbuftrace.h"
1.229     knakahar   77: #include "opt_net_mpsafe.h"
1.222     pooka      78: #endif
1.1       cgd        79:
1.5       mycroft    80: #include <sys/param.h>
                     81: #include <sys/mbuf.h>
1.192     pooka      82: #include <sys/once.h>
1.5       mycroft    83: #include <sys/protosw.h>
                     84: #include <sys/socket.h>
                     85: #include <sys/socketvar.h>
1.27      christos   86: #include <sys/systm.h>
                     87: #include <sys/proc.h>
1.53      itojun     88: #include <sys/domain.h>
1.27      christos   89: #include <sys/sysctl.h>
1.1       cgd        90:
1.5       mycroft    91: #include <net/if.h>
1.1       cgd        92:
1.5       mycroft    93: #include <netinet/in.h>
                     94: #include <netinet/in_systm.h>
1.15      cgd        95: #include <netinet/in_var.h>
1.5       mycroft    96: #include <netinet/ip.h>
                     97: #include <netinet/in_pcb.h>
                     98: #include <netinet/ip_var.h>
                     99: #include <netinet/ip_icmp.h>
                    100: #include <netinet/udp.h>
                    101: #include <netinet/udp_var.h>
1.166     thorpej   102: #include <netinet/udp_private.h>
1.1       cgd       103:
1.53      itojun    104: #ifdef INET6
                    105: #include <netinet/ip6.h>
                    106: #include <netinet/icmp6.h>
                    107: #include <netinet6/ip6_var.h>
1.167     thorpej   108: #include <netinet6/ip6_private.h>
1.53      itojun    109: #include <netinet6/in6_pcb.h>
                    110: #include <netinet6/udp6_var.h>
1.168     thorpej   111: #include <netinet6/udp6_private.h>
1.53      itojun    112: #endif
                    113:
                    114: #ifndef INET6
                    115: /* always need ip6.h for IP6_EXTHDR_GET */
                    116: #include <netinet/ip6.h>
                    117: #endif
                    118:
1.190     christos  119: #ifdef IPSEC
1.105     jonathan  120: #include <netipsec/ipsec.h>
1.169     thorpej   121: #include <netipsec/ipsec_var.h>
                    122: #include <netipsec/ipsec_private.h>
1.147     christos  123: #include <netipsec/esp.h>
1.105     jonathan  124: #ifdef INET6
                    125: #include <netipsec/ipsec6.h>
                    126: #endif
1.190     christos  127: #endif /* IPSEC */
1.105     jonathan  128:
1.64      ws        129: #ifdef IPKDB
                    130: #include <ipkdb/ipkdb.h>
                    131: #endif
                    132:
1.8       mycroft   133: int    udpcksum = 1;
1.141     yamt      134: int    udp_do_loopback_cksum = 0;
1.93      matt      135:
                    136: struct inpcbtable udbtable;
1.164     thorpej   137:
1.166     thorpej   138: percpu_t *udpstat_percpu;
1.8       mycroft   139:
1.72      itojun    140: #ifdef INET
1.190     christos  141: #ifdef IPSEC
1.144     manu      142: static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
1.130     manu      143:        struct socket *);
1.189     christos  144: #endif
1.119     matt      145: static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
                    146:        struct socket *);
                    147: static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
1.144     manu      148:        struct mbuf **, int);
1.129     yamt      149: static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.72      itojun    150: #endif
                    151: #ifdef INET
1.119     matt      152: static void udp_notify (struct inpcb *, int);
1.72      itojun    153: #endif
1.7       mycroft   154:
1.26      mycroft   155: #ifndef UDBHASHSIZE
                    156: #define        UDBHASHSIZE     128
                    157: #endif
                    158: int    udbhashsize = UDBHASHSIZE;
                    159:
1.196     rmind     160: /*
                    161:  * For send - really max datagram size; for receive - 40 1K datagrams.
                    162:  */
                    163: static int     udp_sendspace = 9216;
                    164: static int     udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
                    165:
1.98      matt      166: #ifdef MBUFTRACE
1.150     dogcow    167: struct mowner udp_mowner = MOWNER_INIT("udp", "");
                    168: struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
                    169: struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
1.98      matt      170: #endif
                    171:
1.78      thorpej   172: #ifdef UDP_CSUM_COUNTERS
                    173: #include <sys/device.h>
                    174:
1.140     yamt      175: #if defined(INET)
1.78      thorpej   176: struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    177:     NULL, "udp", "hwcsum bad");
                    178: struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    179:     NULL, "udp", "hwcsum ok");
                    180: struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    181:     NULL, "udp", "hwcsum data");
                    182: struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    183:     NULL, "udp", "swcsum");
                    184:
1.120     matt      185: EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
                    186: EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
                    187: EVCNT_ATTACH_STATIC(udp_hwcsum_data);
                    188: EVCNT_ATTACH_STATIC(udp_swcsum);
1.140     yamt      189: #endif /* defined(INET) */
                    190:
                    191: #define        UDP_CSUM_COUNTER_INCR(ev)       (ev)->ev_count++
1.78      thorpej   192: #else
                    193: #define        UDP_CSUM_COUNTER_INCR(ev)       /* nothing */
                    194: #endif /* UDP_CSUM_COUNTERS */
                    195:
1.179     pooka     196: static void sysctl_net_inet_udp_setup(struct sysctllog **);
                    197:
1.192     pooka     198: static int
                    199: do_udpinit(void)
1.1       cgd       200: {
1.18      mycroft   201:
1.35      mycroft   202:        in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.193     pooka     203:        udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
1.78      thorpej   204:
1.98      matt      205:        MOWNER_ATTACH(&udp_tx_mowner);
                    206:        MOWNER_ATTACH(&udp_rx_mowner);
                    207:        MOWNER_ATTACH(&udp_mowner);
1.166     thorpej   208:
1.192     pooka     209:        return 0;
                    210: }
                    211:
                    212: void
                    213: udp_init_common(void)
                    214: {
                    215:        static ONCE_DECL(doudpinit);
                    216:
                    217:        RUN_ONCE(&doudpinit, do_udpinit);
                    218: }
                    219:
                    220: void
                    221: udp_init(void)
                    222: {
                    223:
                    224:        sysctl_net_inet_udp_setup(NULL);
                    225:
                    226:        udp_init_common();
1.1       cgd       227: }
                    228:
1.129     yamt      229: /*
                    230:  * Checksum extended UDP header and data.
                    231:  */
                    232:
                    233: int
                    234: udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
                    235:     int iphlen, int len)
                    236: {
                    237:
                    238:        switch (af) {
1.72      itojun    239: #ifdef INET
1.129     yamt      240:        case AF_INET:
                    241:                return udp4_input_checksum(m, uh, iphlen, len);
                    242: #endif
                    243: #ifdef INET6
                    244:        case AF_INET6:
                    245:                return udp6_input_checksum(m, uh, iphlen, len);
                    246: #endif
                    247:        }
                    248: #ifdef DIAGNOSTIC
                    249:        panic("udp_input_checksum: unknown af %d", af);
                    250: #endif
                    251:        /* NOTREACHED */
                    252:        return -1;
                    253: }
                    254:
                    255: #ifdef INET
                    256:
                    257: /*
                    258:  * Checksum extended UDP header and data.
                    259:  */
                    260:
                    261: static int
                    262: udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
                    263:     int iphlen, int len)
                    264: {
                    265:
                    266:        /*
                    267:         * XXX it's better to record and check if this mbuf is
                    268:         * already checked.
                    269:         */
                    270:
                    271:        if (uh->uh_sum == 0)
                    272:                return 0;
                    273:
                    274:        switch (m->m_pkthdr.csum_flags &
1.226     ozaki-r   275:            ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_UDPv4) |
1.129     yamt      276:            M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
                    277:        case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
                    278:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
                    279:                goto badcsum;
                    280:
                    281:        case M_CSUM_UDPv4|M_CSUM_DATA: {
                    282:                u_int32_t hw_csum = m->m_pkthdr.csum_data;
                    283:
                    284:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
                    285:                if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
                    286:                        const struct ip *ip =
                    287:                            mtod(m, const struct ip *);
                    288:
                    289:                        hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
                    290:                            ip->ip_dst.s_addr,
                    291:                            htons(hw_csum + len + IPPROTO_UDP));
                    292:                }
                    293:                if ((hw_csum ^ 0xffff) != 0)
                    294:                        goto badcsum;
                    295:                break;
                    296:        }
                    297:
                    298:        case M_CSUM_UDPv4:
                    299:                /* Checksum was okay. */
                    300:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
                    301:                break;
                    302:
                    303:        default:
                    304:                /*
                    305:                 * Need to compute it ourselves.  Maybe skip checksum
                    306:                 * on loopback interfaces.
                    307:                 */
1.226     ozaki-r   308:                if (__predict_true(!(m_get_rcvif_NOMPSAFE(m)->if_flags &
1.129     yamt      309:                                     IFF_LOOPBACK) ||
                    310:                                   udp_do_loopback_cksum)) {
                    311:                        UDP_CSUM_COUNTER_INCR(&udp_swcsum);
                    312:                        if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
                    313:                                goto badcsum;
                    314:                }
                    315:                break;
                    316:        }
                    317:
                    318:        return 0;
                    319:
                    320: badcsum:
1.166     thorpej   321:        UDP_STATINC(UDP_STAT_BADSUM);
1.129     yamt      322:        return -1;
                    323: }
                    324:
1.7       mycroft   325: void
1.27      christos  326: udp_input(struct mbuf *m, ...)
1.1       cgd       327: {
1.53      itojun    328:        va_list ap;
                    329:        struct sockaddr_in src, dst;
                    330:        struct ip *ip;
                    331:        struct udphdr *uh;
1.97      simonb    332:        int iphlen;
1.53      itojun    333:        int len;
                    334:        int n;
1.96      itojun    335:        u_int16_t ip_len;
1.53      itojun    336:
                    337:        va_start(ap, m);
                    338:        iphlen = va_arg(ap, int);
1.97      simonb    339:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.53      itojun    340:        va_end(ap);
                    341:
1.98      matt      342:        MCLAIM(m, &udp_rx_mowner);
1.166     thorpej   343:        UDP_STATINC(UDP_STAT_IPACKETS);
1.53      itojun    344:
                    345:        /*
                    346:         * Get IP and UDP header together in first mbuf.
                    347:         */
                    348:        ip = mtod(m, struct ip *);
                    349:        IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
                    350:        if (uh == NULL) {
1.166     thorpej   351:                UDP_STATINC(UDP_STAT_HDROPS);
1.53      itojun    352:                return;
                    353:        }
1.228     mlelstv   354:        /*
                    355:         * Enforce alignment requirements that are violated in
                    356:         * some cases, see kern/50766 for details.
                    357:         */
                    358:        if (UDP_HDR_ALIGNED_P(uh) == 0) {
                    359:                m = m_copyup(m, iphlen + sizeof(struct udphdr), 0);
                    360:                if (m == NULL) {
                    361:                        UDP_STATINC(UDP_STAT_HDROPS);
                    362:                        return;
                    363:                }
                    364:                ip = mtod(m, struct ip *);
                    365:                uh = (struct udphdr *)(mtod(m, char *) + iphlen);
                    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.166     thorpej   381:                        UDP_STATINC(UDP_STAT_BADLEN);
1.53      itojun    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. */
1.159     dyoung    394:        sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
                    395:        sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
1.53      itojun    396:
1.144     manu      397:        if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
1.166     thorpej   398:                UDP_STATINC(UDP_STAT_HDROPS);
1.144     manu      399:                return;
                    400:        }
1.188     christos  401:        if (m == NULL) {
                    402:                /*
                    403:                 * packet has been processed by ESP stuff -
                    404:                 * e.g. dropped NAT-T-keep-alive-packet ...
                    405:                 */
                    406:                return;
                    407:        }
                    408:        ip = mtod(m, struct ip *);
1.53      itojun    409: #ifdef INET6
                    410:        if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
                    411:                struct sockaddr_in6 src6, dst6;
                    412:
1.175     cegger    413:                memset(&src6, 0, sizeof(src6));
1.53      itojun    414:                src6.sin6_family = AF_INET6;
                    415:                src6.sin6_len = sizeof(struct sockaddr_in6);
1.224     rtr       416:                in6_in_2_v4mapin6(&ip->ip_src, &src6.sin6_addr);
1.53      itojun    417:                src6.sin6_port = uh->uh_sport;
1.175     cegger    418:                memset(&dst6, 0, sizeof(dst6));
1.53      itojun    419:                dst6.sin6_family = AF_INET6;
                    420:                dst6.sin6_len = sizeof(struct sockaddr_in6);
1.224     rtr       421:                in6_in_2_v4mapin6(&ip->ip_dst, &dst6.sin6_addr);
1.53      itojun    422:                dst6.sin6_port = uh->uh_dport;
                    423:
                    424:                n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
                    425:        }
                    426: #endif
                    427:
                    428:        if (n == 0) {
                    429:                if (m->m_flags & (M_BCAST | M_MCAST)) {
1.166     thorpej   430:                        UDP_STATINC(UDP_STAT_NOPORTBCAST);
1.53      itojun    431:                        goto bad;
                    432:                }
1.166     thorpej   433:                UDP_STATINC(UDP_STAT_NOPORT);
1.64      ws        434: #ifdef IPKDB
1.53      itojun    435:                if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
                    436:                                m, iphlen + sizeof(struct udphdr),
                    437:                                m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
                    438:                        /*
                    439:                         * It was a debugger connect packet,
                    440:                         * just drop it now
                    441:                         */
                    442:                        goto bad;
                    443:                }
                    444: #endif
                    445:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
                    446:                m = NULL;
                    447:        }
                    448:
                    449: bad:
                    450:        if (m)
                    451:                m_freem(m);
1.78      thorpej   452:        return;
                    453:
                    454: badcsum:
                    455:        m_freem(m);
1.53      itojun    456: }
1.72      itojun    457: #endif
1.53      itojun    458:
1.72      itojun    459: #ifdef INET
1.53      itojun    460: static void
1.119     matt      461: udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
                    462:        struct sockaddr *src, struct socket *so)
1.53      itojun    463: {
                    464:        struct mbuf *opts = NULL;
                    465:        struct mbuf *n;
1.232     ozaki-r   466:        struct inpcb *inp;
1.53      itojun    467:
1.233     ozaki-r   468:        KASSERT(so != NULL);
1.232     ozaki-r   469:        KASSERT(so->so_proto->pr_domain->dom_family == AF_INET);
                    470:        inp = sotoinpcb(so);
                    471:        KASSERT(inp != NULL);
1.53      itojun    472:
1.190     christos  473: #if defined(IPSEC)
1.53      itojun    474:        /* check AH/ESP integrity. */
1.233     ozaki-r   475:        if (ipsec_used && ipsec4_in_reject(m, inp)) {
1.169     thorpej   476:                IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.162     dyoung    477:                if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
1.110     itojun    478:                        icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
                    479:                            0, 0);
1.53      itojun    480:                return;
                    481:        }
                    482: #endif /*IPSEC*/
                    483:
1.162     dyoung    484:        if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.232     ozaki-r   485:                if (inp->inp_flags & INP_CONTROLOPTS
1.234   ! christos  486:                    || SOOPT_TIMESTAMP(so->so_options)) {
1.53      itojun    487:                        struct ip *ip = mtod(n, struct ip *);
                    488:                        ip_savecontrol(inp, &opts, ip, n);
                    489:                }
                    490:
                    491:                m_adj(n, off);
                    492:                if (sbappendaddr(&so->so_rcv, src, n,
                    493:                                opts) == 0) {
                    494:                        m_freem(n);
                    495:                        if (opts)
                    496:                                m_freem(opts);
1.124     darrenr   497:                        so->so_rcv.sb_overflowed++;
1.166     thorpej   498:                        UDP_STATINC(UDP_STAT_FULLSOCK);
1.53      itojun    499:                } else
                    500:                        sorwakeup(so);
                    501:        }
                    502: }
1.72      itojun    503: #endif
1.53      itojun    504:
1.72      itojun    505: #ifdef INET
1.53      itojun    506: static int
1.119     matt      507: udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
1.144     manu      508:        struct mbuf **mp, int off /* offset of udphdr */)
1.53      itojun    509: {
                    510:        u_int16_t *sport, *dport;
                    511:        int rcvcnt;
                    512:        struct in_addr *src4, *dst4;
1.109     itojun    513:        struct inpcb_hdr *inph;
1.53      itojun    514:        struct inpcb *inp;
1.144     manu      515:        struct mbuf *m = *mp;
1.53      itojun    516:
                    517:        rcvcnt = 0;
                    518:        off += sizeof(struct udphdr);   /* now, offset of payload */
                    519:
                    520:        if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
                    521:                goto bad;
                    522:
                    523:        src4 = &src->sin_addr;
                    524:        sport = &src->sin_port;
                    525:        dst4 = &dst->sin_addr;
                    526:        dport = &dst->sin_port;
                    527:
1.73      itojun    528:        if (IN_MULTICAST(dst4->s_addr) ||
1.226     ozaki-r   529:            in_broadcast(*dst4, m_get_rcvif_NOMPSAFE(m))) {
1.53      itojun    530:                /*
                    531:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    532:                 * for which the local and remote addresses and ports match
                    533:                 * those of the incoming datagram.  This allows more than
                    534:                 * one process to receive multi/broadcasts on the same port.
                    535:                 * (This really ought to be done for unicast datagrams as
                    536:                 * well, but that would cause problems with existing
                    537:                 * applications that open both address-specific sockets and
                    538:                 * a wildcard socket listening to the same port -- they would
                    539:                 * end up receiving duplicates of every unicast datagram.
                    540:                 * Those applications open the multiple sockets to overcome an
                    541:                 * inadequacy of the UDP socket interface, but for backwards
                    542:                 * compatibility we avoid the problem here rather than
                    543:                 * fixing the interface.  Maybe 4.5BSD will remedy this?)
                    544:                 */
                    545:
                    546:                /*
1.92      itojun    547:                 * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71      itojun    548:                 * we need udpiphdr for IPsec processing so we do that later.
1.53      itojun    549:                 */
                    550:                /*
                    551:                 * Locate pcb(s) for datagram.
                    552:                 */
1.191     christos  553:                TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
1.109     itojun    554:                        inp = (struct inpcb *)inph;
                    555:                        if (inp->inp_af != AF_INET)
                    556:                                continue;
                    557:
1.53      itojun    558:                        if (inp->inp_lport != *dport)
                    559:                                continue;
                    560:                        if (!in_nullhost(inp->inp_laddr)) {
                    561:                                if (!in_hosteq(inp->inp_laddr, *dst4))
                    562:                                        continue;
                    563:                        }
                    564:                        if (!in_nullhost(inp->inp_faddr)) {
                    565:                                if (!in_hosteq(inp->inp_faddr, *src4) ||
                    566:                                    inp->inp_fport != *sport)
                    567:                                        continue;
                    568:                        }
                    569:
                    570:                        udp4_sendup(m, off, (struct sockaddr *)src,
                    571:                                inp->inp_socket);
                    572:                        rcvcnt++;
                    573:
                    574:                        /*
                    575:                         * Don't look for additional matches if this one does
                    576:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    577:                         * socket options set.  This heuristic avoids searching
                    578:                         * through all pcbs in the common case of a non-shared
                    579:                         * port.  It assumes that an application will never
                    580:                         * clear these options after setting them.
                    581:                         */
                    582:                        if ((inp->inp_socket->so_options &
                    583:                            (SO_REUSEPORT|SO_REUSEADDR)) == 0)
                    584:                                break;
                    585:                }
                    586:        } else {
                    587:                /*
                    588:                 * Locate pcb for datagram.
                    589:                 */
1.180     dyoung    590:                inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4,
                    591:                    *dport, 0);
1.53      itojun    592:                if (inp == 0) {
1.166     thorpej   593:                        UDP_STATINC(UDP_STAT_PCBHASHMISS);
1.53      itojun    594:                        inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
1.82      itojun    595:                        if (inp == 0)
1.53      itojun    596:                                return rcvcnt;
                    597:                }
                    598:
1.190     christos  599: #ifdef IPSEC
1.130     manu      600:                /* Handle ESP over UDP */
                    601:                if (inp->inp_flags & INP_ESPINUDP_ALL) {
                    602:                        struct sockaddr *sa = (struct sockaddr *)src;
                    603:
1.144     manu      604:                        switch(udp4_espinudp(mp, off, sa, inp->inp_socket)) {
                    605:                        case -1:        /* Error, m was freeed */
                    606:                                rcvcnt = -1;
                    607:                                goto bad;
                    608:                                break;
                    609:
                    610:                        case 1:         /* ESP over UDP */
1.130     manu      611:                                rcvcnt++;
                    612:                                goto bad;
1.144     manu      613:                                break;
                    614:
                    615:                        case 0:         /* plain UDP */
                    616:                        default:        /* Unexpected */
                    617:                                /*
                    618:                                 * Normal UDP processing will take place
                    619:                                 * m may have changed.
                    620:                                 */
                    621:                                m = *mp;
                    622:                                break;
1.130     manu      623:                        }
                    624:                }
1.189     christos  625: #endif
1.130     manu      626:
1.178     minskim   627:                /*
                    628:                 * Check the minimum TTL for socket.
                    629:                 */
                    630:                if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl)
                    631:                        goto bad;
                    632:
1.53      itojun    633:                udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
                    634:                rcvcnt++;
                    635:        }
                    636:
                    637: bad:
                    638:        return rcvcnt;
                    639: }
1.72      itojun    640: #endif
1.53      itojun    641:
1.72      itojun    642: #ifdef INET
1.1       cgd       643: /*
                    644:  * Notify a udp user of an asynchronous error;
                    645:  * just wake up so that he can collect error status.
                    646:  */
1.7       mycroft   647: static void
1.119     matt      648: udp_notify(struct inpcb *inp, int errno)
1.1       cgd       649: {
                    650:        inp->inp_socket->so_error = errno;
                    651:        sorwakeup(inp->inp_socket);
                    652:        sowwakeup(inp->inp_socket);
                    653: }
                    654:
1.27      christos  655: void *
1.157     dyoung    656: udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.1       cgd       657: {
1.66      augustss  658:        struct ip *ip = v;
                    659:        struct udphdr *uh;
1.119     matt      660:        void (*notify)(struct inpcb *, int) = udp_notify;
1.21      mycroft   661:        int errno;
1.1       cgd       662:
1.53      itojun    663:        if (sa->sa_family != AF_INET
                    664:         || sa->sa_len != sizeof(struct sockaddr_in))
1.51      itojun    665:                return NULL;
1.20      mycroft   666:        if ((unsigned)cmd >= PRC_NCMDS)
1.27      christos  667:                return NULL;
1.20      mycroft   668:        errno = inetctlerrmap[cmd];
1.18      mycroft   669:        if (PRC_IS_REDIRECT(cmd))
1.19      mycroft   670:                notify = in_rtchange, ip = 0;
1.18      mycroft   671:        else if (cmd == PRC_HOSTDEAD)
1.19      mycroft   672:                ip = 0;
1.23      cgd       673:        else if (errno == 0)
1.27      christos  674:                return NULL;
1.19      mycroft   675:        if (ip) {
1.158     christos  676:                uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
1.157     dyoung    677:                in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
1.34      mycroft   678:                    ip->ip_src, uh->uh_sport, errno, notify);
1.53      itojun    679:
                    680:                /* XXX mapped address case */
1.19      mycroft   681:        } else
1.157     dyoung    682:                in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
1.34      mycroft   683:                    notify);
1.27      christos  684:        return NULL;
1.1       cgd       685: }
                    686:
1.7       mycroft   687: int
1.173     plunky    688: udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.130     manu      689: {
                    690:        int s;
                    691:        int error = 0;
                    692:        struct inpcb *inp;
                    693:        int family;
1.173     plunky    694:        int optval;
1.130     manu      695:
                    696:        family = so->so_proto->pr_domain->dom_family;
                    697:
                    698:        s = splsoftnet();
                    699:        switch (family) {
                    700: #ifdef INET
                    701:        case PF_INET:
1.173     plunky    702:                if (sopt->sopt_level != IPPROTO_UDP) {
                    703:                        error = ip_ctloutput(op, so, sopt);
1.130     manu      704:                        goto end;
                    705:                }
                    706:                break;
                    707: #endif
                    708: #ifdef INET6
                    709:        case PF_INET6:
1.173     plunky    710:                if (sopt->sopt_level != IPPROTO_UDP) {
                    711:                        error = ip6_ctloutput(op, so, sopt);
1.130     manu      712:                        goto end;
                    713:                }
                    714:                break;
                    715: #endif
                    716:        default:
                    717:                error = EAFNOSUPPORT;
                    718:                goto end;
                    719:        }
                    720:
                    721:
                    722:        switch (op) {
                    723:        case PRCO_SETOPT:
                    724:                inp = sotoinpcb(so);
                    725:
1.173     plunky    726:                switch (sopt->sopt_name) {
1.130     manu      727:                case UDP_ENCAP:
1.173     plunky    728:                        error = sockopt_getint(sopt, &optval);
                    729:                        if (error)
1.153     yamt      730:                                break;
1.131     perry     731:
1.173     plunky    732:                        switch(optval) {
1.130     manu      733:                        case 0:
                    734:                                inp->inp_flags &= ~INP_ESPINUDP_ALL;
                    735:                                break;
                    736:
                    737:                        case UDP_ENCAP_ESPINUDP:
                    738:                                inp->inp_flags &= ~INP_ESPINUDP_ALL;
                    739:                                inp->inp_flags |= INP_ESPINUDP;
                    740:                                break;
1.131     perry     741:
1.130     manu      742:                        case UDP_ENCAP_ESPINUDP_NON_IKE:
                    743:                                inp->inp_flags &= ~INP_ESPINUDP_ALL;
                    744:                                inp->inp_flags |= INP_ESPINUDP_NON_IKE;
                    745:                                break;
                    746:                        default:
                    747:                                error = EINVAL;
                    748:                                break;
                    749:                        }
                    750:                        break;
1.183     christos  751:
1.130     manu      752:                default:
                    753:                        error = ENOPROTOOPT;
                    754:                        break;
                    755:                }
                    756:                break;
                    757:
                    758:        default:
                    759:                error = EINVAL;
                    760:                break;
1.131     perry     761:        }
                    762:
1.130     manu      763: end:
                    764:        splx(s);
                    765:        return error;
                    766: }
1.131     perry     767:
1.130     manu      768:
                    769: int
1.223     riastrad  770: udp_output(struct mbuf *m, struct inpcb *inp)
1.27      christos  771: {
1.66      augustss  772:        struct udpiphdr *ui;
1.125     thorpej   773:        struct route *ro;
1.66      augustss  774:        int len = m->m_pkthdr.len;
1.31      mycroft   775:        int error = 0;
1.27      christos  776:
1.98      matt      777:        MCLAIM(m, &udp_tx_mowner);
1.1       cgd       778:
                    779:        /*
                    780:         * Calculate data length and get a mbuf
                    781:         * for UDP and IP headers.
                    782:         */
1.13      mycroft   783:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
                    784:        if (m == 0) {
                    785:                error = ENOBUFS;
1.39      thorpej   786:                goto release;
                    787:        }
                    788:
                    789:        /*
                    790:         * Compute the packet length of the IP header, and
                    791:         * punt if the length looks bogus.
                    792:         */
1.96      itojun    793:        if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39      thorpej   794:                error = EMSGSIZE;
1.13      mycroft   795:                goto release;
                    796:        }
1.1       cgd       797:
                    798:        /*
                    799:         * Fill in mbuf with extended UDP header
                    800:         * and addresses and length put into network format.
                    801:         */
                    802:        ui = mtod(m, struct udpiphdr *);
                    803:        ui->ui_pr = IPPROTO_UDP;
                    804:        ui->ui_src = inp->inp_laddr;
                    805:        ui->ui_dst = inp->inp_faddr;
                    806:        ui->ui_sport = inp->inp_lport;
                    807:        ui->ui_dport = inp->inp_fport;
1.78      thorpej   808:        ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1       cgd       809:
1.125     thorpej   810:        ro = &inp->inp_route;
                    811:
1.1       cgd       812:        /*
1.78      thorpej   813:         * Set up checksum and output datagram.
1.1       cgd       814:         */
                    815:        if (udpcksum) {
1.78      thorpej   816:                /*
                    817:                 * XXX Cache pseudo-header checksum part for
                    818:                 * XXX "connected" UDP sockets.
                    819:                 */
                    820:                ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
                    821:                    ui->ui_dst.s_addr, htons((u_int16_t)len +
                    822:                    sizeof(struct udphdr) + IPPROTO_UDP));
1.135     yamt      823:                m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1.78      thorpej   824:                m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
                    825:        } else
                    826:                ui->ui_sum = 0;
1.96      itojun    827:        ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1.1       cgd       828:        ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
                    829:        ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1.166     thorpej   830:        UDP_STATINC(UDP_STAT_OPACKETS);
1.48      itojun    831:
1.125     thorpej   832:        return (ip_output(m, inp->inp_options, ro,
1.12      mycroft   833:            inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.231     ozaki-r   834:            inp->inp_moptions, inp));
1.1       cgd       835:
                    836: release:
                    837:        m_freem(m);
                    838:        return (error);
                    839: }
                    840:
1.196     rmind     841: static int
                    842: udp_attach(struct socket *so, int proto)
                    843: {
                    844:        struct inpcb *inp;
                    845:        int error;
                    846:
                    847:        KASSERT(sotoinpcb(so) == NULL);
                    848:
                    849:        /* Assign the lock (must happen even if we will error out). */
                    850:        sosetlock(so);
                    851:
                    852: #ifdef MBUFTRACE
                    853:        so->so_mowner = &udp_mowner;
                    854:        so->so_rcv.sb_mowner = &udp_rx_mowner;
                    855:        so->so_snd.sb_mowner = &udp_tx_mowner;
                    856: #endif
                    857:        if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    858:                error = soreserve(so, udp_sendspace, udp_recvspace);
                    859:                if (error) {
                    860:                        return error;
                    861:                }
                    862:        }
                    863:
                    864:        error = in_pcballoc(so, &udbtable);
                    865:        if (error) {
                    866:                return error;
                    867:        }
                    868:        inp = sotoinpcb(so);
                    869:        inp->inp_ip.ip_ttl = ip_defttl;
                    870:        KASSERT(solocked(so));
                    871:
                    872:        return error;
                    873: }
                    874:
                    875: static void
                    876: udp_detach(struct socket *so)
                    877: {
                    878:        struct inpcb *inp;
                    879:
                    880:        KASSERT(solocked(so));
                    881:        inp = sotoinpcb(so);
                    882:        KASSERT(inp != NULL);
                    883:        in_pcbdetach(inp);
                    884: }
1.1       cgd       885:
1.195     rmind     886: static int
1.219     rtr       887: udp_accept(struct socket *so, struct sockaddr *nam)
1.208     rtr       888: {
                    889:        KASSERT(solocked(so));
                    890:
                    891:        panic("udp_accept");
1.210     rtr       892:
1.208     rtr       893:        return EOPNOTSUPP;
                    894: }
                    895:
                    896: static int
1.218     rtr       897: udp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
1.210     rtr       898: {
                    899:        struct inpcb *inp = sotoinpcb(so);
1.218     rtr       900:        struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1.210     rtr       901:        int error = 0;
                    902:        int s;
                    903:
                    904:        KASSERT(solocked(so));
                    905:        KASSERT(inp != NULL);
                    906:        KASSERT(nam != NULL);
                    907:
                    908:        s = splsoftnet();
1.218     rtr       909:        error = in_pcbbind(inp, sin, l);
1.210     rtr       910:        splx(s);
                    911:
                    912:        return error;
                    913: }
                    914:
                    915: static int
1.214     rtr       916: udp_listen(struct socket *so, struct lwp *l)
1.210     rtr       917: {
                    918:        KASSERT(solocked(so));
                    919:
                    920:        return EOPNOTSUPP;
                    921: }
                    922:
1.211     rtr       923: static int
1.221     rtr       924: udp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
1.211     rtr       925: {
                    926:        struct inpcb *inp = sotoinpcb(so);
                    927:        int error = 0;
                    928:        int s;
                    929:
                    930:        KASSERT(solocked(so));
                    931:        KASSERT(inp != NULL);
                    932:        KASSERT(nam != NULL);
                    933:
                    934:        s = splsoftnet();
1.221     rtr       935:        error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l);
1.211     rtr       936:        if (! error)
                    937:                soisconnected(so);
                    938:        splx(s);
                    939:        return error;
                    940: }
1.210     rtr       941:
                    942: static int
1.217     rtr       943: udp_connect2(struct socket *so, struct socket *so2)
                    944: {
                    945:        KASSERT(solocked(so));
                    946:
                    947:        return EOPNOTSUPP;
                    948: }
                    949:
                    950: static int
1.212     rtr       951: udp_disconnect(struct socket *so)
                    952: {
                    953:        struct inpcb *inp = sotoinpcb(so);
                    954:        int s;
                    955:
                    956:        KASSERT(solocked(so));
                    957:        KASSERT(inp != NULL);
                    958:
                    959:        s = splsoftnet();
                    960:        /*soisdisconnected(so);*/
                    961:        so->so_state &= ~SS_ISCONNECTED;        /* XXX */
                    962:        in_pcbdisconnect(inp);
                    963:        inp->inp_laddr = zeroin_addr;           /* XXX */
                    964:        in_pcbstate(inp, INP_BOUND);            /* XXX */
                    965:        splx(s);
                    966:
                    967:        return 0;
                    968: }
                    969:
                    970: static int
                    971: udp_shutdown(struct socket *so)
                    972: {
                    973:        int s;
                    974:
                    975:        KASSERT(solocked(so));
                    976:
                    977:        s = splsoftnet();
                    978:        socantsendmore(so);
                    979:        splx(s);
                    980:
                    981:        return 0;
                    982: }
                    983:
                    984: static int
                    985: udp_abort(struct socket *so)
                    986: {
                    987:        KASSERT(solocked(so));
                    988:
                    989:        panic("udp_abort");
                    990:
                    991:        return EOPNOTSUPP;
                    992: }
                    993:
                    994: static int
1.202     rtr       995: udp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
1.200     rtr       996: {
1.202     rtr       997:        return in_control(so, cmd, nam, ifp);
1.200     rtr       998: }
                    999:
                   1000: static int
1.203     rtr      1001: udp_stat(struct socket *so, struct stat *ub)
                   1002: {
1.206     rtr      1003:        KASSERT(solocked(so));
                   1004:
1.205     rtr      1005:        /* stat: don't bother with a blocksize. */
                   1006:        return 0;
1.203     rtr      1007: }
                   1008:
                   1009: static int
1.219     rtr      1010: udp_peeraddr(struct socket *so, struct sockaddr *nam)
1.207     rtr      1011: {
1.213     rtr      1012:        int s;
                   1013:
1.207     rtr      1014:        KASSERT(solocked(so));
                   1015:        KASSERT(sotoinpcb(so) != NULL);
                   1016:        KASSERT(nam != NULL);
                   1017:
1.213     rtr      1018:        s = splsoftnet();
1.219     rtr      1019:        in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
1.213     rtr      1020:        splx(s);
                   1021:
1.207     rtr      1022:        return 0;
                   1023: }
                   1024:
                   1025: static int
1.219     rtr      1026: udp_sockaddr(struct socket *so, struct sockaddr *nam)
1.207     rtr      1027: {
1.213     rtr      1028:        int s;
                   1029:
1.207     rtr      1030:        KASSERT(solocked(so));
                   1031:        KASSERT(sotoinpcb(so) != NULL);
                   1032:        KASSERT(nam != NULL);
                   1033:
1.213     rtr      1034:        s = splsoftnet();
1.219     rtr      1035:        in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
1.213     rtr      1036:        splx(s);
                   1037:
1.207     rtr      1038:        return 0;
                   1039: }
                   1040:
                   1041: static int
1.216     rtr      1042: udp_rcvd(struct socket *so, int flags, struct lwp *l)
                   1043: {
                   1044:        KASSERT(solocked(so));
                   1045:
                   1046:        return EOPNOTSUPP;
                   1047: }
                   1048:
                   1049: static int
1.209     rtr      1050: udp_recvoob(struct socket *so, struct mbuf *m, int flags)
                   1051: {
                   1052:        KASSERT(solocked(so));
                   1053:
                   1054:        return EOPNOTSUPP;
                   1055: }
                   1056:
                   1057: static int
1.221     rtr      1058: udp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
1.215     rtr      1059:     struct mbuf *control, struct lwp *l)
                   1060: {
                   1061:        struct inpcb *inp = sotoinpcb(so);
                   1062:        int error = 0;
                   1063:        struct in_addr laddr;                   /* XXX */
                   1064:        int s;
                   1065:
                   1066:        KASSERT(solocked(so));
                   1067:        KASSERT(inp != NULL);
                   1068:        KASSERT(m != NULL);
                   1069:
                   1070:        if (control && control->m_len) {
                   1071:                m_freem(control);
                   1072:                m_freem(m);
                   1073:                return EINVAL;
                   1074:        }
                   1075:
                   1076:        memset(&laddr, 0, sizeof laddr);
                   1077:
                   1078:        s = splsoftnet();
                   1079:        if (nam) {
                   1080:                laddr = inp->inp_laddr;         /* XXX */
                   1081:                if ((so->so_state & SS_ISCONNECTED) != 0) {
                   1082:                        error = EISCONN;
                   1083:                        goto die;
                   1084:                }
1.221     rtr      1085:                error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l);
1.215     rtr      1086:                if (error)
                   1087:                        goto die;
                   1088:        } else {
                   1089:                if ((so->so_state & SS_ISCONNECTED) == 0) {
                   1090:                        error = ENOTCONN;
                   1091:                        goto die;
                   1092:                }
                   1093:        }
                   1094:        error = udp_output(m, inp);
                   1095:        m = NULL;
                   1096:        if (nam) {
                   1097:                in_pcbdisconnect(inp);
                   1098:                inp->inp_laddr = laddr;         /* XXX */
                   1099:                in_pcbstate(inp, INP_BOUND);    /* XXX */
                   1100:        }
                   1101:   die:
                   1102:        if (m)
                   1103:                m_freem(m);
                   1104:
                   1105:        splx(s);
                   1106:        return error;
                   1107: }
                   1108:
                   1109: static int
1.209     rtr      1110: udp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
                   1111: {
                   1112:        KASSERT(solocked(so));
                   1113:
                   1114:        m_freem(m);
                   1115:        m_freem(control);
                   1116:
                   1117:        return EOPNOTSUPP;
                   1118: }
                   1119:
                   1120: static int
1.217     rtr      1121: udp_purgeif(struct socket *so, struct ifnet *ifp)
                   1122: {
                   1123:        int s;
                   1124:
                   1125:        s = splsoftnet();
                   1126:        mutex_enter(softnet_lock);
1.230     ozaki-r  1127:        in_pcbpurgeif0(&udbtable, ifp);
                   1128: #ifdef NET_MPSAFE
                   1129:        mutex_exit(softnet_lock);
1.229     knakahar 1130: #endif
1.217     rtr      1131:        in_purgeif(ifp);
1.230     ozaki-r  1132: #ifdef NET_MPSAFE
                   1133:        mutex_enter(softnet_lock);
                   1134: #endif
1.217     rtr      1135:        in_pcbpurgeif(&udbtable, ifp);
                   1136:        mutex_exit(softnet_lock);
                   1137:        splx(s);
                   1138:
                   1139:        return 0;
                   1140: }
                   1141:
                   1142: static int
1.166     thorpej  1143: sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
                   1144: {
                   1145:
1.172     thorpej  1146:        return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1.166     thorpej  1147: }
                   1148:
1.13      mycroft  1149: /*
                   1150:  * Sysctl for udp variables.
                   1151:  */
1.179     pooka    1152: static void
                   1153: sysctl_net_inet_udp_setup(struct sysctllog **clog)
1.13      mycroft  1154: {
1.194     pooka    1155:
1.116     atatat   1156:        sysctl_createv(clog, 0, NULL, NULL,
                   1157:                       CTLFLAG_PERMANENT,
1.114     atatat   1158:                       CTLTYPE_NODE, "inet", NULL,
                   1159:                       NULL, 0, NULL, 0,
                   1160:                       CTL_NET, PF_INET, CTL_EOL);
1.116     atatat   1161:        sysctl_createv(clog, 0, NULL, NULL,
                   1162:                       CTLFLAG_PERMANENT,
1.122     atatat   1163:                       CTLTYPE_NODE, "udp",
                   1164:                       SYSCTL_DESCR("UDPv4 related settings"),
1.114     atatat   1165:                       NULL, 0, NULL, 0,
                   1166:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
                   1167:
1.116     atatat   1168:        sysctl_createv(clog, 0, NULL, NULL,
                   1169:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1170:                       CTLTYPE_INT, "checksum",
1.123     heas     1171:                       SYSCTL_DESCR("Compute UDP checksums"),
1.114     atatat   1172:                       NULL, 0, &udpcksum, 0,
                   1173:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
                   1174:                       CTL_EOL);
1.116     atatat   1175:        sysctl_createv(clog, 0, NULL, NULL,
                   1176:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1177:                       CTLTYPE_INT, "sendspace",
                   1178:                       SYSCTL_DESCR("Default UDP send buffer size"),
1.114     atatat   1179:                       NULL, 0, &udp_sendspace, 0,
                   1180:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
                   1181:                       CTL_EOL);
1.116     atatat   1182:        sysctl_createv(clog, 0, NULL, NULL,
                   1183:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1184:                       CTLTYPE_INT, "recvspace",
                   1185:                       SYSCTL_DESCR("Default UDP receive buffer size"),
1.114     atatat   1186:                       NULL, 0, &udp_recvspace, 0,
                   1187:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
                   1188:                       CTL_EOL);
1.125     thorpej  1189:        sysctl_createv(clog, 0, NULL, NULL,
                   1190:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1191:                       CTLTYPE_INT, "do_loopback_cksum",
                   1192:                       SYSCTL_DESCR("Perform UDP checksum on loopback"),
                   1193:                       NULL, 0, &udp_do_loopback_cksum, 0,
                   1194:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
                   1195:                       CTL_EOL);
1.132     atatat   1196:        sysctl_createv(clog, 0, NULL, NULL,
                   1197:                       CTLFLAG_PERMANENT,
1.134     atatat   1198:                       CTLTYPE_STRUCT, "pcblist",
1.132     atatat   1199:                       SYSCTL_DESCR("UDP protocol control block list"),
                   1200:                       sysctl_inpcblist, 0, &udbtable, 0,
                   1201:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
                   1202:                       CTL_EOL);
1.139     elad     1203:        sysctl_createv(clog, 0, NULL, NULL,
                   1204:                       CTLFLAG_PERMANENT,
                   1205:                       CTLTYPE_STRUCT, "stats",
                   1206:                       SYSCTL_DESCR("UDP statistics"),
1.166     thorpej  1207:                       sysctl_net_inet_udp_stats, 0, NULL, 0,
1.139     elad     1208:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
                   1209:                       CTL_EOL);
1.1       cgd      1210: }
1.72      itojun   1211: #endif
1.130     manu     1212:
1.166     thorpej  1213: void
                   1214: udp_statinc(u_int stat)
                   1215: {
                   1216:
                   1217:        KASSERT(stat < UDP_NSTATS);
                   1218:        UDP_STATINC(stat);
                   1219: }
                   1220:
1.190     christos 1221: #if defined(INET) && defined(IPSEC)
1.130     manu     1222: /*
                   1223:  * Returns:
                   1224:  * 1 if the packet was processed
                   1225:  * 0 if normal UDP processing should take place
1.144     manu     1226:  * -1 if an error occurent and m was freed
1.130     manu     1227:  */
                   1228: static int
1.154     yamt     1229: udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1.151     christos 1230:     struct socket *so)
1.130     manu     1231: {
                   1232:        size_t len;
1.158     christos 1233:        void *data;
1.130     manu     1234:        struct inpcb *inp;
                   1235:        size_t skip = 0;
                   1236:        size_t minlen;
                   1237:        size_t iphdrlen;
                   1238:        struct ip *ip;
1.136     manu     1239:        struct m_tag *tag;
                   1240:        struct udphdr *udphdr;
                   1241:        u_int16_t sport, dport;
1.144     manu     1242:        struct mbuf *m = *mp;
1.130     manu     1243:
1.131     perry    1244:        /*
1.130     manu     1245:         * Collapse the mbuf chain if the first mbuf is too short
                   1246:         * The longest case is: UDP + non ESP marker + ESP
                   1247:         */
                   1248:        minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
                   1249:        if (minlen > m->m_pkthdr.len)
                   1250:                minlen = m->m_pkthdr.len;
                   1251:
                   1252:        if (m->m_len < minlen) {
1.144     manu     1253:                if ((*mp = m_pullup(m, minlen)) == NULL) {
1.130     manu     1254:                        printf("udp4_espinudp: m_pullup failed\n");
1.144     manu     1255:                        return -1;
1.130     manu     1256:                }
1.144     manu     1257:                m = *mp;
1.130     manu     1258:        }
                   1259:
1.131     perry    1260:        len = m->m_len - off;
1.158     christos 1261:        data = mtod(m, char *) + off;
1.130     manu     1262:        inp = sotoinpcb(so);
                   1263:
                   1264:        /* Ignore keepalive packets */
1.158     christos 1265:        if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1.188     christos 1266:                m_free(m);
                   1267:                *mp = NULL; /* avoid any further processiong by caller ... */
1.130     manu     1268:                return 1;
                   1269:        }
                   1270:
1.131     perry    1271:        /*
                   1272:         * Check that the payload is long enough to hold
1.130     manu     1273:         * an ESP header and compute the length of encapsulation
1.131     perry    1274:         * header to remove
1.130     manu     1275:         */
                   1276:        if (inp->inp_flags & INP_ESPINUDP) {
                   1277:                u_int32_t *st = (u_int32_t *)data;
                   1278:
                   1279:                if ((len <= sizeof(struct esp)) || (*st == 0))
                   1280:                        return 0; /* Normal UDP processing */
                   1281:
                   1282:                skip = sizeof(struct udphdr);
                   1283:        }
                   1284:
                   1285:        if (inp->inp_flags & INP_ESPINUDP_NON_IKE) {
1.142     kleink   1286:                u_int32_t *st = (u_int32_t *)data;
1.130     manu     1287:
                   1288:                if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1.142     kleink   1289:                    || ((st[0] | st[1]) != 0))
1.130     manu     1290:                        return 0; /* Normal UDP processing */
1.131     perry    1291:
1.130     manu     1292:                skip = sizeof(struct udphdr) + sizeof(u_int64_t);
                   1293:        }
                   1294:
                   1295:        /*
1.136     manu     1296:         * Get the UDP ports. They are handled in network
                   1297:         * order everywhere in IPSEC_NAT_T code.
                   1298:         */
1.158     christos 1299:        udphdr = (struct udphdr *)((char *)data - skip);
1.136     manu     1300:        sport = udphdr->uh_sport;
                   1301:        dport = udphdr->uh_dport;
                   1302:
                   1303:        /*
1.130     manu     1304:         * Remove the UDP header (and possibly the non ESP marker)
                   1305:         * IP header lendth is iphdrlen
1.131     perry    1306:         * Before:
1.130     manu     1307:         *   <--- off --->
                   1308:         *   +----+------+-----+
                   1309:         *   | IP |  UDP | ESP |
                   1310:         *   +----+------+-----+
                   1311:         *        <-skip->
                   1312:         * After:
                   1313:         *          +----+-----+
                   1314:         *          | IP | ESP |
                   1315:         *          +----+-----+
                   1316:         *   <-skip->
                   1317:         */
                   1318:        iphdrlen = off - sizeof(struct udphdr);
1.158     christos 1319:        memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1.130     manu     1320:        m_adj(m, skip);
                   1321:
                   1322:        ip = mtod(m, struct ip *);
                   1323:        ip->ip_len = htons(ntohs(ip->ip_len) - skip);
                   1324:        ip->ip_p = IPPROTO_ESP;
                   1325:
                   1326:        /*
1.188     christos 1327:         * We have modified the packet - it is now ESP, so we should not
                   1328:         * return to UDP processing ...
                   1329:         *
1.136     manu     1330:         * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
                   1331:         * the source UDP port. This is required if we want
                   1332:         * to select the right SPD for multiple hosts behind
                   1333:         * same NAT
                   1334:         */
1.137     manu     1335:        if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
                   1336:            sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
                   1337:                printf("udp4_espinudp: m_tag_get failed\n");
1.188     christos 1338:                m_freem(m);
                   1339:                return -1;
1.137     manu     1340:        }
1.136     manu     1341:        ((u_int16_t *)(tag + 1))[0] = sport;
                   1342:        ((u_int16_t *)(tag + 1))[1] = dport;
1.188     christos 1343:        m_tag_prepend(m, tag);
1.136     manu     1344:
1.199     christos 1345:        if (ipsec_used)
                   1346:                ipsec4_common_input(m, iphdrlen, IPPROTO_ESP);
                   1347:        /* XXX: else */
1.130     manu     1348:
1.185     liamjfoy 1349:        /* We handled it, it shouldn't be handled by UDP */
1.188     christos 1350:        *mp = NULL; /* avoid free by caller ... */
1.130     manu     1351:        return 1;
                   1352: }
                   1353: #endif
1.195     rmind    1354:
1.197     rmind    1355: PR_WRAP_USRREQS(udp)
                   1356: #define        udp_attach      udp_attach_wrapper
                   1357: #define        udp_detach      udp_detach_wrapper
1.208     rtr      1358: #define        udp_accept      udp_accept_wrapper
1.210     rtr      1359: #define        udp_bind        udp_bind_wrapper
                   1360: #define        udp_listen      udp_listen_wrapper
1.211     rtr      1361: #define        udp_connect     udp_connect_wrapper
1.217     rtr      1362: #define        udp_connect2    udp_connect2_wrapper
1.212     rtr      1363: #define        udp_disconnect  udp_disconnect_wrapper
                   1364: #define        udp_shutdown    udp_shutdown_wrapper
                   1365: #define        udp_abort       udp_abort_wrapper
1.200     rtr      1366: #define        udp_ioctl       udp_ioctl_wrapper
1.203     rtr      1367: #define        udp_stat        udp_stat_wrapper
1.207     rtr      1368: #define        udp_peeraddr    udp_peeraddr_wrapper
                   1369: #define        udp_sockaddr    udp_sockaddr_wrapper
1.216     rtr      1370: #define        udp_rcvd        udp_rcvd_wrapper
1.209     rtr      1371: #define        udp_recvoob     udp_recvoob_wrapper
1.215     rtr      1372: #define        udp_send        udp_send_wrapper
1.209     rtr      1373: #define        udp_sendoob     udp_sendoob_wrapper
1.217     rtr      1374: #define        udp_purgeif     udp_purgeif_wrapper
1.195     rmind    1375:
                   1376: const struct pr_usrreqs udp_usrreqs = {
1.196     rmind    1377:        .pr_attach      = udp_attach,
                   1378:        .pr_detach      = udp_detach,
1.208     rtr      1379:        .pr_accept      = udp_accept,
1.210     rtr      1380:        .pr_bind        = udp_bind,
                   1381:        .pr_listen      = udp_listen,
1.211     rtr      1382:        .pr_connect     = udp_connect,
1.217     rtr      1383:        .pr_connect2    = udp_connect2,
1.212     rtr      1384:        .pr_disconnect  = udp_disconnect,
                   1385:        .pr_shutdown    = udp_shutdown,
                   1386:        .pr_abort       = udp_abort,
1.200     rtr      1387:        .pr_ioctl       = udp_ioctl,
1.203     rtr      1388:        .pr_stat        = udp_stat,
1.207     rtr      1389:        .pr_peeraddr    = udp_peeraddr,
                   1390:        .pr_sockaddr    = udp_sockaddr,
1.216     rtr      1391:        .pr_rcvd        = udp_rcvd,
1.209     rtr      1392:        .pr_recvoob     = udp_recvoob,
1.215     rtr      1393:        .pr_send        = udp_send,
1.209     rtr      1394:        .pr_sendoob     = udp_sendoob,
1.217     rtr      1395:        .pr_purgeif     = udp_purgeif,
1.195     rmind    1396: };

CVSweb <webmaster@jp.NetBSD.org>