Annotation of Net2/netns/idp_usrreq.c, revision 1.1.1.2

1.1       root        1: /*
                      2:  * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *     This product includes software developed by the University of
                     16:  *     California, Berkeley and its contributors.
                     17:  * 4. Neither the name of the University nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  *
1.1.1.2 ! root       33:  *     from: @(#)idp_usrreq.c  7.11 (Berkeley) 6/27/91
        !            34:  *     idp_usrreq.c,v 1.2 1993/05/20 04:35:47 cgd Exp
1.1       root       35:  */
                     36: 
                     37: #include "param.h"
                     38: #include "malloc.h"
                     39: #include "mbuf.h"
                     40: #include "protosw.h"
                     41: #include "socket.h"
                     42: #include "socketvar.h"
                     43: #include "errno.h"
                     44: #include "stat.h"
                     45: 
                     46: #include "../net/if.h"
                     47: #include "../net/route.h"
                     48: 
                     49: #include "ns.h"
                     50: #include "ns_pcb.h"
                     51: #include "ns_if.h"
                     52: #include "idp.h"
                     53: #include "idp_var.h"
                     54: #include "ns_error.h"
                     55: 
                     56: /*
                     57:  * IDP protocol implementation.
                     58:  */
                     59: 
                     60: struct sockaddr_ns idp_ns = { sizeof(idp_ns), AF_NS };
                     61: 
                     62: /*
                     63:  *  This may also be called for raw listeners.
                     64:  */
                     65: idp_input(m, nsp)
                     66:        struct mbuf *m;
                     67:        register struct nspcb *nsp;
                     68: {
                     69:        register struct idp *idp = mtod(m, struct idp *);
                     70:        struct ifnet *ifp = m->m_pkthdr.rcvif;
                     71: 
                     72:        if (nsp==0)
                     73:                panic("No nspcb");
                     74:        /*
                     75:         * Construct sockaddr format source address.
                     76:         * Stuff source address and datagram in user buffer.
                     77:         */
                     78:        idp_ns.sns_addr = idp->idp_sna;
                     79:        if (ns_neteqnn(idp->idp_sna.x_net, ns_zeronet) && ifp) {
                     80:                register struct ifaddr *ifa;
                     81: 
                     82:                for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
                     83:                        if (ifa->ifa_addr->sa_family == AF_NS) {
                     84:                                idp_ns.sns_addr.x_net =
                     85:                                        IA_SNS(ifa)->sns_addr.x_net;
                     86:                                break;
                     87:                        }
                     88:                }
                     89:        }
                     90:        nsp->nsp_rpt = idp->idp_pt;
                     91:        if ( ! (nsp->nsp_flags & NSP_RAWIN) ) {
                     92:                m->m_len -= sizeof (struct idp);
                     93:                m->m_pkthdr.len -= sizeof (struct idp);
                     94:                m->m_data += sizeof (struct idp);
                     95:        }
                     96:        if (sbappendaddr(&nsp->nsp_socket->so_rcv, (struct sockaddr *)&idp_ns,
                     97:            m, (struct mbuf *)0) == 0)
                     98:                goto bad;
                     99:        sorwakeup(nsp->nsp_socket);
                    100:        return;
                    101: bad:
                    102:        m_freem(m);
                    103: }
                    104: 
                    105: idp_abort(nsp)
                    106:        struct nspcb *nsp;
                    107: {
                    108:        struct socket *so = nsp->nsp_socket;
                    109: 
                    110:        ns_pcbdisconnect(nsp);
                    111:        soisdisconnected(so);
                    112: }
                    113: /*
                    114:  * Drop connection, reporting
                    115:  * the specified error.
                    116:  */
                    117: struct nspcb *
                    118: idp_drop(nsp, errno)
                    119:        register struct nspcb *nsp;
                    120:        int errno;
                    121: {
                    122:        struct socket *so = nsp->nsp_socket;
                    123: 
                    124:        /*
                    125:         * someday, in the xerox world
                    126:         * we will generate error protocol packets
                    127:         * announcing that the socket has gone away.
                    128:         */
                    129:        /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
                    130:                tp->t_state = TCPS_CLOSED;
                    131:                (void) tcp_output(tp);
                    132:        }*/
                    133:        so->so_error = errno;
                    134:        ns_pcbdisconnect(nsp);
                    135:        soisdisconnected(so);
                    136: }
                    137: 
                    138: int noIdpRoute;
                    139: idp_output(nsp, m0)
                    140:        struct nspcb *nsp;
                    141:        struct mbuf *m0;
                    142: {
                    143:        register struct mbuf *m;
                    144:        register struct idp *idp;
                    145:        register struct socket *so;
                    146:        register int len = 0;
                    147:        register struct route *ro;
                    148:        struct mbuf *mprev;
                    149:        extern int idpcksum;
                    150: 
                    151:        /*
                    152:         * Calculate data length.
                    153:         */
                    154:        for (m = m0; m; m = m->m_next) {
                    155:                mprev = m;
                    156:                len += m->m_len;
                    157:        }
                    158:        /*
                    159:         * Make sure packet is actually of even length.
                    160:         */
                    161:        
                    162:        if (len & 1) {
                    163:                m = mprev;
                    164:                if ((m->m_flags & M_EXT) == 0 &&
                    165:                        (m->m_len + m->m_data < &m->m_dat[MLEN])) {
                    166:                        m->m_len++;
                    167:                } else {
                    168:                        struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
                    169: 
                    170:                        if (m1 == 0) {
                    171:                                m_freem(m0);
                    172:                                return (ENOBUFS);
                    173:                        }
                    174:                        m1->m_len = 1;
                    175:                        * mtod(m1, char *) = 0;
                    176:                        m->m_next = m1;
                    177:                }
                    178:                m0->m_pkthdr.len++;
                    179:        }
                    180: 
                    181:        /*
                    182:         * Fill in mbuf with extended IDP header
                    183:         * and addresses and length put into network format.
                    184:         */
                    185:        m = m0;
                    186:        if (nsp->nsp_flags & NSP_RAWOUT) {
                    187:                idp = mtod(m, struct idp *);
                    188:        } else {
                    189:                M_PREPEND(m, sizeof (struct idp), M_DONTWAIT);
                    190:                if (m == 0)
                    191:                        return (ENOBUFS);
                    192:                idp = mtod(m, struct idp *);
                    193:                idp->idp_tc = 0;
                    194:                idp->idp_pt = nsp->nsp_dpt;
                    195:                idp->idp_sna = nsp->nsp_laddr;
                    196:                idp->idp_dna = nsp->nsp_faddr;
                    197:                len += sizeof (struct idp);
                    198:        }
                    199: 
                    200:        idp->idp_len = htons((u_short)len);
                    201: 
                    202:        if (idpcksum) {
                    203:                idp->idp_sum = 0;
                    204:                len = ((len - 1) | 1) + 1;
                    205:                idp->idp_sum = ns_cksum(m, len);
                    206:        } else
                    207:                idp->idp_sum = 0xffff;
                    208: 
                    209:        /*
                    210:         * Output datagram.
                    211:         */
                    212:        so = nsp->nsp_socket;
                    213:        if (so->so_options & SO_DONTROUTE)
                    214:                return (ns_output(m, (struct route *)0,
                    215:                    (so->so_options & SO_BROADCAST) | NS_ROUTETOIF));
                    216:        /*
                    217:         * Use cached route for previous datagram if
                    218:         * possible.  If the previous net was the same
                    219:         * and the interface was a broadcast medium, or
                    220:         * if the previous destination was identical,
                    221:         * then we are ok.
                    222:         *
                    223:         * NB: We don't handle broadcasts because that
                    224:         *     would require 3 subroutine calls.
                    225:         */
                    226:        ro = &nsp->nsp_route;
                    227: #ifdef ancient_history
                    228:        /*
                    229:         * I think that this will all be handled in ns_pcbconnect!
                    230:         */
                    231:        if (ro->ro_rt) {
                    232:                if(ns_neteq(nsp->nsp_lastdst, idp->idp_dna)) {
                    233:                        /*
                    234:                         * This assumes we have no GH type routes
                    235:                         */
                    236:                        if (ro->ro_rt->rt_flags & RTF_HOST) {
                    237:                                if (!ns_hosteq(nsp->nsp_lastdst, idp->idp_dna))
                    238:                                        goto re_route;
                    239: 
                    240:                        }
                    241:                        if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
                    242:                                register struct ns_addr *dst =
                    243:                                                &satons_addr(ro->ro_dst);
                    244:                                dst->x_host = idp->idp_dna.x_host;
                    245:                        }
                    246:                        /* 
                    247:                         * Otherwise, we go through the same gateway
                    248:                         * and dst is already set up.
                    249:                         */
                    250:                } else {
                    251:                re_route:
                    252:                        RTFREE(ro->ro_rt);
                    253:                        ro->ro_rt = (struct rtentry *)0;
                    254:                }
                    255:        }
                    256:        nsp->nsp_lastdst = idp->idp_dna;
                    257: #endif ancient_history
                    258:        if (noIdpRoute) ro = 0;
                    259:        return (ns_output(m, ro, so->so_options & SO_BROADCAST));
                    260: }
                    261: /* ARGSUSED */
                    262: idp_ctloutput(req, so, level, name, value)
                    263:        int req, level;
                    264:        struct socket *so;
                    265:        int name;
                    266:        struct mbuf **value;
                    267: {
                    268:        register struct mbuf *m;
                    269:        struct nspcb *nsp = sotonspcb(so);
                    270:        int mask, error = 0;
                    271:        extern long ns_pexseq;
                    272: 
                    273:        if (nsp == NULL)
                    274:                return (EINVAL);
                    275: 
                    276:        switch (req) {
                    277: 
                    278:        case PRCO_GETOPT:
                    279:                if (value==NULL)
                    280:                        return (EINVAL);
                    281:                m = m_get(M_DONTWAIT, MT_DATA);
                    282:                if (m==NULL)
                    283:                        return (ENOBUFS);
                    284:                switch (name) {
                    285: 
                    286:                case SO_ALL_PACKETS:
                    287:                        mask = NSP_ALL_PACKETS;
                    288:                        goto get_flags;
                    289: 
                    290:                case SO_HEADERS_ON_INPUT:
                    291:                        mask = NSP_RAWIN;
                    292:                        goto get_flags;
                    293:                        
                    294:                case SO_HEADERS_ON_OUTPUT:
                    295:                        mask = NSP_RAWOUT;
                    296:                get_flags:
                    297:                        m->m_len = sizeof(short);
                    298:                        *mtod(m, short *) = nsp->nsp_flags & mask;
                    299:                        break;
                    300: 
                    301:                case SO_DEFAULT_HEADERS:
                    302:                        m->m_len = sizeof(struct idp);
                    303:                        {
                    304:                                register struct idp *idp = mtod(m, struct idp *);
                    305:                                idp->idp_len = 0;
                    306:                                idp->idp_sum = 0;
                    307:                                idp->idp_tc = 0;
                    308:                                idp->idp_pt = nsp->nsp_dpt;
                    309:                                idp->idp_dna = nsp->nsp_faddr;
                    310:                                idp->idp_sna = nsp->nsp_laddr;
                    311:                        }
                    312:                        break;
                    313: 
                    314:                case SO_SEQNO:
                    315:                        m->m_len = sizeof(long);
                    316:                        *mtod(m, long *) = ns_pexseq++;
                    317:                        break;
                    318: 
                    319:                default:
                    320:                        error = EINVAL;
                    321:                }
                    322:                *value = m;
                    323:                break;
                    324: 
                    325:        case PRCO_SETOPT:
                    326:                switch (name) {
                    327:                        int *ok;
                    328: 
                    329:                case SO_ALL_PACKETS:
                    330:                        mask = NSP_ALL_PACKETS;
                    331:                        goto set_head;
                    332: 
                    333:                case SO_HEADERS_ON_INPUT:
                    334:                        mask = NSP_RAWIN;
                    335:                        goto set_head;
                    336: 
                    337:                case SO_HEADERS_ON_OUTPUT:
                    338:                        mask = NSP_RAWOUT;
                    339:                set_head:
                    340:                        if (value && *value) {
                    341:                                ok = mtod(*value, int *);
                    342:                                if (*ok)
                    343:                                        nsp->nsp_flags |= mask;
                    344:                                else
                    345:                                        nsp->nsp_flags &= ~mask;
                    346:                        } else error = EINVAL;
                    347:                        break;
                    348: 
                    349:                case SO_DEFAULT_HEADERS:
                    350:                        {
                    351:                                register struct idp *idp
                    352:                                    = mtod(*value, struct idp *);
                    353:                                nsp->nsp_dpt = idp->idp_pt;
                    354:                        }
                    355:                        break;
                    356: #ifdef NSIP
                    357: 
                    358:                case SO_NSIP_ROUTE:
                    359:                        error = nsip_route(*value);
                    360:                        break;
                    361: #endif NSIP
                    362:                default:
                    363:                        error = EINVAL;
                    364:                }
                    365:                if (value && *value)
                    366:                        m_freem(*value);
                    367:                break;
                    368:        }
                    369:        return (error);
                    370: }
                    371: 
                    372: /*ARGSUSED*/
                    373: idp_usrreq(so, req, m, nam, control)
                    374:        struct socket *so;
                    375:        int req;
                    376:        struct mbuf *m, *nam, *control;
                    377: {
                    378:        struct nspcb *nsp = sotonspcb(so);
                    379:        int error = 0;
                    380: 
                    381:        if (req == PRU_CONTROL)
                    382:                 return (ns_control(so, (int)m, (caddr_t)nam,
                    383:                        (struct ifnet *)control));
                    384:        if (control && control->m_len) {
                    385:                error = EINVAL;
                    386:                goto release;
                    387:        }
                    388:        if (nsp == NULL && req != PRU_ATTACH) {
                    389:                error = EINVAL;
                    390:                goto release;
                    391:        }
                    392:        switch (req) {
                    393: 
                    394:        case PRU_ATTACH:
                    395:                if (nsp != NULL) {
                    396:                        error = EINVAL;
                    397:                        break;
                    398:                }
                    399:                error = ns_pcballoc(so, &nspcb);
                    400:                if (error)
                    401:                        break;
                    402:                error = soreserve(so, (u_long) 2048, (u_long) 2048);
                    403:                if (error)
                    404:                        break;
                    405:                break;
                    406: 
                    407:        case PRU_DETACH:
                    408:                if (nsp == NULL) {
                    409:                        error = ENOTCONN;
                    410:                        break;
                    411:                }
                    412:                ns_pcbdetach(nsp);
                    413:                break;
                    414: 
                    415:        case PRU_BIND:
                    416:                error = ns_pcbbind(nsp, nam);
                    417:                break;
                    418: 
                    419:        case PRU_LISTEN:
                    420:                error = EOPNOTSUPP;
                    421:                break;
                    422: 
                    423:        case PRU_CONNECT:
                    424:                if (!ns_nullhost(nsp->nsp_faddr)) {
                    425:                        error = EISCONN;
                    426:                        break;
                    427:                }
                    428:                error = ns_pcbconnect(nsp, nam);
                    429:                if (error == 0)
                    430:                        soisconnected(so);
                    431:                break;
                    432: 
                    433:        case PRU_CONNECT2:
                    434:                error = EOPNOTSUPP;
                    435:                break;
                    436: 
                    437:        case PRU_ACCEPT:
                    438:                error = EOPNOTSUPP;
                    439:                break;
                    440: 
                    441:        case PRU_DISCONNECT:
                    442:                if (ns_nullhost(nsp->nsp_faddr)) {
                    443:                        error = ENOTCONN;
                    444:                        break;
                    445:                }
                    446:                ns_pcbdisconnect(nsp);
                    447:                soisdisconnected(so);
                    448:                break;
                    449: 
                    450:        case PRU_SHUTDOWN:
                    451:                socantsendmore(so);
                    452:                break;
                    453: 
                    454:        case PRU_SEND:
                    455:        {
                    456:                struct ns_addr laddr;
                    457:                int s;
                    458: 
                    459:                if (nam) {
                    460:                        laddr = nsp->nsp_laddr;
                    461:                        if (!ns_nullhost(nsp->nsp_faddr)) {
                    462:                                error = EISCONN;
                    463:                                break;
                    464:                        }
                    465:                        /*
                    466:                         * Must block input while temporarily connected.
                    467:                         */
                    468:                        s = splnet();
                    469:                        error = ns_pcbconnect(nsp, nam);
                    470:                        if (error) {
                    471:                                splx(s);
                    472:                                break;
                    473:                        }
                    474:                } else {
                    475:                        if (ns_nullhost(nsp->nsp_faddr)) {
                    476:                                error = ENOTCONN;
                    477:                                break;
                    478:                        }
                    479:                }
                    480:                error = idp_output(nsp, m);
                    481:                m = NULL;
                    482:                if (nam) {
                    483:                        ns_pcbdisconnect(nsp);
                    484:                        splx(s);
                    485:                        nsp->nsp_laddr.x_host = laddr.x_host;
                    486:                        nsp->nsp_laddr.x_port = laddr.x_port;
                    487:                }
                    488:        }
                    489:                break;
                    490: 
                    491:        case PRU_ABORT:
                    492:                ns_pcbdetach(nsp);
                    493:                sofree(so);
                    494:                soisdisconnected(so);
                    495:                break;
                    496: 
                    497:        case PRU_SOCKADDR:
                    498:                ns_setsockaddr(nsp, nam);
                    499:                break;
                    500: 
                    501:        case PRU_PEERADDR:
                    502:                ns_setpeeraddr(nsp, nam);
                    503:                break;
                    504: 
                    505:        case PRU_SENSE:
                    506:                /*
                    507:                 * stat: don't bother with a blocksize.
                    508:                 */
                    509:                return (0);
                    510: 
                    511:        case PRU_SENDOOB:
                    512:        case PRU_FASTTIMO:
                    513:        case PRU_SLOWTIMO:
                    514:        case PRU_PROTORCV:
                    515:        case PRU_PROTOSEND:
                    516:                error =  EOPNOTSUPP;
                    517:                break;
                    518: 
                    519:        case PRU_CONTROL:
                    520:        case PRU_RCVD:
                    521:        case PRU_RCVOOB:
                    522:                return (EOPNOTSUPP);    /* do not free mbuf's */
                    523: 
                    524:        default:
                    525:                panic("idp_usrreq");
                    526:        }
                    527: release:
                    528:        if (control != NULL)
                    529:                m_freem(control);
                    530:        if (m != NULL)
                    531:                m_freem(m);
                    532:        return (error);
                    533: }
                    534: /*ARGSUSED*/
                    535: idp_raw_usrreq(so, req, m, nam, control)
                    536:        struct socket *so;
                    537:        int req;
                    538:        struct mbuf *m, *nam, *control;
                    539: {
                    540:        int error = 0;
                    541:        struct nspcb *nsp = sotonspcb(so);
                    542:        extern struct nspcb nsrawpcb;
                    543: 
                    544:        switch (req) {
                    545: 
                    546:        case PRU_ATTACH:
                    547: 
                    548:                if (!(so->so_state & SS_PRIV) || (nsp != NULL)) {
                    549:                        error = EINVAL;
                    550:                        break;
                    551:                }
                    552:                error = ns_pcballoc(so, &nsrawpcb);
                    553:                if (error)
                    554:                        break;
                    555:                error = soreserve(so, (u_long) 2048, (u_long) 2048);
                    556:                if (error)
                    557:                        break;
                    558:                nsp = sotonspcb(so);
                    559:                nsp->nsp_faddr.x_host = ns_broadhost;
                    560:                nsp->nsp_flags = NSP_RAWIN | NSP_RAWOUT;
                    561:                break;
                    562:        default:
                    563:                error = idp_usrreq(so, req, m, nam, control);
                    564:        }
                    565:        return (error);
                    566: }
                    567: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.