Annotation of 42BSD/sys/net/route.c, revision 1.1.1.1

1.1       root        1: /*     route.c 6.2     83/10/20        */
                      2: 
                      3: #include "../h/param.h"
                      4: #include "../h/systm.h"
                      5: #include "../h/mbuf.h"
                      6: #include "../h/protosw.h"
                      7: #include "../h/socket.h"
                      8: #include "../h/dir.h"
                      9: #include "../h/user.h"
                     10: #include "../h/ioctl.h"
                     11: #include "../h/errno.h"
                     12: 
                     13: #include "../net/if.h"
                     14: #include "../net/af.h"
                     15: #include "../net/route.h"
                     16: 
                     17: int    rttrash;                /* routes not in table but not freed */
                     18: struct sockaddr wildcard;      /* zero valued cookie for wildcard searches */
                     19: 
                     20: /*
                     21:  * Packet routing routines.
                     22:  */
                     23: rtalloc(ro)
                     24:        register struct route *ro;
                     25: {
                     26:        register struct rtentry *rt;
                     27:        register struct mbuf *m;
                     28:        register u_long hash;
                     29:        struct sockaddr *dst = &ro->ro_dst;
                     30:        int (*match)(), doinghost;
                     31:        struct afhash h;
                     32:        u_int af = dst->sa_family;
                     33:        struct rtentry *rtmin;
                     34:        struct mbuf **table;
                     35: 
                     36:        if (ro->ro_rt && ro->ro_rt->rt_ifp)                     /* XXX */
                     37:                return;
                     38:        if (af >= AF_MAX)
                     39:                return;
                     40:        (*afswitch[af].af_hash)(dst, &h);
                     41:        rtmin = 0;
                     42:        match = afswitch[af].af_netmatch;
                     43:        hash = h.afh_hosthash, table = rthost, doinghost = 1;
                     44: again:
                     45:        for (m = table[hash % RTHASHSIZ]; m; m = m->m_next) {
                     46:                rt = mtod(m, struct rtentry *);
                     47:                if (rt->rt_hash != hash)
                     48:                        continue;
                     49:                if ((rt->rt_flags & RTF_UP) == 0 ||
                     50:                    (rt->rt_ifp->if_flags & IFF_UP) == 0)
                     51:                        continue;
                     52:                if (doinghost) {
                     53:                        if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst,
                     54:                            sizeof (*dst)))
                     55:                                continue;
                     56:                } else {
                     57:                        if (rt->rt_dst.sa_family != af ||
                     58:                            !(*match)(&rt->rt_dst, dst))
                     59:                                continue;
                     60:                }
                     61:                if (rtmin == 0 || rt->rt_use < rtmin->rt_use)
                     62:                        rtmin = rt;
                     63:        }
                     64:        if (rtmin == 0 && doinghost) {
                     65:                doinghost = 0;
                     66:                hash = h.afh_nethash, table = rtnet;
                     67:                goto again;
                     68:        }
                     69:        /*
                     70:         * Check for wildcard gateway, by convention network 0.
                     71:         */
                     72:        if (rtmin == 0 && dst != &wildcard) {
                     73:                dst = &wildcard, hash = 0;
                     74:                goto again;
                     75:        }
                     76:        ro->ro_rt = rtmin;
                     77:        if (rtmin == 0) {
                     78:                rtstat.rts_unreach++;
                     79:                return;
                     80:        }
                     81:        rtmin->rt_refcnt++;
                     82:        if (dst == &wildcard)
                     83:                rtstat.rts_wildcard++;
                     84: }
                     85: 
                     86: rtfree(rt)
                     87:        register struct rtentry *rt;
                     88: {
                     89: 
                     90:        if (rt == 0)
                     91:                panic("rtfree");
                     92:        rt->rt_refcnt--;
                     93:        if (rt->rt_refcnt == 0 && (rt->rt_flags&RTF_UP) == 0) {
                     94:                rttrash--;
                     95:                (void) m_free(dtom(rt));
                     96:        }
                     97: }
                     98: 
                     99: /*
                    100:  * Force a routing table entry to the specified
                    101:  * destination to go through the given gateway.
                    102:  * Normally called as a result of a routing redirect
                    103:  * message from the network layer.
                    104:  *
                    105:  * N.B.: must be called at splnet or higher
                    106:  *
                    107:  * Should notify all parties with a reference to
                    108:  * the route that it's changed (so, for instance,
                    109:  * current round trip time estimates could be flushed),
                    110:  * but we have no back pointers at the moment.
                    111:  */
                    112: rtredirect(dst, gateway)
                    113:        struct sockaddr *dst, *gateway;
                    114: {
                    115:        struct route ro;
                    116:        register struct rtentry *rt;
                    117: 
                    118:        /* verify the gateway is directly reachable */
                    119:        if (if_ifwithnet(gateway) == 0) {
                    120:                rtstat.rts_badredirect++;
                    121:                return;
                    122:        }
                    123:        ro.ro_dst = *dst;
                    124:        ro.ro_rt = 0;
                    125:        rtalloc(&ro);
                    126:        rt = ro.ro_rt;
                    127:        /*
                    128:         * Create a new entry if we just got back a wildcard entry
                    129:         * or the the lookup failed.  This is necessary for hosts
                    130:         * which use routing redirects generated by smart gateways
                    131:         * to dynamically build the routing tables.
                    132:         */
                    133:        if (rt &&
                    134:            (*afswitch[dst->sa_family].af_netmatch)(&wildcard, &rt->rt_dst)) {
                    135:                rtfree(rt);
                    136:                rt = 0;
                    137:        }
                    138:        if (rt == 0) {
                    139:                rtinit(dst, gateway, RTF_GATEWAY);
                    140:                rtstat.rts_dynamic++;
                    141:                return;
                    142:        }
                    143:        /*
                    144:         * Don't listen to the redirect if it's
                    145:         * for a route to an interface. 
                    146:         */
                    147:        if (rt->rt_flags & RTF_GATEWAY) {
                    148:                /*
                    149:                 * Smash the current notion of the gateway to
                    150:                 * this destination.  This is probably not right,
                    151:                 * as it's conceivable a flurry of redirects could
                    152:                 * cause the gateway value to fluctuate wildly during
                    153:                 * dynamic routing reconfiguration.
                    154:                 */
                    155:                rt->rt_gateway = *gateway;
                    156:                rtfree(rt);
                    157:                rtstat.rts_newgateway++;
                    158:                return;
                    159:        }
                    160: }
                    161: 
                    162: /*
                    163:  * Routing table ioctl interface.
                    164:  */
                    165: rtioctl(cmd, data)
                    166:        int cmd;
                    167:        caddr_t data;
                    168: {
                    169: 
                    170:        if (cmd != SIOCADDRT && cmd != SIOCDELRT)
                    171:                return (EINVAL);
                    172:        if (!suser())
                    173:                return (u.u_error);
                    174:        return (rtrequest(cmd, (struct rtentry *)data));
                    175: }
                    176: 
                    177: /*
                    178:  * Carry out a request to change the routing table.  Called by
                    179:  * interfaces at boot time to make their ``local routes'' known,
                    180:  * for ioctl's, and as the result of routing redirects.
                    181:  */
                    182: rtrequest(req, entry)
                    183:        int req;
                    184:        register struct rtentry *entry;
                    185: {
                    186:        register struct mbuf *m, **mprev;
                    187:        register struct rtentry *rt;
                    188:        struct afhash h;
                    189:        int s, error = 0, (*match)();
                    190:        u_int af;
                    191:        u_long hash;
                    192:        struct ifnet *ifp;
                    193: 
                    194:        af = entry->rt_dst.sa_family;
                    195:        if (af >= AF_MAX)
                    196:                return (EAFNOSUPPORT);
                    197:        (*afswitch[af].af_hash)(&entry->rt_dst, &h);
                    198:        if (entry->rt_flags & RTF_HOST) {
                    199:                hash = h.afh_hosthash;
                    200:                mprev = &rthost[hash % RTHASHSIZ];
                    201:        } else {
                    202:                hash = h.afh_nethash;
                    203:                mprev = &rtnet[hash % RTHASHSIZ];
                    204:        }
                    205:        match = afswitch[af].af_netmatch;
                    206:        s = splimp();
                    207:        for (; m = *mprev; mprev = &m->m_next) {
                    208:                rt = mtod(m, struct rtentry *);
                    209:                if (rt->rt_hash != hash)
                    210:                        continue;
                    211:                if (entry->rt_flags & RTF_HOST) {
                    212: #define        equal(a1, a2) \
                    213:        (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0)
                    214:                        if (!equal(&rt->rt_dst, &entry->rt_dst))
                    215:                                continue;
                    216:                } else {
                    217:                        if (rt->rt_dst.sa_family != entry->rt_dst.sa_family ||
                    218:                            (*match)(&rt->rt_dst, &entry->rt_dst) == 0)
                    219:                                continue;
                    220:                }
                    221:                if (equal(&rt->rt_gateway, &entry->rt_gateway))
                    222:                        break;
                    223:        }
                    224:        switch (req) {
                    225: 
                    226:        case SIOCDELRT:
                    227:                if (m == 0) {
                    228:                        error = ESRCH;
                    229:                        goto bad;
                    230:                }
                    231:                *mprev = m->m_next;
                    232:                if (rt->rt_refcnt > 0) {
                    233:                        rt->rt_flags &= ~RTF_UP;
                    234:                        rttrash++;
                    235:                        m->m_next = 0;
                    236:                } else
                    237:                        (void) m_free(m);
                    238:                break;
                    239: 
                    240:        case SIOCADDRT:
                    241:                if (m) {
                    242:                        error = EEXIST;
                    243:                        goto bad;
                    244:                }
                    245:                ifp = if_ifwithaddr(&entry->rt_gateway);
                    246:                if (ifp == 0) {
                    247:                        ifp = if_ifwithnet(&entry->rt_gateway);
                    248:                        if (ifp == 0) {
                    249:                                error = ENETUNREACH;
                    250:                                goto bad;
                    251:                        }
                    252:                }
                    253:                m = m_get(M_DONTWAIT, MT_RTABLE);
                    254:                if (m == 0) {
                    255:                        error = ENOBUFS;
                    256:                        goto bad;
                    257:                }
                    258:                *mprev = m;
                    259:                m->m_off = MMINOFF;
                    260:                m->m_len = sizeof (struct rtentry);
                    261:                rt = mtod(m, struct rtentry *);
                    262:                rt->rt_hash = hash;
                    263:                rt->rt_dst = entry->rt_dst;
                    264:                rt->rt_gateway = entry->rt_gateway;
                    265:                rt->rt_flags =
                    266:                    RTF_UP | (entry->rt_flags & (RTF_HOST|RTF_GATEWAY));
                    267:                rt->rt_refcnt = 0;
                    268:                rt->rt_use = 0;
                    269:                rt->rt_ifp = ifp;
                    270:                break;
                    271:        }
                    272: bad:
                    273:        splx(s);
                    274:        return (error);
                    275: }
                    276: 
                    277: /*
                    278:  * Set up a routing table entry, normally
                    279:  * for an interface.
                    280:  */
                    281: rtinit(dst, gateway, flags)
                    282:        struct sockaddr *dst, *gateway;
                    283:        int flags;
                    284: {
                    285:        struct rtentry route;
                    286:        int cmd;
                    287: 
                    288:        if (flags == -1) {
                    289:                cmd = (int)SIOCDELRT;
                    290:                flags = 0;
                    291:        } else {
                    292:                cmd = (int)SIOCADDRT;
                    293:        }
                    294:        bzero((caddr_t)&route, sizeof (route));
                    295:        route.rt_dst = *dst;
                    296:        route.rt_gateway = *gateway;
                    297:        route.rt_flags = flags;
                    298:        (void) rtrequest(cmd, &route);
                    299: }

unix.superglobalmegacorp.com

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