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

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

CVSweb <webmaster@jp.NetBSD.org>