]> git.proxmox.com Git - mirror_frr.git/blame - bfdd/bfdd.c
Merge pull request #5097 from ton31337/fix/do_not_reconnect_if_prefix_overflow
[mirror_frr.git] / bfdd / bfdd.c
CommitLineData
e9e2c950
RZ
1/*
2 * BFD daemon code
3 * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF")
4 *
5 * FRR is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
8 * later version.
9 *
10 * FRR is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with FRR; see the file COPYING. If not, write to the Free
17 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 * 02111-1307, USA.
19 */
20
21#include <zebra.h>
22
9f95a33a
DS
23#include "filter.h"
24
e9e2c950
RZ
25#include "bfd.h"
26#include "lib/version.h"
6ed84949 27
e9e2c950
RZ
28
29/*
30 * FRR related code.
31 */
1b88c3cb
DL
32DEFINE_MGROUP(BFDD, "Bidirectional Forwarding Detection Daemon")
33DEFINE_MTYPE(BFDD, BFDD_CONTROL, "long-lived control socket memory")
34DEFINE_MTYPE(BFDD, BFDD_NOTIFICATION, "short-lived control notification data")
e9e2c950
RZ
35
36/* Master of threads. */
37struct thread_master *master;
38
39/* BFDd privileges */
4e6b48d3 40static zebra_capabilities_t _caps_p[] = {ZCAP_BIND, ZCAP_SYS_ADMIN, ZCAP_NET_RAW};
e9e2c950 41
49cc9e7b
RZ
42/* BFD daemon information. */
43static struct frr_daemon_info bfdd_di;
44
e9e2c950
RZ
45void socket_close(int *s)
46{
47 if (*s <= 0)
48 return;
49
50 if (close(*s) != 0)
51 log_error("%s: close(%d): (%d) %s", __func__, *s, errno,
52 strerror(errno));
53
54 *s = -1;
55}
56
57static void sigusr1_handler(void)
58{
59 zlog_rotate();
60}
61
62static void sigterm_handler(void)
63{
64 /* Signalize shutdown. */
65 frr_early_fini();
66
d3af6147
RZ
67 /* Stop receiving message from zebra. */
68 bfdd_zclient_stop();
69
e9e2c950
RZ
70 /* Shutdown controller to avoid receiving anymore commands. */
71 control_shutdown();
72
73 /* Shutdown and free all protocol related memory. */
74 bfd_shutdown();
75
7bcadbae 76 bfd_vrf_terminate();
e9e2c950
RZ
77
78 /* Terminate and free() FRR related memory. */
79 frr_fini();
80
81 exit(0);
82}
83
49cc9e7b
RZ
84static void sighup_handler(void)
85{
86 zlog_info("SIGHUP received");
87
88 /* Reload config file. */
89 vty_read_config(NULL, bfdd_di.config_file, config_default);
90}
91
e9e2c950
RZ
92static struct quagga_signal_t bfd_signals[] = {
93 {
94 .signal = SIGUSR1,
95 .handler = &sigusr1_handler,
96 },
97 {
98 .signal = SIGTERM,
99 .handler = &sigterm_handler,
100 },
101 {
102 .signal = SIGINT,
103 .handler = &sigterm_handler,
104 },
49cc9e7b
RZ
105 {
106 .signal = SIGHUP,
107 .handler = &sighup_handler,
108 },
e9e2c950
RZ
109};
110
adc26455
RZ
111static const struct frr_yang_module_info *bfdd_yang_modules[] = {
112 &frr_interface_info,
113 &frr_bfdd_info,
114};
115
e9e2c950
RZ
116FRR_DAEMON_INFO(bfdd, BFD, .vty_port = 2617,
117 .proghelp = "Implementation of the BFD protocol.",
118 .signals = bfd_signals, .n_signals = array_size(bfd_signals),
adc26455
RZ
119 .privs = &bglobal.bfdd_privs,
120 .yang_modules = bfdd_yang_modules,
121 .n_yang_modules = array_size(bfdd_yang_modules))
e9e2c950
RZ
122
123#define OPTION_CTLSOCK 1001
124static struct option longopts[] = {
125 {"bfdctl", required_argument, NULL, OPTION_CTLSOCK},
126 {0}
127};
128
129
130/*
131 * BFD daemon related code.
132 */
133struct bfd_global bglobal;
134
135struct bfd_diag_str_list diag_list[] = {
40675ea9
RZ
136 {.str = "control-expired", .type = BD_CONTROL_EXPIRED},
137 {.str = "echo-failed", .type = BD_ECHO_FAILED},
138 {.str = "neighbor-down", .type = BD_NEIGHBOR_DOWN},
139 {.str = "forwarding-reset", .type = BD_FORWARDING_RESET},
140 {.str = "path-down", .type = BD_PATH_DOWN},
141 {.str = "concatenated-path-down", .type = BD_CONCATPATH_DOWN},
142 {.str = "administratively-down", .type = BD_ADMIN_DOWN},
143 {.str = "reverse-concat-path-down", .type = BD_REVCONCATPATH_DOWN},
e9e2c950
RZ
144 {.str = NULL},
145};
146
147struct bfd_state_str_list state_list[] = {
40675ea9
RZ
148 {.str = "admin-down", .type = PTM_BFD_ADM_DOWN},
149 {.str = "down", .type = PTM_BFD_DOWN},
150 {.str = "init", .type = PTM_BFD_INIT},
151 {.str = "up", .type = PTM_BFD_UP},
e9e2c950
RZ
152 {.str = NULL},
153};
154
155
156static void bg_init(void)
157{
f21536d2
PG
158 struct zebra_privs_t bfdd_privs = {
159#if defined(FRR_USER) && defined(FRR_GROUP)
160 .user = FRR_USER,
161 .group = FRR_GROUP,
162#endif
163#if defined(VTY_GROUP)
164 .vty_group = VTY_GROUP,
165#endif
166 .caps_p = _caps_p,
167 .cap_num_p = array_size(_caps_p),
168 .cap_num_i = 0,
169 };
170
e9e2c950 171 TAILQ_INIT(&bglobal.bg_bcslist);
d245e522 172 TAILQ_INIT(&bglobal.bg_obslist);
f21536d2
PG
173
174 memcpy(&bglobal.bfdd_privs, &bfdd_privs,
175 sizeof(bfdd_privs));
e9e2c950
RZ
176}
177
178int main(int argc, char *argv[])
179{
89277ebf
DS
180 char ctl_path[512];
181 bool ctlsockused = false;
e9e2c950
RZ
182 int opt;
183
f21536d2
PG
184 /* Initialize system sockets. */
185 bg_init();
186
e9e2c950
RZ
187 frr_preinit(&bfdd_di, argc, argv);
188 frr_opt_add("", longopts,
189 " --bfdctl Specify bfdd control socket\n");
190
89277ebf
DS
191 snprintf(ctl_path, sizeof(ctl_path), BFDD_CONTROL_SOCKET,
192 "", "");
e9e2c950
RZ
193 while (true) {
194 opt = frr_getopt(argc, argv, NULL);
195 if (opt == EOF)
196 break;
197
198 switch (opt) {
199 case OPTION_CTLSOCK:
89277ebf
DS
200 strlcpy(ctl_path, optarg, sizeof(ctl_path));
201 ctlsockused = true;
e9e2c950
RZ
202 break;
203
204 default:
205 frr_help_exit(1);
206 break;
207 }
208 }
209
89277ebf
DS
210 if (bfdd_di.pathspace && !ctlsockused)
211 snprintf(ctl_path, sizeof(ctl_path), BFDD_CONTROL_SOCKET,
212 "/", bfdd_di.pathspace);
213
e9e2c950
RZ
214#if 0 /* TODO add support for JSON configuration files. */
215 parse_config(conf);
216#endif
217
218 /* Initialize logging API. */
219 log_init(1, BLOG_DEBUG, &bfdd_di);
220
e9e2c950
RZ
221 /* Initialize control socket. */
222 control_init(ctl_path);
223
224 /* Initialize FRR infrastructure. */
225 master = frr_init();
226
227 /* Initialize BFD data structures. */
228 bfd_initialize();
229
9fc0bc5c
PG
230 bfd_vrf_init();
231
9f95a33a
DS
232 access_list_init();
233
d3af6147 234 /* Initialize zebra connection. */
f21536d2 235 bfdd_zclient_init(&bglobal.bfdd_privs);
d3af6147 236
e9e2c950
RZ
237 thread_add_read(master, control_accept, NULL, bglobal.bg_csock,
238 &bglobal.bg_csockev);
239
c2f29cf3
RZ
240 /* Install commands. */
241 bfdd_vty_init();
242
e9e2c950
RZ
243 /* read configuration file and daemonize */
244 frr_config_fork();
245
246 frr_run(master);
247 /* NOTREACHED */
248
249 return 0;
250}