]>
Commit | Line | Data |
---|---|---|
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 | 25 | #include "bfd.h" |
6c574029 | 26 | #include "bfdd_nb.h" |
e9e2c950 | 27 | #include "lib/version.h" |
0bdeb5e5 | 28 | #include "lib/command.h" |
6ed84949 | 29 | |
e9e2c950 RZ |
30 | |
31 | /* | |
32 | * FRR related code. | |
33 | */ | |
1b88c3cb DL |
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") | |
e9e2c950 RZ |
37 | |
38 | /* Master of threads. */ | |
39 | struct thread_master *master; | |
40 | ||
41 | /* BFDd privileges */ | |
4e6b48d3 | 42 | static zebra_capabilities_t _caps_p[] = {ZCAP_BIND, ZCAP_SYS_ADMIN, ZCAP_NET_RAW}; |
e9e2c950 | 43 | |
49cc9e7b RZ |
44 | /* BFD daemon information. */ |
45 | static struct frr_daemon_info bfdd_di; | |
46 | ||
e9e2c950 RZ |
47 | void socket_close(int *s) |
48 | { | |
49 | if (*s <= 0) | |
50 | return; | |
51 | ||
52 | if (close(*s) != 0) | |
53 | log_error("%s: close(%d): (%d) %s", __func__, *s, errno, | |
54 | strerror(errno)); | |
55 | ||
56 | *s = -1; | |
57 | } | |
58 | ||
59 | static void sigusr1_handler(void) | |
60 | { | |
61 | zlog_rotate(); | |
62 | } | |
63 | ||
64 | static void sigterm_handler(void) | |
65 | { | |
66 | /* Signalize shutdown. */ | |
67 | frr_early_fini(); | |
68 | ||
d3af6147 RZ |
69 | /* Stop receiving message from zebra. */ |
70 | bfdd_zclient_stop(); | |
71 | ||
e9e2c950 RZ |
72 | /* Shutdown controller to avoid receiving anymore commands. */ |
73 | control_shutdown(); | |
74 | ||
75 | /* Shutdown and free all protocol related memory. */ | |
76 | bfd_shutdown(); | |
77 | ||
7bcadbae | 78 | bfd_vrf_terminate(); |
e9e2c950 RZ |
79 | |
80 | /* Terminate and free() FRR related memory. */ | |
81 | frr_fini(); | |
82 | ||
83 | exit(0); | |
84 | } | |
85 | ||
49cc9e7b RZ |
86 | static void sighup_handler(void) |
87 | { | |
88 | zlog_info("SIGHUP received"); | |
89 | ||
90 | /* Reload config file. */ | |
91 | vty_read_config(NULL, bfdd_di.config_file, config_default); | |
92 | } | |
93 | ||
e9e2c950 RZ |
94 | static struct quagga_signal_t bfd_signals[] = { |
95 | { | |
96 | .signal = SIGUSR1, | |
97 | .handler = &sigusr1_handler, | |
98 | }, | |
99 | { | |
100 | .signal = SIGTERM, | |
101 | .handler = &sigterm_handler, | |
102 | }, | |
103 | { | |
104 | .signal = SIGINT, | |
105 | .handler = &sigterm_handler, | |
106 | }, | |
49cc9e7b RZ |
107 | { |
108 | .signal = SIGHUP, | |
109 | .handler = &sighup_handler, | |
110 | }, | |
e9e2c950 RZ |
111 | }; |
112 | ||
0d8c7a26 | 113 | static const struct frr_yang_module_info *const bfdd_yang_modules[] = { |
adc26455 RZ |
114 | &frr_interface_info, |
115 | &frr_bfdd_info, | |
116 | }; | |
117 | ||
e9e2c950 RZ |
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), | |
adc26455 RZ |
121 | .privs = &bglobal.bfdd_privs, |
122 | .yang_modules = bfdd_yang_modules, | |
123 | .n_yang_modules = array_size(bfdd_yang_modules)) | |
e9e2c950 RZ |
124 | |
125 | #define OPTION_CTLSOCK 1001 | |
2b64873d | 126 | static const struct option longopts[] = { |
e9e2c950 RZ |
127 | {"bfdctl", required_argument, NULL, OPTION_CTLSOCK}, |
128 | {0} | |
129 | }; | |
130 | ||
131 | ||
132 | /* | |
133 | * BFD daemon related code. | |
134 | */ | |
135 | struct bfd_global bglobal; | |
136 | ||
2b64873d | 137 | const struct bfd_diag_str_list diag_list[] = { |
40675ea9 RZ |
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}, | |
e9e2c950 RZ |
146 | {.str = NULL}, |
147 | }; | |
148 | ||
2b64873d | 149 | const struct bfd_state_str_list state_list[] = { |
40675ea9 RZ |
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}, | |
e9e2c950 RZ |
154 | {.str = NULL}, |
155 | }; | |
156 | ||
157 | ||
158 | static void bg_init(void) | |
159 | { | |
f21536d2 PG |
160 | struct zebra_privs_t bfdd_privs = { |
161 | #if defined(FRR_USER) && defined(FRR_GROUP) | |
162 | .user = FRR_USER, | |
163 | .group = FRR_GROUP, | |
164 | #endif | |
165 | #if defined(VTY_GROUP) | |
166 | .vty_group = VTY_GROUP, | |
167 | #endif | |
168 | .caps_p = _caps_p, | |
169 | .cap_num_p = array_size(_caps_p), | |
170 | .cap_num_i = 0, | |
171 | }; | |
172 | ||
e9e2c950 | 173 | TAILQ_INIT(&bglobal.bg_bcslist); |
d245e522 | 174 | TAILQ_INIT(&bglobal.bg_obslist); |
f21536d2 PG |
175 | |
176 | memcpy(&bglobal.bfdd_privs, &bfdd_privs, | |
177 | sizeof(bfdd_privs)); | |
e9e2c950 RZ |
178 | } |
179 | ||
180 | int main(int argc, char *argv[]) | |
181 | { | |
89277ebf DS |
182 | char ctl_path[512]; |
183 | bool ctlsockused = false; | |
e9e2c950 RZ |
184 | int opt; |
185 | ||
f21536d2 PG |
186 | /* Initialize system sockets. */ |
187 | bg_init(); | |
188 | ||
e9e2c950 RZ |
189 | frr_preinit(&bfdd_di, argc, argv); |
190 | frr_opt_add("", longopts, | |
191 | " --bfdctl Specify bfdd control socket\n"); | |
192 | ||
89277ebf DS |
193 | snprintf(ctl_path, sizeof(ctl_path), BFDD_CONTROL_SOCKET, |
194 | "", ""); | |
e9e2c950 RZ |
195 | while (true) { |
196 | opt = frr_getopt(argc, argv, NULL); | |
197 | if (opt == EOF) | |
198 | break; | |
199 | ||
200 | switch (opt) { | |
201 | case OPTION_CTLSOCK: | |
89277ebf DS |
202 | strlcpy(ctl_path, optarg, sizeof(ctl_path)); |
203 | ctlsockused = true; | |
e9e2c950 RZ |
204 | break; |
205 | ||
206 | default: | |
207 | frr_help_exit(1); | |
208 | break; | |
209 | } | |
210 | } | |
211 | ||
89277ebf DS |
212 | if (bfdd_di.pathspace && !ctlsockused) |
213 | snprintf(ctl_path, sizeof(ctl_path), BFDD_CONTROL_SOCKET, | |
214 | "/", bfdd_di.pathspace); | |
215 | ||
e9e2c950 RZ |
216 | #if 0 /* TODO add support for JSON configuration files. */ |
217 | parse_config(conf); | |
218 | #endif | |
219 | ||
d85b048d DL |
220 | /* Initialize FRR infrastructure. */ |
221 | master = frr_init(); | |
e9e2c950 | 222 | |
e9e2c950 RZ |
223 | /* Initialize control socket. */ |
224 | control_init(ctl_path); | |
225 | ||
e9e2c950 RZ |
226 | /* Initialize BFD data structures. */ |
227 | bfd_initialize(); | |
228 | ||
9fc0bc5c PG |
229 | bfd_vrf_init(); |
230 | ||
9f95a33a DS |
231 | access_list_init(); |
232 | ||
d3af6147 | 233 | /* Initialize zebra connection. */ |
f21536d2 | 234 | bfdd_zclient_init(&bglobal.bfdd_privs); |
d3af6147 | 235 | |
e9e2c950 RZ |
236 | thread_add_read(master, control_accept, NULL, bglobal.bg_csock, |
237 | &bglobal.bg_csockev); | |
238 | ||
c2f29cf3 RZ |
239 | /* Install commands. */ |
240 | bfdd_vty_init(); | |
241 | ||
e9e2c950 RZ |
242 | /* read configuration file and daemonize */ |
243 | frr_config_fork(); | |
244 | ||
245 | frr_run(master); | |
246 | /* NOTREACHED */ | |
247 | ||
248 | return 0; | |
249 | } |