]>
Commit | Line | Data |
---|---|---|
d3af6147 RZ |
1 | /* |
2 | * BFD PTM adapter code | |
3 | * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF") | |
4 | * | |
5 | * FRR is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2, or (at your option) any | |
8 | * later version. | |
9 | * | |
10 | * FRR is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with FRR; see the file COPYING. If not, write to the Free | |
17 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
18 | * 02111-1307, USA. | |
19 | */ | |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "lib/libfrr.h" | |
24 | #include "lib/queue.h" | |
25 | #include "lib/stream.h" | |
26 | #include "lib/zclient.h" | |
27 | ||
28 | #include "lib/bfd.h" | |
29 | ||
30 | #include "bfd.h" | |
31 | ||
32 | /* | |
33 | * Data structures | |
34 | */ | |
35 | struct ptm_client_notification { | |
36 | struct bfd_session *pcn_bs; | |
37 | struct ptm_client *pcn_pc; | |
38 | ||
39 | TAILQ_ENTRY(ptm_client_notification) pcn_entry; | |
40 | }; | |
41 | TAILQ_HEAD(pcnqueue, ptm_client_notification); | |
42 | ||
43 | struct ptm_client { | |
44 | uint32_t pc_pid; | |
45 | struct pcnqueue pc_pcnqueue; | |
46 | ||
47 | TAILQ_ENTRY(ptm_client) pc_entry; | |
48 | }; | |
49 | TAILQ_HEAD(pcqueue, ptm_client); | |
50 | ||
51 | static struct pcqueue pcqueue; | |
52 | static struct zclient *zclient; | |
53 | ||
54 | ||
55 | /* | |
56 | * Prototypes | |
57 | */ | |
79b4a6fc | 58 | static int _ptm_msg_address(struct stream *msg, int family, const void *addr); |
d3af6147 RZ |
59 | |
60 | static void _ptm_msg_read_address(struct stream *msg, struct sockaddr_any *sa); | |
45b000d0 | 61 | static int _ptm_msg_read(struct stream *msg, int command, vrf_id_t vrf_id, |
d3af6147 RZ |
62 | struct bfd_peer_cfg *bpc, struct ptm_client **pc); |
63 | ||
64 | static struct ptm_client *pc_lookup(uint32_t pid); | |
65 | static struct ptm_client *pc_new(uint32_t pid); | |
a375de5c | 66 | static void pc_free(struct ptm_client *pc); |
788378fe | 67 | static void pc_free_all(void); |
d3af6147 RZ |
68 | static struct ptm_client_notification *pcn_new(struct ptm_client *pc, |
69 | struct bfd_session *bs); | |
70 | static struct ptm_client_notification *pcn_lookup(struct ptm_client *pc, | |
71 | struct bfd_session *bs); | |
72 | static void pcn_free(struct ptm_client_notification *pcn); | |
73 | ||
74 | ||
45b000d0 PG |
75 | static void bfdd_dest_register(struct stream *msg, vrf_id_t vrf_id); |
76 | static void bfdd_dest_deregister(struct stream *msg, vrf_id_t vrf_id); | |
d3af6147 | 77 | static void bfdd_client_register(struct stream *msg); |
a375de5c | 78 | static void bfdd_client_deregister(struct stream *msg); |
d3af6147 RZ |
79 | |
80 | /* | |
81 | * Functions | |
82 | */ | |
83 | #ifdef BFD_DEBUG | |
84 | static void debug_printbpc(const char *func, unsigned int line, | |
85 | struct bfd_peer_cfg *bpc); | |
86 | ||
87 | static void debug_printbpc(const char *func, unsigned int line, | |
88 | struct bfd_peer_cfg *bpc) | |
89 | { | |
90 | char addr[3][128]; | |
91 | char timers[3][128]; | |
9beff0bd | 92 | char cbit_str[10]; |
d3af6147 RZ |
93 | |
94 | addr[0][0] = addr[1][0] = addr[2][0] = timers[0][0] = timers[1][0] = | |
95 | timers[2][0] = 0; | |
96 | ||
97 | snprintf(addr[0], sizeof(addr[0]), "peer:%s", satostr(&bpc->bpc_peer)); | |
98 | if (bpc->bpc_local.sa_sin.sin_family) | |
99 | snprintf(addr[1], sizeof(addr[1]), " local:%s", | |
100 | satostr(&bpc->bpc_local)); | |
101 | ||
102 | if (bpc->bpc_has_localif) | |
103 | snprintf(addr[2], sizeof(addr[2]), " ifname:%s", | |
104 | bpc->bpc_localif); | |
105 | ||
106 | if (bpc->bpc_has_vrfname) | |
107 | snprintf(addr[2], sizeof(addr[2]), " vrf:%s", bpc->bpc_vrfname); | |
108 | ||
109 | if (bpc->bpc_has_recvinterval) | |
110 | snprintf(timers[0], sizeof(timers[0]), " rx:%lu", | |
111 | bpc->bpc_recvinterval); | |
112 | ||
113 | if (bpc->bpc_has_txinterval) | |
114 | snprintf(timers[1], sizeof(timers[1]), " tx:%lu", | |
115 | bpc->bpc_recvinterval); | |
116 | ||
117 | if (bpc->bpc_has_detectmultiplier) | |
118 | snprintf(timers[2], sizeof(timers[2]), " detect-multiplier:%d", | |
119 | bpc->bpc_detectmultiplier); | |
120 | ||
9beff0bd PG |
121 | sprintf(cbit_str, "CB %x", bpc->bpc_cbit); |
122 | ||
123 | log_debug("%s:%d: %s %s%s%s%s%s%s %s", func, line, | |
d3af6147 | 124 | bpc->bpc_mhop ? "multi-hop" : "single-hop", addr[0], addr[1], |
9beff0bd | 125 | addr[2], timers[0], timers[1], timers[2], cbit_str); |
d3af6147 RZ |
126 | } |
127 | ||
128 | #define DEBUG_PRINTBPC(bpc) debug_printbpc(__FILE__, __LINE__, (bpc)) | |
129 | #else | |
130 | #define DEBUG_PRINTBPC(bpc) | |
131 | #endif /* BFD_DEBUG */ | |
132 | ||
79b4a6fc | 133 | static int _ptm_msg_address(struct stream *msg, int family, const void *addr) |
d3af6147 | 134 | { |
79b4a6fc RZ |
135 | stream_putc(msg, family); |
136 | ||
137 | switch (family) { | |
d3af6147 | 138 | case AF_INET: |
79b4a6fc | 139 | stream_put(msg, addr, sizeof(struct in_addr)); |
d3af6147 RZ |
140 | stream_putc(msg, 32); |
141 | break; | |
142 | ||
143 | case AF_INET6: | |
79b4a6fc | 144 | stream_put(msg, addr, sizeof(struct in6_addr)); |
d3af6147 RZ |
145 | stream_putc(msg, 128); |
146 | break; | |
147 | ||
148 | default: | |
79b4a6fc RZ |
149 | assert(0); |
150 | break; | |
d3af6147 RZ |
151 | } |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | int ptm_bfd_notify(struct bfd_session *bs) | |
157 | { | |
158 | struct stream *msg; | |
d3af6147 | 159 | |
0684c9b1 RZ |
160 | bs->stats.znotification++; |
161 | ||
d3af6147 RZ |
162 | /* |
163 | * Message format: | |
164 | * - header: command, vrf | |
165 | * - l: interface index | |
166 | * - c: family | |
167 | * - AF_INET: | |
168 | * - 4 bytes: ipv4 | |
169 | * - AF_INET6: | |
170 | * - 16 bytes: ipv6 | |
171 | * - c: prefix length | |
172 | * - l: bfd status | |
173 | * - c: family | |
174 | * - AF_INET: | |
175 | * - 4 bytes: ipv4 | |
176 | * - AF_INET6: | |
177 | * - 16 bytes: ipv6 | |
178 | * - c: prefix length | |
9beff0bd | 179 | * - c: cbit |
d3af6147 RZ |
180 | * |
181 | * Commands: ZEBRA_BFD_DEST_REPLAY | |
182 | * | |
183 | * q(64), l(32), w(16), c(8) | |
184 | */ | |
185 | msg = zclient->obuf; | |
186 | stream_reset(msg); | |
187 | ||
188 | /* TODO: VRF handling */ | |
45b000d0 PG |
189 | if (bs->vrf) |
190 | zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, bs->vrf->vrf_id); | |
191 | else | |
192 | zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, VRF_DEFAULT); | |
d3af6147 | 193 | |
971532e2 RZ |
194 | /* This header will be handled by `zebra_ptm.c`. */ |
195 | stream_putl(msg, ZEBRA_INTERFACE_BFD_DEST_UPDATE); | |
196 | ||
d3af6147 | 197 | /* NOTE: Interface is a shortcut to avoid comparing source address. */ |
80edb675 RZ |
198 | if (bs->ifp != NULL) |
199 | stream_putl(msg, bs->ifp->ifindex); | |
200 | else | |
201 | stream_putl(msg, IFINDEX_INTERNAL); | |
d3af6147 RZ |
202 | |
203 | /* BFD destination prefix information. */ | |
79b4a6fc | 204 | _ptm_msg_address(msg, bs->key.family, &bs->key.peer); |
d3af6147 RZ |
205 | |
206 | /* BFD status */ | |
207 | switch (bs->ses_state) { | |
208 | case PTM_BFD_UP: | |
209 | stream_putl(msg, BFD_STATUS_UP); | |
210 | break; | |
211 | ||
212 | case PTM_BFD_ADM_DOWN: | |
213 | case PTM_BFD_DOWN: | |
214 | case PTM_BFD_INIT: | |
215 | stream_putl(msg, BFD_STATUS_DOWN); | |
216 | break; | |
217 | ||
218 | default: | |
219 | stream_putl(msg, BFD_STATUS_UNKNOWN); | |
220 | break; | |
221 | } | |
222 | ||
223 | /* BFD source prefix information. */ | |
79b4a6fc | 224 | _ptm_msg_address(msg, bs->key.family, &bs->key.local); |
d3af6147 | 225 | |
9beff0bd PG |
226 | stream_putc(msg, bs->remote_cbit); |
227 | ||
d3af6147 RZ |
228 | /* Write packet size. */ |
229 | stream_putw_at(msg, 0, stream_get_endp(msg)); | |
230 | ||
231 | return zclient_send_message(zclient); | |
232 | } | |
233 | ||
234 | static void _ptm_msg_read_address(struct stream *msg, struct sockaddr_any *sa) | |
235 | { | |
236 | uint16_t family; | |
237 | ||
238 | STREAM_GETW(msg, family); | |
239 | ||
240 | switch (family) { | |
241 | case AF_INET: | |
242 | sa->sa_sin.sin_family = family; | |
243 | STREAM_GET(&sa->sa_sin.sin_addr, msg, | |
244 | sizeof(sa->sa_sin.sin_addr)); | |
245 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
246 | sa->sa_sin.sin_len = sizeof(sa->sa_sin); | |
247 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
248 | return; | |
249 | ||
250 | case AF_INET6: | |
251 | sa->sa_sin6.sin6_family = family; | |
252 | STREAM_GET(&sa->sa_sin6.sin6_addr, msg, | |
253 | sizeof(sa->sa_sin6.sin6_addr)); | |
254 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
255 | sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6); | |
256 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
257 | return; | |
258 | ||
259 | default: | |
ae9f45a3 | 260 | log_warning("ptm-read-address: invalid family: %d", family); |
d3af6147 RZ |
261 | break; |
262 | } | |
263 | ||
264 | stream_failure: | |
265 | memset(sa, 0, sizeof(*sa)); | |
266 | } | |
267 | ||
45b000d0 | 268 | static int _ptm_msg_read(struct stream *msg, int command, vrf_id_t vrf_id, |
d3af6147 RZ |
269 | struct bfd_peer_cfg *bpc, struct ptm_client **pc) |
270 | { | |
271 | uint32_t pid; | |
f0d2be33 | 272 | uint8_t ttl __attribute__((unused)); |
ae9eebca | 273 | size_t ifnamelen; |
d3af6147 RZ |
274 | |
275 | /* | |
276 | * Register/Deregister/Update Message format: | |
277 | * - header: Command, VRF | |
278 | * - l: pid | |
279 | * - w: family | |
280 | * - AF_INET: | |
281 | * - l: destination ipv4 | |
282 | * - AF_INET6: | |
283 | * - 16 bytes: destination IPv6 | |
284 | * - command != ZEBRA_BFD_DEST_DEREGISTER | |
285 | * - l: min_rx | |
286 | * - l: min_tx | |
287 | * - c: detect multiplier | |
288 | * - c: is_multihop? | |
289 | * - multihop: | |
290 | * - w: family | |
291 | * - AF_INET: | |
292 | * - l: destination ipv4 | |
293 | * - AF_INET6: | |
294 | * - 16 bytes: destination IPv6 | |
295 | * - c: ttl | |
296 | * - no multihop | |
297 | * - AF_INET6: | |
298 | * - w: family | |
299 | * - 16 bytes: ipv6 address | |
300 | * - c: ifname length | |
301 | * - X bytes: interface name | |
9beff0bd | 302 | * - c: bfd_cbit |
d3af6147 RZ |
303 | * |
304 | * q(64), l(32), w(16), c(8) | |
305 | */ | |
306 | ||
307 | /* Initialize parameters return values. */ | |
308 | memset(bpc, 0, sizeof(*bpc)); | |
309 | *pc = NULL; | |
310 | ||
311 | /* Find or allocate process context data. */ | |
312 | STREAM_GETL(msg, pid); | |
313 | ||
314 | *pc = pc_new(pid); | |
315 | if (*pc == NULL) { | |
ae9f45a3 | 316 | log_debug("ptm-read: failed to allocate memory"); |
d3af6147 RZ |
317 | return -1; |
318 | } | |
319 | ||
320 | /* Register/update peer information. */ | |
321 | _ptm_msg_read_address(msg, &bpc->bpc_peer); | |
322 | ||
323 | /* Determine IP type from peer destination. */ | |
324 | bpc->bpc_ipv4 = (bpc->bpc_peer.sa_sin.sin_family == AF_INET); | |
325 | ||
326 | /* Get peer configuration. */ | |
327 | if (command != ZEBRA_BFD_DEST_DEREGISTER) { | |
328 | STREAM_GETL(msg, bpc->bpc_recvinterval); | |
329 | bpc->bpc_has_recvinterval = | |
330 | (bpc->bpc_recvinterval != BPC_DEF_RECEIVEINTERVAL); | |
331 | ||
332 | STREAM_GETL(msg, bpc->bpc_txinterval); | |
333 | bpc->bpc_has_txinterval = | |
334 | (bpc->bpc_txinterval != BPC_DEF_TRANSMITINTERVAL); | |
335 | ||
336 | STREAM_GETC(msg, bpc->bpc_detectmultiplier); | |
337 | bpc->bpc_has_detectmultiplier = | |
338 | (bpc->bpc_detectmultiplier != BPC_DEF_DETECTMULTIPLIER); | |
339 | } | |
340 | ||
341 | /* Read (single|multi)hop and its options. */ | |
342 | STREAM_GETC(msg, bpc->bpc_mhop); | |
343 | if (bpc->bpc_mhop) { | |
344 | /* Read multihop source address and TTL. */ | |
345 | _ptm_msg_read_address(msg, &bpc->bpc_local); | |
346 | STREAM_GETC(msg, ttl); | |
d3af6147 RZ |
347 | } else { |
348 | /* If target is IPv6, then we must obtain local address. */ | |
349 | if (bpc->bpc_ipv4 == false) | |
350 | _ptm_msg_read_address(msg, &bpc->bpc_local); | |
351 | ||
352 | /* | |
353 | * Read interface name and make sure it fits our data | |
354 | * structure, otherwise fail. | |
355 | */ | |
356 | STREAM_GETC(msg, ifnamelen); | |
ae9f45a3 RZ |
357 | if (ifnamelen >= sizeof(bpc->bpc_localif)) { |
358 | log_error("ptm-read: interface name is too big"); | |
d3af6147 RZ |
359 | return -1; |
360 | } | |
361 | ||
362 | bpc->bpc_has_localif = ifnamelen > 0; | |
363 | if (bpc->bpc_has_localif) { | |
364 | STREAM_GET(bpc->bpc_localif, msg, ifnamelen); | |
365 | bpc->bpc_localif[ifnamelen] = 0; | |
366 | } | |
367 | } | |
45b000d0 PG |
368 | if (vrf_id != VRF_DEFAULT) { |
369 | struct vrf *vrf; | |
370 | ||
371 | vrf = vrf_lookup_by_id(vrf_id); | |
372 | if (vrf) { | |
373 | bpc->bpc_has_vrfname = true; | |
374 | strlcpy(bpc->bpc_vrfname, vrf->name, sizeof(bpc->bpc_vrfname)); | |
375 | } else { | |
376 | log_error("ptm-read: vrf id %u could not be identified", vrf_id); | |
377 | return -1; | |
378 | } | |
3a20889f PG |
379 | } else { |
380 | bpc->bpc_has_vrfname = true; | |
381 | strlcpy(bpc->bpc_vrfname, VRF_DEFAULT_NAME, sizeof(bpc->bpc_vrfname)); | |
45b000d0 | 382 | } |
d3af6147 | 383 | |
9beff0bd PG |
384 | STREAM_GETC(msg, bpc->bpc_cbit); |
385 | ||
d3af6147 RZ |
386 | /* Sanity check: peer and local address must match IP types. */ |
387 | if (bpc->bpc_local.sa_sin.sin_family != 0 | |
388 | && (bpc->bpc_local.sa_sin.sin_family | |
389 | != bpc->bpc_peer.sa_sin.sin_family)) { | |
ae9f45a3 | 390 | log_warning("ptm-read: peer family doesn't match local type"); |
d3af6147 RZ |
391 | return -1; |
392 | } | |
393 | ||
394 | return 0; | |
395 | ||
396 | stream_failure: | |
397 | return -1; | |
398 | } | |
399 | ||
45b000d0 | 400 | static void bfdd_dest_register(struct stream *msg, vrf_id_t vrf_id) |
d3af6147 RZ |
401 | { |
402 | struct ptm_client *pc; | |
403 | struct ptm_client_notification *pcn; | |
404 | struct bfd_session *bs; | |
405 | struct bfd_peer_cfg bpc; | |
406 | ||
407 | /* Read the client context and peer data. */ | |
45b000d0 | 408 | if (_ptm_msg_read(msg, ZEBRA_BFD_DEST_REGISTER, vrf_id, &bpc, &pc) == -1) |
d3af6147 RZ |
409 | return; |
410 | ||
411 | DEBUG_PRINTBPC(&bpc); | |
412 | ||
413 | /* Find or start new BFD session. */ | |
414 | bs = bs_peer_find(&bpc); | |
415 | if (bs == NULL) { | |
416 | bs = ptm_bfd_sess_new(&bpc); | |
417 | if (bs == NULL) { | |
ae9f45a3 | 418 | log_debug("ptm-add-dest: failed to create BFD session"); |
d3af6147 RZ |
419 | return; |
420 | } | |
421 | } else { | |
422 | /* Don't try to change echo/shutdown state. */ | |
423 | bpc.bpc_echo = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
424 | bpc.bpc_shutdown = | |
425 | BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
426 | } | |
427 | ||
428 | /* Create client peer notification register. */ | |
429 | pcn = pcn_new(pc, bs); | |
430 | if (pcn == NULL) { | |
ae9f45a3 | 431 | log_error("ptm-add-dest: failed to registrate notifications"); |
d3af6147 RZ |
432 | return; |
433 | } | |
434 | ||
435 | ptm_bfd_notify(bs); | |
436 | } | |
437 | ||
45b000d0 | 438 | static void bfdd_dest_deregister(struct stream *msg, vrf_id_t vrf_id) |
d3af6147 RZ |
439 | { |
440 | struct ptm_client *pc; | |
441 | struct ptm_client_notification *pcn; | |
442 | struct bfd_session *bs; | |
443 | struct bfd_peer_cfg bpc; | |
444 | ||
445 | /* Read the client context and peer data. */ | |
45b000d0 | 446 | if (_ptm_msg_read(msg, ZEBRA_BFD_DEST_DEREGISTER, vrf_id, &bpc, &pc) == -1) |
d3af6147 RZ |
447 | return; |
448 | ||
449 | DEBUG_PRINTBPC(&bpc); | |
450 | ||
451 | /* Find or start new BFD session. */ | |
452 | bs = bs_peer_find(&bpc); | |
453 | if (bs == NULL) { | |
ae9f45a3 | 454 | log_debug("ptm-del-dest: failed to find BFD session"); |
d3af6147 RZ |
455 | return; |
456 | } | |
457 | ||
458 | /* Unregister client peer notification. */ | |
459 | pcn = pcn_lookup(pc, bs); | |
460 | pcn_free(pcn); | |
4c741971 PG |
461 | if (bs->refcount || |
462 | BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG)) | |
463 | return; | |
bc50bcc8 | 464 | ptm_bfd_sess_del(&bpc); |
d3af6147 RZ |
465 | } |
466 | ||
467 | /* | |
468 | * header: command, VRF | |
469 | * l: pid | |
470 | */ | |
471 | static void bfdd_client_register(struct stream *msg) | |
472 | { | |
473 | struct ptm_client *pc; | |
474 | uint32_t pid; | |
475 | ||
476 | /* Find or allocate process context data. */ | |
477 | STREAM_GETL(msg, pid); | |
478 | ||
479 | pc = pc_new(pid); | |
480 | if (pc == NULL) { | |
ae9f45a3 | 481 | log_error("ptm-add-client: failed to register client: %u", pid); |
d3af6147 RZ |
482 | return; |
483 | } | |
484 | ||
485 | return; | |
486 | ||
487 | stream_failure: | |
ae9f45a3 | 488 | log_error("ptm-add-client: failed to register client"); |
d3af6147 RZ |
489 | } |
490 | ||
a375de5c RZ |
491 | /* |
492 | * header: command, VRF | |
493 | * l: pid | |
494 | */ | |
495 | static void bfdd_client_deregister(struct stream *msg) | |
496 | { | |
497 | struct ptm_client *pc; | |
498 | uint32_t pid; | |
499 | ||
500 | /* Find or allocate process context data. */ | |
501 | STREAM_GETL(msg, pid); | |
502 | ||
503 | pc = pc_lookup(pid); | |
504 | if (pc == NULL) { | |
ae9f45a3 | 505 | log_debug("ptm-del-client: failed to find client: %u", pid); |
a375de5c RZ |
506 | return; |
507 | } | |
508 | ||
509 | pc_free(pc); | |
510 | ||
511 | return; | |
512 | ||
513 | stream_failure: | |
ae9f45a3 | 514 | log_error("ptm-del-client: failed to deregister client"); |
a375de5c RZ |
515 | } |
516 | ||
121f9dee | 517 | static int bfdd_replay(ZAPI_CALLBACK_ARGS) |
d3af6147 | 518 | { |
121f9dee | 519 | struct stream *msg = zclient->ibuf; |
d3af6147 RZ |
520 | uint32_t rcmd; |
521 | ||
522 | STREAM_GETL(msg, rcmd); | |
523 | ||
524 | switch (rcmd) { | |
525 | case ZEBRA_BFD_DEST_REGISTER: | |
526 | case ZEBRA_BFD_DEST_UPDATE: | |
45b000d0 | 527 | bfdd_dest_register(msg, vrf_id); |
d3af6147 RZ |
528 | break; |
529 | case ZEBRA_BFD_DEST_DEREGISTER: | |
45b000d0 | 530 | bfdd_dest_deregister(msg, vrf_id); |
d3af6147 RZ |
531 | break; |
532 | case ZEBRA_BFD_CLIENT_REGISTER: | |
533 | bfdd_client_register(msg); | |
534 | break; | |
a375de5c RZ |
535 | case ZEBRA_BFD_CLIENT_DEREGISTER: |
536 | bfdd_client_deregister(msg); | |
537 | break; | |
d3af6147 RZ |
538 | |
539 | default: | |
ae9f45a3 | 540 | log_debug("ptm-replay: invalid message type %u", rcmd); |
d3af6147 RZ |
541 | return -1; |
542 | } | |
543 | ||
544 | return 0; | |
545 | ||
546 | stream_failure: | |
ae9f45a3 | 547 | log_error("ptm-replay: failed to find command"); |
d3af6147 RZ |
548 | return -1; |
549 | } | |
550 | ||
971532e2 RZ |
551 | static void bfdd_zebra_connected(struct zclient *zc) |
552 | { | |
553 | struct stream *msg = zc->obuf; | |
554 | ||
788378fe RZ |
555 | /* Clean-up and free ptm clients data memory. */ |
556 | pc_free_all(); | |
557 | ||
971532e2 RZ |
558 | /* |
559 | * The replay is an empty message just to trigger client daemons | |
560 | * configuration replay. | |
561 | */ | |
562 | stream_reset(msg); | |
563 | zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, VRF_DEFAULT); | |
564 | stream_putl(msg, ZEBRA_BFD_DEST_REPLAY); | |
565 | stream_putw_at(msg, 0, stream_get_endp(msg)); | |
566 | ||
80edb675 RZ |
567 | /* Ask for interfaces information. */ |
568 | zclient_create_header(msg, ZEBRA_INTERFACE_ADD, VRF_DEFAULT); | |
569 | ||
570 | /* Send requests. */ | |
971532e2 RZ |
571 | zclient_send_message(zclient); |
572 | } | |
573 | ||
d245e522 RZ |
574 | static void bfdd_sessions_enable_interface(struct interface *ifp) |
575 | { | |
576 | struct bfd_session_observer *bso; | |
577 | struct bfd_session *bs; | |
a36898e7 | 578 | struct vrf *vrf; |
d245e522 RZ |
579 | |
580 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
b4335515 | 581 | bs = bso->bso_bs; |
d245e522 RZ |
582 | if (bso->bso_isinterface == false) |
583 | continue; | |
d245e522 | 584 | /* Interface name mismatch. */ |
79b4a6fc | 585 | if (strcmp(ifp->name, bs->key.ifname)) |
d245e522 | 586 | continue; |
a36898e7 DS |
587 | vrf = vrf_lookup_by_id(ifp->vrf_id); |
588 | if (!vrf) | |
589 | continue; | |
b4335515 PG |
590 | if (bs->key.vrfname[0] && |
591 | strcmp(vrf->name, bs->key.vrfname)) | |
592 | continue; | |
d245e522 RZ |
593 | /* Skip enabled sessions. */ |
594 | if (bs->sock != -1) | |
595 | continue; | |
596 | ||
597 | /* Try to enable it. */ | |
598 | bfd_session_enable(bs); | |
599 | } | |
600 | } | |
601 | ||
602 | static void bfdd_sessions_disable_interface(struct interface *ifp) | |
603 | { | |
604 | struct bfd_session_observer *bso; | |
605 | struct bfd_session *bs; | |
606 | ||
607 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
608 | if (bso->bso_isinterface == false) | |
609 | continue; | |
610 | ||
611 | /* Interface name mismatch. */ | |
612 | bs = bso->bso_bs; | |
79b4a6fc | 613 | if (strcmp(ifp->name, bs->key.ifname)) |
d245e522 RZ |
614 | continue; |
615 | /* Skip disabled sessions. */ | |
616 | if (bs->sock == -1) | |
617 | continue; | |
618 | ||
619 | /* Try to enable it. */ | |
620 | bfd_session_disable(bs); | |
621 | ||
d245e522 RZ |
622 | } |
623 | } | |
624 | ||
d24af713 PG |
625 | void bfdd_sessions_enable_vrf(struct vrf *vrf) |
626 | { | |
627 | struct bfd_session_observer *bso; | |
628 | struct bfd_session *bs; | |
629 | ||
630 | /* it may affect configs without interfaces */ | |
631 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
632 | bs = bso->bso_bs; | |
f06e248c PG |
633 | /* update name */ |
634 | if (bs->vrf && bs->vrf == vrf) { | |
635 | if (!strmatch(bs->key.vrfname, vrf->name)) | |
636 | bfd_session_update_vrf_name(bs, vrf); | |
637 | } | |
d24af713 PG |
638 | if (bs->vrf) |
639 | continue; | |
640 | if (bs->key.vrfname[0] && | |
641 | strcmp(vrf->name, bs->key.vrfname)) | |
642 | continue; | |
643 | /* need to update the vrf information on | |
644 | * bs so that callbacks are handled | |
645 | */ | |
646 | bs->vrf = vrf; | |
647 | /* Skip enabled sessions. */ | |
648 | if (bs->sock != -1) | |
649 | continue; | |
650 | /* Try to enable it. */ | |
651 | bfd_session_enable(bs); | |
652 | } | |
653 | } | |
654 | ||
655 | void bfdd_sessions_disable_vrf(struct vrf *vrf) | |
656 | { | |
657 | struct bfd_session_observer *bso; | |
658 | struct bfd_session *bs; | |
659 | ||
660 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
661 | if (bso->bso_isinterface) | |
662 | continue; | |
663 | bs = bso->bso_bs; | |
664 | if (bs->key.vrfname[0] && | |
665 | strcmp(vrf->name, bs->key.vrfname)) | |
666 | continue; | |
667 | /* Skip disabled sessions. */ | |
668 | if (bs->sock == -1) | |
669 | continue; | |
670 | ||
671 | /* Try to enable it. */ | |
672 | bfd_session_disable(bs); | |
673 | } | |
674 | } | |
675 | ||
121f9dee | 676 | static int bfdd_interface_update(ZAPI_CALLBACK_ARGS) |
80edb675 | 677 | { |
d245e522 RZ |
678 | struct interface *ifp; |
679 | ||
80edb675 RZ |
680 | /* |
681 | * `zebra_interface_add_read` will handle the interface creation | |
682 | * on `lib/if.c`. We'll use that data structure instead of | |
683 | * rolling our own. | |
684 | */ | |
685 | if (cmd == ZEBRA_INTERFACE_ADD) { | |
121f9dee | 686 | ifp = zebra_interface_add_read(zclient->ibuf, vrf_id); |
d245e522 RZ |
687 | if (ifp == NULL) |
688 | return 0; | |
689 | ||
690 | bfdd_sessions_enable_interface(ifp); | |
80edb675 RZ |
691 | return 0; |
692 | } | |
693 | ||
694 | /* Update interface information. */ | |
121f9dee | 695 | ifp = zebra_interface_state_read(zclient->ibuf, vrf_id); |
d245e522 RZ |
696 | if (ifp == NULL) |
697 | return 0; | |
80edb675 | 698 | |
d245e522 | 699 | bfdd_sessions_disable_interface(ifp); |
80edb675 | 700 | |
9d6c33ea DS |
701 | if_set_index(ifp, IFINDEX_INTERNAL); |
702 | ||
80edb675 RZ |
703 | return 0; |
704 | } | |
705 | ||
121f9dee | 706 | static int bfdd_interface_vrf_update(ZAPI_CALLBACK_ARGS) |
b333abc2 RZ |
707 | { |
708 | struct interface *ifp; | |
709 | vrf_id_t nvrfid; | |
710 | ||
121f9dee | 711 | ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id, &nvrfid); |
b333abc2 RZ |
712 | if (ifp == NULL) |
713 | return 0; | |
a36898e7 DS |
714 | |
715 | if_update_to_new_vrf(ifp, nvrfid); | |
b333abc2 RZ |
716 | |
717 | return 0; | |
718 | } | |
719 | ||
261e0ba9 RZ |
720 | static void bfdd_sessions_enable_address(struct connected *ifc) |
721 | { | |
722 | struct bfd_session_observer *bso; | |
723 | struct bfd_session *bs; | |
724 | struct prefix prefix; | |
725 | ||
726 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
727 | if (bso->bso_isaddress == false) | |
728 | continue; | |
729 | ||
730 | /* Skip enabled sessions. */ | |
731 | bs = bso->bso_bs; | |
732 | if (bs->sock != -1) | |
733 | continue; | |
734 | ||
735 | /* Check address. */ | |
736 | prefix = bso->bso_addr; | |
737 | prefix.prefixlen = ifc->address->prefixlen; | |
738 | if (prefix_cmp(&prefix, ifc->address)) | |
739 | continue; | |
740 | ||
741 | /* Try to enable it. */ | |
742 | bfd_session_enable(bs); | |
743 | } | |
744 | } | |
745 | ||
121f9dee | 746 | static int bfdd_interface_address_update(ZAPI_CALLBACK_ARGS) |
261e0ba9 RZ |
747 | { |
748 | struct connected *ifc; | |
749 | ||
121f9dee | 750 | ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
261e0ba9 RZ |
751 | if (ifc == NULL) |
752 | return 0; | |
753 | ||
754 | bfdd_sessions_enable_address(ifc); | |
755 | ||
756 | return 0; | |
757 | } | |
758 | ||
d3af6147 RZ |
759 | void bfdd_zclient_init(struct zebra_privs_t *bfdd_priv) |
760 | { | |
26f63a1e | 761 | zclient = zclient_new(master, &zclient_options_default); |
d3af6147 RZ |
762 | assert(zclient != NULL); |
763 | zclient_init(zclient, ZEBRA_ROUTE_BFD, 0, bfdd_priv); | |
764 | ||
765 | /* | |
766 | * We'll receive all messages through replay, however it will | |
767 | * contain a special field with the real command inside so we | |
768 | * avoid having to create too many handlers. | |
769 | */ | |
770 | zclient->bfd_dest_replay = bfdd_replay; | |
971532e2 RZ |
771 | |
772 | /* Send replay request on zebra connect. */ | |
773 | zclient->zebra_connected = bfdd_zebra_connected; | |
80edb675 RZ |
774 | |
775 | /* Learn interfaces from zebra instead of the OS. */ | |
776 | zclient->interface_add = bfdd_interface_update; | |
777 | zclient->interface_delete = bfdd_interface_update; | |
b333abc2 RZ |
778 | |
779 | /* Learn about interface VRF. */ | |
780 | zclient->interface_vrf_update = bfdd_interface_vrf_update; | |
261e0ba9 RZ |
781 | |
782 | /* Learn about new addresses being registered. */ | |
783 | zclient->interface_address_add = bfdd_interface_address_update; | |
784 | zclient->interface_address_delete = bfdd_interface_address_update; | |
d3af6147 RZ |
785 | } |
786 | ||
54aadda1 PG |
787 | void bfdd_zclient_register(vrf_id_t vrf_id) |
788 | { | |
789 | if (!zclient || zclient->sock < 0) | |
790 | return; | |
791 | zclient_send_reg_requests(zclient, vrf_id); | |
792 | } | |
793 | ||
794 | void bfdd_zclient_unregister(vrf_id_t vrf_id) | |
795 | { | |
796 | if (!zclient || zclient->sock < 0) | |
797 | return; | |
798 | zclient_send_dereg_requests(zclient, vrf_id); | |
799 | } | |
800 | ||
d3af6147 RZ |
801 | void bfdd_zclient_stop(void) |
802 | { | |
803 | zclient_stop(zclient); | |
788378fe RZ |
804 | |
805 | /* Clean-up and free ptm clients data memory. */ | |
806 | pc_free_all(); | |
d3af6147 RZ |
807 | } |
808 | ||
809 | ||
810 | /* | |
811 | * Client handling. | |
812 | */ | |
813 | static struct ptm_client *pc_lookup(uint32_t pid) | |
814 | { | |
815 | struct ptm_client *pc; | |
816 | ||
817 | TAILQ_FOREACH (pc, &pcqueue, pc_entry) { | |
818 | if (pc->pc_pid != pid) | |
819 | continue; | |
820 | ||
821 | break; | |
822 | } | |
823 | ||
824 | return pc; | |
825 | } | |
826 | ||
827 | static struct ptm_client *pc_new(uint32_t pid) | |
828 | { | |
829 | struct ptm_client *pc; | |
830 | ||
831 | /* Look up first, if not found create the client. */ | |
832 | pc = pc_lookup(pid); | |
833 | if (pc != NULL) | |
834 | return pc; | |
835 | ||
836 | /* Allocate the client data and save it. */ | |
837 | pc = XCALLOC(MTYPE_BFDD_CONTROL, sizeof(*pc)); | |
d3af6147 RZ |
838 | |
839 | pc->pc_pid = pid; | |
840 | TAILQ_INSERT_HEAD(&pcqueue, pc, pc_entry); | |
841 | return pc; | |
842 | } | |
843 | ||
a375de5c RZ |
844 | static void pc_free(struct ptm_client *pc) |
845 | { | |
846 | struct ptm_client_notification *pcn; | |
847 | ||
848 | if (pc == NULL) | |
849 | return; | |
850 | ||
851 | TAILQ_REMOVE(&pcqueue, pc, pc_entry); | |
852 | ||
853 | while (!TAILQ_EMPTY(&pc->pc_pcnqueue)) { | |
854 | pcn = TAILQ_FIRST(&pc->pc_pcnqueue); | |
855 | pcn_free(pcn); | |
856 | } | |
857 | ||
858 | XFREE(MTYPE_BFDD_CONTROL, pc); | |
859 | } | |
860 | ||
788378fe RZ |
861 | static void pc_free_all(void) |
862 | { | |
863 | struct ptm_client *pc; | |
864 | ||
865 | while (!TAILQ_EMPTY(&pcqueue)) { | |
866 | pc = TAILQ_FIRST(&pcqueue); | |
867 | pc_free(pc); | |
868 | } | |
869 | } | |
870 | ||
d3af6147 RZ |
871 | static struct ptm_client_notification *pcn_new(struct ptm_client *pc, |
872 | struct bfd_session *bs) | |
873 | { | |
874 | struct ptm_client_notification *pcn; | |
875 | ||
876 | /* Try to find an existing pcn fist. */ | |
877 | pcn = pcn_lookup(pc, bs); | |
878 | if (pcn != NULL) | |
879 | return pcn; | |
880 | ||
881 | /* Save the client notification data. */ | |
882 | pcn = XCALLOC(MTYPE_BFDD_NOTIFICATION, sizeof(*pcn)); | |
d3af6147 RZ |
883 | |
884 | TAILQ_INSERT_HEAD(&pc->pc_pcnqueue, pcn, pcn_entry); | |
885 | pcn->pcn_pc = pc; | |
886 | pcn->pcn_bs = bs; | |
887 | bs->refcount++; | |
888 | ||
889 | return pcn; | |
890 | } | |
891 | ||
892 | static struct ptm_client_notification *pcn_lookup(struct ptm_client *pc, | |
893 | struct bfd_session *bs) | |
894 | { | |
895 | struct ptm_client_notification *pcn; | |
896 | ||
897 | TAILQ_FOREACH (pcn, &pc->pc_pcnqueue, pcn_entry) { | |
898 | if (pcn->pcn_bs != bs) | |
899 | continue; | |
900 | ||
901 | break; | |
902 | } | |
903 | ||
904 | return pcn; | |
905 | } | |
906 | ||
907 | static void pcn_free(struct ptm_client_notification *pcn) | |
908 | { | |
909 | struct ptm_client *pc; | |
910 | struct bfd_session *bs; | |
911 | ||
912 | if (pcn == NULL) | |
913 | return; | |
914 | ||
915 | /* Handle session de-registration. */ | |
916 | bs = pcn->pcn_bs; | |
917 | pcn->pcn_bs = NULL; | |
918 | bs->refcount--; | |
919 | ||
920 | /* Handle ptm_client deregistration. */ | |
921 | pc = pcn->pcn_pc; | |
922 | pcn->pcn_pc = NULL; | |
923 | TAILQ_REMOVE(&pc->pc_pcnqueue, pcn, pcn_entry); | |
924 | ||
925 | XFREE(MTYPE_BFDD_NOTIFICATION, pcn); | |
926 | } |