3 * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF")
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
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.
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
27 #include "lib/version.h"
28 #include "lib/command.h"
34 DEFINE_MGROUP(BFDD
, "Bidirectional Forwarding Detection Daemon")
35 DEFINE_MTYPE(BFDD
, BFDD_CONTROL
, "long-lived control socket memory")
36 DEFINE_MTYPE(BFDD
, BFDD_NOTIFICATION
, "short-lived control notification data")
38 /* Master of threads. */
39 struct thread_master
*master
;
42 static zebra_capabilities_t _caps_p
[] = {ZCAP_BIND
, ZCAP_SYS_ADMIN
, ZCAP_NET_RAW
};
44 /* BFD daemon information. */
45 static struct frr_daemon_info bfdd_di
;
47 void socket_close(int *s
)
53 log_error("%s: close(%d): (%d) %s", __func__
, *s
, errno
,
59 static void sigusr1_handler(void)
64 static void sigterm_handler(void)
66 /* Signalize shutdown. */
69 /* Stop receiving message from zebra. */
72 /* Shutdown controller to avoid receiving anymore commands. */
75 /* Shutdown and free all protocol related memory. */
80 /* Terminate and free() FRR related memory. */
86 static void sighup_handler(void)
88 zlog_info("SIGHUP received");
90 /* Reload config file. */
91 vty_read_config(NULL
, bfdd_di
.config_file
, config_default
);
94 static struct quagga_signal_t bfd_signals
[] = {
97 .handler
= &sigusr1_handler
,
101 .handler
= &sigterm_handler
,
105 .handler
= &sigterm_handler
,
109 .handler
= &sighup_handler
,
113 static const struct frr_yang_module_info
*const bfdd_yang_modules
[] = {
118 FRR_DAEMON_INFO(bfdd
, BFD
, .vty_port
= 2617,
119 .proghelp
= "Implementation of the BFD protocol.",
120 .signals
= bfd_signals
, .n_signals
= array_size(bfd_signals
),
121 .privs
= &bglobal
.bfdd_privs
,
122 .yang_modules
= bfdd_yang_modules
,
123 .n_yang_modules
= array_size(bfdd_yang_modules
))
125 #define OPTION_CTLSOCK 1001
126 static const struct option longopts
[] = {
127 {"bfdctl", required_argument
, NULL
, OPTION_CTLSOCK
},
133 * BFD daemon related code.
135 struct bfd_global bglobal
;
137 const struct bfd_diag_str_list diag_list
[] = {
138 {.str
= "control-expired", .type
= BD_CONTROL_EXPIRED
},
139 {.str
= "echo-failed", .type
= BD_ECHO_FAILED
},
140 {.str
= "neighbor-down", .type
= BD_NEIGHBOR_DOWN
},
141 {.str
= "forwarding-reset", .type
= BD_FORWARDING_RESET
},
142 {.str
= "path-down", .type
= BD_PATH_DOWN
},
143 {.str
= "concatenated-path-down", .type
= BD_CONCATPATH_DOWN
},
144 {.str
= "administratively-down", .type
= BD_ADMIN_DOWN
},
145 {.str
= "reverse-concat-path-down", .type
= BD_REVCONCATPATH_DOWN
},
149 const struct bfd_state_str_list state_list
[] = {
150 {.str
= "admin-down", .type
= PTM_BFD_ADM_DOWN
},
151 {.str
= "down", .type
= PTM_BFD_DOWN
},
152 {.str
= "init", .type
= PTM_BFD_INIT
},
153 {.str
= "up", .type
= PTM_BFD_UP
},
158 static void bg_init(void)
160 struct zebra_privs_t bfdd_privs
= {
161 #if defined(FRR_USER) && defined(FRR_GROUP)
165 #if defined(VTY_GROUP)
166 .vty_group
= VTY_GROUP
,
169 .cap_num_p
= array_size(_caps_p
),
173 TAILQ_INIT(&bglobal
.bg_bcslist
);
174 TAILQ_INIT(&bglobal
.bg_obslist
);
176 memcpy(&bglobal
.bfdd_privs
, &bfdd_privs
,
180 int main(int argc
, char *argv
[])
183 bool ctlsockused
= false;
186 /* Initialize system sockets. */
189 frr_preinit(&bfdd_di
, argc
, argv
);
190 frr_opt_add("", longopts
,
191 " --bfdctl Specify bfdd control socket\n");
193 snprintf(ctl_path
, sizeof(ctl_path
), BFDD_CONTROL_SOCKET
,
196 opt
= frr_getopt(argc
, argv
, NULL
);
202 strlcpy(ctl_path
, optarg
, sizeof(ctl_path
));
212 if (bfdd_di
.pathspace
&& !ctlsockused
)
213 snprintf(ctl_path
, sizeof(ctl_path
), BFDD_CONTROL_SOCKET
,
214 "/", bfdd_di
.pathspace
);
216 #if 0 /* TODO add support for JSON configuration files. */
220 /* Initialize FRR infrastructure. */
223 /* Initialize control socket. */
224 control_init(ctl_path
);
226 /* Initialize BFD data structures. */
233 /* Initialize zebra connection. */
234 bfdd_zclient_init(&bglobal
.bfdd_privs
);
236 thread_add_read(master
, control_accept
, NULL
, bglobal
.bg_csock
,
237 &bglobal
.bg_csockev
);
239 /* Install commands. */
242 /* read configuration file and daemonize */