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