paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Main routine of bgpd. |
| 2 | Copyright (C) 1996, 97, 98, 1999 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 | #include <zebra.h> |
| 22 | |
| 23 | #include "vector.h" |
| 24 | #include "vty.h" |
| 25 | #include "command.h" |
| 26 | #include "getopt.h" |
| 27 | #include "thread.h" |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 28 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 29 | #include "memory.h" |
| 30 | #include "prefix.h" |
| 31 | #include "log.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 32 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 33 | #include "sigevent.h" |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 34 | #include "zclient.h" |
| 35 | #include "routemap.h" |
| 36 | #include "filter.h" |
| 37 | #include "plist.h" |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 38 | #include "stream.h" |
Feng Lu | 126215c | 2015-05-22 11:39:58 +0200 | [diff] [blame] | 39 | #include "vrf.h" |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 40 | #include "workqueue.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 41 | |
| 42 | #include "bgpd/bgpd.h" |
| 43 | #include "bgpd/bgp_attr.h" |
| 44 | #include "bgpd/bgp_mplsvpn.h" |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 45 | #include "bgpd/bgp_aspath.h" |
| 46 | #include "bgpd/bgp_dump.h" |
| 47 | #include "bgpd/bgp_route.h" |
| 48 | #include "bgpd/bgp_nexthop.h" |
| 49 | #include "bgpd/bgp_regex.h" |
| 50 | #include "bgpd/bgp_clist.h" |
| 51 | #include "bgpd/bgp_debug.h" |
| 52 | #include "bgpd/bgp_filter.h" |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 53 | #include "bgpd/bgp_zebra.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | |
| 55 | /* bgpd options, we use GNU getopt library. */ |
Stephen Hemminger | 372b3c7 | 2009-05-15 10:29:41 -0700 | [diff] [blame] | 56 | static const struct option longopts[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | { |
| 58 | { "daemon", no_argument, NULL, 'd'}, |
| 59 | { "config_file", required_argument, NULL, 'f'}, |
| 60 | { "pid_file", required_argument, NULL, 'i'}, |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 61 | { "socket", required_argument, NULL, 'z'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 62 | { "bgp_port", required_argument, NULL, 'p'}, |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 63 | { "listenon", required_argument, NULL, 'l'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 64 | { "vty_addr", required_argument, NULL, 'A'}, |
| 65 | { "vty_port", required_argument, NULL, 'P'}, |
| 66 | { "retain", no_argument, NULL, 'r'}, |
| 67 | { "no_kernel", no_argument, NULL, 'n'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 68 | { "user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 69 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 70 | { "version", no_argument, NULL, 'v'}, |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 71 | { "dryrun", no_argument, NULL, 'C'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | { "help", no_argument, NULL, 'h'}, |
| 73 | { 0 } |
| 74 | }; |
| 75 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 76 | /* signal definitions */ |
| 77 | void sighup (void); |
| 78 | void sigint (void); |
| 79 | void sigusr1 (void); |
| 80 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 81 | static void bgp_exit (int); |
| 82 | |
Stephen Hemminger | 372b3c7 | 2009-05-15 10:29:41 -0700 | [diff] [blame] | 83 | static struct quagga_signal_t bgp_signals[] = |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 84 | { |
| 85 | { |
| 86 | .signal = SIGHUP, |
| 87 | .handler = &sighup, |
| 88 | }, |
| 89 | { |
| 90 | .signal = SIGUSR1, |
| 91 | .handler = &sigusr1, |
| 92 | }, |
| 93 | { |
| 94 | .signal = SIGINT, |
| 95 | .handler = &sigint, |
| 96 | }, |
hasso | f571dab | 2004-03-22 08:55:25 +0000 | [diff] [blame] | 97 | { |
| 98 | .signal = SIGTERM, |
| 99 | .handler = &sigint, |
| 100 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | /* Configuration file and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG; |
| 105 | |
| 106 | /* Route retain mode flag. */ |
Stephen Hemminger | 372b3c7 | 2009-05-15 10:29:41 -0700 | [diff] [blame] | 107 | static int retain_mode = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 108 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 109 | /* Manually specified configuration file name. */ |
| 110 | char *config_file = NULL; |
| 111 | |
| 112 | /* Process ID saved for use by init system */ |
Stephen Hemminger | 372b3c7 | 2009-05-15 10:29:41 -0700 | [diff] [blame] | 113 | static const char *pid_file = PATH_BGPD_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | |
| 115 | /* VTY port number and address. */ |
| 116 | int vty_port = BGP_VTY_PORT; |
| 117 | char *vty_addr = NULL; |
| 118 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 119 | /* privileges */ |
Stephen Hemminger | 372b3c7 | 2009-05-15 10:29:41 -0700 | [diff] [blame] | 120 | static zebra_capabilities_t _caps_p [] = |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 121 | { |
paul | 98f5163 | 2004-10-25 14:19:15 +0000 | [diff] [blame] | 122 | ZCAP_BIND, |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 123 | ZCAP_NET_RAW, |
Chris Luke | 5c88f19 | 2011-10-18 17:26:51 +0400 | [diff] [blame] | 124 | ZCAP_NET_ADMIN, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | struct zebra_privs_t bgpd_privs = |
| 128 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 129 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 130 | .user = QUAGGA_USER, |
| 131 | .group = QUAGGA_GROUP, |
| 132 | #endif |
| 133 | #ifdef VTY_GROUP |
| 134 | .vty_group = VTY_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 135 | #endif |
| 136 | .caps_p = _caps_p, |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 137 | .cap_num_p = array_size(_caps_p), |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 138 | .cap_num_i = 0, |
| 139 | }; |
| 140 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 141 | /* Help information display. */ |
| 142 | static void |
| 143 | usage (char *progname, int status) |
| 144 | { |
| 145 | if (status != 0) |
| 146 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 147 | else |
| 148 | { |
| 149 | printf ("Usage : %s [OPTION...]\n\n\ |
| 150 | Daemon which manages kernel routing table management and \ |
| 151 | redistribution between different routing protocols.\n\n\ |
| 152 | -d, --daemon Runs in daemon mode\n\ |
| 153 | -f, --config_file Set configuration file name\n\ |
| 154 | -i, --pid_file Set process identifier file name\n\ |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 155 | -z, --socket Set path of zebra socket\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | -p, --bgp_port Set bgp protocol's port number\n\ |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 157 | -l, --listenon Listen on specified address (implies -n)\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | -A, --vty_addr Set vty's bind address\n\ |
| 159 | -P, --vty_port Set vty's port number\n\ |
| 160 | -r, --retain When program terminates, retain added route by bgpd.\n\ |
| 161 | -n, --no_kernel Do not install route to kernel.\n\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 162 | -u, --user User to run as\n\ |
| 163 | -g, --group Group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 164 | -v, --version Print program version\n\ |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 165 | -C, --dryrun Check configuration for validity and exit\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | -h, --help Display this help and exit\n\ |
| 167 | \n\ |
| 168 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 169 | } |
| 170 | |
| 171 | exit (status); |
| 172 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 173 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 174 | /* SIGHUP handler. */ |
| 175 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 176 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | { |
| 178 | zlog (NULL, LOG_INFO, "SIGHUP received"); |
| 179 | |
| 180 | /* Terminate all thread. */ |
| 181 | bgp_terminate (); |
| 182 | bgp_reset (); |
| 183 | zlog_info ("bgpd restarting!"); |
| 184 | |
| 185 | /* Reload config file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 186 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | |
| 188 | /* Create VTY's socket */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 189 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 190 | |
| 191 | /* Try to return to normal operation. */ |
| 192 | } |
| 193 | |
| 194 | /* SIGINT handler. */ |
| 195 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 196 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 198 | zlog_notice ("Terminating on signal"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 199 | |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 200 | if (! retain_mode) |
| 201 | { |
| 202 | bgp_terminate (); |
| 203 | zprivs_terminate (&bgpd_privs); |
| 204 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 206 | bgp_exit (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* SIGUSR1 handler. */ |
| 210 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 211 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 212 | { |
| 213 | zlog_rotate (NULL); |
| 214 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | Try to free up allocations we know about so that diagnostic tools such as |
| 218 | valgrind are able to better illuminate leaks. |
| 219 | |
| 220 | Zebra route removal and protocol teardown are not meant to be done here. |
| 221 | For example, "retain_mode" may be set. |
| 222 | */ |
| 223 | static void |
| 224 | bgp_exit (int status) |
| 225 | { |
| 226 | struct bgp *bgp; |
| 227 | struct listnode *node, *nnode; |
| 228 | int *socket; |
| 229 | struct interface *ifp; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 230 | extern struct zclient *zlookup; |
| 231 | |
| 232 | /* it only makes sense for this to be called on a clean exit */ |
| 233 | assert (status == 0); |
| 234 | |
| 235 | /* reverse bgp_master_init */ |
| 236 | for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp)) |
| 237 | bgp_delete (bgp); |
| 238 | list_free (bm->bgp); |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 239 | bm->bgp = NULL; |
| 240 | |
| 241 | /* |
| 242 | * bgp_delete can re-allocate the process queues after they were |
| 243 | * deleted in bgp_terminate. delete them again. |
| 244 | * |
| 245 | * It might be better to ensure the RIBs (including static routes) |
| 246 | * are cleared by bgp_terminate() during its call to bgp_cleanup_routes(), |
| 247 | * which currently only deletes the kernel routes. |
| 248 | */ |
| 249 | if (bm->process_main_queue) |
| 250 | { |
| 251 | work_queue_free (bm->process_main_queue); |
| 252 | bm->process_main_queue = NULL; |
| 253 | } |
| 254 | if (bm->process_rsclient_queue) |
| 255 | { |
| 256 | work_queue_free (bm->process_rsclient_queue); |
| 257 | bm->process_rsclient_queue = NULL; |
| 258 | } |
| 259 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 260 | /* reverse bgp_master_init */ |
| 261 | for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, socket)) |
| 262 | { |
| 263 | if (close ((int)(long)socket) == -1) |
| 264 | zlog_err ("close (%d): %s", (int)(long)socket, safe_strerror (errno)); |
| 265 | } |
| 266 | list_delete (bm->listen_sockets); |
| 267 | |
| 268 | /* reverse bgp_zebra_init/if_init */ |
| 269 | if (retain_mode) |
| 270 | if_add_hook (IF_DELETE_HOOK, NULL); |
Feng Lu | 126215c | 2015-05-22 11:39:58 +0200 | [diff] [blame] | 271 | for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 272 | { |
| 273 | struct listnode *c_node, *c_nnode; |
| 274 | struct connected *c; |
| 275 | |
| 276 | for (ALL_LIST_ELEMENTS (ifp->connected, c_node, c_nnode, c)) |
| 277 | bgp_connected_delete (c); |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 278 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 279 | |
| 280 | /* reverse bgp_attr_init */ |
| 281 | bgp_attr_finish (); |
| 282 | |
| 283 | /* reverse bgp_dump_init */ |
| 284 | bgp_dump_finish (); |
| 285 | |
| 286 | /* reverse bgp_route_init */ |
| 287 | bgp_route_finish (); |
| 288 | |
| 289 | /* reverse bgp_route_map_init/route_map_init */ |
| 290 | route_map_finish (); |
| 291 | |
| 292 | /* reverse bgp_scan_init */ |
| 293 | bgp_scan_finish (); |
| 294 | |
| 295 | /* reverse access_list_init */ |
| 296 | access_list_add_hook (NULL); |
| 297 | access_list_delete_hook (NULL); |
| 298 | access_list_reset (); |
| 299 | |
| 300 | /* reverse bgp_filter_init */ |
| 301 | as_list_add_hook (NULL); |
| 302 | as_list_delete_hook (NULL); |
| 303 | bgp_filter_reset (); |
| 304 | |
| 305 | /* reverse prefix_list_init */ |
| 306 | prefix_list_add_hook (NULL); |
| 307 | prefix_list_delete_hook (NULL); |
| 308 | prefix_list_reset (); |
| 309 | |
| 310 | /* reverse community_list_init */ |
| 311 | community_list_terminate (bgp_clist); |
| 312 | |
Feng Lu | 126215c | 2015-05-22 11:39:58 +0200 | [diff] [blame] | 313 | vrf_terminate (); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 314 | cmd_terminate (); |
| 315 | vty_terminate (); |
Lou Berger | 6370357 | 2016-01-12 13:42:03 -0500 | [diff] [blame] | 316 | bgp_address_destroy(); |
| 317 | bgp_scan_destroy(); |
| 318 | bgp_zebra_destroy(); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 319 | if (zlookup) |
| 320 | zclient_free (zlookup); |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 321 | if (bgp_nexthop_buf) |
| 322 | stream_free (bgp_nexthop_buf); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 323 | |
| 324 | /* reverse bgp_master_init */ |
Donald Sharp | 774914f | 2015-10-14 08:50:39 -0400 | [diff] [blame] | 325 | if (bm->master) |
| 326 | thread_master_free (bm->master); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 327 | |
| 328 | if (zlog_default) |
| 329 | closezlog (zlog_default); |
| 330 | |
| 331 | if (CONF_BGP_DEBUG (normal, NORMAL)) |
| 332 | log_memstats_stderr ("bgpd"); |
| 333 | |
| 334 | exit (status); |
| 335 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 336 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 337 | /* Main routine of bgpd. Treatment of argument and start bgp finite |
| 338 | state machine is handled at here. */ |
| 339 | int |
| 340 | main (int argc, char **argv) |
| 341 | { |
| 342 | char *p; |
| 343 | int opt; |
| 344 | int daemon_mode = 0; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 345 | int dryrun = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | char *progname; |
| 347 | struct thread thread; |
Paul Jakma | 0d6b2ee | 2008-05-29 18:29:16 +0000 | [diff] [blame] | 348 | int tmp_port; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 349 | |
| 350 | /* Set umask before anything for security */ |
| 351 | umask (0027); |
| 352 | |
| 353 | /* Preserve name of myself. */ |
| 354 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 355 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 356 | zlog_default = openzlog (progname, ZLOG_BGP, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 357 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 358 | |
| 359 | /* BGP master init. */ |
| 360 | bgp_master_init (); |
| 361 | |
| 362 | /* Command line argument treatment. */ |
| 363 | while (1) |
| 364 | { |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 365 | opt = getopt_long (argc, argv, "df:i:z:hp:l:A:P:rnu:g:vC", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 366 | |
| 367 | if (opt == EOF) |
| 368 | break; |
| 369 | |
| 370 | switch (opt) |
| 371 | { |
| 372 | case 0: |
| 373 | break; |
| 374 | case 'd': |
| 375 | daemon_mode = 1; |
| 376 | break; |
| 377 | case 'f': |
| 378 | config_file = optarg; |
| 379 | break; |
| 380 | case 'i': |
| 381 | pid_file = optarg; |
| 382 | break; |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 383 | case 'z': |
| 384 | zclient_serv_path_set (optarg); |
| 385 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | case 'p': |
Paul Jakma | 0d6b2ee | 2008-05-29 18:29:16 +0000 | [diff] [blame] | 387 | tmp_port = atoi (optarg); |
| 388 | if (tmp_port <= 0 || tmp_port > 0xffff) |
| 389 | bm->port = BGP_PORT_DEFAULT; |
| 390 | else |
| 391 | bm->port = tmp_port; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 392 | break; |
| 393 | case 'A': |
| 394 | vty_addr = optarg; |
| 395 | break; |
| 396 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 397 | /* Deal with atoi() returning 0 on failure, and bgpd not |
| 398 | listening on bgp port... */ |
| 399 | if (strcmp(optarg, "0") == 0) |
| 400 | { |
| 401 | vty_port = 0; |
| 402 | break; |
| 403 | } |
| 404 | vty_port = atoi (optarg); |
Paul Jakma | 0d6b2ee | 2008-05-29 18:29:16 +0000 | [diff] [blame] | 405 | if (vty_port <= 0 || vty_port > 0xffff) |
| 406 | vty_port = BGP_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 407 | break; |
| 408 | case 'r': |
| 409 | retain_mode = 1; |
| 410 | break; |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 411 | case 'l': |
| 412 | bm->address = optarg; |
| 413 | /* listenon implies -n */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 414 | case 'n': |
| 415 | bgp_option_set (BGP_OPT_NO_FIB); |
| 416 | break; |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 417 | case 'u': |
| 418 | bgpd_privs.user = optarg; |
| 419 | break; |
| 420 | case 'g': |
| 421 | bgpd_privs.group = optarg; |
| 422 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 423 | case 'v': |
| 424 | print_version (progname); |
| 425 | exit (0); |
| 426 | break; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 427 | case 'C': |
| 428 | dryrun = 1; |
| 429 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 430 | case 'h': |
| 431 | usage (progname, 0); |
| 432 | break; |
| 433 | default: |
| 434 | usage (progname, 1); |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 439 | /* Initializations. */ |
Donald Sharp | f31bab4 | 2015-06-19 19:26:19 -0400 | [diff] [blame] | 440 | srandom (time (NULL)); |
Donald Sharp | 774914f | 2015-10-14 08:50:39 -0400 | [diff] [blame] | 441 | signal_init (bm->master, array_size(bgp_signals), bgp_signals); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 442 | zprivs_init (&bgpd_privs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 443 | cmd_init (1); |
Donald Sharp | 774914f | 2015-10-14 08:50:39 -0400 | [diff] [blame] | 444 | vty_init (bm->master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 445 | memory_init (); |
Feng Lu | 126215c | 2015-05-22 11:39:58 +0200 | [diff] [blame] | 446 | vrf_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 447 | |
| 448 | /* BGP related initialization. */ |
| 449 | bgp_init (); |
| 450 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 451 | /* Parse config file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 452 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 453 | |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 454 | /* Start execution only if not in dry-run mode */ |
| 455 | if(dryrun) |
| 456 | return(0); |
| 457 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 458 | /* Turn into daemon if daemon_mode is set. */ |
Stephen Hemminger | 372b3c7 | 2009-05-15 10:29:41 -0700 | [diff] [blame] | 459 | if (daemon_mode && daemon (0, 0) < 0) |
| 460 | { |
| 461 | zlog_err("BGPd daemon failed: %s", strerror(errno)); |
| 462 | return (1); |
| 463 | } |
| 464 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 465 | |
| 466 | /* Process ID file creation. */ |
| 467 | pid_output (pid_file); |
| 468 | |
| 469 | /* Make bgp vty socket. */ |
| 470 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); |
| 471 | |
| 472 | /* Print banner. */ |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 473 | zlog_notice ("BGPd %s starting: vty@%d, bgp@%s:%d pid %d", QUAGGA_VERSION, |
Paul Jakma | 7e992e9 | 2007-11-13 09:32:23 +0000 | [diff] [blame] | 474 | vty_port, |
Paul Jakma | b63e608 | 2007-11-21 21:25:34 +0000 | [diff] [blame] | 475 | (bm->address ? bm->address : "<all>"), |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 476 | bm->port, |
| 477 | getpid ()); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | |
| 479 | /* Start finite state machine, here we go! */ |
Donald Sharp | 774914f | 2015-10-14 08:50:39 -0400 | [diff] [blame] | 480 | while (thread_fetch (bm->master, &thread)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | thread_call (&thread); |
| 482 | |
| 483 | /* Not reached. */ |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 484 | return (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 485 | } |