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