blob: 04f8fab6307ed96d28530245a0cf5bb88d911430 [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajs274a4a42004-12-07 15:39:31 +00002 * Logging of zebra
paul718e3742002-12-13 20:15:29 +00003 * 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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
David Lampartere0ca5fd2009-09-16 01:52:42 +020023#define QUAGGA_DEFINE_DESC_TABLE
24
paul718e3742002-12-13 20:15:29 +000025#include <zebra.h>
26
27#include "log.h"
28#include "memory.h"
29#include "command.h"
ajs7d149b82004-11-28 23:00:01 +000030#ifndef SUNOS_5
31#include <sys/un.h>
32#endif
Paul Jakma30a22312008-08-15 14:05:22 +010033/* for printstack on solaris */
34#ifdef HAVE_UCONTEXT_H
35#include <ucontext.h>
36#endif
paul718e3742002-12-13 20:15:29 +000037
ajsc4c7d0c2005-02-03 19:22:05 +000038static int logfile_fd = -1; /* Used in signal handler. */
ajs1e221352005-02-03 16:42:40 +000039
paul718e3742002-12-13 20:15:29 +000040struct zlog *zlog_default = NULL;
41
42const char *zlog_proto_names[] =
43{
44 "NONE",
45 "DEFAULT",
46 "ZEBRA",
47 "RIP",
48 "BGP",
49 "OSPF",
50 "RIPNG",
Paul Jakma57345092011-12-25 17:52:09 +010051 "BABEL",
paul718e3742002-12-13 20:15:29 +000052 "OSPF6",
jardin9e867fe2003-12-23 08:56:18 +000053 "ISIS",
paul718e3742002-12-13 20:15:29 +000054 "MASC",
55 NULL,
56};
57
58const char *zlog_priority[] =
59{
60 "emergencies",
61 "alerts",
62 "critical",
63 "errors",
64 "warnings",
65 "notifications",
66 "informational",
67 "debugging",
68 NULL,
69};
70
71
David Lamparter6b0655a2014-06-04 06:53:35 +020072
paul718e3742002-12-13 20:15:29 +000073/* For time string format. */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000074
75size_t
76quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
77{
78 static struct {
79 time_t last;
80 size_t len;
81 char buf[28];
82 } cache;
83 struct timeval clock;
84
85 /* would it be sufficient to use global 'recent_time' here? I fear not... */
86 gettimeofday(&clock, NULL);
87
88 /* first, we update the cache if the time has changed */
89 if (cache.last != clock.tv_sec)
90 {
91 struct tm *tm;
92 cache.last = clock.tv_sec;
93 tm = localtime(&cache.last);
94 cache.len = strftime(cache.buf, sizeof(cache.buf),
95 "%Y/%m/%d %H:%M:%S", tm);
96 }
97 /* note: it's not worth caching the subsecond part, because
98 chances are that back-to-back calls are not sufficiently close together
99 for the clock not to have ticked forward */
100
101 if (buflen > cache.len)
102 {
103 memcpy(buf, cache.buf, cache.len);
104 if ((timestamp_precision > 0) &&
105 (buflen > cache.len+1+timestamp_precision))
106 {
107 /* should we worry about locale issues? */
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000108 static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
109 int prec;
110 char *p = buf+cache.len+1+(prec = timestamp_precision);
111 *p-- = '\0';
112 while (prec > 6)
113 /* this is unlikely to happen, but protect anyway */
114 {
115 *p-- = '0';
116 prec--;
117 }
118 clock.tv_usec /= divisor[prec];
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000119 do
120 {
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000121 *p-- = '0'+(clock.tv_usec % 10);
122 clock.tv_usec /= 10;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000123 }
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000124 while (--prec > 0);
125 *p = '.';
126 return cache.len+1+timestamp_precision;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000127 }
128 buf[cache.len] = '\0';
129 return cache.len;
130 }
131 if (buflen > 0)
132 buf[0] = '\0';
133 return 0;
134}
paul718e3742002-12-13 20:15:29 +0000135
136/* Utility routine for current time printing. */
137static void
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000138time_print(FILE *fp, struct timestamp_control *ctl)
paul718e3742002-12-13 20:15:29 +0000139{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000140 if (!ctl->already_rendered)
141 {
142 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
143 ctl->already_rendered = 1;
144 }
145 fprintf(fp, "%s ", ctl->buf);
paul718e3742002-12-13 20:15:29 +0000146}
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000147
David Lamparter6b0655a2014-06-04 06:53:35 +0200148
paul718e3742002-12-13 20:15:29 +0000149/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +0000150static void
151vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +0000152{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000153 struct timestamp_control tsctl;
154 tsctl.already_rendered = 0;
155
paul718e3742002-12-13 20:15:29 +0000156 /* If zlog is not specified, use default one. */
157 if (zl == NULL)
158 zl = zlog_default;
159
160 /* When zlog_default is also NULL, use stderr for logging. */
161 if (zl == NULL)
162 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000163 tsctl.precision = 0;
164 time_print(stderr, &tsctl);
paul718e3742002-12-13 20:15:29 +0000165 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +0000166 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +0000167 fprintf (stderr, "\n");
168 fflush (stderr);
169
170 /* In this case we return at here. */
171 return;
172 }
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000173 tsctl.precision = zl->timestamp_precision;
paul718e3742002-12-13 20:15:29 +0000174
paul718e3742002-12-13 20:15:29 +0000175 /* Syslog output */
ajs274a4a42004-12-07 15:39:31 +0000176 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
ajsd246bd92004-11-23 17:35:08 +0000177 {
178 va_list ac;
179 va_copy(ac, args);
180 vsyslog (priority|zlog_default->facility, format, ac);
181 va_end(ac);
182 }
paul718e3742002-12-13 20:15:29 +0000183
184 /* File output. */
ajs274a4a42004-12-07 15:39:31 +0000185 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
paul718e3742002-12-13 20:15:29 +0000186 {
ajsd246bd92004-11-23 17:35:08 +0000187 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000188 time_print (zl->fp, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000189 if (zl->record_priority)
190 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000191 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000192 va_copy(ac, args);
193 vfprintf (zl->fp, format, ac);
194 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000195 fprintf (zl->fp, "\n");
196 fflush (zl->fp);
197 }
198
199 /* stdout output. */
ajs274a4a42004-12-07 15:39:31 +0000200 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
paul718e3742002-12-13 20:15:29 +0000201 {
ajsd246bd92004-11-23 17:35:08 +0000202 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000203 time_print (stdout, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000204 if (zl->record_priority)
205 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000206 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000207 va_copy(ac, args);
208 vfprintf (stdout, format, ac);
209 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000210 fprintf (stdout, "\n");
211 fflush (stdout);
212 }
213
paul718e3742002-12-13 20:15:29 +0000214 /* Terminal monitor. */
ajs274a4a42004-12-07 15:39:31 +0000215 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
216 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000217 zlog_proto_names[zl->protocol], format, &tsctl, args);
paul718e3742002-12-13 20:15:29 +0000218}
219
ajs59a06a92004-11-23 18:19:14 +0000220static char *
221str_append(char *dst, int len, const char *src)
222{
223 while ((len-- > 0) && *src)
224 *dst++ = *src++;
225 return dst;
226}
227
228static char *
229num_append(char *s, int len, u_long x)
230{
231 char buf[30];
ajs7d149b82004-11-28 23:00:01 +0000232 char *t;
ajs59a06a92004-11-23 18:19:14 +0000233
ajs7d149b82004-11-28 23:00:01 +0000234 if (!x)
235 return str_append(s,len,"0");
236 *(t = &buf[sizeof(buf)-1]) = '\0';
ajs59a06a92004-11-23 18:19:14 +0000237 while (x && (t > buf))
238 {
239 *--t = '0'+(x % 10);
240 x /= 10;
241 }
242 return str_append(s,len,t);
243}
244
Paul Jakmafb66b292006-05-28 08:26:15 +0000245#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
ajs7d149b82004-11-28 23:00:01 +0000246static char *
247hex_append(char *s, int len, u_long x)
248{
249 char buf[30];
250 char *t;
251
252 if (!x)
253 return str_append(s,len,"0");
254 *(t = &buf[sizeof(buf)-1]) = '\0';
255 while (x && (t > buf))
256 {
257 u_int cc = (x % 16);
258 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
259 x /= 16;
260 }
261 return str_append(s,len,t);
262}
ajs31364272005-01-18 22:18:59 +0000263#endif
ajs7d149b82004-11-28 23:00:01 +0000264
ajs7d149b82004-11-28 23:00:01 +0000265/* Needs to be enhanced to support Solaris. */
266static int
267syslog_connect(void)
268{
269#ifdef SUNOS_5
270 return -1;
271#else
272 int fd;
273 char *s;
274 struct sockaddr_un addr;
275
276 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
277 return -1;
278 addr.sun_family = AF_UNIX;
279#ifdef _PATH_LOG
280#define SYSLOG_SOCKET_PATH _PATH_LOG
281#else
282#define SYSLOG_SOCKET_PATH "/dev/log"
283#endif
284 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
285#undef SYSLOG_SOCKET_PATH
286 *s = '\0';
287 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
288 {
289 close(fd);
290 return -1;
291 }
292 return fd;
293#endif
294}
295
296static void
297syslog_sigsafe(int priority, const char *msg, size_t msglen)
298{
ajs1e221352005-02-03 16:42:40 +0000299 static int syslog_fd = -1;
ajs7d149b82004-11-28 23:00:01 +0000300 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
301 char *s;
302
303 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
304 return;
305
306#define LOC s,buf+sizeof(buf)-s
307 s = buf;
308 s = str_append(LOC,"<");
309 s = num_append(LOC,priority);
310 s = str_append(LOC,">");
311 /* forget about the timestamp, too difficult in a signal handler */
312 s = str_append(LOC,zlog_default->ident);
313 if (zlog_default->syslog_options & LOG_PID)
314 {
315 s = str_append(LOC,"[");
316 s = num_append(LOC,getpid());
317 s = str_append(LOC,"]");
318 }
319 s = str_append(LOC,": ");
320 s = str_append(LOC,msg);
321 write(syslog_fd,buf,s-buf);
322#undef LOC
323}
324
ajs1e221352005-02-03 16:42:40 +0000325static int
326open_crashlog(void)
327{
328#define CRASHLOG_PREFIX "/var/tmp/quagga."
329#define CRASHLOG_SUFFIX "crashlog"
330 if (zlog_default && zlog_default->ident)
331 {
332 /* Avoid strlen since it is not async-signal-safe. */
333 const char *p;
334 size_t ilen;
335
336 for (p = zlog_default->ident, ilen = 0; *p; p++)
337 ilen++;
338 {
339 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
340 char *s = buf;
341#define LOC s,buf+sizeof(buf)-s
342 s = str_append(LOC, CRASHLOG_PREFIX);
343 s = str_append(LOC, zlog_default->ident);
344 s = str_append(LOC, ".");
345 s = str_append(LOC, CRASHLOG_SUFFIX);
346#undef LOC
347 *s = '\0';
348 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
349 }
350 }
351 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
352 LOGFILE_MASK);
353#undef CRASHLOG_SUFFIX
354#undef CRASHLOG_PREFIX
355}
356
ajs7d149b82004-11-28 23:00:01 +0000357/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000358void
ajs31364272005-01-18 22:18:59 +0000359zlog_signal(int signo, const char *action
360#ifdef SA_SIGINFO
361 , siginfo_t *siginfo, void *program_counter
362#endif
363 )
ajs59a06a92004-11-23 18:19:14 +0000364{
365 time_t now;
ajs40abf232005-01-12 17:27:27 +0000366 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
ajs59a06a92004-11-23 18:19:14 +0000367 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000368 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000369#define LOC s,buf+sizeof(buf)-s
370
371 time(&now);
372 if (zlog_default)
373 {
374 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
375 *s++ = ':';
376 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000377 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000378 }
379 s = str_append(LOC,"Received signal ");
380 s = num_append(LOC,signo);
381 s = str_append(LOC," at ");
382 s = num_append(LOC,now);
ajs31364272005-01-18 22:18:59 +0000383#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000384 s = str_append(LOC," (si_addr 0x");
385 s = hex_append(LOC,(u_long)(siginfo->si_addr));
386 if (program_counter)
387 {
388 s = str_append(LOC,", PC 0x");
389 s = hex_append(LOC,(u_long)program_counter);
390 }
391 s = str_append(LOC,"); ");
ajs31364272005-01-18 22:18:59 +0000392#else /* SA_SIGINFO */
393 s = str_append(LOC,"; ");
394#endif /* SA_SIGINFO */
ajs59a06a92004-11-23 18:19:14 +0000395 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000396 if (s < buf+sizeof(buf))
397 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000398
ajs274a4a42004-12-07 15:39:31 +0000399 /* N.B. implicit priority is most severe */
ajs1e221352005-02-03 16:42:40 +0000400#define PRI LOG_CRIT
ajs274a4a42004-12-07 15:39:31 +0000401
ajs1e221352005-02-03 16:42:40 +0000402#define DUMP(FD) write(FD, buf, s-buf);
403 /* If no file logging configured, try to write to fallback log file. */
ajsc4c7d0c2005-02-03 19:22:05 +0000404 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
405 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000406 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000407 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000408 else
409 {
ajs274a4a42004-12-07 15:39:31 +0000410 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000411 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000412 /* Remove trailing '\n' for monitor and syslog */
413 *--s = '\0';
414 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
415 vty_log_fixed(buf,s-buf);
416 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
417 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000418 }
419#undef DUMP
420
ajs31364272005-01-18 22:18:59 +0000421 zlog_backtrace_sigsafe(PRI,
422#ifdef SA_SIGINFO
423 program_counter
424#else
425 NULL
426#endif
427 );
David Lamparter615f9f12013-11-18 23:52:02 +0100428
429 s = buf;
430 if (!thread_current)
431 s = str_append (LOC, "no thread information available\n");
432 else
433 {
434 s = str_append (LOC, "in thread ");
435 s = str_append (LOC, thread_current->funcname);
436 s = str_append (LOC, " scheduled from ");
437 s = str_append (LOC, thread_current->schedfrom);
438 s = str_append (LOC, ":");
439 s = num_append (LOC, thread_current->schedfrom_line);
440 s = str_append (LOC, "\n");
441 }
442
443#define DUMP(FD) write(FD, buf, s-buf);
444 /* If no file logging configured, try to write to fallback log file. */
445 if (logfile_fd >= 0)
446 DUMP(logfile_fd)
447 if (!zlog_default)
448 DUMP(STDERR_FILENO)
449 else
450 {
451 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
452 DUMP(STDOUT_FILENO)
453 /* Remove trailing '\n' for monitor and syslog */
454 *--s = '\0';
455 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
456 vty_log_fixed(buf,s-buf);
457 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
458 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
459 }
460#undef DUMP
461
ajs274a4a42004-12-07 15:39:31 +0000462#undef PRI
ajs063ee522004-11-26 18:11:14 +0000463#undef LOC
464}
ajs59a06a92004-11-23 18:19:14 +0000465
ajs063ee522004-11-26 18:11:14 +0000466/* Log a backtrace using only async-signal-safe functions.
467 Needs to be enhanced to support syslog logging. */
468void
ajs239c26f2005-01-17 15:22:28 +0000469zlog_backtrace_sigsafe(int priority, void *program_counter)
ajs063ee522004-11-26 18:11:14 +0000470{
Paul Jakmafb66b292006-05-28 08:26:15 +0000471#ifdef HAVE_STACK_TRACE
ajs239c26f2005-01-17 15:22:28 +0000472 static const char pclabel[] = "Program counter: ";
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700473 void *array[64];
ajs063ee522004-11-26 18:11:14 +0000474 int size;
475 char buf[100];
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700476 char *s, **bt = NULL;
ajs063ee522004-11-26 18:11:14 +0000477#define LOC s,buf+sizeof(buf)-s
478
Paul Jakmafb66b292006-05-28 08:26:15 +0000479#ifdef HAVE_GLIBC_BACKTRACE
David Lamparter4d474fa2013-11-19 15:00:06 +0100480 size = backtrace(array, array_size(array));
481 if (size <= 0 || (size_t)size > array_size(array))
ajs063ee522004-11-26 18:11:14 +0000482 return;
ajs59a06a92004-11-23 18:19:14 +0000483
ajs1e221352005-02-03 16:42:40 +0000484#define DUMP(FD) { \
ajs239c26f2005-01-17 15:22:28 +0000485 if (program_counter) \
486 { \
ajs1e221352005-02-03 16:42:40 +0000487 write(FD, pclabel, sizeof(pclabel)-1); \
488 backtrace_symbols_fd(&program_counter, 1, FD); \
ajs239c26f2005-01-17 15:22:28 +0000489 } \
ajs1e221352005-02-03 16:42:40 +0000490 write(FD, buf, s-buf); \
491 backtrace_symbols_fd(array, size, FD); \
ajs59a06a92004-11-23 18:19:14 +0000492}
Paul Jakmafb66b292006-05-28 08:26:15 +0000493#elif defined(HAVE_PRINTSTACK)
494#define DUMP(FD) { \
495 if (program_counter) \
496 write((FD), pclabel, sizeof(pclabel)-1); \
497 write((FD), buf, s-buf); \
498 printstack((FD)); \
499}
500#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
501
502 s = buf;
503 s = str_append(LOC,"Backtrace for ");
504 s = num_append(LOC,size);
505 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000506
ajsc4c7d0c2005-02-03 19:22:05 +0000507 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
508 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000509 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000510 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000511 else
512 {
ajs274a4a42004-12-07 15:39:31 +0000513 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000514 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000515 /* Remove trailing '\n' for monitor and syslog */
516 *--s = '\0';
517 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
518 vty_log_fixed(buf,s-buf);
519 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
520 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
521 {
522 int i;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700523#ifdef HAVE_GLIBC_BACKTRACE
524 bt = backtrace_symbols(array, size);
525#endif
ajs274a4a42004-12-07 15:39:31 +0000526 /* Just print the function addresses. */
527 for (i = 0; i < size; i++)
528 {
529 s = buf;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700530 if (bt)
531 s = str_append(LOC, bt[i]);
532 else {
533 s = str_append(LOC,"[bt ");
534 s = num_append(LOC,i);
535 s = str_append(LOC,"] 0x");
536 s = hex_append(LOC,(u_long)(array[i]));
537 }
ajs274a4a42004-12-07 15:39:31 +0000538 *s = '\0';
539 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
540 vty_log_fixed(buf,s-buf);
541 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000542 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000543 }
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700544 if (bt)
545 free(bt);
ajs274a4a42004-12-07 15:39:31 +0000546 }
ajs59a06a92004-11-23 18:19:14 +0000547 }
548#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000549#undef LOC
Paul Jakmafb66b292006-05-28 08:26:15 +0000550#endif /* HAVE_STRACK_TRACE */
ajs063ee522004-11-26 18:11:14 +0000551}
552
553void
554zlog_backtrace(int priority)
555{
556#ifndef HAVE_GLIBC_BACKTRACE
557 zlog(NULL, priority, "No backtrace available on this platform.");
558#else
559 void *array[20];
560 int size, i;
561 char **strings;
562
David Lamparter4d474fa2013-11-19 15:00:06 +0100563 size = backtrace(array, array_size(array));
564 if (size <= 0 || (size_t)size > array_size(array))
ajs063ee522004-11-26 18:11:14 +0000565 {
566 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000567 "(valid range is between 1 and %lu)",
Balaji.G837d16c2012-09-26 14:09:10 +0530568 size, (unsigned long)(array_size(array)));
ajs063ee522004-11-26 18:11:14 +0000569 return;
570 }
571 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
572 if (!(strings = backtrace_symbols(array, size)))
573 {
574 zlog_err("Cannot get backtrace symbols (out of memory?)");
575 for (i = 0; i < size; i++)
576 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
577 }
578 else
579 {
580 for (i = 0; i < size; i++)
581 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
582 free(strings);
583 }
584#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000585}
586
paul718e3742002-12-13 20:15:29 +0000587void
588zlog (struct zlog *zl, int priority, const char *format, ...)
589{
ajsd246bd92004-11-23 17:35:08 +0000590 va_list args;
paul718e3742002-12-13 20:15:29 +0000591
ajsd246bd92004-11-23 17:35:08 +0000592 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000593 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000594 va_end (args);
paul718e3742002-12-13 20:15:29 +0000595}
596
ajsd246bd92004-11-23 17:35:08 +0000597#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
598void \
599FUNCNAME(const char *format, ...) \
600{ \
601 va_list args; \
602 va_start(args, format); \
603 vzlog (NULL, PRIORITY, format, args); \
604 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000605}
606
ajsd246bd92004-11-23 17:35:08 +0000607ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000608
ajsd246bd92004-11-23 17:35:08 +0000609ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000610
ajsd246bd92004-11-23 17:35:08 +0000611ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000612
ajsd246bd92004-11-23 17:35:08 +0000613ZLOG_FUNC(zlog_notice, LOG_NOTICE)
614
615ZLOG_FUNC(zlog_debug, LOG_DEBUG)
616
617#undef ZLOG_FUNC
618
619#define PLOG_FUNC(FUNCNAME,PRIORITY) \
620void \
621FUNCNAME(struct zlog *zl, const char *format, ...) \
622{ \
623 va_list args; \
624 va_start(args, format); \
625 vzlog (zl, PRIORITY, format, args); \
626 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000627}
628
ajsd246bd92004-11-23 17:35:08 +0000629PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000630
ajsd246bd92004-11-23 17:35:08 +0000631PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000632
ajsd246bd92004-11-23 17:35:08 +0000633PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000634
ajsd246bd92004-11-23 17:35:08 +0000635PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000636
ajsd246bd92004-11-23 17:35:08 +0000637PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000638
ajsd246bd92004-11-23 17:35:08 +0000639#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000640
David Lamparter615f9f12013-11-18 23:52:02 +0100641void zlog_thread_info (int log_level)
642{
643 if (thread_current)
644 zlog(NULL, log_level, "Current thread function %s, scheduled from "
645 "file %s, line %u", thread_current->funcname,
646 thread_current->schedfrom, thread_current->schedfrom_line);
647 else
648 zlog(NULL, log_level, "Current thread not known/applicable");
649}
650
ajscee3df12004-11-24 17:14:49 +0000651void
652_zlog_assert_failed (const char *assertion, const char *file,
653 unsigned int line, const char *function)
654{
ajsc4c7d0c2005-02-03 19:22:05 +0000655 /* Force fallback file logging? */
656 if (zlog_default && !zlog_default->fp &&
657 ((logfile_fd = open_crashlog()) >= 0) &&
658 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
659 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
ajs1e221352005-02-03 16:42:40 +0000660 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
661 assertion,file,line,(function ? function : "?"));
662 zlog_backtrace(LOG_CRIT);
David Lamparter615f9f12013-11-18 23:52:02 +0100663 zlog_thread_info(LOG_CRIT);
ajscee3df12004-11-24 17:14:49 +0000664 abort();
665}
666
David Lamparter6b0655a2014-06-04 06:53:35 +0200667
paul718e3742002-12-13 20:15:29 +0000668/* Open log stream */
669struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000670openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000671 int syslog_flags, int syslog_facility)
672{
673 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000674 u_int i;
paul718e3742002-12-13 20:15:29 +0000675
ajs274a4a42004-12-07 15:39:31 +0000676 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000677
678 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000679 zl->protocol = protocol;
680 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000681 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000682
ajs274a4a42004-12-07 15:39:31 +0000683 /* Set default logging levels. */
Balaji.G837d16c2012-09-26 14:09:10 +0530684 for (i = 0; i < array_size(zl->maxlvl); i++)
ajs274a4a42004-12-07 15:39:31 +0000685 zl->maxlvl[i] = ZLOG_DISABLED;
686 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
687 zl->default_lvl = LOG_DEBUG;
688
paul718e3742002-12-13 20:15:29 +0000689 openlog (progname, syslog_flags, zl->facility);
690
691 return zl;
692}
693
694void
695closezlog (struct zlog *zl)
696{
697 closelog();
Chris Caputo228da422009-07-18 05:44:03 +0000698
699 if (zl->fp != NULL)
700 fclose (zl->fp);
paul718e3742002-12-13 20:15:29 +0000701
Tom Goff7e69d992010-11-10 13:01:17 -0800702 if (zl->filename != NULL)
703 free (zl->filename);
704
paul718e3742002-12-13 20:15:29 +0000705 XFREE (MTYPE_ZLOG, zl);
706}
707
708/* Called from command.c. */
709void
ajs274a4a42004-12-07 15:39:31 +0000710zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000711{
712 if (zl == NULL)
713 zl = zlog_default;
714
ajs274a4a42004-12-07 15:39:31 +0000715 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000716}
717
718int
ajs274a4a42004-12-07 15:39:31 +0000719zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000720{
721 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000722 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000723
724 /* There is opend file. */
725 zlog_reset_file (zl);
726
727 /* Set default zl. */
728 if (zl == NULL)
729 zl = zlog_default;
730
731 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000732 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000733 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000734 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000735 if (fp == NULL)
736 return 0;
paul718e3742002-12-13 20:15:29 +0000737
738 /* Set flags. */
739 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000740 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000741 zl->fp = fp;
ajsc4c7d0c2005-02-03 19:22:05 +0000742 logfile_fd = fileno(fp);
paul718e3742002-12-13 20:15:29 +0000743
744 return 1;
745}
746
747/* Reset opend file. */
748int
749zlog_reset_file (struct zlog *zl)
750{
751 if (zl == NULL)
752 zl = zlog_default;
753
paul718e3742002-12-13 20:15:29 +0000754 if (zl->fp)
755 fclose (zl->fp);
756 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000757 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000758 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000759
760 if (zl->filename)
761 free (zl->filename);
762 zl->filename = NULL;
763
764 return 1;
765}
766
767/* Reopen log file. */
768int
769zlog_rotate (struct zlog *zl)
770{
ajs274a4a42004-12-07 15:39:31 +0000771 int level;
paul718e3742002-12-13 20:15:29 +0000772
773 if (zl == NULL)
774 zl = zlog_default;
775
776 if (zl->fp)
777 fclose (zl->fp);
778 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000779 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000780 level = zl->maxlvl[ZLOG_DEST_FILE];
781 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000782
783 if (zl->filename)
784 {
gdtaa593d52003-12-22 20:15:53 +0000785 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000786 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000787
788 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000789 zl->fp = fopen (zl->filename, "a");
790 save_errno = errno;
791 umask(oldumask);
792 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000793 {
ajs274a4a42004-12-07 15:39:31 +0000794 zlog_err("Log rotate failed: cannot open file %s for append: %s",
795 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000796 return -1;
797 }
ajsc4c7d0c2005-02-03 19:22:05 +0000798 logfile_fd = fileno(zl->fp);
ajs274a4a42004-12-07 15:39:31 +0000799 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000800 }
801
802 return 1;
803}
David Lamparter6b0655a2014-06-04 06:53:35 +0200804
paul718e3742002-12-13 20:15:29 +0000805/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000806const char *
Stephen Hemminger1423c802008-08-14 17:59:25 +0100807lookup (const struct message *mes, int key)
paul718e3742002-12-13 20:15:29 +0000808{
Stephen Hemminger1423c802008-08-14 17:59:25 +0100809 const struct message *pnt;
paul718e3742002-12-13 20:15:29 +0000810
811 for (pnt = mes; pnt->key != 0; pnt++)
812 if (pnt->key == key)
813 return pnt->str;
814
815 return "";
816}
817
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000818/* Older/faster version of message lookup function, but requires caller to pass
Paul Jakma11486b52008-02-28 23:26:02 +0000819 * in the array size (instead of relying on a 0 key to terminate the search).
820 *
821 * The return value is the message string if found, or the 'none' pointer
822 * provided otherwise.
823 */
hasso8c328f12004-10-05 21:01:23 +0000824const char *
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400825mes_lookup (const struct message *meslist, int max, int index,
826 const char *none, const char *mesname)
paul718e3742002-12-13 20:15:29 +0000827{
Paul Jakma11486b52008-02-28 23:26:02 +0000828 int pos = index - meslist[0].key;
829
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000830 /* first check for best case: index is in range and matches the key
Paul Jakma11486b52008-02-28 23:26:02 +0000831 * value in that slot.
832 * NB: key numbering might be offset from 0. E.g. protocol constants
833 * often start at 1.
834 */
835 if ((pos >= 0) && (pos < max)
836 && (meslist[pos].key == index))
837 return meslist[pos].str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000838
839 /* fall back to linear search */
840 {
841 int i;
842
843 for (i = 0; i < max; i++, meslist++)
844 {
845 if (meslist->key == index)
846 {
Paul Jakma11486b52008-02-28 23:26:02 +0000847 const char *str = (meslist->str ? meslist->str : none);
848
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400849 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
850 index, str, mesname, i, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000851 return str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000852 }
853 }
854 }
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400855 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000856 assert (none);
857 return none;
paul718e3742002-12-13 20:15:29 +0000858}
ajsca359762004-11-19 23:40:16 +0000859
860/* Wrapper around strerror to handle case where it returns NULL. */
861const char *
862safe_strerror(int errnum)
863{
864 const char *s = strerror(errnum);
865 return (s != NULL) ? s : "Unknown error";
866}
ajsf52d13c2005-10-01 17:38:06 +0000867
Paul Jakmad6d672a2006-05-15 16:56:51 +0000868#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
869static const struct zebra_desc_table command_types[] = {
870 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
871 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
872 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
873 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
874 DESC_ENTRY (ZEBRA_INTERFACE_UP),
875 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
876 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
877 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
878 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
879 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
880 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
881 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
882 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
883 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
884 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
885 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
886 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
887 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
888 DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
889 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
890 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
891 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
Denis Ovsienko4c783762012-01-21 22:50:19 +0400892 DESC_ENTRY (ZEBRA_HELLO),
Paul Jakmad6d672a2006-05-15 16:56:51 +0000893};
894#undef DESC_ENTRY
895
896static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
897
898static const struct zebra_desc_table *
ajsf52d13c2005-10-01 17:38:06 +0000899zroute_lookup(u_int zroute)
900{
ajsf52d13c2005-10-01 17:38:06 +0000901 u_int i;
902
Balaji.G837d16c2012-09-26 14:09:10 +0530903 if (zroute >= array_size(route_types))
ajsf52d13c2005-10-01 17:38:06 +0000904 {
905 zlog_err("unknown zebra route type: %u", zroute);
906 return &unknown;
907 }
Paul Jakmad6d672a2006-05-15 16:56:51 +0000908 if (zroute == route_types[zroute].type)
ajsf52d13c2005-10-01 17:38:06 +0000909 return &route_types[zroute];
Balaji.G837d16c2012-09-26 14:09:10 +0530910 for (i = 0; i < array_size(route_types); i++)
ajsf52d13c2005-10-01 17:38:06 +0000911 {
Paul Jakmad6d672a2006-05-15 16:56:51 +0000912 if (zroute == route_types[i].type)
ajsf52d13c2005-10-01 17:38:06 +0000913 {
914 zlog_warn("internal error: route type table out of order "
915 "while searching for %u, please notify developers", zroute);
916 return &route_types[i];
917 }
918 }
919 zlog_err("internal error: cannot find route type %u in table!", zroute);
920 return &unknown;
921}
922
923const char *
924zebra_route_string(u_int zroute)
925{
926 return zroute_lookup(zroute)->string;
927}
928
929char
930zebra_route_char(u_int zroute)
931{
932 return zroute_lookup(zroute)->chr;
933}
Paul Jakmad6d672a2006-05-15 16:56:51 +0000934
935const char *
936zserv_command_string (unsigned int command)
937{
Balaji.G837d16c2012-09-26 14:09:10 +0530938 if (command >= array_size(command_types))
Paul Jakmad6d672a2006-05-15 16:56:51 +0000939 {
940 zlog_err ("unknown zserv command type: %u", command);
941 return unknown.string;
942 }
943 return command_types[command].string;
944}
Paul Jakma7514fb72007-05-02 16:05:35 +0000945
Paul Jakma7514fb72007-05-02 16:05:35 +0000946int
947proto_name2num(const char *s)
948{
949 unsigned i;
950
Balaji.G837d16c2012-09-26 14:09:10 +0530951 for (i=0; i<array_size(route_types); ++i)
Paul Jakma7514fb72007-05-02 16:05:35 +0000952 if (strcasecmp(s, route_types[i].string) == 0)
953 return route_types[i].type;
954 return -1;
955}
David Lampartere0ca5fd2009-09-16 01:52:42 +0200956
David Lampartere0ca5fd2009-09-16 01:52:42 +0200957int
958proto_redistnum(int afi, const char *s)
959{
960 if (! s)
961 return -1;
962
963 if (afi == AFI_IP)
964 {
965 if (strncmp (s, "k", 1) == 0)
966 return ZEBRA_ROUTE_KERNEL;
967 else if (strncmp (s, "c", 1) == 0)
968 return ZEBRA_ROUTE_CONNECT;
969 else if (strncmp (s, "s", 1) == 0)
970 return ZEBRA_ROUTE_STATIC;
971 else if (strncmp (s, "r", 1) == 0)
972 return ZEBRA_ROUTE_RIP;
973 else if (strncmp (s, "o", 1) == 0)
974 return ZEBRA_ROUTE_OSPF;
975 else if (strncmp (s, "i", 1) == 0)
976 return ZEBRA_ROUTE_ISIS;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400977 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +0200978 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400979 else if (strncmp (s, "ba", 2) == 0)
980 return ZEBRA_ROUTE_BABEL;
David Lampartere0ca5fd2009-09-16 01:52:42 +0200981 }
982 if (afi == AFI_IP6)
983 {
984 if (strncmp (s, "k", 1) == 0)
985 return ZEBRA_ROUTE_KERNEL;
986 else if (strncmp (s, "c", 1) == 0)
987 return ZEBRA_ROUTE_CONNECT;
988 else if (strncmp (s, "s", 1) == 0)
989 return ZEBRA_ROUTE_STATIC;
990 else if (strncmp (s, "r", 1) == 0)
991 return ZEBRA_ROUTE_RIPNG;
992 else if (strncmp (s, "o", 1) == 0)
993 return ZEBRA_ROUTE_OSPF6;
994 else if (strncmp (s, "i", 1) == 0)
995 return ZEBRA_ROUTE_ISIS;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400996 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +0200997 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400998 else if (strncmp (s, "ba", 2) == 0)
999 return ZEBRA_ROUTE_BABEL;
David Lampartere0ca5fd2009-09-16 01:52:42 +02001000 }
1001 return -1;
1002}