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