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

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

CVSweb <webmaster@jp.NetBSD.org>