]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Merge pull request #2122 from donaldsharp/zebra_nhs
[mirror_frr.git] / zebra / main.c
1 /* zebra daemon main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra 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
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
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
19 */
20
21 #include <zebra.h>
22
23 #include <lib/version.h>
24 #include "getopt.h"
25 #include "command.h"
26 #include "thread.h"
27 #include "filter.h"
28 #include "memory.h"
29 #include "zebra_memory.h"
30 #include "memory_vty.h"
31 #include "prefix.h"
32 #include "log.h"
33 #include "plist.h"
34 #include "privs.h"
35 #include "sigevent.h"
36 #include "vrf.h"
37 #include "logicalrouter.h"
38 #include "libfrr.h"
39 #include "routemap.h"
40 #include "frr_pthread.h"
41
42 #include "zebra/rib.h"
43 #include "zebra/zserv.h"
44 #include "zebra/debug.h"
45 #include "zebra/router-id.h"
46 #include "zebra/irdp.h"
47 #include "zebra/rtadv.h"
48 #include "zebra/zebra_ptm.h"
49 #include "zebra/zebra_ns.h"
50 #include "zebra/redistribute.h"
51 #include "zebra/zebra_mpls.h"
52 #include "zebra/label_manager.h"
53 #include "zebra/zebra_netns_notify.h"
54 #include "zebra/zebra_rnh.h"
55 #include "zebra/zebra_pbr.h"
56
57 #if defined(HANDLE_NETLINK_FUZZING)
58 #include "zebra/kernel_netlink.h"
59 #endif /* HANDLE_NETLINK_FUZZING */
60
61 #define ZEBRA_PTM_SUPPORT
62
63 /* Zebra instance */
64 struct zebra_t zebrad = {
65 .rtm_table_default = 0,
66 .packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS,
67 };
68
69 /* process id. */
70 pid_t pid;
71
72 /* Pacify zclient.o in libfrr, which expects this variable. */
73 struct thread_master *master;
74
75 /* Route retain mode flag. */
76 int retain_mode = 0;
77
78 /* Allow non-quagga entities to delete quagga routes */
79 int allow_delete = 0;
80
81 /* Don't delete kernel route. */
82 int keep_kernel_mode = 0;
83
84 bool v6_rr_semantics = false;
85
86 #ifdef HAVE_NETLINK
87 /* Receive buffer size for netlink socket */
88 uint32_t nl_rcvbufsize = 4194304;
89 #endif /* HAVE_NETLINK */
90
91 #define OPTION_V6_RR_SEMANTICS 2000
92 /* Command line options. */
93 struct option longopts[] = {
94 {"batch", no_argument, NULL, 'b'},
95 {"allow_delete", no_argument, NULL, 'a'},
96 {"keep_kernel", no_argument, NULL, 'k'},
97 {"socket", required_argument, NULL, 'z'},
98 {"ecmp", required_argument, NULL, 'e'},
99 {"label_socket", no_argument, NULL, 'l'},
100 {"retain", no_argument, NULL, 'r'},
101 #ifdef HAVE_NETLINK
102 {"vrfwnetns", no_argument, NULL, 'n'},
103 {"nl-bufsize", required_argument, NULL, 's'},
104 {"v6-rr-semantics", no_argument, NULL, OPTION_V6_RR_SEMANTICS},
105 #endif /* HAVE_NETLINK */
106 {0}};
107
108 zebra_capabilities_t _caps_p[] = {
109 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW,
110 };
111
112 /* zebra privileges to run with */
113 struct zebra_privs_t zserv_privs = {
114 #if defined(FRR_USER) && defined(FRR_GROUP)
115 .user = FRR_USER,
116 .group = FRR_GROUP,
117 #endif
118 #ifdef VTY_GROUP
119 .vty_group = VTY_GROUP,
120 #endif
121 .caps_p = _caps_p,
122 .cap_num_p = array_size(_caps_p),
123 .cap_num_i = 0};
124
125 unsigned int multipath_num = MULTIPATH_NUM;
126
127 /* SIGHUP handler. */
128 static void sighup(void)
129 {
130 zlog_info("SIGHUP received");
131
132 /* Reload of config file. */
133 ;
134 }
135
136 /* SIGINT handler. */
137 static void sigint(void)
138 {
139 struct vrf *vrf;
140 struct zebra_vrf *zvrf;
141 struct listnode *ln, *nn;
142 struct zserv *client;
143
144 zlog_notice("Terminating on signal");
145
146 frr_early_fini();
147
148 for (ALL_LIST_ELEMENTS(zebrad.client_list, ln, nn, client))
149 zserv_close_client(client);
150
151 list_delete_all_node(zebrad.client_list);
152 zebra_ptm_finish();
153
154 if (retain_mode)
155 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
156 zvrf = vrf->info;
157 if (zvrf)
158 SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
159 }
160 if (zebrad.lsp_process_q)
161 work_queue_free_and_null(&zebrad.lsp_process_q);
162 vrf_terminate();
163
164 ns_walk_func(zebra_ns_disabled);
165 zebra_ns_notify_close();
166
167 access_list_reset();
168 prefix_list_reset();
169 route_map_finish();
170
171 list_delete_and_null(&zebrad.client_list);
172 work_queue_free_and_null(&zebrad.ribq);
173 meta_queue_free(zebrad.mq);
174
175 frr_fini();
176 exit(0);
177 }
178
179 /* SIGUSR1 handler. */
180 static void sigusr1(void)
181 {
182 zlog_rotate();
183 }
184
185 struct quagga_signal_t zebra_signals[] = {
186 {
187 .signal = SIGHUP,
188 .handler = &sighup,
189 },
190 {
191 .signal = SIGUSR1,
192 .handler = &sigusr1,
193 },
194 {
195 .signal = SIGINT,
196 .handler = &sigint,
197 },
198 {
199 .signal = SIGTERM,
200 .handler = &sigint,
201 },
202 };
203
204 FRR_DAEMON_INFO(
205 zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
206
207 .proghelp =
208 "Daemon which manages kernel routing table management "
209 "and\nredistribution between different routing protocols.",
210
211 .signals = zebra_signals, .n_signals = array_size(zebra_signals),
212
213 .privs = &zserv_privs, )
214
215 /* Main startup routine. */
216 int main(int argc, char **argv)
217 {
218 // int batch_mode = 0;
219 char *zserv_path = NULL;
220 /* Socket to external label manager */
221 char *lblmgr_path = NULL;
222 struct sockaddr_storage dummy;
223 socklen_t dummylen;
224 #if defined(HANDLE_ZAPI_FUZZING)
225 char *zapi_fuzzing = NULL;
226 #endif /* HANDLE_ZAPI_FUZZING */
227 #if defined(HANDLE_NETLINK_FUZZING)
228 char *netlink_fuzzing = NULL;
229 #endif /* HANDLE_NETLINK_FUZZING */
230
231 vrf_configure_backend(VRF_BACKEND_VRF_LITE);
232 logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS);
233
234 frr_preinit(&zebra_di, argc, argv);
235
236 frr_opt_add(
237 "bakz:e:l:r"
238 #ifdef HAVE_NETLINK
239 "s:n"
240 #endif
241 #if defined(HANDLE_ZAPI_FUZZING)
242 "c:"
243 #endif /* HANDLE_ZAPI_FUZZING */
244 #if defined(HANDLE_NETLINK_FUZZING)
245 "w:"
246 #endif /* HANDLE_NETLINK_FUZZING */
247 ,
248 longopts,
249 " -b, --batch Runs in batch mode\n"
250 " -a, --allow_delete Allow other processes to delete zebra routes\n"
251 " -z, --socket Set path of zebra socket\n"
252 " -e, --ecmp Specify ECMP to use.\n"
253 " -l, --label_socket Socket to external label manager\n"
254 " -k, --keep_kernel Don't delete old routes which were installed by zebra.\n"
255 " -r, --retain When program terminates, retain added route by zebra.\n"
256 #ifdef HAVE_NETLINK
257 " -n, --vrfwnetns Use NetNS as VRF backend\n"
258 " -s, --nl-bufsize Set netlink receive buffer size\n"
259 " --v6-rr-semantics Use v6 RR semantics\n"
260 #endif /* HAVE_NETLINK */
261 #if defined(HANDLE_ZAPI_FUZZING)
262 " -c <file> Bypass normal startup and use this file for testing of zapi\n"
263 #endif /* HANDLE_ZAPI_FUZZING */
264 #if defined(HANDLE_NETLINK_FUZZING)
265 " -w <file> Bypass normal startup and use this file for testing of netlink input\n"
266 #endif /* HANDLE_NETLINK_FUZZING */
267 );
268
269 while (1) {
270 int opt = frr_getopt(argc, argv, NULL);
271
272 if (opt == EOF)
273 break;
274
275 switch (opt) {
276 case 0:
277 break;
278 case 'b':
279 // batch_mode = 1;
280 break;
281 case 'a':
282 allow_delete = 1;
283 break;
284 case 'k':
285 keep_kernel_mode = 1;
286 break;
287 case 'e':
288 multipath_num = atoi(optarg);
289 if (multipath_num > MULTIPATH_NUM
290 || multipath_num <= 0) {
291 zlog_err(
292 "Multipath Number specified must be less than %d and greater than 0",
293 MULTIPATH_NUM);
294 return 1;
295 }
296 break;
297 case 'z':
298 zserv_path = optarg;
299 if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
300 fprintf(stderr,
301 "Invalid zserv socket path: %s\n",
302 optarg);
303 exit(1);
304 }
305 break;
306 case 'l':
307 lblmgr_path = optarg;
308 break;
309 case 'r':
310 retain_mode = 1;
311 break;
312 #ifdef HAVE_NETLINK
313 case 's':
314 nl_rcvbufsize = atoi(optarg);
315 break;
316 case 'n':
317 vrf_configure_backend(VRF_BACKEND_NETNS);
318 logicalrouter_configure_backend(
319 LOGICALROUTER_BACKEND_OFF);
320 break;
321 case OPTION_V6_RR_SEMANTICS:
322 v6_rr_semantics = true;
323 break;
324 #endif /* HAVE_NETLINK */
325 #if defined(HANDLE_ZAPI_FUZZING)
326 case 'c':
327 zapi_fuzzing = optarg;
328 break;
329 #endif /* HANDLE_ZAPI_FUZZING */
330 #if defined(HANDLE_NETLINK_FUZZING)
331 case 'w':
332 netlink_fuzzing = optarg;
333 /* This ensures we are aren't writing any of the
334 * startup netlink messages that happen when we
335 * just want to read.
336 */
337 netlink_read = true;
338 break;
339 #endif /* HANDLE_NETLINK_FUZZING */
340 default:
341 frr_help_exit(1);
342 break;
343 }
344 }
345
346 vty_config_lockless();
347 zebrad.master = frr_init();
348
349 /* Zebra related initialize. */
350 zserv_init();
351 rib_init();
352 zebra_if_init();
353 zebra_debug_init();
354 router_id_cmd_init();
355
356 /*
357 * Initialize NS( and implicitly the VRF module), and make kernel
358 * routing socket. */
359 zebra_ns_init();
360
361 zebra_vty_init();
362 access_list_init();
363 prefix_list_init();
364 #if defined(HAVE_RTADV)
365 rtadv_cmd_init();
366 #endif
367 /* PTM socket */
368 #ifdef ZEBRA_PTM_SUPPORT
369 zebra_ptm_init();
370 #endif
371
372 zebra_mpls_init();
373 zebra_mpls_vty_init();
374 zebra_pw_vty_init();
375 zebra_pbr_init();
376
377 /* For debug purpose. */
378 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
379
380 /* Process the configuration file. Among other configuration
381 * directives we can meet those installing static routes. Such
382 * requests will not be executed immediately, but queued in
383 * zebra->ribq structure until we enter the main execution loop.
384 * The notifications from kernel will show originating PID equal
385 * to that after daemon() completes (if ever called).
386 */
387 frr_config_fork();
388
389 /* After we have successfully acquired the pidfile, we can be sure
390 * about being the only copy of zebra process, which is submitting
391 * changes to the FIB.
392 * Clean up zebra-originated routes. The requests will be sent to OS
393 * immediately, so originating PID in notifications from kernel
394 * will be equal to the current getpid(). To know about such routes,
395 * we have to have route_read() called before.
396 */
397 if (!keep_kernel_mode)
398 rib_sweep_route();
399
400 /* Needed for BSD routing socket. */
401 pid = getpid();
402
403 /* Intialize pthread library */
404 frr_pthread_init();
405
406 /* Start Zebra API server */
407 zserv_start(zserv_path);
408
409 /* Init label manager */
410 label_manager_init(lblmgr_path);
411
412 /* RNH init */
413 zebra_rnh_init();
414
415 #if defined(HANDLE_ZAPI_FUZZING)
416 if (zapi_fuzzing) {
417 zserv_read_file(zapi_fuzzing);
418 exit(0);
419 }
420 #endif /* HANDLE_ZAPI_FUZZING */
421 #if defined(HANDLE_NETLINK_FUZZING)
422 if (netlink_fuzzing) {
423 netlink_read_init(netlink_fuzzing);
424 exit(0);
425 }
426 #endif /* HANDLE_NETLINK_FUZZING */
427
428
429 frr_run(zebrad.master);
430
431 /* Not reached... */
432 return 0;
433 }