ospfctl.c revision 1.31
1/*	$OpenBSD: ospfctl.c,v 1.31 2006/03/26 09:45:55 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("  Database Summary List %d\n", nbr->db_sum_lst_cnt);
910		printf("  Link State Request List %d\n", nbr->ls_req_lst_cnt);
911		printf("  Link State Retransmission List %d\n",
912		    nbr->ls_retrans_lst_cnt);
913		break;
914	case IMSG_CTL_END:
915		printf("\n");
916		return (1);
917	default:
918		break;
919	}
920
921	return (0);
922}
923
924int
925show_rib_msg(struct imsg *imsg)
926{
927	struct ctl_rt	*rt;
928	char		*dstnet;
929
930	switch (imsg->hdr.type) {
931	case IMSG_CTL_SHOW_RIB:
932		rt = imsg->data;
933		switch (rt->d_type) {
934		case DT_NET:
935			if (asprintf(&dstnet, "%s/%d", inet_ntoa(rt->prefix),
936			    rt->prefixlen) == -1)
937				err(1, NULL);
938			break;
939		case DT_RTR:
940			if (asprintf(&dstnet, "%s",
941			    inet_ntoa(rt->prefix)) == -1)
942				err(1, NULL);
943			break;
944		default:
945			errx(1, "Invalid route type");
946		}
947
948		printf("%-20s %-17s %-12s %-9s %-7d %s\n", dstnet,
949		    inet_ntoa(rt->nexthop), path_type_name(rt->p_type),
950		    dst_type_name(rt->d_type), rt->cost,
951		    rt->uptime == 0 ? "-" : fmt_timeframe_core(rt->uptime));
952		free(dstnet);
953		break;
954	case IMSG_CTL_END:
955		printf("\n");
956		return (1);
957	default:
958		break;
959	}
960
961	return (0);
962}
963
964void
965show_rib_head(struct in_addr aid, u_int8_t d_type, u_int8_t p_type)
966{
967	char	*header, *format, *format2;
968
969	switch (p_type) {
970	case PT_INTRA_AREA:
971	case PT_INTER_AREA:
972		switch (d_type) {
973		case DT_NET:
974			format = "Network Routing Table";
975			format2 = "";
976			break;
977		case DT_RTR:
978			format = "Router Routing Table";
979			format2 = "Type";
980			break;
981		default:
982			errx(1, "unknown route type");
983		}
984		break;
985	case PT_TYPE1_EXT:
986	case PT_TYPE2_EXT:
987		format = NULL;
988		format2 = "Cost 2";
989		if ((header = strdup("External Routing Table")) == NULL)
990			err(1, NULL);
991		break;
992	default:
993		errx(1, "unknown route type");
994	}
995
996	if (p_type != PT_TYPE1_EXT && p_type != PT_TYPE2_EXT)
997		if (asprintf(&header, "%s (Area %s)", format,
998		    inet_ntoa(aid)) == -1)
999			err(1, NULL);
1000
1001	printf("\n%-18s %s\n", "", header);
1002	free(header);
1003
1004	printf("\n%-18s %-15s %-15s %-12s %-7s %-7s\n", "Destination",
1005	    "Nexthop", "Adv Router", "Path type", "Cost", format2);
1006}
1007
1008const char *
1009print_ospf_rtr_flags(u_int8_t opts)
1010{
1011	static char	optbuf[32];
1012
1013	snprintf(optbuf, sizeof(optbuf), "%s%s%s",
1014	    opts & OSPF_RTR_E ? "AS" : "",
1015	    opts & OSPF_RTR_E && opts & OSPF_RTR_B ? "+" : "",
1016	    opts & OSPF_RTR_B ? "ABR" : "");
1017	return (optbuf);
1018}
1019
1020int
1021show_rib_detail_msg(struct imsg *imsg)
1022{
1023	static struct in_addr	 area_id;
1024	struct ctl_rt		*rt;
1025	struct area		*area;
1026	char			*dstnet;
1027	static u_int8_t		 lasttype;
1028
1029	switch (imsg->hdr.type) {
1030	case IMSG_CTL_SHOW_RIB:
1031		rt = imsg->data;
1032
1033		switch (rt->p_type) {
1034		case PT_INTRA_AREA:
1035		case PT_INTER_AREA:
1036			switch (rt->d_type) {
1037			case DT_NET:
1038				if (lasttype != RIB_NET)
1039					show_rib_head(rt->area, rt->d_type,
1040					     rt->p_type);
1041				if (asprintf(&dstnet, "%s/%d",
1042				    inet_ntoa(rt->prefix), rt->prefixlen) == -1)
1043					err(1, NULL);
1044				lasttype = RIB_NET;
1045				break;
1046			case DT_RTR:
1047				if (lasttype != RIB_RTR)
1048					show_rib_head(rt->area, rt->d_type,
1049					     rt->p_type);
1050				if (asprintf(&dstnet, "%s",
1051				    inet_ntoa(rt->prefix)) == -1)
1052					err(1, NULL);
1053				lasttype = RIB_RTR;
1054				break;
1055			default:
1056				errx(1, "unknown route type");
1057			}
1058			printf("%-18s %-15s ", dstnet, inet_ntoa(rt->nexthop));
1059			printf("%-15s %-12s %-7d", inet_ntoa(rt->adv_rtr),
1060			    path_type_name(rt->p_type), rt->cost);
1061			free(dstnet);
1062
1063			if (rt->d_type == DT_RTR)
1064				printf(" %-7s",
1065				    print_ospf_rtr_flags(rt->flags));
1066
1067			printf("\n");
1068			break;
1069		case PT_TYPE1_EXT:
1070		case PT_TYPE2_EXT:
1071			if (lasttype != RIB_EXT)
1072				show_rib_head(rt->area, rt->d_type, rt->p_type);
1073
1074			if (asprintf(&dstnet, "%s/%d",
1075			    inet_ntoa(rt->prefix), rt->prefixlen) == -1)
1076				err(1, NULL);
1077
1078			printf("%-18s %-15s ", dstnet, inet_ntoa(rt->nexthop));
1079			printf("%-15s %-12s %-7d %-7d\n",
1080			    inet_ntoa(rt->adv_rtr), path_type_name(rt->p_type),
1081			    rt->cost, rt->cost2);
1082
1083			lasttype = RIB_EXT;
1084			break;
1085		default:
1086			errx(1, "unknown route type");
1087		}
1088		break;
1089	case IMSG_CTL_AREA:
1090		area = imsg->data;
1091		area_id = area->id;
1092		break;
1093	case IMSG_CTL_END:
1094		printf("\n");
1095		return (1);
1096	default:
1097		break;
1098	}
1099
1100	return (0);
1101}
1102
1103void
1104show_fib_head(void)
1105{
1106	printf("flags: * = valid, O = OSPF, C = Connected, S = Static\n");
1107	printf("%-6s %-20s %-17s\n", "Flags", "Destination", "Nexthop");
1108}
1109
1110int
1111show_fib_msg(struct imsg *imsg)
1112{
1113	struct kroute		*k;
1114	char			*p;
1115
1116	switch (imsg->hdr.type) {
1117	case IMSG_CTL_KROUTE:
1118		if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(struct kroute))
1119			errx(1, "wrong imsg len");
1120		k = imsg->data;
1121
1122		if (k->flags & F_DOWN)
1123			printf(" ");
1124		else
1125			printf("*");
1126
1127		if (!(k->flags & F_KERNEL))
1128			printf("O");
1129		else if (k->flags & F_CONNECTED)
1130			printf("C");
1131		else if (k->flags & F_STATIC)
1132			printf("S");
1133		else
1134			printf(" ");
1135
1136		printf("     ");
1137		if (asprintf(&p, "%s/%u", inet_ntoa(k->prefix), k->prefixlen) ==
1138		    -1)
1139			err(1, NULL);
1140		printf("%-20s ", p);
1141		free(p);
1142
1143		if (k->nexthop.s_addr)
1144			printf("%s", inet_ntoa(k->nexthop));
1145		else if (k->flags & F_CONNECTED)
1146			printf("link#%u", k->ifindex);
1147		printf("\n");
1148
1149		break;
1150	case IMSG_CTL_END:
1151		printf("\n");
1152		return (1);
1153	default:
1154		break;
1155	}
1156
1157	return (0);
1158}
1159
1160void
1161show_interface_head(void)
1162{
1163	printf("%-15s%-15s%s\n", "Interface", "Flags",
1164	    "Link state");
1165}
1166
1167const int	ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1168const struct ifmedia_status_description
1169		ifm_status_descriptions[] = IFM_STATUS_DESCRIPTIONS;
1170const struct ifmedia_description
1171		ifm_type_descriptions[] = IFM_TYPE_DESCRIPTIONS;
1172
1173const char *
1174get_media_descr(int media_type)
1175{
1176	const struct ifmedia_description	*p;
1177
1178	for (p = ifm_type_descriptions; p->ifmt_string != NULL; p++)
1179		if (media_type == p->ifmt_word)
1180			return (p->ifmt_string);
1181
1182	return ("unknown media");
1183}
1184
1185const char *
1186get_linkstate(int media_type, int link_state)
1187{
1188	const struct ifmedia_status_description	*p;
1189	int					 i;
1190
1191	if (link_state == LINK_STATE_UNKNOWN)
1192		return ("unknown");
1193
1194	for (i = 0; ifm_status_valid_list[i] != 0; i++)
1195		for (p = ifm_status_descriptions; p->ifms_valid != 0; p++) {
1196			if (p->ifms_type != media_type ||
1197			    p->ifms_valid != ifm_status_valid_list[i])
1198				continue;
1199			return (p->ifms_string[link_state == LINK_STATE_UP]);
1200		}
1201
1202	return ("unknown link state");
1203}
1204
1205void
1206print_baudrate(u_long baudrate)
1207{
1208	if (baudrate > IF_Gbps(1))
1209		printf("%lu GBit/s", baudrate / IF_Gbps(1));
1210	else if (baudrate > IF_Mbps(1))
1211		printf("%lu MBit/s", baudrate / IF_Mbps(1));
1212	else if (baudrate > IF_Kbps(1))
1213		printf("%lu KBit/s", baudrate / IF_Kbps(1));
1214	else
1215		printf("%lu Bit/s", baudrate);
1216}
1217
1218int
1219show_fib_interface_msg(struct imsg *imsg)
1220{
1221	struct kif	*k;
1222	int		 ifms_type;
1223
1224	switch (imsg->hdr.type) {
1225	case IMSG_CTL_IFINFO:
1226		k = imsg->data;
1227		printf("%-15s", k->ifname);
1228		printf("%-15s", k->flags & IFF_UP ? "UP" : "");
1229		switch (k->media_type) {
1230		case IFT_ETHER:
1231			ifms_type = IFM_ETHER;
1232			break;
1233		case IFT_FDDI:
1234			ifms_type = IFM_FDDI;
1235			break;
1236		case IFT_ISO88025:
1237			ifms_type = IFM_TOKEN;
1238			break;
1239		case IFT_CARP:
1240			ifms_type = IFM_CARP;
1241			break;
1242		default:
1243			ifms_type = 0;
1244			break;
1245		}
1246
1247		if (ifms_type)
1248			printf("%s, %s", get_media_descr(ifms_type),
1249			    get_linkstate(ifms_type, k->link_state));
1250		else if (k->link_state == LINK_STATE_UNKNOWN)
1251			printf("unknown");
1252		else
1253			printf("link state %u", k->link_state);
1254
1255		if (k->link_state != LINK_STATE_DOWN && k->baudrate > 0) {
1256			printf(", ");
1257			print_baudrate(k->baudrate);
1258		}
1259		printf("\n");
1260		break;
1261	case IMSG_CTL_END:
1262		printf("\n");
1263		return (1);
1264	default:
1265		break;
1266	}
1267
1268	return (0);
1269}
1270