]>
Commit | Line | Data |
---|---|---|
e9e2c950 RZ |
1 | /********************************************************************* |
2 | * Copyright 2013 Cumulus Networks, LLC. All rights reserved. | |
3 | * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved. | |
4 | * | |
5 | * This program 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 Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | * | |
19 | * bfd.c: implements the BFD protocol. | |
20 | * | |
21 | * Authors | |
22 | * ------- | |
23 | * Shrijeet Mukherjee [shm@cumulusnetworks.com] | |
24 | * Kanna Rajagopal [kanna@cumulusnetworks.com] | |
25 | * Radhika Mahankali [Radhika@cumulusnetworks.com] | |
26 | */ | |
27 | ||
28 | #include <zebra.h> | |
29 | ||
e9e2c950 RZ |
30 | #include "lib/jhash.h" |
31 | ||
32 | #include "bfd.h" | |
33 | ||
1b88c3cb DL |
34 | DEFINE_MTYPE_STATIC(BFDD, BFDD_CONFIG, "long-lived configuration memory") |
35 | DEFINE_MTYPE_STATIC(BFDD, BFDD_SESSION_OBSERVER, "Session observer") | |
36 | DEFINE_MTYPE_STATIC(BFDD, BFDD_VRF, "BFD VRF") | |
e9e2c950 RZ |
37 | |
38 | /* | |
39 | * Prototypes | |
40 | */ | |
41 | static uint32_t ptm_bfd_gen_ID(void); | |
42 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd); | |
e9e2c950 RZ |
43 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, |
44 | uint32_t ldisc); | |
45 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc); | |
46 | static const char *get_diag_str(int diag); | |
47 | ||
9f37770f RZ |
48 | static void bs_admin_down_handler(struct bfd_session *bs, int nstate); |
49 | static void bs_down_handler(struct bfd_session *bs, int nstate); | |
50 | static void bs_init_handler(struct bfd_session *bs, int nstate); | |
51 | static void bs_up_handler(struct bfd_session *bs, int nstate); | |
52 | ||
79b4a6fc RZ |
53 | /* Zeroed array with the size of an IPv6 address. */ |
54 | struct in6_addr zero_addr; | |
e9e2c950 RZ |
55 | |
56 | /* | |
57 | * Functions | |
58 | */ | |
79b4a6fc RZ |
59 | void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer, |
60 | struct sockaddr_any *local, bool mhop, const char *ifname, | |
61 | const char *vrfname) | |
d245e522 | 62 | { |
79b4a6fc | 63 | memset(key, 0, sizeof(*key)); |
d245e522 | 64 | |
79b4a6fc RZ |
65 | switch (peer->sa_sin.sin_family) { |
66 | case AF_INET: | |
67 | key->family = AF_INET; | |
68 | memcpy(&key->peer, &peer->sa_sin.sin_addr, | |
69 | sizeof(peer->sa_sin.sin_addr)); | |
70 | memcpy(&key->local, &local->sa_sin.sin_addr, | |
71 | sizeof(local->sa_sin.sin_addr)); | |
72 | break; | |
73 | case AF_INET6: | |
74 | key->family = AF_INET6; | |
75 | memcpy(&key->peer, &peer->sa_sin6.sin6_addr, | |
76 | sizeof(peer->sa_sin6.sin6_addr)); | |
77 | memcpy(&key->local, &local->sa_sin6.sin6_addr, | |
78 | sizeof(local->sa_sin6.sin6_addr)); | |
d245e522 RZ |
79 | break; |
80 | } | |
d245e522 | 81 | |
79b4a6fc RZ |
82 | key->mhop = mhop; |
83 | if (ifname && ifname[0]) | |
84 | strlcpy(key->ifname, ifname, sizeof(key->ifname)); | |
85 | if (vrfname && vrfname[0]) | |
86 | strlcpy(key->vrfname, vrfname, sizeof(key->vrfname)); | |
adc26455 RZ |
87 | else |
88 | strlcpy(key->vrfname, VRF_DEFAULT_NAME, sizeof(key->vrfname)); | |
d245e522 RZ |
89 | } |
90 | ||
e9e2c950 RZ |
91 | struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc) |
92 | { | |
93 | struct bfd_session *bs; | |
94 | struct peer_label *pl; | |
79b4a6fc | 95 | struct bfd_key key; |
e9e2c950 RZ |
96 | |
97 | /* Try to find label first. */ | |
98 | if (bpc->bpc_has_label) { | |
99 | pl = pl_find(bpc->bpc_label); | |
100 | if (pl != NULL) { | |
101 | bs = pl->pl_bs; | |
102 | return bs; | |
103 | } | |
104 | } | |
105 | ||
106 | /* Otherwise fallback to peer/local hash lookup. */ | |
79b4a6fc RZ |
107 | gen_bfd_key(&key, &bpc->bpc_peer, &bpc->bpc_local, bpc->bpc_mhop, |
108 | bpc->bpc_localif, bpc->bpc_vrfname); | |
e9e2c950 | 109 | |
79b4a6fc | 110 | return bfd_key_lookup(key); |
d245e522 RZ |
111 | } |
112 | ||
113 | /* | |
114 | * Starts a disabled BFD session. | |
115 | * | |
116 | * A session is disabled when the specified interface/VRF doesn't exist | |
117 | * yet. It might happen on FRR boot or with virtual interfaces. | |
118 | */ | |
119 | int bfd_session_enable(struct bfd_session *bs) | |
120 | { | |
d245e522 RZ |
121 | struct interface *ifp = NULL; |
122 | struct vrf *vrf = NULL; | |
123 | int psock; | |
124 | ||
125 | /* | |
126 | * If the interface or VRF doesn't exist, then we must register | |
127 | * the session but delay its start. | |
128 | */ | |
218afa36 PG |
129 | if (bs->key.vrfname[0]) { |
130 | vrf = vrf_lookup_by_name(bs->key.vrfname); | |
d245e522 | 131 | if (vrf == NULL) { |
79b4a6fc RZ |
132 | log_error( |
133 | "session-enable: specified VRF doesn't exists."); | |
d245e522 RZ |
134 | return 0; |
135 | } | |
136 | } | |
137 | ||
218afa36 PG |
138 | if (bs->key.ifname[0]) { |
139 | if (vrf) | |
140 | ifp = if_lookup_by_name(bs->key.ifname, vrf->vrf_id); | |
141 | else | |
142 | ifp = if_lookup_by_name_all_vrf(bs->key.ifname); | |
143 | if (ifp == NULL) { | |
79b4a6fc | 144 | log_error( |
218afa36 | 145 | "session-enable: specified interface doesn't exists."); |
d245e522 RZ |
146 | return 0; |
147 | } | |
218afa36 PG |
148 | if (bs->key.ifname[0] && !vrf) { |
149 | vrf = vrf_lookup_by_id(ifp->vrf_id); | |
150 | if (vrf == NULL) { | |
151 | log_error( | |
152 | "session-enable: specified VRF doesn't exists."); | |
153 | return 0; | |
154 | } | |
155 | } | |
d245e522 RZ |
156 | } |
157 | ||
158 | /* Assign interface/VRF pointers. */ | |
159 | bs->vrf = vrf; | |
160 | if (bs->vrf == NULL) | |
161 | bs->vrf = vrf_lookup_by_id(VRF_DEFAULT); | |
162 | ||
79b4a6fc RZ |
163 | if (bs->key.ifname[0] |
164 | && BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH) == 0) | |
d245e522 RZ |
165 | bs->ifp = ifp; |
166 | ||
261e0ba9 RZ |
167 | /* Sanity check: don't leak open sockets. */ |
168 | if (bs->sock != -1) { | |
1ac865e7 | 169 | log_debug("session-enable: previous socket open"); |
261e0ba9 RZ |
170 | close(bs->sock); |
171 | bs->sock = -1; | |
172 | } | |
173 | ||
d245e522 RZ |
174 | /* |
175 | * Get socket for transmitting control packets. Note that if we | |
176 | * could use the destination port (3784) for the source | |
177 | * port we wouldn't need a socket per session. | |
178 | */ | |
179 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6) == 0) { | |
180 | psock = bp_peer_socket(bs); | |
181 | if (psock == -1) | |
261e0ba9 | 182 | return 0; |
d245e522 RZ |
183 | } else { |
184 | psock = bp_peer_socketv6(bs); | |
185 | if (psock == -1) | |
261e0ba9 | 186 | return 0; |
d245e522 RZ |
187 | } |
188 | ||
189 | /* | |
190 | * We've got a valid socket, lets start the timers and the | |
191 | * protocol. | |
192 | */ | |
193 | bs->sock = psock; | |
194 | bfd_recvtimer_update(bs); | |
195 | ptm_bfd_start_xmt_timer(bs, false); | |
196 | ||
d245e522 RZ |
197 | return 0; |
198 | } | |
199 | ||
200 | /* | |
201 | * Disabled a running BFD session. | |
202 | * | |
203 | * A session is disabled when the specified interface/VRF gets removed | |
204 | * (e.g. virtual interfaces). | |
205 | */ | |
206 | void bfd_session_disable(struct bfd_session *bs) | |
207 | { | |
208 | /* Free up socket resources. */ | |
209 | if (bs->sock != -1) { | |
210 | close(bs->sock); | |
211 | bs->sock = -1; | |
212 | } | |
213 | ||
214 | /* Disable all timers. */ | |
215 | bfd_recvtimer_delete(bs); | |
216 | bfd_echo_recvtimer_delete(bs); | |
217 | bfd_xmttimer_delete(bs); | |
218 | bfd_echo_xmttimer_delete(bs); | |
e9e2c950 RZ |
219 | } |
220 | ||
221 | static uint32_t ptm_bfd_gen_ID(void) | |
222 | { | |
843b324d | 223 | uint32_t session_id; |
e9e2c950 | 224 | |
843b324d RZ |
225 | /* |
226 | * RFC 5880, Section 6.8.1. recommends that we should generate | |
227 | * random session identification numbers. | |
228 | */ | |
229 | do { | |
230 | session_id = ((random() << 16) & 0xFFFF0000) | |
231 | | (random() & 0x0000FFFF); | |
232 | } while (session_id == 0 || bfd_id_lookup(session_id) != NULL); | |
233 | ||
234 | return session_id; | |
e9e2c950 RZ |
235 | } |
236 | ||
237 | void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo) | |
238 | { | |
239 | uint64_t jitter, xmt_TO; | |
240 | int maxpercent; | |
241 | ||
242 | xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO; | |
243 | ||
244 | /* | |
245 | * From section 6.5.2: trasmit interval should be randomly jittered | |
246 | * between | |
247 | * 75% and 100% of nominal value, unless detect_mult is 1, then should | |
248 | * be | |
249 | * between 75% and 90%. | |
250 | */ | |
251 | maxpercent = (bfd->detect_mult == 1) ? 16 : 26; | |
252 | jitter = (xmt_TO * (75 + (random() % maxpercent))) / 100; | |
253 | /* XXX remove that division above */ | |
254 | ||
255 | if (is_echo) | |
256 | bfd_echo_xmttimer_update(bfd, jitter); | |
257 | else | |
258 | bfd_xmttimer_update(bfd, jitter); | |
259 | } | |
260 | ||
261 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd) | |
262 | { | |
263 | /* Send the scheduled echo packet */ | |
264 | ptm_bfd_echo_snd(bfd); | |
265 | ||
266 | /* Restart the timer for next time */ | |
267 | ptm_bfd_start_xmt_timer(bfd, true); | |
268 | } | |
269 | ||
270 | void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit) | |
271 | { | |
272 | /* Send the scheduled control packet */ | |
273 | ptm_bfd_snd(bfd, fbit); | |
274 | ||
275 | /* Restart the timer for next time */ | |
276 | ptm_bfd_start_xmt_timer(bfd, false); | |
277 | } | |
278 | ||
8bd859f6 | 279 | void ptm_bfd_echo_stop(struct bfd_session *bfd) |
e9e2c950 RZ |
280 | { |
281 | bfd->echo_xmt_TO = 0; | |
282 | bfd->echo_detect_TO = 0; | |
283 | BFD_UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE); | |
284 | ||
285 | bfd_echo_xmttimer_delete(bfd); | |
286 | bfd_echo_recvtimer_delete(bfd); | |
e9e2c950 RZ |
287 | } |
288 | ||
289 | void ptm_bfd_echo_start(struct bfd_session *bfd) | |
290 | { | |
291 | bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO); | |
451eb5a2 RZ |
292 | if (bfd->echo_detect_TO > 0) |
293 | ptm_bfd_echo_xmt_TO(bfd); | |
e9e2c950 RZ |
294 | } |
295 | ||
bc50bcc8 | 296 | void ptm_bfd_sess_up(struct bfd_session *bfd) |
e9e2c950 | 297 | { |
03e7f088 RZ |
298 | int old_state = bfd->ses_state; |
299 | ||
e9e2c950 RZ |
300 | bfd->local_diag = 0; |
301 | bfd->ses_state = PTM_BFD_UP; | |
e9e2c950 RZ |
302 | monotime(&bfd->uptime); |
303 | ||
c0ef9a8a RZ |
304 | /* Connection is up, lets negotiate timers. */ |
305 | bfd_set_polling(bfd); | |
306 | ||
307 | /* Start sending control packets with poll bit immediately. */ | |
308 | ptm_bfd_snd(bfd, 0); | |
e9e2c950 RZ |
309 | |
310 | control_notify(bfd); | |
311 | ||
0684c9b1 RZ |
312 | if (old_state != bfd->ses_state) { |
313 | bfd->stats.session_up++; | |
03e7f088 RZ |
314 | log_info("state-change: [%s] %s -> %s", bs_to_string(bfd), |
315 | state_list[old_state].str, | |
316 | state_list[bfd->ses_state].str); | |
0684c9b1 | 317 | } |
e9e2c950 RZ |
318 | } |
319 | ||
bc50bcc8 | 320 | void ptm_bfd_sess_dn(struct bfd_session *bfd, uint8_t diag) |
e9e2c950 RZ |
321 | { |
322 | int old_state = bfd->ses_state; | |
323 | ||
324 | bfd->local_diag = diag; | |
325 | bfd->discrs.remote_discr = 0; | |
326 | bfd->ses_state = PTM_BFD_DOWN; | |
327 | bfd->polling = 0; | |
328 | bfd->demand_mode = 0; | |
329 | monotime(&bfd->downtime); | |
330 | ||
331 | ptm_bfd_snd(bfd, 0); | |
332 | ||
b912b189 RZ |
333 | /* Slow down the control packets, the connection is down. */ |
334 | bs_set_slow_timers(bfd); | |
335 | ||
e9e2c950 RZ |
336 | /* only signal clients when going from up->down state */ |
337 | if (old_state == PTM_BFD_UP) | |
338 | control_notify(bfd); | |
339 | ||
e9e2c950 RZ |
340 | /* Stop echo packet transmission if they are active */ |
341 | if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) | |
8bd859f6 | 342 | ptm_bfd_echo_stop(bfd); |
03e7f088 | 343 | |
0684c9b1 RZ |
344 | if (old_state != bfd->ses_state) { |
345 | bfd->stats.session_down++; | |
03e7f088 RZ |
346 | log_info("state-change: [%s] %s -> %s reason:%s", |
347 | bs_to_string(bfd), state_list[old_state].str, | |
348 | state_list[bfd->ses_state].str, | |
349 | get_diag_str(bfd->local_diag)); | |
0684c9b1 | 350 | } |
e9e2c950 RZ |
351 | } |
352 | ||
e9e2c950 RZ |
353 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, |
354 | uint32_t ldisc) | |
355 | { | |
356 | struct bfd_session *bs; | |
357 | ||
358 | bs = bfd_id_lookup(ldisc); | |
359 | if (bs == NULL) | |
360 | return NULL; | |
361 | ||
79b4a6fc | 362 | switch (bs->key.family) { |
e9e2c950 | 363 | case AF_INET: |
79b4a6fc RZ |
364 | if (memcmp(&sa->sa_sin.sin_addr, &bs->key.peer, |
365 | sizeof(sa->sa_sin.sin_addr))) | |
366 | return NULL; | |
e9e2c950 RZ |
367 | break; |
368 | case AF_INET6: | |
79b4a6fc RZ |
369 | if (memcmp(&sa->sa_sin6.sin6_addr, &bs->key.peer, |
370 | sizeof(sa->sa_sin6.sin6_addr))) | |
371 | return NULL; | |
e9e2c950 RZ |
372 | break; |
373 | } | |
374 | ||
79b4a6fc | 375 | return bs; |
e9e2c950 RZ |
376 | } |
377 | ||
b333abc2 | 378 | struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp, |
e9e2c950 RZ |
379 | struct sockaddr_any *peer, |
380 | struct sockaddr_any *local, | |
b333abc2 RZ |
381 | ifindex_t ifindex, vrf_id_t vrfid, |
382 | bool is_mhop) | |
e9e2c950 | 383 | { |
79b4a6fc RZ |
384 | struct interface *ifp; |
385 | struct vrf *vrf; | |
386 | struct bfd_key key; | |
e9e2c950 | 387 | |
9d63adda RZ |
388 | /* Find our session using the ID signaled by the remote end. */ |
389 | if (cp->discrs.remote_discr) | |
390 | return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr)); | |
391 | ||
392 | /* Search for session without using discriminator. */ | |
79b4a6fc | 393 | ifp = if_lookup_by_index(ifindex, vrfid); |
3a20889f PG |
394 | |
395 | vrf = vrf_lookup_by_id(vrfid); | |
9d63adda | 396 | |
79b4a6fc | 397 | gen_bfd_key(&key, peer, local, is_mhop, ifp ? ifp->name : NULL, |
adc26455 | 398 | vrf ? vrf->name : VRF_DEFAULT_NAME); |
e9e2c950 | 399 | |
9d63adda | 400 | /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */ |
79b4a6fc | 401 | return bfd_key_lookup(key); |
e9e2c950 RZ |
402 | } |
403 | ||
e9e2c950 RZ |
404 | int bfd_xmt_cb(struct thread *t) |
405 | { | |
406 | struct bfd_session *bs = THREAD_ARG(t); | |
407 | ||
408 | ptm_bfd_xmt_TO(bs, 0); | |
409 | ||
410 | return 0; | |
411 | } | |
412 | ||
413 | int bfd_echo_xmt_cb(struct thread *t) | |
414 | { | |
415 | struct bfd_session *bs = THREAD_ARG(t); | |
416 | ||
451eb5a2 RZ |
417 | if (bs->echo_xmt_TO > 0) |
418 | ptm_bfd_echo_xmt_TO(bs); | |
e9e2c950 RZ |
419 | |
420 | return 0; | |
421 | } | |
422 | ||
423 | /* Was ptm_bfd_detect_TO() */ | |
424 | int bfd_recvtimer_cb(struct thread *t) | |
425 | { | |
426 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
427 | |
428 | switch (bs->ses_state) { | |
429 | case PTM_BFD_INIT: | |
430 | case PTM_BFD_UP: | |
bc50bcc8 | 431 | ptm_bfd_sess_dn(bs, BD_CONTROL_EXPIRED); |
e9e2c950 RZ |
432 | bfd_recvtimer_update(bs); |
433 | break; | |
434 | ||
435 | default: | |
436 | /* Second detect time expiration, zero remote discr (section | |
437 | * 6.5.1) | |
438 | */ | |
439 | bs->discrs.remote_discr = 0; | |
440 | break; | |
441 | } | |
442 | ||
e9e2c950 RZ |
443 | return 0; |
444 | } | |
445 | ||
446 | /* Was ptm_bfd_echo_detect_TO() */ | |
447 | int bfd_echo_recvtimer_cb(struct thread *t) | |
448 | { | |
449 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
450 | |
451 | switch (bs->ses_state) { | |
452 | case PTM_BFD_INIT: | |
453 | case PTM_BFD_UP: | |
bc50bcc8 | 454 | ptm_bfd_sess_dn(bs, BD_ECHO_FAILED); |
e9e2c950 RZ |
455 | break; |
456 | } | |
457 | ||
e9e2c950 RZ |
458 | return 0; |
459 | } | |
460 | ||
014cab13 | 461 | struct bfd_session *bfd_session_new(void) |
e9e2c950 RZ |
462 | { |
463 | struct bfd_session *bs; | |
464 | ||
465 | bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs)); | |
e9e2c950 | 466 | |
f43b9368 | 467 | bs->timers.desired_min_tx = BFD_DEFDESIREDMINTX; |
e9e2c950 RZ |
468 | bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX; |
469 | bs->timers.required_min_echo = BFD_DEF_REQ_MIN_ECHO; | |
470 | bs->detect_mult = BFD_DEFDETECTMULT; | |
471 | bs->mh_ttl = BFD_DEF_MHOP_TTL; | |
d245e522 | 472 | bs->ses_state = PTM_BFD_DOWN; |
e9e2c950 | 473 | |
b912b189 RZ |
474 | /* Initiate connection with slow timers. */ |
475 | bs_set_slow_timers(bs); | |
f43b9368 RZ |
476 | |
477 | /* Initiate remote settings as well. */ | |
478 | bs->remote_timers = bs->cur_timers; | |
479 | bs->remote_detect_mult = BFD_DEFDETECTMULT; | |
480 | ||
d245e522 | 481 | bs->sock = -1; |
e9e2c950 RZ |
482 | monotime(&bs->uptime); |
483 | bs->downtime = bs->uptime; | |
484 | ||
485 | return bs; | |
486 | } | |
487 | ||
488 | int bfd_session_update_label(struct bfd_session *bs, const char *nlabel) | |
489 | { | |
490 | /* New label treatment: | |
491 | * - Check if the label is taken; | |
492 | * - Try to allocate the memory for it and register; | |
493 | */ | |
494 | if (bs->pl == NULL) { | |
495 | if (pl_find(nlabel) != NULL) { | |
496 | /* Someone is already using it. */ | |
497 | return -1; | |
498 | } | |
499 | ||
500 | if (pl_new(nlabel, bs) == NULL) | |
501 | return -1; | |
502 | ||
503 | return 0; | |
504 | } | |
505 | ||
506 | /* | |
507 | * Test label change consistency: | |
508 | * - Do nothing if it's the same label; | |
509 | * - Check if the future label is already taken; | |
510 | * - Change label; | |
511 | */ | |
512 | if (strcmp(nlabel, bs->pl->pl_label) == 0) | |
513 | return -1; | |
514 | if (pl_find(nlabel) != NULL) | |
515 | return -1; | |
516 | ||
517 | strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label)); | |
518 | return 0; | |
519 | } | |
520 | ||
521 | static void _bfd_session_update(struct bfd_session *bs, | |
522 | struct bfd_peer_cfg *bpc) | |
523 | { | |
524 | if (bpc->bpc_echo) { | |
525 | /* Check if echo mode is already active. */ | |
526 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
527 | goto skip_echo; | |
528 | ||
529 | BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
e9e2c950 RZ |
530 | |
531 | /* Activate/update echo receive timeout timer. */ | |
73c62f8e | 532 | bs_echo_timer_handler(bs); |
e9e2c950 RZ |
533 | } else { |
534 | /* Check if echo mode is already disabled. */ | |
535 | if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
536 | goto skip_echo; | |
537 | ||
538 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
8bd859f6 | 539 | ptm_bfd_echo_stop(bs); |
e9e2c950 RZ |
540 | } |
541 | ||
542 | skip_echo: | |
543 | if (bpc->bpc_has_txinterval) | |
f43b9368 | 544 | bs->timers.desired_min_tx = bpc->bpc_txinterval * 1000; |
e9e2c950 RZ |
545 | |
546 | if (bpc->bpc_has_recvinterval) | |
547 | bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000; | |
548 | ||
549 | if (bpc->bpc_has_detectmultiplier) | |
550 | bs->detect_mult = bpc->bpc_detectmultiplier; | |
551 | ||
552 | if (bpc->bpc_has_echointerval) | |
553 | bs->timers.required_min_echo = bpc->bpc_echointerval * 1000; | |
554 | ||
555 | if (bpc->bpc_has_label) | |
556 | bfd_session_update_label(bs, bpc->bpc_label); | |
557 | ||
558 | if (bpc->bpc_shutdown) { | |
559 | /* Check if already shutdown. */ | |
560 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN)) | |
561 | return; | |
562 | ||
563 | BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
564 | ||
565 | /* Disable all events. */ | |
566 | bfd_recvtimer_delete(bs); | |
567 | bfd_echo_recvtimer_delete(bs); | |
568 | bfd_xmttimer_delete(bs); | |
569 | bfd_echo_xmttimer_delete(bs); | |
570 | ||
571 | /* Change and notify state change. */ | |
572 | bs->ses_state = PTM_BFD_ADM_DOWN; | |
573 | control_notify(bs); | |
574 | ||
d245e522 RZ |
575 | /* Don't try to send packets with a disabled session. */ |
576 | if (bs->sock != -1) | |
577 | ptm_bfd_snd(bs, 0); | |
e9e2c950 RZ |
578 | } else { |
579 | /* Check if already working. */ | |
580 | if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN)) | |
581 | return; | |
582 | ||
583 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
584 | ||
585 | /* Change and notify state change. */ | |
586 | bs->ses_state = PTM_BFD_DOWN; | |
587 | control_notify(bs); | |
588 | ||
589 | /* Enable all timers. */ | |
590 | bfd_recvtimer_update(bs); | |
591 | bfd_xmttimer_update(bs, bs->xmt_TO); | |
e9e2c950 | 592 | } |
9beff0bd PG |
593 | if (bpc->bpc_cbit) { |
594 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CBIT)) | |
595 | return; | |
596 | ||
597 | BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT); | |
598 | } else { | |
599 | if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CBIT)) | |
600 | return; | |
601 | ||
602 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT); | |
603 | } | |
e9e2c950 RZ |
604 | } |
605 | ||
606 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc) | |
607 | { | |
608 | /* User didn't want to update, return failure. */ | |
609 | if (bpc->bpc_createonly) | |
610 | return -1; | |
611 | ||
612 | _bfd_session_update(bs, bpc); | |
613 | ||
e9e2c950 RZ |
614 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); |
615 | ||
616 | return 0; | |
617 | } | |
618 | ||
014cab13 | 619 | void bfd_session_free(struct bfd_session *bs) |
e9e2c950 | 620 | { |
d245e522 | 621 | struct bfd_session_observer *bso; |
e9e2c950 | 622 | |
d245e522 | 623 | bfd_session_disable(bs); |
e9e2c950 | 624 | |
79b4a6fc RZ |
625 | bfd_key_delete(bs->key); |
626 | bfd_id_delete(bs->discrs.my_discr); | |
627 | ||
d245e522 RZ |
628 | /* Remove observer if any. */ |
629 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
630 | if (bso->bso_bs != bs) | |
631 | continue; | |
632 | ||
633 | break; | |
634 | } | |
635 | if (bso != NULL) | |
636 | bs_observer_del(bso); | |
e9e2c950 RZ |
637 | |
638 | pl_free(bs->pl); | |
639 | ||
e9e2c950 RZ |
640 | XFREE(MTYPE_BFDD_CONFIG, bs); |
641 | } | |
642 | ||
643 | struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc) | |
644 | { | |
645 | struct bfd_session *bfd, *l_bfd; | |
e9e2c950 RZ |
646 | |
647 | /* check to see if this needs a new session */ | |
648 | l_bfd = bs_peer_find(bpc); | |
649 | if (l_bfd) { | |
650 | /* Requesting a duplicated peer means update configuration. */ | |
651 | if (bfd_session_update(l_bfd, bpc) == 0) | |
652 | return l_bfd; | |
653 | else | |
654 | return NULL; | |
655 | } | |
656 | ||
d245e522 RZ |
657 | /* Get BFD session storage with its defaults. */ |
658 | bfd = bfd_session_new(); | |
e9e2c950 | 659 | if (bfd == NULL) { |
03e7f088 | 660 | log_error("session-new: allocation failed"); |
e9e2c950 RZ |
661 | return NULL; |
662 | } | |
663 | ||
d245e522 RZ |
664 | /* |
665 | * Store interface/VRF name in case we need to delay session | |
666 | * start. See `bfd_session_enable` for more information. | |
667 | */ | |
668 | if (bpc->bpc_has_localif) | |
79b4a6fc RZ |
669 | strlcpy(bfd->key.ifname, bpc->bpc_localif, |
670 | sizeof(bfd->key.ifname)); | |
e9e2c950 | 671 | |
d245e522 | 672 | if (bpc->bpc_has_vrfname) |
79b4a6fc RZ |
673 | strlcpy(bfd->key.vrfname, bpc->bpc_vrfname, |
674 | sizeof(bfd->key.vrfname)); | |
adc26455 RZ |
675 | else |
676 | strlcpy(bfd->key.vrfname, VRF_DEFAULT_NAME, | |
677 | sizeof(bfd->key.vrfname)); | |
e9e2c950 | 678 | |
d245e522 RZ |
679 | /* Copy remaining data. */ |
680 | if (bpc->bpc_ipv4 == false) | |
681 | BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6); | |
e9e2c950 | 682 | |
79b4a6fc RZ |
683 | bfd->key.family = (bpc->bpc_ipv4) ? AF_INET : AF_INET6; |
684 | switch (bfd->key.family) { | |
685 | case AF_INET: | |
686 | memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin.sin_addr, | |
687 | sizeof(bpc->bpc_peer.sa_sin.sin_addr)); | |
688 | memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin.sin_addr, | |
689 | sizeof(bpc->bpc_local.sa_sin.sin_addr)); | |
690 | break; | |
691 | ||
692 | case AF_INET6: | |
693 | memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin6.sin6_addr, | |
694 | sizeof(bpc->bpc_peer.sa_sin6.sin6_addr)); | |
695 | memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin6.sin6_addr, | |
696 | sizeof(bpc->bpc_local.sa_sin6.sin6_addr)); | |
697 | break; | |
698 | ||
699 | default: | |
700 | assert(1); | |
701 | break; | |
d245e522 | 702 | } |
e9e2c950 | 703 | |
79b4a6fc RZ |
704 | if (bpc->bpc_mhop) |
705 | BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH); | |
706 | ||
707 | bfd->key.mhop = bpc->bpc_mhop; | |
708 | ||
014cab13 RZ |
709 | if (bs_registrate(bfd) == NULL) |
710 | return NULL; | |
711 | ||
712 | /* Apply other configurations. */ | |
713 | _bfd_session_update(bfd, bpc); | |
714 | ||
715 | return bfd; | |
716 | } | |
717 | ||
718 | struct bfd_session *bs_registrate(struct bfd_session *bfd) | |
719 | { | |
79b4a6fc RZ |
720 | /* Registrate session into data structures. */ |
721 | bfd_key_insert(bfd); | |
722 | bfd->discrs.my_discr = ptm_bfd_gen_ID(); | |
723 | bfd_id_insert(bfd); | |
d245e522 RZ |
724 | |
725 | /* Try to enable session and schedule for packet receive/send. */ | |
726 | if (bfd_session_enable(bfd) == -1) { | |
727 | /* Unrecoverable failure, remove the session/peer. */ | |
728 | bfd_session_free(bfd); | |
729 | return NULL; | |
e9e2c950 RZ |
730 | } |
731 | ||
261e0ba9 RZ |
732 | /* Add observer if we have moving parts. */ |
733 | if (bfd->key.ifname[0] || bfd->key.vrfname[0] || bfd->sock == -1) | |
734 | bs_observer_add(bfd); | |
735 | ||
03e7f088 | 736 | log_info("session-new: %s", bs_to_string(bfd)); |
e9e2c950 RZ |
737 | |
738 | control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd); | |
739 | ||
740 | return bfd; | |
741 | } | |
742 | ||
bc50bcc8 | 743 | int ptm_bfd_sess_del(struct bfd_peer_cfg *bpc) |
e9e2c950 RZ |
744 | { |
745 | struct bfd_session *bs; | |
746 | ||
747 | /* Find session and call free(). */ | |
748 | bs = bs_peer_find(bpc); | |
749 | if (bs == NULL) | |
750 | return -1; | |
751 | ||
752 | /* This pointer is being referenced, don't let it be deleted. */ | |
753 | if (bs->refcount > 0) { | |
03e7f088 RZ |
754 | log_error("session-delete: refcount failure: %" PRIu64 |
755 | " references", | |
756 | bs->refcount); | |
e9e2c950 RZ |
757 | return -1; |
758 | } | |
759 | ||
03e7f088 | 760 | log_info("session-delete: %s", bs_to_string(bs)); |
e9e2c950 RZ |
761 | |
762 | control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs); | |
763 | ||
764 | bfd_session_free(bs); | |
765 | ||
766 | return 0; | |
767 | } | |
768 | ||
d3f3a2c4 RZ |
769 | void bfd_set_polling(struct bfd_session *bs) |
770 | { | |
c0ef9a8a RZ |
771 | /* |
772 | * Start polling procedure: the only timers that require polling | |
773 | * to change value without losing connection are: | |
774 | * | |
775 | * - Desired minimum transmission interval; | |
776 | * - Required minimum receive interval; | |
777 | * | |
778 | * RFC 5880, Section 6.8.3. | |
779 | */ | |
d3f3a2c4 RZ |
780 | bs->polling = 1; |
781 | } | |
782 | ||
aef131af RZ |
783 | /* |
784 | * bs_<state>_handler() functions implement the BFD state machine | |
785 | * transition mechanism. `<state>` is the current session state and | |
786 | * the parameter `nstate` is the peer new state. | |
787 | */ | |
9f37770f RZ |
788 | static void bs_admin_down_handler(struct bfd_session *bs |
789 | __attribute__((__unused__)), | |
790 | int nstate __attribute__((__unused__))) | |
aef131af RZ |
791 | { |
792 | /* | |
793 | * We are administratively down, there is no state machine | |
794 | * handling. | |
795 | */ | |
796 | } | |
797 | ||
9f37770f | 798 | static void bs_down_handler(struct bfd_session *bs, int nstate) |
aef131af RZ |
799 | { |
800 | switch (nstate) { | |
801 | case PTM_BFD_ADM_DOWN: | |
802 | /* | |
803 | * Remote peer doesn't want to talk, so lets keep the | |
804 | * connection down. | |
805 | */ | |
806 | case PTM_BFD_UP: | |
807 | /* Peer can't be up yet, wait it go to 'init' or 'down'. */ | |
808 | break; | |
809 | ||
810 | case PTM_BFD_DOWN: | |
811 | /* | |
812 | * Remote peer agreed that the path is down, lets try to | |
813 | * bring it up. | |
814 | */ | |
815 | bs->ses_state = PTM_BFD_INIT; | |
816 | break; | |
817 | ||
818 | case PTM_BFD_INIT: | |
819 | /* | |
820 | * Remote peer told us his path is up, lets turn | |
821 | * activate the session. | |
822 | */ | |
bc50bcc8 | 823 | ptm_bfd_sess_up(bs); |
aef131af RZ |
824 | break; |
825 | ||
826 | default: | |
827 | log_debug("state-change: unhandled neighbor state: %d", nstate); | |
828 | break; | |
829 | } | |
830 | } | |
831 | ||
9f37770f | 832 | static void bs_init_handler(struct bfd_session *bs, int nstate) |
aef131af RZ |
833 | { |
834 | switch (nstate) { | |
835 | case PTM_BFD_ADM_DOWN: | |
836 | /* | |
837 | * Remote peer doesn't want to talk, so lets make the | |
838 | * connection down. | |
839 | */ | |
840 | bs->ses_state = PTM_BFD_DOWN; | |
841 | break; | |
842 | ||
843 | case PTM_BFD_DOWN: | |
844 | /* Remote peer hasn't moved to first stage yet. */ | |
845 | break; | |
846 | ||
847 | case PTM_BFD_INIT: | |
848 | case PTM_BFD_UP: | |
849 | /* We agreed on the settings and the path is up. */ | |
bc50bcc8 | 850 | ptm_bfd_sess_up(bs); |
aef131af RZ |
851 | break; |
852 | ||
853 | default: | |
854 | log_debug("state-change: unhandled neighbor state: %d", nstate); | |
855 | break; | |
856 | } | |
857 | } | |
858 | ||
9f37770f | 859 | static void bs_up_handler(struct bfd_session *bs, int nstate) |
aef131af RZ |
860 | { |
861 | switch (nstate) { | |
862 | case PTM_BFD_ADM_DOWN: | |
863 | case PTM_BFD_DOWN: | |
864 | /* Peer lost or asked to shutdown connection. */ | |
bc50bcc8 | 865 | ptm_bfd_sess_dn(bs, BD_NEIGHBOR_DOWN); |
aef131af RZ |
866 | break; |
867 | ||
868 | case PTM_BFD_INIT: | |
869 | case PTM_BFD_UP: | |
870 | /* Path is up and working. */ | |
871 | break; | |
872 | ||
873 | default: | |
874 | log_debug("state-change: unhandled neighbor state: %d", nstate); | |
875 | break; | |
876 | } | |
877 | } | |
878 | ||
879 | void bs_state_handler(struct bfd_session *bs, int nstate) | |
880 | { | |
881 | switch (bs->ses_state) { | |
882 | case PTM_BFD_ADM_DOWN: | |
883 | bs_admin_down_handler(bs, nstate); | |
884 | break; | |
885 | case PTM_BFD_DOWN: | |
886 | bs_down_handler(bs, nstate); | |
887 | break; | |
888 | case PTM_BFD_INIT: | |
889 | bs_init_handler(bs, nstate); | |
890 | break; | |
891 | case PTM_BFD_UP: | |
892 | bs_up_handler(bs, nstate); | |
893 | break; | |
894 | ||
895 | default: | |
896 | log_debug("state-change: [%s] is in invalid state: %d", | |
897 | bs_to_string(bs), nstate); | |
898 | break; | |
899 | } | |
900 | } | |
901 | ||
c0ef9a8a RZ |
902 | /* |
903 | * Handles echo timer manipulation after updating timer. | |
904 | */ | |
905 | void bs_echo_timer_handler(struct bfd_session *bs) | |
906 | { | |
907 | uint32_t old_timer; | |
908 | ||
909 | /* | |
910 | * Before doing any echo handling, check if it is possible to | |
911 | * use it. | |
912 | * | |
913 | * - Check for `echo-mode` configuration. | |
914 | * - Check that we are not using multi hop (RFC 5883, | |
915 | * Section 3). | |
916 | * - Check that we are already at the up state. | |
917 | */ | |
918 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO) == 0 | |
919 | || BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH) | |
920 | || bs->ses_state != PTM_BFD_UP) | |
921 | return; | |
922 | ||
923 | /* Remote peer asked to stop echo. */ | |
924 | if (bs->remote_timers.required_min_echo == 0) { | |
925 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) | |
8bd859f6 | 926 | ptm_bfd_echo_stop(bs); |
c0ef9a8a RZ |
927 | |
928 | return; | |
929 | } | |
930 | ||
931 | /* | |
932 | * Calculate the echo transmission timer: we must not send | |
933 | * echo packets faster than the minimum required time | |
934 | * announced by the remote system. | |
935 | * | |
936 | * RFC 5880, Section 6.8.9. | |
937 | */ | |
938 | old_timer = bs->echo_xmt_TO; | |
939 | if (bs->remote_timers.required_min_echo > bs->timers.required_min_echo) | |
940 | bs->echo_xmt_TO = bs->remote_timers.required_min_echo; | |
941 | else | |
942 | bs->echo_xmt_TO = bs->timers.required_min_echo; | |
943 | ||
944 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE) == 0 | |
945 | || old_timer != bs->echo_xmt_TO) | |
946 | ptm_bfd_echo_start(bs); | |
947 | } | |
948 | ||
949 | /* | |
950 | * RFC 5880 Section 6.5. | |
951 | * | |
952 | * When a BFD control packet with the final bit is received, we must | |
953 | * update the session parameters. | |
954 | */ | |
955 | void bs_final_handler(struct bfd_session *bs) | |
956 | { | |
957 | /* Start using our new timers. */ | |
f43b9368 RZ |
958 | bs->cur_timers.desired_min_tx = bs->timers.desired_min_tx; |
959 | bs->cur_timers.required_min_rx = bs->timers.required_min_rx; | |
c0ef9a8a RZ |
960 | |
961 | /* | |
962 | * TODO: demand mode. See RFC 5880 Section 6.1. | |
963 | * | |
964 | * When using demand mode we must disable the detection timer | |
965 | * for lost control packets. | |
966 | */ | |
967 | if (bs->demand_mode) { | |
968 | /* Notify watchers about changed timers. */ | |
969 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); | |
970 | return; | |
971 | } | |
972 | ||
973 | /* | |
974 | * Calculate detection time based on new timers. | |
975 | * | |
976 | * Transmission calculation: | |
977 | * We must respect the RequiredMinRxInterval from the remote | |
978 | * system: if our desired transmission timer is more than the | |
979 | * minimum receive rate, then we must lower it to at least the | |
980 | * minimum receive interval. | |
981 | * | |
982 | * RFC 5880, Section 6.8.3. | |
983 | */ | |
984 | if (bs->timers.desired_min_tx > bs->remote_timers.required_min_rx) | |
985 | bs->xmt_TO = bs->remote_timers.required_min_rx; | |
986 | else | |
987 | bs->xmt_TO = bs->timers.desired_min_tx; | |
988 | ||
989 | /* Apply new transmission timer immediately. */ | |
990 | ptm_bfd_start_xmt_timer(bs, false); | |
991 | ||
992 | /* | |
993 | * Detection timeout calculation: | |
994 | * The minimum detection timeout is the remote detection | |
995 | * multipler (number of packets to be missed) times the agreed | |
996 | * transmission interval. | |
997 | * | |
998 | * RFC 5880, Section 6.8.4. | |
999 | * | |
1000 | * TODO: support sending/counting more packets inside detection | |
1001 | * timeout. | |
1002 | */ | |
1003 | if (bs->remote_timers.required_min_rx > bs->timers.desired_min_tx) | |
1004 | bs->detect_TO = bs->remote_detect_mult | |
1005 | * bs->remote_timers.required_min_rx; | |
1006 | else | |
1007 | bs->detect_TO = bs->remote_detect_mult | |
1008 | * bs->timers.desired_min_tx; | |
1009 | ||
1010 | /* Apply new receive timer immediately. */ | |
1011 | bfd_recvtimer_update(bs); | |
1012 | ||
1013 | /* Notify watchers about changed timers. */ | |
1014 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); | |
1015 | } | |
1016 | ||
b912b189 RZ |
1017 | void bs_set_slow_timers(struct bfd_session *bs) |
1018 | { | |
1019 | /* | |
1020 | * BFD connection must use slow timers before going up or after | |
1021 | * losing connectivity to avoid wasting bandwidth. | |
1022 | * | |
1023 | * RFC 5880, Section 6.8.3. | |
1024 | */ | |
1025 | bs->cur_timers.desired_min_tx = BFD_DEF_SLOWTX; | |
1026 | bs->cur_timers.required_min_rx = BFD_DEF_SLOWTX; | |
1027 | bs->cur_timers.required_min_echo = 0; | |
1028 | ||
1029 | /* Set the appropriated timeouts for slow connection. */ | |
1030 | bs->detect_TO = (BFD_DEFDETECTMULT * BFD_DEF_SLOWTX); | |
1031 | bs->xmt_TO = BFD_DEF_SLOWTX; | |
1032 | } | |
e9e2c950 RZ |
1033 | |
1034 | /* | |
1035 | * Helper functions. | |
1036 | */ | |
1037 | static const char *get_diag_str(int diag) | |
1038 | { | |
1039 | for (int i = 0; diag_list[i].str; i++) { | |
1040 | if (diag_list[i].type == diag) | |
1041 | return diag_list[i].str; | |
1042 | } | |
1043 | return "N/A"; | |
1044 | } | |
1045 | ||
1046 | const char *satostr(struct sockaddr_any *sa) | |
1047 | { | |
1048 | #define INETSTR_BUFCOUNT 8 | |
1049 | static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN]; | |
1050 | static int bufidx; | |
1051 | struct sockaddr_in *sin = &sa->sa_sin; | |
1052 | struct sockaddr_in6 *sin6 = &sa->sa_sin6; | |
1053 | ||
1054 | bufidx += (bufidx + 1) % INETSTR_BUFCOUNT; | |
1055 | buf[bufidx][0] = 0; | |
1056 | ||
1057 | switch (sin->sin_family) { | |
1058 | case AF_INET: | |
1059 | inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx], | |
1060 | sizeof(buf[bufidx])); | |
1061 | break; | |
1062 | case AF_INET6: | |
1063 | inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx], | |
1064 | sizeof(buf[bufidx])); | |
1065 | break; | |
1066 | ||
1067 | default: | |
1068 | strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx])); | |
1069 | break; | |
1070 | } | |
1071 | ||
1072 | return buf[bufidx]; | |
1073 | } | |
1074 | ||
1075 | const char *diag2str(uint8_t diag) | |
1076 | { | |
1077 | switch (diag) { | |
1078 | case 0: | |
1079 | return "ok"; | |
1080 | case 1: | |
1081 | return "control detection time expired"; | |
1082 | case 2: | |
1083 | return "echo function failed"; | |
1084 | case 3: | |
1085 | return "neighbor signaled session down"; | |
1086 | case 4: | |
1087 | return "forwarding plane reset"; | |
1088 | case 5: | |
1089 | return "path down"; | |
1090 | case 6: | |
1091 | return "concatenated path down"; | |
1092 | case 7: | |
1093 | return "administratively down"; | |
1094 | case 8: | |
1095 | return "reverse concatenated path down"; | |
1096 | default: | |
1097 | return "unknown"; | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | int strtosa(const char *addr, struct sockaddr_any *sa) | |
1102 | { | |
1103 | memset(sa, 0, sizeof(*sa)); | |
1104 | ||
1105 | if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) { | |
1106 | sa->sa_sin.sin_family = AF_INET; | |
1107 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
1108 | sa->sa_sin.sin_len = sizeof(sa->sa_sin); | |
1109 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
1110 | return 0; | |
1111 | } | |
1112 | ||
1113 | if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) { | |
1114 | sa->sa_sin6.sin6_family = AF_INET6; | |
1115 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
1116 | sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6); | |
1117 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | return -1; | |
1122 | } | |
1123 | ||
1124 | void integer2timestr(uint64_t time, char *buf, size_t buflen) | |
1125 | { | |
1126 | unsigned int year, month, day, hour, minute, second; | |
1127 | int rv; | |
1128 | ||
1129 | #define MINUTES (60) | |
23586b05 DS |
1130 | #define HOURS (60 * MINUTES) |
1131 | #define DAYS (24 * HOURS) | |
1132 | #define MONTHS (30 * DAYS) | |
1133 | #define YEARS (12 * MONTHS) | |
e9e2c950 RZ |
1134 | if (time >= YEARS) { |
1135 | year = time / YEARS; | |
1136 | time -= year * YEARS; | |
1137 | ||
1138 | rv = snprintf(buf, buflen, "%u year(s), ", year); | |
1139 | buf += rv; | |
1140 | buflen -= rv; | |
1141 | } | |
1142 | if (time >= MONTHS) { | |
1143 | month = time / MONTHS; | |
1144 | time -= month * MONTHS; | |
1145 | ||
1146 | rv = snprintf(buf, buflen, "%u month(s), ", month); | |
1147 | buf += rv; | |
1148 | buflen -= rv; | |
1149 | } | |
1150 | if (time >= DAYS) { | |
1151 | day = time / DAYS; | |
1152 | time -= day * DAYS; | |
1153 | ||
1154 | rv = snprintf(buf, buflen, "%u day(s), ", day); | |
1155 | buf += rv; | |
1156 | buflen -= rv; | |
1157 | } | |
1158 | if (time >= HOURS) { | |
1159 | hour = time / HOURS; | |
1160 | time -= hour * HOURS; | |
1161 | ||
1162 | rv = snprintf(buf, buflen, "%u hour(s), ", hour); | |
1163 | buf += rv; | |
1164 | buflen -= rv; | |
1165 | } | |
1166 | if (time >= MINUTES) { | |
1167 | minute = time / MINUTES; | |
1168 | time -= minute * MINUTES; | |
1169 | ||
1170 | rv = snprintf(buf, buflen, "%u minute(s), ", minute); | |
1171 | buf += rv; | |
1172 | buflen -= rv; | |
1173 | } | |
1174 | second = time % MINUTES; | |
1175 | snprintf(buf, buflen, "%u second(s)", second); | |
1176 | } | |
1177 | ||
79b4a6fc | 1178 | const char *bs_to_string(const struct bfd_session *bs) |
03e7f088 RZ |
1179 | { |
1180 | static char buf[256]; | |
79b4a6fc | 1181 | char addr_buf[INET6_ADDRSTRLEN]; |
03e7f088 RZ |
1182 | int pos; |
1183 | bool is_mhop = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH); | |
1184 | ||
1185 | pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no"); | |
79b4a6fc RZ |
1186 | pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s", |
1187 | inet_ntop(bs->key.family, &bs->key.peer, addr_buf, | |
1188 | sizeof(addr_buf))); | |
1189 | pos += snprintf(buf + pos, sizeof(buf) - pos, " local:%s", | |
1190 | inet_ntop(bs->key.family, &bs->key.local, addr_buf, | |
1191 | sizeof(addr_buf))); | |
1192 | if (bs->key.vrfname[0]) | |
1193 | pos += snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s", | |
1194 | bs->key.vrfname); | |
1195 | if (bs->key.ifname[0]) | |
1196 | pos += snprintf(buf + pos, sizeof(buf) - pos, " ifname:%s", | |
1197 | bs->key.ifname); | |
3cef9b7f DS |
1198 | |
1199 | (void)pos; | |
1200 | ||
03e7f088 RZ |
1201 | return buf; |
1202 | } | |
1203 | ||
d245e522 RZ |
1204 | int bs_observer_add(struct bfd_session *bs) |
1205 | { | |
1206 | struct bfd_session_observer *bso; | |
1207 | ||
ed74032b DS |
1208 | bso = XCALLOC(MTYPE_BFDD_SESSION_OBSERVER, sizeof(*bso)); |
1209 | bso->bso_isaddress = false; | |
d245e522 RZ |
1210 | bso->bso_bs = bs; |
1211 | bso->bso_isinterface = !BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH); | |
1212 | if (bso->bso_isinterface) | |
79b4a6fc | 1213 | strlcpy(bso->bso_entryname, bs->key.ifname, |
d245e522 | 1214 | sizeof(bso->bso_entryname)); |
261e0ba9 RZ |
1215 | /* Handle socket binding failures caused by missing local addresses. */ |
1216 | if (bs->sock == -1) { | |
1217 | bso->bso_isaddress = true; | |
1218 | bso->bso_addr.family = bs->key.family; | |
1219 | memcpy(&bso->bso_addr.u.prefix, &bs->key.local, | |
1220 | sizeof(bs->key.local)); | |
1221 | } | |
1222 | ||
d245e522 RZ |
1223 | TAILQ_INSERT_TAIL(&bglobal.bg_obslist, bso, bso_entry); |
1224 | ||
1225 | return 0; | |
1226 | } | |
1227 | ||
1228 | void bs_observer_del(struct bfd_session_observer *bso) | |
1229 | { | |
1230 | TAILQ_REMOVE(&bglobal.bg_obslist, bso, bso_entry); | |
1231 | XFREE(MTYPE_BFDD_SESSION_OBSERVER, bso); | |
1232 | } | |
1233 | ||
79b4a6fc RZ |
1234 | void bs_to_bpc(struct bfd_session *bs, struct bfd_peer_cfg *bpc) |
1235 | { | |
1236 | memset(bpc, 0, sizeof(*bpc)); | |
1237 | ||
1238 | bpc->bpc_ipv4 = (bs->key.family == AF_INET); | |
1239 | bpc->bpc_mhop = bs->key.mhop; | |
1240 | ||
1241 | switch (bs->key.family) { | |
1242 | case AF_INET: | |
1243 | bpc->bpc_peer.sa_sin.sin_family = AF_INET; | |
1244 | memcpy(&bpc->bpc_peer.sa_sin.sin_addr, &bs->key.peer, | |
1245 | sizeof(bpc->bpc_peer.sa_sin.sin_addr)); | |
1246 | ||
1247 | if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local))) { | |
1248 | bpc->bpc_local.sa_sin.sin_family = AF_INET6; | |
5ff7d1be | 1249 | memcpy(&bpc->bpc_local.sa_sin.sin_addr, &bs->key.local, |
79b4a6fc RZ |
1250 | sizeof(bpc->bpc_local.sa_sin.sin_addr)); |
1251 | } | |
1252 | break; | |
1253 | ||
1254 | case AF_INET6: | |
1255 | bpc->bpc_peer.sa_sin.sin_family = AF_INET6; | |
1256 | memcpy(&bpc->bpc_peer.sa_sin6.sin6_addr, &bs->key.peer, | |
1257 | sizeof(bpc->bpc_peer.sa_sin6.sin6_addr)); | |
1258 | ||
1259 | bpc->bpc_local.sa_sin6.sin6_family = AF_INET6; | |
5ff7d1be | 1260 | memcpy(&bpc->bpc_local.sa_sin6.sin6_addr, &bs->key.local, |
79b4a6fc RZ |
1261 | sizeof(bpc->bpc_local.sa_sin6.sin6_addr)); |
1262 | break; | |
1263 | } | |
1264 | ||
1265 | if (bs->key.ifname[0]) { | |
1266 | bpc->bpc_has_localif = true; | |
1267 | strlcpy(bpc->bpc_localif, bs->key.ifname, | |
1268 | sizeof(bpc->bpc_localif)); | |
1269 | } | |
1270 | ||
1271 | if (bs->key.vrfname[0]) { | |
1272 | bpc->bpc_has_vrfname = true; | |
1273 | strlcpy(bpc->bpc_vrfname, bs->key.vrfname, | |
1274 | sizeof(bpc->bpc_vrfname)); | |
1275 | } | |
1276 | } | |
1277 | ||
e9e2c950 RZ |
1278 | |
1279 | /* | |
1280 | * BFD hash data structures to find sessions. | |
1281 | */ | |
1282 | static struct hash *bfd_id_hash; | |
79b4a6fc | 1283 | static struct hash *bfd_key_hash; |
e9e2c950 | 1284 | |
d8b87afe QY |
1285 | static unsigned int bfd_id_hash_do(const void *p); |
1286 | static unsigned int bfd_key_hash_do(const void *p); | |
e9e2c950 | 1287 | |
e3b78da8 | 1288 | static void _bfd_free(struct hash_bucket *hb, |
e9e2c950 | 1289 | void *arg __attribute__((__unused__))); |
e9e2c950 RZ |
1290 | |
1291 | /* BFD hash for our discriminator. */ | |
d8b87afe | 1292 | static unsigned int bfd_id_hash_do(const void *p) |
e9e2c950 | 1293 | { |
d8b87afe | 1294 | const struct bfd_session *bs = p; |
e9e2c950 RZ |
1295 | |
1296 | return jhash_1word(bs->discrs.my_discr, 0); | |
1297 | } | |
1298 | ||
74df8d6d | 1299 | static bool bfd_id_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1300 | { |
1301 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
1302 | ||
1303 | return bs1->discrs.my_discr == bs2->discrs.my_discr; | |
1304 | } | |
1305 | ||
1306 | /* BFD hash for single hop. */ | |
d8b87afe | 1307 | static unsigned int bfd_key_hash_do(const void *p) |
e9e2c950 | 1308 | { |
d8b87afe | 1309 | const struct bfd_session *bs = p; |
e9e2c950 | 1310 | |
79b4a6fc | 1311 | return jhash(&bs->key, sizeof(bs->key), 0); |
e9e2c950 RZ |
1312 | } |
1313 | ||
79b4a6fc | 1314 | static bool bfd_key_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1315 | { |
1316 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
1317 | ||
79b4a6fc | 1318 | return memcmp(&bs1->key, &bs2->key, sizeof(bs1->key)) == 0; |
e9e2c950 RZ |
1319 | } |
1320 | ||
e9e2c950 | 1321 | |
e9e2c950 RZ |
1322 | /* |
1323 | * Hash public interface / exported functions. | |
1324 | */ | |
1325 | ||
1326 | /* Lookup functions. */ | |
1327 | struct bfd_session *bfd_id_lookup(uint32_t id) | |
1328 | { | |
1329 | struct bfd_session bs; | |
1330 | ||
1331 | bs.discrs.my_discr = id; | |
1332 | ||
1333 | return hash_lookup(bfd_id_hash, &bs); | |
1334 | } | |
1335 | ||
5ef92a2b PG |
1336 | struct bfd_key_walk_partial_lookup { |
1337 | struct bfd_session *given; | |
1338 | struct bfd_session *result; | |
1339 | }; | |
1340 | ||
1341 | /* ignore some parameters */ | |
307fc425 RZ |
1342 | static int bfd_key_lookup_ignore_partial_walker(struct hash_bucket *b, |
1343 | void *data) | |
5ef92a2b | 1344 | { |
307fc425 | 1345 | struct bfd_key_walk_partial_lookup *ctx = |
5ef92a2b PG |
1346 | (struct bfd_key_walk_partial_lookup *)data; |
1347 | struct bfd_session *given = ctx->given; | |
1348 | struct bfd_session *parsed = b->data; | |
1349 | ||
1350 | if (given->key.family != parsed->key.family) | |
1351 | return HASHWALK_CONTINUE; | |
1352 | if (given->key.mhop != parsed->key.mhop) | |
1353 | return HASHWALK_CONTINUE; | |
307fc425 RZ |
1354 | if (memcmp(&given->key.peer, &parsed->key.peer, |
1355 | sizeof(struct in6_addr))) | |
5ef92a2b PG |
1356 | return HASHWALK_CONTINUE; |
1357 | if (memcmp(given->key.vrfname, parsed->key.vrfname, MAXNAMELEN)) | |
1358 | return HASHWALK_CONTINUE; | |
1359 | ctx->result = parsed; | |
1360 | /* ignore localaddr or interface */ | |
1361 | return HASHWALK_ABORT; | |
1362 | } | |
1363 | ||
79b4a6fc | 1364 | struct bfd_session *bfd_key_lookup(struct bfd_key key) |
e9e2c950 RZ |
1365 | { |
1366 | struct bfd_session bs, *bsp; | |
5ef92a2b PG |
1367 | struct bfd_key_walk_partial_lookup ctx; |
1368 | char peer_buf[INET6_ADDRSTRLEN]; | |
e9e2c950 | 1369 | |
79b4a6fc RZ |
1370 | bs.key = key; |
1371 | bsp = hash_lookup(bfd_key_hash, &bs); | |
5ef92a2b PG |
1372 | if (bsp) |
1373 | return bsp; | |
e9e2c950 | 1374 | |
5ef92a2b PG |
1375 | inet_ntop(bs.key.family, &bs.key.peer, peer_buf, |
1376 | sizeof(peer_buf)); | |
79b4a6fc | 1377 | /* Handle cases where local-address is optional. */ |
5ef92a2b | 1378 | if (bs.key.family == AF_INET) { |
79b4a6fc RZ |
1379 | memset(&bs.key.local, 0, sizeof(bs.key.local)); |
1380 | bsp = hash_lookup(bfd_key_hash, &bs); | |
5ef92a2b PG |
1381 | if (bsp) { |
1382 | char addr_buf[INET6_ADDRSTRLEN]; | |
1383 | ||
1384 | inet_ntop(bs.key.family, &key.local, addr_buf, | |
1385 | sizeof(addr_buf)); | |
1386 | log_debug(" peer %s found, but loc-addr %s ignored", | |
1387 | peer_buf, addr_buf); | |
1388 | return bsp; | |
1389 | } | |
e9e2c950 RZ |
1390 | } |
1391 | ||
79b4a6fc | 1392 | bs.key = key; |
5ef92a2b PG |
1393 | /* Handle cases where ifname is optional. */ |
1394 | if (bs.key.ifname[0]) { | |
79b4a6fc RZ |
1395 | memset(bs.key.ifname, 0, sizeof(bs.key.ifname)); |
1396 | bsp = hash_lookup(bfd_key_hash, &bs); | |
5ef92a2b PG |
1397 | if (bsp) { |
1398 | log_debug(" peer %s found, but ifp %s ignored", | |
1399 | peer_buf, key.ifname); | |
1400 | return bsp; | |
1401 | } | |
1402 | } | |
e9e2c950 | 1403 | |
5ef92a2b PG |
1404 | /* Handle cases where local-address and ifname are optional. */ |
1405 | if (bs.key.family == AF_INET) { | |
1406 | memset(&bs.key.local, 0, sizeof(bs.key.local)); | |
1407 | bsp = hash_lookup(bfd_key_hash, &bs); | |
1408 | if (bsp) { | |
1409 | char addr_buf[INET6_ADDRSTRLEN]; | |
1410 | ||
1411 | inet_ntop(bs.key.family, &bs.key.local, addr_buf, | |
1412 | sizeof(addr_buf)); | |
1413 | log_debug(" peer %s found, but ifp %s" | |
1414 | " and loc-addr %s ignored", | |
1415 | peer_buf, key.ifname, | |
1416 | addr_buf); | |
1417 | return bsp; | |
79b4a6fc RZ |
1418 | } |
1419 | } | |
5ef92a2b | 1420 | bs.key = key; |
e9e2c950 | 1421 | |
5ef92a2b PG |
1422 | /* Handle case where a context more complex ctx is present. |
1423 | * input has no iface nor local-address, but a context may | |
1424 | * exist | |
1425 | */ | |
1426 | ctx.result = NULL; | |
1427 | ctx.given = &bs; | |
1428 | hash_walk(bfd_key_hash, | |
1429 | &bfd_key_lookup_ignore_partial_walker, | |
1430 | &ctx); | |
1431 | /* change key */ | |
1432 | if (ctx.result) { | |
1433 | bsp = ctx.result; | |
1434 | log_debug(" peer %s found, but ifp" | |
1435 | " and/or loc-addr params ignored"); | |
1436 | } | |
79b4a6fc | 1437 | return bsp; |
e9e2c950 RZ |
1438 | } |
1439 | ||
e9e2c950 RZ |
1440 | /* |
1441 | * Delete functions. | |
1442 | * | |
1443 | * Delete functions searches and remove the item from the hash and | |
1444 | * returns a pointer to the removed item data. If the item was not found | |
1445 | * then it returns NULL. | |
1446 | * | |
1447 | * The data stored inside the hash is not free()ed, so you must do it | |
1448 | * manually after getting the pointer back. | |
1449 | */ | |
1450 | struct bfd_session *bfd_id_delete(uint32_t id) | |
1451 | { | |
1452 | struct bfd_session bs; | |
1453 | ||
1454 | bs.discrs.my_discr = id; | |
1455 | ||
1456 | return hash_release(bfd_id_hash, &bs); | |
1457 | } | |
1458 | ||
79b4a6fc | 1459 | struct bfd_session *bfd_key_delete(struct bfd_key key) |
e9e2c950 RZ |
1460 | { |
1461 | struct bfd_session bs, *bsp; | |
1462 | ||
79b4a6fc RZ |
1463 | bs.key = key; |
1464 | bsp = hash_lookup(bfd_key_hash, &bs); | |
1465 | if (bsp == NULL && key.ifname[0]) { | |
1466 | memset(bs.key.ifname, 0, sizeof(bs.key.ifname)); | |
1467 | bsp = hash_lookup(bfd_key_hash, &bs); | |
e9e2c950 RZ |
1468 | } |
1469 | ||
79b4a6fc | 1470 | return hash_release(bfd_key_hash, bsp); |
e9e2c950 RZ |
1471 | } |
1472 | ||
e9e2c950 RZ |
1473 | /* Iteration functions. */ |
1474 | void bfd_id_iterate(hash_iter_func hif, void *arg) | |
1475 | { | |
1476 | hash_iterate(bfd_id_hash, hif, arg); | |
1477 | } | |
1478 | ||
79b4a6fc | 1479 | void bfd_key_iterate(hash_iter_func hif, void *arg) |
e9e2c950 | 1480 | { |
79b4a6fc | 1481 | hash_iterate(bfd_key_hash, hif, arg); |
e9e2c950 RZ |
1482 | } |
1483 | ||
e9e2c950 RZ |
1484 | /* |
1485 | * Insert functions. | |
1486 | * | |
1487 | * Inserts session into hash and returns `true` on success, otherwise | |
1488 | * `false`. | |
1489 | */ | |
1490 | bool bfd_id_insert(struct bfd_session *bs) | |
1491 | { | |
1492 | return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs); | |
1493 | } | |
1494 | ||
79b4a6fc | 1495 | bool bfd_key_insert(struct bfd_session *bs) |
e9e2c950 | 1496 | { |
79b4a6fc | 1497 | return (hash_get(bfd_key_hash, bs, hash_alloc_intern) == bs); |
e9e2c950 RZ |
1498 | } |
1499 | ||
e9e2c950 RZ |
1500 | void bfd_initialize(void) |
1501 | { | |
1502 | bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp, | |
79b4a6fc RZ |
1503 | "BFD session discriminator hash"); |
1504 | bfd_key_hash = hash_create(bfd_key_hash_do, bfd_key_hash_cmp, | |
1505 | "BFD session hash"); | |
e9e2c950 RZ |
1506 | } |
1507 | ||
e3b78da8 | 1508 | static void _bfd_free(struct hash_bucket *hb, |
e9e2c950 RZ |
1509 | void *arg __attribute__((__unused__))) |
1510 | { | |
1511 | struct bfd_session *bs = hb->data; | |
1512 | ||
1513 | bfd_session_free(bs); | |
1514 | } | |
1515 | ||
e9e2c950 RZ |
1516 | void bfd_shutdown(void) |
1517 | { | |
1518 | /* | |
1519 | * Close and free all BFD sessions. | |
1520 | * | |
1521 | * _bfd_free() will call bfd_session_free() which will take care | |
1522 | * of removing the session from all hashes, so we just run an | |
1523 | * assert() here to make sure it really happened. | |
1524 | */ | |
1525 | bfd_id_iterate(_bfd_free, NULL); | |
79b4a6fc | 1526 | assert(bfd_key_hash->count == 0); |
e9e2c950 | 1527 | |
e9e2c950 RZ |
1528 | /* Now free the hashes themselves. */ |
1529 | hash_free(bfd_id_hash); | |
79b4a6fc | 1530 | hash_free(bfd_key_hash); |
e9e2c950 | 1531 | } |
9fc0bc5c | 1532 | |
adc26455 RZ |
1533 | struct bfd_session_iterator { |
1534 | int bsi_stop; | |
1535 | bool bsi_mhop; | |
1536 | const struct bfd_session *bsi_bs; | |
1537 | }; | |
1538 | ||
307fc425 | 1539 | static int _bfd_session_next(struct hash_bucket *hb, void *arg) |
adc26455 RZ |
1540 | { |
1541 | struct bfd_session_iterator *bsi = arg; | |
1542 | struct bfd_session *bs = hb->data; | |
1543 | ||
1544 | /* Previous entry signaled stop. */ | |
1545 | if (bsi->bsi_stop == 1) { | |
1546 | /* Match the single/multi hop sessions. */ | |
1547 | if (bs->key.mhop != bsi->bsi_mhop) | |
1548 | return HASHWALK_CONTINUE; | |
1549 | ||
1550 | bsi->bsi_bs = bs; | |
1551 | return HASHWALK_ABORT; | |
1552 | } | |
1553 | ||
1554 | /* We found the current item, stop in the next one. */ | |
1555 | if (bsi->bsi_bs == hb->data) { | |
1556 | bsi->bsi_stop = 1; | |
1557 | /* Set entry to NULL to signal end of list. */ | |
1558 | bsi->bsi_bs = NULL; | |
1559 | } else if (bsi->bsi_bs == NULL && bsi->bsi_mhop == bs->key.mhop) { | |
1560 | /* We want the first list item. */ | |
1561 | bsi->bsi_stop = 1; | |
1562 | bsi->bsi_bs = hb->data; | |
1563 | return HASHWALK_ABORT; | |
1564 | } | |
1565 | ||
1566 | return HASHWALK_CONTINUE; | |
1567 | } | |
1568 | ||
1569 | /* | |
1570 | * bfd_session_next: uses the current session to find the next. | |
1571 | * | |
1572 | * `bs` might point to NULL to get the first item of the data structure. | |
1573 | */ | |
1574 | const struct bfd_session *bfd_session_next(const struct bfd_session *bs, | |
1575 | bool mhop) | |
1576 | { | |
1577 | struct bfd_session_iterator bsi; | |
1578 | ||
1579 | bsi.bsi_stop = 0; | |
1580 | bsi.bsi_bs = bs; | |
1581 | bsi.bsi_mhop = mhop; | |
1582 | hash_walk(bfd_key_hash, _bfd_session_next, &bsi); | |
1583 | if (bsi.bsi_stop == 0) | |
1584 | return NULL; | |
1585 | ||
1586 | return bsi.bsi_bs; | |
1587 | } | |
1588 | ||
307fc425 RZ |
1589 | static void _bfd_session_remove_manual(struct hash_bucket *hb, |
1590 | void *arg __attribute__((__unused__))) | |
2a573ff6 RZ |
1591 | { |
1592 | struct bfd_session *bs = hb->data; | |
1593 | ||
1594 | /* Delete only manually configured sessions. */ | |
1595 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG) == 0) | |
1596 | return; | |
1597 | ||
1598 | bs->refcount--; | |
1599 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG); | |
1600 | ||
1601 | /* Don't delete sessions still in use. */ | |
1602 | if (bs->refcount != 0) | |
1603 | return; | |
1604 | ||
1605 | bfd_session_free(bs); | |
1606 | } | |
1607 | ||
1608 | /* | |
1609 | * bfd_sessions_remove_manual: remove all manually configured sessions. | |
1610 | * | |
1611 | * NOTE: this function doesn't remove automatically created sessions. | |
1612 | */ | |
1613 | void bfd_sessions_remove_manual(void) | |
1614 | { | |
1615 | hash_iterate(bfd_key_hash, _bfd_session_remove_manual, NULL); | |
1616 | } | |
1617 | ||
014cab13 RZ |
1618 | /* |
1619 | * VRF related functions. | |
1620 | */ | |
9fc0bc5c PG |
1621 | static int bfd_vrf_new(struct vrf *vrf) |
1622 | { | |
1623 | log_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id); | |
1624 | return 0; | |
1625 | } | |
1626 | ||
1627 | static int bfd_vrf_delete(struct vrf *vrf) | |
1628 | { | |
1629 | log_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id); | |
1630 | return 0; | |
1631 | } | |
1632 | ||
43ba41c1 PG |
1633 | static int bfd_vrf_update(struct vrf *vrf) |
1634 | { | |
1635 | if (!vrf_is_enabled(vrf)) | |
1636 | return 0; | |
1637 | log_debug("VRF update: %s(%u)", vrf->name, vrf->vrf_id); | |
1638 | /* a different name is given; update bfd list */ | |
1639 | bfdd_sessions_enable_vrf(vrf); | |
1640 | return 0; | |
1641 | } | |
1642 | ||
9fc0bc5c PG |
1643 | static int bfd_vrf_enable(struct vrf *vrf) |
1644 | { | |
7bcadbae PG |
1645 | struct bfd_vrf_global *bvrf; |
1646 | ||
1647 | /* a different name */ | |
1648 | if (!vrf->info) { | |
1649 | bvrf = XCALLOC(MTYPE_BFDD_VRF, sizeof(struct bfd_vrf_global)); | |
1650 | bvrf->vrf = vrf; | |
1651 | vrf->info = (void *)bvrf; | |
1652 | } else | |
1653 | bvrf = vrf->info; | |
9fc0bc5c | 1654 | log_debug("VRF enable add %s id %u", vrf->name, vrf->vrf_id); |
30c5d21e PG |
1655 | if (vrf->vrf_id == VRF_DEFAULT || |
1656 | vrf_get_backend() == VRF_BACKEND_NETNS) { | |
1657 | if (!bvrf->bg_shop) | |
1658 | bvrf->bg_shop = bp_udp_shop(vrf->vrf_id); | |
1659 | if (!bvrf->bg_mhop) | |
1660 | bvrf->bg_mhop = bp_udp_mhop(vrf->vrf_id); | |
1661 | if (!bvrf->bg_shop6) | |
1662 | bvrf->bg_shop6 = bp_udp6_shop(vrf->vrf_id); | |
1663 | if (!bvrf->bg_mhop6) | |
1664 | bvrf->bg_mhop6 = bp_udp6_mhop(vrf->vrf_id); | |
1665 | if (!bvrf->bg_echo) | |
1666 | bvrf->bg_echo = bp_echo_socket(vrf->vrf_id); | |
1667 | if (!bvrf->bg_echov6) | |
1668 | bvrf->bg_echov6 = bp_echov6_socket(vrf->vrf_id); | |
1669 | ||
1670 | /* Add descriptors to the event loop. */ | |
1671 | if (!bvrf->bg_ev[0]) | |
1672 | thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_shop, | |
1673 | &bvrf->bg_ev[0]); | |
1674 | if (!bvrf->bg_ev[1]) | |
1675 | thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_mhop, | |
1676 | &bvrf->bg_ev[1]); | |
1677 | if (!bvrf->bg_ev[2]) | |
1678 | thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_shop6, | |
1679 | &bvrf->bg_ev[2]); | |
1680 | if (!bvrf->bg_ev[3]) | |
1681 | thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_mhop6, | |
1682 | &bvrf->bg_ev[3]); | |
1683 | if (!bvrf->bg_ev[4]) | |
1684 | thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_echo, | |
1685 | &bvrf->bg_ev[4]); | |
1686 | if (!bvrf->bg_ev[5]) | |
1687 | thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_echov6, | |
1688 | &bvrf->bg_ev[5]); | |
1689 | } | |
d24af713 | 1690 | if (vrf->vrf_id != VRF_DEFAULT) { |
54aadda1 | 1691 | bfdd_zclient_register(vrf->vrf_id); |
d24af713 PG |
1692 | bfdd_sessions_enable_vrf(vrf); |
1693 | } | |
9fc0bc5c PG |
1694 | return 0; |
1695 | } | |
1696 | ||
1697 | static int bfd_vrf_disable(struct vrf *vrf) | |
1698 | { | |
7bcadbae PG |
1699 | struct bfd_vrf_global *bvrf; |
1700 | ||
1701 | if (!vrf->info) | |
9fc0bc5c | 1702 | return 0; |
7bcadbae | 1703 | bvrf = vrf->info; |
54aadda1 | 1704 | |
d24af713 PG |
1705 | if (vrf->vrf_id != VRF_DEFAULT) { |
1706 | bfdd_sessions_disable_vrf(vrf); | |
54aadda1 | 1707 | bfdd_zclient_unregister(vrf->vrf_id); |
d24af713 | 1708 | } |
54aadda1 | 1709 | |
9fc0bc5c | 1710 | log_debug("VRF disable %s id %d", vrf->name, vrf->vrf_id); |
7bcadbae PG |
1711 | /* Close all descriptors. */ |
1712 | socket_close(&bvrf->bg_echo); | |
1713 | socket_close(&bvrf->bg_shop); | |
1714 | socket_close(&bvrf->bg_mhop); | |
1715 | socket_close(&bvrf->bg_shop6); | |
1716 | socket_close(&bvrf->bg_mhop6); | |
8cf3c217 PG |
1717 | socket_close(&bvrf->bg_echo); |
1718 | socket_close(&bvrf->bg_echov6); | |
7bcadbae PG |
1719 | |
1720 | /* free context */ | |
1721 | XFREE(MTYPE_BFDD_VRF, bvrf); | |
1722 | vrf->info = NULL; | |
1723 | ||
9fc0bc5c PG |
1724 | return 0; |
1725 | } | |
1726 | ||
1727 | void bfd_vrf_init(void) | |
1728 | { | |
1729 | vrf_init(bfd_vrf_new, bfd_vrf_enable, bfd_vrf_disable, | |
43ba41c1 | 1730 | bfd_vrf_delete, bfd_vrf_update); |
9fc0bc5c PG |
1731 | } |
1732 | ||
7bcadbae PG |
1733 | void bfd_vrf_terminate(void) |
1734 | { | |
1735 | vrf_terminate(); | |
1736 | } | |
1737 | ||
1738 | struct bfd_vrf_global *bfd_vrf_look_by_session(struct bfd_session *bfd) | |
1739 | { | |
1740 | struct vrf *vrf; | |
1741 | ||
1742 | if (!vrf_is_backend_netns()) { | |
1743 | vrf = vrf_lookup_by_id(VRF_DEFAULT); | |
1744 | if (vrf) | |
1745 | return (struct bfd_vrf_global *)vrf->info; | |
1746 | return NULL; | |
1747 | } | |
1748 | if (!bfd) | |
1749 | return NULL; | |
1750 | if (!bfd->vrf) | |
1751 | return NULL; | |
1752 | return bfd->vrf->info; | |
1753 | } | |
f06e248c PG |
1754 | |
1755 | void bfd_session_update_vrf_name(struct bfd_session *bs, struct vrf *vrf) | |
1756 | { | |
1757 | if (!vrf || !bs) | |
1758 | return; | |
1759 | /* update key */ | |
1760 | hash_release(bfd_key_hash, bs); | |
955d3ad8 PG |
1761 | /* |
1762 | * HACK: Change the BFD VRF in the running configuration directly, | |
1763 | * bypassing the northbound layer. This is necessary to avoid deleting | |
1764 | * the BFD and readding it in the new VRF, which would have | |
1765 | * several implications. | |
1766 | */ | |
1767 | if (yang_module_find("frr-bfdd") && bs->key.vrfname[0]) { | |
1768 | struct lyd_node *bfd_dnode; | |
1769 | char xpath[XPATH_MAXLEN], xpath_srcaddr[XPATH_MAXLEN + 32]; | |
1770 | char addr_buf[INET6_ADDRSTRLEN]; | |
1771 | int slen; | |
1772 | ||
1773 | /* build xpath */ | |
1774 | if (bs->key.mhop) { | |
1775 | inet_ntop(bs->key.family, &bs->key.local, addr_buf, sizeof(addr_buf)); | |
1776 | snprintf(xpath_srcaddr, sizeof(xpath_srcaddr), "[source-addr='%s']", | |
1777 | addr_buf); | |
1778 | } else | |
1779 | xpath_srcaddr[0] = 0; | |
1780 | inet_ntop(bs->key.family, &bs->key.peer, addr_buf, sizeof(addr_buf)); | |
1781 | slen = snprintf(xpath, sizeof(xpath), | |
1782 | "/frr-bfdd:bfdd/bfd/sessions/%s%s[dest-addr='%s']", | |
1783 | bs->key.mhop ? "multi-hop" : "single-hop", xpath_srcaddr, | |
1784 | addr_buf); | |
1785 | if (bs->key.ifname[0]) | |
1786 | slen += snprintf(xpath + slen, sizeof(xpath) - slen, | |
1787 | "[interface='%s']", bs->key.ifname); | |
1788 | else | |
1789 | slen += snprintf(xpath + slen, sizeof(xpath) - slen, | |
1790 | "[interface='']"); | |
1791 | snprintf(xpath + slen, sizeof(xpath) - slen, "[vrf='%s']/vrf", | |
1792 | bs->key.vrfname); | |
1793 | ||
1794 | pthread_rwlock_wrlock(&running_config->lock); | |
1795 | { | |
1796 | bfd_dnode = yang_dnode_get( | |
1797 | running_config->dnode, | |
1798 | xpath, bs->key.vrfname); | |
1799 | if (bfd_dnode) { | |
1800 | yang_dnode_change_leaf(bfd_dnode, vrf->name); | |
1801 | running_config->version++; | |
1802 | } | |
1803 | pthread_rwlock_unlock(&running_config->lock); | |
1804 | } | |
1805 | } | |
f06e248c PG |
1806 | memset(bs->key.vrfname, 0, sizeof(bs->key.vrfname)); |
1807 | strlcpy(bs->key.vrfname, vrf->name, sizeof(bs->key.vrfname)); | |
1808 | hash_get(bfd_key_hash, bs, hash_alloc_intern); | |
1809 | } |