]>
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); | |
61 | static int _ptm_msg_read(struct stream *msg, int command, | |
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 | ||
75 | static void bfdd_dest_register(struct stream *msg); | |
76 | static void bfdd_dest_deregister(struct stream *msg); | |
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]; | |
92 | ||
93 | addr[0][0] = addr[1][0] = addr[2][0] = timers[0][0] = timers[1][0] = | |
94 | timers[2][0] = 0; | |
95 | ||
96 | snprintf(addr[0], sizeof(addr[0]), "peer:%s", satostr(&bpc->bpc_peer)); | |
97 | if (bpc->bpc_local.sa_sin.sin_family) | |
98 | snprintf(addr[1], sizeof(addr[1]), " local:%s", | |
99 | satostr(&bpc->bpc_local)); | |
100 | ||
101 | if (bpc->bpc_has_localif) | |
102 | snprintf(addr[2], sizeof(addr[2]), " ifname:%s", | |
103 | bpc->bpc_localif); | |
104 | ||
105 | if (bpc->bpc_has_vrfname) | |
106 | snprintf(addr[2], sizeof(addr[2]), " vrf:%s", bpc->bpc_vrfname); | |
107 | ||
108 | if (bpc->bpc_has_recvinterval) | |
109 | snprintf(timers[0], sizeof(timers[0]), " rx:%lu", | |
110 | bpc->bpc_recvinterval); | |
111 | ||
112 | if (bpc->bpc_has_txinterval) | |
113 | snprintf(timers[1], sizeof(timers[1]), " tx:%lu", | |
114 | bpc->bpc_recvinterval); | |
115 | ||
116 | if (bpc->bpc_has_detectmultiplier) | |
117 | snprintf(timers[2], sizeof(timers[2]), " detect-multiplier:%d", | |
118 | bpc->bpc_detectmultiplier); | |
119 | ||
120 | log_debug("%s:%d: %s %s%s%s%s%s%s", func, line, | |
121 | bpc->bpc_mhop ? "multi-hop" : "single-hop", addr[0], addr[1], | |
122 | addr[2], timers[0], timers[1], timers[2]); | |
123 | } | |
124 | ||
125 | #define DEBUG_PRINTBPC(bpc) debug_printbpc(__FILE__, __LINE__, (bpc)) | |
126 | #else | |
127 | #define DEBUG_PRINTBPC(bpc) | |
128 | #endif /* BFD_DEBUG */ | |
129 | ||
79b4a6fc | 130 | static int _ptm_msg_address(struct stream *msg, int family, const void *addr) |
d3af6147 | 131 | { |
79b4a6fc RZ |
132 | stream_putc(msg, family); |
133 | ||
134 | switch (family) { | |
d3af6147 | 135 | case AF_INET: |
79b4a6fc | 136 | stream_put(msg, addr, sizeof(struct in_addr)); |
d3af6147 RZ |
137 | stream_putc(msg, 32); |
138 | break; | |
139 | ||
140 | case AF_INET6: | |
79b4a6fc | 141 | stream_put(msg, addr, sizeof(struct in6_addr)); |
d3af6147 RZ |
142 | stream_putc(msg, 128); |
143 | break; | |
144 | ||
145 | default: | |
79b4a6fc RZ |
146 | assert(0); |
147 | break; | |
d3af6147 RZ |
148 | } |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
153 | int ptm_bfd_notify(struct bfd_session *bs) | |
154 | { | |
155 | struct stream *msg; | |
d3af6147 | 156 | |
0684c9b1 RZ |
157 | bs->stats.znotification++; |
158 | ||
d3af6147 RZ |
159 | /* |
160 | * Message format: | |
161 | * - header: command, vrf | |
162 | * - l: interface index | |
163 | * - c: family | |
164 | * - AF_INET: | |
165 | * - 4 bytes: ipv4 | |
166 | * - AF_INET6: | |
167 | * - 16 bytes: ipv6 | |
168 | * - c: prefix length | |
169 | * - l: bfd status | |
170 | * - c: family | |
171 | * - AF_INET: | |
172 | * - 4 bytes: ipv4 | |
173 | * - AF_INET6: | |
174 | * - 16 bytes: ipv6 | |
175 | * - c: prefix length | |
176 | * | |
177 | * Commands: ZEBRA_BFD_DEST_REPLAY | |
178 | * | |
179 | * q(64), l(32), w(16), c(8) | |
180 | */ | |
181 | msg = zclient->obuf; | |
182 | stream_reset(msg); | |
183 | ||
184 | /* TODO: VRF handling */ | |
185 | zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, VRF_DEFAULT); | |
186 | ||
971532e2 RZ |
187 | /* This header will be handled by `zebra_ptm.c`. */ |
188 | stream_putl(msg, ZEBRA_INTERFACE_BFD_DEST_UPDATE); | |
189 | ||
d3af6147 | 190 | /* NOTE: Interface is a shortcut to avoid comparing source address. */ |
80edb675 RZ |
191 | if (bs->ifp != NULL) |
192 | stream_putl(msg, bs->ifp->ifindex); | |
193 | else | |
194 | stream_putl(msg, IFINDEX_INTERNAL); | |
d3af6147 RZ |
195 | |
196 | /* BFD destination prefix information. */ | |
79b4a6fc | 197 | _ptm_msg_address(msg, bs->key.family, &bs->key.peer); |
d3af6147 RZ |
198 | |
199 | /* BFD status */ | |
200 | switch (bs->ses_state) { | |
201 | case PTM_BFD_UP: | |
202 | stream_putl(msg, BFD_STATUS_UP); | |
203 | break; | |
204 | ||
205 | case PTM_BFD_ADM_DOWN: | |
206 | case PTM_BFD_DOWN: | |
207 | case PTM_BFD_INIT: | |
208 | stream_putl(msg, BFD_STATUS_DOWN); | |
209 | break; | |
210 | ||
211 | default: | |
212 | stream_putl(msg, BFD_STATUS_UNKNOWN); | |
213 | break; | |
214 | } | |
215 | ||
216 | /* BFD source prefix information. */ | |
79b4a6fc | 217 | _ptm_msg_address(msg, bs->key.family, &bs->key.local); |
d3af6147 RZ |
218 | |
219 | /* Write packet size. */ | |
220 | stream_putw_at(msg, 0, stream_get_endp(msg)); | |
221 | ||
222 | return zclient_send_message(zclient); | |
223 | } | |
224 | ||
225 | static void _ptm_msg_read_address(struct stream *msg, struct sockaddr_any *sa) | |
226 | { | |
227 | uint16_t family; | |
228 | ||
229 | STREAM_GETW(msg, family); | |
230 | ||
231 | switch (family) { | |
232 | case AF_INET: | |
233 | sa->sa_sin.sin_family = family; | |
234 | STREAM_GET(&sa->sa_sin.sin_addr, msg, | |
235 | sizeof(sa->sa_sin.sin_addr)); | |
236 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
237 | sa->sa_sin.sin_len = sizeof(sa->sa_sin); | |
238 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
239 | return; | |
240 | ||
241 | case AF_INET6: | |
242 | sa->sa_sin6.sin6_family = family; | |
243 | STREAM_GET(&sa->sa_sin6.sin6_addr, msg, | |
244 | sizeof(sa->sa_sin6.sin6_addr)); | |
245 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
246 | sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6); | |
247 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
248 | return; | |
249 | ||
250 | default: | |
ae9f45a3 | 251 | log_warning("ptm-read-address: invalid family: %d", family); |
d3af6147 RZ |
252 | break; |
253 | } | |
254 | ||
255 | stream_failure: | |
256 | memset(sa, 0, sizeof(*sa)); | |
257 | } | |
258 | ||
259 | static int _ptm_msg_read(struct stream *msg, int command, | |
260 | struct bfd_peer_cfg *bpc, struct ptm_client **pc) | |
261 | { | |
262 | uint32_t pid; | |
f0d2be33 | 263 | uint8_t ttl __attribute__((unused)); |
ae9eebca | 264 | size_t ifnamelen; |
d3af6147 RZ |
265 | |
266 | /* | |
267 | * Register/Deregister/Update Message format: | |
268 | * - header: Command, VRF | |
269 | * - l: pid | |
270 | * - w: family | |
271 | * - AF_INET: | |
272 | * - l: destination ipv4 | |
273 | * - AF_INET6: | |
274 | * - 16 bytes: destination IPv6 | |
275 | * - command != ZEBRA_BFD_DEST_DEREGISTER | |
276 | * - l: min_rx | |
277 | * - l: min_tx | |
278 | * - c: detect multiplier | |
279 | * - c: is_multihop? | |
280 | * - multihop: | |
281 | * - w: family | |
282 | * - AF_INET: | |
283 | * - l: destination ipv4 | |
284 | * - AF_INET6: | |
285 | * - 16 bytes: destination IPv6 | |
286 | * - c: ttl | |
287 | * - no multihop | |
288 | * - AF_INET6: | |
289 | * - w: family | |
290 | * - 16 bytes: ipv6 address | |
291 | * - c: ifname length | |
292 | * - X bytes: interface name | |
293 | * | |
294 | * q(64), l(32), w(16), c(8) | |
295 | */ | |
296 | ||
297 | /* Initialize parameters return values. */ | |
298 | memset(bpc, 0, sizeof(*bpc)); | |
299 | *pc = NULL; | |
300 | ||
301 | /* Find or allocate process context data. */ | |
302 | STREAM_GETL(msg, pid); | |
303 | ||
304 | *pc = pc_new(pid); | |
305 | if (*pc == NULL) { | |
ae9f45a3 | 306 | log_debug("ptm-read: failed to allocate memory"); |
d3af6147 RZ |
307 | return -1; |
308 | } | |
309 | ||
310 | /* Register/update peer information. */ | |
311 | _ptm_msg_read_address(msg, &bpc->bpc_peer); | |
312 | ||
313 | /* Determine IP type from peer destination. */ | |
314 | bpc->bpc_ipv4 = (bpc->bpc_peer.sa_sin.sin_family == AF_INET); | |
315 | ||
316 | /* Get peer configuration. */ | |
317 | if (command != ZEBRA_BFD_DEST_DEREGISTER) { | |
318 | STREAM_GETL(msg, bpc->bpc_recvinterval); | |
319 | bpc->bpc_has_recvinterval = | |
320 | (bpc->bpc_recvinterval != BPC_DEF_RECEIVEINTERVAL); | |
321 | ||
322 | STREAM_GETL(msg, bpc->bpc_txinterval); | |
323 | bpc->bpc_has_txinterval = | |
324 | (bpc->bpc_txinterval != BPC_DEF_TRANSMITINTERVAL); | |
325 | ||
326 | STREAM_GETC(msg, bpc->bpc_detectmultiplier); | |
327 | bpc->bpc_has_detectmultiplier = | |
328 | (bpc->bpc_detectmultiplier != BPC_DEF_DETECTMULTIPLIER); | |
329 | } | |
330 | ||
331 | /* Read (single|multi)hop and its options. */ | |
332 | STREAM_GETC(msg, bpc->bpc_mhop); | |
333 | if (bpc->bpc_mhop) { | |
334 | /* Read multihop source address and TTL. */ | |
335 | _ptm_msg_read_address(msg, &bpc->bpc_local); | |
336 | STREAM_GETC(msg, ttl); | |
d3af6147 RZ |
337 | } else { |
338 | /* If target is IPv6, then we must obtain local address. */ | |
339 | if (bpc->bpc_ipv4 == false) | |
340 | _ptm_msg_read_address(msg, &bpc->bpc_local); | |
341 | ||
342 | /* | |
343 | * Read interface name and make sure it fits our data | |
344 | * structure, otherwise fail. | |
345 | */ | |
346 | STREAM_GETC(msg, ifnamelen); | |
ae9f45a3 RZ |
347 | if (ifnamelen >= sizeof(bpc->bpc_localif)) { |
348 | log_error("ptm-read: interface name is too big"); | |
d3af6147 RZ |
349 | return -1; |
350 | } | |
351 | ||
352 | bpc->bpc_has_localif = ifnamelen > 0; | |
353 | if (bpc->bpc_has_localif) { | |
354 | STREAM_GET(bpc->bpc_localif, msg, ifnamelen); | |
355 | bpc->bpc_localif[ifnamelen] = 0; | |
356 | } | |
357 | } | |
358 | ||
359 | /* Sanity check: peer and local address must match IP types. */ | |
360 | if (bpc->bpc_local.sa_sin.sin_family != 0 | |
361 | && (bpc->bpc_local.sa_sin.sin_family | |
362 | != bpc->bpc_peer.sa_sin.sin_family)) { | |
ae9f45a3 | 363 | log_warning("ptm-read: peer family doesn't match local type"); |
d3af6147 RZ |
364 | return -1; |
365 | } | |
366 | ||
367 | return 0; | |
368 | ||
369 | stream_failure: | |
370 | return -1; | |
371 | } | |
372 | ||
373 | static void bfdd_dest_register(struct stream *msg) | |
374 | { | |
375 | struct ptm_client *pc; | |
376 | struct ptm_client_notification *pcn; | |
377 | struct bfd_session *bs; | |
378 | struct bfd_peer_cfg bpc; | |
379 | ||
380 | /* Read the client context and peer data. */ | |
381 | if (_ptm_msg_read(msg, ZEBRA_BFD_DEST_REGISTER, &bpc, &pc) == -1) | |
382 | return; | |
383 | ||
384 | DEBUG_PRINTBPC(&bpc); | |
385 | ||
386 | /* Find or start new BFD session. */ | |
387 | bs = bs_peer_find(&bpc); | |
388 | if (bs == NULL) { | |
389 | bs = ptm_bfd_sess_new(&bpc); | |
390 | if (bs == NULL) { | |
ae9f45a3 | 391 | log_debug("ptm-add-dest: failed to create BFD session"); |
d3af6147 RZ |
392 | return; |
393 | } | |
394 | } else { | |
395 | /* Don't try to change echo/shutdown state. */ | |
396 | bpc.bpc_echo = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
397 | bpc.bpc_shutdown = | |
398 | BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
399 | } | |
400 | ||
401 | /* Create client peer notification register. */ | |
402 | pcn = pcn_new(pc, bs); | |
403 | if (pcn == NULL) { | |
ae9f45a3 | 404 | log_error("ptm-add-dest: failed to registrate notifications"); |
d3af6147 RZ |
405 | return; |
406 | } | |
407 | ||
408 | ptm_bfd_notify(bs); | |
409 | } | |
410 | ||
411 | static void bfdd_dest_deregister(struct stream *msg) | |
412 | { | |
413 | struct ptm_client *pc; | |
414 | struct ptm_client_notification *pcn; | |
415 | struct bfd_session *bs; | |
416 | struct bfd_peer_cfg bpc; | |
417 | ||
418 | /* Read the client context and peer data. */ | |
419 | if (_ptm_msg_read(msg, ZEBRA_BFD_DEST_DEREGISTER, &bpc, &pc) == -1) | |
420 | return; | |
421 | ||
422 | DEBUG_PRINTBPC(&bpc); | |
423 | ||
424 | /* Find or start new BFD session. */ | |
425 | bs = bs_peer_find(&bpc); | |
426 | if (bs == NULL) { | |
ae9f45a3 | 427 | log_debug("ptm-del-dest: failed to find BFD session"); |
d3af6147 RZ |
428 | return; |
429 | } | |
430 | ||
431 | /* Unregister client peer notification. */ | |
432 | pcn = pcn_lookup(pc, bs); | |
433 | pcn_free(pcn); | |
434 | } | |
435 | ||
436 | /* | |
437 | * header: command, VRF | |
438 | * l: pid | |
439 | */ | |
440 | static void bfdd_client_register(struct stream *msg) | |
441 | { | |
442 | struct ptm_client *pc; | |
443 | uint32_t pid; | |
444 | ||
445 | /* Find or allocate process context data. */ | |
446 | STREAM_GETL(msg, pid); | |
447 | ||
448 | pc = pc_new(pid); | |
449 | if (pc == NULL) { | |
ae9f45a3 | 450 | log_error("ptm-add-client: failed to register client: %u", pid); |
d3af6147 RZ |
451 | return; |
452 | } | |
453 | ||
454 | return; | |
455 | ||
456 | stream_failure: | |
ae9f45a3 | 457 | log_error("ptm-add-client: failed to register client"); |
d3af6147 RZ |
458 | } |
459 | ||
a375de5c RZ |
460 | /* |
461 | * header: command, VRF | |
462 | * l: pid | |
463 | */ | |
464 | static void bfdd_client_deregister(struct stream *msg) | |
465 | { | |
466 | struct ptm_client *pc; | |
467 | uint32_t pid; | |
468 | ||
469 | /* Find or allocate process context data. */ | |
470 | STREAM_GETL(msg, pid); | |
471 | ||
472 | pc = pc_lookup(pid); | |
473 | if (pc == NULL) { | |
ae9f45a3 | 474 | log_debug("ptm-del-client: failed to find client: %u", pid); |
a375de5c RZ |
475 | return; |
476 | } | |
477 | ||
478 | pc_free(pc); | |
479 | ||
480 | return; | |
481 | ||
482 | stream_failure: | |
ae9f45a3 | 483 | log_error("ptm-del-client: failed to deregister client"); |
a375de5c RZ |
484 | } |
485 | ||
d3af6147 RZ |
486 | static int bfdd_replay(int cmd, struct zclient *zc, uint16_t len, vrf_id_t vid) |
487 | { | |
488 | struct stream *msg = zc->ibuf; | |
489 | uint32_t rcmd; | |
490 | ||
491 | STREAM_GETL(msg, rcmd); | |
492 | ||
493 | switch (rcmd) { | |
494 | case ZEBRA_BFD_DEST_REGISTER: | |
495 | case ZEBRA_BFD_DEST_UPDATE: | |
496 | bfdd_dest_register(msg); | |
497 | break; | |
498 | case ZEBRA_BFD_DEST_DEREGISTER: | |
499 | bfdd_dest_deregister(msg); | |
500 | break; | |
501 | case ZEBRA_BFD_CLIENT_REGISTER: | |
502 | bfdd_client_register(msg); | |
503 | break; | |
a375de5c RZ |
504 | case ZEBRA_BFD_CLIENT_DEREGISTER: |
505 | bfdd_client_deregister(msg); | |
506 | break; | |
d3af6147 RZ |
507 | |
508 | default: | |
ae9f45a3 | 509 | log_debug("ptm-replay: invalid message type %u", rcmd); |
d3af6147 RZ |
510 | return -1; |
511 | } | |
512 | ||
513 | return 0; | |
514 | ||
515 | stream_failure: | |
ae9f45a3 | 516 | log_error("ptm-replay: failed to find command"); |
d3af6147 RZ |
517 | return -1; |
518 | } | |
519 | ||
971532e2 RZ |
520 | static void bfdd_zebra_connected(struct zclient *zc) |
521 | { | |
522 | struct stream *msg = zc->obuf; | |
523 | ||
788378fe RZ |
524 | /* Clean-up and free ptm clients data memory. */ |
525 | pc_free_all(); | |
526 | ||
971532e2 RZ |
527 | /* |
528 | * The replay is an empty message just to trigger client daemons | |
529 | * configuration replay. | |
530 | */ | |
531 | stream_reset(msg); | |
532 | zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, VRF_DEFAULT); | |
533 | stream_putl(msg, ZEBRA_BFD_DEST_REPLAY); | |
534 | stream_putw_at(msg, 0, stream_get_endp(msg)); | |
535 | ||
80edb675 RZ |
536 | /* Ask for interfaces information. */ |
537 | zclient_create_header(msg, ZEBRA_INTERFACE_ADD, VRF_DEFAULT); | |
538 | ||
539 | /* Send requests. */ | |
971532e2 RZ |
540 | zclient_send_message(zclient); |
541 | } | |
542 | ||
d245e522 RZ |
543 | static void bfdd_sessions_enable_interface(struct interface *ifp) |
544 | { | |
545 | struct bfd_session_observer *bso; | |
546 | struct bfd_session *bs; | |
547 | ||
548 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
549 | if (bso->bso_isinterface == false) | |
550 | continue; | |
551 | ||
552 | /* Interface name mismatch. */ | |
553 | bs = bso->bso_bs; | |
79b4a6fc | 554 | if (strcmp(ifp->name, bs->key.ifname)) |
d245e522 RZ |
555 | continue; |
556 | /* Skip enabled sessions. */ | |
557 | if (bs->sock != -1) | |
558 | continue; | |
559 | ||
560 | /* Try to enable it. */ | |
561 | bfd_session_enable(bs); | |
562 | } | |
563 | } | |
564 | ||
565 | static void bfdd_sessions_disable_interface(struct interface *ifp) | |
566 | { | |
567 | struct bfd_session_observer *bso; | |
568 | struct bfd_session *bs; | |
569 | ||
570 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
571 | if (bso->bso_isinterface == false) | |
572 | continue; | |
573 | ||
574 | /* Interface name mismatch. */ | |
575 | bs = bso->bso_bs; | |
79b4a6fc | 576 | if (strcmp(ifp->name, bs->key.ifname)) |
d245e522 RZ |
577 | continue; |
578 | /* Skip disabled sessions. */ | |
579 | if (bs->sock == -1) | |
580 | continue; | |
581 | ||
582 | /* Try to enable it. */ | |
583 | bfd_session_disable(bs); | |
584 | ||
585 | TAILQ_INSERT_HEAD(&bglobal.bg_obslist, bso, bso_entry); | |
586 | } | |
587 | } | |
588 | ||
589 | static int bfdd_interface_update(int cmd, struct zclient *zc, | |
590 | uint16_t len __attribute__((__unused__)), | |
80edb675 RZ |
591 | vrf_id_t vrfid) |
592 | { | |
d245e522 RZ |
593 | struct interface *ifp; |
594 | ||
80edb675 RZ |
595 | /* |
596 | * `zebra_interface_add_read` will handle the interface creation | |
597 | * on `lib/if.c`. We'll use that data structure instead of | |
598 | * rolling our own. | |
599 | */ | |
600 | if (cmd == ZEBRA_INTERFACE_ADD) { | |
d245e522 RZ |
601 | ifp = zebra_interface_add_read(zc->ibuf, vrfid); |
602 | if (ifp == NULL) | |
603 | return 0; | |
604 | ||
605 | bfdd_sessions_enable_interface(ifp); | |
80edb675 RZ |
606 | return 0; |
607 | } | |
608 | ||
609 | /* Update interface information. */ | |
d245e522 RZ |
610 | ifp = zebra_interface_state_read(zc->ibuf, vrfid); |
611 | if (ifp == NULL) | |
612 | return 0; | |
80edb675 | 613 | |
d245e522 | 614 | bfdd_sessions_disable_interface(ifp); |
80edb675 | 615 | |
9d6c33ea DS |
616 | if_set_index(ifp, IFINDEX_INTERNAL); |
617 | ||
80edb675 RZ |
618 | return 0; |
619 | } | |
620 | ||
b333abc2 RZ |
621 | static int bfdd_interface_vrf_update(int command __attribute__((__unused__)), |
622 | struct zclient *zclient, | |
623 | zebra_size_t length | |
624 | __attribute__((__unused__)), | |
625 | vrf_id_t vrfid) | |
626 | { | |
627 | struct interface *ifp; | |
628 | vrf_id_t nvrfid; | |
629 | ||
630 | ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrfid, &nvrfid); | |
631 | if (ifp == NULL) | |
632 | return 0; | |
633 | ||
634 | if_update_to_new_vrf(ifp, nvrfid); | |
635 | ||
636 | return 0; | |
637 | } | |
638 | ||
261e0ba9 RZ |
639 | static void bfdd_sessions_enable_address(struct connected *ifc) |
640 | { | |
641 | struct bfd_session_observer *bso; | |
642 | struct bfd_session *bs; | |
643 | struct prefix prefix; | |
644 | ||
645 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
646 | if (bso->bso_isaddress == false) | |
647 | continue; | |
648 | ||
649 | /* Skip enabled sessions. */ | |
650 | bs = bso->bso_bs; | |
651 | if (bs->sock != -1) | |
652 | continue; | |
653 | ||
654 | /* Check address. */ | |
655 | prefix = bso->bso_addr; | |
656 | prefix.prefixlen = ifc->address->prefixlen; | |
657 | if (prefix_cmp(&prefix, ifc->address)) | |
658 | continue; | |
659 | ||
660 | /* Try to enable it. */ | |
661 | bfd_session_enable(bs); | |
662 | } | |
663 | } | |
664 | ||
665 | static int bfdd_interface_address_update(int cmd, struct zclient *zc, | |
666 | zebra_size_t len | |
667 | __attribute__((__unused__)), | |
668 | vrf_id_t vrfid) | |
669 | { | |
670 | struct connected *ifc; | |
671 | ||
672 | ifc = zebra_interface_address_read(cmd, zc->ibuf, vrfid); | |
673 | if (ifc == NULL) | |
674 | return 0; | |
675 | ||
676 | bfdd_sessions_enable_address(ifc); | |
677 | ||
678 | return 0; | |
679 | } | |
680 | ||
d3af6147 RZ |
681 | void bfdd_zclient_init(struct zebra_privs_t *bfdd_priv) |
682 | { | |
26f63a1e | 683 | zclient = zclient_new(master, &zclient_options_default); |
d3af6147 RZ |
684 | assert(zclient != NULL); |
685 | zclient_init(zclient, ZEBRA_ROUTE_BFD, 0, bfdd_priv); | |
686 | ||
687 | /* | |
688 | * We'll receive all messages through replay, however it will | |
689 | * contain a special field with the real command inside so we | |
690 | * avoid having to create too many handlers. | |
691 | */ | |
692 | zclient->bfd_dest_replay = bfdd_replay; | |
971532e2 RZ |
693 | |
694 | /* Send replay request on zebra connect. */ | |
695 | zclient->zebra_connected = bfdd_zebra_connected; | |
80edb675 RZ |
696 | |
697 | /* Learn interfaces from zebra instead of the OS. */ | |
698 | zclient->interface_add = bfdd_interface_update; | |
699 | zclient->interface_delete = bfdd_interface_update; | |
b333abc2 RZ |
700 | |
701 | /* Learn about interface VRF. */ | |
702 | zclient->interface_vrf_update = bfdd_interface_vrf_update; | |
261e0ba9 RZ |
703 | |
704 | /* Learn about new addresses being registered. */ | |
705 | zclient->interface_address_add = bfdd_interface_address_update; | |
706 | zclient->interface_address_delete = bfdd_interface_address_update; | |
d3af6147 RZ |
707 | } |
708 | ||
709 | void bfdd_zclient_stop(void) | |
710 | { | |
711 | zclient_stop(zclient); | |
788378fe RZ |
712 | |
713 | /* Clean-up and free ptm clients data memory. */ | |
714 | pc_free_all(); | |
d3af6147 RZ |
715 | } |
716 | ||
717 | ||
718 | /* | |
719 | * Client handling. | |
720 | */ | |
721 | static struct ptm_client *pc_lookup(uint32_t pid) | |
722 | { | |
723 | struct ptm_client *pc; | |
724 | ||
725 | TAILQ_FOREACH (pc, &pcqueue, pc_entry) { | |
726 | if (pc->pc_pid != pid) | |
727 | continue; | |
728 | ||
729 | break; | |
730 | } | |
731 | ||
732 | return pc; | |
733 | } | |
734 | ||
735 | static struct ptm_client *pc_new(uint32_t pid) | |
736 | { | |
737 | struct ptm_client *pc; | |
738 | ||
739 | /* Look up first, if not found create the client. */ | |
740 | pc = pc_lookup(pid); | |
741 | if (pc != NULL) | |
742 | return pc; | |
743 | ||
744 | /* Allocate the client data and save it. */ | |
745 | pc = XCALLOC(MTYPE_BFDD_CONTROL, sizeof(*pc)); | |
d3af6147 RZ |
746 | |
747 | pc->pc_pid = pid; | |
748 | TAILQ_INSERT_HEAD(&pcqueue, pc, pc_entry); | |
749 | return pc; | |
750 | } | |
751 | ||
a375de5c RZ |
752 | static void pc_free(struct ptm_client *pc) |
753 | { | |
754 | struct ptm_client_notification *pcn; | |
755 | ||
756 | if (pc == NULL) | |
757 | return; | |
758 | ||
759 | TAILQ_REMOVE(&pcqueue, pc, pc_entry); | |
760 | ||
761 | while (!TAILQ_EMPTY(&pc->pc_pcnqueue)) { | |
762 | pcn = TAILQ_FIRST(&pc->pc_pcnqueue); | |
763 | pcn_free(pcn); | |
764 | } | |
765 | ||
766 | XFREE(MTYPE_BFDD_CONTROL, pc); | |
767 | } | |
768 | ||
788378fe RZ |
769 | static void pc_free_all(void) |
770 | { | |
771 | struct ptm_client *pc; | |
772 | ||
773 | while (!TAILQ_EMPTY(&pcqueue)) { | |
774 | pc = TAILQ_FIRST(&pcqueue); | |
775 | pc_free(pc); | |
776 | } | |
777 | } | |
778 | ||
d3af6147 RZ |
779 | static struct ptm_client_notification *pcn_new(struct ptm_client *pc, |
780 | struct bfd_session *bs) | |
781 | { | |
782 | struct ptm_client_notification *pcn; | |
783 | ||
784 | /* Try to find an existing pcn fist. */ | |
785 | pcn = pcn_lookup(pc, bs); | |
786 | if (pcn != NULL) | |
787 | return pcn; | |
788 | ||
789 | /* Save the client notification data. */ | |
790 | pcn = XCALLOC(MTYPE_BFDD_NOTIFICATION, sizeof(*pcn)); | |
d3af6147 RZ |
791 | |
792 | TAILQ_INSERT_HEAD(&pc->pc_pcnqueue, pcn, pcn_entry); | |
793 | pcn->pcn_pc = pc; | |
794 | pcn->pcn_bs = bs; | |
795 | bs->refcount++; | |
796 | ||
797 | return pcn; | |
798 | } | |
799 | ||
800 | static struct ptm_client_notification *pcn_lookup(struct ptm_client *pc, | |
801 | struct bfd_session *bs) | |
802 | { | |
803 | struct ptm_client_notification *pcn; | |
804 | ||
805 | TAILQ_FOREACH (pcn, &pc->pc_pcnqueue, pcn_entry) { | |
806 | if (pcn->pcn_bs != bs) | |
807 | continue; | |
808 | ||
809 | break; | |
810 | } | |
811 | ||
812 | return pcn; | |
813 | } | |
814 | ||
815 | static void pcn_free(struct ptm_client_notification *pcn) | |
816 | { | |
817 | struct ptm_client *pc; | |
818 | struct bfd_session *bs; | |
819 | ||
820 | if (pcn == NULL) | |
821 | return; | |
822 | ||
823 | /* Handle session de-registration. */ | |
824 | bs = pcn->pcn_bs; | |
825 | pcn->pcn_bs = NULL; | |
826 | bs->refcount--; | |
827 | ||
828 | /* Handle ptm_client deregistration. */ | |
829 | pc = pcn->pcn_pc; | |
830 | pcn->pcn_pc = NULL; | |
831 | TAILQ_REMOVE(&pc->pc_pcnqueue, pcn, pcn_entry); | |
832 | ||
833 | XFREE(MTYPE_BFDD_NOTIFICATION, pcn); | |
834 | } |