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