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

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

CVSweb <webmaster@jp.NetBSD.org>