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

1.34    ! mycroft     1: /*     $NetBSD: udp_usrreq.c,v 1.33 1996/05/23 17:05:45 mycroft Exp $  */
1.14      cgd         2:
1.1       cgd         3: /*
1.13      mycroft     4:  * Copyright (c) 1982, 1986, 1988, 1990, 1993
                      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.14      cgd        35:  *     @(#)udp_usrreq.c        8.4 (Berkeley) 1/21/94
1.1       cgd        36:  */
                     37:
1.5       mycroft    38: #include <sys/param.h>
                     39: #include <sys/malloc.h>
                     40: #include <sys/mbuf.h>
                     41: #include <sys/protosw.h>
                     42: #include <sys/socket.h>
                     43: #include <sys/socketvar.h>
1.13      mycroft    44: #include <sys/errno.h>
1.5       mycroft    45: #include <sys/stat.h>
1.27      christos   46: #include <sys/systm.h>
                     47: #include <sys/proc.h>
                     48:
                     49: #include <vm/vm.h>
                     50: #include <sys/sysctl.h>
1.1       cgd        51:
1.5       mycroft    52: #include <net/if.h>
                     53: #include <net/route.h>
1.1       cgd        54:
1.5       mycroft    55: #include <netinet/in.h>
                     56: #include <netinet/in_systm.h>
1.15      cgd        57: #include <netinet/in_var.h>
1.5       mycroft    58: #include <netinet/ip.h>
                     59: #include <netinet/in_pcb.h>
                     60: #include <netinet/ip_var.h>
                     61: #include <netinet/ip_icmp.h>
                     62: #include <netinet/udp.h>
                     63: #include <netinet/udp_var.h>
1.1       cgd        64:
1.27      christos   65: #include <machine/stdarg.h>
                     66:
1.8       mycroft    67: /*
                     68:  * UDP protocol implementation.
                     69:  * Per RFC 768, August, 1980.
                     70:  */
                     71: #ifndef        COMPAT_42
                     72: int    udpcksum = 1;
                     73: #else
                     74: int    udpcksum = 0;           /* XXX */
                     75: #endif
                     76:
1.13      mycroft    77: static void udp_notify __P((struct inpcb *, int));
                     78: static struct mbuf *udp_saveopt __P((caddr_t, int, 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.26      mycroft    89:        in_pcbinit(&udbtable, 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.4       hpeyerl   176:                struct socket *last;
                    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);
        !           198:                udpsrc.sin_port = uh->uh_sport;
        !           199:                udpsrc.sin_addr = ip->ip_src;
        !           200:
1.4       hpeyerl   201:                m->m_len -= sizeof (struct udpiphdr);
                    202:                m->m_data += sizeof (struct udpiphdr);
                    203:                /*
                    204:                 * Locate pcb(s) for datagram.
                    205:                 * (Algorithm copied from raw_intr().)
                    206:                 */
                    207:                last = NULL;
1.22      cgd       208:                for (inp = udbtable.inpt_queue.cqh_first;
                    209:                    inp != (struct inpcb *)&udbtable.inpt_queue;
                    210:                    inp = inp->inp_queue.cqe_next) {
1.4       hpeyerl   211:                        if (inp->inp_lport != uh->uh_dport)
                    212:                                continue;
1.34    ! mycroft   213:                        if (!in_nullhost(inp->inp_laddr)) {
        !           214:                                if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
1.4       hpeyerl   215:                                        continue;
                    216:                        }
1.34    ! mycroft   217:                        if (!in_nullhost(inp->inp_faddr)) {
        !           218:                                if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
1.4       hpeyerl   219:                                    inp->inp_fport != uh->uh_sport)
                    220:                                        continue;
                    221:                        }
                    222:
                    223:                        if (last != NULL) {
                    224:                                struct mbuf *n;
                    225:
                    226:                                if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
                    227:                                        if (sbappendaddr(&last->so_rcv,
1.34    ! mycroft   228:                                                sintosa(&udpsrc), n,
1.17      mycroft   229:                                                (struct mbuf *)0) == 0) {
1.4       hpeyerl   230:                                                m_freem(n);
1.13      mycroft   231:                                                udpstat.udps_fullsock++;
                    232:                                        } else
1.4       hpeyerl   233:                                                sorwakeup(last);
                    234:                                }
                    235:                        }
                    236:                        last = inp->inp_socket;
                    237:                        /*
1.13      mycroft   238:                         * Don't look for additional matches if this one does
                    239:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    240:                         * socket options set.  This heuristic avoids searching
                    241:                         * through all pcbs in the common case of a non-shared
                    242:                         * port.  It * assumes that an application will never
                    243:                         * clear these options after setting them.
1.4       hpeyerl   244:                         */
1.28      christos  245:                        if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
1.4       hpeyerl   246:                                break;
                    247:                }
1.6       mycroft   248:
1.4       hpeyerl   249:                if (last == NULL) {
                    250:                        /*
                    251:                         * No matching pcb found; discard datagram.
                    252:                         * (No need to send an ICMP Port Unreachable
                    253:                         * for a broadcast or multicast datgram.)
                    254:                         */
1.13      mycroft   255:                        udpstat.udps_noportbcast++;
1.4       hpeyerl   256:                        goto bad;
                    257:                }
1.34    ! mycroft   258:                if (sbappendaddr(&last->so_rcv, sintosa(&udpsrc), m,
1.17      mycroft   259:                    (struct mbuf *)0) == 0) {
1.13      mycroft   260:                        udpstat.udps_fullsock++;
1.4       hpeyerl   261:                        goto bad;
1.13      mycroft   262:                }
1.4       hpeyerl   263:                sorwakeup(last);
                    264:                return;
                    265:        }
1.1       cgd       266:        /*
                    267:         * Locate pcb for datagram.
                    268:         */
1.26      mycroft   269:        inp = in_pcbhashlookup(&udbtable, ip->ip_src, uh->uh_sport,
                    270:            ip->ip_dst, uh->uh_dport);
                    271:        if (inp == 0) {
                    272:                ++udpstat.udps_pcbhashmiss;
1.18      mycroft   273:                inp = in_pcblookup(&udbtable, ip->ip_src, uh->uh_sport,
1.1       cgd       274:                    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD);
1.18      mycroft   275:                if (inp == 0) {
                    276:                        udpstat.udps_noport++;
                    277:                        if (m->m_flags & (M_BCAST | M_MCAST)) {
                    278:                                udpstat.udps_noportbcast++;
                    279:                                goto bad;
                    280:                        }
                    281:                        *ip = save_ip;
                    282:                        ip->ip_len += iphlen;
                    283:                        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
                    284:                        return;
1.13      mycroft   285:                }
1.1       cgd       286:        }
                    287:
                    288:        /*
                    289:         * Construct sockaddr format source address.
                    290:         * Stuff source address and datagram in user buffer.
                    291:         */
1.34    ! mycroft   292:        udpsrc.sin_family = AF_INET;
        !           293:        udpsrc.sin_len = sizeof(struct sockaddr_in);
        !           294:        udpsrc.sin_port = uh->uh_sport;
        !           295:        udpsrc.sin_addr = ip->ip_src;
        !           296:
1.1       cgd       297:        if (inp->inp_flags & INP_CONTROLOPTS) {
                    298:                struct mbuf **mp = &opts;
                    299:
                    300:                if (inp->inp_flags & INP_RECVDSTADDR) {
                    301:                        *mp = udp_saveopt((caddr_t) &ip->ip_dst,
                    302:                            sizeof(struct in_addr), IP_RECVDSTADDR);
                    303:                        if (*mp)
                    304:                                mp = &(*mp)->m_next;
                    305:                }
                    306: #ifdef notyet
                    307:                /* options were tossed above */
                    308:                if (inp->inp_flags & INP_RECVOPTS) {
                    309:                        *mp = udp_saveopt((caddr_t) opts_deleted_above,
                    310:                            sizeof(struct in_addr), IP_RECVOPTS);
                    311:                        if (*mp)
                    312:                                mp = &(*mp)->m_next;
                    313:                }
                    314:                /* ip_srcroute doesn't do what we want here, need to fix */
                    315:                if (inp->inp_flags & INP_RECVRETOPTS) {
                    316:                        *mp = udp_saveopt((caddr_t) ip_srcroute(),
                    317:                            sizeof(struct in_addr), IP_RECVRETOPTS);
                    318:                        if (*mp)
                    319:                                mp = &(*mp)->m_next;
                    320:                }
                    321: #endif
                    322:        }
                    323:        iphlen += sizeof(struct udphdr);
                    324:        m->m_len -= iphlen;
                    325:        m->m_pkthdr.len -= iphlen;
                    326:        m->m_data += iphlen;
1.34    ! mycroft   327:        if (sbappendaddr(&inp->inp_socket->so_rcv, sintosa(&udpsrc), m,
1.17      mycroft   328:            opts) == 0) {
1.1       cgd       329:                udpstat.udps_fullsock++;
                    330:                goto bad;
                    331:        }
                    332:        sorwakeup(inp->inp_socket);
                    333:        return;
                    334: bad:
                    335:        m_freem(m);
                    336:        if (opts)
                    337:                m_freem(opts);
                    338: }
                    339:
                    340: /*
                    341:  * Create a "control" mbuf containing the specified data
                    342:  * with the specified type for presentation with a datagram.
                    343:  */
1.13      mycroft   344: struct mbuf *
1.1       cgd       345: udp_saveopt(p, size, type)
                    346:        caddr_t p;
                    347:        register int size;
                    348:        int type;
                    349: {
                    350:        register struct cmsghdr *cp;
                    351:        struct mbuf *m;
                    352:
                    353:        if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
                    354:                return ((struct mbuf *) NULL);
                    355:        cp = (struct cmsghdr *) mtod(m, struct cmsghdr *);
1.13      mycroft   356:        bcopy(p, CMSG_DATA(cp), size);
1.1       cgd       357:        size += sizeof(*cp);
                    358:        m->m_len = size;
                    359:        cp->cmsg_len = size;
                    360:        cp->cmsg_level = IPPROTO_IP;
                    361:        cp->cmsg_type = type;
                    362:        return (m);
                    363: }
                    364:
                    365: /*
                    366:  * Notify a udp user of an asynchronous error;
                    367:  * just wake up so that he can collect error status.
                    368:  */
1.7       mycroft   369: static void
1.1       cgd       370: udp_notify(inp, errno)
                    371:        register struct inpcb *inp;
1.7       mycroft   372:        int errno;
1.1       cgd       373: {
1.34    ! mycroft   374:
1.1       cgd       375:        inp->inp_socket->so_error = errno;
                    376:        sorwakeup(inp->inp_socket);
                    377:        sowwakeup(inp->inp_socket);
                    378: }
                    379:
1.27      christos  380: void *
                    381: udp_ctlinput(cmd, sa, v)
1.1       cgd       382:        int cmd;
                    383:        struct sockaddr *sa;
1.27      christos  384:        void *v;
1.1       cgd       385: {
1.27      christos  386:        register struct ip *ip = v;
1.1       cgd       387:        register struct udphdr *uh;
1.21      mycroft   388:        extern int inetctlerrmap[];
1.18      mycroft   389:        void (*notify) __P((struct inpcb *, int)) = udp_notify;
1.21      mycroft   390:        int errno;
1.1       cgd       391:
1.20      mycroft   392:        if ((unsigned)cmd >= PRC_NCMDS)
1.27      christos  393:                return NULL;
1.20      mycroft   394:        errno = inetctlerrmap[cmd];
1.18      mycroft   395:        if (PRC_IS_REDIRECT(cmd))
1.19      mycroft   396:                notify = in_rtchange, ip = 0;
1.18      mycroft   397:        else if (cmd == PRC_HOSTDEAD)
1.19      mycroft   398:                ip = 0;
1.23      cgd       399:        else if (errno == 0)
1.27      christos  400:                return NULL;
1.19      mycroft   401:        if (ip) {
1.1       cgd       402:                uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34    ! mycroft   403:                in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
        !           404:                    ip->ip_src, uh->uh_sport, errno, notify);
1.19      mycroft   405:        } else
1.34    ! mycroft   406:                in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
        !           407:                    notify);
1.27      christos  408:        return NULL;
1.1       cgd       409: }
                    410:
1.7       mycroft   411: int
1.27      christos  412: #if __STDC__
                    413: udp_output(struct mbuf *m, ...)
                    414: #else
                    415: udp_output(m, va_alist)
                    416:        struct mbuf *m;
                    417:        va_dcl
                    418: #endif
                    419: {
1.1       cgd       420:        register struct inpcb *inp;
                    421:        register struct udpiphdr *ui;
                    422:        register int len = m->m_pkthdr.len;
1.31      mycroft   423:        int error = 0;
1.27      christos  424:        va_list ap;
                    425:
                    426:        va_start(ap, m);
                    427:        inp = va_arg(ap, struct inpcb *);
                    428:        va_end(ap);
1.1       cgd       429:
                    430:        /*
                    431:         * Calculate data length and get a mbuf
                    432:         * for UDP and IP headers.
                    433:         */
1.13      mycroft   434:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
                    435:        if (m == 0) {
                    436:                error = ENOBUFS;
                    437:                goto release;
                    438:        }
1.1       cgd       439:
                    440:        /*
                    441:         * Fill in mbuf with extended UDP header
                    442:         * and addresses and length put into network format.
                    443:         */
                    444:        ui = mtod(m, struct udpiphdr *);
1.25      cgd       445:        bzero(ui->ui_x1, sizeof ui->ui_x1);
1.1       cgd       446:        ui->ui_pr = IPPROTO_UDP;
1.15      cgd       447:        ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1.1       cgd       448:        ui->ui_src = inp->inp_laddr;
                    449:        ui->ui_dst = inp->inp_faddr;
                    450:        ui->ui_sport = inp->inp_lport;
                    451:        ui->ui_dport = inp->inp_fport;
                    452:        ui->ui_ulen = ui->ui_len;
                    453:
                    454:        /*
                    455:         * Stuff checksum and output datagram.
                    456:         */
                    457:        ui->ui_sum = 0;
                    458:        if (udpcksum) {
                    459:            if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
                    460:                ui->ui_sum = 0xffff;
                    461:        }
                    462:        ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
                    463:        ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
                    464:        ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
                    465:        udpstat.udps_opackets++;
1.31      mycroft   466:        return (ip_output(m, inp->inp_options, &inp->inp_route,
1.12      mycroft   467:            inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.31      mycroft   468:            inp->inp_moptions));
1.1       cgd       469:
                    470: release:
                    471:        m_freem(m);
                    472:        return (error);
                    473: }
                    474:
                    475: u_long udp_sendspace = 9216;           /* really max datagram size */
                    476: u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
                    477:                                        /* 40 1K datagrams */
                    478:
                    479: /*ARGSUSED*/
1.7       mycroft   480: int
1.31      mycroft   481: udp_usrreq(so, req, m, nam, control, p)
1.1       cgd       482:        struct socket *so;
                    483:        int req;
1.31      mycroft   484:        struct mbuf *m, *nam, *control;
1.30      mycroft   485:        struct proc *p;
1.1       cgd       486: {
1.31      mycroft   487:        register struct inpcb *inp;
1.1       cgd       488:        int s;
1.31      mycroft   489:        register int error = 0;
1.1       cgd       490:
                    491:        if (req == PRU_CONTROL)
1.31      mycroft   492:                return (in_control(so, (long)m, (caddr_t)nam,
1.30      mycroft   493:                    (struct ifnet *)control, p));
1.31      mycroft   494:
                    495:        s = splsoftnet();
                    496:        inp = sotoinpcb(so);
1.32      mycroft   497: #ifdef DIAGNOSTIC
                    498:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    499:                panic("udp_usrreq: unexpected control mbuf");
                    500: #endif
1.31      mycroft   501:        if (inp == 0 && req != PRU_ATTACH) {
                    502:                error = EINVAL;
                    503:                goto release;
                    504:        }
                    505:
1.1       cgd       506:        /*
                    507:         * Note: need to block udp_input while changing
                    508:         * the udp pcb queue and/or pcb addresses.
                    509:         */
                    510:        switch (req) {
                    511:
                    512:        case PRU_ATTACH:
1.31      mycroft   513:                if (inp != 0) {
                    514:                        error = EISCONN;
1.1       cgd       515:                        break;
                    516:                }
1.31      mycroft   517:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    518:                        error = soreserve(so, udp_sendspace, udp_recvspace);
                    519:                        if (error)
                    520:                                break;
                    521:                }
1.18      mycroft   522:                error = in_pcballoc(so, &udbtable);
1.1       cgd       523:                if (error)
                    524:                        break;
1.31      mycroft   525:                inp = sotoinpcb(so);
                    526:                inp->inp_ip.ip_ttl = ip_defttl;
1.1       cgd       527:                break;
                    528:
                    529:        case PRU_DETACH:
1.31      mycroft   530:                in_pcbdetach(inp);
1.1       cgd       531:                break;
                    532:
                    533:        case PRU_BIND:
1.31      mycroft   534:                error = in_pcbbind(inp, nam, p);
1.1       cgd       535:                break;
                    536:
                    537:        case PRU_LISTEN:
                    538:                error = EOPNOTSUPP;
                    539:                break;
                    540:
                    541:        case PRU_CONNECT:
1.31      mycroft   542:                error = in_pcbconnect(inp, nam);
                    543:                if (error)
1.1       cgd       544:                        break;
1.31      mycroft   545:                soisconnected(so);
1.1       cgd       546:                break;
                    547:
                    548:        case PRU_CONNECT2:
                    549:                error = EOPNOTSUPP;
                    550:                break;
                    551:
                    552:        case PRU_DISCONNECT:
1.31      mycroft   553:                /*soisdisconnected(so);*/
                    554:                so->so_state &= ~SS_ISCONNECTED;        /* XXX */
1.1       cgd       555:                in_pcbdisconnect(inp);
1.34    ! mycroft   556:                inp->inp_laddr = zeroin_addr;           /* XXX */
1.1       cgd       557:                break;
                    558:
                    559:        case PRU_SHUTDOWN:
                    560:                socantsendmore(so);
                    561:                break;
                    562:
1.31      mycroft   563:        case PRU_RCVD:
                    564:                error = EOPNOTSUPP;
1.1       cgd       565:                break;
                    566:
1.31      mycroft   567:        case PRU_SEND:
1.32      mycroft   568:                if (control && control->m_len) {
                    569:                        m_freem(control);
                    570:                        m_freem(m);
                    571:                        error = EINVAL;
                    572:                        break;
                    573:                }
1.31      mycroft   574:        {
                    575:                struct in_addr laddr;
1.1       cgd       576:
1.31      mycroft   577:                if (nam) {
                    578:                        laddr = inp->inp_laddr;
                    579:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
                    580:                                error = EISCONN;
1.32      mycroft   581:                                goto die;
1.31      mycroft   582:                        }
                    583:                        error = in_pcbconnect(inp, nam);
1.32      mycroft   584:                        if (error) {
                    585:                        die:
                    586:                                m_freem(m);
1.31      mycroft   587:                                break;
1.32      mycroft   588:                        }
1.31      mycroft   589:                } else {
                    590:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
                    591:                                error = ENOTCONN;
1.32      mycroft   592:                                goto die;
1.31      mycroft   593:                        }
                    594:                }
1.33      mycroft   595:                error = udp_output(m, inp);
1.31      mycroft   596:                if (nam) {
                    597:                        in_pcbdisconnect(inp);
                    598:                        inp->inp_laddr = laddr;
                    599:                }
                    600:        }
1.1       cgd       601:                break;
                    602:
                    603:        case PRU_SENSE:
                    604:                /*
                    605:                 * stat: don't bother with a blocksize.
                    606:                 */
1.31      mycroft   607:                splx(s);
1.1       cgd       608:                return (0);
                    609:
1.31      mycroft   610:        case PRU_RCVOOB:
                    611:                error =  EOPNOTSUPP;
                    612:                break;
                    613:
1.1       cgd       614:        case PRU_SENDOOB:
1.32      mycroft   615:                m_freem(control);
1.31      mycroft   616:                m_freem(m);
1.1       cgd       617:                error =  EOPNOTSUPP;
                    618:                break;
                    619:
1.31      mycroft   620:        case PRU_SOCKADDR:
                    621:                in_setsockaddr(inp, nam);
                    622:                break;
                    623:
                    624:        case PRU_PEERADDR:
                    625:                in_setpeeraddr(inp, nam);
                    626:                break;
1.1       cgd       627:
                    628:        default:
                    629:                panic("udp_usrreq");
                    630:        }
                    631:
                    632: release:
1.31      mycroft   633:        splx(s);
1.1       cgd       634:        return (error);
1.13      mycroft   635: }
                    636:
                    637: /*
                    638:  * Sysctl for udp variables.
                    639:  */
1.27      christos  640: int
1.13      mycroft   641: udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                    642:        int *name;
                    643:        u_int namelen;
                    644:        void *oldp;
                    645:        size_t *oldlenp;
                    646:        void *newp;
                    647:        size_t newlen;
                    648: {
                    649:        /* All sysctl names at this level are terminal. */
                    650:        if (namelen != 1)
                    651:                return (ENOTDIR);
                    652:
                    653:        switch (name[0]) {
                    654:        case UDPCTL_CHECKSUM:
                    655:                return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
                    656:        default:
                    657:                return (ENOPROTOOPT);
                    658:        }
                    659:        /* NOTREACHED */
1.1       cgd       660: }

CVSweb <webmaster@jp.NetBSD.org>