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

1.44    ! thorpej     1: /*     $NetBSD: udp_usrreq.c,v 1.43 1997/09/12 10:58:31 drochner Exp $ */
1.14      cgd         2:
1.1       cgd         3: /*
1.44    ! thorpej     4:  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
1.13      mycroft     5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      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. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  *
1.44    ! thorpej    35:  *     @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
1.1       cgd        36:  */
1.38      ws         37: #include "ipkdb.h"
1.1       cgd        38:
1.5       mycroft    39: #include <sys/param.h>
                     40: #include <sys/malloc.h>
                     41: #include <sys/mbuf.h>
                     42: #include <sys/protosw.h>
                     43: #include <sys/socket.h>
                     44: #include <sys/socketvar.h>
1.13      mycroft    45: #include <sys/errno.h>
1.5       mycroft    46: #include <sys/stat.h>
1.27      christos   47: #include <sys/systm.h>
                     48: #include <sys/proc.h>
                     49:
                     50: #include <vm/vm.h>
                     51: #include <sys/sysctl.h>
1.1       cgd        52:
1.5       mycroft    53: #include <net/if.h>
                     54: #include <net/route.h>
1.1       cgd        55:
1.5       mycroft    56: #include <netinet/in.h>
                     57: #include <netinet/in_systm.h>
1.15      cgd        58: #include <netinet/in_var.h>
1.5       mycroft    59: #include <netinet/ip.h>
                     60: #include <netinet/in_pcb.h>
                     61: #include <netinet/ip_var.h>
                     62: #include <netinet/ip_icmp.h>
                     63: #include <netinet/udp.h>
                     64: #include <netinet/udp_var.h>
1.1       cgd        65:
1.27      christos   66: #include <machine/stdarg.h>
                     67:
1.8       mycroft    68: /*
                     69:  * UDP protocol implementation.
                     70:  * Per RFC 768, August, 1980.
                     71:  */
                     72: #ifndef        COMPAT_42
                     73: int    udpcksum = 1;
                     74: #else
                     75: int    udpcksum = 0;           /* XXX */
                     76: #endif
                     77:
1.13      mycroft    78: static void udp_notify __P((struct inpcb *, int));
1.7       mycroft    79:
1.26      mycroft    80: #ifndef UDBHASHSIZE
                     81: #define        UDBHASHSIZE     128
                     82: #endif
                     83: int    udbhashsize = UDBHASHSIZE;
                     84:
1.7       mycroft    85: void
1.1       cgd        86: udp_init()
                     87: {
1.18      mycroft    88:
1.35      mycroft    89:        in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.1       cgd        90: }
                     91:
1.7       mycroft    92: void
1.27      christos   93: #if __STDC__
                     94: udp_input(struct mbuf *m, ...)
                     95: #else
                     96: udp_input(m, va_alist)
                     97:        struct mbuf *m;
                     98:        va_dcl
                     99: #endif
1.1       cgd       100: {
                    101:        register struct ip *ip;
                    102:        register struct udphdr *uh;
                    103:        register struct inpcb *inp;
                    104:        struct mbuf *opts = 0;
                    105:        int len;
                    106:        struct ip save_ip;
1.27      christos  107:        int iphlen;
                    108:        va_list ap;
1.34      mycroft   109:        struct sockaddr_in udpsrc;
1.27      christos  110:
                    111:        va_start(ap, m);
                    112:        iphlen = va_arg(ap, int);
                    113:        va_end(ap);
1.1       cgd       114:
                    115:        udpstat.udps_ipackets++;
                    116:
                    117:        /*
                    118:         * Strip IP options, if any; should skip this,
                    119:         * make available to user, and use on returned packets,
                    120:         * but we don't yet have a way to check the checksum
                    121:         * with options still present.
                    122:         */
                    123:        if (iphlen > sizeof (struct ip)) {
                    124:                ip_stripoptions(m, (struct mbuf *)0);
                    125:                iphlen = sizeof(struct ip);
                    126:        }
                    127:
                    128:        /*
                    129:         * Get IP and UDP header together in first mbuf.
                    130:         */
                    131:        ip = mtod(m, struct ip *);
                    132:        if (m->m_len < iphlen + sizeof(struct udphdr)) {
                    133:                if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
                    134:                        udpstat.udps_hdrops++;
                    135:                        return;
                    136:                }
                    137:                ip = mtod(m, struct ip *);
                    138:        }
                    139:        uh = (struct udphdr *)((caddr_t)ip + iphlen);
                    140:
                    141:        /*
                    142:         * Make mbuf data length reflect UDP length.
                    143:         * If not enough data to reflect UDP length, drop.
                    144:         */
1.15      cgd       145:        len = ntohs((u_int16_t)uh->uh_ulen);
1.1       cgd       146:        if (ip->ip_len != len) {
                    147:                if (len > ip->ip_len) {
                    148:                        udpstat.udps_badlen++;
                    149:                        goto bad;
                    150:                }
                    151:                m_adj(m, len - ip->ip_len);
                    152:                /* ip->ip_len = len; */
                    153:        }
                    154:        /*
                    155:         * Save a copy of the IP header in case we want restore it
                    156:         * for sending an ICMP error message in response.
                    157:         */
                    158:        save_ip = *ip;
                    159:
                    160:        /*
                    161:         * Checksum extended UDP header and data.
                    162:         */
1.29      mrg       163:        if (uh->uh_sum) {
1.25      cgd       164:                bzero(((struct ipovly *)ip)->ih_x1,
                    165:                    sizeof ((struct ipovly *)ip)->ih_x1);
1.1       cgd       166:                ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
1.27      christos  167:                if ((uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) != 0) {
1.1       cgd       168:                        udpstat.udps_badsum++;
                    169:                        m_freem(m);
                    170:                        return;
                    171:                }
                    172:        }
1.13      mycroft   173:
1.16      mycroft   174:        if (IN_MULTICAST(ip->ip_dst.s_addr) ||
1.13      mycroft   175:            in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1.40      thorpej   176:                struct inpcb *last;
1.4       hpeyerl   177:                /*
                    178:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    179:                 * for which the local and remote addresses and ports match
                    180:                 * those of the incoming datagram.  This allows more than
                    181:                 * one process to receive multi/broadcasts on the same port.
                    182:                 * (This really ought to be done for unicast datagrams as
                    183:                 * well, but that would cause problems with existing
                    184:                 * applications that open both address-specific sockets and
                    185:                 * a wildcard socket listening to the same port -- they would
                    186:                 * end up receiving duplicates of every unicast datagram.
                    187:                 * Those applications open the multiple sockets to overcome an
                    188:                 * inadequacy of the UDP socket interface, but for backwards
                    189:                 * compatibility we avoid the problem here rather than
1.13      mycroft   190:                 * fixing the interface.  Maybe 4.5BSD will remedy this?)
1.4       hpeyerl   191:                 */
1.13      mycroft   192:
1.4       hpeyerl   193:                /*
                    194:                 * Construct sockaddr format source address.
                    195:                 */
1.34      mycroft   196:                udpsrc.sin_family = AF_INET;
                    197:                udpsrc.sin_len = sizeof(struct sockaddr_in);
1.36      mycroft   198:                udpsrc.sin_addr = ip->ip_src;
1.34      mycroft   199:                udpsrc.sin_port = uh->uh_sport;
1.36      mycroft   200:                bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero));
1.34      mycroft   201:
1.43      drochner  202:                iphlen += sizeof(struct udphdr);
                    203:                m->m_len -= iphlen;
                    204:                m->m_pkthdr.len -= iphlen;
                    205:                m->m_data += iphlen;
1.4       hpeyerl   206:                /*
                    207:                 * Locate pcb(s) for datagram.
                    208:                 * (Algorithm copied from raw_intr().)
                    209:                 */
                    210:                last = NULL;
1.22      cgd       211:                for (inp = udbtable.inpt_queue.cqh_first;
                    212:                    inp != (struct inpcb *)&udbtable.inpt_queue;
                    213:                    inp = inp->inp_queue.cqe_next) {
1.4       hpeyerl   214:                        if (inp->inp_lport != uh->uh_dport)
                    215:                                continue;
1.34      mycroft   216:                        if (!in_nullhost(inp->inp_laddr)) {
                    217:                                if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
1.4       hpeyerl   218:                                        continue;
                    219:                        }
1.34      mycroft   220:                        if (!in_nullhost(inp->inp_faddr)) {
                    221:                                if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
1.4       hpeyerl   222:                                    inp->inp_fport != uh->uh_sport)
                    223:                                        continue;
                    224:                        }
                    225:
                    226:                        if (last != NULL) {
                    227:                                struct mbuf *n;
                    228:
                    229:                                if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
1.40      thorpej   230:                                        if (last->inp_flags & INP_CONTROLOPTS
                    231:                                            || last->inp_socket->so_options &
                    232:                                               SO_TIMESTAMP) {
                    233:                                                ip_savecontrol(last, &opts,
                    234:                                                    ip, n);
                    235:                                        }
                    236:                                        if (sbappendaddr(
                    237:                                            &last->inp_socket->so_rcv,
                    238:                                            sintosa(&udpsrc), n, opts) == 0) {
1.4       hpeyerl   239:                                                m_freem(n);
1.40      thorpej   240:                                                if (opts)
                    241:                                                        m_freem(opts);
1.13      mycroft   242:                                        } else
1.40      thorpej   243:                                                sorwakeup(last->inp_socket);
                    244:                                        opts = 0;
1.4       hpeyerl   245:                                }
                    246:                        }
1.40      thorpej   247:                        last = inp;
1.4       hpeyerl   248:                        /*
1.13      mycroft   249:                         * Don't look for additional matches if this one does
                    250:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    251:                         * socket options set.  This heuristic avoids searching
                    252:                         * through all pcbs in the common case of a non-shared
                    253:                         * port.  It * assumes that an application will never
                    254:                         * clear these options after setting them.
1.4       hpeyerl   255:                         */
1.40      thorpej   256:                        if ((last->inp_socket->so_options &
                    257:                            (SO_REUSEPORT|SO_REUSEADDR)) == 0)
1.4       hpeyerl   258:                                break;
                    259:                }
1.6       mycroft   260:
1.4       hpeyerl   261:                if (last == NULL) {
                    262:                        /*
                    263:                         * No matching pcb found; discard datagram.
                    264:                         * (No need to send an ICMP Port Unreachable
                    265:                         * for a broadcast or multicast datgram.)
                    266:                         */
1.13      mycroft   267:                        udpstat.udps_noportbcast++;
1.4       hpeyerl   268:                        goto bad;
                    269:                }
1.40      thorpej   270:                if (last->inp_flags & INP_CONTROLOPTS ||
                    271:                    last->inp_socket->so_options & SO_TIMESTAMP)
                    272:                        ip_savecontrol(last, &opts, ip, m);
                    273:                if (sbappendaddr(&last->inp_socket->so_rcv,
                    274:                    sintosa(&udpsrc), m, opts) == 0) {
1.13      mycroft   275:                        udpstat.udps_fullsock++;
1.4       hpeyerl   276:                        goto bad;
1.13      mycroft   277:                }
1.40      thorpej   278:                sorwakeup(last->inp_socket);
1.4       hpeyerl   279:                return;
                    280:        }
1.1       cgd       281:        /*
                    282:         * Locate pcb for datagram.
                    283:         */
1.35      mycroft   284:        inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport,
1.26      mycroft   285:            ip->ip_dst, uh->uh_dport);
                    286:        if (inp == 0) {
                    287:                ++udpstat.udps_pcbhashmiss;
1.35      mycroft   288:                inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport);
1.18      mycroft   289:                if (inp == 0) {
                    290:                        udpstat.udps_noport++;
                    291:                        if (m->m_flags & (M_BCAST | M_MCAST)) {
                    292:                                udpstat.udps_noportbcast++;
                    293:                                goto bad;
                    294:                        }
                    295:                        *ip = save_ip;
1.38      ws        296: #if NIPKDB > 0
                    297:                        if (checkipkdb(&ip->ip_src,
                    298:                                       uh->uh_sport,
                    299:                                       uh->uh_dport,
                    300:                                       m,
                    301:                                       iphlen + sizeof(struct udphdr),
                    302:                                       len - sizeof(struct udphdr)))
1.37      ws        303:                        /* It was a debugger connect packet, just drop it now */
                    304:                                goto bad;
                    305: #endif
1.18      mycroft   306:                        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
                    307:                        return;
1.13      mycroft   308:                }
1.1       cgd       309:        }
                    310:
                    311:        /*
                    312:         * Construct sockaddr format source address.
                    313:         * Stuff source address and datagram in user buffer.
                    314:         */
1.34      mycroft   315:        udpsrc.sin_family = AF_INET;
                    316:        udpsrc.sin_len = sizeof(struct sockaddr_in);
1.36      mycroft   317:        udpsrc.sin_addr = ip->ip_src;
1.34      mycroft   318:        udpsrc.sin_port = uh->uh_sport;
1.36      mycroft   319:        bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero));
1.34      mycroft   320:
1.40      thorpej   321:        if (inp->inp_flags & INP_CONTROLOPTS ||
                    322:            inp->inp_socket->so_options & SO_TIMESTAMP)
                    323:                ip_savecontrol(inp, &opts, ip, m);
1.1       cgd       324:        iphlen += sizeof(struct udphdr);
                    325:        m->m_len -= iphlen;
                    326:        m->m_pkthdr.len -= iphlen;
                    327:        m->m_data += iphlen;
1.34      mycroft   328:        if (sbappendaddr(&inp->inp_socket->so_rcv, sintosa(&udpsrc), m,
1.17      mycroft   329:            opts) == 0) {
1.1       cgd       330:                udpstat.udps_fullsock++;
                    331:                goto bad;
                    332:        }
                    333:        sorwakeup(inp->inp_socket);
                    334:        return;
                    335: bad:
                    336:        m_freem(m);
                    337:        if (opts)
                    338:                m_freem(opts);
                    339: }
                    340:
                    341: /*
                    342:  * Notify a udp user of an asynchronous error;
                    343:  * just wake up so that he can collect error status.
                    344:  */
1.7       mycroft   345: static void
1.1       cgd       346: udp_notify(inp, errno)
                    347:        register struct inpcb *inp;
1.7       mycroft   348:        int errno;
1.1       cgd       349: {
1.34      mycroft   350:
1.1       cgd       351:        inp->inp_socket->so_error = errno;
                    352:        sorwakeup(inp->inp_socket);
                    353:        sowwakeup(inp->inp_socket);
                    354: }
                    355:
1.27      christos  356: void *
                    357: udp_ctlinput(cmd, sa, v)
1.1       cgd       358:        int cmd;
                    359:        struct sockaddr *sa;
1.27      christos  360:        void *v;
1.1       cgd       361: {
1.27      christos  362:        register struct ip *ip = v;
1.1       cgd       363:        register struct udphdr *uh;
1.21      mycroft   364:        extern int inetctlerrmap[];
1.18      mycroft   365:        void (*notify) __P((struct inpcb *, int)) = udp_notify;
1.21      mycroft   366:        int errno;
1.1       cgd       367:
1.20      mycroft   368:        if ((unsigned)cmd >= PRC_NCMDS)
1.27      christos  369:                return NULL;
1.20      mycroft   370:        errno = inetctlerrmap[cmd];
1.18      mycroft   371:        if (PRC_IS_REDIRECT(cmd))
1.19      mycroft   372:                notify = in_rtchange, ip = 0;
1.18      mycroft   373:        else if (cmd == PRC_HOSTDEAD)
1.19      mycroft   374:                ip = 0;
1.23      cgd       375:        else if (errno == 0)
1.27      christos  376:                return NULL;
1.19      mycroft   377:        if (ip) {
1.1       cgd       378:                uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34      mycroft   379:                in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
                    380:                    ip->ip_src, uh->uh_sport, errno, notify);
1.19      mycroft   381:        } else
1.34      mycroft   382:                in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
                    383:                    notify);
1.27      christos  384:        return NULL;
1.1       cgd       385: }
                    386:
1.7       mycroft   387: int
1.27      christos  388: #if __STDC__
                    389: udp_output(struct mbuf *m, ...)
                    390: #else
                    391: udp_output(m, va_alist)
                    392:        struct mbuf *m;
                    393:        va_dcl
                    394: #endif
                    395: {
1.1       cgd       396:        register struct inpcb *inp;
                    397:        register struct udpiphdr *ui;
                    398:        register int len = m->m_pkthdr.len;
1.31      mycroft   399:        int error = 0;
1.27      christos  400:        va_list ap;
                    401:
                    402:        va_start(ap, m);
                    403:        inp = va_arg(ap, struct inpcb *);
                    404:        va_end(ap);
1.1       cgd       405:
                    406:        /*
                    407:         * Calculate data length and get a mbuf
                    408:         * for UDP and IP headers.
                    409:         */
1.13      mycroft   410:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
                    411:        if (m == 0) {
                    412:                error = ENOBUFS;
1.39      thorpej   413:                goto release;
                    414:        }
                    415:
                    416:        /*
                    417:         * Compute the packet length of the IP header, and
                    418:         * punt if the length looks bogus.
                    419:         */
                    420:        if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
                    421:                error = EMSGSIZE;
1.13      mycroft   422:                goto release;
                    423:        }
1.1       cgd       424:
                    425:        /*
                    426:         * Fill in mbuf with extended UDP header
                    427:         * and addresses and length put into network format.
                    428:         */
                    429:        ui = mtod(m, struct udpiphdr *);
1.25      cgd       430:        bzero(ui->ui_x1, sizeof ui->ui_x1);
1.1       cgd       431:        ui->ui_pr = IPPROTO_UDP;
1.15      cgd       432:        ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1.1       cgd       433:        ui->ui_src = inp->inp_laddr;
                    434:        ui->ui_dst = inp->inp_faddr;
                    435:        ui->ui_sport = inp->inp_lport;
                    436:        ui->ui_dport = inp->inp_fport;
                    437:        ui->ui_ulen = ui->ui_len;
                    438:
                    439:        /*
                    440:         * Stuff checksum and output datagram.
                    441:         */
                    442:        ui->ui_sum = 0;
                    443:        if (udpcksum) {
                    444:            if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
                    445:                ui->ui_sum = 0xffff;
                    446:        }
                    447:        ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
                    448:        ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
                    449:        ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
                    450:        udpstat.udps_opackets++;
1.31      mycroft   451:        return (ip_output(m, inp->inp_options, &inp->inp_route,
1.12      mycroft   452:            inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.31      mycroft   453:            inp->inp_moptions));
1.1       cgd       454:
                    455: release:
                    456:        m_freem(m);
                    457:        return (error);
                    458: }
                    459:
1.42      thorpej   460: int    udp_sendspace = 9216;           /* really max datagram size */
                    461: int    udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1       cgd       462:                                        /* 40 1K datagrams */
                    463:
                    464: /*ARGSUSED*/
1.7       mycroft   465: int
1.31      mycroft   466: udp_usrreq(so, req, m, nam, control, p)
1.1       cgd       467:        struct socket *so;
                    468:        int req;
1.31      mycroft   469:        struct mbuf *m, *nam, *control;
1.30      mycroft   470:        struct proc *p;
1.1       cgd       471: {
1.31      mycroft   472:        register struct inpcb *inp;
1.1       cgd       473:        int s;
1.31      mycroft   474:        register int error = 0;
1.1       cgd       475:
                    476:        if (req == PRU_CONTROL)
1.31      mycroft   477:                return (in_control(so, (long)m, (caddr_t)nam,
1.30      mycroft   478:                    (struct ifnet *)control, p));
1.31      mycroft   479:
                    480:        s = splsoftnet();
                    481:        inp = sotoinpcb(so);
1.32      mycroft   482: #ifdef DIAGNOSTIC
                    483:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    484:                panic("udp_usrreq: unexpected control mbuf");
                    485: #endif
1.31      mycroft   486:        if (inp == 0 && req != PRU_ATTACH) {
                    487:                error = EINVAL;
                    488:                goto release;
                    489:        }
                    490:
1.1       cgd       491:        /*
                    492:         * Note: need to block udp_input while changing
                    493:         * the udp pcb queue and/or pcb addresses.
                    494:         */
                    495:        switch (req) {
                    496:
                    497:        case PRU_ATTACH:
1.31      mycroft   498:                if (inp != 0) {
                    499:                        error = EISCONN;
1.1       cgd       500:                        break;
                    501:                }
1.31      mycroft   502:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    503:                        error = soreserve(so, udp_sendspace, udp_recvspace);
                    504:                        if (error)
                    505:                                break;
                    506:                }
1.18      mycroft   507:                error = in_pcballoc(so, &udbtable);
1.1       cgd       508:                if (error)
                    509:                        break;
1.31      mycroft   510:                inp = sotoinpcb(so);
                    511:                inp->inp_ip.ip_ttl = ip_defttl;
1.1       cgd       512:                break;
                    513:
                    514:        case PRU_DETACH:
1.31      mycroft   515:                in_pcbdetach(inp);
1.1       cgd       516:                break;
                    517:
                    518:        case PRU_BIND:
1.31      mycroft   519:                error = in_pcbbind(inp, nam, p);
1.1       cgd       520:                break;
                    521:
                    522:        case PRU_LISTEN:
                    523:                error = EOPNOTSUPP;
                    524:                break;
                    525:
                    526:        case PRU_CONNECT:
1.31      mycroft   527:                error = in_pcbconnect(inp, nam);
                    528:                if (error)
1.1       cgd       529:                        break;
1.31      mycroft   530:                soisconnected(so);
1.1       cgd       531:                break;
                    532:
                    533:        case PRU_CONNECT2:
                    534:                error = EOPNOTSUPP;
                    535:                break;
                    536:
                    537:        case PRU_DISCONNECT:
1.31      mycroft   538:                /*soisdisconnected(so);*/
                    539:                so->so_state &= ~SS_ISCONNECTED;        /* XXX */
1.1       cgd       540:                in_pcbdisconnect(inp);
1.34      mycroft   541:                inp->inp_laddr = zeroin_addr;           /* XXX */
1.35      mycroft   542:                in_pcbstate(inp, INP_BOUND);            /* XXX */
1.1       cgd       543:                break;
                    544:
                    545:        case PRU_SHUTDOWN:
                    546:                socantsendmore(so);
                    547:                break;
                    548:
1.31      mycroft   549:        case PRU_RCVD:
                    550:                error = EOPNOTSUPP;
1.1       cgd       551:                break;
                    552:
1.31      mycroft   553:        case PRU_SEND:
1.32      mycroft   554:                if (control && control->m_len) {
                    555:                        m_freem(control);
                    556:                        m_freem(m);
                    557:                        error = EINVAL;
                    558:                        break;
                    559:                }
1.31      mycroft   560:        {
1.35      mycroft   561:                struct in_addr laddr;                   /* XXX */
1.1       cgd       562:
1.31      mycroft   563:                if (nam) {
1.35      mycroft   564:                        laddr = inp->inp_laddr;         /* XXX */
1.31      mycroft   565:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
                    566:                                error = EISCONN;
1.32      mycroft   567:                                goto die;
1.31      mycroft   568:                        }
                    569:                        error = in_pcbconnect(inp, nam);
1.32      mycroft   570:                        if (error) {
                    571:                        die:
                    572:                                m_freem(m);
1.31      mycroft   573:                                break;
1.32      mycroft   574:                        }
1.31      mycroft   575:                } else {
                    576:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
                    577:                                error = ENOTCONN;
1.32      mycroft   578:                                goto die;
1.31      mycroft   579:                        }
                    580:                }
1.33      mycroft   581:                error = udp_output(m, inp);
1.31      mycroft   582:                if (nam) {
                    583:                        in_pcbdisconnect(inp);
1.35      mycroft   584:                        inp->inp_laddr = laddr;         /* XXX */
                    585:                        in_pcbstate(inp, INP_BOUND);    /* XXX */
1.31      mycroft   586:                }
                    587:        }
1.1       cgd       588:                break;
                    589:
                    590:        case PRU_SENSE:
                    591:                /*
                    592:                 * stat: don't bother with a blocksize.
                    593:                 */
1.31      mycroft   594:                splx(s);
1.1       cgd       595:                return (0);
                    596:
1.31      mycroft   597:        case PRU_RCVOOB:
                    598:                error =  EOPNOTSUPP;
                    599:                break;
                    600:
1.1       cgd       601:        case PRU_SENDOOB:
1.32      mycroft   602:                m_freem(control);
1.31      mycroft   603:                m_freem(m);
1.1       cgd       604:                error =  EOPNOTSUPP;
                    605:                break;
                    606:
1.31      mycroft   607:        case PRU_SOCKADDR:
                    608:                in_setsockaddr(inp, nam);
                    609:                break;
                    610:
                    611:        case PRU_PEERADDR:
                    612:                in_setpeeraddr(inp, nam);
                    613:                break;
1.1       cgd       614:
                    615:        default:
                    616:                panic("udp_usrreq");
                    617:        }
                    618:
                    619: release:
1.31      mycroft   620:        splx(s);
1.1       cgd       621:        return (error);
1.13      mycroft   622: }
                    623:
                    624: /*
                    625:  * Sysctl for udp variables.
                    626:  */
1.27      christos  627: int
1.13      mycroft   628: udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                    629:        int *name;
                    630:        u_int namelen;
                    631:        void *oldp;
                    632:        size_t *oldlenp;
                    633:        void *newp;
                    634:        size_t newlen;
                    635: {
                    636:        /* All sysctl names at this level are terminal. */
                    637:        if (namelen != 1)
                    638:                return (ENOTDIR);
                    639:
                    640:        switch (name[0]) {
                    641:        case UDPCTL_CHECKSUM:
                    642:                return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1.42      thorpej   643:        case UDPCTL_SENDSPACE:
                    644:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                    645:                    &udp_sendspace));
                    646:        case UDPCTL_RECVSPACE:
                    647:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                    648:                    &udp_recvspace));
1.13      mycroft   649:        default:
                    650:                return (ENOPROTOOPT);
                    651:        }
                    652:        /* NOTREACHED */
1.1       cgd       653: }

CVSweb <webmaster@jp.NetBSD.org>