paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1 | /* zebra daemon main routine. |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 |
| 17 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 24 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 25 | #include "getopt.h" |
| 26 | #include "command.h" |
| 27 | #include "thread.h" |
| 28 | #include "filter.h" |
| 29 | #include "memory.h" |
| 30 | #include "prefix.h" |
| 31 | #include "log.h" |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 32 | #include "plist.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 33 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 34 | #include "sigevent.h" |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 35 | #include "vrf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | |
| 37 | #include "zebra/rib.h" |
| 38 | #include "zebra/zserv.h" |
| 39 | #include "zebra/debug.h" |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 40 | #include "zebra/router-id.h" |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 41 | #include "zebra/irdp.h" |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 42 | #include "zebra/rtadv.h" |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 43 | #include "zebra/zebra_fpm.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 45 | /* Zebra instance */ |
| 46 | struct zebra_t zebrad = |
| 47 | { |
| 48 | .rtm_table_default = 0, |
| 49 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 50 | |
| 51 | /* process id. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | pid_t pid; |
| 53 | |
gdt | 87efd64 | 2004-06-30 17:36:11 +0000 | [diff] [blame] | 54 | /* Pacify zclient.o in libzebra, which expects this variable. */ |
| 55 | struct thread_master *master; |
| 56 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | /* Route retain mode flag. */ |
| 58 | int retain_mode = 0; |
| 59 | |
| 60 | /* Don't delete kernel route. */ |
| 61 | int keep_kernel_mode = 0; |
| 62 | |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 63 | #ifdef HAVE_NETLINK |
| 64 | /* Receive buffer size for netlink socket */ |
| 65 | u_int32_t nl_rcvbufsize = 0; |
| 66 | #endif /* HAVE_NETLINK */ |
| 67 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | /* Command line options. */ |
| 69 | struct option longopts[] = |
| 70 | { |
| 71 | { "batch", no_argument, NULL, 'b'}, |
| 72 | { "daemon", no_argument, NULL, 'd'}, |
| 73 | { "keep_kernel", no_argument, NULL, 'k'}, |
Avneesh Sachdev | 5779fda | 2016-04-04 10:54:58 -0700 | [diff] [blame^] | 74 | { "fpm_format", required_argument, NULL, 'F'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | { "config_file", required_argument, NULL, 'f'}, |
| 76 | { "pid_file", required_argument, NULL, 'i'}, |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 77 | { "socket", required_argument, NULL, 'z'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | { "help", no_argument, NULL, 'h'}, |
| 79 | { "vty_addr", required_argument, NULL, 'A'}, |
| 80 | { "vty_port", required_argument, NULL, 'P'}, |
| 81 | { "retain", no_argument, NULL, 'r'}, |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 82 | { "dryrun", no_argument, NULL, 'C'}, |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 83 | #ifdef HAVE_NETLINK |
hasso | 583d800 | 2005-01-16 23:34:02 +0000 | [diff] [blame] | 84 | { "nl-bufsize", required_argument, NULL, 's'}, |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 85 | #endif /* HAVE_NETLINK */ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 86 | { "user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 87 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | { "version", no_argument, NULL, 'v'}, |
| 89 | { 0 } |
| 90 | }; |
| 91 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 92 | zebra_capabilities_t _caps_p [] = |
| 93 | { |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 94 | ZCAP_NET_ADMIN, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 95 | ZCAP_SYS_ADMIN, |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 96 | ZCAP_NET_RAW, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /* zebra privileges to run with */ |
| 100 | struct zebra_privs_t zserv_privs = |
| 101 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 102 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 103 | .user = QUAGGA_USER, |
| 104 | .group = QUAGGA_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 105 | #endif |
| 106 | #ifdef VTY_GROUP |
| 107 | .vty_group = VTY_GROUP, |
| 108 | #endif |
| 109 | .caps_p = _caps_p, |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 110 | .cap_num_p = array_size(_caps_p), |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 111 | .cap_num_i = 0 |
| 112 | }; |
| 113 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | /* Default configuration file path. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE; |
| 116 | |
| 117 | /* Process ID saved for use by init system */ |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 118 | const char *pid_file = PATH_ZEBRA_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | |
| 120 | /* Help information display. */ |
| 121 | static void |
| 122 | usage (char *progname, int status) |
| 123 | { |
| 124 | if (status != 0) |
| 125 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 126 | else |
| 127 | { |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 128 | printf ("Usage : %s [OPTION...]\n\n"\ |
| 129 | "Daemon which manages kernel routing table management and "\ |
| 130 | "redistribution between different routing protocols.\n\n"\ |
| 131 | "-b, --batch Runs in batch mode\n"\ |
| 132 | "-d, --daemon Runs in daemon mode\n"\ |
| 133 | "-f, --config_file Set configuration file name\n"\ |
Avneesh Sachdev | 5779fda | 2016-04-04 10:54:58 -0700 | [diff] [blame^] | 134 | "-F, --fpm_format Set fpm format to 'netlink' or 'protobuf'\n"\ |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 135 | "-i, --pid_file Set process identifier file name\n"\ |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 136 | "-z, --socket Set path of zebra socket\n"\ |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 137 | "-k, --keep_kernel Don't delete old routes which installed by "\ |
| 138 | "zebra.\n"\ |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 139 | "-C, --dryrun Check configuration for validity and exit\n"\ |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 140 | "-A, --vty_addr Set vty's bind address\n"\ |
| 141 | "-P, --vty_port Set vty's port number\n"\ |
| 142 | "-r, --retain When program terminates, retain added route "\ |
| 143 | "by zebra.\n"\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 144 | "-u, --user User to run as\n"\ |
| 145 | "-g, --group Group to run as\n", progname); |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 146 | #ifdef HAVE_NETLINK |
| 147 | printf ("-s, --nl-bufsize Set netlink receive buffer size\n"); |
| 148 | #endif /* HAVE_NETLINK */ |
| 149 | printf ("-v, --version Print program version\n"\ |
| 150 | "-h, --help Display this help and exit\n"\ |
| 151 | "\n"\ |
| 152 | "Report bugs to %s\n", ZEBRA_BUG_ADDRESS); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | exit (status); |
| 156 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 157 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | /* SIGHUP handler. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 159 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 160 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | { |
| 162 | zlog_info ("SIGHUP received"); |
| 163 | |
| 164 | /* Reload of config file. */ |
| 165 | ; |
| 166 | } |
| 167 | |
| 168 | /* SIGINT handler. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 169 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 170 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 172 | zlog_notice ("Terminating on signal"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | |
| 174 | if (!retain_mode) |
| 175 | rib_close (); |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 176 | #ifdef HAVE_IRDP |
| 177 | irdp_finish(); |
| 178 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 179 | |
| 180 | exit (0); |
| 181 | } |
| 182 | |
| 183 | /* SIGUSR1 handler. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 184 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 185 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | { |
| 187 | zlog_rotate (NULL); |
| 188 | } |
| 189 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 190 | struct quagga_signal_t zebra_signals[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 191 | { |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 192 | { |
| 193 | .signal = SIGHUP, |
| 194 | .handler = &sighup, |
| 195 | }, |
| 196 | { |
| 197 | .signal = SIGUSR1, |
| 198 | .handler = &sigusr1, |
| 199 | }, |
| 200 | { |
| 201 | .signal = SIGINT, |
hasso | 8c903fb | 2004-03-17 20:39:18 +0000 | [diff] [blame] | 202 | .handler = &sigint, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 203 | }, |
hasso | f571dab | 2004-03-22 08:55:25 +0000 | [diff] [blame] | 204 | { |
| 205 | .signal = SIGTERM, |
| 206 | .handler = &sigint, |
| 207 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 208 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 209 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 210 | /* Callback upon creating a new VRF. */ |
| 211 | static int |
| 212 | zebra_vrf_new (vrf_id_t vrf_id, void **info) |
| 213 | { |
| 214 | struct zebra_vrf *zvrf = *info; |
| 215 | |
| 216 | if (! zvrf) |
| 217 | { |
| 218 | zvrf = zebra_vrf_alloc (vrf_id); |
| 219 | *info = (void *)zvrf; |
Feng Lu | ac19a44 | 2015-05-22 11:40:07 +0200 | [diff] [blame] | 220 | router_id_init (zvrf); |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Feng Lu | 267ceb2 | 2015-05-22 11:40:09 +0200 | [diff] [blame] | 226 | /* Callback upon enabling a VRF. */ |
| 227 | static int |
| 228 | zebra_vrf_enable (vrf_id_t vrf_id, void **info) |
| 229 | { |
| 230 | struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info); |
| 231 | |
| 232 | assert (zvrf); |
| 233 | |
Donald Sharp | 6425773 | 2015-11-20 08:33:30 -0500 | [diff] [blame] | 234 | #if defined (HAVE_RTADV) |
Feng Lu | 49f7609 | 2015-05-22 11:40:10 +0200 | [diff] [blame] | 235 | rtadv_init (zvrf); |
| 236 | #endif |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 237 | kernel_init (zvrf); |
| 238 | interface_list (zvrf); |
| 239 | route_read (zvrf); |
| 240 | |
Feng Lu | 267ceb2 | 2015-05-22 11:40:09 +0200 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* Callback upon disabling a VRF. */ |
| 245 | static int |
| 246 | zebra_vrf_disable (vrf_id_t vrf_id, void **info) |
| 247 | { |
| 248 | struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info); |
| 249 | struct listnode *list_node; |
| 250 | struct interface *ifp; |
| 251 | |
| 252 | assert (zvrf); |
| 253 | |
| 254 | rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]); |
| 255 | rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); |
| 256 | |
| 257 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), list_node, ifp)) |
| 258 | { |
| 259 | int operative = if_is_operative (ifp); |
| 260 | UNSET_FLAG (ifp->flags, IFF_UP); |
| 261 | if (operative) |
| 262 | if_down (ifp); |
| 263 | } |
| 264 | |
Donald Sharp | 6425773 | 2015-11-20 08:33:30 -0500 | [diff] [blame] | 265 | #if defined (HAVE_RTADV) |
Feng Lu | 49f7609 | 2015-05-22 11:40:10 +0200 | [diff] [blame] | 266 | rtadv_terminate (zvrf); |
| 267 | #endif |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 268 | kernel_terminate (zvrf); |
Feng Lu | 49f7609 | 2015-05-22 11:40:10 +0200 | [diff] [blame] | 269 | |
Feng Lu | 267ceb2 | 2015-05-22 11:40:09 +0200 | [diff] [blame] | 270 | list_delete_all_node (zvrf->rid_all_sorted_list); |
| 271 | list_delete_all_node (zvrf->rid_lo_sorted_list); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 276 | /* Zebra VRF initialization. */ |
| 277 | static void |
| 278 | zebra_vrf_init (void) |
| 279 | { |
| 280 | vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new); |
Feng Lu | 267ceb2 | 2015-05-22 11:40:09 +0200 | [diff] [blame] | 281 | vrf_add_hook (VRF_ENABLE_HOOK, zebra_vrf_enable); |
| 282 | vrf_add_hook (VRF_DISABLE_HOOK, zebra_vrf_disable); |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 283 | vrf_init (); |
| 284 | } |
| 285 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 286 | /* Main startup routine. */ |
| 287 | int |
| 288 | main (int argc, char **argv) |
| 289 | { |
| 290 | char *p; |
| 291 | char *vty_addr = NULL; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 292 | int vty_port = ZEBRA_VTY_PORT; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 293 | int dryrun = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 294 | int batch_mode = 0; |
| 295 | int daemon_mode = 0; |
| 296 | char *config_file = NULL; |
| 297 | char *progname; |
| 298 | struct thread thread; |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 299 | char *zserv_path = NULL; |
Avneesh Sachdev | 5779fda | 2016-04-04 10:54:58 -0700 | [diff] [blame^] | 300 | char *fpm_format = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | |
| 302 | /* Set umask before anything for security */ |
| 303 | umask (0027); |
| 304 | |
| 305 | /* preserve my name */ |
| 306 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 307 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 308 | zlog_default = openzlog (progname, ZLOG_ZEBRA, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 309 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 310 | |
| 311 | while (1) |
| 312 | { |
| 313 | int opt; |
| 314 | |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 315 | #ifdef HAVE_NETLINK |
Avneesh Sachdev | 5779fda | 2016-04-04 10:54:58 -0700 | [diff] [blame^] | 316 | opt = getopt_long (argc, argv, "bdkf:F:i:z:hA:P:ru:g:vs:C", longopts, 0); |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 317 | #else |
Avneesh Sachdev | 5779fda | 2016-04-04 10:54:58 -0700 | [diff] [blame^] | 318 | opt = getopt_long (argc, argv, "bdkf:F:i:z:hA:P:ru:g:vC", longopts, 0); |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 319 | #endif /* HAVE_NETLINK */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 320 | |
| 321 | if (opt == EOF) |
| 322 | break; |
| 323 | |
| 324 | switch (opt) |
| 325 | { |
| 326 | case 0: |
| 327 | break; |
| 328 | case 'b': |
| 329 | batch_mode = 1; |
| 330 | case 'd': |
| 331 | daemon_mode = 1; |
| 332 | break; |
| 333 | case 'k': |
| 334 | keep_kernel_mode = 1; |
| 335 | break; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 336 | case 'C': |
| 337 | dryrun = 1; |
| 338 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | case 'f': |
| 340 | config_file = optarg; |
| 341 | break; |
Avneesh Sachdev | 5779fda | 2016-04-04 10:54:58 -0700 | [diff] [blame^] | 342 | case 'F': |
| 343 | fpm_format = optarg; |
| 344 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 345 | case 'A': |
| 346 | vty_addr = optarg; |
| 347 | break; |
| 348 | case 'i': |
| 349 | pid_file = optarg; |
| 350 | break; |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 351 | case 'z': |
| 352 | zserv_path = optarg; |
| 353 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 354 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 355 | /* Deal with atoi() returning 0 on failure, and zebra not |
| 356 | listening on zebra port... */ |
| 357 | if (strcmp(optarg, "0") == 0) |
| 358 | { |
| 359 | vty_port = 0; |
| 360 | break; |
| 361 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 362 | vty_port = atoi (optarg); |
Paul Jakma | 0d6b2ee | 2008-05-29 18:29:16 +0000 | [diff] [blame] | 363 | if (vty_port <= 0 || vty_port > 0xffff) |
| 364 | vty_port = ZEBRA_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 365 | break; |
| 366 | case 'r': |
| 367 | retain_mode = 1; |
| 368 | break; |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 369 | #ifdef HAVE_NETLINK |
| 370 | case 's': |
| 371 | nl_rcvbufsize = atoi (optarg); |
| 372 | break; |
| 373 | #endif /* HAVE_NETLINK */ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 374 | case 'u': |
| 375 | zserv_privs.user = optarg; |
| 376 | break; |
| 377 | case 'g': |
| 378 | zserv_privs.group = optarg; |
| 379 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 380 | case 'v': |
| 381 | print_version (progname); |
| 382 | exit (0); |
| 383 | break; |
| 384 | case 'h': |
| 385 | usage (progname, 0); |
| 386 | break; |
| 387 | default: |
| 388 | usage (progname, 1); |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /* Make master thread emulator. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 394 | zebrad.master = thread_master_create (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 395 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 396 | /* privs initialise */ |
| 397 | zprivs_init (&zserv_privs); |
| 398 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 399 | /* Vty related initialize. */ |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 400 | signal_init (zebrad.master, array_size(zebra_signals), zebra_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 401 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 402 | vty_init (zebrad.master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 403 | memory_init (); |
| 404 | |
| 405 | /* Zebra related initialize. */ |
| 406 | zebra_init (); |
| 407 | rib_init (); |
| 408 | zebra_if_init (); |
| 409 | zebra_debug_init (); |
Feng Lu | ac19a44 | 2015-05-22 11:40:07 +0200 | [diff] [blame] | 410 | router_id_cmd_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 411 | zebra_vty_init (); |
| 412 | access_list_init (); |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 413 | prefix_list_init (); |
Donald Sharp | 6425773 | 2015-11-20 08:33:30 -0500 | [diff] [blame] | 414 | #if defined (HAVE_RTADV) |
Feng Lu | 49f7609 | 2015-05-22 11:40:10 +0200 | [diff] [blame] | 415 | rtadv_cmd_init (); |
Joachim Nilsson | 36735ed | 2012-05-09 13:38:36 +0200 | [diff] [blame] | 416 | #endif |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 417 | #ifdef HAVE_IRDP |
| 418 | irdp_init(); |
| 419 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 420 | |
| 421 | /* For debug purpose. */ |
| 422 | /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */ |
| 423 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 424 | /* Initialize VRF module, and make kernel routing socket. */ |
| 425 | zebra_vrf_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 426 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | #ifdef HAVE_SNMP |
| 428 | zebra_snmp_init (); |
| 429 | #endif /* HAVE_SNMP */ |
| 430 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 431 | #ifdef HAVE_FPM |
Avneesh Sachdev | 5779fda | 2016-04-04 10:54:58 -0700 | [diff] [blame^] | 432 | zfpm_init (zebrad.master, 1, 0, fpm_format); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 433 | #else |
Avneesh Sachdev | 5779fda | 2016-04-04 10:54:58 -0700 | [diff] [blame^] | 434 | zfpm_init (zebrad.master, 0, 0, fpm_format); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 435 | #endif |
| 436 | |
Denis Ovsienko | 91b7351 | 2007-09-14 13:31:52 +0000 | [diff] [blame] | 437 | /* Process the configuration file. Among other configuration |
| 438 | * directives we can meet those installing static routes. Such |
| 439 | * requests will not be executed immediately, but queued in |
| 440 | * zebra->ribq structure until we enter the main execution loop. |
| 441 | * The notifications from kernel will show originating PID equal |
| 442 | * to that after daemon() completes (if ever called). |
| 443 | */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 444 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 445 | |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 446 | /* Don't start execution if we are in dry-run mode */ |
| 447 | if (dryrun) |
| 448 | return(0); |
Christian Franke | 581ecbf | 2016-05-03 19:59:43 +0200 | [diff] [blame] | 449 | |
| 450 | /* Count up events for interfaces */ |
| 451 | if_startup_count_up (); |
| 452 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 453 | /* Clean up rib. */ |
| 454 | rib_weed_tables (); |
| 455 | |
| 456 | /* Exit when zebra is working in batch mode. */ |
| 457 | if (batch_mode) |
| 458 | exit (0); |
| 459 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 460 | /* Daemonize. */ |
Stephen Hemminger | 065de90 | 2009-08-07 11:13:49 -0700 | [diff] [blame] | 461 | if (daemon_mode && daemon (0, 0) < 0) |
| 462 | { |
| 463 | zlog_err("Zebra daemon failed: %s", strerror(errno)); |
| 464 | exit (1); |
| 465 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 466 | |
| 467 | /* Output pid of zebra. */ |
| 468 | pid_output (pid_file); |
| 469 | |
Denis Ovsienko | 91b7351 | 2007-09-14 13:31:52 +0000 | [diff] [blame] | 470 | /* After we have successfully acquired the pidfile, we can be sure |
| 471 | * about being the only copy of zebra process, which is submitting |
| 472 | * changes to the FIB. |
| 473 | * Clean up zebra-originated routes. The requests will be sent to OS |
| 474 | * immediately, so originating PID in notifications from kernel |
| 475 | * will be equal to the current getpid(). To know about such routes, |
| 476 | * we have to have route_read() called before. |
| 477 | */ |
| 478 | if (! keep_kernel_mode) |
| 479 | rib_sweep_route (); |
| 480 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | /* Needed for BSD routing socket. */ |
| 482 | pid = getpid (); |
| 483 | |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 484 | /* This must be done only after locking pidfile (bug #403). */ |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 485 | zebra_zserv_socket_init (zserv_path); |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 486 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | /* Make vty server socket. */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 488 | vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 489 | |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 490 | /* Print banner. */ |
| 491 | zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port); |
| 492 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 493 | while (thread_fetch (zebrad.master, &thread)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 494 | thread_call (&thread); |
| 495 | |
| 496 | /* Not reached... */ |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 497 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 498 | } |