]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_zebra.c
Merge pull request #6114 from rgirada/frr-static
[mirror_frr.git] / ospf6d / ospf6_zebra.c
1 /*
2 * Copyright (C) 2003 Yasuhiro Ohara
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "log.h"
24 #include "vty.h"
25 #include "command.h"
26 #include "prefix.h"
27 #include "stream.h"
28 #include "zclient.h"
29 #include "memory.h"
30 #include "lib/bfd.h"
31 #include "lib_errors.h"
32
33 #include "ospf6_proto.h"
34 #include "ospf6_top.h"
35 #include "ospf6_interface.h"
36 #include "ospf6_route.h"
37 #include "ospf6_lsa.h"
38 #include "ospf6_lsdb.h"
39 #include "ospf6_asbr.h"
40 #include "ospf6_zebra.h"
41 #include "ospf6d.h"
42
43 DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_DISTANCE, "OSPF6 distance")
44
45 unsigned char conf_debug_ospf6_zebra = 0;
46
47 /* information about zebra. */
48 struct zclient *zclient = NULL;
49
50 /* Router-id update message from zebra. */
51 static int ospf6_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
52 {
53 struct prefix router_id;
54 struct ospf6 *o = ospf6;
55
56 zebra_router_id_update_read(zclient->ibuf, &router_id);
57
58 om6->zebra_router_id = router_id.u.prefix4.s_addr;
59
60 if (o == NULL)
61 return 0;
62
63 o->router_id_zebra = router_id.u.prefix4;
64 if (IS_OSPF6_DEBUG_ZEBRA(RECV)) {
65 char buf[INET_ADDRSTRLEN];
66
67 zlog_debug("%s: zebra router-id %s update", __func__,
68 inet_ntop(AF_INET, &router_id.u.prefix4, buf,
69 INET_ADDRSTRLEN));
70 }
71
72 ospf6_router_id_update();
73
74 return 0;
75 }
76
77 /* redistribute function */
78 void ospf6_zebra_redistribute(int type)
79 {
80 if (vrf_bitmap_check(zclient->redist[AFI_IP6][type], VRF_DEFAULT))
81 return;
82 vrf_bitmap_set(zclient->redist[AFI_IP6][type], VRF_DEFAULT);
83
84 if (zclient->sock > 0)
85 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient,
86 AFI_IP6, type, 0, VRF_DEFAULT);
87 }
88
89 void ospf6_zebra_no_redistribute(int type)
90 {
91 if (!vrf_bitmap_check(zclient->redist[AFI_IP6][type], VRF_DEFAULT))
92 return;
93 vrf_bitmap_unset(zclient->redist[AFI_IP6][type], VRF_DEFAULT);
94 if (zclient->sock > 0)
95 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
96 AFI_IP6, type, 0, VRF_DEFAULT);
97 }
98
99 static int ospf6_zebra_if_address_update_add(ZAPI_CALLBACK_ARGS)
100 {
101 struct connected *c;
102 char buf[128];
103
104 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
105 zclient->ibuf, vrf_id);
106 if (c == NULL)
107 return 0;
108
109 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
110 zlog_debug("Zebra Interface address add: %s %5s %s/%d",
111 c->ifp->name, prefix_family_str(c->address),
112 inet_ntop(c->address->family, &c->address->u.prefix,
113 buf, sizeof(buf)),
114 c->address->prefixlen);
115
116 if (c->address->family == AF_INET6) {
117 ospf6_interface_state_update(c->ifp);
118 ospf6_interface_connected_route_update(c->ifp);
119 }
120 return 0;
121 }
122
123 static int ospf6_zebra_if_address_update_delete(ZAPI_CALLBACK_ARGS)
124 {
125 struct connected *c;
126 char buf[128];
127
128 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
129 zclient->ibuf, vrf_id);
130 if (c == NULL)
131 return 0;
132
133 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
134 zlog_debug("Zebra Interface address delete: %s %5s %s/%d",
135 c->ifp->name, prefix_family_str(c->address),
136 inet_ntop(c->address->family, &c->address->u.prefix,
137 buf, sizeof(buf)),
138 c->address->prefixlen);
139
140 if (c->address->family == AF_INET6) {
141 ospf6_interface_connected_route_update(c->ifp);
142 ospf6_interface_state_update(c->ifp);
143 }
144
145 connected_free(&c);
146
147 return 0;
148 }
149
150 static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS)
151 {
152 struct zapi_route api;
153 unsigned long ifindex;
154 struct in6_addr *nexthop;
155
156 if (ospf6 == NULL)
157 return 0;
158
159 if (zapi_route_decode(zclient->ibuf, &api) < 0)
160 return -1;
161
162 /* we completely ignore srcdest routes for now. */
163 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
164 return 0;
165
166 if (IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
167 return 0;
168
169 ifindex = api.nexthops[0].ifindex;
170 nexthop = &api.nexthops[0].gate.ipv6;
171
172 if (IS_OSPF6_DEBUG_ZEBRA(RECV)) {
173 char prefixstr[PREFIX2STR_BUFFER], nexthopstr[128];
174
175 prefix2str(&api.prefix, prefixstr, sizeof(prefixstr));
176 inet_ntop(AF_INET6, nexthop, nexthopstr, sizeof(nexthopstr));
177
178 zlog_debug(
179 "Zebra Receive route %s: %s %s nexthop %s ifindex %ld tag %" ROUTE_TAG_PRI,
180 (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD ? "add"
181 : "delete"),
182 zebra_route_string(api.type), prefixstr, nexthopstr,
183 ifindex, api.tag);
184 }
185
186 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
187 ospf6_asbr_redistribute_add(api.type, ifindex, &api.prefix,
188 api.nexthop_num, nexthop, api.tag);
189 else
190 ospf6_asbr_redistribute_remove(api.type, ifindex, &api.prefix);
191
192 return 0;
193 }
194
195 DEFUN (show_zebra,
196 show_ospf6_zebra_cmd,
197 "show ipv6 ospf6 zebra",
198 SHOW_STR
199 IPV6_STR
200 OSPF6_STR
201 ZEBRA_STR)
202 {
203 int i;
204 if (zclient == NULL) {
205 vty_out(vty, "Not connected to zebra\n");
206 return CMD_SUCCESS;
207 }
208
209 vty_out(vty, "Zebra Information\n");
210 vty_out(vty, " fail: %d\n", zclient->fail);
211 vty_out(vty, " redistribute default: %d\n",
212 vrf_bitmap_check(zclient->default_information[AFI_IP6],
213 VRF_DEFAULT));
214 vty_out(vty, " redistribute:");
215 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
216 if (vrf_bitmap_check(zclient->redist[AFI_IP6][i], VRF_DEFAULT))
217 vty_out(vty, " %s", zebra_route_string(i));
218 }
219 vty_out(vty, "\n");
220 return CMD_SUCCESS;
221 }
222
223 #define ADD 0
224 #define REM 1
225 static void ospf6_zebra_route_update(int type, struct ospf6_route *request)
226 {
227 struct zapi_route api;
228 char buf[PREFIX2STR_BUFFER];
229 int nhcount;
230 int ret = 0;
231 struct prefix *dest;
232
233 if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
234 prefix2str(&request->prefix, buf, sizeof(buf));
235 zlog_debug("Send %s route: %s",
236 (type == REM ? "remove" : "add"), buf);
237 }
238
239 if (zclient->sock < 0) {
240 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
241 zlog_debug(" Not connected to Zebra");
242 return;
243 }
244
245 if (request->path.origin.adv_router == ospf6->router_id
246 && (request->path.type == OSPF6_PATH_TYPE_EXTERNAL1
247 || request->path.type == OSPF6_PATH_TYPE_EXTERNAL2)) {
248 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
249 zlog_debug(" Ignore self-originated external route");
250 return;
251 }
252
253 /* If removing is the best path and if there's another path,
254 treat this request as add the secondary path */
255 if (type == REM && ospf6_route_is_best(request) && request->next
256 && ospf6_route_is_same(request, request->next)) {
257 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
258 zlog_debug(
259 " Best-path removal resulted Secondary addition");
260 type = ADD;
261 request = request->next;
262 }
263
264 /* Only the best path will be sent to zebra. */
265 if (!ospf6_route_is_best(request)) {
266 /* this is not preferred best route, ignore */
267 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
268 zlog_debug(" Ignore non-best route");
269 return;
270 }
271
272 nhcount = ospf6_route_num_nexthops(request);
273 if (nhcount == 0) {
274 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
275 zlog_debug(" No nexthop, ignore");
276 return;
277 }
278
279 dest = &request->prefix;
280
281 memset(&api, 0, sizeof(api));
282 api.vrf_id = VRF_DEFAULT;
283 api.type = ZEBRA_ROUTE_OSPF6;
284 api.safi = SAFI_UNICAST;
285 api.prefix = *dest;
286 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
287 api.nexthop_num = MIN(nhcount, MULTIPATH_NUM);
288 ospf6_route_zebra_copy_nexthops(request, api.nexthops, api.nexthop_num);
289 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
290 api.metric = (request->path.metric_type == 2 ? request->path.u.cost_e2
291 : request->path.cost);
292 if (request->path.tag) {
293 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
294 api.tag = request->path.tag;
295 }
296
297 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
298 api.distance =
299 ospf6_distance_apply((struct prefix_ipv6 *)dest, request);
300
301 if (type == REM)
302 ret = zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
303 else
304 ret = zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
305
306 if (ret < 0)
307 flog_err(EC_LIB_ZAPI_SOCKET,
308 "zclient_route_send() %s failed: %s",
309 (type == REM ? "delete" : "add"),
310 safe_strerror(errno));
311
312 return;
313 }
314
315 void ospf6_zebra_route_update_add(struct ospf6_route *request)
316 {
317 ospf6_zebra_route_update(ADD, request);
318 }
319
320 void ospf6_zebra_route_update_remove(struct ospf6_route *request)
321 {
322 ospf6_zebra_route_update(REM, request);
323 }
324
325 void ospf6_zebra_add_discard(struct ospf6_route *request)
326 {
327 struct zapi_route api;
328 char buf[INET6_ADDRSTRLEN];
329 struct prefix *dest = &request->prefix;
330
331 if (!CHECK_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED)) {
332 memset(&api, 0, sizeof(api));
333 api.vrf_id = VRF_DEFAULT;
334 api.type = ZEBRA_ROUTE_OSPF6;
335 api.safi = SAFI_UNICAST;
336 api.prefix = *dest;
337 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
338
339 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
340
341 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
342 zlog_debug("Zebra: Route add discard %s/%d",
343 inet_ntop(AF_INET6, &dest->u.prefix6, buf,
344 INET6_ADDRSTRLEN),
345 dest->prefixlen);
346
347 SET_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED);
348 } else {
349 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
350 zlog_debug(
351 "Zebra: Blackhole route present already %s/%d",
352 inet_ntop(AF_INET6, &dest->u.prefix6, buf,
353 INET6_ADDRSTRLEN),
354 dest->prefixlen);
355 }
356 }
357
358 void ospf6_zebra_delete_discard(struct ospf6_route *request)
359 {
360 struct zapi_route api;
361 char buf[INET6_ADDRSTRLEN];
362 struct prefix *dest = &request->prefix;
363
364 if (CHECK_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED)) {
365 memset(&api, 0, sizeof(api));
366 api.vrf_id = VRF_DEFAULT;
367 api.type = ZEBRA_ROUTE_OSPF6;
368 api.safi = SAFI_UNICAST;
369 api.prefix = *dest;
370 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
371
372 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
373
374 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
375 zlog_debug("Zebra: Route delete discard %s/%d",
376 inet_ntop(AF_INET6, &dest->u.prefix6, buf,
377 INET6_ADDRSTRLEN),
378 dest->prefixlen);
379
380 UNSET_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED);
381 } else {
382 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
383 zlog_debug(
384 "Zebra: Blackhole route already deleted %s/%d",
385 inet_ntop(AF_INET6, &dest->u.prefix6, buf,
386 INET6_ADDRSTRLEN),
387 dest->prefixlen);
388 }
389 }
390
391 static struct ospf6_distance *ospf6_distance_new(void)
392 {
393 return XCALLOC(MTYPE_OSPF6_DISTANCE, sizeof(struct ospf6_distance));
394 }
395
396 static void ospf6_distance_free(struct ospf6_distance *odistance)
397 {
398 XFREE(MTYPE_OSPF6_DISTANCE, odistance);
399 }
400
401 int ospf6_distance_set(struct vty *vty, struct ospf6 *o,
402 const char *distance_str, const char *ip_str,
403 const char *access_list_str)
404 {
405 int ret;
406 struct prefix_ipv6 p;
407 uint8_t distance;
408 struct route_node *rn;
409 struct ospf6_distance *odistance;
410
411 ret = str2prefix_ipv6(ip_str, &p);
412 if (ret == 0) {
413 vty_out(vty, "Malformed prefix\n");
414 return CMD_WARNING_CONFIG_FAILED;
415 }
416
417 distance = atoi(distance_str);
418
419 /* Get OSPF6 distance node. */
420 rn = route_node_get(o->distance_table, (struct prefix *)&p);
421 if (rn->info) {
422 odistance = rn->info;
423 route_unlock_node(rn);
424 } else {
425 odistance = ospf6_distance_new();
426 rn->info = odistance;
427 }
428
429 /* Set distance value. */
430 odistance->distance = distance;
431
432 /* Reset access-list configuration. */
433 if (odistance->access_list) {
434 free(odistance->access_list);
435 odistance->access_list = NULL;
436 }
437 if (access_list_str)
438 odistance->access_list = strdup(access_list_str);
439
440 return CMD_SUCCESS;
441 }
442
443 int ospf6_distance_unset(struct vty *vty, struct ospf6 *o,
444 const char *distance_str, const char *ip_str,
445 const char *access_list_str)
446 {
447 int ret;
448 struct prefix_ipv6 p;
449 struct route_node *rn;
450 struct ospf6_distance *odistance;
451
452 ret = str2prefix_ipv6(ip_str, &p);
453 if (ret == 0) {
454 vty_out(vty, "Malformed prefix\n");
455 return CMD_WARNING_CONFIG_FAILED;
456 }
457
458 rn = route_node_lookup(o->distance_table, (struct prefix *)&p);
459 if (!rn) {
460 vty_out(vty, "Cant't find specified prefix\n");
461 return CMD_WARNING_CONFIG_FAILED;
462 }
463
464 odistance = rn->info;
465
466 if (odistance->access_list)
467 free(odistance->access_list);
468 ospf6_distance_free(odistance);
469
470 rn->info = NULL;
471 route_unlock_node(rn);
472 route_unlock_node(rn);
473
474 return CMD_SUCCESS;
475 }
476
477 void ospf6_distance_reset(struct ospf6 *o)
478 {
479 struct route_node *rn;
480 struct ospf6_distance *odistance;
481
482 for (rn = route_top(o->distance_table); rn; rn = route_next(rn))
483 if ((odistance = rn->info) != NULL) {
484 if (odistance->access_list)
485 free(odistance->access_list);
486 ospf6_distance_free(odistance);
487 rn->info = NULL;
488 route_unlock_node(rn);
489 }
490 }
491
492 uint8_t ospf6_distance_apply(struct prefix_ipv6 *p, struct ospf6_route * or)
493 {
494 struct ospf6 *o;
495
496 o = ospf6;
497 if (o == NULL)
498 return 0;
499
500 if (o->distance_intra)
501 if (or->path.type == OSPF6_PATH_TYPE_INTRA)
502 return o->distance_intra;
503
504 if (o->distance_inter)
505 if (or->path.type == OSPF6_PATH_TYPE_INTER)
506 return o->distance_inter;
507
508 if (o->distance_external)
509 if (or->path.type == OSPF6_PATH_TYPE_EXTERNAL1 ||
510 or->path.type == OSPF6_PATH_TYPE_EXTERNAL2)
511 return o->distance_external;
512
513 if (o->distance_all)
514 return o->distance_all;
515
516 return 0;
517 }
518
519 static void ospf6_zebra_connected(struct zclient *zclient)
520 {
521 /* Send the client registration */
522 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
523
524 zclient_send_reg_requests(zclient, VRF_DEFAULT);
525 }
526
527 void ospf6_zebra_init(struct thread_master *master)
528 {
529 /* Allocate zebra structure. */
530 zclient = zclient_new(master, &zclient_options_default);
531 zclient_init(zclient, ZEBRA_ROUTE_OSPF6, 0, &ospf6d_privs);
532 zclient->zebra_connected = ospf6_zebra_connected;
533 zclient->router_id_update = ospf6_router_id_update_zebra;
534 zclient->interface_address_add = ospf6_zebra_if_address_update_add;
535 zclient->interface_address_delete =
536 ospf6_zebra_if_address_update_delete;
537 zclient->redistribute_route_add = ospf6_zebra_read_route;
538 zclient->redistribute_route_del = ospf6_zebra_read_route;
539
540 /* Install command element for zebra node. */
541 install_element(VIEW_NODE, &show_ospf6_zebra_cmd);
542 }
543
544 /* Debug */
545
546 DEFUN (debug_ospf6_zebra_sendrecv,
547 debug_ospf6_zebra_sendrecv_cmd,
548 "debug ospf6 zebra [<send|recv>]",
549 DEBUG_STR
550 OSPF6_STR
551 "Debug connection between zebra\n"
552 "Debug Sending zebra\n"
553 "Debug Receiving zebra\n"
554 )
555 {
556 int idx_send_recv = 3;
557 unsigned char level = 0;
558
559 if (argc == 4) {
560 if (strmatch(argv[idx_send_recv]->text, "send"))
561 level = OSPF6_DEBUG_ZEBRA_SEND;
562 else if (strmatch(argv[idx_send_recv]->text, "recv"))
563 level = OSPF6_DEBUG_ZEBRA_RECV;
564 } else
565 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
566
567 OSPF6_DEBUG_ZEBRA_ON(level);
568 return CMD_SUCCESS;
569 }
570
571 DEFUN (no_debug_ospf6_zebra_sendrecv,
572 no_debug_ospf6_zebra_sendrecv_cmd,
573 "no debug ospf6 zebra [<send|recv>]",
574 NO_STR
575 DEBUG_STR
576 OSPF6_STR
577 "Debug connection between zebra\n"
578 "Debug Sending zebra\n"
579 "Debug Receiving zebra\n"
580 )
581 {
582 int idx_send_recv = 4;
583 unsigned char level = 0;
584
585 if (argc == 5) {
586 if (strmatch(argv[idx_send_recv]->text, "send"))
587 level = OSPF6_DEBUG_ZEBRA_SEND;
588 else if (strmatch(argv[idx_send_recv]->text, "recv"))
589 level = OSPF6_DEBUG_ZEBRA_RECV;
590 } else
591 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
592
593 OSPF6_DEBUG_ZEBRA_OFF(level);
594 return CMD_SUCCESS;
595 }
596
597
598 int config_write_ospf6_debug_zebra(struct vty *vty)
599 {
600 if (IS_OSPF6_DEBUG_ZEBRA(SEND) && IS_OSPF6_DEBUG_ZEBRA(RECV))
601 vty_out(vty, "debug ospf6 zebra\n");
602 else {
603 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
604 vty_out(vty, "debug ospf6 zebra send\n");
605 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
606 vty_out(vty, "debug ospf6 zebra recv\n");
607 }
608 return 0;
609 }
610
611 void install_element_ospf6_debug_zebra(void)
612 {
613 install_element(ENABLE_NODE, &debug_ospf6_zebra_sendrecv_cmd);
614 install_element(ENABLE_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
615 install_element(CONFIG_NODE, &debug_ospf6_zebra_sendrecv_cmd);
616 install_element(CONFIG_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
617 }