]>
Commit | Line | Data |
---|---|---|
4f04a76b DL |
1 | /* |
2 | * libfrr overall management functions | |
3 | * | |
4 | * Copyright (C) 2016 David Lamparter for NetDEF, Inc. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the Free | |
8 | * Software Foundation; either version 2 of the License, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
14 | * more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
4f04a76b DL |
19 | */ |
20 | ||
21 | #include <zebra.h> | |
689f5a8c | 22 | #include <sys/un.h> |
4f04a76b DL |
23 | |
24 | #include "libfrr.h" | |
25 | #include "getopt.h" | |
26 | #include "vty.h" | |
27 | #include "command.h" | |
28 | #include "version.h" | |
857b5446 | 29 | #include "memory_vty.h" |
eb05883f | 30 | #include "zclient.h" |
cf7466ac | 31 | #include "log_int.h" |
30771d65 | 32 | #include "module.h" |
eb05883f | 33 | |
d62a17ae | 34 | DEFINE_HOOK(frr_late_init, (struct thread_master * tm), (tm)) |
a5b38c5b | 35 | |
eb05883f DL |
36 | const char frr_sysconfdir[] = SYSCONFDIR; |
37 | const char frr_vtydir[] = DAEMON_VTY_DIR; | |
80b4df3b | 38 | const char frr_moduledir[] = MODULE_PATH; |
eb05883f | 39 | |
4f138a3e DL |
40 | char frr_protoname[256] = "NONE"; |
41 | char frr_protonameinst[256] = "NONE"; | |
b85120bc | 42 | |
eb05883f | 43 | char config_default[256]; |
689f5a8c | 44 | char frr_zclientpath[256]; |
eb05883f DL |
45 | static char pidfile_default[256]; |
46 | static char vtypath_default[256]; | |
4f04a76b DL |
47 | |
48 | static char comb_optstr[256]; | |
49 | static struct option comb_lo[64]; | |
50 | static struct option *comb_next_lo = &comb_lo[0]; | |
51 | static char comb_helpstr[4096]; | |
52 | ||
53 | struct optspec { | |
54 | const char *optstr; | |
55 | const char *helpstr; | |
56 | const struct option *longopts; | |
57 | }; | |
58 | ||
59 | static void opt_extend(const struct optspec *os) | |
60 | { | |
61 | const struct option *lo; | |
62 | ||
63 | strcat(comb_optstr, os->optstr); | |
64 | strcat(comb_helpstr, os->helpstr); | |
65 | for (lo = os->longopts; lo->name; lo++) | |
66 | memcpy(comb_next_lo++, lo, sizeof(*lo)); | |
67 | } | |
68 | ||
69 | ||
80b4df3b MW |
70 | #define OPTION_VTYSOCK 1000 |
71 | #define OPTION_MODULEDIR 1002 | |
4f04a76b DL |
72 | |
73 | static const struct option lo_always[] = { | |
d62a17ae | 74 | {"help", no_argument, NULL, 'h'}, |
75 | {"version", no_argument, NULL, 'v'}, | |
76 | {"daemon", no_argument, NULL, 'd'}, | |
77 | {"module", no_argument, NULL, 'M'}, | |
78 | {"vty_socket", required_argument, NULL, OPTION_VTYSOCK}, | |
79 | {"moduledir", required_argument, NULL, OPTION_MODULEDIR}, | |
80 | {NULL}}; | |
4f04a76b | 81 | static const struct optspec os_always = { |
30771d65 | 82 | "hvdM:", |
4f04a76b DL |
83 | " -h, --help Display this help and exit\n" |
84 | " -v, --version Print program version\n" | |
eb05883f | 85 | " -d, --daemon Runs in daemon mode\n" |
30771d65 | 86 | " -M, --module Load specified module\n" |
80b4df3b MW |
87 | " --vty_socket Override vty socket path\n" |
88 | " --moduledir Override modules directory\n", | |
d62a17ae | 89 | lo_always}; |
4f04a76b DL |
90 | |
91 | ||
eb05883f | 92 | static const struct option lo_cfg_pid_dry[] = { |
d62a17ae | 93 | {"pid_file", required_argument, NULL, 'i'}, |
94 | {"config_file", required_argument, NULL, 'f'}, | |
95 | {"dryrun", no_argument, NULL, 'C'}, | |
96 | {NULL}}; | |
eb05883f DL |
97 | static const struct optspec os_cfg_pid_dry = { |
98 | "f:i:C", | |
99 | " -f, --config_file Set configuration file name\n" | |
100 | " -i, --pid_file Set process identifier file name\n" | |
101 | " -C, --dryrun Check configuration for validity and exit\n", | |
d62a17ae | 102 | lo_cfg_pid_dry}; |
eb05883f DL |
103 | |
104 | ||
105 | static const struct option lo_zclient[] = { | |
d62a17ae | 106 | {"socket", required_argument, NULL, 'z'}, |
107 | {NULL}}; | |
eb05883f | 108 | static const struct optspec os_zclient = { |
d62a17ae | 109 | "z:", " -z, --socket Set path of zebra socket\n", lo_zclient}; |
eb05883f DL |
110 | |
111 | ||
4f04a76b | 112 | static const struct option lo_vty[] = { |
d62a17ae | 113 | {"vty_addr", required_argument, NULL, 'A'}, |
114 | {"vty_port", required_argument, NULL, 'P'}, | |
115 | {NULL}}; | |
4f04a76b DL |
116 | static const struct optspec os_vty = { |
117 | "A:P:", | |
118 | " -A, --vty_addr Set vty's bind address\n" | |
119 | " -P, --vty_port Set vty's port number\n", | |
d62a17ae | 120 | lo_vty}; |
4f04a76b DL |
121 | |
122 | ||
d62a17ae | 123 | static const struct option lo_user[] = {{"user", required_argument, NULL, 'u'}, |
124 | {"group", required_argument, NULL, 'g'}, | |
125 | {NULL}}; | |
126 | static const struct optspec os_user = {"u:g:", | |
127 | " -u, --user User to run as\n" | |
128 | " -g, --group Group to run as\n", | |
129 | lo_user}; | |
4f04a76b DL |
130 | |
131 | ||
689f5a8c DL |
132 | bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len, |
133 | const char *path) | |
134 | { | |
135 | memset(sa, 0, sizeof(*sa)); | |
136 | ||
137 | if (!path) | |
138 | path = ZEBRA_SERV_PATH; | |
139 | ||
140 | if (!strncmp(path, ZAPI_TCP_PATHNAME, strlen(ZAPI_TCP_PATHNAME))) { | |
141 | int af; | |
142 | int port = ZEBRA_PORT; | |
143 | char *err = NULL; | |
144 | struct sockaddr_in *sin = NULL; | |
145 | struct sockaddr_in6 *sin6 = NULL; | |
146 | ||
147 | path += strlen(ZAPI_TCP_PATHNAME); | |
148 | ||
149 | switch (path[0]) { | |
150 | case '4': | |
151 | path++; | |
152 | af = AF_INET; | |
153 | break; | |
154 | case '6': | |
155 | path++; | |
156 | /* fallthrough */ | |
157 | default: | |
158 | af = AF_INET6; | |
159 | break; | |
160 | } | |
161 | ||
162 | switch (path[0]) { | |
163 | case '\0': | |
164 | break; | |
165 | case ':': | |
166 | path++; | |
167 | port = strtoul(path, &err, 10); | |
168 | if (*err || !*path) | |
169 | return false; | |
170 | break; | |
171 | default: | |
172 | return false; | |
173 | } | |
174 | ||
175 | sa->ss_family = af; | |
176 | switch (af) { | |
177 | case AF_INET: | |
178 | sin = (struct sockaddr_in *)sa; | |
179 | sin->sin_port = htons(port); | |
180 | sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
181 | *sa_len = sizeof(struct sockaddr_in); | |
182 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN | |
183 | sin->sin_len = *sa_len; | |
184 | #endif | |
185 | break; | |
186 | case AF_INET6: | |
187 | sin6 = (struct sockaddr_in6 *)sa; | |
188 | sin6->sin6_port = htons(port); | |
189 | inet_pton(AF_INET6, "::1", &sin6->sin6_addr); | |
190 | *sa_len = sizeof(struct sockaddr_in6); | |
191 | #ifdef SIN6_LEN | |
192 | sin6->sin6_len = *sa_len; | |
193 | #endif | |
194 | break; | |
195 | } | |
196 | } else { | |
197 | /* "sun" is a #define on solaris */ | |
198 | struct sockaddr_un *suna = (struct sockaddr_un *)sa; | |
199 | ||
200 | suna->sun_family = AF_UNIX; | |
201 | strlcpy(suna->sun_path, path, sizeof(suna->sun_path)); | |
202 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN | |
203 | *sa_len = suna->sun_len = SUN_LEN(suna); | |
204 | #else | |
205 | *sa_len = sizeof(suna->sun_family) + strlen(suna->sun_path); | |
206 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ | |
207 | #if 0 | |
208 | /* this is left here for future reference; Linux abstract | |
209 | * socket namespace support can be enabled by replacing | |
210 | * above #if 0 with #ifdef GNU_LINUX. | |
211 | * | |
212 | * THIS IS A SECURITY ISSUE, the abstract socket namespace | |
213 | * does not have user/group permission control on sockets. | |
214 | * we'd need to implement SCM_CREDENTIALS support first to | |
215 | * check that only proper users can connect to abstract | |
216 | * sockets. (same problem as tcp-zebra, except there is a | |
217 | * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.) | |
218 | */ | |
219 | if (suna->sun_path[0] == '@') | |
220 | suna->sun_path[0] = '\0'; | |
221 | #endif | |
222 | } | |
223 | return true; | |
224 | } | |
225 | ||
4f04a76b DL |
226 | static struct frr_daemon_info *di = NULL; |
227 | ||
228 | void frr_preinit(struct frr_daemon_info *daemon, int argc, char **argv) | |
229 | { | |
230 | di = daemon; | |
231 | ||
232 | /* basename(), opencoded. */ | |
233 | char *p = strrchr(argv[0], '/'); | |
234 | di->progname = p ? p + 1 : argv[0]; | |
235 | ||
236 | umask(0027); | |
237 | ||
238 | opt_extend(&os_always); | |
eb05883f DL |
239 | if (!(di->flags & FRR_NO_CFG_PID_DRY)) |
240 | opt_extend(&os_cfg_pid_dry); | |
4f04a76b DL |
241 | if (!(di->flags & FRR_NO_PRIVSEP)) |
242 | opt_extend(&os_user); | |
eb05883f DL |
243 | if (!(di->flags & FRR_NO_ZCLIENT)) |
244 | opt_extend(&os_zclient); | |
4f04a76b DL |
245 | if (!(di->flags & FRR_NO_TCPVTY)) |
246 | opt_extend(&os_vty); | |
eb05883f DL |
247 | |
248 | snprintf(config_default, sizeof(config_default), "%s/%s.conf", | |
d62a17ae | 249 | frr_sysconfdir, di->name); |
eb05883f | 250 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s.pid", |
d62a17ae | 251 | frr_vtydir, di->name); |
b85120bc DL |
252 | |
253 | strlcpy(frr_protoname, di->logname, sizeof(frr_protoname)); | |
254 | strlcpy(frr_protonameinst, di->logname, sizeof(frr_protonameinst)); | |
689f5a8c DL |
255 | |
256 | strlcpy(frr_zclientpath, ZEBRA_SERV_PATH, sizeof(frr_zclientpath)); | |
4f04a76b DL |
257 | } |
258 | ||
259 | void frr_opt_add(const char *optstr, const struct option *longopts, | |
d62a17ae | 260 | const char *helpstr) |
4f04a76b | 261 | { |
d62a17ae | 262 | const struct optspec main_opts = {optstr, helpstr, longopts}; |
4f04a76b DL |
263 | opt_extend(&main_opts); |
264 | } | |
265 | ||
266 | void frr_help_exit(int status) | |
267 | { | |
268 | FILE *target = status ? stderr : stdout; | |
269 | ||
270 | if (status != 0) | |
271 | fprintf(stderr, "Invalid options.\n\n"); | |
272 | ||
273 | if (di->printhelp) | |
274 | di->printhelp(target); | |
275 | else | |
276 | fprintf(target, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s", | |
d62a17ae | 277 | di->progname, di->proghelp, di->copyright ? "\n\n" : "", |
278 | di->copyright ? di->copyright : "", comb_helpstr); | |
4f04a76b DL |
279 | fprintf(target, "\nReport bugs to %s\n", FRR_BUG_ADDRESS); |
280 | exit(status); | |
281 | } | |
282 | ||
30771d65 DL |
283 | struct option_chain { |
284 | struct option_chain *next; | |
285 | const char *arg; | |
286 | }; | |
80b4df3b | 287 | |
30771d65 | 288 | static struct option_chain *modules = NULL, **modnext = &modules; |
4f04a76b DL |
289 | static int errors = 0; |
290 | ||
291 | static int frr_opt(int opt) | |
292 | { | |
293 | static int vty_port_set = 0; | |
294 | static int vty_addr_set = 0; | |
30771d65 | 295 | struct option_chain *oc; |
4f04a76b DL |
296 | char *err; |
297 | ||
298 | switch (opt) { | |
299 | case 'h': | |
300 | frr_help_exit(0); | |
301 | break; | |
302 | case 'v': | |
303 | print_version(di->progname); | |
304 | exit(0); | |
305 | break; | |
eb05883f DL |
306 | case 'd': |
307 | di->daemon_mode = 1; | |
308 | break; | |
30771d65 DL |
309 | case 'M': |
310 | oc = XMALLOC(MTYPE_TMP, sizeof(*oc)); | |
311 | oc->arg = optarg; | |
312 | oc->next = NULL; | |
313 | *modnext = oc; | |
314 | modnext = &oc->next; | |
315 | break; | |
eb05883f DL |
316 | case 'i': |
317 | if (di->flags & FRR_NO_CFG_PID_DRY) | |
318 | return 1; | |
319 | di->pid_file = optarg; | |
320 | break; | |
321 | case 'f': | |
322 | if (di->flags & FRR_NO_CFG_PID_DRY) | |
323 | return 1; | |
324 | di->config_file = optarg; | |
325 | break; | |
326 | case 'C': | |
327 | if (di->flags & FRR_NO_CFG_PID_DRY) | |
328 | return 1; | |
329 | di->dryrun = 1; | |
330 | break; | |
331 | case 'z': | |
332 | if (di->flags & FRR_NO_ZCLIENT) | |
333 | return 1; | |
689f5a8c | 334 | strlcpy(frr_zclientpath, optarg, sizeof(frr_zclientpath)); |
eb05883f | 335 | break; |
4f04a76b DL |
336 | case 'A': |
337 | if (di->flags & FRR_NO_TCPVTY) | |
338 | return 1; | |
339 | if (vty_addr_set) { | |
d62a17ae | 340 | fprintf(stderr, |
341 | "-A option specified more than once!\n"); | |
4f04a76b DL |
342 | errors++; |
343 | break; | |
344 | } | |
345 | vty_addr_set = 1; | |
346 | di->vty_addr = optarg; | |
347 | break; | |
348 | case 'P': | |
349 | if (di->flags & FRR_NO_TCPVTY) | |
350 | return 1; | |
351 | if (vty_port_set) { | |
d62a17ae | 352 | fprintf(stderr, |
353 | "-P option specified more than once!\n"); | |
4f04a76b DL |
354 | errors++; |
355 | break; | |
356 | } | |
357 | vty_port_set = 1; | |
358 | di->vty_port = strtoul(optarg, &err, 0); | |
359 | if (*err || !*optarg) { | |
d62a17ae | 360 | fprintf(stderr, |
361 | "invalid port number \"%s\" for -P option\n", | |
362 | optarg); | |
4f04a76b DL |
363 | errors++; |
364 | break; | |
365 | } | |
366 | break; | |
367 | case OPTION_VTYSOCK: | |
368 | if (di->vty_sock_path) { | |
d62a17ae | 369 | fprintf(stderr, |
370 | "--vty_socket option specified more than once!\n"); | |
4f04a76b DL |
371 | errors++; |
372 | break; | |
373 | } | |
374 | di->vty_sock_path = optarg; | |
375 | break; | |
80b4df3b MW |
376 | case OPTION_MODULEDIR: |
377 | if (di->module_path) { | |
d62a17ae | 378 | fprintf(stderr, |
379 | "----moduledir option specified more than once!\n"); | |
80b4df3b MW |
380 | errors++; |
381 | break; | |
382 | } | |
383 | di->module_path = optarg; | |
384 | break; | |
4f04a76b DL |
385 | case 'u': |
386 | if (di->flags & FRR_NO_PRIVSEP) | |
387 | return 1; | |
388 | di->privs->user = optarg; | |
389 | break; | |
390 | case 'g': | |
391 | if (di->flags & FRR_NO_PRIVSEP) | |
392 | return 1; | |
393 | di->privs->group = optarg; | |
394 | break; | |
395 | default: | |
396 | return 1; | |
397 | } | |
398 | return 0; | |
399 | } | |
400 | ||
d62a17ae | 401 | int frr_getopt(int argc, char *const argv[], int *longindex) |
4f04a76b DL |
402 | { |
403 | int opt; | |
404 | int lidx; | |
405 | ||
406 | comb_next_lo->name = NULL; | |
407 | ||
408 | do { | |
409 | opt = getopt_long(argc, argv, comb_optstr, comb_lo, &lidx); | |
410 | if (frr_opt(opt)) | |
411 | break; | |
412 | } while (opt != -1); | |
413 | ||
414 | if (opt == -1 && errors) | |
415 | frr_help_exit(1); | |
416 | if (longindex) | |
417 | *longindex = lidx; | |
418 | return opt; | |
419 | } | |
420 | ||
a5b38c5b | 421 | static struct thread_master *master; |
4f04a76b DL |
422 | struct thread_master *frr_init(void) |
423 | { | |
30771d65 DL |
424 | struct option_chain *oc; |
425 | struct frrmod_runtime *module; | |
426 | char moderr[256]; | |
80b4df3b MW |
427 | const char *dir; |
428 | dir = di->module_path ? di->module_path : frr_moduledir; | |
4f04a76b DL |
429 | |
430 | srandom(time(NULL)); | |
431 | ||
b85120bc | 432 | if (di->instance) |
d62a17ae | 433 | snprintf(frr_protonameinst, sizeof(frr_protonameinst), "%s[%u]", |
434 | di->logname, di->instance); | |
b85120bc | 435 | |
d62a17ae | 436 | openzlog(di->progname, di->logname, di->instance, |
437 | LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); | |
4f04a76b | 438 | #if defined(HAVE_CUMULUS) |
d62a17ae | 439 | zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
4f04a76b DL |
440 | #endif |
441 | ||
689f5a8c DL |
442 | if (!frr_zclient_addr(&zclient_addr, &zclient_addr_len, |
443 | frr_zclientpath)) { | |
444 | fprintf(stderr, "Invalid zserv socket path: %s\n", | |
445 | frr_zclientpath); | |
446 | exit(1); | |
447 | } | |
448 | ||
30771d65 DL |
449 | frrmod_init(di->module); |
450 | while (modules) { | |
451 | modules = (oc = modules)->next; | |
80b4df3b | 452 | module = frrmod_load(oc->arg, dir, moderr, sizeof(moderr)); |
30771d65 DL |
453 | if (!module) { |
454 | fprintf(stderr, "%s\n", moderr); | |
455 | exit(1); | |
456 | } | |
457 | XFREE(MTYPE_TMP, oc); | |
458 | } | |
459 | ||
4f04a76b DL |
460 | zprivs_init(di->privs); |
461 | ||
972a411c | 462 | master = thread_master_create(NULL); |
4f04a76b DL |
463 | signal_init(master, di->n_signals, di->signals); |
464 | ||
857b5446 DL |
465 | if (di->flags & FRR_LIMITED_CLI) |
466 | cmd_init(-1); | |
467 | else | |
468 | cmd_init(1); | |
469 | vty_init(master); | |
470 | memory_init(); | |
471 | ||
4f04a76b DL |
472 | return master; |
473 | } | |
474 | ||
eb05883f | 475 | void frr_config_fork(void) |
4f04a76b | 476 | { |
a5b38c5b DL |
477 | hook_call(frr_late_init, master); |
478 | ||
eb05883f | 479 | if (di->instance) { |
d62a17ae | 480 | snprintf(config_default, sizeof(config_default), |
481 | "%s/%s-%d.conf", frr_sysconfdir, di->name, | |
482 | di->instance); | |
483 | snprintf(pidfile_default, sizeof(pidfile_default), | |
484 | "%s/%s-%d.pid", frr_vtydir, di->name, di->instance); | |
eb05883f DL |
485 | } |
486 | ||
487 | vty_read_config(di->config_file, config_default); | |
488 | ||
489 | /* Don't start execution if we are in dry-run mode */ | |
490 | if (di->dryrun) | |
491 | exit(0); | |
492 | ||
493 | /* Daemonize. */ | |
d62a17ae | 494 | if (di->daemon_mode && daemon(0, 0) < 0) { |
eb05883f DL |
495 | zlog_err("Zebra daemon failed: %s", strerror(errno)); |
496 | exit(1); | |
497 | } | |
498 | ||
499 | if (!di->pid_file) | |
500 | di->pid_file = pidfile_default; | |
d62a17ae | 501 | pid_output(di->pid_file); |
eb05883f DL |
502 | } |
503 | ||
504 | void frr_vty_serv(void) | |
505 | { | |
d62a17ae | 506 | /* allow explicit override of vty_path in the future |
eb05883f DL |
507 | * (not currently set anywhere) */ |
508 | if (!di->vty_path) { | |
509 | const char *dir; | |
510 | dir = di->vty_sock_path ? di->vty_sock_path : frr_vtydir; | |
511 | ||
512 | if (di->instance) | |
513 | snprintf(vtypath_default, sizeof(vtypath_default), | |
d62a17ae | 514 | "%s/%s-%d.vty", dir, di->name, di->instance); |
eb05883f DL |
515 | else |
516 | snprintf(vtypath_default, sizeof(vtypath_default), | |
d62a17ae | 517 | "%s/%s.vty", dir, di->name); |
eb05883f DL |
518 | |
519 | di->vty_path = vtypath_default; | |
520 | } | |
521 | ||
522 | vty_serv_sock(di->vty_addr, di->vty_port, di->vty_path); | |
4f04a76b DL |
523 | } |
524 | ||
16077f2f DL |
525 | void frr_run(struct thread_master *master) |
526 | { | |
527 | char instanceinfo[64] = ""; | |
528 | ||
529 | frr_vty_serv(); | |
530 | ||
531 | if (di->instance) | |
532 | snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ", | |
d62a17ae | 533 | di->instance); |
534 | ||
535 | zlog_notice("%s %s starting: %svty@%d%s", di->name, FRR_VERSION, | |
536 | instanceinfo, di->vty_port, di->startinfo); | |
16077f2f DL |
537 | |
538 | struct thread thread; | |
539 | while (thread_fetch(master, &thread)) | |
540 | thread_call(&thread); | |
541 | } |