123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715 |
- #include <u.h>
- #include <libc.h>
- #include <bio.h>
- #include <ip.h>
- enum
- {
- Version= 1,
- Pasize= 4,
- /*
- * definitions that are innately tied to BSD
- */
- AF_INET= 2,
- AF_UNSPEC= 0,
- /*
- * Packet types.
- */
- Request= 1,
- Response= 2,
- Traceon= 3,
- Traceoff= 4,
- Infinity= 16, /* infinite hop count */
- Maxpacket= 488, /* largest packet body */
- };
- /*
- * network info
- */
- typedef struct Rip Rip;
- struct Rip
- {
- uchar family[2];
- uchar port[2];
- uchar addr[Pasize];
- uchar pad[8];
- uchar metric[4];
- };
- typedef struct Ripmsg Ripmsg;
- struct Ripmsg
- {
- uchar type;
- uchar vers;
- uchar pad[2];
- Rip rip[1]; /* the rest of the packet consists of routes */
- };
- enum
- {
- Maxroutes= (Maxpacket-4)/sizeof(Ripmsg),
- };
- /*
- * internal route info
- */
- enum
- {
- Nroute= 2048, /* this has to be smaller than what /ip has */
- Nhash= 256, /* routing hash buckets */
- Nifc= 16,
- };
- typedef struct Route Route;
- struct Route
- {
- Route *next;
- uchar dest[Pasize];
- uchar mask[Pasize];
- uchar gate[Pasize];
- int metric;
- int inuse;
- long time;
- };
- struct {
- Route route[Nroute];
- Route *hash[Nhash];
- int nroute;
- Route def; /* default route (immutable by us) */
- } ralloc;
- typedef struct Ifc Ifc;
- struct Ifc
- {
- int bcast;
- uchar addr[Pasize]; /* my address */
- uchar mask[Pasize]; /* subnet mask */
- uchar net[Pasize]; /* subnet */
- uchar *cmask; /* class mask */
- uchar cnet[Pasize]; /* class net */
- };
- struct {
- Ifc ifc[Nifc];
- int nifc;
- } ialloc;
- /*
- * specific networks to broadcast on
- */
- typedef struct Bnet Bnet;
- struct Bnet
- {
- Bnet *next;
- uchar addr[Pasize];
- };
- Bnet *bnets;
- int ripfd;
- long now;
- int debug;
- int readonly;
- char routefile[256];
- char netdir[256];
- int openport(void);
- void readroutes(void);
- void readifcs(void);
- void considerroute(Route*);
- void installroute(Route*);
- void removeroute(Route*);
- uchar *getmask(uchar*);
- void broadcast(void);
- void timeoutroutes(void);
- void
- fatal(int syserr, char *fmt, ...)
- {
- char buf[ERRMAX], sysbuf[ERRMAX];
- va_list arg;
- va_start(arg, fmt);
- vseprint(buf, buf+sizeof(buf), fmt, arg);
- va_end(arg);
- if(syserr) {
- errstr(sysbuf, sizeof sysbuf);
- fprint(2, "routed: %s: %s\n", buf, sysbuf);
- }
- else
- fprint(2, "routed: %s\n", buf);
- exits(buf);
- }
- ulong
- v4parseipmask(uchar *ip, char *p)
- {
- ulong x;
- uchar v6ip[IPaddrlen];
- x = parseipmask(v6ip, p);
- memmove(ip, v6ip+IPv4off, 4);
- return x;
- }
- uchar*
- v4defmask(uchar *ip)
- {
- uchar v6ip[IPaddrlen];
- v4tov6(v6ip, ip);
- ip = defmask(v6ip);
- return ip+IPv4off;
- }
- void
- v4maskip(uchar *from, uchar *mask, uchar *to)
- {
- int i;
- for(i = 0; i < Pasize; i++)
- *to++ = *from++ & *mask++;
- }
- void
- v6tov4mask(uchar *v4, uchar *v6)
- {
- memmove(v4, v6+IPv4off, 4);
- }
- #define equivip(a, b) (memcmp((a), (b), Pasize) == 0)
- void
- ding(void *u, char *msg)
- {
- USED(u);
- if(strstr(msg, "alarm"))
- noted(NCONT);
- noted(NDFLT);
- }
- void
- usage(void)
- {
- fprint(2, "usage: %s [-bnd] [-x netmtpt]\n", argv0);
- exits("usage");
- }
- void
- main(int argc, char *argv[])
- {
- int i, n;
- OUdphdr *up;
- Ripmsg *m;
- Rip *r;
- Route route;
- Bnet *bn, **l;
- int dobroadcast;
- char buf[2*1024];
- long diff;
- char *p;
- static long btime;
- uchar raddr[Pasize];
- setnetmtpt(netdir, sizeof(netdir), nil);
- dobroadcast = 0;
- ARGBEGIN{
- case 'b':
- dobroadcast++;
- break;
- case 'd':
- debug++;
- break;
- case 'n':
- readonly++;
- break;
- case 'x':
- p = ARGF();
- if(p == nil)
- usage();
- setnetmtpt(netdir, sizeof(netdir), p);
- break;
- default:
- usage();
- }ARGEND
- /* specific broadcast nets */
- l = &bnets;
- while(argc > 0){
- bn = (Bnet*)malloc(sizeof(Bnet));
- if(bn == 0)
- fatal(1, "out of mem");
- v4parseip(bn->addr, *argv);
- *l = bn;
- l = &bn->next;
- argc--;
- argv++;
- dobroadcast++;
- }
- /* command returns */
- if(!debug)
- switch(rfork(RFNOTEG|RFPROC|RFFDG|RFNOWAIT)) {
- case -1:
- fatal(1, "fork");
- case 0:
- break;
- default:
- exits(0);
- }
- fmtinstall('E', eipfmt);
- fmtinstall('V', eipfmt);
- snprint(routefile, sizeof(routefile), "%s/iproute", netdir);
- snprint(buf, sizeof(buf), "%s/iproute", netdir);
- now = time(0);
- readifcs();
- readroutes();
- notify(ding);
- ripfd = openport();
- for(;;) {
- diff = btime - time(0);
- if(diff <= 0){
- if(dobroadcast)
- broadcast();
- timeoutroutes();
- btime = time(0) + 2*60;
- diff = 2*60;
- }
- alarm(diff*1000);
- n = read(ripfd, buf, sizeof(buf));
- alarm(0);
- if(n <= 0)
- continue;
- n = (n-OUdphdrsize-4)/sizeof(Rip);
- if(n <= 0)
- continue;
- up = (OUdphdr*)buf;
- m = (Ripmsg*)(buf+OUdphdrsize);
- if(m->type != Response || m->vers != Version)
- continue;
- v6tov4(raddr, up->raddr);
- /* ignore our own messages */
- for(i = 0; i < ialloc.nifc; i++)
- if(equivip(ialloc.ifc[i].addr, raddr))
- continue;
- now = time(0);
- for(r = m->rip; r < &m->rip[n]; r++){
- memmove(route.gate, raddr, Pasize);
- memmove(route.mask, getmask(r->addr), Pasize);
- v4maskip(r->addr, route.mask, route.dest);
- route.metric = nhgetl(r->metric) + 1;
- if(route.metric < 1)
- continue;
- considerroute(&route);
- }
- }
- exits(0);
- }
- int
- openport(void)
- {
- char data[128];
- char devdir[40];
- int ripctl, rip;
- snprint(data, sizeof(data), "%s/udp!*!rip", netdir);
- ripctl = announce(data, devdir);
- if(ripctl < 0)
- fatal(1, "can't announce");
- if(fprint(ripctl, "headers") < 0)
- fatal(1, "can't set header mode");
- fprint(ripctl, "oldheaders");
- sprint(data, "%s/data", devdir);
- rip = open(data, ORDWR);
- if(rip < 0)
- fatal(1, "open udp data");
- return rip;
- }
- Ipifc *ifcs;
- void
- readifcs(void)
- {
- Ipifc *ifc;
- Iplifc *lifc;
- Ifc *ip;
- Bnet *bn;
- Route route;
- int i;
- ifcs = readipifc(netdir, ifcs, -1);
- i = 0;
- for(ifc = ifcs; ifc != nil; ifc = ifc->next){
- for(lifc = ifc->lifc; lifc != nil && i < Nifc; lifc = lifc->next){
- // ignore any interfaces that aren't v4
- if(memcmp(lifc->ip, v4prefix, IPaddrlen-IPv4addrlen) != 0)
- continue;
- ip = &ialloc.ifc[i++];
- v6tov4(ip->addr, lifc->ip);
- v6tov4mask(ip->mask, lifc->mask);
- v6tov4(ip->net, lifc->net);
- ip->cmask = v4defmask(ip->net);
- v4maskip(ip->net, ip->cmask, ip->cnet);
- ip->bcast = 0;
-
- /* add as a route */
- memmove(route.mask, ip->mask, Pasize);
- memmove(route.dest, ip->net, Pasize);
- memset(route.gate, 0, Pasize);
- route.metric = 0;
- considerroute(&route);
-
- /* mark as broadcast */
- if(bnets == 0)
- ip->bcast = 1;
- else for(bn = bnets; bn; bn = bn->next)
- if(memcmp(bn->addr, ip->net, Pasize) == 0){
- ip->bcast = 1;
- break;
- }
- }
- }
- ialloc.nifc = i;
- }
- void
- readroutes(void)
- {
- int n;
- char *p;
- Biobuf *b;
- char *f[6];
- Route route;
- b = Bopen(routefile, OREAD);
- if(b == 0)
- return;
- while(p = Brdline(b, '\n')){
- p[Blinelen(b)-1] = 0;
- n = getfields(p, f, 6, 1, " \t");
- if(n < 5)
- continue;
- v4parseip(route.dest, f[0]);
- v4parseipmask(route.mask, f[1]);
- v4parseip(route.gate, f[2]);
- route.metric = Infinity;
- if(equivip(route.dest, ralloc.def.dest)
- && equivip(route.mask, ralloc.def.mask))
- memmove(ralloc.def.gate, route.gate, Pasize);
- else if(!equivip(route.dest, route.gate) && strchr(f[3], 'i') == 0)
- considerroute(&route);
- }
- Bterm(b);
- }
- /*
- * route's hashed by net, not subnet
- */
- ulong
- rhash(uchar *d)
- {
- ulong h;
- uchar net[Pasize];
- v4maskip(d, v4defmask(d), net);
- h = net[0] + net[1] + net[2];
- return h % Nhash;
- }
- /*
- * consider installing a route. Do so only if it is better than what
- * we have.
- */
- void
- considerroute(Route *r)
- {
- ulong h;
- Route *hp;
- if(debug)
- fprint(2, "consider %16V & %16V -> %16V %d\n", r->dest, r->mask, r->gate, r->metric);
- r->next = 0;
- r->time = now;
- r->inuse = 1;
- /* don't allow our default route to be highjacked */
- if(equivip(r->dest, ralloc.def.dest) || equivip(r->mask, ralloc.def.mask))
- return;
- h = rhash(r->dest);
- for(hp = ralloc.hash[h]; hp; hp = hp->next){
- if(equivip(hp->dest, r->dest)){
- /*
- * found a match, replace if better (or much newer)
- */
- if(r->metric < hp->metric || now-hp->time > 5*60){
- removeroute(hp);
- memmove(hp->mask, r->mask, Pasize);
- memmove(hp->gate, r->gate, Pasize);
- hp->metric = r->metric;
- installroute(hp);
- }
- if(equivip(hp->gate, r->gate))
- hp->time = now;
- return;
- }
- }
- /*
- * no match, look for space
- */
- for(hp = ralloc.route; hp < &ralloc.route[Nroute]; hp++)
- if(hp->inuse == 0)
- break;
- if(hp == 0)
- fatal(0, "no more routes");
- memmove(hp, r, sizeof(Route));
- hp->next = ralloc.hash[h];
- ralloc.hash[h] = hp;
- installroute(hp);
- }
- void
- removeroute(Route *r)
- {
- int fd;
- fd = open(routefile, ORDWR);
- if(fd < 0){
- fprint(2, "can't open oproute\n");
- return;
- }
- if(!readonly)
- fprint(fd, "delete %V", r->dest);
- if(debug)
- fprint(2, "removeroute %V\n", r->dest);
- close(fd);
- }
- /*
- * pass a route to the kernel or /ip. Don't bother if it is just the default
- * gateway.
- */
- void
- installroute(Route *r)
- {
- int fd;
- ulong h;
- Route *hp;
- uchar net[Pasize];
- /*
- * don't install routes whose gateway is 00000000
- */
- if(equivip(r->gate, ralloc.def.dest))
- return;
- fd = open(routefile, ORDWR);
- if(fd < 0){
- fprint(2, "can't open oproute\n");
- return;
- }
- h = rhash(r->dest);
- /*
- * if the gateway is the same as the default gateway
- * we may be able to avoid a entry in the kernel
- */
- if(equivip(r->gate, ralloc.def.gate)){
- /*
- * look for a less specific match
- */
- for(hp = ralloc.hash[h]; hp; hp = hp->next){
- v4maskip(hp->mask, r->dest, net);
- if(equivip(net, hp->dest) && !equivip(hp->gate, ralloc.def.gate))
- break;
- }
- /*
- * if no less specific match, just use the default
- */
- if(hp == 0){
- if(!readonly)
- fprint(fd, "delete %V", r->dest);
- if(debug)
- fprint(2, "delete %V\n", r->dest);
- close(fd);
- return;
- }
- }
- if(!readonly)
- fprint(fd, "add %V %V %V", r->dest, r->mask, r->gate);
- if(debug)
- fprint(2, "add %V & %V -> %V\n", r->dest, r->mask, r->gate);
- close(fd);
- }
- /*
- * return true of dest is on net
- */
- int
- onnet(uchar *dest, uchar *net, uchar *netmask)
- {
- uchar dnet[Pasize];
- v4maskip(dest, netmask, dnet);
- return equivip(dnet, net);
- }
- /*
- * figure out what mask to use, if we have a direct connected network
- * with the same class net use its subnet mask.
- */
- uchar*
- getmask(uchar *dest)
- {
- int i;
- Ifc *ip;
- ulong mask, nmask;
- uchar *m;
- m = 0;
- mask = 0xffffffff;
- for(i = 0; i < ialloc.nifc; i++){
- ip = &ialloc.ifc[i];
- if(onnet(dest, ip->cnet, ip->cmask)){
- nmask = nhgetl(ip->mask);
- if(nmask < mask){
- mask = nmask;
- m = ip->mask;
- }
- }
- }
- if(m == 0)
- m = v4defmask(dest);
- return m;
- }
- /*
- * broadcast routes onto all networks
- */
- void
- sendto(Ifc *ip)
- {
- int h, n;
- Route *r;
- uchar mbuf[OUdphdrsize+512];
- Ripmsg *m;
- OUdphdr *u;
- uchar raddr[Pasize];
- u = (OUdphdr*)mbuf;
- for(n = 0; n < Pasize; n++)
- raddr[n] = ip->net[n] | ~(ip->mask[n]);
- v4tov6(u->raddr, raddr);
- hnputs(u->rport, 520);
- m = (Ripmsg*)(mbuf+OUdphdrsize);
- m->type = Response;
- m->vers = Version;
- if(debug)
- fprint(2, "to %V\n", u->raddr);
- n = 0;
- for(h = 0; h < Nhash; h++){
- for(r = ralloc.hash[h]; r; r = r->next){
- /*
- * don't send any route back to the net
- * it came from
- */
- if(onnet(r->gate, ip->net, ip->mask))
- continue;
- /*
- * don't tell a network about itself
- */
- if(equivip(r->dest, ip->net))
- continue;
- /*
- * don't tell nets about other net's subnets
- */
- if(!equivip(r->mask, v4defmask(r->dest))
- && !equivip(ip->cmask, v4defmask(r->dest)))
- continue;
- memset(&m->rip[n], 0, sizeof(m->rip[n]));
- memmove(m->rip[n].addr, r->dest, Pasize);
- if(r->metric < 1)
- hnputl(m->rip[n].metric, 1);
- else
- hnputl(m->rip[n].metric, r->metric);
- hnputs(m->rip[n].family, AF_INET);
- if(debug)
- fprint(2, " %16V & %16V -> %16V %2d\n", r->dest, r->mask, r->gate, r->metric);
- if(++n == Maxroutes && !readonly){
- write(ripfd, mbuf, OUdphdrsize+4+n*20);
- n = 0;
- }
- }
- }
- if(n && !readonly)
- write(ripfd, mbuf, OUdphdrsize+4+n*20);
- }
- void
- broadcast(void)
- {
- int i;
- readifcs();
- for(i = 0; i < ialloc.nifc; i++){
- if(ialloc.ifc[i].bcast)
- sendto(&ialloc.ifc[i]);
- }
- }
- /*
- * timeout any routes that haven't been refreshed and aren't wired
- */
- void
- timeoutroutes(void)
- {
- int h;
- long now;
- Route *r, **l;
- now = time(0);
- for(h = 0; h < Nhash; h++){
- l = &ralloc.hash[h];
- for(r = *l; r; r = *l){
- if(r->metric < Infinity && now - r->time > 10*60){
- removeroute(r);
- r->inuse = 0;
- *l = r->next;
- continue;
- }
- l = &r->next;
- }
- }
- }
|