Annotation of Net2/netns/ns.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: @(#)ns.c  7.8 (Berkeley) 6/27/91
        !            34:  *     ns.c,v 1.2 1993/05/20 04:35:50 cgd Exp
1.1       root       35:  */
                     36: 
                     37: #include "param.h"
                     38: #include "mbuf.h"
                     39: #include "ioctl.h"
                     40: #include "protosw.h"
                     41: #include "errno.h"
                     42: #include "socket.h"
                     43: #include "socketvar.h"
                     44: 
                     45: 
                     46: #include "../net/if.h"
                     47: #include "../net/route.h"
                     48: #include "../net/af.h"
                     49: 
                     50: #include "ns.h"
                     51: #include "ns_if.h"
                     52: 
                     53: #ifdef NS
                     54: 
                     55: struct ns_ifaddr *ns_ifaddr;
                     56: int ns_interfaces;
                     57: extern struct sockaddr_ns ns_netmask, ns_hostmask;
                     58: 
                     59: /*
                     60:  * Generic internet control operations (ioctl's).
                     61:  */
                     62: /* ARGSUSED */
                     63: ns_control(so, cmd, data, ifp)
                     64:        struct socket *so;
                     65:        int cmd;
                     66:        caddr_t data;
                     67:        register struct ifnet *ifp;
                     68: {
                     69:        register struct ifreq *ifr = (struct ifreq *)data;
                     70:        register struct ns_aliasreq *ifra = (struct ns_aliasreq *)data;
                     71:        register struct ns_ifaddr *ia;
                     72:        struct ifaddr *ifa;
                     73:        struct ns_ifaddr *oia;
                     74:        struct mbuf *m;
                     75:        int error, dstIsNew, hostIsNew;
                     76: 
                     77:        /*
                     78:         * Find address for this interface, if it exists.
                     79:         */
                     80:        if (ifp == 0)
                     81:                return (EADDRNOTAVAIL);
                     82:        for (ia = ns_ifaddr; ia; ia = ia->ia_next)
                     83:                if (ia->ia_ifp == ifp)
                     84:                        break;
                     85: 
                     86:        switch (cmd) {
                     87: 
                     88:        case SIOCGIFADDR:
                     89:                if (ia == (struct ns_ifaddr *)0)
                     90:                        return (EADDRNOTAVAIL);
                     91:                *(struct sockaddr_ns *)&ifr->ifr_addr = ia->ia_addr;
                     92:                return (0);
                     93: 
                     94: 
                     95:        case SIOCGIFBRDADDR:
                     96:                if (ia == (struct ns_ifaddr *)0)
                     97:                        return (EADDRNOTAVAIL);
                     98:                if ((ifp->if_flags & IFF_BROADCAST) == 0)
                     99:                        return (EINVAL);
                    100:                *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
                    101:                return (0);
                    102: 
                    103:        case SIOCGIFDSTADDR:
                    104:                if (ia == (struct ns_ifaddr *)0)
                    105:                        return (EADDRNOTAVAIL);
                    106:                if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
                    107:                        return (EINVAL);
                    108:                *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
                    109:                return (0);
                    110:        }
                    111: 
                    112:        if ((so->so_state & SS_PRIV) == 0)
                    113:                return (EPERM);
                    114: 
                    115:        switch (cmd) {
                    116:        case SIOCAIFADDR:
                    117:        case SIOCDIFADDR:
                    118:                if (ifra->ifra_addr.sns_family == AF_NS)
                    119:                    for (oia = ia; ia; ia = ia->ia_next) {
                    120:                        if (ia->ia_ifp == ifp  &&
                    121:                            ns_neteq(ia->ia_addr.sns_addr,
                    122:                                  ifra->ifra_addr.sns_addr))
                    123:                            break;
                    124:                    }
                    125:                if (cmd == SIOCDIFADDR && ia == 0)
                    126:                        return (EADDRNOTAVAIL);
                    127:                /* FALLTHROUGH */
                    128: 
                    129:        case SIOCSIFADDR:
                    130:        case SIOCSIFDSTADDR:
                    131:                if (ia == (struct ns_ifaddr *)0) {
                    132:                        m = m_getclr(M_WAIT, MT_IFADDR);
                    133:                        if (m == (struct mbuf *)NULL)
                    134:                                return (ENOBUFS);
                    135:                        if (ia = ns_ifaddr) {
                    136:                                for ( ; ia->ia_next; ia = ia->ia_next)
                    137:                                        ;
                    138:                                ia->ia_next = mtod(m, struct ns_ifaddr *);
                    139:                        } else
                    140:                                ns_ifaddr = mtod(m, struct ns_ifaddr *);
                    141:                        ia = mtod(m, struct ns_ifaddr *);
                    142:                        if (ifa = ifp->if_addrlist) {
                    143:                                for ( ; ifa->ifa_next; ifa = ifa->ifa_next)
                    144:                                        ;
                    145:                                ifa->ifa_next = (struct ifaddr *) ia;
                    146:                        } else
                    147:                                ifp->if_addrlist = (struct ifaddr *) ia;
                    148:                        ia->ia_ifp = ifp;
                    149:                        ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
                    150: 
                    151:                        ia->ia_ifa.ifa_netmask =
                    152:                                (struct sockaddr *)&ns_netmask;
                    153: 
                    154:                        ia->ia_ifa.ifa_dstaddr =
                    155:                                (struct sockaddr *)&ia->ia_dstaddr;
                    156:                        if (ifp->if_flags & IFF_BROADCAST) {
                    157:                                ia->ia_broadaddr.sns_family = AF_NS;
                    158:                                ia->ia_broadaddr.sns_len = sizeof(ia->ia_addr);
                    159:                                ia->ia_broadaddr.sns_addr.x_host = ns_broadhost;
                    160:                        }
                    161:                        ns_interfaces++;
                    162:                }
                    163:        }
                    164: 
                    165:        switch (cmd) {
                    166:                int error;
                    167: 
                    168:        case SIOCSIFDSTADDR:
                    169:                if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
                    170:                        return (EINVAL);
                    171:                if (ia->ia_flags & IFA_ROUTE) {
                    172:                        rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
                    173:                        ia->ia_flags &= ~IFA_ROUTE;
                    174:                }
                    175:                if (ifp->if_ioctl) {
                    176:                        error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ia);
                    177:                        if (error)
                    178:                                return (error);
                    179:                }
                    180:                *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
                    181:                return (0);
                    182: 
                    183:        case SIOCSIFADDR:
                    184:                return (ns_ifinit(ifp, ia,
                    185:                                (struct sockaddr_ns *)&ifr->ifr_addr, 1));
                    186: 
                    187:        case SIOCDIFADDR:
                    188:                ns_ifscrub(ifp, ia);
                    189:                if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia)
                    190:                        ifp->if_addrlist = ifa->ifa_next;
                    191:                else {
                    192:                        while (ifa->ifa_next &&
                    193:                               (ifa->ifa_next != (struct ifaddr *)ia))
                    194:                                    ifa = ifa->ifa_next;
                    195:                        if (ifa->ifa_next)
                    196:                            ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next;
                    197:                        else
                    198:                                printf("Couldn't unlink nsifaddr from ifp\n");
                    199:                }
                    200:                oia = ia;
                    201:                if (oia == (ia = ns_ifaddr)) {
                    202:                        ns_ifaddr = ia->ia_next;
                    203:                } else {
                    204:                        while (ia->ia_next && (ia->ia_next != oia)) {
                    205:                                ia = ia->ia_next;
                    206:                        }
                    207:                        if (ia->ia_next)
                    208:                            ia->ia_next = oia->ia_next;
                    209:                        else
                    210:                                printf("Didn't unlink nsifadr from list\n");
                    211:                }
                    212:                (void) m_free(dtom(oia));
                    213:                if (0 == --ns_interfaces) {
                    214:                        /*
                    215:                         * We reset to virginity and start all over again
                    216:                         */
                    217:                        ns_thishost = ns_zerohost;
                    218:                }
                    219:                return (0);
                    220:        
                    221:        case SIOCAIFADDR:
                    222:                dstIsNew = 0; hostIsNew = 1;
                    223:                if (ia->ia_addr.sns_family == AF_NS) {
                    224:                        if (ifra->ifra_addr.sns_len == 0) {
                    225:                                ifra->ifra_addr = ia->ia_addr;
                    226:                                hostIsNew = 0;
                    227:                        } else if (ns_neteq(ifra->ifra_addr.sns_addr,
                    228:                                         ia->ia_addr.sns_addr))
                    229:                                hostIsNew = 0;
                    230:                }
                    231:                if ((ifp->if_flags & IFF_POINTOPOINT) &&
                    232:                    (ifra->ifra_dstaddr.sns_family == AF_NS)) {
                    233:                        if (hostIsNew == 0)
                    234:                                ns_ifscrub(ifp, ia);
                    235:                        ia->ia_dstaddr = ifra->ifra_dstaddr;
                    236:                        dstIsNew  = 1;
                    237:                }
                    238:                if (ifra->ifra_addr.sns_family == AF_NS &&
                    239:                                            (hostIsNew || dstIsNew))
                    240:                        error = ns_ifinit(ifp, ia, &ifra->ifra_addr, 0);
                    241:                return (error);
                    242: 
                    243:        default:
                    244:                if (ifp->if_ioctl == 0)
                    245:                        return (EOPNOTSUPP);
                    246:                return ((*ifp->if_ioctl)(ifp, cmd, data));
                    247:        }
                    248: }
                    249: 
                    250: /*
                    251: * Delete any previous route for an old address.
                    252: */
                    253: ns_ifscrub(ifp, ia)
                    254:        register struct ifnet *ifp;
                    255:        register struct ns_ifaddr *ia; 
                    256: {
                    257:        if (ia->ia_flags & IFA_ROUTE) {
                    258:                if (ifp->if_flags & IFF_POINTOPOINT) {
                    259:                        rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
                    260:                } else
                    261:                        rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
                    262:                ia->ia_flags &= ~IFA_ROUTE;
                    263:        }
                    264: }
                    265: /*
                    266:  * Initialize an interface's internet address
                    267:  * and routing table entry.
                    268:  */
                    269: ns_ifinit(ifp, ia, sns, scrub)
                    270:        register struct ifnet *ifp;
                    271:        register struct ns_ifaddr *ia;
                    272:        register struct sockaddr_ns *sns;
                    273: {
                    274:        struct sockaddr_ns oldaddr;
                    275:        register union ns_host *h = &ia->ia_addr.sns_addr.x_host;
                    276:        int s = splimp(), error;
                    277: 
                    278:        /*
                    279:         * Set up new addresses.
                    280:         */
                    281:        oldaddr = ia->ia_addr;
                    282:        ia->ia_addr = *sns;
                    283:        /*
                    284:         * The convention we shall adopt for naming is that
                    285:         * a supplied address of zero means that "we don't care".
                    286:         * if there is a single interface, use the address of that
                    287:         * interface as our 6 byte host address.
                    288:         * if there are multiple interfaces, use any address already
                    289:         * used.
                    290:         *
                    291:         * Give the interface a chance to initialize
                    292:         * if this is its first address,
                    293:         * and to validate the address if necessary.
                    294:         */
                    295:        if (ns_hosteqnh(ns_thishost, ns_zerohost)) {
                    296:                if (ifp->if_ioctl &&
                    297:                     (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, ia))) {
                    298:                        ia->ia_addr = oldaddr;
                    299:                        splx(s);
                    300:                        return (error);
                    301:                }
                    302:                ns_thishost = *h;
                    303:        } else if (ns_hosteqnh(sns->sns_addr.x_host, ns_zerohost)
                    304:            || ns_hosteqnh(sns->sns_addr.x_host, ns_thishost)) {
                    305:                *h = ns_thishost;
                    306:                if (ifp->if_ioctl &&
                    307:                     (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, ia))) {
                    308:                        ia->ia_addr = oldaddr;
                    309:                        splx(s);
                    310:                        return (error);
                    311:                }
                    312:                if (!ns_hosteqnh(ns_thishost,*h)) {
                    313:                        ia->ia_addr = oldaddr;
                    314:                        splx(s);
                    315:                        return (EINVAL);
                    316:                }
                    317:        } else {
                    318:                ia->ia_addr = oldaddr;
                    319:                splx(s);
                    320:                return (EINVAL);
                    321:        }
                    322:        /*
                    323:         * Add route for the network.
                    324:         */
                    325:        if (scrub) {
                    326:                ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
                    327:                ns_ifscrub(ifp, ia);
                    328:                ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
                    329:        }
                    330:        if (ifp->if_flags & IFF_POINTOPOINT)
                    331:                rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
                    332:        else {
                    333:                ia->ia_broadaddr.sns_addr.x_net = ia->ia_net;
                    334:                rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
                    335:        }
                    336:        ia->ia_flags |= IFA_ROUTE;
                    337:        return (0);
                    338: }
                    339: 
                    340: /*
                    341:  * Return address info for specified internet network.
                    342:  */
                    343: struct ns_ifaddr *
                    344: ns_iaonnetof(dst)
                    345:        register struct ns_addr *dst;
                    346: {
                    347:        register struct ns_ifaddr *ia;
                    348:        register struct ns_addr *compare;
                    349:        register struct ifnet *ifp;
                    350:        struct ns_ifaddr *ia_maybe = 0;
                    351:        union ns_net net = dst->x_net;
                    352: 
                    353:        for (ia = ns_ifaddr; ia; ia = ia->ia_next) {
                    354:                if (ifp = ia->ia_ifp) {
                    355:                        if (ifp->if_flags & IFF_POINTOPOINT) {
                    356:                                compare = &satons_addr(ia->ia_dstaddr);
                    357:                                if (ns_hosteq(*dst, *compare))
                    358:                                        return (ia);
                    359:                                if (ns_neteqnn(net, ia->ia_net))
                    360:                                        ia_maybe = ia;
                    361:                        } else {
                    362:                                if (ns_neteqnn(net, ia->ia_net))
                    363:                                        return (ia);
                    364:                        }
                    365:                }
                    366:        }
                    367:        return (ia_maybe);
                    368: }
                    369: #endif

unix.superglobalmegacorp.com

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