xref: /openbsd-src/usr.sbin/ldpctl/ldpctl.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*	$OpenBSD: ldpctl.c,v 1.12 2010/09/01 13:59:17 claudio Exp $
2  *
3  * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
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 #include <netmpls/mpls.h>
29 
30 #include <err.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "ldp.h"
37 #include "ldpd.h"
38 #include "ldpe.h"
39 #include "parser.h"
40 
41 __dead void	 usage(void);
42 const char	*fmt_timeframe_core(time_t);
43 const char	*get_linkstate(int, int);
44 int		 show_interface_msg(struct imsg *);
45 int		 get_ifms_type(int);
46 int		 show_lib_msg(struct imsg *);
47 int		 show_nbr_msg(struct imsg *);
48 void		 show_fib_head(void);
49 int		 show_fib_msg(struct imsg *);
50 void		 show_interface_head(void);
51 int		 show_fib_interface_msg(struct imsg *);
52 const char	*get_media_descr(int);
53 void		 print_baudrate(u_int64_t);
54 
55 struct imsgbuf	*ibuf;
56 
57 __dead void
58 usage(void)
59 {
60 	extern char *__progname;
61 
62 	fprintf(stderr, "usage: %s command [argument ...]\n", __progname);
63 	exit(1);
64 }
65 
66 int
67 main(int argc, char *argv[])
68 {
69 	struct sockaddr_un	 sun;
70 	struct parse_result	*res;
71 	struct imsg		 imsg;
72 	unsigned int		 ifidx = 0;
73 	int			 ctl_sock;
74 	int			 done = 0, verbose = 0;
75 	int			 n;
76 
77 	/* parse options */
78 	if ((res = parse(argc - 1, argv + 1)) == NULL)
79 		exit(1);
80 
81 	/* connect to ldpd control socket */
82 	if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
83 		err(1, "socket");
84 
85 	bzero(&sun, sizeof(sun));
86 	sun.sun_family = AF_UNIX;
87 	strlcpy(sun.sun_path, LDPD_SOCKET, sizeof(sun.sun_path));
88 	if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
89 		err(1, "connect: %s", LDPD_SOCKET);
90 
91 	if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
92 		err(1, NULL);
93 	imsg_init(ibuf, ctl_sock);
94 	done = 0;
95 
96 	/* process user request */
97 	switch (res->action) {
98 	case NONE:
99 		usage();
100 		/* not reached */
101 	case SHOW:
102 	case SHOW_IFACE:
103 		printf("%-11s %-18s %-10s %-10s %-8s\n",
104 		    "Interface", "Address", "State", "Linkstate",
105 		    "Uptime");
106 		if (*res->ifname) {
107 			ifidx = if_nametoindex(res->ifname);
108 			if (ifidx == 0)
109 				errx(1, "no such interface %s", res->ifname);
110 		}
111 		imsg_compose(ibuf, IMSG_CTL_SHOW_INTERFACE, 0, 0, -1,
112 		    &ifidx, sizeof(ifidx));
113 		break;
114 	case SHOW_NBR:
115 		printf("%-15s %-18s %-15s %-9s %-10s\n", "ID",
116 		    "State", "Address", "Iface", "Uptime");
117 		imsg_compose(ibuf, IMSG_CTL_SHOW_NBR, 0, 0, -1, NULL, 0);
118 		break;
119 	case SHOW_LIB:
120 		printf("%-20s %-17s %-14s %-14s %-10s\n", "Destination",
121 		    "Nexthop", "Local Label", "Remote Label", "In Use");
122 		imsg_compose(ibuf, IMSG_CTL_SHOW_LIB, 0, 0, -1, NULL, 0);
123 		break;
124 	case SHOW_FIB:
125 		if (!res->addr.s_addr)
126 			imsg_compose(ibuf, IMSG_CTL_KROUTE, 0, 0, -1,
127 			    &res->flags, sizeof(res->flags));
128 		else
129 			imsg_compose(ibuf, IMSG_CTL_KROUTE_ADDR, 0, 0, -1,
130 			    &res->addr, sizeof(res->addr));
131 		show_fib_head();
132 		break;
133 	case SHOW_FIB_IFACE:
134 		if (*res->ifname)
135 			imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, -1,
136 			    res->ifname, sizeof(res->ifname));
137 		else
138 			imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, -1, NULL, 0);
139 		show_interface_head();
140 		break;
141 	case FIB:
142 		errx(1, "fib couple|decouple");
143 		break;
144 	case FIB_COUPLE:
145 		imsg_compose(ibuf, IMSG_CTL_FIB_COUPLE, 0, 0, -1, NULL, 0);
146 		printf("couple request sent.\n");
147 		done = 1;
148 		break;
149 	case FIB_DECOUPLE:
150 		imsg_compose(ibuf, IMSG_CTL_FIB_DECOUPLE, 0, 0, -1, NULL, 0);
151 		printf("decouple request sent.\n");
152 		done = 1;
153 		break;
154 	case LOG_VERBOSE:
155 		verbose = 1;
156 		/* FALLTHROUGH */
157 	case LOG_BRIEF:
158 		imsg_compose(ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1,
159 		    &verbose, sizeof(verbose));
160 		printf("logging request sent.\n");
161 		done = 1;
162 		break;
163 	case RELOAD:
164 		imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0);
165 		printf("reload request sent.\n");
166 		done = 1;
167 		break;
168 	}
169 
170 	while (ibuf->w.queued)
171 		if (msgbuf_write(&ibuf->w) < 0)
172 			err(1, "write error");
173 
174 	while (!done) {
175 		if ((n = imsg_read(ibuf)) == -1)
176 			errx(1, "imsg_read error");
177 		if (n == 0)
178 			errx(1, "pipe closed");
179 
180 		while (!done) {
181 			if ((n = imsg_get(ibuf, &imsg)) == -1)
182 				errx(1, "imsg_get error");
183 			if (n == 0)
184 				break;
185 			switch (res->action) {
186 			case SHOW:
187 			case SHOW_IFACE:
188 				done = show_interface_msg(&imsg);
189 				break;
190 			case SHOW_NBR:
191 				done = show_nbr_msg(&imsg);
192 				break;
193 			case SHOW_LIB:
194 				done = show_lib_msg(&imsg);
195 				break;
196 			case SHOW_FIB:
197 				done = show_fib_msg(&imsg);
198 				break;
199 			case SHOW_FIB_IFACE:
200 				done = show_fib_interface_msg(&imsg);
201 				break;
202 			case NONE:
203 			case FIB:
204 			case FIB_COUPLE:
205 			case FIB_DECOUPLE:
206 			case LOG_VERBOSE:
207 			case LOG_BRIEF:
208 			case RELOAD:
209 				break;
210 			}
211 			imsg_free(&imsg);
212 		}
213 	}
214 	close(ctl_sock);
215 	free(ibuf);
216 
217 	return (0);
218 }
219 
220 int
221 get_ifms_type(int mediatype)
222 {
223 	switch (mediatype) {
224 	case IFT_ETHER:
225 		return (IFM_ETHER);
226 		break;
227 	case IFT_FDDI:
228 		return (IFM_FDDI);
229 		break;
230 	case IFT_CARP:
231 		return (IFM_CARP);
232 		break;
233 	default:
234 		return (0);
235 		break;
236 	}
237 }
238 
239 #define	TF_BUFS	8
240 #define	TF_LEN	9
241 
242 const char *
243 fmt_timeframe_core(time_t t)
244 {
245 	char		*buf;
246 	static char	 tfbuf[TF_BUFS][TF_LEN];	/* ring buffer */
247 	static int	 idx = 0;
248 	unsigned int	 sec, min, hrs, day, week;
249 
250 	if (t == 0)
251 		return ("Stopped");
252 
253 	buf = tfbuf[idx++];
254 	if (idx == TF_BUFS)
255 		idx = 0;
256 
257 	week = t;
258 
259 	sec = week % 60;
260 	week /= 60;
261 	min = week % 60;
262 	week /= 60;
263 	hrs = week % 24;
264 	week /= 24;
265 	day = week % 7;
266 	week /= 7;
267 
268 	if (week > 0)
269 		snprintf(buf, TF_LEN, "%02uw%01ud%02uh", week, day, hrs);
270 	else if (day > 0)
271 		snprintf(buf, TF_LEN, "%01ud%02uh%02um", day, hrs, min);
272 	else
273 		snprintf(buf, TF_LEN, "%02u:%02u:%02u", hrs, min, sec);
274 
275 	return (buf);
276 }
277 
278 /* prototype defined in ldpd.h and shared with the kroute.c version */
279 u_int8_t
280 mask2prefixlen(in_addr_t ina)
281 {
282 	if (ina == 0)
283 		return (0);
284 	else
285 		return (33 - ffs(ntohl(ina)));
286 }
287 
288 int
289 show_interface_msg(struct imsg *imsg)
290 {
291 	struct ctl_iface	*iface;
292 	char			*netid;
293 
294 	switch (imsg->hdr.type) {
295 	case IMSG_CTL_SHOW_INTERFACE:
296 		iface = imsg->data;
297 
298 		if (asprintf(&netid, "%s/%d", inet_ntoa(iface->addr),
299 		    mask2prefixlen(iface->mask.s_addr)) == -1)
300 			err(1, NULL);
301 		printf("%-11s %-18s %-10s %-10s %-8s\n",
302 		    iface->name, netid, if_state_name(iface->state),
303 		    get_linkstate(iface->mediatype, iface->linkstate),
304 		    iface->uptime == 0 ? "00:00:00" :
305 		    fmt_timeframe_core(iface->uptime));
306 		free(netid);
307 		break;
308 	case IMSG_CTL_END:
309 		printf("\n");
310 		return (1);
311 	default:
312 		break;
313 	}
314 
315 	return (0);
316 }
317 
318 int
319 show_lib_msg(struct imsg *imsg)
320 {
321 	struct ctl_rt	*rt;
322 	char		*dstnet, *remote;
323 
324 	switch (imsg->hdr.type) {
325 	case IMSG_CTL_SHOW_LIB:
326 		rt = imsg->data;
327 		if (asprintf(&dstnet, "%s/%d", inet_ntoa(rt->prefix),
328 		    rt->prefixlen) == -1)
329 			err(1, NULL);
330 		if (!rt->in_use) {
331 			if (asprintf(&remote, "-") == -1)
332 				err(1, NULL);
333 		} else if (rt->connected || rt->remote_label == NO_LABEL) {
334 			if (asprintf(&remote, "Untagged") == -1)
335 				err(1, NULL);
336 		} else if (rt->remote_label == MPLS_LABEL_IMPLNULL) {
337 			if (asprintf(&remote, "Pop tag") == -1)
338 				err(1, NULL);
339 		} else {
340 			if (asprintf(&remote, "%u", rt->remote_label) == -1)
341 				err(1, NULL);
342 		}
343 
344 		printf("%-20s %-17s %-14u %-14s %s\n", dstnet,
345 		    inet_ntoa(rt->nexthop), rt->local_label, remote,
346 		    rt->in_use ? "yes" : "no");
347 		free(remote);
348 		free(dstnet);
349 
350 		break;
351 	case IMSG_CTL_END:
352 		printf("\n");
353 		return (1);
354 	default:
355 		break;
356 	}
357 
358 	return (0);
359 }
360 
361 int
362 show_nbr_msg(struct imsg *imsg)
363 {
364 	struct ctl_nbr	*nbr;
365 	char		*state;
366 
367 	switch (imsg->hdr.type) {
368 	case IMSG_CTL_SHOW_NBR:
369 		nbr = imsg->data;
370 		if (asprintf(&state, "%s/%s", nbr_state_name(nbr->nbr_state),
371 		    if_state_name(nbr->iface_state)) == -1)
372 			err(1, NULL);
373 		printf("%-15s %-19s", inet_ntoa(nbr->id),
374 		    state);
375 		printf("%-15s %-10s", inet_ntoa(nbr->addr), nbr->name);
376 		printf("%-15s\n", nbr->uptime == 0 ? "-" :
377 		    fmt_timeframe_core(nbr->uptime));
378 		free(state);
379 		break;
380 	case IMSG_CTL_END:
381 		printf("\n");
382 		return (1);
383 	default:
384 		break;
385 	}
386 
387 	return (0);
388 }
389 
390 void
391 show_fib_head(void)
392 {
393 	printf("Flags: C = Connected, S = Static\n");
394 	printf(" %-4s %-20s %-17s %-17s %s\n", "Prio", "Destination",
395 	    "Nexthop", "Local Label", "Remote Label");
396 }
397 
398 int
399 show_fib_msg(struct imsg *imsg)
400 {
401 	struct kroute		*k;
402 	char			*p;
403 
404 	switch (imsg->hdr.type) {
405 	case IMSG_CTL_KROUTE:
406 		if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(struct kroute))
407 			errx(1, "wrong imsg len");
408 		k = imsg->data;
409 
410 		if (k->flags & F_CONNECTED)
411 			printf("C");
412 		else if (k->flags & F_STATIC)
413 			printf("S");
414 		else
415 			printf(" ");
416 
417 		printf(" %3d ", k->priority);
418 		if (asprintf(&p, "%s/%u", inet_ntoa(k->prefix),
419 		    k->prefixlen) == -1)
420 			err(1, NULL);
421 		printf("%-20s ", p);
422 		free(p);
423 
424 		if (k->nexthop.s_addr)
425 			printf("%-18s", inet_ntoa(k->nexthop));
426 		else if (k->flags & F_CONNECTED)
427 			printf("link#%-13u", k->ifindex);
428 
429 		if (k->local_label == NO_LABEL) {
430 			printf("%-18s", "-");
431 		} else if (k->local_label == MPLS_LABEL_IMPLNULL) {
432 			printf("%-18s", "imp-null");
433 		} else
434 			printf("%-18u", k->local_label);
435 
436 		if (k->remote_label == NO_LABEL) {
437 			printf("-");
438 		} else if (k->remote_label == MPLS_LABEL_IMPLNULL) {
439 			printf("Pop");
440 		} else {
441 			printf("%u", k->remote_label);
442 		}
443 
444 		printf("\n");
445 
446 		break;
447 	case IMSG_CTL_END:
448 		printf("\n");
449 		return (1);
450 	default:
451 		break;
452 	}
453 
454 	return (0);
455 }
456 
457 void
458 show_interface_head(void)
459 {
460 	printf("%-15s%-15s%s\n", "Interface", "Flags",
461 	    "Link state");
462 }
463 
464 int
465 show_fib_interface_msg(struct imsg *imsg)
466 {
467 	struct kif	*k;
468 	int		 ifms_type;
469 
470 	switch (imsg->hdr.type) {
471 	case IMSG_CTL_IFINFO:
472 		k = imsg->data;
473 		printf("%-15s", k->ifname);
474 		printf("%-15s", k->flags & IFF_UP ? "UP" : "");
475 		ifms_type = get_ifms_type(k->media_type);
476 		if (ifms_type)
477 			printf("%s, ", get_media_descr(ifms_type));
478 
479 		printf("%s", get_linkstate(k->media_type, k->link_state));
480 
481 		if (k->link_state != LINK_STATE_DOWN && k->baudrate > 0) {
482 			printf(", ");
483 			print_baudrate(k->baudrate);
484 		}
485 		printf("\n");
486 		break;
487 	case IMSG_CTL_END:
488 		printf("\n");
489 		return (1);
490 	default:
491 		break;
492 	}
493 
494 	return (0);
495 }
496 
497 const struct if_status_description
498 		if_status_descriptions[] = LINK_STATE_DESCRIPTIONS;
499 const struct ifmedia_description
500 		ifm_type_descriptions[] = IFM_TYPE_DESCRIPTIONS;
501 
502 const char *
503 get_media_descr(int media_type)
504 {
505 	const struct ifmedia_description	*p;
506 
507 	for (p = ifm_type_descriptions; p->ifmt_string != NULL; p++)
508 		if (media_type == p->ifmt_word)
509 			return (p->ifmt_string);
510 
511 	return ("unknown");
512 }
513 
514 const char *
515 get_linkstate(int media_type, int link_state)
516 {
517 	const struct if_status_description *p;
518 	static char buf[8];
519 
520 	for (p = if_status_descriptions; p->ifs_string != NULL; p++) {
521 		if (LINK_STATE_DESC_MATCH(p, media_type, link_state))
522 			return (p->ifs_string);
523 	}
524 	snprintf(buf, sizeof(buf), "[#%d]", link_state);
525 	return (buf);
526 }
527 
528 void
529 print_baudrate(u_int64_t baudrate)
530 {
531 	if (baudrate > IF_Gbps(1))
532 		printf("%llu GBit/s", baudrate / IF_Gbps(1));
533 	else if (baudrate > IF_Mbps(1))
534 		printf("%llu MBit/s", baudrate / IF_Mbps(1));
535 	else if (baudrate > IF_Kbps(1))
536 		printf("%llu KBit/s", baudrate / IF_Kbps(1));
537 	else
538 		printf("%llu Bit/s", baudrate);
539 }
540