1 /*
2  * Logging of zebra
3  * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
4  *
5  * This file is part of GNU Zebra.
6  *
7  * GNU Zebra is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2, or (at your option) any
10  * later version.
11  *
12  * GNU Zebra is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; see the file COPYING; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #define FRR_DEFINE_DESC_TABLE
23 
24 #include <zebra.h>
25 
26 #include "zclient.h"
27 #include "log.h"
28 #include "memory.h"
29 #include "command.h"
30 #include "lib_errors.h"
31 #include "lib/hook.h"
32 #include "printfrr.h"
33 #include "frr_pthread.h"
34 
35 #ifdef HAVE_LIBUNWIND
36 #define UNW_LOCAL_ONLY
37 #include <libunwind.h>
38 #include <dlfcn.h>
39 #endif
40 
41 /**
42  * Looks up a message in a message list by key.
43  *
44  * If the message is not found, returns the provided error message.
45  *
46  * Terminates when it hits a struct message that's all zeros.
47  *
48  * @param mz the message list
49  * @param kz the message key
50  * @param nf the message to return if not found
51  * @return the message
52  */
lookup_msg(const struct message * mz,int kz,const char * nf)53 const char *lookup_msg(const struct message *mz, int kz, const char *nf)
54 {
55 	static struct message nt = {0};
56 	const char *rz = nf ? nf : "(no message found)";
57 	const struct message *pnt;
58 	for (pnt = mz; memcmp(pnt, &nt, sizeof(struct message)); pnt++)
59 		if (pnt->key == kz) {
60 			rz = pnt->str ? pnt->str : rz;
61 			break;
62 		}
63 	return rz;
64 }
65 
66 /* For time string format. */
quagga_timestamp(int timestamp_precision,char * buf,size_t buflen)67 size_t quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
68 {
69 	static struct {
70 		time_t last;
71 		size_t len;
72 		char buf[28];
73 	} cache;
74 	struct timeval clock;
75 
76 	gettimeofday(&clock, NULL);
77 
78 	/* first, we update the cache if the time has changed */
79 	if (cache.last != clock.tv_sec) {
80 		struct tm tm;
81 		cache.last = clock.tv_sec;
82 		localtime_r(&cache.last, &tm);
83 		cache.len = strftime(cache.buf, sizeof(cache.buf),
84 				     "%Y/%m/%d %H:%M:%S", &tm);
85 	}
86 	/* note: it's not worth caching the subsecond part, because
87 	   chances are that back-to-back calls are not sufficiently close
88 	   together
89 	   for the clock not to have ticked forward */
90 
91 	if (buflen > cache.len) {
92 		memcpy(buf, cache.buf, cache.len);
93 		if ((timestamp_precision > 0)
94 		    && (buflen > cache.len + 1 + timestamp_precision)) {
95 			/* should we worry about locale issues? */
96 			static const int divisor[] = {0,   100000, 10000, 1000,
97 						      100, 10,     1};
98 			int prec;
99 			char *p = buf + cache.len + 1
100 				  + (prec = timestamp_precision);
101 			*p-- = '\0';
102 			while (prec > 6)
103 			/* this is unlikely to happen, but protect anyway */
104 			{
105 				*p-- = '0';
106 				prec--;
107 			}
108 			clock.tv_usec /= divisor[prec];
109 			do {
110 				*p-- = '0' + (clock.tv_usec % 10);
111 				clock.tv_usec /= 10;
112 			} while (--prec > 0);
113 			*p = '.';
114 			return cache.len + 1 + timestamp_precision;
115 		}
116 		buf[cache.len] = '\0';
117 		return cache.len;
118 	}
119 	if (buflen > 0)
120 		buf[0] = '\0';
121 	return 0;
122 }
123 
124 /*
125  * crash handling
126  *
127  * NB: only AS-Safe (async-signal) functions can be used here!
128  */
129 
130 /* Note: the goal here is to use only async-signal-safe functions. */
zlog_signal(int signo,const char * action,void * siginfo_v,void * program_counter)131 void zlog_signal(int signo, const char *action, void *siginfo_v,
132 		 void *program_counter)
133 {
134 	siginfo_t *siginfo = siginfo_v;
135 	time_t now;
136 	char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
137 		 + 100];
138 	struct fbuf fb = { .buf = buf, .pos = buf, .len = sizeof(buf) };
139 
140 	time(&now);
141 
142 	bprintfrr(&fb, "Received signal %d at %lld", signo, (long long)now);
143 	if (program_counter)
144 		bprintfrr(&fb, " (si_addr 0x%tx, PC 0x%tx)",
145 			  (ptrdiff_t)siginfo->si_addr,
146 			  (ptrdiff_t)program_counter);
147 	else
148 		bprintfrr(&fb, " (si_addr 0x%tx)",
149 			  (ptrdiff_t)siginfo->si_addr);
150 	bprintfrr(&fb, "; %s\n", action);
151 
152 	zlog_sigsafe(fb.buf, fb.pos - fb.buf);
153 
154 	zlog_backtrace_sigsafe(LOG_CRIT, program_counter);
155 
156 	fb.pos = buf;
157 
158 	struct thread *tc;
159 	tc = pthread_getspecific(thread_current);
160 
161 	if (!tc)
162 		bprintfrr(&fb, "no thread information available\n");
163 	else
164 		bprintfrr(&fb, "in thread %s scheduled from %s:%d\n",
165 			  tc->funcname, tc->schedfrom, tc->schedfrom_line);
166 
167 	zlog_sigsafe(fb.buf, fb.pos - fb.buf);
168 }
169 
170 /* Log a backtrace using only async-signal-safe functions.
171    Needs to be enhanced to support syslog logging. */
zlog_backtrace_sigsafe(int priority,void * program_counter)172 void zlog_backtrace_sigsafe(int priority, void *program_counter)
173 {
174 #ifdef HAVE_LIBUNWIND
175 	char buf[256];
176 	struct fbuf fb = { .buf = buf, .len = sizeof(buf) };
177 	unw_cursor_t cursor;
178 	unw_context_t uc;
179 	unw_word_t ip, off, sp;
180 	Dl_info dlinfo;
181 
182 	memset(&uc, 0, sizeof(uc));
183 	memset(&cursor, 0, sizeof(cursor));
184 
185 	unw_getcontext(&uc);
186 	unw_init_local(&cursor, &uc);
187 	while (unw_step(&cursor) > 0) {
188 		char name[128] = "?";
189 
190 		unw_get_reg(&cursor, UNW_REG_IP, &ip);
191 		unw_get_reg(&cursor, UNW_REG_SP, &sp);
192 
193 		if (!unw_get_proc_name(&cursor, buf, sizeof(buf), &off))
194 			snprintfrr(name, sizeof(name), "%s+%#lx",
195 				   buf, (long)off);
196 
197 		fb.pos = buf;
198 		if (unw_is_signal_frame(&cursor))
199 			bprintfrr(&fb, "    ---- signal ----\n");
200 		bprintfrr(&fb, "%-30s %16lx %16lx", name, (long)ip, (long)sp);
201 		if (dladdr((void *)ip, &dlinfo))
202 			bprintfrr(&fb, " %s (mapped at %p)",
203 				  dlinfo.dli_fname, dlinfo.dli_fbase);
204 		bprintfrr(&fb, "\n");
205 		zlog_sigsafe(fb.buf, fb.pos - fb.buf);
206 	}
207 #elif defined(HAVE_GLIBC_BACKTRACE)
208 	void *array[64];
209 	int size, i;
210 	char buf[128];
211 	struct fbuf fb = { .buf = buf, .pos = buf, .len = sizeof(buf) };
212 	char **bt = NULL;
213 
214 	size = backtrace(array, array_size(array));
215 	if (size <= 0 || (size_t)size > array_size(array))
216 		return;
217 
218 	bprintfrr(&fb, "Backtrace for %d stack frames:", size);
219 	zlog_sigsafe(fb.pos, fb.buf - fb.pos);
220 
221 	bt = backtrace_symbols(array, size);
222 
223 	for (i = 0; i < size; i++) {
224 		fb.pos = buf;
225 		if (bt)
226 			bprintfrr(&fb, "%s", bt[i]);
227 		else
228 			bprintfrr(&fb, "[bt %d] 0x%tx", i,
229 				  (ptrdiff_t)(array[i]));
230 		zlog_sigsafe(fb.buf, fb.pos - fb.buf);
231 	}
232 	if (bt)
233 		free(bt);
234 #endif /* HAVE_STRACK_TRACE */
235 }
236 
zlog_backtrace(int priority)237 void zlog_backtrace(int priority)
238 {
239 #ifdef HAVE_LIBUNWIND
240 	char buf[100];
241 	unw_cursor_t cursor;
242 	unw_context_t uc;
243 	unw_word_t ip, off, sp;
244 	Dl_info dlinfo;
245 
246 	unw_getcontext(&uc);
247 	unw_init_local(&cursor, &uc);
248 	zlog(priority, "Backtrace:");
249 	while (unw_step(&cursor) > 0) {
250 		char name[128] = "?";
251 
252 		unw_get_reg(&cursor, UNW_REG_IP, &ip);
253 		unw_get_reg(&cursor, UNW_REG_SP, &sp);
254 
255 		if (unw_is_signal_frame(&cursor))
256 			zlog(priority, "    ---- signal ----");
257 
258 		if (!unw_get_proc_name(&cursor, buf, sizeof(buf), &off))
259 			snprintf(name, sizeof(name), "%s+%#lx",
260 				buf, (long)off);
261 
262 		if (dladdr((void *)ip, &dlinfo))
263 			zlog(priority, "%-30s %16lx %16lx %s (mapped at %p)",
264 				name, (long)ip, (long)sp,
265 				dlinfo.dli_fname, dlinfo.dli_fbase);
266 		else
267 			zlog(priority, "%-30s %16lx %16lx",
268 				name, (long)ip, (long)sp);
269 	}
270 #elif defined(HAVE_GLIBC_BACKTRACE)
271 	void *array[20];
272 	int size, i;
273 	char **strings;
274 
275 	size = backtrace(array, array_size(array));
276 	if (size <= 0 || (size_t)size > array_size(array)) {
277 		flog_err_sys(
278 			EC_LIB_SYSTEM_CALL,
279 			"Cannot get backtrace, returned invalid # of frames %d (valid range is between 1 and %lu)",
280 			size, (unsigned long)(array_size(array)));
281 		return;
282 	}
283 	zlog(priority, "Backtrace for %d stack frames:", size);
284 	if (!(strings = backtrace_symbols(array, size))) {
285 		flog_err_sys(EC_LIB_SYSTEM_CALL,
286 			     "Cannot get backtrace symbols (out of memory?)");
287 		for (i = 0; i < size; i++)
288 			zlog(priority, "[bt %d] %p", i, array[i]);
289 	} else {
290 		for (i = 0; i < size; i++)
291 			zlog(priority, "[bt %d] %s", i, strings[i]);
292 		free(strings);
293 	}
294 #else /* !HAVE_GLIBC_BACKTRACE && !HAVE_LIBUNWIND */
295 	zlog(priority, "No backtrace available on this platform.");
296 #endif
297 }
298 
zlog_thread_info(int log_level)299 void zlog_thread_info(int log_level)
300 {
301 	struct thread *tc;
302 	tc = pthread_getspecific(thread_current);
303 
304 	if (tc)
305 		zlog(log_level,
306 		     "Current thread function %s, scheduled from file %s, line %u",
307 		     tc->funcname, tc->schedfrom, tc->schedfrom_line);
308 	else
309 		zlog(log_level, "Current thread not known/applicable");
310 }
311 
_zlog_assert_failed(const char * assertion,const char * file,unsigned int line,const char * function)312 void _zlog_assert_failed(const char *assertion, const char *file,
313 			 unsigned int line, const char *function)
314 {
315 	zlog(LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
316 	     assertion, file, line, (function ? function : "?"));
317 	zlog_backtrace(LOG_CRIT);
318 	zlog_thread_info(LOG_CRIT);
319 	log_memstats(stderr, "log");
320 	abort();
321 }
322 
memory_oom(size_t size,const char * name)323 void memory_oom(size_t size, const char *name)
324 {
325 	zlog(LOG_CRIT,
326 	     "out of memory: failed to allocate %zu bytes for %s object",
327 	     size, name);
328 	zlog_backtrace(LOG_CRIT);
329 	log_memstats(stderr, "log");
330 	abort();
331 }
332 
333 /* Wrapper around strerror to handle case where it returns NULL. */
safe_strerror(int errnum)334 const char *safe_strerror(int errnum)
335 {
336 	const char *s = strerror(errnum);
337 	return (s != NULL) ? s : "Unknown error";
338 }
339 
340 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
341 static const struct zebra_desc_table command_types[] = {
342 	DESC_ENTRY(ZEBRA_INTERFACE_ADD),
343 	DESC_ENTRY(ZEBRA_INTERFACE_DELETE),
344 	DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD),
345 	DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE),
346 	DESC_ENTRY(ZEBRA_INTERFACE_UP),
347 	DESC_ENTRY(ZEBRA_INTERFACE_DOWN),
348 	DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER),
349 	DESC_ENTRY(ZEBRA_ROUTE_ADD),
350 	DESC_ENTRY(ZEBRA_ROUTE_DELETE),
351 	DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER),
352 	DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD),
353 	DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE),
354 	DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
355 	DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
356 	DESC_ENTRY(ZEBRA_ROUTER_ID_ADD),
357 	DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE),
358 	DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE),
359 	DESC_ENTRY(ZEBRA_HELLO),
360 	DESC_ENTRY(ZEBRA_CAPABILITIES),
361 	DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER),
362 	DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER),
363 	DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE),
364 	DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD),
365 	DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE),
366 	DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE),
367 	DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER),
368 	DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER),
369 	DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE),
370 	DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER),
371 	DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER),
372 	DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE),
373 	DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY),
374 	DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD),
375 	DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL),
376 	DESC_ENTRY(ZEBRA_VRF_UNREGISTER),
377 	DESC_ENTRY(ZEBRA_VRF_ADD),
378 	DESC_ENTRY(ZEBRA_VRF_DELETE),
379 	DESC_ENTRY(ZEBRA_VRF_LABEL),
380 	DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE),
381 	DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER),
382 	DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER),
383 	DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV),
384 	DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV),
385 	DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB),
386 	DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS),
387 	DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD),
388 	DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE),
389 	DESC_ENTRY(ZEBRA_MPLS_LABELS_REPLACE),
390 	DESC_ENTRY(ZEBRA_SR_POLICY_SET),
391 	DESC_ENTRY(ZEBRA_SR_POLICY_DELETE),
392 	DESC_ENTRY(ZEBRA_SR_POLICY_NOTIFY_STATUS),
393 	DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS),
394 	DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT),
395 	DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC),
396 	DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK),
397 	DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK),
398 	DESC_ENTRY(ZEBRA_FEC_REGISTER),
399 	DESC_ENTRY(ZEBRA_FEC_UNREGISTER),
400 	DESC_ENTRY(ZEBRA_FEC_UPDATE),
401 	DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI),
402 	DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW),
403 	DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP),
404 	DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET),
405 	DESC_ENTRY(ZEBRA_LOCAL_ES_ADD),
406 	DESC_ENTRY(ZEBRA_LOCAL_ES_DEL),
407 	DESC_ENTRY(ZEBRA_REMOTE_ES_VTEP_ADD),
408 	DESC_ENTRY(ZEBRA_REMOTE_ES_VTEP_DEL),
409 	DESC_ENTRY(ZEBRA_LOCAL_ES_EVI_ADD),
410 	DESC_ENTRY(ZEBRA_LOCAL_ES_EVI_DEL),
411 	DESC_ENTRY(ZEBRA_VNI_ADD),
412 	DESC_ENTRY(ZEBRA_VNI_DEL),
413 	DESC_ENTRY(ZEBRA_L3VNI_ADD),
414 	DESC_ENTRY(ZEBRA_L3VNI_DEL),
415 	DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD),
416 	DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL),
417 	DESC_ENTRY(ZEBRA_MACIP_ADD),
418 	DESC_ENTRY(ZEBRA_MACIP_DEL),
419 	DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD),
420 	DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL),
421 	DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD),
422 	DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL),
423 	DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION),
424 	DESC_ENTRY(ZEBRA_PW_ADD),
425 	DESC_ENTRY(ZEBRA_PW_DELETE),
426 	DESC_ENTRY(ZEBRA_PW_SET),
427 	DESC_ENTRY(ZEBRA_PW_UNSET),
428 	DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE),
429 	DESC_ENTRY(ZEBRA_RULE_ADD),
430 	DESC_ENTRY(ZEBRA_RULE_DELETE),
431 	DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER),
432 	DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT),
433 	DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK),
434 	DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK),
435 	DESC_ENTRY(ZEBRA_IPSET_CREATE),
436 	DESC_ENTRY(ZEBRA_IPSET_DESTROY),
437 	DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD),
438 	DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE),
439 	DESC_ENTRY(ZEBRA_IPSET_NOTIFY_OWNER),
440 	DESC_ENTRY(ZEBRA_IPSET_ENTRY_NOTIFY_OWNER),
441 	DESC_ENTRY(ZEBRA_IPTABLE_ADD),
442 	DESC_ENTRY(ZEBRA_IPTABLE_DELETE),
443 	DESC_ENTRY(ZEBRA_IPTABLE_NOTIFY_OWNER),
444 	DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL),
445 	DESC_ENTRY(ZEBRA_VXLAN_SG_ADD),
446 	DESC_ENTRY(ZEBRA_VXLAN_SG_DEL),
447 	DESC_ENTRY(ZEBRA_VXLAN_SG_REPLAY),
448 	DESC_ENTRY(ZEBRA_MLAG_PROCESS_UP),
449 	DESC_ENTRY(ZEBRA_MLAG_PROCESS_DOWN),
450 	DESC_ENTRY(ZEBRA_MLAG_CLIENT_REGISTER),
451 	DESC_ENTRY(ZEBRA_MLAG_CLIENT_UNREGISTER),
452 	DESC_ENTRY(ZEBRA_MLAG_FORWARD_MSG),
453 	DESC_ENTRY(ZEBRA_ERROR),
454 	DESC_ENTRY(ZEBRA_CLIENT_CAPABILITIES),
455 	DESC_ENTRY(ZEBRA_OPAQUE_MESSAGE),
456 	DESC_ENTRY(ZEBRA_OPAQUE_REGISTER),
457 	DESC_ENTRY(ZEBRA_OPAQUE_UNREGISTER),
458 	DESC_ENTRY(ZEBRA_NEIGH_DISCOVER)};
459 #undef DESC_ENTRY
460 
461 static const struct zebra_desc_table unknown = {0, "unknown", '?'};
462 
zroute_lookup(unsigned int zroute)463 static const struct zebra_desc_table *zroute_lookup(unsigned int zroute)
464 {
465 	unsigned int i;
466 
467 	if (zroute >= array_size(route_types)) {
468 		flog_err(EC_LIB_DEVELOPMENT, "unknown zebra route type: %u",
469 			 zroute);
470 		return &unknown;
471 	}
472 	if (zroute == route_types[zroute].type)
473 		return &route_types[zroute];
474 	for (i = 0; i < array_size(route_types); i++) {
475 		if (zroute == route_types[i].type) {
476 			zlog_warn(
477 				"internal error: route type table out of order while searching for %u, please notify developers",
478 				zroute);
479 			return &route_types[i];
480 		}
481 	}
482 	flog_err(EC_LIB_DEVELOPMENT,
483 		 "internal error: cannot find route type %u in table!", zroute);
484 	return &unknown;
485 }
486 
zebra_route_string(unsigned int zroute)487 const char *zebra_route_string(unsigned int zroute)
488 {
489 	return zroute_lookup(zroute)->string;
490 }
491 
zebra_route_char(unsigned int zroute)492 char zebra_route_char(unsigned int zroute)
493 {
494 	return zroute_lookup(zroute)->chr;
495 }
496 
zserv_command_string(unsigned int command)497 const char *zserv_command_string(unsigned int command)
498 {
499 	if (command >= array_size(command_types)) {
500 		flog_err(EC_LIB_DEVELOPMENT, "unknown zserv command type: %u",
501 			 command);
502 		return unknown.string;
503 	}
504 	return command_types[command].string;
505 }
506 
proto_name2num(const char * s)507 int proto_name2num(const char *s)
508 {
509 	unsigned i;
510 
511 	for (i = 0; i < array_size(route_types); ++i)
512 		if (strcasecmp(s, route_types[i].string) == 0)
513 			return route_types[i].type;
514 	return -1;
515 }
516 
proto_redistnum(int afi,const char * s)517 int proto_redistnum(int afi, const char *s)
518 {
519 	if (!s)
520 		return -1;
521 
522 	if (afi == AFI_IP) {
523 		if (strmatch(s, "kernel"))
524 			return ZEBRA_ROUTE_KERNEL;
525 		else if (strmatch(s, "connected"))
526 			return ZEBRA_ROUTE_CONNECT;
527 		else if (strmatch(s, "static"))
528 			return ZEBRA_ROUTE_STATIC;
529 		else if (strmatch(s, "rip"))
530 			return ZEBRA_ROUTE_RIP;
531 		else if (strmatch(s, "eigrp"))
532 			return ZEBRA_ROUTE_EIGRP;
533 		else if (strmatch(s, "ospf"))
534 			return ZEBRA_ROUTE_OSPF;
535 		else if (strmatch(s, "isis"))
536 			return ZEBRA_ROUTE_ISIS;
537 		else if (strmatch(s, "bgp"))
538 			return ZEBRA_ROUTE_BGP;
539 		else if (strmatch(s, "table"))
540 			return ZEBRA_ROUTE_TABLE;
541 		else if (strmatch(s, "vnc"))
542 			return ZEBRA_ROUTE_VNC;
543 		else if (strmatch(s, "vnc-direct"))
544 			return ZEBRA_ROUTE_VNC_DIRECT;
545 		else if (strmatch(s, "nhrp"))
546 			return ZEBRA_ROUTE_NHRP;
547 		else if (strmatch(s, "babel"))
548 			return ZEBRA_ROUTE_BABEL;
549 		else if (strmatch(s, "sharp"))
550 			return ZEBRA_ROUTE_SHARP;
551 		else if (strmatch(s, "openfabric"))
552 			return ZEBRA_ROUTE_OPENFABRIC;
553 	}
554 	if (afi == AFI_IP6) {
555 		if (strmatch(s, "kernel"))
556 			return ZEBRA_ROUTE_KERNEL;
557 		else if (strmatch(s, "connected"))
558 			return ZEBRA_ROUTE_CONNECT;
559 		else if (strmatch(s, "static"))
560 			return ZEBRA_ROUTE_STATIC;
561 		else if (strmatch(s, "ripng"))
562 			return ZEBRA_ROUTE_RIPNG;
563 		else if (strmatch(s, "ospf6"))
564 			return ZEBRA_ROUTE_OSPF6;
565 		else if (strmatch(s, "isis"))
566 			return ZEBRA_ROUTE_ISIS;
567 		else if (strmatch(s, "bgp"))
568 			return ZEBRA_ROUTE_BGP;
569 		else if (strmatch(s, "table"))
570 			return ZEBRA_ROUTE_TABLE;
571 		else if (strmatch(s, "vnc"))
572 			return ZEBRA_ROUTE_VNC;
573 		else if (strmatch(s, "vnc-direct"))
574 			return ZEBRA_ROUTE_VNC_DIRECT;
575 		else if (strmatch(s, "nhrp"))
576 			return ZEBRA_ROUTE_NHRP;
577 		else if (strmatch(s, "babel"))
578 			return ZEBRA_ROUTE_BABEL;
579 		else if (strmatch(s, "sharp"))
580 			return ZEBRA_ROUTE_SHARP;
581 		else if (strmatch(s, "openfabric"))
582 			return ZEBRA_ROUTE_OPENFABRIC;
583 	}
584 	return -1;
585 }
586 
zlog_hexdump(const void * mem,size_t len)587 void zlog_hexdump(const void *mem, size_t len)
588 {
589 	char line[64];
590 	const uint8_t *src = mem;
591 	const uint8_t *end = src + len;
592 
593 	if (len == 0) {
594 		zlog_debug("%016lx: (zero length / no data)", (long)src);
595 		return;
596 	}
597 
598 	while (src < end) {
599 		struct fbuf fb = {
600 			.buf = line,
601 			.pos = line,
602 			.len = sizeof(line),
603 		};
604 		const uint8_t *lineend = src + 8;
605 		unsigned line_bytes = 0;
606 
607 		bprintfrr(&fb, "%016lx: ", (long)src);
608 
609 		while (src < lineend && src < end) {
610 			bprintfrr(&fb, "%02x ", *src++);
611 			line_bytes++;
612 		}
613 		if (line_bytes < 8)
614 			bprintfrr(&fb, "%*s", (8 - line_bytes) * 3, "");
615 
616 		src -= line_bytes;
617 		while (src < lineend && src < end && fb.pos < fb.buf + fb.len) {
618 			uint8_t byte = *src++;
619 
620 			if (isprint(byte))
621 				*fb.pos++ = byte;
622 			else
623 				*fb.pos++ = '.';
624 		}
625 
626 		zlog_debug("%.*s", (int)(fb.pos - fb.buf), fb.buf);
627 	}
628 }
629 
zlog_sanitize(char * buf,size_t bufsz,const void * in,size_t inlen)630 const char *zlog_sanitize(char *buf, size_t bufsz, const void *in, size_t inlen)
631 {
632 	const char *inbuf = in;
633 	char *pos = buf, *end = buf + bufsz;
634 	const char *iend = inbuf + inlen;
635 
636 	memset(buf, 0, bufsz);
637 	for (; inbuf < iend; inbuf++) {
638 		/* don't write partial escape sequence */
639 		if (end - pos < 5)
640 			break;
641 
642 		if (*inbuf == '\n')
643 			snprintf(pos, end - pos, "\\n");
644 		else if (*inbuf == '\r')
645 			snprintf(pos, end - pos, "\\r");
646 		else if (*inbuf == '\t')
647 			snprintf(pos, end - pos, "\\t");
648 		else if (*inbuf < ' ' || *inbuf == '"' || *inbuf >= 127)
649 			snprintf(pos, end - pos, "\\x%02hhx", *inbuf);
650 		else
651 			*pos = *inbuf;
652 
653 		pos += strlen(pos);
654 	}
655 	return buf;
656 }
657