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

1.27    ! christos    1: /*     $NetBSD: udp_usrreq.c,v 1.26 1996/01/31 03:49:38 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:
                     77: struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
1.1       cgd        78:
1.13      mycroft    79: static void udp_detach __P((struct inpcb *));
                     80: static void udp_notify __P((struct inpcb *, int));
                     81: static struct mbuf *udp_saveopt __P((caddr_t, int, int));
1.7       mycroft    82:
1.26      mycroft    83: #ifndef UDBHASHSIZE
                     84: #define        UDBHASHSIZE     128
                     85: #endif
                     86: int    udbhashsize = UDBHASHSIZE;
                     87:
1.7       mycroft    88: void
1.1       cgd        89: udp_init()
                     90: {
1.18      mycroft    91:
1.26      mycroft    92:        in_pcbinit(&udbtable, udbhashsize);
1.1       cgd        93: }
                     94:
1.7       mycroft    95: void
1.27    ! christos   96: #if __STDC__
        !            97: udp_input(struct mbuf *m, ...)
        !            98: #else
        !            99: udp_input(m, va_alist)
        !           100:        struct mbuf *m;
        !           101:        va_dcl
        !           102: #endif
1.1       cgd       103: {
                    104:        register struct ip *ip;
                    105:        register struct udphdr *uh;
                    106:        register struct inpcb *inp;
                    107:        struct mbuf *opts = 0;
                    108:        int len;
                    109:        struct ip save_ip;
1.27    ! christos  110:        int iphlen;
        !           111:        va_list ap;
        !           112:
        !           113:        va_start(ap, m);
        !           114:        iphlen = va_arg(ap, int);
        !           115:        va_end(ap);
1.1       cgd       116:
                    117:        udpstat.udps_ipackets++;
                    118:
                    119:        /*
                    120:         * Strip IP options, if any; should skip this,
                    121:         * make available to user, and use on returned packets,
                    122:         * but we don't yet have a way to check the checksum
                    123:         * with options still present.
                    124:         */
                    125:        if (iphlen > sizeof (struct ip)) {
                    126:                ip_stripoptions(m, (struct mbuf *)0);
                    127:                iphlen = sizeof(struct ip);
                    128:        }
                    129:
                    130:        /*
                    131:         * Get IP and UDP header together in first mbuf.
                    132:         */
                    133:        ip = mtod(m, struct ip *);
                    134:        if (m->m_len < iphlen + sizeof(struct udphdr)) {
                    135:                if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
                    136:                        udpstat.udps_hdrops++;
                    137:                        return;
                    138:                }
                    139:                ip = mtod(m, struct ip *);
                    140:        }
                    141:        uh = (struct udphdr *)((caddr_t)ip + iphlen);
                    142:
                    143:        /*
                    144:         * Make mbuf data length reflect UDP length.
                    145:         * If not enough data to reflect UDP length, drop.
                    146:         */
1.15      cgd       147:        len = ntohs((u_int16_t)uh->uh_ulen);
1.1       cgd       148:        if (ip->ip_len != len) {
                    149:                if (len > ip->ip_len) {
                    150:                        udpstat.udps_badlen++;
                    151:                        goto bad;
                    152:                }
                    153:                m_adj(m, len - ip->ip_len);
                    154:                /* ip->ip_len = len; */
                    155:        }
                    156:        /*
                    157:         * Save a copy of the IP header in case we want restore it
                    158:         * for sending an ICMP error message in response.
                    159:         */
                    160:        save_ip = *ip;
                    161:
                    162:        /*
                    163:         * Checksum extended UDP header and data.
                    164:         */
                    165:        if (udpcksum && uh->uh_sum) {
1.25      cgd       166:                bzero(((struct ipovly *)ip)->ih_x1,
                    167:                    sizeof ((struct ipovly *)ip)->ih_x1);
1.1       cgd       168:                ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
1.27    ! christos  169:                if ((uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) != 0) {
1.1       cgd       170:                        udpstat.udps_badsum++;
                    171:                        m_freem(m);
                    172:                        return;
                    173:                }
                    174:        }
1.13      mycroft   175:
1.16      mycroft   176:        if (IN_MULTICAST(ip->ip_dst.s_addr) ||
1.13      mycroft   177:            in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1.4       hpeyerl   178:                struct socket *last;
                    179:                /*
                    180:                 * Deliver a multicast or broadcast datagram to *all* sockets
                    181:                 * for which the local and remote addresses and ports match
                    182:                 * those of the incoming datagram.  This allows more than
                    183:                 * one process to receive multi/broadcasts on the same port.
                    184:                 * (This really ought to be done for unicast datagrams as
                    185:                 * well, but that would cause problems with existing
                    186:                 * applications that open both address-specific sockets and
                    187:                 * a wildcard socket listening to the same port -- they would
                    188:                 * end up receiving duplicates of every unicast datagram.
                    189:                 * Those applications open the multiple sockets to overcome an
                    190:                 * inadequacy of the UDP socket interface, but for backwards
                    191:                 * compatibility we avoid the problem here rather than
1.13      mycroft   192:                 * fixing the interface.  Maybe 4.5BSD will remedy this?)
1.4       hpeyerl   193:                 */
1.13      mycroft   194:
1.4       hpeyerl   195:                /*
                    196:                 * Construct sockaddr format source address.
                    197:                 */
                    198:                udp_in.sin_port = uh->uh_sport;
                    199:                udp_in.sin_addr = ip->ip_src;
                    200:                m->m_len -= sizeof (struct udpiphdr);
                    201:                m->m_data += sizeof (struct udpiphdr);
                    202:                /*
                    203:                 * Locate pcb(s) for datagram.
                    204:                 * (Algorithm copied from raw_intr().)
                    205:                 */
                    206:                last = NULL;
1.22      cgd       207:                for (inp = udbtable.inpt_queue.cqh_first;
                    208:                    inp != (struct inpcb *)&udbtable.inpt_queue;
                    209:                    inp = inp->inp_queue.cqe_next) {
1.4       hpeyerl   210:                        if (inp->inp_lport != uh->uh_dport)
                    211:                                continue;
                    212:                        if (inp->inp_laddr.s_addr != INADDR_ANY) {
                    213:                                if (inp->inp_laddr.s_addr !=
                    214:                                    ip->ip_dst.s_addr)
                    215:                                        continue;
                    216:                        }
                    217:                        if (inp->inp_faddr.s_addr != INADDR_ANY) {
1.6       mycroft   218:                                if (inp->inp_faddr.s_addr !=
1.4       hpeyerl   219:                                    ip->ip_src.s_addr ||
                    220:                                    inp->inp_fport != uh->uh_sport)
                    221:                                        continue;
                    222:                        }
                    223:
                    224:                        if (last != NULL) {
                    225:                                struct mbuf *n;
                    226:
                    227:                                if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
                    228:                                        if (sbappendaddr(&last->so_rcv,
1.17      mycroft   229:                                                sintosa(&udp_in), n,
                    230:                                                (struct mbuf *)0) == 0) {
1.4       hpeyerl   231:                                                m_freem(n);
1.13      mycroft   232:                                                udpstat.udps_fullsock++;
                    233:                                        } else
1.4       hpeyerl   234:                                                sorwakeup(last);
                    235:                                }
                    236:                        }
                    237:                        last = inp->inp_socket;
                    238:                        /*
1.13      mycroft   239:                         * Don't look for additional matches if this one does
                    240:                         * not have either the SO_REUSEPORT or SO_REUSEADDR
                    241:                         * socket options set.  This heuristic avoids searching
                    242:                         * through all pcbs in the common case of a non-shared
                    243:                         * port.  It * assumes that an application will never
                    244:                         * clear these options after setting them.
1.4       hpeyerl   245:                         */
1.13      mycroft   246:                        if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR) == 0))
1.4       hpeyerl   247:                                break;
                    248:                }
1.6       mycroft   249:
1.4       hpeyerl   250:                if (last == NULL) {
                    251:                        /*
                    252:                         * No matching pcb found; discard datagram.
                    253:                         * (No need to send an ICMP Port Unreachable
                    254:                         * for a broadcast or multicast datgram.)
                    255:                         */
1.13      mycroft   256:                        udpstat.udps_noportbcast++;
1.4       hpeyerl   257:                        goto bad;
                    258:                }
1.17      mycroft   259:                if (sbappendaddr(&last->so_rcv, sintosa(&udp_in), m,
                    260:                    (struct mbuf *)0) == 0) {
1.13      mycroft   261:                        udpstat.udps_fullsock++;
1.4       hpeyerl   262:                        goto bad;
1.13      mycroft   263:                }
1.4       hpeyerl   264:                sorwakeup(last);
                    265:                return;
                    266:        }
1.1       cgd       267:        /*
                    268:         * Locate pcb for datagram.
                    269:         */
1.26      mycroft   270:        inp = in_pcbhashlookup(&udbtable, ip->ip_src, uh->uh_sport,
                    271:            ip->ip_dst, uh->uh_dport);
                    272:        if (inp == 0) {
                    273:                ++udpstat.udps_pcbhashmiss;
1.18      mycroft   274:                inp = in_pcblookup(&udbtable, ip->ip_src, uh->uh_sport,
1.1       cgd       275:                    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD);
1.18      mycroft   276:                if (inp == 0) {
                    277:                        udpstat.udps_noport++;
                    278:                        if (m->m_flags & (M_BCAST | M_MCAST)) {
                    279:                                udpstat.udps_noportbcast++;
                    280:                                goto bad;
                    281:                        }
                    282:                        *ip = save_ip;
                    283:                        ip->ip_len += iphlen;
                    284:                        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
                    285:                        return;
1.13      mycroft   286:                }
1.1       cgd       287:        }
                    288:
                    289:        /*
                    290:         * Construct sockaddr format source address.
                    291:         * Stuff source address and datagram in user buffer.
                    292:         */
                    293:        udp_in.sin_port = uh->uh_sport;
                    294:        udp_in.sin_addr = ip->ip_src;
                    295:        if (inp->inp_flags & INP_CONTROLOPTS) {
                    296:                struct mbuf **mp = &opts;
                    297:
                    298:                if (inp->inp_flags & INP_RECVDSTADDR) {
                    299:                        *mp = udp_saveopt((caddr_t) &ip->ip_dst,
                    300:                            sizeof(struct in_addr), IP_RECVDSTADDR);
                    301:                        if (*mp)
                    302:                                mp = &(*mp)->m_next;
                    303:                }
                    304: #ifdef notyet
                    305:                /* options were tossed above */
                    306:                if (inp->inp_flags & INP_RECVOPTS) {
                    307:                        *mp = udp_saveopt((caddr_t) opts_deleted_above,
                    308:                            sizeof(struct in_addr), IP_RECVOPTS);
                    309:                        if (*mp)
                    310:                                mp = &(*mp)->m_next;
                    311:                }
                    312:                /* ip_srcroute doesn't do what we want here, need to fix */
                    313:                if (inp->inp_flags & INP_RECVRETOPTS) {
                    314:                        *mp = udp_saveopt((caddr_t) ip_srcroute(),
                    315:                            sizeof(struct in_addr), IP_RECVRETOPTS);
                    316:                        if (*mp)
                    317:                                mp = &(*mp)->m_next;
                    318:                }
                    319: #endif
                    320:        }
                    321:        iphlen += sizeof(struct udphdr);
                    322:        m->m_len -= iphlen;
                    323:        m->m_pkthdr.len -= iphlen;
                    324:        m->m_data += iphlen;
1.17      mycroft   325:        if (sbappendaddr(&inp->inp_socket->so_rcv, sintosa(&udp_in), m,
                    326:            opts) == 0) {
1.1       cgd       327:                udpstat.udps_fullsock++;
                    328:                goto bad;
                    329:        }
                    330:        sorwakeup(inp->inp_socket);
                    331:        return;
                    332: bad:
                    333:        m_freem(m);
                    334:        if (opts)
                    335:                m_freem(opts);
                    336: }
                    337:
                    338: /*
                    339:  * Create a "control" mbuf containing the specified data
                    340:  * with the specified type for presentation with a datagram.
                    341:  */
1.13      mycroft   342: struct mbuf *
1.1       cgd       343: udp_saveopt(p, size, type)
                    344:        caddr_t p;
                    345:        register int size;
                    346:        int type;
                    347: {
                    348:        register struct cmsghdr *cp;
                    349:        struct mbuf *m;
                    350:
                    351:        if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
                    352:                return ((struct mbuf *) NULL);
                    353:        cp = (struct cmsghdr *) mtod(m, struct cmsghdr *);
1.13      mycroft   354:        bcopy(p, CMSG_DATA(cp), size);
1.1       cgd       355:        size += sizeof(*cp);
                    356:        m->m_len = size;
                    357:        cp->cmsg_len = size;
                    358:        cp->cmsg_level = IPPROTO_IP;
                    359:        cp->cmsg_type = type;
                    360:        return (m);
                    361: }
                    362:
                    363: /*
                    364:  * Notify a udp user of an asynchronous error;
                    365:  * just wake up so that he can collect error status.
                    366:  */
1.7       mycroft   367: static void
1.1       cgd       368: udp_notify(inp, errno)
                    369:        register struct inpcb *inp;
1.7       mycroft   370:        int errno;
1.1       cgd       371: {
                    372:        inp->inp_socket->so_error = errno;
                    373:        sorwakeup(inp->inp_socket);
                    374:        sowwakeup(inp->inp_socket);
                    375: }
                    376:
1.27    ! christos  377: void *
        !           378: udp_ctlinput(cmd, sa, v)
1.1       cgd       379:        int cmd;
                    380:        struct sockaddr *sa;
1.27    ! christos  381:        void *v;
1.1       cgd       382: {
1.27    ! christos  383:        register struct ip *ip = v;
1.1       cgd       384:        register struct udphdr *uh;
1.21      mycroft   385:        extern int inetctlerrmap[];
1.18      mycroft   386:        void (*notify) __P((struct inpcb *, int)) = udp_notify;
1.21      mycroft   387:        int errno;
1.1       cgd       388:
1.20      mycroft   389:        if ((unsigned)cmd >= PRC_NCMDS)
1.27    ! christos  390:                return NULL;
1.20      mycroft   391:        errno = inetctlerrmap[cmd];
1.18      mycroft   392:        if (PRC_IS_REDIRECT(cmd))
1.19      mycroft   393:                notify = in_rtchange, ip = 0;
1.18      mycroft   394:        else if (cmd == PRC_HOSTDEAD)
1.19      mycroft   395:                ip = 0;
1.23      cgd       396:        else if (errno == 0)
1.27    ! christos  397:                return NULL;
1.19      mycroft   398:        if (ip) {
1.1       cgd       399:                uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.18      mycroft   400:                in_pcbnotify(&udbtable, sa, uh->uh_dport, ip->ip_src,
1.20      mycroft   401:                    uh->uh_sport, errno, notify);
1.19      mycroft   402:        } else
1.20      mycroft   403:                in_pcbnotifyall(&udbtable, sa, errno, notify);
1.27    ! christos  404:        return NULL;
1.1       cgd       405: }
                    406:
1.7       mycroft   407: int
1.27    ! christos  408: #if __STDC__
        !           409: udp_output(struct mbuf *m, ...)
        !           410: #else
        !           411: udp_output(m, va_alist)
        !           412:        struct mbuf *m;
        !           413:        va_dcl
        !           414: #endif
        !           415: {
1.1       cgd       416:        register struct inpcb *inp;
                    417:        struct mbuf *addr, *control;
                    418:        register struct udpiphdr *ui;
                    419:        register int len = m->m_pkthdr.len;
                    420:        struct in_addr laddr;
1.27    ! christos  421:        int s = 0, error = 0;
        !           422:        va_list ap;
        !           423:
        !           424:        va_start(ap, m);
        !           425:        inp = va_arg(ap, struct inpcb *);
        !           426:        addr = va_arg(ap, struct mbuf *);
        !           427:        control = va_arg(ap, struct mbuf *);
        !           428:        va_end(ap);
1.1       cgd       429:
                    430:        if (control)
                    431:                m_freem(control);               /* XXX */
                    432:
                    433:        if (addr) {
                    434:                laddr = inp->inp_laddr;
                    435:                if (inp->inp_faddr.s_addr != INADDR_ANY) {
                    436:                        error = EISCONN;
                    437:                        goto release;
                    438:                }
                    439:                /*
                    440:                 * Must block input while temporarily connected.
                    441:                 */
1.24      mycroft   442:                s = splsoftnet();
1.1       cgd       443:                error = in_pcbconnect(inp, addr);
                    444:                if (error) {
                    445:                        splx(s);
                    446:                        goto release;
                    447:                }
                    448:        } else {
                    449:                if (inp->inp_faddr.s_addr == INADDR_ANY) {
                    450:                        error = ENOTCONN;
                    451:                        goto release;
                    452:                }
                    453:        }
                    454:        /*
                    455:         * Calculate data length and get a mbuf
                    456:         * for UDP and IP headers.
                    457:         */
1.13      mycroft   458:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
                    459:        if (m == 0) {
                    460:                error = ENOBUFS;
                    461:                goto release;
                    462:        }
1.1       cgd       463:
                    464:        /*
                    465:         * Fill in mbuf with extended UDP header
                    466:         * and addresses and length put into network format.
                    467:         */
                    468:        ui = mtod(m, struct udpiphdr *);
1.25      cgd       469:        bzero(ui->ui_x1, sizeof ui->ui_x1);
1.1       cgd       470:        ui->ui_pr = IPPROTO_UDP;
1.15      cgd       471:        ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1.1       cgd       472:        ui->ui_src = inp->inp_laddr;
                    473:        ui->ui_dst = inp->inp_faddr;
                    474:        ui->ui_sport = inp->inp_lport;
                    475:        ui->ui_dport = inp->inp_fport;
                    476:        ui->ui_ulen = ui->ui_len;
                    477:
                    478:        /*
                    479:         * Stuff checksum and output datagram.
                    480:         */
                    481:        ui->ui_sum = 0;
                    482:        if (udpcksum) {
                    483:            if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
                    484:                ui->ui_sum = 0xffff;
                    485:        }
                    486:        ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
                    487:        ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
                    488:        ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
                    489:        udpstat.udps_opackets++;
                    490:        error = ip_output(m, inp->inp_options, &inp->inp_route,
1.12      mycroft   491:            inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
                    492:            inp->inp_moptions);
1.1       cgd       493:
                    494:        if (addr) {
                    495:                in_pcbdisconnect(inp);
                    496:                inp->inp_laddr = laddr;
                    497:                splx(s);
                    498:        }
                    499:        return (error);
                    500:
                    501: release:
                    502:        m_freem(m);
                    503:        return (error);
                    504: }
                    505:
                    506: u_long udp_sendspace = 9216;           /* really max datagram size */
                    507: u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
                    508:                                        /* 40 1K datagrams */
                    509:
                    510: /*ARGSUSED*/
1.7       mycroft   511: int
1.1       cgd       512: udp_usrreq(so, req, m, addr, control)
                    513:        struct socket *so;
                    514:        int req;
                    515:        struct mbuf *m, *addr, *control;
                    516: {
                    517:        struct inpcb *inp = sotoinpcb(so);
                    518:        int error = 0;
                    519:        int s;
                    520:
                    521:        if (req == PRU_CONTROL)
1.15      cgd       522:                return (in_control(so, (long)m, (caddr_t)addr,
1.1       cgd       523:                        (struct ifnet *)control));
                    524:        if (inp == NULL && req != PRU_ATTACH) {
                    525:                error = EINVAL;
                    526:                goto release;
                    527:        }
                    528:        /*
                    529:         * Note: need to block udp_input while changing
                    530:         * the udp pcb queue and/or pcb addresses.
                    531:         */
                    532:        switch (req) {
                    533:
                    534:        case PRU_ATTACH:
                    535:                if (inp != NULL) {
                    536:                        error = EINVAL;
                    537:                        break;
                    538:                }
1.24      mycroft   539:                s = splsoftnet();
1.18      mycroft   540:                error = in_pcballoc(so, &udbtable);
1.1       cgd       541:                splx(s);
                    542:                if (error)
                    543:                        break;
                    544:                error = soreserve(so, udp_sendspace, udp_recvspace);
                    545:                if (error)
                    546:                        break;
1.13      mycroft   547:                ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl;
1.1       cgd       548:                break;
                    549:
                    550:        case PRU_DETACH:
                    551:                udp_detach(inp);
                    552:                break;
                    553:
                    554:        case PRU_BIND:
1.24      mycroft   555:                s = splsoftnet();
1.1       cgd       556:                error = in_pcbbind(inp, addr);
                    557:                splx(s);
                    558:                break;
                    559:
                    560:        case PRU_LISTEN:
                    561:                error = EOPNOTSUPP;
                    562:                break;
                    563:
                    564:        case PRU_CONNECT:
                    565:                if (inp->inp_faddr.s_addr != INADDR_ANY) {
                    566:                        error = EISCONN;
                    567:                        break;
                    568:                }
1.24      mycroft   569:                s = splsoftnet();
1.1       cgd       570:                error = in_pcbconnect(inp, addr);
                    571:                splx(s);
                    572:                if (error == 0)
                    573:                        soisconnected(so);
                    574:                break;
                    575:
                    576:        case PRU_CONNECT2:
                    577:                error = EOPNOTSUPP;
                    578:                break;
                    579:
                    580:        case PRU_ACCEPT:
                    581:                error = EOPNOTSUPP;
                    582:                break;
                    583:
                    584:        case PRU_DISCONNECT:
                    585:                if (inp->inp_faddr.s_addr == INADDR_ANY) {
                    586:                        error = ENOTCONN;
                    587:                        break;
                    588:                }
1.24      mycroft   589:                s = splsoftnet();
1.1       cgd       590:                in_pcbdisconnect(inp);
                    591:                inp->inp_laddr.s_addr = INADDR_ANY;
                    592:                splx(s);
                    593:                so->so_state &= ~SS_ISCONNECTED;                /* XXX */
                    594:                break;
                    595:
                    596:        case PRU_SHUTDOWN:
                    597:                socantsendmore(so);
                    598:                break;
                    599:
                    600:        case PRU_SEND:
1.27    ! christos  601:                return (udp_output(m, inp, addr, control));
1.1       cgd       602:
                    603:        case PRU_ABORT:
                    604:                soisdisconnected(so);
                    605:                udp_detach(inp);
                    606:                break;
                    607:
                    608:        case PRU_SOCKADDR:
                    609:                in_setsockaddr(inp, addr);
                    610:                break;
                    611:
                    612:        case PRU_PEERADDR:
                    613:                in_setpeeraddr(inp, addr);
                    614:                break;
                    615:
                    616:        case PRU_SENSE:
                    617:                /*
                    618:                 * stat: don't bother with a blocksize.
                    619:                 */
                    620:                return (0);
                    621:
                    622:        case PRU_SENDOOB:
                    623:        case PRU_FASTTIMO:
                    624:        case PRU_SLOWTIMO:
                    625:        case PRU_PROTORCV:
                    626:        case PRU_PROTOSEND:
                    627:                error =  EOPNOTSUPP;
                    628:                break;
                    629:
                    630:        case PRU_RCVD:
                    631:        case PRU_RCVOOB:
                    632:                return (EOPNOTSUPP);    /* do not free mbuf's */
                    633:
                    634:        default:
                    635:                panic("udp_usrreq");
                    636:        }
                    637:
                    638: release:
                    639:        if (control) {
                    640:                printf("udp control data unexpectedly retained\n");
                    641:                m_freem(control);
                    642:        }
                    643:        if (m)
                    644:                m_freem(m);
                    645:        return (error);
                    646: }
                    647:
1.7       mycroft   648: static void
1.1       cgd       649: udp_detach(inp)
                    650:        struct inpcb *inp;
                    651: {
1.24      mycroft   652:        int s = splsoftnet();
1.1       cgd       653:
                    654:        in_pcbdetach(inp);
                    655:        splx(s);
1.13      mycroft   656: }
                    657:
                    658: /*
                    659:  * Sysctl for udp variables.
                    660:  */
1.27    ! christos  661: int
1.13      mycroft   662: udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                    663:        int *name;
                    664:        u_int namelen;
                    665:        void *oldp;
                    666:        size_t *oldlenp;
                    667:        void *newp;
                    668:        size_t newlen;
                    669: {
                    670:        /* All sysctl names at this level are terminal. */
                    671:        if (namelen != 1)
                    672:                return (ENOTDIR);
                    673:
                    674:        switch (name[0]) {
                    675:        case UDPCTL_CHECKSUM:
                    676:                return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
                    677:        default:
                    678:                return (ENOPROTOOPT);
                    679:        }
                    680:        /* NOTREACHED */
1.1       cgd       681: }

CVSweb <webmaster@jp.NetBSD.org>