]>
Commit | Line | Data |
---|---|---|
edd7c245 | 1 | /* zebra daemon main routine. |
718e3744 | 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 | * | |
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 | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
5e4fa164 | 23 | #include <lib/version.h> |
718e3744 | 24 | #include "getopt.h" |
25 | #include "command.h" | |
26 | #include "thread.h" | |
27 | #include "filter.h" | |
28 | #include "memory.h" | |
4a1ab8e4 | 29 | #include "zebra_memory.h" |
fc7948fa | 30 | #include "memory_vty.h" |
718e3744 | 31 | #include "prefix.h" |
32 | #include "log.h" | |
7514fb77 | 33 | #include "plist.h" |
edd7c245 | 34 | #include "privs.h" |
2d75d052 | 35 | #include "sigevent.h" |
b72ede27 | 36 | #include "vrf.h" |
736d41ad | 37 | #include "logicalrouter.h" |
4f04a76b | 38 | #include "libfrr.h" |
bf094f69 | 39 | #include "routemap.h" |
329e35da | 40 | #include "frr_pthread.h" |
718e3744 | 41 | |
42 | #include "zebra/rib.h" | |
43 | #include "zebra/zserv.h" | |
44 | #include "zebra/debug.h" | |
18a6dce6 | 45 | #include "zebra/router-id.h" |
ca776988 | 46 | #include "zebra/irdp.h" |
a1ac18c4 | 47 | #include "zebra/rtadv.h" |
244c1cdc | 48 | #include "zebra/zebra_ptm.h" |
fe18ee2d | 49 | #include "zebra/zebra_ns.h" |
e2b1be64 | 50 | #include "zebra/redistribute.h" |
7758e3f3 | 51 | #include "zebra/zebra_mpls.h" |
fea12efb | 52 | #include "zebra/label_manager.h" |
e27dec3c | 53 | #include "zebra/zebra_netns_notify.h" |
453844ab | 54 | #include "zebra/zebra_rnh.h" |
4c0ec639 | 55 | #include "zebra/zebra_pbr.h" |
244c1cdc | 56 | |
acfa8927 | 57 | #if defined(HANDLE_NETLINK_FUZZING) |
81a2f870 | 58 | #include "zebra/kernel_netlink.h" |
acfa8927 | 59 | #endif /* HANDLE_NETLINK_FUZZING */ |
81a2f870 | 60 | |
244c1cdc | 61 | #define ZEBRA_PTM_SUPPORT |
718e3744 | 62 | |
b21b19c5 | 63 | /* Zebra instance */ |
d62a17ae | 64 | struct zebra_t zebrad = { |
65 | .rtm_table_default = 0, | |
a37ef435 | 66 | .packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS, |
b21b19c5 | 67 | }; |
718e3744 | 68 | |
69 | /* process id. */ | |
718e3744 | 70 | pid_t pid; |
71 | ||
55c72803 | 72 | /* Pacify zclient.o in libfrr, which expects this variable. */ |
87efd646 | 73 | struct thread_master *master; |
74 | ||
718e3744 | 75 | /* Route retain mode flag. */ |
76 | int retain_mode = 0; | |
77 | ||
6baf7bb8 DS |
78 | /* Allow non-quagga entities to delete quagga routes */ |
79 | int allow_delete = 0; | |
80 | ||
718e3744 | 81 | /* Don't delete kernel route. */ |
82 | int keep_kernel_mode = 0; | |
83 | ||
6b093863 DS |
84 | bool v6_rr_semantics = false; |
85 | ||
c34b6b57 | 86 | #ifdef HAVE_NETLINK |
87 | /* Receive buffer size for netlink socket */ | |
d7c0a89a | 88 | uint32_t nl_rcvbufsize = 4194304; |
c34b6b57 | 89 | #endif /* HAVE_NETLINK */ |
90 | ||
6b093863 | 91 | #define OPTION_V6_RR_SEMANTICS 2000 |
718e3744 | 92 | /* Command line options. */ |
6b093863 DS |
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'}, | |
c34b6b57 | 101 | #ifdef HAVE_NETLINK |
6b093863 DS |
102 | {"vrfwnetns", no_argument, NULL, 'n'}, |
103 | {"nl-bufsize", required_argument, NULL, 's'}, | |
104 | {"v6-rr-semantics", no_argument, NULL, OPTION_V6_RR_SEMANTICS}, | |
c34b6b57 | 105 | #endif /* HAVE_NETLINK */ |
6b093863 | 106 | {0}}; |
718e3744 | 107 | |
d62a17ae | 108 | zebra_capabilities_t _caps_p[] = { |
9d303b37 | 109 | ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW, |
edd7c245 | 110 | }; |
111 | ||
112 | /* zebra privileges to run with */ | |
d62a17ae | 113 | struct zebra_privs_t zserv_privs = { |
b2f36157 | 114 | #if defined(FRR_USER) && defined(FRR_GROUP) |
d62a17ae | 115 | .user = FRR_USER, |
116 | .group = FRR_GROUP, | |
edd7c245 | 117 | #endif |
118 | #ifdef VTY_GROUP | |
d62a17ae | 119 | .vty_group = VTY_GROUP, |
edd7c245 | 120 | #endif |
d62a17ae | 121 | .caps_p = _caps_p, |
122 | .cap_num_p = array_size(_caps_p), | |
123 | .cap_num_i = 0}; | |
edd7c245 | 124 | |
37fe7731 DS |
125 | unsigned int multipath_num = MULTIPATH_NUM; |
126 | ||
718e3744 | 127 | /* SIGHUP handler. */ |
d62a17ae | 128 | static void sighup(void) |
718e3744 | 129 | { |
d62a17ae | 130 | zlog_info("SIGHUP received"); |
718e3744 | 131 | |
d62a17ae | 132 | /* Reload of config file. */ |
133 | ; | |
718e3744 | 134 | } |
135 | ||
136 | /* SIGINT handler. */ | |
d62a17ae | 137 | static void sigint(void) |
718e3744 | 138 | { |
d62a17ae | 139 | struct vrf *vrf; |
140 | struct zebra_vrf *zvrf; | |
f3e33b69 QY |
141 | struct listnode *ln, *nn; |
142 | struct zserv *client; | |
fe18ee2d | 143 | |
d62a17ae | 144 | zlog_notice("Terminating on signal"); |
718e3744 | 145 | |
03951374 | 146 | frr_early_fini(); |
718e3744 | 147 | |
f3e33b69 QY |
148 | for (ALL_LIST_ELEMENTS(zebrad.client_list, ln, nn, client)) |
149 | zserv_close_client(client); | |
150 | ||
d62a17ae | 151 | list_delete_all_node(zebrad.client_list); |
f88bd20c | 152 | zebra_ptm_finish(); |
d62a17ae | 153 | |
154 | if (retain_mode) | |
a2addae8 | 155 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { |
d62a17ae | 156 | zvrf = vrf->info; |
157 | if (zvrf) | |
158 | SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN); | |
159 | } | |
40289934 | 160 | if (zebrad.lsp_process_q) |
e208c8f9 | 161 | work_queue_free_and_null(&zebrad.lsp_process_q); |
d62a17ae | 162 | vrf_terminate(); |
163 | ||
ff705b15 | 164 | ns_walk_func(zebra_ns_disabled); |
e27dec3c | 165 | zebra_ns_notify_close(); |
d62a17ae | 166 | |
167 | access_list_reset(); | |
168 | prefix_list_reset(); | |
169 | route_map_finish(); | |
03951374 | 170 | |
affe9e99 | 171 | list_delete_and_null(&zebrad.client_list); |
e208c8f9 | 172 | work_queue_free_and_null(&zebrad.ribq); |
d62a17ae | 173 | meta_queue_free(zebrad.mq); |
d62a17ae | 174 | |
03951374 | 175 | frr_fini(); |
d62a17ae | 176 | exit(0); |
718e3744 | 177 | } |
178 | ||
179 | /* SIGUSR1 handler. */ | |
d62a17ae | 180 | static void sigusr1(void) |
718e3744 | 181 | { |
d62a17ae | 182 | zlog_rotate(); |
718e3744 | 183 | } |
184 | ||
d62a17ae | 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 | }, | |
2d75d052 | 202 | }; |
b72ede27 | 203 | |
d62a17ae | 204 | FRR_DAEMON_INFO( |
205 | zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT, | |
4f04a76b | 206 | |
d62a17ae | 207 | .proghelp = |
208 | "Daemon which manages kernel routing table management " | |
4f04a76b DL |
209 | "and\nredistribution between different routing protocols.", |
210 | ||
d62a17ae | 211 | .signals = zebra_signals, .n_signals = array_size(zebra_signals), |
4f04a76b | 212 | |
d62a17ae | 213 | .privs = &zserv_privs, ) |
4f04a76b | 214 | |
718e3744 | 215 | /* Main startup routine. */ |
d62a17ae | 216 | int main(int argc, char **argv) |
718e3744 | 217 | { |
d62a17ae | 218 | // int batch_mode = 0; |
219 | char *zserv_path = NULL; | |
220 | /* Socket to external label manager */ | |
221 | char *lblmgr_path = NULL; | |
689f5a8c DL |
222 | struct sockaddr_storage dummy; |
223 | socklen_t dummylen; | |
411314ed | 224 | #if defined(HANDLE_ZAPI_FUZZING) |
81a2f870 | 225 | char *zapi_fuzzing = NULL; |
acfa8927 SW |
226 | #endif /* HANDLE_ZAPI_FUZZING */ |
227 | #if defined(HANDLE_NETLINK_FUZZING) | |
81a2f870 | 228 | char *netlink_fuzzing = NULL; |
acfa8927 | 229 | #endif /* HANDLE_NETLINK_FUZZING */ |
fea12efb | 230 | |
78dd30b2 | 231 | vrf_configure_backend(VRF_BACKEND_VRF_LITE); |
996c9314 | 232 | logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS); |
78dd30b2 | 233 | |
d62a17ae | 234 | frr_preinit(&zebra_di, argc, argv); |
718e3744 | 235 | |
d62a17ae | 236 | frr_opt_add( |
237 | "bakz:e:l:r" | |
4f04a76b | 238 | #ifdef HAVE_NETLINK |
78dd30b2 | 239 | "s:n" |
411314ed DS |
240 | #endif |
241 | #if defined(HANDLE_ZAPI_FUZZING) | |
acfa8927 SW |
242 | "c:" |
243 | #endif /* HANDLE_ZAPI_FUZZING */ | |
244 | #if defined(HANDLE_NETLINK_FUZZING) | |
245 | "w:" | |
246 | #endif /* HANDLE_NETLINK_FUZZING */ | |
d62a17ae | 247 | , |
248 | longopts, | |
6b093863 DS |
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" | |
f6b66ba9 | 254 | " -k, --keep_kernel Don't delete old routes which were installed by zebra.\n" |
6b093863 | 255 | " -r, --retain When program terminates, retain added route by zebra.\n" |
4f04a76b | 256 | #ifdef HAVE_NETLINK |
f6b66ba9 | 257 | " -n, --vrfwnetns Use NetNS as VRF backend\n" |
6b093863 DS |
258 | " -s, --nl-bufsize Set netlink receive buffer size\n" |
259 | " --v6-rr-semantics Use v6 RR semantics\n" | |
4f04a76b | 260 | #endif /* HAVE_NETLINK */ |
411314ed | 261 | #if defined(HANDLE_ZAPI_FUZZING) |
81a2f870 | 262 | " -c <file> Bypass normal startup and use this file for testing of zapi\n" |
acfa8927 SW |
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 */ | |
6b093863 | 267 | ); |
d62a17ae | 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; | |
689f5a8c DL |
299 | if (!frr_zclient_addr(&dummy, &dummylen, optarg)) { |
300 | fprintf(stderr, | |
301 | "Invalid zserv socket path: %s\n", | |
302 | optarg); | |
303 | exit(1); | |
304 | } | |
d62a17ae | 305 | break; |
306 | case 'l': | |
307 | lblmgr_path = optarg; | |
308 | break; | |
309 | case 'r': | |
310 | retain_mode = 1; | |
311 | break; | |
c34b6b57 | 312 | #ifdef HAVE_NETLINK |
d62a17ae | 313 | case 's': |
314 | nl_rcvbufsize = atoi(optarg); | |
315 | break; | |
78dd30b2 PG |
316 | case 'n': |
317 | vrf_configure_backend(VRF_BACKEND_NETNS); | |
736d41ad | 318 | logicalrouter_configure_backend( |
996c9314 | 319 | LOGICALROUTER_BACKEND_OFF); |
78dd30b2 | 320 | break; |
6b093863 DS |
321 | case OPTION_V6_RR_SEMANTICS: |
322 | v6_rr_semantics = true; | |
323 | break; | |
c34b6b57 | 324 | #endif /* HAVE_NETLINK */ |
411314ed DS |
325 | #if defined(HANDLE_ZAPI_FUZZING) |
326 | case 'c': | |
81a2f870 | 327 | zapi_fuzzing = optarg; |
81a2f870 | 328 | break; |
acfa8927 SW |
329 | #endif /* HANDLE_ZAPI_FUZZING */ |
330 | #if defined(HANDLE_NETLINK_FUZZING) | |
81a2f870 SW |
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 | */ | |
acfa8927 | 337 | netlink_read = true; |
411314ed | 338 | break; |
acfa8927 | 339 | #endif /* HANDLE_NETLINK_FUZZING */ |
d62a17ae | 340 | default: |
341 | frr_help_exit(1); | |
342 | break; | |
343 | } | |
718e3744 | 344 | } |
d62a17ae | 345 | |
346 | vty_config_lockless(); | |
347 | zebrad.master = frr_init(); | |
348 | ||
349 | /* Zebra related initialize. */ | |
5f145fb8 | 350 | zserv_init(); |
d62a17ae | 351 | rib_init(); |
352 | zebra_if_init(); | |
353 | zebra_debug_init(); | |
354 | router_id_cmd_init(); | |
f84fc2c9 DS |
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(); | |
d62a17ae | 362 | access_list_init(); |
363 | prefix_list_init(); | |
364 | #if defined(HAVE_RTADV) | |
365 | rtadv_cmd_init(); | |
36735ed9 | 366 | #endif |
d62a17ae | 367 | /* PTM socket */ |
244c1cdc | 368 | #ifdef ZEBRA_PTM_SUPPORT |
d62a17ae | 369 | zebra_ptm_init(); |
244c1cdc | 370 | #endif |
718e3744 | 371 | |
d62a17ae | 372 | zebra_mpls_init(); |
373 | zebra_mpls_vty_init(); | |
2dd0d726 | 374 | zebra_pw_vty_init(); |
4c0ec639 | 375 | zebra_pbr_init(); |
7758e3f3 | 376 | |
996c9314 LB |
377 | /* For debug purpose. */ |
378 | /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */ | |
718e3744 | 379 | |
d62a17ae | 380 | /* Process the configuration file. Among other configuration |
9d303b37 DL |
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 | */ | |
d62a17ae | 387 | frr_config_fork(); |
718e3744 | 388 | |
d62a17ae | 389 | /* After we have successfully acquired the pidfile, we can be sure |
9d303b37 DL |
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 | */ | |
d62a17ae | 397 | if (!keep_kernel_mode) |
398 | rib_sweep_route(); | |
91b7351d | 399 | |
d62a17ae | 400 | /* Needed for BSD routing socket. */ |
401 | pid = getpid(); | |
718e3744 | 402 | |
21ccc0cf | 403 | /* Intialize pthread library */ |
329e35da QY |
404 | frr_pthread_init(); |
405 | ||
21ccc0cf QY |
406 | /* Start Zebra API server */ |
407 | zserv_start(zserv_path); | |
97be79f9 | 408 | |
d62a17ae | 409 | /* Init label manager */ |
410 | label_manager_init(lblmgr_path); | |
fea12efb | 411 | |
453844ab QY |
412 | /* RNH init */ |
413 | zebra_rnh_init(); | |
414 | ||
2875801f | 415 | #if defined(HANDLE_ZAPI_FUZZING) |
81a2f870 SW |
416 | if (zapi_fuzzing) { |
417 | zserv_read_file(zapi_fuzzing); | |
418 | exit(0); | |
acfa8927 SW |
419 | } |
420 | #endif /* HANDLE_ZAPI_FUZZING */ | |
421 | #if defined(HANDLE_NETLINK_FUZZING) | |
422 | if (netlink_fuzzing) { | |
81a2f870 | 423 | netlink_read_init(netlink_fuzzing); |
2875801f QY |
424 | exit(0); |
425 | } | |
acfa8927 | 426 | #endif /* HANDLE_NETLINK_FUZZING */ |
2875801f QY |
427 | |
428 | ||
d62a17ae | 429 | frr_run(zebrad.master); |
718e3744 | 430 | |
d62a17ae | 431 | /* Not reached... */ |
432 | return 0; | |
718e3744 | 433 | } |