Skip to content

Commit

Permalink
Update 44ripd.
Browse files Browse the repository at this point in the history
Updates for the latest version of OpenBSD (6.8 as of
this writing) as well as endian fixes and changes to
the use of tunnels.

Signed-off-by: Dan Cross <[email protected]>
  • Loading branch information
Dan Cross committed Jan 5, 2021
1 parent 94ce425 commit 827fd9d
Show file tree
Hide file tree
Showing 15 changed files with 481 additions and 206 deletions.
45 changes: 28 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,60 @@ CFLAGS= $(FLAGS) -g
SRCS= main.c rip.c lib.c openbsd/sys.c compat.c
OBJS= main.o rip.o lib.o openbsd/sys.o compat.o
PROG= 44ripd
PROGS= $(PROG) amprroute uptunnel
TESTS= testbitvec testipmapfind testipmapnearest \
testisvalidnetmask testnetmask2cidr testrevbits
DTESTS= testipmapinsert
TOBJS= lib.o openbsd/sys.o compat.o
LIBS=
TOBJS= lib.o openbsd/sys.o compat.o testlib.o
LIBS=

all: $(PROG)
all: $(PROGS)

$(PROG): $(OBJS)
$(CC) -o $(PROG) $(OBJS) $(LIBS)

fast$(PROG): $(SRCS) dat.h fns.h
$(CC) $(FLAGS) -Ofast -fwhole-program -flto -o fast$(PROG) $(SRCS)
fast$(PROG): $(SRCS) dat.h fns.h Makefile
$(CC) $(FLAGS) -Ofast -o fast$(PROG) $(SRCS)

amprroute: $(OBJS) amprroute.o
$(CC) -o amprroute amprroute.o lib.o openbsd/sys.o

uptunnel: $(OBJS) uptunnel.o
$(CC) -o uptunnel uptunnel.o lib.o openbsd/sys.o

$(OBJS): dat.h fns.h openbsd/stdalign.h Makefile

.c.o:
$(CC) $(CFLAGS) -c -o $@ $<

clean:
rm -f $(PROGS) fast$(PROG) $(TESTS) $(DTESTS) openbsd/sys.o *.o

tests: $(TESTS) $(DTESTS)
for t in $(TESTS); do ./$$t; done
./testipmapinsert < testdata/testipmapinsert.data
./testipmapinsert < testdata/testipmapinsert.data2
./testipmapinsert < testdata/testipmapinsert.data3

.c.o:
$(CC) $(CFLAGS) -c -o $@ $<

clean:
rm -f $(PROG) fast$(PROG) $(OBJS) test*.o $(TESTS) $(DTESTS)
$(TOBJS): dat.h fns.h testfns.h openbsd/stdalign.h Makefile

testbitvec: testbitvec.o $(TOBJS) dat.h fns.h
testbitvec: testbitvec.o $(TOBJS)
$(CC) -o testbitvec testbitvec.o $(TOBJS)

testipmapfind: testipmapfind.o $(TOBJS) dat.h fns.h
testipmapfind: testipmapfind.o $(TOBJS)
$(CC) -o testipmapfind testipmapfind.o $(TOBJS)

testipmapinsert: testipmapinsert.o $(TOBJS) dat.h fns.h
testipmapinsert: testipmapinsert.o $(TOBJS)
$(CC) -o testipmapinsert testipmapinsert.o $(TOBJS)

testipmapnearest: testipmapnearest.o $(TOBJS) dat.h fns.h
testipmapnearest: testipmapnearest.o $(TOBJS)
$(CC) -o testipmapnearest testipmapnearest.o $(TOBJS)

testisvalidnetmask: testisvalidnetmask.o $(TOBJS) dat.h fns.h
testisvalidnetmask: testisvalidnetmask.o $(TOBJS)
$(CC) -o testisvalidnetmask testisvalidnetmask.o $(TOBJS)

testnetmask2cidr: testnetmask2cidr.o $(TOBJS) dat.h fns.h
testnetmask2cidr: testnetmask2cidr.o $(TOBJS)
$(CC) -o testnetmask2cidr testnetmask2cidr.o $(TOBJS)

testrevbits: testrevbits.o $(TOBJS) dat.h fns.h
testrevbits: testrevbits.o $(TOBJS)
$(CC) -o testrevbits testrevbits.o $(TOBJS)
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
==================================================

This is 44ripd, a daemon for maintaining routes and tunnels on the
AMPRNet amateur radio IP network (IPv4 network 44/8). This software
implements a listener for RIPv2 packets containing route and tunnel
information for gateways to APMRNet sub-networks as well as support
for maintaining routes and IPENCAP tunnels. It runs on OpenBSD,
but is likely portable to other BSD variants with fairly little
work. The author current runs it on a Ubiquiti Networks EdgeRouter
Lite running OpenBSD/Octeon. The software is released under the
2-clause BSD license.
AMPRNet amateur radio IP network (IPv4 network 44/9 and 44.128.0.0/10).

This software implements a listener for RIPv2 packets containing route
and tunnel information for gateways to APMRNet sub-networks as well as
support for maintaining routes and IPENCAP tunnels. It runs on OpenBSD,
but is likely portable to other BSD variants with fairly little work.
The author current runs it on a Ubiquiti Networks EdgeRouter Lite
running OpenBSD/Octeon.

The software is released under the 2-clause BSD license.

Author
------
44ripd was written by Dan Cross, AC2OI. Reach me via email at
44ripd was written by Dan Cross, KZ2X. Reach me via email at
[email protected] or find me on the web at http://pub.gajendra.net/

TODO
Expand Down
79 changes: 79 additions & 0 deletions amprroute.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* A program to manually add or remove tunnel routes. route(8)
* is not sufficiently expressive to do this.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <arpa/inet.h>

#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "dat.h"
#include "fns.h"

int
main(int argc, char *argv[])
{
Route route;
Tunnel tunnel;
struct in_addr addr;
char *slash, *net, *ifname;
unsigned int cidr;
uint32_t netmask;
int ch, rdomain;

rdomain = 0;
while ((ch = getopt(argc, argv, "D:?h")) != -1) {
switch (ch) {
case 'D':
rdomain = strnum(optarg);
break;
case '?':
case 'h':
default:
fatal("usage: amprroute network/cidr ifname");
break;
}
}
argc -= optind;
argv += optind;

initlog();
initsys(rdomain);

if (argc != 2)
fatal("usage: amprroute network/cidr ifname");
net = argv[0];
ifname = argv[1];

slash = strchr(net, '/');
if (slash == NULL)
fatal("network missing CIDR");
*slash++ = '\0';
cidr = strnum(slash);
netmask = cidr2netmask(cidr);

memset(&addr, 0, sizeof(addr));
if (inet_pton(AF_INET, net, &addr) <= 0)
fatal("cannot parse network: %s", net);

memset(&route, 0, sizeof(route));
route.subnetmask = netmask;
route.ipnet = ntohl(addr.s_addr);

memset(&tunnel, 0, sizeof(tunnel));
strlcpy(tunnel.ifname, ifname, sizeof(tunnel.ifname));

addroute(&route, &tunnel, rdomain);

return 0;
}
9 changes: 6 additions & 3 deletions fns.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ int parserippkt(const octet *restrict data, size_t len, RIPPacket *restrict pack
int verifyripauth(RIPPacket *restrict packet, const char *restrict password);
int parseripresponse(const RIPPacket *restrict pkt, int k, RIPResponse *restrict response);
bool isvalidnetmask(uint32_t netmask);
int netmask2cidr(uint32_t netmask);
unsigned int netmask2cidr(uint32_t netmask);
uint32_t cidr2netmask(unsigned int cidr);
uint32_t revbits(uint32_t w);
IPMap *mkipmap(void);
void freeipmap(IPMap *map, void (*freedatum)(void *));
Expand All @@ -21,20 +22,22 @@ void *ipmapinsert(IPMap *map, uint32_t key, size_t keylen, void *datum);
void *ipmapremove(IPMap *map, uint32_t key, size_t keylen);
void *ipmapnearest(IPMap *map, uint32_t key, size_t keylen);
void *ipmapfind(IPMap *map, uint32_t key, size_t keylen);
int initsock(const char *restrict group, int port, int rtable);
int initsock(const char *restrict iface, const char *restrict group, int port, int rtable);
void initsys(int rtable);
int uptunnel(Tunnel *tunnel, int rtable);
int uptunnel(Tunnel *tunnel, int rdomain, int tunneldomain);
int downtunnel(Tunnel *tunnel);
int addroute(Route *route, Tunnel *tunnel, int rtable);
int chroute(Route *route, Tunnel *tunnel, int rtable);
int rmroute(Route *route, int rtable);
void ipaddrstr(uint32_t addr, char buf[static INET_ADDRSTRLEN]);
void routestr(Route *route, Tunnel *tunnel, char *buf, size_t size);
Bitvec *mkbitvec(void);
void freebitvec(Bitvec *bits);
int bitget(Bitvec *bits, size_t bit);
void bitset(Bitvec *bits, size_t bit);
void bitclr(Bitvec *bits, size_t bit);
size_t nextbit(Bitvec *bits);
unsigned int strnum(const char *restrict str);

void initlog(void);
void debug(const char *restrict fmt, ...);
Expand Down
33 changes: 32 additions & 1 deletion lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ isvalidnetmask(uint32_t netmask)
return ((~netmask + 1) & ~netmask) == 0;
}

int
unsigned int
netmask2cidr(uint32_t netmask)
{
int cidr = 32;
Expand All @@ -46,6 +46,17 @@ netmask2cidr(uint32_t netmask)
return cidr;
}

uint32_t
cidr2netmask(unsigned int cidr)
{
if (cidr == 0)
return 0;
if (cidr > 32)
return ~0U;

return ~0U << (32 - cidr);
}

/*
* Reverse the bits of an unsigned 32-bit integer. See Hacker's
* Delight, second edition, for more details.
Expand Down Expand Up @@ -501,6 +512,26 @@ nextbit(Bitvec *bits)
return bits->firstclr;
}

enum {
MAX_NUM = (1 << 20),
};

unsigned int
strnum(const char *restrict str)
{
char *ep;
unsigned long r;

ep = NULL;
r = strtoul(str, &ep, 10);
if (ep != NULL && *ep != '\0')
fatal("bad unsigned integer: %s", str);
if (r > MAX_NUM)
fatal("integer range error: %s", str);

return (unsigned int)r;
}

void
initlog(void)
{
Expand Down
Loading

0 comments on commit 827fd9d

Please sign in to comment.