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

1.103.2.6! skrll       1: /*     $NetBSD: udp_usrreq.c,v 1.103.2.5 2004/12/18 09:33:06 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.103.2.2  skrll      44:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
1.44      thorpej    60:  *     @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
1.1       cgd        61:  */
1.91      lukem      62:
                     63: #include <sys/cdefs.h>
1.103.2.6! skrll      64: __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.103.2.5 2004/12/18 09:33:06 skrll Exp $");
1.50      thorpej    65:
1.77      soda       66: #include "opt_inet.h"
1.50      thorpej    67: #include "opt_ipsec.h"
1.78      thorpej    68: #include "opt_inet_csum.h"
1.64      ws         69: #include "opt_ipkdb.h"
1.101     martin     70: #include "opt_mbuftrace.h"
1.1       cgd        71:
1.5       mycroft    72: #include <sys/param.h>
                     73: #include <sys/malloc.h>
                     74: #include <sys/mbuf.h>
                     75: #include <sys/protosw.h>
                     76: #include <sys/socket.h>
                     77: #include <sys/socketvar.h>
1.13      mycroft    78: #include <sys/errno.h>
1.5       mycroft    79: #include <sys/stat.h>
1.27      christos   80: #include <sys/systm.h>
                     81: #include <sys/proc.h>
1.53      itojun     82: #include <sys/domain.h>
1.27      christos   83: #include <sys/sysctl.h>
1.1       cgd        84:
1.5       mycroft    85: #include <net/if.h>
                     86: #include <net/route.h>
1.1       cgd        87:
1.5       mycroft    88: #include <netinet/in.h>
                     89: #include <netinet/in_systm.h>
1.15      cgd        90: #include <netinet/in_var.h>
1.5       mycroft    91: #include <netinet/ip.h>
                     92: #include <netinet/in_pcb.h>
                     93: #include <netinet/ip_var.h>
                     94: #include <netinet/ip_icmp.h>
                     95: #include <netinet/udp.h>
                     96: #include <netinet/udp_var.h>
1.1       cgd        97:
1.53      itojun     98: #ifdef INET6
                     99: #include <netinet/ip6.h>
                    100: #include <netinet/icmp6.h>
                    101: #include <netinet6/ip6_var.h>
                    102: #include <netinet6/in6_pcb.h>
                    103: #include <netinet6/udp6_var.h>
                    104: #endif
                    105:
                    106: #ifndef INET6
                    107: /* always need ip6.h for IP6_EXTHDR_GET */
                    108: #include <netinet/ip6.h>
                    109: #endif
                    110:
1.76      itojun    111: #include "faith.h"
                    112: #if defined(NFAITH) && NFAITH > 0
                    113: #include <net/if_faith.h>
                    114: #endif
                    115:
1.27      christos  116: #include <machine/stdarg.h>
                    117:
1.103.2.2  skrll     118: #ifdef FAST_IPSEC
                    119: #include <netipsec/ipsec.h>
                    120: #include <netipsec/ipsec_var.h>                        /* XXX ipsecstat namespace */
                    121: #ifdef INET6
                    122: #include <netipsec/ipsec6.h>
                    123: #endif
                    124: #endif /* FAST_IPSEC*/
                    125:
1.48      itojun    126: #ifdef IPSEC
                    127: #include <netinet6/ipsec.h>
                    128: #include <netkey/key.h>
                    129: #endif /*IPSEC*/
                    130:
1.64      ws        131: #ifdef IPKDB
                    132: #include <ipkdb/ipkdb.h>
                    133: #endif
                    134:
1.8       mycroft   135: /*
                    136:  * UDP protocol implementation.
                    137:  * Per RFC 768, August, 1980.
                    138:  */
                    139: #ifndef        COMPAT_42
                    140: int    udpcksum = 1;
                    141: #else
                    142: int    udpcksum = 0;           /* XXX */
                    143: #endif
1.103.2.6! skrll     144: int    udp_do_loopback_cksum = 0;
1.93      matt      145:
                    146: struct inpcbtable udbtable;
                    147: struct udpstat udpstat;
1.8       mycroft   148:
1.72      itojun    149: #ifdef INET
1.103.2.2  skrll     150: static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
                    151:        struct socket *);
                    152: static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
                    153:        struct mbuf *, int);
1.103.2.6! skrll     154: static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.72      itojun    155: #endif
1.53      itojun    156: #ifdef INET6
1.103.2.2  skrll     157: static void udp6_sendup (struct mbuf *, int, struct sockaddr *,
                    158:        struct socket *);
                    159: static int udp6_realinput (int, struct sockaddr_in6 *,
                    160:        struct sockaddr_in6 *, struct mbuf *, int);
1.103.2.6! skrll     161: static int udp6_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.53      itojun    162: #endif
1.72      itojun    163: #ifdef INET
1.103.2.2  skrll     164: static void udp_notify (struct inpcb *, int);
1.72      itojun    165: #endif
1.7       mycroft   166:
1.26      mycroft   167: #ifndef UDBHASHSIZE
                    168: #define        UDBHASHSIZE     128
                    169: #endif
                    170: int    udbhashsize = UDBHASHSIZE;
                    171:
1.98      matt      172: #ifdef MBUFTRACE
                    173: struct mowner udp_mowner = { "udp" };
                    174: struct mowner udp_rx_mowner = { "udp", "rx" };
                    175: struct mowner udp_tx_mowner = { "udp", "tx" };
                    176: #endif
                    177:
1.78      thorpej   178: #ifdef UDP_CSUM_COUNTERS
                    179: #include <sys/device.h>
                    180:
                    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:
                    190: #define        UDP_CSUM_COUNTER_INCR(ev)       (ev)->ev_count++
                    191:
1.103.2.2  skrll     192: EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
                    193: EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
                    194: EVCNT_ATTACH_STATIC(udp_hwcsum_data);
                    195: EVCNT_ATTACH_STATIC(udp_swcsum);
                    196:
1.78      thorpej   197: #else
                    198:
                    199: #define        UDP_CSUM_COUNTER_INCR(ev)       /* nothing */
                    200:
                    201: #endif /* UDP_CSUM_COUNTERS */
                    202:
1.7       mycroft   203: void
1.103.2.2  skrll     204: udp_init(void)
1.1       cgd       205: {
1.18      mycroft   206:
1.35      mycroft   207:        in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.98      matt      208:
                    209:        MOWNER_ATTACH(&udp_tx_mowner);
                    210:        MOWNER_ATTACH(&udp_rx_mowner);
                    211:        MOWNER_ATTACH(&udp_mowner);
1.1       cgd       212: }
                    213:
1.103.2.6! skrll     214: /*
        !           215:  * Checksum extended UDP header and data.
        !           216:  */
        !           217:
        !           218: int
        !           219: udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
        !           220:     int iphlen, int len)
        !           221: {
        !           222:
        !           223:        switch (af) {
1.72      itojun    224: #ifdef INET
1.103.2.6! skrll     225:        case AF_INET:
        !           226:                return udp4_input_checksum(m, uh, iphlen, len);
        !           227: #endif
        !           228: #ifdef INET6
        !           229:        case AF_INET6:
        !           230:                return udp6_input_checksum(m, uh, iphlen, len);
        !           231: #endif
        !           232:        }
        !           233: #ifdef DIAGNOSTIC
        !           234:        panic("udp_input_checksum: unknown af %d", af);
        !           235: #endif
        !           236:        /* NOTREACHED */
        !           237:        return -1;
        !           238: }
        !           239:
        !           240: #ifdef INET
        !           241:
        !           242: /*
        !           243:  * Checksum extended UDP header and data.
        !           244:  */
        !           245:
        !           246: static int
        !           247: udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
        !           248:     int iphlen, int len)
        !           249: {
        !           250:
        !           251:        /*
        !           252:         * XXX it's better to record and check if this mbuf is
        !           253:         * already checked.
        !           254:         */
        !           255:
        !           256:        if (uh->uh_sum == 0)
        !           257:                return 0;
        !           258:
        !           259:        switch (m->m_pkthdr.csum_flags &
        !           260:            ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
        !           261:            M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
        !           262:        case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
        !           263:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
        !           264:                goto badcsum;
        !           265:
        !           266:        case M_CSUM_UDPv4|M_CSUM_DATA: {
        !           267:                u_int32_t hw_csum = m->m_pkthdr.csum_data;
        !           268:
        !           269:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
        !           270:                if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
        !           271:                        const struct ip *ip =
        !           272:                            mtod(m, const struct ip *);
        !           273:
        !           274:                        hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
        !           275:                            ip->ip_dst.s_addr,
        !           276:                            htons(hw_csum + len + IPPROTO_UDP));
        !           277:                }
        !           278:                if ((hw_csum ^ 0xffff) != 0)
        !           279:                        goto badcsum;
        !           280:                break;
        !           281:        }
        !           282:
        !           283:        case M_CSUM_UDPv4:
        !           284:                /* Checksum was okay. */
        !           285:                UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
        !           286:                break;
        !           287:
        !           288:        default:
        !           289:                /*
        !           290:                 * Need to compute it ourselves.  Maybe skip checksum
        !           291:                 * on loopback interfaces.
        !           292:                 */
        !           293:                if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
        !           294:                                     IFF_LOOPBACK) ||
        !           295:                                   udp_do_loopback_cksum)) {
        !           296:                        UDP_CSUM_COUNTER_INCR(&udp_swcsum);
        !           297:                        if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
        !           298:                                goto badcsum;
        !           299:                }
        !           300:                break;
        !           301:        }
        !           302:
        !           303:        return 0;
        !           304:
        !           305: badcsum:
        !           306:        udpstat.udps_badsum++;
        !           307:        return -1;
        !           308: }
        !           309:
1.7       mycroft   310: void
1.27      christos  311: udp_input(struct mbuf *m, ...)
1.1       cgd       312: {
1.53      itojun    313:        va_list ap;
                    314:        struct sockaddr_in src, dst;
                    315:        struct ip *ip;
                    316:        struct udphdr *uh;
1.97      simonb    317:        int iphlen;
1.53      itojun    318:        int len;
                    319:        int n;
1.96      itojun    320:        u_int16_t ip_len;
1.53      itojun    321:
                    322:        va_start(ap, m);
                    323:        iphlen = va_arg(ap, int);
1.97      simonb    324:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.53      itojun    325:        va_end(ap);
                    326:
1.98      matt      327:        MCLAIM(m, &udp_rx_mowner);
1.53      itojun    328:        udpstat.udps_ipackets++;
                    329:
                    330:        /*
                    331:         * Get IP and UDP header together in first mbuf.
                    332:         */
                    333:        ip = mtod(m, struct ip *);
                    334:        IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
                    335:        if (uh == NULL) {
                    336:                udpstat.udps_hdrops++;
                    337:                return;
                    338:        }
1.95      thorpej   339:        KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53      itojun    340:
1.57      itojun    341:        /* destination port of 0 is illegal, based on RFC768. */
                    342:        if (uh->uh_dport == 0)
                    343:                goto bad;
                    344:
1.53      itojun    345:        /*
                    346:         * Make mbuf data length reflect UDP length.
                    347:         * If not enough data to reflect UDP length, drop.
                    348:         */
1.96      itojun    349:        ip_len = ntohs(ip->ip_len);
1.53      itojun    350:        len = ntohs((u_int16_t)uh->uh_ulen);
1.96      itojun    351:        if (ip_len != iphlen + len) {
                    352:                if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.53      itojun    353:                        udpstat.udps_badlen++;
                    354:                        goto bad;
                    355:                }
1.96      itojun    356:                m_adj(m, iphlen + len - ip_len);
1.53      itojun    357:        }
                    358:
                    359:        /*
                    360:         * Checksum extended UDP header and data.
                    361:         */
1.103.2.6! skrll     362:        if (udp4_input_checksum(m, uh, iphlen, len))
        !           363:                goto badcsum;
1.53      itojun    364:
                    365:        /* construct source and dst sockaddrs. */
                    366:        bzero(&src, sizeof(src));
                    367:        src.sin_family = AF_INET;
                    368:        src.sin_len = sizeof(struct sockaddr_in);
                    369:        bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
                    370:        src.sin_port = uh->uh_sport;
                    371:        bzero(&dst, sizeof(dst));
                    372:        dst.sin_family = AF_INET;
                    373:        dst.sin_len = sizeof(struct sockaddr_in);
                    374:        bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
                    375:        dst.sin_port = uh->uh_dport;
                    376:
                    377:        n = udp4_realinput(&src, &dst, m, iphlen);
                    378: #ifdef INET6
                    379:        if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
                    380:                struct sockaddr_in6 src6, dst6;
                    381:
                    382:                bzero(&src6, sizeof(src6));
                    383:                src6.sin6_family = AF_INET6;
                    384:                src6.sin6_len = sizeof(struct sockaddr_in6);
                    385:                src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
                    386:                bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
                    387:                        sizeof(ip->ip_src));
                    388:                src6.sin6_port = uh->uh_sport;
                    389:                bzero(&dst6, sizeof(dst6));
                    390:                dst6.sin6_family = AF_INET6;
                    391:                dst6.sin6_len = sizeof(struct sockaddr_in6);
                    392:                dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
                    393:                bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
                    394:                        sizeof(ip->ip_dst));
                    395:                dst6.sin6_port = uh->uh_dport;
                    396:
                    397:                n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
                    398:        }
                    399: #endif
                    400:
                    401:        if (n == 0) {
                    402:                if (m->m_flags & (M_BCAST | M_MCAST)) {
                    403:                        udpstat.udps_noportbcast++;
                    404:                        goto bad;
                    405:                }
1.61      itojun    406:                udpstat.udps_noport++;
1.64      ws        407: #ifdef IPKDB
1.53      itojun    408:                if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
                    409:                                m, iphlen + sizeof(struct udphdr),
                    410:                                m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
                    411:                        /*
                    412:                         * It was a debugger connect packet,
                    413:                         * just drop it now
                    414:                         */
                    415:                        goto bad;
                    416:                }
                    417: #endif
                    418:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
                    419:                m = NULL;
                    420:        }
                    421:
                    422: bad:
                    423:        if (m)
                    424:                m_freem(m);
1.78      thorpej   425:        return;
                    426:
                    427: badcsum:
                    428:        m_freem(m);
1.53      itojun    429: }
1.72      itojun    430: #endif
1.53      itojun    431:
                    432: #ifdef INET6
1.103.2.6! skrll     433: static int
        !           434: udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
        !           435: {
        !           436:
        !           437:        if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
        !           438:                goto good;
        !           439:        }
        !           440:        if (uh->uh_sum == 0) {
        !           441:                udp6stat.udp6s_nosum++;
        !           442:                goto bad;
        !           443:        }
        !           444:        if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
        !           445:                udp6stat.udp6s_badsum++;
        !           446:                goto bad;
        !           447:        }
        !           448:
        !           449: good:
        !           450:        return 0;
        !           451: bad:
        !           452:        return -1;
        !           453: }
        !           454:
1.53      itojun    455: int
1.103.2.2  skrll     456: udp6_input(struct mbuf **mp, int *offp, int proto)
1.53      itojun    457: {
                    458:        struct mbuf *m = *mp;
                    459:        int off = *offp;
                    460:        struct sockaddr_in6 src, dst;
                    461:        struct ip6_hdr *ip6;
                    462:        struct udphdr *uh;
                    463:        u_int32_t plen, ulen;
                    464:
1.76      itojun    465:        ip6 = mtod(m, struct ip6_hdr *);
                    466:
1.53      itojun    467: #if defined(NFAITH) && 0 < NFAITH
1.76      itojun    468:        if (faithprefix(&ip6->ip6_dst)) {
                    469:                /* send icmp6 host unreach? */
                    470:                m_freem(m);
                    471:                return IPPROTO_DONE;
1.53      itojun    472:        }
                    473: #endif
                    474:
                    475:        udp6stat.udp6s_ipackets++;
                    476:
                    477:        /* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
                    478:        plen = m->m_pkthdr.len - off;
                    479:        IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
                    480:        if (uh == NULL) {
                    481:                ip6stat.ip6s_tooshort++;
                    482:                return IPPROTO_DONE;
                    483:        }
1.95      thorpej   484:        KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53      itojun    485:        ulen = ntohs((u_short)uh->uh_ulen);
1.69      itojun    486:        /*
                    487:         * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
                    488:         * iff payload length > 0xffff.
                    489:         */
1.53      itojun    490:        if (ulen == 0 && plen > 0xffff)
                    491:                ulen = plen;
                    492:
                    493:        if (plen != ulen) {
                    494:                udp6stat.udp6s_badlen++;
1.54      itojun    495:                goto bad;
                    496:        }
                    497:
1.57      itojun    498:        /* destination port of 0 is illegal, based on RFC768. */
                    499:        if (uh->uh_dport == 0)
                    500:                goto bad;
                    501:
1.54      itojun    502:        /* Be proactive about malicious use of IPv4 mapped address */
                    503:        if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
                    504:            IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
                    505:                /* XXX stat */
1.53      itojun    506:                goto bad;
                    507:        }
                    508:
                    509:        /*
1.103.2.5  skrll     510:         * Checksum extended UDP header and data.  Maybe skip checksum
                    511:         * on loopback interfaces.
1.53      itojun    512:         */
1.103.2.6! skrll     513:        if (udp6_input_checksum(m, uh, off, ulen))
        !           514:                goto bad;
1.53      itojun    515:
                    516:        /*
                    517:         * Construct source and dst sockaddrs.
                    518:         * Note that ifindex (s6_addr16[1]) is already filled.
                    519:         */
                    520:        bzero(&src, sizeof(src));
                    521:        src.sin6_family = AF_INET6;
                    522:        src.sin6_len = sizeof(struct sockaddr_in6);
1.69      itojun    523:        /* KAME hack: recover scopeid */
                    524:        (void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1.53      itojun    525:        src.sin6_port = uh->uh_sport;
                    526:        bzero(&dst, sizeof(dst));
                    527:        dst.sin6_family = AF_INET6;
                    528:        dst.sin6_len = sizeof(struct sockaddr_in6);
1.69      itojun    529:        /* KAME hack: recover scopeid */
                    530:        (void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif);
1.53      itojun    531:        dst.sin6_port = uh->uh_dport;
                    532:
                    533:        if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
                    534:                if (m->m_flags & M_MCAST) {
                    535:                        udp6stat.udp6s_noportmcast++;
                    536:                        goto bad;
                    537:                }
1.61      itojun    538:                udp6stat.udp6s_noport++;
1.53      itojun    539:                icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
                    540:                m = NULL;
                    541:        }
                    542:
                    543: bad:
                    544:        if (m)
                    545:                m_freem(m);
                    546:        return IPPROTO_DONE;
                    547: }
                    548: #endif
                    549:
1.72      itojun    550: #ifdef INET
1.53      itojun    551: static void
1.103.2.2  skrll     552: udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
                    553:        struct sockaddr *src, struct socket *so)
1.53      itojun    554: {
                    555:        struct mbuf *opts = NULL;
                    556:        struct mbuf *n;
                    557:        struct inpcb *inp = NULL;
                    558:
                    559:        if (!so)
                    560:                return;
                    561:        switch (so->so_proto->pr_domain->dom_family) {
                    562:        case AF_INET:
                    563:                inp = sotoinpcb(so);
                    564:                break;
                    565: #ifdef INET6
                    566:        case AF_INET6:
                    567:                break;
                    568: #endif
                    569:        default:
                    570:                return;
                    571:        }
                    572:
1.103.2.2  skrll     573: #if defined(IPSEC) || defined(FAST_IPSEC)
1.53      itojun    574:        /* check AH/ESP integrity. */
                    575:        if (so != NULL && ipsec4_in_reject_so(m, so)) {
                    576:                ipsecstat.in_polvio++;
1.103.2.2  skrll     577:                if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
                    578:                        icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
                    579:                            0, 0);
1.53      itojun    580:                return;
                    581:        }
                    582: #endif /*IPSEC*/
                    583:
                    584:        if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
                    585:                if (inp && (inp->inp_flags & INP_CONTROLOPTS
                    586:                         || so->so_options & SO_TIMESTAMP)) {
                    587:                        struct ip *ip = mtod(n, struct ip *);
                    588:                        ip_savecontrol(inp, &opts, ip, n);
                    589:                }
                    590:
                    591:                m_adj(n, off);
                    592:                if (sbappendaddr(&so->so_rcv, src, n,
                    593:                                opts) == 0) {
                    594:                        m_freem(n);
                    595:                        if (opts)
                    596:                                m_freem(opts);
1.103.2.3  skrll     597:                        so->so_rcv.sb_overflowed++;
1.81      enami     598:                        udpstat.udps_fullsock++;
1.53      itojun    599:                } else
                    600:                        sorwakeup(so);
                    601:        }
                    602: }
1.72      itojun    603: #endif
1.53      itojun    604:
                    605: #ifdef INET6
                    606: static void
1.103.2.2  skrll     607: udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
                    608:        struct sockaddr *src, struct socket *so)
1.53      itojun    609: {
                    610:        struct mbuf *opts = NULL;
                    611:        struct mbuf *n;
                    612:        struct in6pcb *in6p = NULL;
                    613:
                    614:        if (!so)
                    615:                return;
                    616:        if (so->so_proto->pr_domain->dom_family != AF_INET6)
                    617:                return;
                    618:        in6p = sotoin6pcb(so);
                    619:
1.103.2.2  skrll     620: #if defined(IPSEC) || defined(FAST_IPSEC)
1.53      itojun    621:        /* check AH/ESP integrity. */
                    622:        if (so != NULL && ipsec6_in_reject_so(m, so)) {
                    623:                ipsec6stat.in_polvio++;
1.103.2.2  skrll     624:                if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
                    625:                        icmp6_error(n, ICMP6_DST_UNREACH,
                    626:                            ICMP6_DST_UNREACH_ADMIN, 0);
1.53      itojun    627:                return;
                    628:        }
                    629: #endif /*IPSEC*/
                    630:
                    631:        if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
                    632:                if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
                    633:                          || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
                    634:                        struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
                    635:                        ip6_savecontrol(in6p, &opts, ip6, n);
                    636:                }
                    637:
                    638:                m_adj(n, off);
                    639:                if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
                    640:                        m_freem(n);
                    641:                        if (opts)
                    642:                                m_freem(opts);
1.103.2.3  skrll     643:                        so->so_rcv.sb_overflowed++;
1.53      itojun    644:                        udp6stat.udp6s_fullsock++;
                    645:                } else
                    646:                        sorwakeup(so);
                    647:        }
                    648: }
                    649: #endif
                    650:
1.72      itojun    651: #ifdef INET
1.53      itojun    652: static int
1.103.2.2  skrll     653: udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
                    654:        struct mbuf *m, int off /* offset of udphdr */)
1.53      itojun    655: {
                    656:        u_int16_t *sport, *dport;
                    657:        int rcvcnt;
                    658:        struct in_addr *src4, *dst4;
1.103.2.2  skrll     659:        struct inpcb_hdr *inph;
1.53      itojun    660:        struct inpcb *inp;
                    661:
                    662:        rcvcnt = 0;
                    663:        off += sizeof(struct udphdr);   /* now, offset of payload */
                    664:
                    665:        if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
                    666:                goto bad;
                    667:
                    668:        src4 = &src->sin_addr;
                    669:        sport = &src->sin_port;
                    670:        dst4 = &dst->sin_addr;
                    671:        dport = &dst->sin_port;
                    672:
1.73      itojun    673:        if (IN_MULTICAST(dst4->s_addr) ||
1.53      itojun    674:            in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
                    675:                /*
                    676:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    677:                 * for which the local and remote addresses and ports match
                    678:                 * those of the incoming datagram.  This allows more than
                    679:                 * one process to receive multi/broadcasts on the same port.
                    680:                 * (This really ought to be done for unicast datagrams as
                    681:                 * well, but that would cause problems with existing
                    682:                 * applications that open both address-specific sockets and
                    683:                 * a wildcard socket listening to the same port -- they would
                    684:                 * end up receiving duplicates of every unicast datagram.
                    685:                 * Those applications open the multiple sockets to overcome an
                    686:                 * inadequacy of the UDP socket interface, but for backwards
                    687:                 * compatibility we avoid the problem here rather than
                    688:                 * fixing the interface.  Maybe 4.5BSD will remedy this?)
                    689:                 */
                    690:
                    691:                /*
1.92      itojun    692:                 * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71      itojun    693:                 * we need udpiphdr for IPsec processing so we do that later.
1.53      itojun    694:                 */
                    695:                /*
                    696:                 * Locate pcb(s) for datagram.
                    697:                 */
1.103.2.2  skrll     698:                CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
                    699:                        inp = (struct inpcb *)inph;
                    700:                        if (inp->inp_af != AF_INET)
                    701:                                continue;
                    702:
1.53      itojun    703:                        if (inp->inp_lport != *dport)
                    704:                                continue;
                    705:                        if (!in_nullhost(inp->inp_laddr)) {
                    706:                                if (!in_hosteq(inp->inp_laddr, *dst4))
                    707:                                        continue;
                    708:                        }
                    709:                        if (!in_nullhost(inp->inp_faddr)) {
                    710:                                if (!in_hosteq(inp->inp_faddr, *src4) ||
                    711:                                    inp->inp_fport != *sport)
                    712:                                        continue;
                    713:                        }
                    714:
                    715:                        udp4_sendup(m, off, (struct sockaddr *)src,
                    716:                                inp->inp_socket);
                    717:                        rcvcnt++;
                    718:
                    719:                        /*
                    720:                         * Don't look for additional matches if this one does
                    721:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    722:                         * socket options set.  This heuristic avoids searching
                    723:                         * through all pcbs in the common case of a non-shared
                    724:                         * port.  It assumes that an application will never
                    725:                         * clear these options after setting them.
                    726:                         */
                    727:                        if ((inp->inp_socket->so_options &
                    728:                            (SO_REUSEPORT|SO_REUSEADDR)) == 0)
                    729:                                break;
                    730:                }
                    731:        } else {
                    732:                /*
                    733:                 * Locate pcb for datagram.
                    734:                 */
                    735:                inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
                    736:                if (inp == 0) {
                    737:                        ++udpstat.udps_pcbhashmiss;
                    738:                        inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
1.82      itojun    739:                        if (inp == 0)
1.53      itojun    740:                                return rcvcnt;
                    741:                }
                    742:
                    743:                udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
                    744:                rcvcnt++;
                    745:        }
                    746:
                    747: bad:
                    748:        return rcvcnt;
                    749: }
1.72      itojun    750: #endif
1.53      itojun    751:
                    752: #ifdef INET6
                    753: static int
1.103.2.2  skrll     754: udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
                    755:        struct mbuf *m, int off)
1.53      itojun    756: {
1.79      itojun    757:        u_int16_t sport, dport;
1.53      itojun    758:        int rcvcnt;
1.79      itojun    759:        struct in6_addr src6, dst6;
                    760:        const struct in_addr *dst4;
1.103.2.2  skrll     761:        struct inpcb_hdr *inph;
1.53      itojun    762:        struct in6pcb *in6p;
                    763:
                    764:        rcvcnt = 0;
                    765:        off += sizeof(struct udphdr);   /* now, offset of payload */
                    766:
                    767:        if (af != AF_INET && af != AF_INET6)
                    768:                goto bad;
                    769:        if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
                    770:                goto bad;
                    771:
1.79      itojun    772:        in6_embedscope(&src6, src, NULL, NULL);
                    773:        sport = src->sin6_port;
                    774:        in6_embedscope(&dst6, dst, NULL, NULL);
                    775:        dport = dst->sin6_port;
1.88      itojun    776:        dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
1.53      itojun    777:
1.79      itojun    778:        if (IN6_IS_ADDR_MULTICAST(&dst6) ||
                    779:            (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
1.53      itojun    780:                /*
                    781:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    782:                 * for which the local and remote addresses and ports match
                    783:                 * those of the incoming datagram.  This allows more than
                    784:                 * one process to receive multi/broadcasts on the same port.
                    785:                 * (This really ought to be done for unicast datagrams as
                    786:                 * well, but that would cause problems with existing
                    787:                 * applications that open both address-specific sockets and
                    788:                 * a wildcard socket listening to the same port -- they would
                    789:                 * end up receiving duplicates of every unicast datagram.
                    790:                 * Those applications open the multiple sockets to overcome an
                    791:                 * inadequacy of the UDP socket interface, but for backwards
                    792:                 * compatibility we avoid the problem here rather than
                    793:                 * fixing the interface.  Maybe 4.5BSD will remedy this?)
                    794:                 */
                    795:
                    796:                /*
1.92      itojun    797:                 * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71      itojun    798:                 * we need udpiphdr for IPsec processing so we do that later.
1.53      itojun    799:                 */
                    800:                /*
                    801:                 * Locate pcb(s) for datagram.
                    802:                 */
1.103.2.2  skrll     803:                CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
                    804:                        in6p = (struct in6pcb *)inph;
                    805:                        if (in6p->in6p_af != AF_INET6)
                    806:                                continue;
                    807:
1.79      itojun    808:                        if (in6p->in6p_lport != dport)
1.53      itojun    809:                                continue;
                    810:                        if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1.90      itojun    811:                                if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6))
1.53      itojun    812:                                        continue;
1.92      itojun    813:                        } else {
1.79      itojun    814:                                if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
1.85      itojun    815:                                    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55      itojun    816:                                        continue;
                    817:                        }
1.53      itojun    818:                        if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1.79      itojun    819:                                if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
                    820:                                    &src6) || in6p->in6p_fport != sport)
1.53      itojun    821:                                        continue;
1.92      itojun    822:                        } else {
1.79      itojun    823:                                if (IN6_IS_ADDR_V4MAPPED(&src6) &&
1.85      itojun    824:                                    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55      itojun    825:                                        continue;
                    826:                        }
1.53      itojun    827:
                    828:                        udp6_sendup(m, off, (struct sockaddr *)src,
                    829:                                in6p->in6p_socket);
                    830:                        rcvcnt++;
                    831:
                    832:                        /*
                    833:                         * Don't look for additional matches if this one does
                    834:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    835:                         * socket options set.  This heuristic avoids searching
                    836:                         * through all pcbs in the common case of a non-shared
                    837:                         * port.  It assumes that an application will never
                    838:                         * clear these options after setting them.
                    839:                         */
                    840:                        if ((in6p->in6p_socket->so_options &
                    841:                            (SO_REUSEPORT|SO_REUSEADDR)) == 0)
                    842:                                break;
                    843:                }
                    844:        } else {
                    845:                /*
                    846:                 * Locate pcb for datagram.
                    847:                 */
1.103.2.2  skrll     848:                in6p = in6_pcblookup_connect(&udbtable, &src6, sport,
1.79      itojun    849:                    &dst6, dport, 0);
1.53      itojun    850:                if (in6p == 0) {
                    851:                        ++udpstat.udps_pcbhashmiss;
1.103.2.2  skrll     852:                        in6p = in6_pcblookup_bind(&udbtable, &dst6, dport, 0);
1.82      itojun    853:                        if (in6p == 0)
1.53      itojun    854:                                return rcvcnt;
                    855:                }
                    856:
                    857:                udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
                    858:                rcvcnt++;
                    859:        }
                    860:
                    861: bad:
                    862:        return rcvcnt;
                    863: }
                    864: #endif
1.1       cgd       865:
1.72      itojun    866: #ifdef INET
1.1       cgd       867: /*
                    868:  * Notify a udp user of an asynchronous error;
                    869:  * just wake up so that he can collect error status.
                    870:  */
1.7       mycroft   871: static void
1.103.2.2  skrll     872: udp_notify(struct inpcb *inp, int errno)
1.1       cgd       873: {
                    874:        inp->inp_socket->so_error = errno;
                    875:        sorwakeup(inp->inp_socket);
                    876:        sowwakeup(inp->inp_socket);
                    877: }
                    878:
1.27      christos  879: void *
1.103.2.2  skrll     880: udp_ctlinput(int cmd, struct sockaddr *sa, void *v)
1.1       cgd       881: {
1.66      augustss  882:        struct ip *ip = v;
                    883:        struct udphdr *uh;
1.103.2.2  skrll     884:        void (*notify)(struct inpcb *, int) = udp_notify;
1.21      mycroft   885:        int errno;
1.1       cgd       886:
1.53      itojun    887:        if (sa->sa_family != AF_INET
                    888:         || sa->sa_len != sizeof(struct sockaddr_in))
1.51      itojun    889:                return NULL;
1.20      mycroft   890:        if ((unsigned)cmd >= PRC_NCMDS)
1.27      christos  891:                return NULL;
1.20      mycroft   892:        errno = inetctlerrmap[cmd];
1.18      mycroft   893:        if (PRC_IS_REDIRECT(cmd))
1.19      mycroft   894:                notify = in_rtchange, ip = 0;
1.18      mycroft   895:        else if (cmd == PRC_HOSTDEAD)
1.19      mycroft   896:                ip = 0;
1.23      cgd       897:        else if (errno == 0)
1.27      christos  898:                return NULL;
1.19      mycroft   899:        if (ip) {
1.1       cgd       900:                uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34      mycroft   901:                in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
                    902:                    ip->ip_src, uh->uh_sport, errno, notify);
1.53      itojun    903:
                    904:                /* XXX mapped address case */
1.19      mycroft   905:        } else
1.34      mycroft   906:                in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
                    907:                    notify);
1.27      christos  908:        return NULL;
1.1       cgd       909: }
                    910:
1.7       mycroft   911: int
1.27      christos  912: udp_output(struct mbuf *m, ...)
                    913: {
1.66      augustss  914:        struct inpcb *inp;
                    915:        struct udpiphdr *ui;
1.103.2.5  skrll     916:        struct route *ro;
1.66      augustss  917:        int len = m->m_pkthdr.len;
1.31      mycroft   918:        int error = 0;
1.27      christos  919:        va_list ap;
                    920:
1.98      matt      921:        MCLAIM(m, &udp_tx_mowner);
1.27      christos  922:        va_start(ap, m);
                    923:        inp = va_arg(ap, struct inpcb *);
                    924:        va_end(ap);
1.1       cgd       925:
                    926:        /*
                    927:         * Calculate data length and get a mbuf
                    928:         * for UDP and IP headers.
                    929:         */
1.13      mycroft   930:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
                    931:        if (m == 0) {
                    932:                error = ENOBUFS;
1.39      thorpej   933:                goto release;
                    934:        }
                    935:
                    936:        /*
                    937:         * Compute the packet length of the IP header, and
                    938:         * punt if the length looks bogus.
                    939:         */
1.96      itojun    940:        if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39      thorpej   941:                error = EMSGSIZE;
1.13      mycroft   942:                goto release;
                    943:        }
1.1       cgd       944:
                    945:        /*
                    946:         * Fill in mbuf with extended UDP header
                    947:         * and addresses and length put into network format.
                    948:         */
                    949:        ui = mtod(m, struct udpiphdr *);
                    950:        ui->ui_pr = IPPROTO_UDP;
                    951:        ui->ui_src = inp->inp_laddr;
                    952:        ui->ui_dst = inp->inp_faddr;
                    953:        ui->ui_sport = inp->inp_lport;
                    954:        ui->ui_dport = inp->inp_fport;
1.78      thorpej   955:        ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1       cgd       956:
1.103.2.5  skrll     957:        ro = &inp->inp_route;
                    958:
1.1       cgd       959:        /*
1.78      thorpej   960:         * Set up checksum and output datagram.
1.1       cgd       961:         */
                    962:        if (udpcksum) {
1.78      thorpej   963:                /*
                    964:                 * XXX Cache pseudo-header checksum part for
                    965:                 * XXX "connected" UDP sockets.
1.103.2.5  skrll     966:                 * Maybe skip checksums on loopback interfaces.
1.78      thorpej   967:                 */
                    968:                ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
                    969:                    ui->ui_dst.s_addr, htons((u_int16_t)len +
                    970:                    sizeof(struct udphdr) + IPPROTO_UDP));
1.103.2.5  skrll     971:                if (__predict_true(ro->ro_rt == NULL ||
                    972:                                   !(ro->ro_rt->rt_ifp->if_flags &
                    973:                                     IFF_LOOPBACK) ||
                    974:                                   udp_do_loopback_cksum))
                    975:                        m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
                    976:                else
                    977:                        m->m_pkthdr.csum_flags = 0;
1.78      thorpej   978:                m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
                    979:        } else
                    980:                ui->ui_sum = 0;
1.96      itojun    981:        ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1.1       cgd       982:        ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
                    983:        ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
                    984:        udpstat.udps_opackets++;
1.48      itojun    985:
1.103.2.5  skrll     986:        return (ip_output(m, inp->inp_options, ro,
1.12      mycroft   987:            inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.103.2.2  skrll     988:            inp->inp_moptions, inp->inp_socket));
1.1       cgd       989:
                    990: release:
                    991:        m_freem(m);
                    992:        return (error);
                    993: }
                    994:
1.42      thorpej   995: int    udp_sendspace = 9216;           /* really max datagram size */
                    996: int    udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1       cgd       997:                                        /* 40 1K datagrams */
                    998:
                    999: /*ARGSUSED*/
1.7       mycroft  1000: int
1.103.2.2  skrll    1001: udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1.103.2.4  skrll    1002:        struct mbuf *control, struct lwp *l)
1.1       cgd      1003: {
1.66      augustss 1004:        struct inpcb *inp;
1.103.2.4  skrll    1005:        struct proc *p;
1.1       cgd      1006:        int s;
1.66      augustss 1007:        int error = 0;
1.1       cgd      1008:
1.103.2.4  skrll    1009:        p = l ? l->l_proc : NULL;
1.1       cgd      1010:        if (req == PRU_CONTROL)
1.31      mycroft  1011:                return (in_control(so, (long)m, (caddr_t)nam,
1.30      mycroft  1012:                    (struct ifnet *)control, p));
1.59      thorpej  1013:
1.60      thorpej  1014:        if (req == PRU_PURGEIF) {
1.80      itojun   1015:                in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
1.60      thorpej  1016:                in_purgeif((struct ifnet *)control);
                   1017:                in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1.59      thorpej  1018:                return (0);
                   1019:        }
1.31      mycroft  1020:
                   1021:        s = splsoftnet();
                   1022:        inp = sotoinpcb(so);
1.32      mycroft  1023: #ifdef DIAGNOSTIC
                   1024:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                   1025:                panic("udp_usrreq: unexpected control mbuf");
                   1026: #endif
1.31      mycroft  1027:        if (inp == 0 && req != PRU_ATTACH) {
                   1028:                error = EINVAL;
                   1029:                goto release;
                   1030:        }
                   1031:
1.1       cgd      1032:        /*
                   1033:         * Note: need to block udp_input while changing
                   1034:         * the udp pcb queue and/or pcb addresses.
                   1035:         */
                   1036:        switch (req) {
                   1037:
                   1038:        case PRU_ATTACH:
1.31      mycroft  1039:                if (inp != 0) {
                   1040:                        error = EISCONN;
1.1       cgd      1041:                        break;
                   1042:                }
1.98      matt     1043: #ifdef MBUFTRACE
                   1044:                so->so_mowner = &udp_mowner;
                   1045:                so->so_rcv.sb_mowner = &udp_rx_mowner;
                   1046:                so->so_snd.sb_mowner = &udp_tx_mowner;
                   1047: #endif
1.31      mycroft  1048:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                   1049:                        error = soreserve(so, udp_sendspace, udp_recvspace);
                   1050:                        if (error)
                   1051:                                break;
                   1052:                }
1.18      mycroft  1053:                error = in_pcballoc(so, &udbtable);
1.1       cgd      1054:                if (error)
                   1055:                        break;
1.31      mycroft  1056:                inp = sotoinpcb(so);
                   1057:                inp->inp_ip.ip_ttl = ip_defttl;
1.1       cgd      1058:                break;
                   1059:
                   1060:        case PRU_DETACH:
1.31      mycroft  1061:                in_pcbdetach(inp);
1.1       cgd      1062:                break;
                   1063:
                   1064:        case PRU_BIND:
1.31      mycroft  1065:                error = in_pcbbind(inp, nam, p);
1.1       cgd      1066:                break;
                   1067:
                   1068:        case PRU_LISTEN:
                   1069:                error = EOPNOTSUPP;
                   1070:                break;
                   1071:
                   1072:        case PRU_CONNECT:
1.31      mycroft  1073:                error = in_pcbconnect(inp, nam);
                   1074:                if (error)
1.1       cgd      1075:                        break;
1.31      mycroft  1076:                soisconnected(so);
1.1       cgd      1077:                break;
                   1078:
                   1079:        case PRU_CONNECT2:
                   1080:                error = EOPNOTSUPP;
                   1081:                break;
                   1082:
                   1083:        case PRU_DISCONNECT:
1.31      mycroft  1084:                /*soisdisconnected(so);*/
                   1085:                so->so_state &= ~SS_ISCONNECTED;        /* XXX */
1.1       cgd      1086:                in_pcbdisconnect(inp);
1.34      mycroft  1087:                inp->inp_laddr = zeroin_addr;           /* XXX */
1.35      mycroft  1088:                in_pcbstate(inp, INP_BOUND);            /* XXX */
1.1       cgd      1089:                break;
                   1090:
                   1091:        case PRU_SHUTDOWN:
                   1092:                socantsendmore(so);
                   1093:                break;
                   1094:
1.31      mycroft  1095:        case PRU_RCVD:
                   1096:                error = EOPNOTSUPP;
1.1       cgd      1097:                break;
                   1098:
1.31      mycroft  1099:        case PRU_SEND:
1.32      mycroft  1100:                if (control && control->m_len) {
                   1101:                        m_freem(control);
                   1102:                        m_freem(m);
                   1103:                        error = EINVAL;
                   1104:                        break;
                   1105:                }
1.31      mycroft  1106:        {
1.35      mycroft  1107:                struct in_addr laddr;                   /* XXX */
1.1       cgd      1108:
1.31      mycroft  1109:                if (nam) {
1.35      mycroft  1110:                        laddr = inp->inp_laddr;         /* XXX */
1.31      mycroft  1111:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
                   1112:                                error = EISCONN;
1.32      mycroft  1113:                                goto die;
1.31      mycroft  1114:                        }
                   1115:                        error = in_pcbconnect(inp, nam);
1.100     matt     1116:                        if (error)
                   1117:                                goto die;
1.31      mycroft  1118:                } else {
                   1119:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
                   1120:                                error = ENOTCONN;
1.32      mycroft  1121:                                goto die;
1.31      mycroft  1122:                        }
                   1123:                }
1.33      mycroft  1124:                error = udp_output(m, inp);
1.100     matt     1125:                m = NULL;
1.31      mycroft  1126:                if (nam) {
                   1127:                        in_pcbdisconnect(inp);
1.35      mycroft  1128:                        inp->inp_laddr = laddr;         /* XXX */
                   1129:                        in_pcbstate(inp, INP_BOUND);    /* XXX */
1.31      mycroft  1130:                }
1.100     matt     1131:          die:
                   1132:                if (m)
                   1133:                        m_freem(m);
1.31      mycroft  1134:        }
1.1       cgd      1135:                break;
                   1136:
                   1137:        case PRU_SENSE:
                   1138:                /*
                   1139:                 * stat: don't bother with a blocksize.
                   1140:                 */
1.31      mycroft  1141:                splx(s);
1.1       cgd      1142:                return (0);
                   1143:
1.31      mycroft  1144:        case PRU_RCVOOB:
                   1145:                error =  EOPNOTSUPP;
                   1146:                break;
                   1147:
1.1       cgd      1148:        case PRU_SENDOOB:
1.32      mycroft  1149:                m_freem(control);
1.31      mycroft  1150:                m_freem(m);
1.1       cgd      1151:                error =  EOPNOTSUPP;
                   1152:                break;
                   1153:
1.31      mycroft  1154:        case PRU_SOCKADDR:
                   1155:                in_setsockaddr(inp, nam);
                   1156:                break;
                   1157:
                   1158:        case PRU_PEERADDR:
                   1159:                in_setpeeraddr(inp, nam);
                   1160:                break;
1.1       cgd      1161:
                   1162:        default:
                   1163:                panic("udp_usrreq");
                   1164:        }
                   1165:
                   1166: release:
1.31      mycroft  1167:        splx(s);
1.1       cgd      1168:        return (error);
1.13      mycroft  1169: }
                   1170:
                   1171: /*
                   1172:  * Sysctl for udp variables.
                   1173:  */
1.103.2.2  skrll    1174: SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup")
1.13      mycroft  1175: {
1.103.2.2  skrll    1176:
                   1177:        sysctl_createv(clog, 0, NULL, NULL,
                   1178:                       CTLFLAG_PERMANENT,
                   1179:                       CTLTYPE_NODE, "net", NULL,
                   1180:                       NULL, 0, NULL, 0,
                   1181:                       CTL_NET, CTL_EOL);
                   1182:        sysctl_createv(clog, 0, NULL, NULL,
                   1183:                       CTLFLAG_PERMANENT,
                   1184:                       CTLTYPE_NODE, "inet", NULL,
                   1185:                       NULL, 0, NULL, 0,
                   1186:                       CTL_NET, PF_INET, CTL_EOL);
                   1187:        sysctl_createv(clog, 0, NULL, NULL,
                   1188:                       CTLFLAG_PERMANENT,
                   1189:                       CTLTYPE_NODE, "udp",
                   1190:                       SYSCTL_DESCR("UDPv4 related settings"),
                   1191:                       NULL, 0, NULL, 0,
                   1192:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
                   1193:
                   1194:        sysctl_createv(clog, 0, NULL, NULL,
                   1195:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1196:                       CTLTYPE_INT, "checksum",
                   1197:                       SYSCTL_DESCR("Compute UDP checksums"),
                   1198:                       NULL, 0, &udpcksum, 0,
                   1199:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
                   1200:                       CTL_EOL);
                   1201:        sysctl_createv(clog, 0, NULL, NULL,
                   1202:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1203:                       CTLTYPE_INT, "sendspace",
                   1204:                       SYSCTL_DESCR("Default UDP send buffer size"),
                   1205:                       NULL, 0, &udp_sendspace, 0,
                   1206:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
                   1207:                       CTL_EOL);
                   1208:        sysctl_createv(clog, 0, NULL, NULL,
                   1209:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1210:                       CTLTYPE_INT, "recvspace",
                   1211:                       SYSCTL_DESCR("Default UDP receive buffer size"),
                   1212:                       NULL, 0, &udp_recvspace, 0,
                   1213:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
                   1214:                       CTL_EOL);
1.103.2.5  skrll    1215:        sysctl_createv(clog, 0, NULL, NULL,
                   1216:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1217:                       CTLTYPE_INT, "do_loopback_cksum",
                   1218:                       SYSCTL_DESCR("Perform UDP checksum on loopback"),
                   1219:                       NULL, 0, &udp_do_loopback_cksum, 0,
                   1220:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
                   1221:                       CTL_EOL);
1.1       cgd      1222: }
1.72      itojun   1223: #endif

CVSweb <webmaster@jp.NetBSD.org>