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

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

CVSweb <webmaster@jp.NetBSD.org>