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

1.190.2.2! rmind       1: /*     $NetBSD: udp_usrreq.c,v 1.190.2.1 2013/07/17 03:16:31 rmind Exp $       */
1.48      itojun      2:
                      3: /*
                      4:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      5:  * All rights reserved.
1.94      itojun      6:  *
1.48      itojun      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. Neither the name of the project nor the names of its contributors
                     16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
1.94      itojun     18:  *
1.48      itojun     19:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
1.14      cgd        31:
1.1       cgd        32: /*
1.44      thorpej    33:  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
1.13      mycroft    34:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        35:  *
                     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
1.104     agc        44:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
1.44      thorpej    60:  *     @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
1.1       cgd        61:  */
1.91      lukem      62:
1.190.2.1  rmind      63: /*
                     64:  * UDP protocol implementation.
                     65:  * Per RFC 768, August, 1980.
                     66:  */
                     67:
1.91      lukem      68: #include <sys/cdefs.h>
1.190.2.2! rmind      69: __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.190.2.1 2013/07/17 03:16:31 rmind Exp $");
1.50      thorpej    70:
1.77      soda       71: #include "opt_inet.h"
1.174     christos   72: #include "opt_compat_netbsd.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.1       cgd        77:
1.5       mycroft    78: #include <sys/param.h>
                     79: #include <sys/mbuf.h>
                     80: #include <sys/protosw.h>
                     81: #include <sys/socket.h>
                     82: #include <sys/socketvar.h>
1.27      christos   83: #include <sys/systm.h>
1.190.2.1  rmind      84: #include <sys/kmem.h>
1.53      itojun     85: #include <sys/domain.h>
1.27      christos   86: #include <sys/sysctl.h>
1.1       cgd        87:
1.5       mycroft    88: #include <net/if.h>
                     89: #include <net/route.h>
1.1       cgd        90:
1.5       mycroft    91: #include <netinet/in.h>
                     92: #include <netinet/in_systm.h>
1.15      cgd        93: #include <netinet/in_var.h>
1.5       mycroft    94: #include <netinet/ip.h>
                     95: #include <netinet/in_pcb.h>
                     96: #include <netinet/ip_var.h>
                     97: #include <netinet/ip_icmp.h>
                     98: #include <netinet/udp.h>
                     99: #include <netinet/udp_var.h>
1.166     thorpej   100: #include <netinet/udp_private.h>
1.1       cgd       101:
1.53      itojun    102: #ifdef INET6
                    103: #include <netinet/ip6.h>
                    104: #include <netinet/icmp6.h>
                    105: #include <netinet6/ip6_var.h>
1.167     thorpej   106: #include <netinet6/ip6_private.h>
1.53      itojun    107: #include <netinet6/in6_pcb.h>
                    108: #include <netinet6/udp6_var.h>
1.168     thorpej   109: #include <netinet6/udp6_private.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.190     christos  117: #ifdef IPSEC
1.105     jonathan  118: #include <netipsec/ipsec.h>
1.169     thorpej   119: #include <netipsec/ipsec_var.h>
                    120: #include <netipsec/ipsec_private.h>
1.147     christos  121: #include <netipsec/esp.h>
1.105     jonathan  122: #ifdef INET6
                    123: #include <netipsec/ipsec6.h>
                    124: #endif
1.190     christos  125: #endif /* IPSEC */
1.105     jonathan  126:
1.174     christos  127: #ifdef COMPAT_50
                    128: #include <compat/sys/socket.h>
                    129: #endif
                    130:
1.64      ws        131: #ifdef IPKDB
                    132: #include <ipkdb/ipkdb.h>
                    133: #endif
                    134:
1.8       mycroft   135: int    udpcksum = 1;
1.141     yamt      136: int    udp_do_loopback_cksum = 0;
1.93      matt      137:
1.190.2.1  rmind     138: inpcbtable_t *         udbtable __read_mostly;
                    139: percpu_t *             udpstat_percpu;
1.8       mycroft   140:
1.72      itojun    141: #ifdef INET
1.190     christos  142: #ifdef IPSEC
1.144     manu      143: static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
1.130     manu      144:        struct socket *);
1.189     christos  145: #endif
1.119     matt      146: static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
                    147:        struct socket *);
                    148: static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
1.144     manu      149:        struct mbuf **, int);
1.129     yamt      150: static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.72      itojun    151: #endif
1.7       mycroft   152:
1.26      mycroft   153: #ifndef UDBHASHSIZE
                    154: #define        UDBHASHSIZE     128
                    155: #endif
                    156: int    udbhashsize = UDBHASHSIZE;
                    157:
1.190.2.1  rmind     158: static int     udp_sendspace = 9216;
                    159: static int     udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
                    160:
1.98      matt      161: #ifdef MBUFTRACE
1.150     dogcow    162: struct mowner udp_mowner = MOWNER_INIT("udp", "");
                    163: struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
                    164: struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
1.98      matt      165: #endif
                    166:
1.78      thorpej   167: #ifdef UDP_CSUM_COUNTERS
                    168: #include <sys/device.h>
                    169:
1.140     yamt      170: #if defined(INET)
1.78      thorpej   171: struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    172:     NULL, "udp", "hwcsum bad");
                    173: struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    174:     NULL, "udp", "hwcsum ok");
                    175: struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    176:     NULL, "udp", "hwcsum data");
                    177: struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    178:     NULL, "udp", "swcsum");
                    179:
1.120     matt      180: EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
                    181: EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
                    182: EVCNT_ATTACH_STATIC(udp_hwcsum_data);
                    183: EVCNT_ATTACH_STATIC(udp_swcsum);
1.140     yamt      184: #endif /* defined(INET) */
                    185:
                    186: #define        UDP_CSUM_COUNTER_INCR(ev)       (ev)->ev_count++
1.78      thorpej   187: #else
                    188: #define        UDP_CSUM_COUNTER_INCR(ev)       /* nothing */
                    189: #endif /* UDP_CSUM_COUNTERS */
                    190:
1.179     pooka     191: static void sysctl_net_inet_udp_setup(struct sysctllog **);
                    192:
1.7       mycroft   193: void
1.119     matt      194: udp_init(void)
1.1       cgd       195: {
1.190.2.1  rmind     196:        udbtable = inpcb_init(udbhashsize, udbhashsize, 0);
1.179     pooka     197:        sysctl_net_inet_udp_setup(NULL);
                    198:
1.98      matt      199:        MOWNER_ATTACH(&udp_tx_mowner);
                    200:        MOWNER_ATTACH(&udp_rx_mowner);
                    201:        MOWNER_ATTACH(&udp_mowner);
1.166     thorpej   202:
1.171     yamt      203: #ifdef INET
1.166     thorpej   204:        udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
1.171     yamt      205: #endif
1.1       cgd       206: }
                    207:
1.129     yamt      208: /*
                    209:  * Checksum extended UDP header and data.
                    210:  */
                    211:
                    212: int
                    213: udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
                    214:     int iphlen, int len)
                    215: {
                    216:        switch (af) {
1.72      itojun    217: #ifdef INET
1.129     yamt      218:        case AF_INET:
                    219:                return udp4_input_checksum(m, uh, iphlen, len);
                    220: #endif
                    221: #ifdef INET6
                    222:        case AF_INET6:
                    223:                return udp6_input_checksum(m, uh, iphlen, len);
                    224: #endif
1.190.2.1  rmind     225:        default:
                    226:                KASSERT(false);
1.129     yamt      227:        }
                    228:        return -1;
                    229: }
                    230:
                    231: #ifdef INET
                    232:
                    233: /*
                    234:  * Checksum extended UDP header and data.
                    235:  */
                    236:
                    237: static int
                    238: udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
                    239:     int iphlen, int len)
                    240: {
                    241:
                    242:        /*
                    243:         * XXX it's better to record and check if this mbuf is
                    244:         * already checked.
                    245:         */
                    246:
                    247:        if (uh->uh_sum == 0)
                    248:                return 0;
                    249:
                    250:        switch (m->m_pkthdr.csum_flags &
                    251:            ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
                    252:            M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
                    253:        case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
                    254:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
                    255:                goto badcsum;
                    256:
                    257:        case M_CSUM_UDPv4|M_CSUM_DATA: {
                    258:                u_int32_t hw_csum = m->m_pkthdr.csum_data;
                    259:
                    260:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
                    261:                if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
                    262:                        const struct ip *ip =
                    263:                            mtod(m, const struct ip *);
                    264:
                    265:                        hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
                    266:                            ip->ip_dst.s_addr,
                    267:                            htons(hw_csum + len + IPPROTO_UDP));
                    268:                }
                    269:                if ((hw_csum ^ 0xffff) != 0)
                    270:                        goto badcsum;
                    271:                break;
                    272:        }
                    273:
                    274:        case M_CSUM_UDPv4:
                    275:                /* Checksum was okay. */
                    276:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
                    277:                break;
                    278:
                    279:        default:
                    280:                /*
                    281:                 * Need to compute it ourselves.  Maybe skip checksum
                    282:                 * on loopback interfaces.
                    283:                 */
                    284:                if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
                    285:                                     IFF_LOOPBACK) ||
                    286:                                   udp_do_loopback_cksum)) {
                    287:                        UDP_CSUM_COUNTER_INCR(&udp_swcsum);
                    288:                        if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
                    289:                                goto badcsum;
                    290:                }
                    291:                break;
                    292:        }
                    293:
                    294:        return 0;
                    295:
                    296: badcsum:
1.166     thorpej   297:        UDP_STATINC(UDP_STAT_BADSUM);
1.129     yamt      298:        return -1;
                    299: }
                    300:
1.7       mycroft   301: void
1.27      christos  302: udp_input(struct mbuf *m, ...)
1.1       cgd       303: {
1.53      itojun    304:        va_list ap;
                    305:        struct sockaddr_in src, dst;
                    306:        struct ip *ip;
                    307:        struct udphdr *uh;
1.97      simonb    308:        int iphlen;
1.53      itojun    309:        int len;
                    310:        int n;
1.96      itojun    311:        u_int16_t ip_len;
1.53      itojun    312:
                    313:        va_start(ap, m);
                    314:        iphlen = va_arg(ap, int);
1.97      simonb    315:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.53      itojun    316:        va_end(ap);
                    317:
1.98      matt      318:        MCLAIM(m, &udp_rx_mowner);
1.166     thorpej   319:        UDP_STATINC(UDP_STAT_IPACKETS);
1.53      itojun    320:
                    321:        /*
                    322:         * Get IP and UDP header together in first mbuf.
                    323:         */
                    324:        ip = mtod(m, struct ip *);
                    325:        IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
                    326:        if (uh == NULL) {
1.166     thorpej   327:                UDP_STATINC(UDP_STAT_HDROPS);
1.53      itojun    328:                return;
                    329:        }
1.95      thorpej   330:        KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53      itojun    331:
1.57      itojun    332:        /* destination port of 0 is illegal, based on RFC768. */
                    333:        if (uh->uh_dport == 0)
                    334:                goto bad;
                    335:
1.53      itojun    336:        /*
                    337:         * Make mbuf data length reflect UDP length.
                    338:         * If not enough data to reflect UDP length, drop.
                    339:         */
1.96      itojun    340:        ip_len = ntohs(ip->ip_len);
1.53      itojun    341:        len = ntohs((u_int16_t)uh->uh_ulen);
1.96      itojun    342:        if (ip_len != iphlen + len) {
                    343:                if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.166     thorpej   344:                        UDP_STATINC(UDP_STAT_BADLEN);
1.53      itojun    345:                        goto bad;
                    346:                }
1.96      itojun    347:                m_adj(m, iphlen + len - ip_len);
1.53      itojun    348:        }
                    349:
                    350:        /*
                    351:         * Checksum extended UDP header and data.
                    352:         */
1.129     yamt      353:        if (udp4_input_checksum(m, uh, iphlen, len))
                    354:                goto badcsum;
1.53      itojun    355:
                    356:        /* construct source and dst sockaddrs. */
1.159     dyoung    357:        sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
                    358:        sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
1.53      itojun    359:
1.144     manu      360:        if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
1.166     thorpej   361:                UDP_STATINC(UDP_STAT_HDROPS);
1.144     manu      362:                return;
                    363:        }
1.188     christos  364:        if (m == NULL) {
                    365:                /*
                    366:                 * packet has been processed by ESP stuff -
                    367:                 * e.g. dropped NAT-T-keep-alive-packet ...
                    368:                 */
                    369:                return;
                    370:        }
                    371:        ip = mtod(m, struct ip *);
1.53      itojun    372: #ifdef INET6
                    373:        if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
                    374:                struct sockaddr_in6 src6, dst6;
                    375:
1.175     cegger    376:                memset(&src6, 0, sizeof(src6));
1.53      itojun    377:                src6.sin6_family = AF_INET6;
                    378:                src6.sin6_len = sizeof(struct sockaddr_in6);
                    379:                src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
1.177     tsutsui   380:                memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
1.53      itojun    381:                        sizeof(ip->ip_src));
                    382:                src6.sin6_port = uh->uh_sport;
1.175     cegger    383:                memset(&dst6, 0, sizeof(dst6));
1.53      itojun    384:                dst6.sin6_family = AF_INET6;
                    385:                dst6.sin6_len = sizeof(struct sockaddr_in6);
                    386:                dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
1.177     tsutsui   387:                memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
1.53      itojun    388:                        sizeof(ip->ip_dst));
                    389:                dst6.sin6_port = uh->uh_dport;
                    390:
                    391:                n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
                    392:        }
                    393: #endif
                    394:
                    395:        if (n == 0) {
                    396:                if (m->m_flags & (M_BCAST | M_MCAST)) {
1.166     thorpej   397:                        UDP_STATINC(UDP_STAT_NOPORTBCAST);
1.53      itojun    398:                        goto bad;
                    399:                }
1.166     thorpej   400:                UDP_STATINC(UDP_STAT_NOPORT);
1.64      ws        401: #ifdef IPKDB
1.53      itojun    402:                if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
                    403:                                m, iphlen + sizeof(struct udphdr),
                    404:                                m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
                    405:                        /*
                    406:                         * It was a debugger connect packet,
                    407:                         * just drop it now
                    408:                         */
                    409:                        goto bad;
                    410:                }
                    411: #endif
                    412:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
                    413:                m = NULL;
                    414:        }
                    415:
                    416: bad:
                    417:        if (m)
                    418:                m_freem(m);
1.78      thorpej   419:        return;
                    420:
                    421: badcsum:
                    422:        m_freem(m);
1.53      itojun    423: }
                    424:
                    425: static void
1.119     matt      426: udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
                    427:        struct sockaddr *src, struct socket *so)
1.53      itojun    428: {
                    429:        struct mbuf *opts = NULL;
                    430:        struct mbuf *n;
1.190.2.1  rmind     431:        inpcb_t *inp = NULL;
1.53      itojun    432:
                    433:        if (!so)
                    434:                return;
                    435:        switch (so->so_proto->pr_domain->dom_family) {
                    436:        case AF_INET:
                    437:                inp = sotoinpcb(so);
                    438:                break;
                    439: #ifdef INET6
                    440:        case AF_INET6:
                    441:                break;
                    442: #endif
                    443:        default:
                    444:                return;
                    445:        }
                    446:
1.190     christos  447: #if defined(IPSEC)
1.53      itojun    448:        /* check AH/ESP integrity. */
                    449:        if (so != NULL && ipsec4_in_reject_so(m, so)) {
1.169     thorpej   450:                IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.162     dyoung    451:                if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
1.110     itojun    452:                        icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
                    453:                            0, 0);
1.53      itojun    454:                return;
                    455:        }
                    456: #endif /*IPSEC*/
                    457:
1.162     dyoung    458:        if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.190.2.1  rmind     459:                if (inp && ((inpcb_get_flags(inp) & INP_CONTROLOPTS) != 0
1.174     christos  460: #ifdef SO_OTIMESTAMP
                    461:                         || so->so_options & SO_OTIMESTAMP
                    462: #endif
1.53      itojun    463:                         || so->so_options & SO_TIMESTAMP)) {
                    464:                        struct ip *ip = mtod(n, struct ip *);
                    465:                        ip_savecontrol(inp, &opts, ip, n);
                    466:                }
                    467:
                    468:                m_adj(n, off);
                    469:                if (sbappendaddr(&so->so_rcv, src, n,
                    470:                                opts) == 0) {
                    471:                        m_freem(n);
                    472:                        if (opts)
                    473:                                m_freem(opts);
1.124     darrenr   474:                        so->so_rcv.sb_overflowed++;
1.166     thorpej   475:                        UDP_STATINC(UDP_STAT_FULLSOCK);
1.53      itojun    476:                } else
                    477:                        sorwakeup(so);
                    478:        }
                    479: }
                    480:
1.190.2.1  rmind     481: struct udp_pcb_ctx {
                    482:        struct mbuf *           mbuf;
                    483:        struct sockaddr_in *    src;
                    484:        struct sockaddr_in *    dst;
                    485:        int                     off;
                    486:        int                     rcvcnt;
                    487: };
1.53      itojun    488:
1.190.2.1  rmind     489: static int
                    490: udp4_pcb_process(inpcb_t *inp, void *arg)
                    491: {
                    492:        struct udp_pcb_ctx *uctx = arg;
                    493:        struct in_addr dst4 = uctx->dst->sin_addr;
                    494:        in_port_t dport = uctx->dst->sin_port;
                    495:        struct in_addr laddr, faddr;
                    496:        in_port_t lport, fport;
                    497:        struct socket *so;
1.53      itojun    498:
1.190.2.1  rmind     499:        inpcb_get_ports(inp, &lport, &fport);
                    500:        if (lport != dport) {
                    501:                return 0;
1.53      itojun    502:        }
1.190.2.1  rmind     503:        inpcb_get_addrs(inp, &laddr, &faddr);
                    504:        if (!in_nullhost(laddr) && !in_hosteq(laddr, dst4)) {
                    505:                return 0;
                    506:        }
                    507:        if (!in_nullhost(faddr)) {
                    508:                struct in_addr src4 = uctx->src->sin_addr;
                    509:                in_port_t sport = uctx->src->sin_port;
1.53      itojun    510:
1.190.2.1  rmind     511:                if (!in_hosteq(faddr, src4) || fport != sport) {
                    512:                        return 0;
1.53      itojun    513:                }
1.190.2.1  rmind     514:        }
1.53      itojun    515:
1.190.2.1  rmind     516:        so = inpcb_get_socket(inp);
                    517:        udp4_sendup(uctx->mbuf, uctx->off, (struct sockaddr *)uctx->src, so);
                    518:        uctx->rcvcnt++;
                    519:
                    520:        /*
                    521:         * Do not look for additional matches if this one does not have
                    522:         * either the SO_REUSEPORT or SO_REUSEADDR socket options set.
                    523:         * This heuristic avoids searching through all PCBs in the common
                    524:         * case of a non-shared port.  It assumes that an application will
                    525:         * never clear these options after setting them.
                    526:         */
                    527:        if ((so->so_options & (SO_REUSEPORT|SO_REUSEADDR)) == 0) {
                    528:                return EJUSTRETURN;
1.53      itojun    529:        }
1.190.2.1  rmind     530:        return 0;
1.53      itojun    531: }
                    532:
                    533: static int
1.119     matt      534: udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
1.190.2.1  rmind     535:     struct mbuf **mp, int off /* offset of udphdr */)
1.53      itojun    536: {
1.190.2.1  rmind     537:        in_port_t *sport, *dport;
1.53      itojun    538:        struct in_addr *src4, *dst4;
1.190.2.1  rmind     539:        inpcb_t *inp;
1.144     manu      540:        struct mbuf *m = *mp;
1.190.2.1  rmind     541:        int rcvcnt;
1.53      itojun    542:
                    543:        rcvcnt = 0;
                    544:        off += sizeof(struct udphdr);   /* now, offset of payload */
                    545:
                    546:        if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
                    547:                goto bad;
                    548:
                    549:        src4 = &src->sin_addr;
                    550:        sport = &src->sin_port;
                    551:        dst4 = &dst->sin_addr;
                    552:        dport = &dst->sin_port;
                    553:
1.73      itojun    554:        if (IN_MULTICAST(dst4->s_addr) ||
1.53      itojun    555:            in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
1.190.2.1  rmind     556:                struct udp_pcb_ctx uctx = {
                    557:                        .mbuf = m, .src = src, .dst = dst,
                    558:                        .off = off, .rcvcnt = 0
                    559:                };
                    560:                int error;
                    561:
1.53      itojun    562:                /*
                    563:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    564:                 * for which the local and remote addresses and ports match
                    565:                 * those of the incoming datagram.  This allows more than
                    566:                 * one process to receive multi/broadcasts on the same port.
                    567:                 */
1.190.2.1  rmind     568:                error = inpcb_foreach(udbtable, AF_INET,
                    569:                    udp4_pcb_process, &uctx);
                    570:                KASSERT(error == 0 || error == EJUSTRETURN);
                    571:                rcvcnt = uctx.rcvcnt;
1.53      itojun    572:        } else {
                    573:                /*
1.190.2.1  rmind     574:                 * Locate PCB for datagram.
1.53      itojun    575:                 */
1.190.2.1  rmind     576:                struct socket *so;
                    577:
                    578:                inp = inpcb_lookup_connect(udbtable, *src4, *sport, *dst4,
1.180     dyoung    579:                    *dport, 0);
1.190.2.1  rmind     580:                if (inp == NULL) {
1.166     thorpej   581:                        UDP_STATINC(UDP_STAT_PCBHASHMISS);
1.190.2.1  rmind     582:                        inp = inpcb_lookup_bind(udbtable, *dst4, *dport);
                    583:                        if (inp == NULL)
1.53      itojun    584:                                return rcvcnt;
                    585:                }
1.190.2.1  rmind     586:                so = inpcb_get_socket(inp);
1.53      itojun    587:
1.190     christos  588: #ifdef IPSEC
1.130     manu      589:                /* Handle ESP over UDP */
1.190.2.1  rmind     590:                if (inpcb_get_flags(inp) & INP_ESPINUDP_ALL) {
1.130     manu      591:                        struct sockaddr *sa = (struct sockaddr *)src;
                    592:
1.190.2.1  rmind     593:                        switch (udp4_espinudp(mp, off, sa, so)) {
1.144     manu      594:                        case -1:        /* Error, m was freeed */
                    595:                                rcvcnt = -1;
                    596:                                goto bad;
                    597:                                break;
                    598:
                    599:                        case 1:         /* ESP over UDP */
1.130     manu      600:                                rcvcnt++;
                    601:                                goto bad;
1.144     manu      602:                                break;
                    603:
                    604:                        case 0:         /* plain UDP */
                    605:                        default:        /* Unexpected */
                    606:                                /*
                    607:                                 * Normal UDP processing will take place
                    608:                                 * m may have changed.
                    609:                                 */
                    610:                                m = *mp;
                    611:                                break;
1.130     manu      612:                        }
                    613:                }
1.189     christos  614: #endif
1.130     manu      615:
1.178     minskim   616:                /*
                    617:                 * Check the minimum TTL for socket.
                    618:                 */
1.190.2.1  rmind     619:                if (mtod(m, struct ip *)->ip_ttl < inpcb_get_minttl(inp)) {
1.178     minskim   620:                        goto bad;
1.53      itojun    621:                }
1.190.2.1  rmind     622:                udp4_sendup(m, off, (struct sockaddr *)src, so);
1.53      itojun    623:                rcvcnt++;
                    624:        }
                    625:
                    626: bad:
                    627:        return rcvcnt;
                    628: }
                    629: #endif
1.1       cgd       630:
1.72      itojun    631: #ifdef INET
1.1       cgd       632: /*
1.190.2.1  rmind     633:  * Notify a UDP user of an asynchronous error;
1.1       cgd       634:  * just wake up so that he can collect error status.
                    635:  */
1.7       mycroft   636: static void
1.190.2.1  rmind     637: udp_notify(inpcb_t *inp, int errno)
1.1       cgd       638: {
1.190.2.1  rmind     639:        struct socket *so = inpcb_get_socket(inp);
                    640:
                    641:        so->so_error = errno;
                    642:        sorwakeup(so);
                    643:        sowwakeup(so);
1.1       cgd       644: }
                    645:
1.27      christos  646: void *
1.157     dyoung    647: udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.1       cgd       648: {
1.66      augustss  649:        struct ip *ip = v;
                    650:        struct udphdr *uh;
1.21      mycroft   651:        int errno;
1.190.2.1  rmind     652:        bool rdr;
1.1       cgd       653:
1.190.2.1  rmind     654:        if (sa->sa_family != AF_INET ||
                    655:            sa->sa_len != sizeof(struct sockaddr_in))
1.51      itojun    656:                return NULL;
1.20      mycroft   657:        if ((unsigned)cmd >= PRC_NCMDS)
1.27      christos  658:                return NULL;
1.20      mycroft   659:        errno = inetctlerrmap[cmd];
1.190.2.1  rmind     660:
                    661:        rdr = PRC_IS_REDIRECT(cmd);
                    662:        if (rdr || cmd == PRC_HOSTDEAD || ip == NULL) {
                    663:                inpcb_notifyall(udbtable, satocsin(sa)->sin_addr,
                    664:                    errno, rdr ? inpcb_rtchange : udp_notify);
1.27      christos  665:                return NULL;
1.190.2.1  rmind     666:        } else if (errno == 0) {
                    667:                return NULL;
                    668:        }
1.53      itojun    669:
1.190.2.1  rmind     670:        /* Note: mapped address case */
                    671:        uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
                    672:        inpcb_notify(udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
                    673:            ip->ip_src, uh->uh_sport, errno, udp_notify);
1.27      christos  674:        return NULL;
1.1       cgd       675: }
                    676:
1.7       mycroft   677: int
1.173     plunky    678: udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.130     manu      679: {
1.190.2.1  rmind     680:        int s, family, optval, inpflags, error = 0;
                    681:        inpcb_t *inp;
1.130     manu      682:
                    683:        family = so->so_proto->pr_domain->dom_family;
                    684:
                    685:        s = splsoftnet();
                    686:        switch (family) {
                    687: #ifdef INET
                    688:        case PF_INET:
1.173     plunky    689:                if (sopt->sopt_level != IPPROTO_UDP) {
                    690:                        error = ip_ctloutput(op, so, sopt);
1.130     manu      691:                        goto end;
                    692:                }
                    693:                break;
                    694: #endif
                    695: #ifdef INET6
                    696:        case PF_INET6:
1.173     plunky    697:                if (sopt->sopt_level != IPPROTO_UDP) {
                    698:                        error = ip6_ctloutput(op, so, sopt);
1.130     manu      699:                        goto end;
                    700:                }
                    701:                break;
                    702: #endif
                    703:        default:
                    704:                error = EAFNOSUPPORT;
                    705:                goto end;
                    706:        }
                    707:
                    708:        switch (op) {
                    709:        case PRCO_SETOPT:
                    710:                inp = sotoinpcb(so);
                    711:
1.173     plunky    712:                switch (sopt->sopt_name) {
1.130     manu      713:                case UDP_ENCAP:
1.173     plunky    714:                        error = sockopt_getint(sopt, &optval);
                    715:                        if (error)
1.153     yamt      716:                                break;
1.131     perry     717:
1.190.2.1  rmind     718:                        inpflags = inpcb_get_flags(inp);
1.173     plunky    719:                        switch(optval) {
1.130     manu      720:                        case 0:
1.190.2.1  rmind     721:                                inpflags &= ~INP_ESPINUDP_ALL;
1.130     manu      722:                                break;
                    723:
                    724:                        case UDP_ENCAP_ESPINUDP:
1.190.2.1  rmind     725:                                inpflags &= ~INP_ESPINUDP_ALL;
                    726:                                inpflags |= INP_ESPINUDP;
1.130     manu      727:                                break;
1.131     perry     728:
1.130     manu      729:                        case UDP_ENCAP_ESPINUDP_NON_IKE:
1.190.2.1  rmind     730:                                inpflags &= ~INP_ESPINUDP_ALL;
                    731:                                inpflags |= INP_ESPINUDP_NON_IKE;
1.130     manu      732:                                break;
                    733:                        default:
                    734:                                error = EINVAL;
                    735:                                break;
                    736:                        }
1.190.2.1  rmind     737:                        inpcb_set_flags(inp, inpflags);
1.130     manu      738:                        break;
1.190.2.1  rmind     739:
1.130     manu      740:                default:
                    741:                        error = ENOPROTOOPT;
                    742:                        break;
                    743:                }
                    744:                break;
                    745:
                    746:        default:
                    747:                error = EINVAL;
                    748:                break;
1.131     perry     749:        }
                    750:
1.130     manu      751: end:
                    752:        splx(s);
                    753:        return error;
                    754: }
1.131     perry     755:
1.130     manu      756:
                    757: int
1.27      christos  758: udp_output(struct mbuf *m, ...)
                    759: {
1.190.2.1  rmind     760:        inpcb_t *inp;
                    761:        struct socket *so;
1.66      augustss  762:        struct udpiphdr *ui;
1.125     thorpej   763:        struct route *ro;
1.66      augustss  764:        int len = m->m_pkthdr.len;
1.31      mycroft   765:        int error = 0;
1.27      christos  766:        va_list ap;
                    767:
1.98      matt      768:        MCLAIM(m, &udp_tx_mowner);
1.27      christos  769:        va_start(ap, m);
1.190.2.1  rmind     770:        inp = va_arg(ap, inpcb_t *);
1.27      christos  771:        va_end(ap);
1.1       cgd       772:
1.190.2.1  rmind     773:        so = inpcb_get_socket(inp);
                    774:        KASSERT(solocked(so));
                    775:
1.1       cgd       776:        /*
                    777:         * Calculate data length and get a mbuf
                    778:         * for UDP and IP headers.
                    779:         */
1.13      mycroft   780:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
                    781:        if (m == 0) {
                    782:                error = ENOBUFS;
1.39      thorpej   783:                goto release;
                    784:        }
                    785:
                    786:        /*
                    787:         * Compute the packet length of the IP header, and
                    788:         * punt if the length looks bogus.
                    789:         */
1.96      itojun    790:        if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39      thorpej   791:                error = EMSGSIZE;
1.13      mycroft   792:                goto release;
                    793:        }
1.1       cgd       794:
                    795:        /*
                    796:         * Fill in mbuf with extended UDP header
                    797:         * and addresses and length put into network format.
                    798:         */
                    799:        ui = mtod(m, struct udpiphdr *);
                    800:        ui->ui_pr = IPPROTO_UDP;
1.190.2.1  rmind     801:
                    802:        inpcb_get_addrs(inp, &ui->ui_src, &ui->ui_dst);
                    803:        inpcb_get_ports(inp, &ui->ui_sport, &ui->ui_dport);
1.78      thorpej   804:        ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1       cgd       805:
1.190.2.1  rmind     806:        ro = inpcb_get_route(inp);
1.125     thorpej   807:
1.1       cgd       808:        /*
1.78      thorpej   809:         * Set up checksum and output datagram.
1.1       cgd       810:         */
                    811:        if (udpcksum) {
1.78      thorpej   812:                /*
                    813:                 * XXX Cache pseudo-header checksum part for
                    814:                 * XXX "connected" UDP sockets.
                    815:                 */
                    816:                ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
                    817:                    ui->ui_dst.s_addr, htons((u_int16_t)len +
                    818:                    sizeof(struct udphdr) + IPPROTO_UDP));
1.135     yamt      819:                m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1.78      thorpej   820:                m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
                    821:        } else
                    822:                ui->ui_sum = 0;
1.190.2.1  rmind     823:
1.96      itojun    824:        ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1.190.2.1  rmind     825:
                    826:        struct ip *inp_ip = in_getiphdr(inp);
                    827:        ((struct ip *)ui)->ip_ttl = inp_ip->ip_ttl;     /* XXX */
                    828:        ((struct ip *)ui)->ip_tos = inp_ip->ip_tos;     /* XXX */
1.166     thorpej   829:        UDP_STATINC(UDP_STAT_OPACKETS);
1.48      itojun    830:
1.190.2.1  rmind     831:        return (ip_output(m, inpcb_get_options(inp), ro,
                    832:            so->so_options & (SO_DONTROUTE | SO_BROADCAST),
                    833:            inpcb_get_moptions(inp), so));
1.1       cgd       834:
                    835: release:
                    836:        m_freem(m);
1.190.2.1  rmind     837:        return error;
1.1       cgd       838: }
                    839:
1.190.2.2! rmind     840: static int
        !           841: udp_attach(struct socket *so, int proto)
        !           842: {
        !           843:        inpcb_t *inp;
        !           844:        struct ip *ip;
        !           845:        int s, error;
        !           846:
        !           847:        KASSERT(sotoinpcb(so) == NULL);
        !           848:
        !           849:        s = splsoftnet();
        !           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:                        goto out;
        !           861:                }
        !           862:        }
        !           863:
        !           864:        error = inpcb_create(so, udbtable);
        !           865:        if (error) {
        !           866:                goto out;
        !           867:        }
        !           868:        inp = sotoinpcb(so);
        !           869:        ip = in_getiphdr(inp);
        !           870:        ip->ip_ttl = ip_defttl;
        !           871: out:
        !           872:        splx(s);
        !           873:        return error;
        !           874: }
        !           875:
        !           876: static void
        !           877: udp_detach(struct socket *so)
        !           878: {
        !           879:        inpcb_t *inp;
        !           880:        int s;
        !           881:
        !           882:        KASSERT(solocked(so));
        !           883:
        !           884:        s = splsoftnet();
        !           885:        inp = sotoinpcb(so);
        !           886:        KASSERT(inp != NULL);
        !           887:        inpcb_destroy(inp);
        !           888:        splx(s);
        !           889: }
        !           890:
        !           891: static int
1.119     matt      892: udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1.190.2.1  rmind     893:     struct mbuf *control, struct lwp *l)
1.1       cgd       894: {
1.190.2.1  rmind     895:        inpcb_t *inp;
                    896:        int s, error = 0;
1.59      thorpej   897:
1.190.2.2! rmind     898:        KASSERT(req != PRU_ATTACH);
        !           899:        KASSERT(req != PRU_DETACH);
        !           900:
1.190.2.1  rmind     901:        if (req == PRU_CONTROL) {
                    902:                return in_control(so, (long)m, nam, (ifnet_t *)control, l);
                    903:        }
1.149     tls       904:        s = splsoftnet();
1.60      thorpej   905:        if (req == PRU_PURGEIF) {
1.170     ad        906:                mutex_enter(softnet_lock);
1.190.2.1  rmind     907:                inpcb_purgeif0(udbtable, (ifnet_t *)control);
                    908:                in_purgeif((ifnet_t *)control);
                    909:                inpcb_purgeif(udbtable, (ifnet_t *)control);
1.170     ad        910:                mutex_exit(softnet_lock);
1.149     tls       911:                splx(s);
1.59      thorpej   912:                return (0);
                    913:        }
1.31      mycroft   914:
1.190.2.2! rmind     915:        KASSERT(solocked(so));
1.31      mycroft   916:        inp = sotoinpcb(so);
1.190.2.1  rmind     917:
                    918:        KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
1.190.2.2! rmind     919:        if (inp == NULL) {
1.31      mycroft   920:                error = EINVAL;
                    921:                goto release;
                    922:        }
                    923:
1.1       cgd       924:        /*
                    925:         * Note: need to block udp_input while changing
                    926:         * the udp pcb queue and/or pcb addresses.
                    927:         */
                    928:        switch (req) {
                    929:        case PRU_BIND:
1.190.2.1  rmind     930:                error = inpcb_bind(inp, nam, l);
1.1       cgd       931:                break;
                    932:
                    933:        case PRU_LISTEN:
                    934:                error = EOPNOTSUPP;
                    935:                break;
                    936:
                    937:        case PRU_CONNECT:
1.190.2.1  rmind     938:                error = inpcb_connect(inp, nam, l);
1.31      mycroft   939:                if (error)
1.1       cgd       940:                        break;
1.31      mycroft   941:                soisconnected(so);
1.1       cgd       942:                break;
                    943:
                    944:        case PRU_CONNECT2:
                    945:                error = EOPNOTSUPP;
                    946:                break;
                    947:
                    948:        case PRU_DISCONNECT:
1.31      mycroft   949:                /*soisdisconnected(so);*/
1.190.2.1  rmind     950:                so->so_state &= ~SS_ISCONNECTED;                /* XXX */
                    951:                inpcb_disconnect(inp);
                    952:                inpcb_set_addrs(inp, &zeroin_addr, NULL);       /* XXX */
                    953:                inpcb_set_state(inp, INP_BOUND);                /* XXX */
1.1       cgd       954:                break;
                    955:
                    956:        case PRU_SHUTDOWN:
                    957:                socantsendmore(so);
                    958:                break;
                    959:
1.31      mycroft   960:        case PRU_RCVD:
                    961:                error = EOPNOTSUPP;
1.1       cgd       962:                break;
                    963:
1.31      mycroft   964:        case PRU_SEND:
1.32      mycroft   965:                if (control && control->m_len) {
                    966:                        m_freem(control);
                    967:                        m_freem(m);
                    968:                        error = EINVAL;
                    969:                        break;
                    970:                }
1.31      mycroft   971:        {
1.190.2.1  rmind     972:                /*
                    973:                 * Note: sendto case - temporarily connect the socket
                    974:                 * to the destination, send and then disconnect.
                    975:                 * XXX: save the local address, restore after.
                    976:                 */
                    977:                struct in_addr laddr;
1.1       cgd       978:
1.181     mrg       979:                memset(&laddr, 0, sizeof laddr);
1.31      mycroft   980:                if (nam) {
1.190.2.1  rmind     981:                        inpcb_get_addrs(inp, &laddr, NULL);
1.31      mycroft   982:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
                    983:                                error = EISCONN;
1.32      mycroft   984:                                goto die;
1.31      mycroft   985:                        }
1.190.2.1  rmind     986:                        error = inpcb_connect(inp, nam, l);
1.100     matt      987:                        if (error)
                    988:                                goto die;
1.31      mycroft   989:                } else {
                    990:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
                    991:                                error = ENOTCONN;
1.32      mycroft   992:                                goto die;
1.31      mycroft   993:                        }
                    994:                }
1.33      mycroft   995:                error = udp_output(m, inp);
1.100     matt      996:                m = NULL;
1.31      mycroft   997:                if (nam) {
1.190.2.1  rmind     998:                        inpcb_disconnect(inp);
                    999:                        inpcb_set_addrs(inp, &laddr, NULL);
                   1000:                        inpcb_set_state(inp, INP_BOUND);
1.31      mycroft  1001:                }
1.190.2.1  rmind    1002: die:
1.100     matt     1003:                if (m)
                   1004:                        m_freem(m);
1.31      mycroft  1005:        }
1.1       cgd      1006:                break;
                   1007:
                   1008:        case PRU_SENSE:
                   1009:                /*
                   1010:                 * stat: don't bother with a blocksize.
                   1011:                 */
1.31      mycroft  1012:                splx(s);
1.1       cgd      1013:                return (0);
                   1014:
1.31      mycroft  1015:        case PRU_RCVOOB:
                   1016:                error =  EOPNOTSUPP;
                   1017:                break;
                   1018:
1.1       cgd      1019:        case PRU_SENDOOB:
1.32      mycroft  1020:                m_freem(control);
1.31      mycroft  1021:                m_freem(m);
1.1       cgd      1022:                error =  EOPNOTSUPP;
                   1023:                break;
                   1024:
1.31      mycroft  1025:        case PRU_SOCKADDR:
1.190.2.1  rmind    1026:                inpcb_fetch_sockaddr(inp, nam);
1.31      mycroft  1027:                break;
                   1028:
                   1029:        case PRU_PEERADDR:
1.190.2.1  rmind    1030:                inpcb_fetch_peeraddr(inp, nam);
1.31      mycroft  1031:                break;
1.1       cgd      1032:
                   1033:        default:
                   1034:                panic("udp_usrreq");
                   1035:        }
                   1036:
                   1037: release:
1.31      mycroft  1038:        splx(s);
1.1       cgd      1039:        return (error);
1.13      mycroft  1040: }
                   1041:
1.166     thorpej  1042: static int
                   1043: sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
                   1044: {
                   1045:
1.172     thorpej  1046:        return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1.166     thorpej  1047: }
                   1048:
1.13      mycroft  1049: /*
                   1050:  * Sysctl for udp variables.
                   1051:  */
1.179     pooka    1052: static void
                   1053: sysctl_net_inet_udp_setup(struct sysctllog **clog)
1.13      mycroft  1054: {
1.116     atatat   1055:        sysctl_createv(clog, 0, NULL, NULL,
                   1056:                       CTLFLAG_PERMANENT,
1.114     atatat   1057:                       CTLTYPE_NODE, "net", NULL,
                   1058:                       NULL, 0, NULL, 0,
                   1059:                       CTL_NET, CTL_EOL);
1.116     atatat   1060:        sysctl_createv(clog, 0, NULL, NULL,
                   1061:                       CTLFLAG_PERMANENT,
1.114     atatat   1062:                       CTLTYPE_NODE, "inet", NULL,
                   1063:                       NULL, 0, NULL, 0,
                   1064:                       CTL_NET, PF_INET, CTL_EOL);
1.116     atatat   1065:        sysctl_createv(clog, 0, NULL, NULL,
                   1066:                       CTLFLAG_PERMANENT,
1.122     atatat   1067:                       CTLTYPE_NODE, "udp",
                   1068:                       SYSCTL_DESCR("UDPv4 related settings"),
1.114     atatat   1069:                       NULL, 0, NULL, 0,
                   1070:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
                   1071:
1.116     atatat   1072:        sysctl_createv(clog, 0, NULL, NULL,
                   1073:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1074:                       CTLTYPE_INT, "checksum",
1.123     heas     1075:                       SYSCTL_DESCR("Compute UDP checksums"),
1.114     atatat   1076:                       NULL, 0, &udpcksum, 0,
                   1077:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
                   1078:                       CTL_EOL);
1.116     atatat   1079:        sysctl_createv(clog, 0, NULL, NULL,
                   1080:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1081:                       CTLTYPE_INT, "sendspace",
                   1082:                       SYSCTL_DESCR("Default UDP send buffer size"),
1.114     atatat   1083:                       NULL, 0, &udp_sendspace, 0,
                   1084:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
                   1085:                       CTL_EOL);
1.116     atatat   1086:        sysctl_createv(clog, 0, NULL, NULL,
                   1087:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122     atatat   1088:                       CTLTYPE_INT, "recvspace",
                   1089:                       SYSCTL_DESCR("Default UDP receive buffer size"),
1.114     atatat   1090:                       NULL, 0, &udp_recvspace, 0,
                   1091:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
                   1092:                       CTL_EOL);
1.125     thorpej  1093:        sysctl_createv(clog, 0, NULL, NULL,
                   1094:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1095:                       CTLTYPE_INT, "do_loopback_cksum",
                   1096:                       SYSCTL_DESCR("Perform UDP checksum on loopback"),
                   1097:                       NULL, 0, &udp_do_loopback_cksum, 0,
                   1098:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
                   1099:                       CTL_EOL);
1.132     atatat   1100:        sysctl_createv(clog, 0, NULL, NULL,
                   1101:                       CTLFLAG_PERMANENT,
1.134     atatat   1102:                       CTLTYPE_STRUCT, "pcblist",
1.132     atatat   1103:                       SYSCTL_DESCR("UDP protocol control block list"),
1.190.2.1  rmind    1104:                       sysctl_inpcblist, 0, udbtable, 0,
1.132     atatat   1105:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
                   1106:                       CTL_EOL);
1.139     elad     1107:        sysctl_createv(clog, 0, NULL, NULL,
                   1108:                       CTLFLAG_PERMANENT,
                   1109:                       CTLTYPE_STRUCT, "stats",
                   1110:                       SYSCTL_DESCR("UDP statistics"),
1.166     thorpej  1111:                       sysctl_net_inet_udp_stats, 0, NULL, 0,
1.139     elad     1112:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
                   1113:                       CTL_EOL);
1.1       cgd      1114: }
1.72      itojun   1115: #endif
1.130     manu     1116:
1.166     thorpej  1117: void
                   1118: udp_statinc(u_int stat)
                   1119: {
                   1120:
                   1121:        KASSERT(stat < UDP_NSTATS);
                   1122:        UDP_STATINC(stat);
                   1123: }
                   1124:
1.190     christos 1125: #if defined(INET) && defined(IPSEC)
1.130     manu     1126: /*
                   1127:  * Returns:
                   1128:  * 1 if the packet was processed
                   1129:  * 0 if normal UDP processing should take place
1.144     manu     1130:  * -1 if an error occurent and m was freed
1.130     manu     1131:  */
                   1132: static int
1.154     yamt     1133: udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1.151     christos 1134:     struct socket *so)
1.130     manu     1135: {
                   1136:        size_t len;
1.158     christos 1137:        void *data;
1.190.2.1  rmind    1138:        inpcb_t *inp;
1.130     manu     1139:        size_t skip = 0;
                   1140:        size_t minlen;
                   1141:        size_t iphdrlen;
                   1142:        struct ip *ip;
1.136     manu     1143:        struct m_tag *tag;
                   1144:        struct udphdr *udphdr;
1.190.2.1  rmind    1145:        in_port_t sport, dport;
1.144     manu     1146:        struct mbuf *m = *mp;
1.190.2.1  rmind    1147:        int inpflags;
1.130     manu     1148:
1.131     perry    1149:        /*
1.130     manu     1150:         * Collapse the mbuf chain if the first mbuf is too short
                   1151:         * The longest case is: UDP + non ESP marker + ESP
                   1152:         */
                   1153:        minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
                   1154:        if (minlen > m->m_pkthdr.len)
                   1155:                minlen = m->m_pkthdr.len;
                   1156:
                   1157:        if (m->m_len < minlen) {
1.144     manu     1158:                if ((*mp = m_pullup(m, minlen)) == NULL) {
1.130     manu     1159:                        printf("udp4_espinudp: m_pullup failed\n");
1.144     manu     1160:                        return -1;
1.130     manu     1161:                }
1.144     manu     1162:                m = *mp;
1.130     manu     1163:        }
                   1164:
1.131     perry    1165:        len = m->m_len - off;
1.158     christos 1166:        data = mtod(m, char *) + off;
1.130     manu     1167:        inp = sotoinpcb(so);
                   1168:
                   1169:        /* Ignore keepalive packets */
1.158     christos 1170:        if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1.188     christos 1171:                m_free(m);
                   1172:                *mp = NULL; /* avoid any further processiong by caller ... */
1.130     manu     1173:                return 1;
                   1174:        }
1.190.2.1  rmind    1175:        inpflags = inpcb_get_flags(inp);
1.130     manu     1176:
1.131     perry    1177:        /*
                   1178:         * Check that the payload is long enough to hold
1.130     manu     1179:         * an ESP header and compute the length of encapsulation
1.131     perry    1180:         * header to remove
1.130     manu     1181:         */
1.190.2.1  rmind    1182:        if (inpflags & INP_ESPINUDP) {
1.130     manu     1183:                u_int32_t *st = (u_int32_t *)data;
                   1184:
                   1185:                if ((len <= sizeof(struct esp)) || (*st == 0))
                   1186:                        return 0; /* Normal UDP processing */
                   1187:
                   1188:                skip = sizeof(struct udphdr);
                   1189:        }
                   1190:
1.190.2.1  rmind    1191:        if (inpflags & INP_ESPINUDP_NON_IKE) {
1.142     kleink   1192:                u_int32_t *st = (u_int32_t *)data;
1.130     manu     1193:
                   1194:                if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1.142     kleink   1195:                    || ((st[0] | st[1]) != 0))
1.130     manu     1196:                        return 0; /* Normal UDP processing */
1.131     perry    1197:
1.130     manu     1198:                skip = sizeof(struct udphdr) + sizeof(u_int64_t);
                   1199:        }
                   1200:
                   1201:        /*
1.136     manu     1202:         * Get the UDP ports. They are handled in network
                   1203:         * order everywhere in IPSEC_NAT_T code.
                   1204:         */
1.158     christos 1205:        udphdr = (struct udphdr *)((char *)data - skip);
1.136     manu     1206:        sport = udphdr->uh_sport;
                   1207:        dport = udphdr->uh_dport;
                   1208:
                   1209:        /*
1.130     manu     1210:         * Remove the UDP header (and possibly the non ESP marker)
                   1211:         * IP header lendth is iphdrlen
1.131     perry    1212:         * Before:
1.130     manu     1213:         *   <--- off --->
                   1214:         *   +----+------+-----+
                   1215:         *   | IP |  UDP | ESP |
                   1216:         *   +----+------+-----+
                   1217:         *        <-skip->
                   1218:         * After:
                   1219:         *          +----+-----+
                   1220:         *          | IP | ESP |
                   1221:         *          +----+-----+
                   1222:         *   <-skip->
                   1223:         */
                   1224:        iphdrlen = off - sizeof(struct udphdr);
1.158     christos 1225:        memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1.130     manu     1226:        m_adj(m, skip);
                   1227:
                   1228:        ip = mtod(m, struct ip *);
                   1229:        ip->ip_len = htons(ntohs(ip->ip_len) - skip);
                   1230:        ip->ip_p = IPPROTO_ESP;
                   1231:
                   1232:        /*
1.188     christos 1233:         * We have modified the packet - it is now ESP, so we should not
                   1234:         * return to UDP processing ...
                   1235:         *
1.136     manu     1236:         * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
                   1237:         * the source UDP port. This is required if we want
                   1238:         * to select the right SPD for multiple hosts behind
                   1239:         * same NAT
                   1240:         */
1.137     manu     1241:        if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
                   1242:            sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
                   1243:                printf("udp4_espinudp: m_tag_get failed\n");
1.188     christos 1244:                m_freem(m);
                   1245:                return -1;
1.137     manu     1246:        }
1.136     manu     1247:        ((u_int16_t *)(tag + 1))[0] = sport;
                   1248:        ((u_int16_t *)(tag + 1))[1] = dport;
1.188     christos 1249:        m_tag_prepend(m, tag);
1.136     manu     1250:
1.190     christos 1251: #ifdef IPSEC
1.188     christos 1252:        ipsec4_common_input(m, iphdrlen, IPPROTO_ESP);
1.147     christos 1253: #else
1.188     christos 1254:        esp4_input(m, iphdrlen);
1.147     christos 1255: #endif
1.130     manu     1256:
1.185     liamjfoy 1257:        /* We handled it, it shouldn't be handled by UDP */
1.188     christos 1258:        *mp = NULL; /* avoid free by caller ... */
1.130     manu     1259:        return 1;
                   1260: }
                   1261: #endif
1.190.2.2! rmind    1262:
        !          1263: PR_WRAP_USRREQ(udp_usrreq)
        !          1264:
        !          1265: #define        udp_usrreq      udp_usrreq_wrapper
        !          1266:
        !          1267: const struct pr_usrreqs udp_usrreqs = {
        !          1268:        .pr_attach      = udp_attach,
        !          1269:        .pr_detach      = udp_detach,
        !          1270:        .pr_generic     = udp_usrreq,
        !          1271: };

CVSweb <webmaster@jp.NetBSD.org>