Re: traceroute AS printing

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Stuart Henderson
Date: Tuesday, May 5, 2009 - 5:48 am

On 2009/05/05 11:00, Stuart Henderson wrote:

it helps if I refrain from breaking the static copy of traceroute
in /var/www. added bgplg support too. traceroute.8 and .c are
unchanged from the last version.

Index: usr.sbin/traceroute/Makefile
===================================================================
RCS file: /cvs/src/usr.sbin/traceroute/Makefile,v
retrieving revision 1.4
diff -u -N -p usr.sbin/traceroute/Makefile
--- usr.sbin/traceroute/Makefile	10 Nov 2004 22:07:46 -0000	1.4
+++ usr.sbin/traceroute/Makefile	5 May 2009 12:45:59 -0000
@@ -1,9 +1,16 @@
 #	$OpenBSD: Makefile,v 1.4 2004/11/10 22:07:46 drahn Exp $
 
+# for bgplg(8)
+PROGDIR?=	${.CURDIR}
+
+.PATH:		${PROGDIR}/../bgpd
+
 PROG=	traceroute
 MAN=	traceroute.8
 BINOWN=	root
 BINMODE=4555
 LDSTATIC=${STATIC}
+SRCS=	traceroute.c buffer.c imsg.c log.c util.c
+CFLAGS+= -I${PROGDIR} -I${PROGDIR}/../bgpd
 
 .include <bsd.prog.mk>
Index: usr.sbin/traceroute/traceroute.8
===================================================================
RCS file: /cvs/src/usr.sbin/traceroute/traceroute.8,v
retrieving revision 1.39
diff -u -N -p usr.sbin/traceroute/traceroute.8
--- usr.sbin/traceroute/traceroute.8	31 May 2007 19:20:30 -0000	1.39
+++ usr.sbin/traceroute/traceroute.8	5 May 2009 12:45:59 -0000
@@ -42,7 +42,8 @@
 .Sh SYNOPSIS
 .Nm traceroute
 .Bk -words
-.Op Fl cDdIlnrSv
+.Op Fl bcDdIlnrSv
+.Op Fl B Ar socket
 .Op Fl f Ar first_ttl
 .Op Fl g Ar gateway_addr
 .Op Fl m Ar max_ttl
@@ -74,6 +75,17 @@ name.
 .Pp
 The options are as follows:
 .Bl -tag -width Ds
+.It Xo
+.Fl B Ar socket |
+.Fl b
+.Xc
+Look up the AS number of each hop from a locally running
+.Xr bgpd 8 .
+An alternative to the default control socket,
+.Pa /var/run/bgpd.sock ,
+may be given with the
+.Fl B
+option.
 .It Fl c
 Do not increment the destination port number in successive UDP packets.
 Rather, all UDP packets will have the same destination port, as set via the
@@ -399,6 +411,7 @@ Because of the load it could impose on the network, it
 during normal operations or from automated scripts.
 .Sh SEE ALSO
 .Xr netstat 1 ,
+.Xr bgpd 8 ,
 .Xr ping 8
 .Sh HISTORY
 The very first
Index: usr.sbin/traceroute/traceroute.c
===================================================================
RCS file: /cvs/src/usr.sbin/traceroute/traceroute.c,v
retrieving revision 1.66
diff -u -N -p usr.sbin/traceroute/traceroute.c
--- usr.sbin/traceroute/traceroute.c	4 Oct 2008 02:21:49 -0000	1.66
+++ usr.sbin/traceroute/traceroute.c	5 May 2009 12:45:59 -0000
@@ -224,6 +224,7 @@ static char rcsid[] = "$OpenBSD: traceroute.c,v 1.66 2
 #include <sys/file.h>
 #include <sys/ioctl.h>
 #include <sys/sysctl.h>
+#include <sys/un.h>
 
 #include <netinet/in_systm.h>
 #include <netinet/in.h>
@@ -243,6 +244,9 @@ static char rcsid[] = "$OpenBSD: traceroute.c,v 1.66 2
 #include <string.h>
 #include <unistd.h>
 
+#include "bgpd.h"
+#include "log.h"
+
 #define	MAX_LSRR	((MAX_IPOPTLEN - 4) / 4)
 
 /*
@@ -270,6 +274,9 @@ void print(u_char *, int, struct sockaddr_in *);
 char *inetname(struct in_addr);
 u_short in_cksum(u_short *, int);
 void usage(void);
+u_int32_t find_asn(struct sockaddr_in *);
+int read_rib_summary_msg(struct imsg *, u_int32_t *);
+int show_result(struct imsg *);
 
 int s;				/* receive (icmp) socket file descriptor */
 int sndsock;			/* send (udp) socket file descriptor */
@@ -293,15 +300,20 @@ int verbose;
 int waittime = 5;		/* time to wait for response (in seconds) */
 int nflag;			/* print addresses numerically */
 int dump;
+int bflag;			/* print origin AS of addresses */
+char *bgpsock = "/var/run/bgpd.sock";
+				/* path to bgpd control socket */
+struct imsgbuf *ibuf;
 
 int
 main(int argc, char *argv[])
 {
 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
 	int ttl_flag = 0, incflag = 1, protoset = 0, sump = 0;
-	int ch, i, lsrr = 0, on = 1, probe, seq = 0, tos = 0;
+	int ch, fd, i, lsrr = 0, on = 1, probe, seq = 0, tos = 0;
 	size_t size = sizeof(max_ttl);
 	struct sockaddr_in from, to;
+	struct sockaddr_un sun;
 	struct hostent *hp;
 	u_int32_t tmprnd;
 	struct ip *ip;
@@ -323,11 +335,18 @@ main(int argc, char *argv[])
 	(void) sysctl(mib, sizeof(mib)/sizeof(mib[0]), &max_ttl, &size,
 	    NULL, 0);
 
-	while ((ch = getopt(argc, argv, "SDIdg:f:m:np:q:rs:t:w:vlP:c")) != -1)
+	while ((ch = getopt(argc, argv, "SDIbB:dg:f:m:np:q:rs:t:w:vlP:c")) != -1)
 		switch (ch) {
 		case 'S':
 			sump = 1;
 			break;
+		case 'B':
+			if (*optarg)
+				bgpsock=optarg;
+			/* FALLTHROUGH */
+		case 'b':
+			bflag = 1;
+			break;
 		case 'f':
 			errno = 0;
 			ep = NULL;
@@ -480,6 +499,23 @@ main(int argc, char *argv[])
 		datalen = (int)l;
 	}
 
+	if (bflag) {
+		if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+			err(1, "socket");
+
+		bzero(&sun, sizeof(sun));
+		sun.sun_family = AF_UNIX;
+		if (strlcpy(sun.sun_path, bgpsock, sizeof(sun.sun_path)) >=
+		    sizeof(sun.sun_path))
+			errx(1, "socket name too long");
+		if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
+			err(1, "connect: %s", bgpsock);
+
+		if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
+			err(1, NULL);
+		imsg_init(ibuf, fd);
+	}
+
 	switch (proto) {
 	case IPPROTO_UDP:
 		headerlen = (sizeof(struct ip) + lsrrlen +
@@ -703,6 +739,11 @@ main(int argc, char *argv[])
 		if (got_there || (unreachable && (unreachable + timeout) >= nprobes))
 			break;
 	}
+	if(fd > 0)
+		close(fd);
+	if(ibuf != NULL)
+		free(ibuf);
+
 	exit(0);
 }
 
@@ -951,6 +992,7 @@ print(u_char *buf, int cc, struct sockaddr_in *from)
 {
 	struct ip *ip;
 	int hlen;
+	u_int32_t as;
 
 	ip = (struct ip *) buf;
 	hlen = ip->ip_hl << 2;
@@ -962,11 +1004,97 @@ print(u_char *buf, int cc, struct sockaddr_in *from)
 		printf(" %s (%s)", inetname(from->sin_addr),
 		    inet_ntoa(from->sin_addr));
 
+	if (bflag) {
+		if (as = find_asn(from))
+			printf(" [AS %s]", log_as(as));
+	}
+
 	if (verbose)
 		printf(" %d bytes to %s", cc, inet_ntoa (ip->ip_dst));
 }
 
+u_int32_t
+find_asn(struct sockaddr_in *from)
+{
+	struct bgpd_addr		 baddr;
+	int				 done = 0, n = 0;
+	struct imsg			 imsg;
+	struct ctl_show_rib_request	 ribreq;
+	enum imsg_type			 type;
+	u_int32_t			 as = 0;
 
+	bzero(&baddr, sizeof(baddr));
+	baddr.af=AF_INET;
+	baddr.v4=from->sin_addr;
+
+	bzero(&ribreq, sizeof(ribreq));
+	memcpy(&ribreq.prefix, &baddr, sizeof(baddr));
+	ribreq.prefixlen = 32;
+	type = IMSG_CTL_SHOW_RIB_PREFIX;
+	imsg_compose(ibuf, type, 0, 0, -1, &ribreq, sizeof(ribreq));
+
+	while (ibuf->w.queued)
+		if (msgbuf_write(&ibuf->w) < 0)
+			err(1, "write error");
+
+	while (!done) {
+		if ((n = imsg_read(ibuf)) == -1)
+			errx(1, "imsg_read error");
+		if (n == 0)
+			errx(1, "pipe closed");
+
+		while (!done) {
+			if ((n = imsg_get(ibuf, &imsg)) == -1)
+				errx(1, "imsg_get error");
+			if (n == 0)
+				break;
+
+			if (imsg.hdr.type == IMSG_CTL_RESULT) {
+				imsg_free(&imsg);
+				break;
+			}
+
+			done = read_rib_summary_msg(&imsg,&as);
+			imsg_free(&imsg);
+		}
+	}
+	return(as);
+}
+
+int
+read_rib_summary_msg(struct imsg *imsg, u_int32_t *as)
+{
+	struct ctl_show_rib	 rib;
+	char			*aspath;
+	u_char			*asdata;
+	u_int16_t		 len, seg_size;
+	u_int8_t		*seg;
+	u_int8_t		 i, seg_type, seg_len;
+
+	switch (imsg->hdr.type) {
+	case IMSG_CTL_SHOW_RIB:
+		memcpy(&rib, imsg->data, sizeof(rib));
+		asdata = imsg->data;
+		asdata += sizeof(struct ctl_show_rib);
+		len = rib.aspath_len;
+		seg = asdata;
+
+		for (; len > 0; len -= seg_size, seg += seg_size) {
+			seg_type = seg[0];
+			seg_len = seg[1];
+			seg_size = 2 + sizeof(u_int32_t) * seg_len;
+			if(seg_len > 0)
+				*as = aspath_extract(seg, seg_len-1);
+		}
+		break;
+	case IMSG_CTL_END:
+		return (1);
+	default:
+		break;
+	}
+	return (0);
+}
+
 /*
  * Checksum routine for Internet Protocol family headers (C Version)
  */
@@ -1039,8 +1167,8 @@ usage(void)
 	extern char *__progname;
 
 	fprintf(stderr,
-	    "usage: %s [-cDdIlnrSv] [-f first_ttl] [-g gateway_addr] [-m max_ttl]\n"
-	    "\t[-P proto] [-p port] [-q nqueries] [-s src_addr] [-t tos]\n"
+	    "usage: %s [-bcDdIlnrSv] [-B socket] [-f first_ttl] [-g gateway_addr]\n"
+	    "\t[-m max_ttl] [-P proto] [-p port] [-q nqueries] [-s src_addr] [-t tos]\n"
 	    "\t[-w waittime] host [packetsize]\n", __progname);
 	exit(1);
 }
Index: usr.bin/bgplg/bgplg.c
===================================================================
RCS file: /cvs/src/usr.bin/bgplg/bgplg.c,v
retrieving revision 1.7
diff -u -N -p usr.bin/bgplg/bgplg.c
--- usr.bin/bgplg/bgplg.c	10 Oct 2007 13:23:40 -0000	1.7
+++ usr.bin/bgplg/bgplg.c	5 May 2009 12:45:59 -0000
@@ -38,7 +38,7 @@
 #define BGPDSOCK	"/logs/bgpd.rsock"
 #define BGPCTL		"/bin/bgpctl", "-s", BGPDSOCK
 #define PING		"/bin/ping"
-#define TRACEROUTE	"/bin/traceroute"
+#define TRACEROUTE	"/bin/traceroute", "-B", BGPDSOCK
 #define CONTENT_TYPE	"text/html"
 
 static struct cmd cmds[] = CMDS;
Index: usr.bin/bgplg/bgplgsh.c
===================================================================
RCS file: /cvs/src/usr.bin/bgplg/bgplgsh.c,v
retrieving revision 1.2
diff -u -N -p usr.bin/bgplg/bgplgsh.c
--- usr.bin/bgplg/bgplgsh.c	12 Dec 2006 11:43:50 -0000	1.2
+++ usr.bin/bgplg/bgplgsh.c	5 May 2009 12:45:59 -0000
@@ -37,7 +37,7 @@
 #define BGPDSOCK	"/var/www/logs/bgpd.rsock"
 #define BGPCTL		"/usr/sbin/bgpctl", "-s", BGPDSOCK
 #define PING		"/sbin/ping"
-#define TRACEROUTE	"/usr/sbin/traceroute"
+#define TRACEROUTE	"/usr/sbin/traceroute", "-B", BGPDSOCK
 
 static volatile int quit;
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
traceroute AS printing, Stuart Henderson, (Sat May 2, 4:11 am)
Re: traceroute AS printing, patrick keshishian, (Sat May 2, 5:36 am)
Re: traceroute AS printing, Gilles Chehade, (Sat May 2, 6:39 am)
Re: traceroute AS printing, Claudio Jeker, (Sat May 2, 7:05 am)
Re: traceroute AS printing, Stuart Henderson, (Sat May 2, 1:32 pm)
Re: traceroute AS printing, Stuart Henderson, (Tue May 5, 3:00 am)
Re: traceroute AS printing, Stuart Henderson, (Tue May 5, 5:48 am)
Re: traceroute AS printing, patrick keshishian, (Tue May 5, 9:00 am)
Re: traceroute AS printing, Claudio Jeker, (Tue May 5, 11:18 am)
Re: traceroute AS printing, Henning Brauer, (Thu May 7, 6:44 am)