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