]>
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 | ||
34 | DEFINE_QOBJ_TYPE(bfd_session); | |
35 | ||
36 | /* | |
37 | * Prototypes | |
38 | */ | |
79b4a6fc RZ |
39 | void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer, |
40 | struct sockaddr_any *local, bool mhop, const char *ifname, | |
41 | const char *vrfname); | |
d245e522 | 42 | |
e9e2c950 RZ |
43 | static uint32_t ptm_bfd_gen_ID(void); |
44 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd); | |
45 | static void bfd_session_free(struct bfd_session *bs); | |
d245e522 | 46 | static struct bfd_session *bfd_session_new(void); |
e9e2c950 RZ |
47 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, |
48 | uint32_t ldisc); | |
49 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc); | |
50 | static const char *get_diag_str(int diag); | |
51 | ||
9f37770f RZ |
52 | static void bs_admin_down_handler(struct bfd_session *bs, int nstate); |
53 | static void bs_down_handler(struct bfd_session *bs, int nstate); | |
54 | static void bs_init_handler(struct bfd_session *bs, int nstate); | |
55 | static void bs_up_handler(struct bfd_session *bs, int nstate); | |
56 | ||
79b4a6fc RZ |
57 | /* Zeroed array with the size of an IPv6 address. */ |
58 | struct in6_addr zero_addr; | |
e9e2c950 RZ |
59 | |
60 | /* | |
61 | * Functions | |
62 | */ | |
79b4a6fc RZ |
63 | void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer, |
64 | struct sockaddr_any *local, bool mhop, const char *ifname, | |
65 | const char *vrfname) | |
d245e522 | 66 | { |
79b4a6fc | 67 | memset(key, 0, sizeof(*key)); |
d245e522 | 68 | |
79b4a6fc RZ |
69 | switch (peer->sa_sin.sin_family) { |
70 | case AF_INET: | |
71 | key->family = AF_INET; | |
72 | memcpy(&key->peer, &peer->sa_sin.sin_addr, | |
73 | sizeof(peer->sa_sin.sin_addr)); | |
74 | memcpy(&key->local, &local->sa_sin.sin_addr, | |
75 | sizeof(local->sa_sin.sin_addr)); | |
76 | break; | |
77 | case AF_INET6: | |
78 | key->family = AF_INET6; | |
79 | memcpy(&key->peer, &peer->sa_sin6.sin6_addr, | |
80 | sizeof(peer->sa_sin6.sin6_addr)); | |
81 | memcpy(&key->local, &local->sa_sin6.sin6_addr, | |
82 | sizeof(local->sa_sin6.sin6_addr)); | |
d245e522 RZ |
83 | break; |
84 | } | |
d245e522 | 85 | |
79b4a6fc RZ |
86 | key->mhop = mhop; |
87 | if (ifname && ifname[0]) | |
88 | strlcpy(key->ifname, ifname, sizeof(key->ifname)); | |
89 | if (vrfname && vrfname[0]) | |
90 | strlcpy(key->vrfname, vrfname, 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 | */ | |
79b4a6fc RZ |
131 | if (bs->key.ifname[0]) { |
132 | ifp = if_lookup_by_name_all_vrf(bs->key.ifname); | |
d245e522 RZ |
133 | if (ifp == NULL) { |
134 | log_error( | |
135 | "session-enable: specified interface doesn't exists."); | |
136 | return 0; | |
137 | } | |
138 | ||
139 | vrf = vrf_lookup_by_id(ifp->vrf_id); | |
140 | if (vrf == NULL) { | |
79b4a6fc RZ |
141 | log_error( |
142 | "session-enable: specified VRF doesn't exists."); | |
d245e522 RZ |
143 | return 0; |
144 | } | |
145 | } | |
146 | ||
79b4a6fc RZ |
147 | if (bs->key.vrfname[0]) { |
148 | vrf = vrf_lookup_by_name(bs->key.vrfname); | |
d245e522 | 149 | if (vrf == NULL) { |
79b4a6fc RZ |
150 | log_error( |
151 | "session-enable: specified VRF doesn't exists."); | |
d245e522 RZ |
152 | return 0; |
153 | } | |
154 | } | |
155 | ||
156 | /* Assign interface/VRF pointers. */ | |
157 | bs->vrf = vrf; | |
158 | if (bs->vrf == NULL) | |
159 | bs->vrf = vrf_lookup_by_id(VRF_DEFAULT); | |
160 | ||
79b4a6fc RZ |
161 | if (bs->key.ifname[0] |
162 | && BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH) == 0) | |
d245e522 RZ |
163 | bs->ifp = ifp; |
164 | ||
261e0ba9 RZ |
165 | /* Sanity check: don't leak open sockets. */ |
166 | if (bs->sock != -1) { | |
1ac865e7 | 167 | log_debug("session-enable: previous socket open"); |
261e0ba9 RZ |
168 | close(bs->sock); |
169 | bs->sock = -1; | |
170 | } | |
171 | ||
d245e522 RZ |
172 | /* |
173 | * Get socket for transmitting control packets. Note that if we | |
174 | * could use the destination port (3784) for the source | |
175 | * port we wouldn't need a socket per session. | |
176 | */ | |
177 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6) == 0) { | |
178 | psock = bp_peer_socket(bs); | |
179 | if (psock == -1) | |
261e0ba9 | 180 | return 0; |
d245e522 RZ |
181 | } else { |
182 | psock = bp_peer_socketv6(bs); | |
183 | if (psock == -1) | |
261e0ba9 | 184 | return 0; |
d245e522 RZ |
185 | } |
186 | ||
187 | /* | |
188 | * We've got a valid socket, lets start the timers and the | |
189 | * protocol. | |
190 | */ | |
191 | bs->sock = psock; | |
192 | bfd_recvtimer_update(bs); | |
193 | ptm_bfd_start_xmt_timer(bs, false); | |
194 | ||
d245e522 RZ |
195 | return 0; |
196 | } | |
197 | ||
198 | /* | |
199 | * Disabled a running BFD session. | |
200 | * | |
201 | * A session is disabled when the specified interface/VRF gets removed | |
202 | * (e.g. virtual interfaces). | |
203 | */ | |
204 | void bfd_session_disable(struct bfd_session *bs) | |
205 | { | |
206 | /* Free up socket resources. */ | |
207 | if (bs->sock != -1) { | |
208 | close(bs->sock); | |
209 | bs->sock = -1; | |
210 | } | |
211 | ||
212 | /* Disable all timers. */ | |
213 | bfd_recvtimer_delete(bs); | |
214 | bfd_echo_recvtimer_delete(bs); | |
215 | bfd_xmttimer_delete(bs); | |
216 | bfd_echo_xmttimer_delete(bs); | |
e9e2c950 RZ |
217 | } |
218 | ||
219 | static uint32_t ptm_bfd_gen_ID(void) | |
220 | { | |
843b324d | 221 | uint32_t session_id; |
e9e2c950 | 222 | |
843b324d RZ |
223 | /* |
224 | * RFC 5880, Section 6.8.1. recommends that we should generate | |
225 | * random session identification numbers. | |
226 | */ | |
227 | do { | |
228 | session_id = ((random() << 16) & 0xFFFF0000) | |
229 | | (random() & 0x0000FFFF); | |
230 | } while (session_id == 0 || bfd_id_lookup(session_id) != NULL); | |
231 | ||
232 | return session_id; | |
e9e2c950 RZ |
233 | } |
234 | ||
235 | void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo) | |
236 | { | |
237 | uint64_t jitter, xmt_TO; | |
238 | int maxpercent; | |
239 | ||
240 | xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO; | |
241 | ||
242 | /* | |
243 | * From section 6.5.2: trasmit interval should be randomly jittered | |
244 | * between | |
245 | * 75% and 100% of nominal value, unless detect_mult is 1, then should | |
246 | * be | |
247 | * between 75% and 90%. | |
248 | */ | |
249 | maxpercent = (bfd->detect_mult == 1) ? 16 : 26; | |
250 | jitter = (xmt_TO * (75 + (random() % maxpercent))) / 100; | |
251 | /* XXX remove that division above */ | |
252 | ||
253 | if (is_echo) | |
254 | bfd_echo_xmttimer_update(bfd, jitter); | |
255 | else | |
256 | bfd_xmttimer_update(bfd, jitter); | |
257 | } | |
258 | ||
259 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd) | |
260 | { | |
261 | /* Send the scheduled echo packet */ | |
262 | ptm_bfd_echo_snd(bfd); | |
263 | ||
264 | /* Restart the timer for next time */ | |
265 | ptm_bfd_start_xmt_timer(bfd, true); | |
266 | } | |
267 | ||
268 | void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit) | |
269 | { | |
270 | /* Send the scheduled control packet */ | |
271 | ptm_bfd_snd(bfd, fbit); | |
272 | ||
273 | /* Restart the timer for next time */ | |
274 | ptm_bfd_start_xmt_timer(bfd, false); | |
275 | } | |
276 | ||
8bd859f6 | 277 | void ptm_bfd_echo_stop(struct bfd_session *bfd) |
e9e2c950 RZ |
278 | { |
279 | bfd->echo_xmt_TO = 0; | |
280 | bfd->echo_detect_TO = 0; | |
281 | BFD_UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE); | |
282 | ||
283 | bfd_echo_xmttimer_delete(bfd); | |
284 | bfd_echo_recvtimer_delete(bfd); | |
e9e2c950 RZ |
285 | } |
286 | ||
287 | void ptm_bfd_echo_start(struct bfd_session *bfd) | |
288 | { | |
289 | bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO); | |
451eb5a2 RZ |
290 | if (bfd->echo_detect_TO > 0) |
291 | ptm_bfd_echo_xmt_TO(bfd); | |
e9e2c950 RZ |
292 | } |
293 | ||
bc50bcc8 | 294 | void ptm_bfd_sess_up(struct bfd_session *bfd) |
e9e2c950 | 295 | { |
03e7f088 RZ |
296 | int old_state = bfd->ses_state; |
297 | ||
e9e2c950 RZ |
298 | bfd->local_diag = 0; |
299 | bfd->ses_state = PTM_BFD_UP; | |
e9e2c950 RZ |
300 | monotime(&bfd->uptime); |
301 | ||
c0ef9a8a RZ |
302 | /* Connection is up, lets negotiate timers. */ |
303 | bfd_set_polling(bfd); | |
304 | ||
305 | /* Start sending control packets with poll bit immediately. */ | |
306 | ptm_bfd_snd(bfd, 0); | |
e9e2c950 RZ |
307 | |
308 | control_notify(bfd); | |
309 | ||
0684c9b1 RZ |
310 | if (old_state != bfd->ses_state) { |
311 | bfd->stats.session_up++; | |
03e7f088 RZ |
312 | log_info("state-change: [%s] %s -> %s", bs_to_string(bfd), |
313 | state_list[old_state].str, | |
314 | state_list[bfd->ses_state].str); | |
0684c9b1 | 315 | } |
e9e2c950 RZ |
316 | } |
317 | ||
bc50bcc8 | 318 | void ptm_bfd_sess_dn(struct bfd_session *bfd, uint8_t diag) |
e9e2c950 RZ |
319 | { |
320 | int old_state = bfd->ses_state; | |
321 | ||
322 | bfd->local_diag = diag; | |
323 | bfd->discrs.remote_discr = 0; | |
324 | bfd->ses_state = PTM_BFD_DOWN; | |
325 | bfd->polling = 0; | |
326 | bfd->demand_mode = 0; | |
327 | monotime(&bfd->downtime); | |
328 | ||
329 | ptm_bfd_snd(bfd, 0); | |
330 | ||
b912b189 RZ |
331 | /* Slow down the control packets, the connection is down. */ |
332 | bs_set_slow_timers(bfd); | |
333 | ||
e9e2c950 RZ |
334 | /* only signal clients when going from up->down state */ |
335 | if (old_state == PTM_BFD_UP) | |
336 | control_notify(bfd); | |
337 | ||
e9e2c950 RZ |
338 | /* Stop echo packet transmission if they are active */ |
339 | if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) | |
8bd859f6 | 340 | ptm_bfd_echo_stop(bfd); |
03e7f088 | 341 | |
0684c9b1 RZ |
342 | if (old_state != bfd->ses_state) { |
343 | bfd->stats.session_down++; | |
03e7f088 RZ |
344 | log_info("state-change: [%s] %s -> %s reason:%s", |
345 | bs_to_string(bfd), state_list[old_state].str, | |
346 | state_list[bfd->ses_state].str, | |
347 | get_diag_str(bfd->local_diag)); | |
0684c9b1 | 348 | } |
e9e2c950 RZ |
349 | } |
350 | ||
e9e2c950 RZ |
351 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, |
352 | uint32_t ldisc) | |
353 | { | |
354 | struct bfd_session *bs; | |
355 | ||
356 | bs = bfd_id_lookup(ldisc); | |
357 | if (bs == NULL) | |
358 | return NULL; | |
359 | ||
79b4a6fc | 360 | switch (bs->key.family) { |
e9e2c950 | 361 | case AF_INET: |
79b4a6fc RZ |
362 | if (memcmp(&sa->sa_sin.sin_addr, &bs->key.peer, |
363 | sizeof(sa->sa_sin.sin_addr))) | |
364 | return NULL; | |
e9e2c950 RZ |
365 | break; |
366 | case AF_INET6: | |
79b4a6fc RZ |
367 | if (memcmp(&sa->sa_sin6.sin6_addr, &bs->key.peer, |
368 | sizeof(sa->sa_sin6.sin6_addr))) | |
369 | return NULL; | |
e9e2c950 RZ |
370 | break; |
371 | } | |
372 | ||
79b4a6fc | 373 | return bs; |
e9e2c950 RZ |
374 | } |
375 | ||
b333abc2 | 376 | struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp, |
e9e2c950 RZ |
377 | struct sockaddr_any *peer, |
378 | struct sockaddr_any *local, | |
b333abc2 RZ |
379 | ifindex_t ifindex, vrf_id_t vrfid, |
380 | bool is_mhop) | |
e9e2c950 | 381 | { |
79b4a6fc RZ |
382 | struct interface *ifp; |
383 | struct vrf *vrf; | |
384 | struct bfd_key key; | |
e9e2c950 | 385 | |
9d63adda RZ |
386 | /* Find our session using the ID signaled by the remote end. */ |
387 | if (cp->discrs.remote_discr) | |
388 | return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr)); | |
389 | ||
390 | /* Search for session without using discriminator. */ | |
79b4a6fc RZ |
391 | ifp = if_lookup_by_index(ifindex, vrfid); |
392 | if (vrfid == VRF_DEFAULT) { | |
393 | /* | |
394 | * Don't use the default vrf, otherwise we won't find | |
395 | * sessions that doesn't specify it. | |
396 | */ | |
397 | vrf = NULL; | |
398 | } else | |
399 | vrf = vrf_lookup_by_id(vrfid); | |
9d63adda | 400 | |
79b4a6fc RZ |
401 | gen_bfd_key(&key, peer, local, is_mhop, ifp ? ifp->name : NULL, |
402 | vrf ? vrf->name : NULL); | |
e9e2c950 | 403 | |
9d63adda | 404 | /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */ |
79b4a6fc | 405 | return bfd_key_lookup(key); |
e9e2c950 RZ |
406 | } |
407 | ||
e9e2c950 RZ |
408 | int bfd_xmt_cb(struct thread *t) |
409 | { | |
410 | struct bfd_session *bs = THREAD_ARG(t); | |
411 | ||
412 | ptm_bfd_xmt_TO(bs, 0); | |
413 | ||
414 | return 0; | |
415 | } | |
416 | ||
417 | int bfd_echo_xmt_cb(struct thread *t) | |
418 | { | |
419 | struct bfd_session *bs = THREAD_ARG(t); | |
420 | ||
451eb5a2 RZ |
421 | if (bs->echo_xmt_TO > 0) |
422 | ptm_bfd_echo_xmt_TO(bs); | |
e9e2c950 RZ |
423 | |
424 | return 0; | |
425 | } | |
426 | ||
427 | /* Was ptm_bfd_detect_TO() */ | |
428 | int bfd_recvtimer_cb(struct thread *t) | |
429 | { | |
430 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
431 | |
432 | switch (bs->ses_state) { | |
433 | case PTM_BFD_INIT: | |
434 | case PTM_BFD_UP: | |
bc50bcc8 | 435 | ptm_bfd_sess_dn(bs, BD_CONTROL_EXPIRED); |
e9e2c950 RZ |
436 | bfd_recvtimer_update(bs); |
437 | break; | |
438 | ||
439 | default: | |
440 | /* Second detect time expiration, zero remote discr (section | |
441 | * 6.5.1) | |
442 | */ | |
443 | bs->discrs.remote_discr = 0; | |
444 | break; | |
445 | } | |
446 | ||
e9e2c950 RZ |
447 | return 0; |
448 | } | |
449 | ||
450 | /* Was ptm_bfd_echo_detect_TO() */ | |
451 | int bfd_echo_recvtimer_cb(struct thread *t) | |
452 | { | |
453 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
454 | |
455 | switch (bs->ses_state) { | |
456 | case PTM_BFD_INIT: | |
457 | case PTM_BFD_UP: | |
bc50bcc8 | 458 | ptm_bfd_sess_dn(bs, BD_ECHO_FAILED); |
e9e2c950 RZ |
459 | break; |
460 | } | |
461 | ||
e9e2c950 RZ |
462 | return 0; |
463 | } | |
464 | ||
d245e522 | 465 | static struct bfd_session *bfd_session_new(void) |
e9e2c950 RZ |
466 | { |
467 | struct bfd_session *bs; | |
468 | ||
469 | bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs)); | |
e9e2c950 RZ |
470 | |
471 | QOBJ_REG(bs, bfd_session); | |
472 | ||
f43b9368 | 473 | bs->timers.desired_min_tx = BFD_DEFDESIREDMINTX; |
e9e2c950 RZ |
474 | bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX; |
475 | bs->timers.required_min_echo = BFD_DEF_REQ_MIN_ECHO; | |
476 | bs->detect_mult = BFD_DEFDETECTMULT; | |
477 | bs->mh_ttl = BFD_DEF_MHOP_TTL; | |
d245e522 | 478 | bs->ses_state = PTM_BFD_DOWN; |
e9e2c950 | 479 | |
b912b189 RZ |
480 | /* Initiate connection with slow timers. */ |
481 | bs_set_slow_timers(bs); | |
f43b9368 RZ |
482 | |
483 | /* Initiate remote settings as well. */ | |
484 | bs->remote_timers = bs->cur_timers; | |
485 | bs->remote_detect_mult = BFD_DEFDETECTMULT; | |
486 | ||
d245e522 | 487 | bs->sock = -1; |
e9e2c950 RZ |
488 | monotime(&bs->uptime); |
489 | bs->downtime = bs->uptime; | |
490 | ||
491 | return bs; | |
492 | } | |
493 | ||
494 | int bfd_session_update_label(struct bfd_session *bs, const char *nlabel) | |
495 | { | |
496 | /* New label treatment: | |
497 | * - Check if the label is taken; | |
498 | * - Try to allocate the memory for it and register; | |
499 | */ | |
500 | if (bs->pl == NULL) { | |
501 | if (pl_find(nlabel) != NULL) { | |
502 | /* Someone is already using it. */ | |
503 | return -1; | |
504 | } | |
505 | ||
506 | if (pl_new(nlabel, bs) == NULL) | |
507 | return -1; | |
508 | ||
509 | return 0; | |
510 | } | |
511 | ||
512 | /* | |
513 | * Test label change consistency: | |
514 | * - Do nothing if it's the same label; | |
515 | * - Check if the future label is already taken; | |
516 | * - Change label; | |
517 | */ | |
518 | if (strcmp(nlabel, bs->pl->pl_label) == 0) | |
519 | return -1; | |
520 | if (pl_find(nlabel) != NULL) | |
521 | return -1; | |
522 | ||
523 | strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label)); | |
524 | return 0; | |
525 | } | |
526 | ||
527 | static void _bfd_session_update(struct bfd_session *bs, | |
528 | struct bfd_peer_cfg *bpc) | |
529 | { | |
530 | if (bpc->bpc_echo) { | |
531 | /* Check if echo mode is already active. */ | |
532 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
533 | goto skip_echo; | |
534 | ||
535 | BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
e9e2c950 RZ |
536 | |
537 | /* Activate/update echo receive timeout timer. */ | |
73c62f8e | 538 | bs_echo_timer_handler(bs); |
e9e2c950 RZ |
539 | } else { |
540 | /* Check if echo mode is already disabled. */ | |
541 | if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
542 | goto skip_echo; | |
543 | ||
544 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
8bd859f6 | 545 | ptm_bfd_echo_stop(bs); |
e9e2c950 RZ |
546 | } |
547 | ||
548 | skip_echo: | |
549 | if (bpc->bpc_has_txinterval) | |
f43b9368 | 550 | bs->timers.desired_min_tx = bpc->bpc_txinterval * 1000; |
e9e2c950 RZ |
551 | |
552 | if (bpc->bpc_has_recvinterval) | |
553 | bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000; | |
554 | ||
555 | if (bpc->bpc_has_detectmultiplier) | |
556 | bs->detect_mult = bpc->bpc_detectmultiplier; | |
557 | ||
558 | if (bpc->bpc_has_echointerval) | |
559 | bs->timers.required_min_echo = bpc->bpc_echointerval * 1000; | |
560 | ||
561 | if (bpc->bpc_has_label) | |
562 | bfd_session_update_label(bs, bpc->bpc_label); | |
563 | ||
564 | if (bpc->bpc_shutdown) { | |
565 | /* Check if already shutdown. */ | |
566 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN)) | |
567 | return; | |
568 | ||
569 | BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
570 | ||
571 | /* Disable all events. */ | |
572 | bfd_recvtimer_delete(bs); | |
573 | bfd_echo_recvtimer_delete(bs); | |
574 | bfd_xmttimer_delete(bs); | |
575 | bfd_echo_xmttimer_delete(bs); | |
576 | ||
577 | /* Change and notify state change. */ | |
578 | bs->ses_state = PTM_BFD_ADM_DOWN; | |
579 | control_notify(bs); | |
580 | ||
d245e522 RZ |
581 | /* Don't try to send packets with a disabled session. */ |
582 | if (bs->sock != -1) | |
583 | ptm_bfd_snd(bs, 0); | |
e9e2c950 RZ |
584 | } else { |
585 | /* Check if already working. */ | |
586 | if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN)) | |
587 | return; | |
588 | ||
589 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
590 | ||
591 | /* Change and notify state change. */ | |
592 | bs->ses_state = PTM_BFD_DOWN; | |
593 | control_notify(bs); | |
594 | ||
595 | /* Enable all timers. */ | |
596 | bfd_recvtimer_update(bs); | |
597 | bfd_xmttimer_update(bs, bs->xmt_TO); | |
e9e2c950 RZ |
598 | } |
599 | } | |
600 | ||
601 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc) | |
602 | { | |
603 | /* User didn't want to update, return failure. */ | |
604 | if (bpc->bpc_createonly) | |
605 | return -1; | |
606 | ||
607 | _bfd_session_update(bs, bpc); | |
608 | ||
e9e2c950 RZ |
609 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); |
610 | ||
611 | return 0; | |
612 | } | |
613 | ||
614 | static void bfd_session_free(struct bfd_session *bs) | |
615 | { | |
d245e522 | 616 | struct bfd_session_observer *bso; |
e9e2c950 | 617 | |
d245e522 | 618 | bfd_session_disable(bs); |
e9e2c950 | 619 | |
79b4a6fc RZ |
620 | bfd_key_delete(bs->key); |
621 | bfd_id_delete(bs->discrs.my_discr); | |
622 | ||
d245e522 RZ |
623 | /* Remove observer if any. */ |
624 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
625 | if (bso->bso_bs != bs) | |
626 | continue; | |
627 | ||
628 | break; | |
629 | } | |
630 | if (bso != NULL) | |
631 | bs_observer_del(bso); | |
e9e2c950 RZ |
632 | |
633 | pl_free(bs->pl); | |
634 | ||
635 | QOBJ_UNREG(bs); | |
636 | XFREE(MTYPE_BFDD_CONFIG, bs); | |
637 | } | |
638 | ||
639 | struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc) | |
640 | { | |
641 | struct bfd_session *bfd, *l_bfd; | |
e9e2c950 RZ |
642 | |
643 | /* check to see if this needs a new session */ | |
644 | l_bfd = bs_peer_find(bpc); | |
645 | if (l_bfd) { | |
646 | /* Requesting a duplicated peer means update configuration. */ | |
647 | if (bfd_session_update(l_bfd, bpc) == 0) | |
648 | return l_bfd; | |
649 | else | |
650 | return NULL; | |
651 | } | |
652 | ||
d245e522 RZ |
653 | /* Get BFD session storage with its defaults. */ |
654 | bfd = bfd_session_new(); | |
e9e2c950 | 655 | if (bfd == NULL) { |
03e7f088 | 656 | log_error("session-new: allocation failed"); |
e9e2c950 RZ |
657 | return NULL; |
658 | } | |
659 | ||
d245e522 RZ |
660 | /* |
661 | * Store interface/VRF name in case we need to delay session | |
662 | * start. See `bfd_session_enable` for more information. | |
663 | */ | |
664 | if (bpc->bpc_has_localif) | |
79b4a6fc RZ |
665 | strlcpy(bfd->key.ifname, bpc->bpc_localif, |
666 | sizeof(bfd->key.ifname)); | |
e9e2c950 | 667 | |
d245e522 | 668 | if (bpc->bpc_has_vrfname) |
79b4a6fc RZ |
669 | strlcpy(bfd->key.vrfname, bpc->bpc_vrfname, |
670 | sizeof(bfd->key.vrfname)); | |
e9e2c950 | 671 | |
d245e522 RZ |
672 | /* Copy remaining data. */ |
673 | if (bpc->bpc_ipv4 == false) | |
674 | BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6); | |
e9e2c950 | 675 | |
79b4a6fc RZ |
676 | bfd->key.family = (bpc->bpc_ipv4) ? AF_INET : AF_INET6; |
677 | switch (bfd->key.family) { | |
678 | case AF_INET: | |
679 | memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin.sin_addr, | |
680 | sizeof(bpc->bpc_peer.sa_sin.sin_addr)); | |
681 | memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin.sin_addr, | |
682 | sizeof(bpc->bpc_local.sa_sin.sin_addr)); | |
683 | break; | |
684 | ||
685 | case AF_INET6: | |
686 | memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin6.sin6_addr, | |
687 | sizeof(bpc->bpc_peer.sa_sin6.sin6_addr)); | |
688 | memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin6.sin6_addr, | |
689 | sizeof(bpc->bpc_local.sa_sin6.sin6_addr)); | |
690 | break; | |
691 | ||
692 | default: | |
693 | assert(1); | |
694 | break; | |
d245e522 | 695 | } |
e9e2c950 | 696 | |
79b4a6fc RZ |
697 | if (bpc->bpc_mhop) |
698 | BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH); | |
699 | ||
700 | bfd->key.mhop = bpc->bpc_mhop; | |
701 | ||
702 | /* Registrate session into data structures. */ | |
703 | bfd_key_insert(bfd); | |
704 | bfd->discrs.my_discr = ptm_bfd_gen_ID(); | |
705 | bfd_id_insert(bfd); | |
d245e522 RZ |
706 | |
707 | /* Try to enable session and schedule for packet receive/send. */ | |
708 | if (bfd_session_enable(bfd) == -1) { | |
709 | /* Unrecoverable failure, remove the session/peer. */ | |
710 | bfd_session_free(bfd); | |
711 | return NULL; | |
e9e2c950 RZ |
712 | } |
713 | ||
261e0ba9 RZ |
714 | /* Add observer if we have moving parts. */ |
715 | if (bfd->key.ifname[0] || bfd->key.vrfname[0] || bfd->sock == -1) | |
716 | bs_observer_add(bfd); | |
717 | ||
d245e522 | 718 | /* Apply other configurations. */ |
e9e2c950 RZ |
719 | _bfd_session_update(bfd, bpc); |
720 | ||
03e7f088 | 721 | log_info("session-new: %s", bs_to_string(bfd)); |
e9e2c950 RZ |
722 | |
723 | control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd); | |
724 | ||
725 | return bfd; | |
726 | } | |
727 | ||
bc50bcc8 | 728 | int ptm_bfd_sess_del(struct bfd_peer_cfg *bpc) |
e9e2c950 RZ |
729 | { |
730 | struct bfd_session *bs; | |
731 | ||
732 | /* Find session and call free(). */ | |
733 | bs = bs_peer_find(bpc); | |
734 | if (bs == NULL) | |
735 | return -1; | |
736 | ||
737 | /* This pointer is being referenced, don't let it be deleted. */ | |
738 | if (bs->refcount > 0) { | |
03e7f088 RZ |
739 | log_error("session-delete: refcount failure: %" PRIu64 |
740 | " references", | |
741 | bs->refcount); | |
e9e2c950 RZ |
742 | return -1; |
743 | } | |
744 | ||
03e7f088 | 745 | log_info("session-delete: %s", bs_to_string(bs)); |
e9e2c950 RZ |
746 | |
747 | control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs); | |
748 | ||
749 | bfd_session_free(bs); | |
750 | ||
751 | return 0; | |
752 | } | |
753 | ||
d3f3a2c4 RZ |
754 | void bfd_set_polling(struct bfd_session *bs) |
755 | { | |
c0ef9a8a RZ |
756 | /* |
757 | * Start polling procedure: the only timers that require polling | |
758 | * to change value without losing connection are: | |
759 | * | |
760 | * - Desired minimum transmission interval; | |
761 | * - Required minimum receive interval; | |
762 | * | |
763 | * RFC 5880, Section 6.8.3. | |
764 | */ | |
d3f3a2c4 RZ |
765 | bs->polling = 1; |
766 | } | |
767 | ||
aef131af RZ |
768 | /* |
769 | * bs_<state>_handler() functions implement the BFD state machine | |
770 | * transition mechanism. `<state>` is the current session state and | |
771 | * the parameter `nstate` is the peer new state. | |
772 | */ | |
9f37770f RZ |
773 | static void bs_admin_down_handler(struct bfd_session *bs |
774 | __attribute__((__unused__)), | |
775 | int nstate __attribute__((__unused__))) | |
aef131af RZ |
776 | { |
777 | /* | |
778 | * We are administratively down, there is no state machine | |
779 | * handling. | |
780 | */ | |
781 | } | |
782 | ||
9f37770f | 783 | static void bs_down_handler(struct bfd_session *bs, int nstate) |
aef131af RZ |
784 | { |
785 | switch (nstate) { | |
786 | case PTM_BFD_ADM_DOWN: | |
787 | /* | |
788 | * Remote peer doesn't want to talk, so lets keep the | |
789 | * connection down. | |
790 | */ | |
791 | case PTM_BFD_UP: | |
792 | /* Peer can't be up yet, wait it go to 'init' or 'down'. */ | |
793 | break; | |
794 | ||
795 | case PTM_BFD_DOWN: | |
796 | /* | |
797 | * Remote peer agreed that the path is down, lets try to | |
798 | * bring it up. | |
799 | */ | |
800 | bs->ses_state = PTM_BFD_INIT; | |
801 | break; | |
802 | ||
803 | case PTM_BFD_INIT: | |
804 | /* | |
805 | * Remote peer told us his path is up, lets turn | |
806 | * activate the session. | |
807 | */ | |
bc50bcc8 | 808 | ptm_bfd_sess_up(bs); |
aef131af RZ |
809 | break; |
810 | ||
811 | default: | |
812 | log_debug("state-change: unhandled neighbor state: %d", nstate); | |
813 | break; | |
814 | } | |
815 | } | |
816 | ||
9f37770f | 817 | static void bs_init_handler(struct bfd_session *bs, int nstate) |
aef131af RZ |
818 | { |
819 | switch (nstate) { | |
820 | case PTM_BFD_ADM_DOWN: | |
821 | /* | |
822 | * Remote peer doesn't want to talk, so lets make the | |
823 | * connection down. | |
824 | */ | |
825 | bs->ses_state = PTM_BFD_DOWN; | |
826 | break; | |
827 | ||
828 | case PTM_BFD_DOWN: | |
829 | /* Remote peer hasn't moved to first stage yet. */ | |
830 | break; | |
831 | ||
832 | case PTM_BFD_INIT: | |
833 | case PTM_BFD_UP: | |
834 | /* We agreed on the settings and the path is up. */ | |
bc50bcc8 | 835 | ptm_bfd_sess_up(bs); |
aef131af RZ |
836 | break; |
837 | ||
838 | default: | |
839 | log_debug("state-change: unhandled neighbor state: %d", nstate); | |
840 | break; | |
841 | } | |
842 | } | |
843 | ||
9f37770f | 844 | static void bs_up_handler(struct bfd_session *bs, int nstate) |
aef131af RZ |
845 | { |
846 | switch (nstate) { | |
847 | case PTM_BFD_ADM_DOWN: | |
848 | case PTM_BFD_DOWN: | |
849 | /* Peer lost or asked to shutdown connection. */ | |
bc50bcc8 | 850 | ptm_bfd_sess_dn(bs, BD_NEIGHBOR_DOWN); |
aef131af RZ |
851 | break; |
852 | ||
853 | case PTM_BFD_INIT: | |
854 | case PTM_BFD_UP: | |
855 | /* Path is up and working. */ | |
856 | break; | |
857 | ||
858 | default: | |
859 | log_debug("state-change: unhandled neighbor state: %d", nstate); | |
860 | break; | |
861 | } | |
862 | } | |
863 | ||
864 | void bs_state_handler(struct bfd_session *bs, int nstate) | |
865 | { | |
866 | switch (bs->ses_state) { | |
867 | case PTM_BFD_ADM_DOWN: | |
868 | bs_admin_down_handler(bs, nstate); | |
869 | break; | |
870 | case PTM_BFD_DOWN: | |
871 | bs_down_handler(bs, nstate); | |
872 | break; | |
873 | case PTM_BFD_INIT: | |
874 | bs_init_handler(bs, nstate); | |
875 | break; | |
876 | case PTM_BFD_UP: | |
877 | bs_up_handler(bs, nstate); | |
878 | break; | |
879 | ||
880 | default: | |
881 | log_debug("state-change: [%s] is in invalid state: %d", | |
882 | bs_to_string(bs), nstate); | |
883 | break; | |
884 | } | |
885 | } | |
886 | ||
c0ef9a8a RZ |
887 | /* |
888 | * Handles echo timer manipulation after updating timer. | |
889 | */ | |
890 | void bs_echo_timer_handler(struct bfd_session *bs) | |
891 | { | |
892 | uint32_t old_timer; | |
893 | ||
894 | /* | |
895 | * Before doing any echo handling, check if it is possible to | |
896 | * use it. | |
897 | * | |
898 | * - Check for `echo-mode` configuration. | |
899 | * - Check that we are not using multi hop (RFC 5883, | |
900 | * Section 3). | |
901 | * - Check that we are already at the up state. | |
902 | */ | |
903 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO) == 0 | |
904 | || BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH) | |
905 | || bs->ses_state != PTM_BFD_UP) | |
906 | return; | |
907 | ||
908 | /* Remote peer asked to stop echo. */ | |
909 | if (bs->remote_timers.required_min_echo == 0) { | |
910 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) | |
8bd859f6 | 911 | ptm_bfd_echo_stop(bs); |
c0ef9a8a RZ |
912 | |
913 | return; | |
914 | } | |
915 | ||
916 | /* | |
917 | * Calculate the echo transmission timer: we must not send | |
918 | * echo packets faster than the minimum required time | |
919 | * announced by the remote system. | |
920 | * | |
921 | * RFC 5880, Section 6.8.9. | |
922 | */ | |
923 | old_timer = bs->echo_xmt_TO; | |
924 | if (bs->remote_timers.required_min_echo > bs->timers.required_min_echo) | |
925 | bs->echo_xmt_TO = bs->remote_timers.required_min_echo; | |
926 | else | |
927 | bs->echo_xmt_TO = bs->timers.required_min_echo; | |
928 | ||
929 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE) == 0 | |
930 | || old_timer != bs->echo_xmt_TO) | |
931 | ptm_bfd_echo_start(bs); | |
932 | } | |
933 | ||
934 | /* | |
935 | * RFC 5880 Section 6.5. | |
936 | * | |
937 | * When a BFD control packet with the final bit is received, we must | |
938 | * update the session parameters. | |
939 | */ | |
940 | void bs_final_handler(struct bfd_session *bs) | |
941 | { | |
942 | /* Start using our new timers. */ | |
f43b9368 RZ |
943 | bs->cur_timers.desired_min_tx = bs->timers.desired_min_tx; |
944 | bs->cur_timers.required_min_rx = bs->timers.required_min_rx; | |
c0ef9a8a RZ |
945 | |
946 | /* | |
947 | * TODO: demand mode. See RFC 5880 Section 6.1. | |
948 | * | |
949 | * When using demand mode we must disable the detection timer | |
950 | * for lost control packets. | |
951 | */ | |
952 | if (bs->demand_mode) { | |
953 | /* Notify watchers about changed timers. */ | |
954 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); | |
955 | return; | |
956 | } | |
957 | ||
958 | /* | |
959 | * Calculate detection time based on new timers. | |
960 | * | |
961 | * Transmission calculation: | |
962 | * We must respect the RequiredMinRxInterval from the remote | |
963 | * system: if our desired transmission timer is more than the | |
964 | * minimum receive rate, then we must lower it to at least the | |
965 | * minimum receive interval. | |
966 | * | |
967 | * RFC 5880, Section 6.8.3. | |
968 | */ | |
969 | if (bs->timers.desired_min_tx > bs->remote_timers.required_min_rx) | |
970 | bs->xmt_TO = bs->remote_timers.required_min_rx; | |
971 | else | |
972 | bs->xmt_TO = bs->timers.desired_min_tx; | |
973 | ||
974 | /* Apply new transmission timer immediately. */ | |
975 | ptm_bfd_start_xmt_timer(bs, false); | |
976 | ||
977 | /* | |
978 | * Detection timeout calculation: | |
979 | * The minimum detection timeout is the remote detection | |
980 | * multipler (number of packets to be missed) times the agreed | |
981 | * transmission interval. | |
982 | * | |
983 | * RFC 5880, Section 6.8.4. | |
984 | * | |
985 | * TODO: support sending/counting more packets inside detection | |
986 | * timeout. | |
987 | */ | |
988 | if (bs->remote_timers.required_min_rx > bs->timers.desired_min_tx) | |
989 | bs->detect_TO = bs->remote_detect_mult | |
990 | * bs->remote_timers.required_min_rx; | |
991 | else | |
992 | bs->detect_TO = bs->remote_detect_mult | |
993 | * bs->timers.desired_min_tx; | |
994 | ||
995 | /* Apply new receive timer immediately. */ | |
996 | bfd_recvtimer_update(bs); | |
997 | ||
998 | /* Notify watchers about changed timers. */ | |
999 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); | |
1000 | } | |
1001 | ||
b912b189 RZ |
1002 | void bs_set_slow_timers(struct bfd_session *bs) |
1003 | { | |
1004 | /* | |
1005 | * BFD connection must use slow timers before going up or after | |
1006 | * losing connectivity to avoid wasting bandwidth. | |
1007 | * | |
1008 | * RFC 5880, Section 6.8.3. | |
1009 | */ | |
1010 | bs->cur_timers.desired_min_tx = BFD_DEF_SLOWTX; | |
1011 | bs->cur_timers.required_min_rx = BFD_DEF_SLOWTX; | |
1012 | bs->cur_timers.required_min_echo = 0; | |
1013 | ||
1014 | /* Set the appropriated timeouts for slow connection. */ | |
1015 | bs->detect_TO = (BFD_DEFDETECTMULT * BFD_DEF_SLOWTX); | |
1016 | bs->xmt_TO = BFD_DEF_SLOWTX; | |
1017 | } | |
e9e2c950 RZ |
1018 | |
1019 | /* | |
1020 | * Helper functions. | |
1021 | */ | |
1022 | static const char *get_diag_str(int diag) | |
1023 | { | |
1024 | for (int i = 0; diag_list[i].str; i++) { | |
1025 | if (diag_list[i].type == diag) | |
1026 | return diag_list[i].str; | |
1027 | } | |
1028 | return "N/A"; | |
1029 | } | |
1030 | ||
1031 | const char *satostr(struct sockaddr_any *sa) | |
1032 | { | |
1033 | #define INETSTR_BUFCOUNT 8 | |
1034 | static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN]; | |
1035 | static int bufidx; | |
1036 | struct sockaddr_in *sin = &sa->sa_sin; | |
1037 | struct sockaddr_in6 *sin6 = &sa->sa_sin6; | |
1038 | ||
1039 | bufidx += (bufidx + 1) % INETSTR_BUFCOUNT; | |
1040 | buf[bufidx][0] = 0; | |
1041 | ||
1042 | switch (sin->sin_family) { | |
1043 | case AF_INET: | |
1044 | inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx], | |
1045 | sizeof(buf[bufidx])); | |
1046 | break; | |
1047 | case AF_INET6: | |
1048 | inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx], | |
1049 | sizeof(buf[bufidx])); | |
1050 | break; | |
1051 | ||
1052 | default: | |
1053 | strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx])); | |
1054 | break; | |
1055 | } | |
1056 | ||
1057 | return buf[bufidx]; | |
1058 | } | |
1059 | ||
1060 | const char *diag2str(uint8_t diag) | |
1061 | { | |
1062 | switch (diag) { | |
1063 | case 0: | |
1064 | return "ok"; | |
1065 | case 1: | |
1066 | return "control detection time expired"; | |
1067 | case 2: | |
1068 | return "echo function failed"; | |
1069 | case 3: | |
1070 | return "neighbor signaled session down"; | |
1071 | case 4: | |
1072 | return "forwarding plane reset"; | |
1073 | case 5: | |
1074 | return "path down"; | |
1075 | case 6: | |
1076 | return "concatenated path down"; | |
1077 | case 7: | |
1078 | return "administratively down"; | |
1079 | case 8: | |
1080 | return "reverse concatenated path down"; | |
1081 | default: | |
1082 | return "unknown"; | |
1083 | } | |
1084 | } | |
1085 | ||
1086 | int strtosa(const char *addr, struct sockaddr_any *sa) | |
1087 | { | |
1088 | memset(sa, 0, sizeof(*sa)); | |
1089 | ||
1090 | if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) { | |
1091 | sa->sa_sin.sin_family = AF_INET; | |
1092 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
1093 | sa->sa_sin.sin_len = sizeof(sa->sa_sin); | |
1094 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
1095 | return 0; | |
1096 | } | |
1097 | ||
1098 | if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) { | |
1099 | sa->sa_sin6.sin6_family = AF_INET6; | |
1100 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
1101 | sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6); | |
1102 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
1103 | return 0; | |
1104 | } | |
1105 | ||
1106 | return -1; | |
1107 | } | |
1108 | ||
1109 | void integer2timestr(uint64_t time, char *buf, size_t buflen) | |
1110 | { | |
1111 | unsigned int year, month, day, hour, minute, second; | |
1112 | int rv; | |
1113 | ||
1114 | #define MINUTES (60) | |
23586b05 DS |
1115 | #define HOURS (60 * MINUTES) |
1116 | #define DAYS (24 * HOURS) | |
1117 | #define MONTHS (30 * DAYS) | |
1118 | #define YEARS (12 * MONTHS) | |
e9e2c950 RZ |
1119 | if (time >= YEARS) { |
1120 | year = time / YEARS; | |
1121 | time -= year * YEARS; | |
1122 | ||
1123 | rv = snprintf(buf, buflen, "%u year(s), ", year); | |
1124 | buf += rv; | |
1125 | buflen -= rv; | |
1126 | } | |
1127 | if (time >= MONTHS) { | |
1128 | month = time / MONTHS; | |
1129 | time -= month * MONTHS; | |
1130 | ||
1131 | rv = snprintf(buf, buflen, "%u month(s), ", month); | |
1132 | buf += rv; | |
1133 | buflen -= rv; | |
1134 | } | |
1135 | if (time >= DAYS) { | |
1136 | day = time / DAYS; | |
1137 | time -= day * DAYS; | |
1138 | ||
1139 | rv = snprintf(buf, buflen, "%u day(s), ", day); | |
1140 | buf += rv; | |
1141 | buflen -= rv; | |
1142 | } | |
1143 | if (time >= HOURS) { | |
1144 | hour = time / HOURS; | |
1145 | time -= hour * HOURS; | |
1146 | ||
1147 | rv = snprintf(buf, buflen, "%u hour(s), ", hour); | |
1148 | buf += rv; | |
1149 | buflen -= rv; | |
1150 | } | |
1151 | if (time >= MINUTES) { | |
1152 | minute = time / MINUTES; | |
1153 | time -= minute * MINUTES; | |
1154 | ||
1155 | rv = snprintf(buf, buflen, "%u minute(s), ", minute); | |
1156 | buf += rv; | |
1157 | buflen -= rv; | |
1158 | } | |
1159 | second = time % MINUTES; | |
1160 | snprintf(buf, buflen, "%u second(s)", second); | |
1161 | } | |
1162 | ||
79b4a6fc | 1163 | const char *bs_to_string(const struct bfd_session *bs) |
03e7f088 RZ |
1164 | { |
1165 | static char buf[256]; | |
79b4a6fc | 1166 | char addr_buf[INET6_ADDRSTRLEN]; |
03e7f088 RZ |
1167 | int pos; |
1168 | bool is_mhop = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH); | |
1169 | ||
1170 | pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no"); | |
79b4a6fc RZ |
1171 | pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s", |
1172 | inet_ntop(bs->key.family, &bs->key.peer, addr_buf, | |
1173 | sizeof(addr_buf))); | |
1174 | pos += snprintf(buf + pos, sizeof(buf) - pos, " local:%s", | |
1175 | inet_ntop(bs->key.family, &bs->key.local, addr_buf, | |
1176 | sizeof(addr_buf))); | |
1177 | if (bs->key.vrfname[0]) | |
1178 | pos += snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s", | |
1179 | bs->key.vrfname); | |
1180 | if (bs->key.ifname[0]) | |
1181 | pos += snprintf(buf + pos, sizeof(buf) - pos, " ifname:%s", | |
1182 | bs->key.ifname); | |
3cef9b7f DS |
1183 | |
1184 | (void)pos; | |
1185 | ||
03e7f088 RZ |
1186 | return buf; |
1187 | } | |
1188 | ||
d245e522 RZ |
1189 | int bs_observer_add(struct bfd_session *bs) |
1190 | { | |
1191 | struct bfd_session_observer *bso; | |
1192 | ||
ed74032b DS |
1193 | bso = XCALLOC(MTYPE_BFDD_SESSION_OBSERVER, sizeof(*bso)); |
1194 | bso->bso_isaddress = false; | |
d245e522 RZ |
1195 | bso->bso_bs = bs; |
1196 | bso->bso_isinterface = !BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH); | |
1197 | if (bso->bso_isinterface) | |
79b4a6fc | 1198 | strlcpy(bso->bso_entryname, bs->key.ifname, |
d245e522 RZ |
1199 | sizeof(bso->bso_entryname)); |
1200 | else | |
79b4a6fc | 1201 | strlcpy(bso->bso_entryname, bs->key.vrfname, |
d245e522 RZ |
1202 | sizeof(bso->bso_entryname)); |
1203 | ||
261e0ba9 RZ |
1204 | /* Handle socket binding failures caused by missing local addresses. */ |
1205 | if (bs->sock == -1) { | |
1206 | bso->bso_isaddress = true; | |
1207 | bso->bso_addr.family = bs->key.family; | |
1208 | memcpy(&bso->bso_addr.u.prefix, &bs->key.local, | |
1209 | sizeof(bs->key.local)); | |
1210 | } | |
1211 | ||
d245e522 RZ |
1212 | TAILQ_INSERT_TAIL(&bglobal.bg_obslist, bso, bso_entry); |
1213 | ||
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | void bs_observer_del(struct bfd_session_observer *bso) | |
1218 | { | |
1219 | TAILQ_REMOVE(&bglobal.bg_obslist, bso, bso_entry); | |
1220 | XFREE(MTYPE_BFDD_SESSION_OBSERVER, bso); | |
1221 | } | |
1222 | ||
79b4a6fc RZ |
1223 | void bs_to_bpc(struct bfd_session *bs, struct bfd_peer_cfg *bpc) |
1224 | { | |
1225 | memset(bpc, 0, sizeof(*bpc)); | |
1226 | ||
1227 | bpc->bpc_ipv4 = (bs->key.family == AF_INET); | |
1228 | bpc->bpc_mhop = bs->key.mhop; | |
1229 | ||
1230 | switch (bs->key.family) { | |
1231 | case AF_INET: | |
1232 | bpc->bpc_peer.sa_sin.sin_family = AF_INET; | |
1233 | memcpy(&bpc->bpc_peer.sa_sin.sin_addr, &bs->key.peer, | |
1234 | sizeof(bpc->bpc_peer.sa_sin.sin_addr)); | |
1235 | ||
1236 | if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local))) { | |
1237 | bpc->bpc_local.sa_sin.sin_family = AF_INET6; | |
5ff7d1be | 1238 | memcpy(&bpc->bpc_local.sa_sin.sin_addr, &bs->key.local, |
79b4a6fc RZ |
1239 | sizeof(bpc->bpc_local.sa_sin.sin_addr)); |
1240 | } | |
1241 | break; | |
1242 | ||
1243 | case AF_INET6: | |
1244 | bpc->bpc_peer.sa_sin.sin_family = AF_INET6; | |
1245 | memcpy(&bpc->bpc_peer.sa_sin6.sin6_addr, &bs->key.peer, | |
1246 | sizeof(bpc->bpc_peer.sa_sin6.sin6_addr)); | |
1247 | ||
1248 | bpc->bpc_local.sa_sin6.sin6_family = AF_INET6; | |
5ff7d1be | 1249 | memcpy(&bpc->bpc_local.sa_sin6.sin6_addr, &bs->key.local, |
79b4a6fc RZ |
1250 | sizeof(bpc->bpc_local.sa_sin6.sin6_addr)); |
1251 | break; | |
1252 | } | |
1253 | ||
1254 | if (bs->key.ifname[0]) { | |
1255 | bpc->bpc_has_localif = true; | |
1256 | strlcpy(bpc->bpc_localif, bs->key.ifname, | |
1257 | sizeof(bpc->bpc_localif)); | |
1258 | } | |
1259 | ||
1260 | if (bs->key.vrfname[0]) { | |
1261 | bpc->bpc_has_vrfname = true; | |
1262 | strlcpy(bpc->bpc_vrfname, bs->key.vrfname, | |
1263 | sizeof(bpc->bpc_vrfname)); | |
1264 | } | |
1265 | } | |
1266 | ||
e9e2c950 RZ |
1267 | |
1268 | /* | |
1269 | * BFD hash data structures to find sessions. | |
1270 | */ | |
1271 | static struct hash *bfd_id_hash; | |
79b4a6fc | 1272 | static struct hash *bfd_key_hash; |
e9e2c950 RZ |
1273 | |
1274 | static unsigned int bfd_id_hash_do(void *p); | |
79b4a6fc | 1275 | static unsigned int bfd_key_hash_do(void *p); |
e9e2c950 | 1276 | |
e3b78da8 | 1277 | static void _bfd_free(struct hash_bucket *hb, |
e9e2c950 | 1278 | void *arg __attribute__((__unused__))); |
e9e2c950 RZ |
1279 | |
1280 | /* BFD hash for our discriminator. */ | |
1281 | static unsigned int bfd_id_hash_do(void *p) | |
1282 | { | |
1283 | struct bfd_session *bs = p; | |
1284 | ||
1285 | return jhash_1word(bs->discrs.my_discr, 0); | |
1286 | } | |
1287 | ||
74df8d6d | 1288 | static bool bfd_id_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1289 | { |
1290 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
1291 | ||
1292 | return bs1->discrs.my_discr == bs2->discrs.my_discr; | |
1293 | } | |
1294 | ||
1295 | /* BFD hash for single hop. */ | |
79b4a6fc | 1296 | static unsigned int bfd_key_hash_do(void *p) |
e9e2c950 RZ |
1297 | { |
1298 | struct bfd_session *bs = p; | |
1299 | ||
79b4a6fc | 1300 | return jhash(&bs->key, sizeof(bs->key), 0); |
e9e2c950 RZ |
1301 | } |
1302 | ||
79b4a6fc | 1303 | static bool bfd_key_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1304 | { |
1305 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
1306 | ||
79b4a6fc | 1307 | return memcmp(&bs1->key, &bs2->key, sizeof(bs1->key)) == 0; |
e9e2c950 RZ |
1308 | } |
1309 | ||
e9e2c950 | 1310 | |
e9e2c950 RZ |
1311 | /* |
1312 | * Hash public interface / exported functions. | |
1313 | */ | |
1314 | ||
1315 | /* Lookup functions. */ | |
1316 | struct bfd_session *bfd_id_lookup(uint32_t id) | |
1317 | { | |
1318 | struct bfd_session bs; | |
1319 | ||
1320 | bs.discrs.my_discr = id; | |
1321 | ||
1322 | return hash_lookup(bfd_id_hash, &bs); | |
1323 | } | |
1324 | ||
79b4a6fc | 1325 | struct bfd_session *bfd_key_lookup(struct bfd_key key) |
e9e2c950 RZ |
1326 | { |
1327 | struct bfd_session bs, *bsp; | |
1328 | ||
79b4a6fc RZ |
1329 | bs.key = key; |
1330 | bsp = hash_lookup(bfd_key_hash, &bs); | |
e9e2c950 | 1331 | |
79b4a6fc RZ |
1332 | /* Handle cases where local-address is optional. */ |
1333 | if (bsp == NULL && bs.key.family == AF_INET) { | |
1334 | memset(&bs.key.local, 0, sizeof(bs.key.local)); | |
1335 | bsp = hash_lookup(bfd_key_hash, &bs); | |
e9e2c950 RZ |
1336 | } |
1337 | ||
79b4a6fc RZ |
1338 | /* Handle cases where ifname is optional. */ |
1339 | bs.key = key; | |
1340 | if (bsp == NULL && bs.key.ifname[0]) { | |
1341 | memset(bs.key.ifname, 0, sizeof(bs.key.ifname)); | |
1342 | bsp = hash_lookup(bfd_key_hash, &bs); | |
e9e2c950 | 1343 | |
79b4a6fc RZ |
1344 | /* Handle cases where local-address and ifname are optional. */ |
1345 | if (bsp == NULL && bs.key.family == AF_INET) { | |
1346 | memset(&bs.key.local, 0, sizeof(bs.key.local)); | |
1347 | bsp = hash_lookup(bfd_key_hash, &bs); | |
1348 | } | |
1349 | } | |
e9e2c950 | 1350 | |
79b4a6fc | 1351 | return bsp; |
e9e2c950 RZ |
1352 | } |
1353 | ||
e9e2c950 RZ |
1354 | /* |
1355 | * Delete functions. | |
1356 | * | |
1357 | * Delete functions searches and remove the item from the hash and | |
1358 | * returns a pointer to the removed item data. If the item was not found | |
1359 | * then it returns NULL. | |
1360 | * | |
1361 | * The data stored inside the hash is not free()ed, so you must do it | |
1362 | * manually after getting the pointer back. | |
1363 | */ | |
1364 | struct bfd_session *bfd_id_delete(uint32_t id) | |
1365 | { | |
1366 | struct bfd_session bs; | |
1367 | ||
1368 | bs.discrs.my_discr = id; | |
1369 | ||
1370 | return hash_release(bfd_id_hash, &bs); | |
1371 | } | |
1372 | ||
79b4a6fc | 1373 | struct bfd_session *bfd_key_delete(struct bfd_key key) |
e9e2c950 RZ |
1374 | { |
1375 | struct bfd_session bs, *bsp; | |
1376 | ||
79b4a6fc RZ |
1377 | bs.key = key; |
1378 | bsp = hash_lookup(bfd_key_hash, &bs); | |
1379 | if (bsp == NULL && key.ifname[0]) { | |
1380 | memset(bs.key.ifname, 0, sizeof(bs.key.ifname)); | |
1381 | bsp = hash_lookup(bfd_key_hash, &bs); | |
e9e2c950 RZ |
1382 | } |
1383 | ||
79b4a6fc | 1384 | return hash_release(bfd_key_hash, bsp); |
e9e2c950 RZ |
1385 | } |
1386 | ||
e9e2c950 RZ |
1387 | /* Iteration functions. */ |
1388 | void bfd_id_iterate(hash_iter_func hif, void *arg) | |
1389 | { | |
1390 | hash_iterate(bfd_id_hash, hif, arg); | |
1391 | } | |
1392 | ||
79b4a6fc | 1393 | void bfd_key_iterate(hash_iter_func hif, void *arg) |
e9e2c950 | 1394 | { |
79b4a6fc | 1395 | hash_iterate(bfd_key_hash, hif, arg); |
e9e2c950 RZ |
1396 | } |
1397 | ||
e9e2c950 RZ |
1398 | /* |
1399 | * Insert functions. | |
1400 | * | |
1401 | * Inserts session into hash and returns `true` on success, otherwise | |
1402 | * `false`. | |
1403 | */ | |
1404 | bool bfd_id_insert(struct bfd_session *bs) | |
1405 | { | |
1406 | return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs); | |
1407 | } | |
1408 | ||
79b4a6fc | 1409 | bool bfd_key_insert(struct bfd_session *bs) |
e9e2c950 | 1410 | { |
79b4a6fc | 1411 | return (hash_get(bfd_key_hash, bs, hash_alloc_intern) == bs); |
e9e2c950 RZ |
1412 | } |
1413 | ||
e9e2c950 RZ |
1414 | void bfd_initialize(void) |
1415 | { | |
1416 | bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp, | |
79b4a6fc RZ |
1417 | "BFD session discriminator hash"); |
1418 | bfd_key_hash = hash_create(bfd_key_hash_do, bfd_key_hash_cmp, | |
1419 | "BFD session hash"); | |
e9e2c950 RZ |
1420 | } |
1421 | ||
e3b78da8 | 1422 | static void _bfd_free(struct hash_bucket *hb, |
e9e2c950 RZ |
1423 | void *arg __attribute__((__unused__))) |
1424 | { | |
1425 | struct bfd_session *bs = hb->data; | |
1426 | ||
1427 | bfd_session_free(bs); | |
1428 | } | |
1429 | ||
e9e2c950 RZ |
1430 | void bfd_shutdown(void) |
1431 | { | |
1432 | /* | |
1433 | * Close and free all BFD sessions. | |
1434 | * | |
1435 | * _bfd_free() will call bfd_session_free() which will take care | |
1436 | * of removing the session from all hashes, so we just run an | |
1437 | * assert() here to make sure it really happened. | |
1438 | */ | |
1439 | bfd_id_iterate(_bfd_free, NULL); | |
79b4a6fc | 1440 | assert(bfd_key_hash->count == 0); |
e9e2c950 | 1441 | |
e9e2c950 RZ |
1442 | /* Now free the hashes themselves. */ |
1443 | hash_free(bfd_id_hash); | |
79b4a6fc | 1444 | hash_free(bfd_key_hash); |
e9e2c950 | 1445 | } |