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

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

CVSweb <webmaster@jp.NetBSD.org>