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