]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Refactor code for new fuzzing netlink flag
[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
142 zlog_notice("Terminating on signal");
143
144 frr_early_fini();
145
146 list_delete_all_node(zebrad.client_list);
147 zebra_ptm_finish();
148
149 if (retain_mode)
150 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
151 zvrf = vrf->info;
152 if (zvrf)
153 SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
154 }
155 if (zebrad.lsp_process_q)
156 work_queue_free_and_null(&zebrad.lsp_process_q);
157 vrf_terminate();
158
159 ns_walk_func(zebra_ns_disabled);
160 zebra_ns_notify_close();
161
162 access_list_reset();
163 prefix_list_reset();
164 route_map_finish();
165
166 list_delete_and_null(&zebrad.client_list);
167 work_queue_free_and_null(&zebrad.ribq);
168 meta_queue_free(zebrad.mq);
169
170 frr_fini();
171 exit(0);
172 }
173
174 /* SIGUSR1 handler. */
175 static void sigusr1(void)
176 {
177 zlog_rotate();
178 }
179
180 struct quagga_signal_t zebra_signals[] = {
181 {
182 .signal = SIGHUP,
183 .handler = &sighup,
184 },
185 {
186 .signal = SIGUSR1,
187 .handler = &sigusr1,
188 },
189 {
190 .signal = SIGINT,
191 .handler = &sigint,
192 },
193 {
194 .signal = SIGTERM,
195 .handler = &sigint,
196 },
197 };
198
199 FRR_DAEMON_INFO(
200 zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
201
202 .proghelp =
203 "Daemon which manages kernel routing table management "
204 "and\nredistribution between different routing protocols.",
205
206 .signals = zebra_signals, .n_signals = array_size(zebra_signals),
207
208 .privs = &zserv_privs, )
209
210 /* Main startup routine. */
211 int main(int argc, char **argv)
212 {
213 // int batch_mode = 0;
214 char *zserv_path = NULL;
215 /* Socket to external label manager */
216 char *lblmgr_path = NULL;
217 struct sockaddr_storage dummy;
218 socklen_t dummylen;
219 #if defined(HANDLE_ZAPI_FUZZING)
220 char *zapi_fuzzing = NULL;
221 #endif /* HANDLE_ZAPI_FUZZING */
222 #if defined(HANDLE_NETLINK_FUZZING)
223 char *netlink_fuzzing = NULL;
224 #endif /* HANDLE_NETLINK_FUZZING */
225
226 vrf_configure_backend(VRF_BACKEND_VRF_LITE);
227 logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS);
228
229 frr_preinit(&zebra_di, argc, argv);
230
231 frr_opt_add(
232 "bakz:e:l:r"
233 #ifdef HAVE_NETLINK
234 "s:n"
235 #endif
236 #if defined(HANDLE_ZAPI_FUZZING)
237 "c:"
238 #endif /* HANDLE_ZAPI_FUZZING */
239 #if defined(HANDLE_NETLINK_FUZZING)
240 "w:"
241 #endif /* HANDLE_NETLINK_FUZZING */
242 ,
243 longopts,
244 " -b, --batch Runs in batch mode\n"
245 " -a, --allow_delete Allow other processes to delete zebra routes\n"
246 " -z, --socket Set path of zebra socket\n"
247 " -e, --ecmp Specify ECMP to use.\n"
248 " -l, --label_socket Socket to external label manager\n"
249 " -k, --keep_kernel Don't delete old routes which were installed by zebra.\n"
250 " -r, --retain When program terminates, retain added route by zebra.\n"
251 #ifdef HAVE_NETLINK
252 " -n, --vrfwnetns Use NetNS as VRF backend\n"
253 " -s, --nl-bufsize Set netlink receive buffer size\n"
254 " --v6-rr-semantics Use v6 RR semantics\n"
255 #endif /* HAVE_NETLINK */
256 #if defined(HANDLE_ZAPI_FUZZING)
257 " -c <file> Bypass normal startup and use this file for testing of zapi\n"
258 #endif /* HANDLE_ZAPI_FUZZING */
259 #if defined(HANDLE_NETLINK_FUZZING)
260 " -w <file> Bypass normal startup and use this file for testing of netlink input\n"
261 #endif /* HANDLE_NETLINK_FUZZING */
262 );
263
264 while (1) {
265 int opt = frr_getopt(argc, argv, NULL);
266
267 if (opt == EOF)
268 break;
269
270 switch (opt) {
271 case 0:
272 break;
273 case 'b':
274 // batch_mode = 1;
275 break;
276 case 'a':
277 allow_delete = 1;
278 break;
279 case 'k':
280 keep_kernel_mode = 1;
281 break;
282 case 'e':
283 multipath_num = atoi(optarg);
284 if (multipath_num > MULTIPATH_NUM
285 || multipath_num <= 0) {
286 zlog_err(
287 "Multipath Number specified must be less than %d and greater than 0",
288 MULTIPATH_NUM);
289 return 1;
290 }
291 break;
292 case 'z':
293 zserv_path = optarg;
294 if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
295 fprintf(stderr,
296 "Invalid zserv socket path: %s\n",
297 optarg);
298 exit(1);
299 }
300 break;
301 case 'l':
302 lblmgr_path = optarg;
303 break;
304 case 'r':
305 retain_mode = 1;
306 break;
307 #ifdef HAVE_NETLINK
308 case 's':
309 nl_rcvbufsize = atoi(optarg);
310 break;
311 case 'n':
312 vrf_configure_backend(VRF_BACKEND_NETNS);
313 logicalrouter_configure_backend(
314 LOGICALROUTER_BACKEND_OFF);
315 break;
316 case OPTION_V6_RR_SEMANTICS:
317 v6_rr_semantics = true;
318 break;
319 #endif /* HAVE_NETLINK */
320 #if defined(HANDLE_ZAPI_FUZZING)
321 case 'c':
322 zapi_fuzzing = optarg;
323 break;
324 #endif /* HANDLE_ZAPI_FUZZING */
325 #if defined(HANDLE_NETLINK_FUZZING)
326 case 'w':
327 netlink_fuzzing = optarg;
328 /* This ensures we are aren't writing any of the
329 * startup netlink messages that happen when we
330 * just want to read.
331 */
332 netlink_read = true;
333 break;
334 #endif /* HANDLE_NETLINK_FUZZING */
335 default:
336 frr_help_exit(1);
337 break;
338 }
339 }
340
341 vty_config_lockless();
342 zebrad.master = frr_init();
343
344 /* Zebra related initialize. */
345 zserv_init();
346 rib_init();
347 zebra_if_init();
348 zebra_debug_init();
349 router_id_cmd_init();
350
351 /*
352 * Initialize NS( and implicitly the VRF module), and make kernel
353 * routing socket. */
354 zebra_ns_init();
355
356 zebra_vty_init();
357 access_list_init();
358 prefix_list_init();
359 #if defined(HAVE_RTADV)
360 rtadv_cmd_init();
361 #endif
362 /* PTM socket */
363 #ifdef ZEBRA_PTM_SUPPORT
364 zebra_ptm_init();
365 #endif
366
367 zebra_mpls_init();
368 zebra_mpls_vty_init();
369 zebra_pw_vty_init();
370 zebra_pbr_init();
371
372 /* For debug purpose. */
373 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
374
375 /* Process the configuration file. Among other configuration
376 * directives we can meet those installing static routes. Such
377 * requests will not be executed immediately, but queued in
378 * zebra->ribq structure until we enter the main execution loop.
379 * The notifications from kernel will show originating PID equal
380 * to that after daemon() completes (if ever called).
381 */
382 frr_config_fork();
383
384 /* After we have successfully acquired the pidfile, we can be sure
385 * about being the only copy of zebra process, which is submitting
386 * changes to the FIB.
387 * Clean up zebra-originated routes. The requests will be sent to OS
388 * immediately, so originating PID in notifications from kernel
389 * will be equal to the current getpid(). To know about such routes,
390 * we have to have route_read() called before.
391 */
392 if (!keep_kernel_mode)
393 rib_sweep_route();
394
395 /* Needed for BSD routing socket. */
396 pid = getpid();
397
398 /* Intialize pthread library */
399 frr_pthread_init();
400
401 /* Start Zebra API server */
402 zserv_start(zserv_path);
403
404 /* Init label manager */
405 label_manager_init(lblmgr_path);
406
407 /* RNH init */
408 zebra_rnh_init();
409
410 #if defined(HANDLE_ZAPI_FUZZING)
411 if (zapi_fuzzing) {
412 zserv_read_file(zapi_fuzzing);
413 exit(0);
414 }
415 #endif /* HANDLE_ZAPI_FUZZING */
416 #if defined(HANDLE_NETLINK_FUZZING)
417 if (netlink_fuzzing) {
418 netlink_read_init(netlink_fuzzing);
419 exit(0);
420 }
421 #endif /* HANDLE_NETLINK_FUZZING */
422
423
424 frr_run(zebrad.master);
425
426 /* Not reached... */
427 return 0;
428 }