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

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

CVSweb <webmaster@jp.NetBSD.org>