ospfctl.c revision 1.49
1/*	$OpenBSD: ospfctl.c,v 1.49 2009/09/14 11:49:25 claudio Exp $ */
2
3/*
4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
5 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
6 * Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <sys/un.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <net/if_media.h>
27#include <net/if_types.h>
28
29#include <err.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35#include "ospf.h"
36#include "ospfd.h"
37#include "ospfe.h"
38#include "parser.h"
39
40__dead void	 usage(void);
41int		 show_summary_msg(struct imsg *);
42int		 get_ifms_type(int);
43int		 show_interface_msg(struct imsg *);
44int		 show_interface_detail_msg(struct imsg *);
45const char	*print_link(int);
46const char	*fmt_timeframe(time_t t);
47const char	*fmt_timeframe_core(time_t t);
48const char	*log_id(u_int32_t );
49const char	*log_adv_rtr(u_int32_t);
50void		 show_database_head(struct in_addr, u_int8_t);
51int		 show_database_msg(struct imsg *);
52char		*print_ls_type(u_int8_t);
53void		 show_db_hdr_msg_detail(struct lsa_hdr *);
54char		*print_rtr_link_type(u_int8_t);
55const char	*print_ospf_flags(u_int8_t);
56int		 show_db_msg_detail(struct imsg *imsg);
57int		 show_nbr_msg(struct imsg *);
58const char	*print_ospf_options(u_int8_t);
59int		 show_nbr_detail_msg(struct imsg *);
60int		 show_rib_msg(struct imsg *);
61void		 show_rib_head(struct in_addr, u_int8_t, u_int8_t);
62const char	*print_ospf_rtr_flags(u_int8_t);
63int		 show_rib_detail_msg(struct imsg *);
64void		 show_fib_head(void);
65int		 show_fib_msg(struct imsg *);
66void		 show_interface_head(void);
67const char *	 get_media_descr(int);
68const char *	 get_linkstate(int, int);
69void		 print_baudrate(u_int64_t);
70int		 show_fib_interface_msg(struct imsg *);
71
72struct imsgbuf	*ibuf;
73
74__dead void
75usage(void)
76{
77	extern char *__progname;
78
79	fprintf(stderr, "usage: %s [-s socket] command [argument ...]\n",
80	    __progname);
81	exit(1);
82}
83
84int
85main(int argc, char *argv[])
86{
87	struct sockaddr_un	 sun;
88	struct parse_result	*res;
89	struct imsg		 imsg;
90	unsigned int		 ifidx = 0;
91	int			 ctl_sock;
92	int			 done = 0;
93	int			 n;
94	int			 ch;
95	char			*sockname;
96
97	sockname = OSPFD_SOCKET;
98	while ((ch = getopt(argc, argv, "s:")) != -1) {
99		switch (ch) {
100		case 's':
101			sockname = optarg;
102			break;
103		default:
104			usage();
105			/* NOTREACHED */
106		}
107	}
108	argc -= optind;
109	argv += optind;
110
111	/* parse options */
112	if ((res = parse(argc, argv)) == NULL)
113		exit(1);
114
115	/* connect to ospfd control socket */
116	if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
117		err(1, "socket");
118
119	bzero(&sun, sizeof(sun));
120	sun.sun_family = AF_UNIX;
121
122	strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path));
123	if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
124		err(1, "connect: %s", sockname);
125
126	if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
127		err(1, NULL);
128	imsg_init(ibuf, ctl_sock);
129	done = 0;
130
131	/* process user request */
132	switch (res->action) {
133	case NONE:
134		usage();
135		/* not reached */
136	case SHOW:
137	case SHOW_SUM:
138		imsg_compose(ibuf, IMSG_CTL_SHOW_SUM, 0, 0, -1, NULL, 0);
139		break;
140	case SHOW_IFACE:
141		printf("%-11s %-18s %-6s %-10s %-10s %-8s %3s %3s\n",
142		    "Interface", "Address", "State", "HelloTimer", "Linkstate",
143		    "Uptime", "nc", "ac");
144		/*FALLTHROUGH*/
145	case SHOW_IFACE_DTAIL:
146		if (*res->ifname) {
147			ifidx = if_nametoindex(res->ifname);
148			if (ifidx == 0)
149				errx(1, "no such interface %s", res->ifname);
150		}
151		imsg_compose(ibuf, IMSG_CTL_SHOW_INTERFACE, 0, 0, -1,
152		    &ifidx, sizeof(ifidx));
153		break;
154	case SHOW_NBR:
155		printf("%-15s %-3s %-12s %-8s %-15s %-9s %s\n", "ID", "Pri",
156		    "State", "DeadTime", "Address", "Iface","Uptime");
157		/*FALLTHROUGH*/
158	case SHOW_NBR_DTAIL:
159		imsg_compose(ibuf, IMSG_CTL_SHOW_NBR, 0, 0, -1, NULL, 0);
160		break;
161	case SHOW_DB:
162		imsg_compose(ibuf, IMSG_CTL_SHOW_DATABASE, 0, 0, -1, NULL, 0);
163		break;
164	case SHOW_DBBYAREA:
165		imsg_compose(ibuf, IMSG_CTL_SHOW_DATABASE, 0, 0, -1,
166		    &res->addr, sizeof(res->addr));
167		break;
168	case SHOW_DBEXT:
169		imsg_compose(ibuf, IMSG_CTL_SHOW_DB_EXT, 0, 0, -1, NULL, 0);
170		break;
171	case SHOW_DBNET:
172		imsg_compose(ibuf, IMSG_CTL_SHOW_DB_NET, 0, 0, -1, NULL, 0);
173		break;
174	case SHOW_DBRTR:
175		imsg_compose(ibuf, IMSG_CTL_SHOW_DB_RTR, 0, 0, -1, NULL, 0);
176		break;
177	case SHOW_DBSELF:
178		imsg_compose(ibuf, IMSG_CTL_SHOW_DB_SELF, 0, 0, -1, NULL, 0);
179		break;
180	case SHOW_DBSUM:
181		imsg_compose(ibuf, IMSG_CTL_SHOW_DB_SUM, 0, 0, -1, NULL, 0);
182		break;
183	case SHOW_DBASBR:
184		imsg_compose(ibuf, IMSG_CTL_SHOW_DB_ASBR, 0, 0, -1, NULL, 0);
185		break;
186	case SHOW_RIB:
187		printf("%-20s %-17s %-12s %-9s %-7s %-8s\n", "Destination",
188		    "Nexthop", "Path Type", "Type", "Cost", "Uptime");
189		/*FALLTHROUGH*/
190	case SHOW_RIB_DTAIL:
191		imsg_compose(ibuf, IMSG_CTL_SHOW_RIB, 0, 0, -1, NULL, 0);
192		break;
193	case SHOW_FIB:
194		if (!res->addr.s_addr)
195			imsg_compose(ibuf, IMSG_CTL_KROUTE, 0, 0, -1,
196			    &res->flags, sizeof(res->flags));
197		else
198			imsg_compose(ibuf, IMSG_CTL_KROUTE_ADDR, 0, 0, -1,
199			    &res->addr, sizeof(res->addr));
200		show_fib_head();
201		break;
202	case SHOW_FIB_IFACE:
203		if (*res->ifname)
204			imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, -1,
205			    res->ifname, sizeof(res->ifname));
206		else
207			imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, -1, NULL, 0);
208		show_interface_head();
209		break;
210	case FIB:
211		errx(1, "fib couple|decouple");
212		break;
213	case FIB_COUPLE:
214		imsg_compose(ibuf, IMSG_CTL_FIB_COUPLE, 0, 0, -1, NULL, 0);
215		printf("couple request sent.\n");
216		done = 1;
217		break;
218	case FIB_DECOUPLE:
219		imsg_compose(ibuf, IMSG_CTL_FIB_DECOUPLE, 0, 0, -1, NULL, 0);
220		printf("decouple request sent.\n");
221		done = 1;
222		break;
223	case RELOAD:
224		imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0);
225		printf("reload request sent.\n");
226		done = 1;
227		break;
228	}
229
230	while (ibuf->w.queued)
231		if (msgbuf_write(&ibuf->w) < 0)
232			err(1, "write error");
233
234	while (!done) {
235		if ((n = imsg_read(ibuf)) == -1)
236			errx(1, "imsg_read error");
237		if (n == 0)
238			errx(1, "pipe closed");
239
240		while (!done) {
241			if ((n = imsg_get(ibuf, &imsg)) == -1)
242				errx(1, "imsg_get error");
243			if (n == 0)
244				break;
245			switch (res->action) {
246			case SHOW:
247			case SHOW_SUM:
248				done = show_summary_msg(&imsg);
249				break;
250			case SHOW_IFACE:
251				done = show_interface_msg(&imsg);
252				break;
253			case SHOW_IFACE_DTAIL:
254				done = show_interface_detail_msg(&imsg);
255				break;
256			case SHOW_NBR:
257				done = show_nbr_msg(&imsg);
258				break;
259			case SHOW_NBR_DTAIL:
260				done = show_nbr_detail_msg(&imsg);
261				break;
262			case SHOW_DB:
263			case SHOW_DBBYAREA:
264			case SHOW_DBSELF:
265				done = show_database_msg(&imsg);
266				break;
267			case SHOW_DBEXT:
268			case SHOW_DBNET:
269			case SHOW_DBRTR:
270			case SHOW_DBSUM:
271			case SHOW_DBASBR:
272				done = show_db_msg_detail(&imsg);
273				break;
274			case SHOW_RIB:
275				done = show_rib_msg(&imsg);
276				break;
277			case SHOW_RIB_DTAIL:
278				done = show_rib_detail_msg(&imsg);
279				break;
280			case SHOW_FIB:
281				done = show_fib_msg(&imsg);
282				break;
283			case SHOW_FIB_IFACE:
284				done = show_fib_interface_msg(&imsg);
285				break;
286			case NONE:
287			case FIB:
288			case FIB_COUPLE:
289			case FIB_DECOUPLE:
290			case RELOAD:
291				break;
292			}
293			imsg_free(&imsg);
294		}
295	}
296	close(ctl_sock);
297	free(ibuf);
298
299	return (0);
300}
301
302int
303show_summary_msg(struct imsg *imsg)
304{
305	struct ctl_sum		*sum;
306	struct ctl_sum_area	*sumarea;
307
308	switch (imsg->hdr.type) {
309	case IMSG_CTL_SHOW_SUM:
310		sum = imsg->data;
311		printf("Router ID: %s\n", inet_ntoa(sum->rtr_id));
312		printf("Uptime: %s\n", fmt_timeframe_core(sum->uptime));
313		printf("RFC1583 compatibility flag is ");
314		if (sum->rfc1583compat)
315			printf("enabled\n");
316		else
317			printf("disabled\n");
318
319		printf("SPF delay is %d sec(s), hold time between two SPFs "
320		    "is %d sec(s)\n", sum->spf_delay, sum->spf_hold_time);
321		printf("Number of external LSA(s) %d\n", sum->num_ext_lsa);
322		printf("Number of areas attached to this router: %d\n",
323		    sum->num_area);
324		break;
325	case IMSG_CTL_SHOW_SUM_AREA:
326		sumarea = imsg->data;
327		printf("\nArea ID: %s\n", inet_ntoa(sumarea->area));
328		printf("  Number of interfaces in this area: %d\n",
329		    sumarea->num_iface);
330		printf("  Number of fully adjacent neighbors in this "
331		    "area: %d\n", sumarea->num_adj_nbr);
332		printf("  SPF algorithm executed %d time(s)\n",
333		    sumarea->num_spf_calc);
334		printf("  Number LSA(s) %d\n", sumarea->num_lsa);
335		break;
336	case IMSG_CTL_END:
337		printf("\n");
338		return (1);
339	default:
340		break;
341	}
342
343	return (0);
344}
345
346int
347get_ifms_type(int mediatype)
348{
349	switch (mediatype) {
350	case IFT_ETHER:
351		return (IFM_ETHER);
352	case IFT_FDDI:
353		return (IFM_FDDI);
354	case IFT_CARP:
355		return (IFM_CARP);
356	case IFT_PPP:
357		return (IFM_TDM);
358	default:
359		return (0);
360	}
361}
362
363int
364show_interface_msg(struct imsg *imsg)
365{
366	struct ctl_iface	*iface;
367	char			*netid;
368
369	switch (imsg->hdr.type) {
370	case IMSG_CTL_SHOW_INTERFACE:
371		iface = imsg->data;
372
373		if (asprintf(&netid, "%s/%d", inet_ntoa(iface->addr),
374		    mask2prefixlen(iface->mask.s_addr)) == -1)
375			err(1, NULL);
376		printf("%-11s %-18s %-6s %-10s %-10s %s %3d %3d\n",
377		    iface->name, netid, if_state_name(iface->state),
378		    iface->hello_timer < 0 ? "-" :
379		    fmt_timeframe_core(iface->hello_timer),
380		    get_linkstate(iface->mediatype, iface->linkstate),
381		    fmt_timeframe_core(iface->uptime),
382		    iface->nbr_cnt, iface->adj_cnt);
383		free(netid);
384		break;
385	case IMSG_CTL_END:
386		printf("\n");
387		return (1);
388	default:
389		break;
390	}
391
392	return (0);
393}
394
395int
396show_interface_detail_msg(struct imsg *imsg)
397{
398	struct ctl_iface	*iface;
399
400	switch (imsg->hdr.type) {
401	case IMSG_CTL_SHOW_INTERFACE:
402		iface = imsg->data;
403		printf("\n");
404		printf("Interface %s, line protocol is %s\n",
405		    iface->name, print_link(iface->flags));
406		printf("  Internet address %s/%d, ",
407		    inet_ntoa(iface->addr),
408		    mask2prefixlen(iface->mask.s_addr));
409		printf("Area %s\n", inet_ntoa(iface->area));
410		printf("  Linkstate %s\n",
411		    get_linkstate(iface->mediatype, iface->linkstate));
412		printf("  Router ID %s, network type %s, cost: %d\n",
413		    inet_ntoa(iface->rtr_id),
414		    if_type_name(iface->type), iface->metric);
415		printf("  Transmit delay is %d sec(s), state %s, priority %d\n",
416		    iface->transmit_delay, if_state_name(iface->state),
417		    iface->priority);
418		printf("  Designated Router (ID) %s, ",
419		    inet_ntoa(iface->dr_id));
420		printf("interface address %s\n", inet_ntoa(iface->dr_addr));
421		printf("  Backup Designated Router (ID) %s, ",
422		    inet_ntoa(iface->bdr_id));
423		printf("interface address %s\n", inet_ntoa(iface->bdr_addr));
424		printf("  Timer intervals configured, "
425		    "hello %d, dead %d, wait %d, retransmit %d\n",
426		     iface->hello_interval, iface->dead_interval,
427		     iface->dead_interval, iface->rxmt_interval);
428		if (iface->passive)
429			printf("    Passive interface (No Hellos)\n");
430		else if (iface->hello_timer < 0)
431			printf("    Hello timer not running\n");
432		else
433			printf("    Hello timer due in %s\n",
434			    fmt_timeframe_core(iface->hello_timer));
435		printf("    Uptime %s\n", fmt_timeframe_core(iface->uptime));
436		printf("  Neighbor count is %d, adjacent neighbor count is "
437		    "%d\n", iface->nbr_cnt, iface->adj_cnt);
438		if (iface->auth_type > 0) {
439			switch (iface->auth_type) {
440			case AUTH_SIMPLE:
441				printf("  Simple password authentication "
442				    "enabled\n");
443				break;
444			case AUTH_CRYPT:
445				printf("  Message digest authentication "
446				    "enabled\n");
447				printf("    Primary key id is %d\n",
448				    iface->auth_keyid);
449				break;
450			default:
451				break;
452			}
453		}
454		break;
455	case IMSG_CTL_END:
456		printf("\n");
457		return (1);
458	default:
459		break;
460	}
461
462	return (0);
463}
464
465const char *
466print_link(int state)
467{
468	if (state & IFF_UP)
469		return ("UP");
470	else
471		return ("DOWN");
472}
473
474#define TF_BUFS	8
475#define TF_LEN	9
476
477const char *
478fmt_timeframe(time_t t)
479{
480	if (t == 0)
481		return ("Never");
482	else
483		return (fmt_timeframe_core(time(NULL) - t));
484}
485
486const char *
487fmt_timeframe_core(time_t t)
488{
489	char		*buf;
490	static char	 tfbuf[TF_BUFS][TF_LEN];	/* ring buffer */
491	static int	 idx = 0;
492	unsigned int	 sec, min, hrs, day, week;
493
494	if (t == 0)
495		return ("00:00:00");
496
497	buf = tfbuf[idx++];
498	if (idx == TF_BUFS)
499		idx = 0;
500
501	week = t;
502
503	sec = week % 60;
504	week /= 60;
505	min = week % 60;
506	week /= 60;
507	hrs = week % 24;
508	week /= 24;
509	day = week % 7;
510	week /= 7;
511
512	if (week > 0)
513		snprintf(buf, TF_LEN, "%02uw%01ud%02uh", week, day, hrs);
514	else if (day > 0)
515		snprintf(buf, TF_LEN, "%01ud%02uh%02um", day, hrs, min);
516	else
517		snprintf(buf, TF_LEN, "%02u:%02u:%02u", hrs, min, sec);
518
519	return (buf);
520}
521
522const char *
523log_id(u_int32_t id)
524{
525	static char	buf[48];
526	struct in_addr	addr;
527
528	addr.s_addr = id;
529
530	if (inet_ntop(AF_INET, &addr, buf, sizeof(buf)) == NULL)
531		return ("?");
532	else
533		return (buf);
534}
535
536const char *
537log_adv_rtr(u_int32_t adv_rtr)
538{
539	static char	buf[48];
540	struct in_addr	addr;
541
542	addr.s_addr = adv_rtr;
543
544	if (inet_ntop(AF_INET, &addr, buf, sizeof(buf)) == NULL)
545		return ("?");
546	else
547		return (buf);
548}
549
550/* prototype defined in ospfd.h and shared with the kroute.c version */
551u_int8_t
552mask2prefixlen(in_addr_t ina)
553{
554	if (ina == 0)
555		return (0);
556	else
557		return (33 - ffs(ntohl(ina)));
558}
559
560void
561show_database_head(struct in_addr aid, u_int8_t type)
562{
563	char	*header, *format;
564
565	switch (type) {
566	case LSA_TYPE_ROUTER:
567		format = "Router Link States";
568		break;
569	case LSA_TYPE_NETWORK:
570		format = "Net Link States";
571		break;
572	case LSA_TYPE_SUM_NETWORK:
573		format = "Summary Net Link States";
574		break;
575	case LSA_TYPE_SUM_ROUTER:
576		format = "Summary Router Link States";
577		break;
578	case LSA_TYPE_EXTERNAL:
579		format = NULL;
580		if ((header = strdup("Type-5 AS External Link States")) == NULL)
581			err(1, NULL);
582		break;
583	default:
584		errx(1, "unknown LSA type");
585	}
586	if (type != LSA_TYPE_EXTERNAL)
587		if (asprintf(&header, "%s (Area %s)", format,
588		    inet_ntoa(aid)) == -1)
589			err(1, NULL);
590
591	printf("\n%-15s %s\n\n", "", header);
592	free(header);
593}
594
595int
596show_database_msg(struct imsg *imsg)
597{
598	static struct in_addr	 area_id;
599	static u_int8_t		 lasttype;
600	struct area		*area;
601	struct lsa_hdr		*lsa;
602
603	switch (imsg->hdr.type) {
604	case IMSG_CTL_SHOW_DATABASE:
605	case IMSG_CTL_SHOW_DB_SELF:
606		lsa = imsg->data;
607		if (lsa->type != lasttype) {
608			show_database_head(area_id, lsa->type);
609			printf("%-15s %-15s %-4s %-10s %-8s\n", "Link ID",
610			    "Adv Router", "Age", "Seq#", "Checksum");
611		}
612		printf("%-15s %-15s %-4d 0x%08x 0x%04x\n",
613		    log_id(lsa->ls_id), log_adv_rtr(lsa->adv_rtr),
614		    ntohs(lsa->age), ntohl(lsa->seq_num),
615		    ntohs(lsa->ls_chksum));
616		lasttype = lsa->type;
617		break;
618	case IMSG_CTL_AREA:
619		area = imsg->data;
620		area_id = area->id;
621		lasttype = 0;
622		break;
623	case IMSG_CTL_END:
624		printf("\n");
625		return (1);
626	default:
627		break;
628	}
629
630	return (0);
631}
632
633char *
634print_ls_type(u_int8_t type)
635{
636	switch (type) {
637	case LSA_TYPE_ROUTER:
638		return ("Router");
639	case LSA_TYPE_NETWORK:
640		return ("Network");
641	case LSA_TYPE_SUM_NETWORK:
642		return ("Summary (Network)");
643	case LSA_TYPE_SUM_ROUTER:
644		return ("Summary (Router)");
645	case LSA_TYPE_EXTERNAL:
646		return ("AS External");
647	default:
648		return ("Unknown");
649	}
650}
651
652void
653show_db_hdr_msg_detail(struct lsa_hdr *lsa)
654{
655	printf("LS age: %d\n", ntohs(lsa->age));
656	printf("Options: %s\n", print_ospf_options(lsa->opts));
657	printf("LS Type: %s\n", print_ls_type(lsa->type));
658
659	switch (lsa->type) {
660	case LSA_TYPE_ROUTER:
661		printf("Link State ID: %s\n", log_id(lsa->ls_id));
662		break;
663	case LSA_TYPE_NETWORK:
664		printf("Link State ID: %s (address of Designated Router)\n",
665		    log_id(lsa->ls_id));
666		break;
667	case LSA_TYPE_SUM_NETWORK:
668		printf("Link State ID: %s (Network ID)\n", log_id(lsa->ls_id));
669		break;
670	case LSA_TYPE_SUM_ROUTER:
671		printf("Link State ID: %s (ASBR Router ID)\n",
672		    log_id(lsa->ls_id));
673		break;
674	case LSA_TYPE_EXTERNAL:
675		printf("Link State ID: %s (External Network Number)\n",
676		     log_id(lsa->ls_id));
677		break;
678	}
679
680	printf("Advertising Router: %s\n", log_adv_rtr(lsa->adv_rtr));
681	printf("LS Seq Number: 0x%08x\n", ntohl(lsa->seq_num));
682	printf("Checksum: 0x%04x\n", ntohs(lsa->ls_chksum));
683	printf("Length: %d\n", ntohs(lsa->len));
684}
685
686char *
687print_rtr_link_type(u_int8_t type)
688{
689	switch (type) {
690	case LINK_TYPE_POINTTOPOINT:
691		return ("Point-to-Point");
692	case LINK_TYPE_TRANSIT_NET:
693		return ("Transit Network");
694	case LINK_TYPE_STUB_NET:
695		return ("Stub Network");
696	case LINK_TYPE_VIRTUAL:
697		return ("Virtual Link");
698	default:
699		return ("Unknown");
700	}
701}
702
703const char *
704print_ospf_flags(u_int8_t opts)
705{
706	static char	optbuf[32];
707
708	snprintf(optbuf, sizeof(optbuf), "*|*|*|*|*|%s|%s|%s",
709	    opts & OSPF_RTR_V ? "V" : "-",
710	    opts & OSPF_RTR_E ? "E" : "-",
711	    opts & OSPF_RTR_B ? "B" : "-");
712	return (optbuf);
713}
714
715int
716show_db_msg_detail(struct imsg *imsg)
717{
718	static struct in_addr	 area_id;
719	static u_int8_t		 lasttype;
720	struct in_addr		 addr, data;
721	struct area		*area;
722	struct lsa		*lsa;
723	struct lsa_rtr_link	*rtr_link;
724	struct lsa_asext	*asext;
725	u_int16_t		 i, nlinks, off;
726
727	/* XXX sanity checks! */
728
729	switch (imsg->hdr.type) {
730	case IMSG_CTL_SHOW_DB_EXT:
731		lsa = imsg->data;
732		if (lsa->hdr.type != lasttype)
733			show_database_head(area_id, lsa->hdr.type);
734		show_db_hdr_msg_detail(&lsa->hdr);
735		addr.s_addr = lsa->data.asext.mask;
736		printf("Network Mask: %s\n", inet_ntoa(addr));
737
738		asext = (struct lsa_asext *)((char *)lsa + sizeof(lsa->hdr));
739
740		printf("    Metric type: ");
741		if (ntohl(lsa->data.asext.metric) & LSA_ASEXT_E_FLAG)
742			printf("2\n");
743		else
744			printf("1\n");
745		printf("    Metric: %d\n", ntohl(asext->metric)
746		    & LSA_METRIC_MASK);
747		addr.s_addr = asext->fw_addr;
748		printf("    Forwarding Address: %s\n", inet_ntoa(addr));
749		printf("    External Route Tag: %d\n\n", ntohl(asext->ext_tag));
750
751		lasttype = lsa->hdr.type;
752		break;
753	case IMSG_CTL_SHOW_DB_NET:
754		lsa = imsg->data;
755		if (lsa->hdr.type != lasttype)
756			show_database_head(area_id, lsa->hdr.type);
757		show_db_hdr_msg_detail(&lsa->hdr);
758		addr.s_addr = lsa->data.net.mask;
759		printf("Network Mask: %s\n", inet_ntoa(addr));
760
761		nlinks = (ntohs(lsa->hdr.len) - sizeof(struct lsa_hdr)
762		    - sizeof(u_int32_t)) / sizeof(struct lsa_net_link);
763		off = sizeof(lsa->hdr) + sizeof(u_int32_t);
764
765		for (i = 0; i < nlinks; i++) {
766			addr.s_addr = lsa->data.net.att_rtr[i];
767			printf("    Attached Router: %s\n", inet_ntoa(addr));
768		}
769
770		printf("\n");
771		lasttype = lsa->hdr.type;
772		break;
773	case IMSG_CTL_SHOW_DB_RTR:
774		lsa = imsg->data;
775		if (lsa->hdr.type != lasttype)
776			show_database_head(area_id, lsa->hdr.type);
777		show_db_hdr_msg_detail(&lsa->hdr);
778		printf("Flags: %s\n", print_ospf_flags(lsa->data.rtr.flags));
779		nlinks = ntohs(lsa->data.rtr.nlinks);
780		printf("Number of Links: %d\n\n", nlinks);
781
782		off = sizeof(lsa->hdr) + sizeof(struct lsa_rtr);
783
784		for (i = 0; i < nlinks; i++) {
785			rtr_link = (struct lsa_rtr_link *)((char *)lsa + off);
786
787			printf("    Link connected to: %s\n",
788			    print_rtr_link_type(rtr_link->type));
789
790			addr.s_addr = rtr_link->id;
791			data.s_addr = rtr_link->data;
792
793			switch (rtr_link->type) {
794			case LINK_TYPE_POINTTOPOINT:
795			case LINK_TYPE_VIRTUAL:
796				printf("    Link ID (Neighbors Router ID):"
797				    " %s\n", inet_ntoa(addr));
798				printf("    Link Data (Router Interface "
799				    "address): %s\n", inet_ntoa(data));
800				break;
801			case LINK_TYPE_TRANSIT_NET:
802				printf("    Link ID (Designated Router "
803				    "address): %s\n", inet_ntoa(addr));
804				printf("    Link Data (Router Interface "
805				    "address): %s\n", inet_ntoa(data));
806				break;
807			case LINK_TYPE_STUB_NET:
808				printf("    Link ID (Network ID): %s\n",
809				    inet_ntoa(addr));
810				printf("    Link Data (Network Mask): %s\n",
811				    inet_ntoa(data));
812				break;
813			default:
814				printf("    Link ID (Unknown): %s\n",
815				    inet_ntoa(addr));
816				printf("    Link Data (Unknown): %s\n",
817				    inet_ntoa(data));
818				break;
819			}
820
821			printf("    Metric: %d\n\n", ntohs(rtr_link->metric));
822
823			off += sizeof(struct lsa_rtr_link) +
824			    rtr_link->num_tos * sizeof(u_int32_t);
825		}
826
827		lasttype = lsa->hdr.type;
828		break;
829	case IMSG_CTL_SHOW_DB_SUM:
830	case IMSG_CTL_SHOW_DB_ASBR:
831		lsa = imsg->data;
832		if (lsa->hdr.type != lasttype)
833			show_database_head(area_id, lsa->hdr.type);
834		show_db_hdr_msg_detail(&lsa->hdr);
835		addr.s_addr = lsa->data.sum.mask;
836		printf("Network Mask: %s\n", inet_ntoa(addr));
837		printf("Metric: %d\n\n", ntohl(lsa->data.sum.metric) &
838		    LSA_METRIC_MASK);
839		lasttype = lsa->hdr.type;
840		break;
841	case IMSG_CTL_AREA:
842		area = imsg->data;
843		area_id = area->id;
844		lasttype = 0;
845		break;
846	case IMSG_CTL_END:
847		return (1);
848	default:
849		break;
850	}
851
852	return (0);
853}
854
855int
856show_nbr_msg(struct imsg *imsg)
857{
858	struct ctl_nbr	*nbr;
859	char		*state;
860
861	switch (imsg->hdr.type) {
862	case IMSG_CTL_SHOW_NBR:
863		nbr = imsg->data;
864		if (asprintf(&state, "%s/%s", nbr_state_name(nbr->nbr_state),
865		    if_state_name(nbr->iface_state)) == -1)
866			err(1, NULL);
867		printf("%-15s %-3d %-12s %-9s", inet_ntoa(nbr->id),
868		    nbr->priority, state, fmt_timeframe_core(nbr->dead_timer));
869		printf("%-15s %-9s %s\n", inet_ntoa(nbr->addr), nbr->name,
870		    nbr->uptime == 0 ? "-" : fmt_timeframe_core(nbr->uptime));
871		free(state);
872		break;
873	case IMSG_CTL_END:
874		printf("\n");
875		return (1);
876	default:
877		break;
878	}
879
880	return (0);
881}
882
883const char *
884print_ospf_options(u_int8_t opts)
885{
886	static char	optbuf[32];
887
888	snprintf(optbuf, sizeof(optbuf), "*|*|%s|%s|%s|%s|%s|*",
889	    opts & OSPF_OPTION_DC ? "DC" : "-",
890	    opts & OSPF_OPTION_EA ? "EA" : "-",
891	    opts & OSPF_OPTION_NP ? "N/P" : "-",
892	    opts & OSPF_OPTION_MC ? "MC" : "-",
893	    opts & OSPF_OPTION_E ? "E" : "-");
894	return (optbuf);
895}
896
897int
898show_nbr_detail_msg(struct imsg *imsg)
899{
900	struct ctl_nbr	*nbr;
901
902	switch (imsg->hdr.type) {
903	case IMSG_CTL_SHOW_NBR:
904		nbr = imsg->data;
905		printf("\nNeighbor %s, ", inet_ntoa(nbr->id));
906		printf("interface address %s\n", inet_ntoa(nbr->addr));
907		printf("  Area %s, interface %s\n", inet_ntoa(nbr->area),
908		    nbr->name);
909		printf("  Neighbor priority is %d, "
910		    "State is %s, %d state changes\n",
911		    nbr->priority, nbr_state_name(nbr->nbr_state),
912		    nbr->state_chng_cnt);
913		printf("  DR is %s, ", inet_ntoa(nbr->dr));
914		printf("BDR is %s\n", inet_ntoa(nbr->bdr));
915		printf("  Options %s\n", print_ospf_options(nbr->options));
916		printf("  Dead timer due in %s\n",
917		    fmt_timeframe_core(nbr->dead_timer));
918		printf("  Uptime %s\n", fmt_timeframe_core(nbr->uptime));
919		printf("  Database Summary List %d\n", nbr->db_sum_lst_cnt);
920		printf("  Link State Request List %d\n", nbr->ls_req_lst_cnt);
921		printf("  Link State Retransmission List %d\n",
922		    nbr->ls_retrans_lst_cnt);
923		break;
924	case IMSG_CTL_END:
925		printf("\n");
926		return (1);
927	default:
928		break;
929	}
930
931	return (0);
932}
933
934int
935show_rib_msg(struct imsg *imsg)
936{
937	struct ctl_rt	*rt;
938	char		*dstnet;
939
940	switch (imsg->hdr.type) {
941	case IMSG_CTL_SHOW_RIB:
942		rt = imsg->data;
943		switch (rt->d_type) {
944		case DT_NET:
945			if (asprintf(&dstnet, "%s/%d", inet_ntoa(rt->prefix),
946			    rt->prefixlen) == -1)
947				err(1, NULL);
948			break;
949		case DT_RTR:
950			if (asprintf(&dstnet, "%s",
951			    inet_ntoa(rt->prefix)) == -1)
952				err(1, NULL);
953			break;
954		default:
955			errx(1, "Invalid route type");
956		}
957
958		printf("%-20s %-17s %-12s %-9s %-7d %s\n", dstnet,
959		    inet_ntoa(rt->nexthop), path_type_name(rt->p_type),
960		    dst_type_name(rt->d_type), rt->cost,
961		    rt->uptime == 0 ? "-" : fmt_timeframe_core(rt->uptime));
962		free(dstnet);
963		break;
964	case IMSG_CTL_END:
965		printf("\n");
966		return (1);
967	default:
968		break;
969	}
970
971	return (0);
972}
973
974void
975show_rib_head(struct in_addr aid, u_int8_t d_type, u_int8_t p_type)
976{
977	char	*header, *format, *format2;
978
979	switch (p_type) {
980	case PT_INTRA_AREA:
981	case PT_INTER_AREA:
982		switch (d_type) {
983		case DT_NET:
984			format = "Network Routing Table";
985			format2 = "";
986			break;
987		case DT_RTR:
988			format = "Router Routing Table";
989			format2 = "Type";
990			break;
991		default:
992			errx(1, "unknown route type");
993		}
994		break;
995	case PT_TYPE1_EXT:
996	case PT_TYPE2_EXT:
997		format = NULL;
998		format2 = "Cost 2";
999		if ((header = strdup("External Routing Table")) == NULL)
1000			err(1, NULL);
1001		break;
1002	default:
1003		errx(1, "unknown route type");
1004	}
1005
1006	if (p_type != PT_TYPE1_EXT && p_type != PT_TYPE2_EXT)
1007		if (asprintf(&header, "%s (Area %s)", format,
1008		    inet_ntoa(aid)) == -1)
1009			err(1, NULL);
1010
1011	printf("\n%-18s %s\n", "", header);
1012	free(header);
1013
1014	printf("\n%-18s %-15s %-15s %-12s %-7s %-7s\n", "Destination",
1015	    "Nexthop", "Adv Router", "Path type", "Cost", format2);
1016}
1017
1018const char *
1019print_ospf_rtr_flags(u_int8_t opts)
1020{
1021	static char	optbuf[32];
1022
1023	snprintf(optbuf, sizeof(optbuf), "%s%s%s",
1024	    opts & OSPF_RTR_E ? "AS" : "",
1025	    opts & OSPF_RTR_E && opts & OSPF_RTR_B ? "+" : "",
1026	    opts & OSPF_RTR_B ? "ABR" : "");
1027	return (optbuf);
1028}
1029
1030int
1031show_rib_detail_msg(struct imsg *imsg)
1032{
1033	static struct in_addr	 area_id;
1034	struct ctl_rt		*rt;
1035	struct area		*area;
1036	char			*dstnet;
1037	static u_int8_t		 lasttype;
1038
1039	switch (imsg->hdr.type) {
1040	case IMSG_CTL_SHOW_RIB:
1041		rt = imsg->data;
1042
1043		switch (rt->p_type) {
1044		case PT_INTRA_AREA:
1045		case PT_INTER_AREA:
1046			switch (rt->d_type) {
1047			case DT_NET:
1048				if (lasttype != RIB_NET)
1049					show_rib_head(rt->area, rt->d_type,
1050					     rt->p_type);
1051				if (asprintf(&dstnet, "%s/%d",
1052				    inet_ntoa(rt->prefix), rt->prefixlen) == -1)
1053					err(1, NULL);
1054				lasttype = RIB_NET;
1055				break;
1056			case DT_RTR:
1057				if (lasttype != RIB_RTR)
1058					show_rib_head(rt->area, rt->d_type,
1059					     rt->p_type);
1060				if (asprintf(&dstnet, "%s",
1061				    inet_ntoa(rt->prefix)) == -1)
1062					err(1, NULL);
1063				lasttype = RIB_RTR;
1064				break;
1065			default:
1066				errx(1, "unknown route type");
1067			}
1068			printf("%-18s %-15s ", dstnet, inet_ntoa(rt->nexthop));
1069			printf("%-15s %-12s %-7d", inet_ntoa(rt->adv_rtr),
1070			    path_type_name(rt->p_type), rt->cost);
1071			free(dstnet);
1072
1073			if (rt->d_type == DT_RTR)
1074				printf(" %-7s",
1075				    print_ospf_rtr_flags(rt->flags));
1076
1077			printf("\n");
1078			break;
1079		case PT_TYPE1_EXT:
1080		case PT_TYPE2_EXT:
1081			if (lasttype != RIB_EXT)
1082				show_rib_head(rt->area, rt->d_type, rt->p_type);
1083
1084			if (asprintf(&dstnet, "%s/%d",
1085			    inet_ntoa(rt->prefix), rt->prefixlen) == -1)
1086				err(1, NULL);
1087
1088			printf("%-18s %-15s ", dstnet, inet_ntoa(rt->nexthop));
1089			printf("%-15s %-12s %-7d %-7d\n",
1090			    inet_ntoa(rt->adv_rtr), path_type_name(rt->p_type),
1091			    rt->cost, rt->cost2);
1092			free(dstnet);
1093
1094			lasttype = RIB_EXT;
1095			break;
1096		default:
1097			errx(1, "unknown route type");
1098		}
1099		break;
1100	case IMSG_CTL_AREA:
1101		area = imsg->data;
1102		area_id = area->id;
1103		break;
1104	case IMSG_CTL_END:
1105		printf("\n");
1106		return (1);
1107	default:
1108		break;
1109	}
1110
1111	return (0);
1112}
1113
1114void
1115show_fib_head(void)
1116{
1117	printf("flags: * = valid, O = OSPF, C = Connected, S = Static\n");
1118	printf("%-6s %-4s %-20s %-17s\n", "Flags", "Prio", "Destination", "Nexthop");
1119}
1120
1121int
1122show_fib_msg(struct imsg *imsg)
1123{
1124	struct kroute		*k;
1125	char			*p;
1126
1127	switch (imsg->hdr.type) {
1128	case IMSG_CTL_KROUTE:
1129		if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(struct kroute))
1130			errx(1, "wrong imsg len");
1131		k = imsg->data;
1132
1133		if (k->flags & F_DOWN)
1134			printf(" ");
1135		else
1136			printf("*");
1137
1138		if (!(k->flags & F_KERNEL))
1139			printf("O");
1140		else if (k->flags & F_CONNECTED)
1141			printf("C");
1142		else if (k->flags & F_STATIC)
1143			printf("S");
1144		else
1145			printf(" ");
1146
1147		printf("     ");
1148		printf("%4d ", k->priority);
1149		if (asprintf(&p, "%s/%u", inet_ntoa(k->prefix), k->prefixlen) ==
1150		    -1)
1151			err(1, NULL);
1152		printf("%-20s ", p);
1153		free(p);
1154
1155		if (k->nexthop.s_addr)
1156			printf("%s", inet_ntoa(k->nexthop));
1157		else if (k->flags & F_CONNECTED)
1158			printf("link#%u", k->ifindex);
1159		printf("\n");
1160
1161		break;
1162	case IMSG_CTL_END:
1163		printf("\n");
1164		return (1);
1165	default:
1166		break;
1167	}
1168
1169	return (0);
1170}
1171
1172void
1173show_interface_head(void)
1174{
1175	printf("%-15s%-15s%s\n", "Interface", "Flags",
1176	    "Link state");
1177}
1178
1179const struct if_status_description
1180		if_status_descriptions[] = LINK_STATE_DESCRIPTIONS;
1181const struct ifmedia_description
1182		ifm_type_descriptions[] = IFM_TYPE_DESCRIPTIONS;
1183
1184const char *
1185get_media_descr(int media_type)
1186{
1187	const struct ifmedia_description	*p;
1188
1189	for (p = ifm_type_descriptions; p->ifmt_string != NULL; p++)
1190		if (media_type == p->ifmt_word)
1191			return (p->ifmt_string);
1192
1193	return ("unknown");
1194}
1195
1196const char *
1197get_linkstate(int media_type, int link_state)
1198{
1199	const struct if_status_description *p;
1200	static char buf[8];
1201
1202	for (p = if_status_descriptions; p->ifs_string != NULL; p++) {
1203		if (LINK_STATE_DESC_MATCH(p, media_type, link_state))
1204			return (p->ifs_string);
1205	}
1206	snprintf(buf, sizeof(buf), "[#%d]", link_state);
1207	return (buf);
1208}
1209
1210void
1211print_baudrate(u_int64_t baudrate)
1212{
1213	if (baudrate > IF_Gbps(1))
1214		printf("%llu GBit/s", baudrate / IF_Gbps(1));
1215	else if (baudrate > IF_Mbps(1))
1216		printf("%llu MBit/s", baudrate / IF_Mbps(1));
1217	else if (baudrate > IF_Kbps(1))
1218		printf("%llu KBit/s", baudrate / IF_Kbps(1));
1219	else
1220		printf("%llu Bit/s", baudrate);
1221}
1222
1223int
1224show_fib_interface_msg(struct imsg *imsg)
1225{
1226	struct kif	*k;
1227	int		 ifms_type;
1228
1229	switch (imsg->hdr.type) {
1230	case IMSG_CTL_IFINFO:
1231		k = imsg->data;
1232		printf("%-15s", k->ifname);
1233		printf("%-15s", k->flags & IFF_UP ? "UP" : "");
1234		ifms_type = get_ifms_type(k->media_type);
1235		if (ifms_type)
1236			printf("%s, ", get_media_descr(ifms_type));
1237
1238		printf("%s", get_linkstate(k->media_type, k->link_state));
1239
1240		if (k->link_state != LINK_STATE_DOWN && k->baudrate > 0) {
1241			printf(", ");
1242			print_baudrate(k->baudrate);
1243		}
1244		printf("\n");
1245		break;
1246	case IMSG_CTL_END:
1247		printf("\n");
1248		return (1);
1249	default:
1250		break;
1251	}
1252
1253	return (0);
1254}
1255