]> git.proxmox.com Git - mirror_frr.git/blame - ripd/rip_main.c
[zebra/linux] Use BPF to filter out responses, to try avoid netlink overruns
[mirror_frr.git] / ripd / rip_main.c
CommitLineData
718e3744 1/* RIPd main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
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
5e4fa164 24#include <lib/version.h>
718e3744 25#include "getopt.h"
26#include "thread.h"
27#include "command.h"
28#include "memory.h"
29#include "prefix.h"
30#include "filter.h"
31#include "keychain.h"
32#include "log.h"
edd7c245 33#include "privs.h"
2d75d052 34#include "sigevent.h"
718e3744 35
36#include "ripd/ripd.h"
37
38/* ripd options. */
39static struct option longopts[] =
40{
41 { "daemon", no_argument, NULL, 'd'},
42 { "config_file", required_argument, NULL, 'f'},
43 { "pid_file", required_argument, NULL, 'i'},
44 { "help", no_argument, NULL, 'h'},
876b8be0 45 { "dryrun", no_argument, NULL, 'C'},
718e3744 46 { "vty_addr", required_argument, NULL, 'A'},
47 { "vty_port", required_argument, NULL, 'P'},
48 { "retain", no_argument, NULL, 'r'},
edd7c245 49 { "user", required_argument, NULL, 'u'},
c065230a 50 { "group", required_argument, NULL, 'g'},
718e3744 51 { "version", no_argument, NULL, 'v'},
52 { 0 }
53};
54
edd7c245 55/* ripd privileges */
56zebra_capabilities_t _caps_p [] =
57{
ceacedba 58 ZCAP_NET_RAW,
edd7c245 59 ZCAP_BIND
60};
61
62struct zebra_privs_t ripd_privs =
63{
d81fadfd 64#if defined(QUAGGA_USER)
65 .user = QUAGGA_USER,
edd7c245 66#endif
d81fadfd 67#if defined QUAGGA_GROUP
68 .group = QUAGGA_GROUP,
69#endif
70#ifdef VTY_GROUP
71 .vty_group = VTY_GROUP,
edd7c245 72#endif
73 .caps_p = _caps_p,
74 .cap_num_p = 2,
75 .cap_num_i = 0
76};
77
718e3744 78/* Configuration file and directory. */
718e3744 79char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
80char *config_file = NULL;
81
82/* ripd program name */
83
84/* Route retain mode flag. */
85int retain_mode = 0;
86
87/* RIP VTY bind address. */
88char *vty_addr = NULL;
89
90/* RIP VTY connection port. */
91int vty_port = RIP_VTY_PORT;
92
93/* Master of threads. */
94struct thread_master *master;
95
96/* Process ID saved for use by init system */
8a676be3 97const char *pid_file = PATH_RIPD_PID;
718e3744 98
99/* Help information display. */
100static void
101usage (char *progname, int status)
102{
103 if (status != 0)
104 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
105 else
106 {
107 printf ("Usage : %s [OPTION...]\n\
108Daemon which manages RIP version 1 and 2.\n\n\
109-d, --daemon Runs in daemon mode\n\
110-f, --config_file Set configuration file name\n\
111-i, --pid_file Set process identifier file name\n\
112-A, --vty_addr Set vty's bind address\n\
113-P, --vty_port Set vty's port number\n\
876b8be0 114-C, --dryrun Check configuration for validity and exit\n\
718e3744 115-r, --retain When program terminates, retain added route by ripd.\n\
c065230a 116-u, --user User to run as\n\
117-g, --group Group to run as\n\
718e3744 118-v, --version Print program version\n\
119-h, --help Display this help and exit\n\
120\n\
121Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
122 }
123
124 exit (status);
125}
126\f
718e3744 127/* SIGHUP handler. */
dc63bfd4 128static void
2d75d052 129sighup (void)
718e3744 130{
131 zlog_info ("SIGHUP received");
132 rip_clean ();
133 rip_reset ();
134 zlog_info ("ripd restarting!");
135
136 /* Reload config file. */
320ec10a 137 vty_read_config (config_file, config_default);
718e3744 138
139 /* Create VTY's socket */
140 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
141
142 /* Try to return to normal operation. */
143}
144
145/* SIGINT handler. */
dc63bfd4 146static void
2d75d052 147sigint (void)
718e3744 148{
887c44a4 149 zlog_notice ("Terminating on signal");
718e3744 150
151 if (! retain_mode)
152 rip_clean ();
153
154 exit (0);
155}
156
157/* SIGUSR1 handler. */
dc63bfd4 158static void
2d75d052 159sigusr1 (void)
718e3744 160{
161 zlog_rotate (NULL);
162}
163
dc63bfd4 164static struct quagga_signal_t ripd_signals[] =
718e3744 165{
2d75d052 166 {
167 .signal = SIGHUP,
168 .handler = &sighup,
169 },
170 {
171 .signal = SIGUSR1,
172 .handler = &sigusr1,
173 },
174 {
175 .signal = SIGINT,
8c903fbb 176 .handler = &sigint,
2d75d052 177 },
f571dab0 178 {
179 .signal = SIGTERM,
180 .handler = &sigint,
181 },
2d75d052 182};
718e3744 183\f
184/* Main routine of ripd. */
185int
186main (int argc, char **argv)
187{
188 char *p;
189 int daemon_mode = 0;
876b8be0 190 int dryrun = 0;
718e3744 191 char *progname;
192 struct thread thread;
193
194 /* Set umask before anything for security */
195 umask (0027);
196
197 /* Get program name. */
198 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
199
200 /* First of all we need logging init. */
274a4a44 201 zlog_default = openzlog (progname, ZLOG_RIP,
718e3744 202 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
203
204 /* Command line option parse. */
205 while (1)
206 {
207 int opt;
208
876b8be0 209 opt = getopt_long (argc, argv, "df:i:hA:P:u:g:rvC", longopts, 0);
718e3744 210
211 if (opt == EOF)
212 break;
213
214 switch (opt)
215 {
216 case 0:
217 break;
218 case 'd':
219 daemon_mode = 1;
220 break;
221 case 'f':
222 config_file = optarg;
223 break;
224 case 'A':
225 vty_addr = optarg;
226 break;
227 case 'i':
228 pid_file = optarg;
229 break;
230 case 'P':
4fc4e7ab 231 /* Deal with atoi() returning 0 on failure, and ripd not
232 listening on rip port... */
233 if (strcmp(optarg, "0") == 0)
234 {
235 vty_port = 0;
236 break;
237 }
238 vty_port = atoi (optarg);
239 vty_port = (vty_port ? vty_port : RIP_VTY_PORT);
718e3744 240 break;
241 case 'r':
242 retain_mode = 1;
243 break;
876b8be0
PJ
244 case 'C':
245 dryrun = 1;
246 break;
c065230a 247 case 'u':
248 ripd_privs.user = optarg;
249 break;
250 case 'g':
251 ripd_privs.group = optarg;
252 break;
718e3744 253 case 'v':
254 print_version (progname);
255 exit (0);
256 break;
257 case 'h':
258 usage (progname, 0);
259 break;
260 default:
261 usage (progname, 1);
262 break;
263 }
264 }
265
266 /* Prepare master thread. */
267 master = thread_master_create ();
268
269 /* Library initialization. */
edd7c245 270 zprivs_init (&ripd_privs);
2d75d052 271 signal_init (master, Q_SIGC(ripd_signals), ripd_signals);
718e3744 272 cmd_init (1);
b21b19c5 273 vty_init (master);
718e3744 274 memory_init ();
275 keychain_init ();
276
277 /* RIP related initialization. */
278 rip_init ();
279 rip_if_init ();
280 rip_zclient_init ();
281 rip_peer_init ();
282
283 /* Sort all installed commands. */
284 sort_node ();
285
286 /* Get configuration file. */
320ec10a 287 vty_read_config (config_file, config_default);
718e3744 288
876b8be0
PJ
289 /* Start execution only if not in dry-run mode */
290 if(dryrun)
291 return (0);
292
718e3744 293 /* Change to the daemon program. */
294 if (daemon_mode)
295 daemon (0, 0);
296
297 /* Pid file create. */
298 pid_output (pid_file);
299
300 /* Create VTY's socket */
301 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
302
887c44a4 303 /* Print banner. */
304 zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
305
718e3744 306 /* Execute each thread. */
307 while (thread_fetch (master, &thread))
308 thread_call (&thread);
309
310 /* Not reached. */
e8e1946e 311 return (0);
718e3744 312}