]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Merge pull request #2034 from vincentbernat/fix/rfc8365-auto-rt
[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
41 #include "zebra/rib.h"
42 #include "zebra/zserv.h"
43 #include "zebra/debug.h"
44 #include "zebra/router-id.h"
45 #include "zebra/irdp.h"
46 #include "zebra/rtadv.h"
47 #include "zebra/zebra_ptm.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/redistribute.h"
50 #include "zebra/zebra_mpls.h"
51 #include "zebra/label_manager.h"
52 #include "zebra/zebra_netns_notify.h"
53 #include "zebra/zebra_rnh.h"
54
55 #define ZEBRA_PTM_SUPPORT
56
57 /* Zebra instance */
58 struct zebra_t zebrad = {
59 .rtm_table_default = 0,
60 .packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS,
61 };
62
63 /* process id. */
64 pid_t pid;
65
66 /* Pacify zclient.o in libfrr, which expects this variable. */
67 struct thread_master *master;
68
69 /* Route retain mode flag. */
70 int retain_mode = 0;
71
72 /* Allow non-quagga entities to delete quagga routes */
73 int allow_delete = 0;
74
75 /* Don't delete kernel route. */
76 int keep_kernel_mode = 0;
77
78 #ifdef HAVE_NETLINK
79 /* Receive buffer size for netlink socket */
80 uint32_t nl_rcvbufsize = 4194304;
81 #endif /* HAVE_NETLINK */
82
83 /* Command line options. */
84 struct option longopts[] = {{"batch", no_argument, NULL, 'b'},
85 {"allow_delete", no_argument, NULL, 'a'},
86 {"keep_kernel", no_argument, NULL, 'k'},
87 {"socket", required_argument, NULL, 'z'},
88 {"ecmp", required_argument, NULL, 'e'},
89 {"label_socket", no_argument, NULL, 'l'},
90 {"retain", no_argument, NULL, 'r'},
91 #ifdef HAVE_NETLINK
92 {"vrfwnetns", no_argument, NULL, 'n'},
93 {"nl-bufsize", required_argument, NULL, 's'},
94 #endif /* HAVE_NETLINK */
95 {0}};
96
97 zebra_capabilities_t _caps_p[] = {
98 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW,
99 };
100
101 /* zebra privileges to run with */
102 struct zebra_privs_t zserv_privs = {
103 #if defined(FRR_USER) && defined(FRR_GROUP)
104 .user = FRR_USER,
105 .group = FRR_GROUP,
106 #endif
107 #ifdef VTY_GROUP
108 .vty_group = VTY_GROUP,
109 #endif
110 .caps_p = _caps_p,
111 .cap_num_p = array_size(_caps_p),
112 .cap_num_i = 0};
113
114 unsigned int multipath_num = MULTIPATH_NUM;
115
116 /* SIGHUP handler. */
117 static void sighup(void)
118 {
119 zlog_info("SIGHUP received");
120
121 /* Reload of config file. */
122 ;
123 }
124
125 /* SIGINT handler. */
126 static void sigint(void)
127 {
128 struct vrf *vrf;
129 struct zebra_vrf *zvrf;
130
131 zlog_notice("Terminating on signal");
132
133 frr_early_fini();
134
135 list_delete_all_node(zebrad.client_list);
136 zebra_ptm_finish();
137
138 if (retain_mode)
139 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
140 zvrf = vrf->info;
141 if (zvrf)
142 SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
143 }
144 if (zebrad.lsp_process_q)
145 work_queue_free_and_null(&zebrad.lsp_process_q);
146 vrf_terminate();
147
148 ns_walk_func(zebra_ns_disabled);
149 zebra_ns_notify_close();
150
151 access_list_reset();
152 prefix_list_reset();
153 route_map_finish();
154
155 list_delete_and_null(&zebrad.client_list);
156 work_queue_free_and_null(&zebrad.ribq);
157 meta_queue_free(zebrad.mq);
158
159 frr_fini();
160 exit(0);
161 }
162
163 /* SIGUSR1 handler. */
164 static void sigusr1(void)
165 {
166 zlog_rotate();
167 }
168
169 struct quagga_signal_t zebra_signals[] = {
170 {
171 .signal = SIGHUP,
172 .handler = &sighup,
173 },
174 {
175 .signal = SIGUSR1,
176 .handler = &sigusr1,
177 },
178 {
179 .signal = SIGINT,
180 .handler = &sigint,
181 },
182 {
183 .signal = SIGTERM,
184 .handler = &sigint,
185 },
186 };
187
188 FRR_DAEMON_INFO(
189 zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
190
191 .proghelp =
192 "Daemon which manages kernel routing table management "
193 "and\nredistribution between different routing protocols.",
194
195 .signals = zebra_signals, .n_signals = array_size(zebra_signals),
196
197 .privs = &zserv_privs, )
198
199 /* Main startup routine. */
200 int main(int argc, char **argv)
201 {
202 // int batch_mode = 0;
203 char *zserv_path = NULL;
204 /* Socket to external label manager */
205 char *lblmgr_path = NULL;
206 struct sockaddr_storage dummy;
207 socklen_t dummylen;
208 #if defined(HANDLE_ZAPI_FUZZING)
209 char *fuzzing = NULL;
210 #endif
211
212 vrf_configure_backend(VRF_BACKEND_VRF_LITE);
213 logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS);
214
215 frr_preinit(&zebra_di, argc, argv);
216
217 frr_opt_add(
218 "bakz:e:l:r"
219 #ifdef HAVE_NETLINK
220 "s:n"
221 #endif
222 #if defined(HANDLE_ZAPI_FUZZING)
223 "c:"
224 #endif
225 ,
226 longopts,
227 " -b, --batch Runs in batch mode\n"
228 " -a, --allow_delete Allow other processes to delete zebra routes\n"
229 " -z, --socket Set path of zebra socket\n"
230 " -e, --ecmp Specify ECMP to use.\n"
231 " -l, --label_socket Socket to external label manager\n"
232 " -k, --keep_kernel Don't delete old routes which installed by zebra.\n"
233 " -r, --retain When program terminates, retain added route by zebra.\n"
234 #ifdef HAVE_NETLINK
235 " -n, --vrfwnetns Set VRF with NetNS\n"
236 " -s, --nl-bufsize Set netlink receive buffer size\n"
237 #endif /* HAVE_NETLINK */
238 #if defined(HANDLE_ZAPI_FUZZING)
239 " -c <file> Bypass normal startup use this file for tetsting of zapi"
240 #endif
241 );
242
243 while (1) {
244 int opt = frr_getopt(argc, argv, NULL);
245
246 if (opt == EOF)
247 break;
248
249 switch (opt) {
250 case 0:
251 break;
252 case 'b':
253 // batch_mode = 1;
254 break;
255 case 'a':
256 allow_delete = 1;
257 break;
258 case 'k':
259 keep_kernel_mode = 1;
260 break;
261 case 'e':
262 multipath_num = atoi(optarg);
263 if (multipath_num > MULTIPATH_NUM
264 || multipath_num <= 0) {
265 zlog_err(
266 "Multipath Number specified must be less than %d and greater than 0",
267 MULTIPATH_NUM);
268 return 1;
269 }
270 break;
271 case 'z':
272 zserv_path = optarg;
273 if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
274 fprintf(stderr,
275 "Invalid zserv socket path: %s\n",
276 optarg);
277 exit(1);
278 }
279 break;
280 case 'l':
281 lblmgr_path = optarg;
282 break;
283 case 'r':
284 retain_mode = 1;
285 break;
286 #ifdef HAVE_NETLINK
287 case 's':
288 nl_rcvbufsize = atoi(optarg);
289 break;
290 case 'n':
291 vrf_configure_backend(VRF_BACKEND_NETNS);
292 logicalrouter_configure_backend(
293 LOGICALROUTER_BACKEND_OFF);
294 break;
295 #endif /* HAVE_NETLINK */
296 #if defined(HANDLE_ZAPI_FUZZING)
297 case 'c':
298 fuzzing = optarg;
299 break;
300 #endif
301 default:
302 frr_help_exit(1);
303 break;
304 }
305 }
306
307 vty_config_lockless();
308 zebrad.master = frr_init();
309
310 /* Zebra related initialize. */
311 zserv_init();
312 rib_init();
313 zebra_if_init();
314 zebra_debug_init();
315 router_id_cmd_init();
316
317 /*
318 * Initialize NS( and implicitly the VRF module), and make kernel
319 * routing socket. */
320 zebra_ns_init();
321
322 zebra_vty_init();
323 access_list_init();
324 prefix_list_init();
325 #if defined(HAVE_RTADV)
326 rtadv_cmd_init();
327 #endif
328 /* PTM socket */
329 #ifdef ZEBRA_PTM_SUPPORT
330 zebra_ptm_init();
331 #endif
332
333 zebra_mpls_init();
334 zebra_mpls_vty_init();
335 zebra_pw_vty_init();
336
337 /* For debug purpose. */
338 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
339
340 #if defined(HANDLE_ZAPI_FUZZING)
341 if (fuzzing) {
342 zserv_read_file(fuzzing);
343 exit(0);
344 }
345 #endif
346
347 /* Process the configuration file. Among other configuration
348 * directives we can meet those installing static routes. Such
349 * requests will not be executed immediately, but queued in
350 * zebra->ribq structure until we enter the main execution loop.
351 * The notifications from kernel will show originating PID equal
352 * to that after daemon() completes (if ever called).
353 */
354 frr_config_fork();
355
356 /* After we have successfully acquired the pidfile, we can be sure
357 * about being the only copy of zebra process, which is submitting
358 * changes to the FIB.
359 * Clean up zebra-originated routes. The requests will be sent to OS
360 * immediately, so originating PID in notifications from kernel
361 * will be equal to the current getpid(). To know about such routes,
362 * we have to have route_read() called before.
363 */
364 if (!keep_kernel_mode)
365 rib_sweep_route();
366
367 /* Needed for BSD routing socket. */
368 pid = getpid();
369
370 /* This must be done only after locking pidfile (bug #403). */
371 zebra_zserv_socket_init(zserv_path);
372
373 /* Init label manager */
374 label_manager_init(lblmgr_path);
375
376 /* RNH init */
377 zebra_rnh_init();
378
379 frr_run(zebrad.master);
380
381 /* Not reached... */
382 return 0;
383 }