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

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

CVSweb <webmaster@jp.NetBSD.org>