[BACK]Return to udp_usrreq.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet

Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.103.2.4

1.103.2.3  skrll       1: /*     $NetBSD: udp_usrreq.c,v 1.103.2.2 2004/08/03 10:54:46 skrll Exp $       */
1.48      itojun      2:
                      3: /*
                      4:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      5:  * All rights reserved.
1.94      itojun      6:  *
1.48      itojun      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. Neither the name of the project nor the names of its contributors
                     16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
1.94      itojun     18:  *
1.48      itojun     19:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
1.14      cgd        31:
1.1       cgd        32: /*
1.44      thorpej    33:  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
1.13      mycroft    34:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        35:  *
                     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
1.103.2.2  skrll      44:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
1.44      thorpej    60:  *     @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
1.1       cgd        61:  */
1.91      lukem      62:
                     63: #include <sys/cdefs.h>
1.103.2.3  skrll      64: __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.103.2.2 2004/08/03 10:54:46 skrll Exp $");
1.50      thorpej    65:
1.77      soda       66: #include "opt_inet.h"
1.50      thorpej    67: #include "opt_ipsec.h"
1.78      thorpej    68: #include "opt_inet_csum.h"
1.64      ws         69: #include "opt_ipkdb.h"
1.101     martin     70: #include "opt_mbuftrace.h"
1.1       cgd        71:
1.5       mycroft    72: #include <sys/param.h>
                     73: #include <sys/malloc.h>
                     74: #include <sys/mbuf.h>
                     75: #include <sys/protosw.h>
                     76: #include <sys/socket.h>
                     77: #include <sys/socketvar.h>
1.13      mycroft    78: #include <sys/errno.h>
1.5       mycroft    79: #include <sys/stat.h>
1.27      christos   80: #include <sys/systm.h>
                     81: #include <sys/proc.h>
1.53      itojun     82: #include <sys/domain.h>
1.27      christos   83: #include <sys/sysctl.h>
1.1       cgd        84:
1.5       mycroft    85: #include <net/if.h>
                     86: #include <net/route.h>
1.1       cgd        87:
1.5       mycroft    88: #include <netinet/in.h>
                     89: #include <netinet/in_systm.h>
1.15      cgd        90: #include <netinet/in_var.h>
1.5       mycroft    91: #include <netinet/ip.h>
                     92: #include <netinet/in_pcb.h>
                     93: #include <netinet/ip_var.h>
                     94: #include <netinet/ip_icmp.h>
                     95: #include <netinet/udp.h>
                     96: #include <netinet/udp_var.h>
1.1       cgd        97:
1.53      itojun     98: #ifdef INET6
                     99: #include <netinet/ip6.h>
                    100: #include <netinet/icmp6.h>
                    101: #include <netinet6/ip6_var.h>
                    102: #include <netinet6/in6_pcb.h>
                    103: #include <netinet6/udp6_var.h>
                    104: #endif
                    105:
                    106: #ifndef INET6
                    107: /* always need ip6.h for IP6_EXTHDR_GET */
                    108: #include <netinet/ip6.h>
                    109: #endif
                    110:
1.76      itojun    111: #include "faith.h"
                    112: #if defined(NFAITH) && NFAITH > 0
                    113: #include <net/if_faith.h>
                    114: #endif
                    115:
1.27      christos  116: #include <machine/stdarg.h>
                    117:
1.103.2.2  skrll     118: #ifdef FAST_IPSEC
                    119: #include <netipsec/ipsec.h>
                    120: #include <netipsec/ipsec_var.h>                        /* XXX ipsecstat namespace */
                    121: #ifdef INET6
                    122: #include <netipsec/ipsec6.h>
                    123: #endif
                    124: #endif /* FAST_IPSEC*/
                    125:
1.48      itojun    126: #ifdef IPSEC
                    127: #include <netinet6/ipsec.h>
                    128: #include <netkey/key.h>
                    129: #endif /*IPSEC*/
                    130:
1.64      ws        131: #ifdef IPKDB
                    132: #include <ipkdb/ipkdb.h>
                    133: #endif
                    134:
1.8       mycroft   135: /*
                    136:  * UDP protocol implementation.
                    137:  * Per RFC 768, August, 1980.
                    138:  */
                    139: #ifndef        COMPAT_42
                    140: int    udpcksum = 1;
                    141: #else
                    142: int    udpcksum = 0;           /* XXX */
                    143: #endif
1.93      matt      144:
                    145: struct inpcbtable udbtable;
                    146: struct udpstat udpstat;
1.8       mycroft   147:
1.72      itojun    148: #ifdef INET
1.103.2.2  skrll     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.103.2.2  skrll     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.103.2.2  skrll     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.103.2.2  skrll     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.103.2.2  skrll     201: udp_init(void)
1.1       cgd       202: {
1.18      mycroft   203:
1.35      mycroft   204:        in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.98      matt      205:
                    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.103.2.2  skrll     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.103.2.2  skrll     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.103.2.2  skrll     275:                        if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR)
                    276:                                hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
                    277:                                    ip->ip_dst.s_addr,
                    278:                                    htons(hw_csum + len + IPPROTO_UDP));
                    279:                        if ((hw_csum ^ 0xffff) != 0)
1.78      thorpej   280:                                goto badcsum;
                    281:                        break;
1.103.2.2  skrll     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.103.2.2  skrll     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.103.2.2  skrll     424:        if (uh->uh_sum == 0) {
1.53      itojun    425:                udp6stat.udp6s_nosum++;
1.103.2.2  skrll     426:                goto bad;
                    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.103.2.2  skrll     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.103.2.2  skrll     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.103.2.2  skrll     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.103.2.3  skrll     514:                        so->so_rcv.sb_overflowed++;
1.81      enami     515:                        udpstat.udps_fullsock++;
1.53      itojun    516:                } else
                    517:                        sorwakeup(so);
                    518:        }
                    519: }
1.72      itojun    520: #endif
1.53      itojun    521:
                    522: #ifdef INET6
                    523: static void
1.103.2.2  skrll     524: udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
                    525:        struct sockaddr *src, struct socket *so)
1.53      itojun    526: {
                    527:        struct mbuf *opts = NULL;
                    528:        struct mbuf *n;
                    529:        struct in6pcb *in6p = NULL;
                    530:
                    531:        if (!so)
                    532:                return;
                    533:        if (so->so_proto->pr_domain->dom_family != AF_INET6)
                    534:                return;
                    535:        in6p = sotoin6pcb(so);
                    536:
1.103.2.2  skrll     537: #if defined(IPSEC) || defined(FAST_IPSEC)
1.53      itojun    538:        /* check AH/ESP integrity. */
                    539:        if (so != NULL && ipsec6_in_reject_so(m, so)) {
                    540:                ipsec6stat.in_polvio++;
1.103.2.2  skrll     541:                if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
                    542:                        icmp6_error(n, ICMP6_DST_UNREACH,
                    543:                            ICMP6_DST_UNREACH_ADMIN, 0);
1.53      itojun    544:                return;
                    545:        }
                    546: #endif /*IPSEC*/
                    547:
                    548:        if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
                    549:                if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
                    550:                          || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
                    551:                        struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
                    552:                        ip6_savecontrol(in6p, &opts, ip6, n);
                    553:                }
                    554:
                    555:                m_adj(n, off);
                    556:                if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
                    557:                        m_freem(n);
                    558:                        if (opts)
                    559:                                m_freem(opts);
1.103.2.3  skrll     560:                        so->so_rcv.sb_overflowed++;
1.53      itojun    561:                        udp6stat.udp6s_fullsock++;
                    562:                } else
                    563:                        sorwakeup(so);
                    564:        }
                    565: }
                    566: #endif
                    567:
1.72      itojun    568: #ifdef INET
1.53      itojun    569: static int
1.103.2.2  skrll     570: udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
                    571:        struct mbuf *m, int off /* offset of udphdr */)
1.53      itojun    572: {
                    573:        u_int16_t *sport, *dport;
                    574:        int rcvcnt;
                    575:        struct in_addr *src4, *dst4;
1.103.2.2  skrll     576:        struct inpcb_hdr *inph;
1.53      itojun    577:        struct inpcb *inp;
                    578:
                    579:        rcvcnt = 0;
                    580:        off += sizeof(struct udphdr);   /* now, offset of payload */
                    581:
                    582:        if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
                    583:                goto bad;
                    584:
                    585:        src4 = &src->sin_addr;
                    586:        sport = &src->sin_port;
                    587:        dst4 = &dst->sin_addr;
                    588:        dport = &dst->sin_port;
                    589:
1.73      itojun    590:        if (IN_MULTICAST(dst4->s_addr) ||
1.53      itojun    591:            in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
                    592:                /*
                    593:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    594:                 * for which the local and remote addresses and ports match
                    595:                 * those of the incoming datagram.  This allows more than
                    596:                 * one process to receive multi/broadcasts on the same port.
                    597:                 * (This really ought to be done for unicast datagrams as
                    598:                 * well, but that would cause problems with existing
                    599:                 * applications that open both address-specific sockets and
                    600:                 * a wildcard socket listening to the same port -- they would
                    601:                 * end up receiving duplicates of every unicast datagram.
                    602:                 * Those applications open the multiple sockets to overcome an
                    603:                 * inadequacy of the UDP socket interface, but for backwards
                    604:                 * compatibility we avoid the problem here rather than
                    605:                 * fixing the interface.  Maybe 4.5BSD will remedy this?)
                    606:                 */
                    607:
                    608:                /*
1.92      itojun    609:                 * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71      itojun    610:                 * we need udpiphdr for IPsec processing so we do that later.
1.53      itojun    611:                 */
                    612:                /*
                    613:                 * Locate pcb(s) for datagram.
                    614:                 */
1.103.2.2  skrll     615:                CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
                    616:                        inp = (struct inpcb *)inph;
                    617:                        if (inp->inp_af != AF_INET)
                    618:                                continue;
                    619:
1.53      itojun    620:                        if (inp->inp_lport != *dport)
                    621:                                continue;
                    622:                        if (!in_nullhost(inp->inp_laddr)) {
                    623:                                if (!in_hosteq(inp->inp_laddr, *dst4))
                    624:                                        continue;
                    625:                        }
                    626:                        if (!in_nullhost(inp->inp_faddr)) {
                    627:                                if (!in_hosteq(inp->inp_faddr, *src4) ||
                    628:                                    inp->inp_fport != *sport)
                    629:                                        continue;
                    630:                        }
                    631:
                    632:                        udp4_sendup(m, off, (struct sockaddr *)src,
                    633:                                inp->inp_socket);
                    634:                        rcvcnt++;
                    635:
                    636:                        /*
                    637:                         * Don't look for additional matches if this one does
                    638:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    639:                         * socket options set.  This heuristic avoids searching
                    640:                         * through all pcbs in the common case of a non-shared
                    641:                         * port.  It assumes that an application will never
                    642:                         * clear these options after setting them.
                    643:                         */
                    644:                        if ((inp->inp_socket->so_options &
                    645:                            (SO_REUSEPORT|SO_REUSEADDR)) == 0)
                    646:                                break;
                    647:                }
                    648:        } else {
                    649:                /*
                    650:                 * Locate pcb for datagram.
                    651:                 */
                    652:                inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
                    653:                if (inp == 0) {
                    654:                        ++udpstat.udps_pcbhashmiss;
                    655:                        inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
1.82      itojun    656:                        if (inp == 0)
1.53      itojun    657:                                return rcvcnt;
                    658:                }
                    659:
                    660:                udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
                    661:                rcvcnt++;
                    662:        }
                    663:
                    664: bad:
                    665:        return rcvcnt;
                    666: }
1.72      itojun    667: #endif
1.53      itojun    668:
                    669: #ifdef INET6
                    670: static int
1.103.2.2  skrll     671: udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
                    672:        struct mbuf *m, int off)
1.53      itojun    673: {
1.79      itojun    674:        u_int16_t sport, dport;
1.53      itojun    675:        int rcvcnt;
1.79      itojun    676:        struct in6_addr src6, dst6;
                    677:        const struct in_addr *dst4;
1.103.2.2  skrll     678:        struct inpcb_hdr *inph;
1.53      itojun    679:        struct in6pcb *in6p;
                    680:
                    681:        rcvcnt = 0;
                    682:        off += sizeof(struct udphdr);   /* now, offset of payload */
                    683:
                    684:        if (af != AF_INET && af != AF_INET6)
                    685:                goto bad;
                    686:        if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
                    687:                goto bad;
                    688:
1.79      itojun    689:        in6_embedscope(&src6, src, NULL, NULL);
                    690:        sport = src->sin6_port;
                    691:        in6_embedscope(&dst6, dst, NULL, NULL);
                    692:        dport = dst->sin6_port;
1.88      itojun    693:        dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
1.53      itojun    694:
1.79      itojun    695:        if (IN6_IS_ADDR_MULTICAST(&dst6) ||
                    696:            (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
1.53      itojun    697:                /*
                    698:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    699:                 * for which the local and remote addresses and ports match
                    700:                 * those of the incoming datagram.  This allows more than
                    701:                 * one process to receive multi/broadcasts on the same port.
                    702:                 * (This really ought to be done for unicast datagrams as
                    703:                 * well, but that would cause problems with existing
                    704:                 * applications that open both address-specific sockets and
                    705:                 * a wildcard socket listening to the same port -- they would
                    706:                 * end up receiving duplicates of every unicast datagram.
                    707:                 * Those applications open the multiple sockets to overcome an
                    708:                 * inadequacy of the UDP socket interface, but for backwards
                    709:                 * compatibility we avoid the problem here rather than
                    710:                 * fixing the interface.  Maybe 4.5BSD will remedy this?)
                    711:                 */
                    712:
                    713:                /*
1.92      itojun    714:                 * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71      itojun    715:                 * we need udpiphdr for IPsec processing so we do that later.
1.53      itojun    716:                 */
                    717:                /*
                    718:                 * Locate pcb(s) for datagram.
                    719:                 */
1.103.2.2  skrll     720:                CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
                    721:                        in6p = (struct in6pcb *)inph;
                    722:                        if (in6p->in6p_af != AF_INET6)
                    723:                                continue;
                    724:
1.79      itojun    725:                        if (in6p->in6p_lport != dport)
1.53      itojun    726:                                continue;
                    727:                        if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1.90      itojun    728:                                if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6))
1.53      itojun    729:                                        continue;
1.92      itojun    730:                        } else {
1.79      itojun    731:                                if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
1.85      itojun    732:                                    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55      itojun    733:                                        continue;
                    734:                        }
1.53      itojun    735:                        if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1.79      itojun    736:                                if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
                    737:                                    &src6) || in6p->in6p_fport != sport)
1.53      itojun    738:                                        continue;
1.92      itojun    739:                        } else {
1.79      itojun    740:                                if (IN6_IS_ADDR_V4MAPPED(&src6) &&
1.85      itojun    741:                                    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55      itojun    742:                                        continue;
                    743:                        }
1.53      itojun    744:
                    745:                        udp6_sendup(m, off, (struct sockaddr *)src,
                    746:                                in6p->in6p_socket);
                    747:                        rcvcnt++;
                    748:
                    749:                        /*
                    750:                         * Don't look for additional matches if this one does
                    751:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    752:                         * socket options set.  This heuristic avoids searching
                    753:                         * through all pcbs in the common case of a non-shared
                    754:                         * port.  It assumes that an application will never
                    755:                         * clear these options after setting them.
                    756:                         */
                    757:                        if ((in6p->in6p_socket->so_options &
                    758:                            (SO_REUSEPORT|SO_REUSEADDR)) == 0)
                    759:                                break;
                    760:                }
                    761:        } else {
                    762:                /*
                    763:                 * Locate pcb for datagram.
                    764:                 */
1.103.2.2  skrll     765:                in6p = in6_pcblookup_connect(&udbtable, &src6, sport,
1.79      itojun    766:                    &dst6, dport, 0);
1.53      itojun    767:                if (in6p == 0) {
                    768:                        ++udpstat.udps_pcbhashmiss;
1.103.2.2  skrll     769:                        in6p = in6_pcblookup_bind(&udbtable, &dst6, dport, 0);
1.82      itojun    770:                        if (in6p == 0)
1.53      itojun    771:                                return rcvcnt;
                    772:                }
                    773:
                    774:                udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
                    775:                rcvcnt++;
                    776:        }
                    777:
                    778: bad:
                    779:        return rcvcnt;
                    780: }
                    781: #endif
1.1       cgd       782:
1.72      itojun    783: #ifdef INET
1.1       cgd       784: /*
                    785:  * Notify a udp user of an asynchronous error;
                    786:  * just wake up so that he can collect error status.
                    787:  */
1.7       mycroft   788: static void
1.103.2.2  skrll     789: udp_notify(struct inpcb *inp, int errno)
1.1       cgd       790: {
                    791:        inp->inp_socket->so_error = errno;
                    792:        sorwakeup(inp->inp_socket);
                    793:        sowwakeup(inp->inp_socket);
                    794: }
                    795:
1.27      christos  796: void *
1.103.2.2  skrll     797: udp_ctlinput(int cmd, struct sockaddr *sa, void *v)
1.1       cgd       798: {
1.66      augustss  799:        struct ip *ip = v;
                    800:        struct udphdr *uh;
1.103.2.2  skrll     801:        void (*notify)(struct inpcb *, int) = udp_notify;
1.21      mycroft   802:        int errno;
1.1       cgd       803:
1.53      itojun    804:        if (sa->sa_family != AF_INET
                    805:         || sa->sa_len != sizeof(struct sockaddr_in))
1.51      itojun    806:                return NULL;
1.20      mycroft   807:        if ((unsigned)cmd >= PRC_NCMDS)
1.27      christos  808:                return NULL;
1.20      mycroft   809:        errno = inetctlerrmap[cmd];
1.18      mycroft   810:        if (PRC_IS_REDIRECT(cmd))
1.19      mycroft   811:                notify = in_rtchange, ip = 0;
1.18      mycroft   812:        else if (cmd == PRC_HOSTDEAD)
1.19      mycroft   813:                ip = 0;
1.23      cgd       814:        else if (errno == 0)
1.27      christos  815:                return NULL;
1.19      mycroft   816:        if (ip) {
1.1       cgd       817:                uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34      mycroft   818:                in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
                    819:                    ip->ip_src, uh->uh_sport, errno, notify);
1.53      itojun    820:
                    821:                /* XXX mapped address case */
1.19      mycroft   822:        } else
1.34      mycroft   823:                in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
                    824:                    notify);
1.27      christos  825:        return NULL;
1.1       cgd       826: }
                    827:
1.7       mycroft   828: int
1.27      christos  829: udp_output(struct mbuf *m, ...)
                    830: {
1.66      augustss  831:        struct inpcb *inp;
                    832:        struct udpiphdr *ui;
                    833:        int len = m->m_pkthdr.len;
1.31      mycroft   834:        int error = 0;
1.27      christos  835:        va_list ap;
                    836:
1.98      matt      837:        MCLAIM(m, &udp_tx_mowner);
1.27      christos  838:        va_start(ap, m);
                    839:        inp = va_arg(ap, struct inpcb *);
                    840:        va_end(ap);
1.1       cgd       841:
                    842:        /*
                    843:         * Calculate data length and get a mbuf
                    844:         * for UDP and IP headers.
                    845:         */
1.13      mycroft   846:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
                    847:        if (m == 0) {
                    848:                error = ENOBUFS;
1.39      thorpej   849:                goto release;
                    850:        }
                    851:
                    852:        /*
                    853:         * Compute the packet length of the IP header, and
                    854:         * punt if the length looks bogus.
                    855:         */
1.96      itojun    856:        if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39      thorpej   857:                error = EMSGSIZE;
1.13      mycroft   858:                goto release;
                    859:        }
1.1       cgd       860:
                    861:        /*
                    862:         * Fill in mbuf with extended UDP header
                    863:         * and addresses and length put into network format.
                    864:         */
                    865:        ui = mtod(m, struct udpiphdr *);
                    866:        ui->ui_pr = IPPROTO_UDP;
                    867:        ui->ui_src = inp->inp_laddr;
                    868:        ui->ui_dst = inp->inp_faddr;
                    869:        ui->ui_sport = inp->inp_lport;
                    870:        ui->ui_dport = inp->inp_fport;
1.78      thorpej   871:        ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1       cgd       872:
                    873:        /*
1.78      thorpej   874:         * Set up checksum and output datagram.
1.1       cgd       875:         */
                    876:        if (udpcksum) {
1.78      thorpej   877:                /*
                    878:                 * XXX Cache pseudo-header checksum part for
                    879:                 * XXX "connected" UDP sockets.
                    880:                 */
                    881:                ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
                    882:                    ui->ui_dst.s_addr, htons((u_int16_t)len +
                    883:                    sizeof(struct udphdr) + IPPROTO_UDP));
                    884:                m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
                    885:                m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
                    886:        } else
                    887:                ui->ui_sum = 0;
1.96      itojun    888:        ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1.1       cgd       889:        ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
                    890:        ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
                    891:        udpstat.udps_opackets++;
1.48      itojun    892:
1.31      mycroft   893:        return (ip_output(m, inp->inp_options, &inp->inp_route,
1.12      mycroft   894:            inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.103.2.2  skrll     895:            inp->inp_moptions, inp->inp_socket));
1.1       cgd       896:
                    897: release:
                    898:        m_freem(m);
                    899:        return (error);
                    900: }
                    901:
1.42      thorpej   902: int    udp_sendspace = 9216;           /* really max datagram size */
                    903: int    udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1       cgd       904:                                        /* 40 1K datagrams */
                    905:
                    906: /*ARGSUSED*/
1.7       mycroft   907: int
1.103.2.2  skrll     908: udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1.103.2.4! skrll     909:        struct mbuf *control, struct lwp *l)
1.1       cgd       910: {
1.66      augustss  911:        struct inpcb *inp;
1.103.2.4! skrll     912:        struct proc *p;
1.1       cgd       913:        int s;
1.66      augustss  914:        int error = 0;
1.1       cgd       915:
1.103.2.4! skrll     916:        p = l ? l->l_proc : NULL;
1.1       cgd       917:        if (req == PRU_CONTROL)
1.31      mycroft   918:                return (in_control(so, (long)m, (caddr_t)nam,
1.30      mycroft   919:                    (struct ifnet *)control, p));
1.59      thorpej   920:
1.60      thorpej   921:        if (req == PRU_PURGEIF) {
1.80      itojun    922:                in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
1.60      thorpej   923:                in_purgeif((struct ifnet *)control);
                    924:                in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1.59      thorpej   925:                return (0);
                    926:        }
1.31      mycroft   927:
                    928:        s = splsoftnet();
                    929:        inp = sotoinpcb(so);
1.32      mycroft   930: #ifdef DIAGNOSTIC
                    931:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    932:                panic("udp_usrreq: unexpected control mbuf");
                    933: #endif
1.31      mycroft   934:        if (inp == 0 && req != PRU_ATTACH) {
                    935:                error = EINVAL;
                    936:                goto release;
                    937:        }
                    938:
1.1       cgd       939:        /*
                    940:         * Note: need to block udp_input while changing
                    941:         * the udp pcb queue and/or pcb addresses.
                    942:         */
                    943:        switch (req) {
                    944:
                    945:        case PRU_ATTACH:
1.31      mycroft   946:                if (inp != 0) {
                    947:                        error = EISCONN;
1.1       cgd       948:                        break;
                    949:                }
1.98      matt      950: #ifdef MBUFTRACE
                    951:                so->so_mowner = &udp_mowner;
                    952:                so->so_rcv.sb_mowner = &udp_rx_mowner;
                    953:                so->so_snd.sb_mowner = &udp_tx_mowner;
                    954: #endif
1.31      mycroft   955:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    956:                        error = soreserve(so, udp_sendspace, udp_recvspace);
                    957:                        if (error)
                    958:                                break;
                    959:                }
1.18      mycroft   960:                error = in_pcballoc(so, &udbtable);
1.1       cgd       961:                if (error)
                    962:                        break;
1.31      mycroft   963:                inp = sotoinpcb(so);
                    964:                inp->inp_ip.ip_ttl = ip_defttl;
1.1       cgd       965:                break;
                    966:
                    967:        case PRU_DETACH:
1.31      mycroft   968:                in_pcbdetach(inp);
1.1       cgd       969:                break;
                    970:
                    971:        case PRU_BIND:
1.31      mycroft   972:                error = in_pcbbind(inp, nam, p);
1.1       cgd       973:                break;
                    974:
                    975:        case PRU_LISTEN:
                    976:                error = EOPNOTSUPP;
                    977:                break;
                    978:
                    979:        case PRU_CONNECT:
1.31      mycroft   980:                error = in_pcbconnect(inp, nam);
                    981:                if (error)
1.1       cgd       982:                        break;
1.31      mycroft   983:                soisconnected(so);
1.1       cgd       984:                break;
                    985:
                    986:        case PRU_CONNECT2:
                    987:                error = EOPNOTSUPP;
                    988:                break;
                    989:
                    990:        case PRU_DISCONNECT:
1.31      mycroft   991:                /*soisdisconnected(so);*/
                    992:                so->so_state &= ~SS_ISCONNECTED;        /* XXX */
1.1       cgd       993:                in_pcbdisconnect(inp);
1.34      mycroft   994:                inp->inp_laddr = zeroin_addr;           /* XXX */
1.35      mycroft   995:                in_pcbstate(inp, INP_BOUND);            /* XXX */
1.1       cgd       996:                break;
                    997:
                    998:        case PRU_SHUTDOWN:
                    999:                socantsendmore(so);
                   1000:                break;
                   1001:
1.31      mycroft  1002:        case PRU_RCVD:
                   1003:                error = EOPNOTSUPP;
1.1       cgd      1004:                break;
                   1005:
1.31      mycroft  1006:        case PRU_SEND:
1.32      mycroft  1007:                if (control && control->m_len) {
                   1008:                        m_freem(control);
                   1009:                        m_freem(m);
                   1010:                        error = EINVAL;
                   1011:                        break;
                   1012:                }
1.31      mycroft  1013:        {
1.35      mycroft  1014:                struct in_addr laddr;                   /* XXX */
1.1       cgd      1015:
1.31      mycroft  1016:                if (nam) {
1.35      mycroft  1017:                        laddr = inp->inp_laddr;         /* XXX */
1.31      mycroft  1018:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
                   1019:                                error = EISCONN;
1.32      mycroft  1020:                                goto die;
1.31      mycroft  1021:                        }
                   1022:                        error = in_pcbconnect(inp, nam);
1.100     matt     1023:                        if (error)
                   1024:                                goto die;
1.31      mycroft  1025:                } else {
                   1026:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
                   1027:                                error = ENOTCONN;
1.32      mycroft  1028:                                goto die;
1.31      mycroft  1029:                        }
                   1030:                }
1.33      mycroft  1031:                error = udp_output(m, inp);
1.100     matt     1032:                m = NULL;
1.31      mycroft  1033:                if (nam) {
                   1034:                        in_pcbdisconnect(inp);
1.35      mycroft  1035:                        inp->inp_laddr = laddr;         /* XXX */
                   1036:                        in_pcbstate(inp, INP_BOUND);    /* XXX */
1.31      mycroft  1037:                }
1.100     matt     1038:          die:
                   1039:                if (m)
                   1040:                        m_freem(m);
1.31      mycroft  1041:        }
1.1       cgd      1042:                break;
                   1043:
                   1044:        case PRU_SENSE:
                   1045:                /*
                   1046:                 * stat: don't bother with a blocksize.
                   1047:                 */
1.31      mycroft  1048:                splx(s);
1.1       cgd      1049:                return (0);
                   1050:
1.31      mycroft  1051:        case PRU_RCVOOB:
                   1052:                error =  EOPNOTSUPP;
                   1053:                break;
                   1054:
1.1       cgd      1055:        case PRU_SENDOOB:
1.32      mycroft  1056:                m_freem(control);
1.31      mycroft  1057:                m_freem(m);
1.1       cgd      1058:                error =  EOPNOTSUPP;
                   1059:                break;
                   1060:
1.31      mycroft  1061:        case PRU_SOCKADDR:
                   1062:                in_setsockaddr(inp, nam);
                   1063:                break;
                   1064:
                   1065:        case PRU_PEERADDR:
                   1066:                in_setpeeraddr(inp, nam);
                   1067:                break;
1.1       cgd      1068:
                   1069:        default:
                   1070:                panic("udp_usrreq");
                   1071:        }
                   1072:
                   1073: release:
1.31      mycroft  1074:        splx(s);
1.1       cgd      1075:        return (error);
1.13      mycroft  1076: }
                   1077:
                   1078: /*
                   1079:  * Sysctl for udp variables.
                   1080:  */
1.103.2.2  skrll    1081: SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup")
1.13      mycroft  1082: {
1.103.2.2  skrll    1083:
                   1084:        sysctl_createv(clog, 0, NULL, NULL,
                   1085:                       CTLFLAG_PERMANENT,
                   1086:                       CTLTYPE_NODE, "net", NULL,
                   1087:                       NULL, 0, NULL, 0,
                   1088:                       CTL_NET, CTL_EOL);
                   1089:        sysctl_createv(clog, 0, NULL, NULL,
                   1090:                       CTLFLAG_PERMANENT,
                   1091:                       CTLTYPE_NODE, "inet", NULL,
                   1092:                       NULL, 0, NULL, 0,
                   1093:                       CTL_NET, PF_INET, CTL_EOL);
                   1094:        sysctl_createv(clog, 0, NULL, NULL,
                   1095:                       CTLFLAG_PERMANENT,
                   1096:                       CTLTYPE_NODE, "udp",
                   1097:                       SYSCTL_DESCR("UDPv4 related settings"),
                   1098:                       NULL, 0, NULL, 0,
                   1099:                       CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
                   1100:
                   1101:        sysctl_createv(clog, 0, NULL, NULL,
                   1102:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1103:                       CTLTYPE_INT, "checksum",
                   1104:                       SYSCTL_DESCR("Compute UDP checksums"),
                   1105:                       NULL, 0, &udpcksum, 0,
                   1106:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
                   1107:                       CTL_EOL);
                   1108:        sysctl_createv(clog, 0, NULL, NULL,
                   1109:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1110:                       CTLTYPE_INT, "sendspace",
                   1111:                       SYSCTL_DESCR("Default UDP send buffer size"),
                   1112:                       NULL, 0, &udp_sendspace, 0,
                   1113:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
                   1114:                       CTL_EOL);
                   1115:        sysctl_createv(clog, 0, NULL, NULL,
                   1116:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1117:                       CTLTYPE_INT, "recvspace",
                   1118:                       SYSCTL_DESCR("Default UDP receive buffer size"),
                   1119:                       NULL, 0, &udp_recvspace, 0,
                   1120:                       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
                   1121:                       CTL_EOL);
1.1       cgd      1122: }
1.72      itojun   1123: #endif

CVSweb <webmaster@jp.NetBSD.org>