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