]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
lib, bgpd, tests: Refactor FILTER_X in zebra.h
[mirror_frr.git] / bgpd / bgp_zebra.c
1 /* zebra client
2 Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
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
17 along with GNU Zebra; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "stream.h"
25 #include "network.h"
26 #include "prefix.h"
27 #include "log.h"
28 #include "sockunion.h"
29 #include "zclient.h"
30 #include "routemap.h"
31 #include "thread.h"
32 #include "queue.h"
33 #include "memory.h"
34 #include "lib/json.h"
35 #include "lib/bfd.h"
36 #include "filter.h"
37
38 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_route.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_nexthop.h"
42 #include "bgpd/bgp_zebra.h"
43 #include "bgpd/bgp_fsm.h"
44 #include "bgpd/bgp_debug.h"
45 #include "bgpd/bgp_mpath.h"
46 #include "bgpd/bgp_nexthop.h"
47 #include "bgpd/bgp_nht.h"
48
49 /* All information about zebra. */
50 struct zclient *zclient = NULL;
51
52 /* Growable buffer for nexthops sent to zebra */
53 struct stream *bgp_nexthop_buf = NULL;
54 struct stream *bgp_ifindices_buf = NULL;
55
56 /* These array buffers are used in making a copy of the attributes for
57 route-map apply. Arrays are being used here to minimize mallocs and
58 frees for the temporary copy of the attributes.
59 Given the zapi api expects the nexthop buffer to contain pointer to
60 pointers for nexthops, we couldnt have used a single nexthop variable
61 on the stack, hence we had two options:
62 1. maintain a linked-list and free it after zapi_*_route call
63 2. use an array to avoid number of mallocs.
64 Number of supported next-hops are finite, use of arrays should be ok. */
65 struct attr attr_cp[MULTIPATH_NUM];
66 struct attr_extra attr_extra_cp[MULTIPATH_NUM];
67 int attr_index = 0;
68
69 /* Once per address-family initialization of the attribute array */
70 #define BGP_INFO_ATTR_BUF_INIT()\
71 do {\
72 memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr));\
73 memset(attr_extra_cp, 0, MULTIPATH_NUM * sizeof(struct attr_extra));\
74 attr_index = 0;\
75 } while (0)
76
77 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
78 do { \
79 *info_dst = *info_src; \
80 assert(attr_index != MULTIPATH_NUM);\
81 attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
82 bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
83 bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
84 info_dst->attr = &attr_cp[attr_index]; \
85 attr_index++;\
86 } while (0)
87
88 #define BGP_INFO_ATTR_BUF_FREE(info) \
89 do { \
90 bgp_attr_deep_free(info->attr); \
91 } while (0)
92
93
94 /* Can we install into zebra? */
95 static inline int
96 bgp_install_info_to_zebra (struct bgp *bgp)
97 {
98 if (zclient->sock <= 0)
99 return 0;
100
101 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
102 return 0;
103
104 return 1;
105 }
106
107 /* Router-id update message from zebra. */
108 static int
109 bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length,
110 vrf_id_t vrf_id)
111 {
112 struct prefix router_id;
113
114 zebra_router_id_update_read(zclient->ibuf,&router_id);
115
116 if (BGP_DEBUG (zebra, ZEBRA))
117 {
118 char buf[PREFIX2STR_BUFFER];
119 prefix2str(&router_id, buf, sizeof(buf));
120 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id, buf);
121 }
122
123 bgp_router_id_zebra_bump (vrf_id, &router_id);
124 return 0;
125 }
126
127 /* Nexthop update message from zebra. */
128 static int
129 bgp_read_nexthop_update (int command, struct zclient *zclient,
130 zebra_size_t length, vrf_id_t vrf_id)
131 {
132 bgp_parse_nexthop_update(command, vrf_id);
133 return 0;
134 }
135
136 static int
137 bgp_read_import_check_update(int command, struct zclient *zclient,
138 zebra_size_t length, vrf_id_t vrf_id)
139 {
140 bgp_parse_nexthop_update(command, vrf_id);
141 return 0;
142 }
143
144 /* Set or clear interface on which unnumbered neighbor is configured. This
145 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
146 * interface.
147 */
148 static void
149 bgp_update_interface_nbrs (struct bgp *bgp, struct interface *ifp,
150 struct interface *upd_ifp)
151 {
152 struct listnode *node, *nnode;
153 struct peer *peer;
154
155 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
156 {
157 if (peer->conf_if &&
158 (strcmp (peer->conf_if, ifp->name) == 0))
159 {
160 if (upd_ifp)
161 {
162 peer->ifp = upd_ifp;
163 bgp_zebra_initiate_radv (bgp, peer);
164 }
165 else
166 {
167 bgp_zebra_terminate_radv (bgp, peer);
168 peer->ifp = upd_ifp;
169 }
170 }
171 }
172 }
173
174 static void
175 bgp_start_interface_nbrs (struct bgp *bgp, struct interface *ifp)
176 {
177 struct listnode *node, *nnode;
178 struct peer *peer;
179
180 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
181 {
182 if (peer->conf_if &&
183 (strcmp (peer->conf_if, ifp->name) == 0) &&
184 peer->status != Established)
185 {
186 if (peer_active(peer))
187 BGP_EVENT_ADD (peer, BGP_Stop);
188 BGP_EVENT_ADD (peer, BGP_Start);
189 }
190 }
191 }
192
193 static void
194 bgp_nbr_connected_add (struct bgp *bgp, struct nbr_connected *ifc)
195 {
196 struct listnode *node;
197 struct connected *connected;
198 struct interface *ifp;
199 struct prefix *p;
200
201 /* Kick-off the FSM for any relevant peers only if there is a
202 * valid local address on the interface.
203 */
204 ifp = ifc->ifp;
205 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
206 {
207 p = connected->address;
208 if (p->family == AF_INET6 &&
209 IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
210 break;
211 }
212 if (!connected)
213 return;
214
215 bgp_start_interface_nbrs (bgp, ifp);
216 }
217
218 static void
219 bgp_nbr_connected_delete (struct bgp *bgp, struct nbr_connected *ifc, int del)
220 {
221 struct listnode *node, *nnode;
222 struct peer *peer;
223 struct interface *ifp;
224
225 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
226 {
227 if (peer->conf_if && (strcmp (peer->conf_if, ifc->ifp->name) == 0))
228 {
229 peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
230 BGP_EVENT_ADD (peer, BGP_Stop);
231 }
232 }
233 /* Free neighbor also, if we're asked to. */
234 if (del)
235 {
236 ifp = ifc->ifp;
237 listnode_delete (ifp->nbr_connected, ifc);
238 nbr_connected_free (ifc);
239 }
240 }
241
242 /* Inteface addition message from zebra. */
243 static int
244 bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length,
245 vrf_id_t vrf_id)
246 {
247 struct interface *ifp;
248 struct bgp *bgp;
249
250 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
251 if (!ifp) // unexpected
252 return 0;
253
254 if (BGP_DEBUG (zebra, ZEBRA) && ifp)
255 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id, ifp->name);
256
257 bgp = bgp_lookup_by_vrf_id (vrf_id);
258 if (!bgp)
259 return 0;
260
261 bgp_update_interface_nbrs (bgp, ifp, ifp);
262 return 0;
263 }
264
265 static int
266 bgp_interface_delete (int command, struct zclient *zclient,
267 zebra_size_t length, vrf_id_t vrf_id)
268 {
269 struct stream *s;
270 struct interface *ifp;
271 struct bgp *bgp;
272
273 s = zclient->ibuf;
274 ifp = zebra_interface_state_read (s, vrf_id);
275 if (!ifp) /* This may happen if we've just unregistered for a VRF. */
276 return 0;
277
278 ifp->ifindex = IFINDEX_DELETED;
279
280 if (BGP_DEBUG (zebra, ZEBRA))
281 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);
282
283 bgp = bgp_lookup_by_vrf_id (vrf_id);
284 if (!bgp)
285 return 0;
286
287 bgp_update_interface_nbrs (bgp, ifp, NULL);
288 return 0;
289 }
290
291 static int
292 bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length,
293 vrf_id_t vrf_id)
294 {
295 struct stream *s;
296 struct interface *ifp;
297 struct connected *c;
298 struct nbr_connected *nc;
299 struct listnode *node, *nnode;
300 struct bgp *bgp;
301
302 s = zclient->ibuf;
303 ifp = zebra_interface_state_read (s, vrf_id);
304
305 if (! ifp)
306 return 0;
307
308 if (BGP_DEBUG (zebra, ZEBRA))
309 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id, ifp->name);
310
311 bgp = bgp_lookup_by_vrf_id (vrf_id);
312 if (!bgp)
313 return 0;
314
315 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
316 bgp_connected_add (bgp, c);
317
318 for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
319 bgp_nbr_connected_add (bgp, nc);
320
321 return 0;
322 }
323
324 static int
325 bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length,
326 vrf_id_t vrf_id)
327 {
328 struct stream *s;
329 struct interface *ifp;
330 struct connected *c;
331 struct nbr_connected *nc;
332 struct listnode *node, *nnode;
333 struct bgp *bgp;
334
335 s = zclient->ibuf;
336 ifp = zebra_interface_state_read (s, vrf_id);
337 if (! ifp)
338 return 0;
339
340 if (BGP_DEBUG (zebra, ZEBRA))
341 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id, ifp->name);
342
343 bgp = bgp_lookup_by_vrf_id (vrf_id);
344 if (!bgp)
345 return 0;
346
347 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
348 bgp_connected_delete (bgp, c);
349
350 for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
351 bgp_nbr_connected_delete (bgp, nc, 1);
352
353 /* Fast external-failover */
354 {
355 struct peer *peer;
356
357 if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
358 return 0;
359
360 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
361 {
362 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
363 continue;
364
365 if (ifp == peer->nexthop.ifp)
366 {
367 BGP_EVENT_ADD (peer, BGP_Stop);
368 peer->last_reset = PEER_DOWN_IF_DOWN;
369 }
370 }
371 }
372
373 return 0;
374 }
375
376 static int
377 bgp_interface_address_add (int command, struct zclient *zclient,
378 zebra_size_t length, vrf_id_t vrf_id)
379 {
380 struct connected *ifc;
381
382 ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
383
384 if (ifc == NULL)
385 return 0;
386
387 if (bgp_debug_zebra(ifc->address))
388 {
389 char buf[PREFIX2STR_BUFFER];
390 prefix2str(ifc->address, buf, sizeof(buf));
391 zlog_debug("Rx Intf address add VRF %u IF %s addr %s",
392 vrf_id, ifc->ifp->name, buf);
393 }
394
395 if (if_is_operative (ifc->ifp))
396 {
397 struct bgp *bgp;
398
399 bgp = bgp_lookup_by_vrf_id (vrf_id);
400 if (!bgp)
401 return 0;
402
403 bgp_connected_add (bgp, ifc);
404 /* If we have learnt of any neighbors on this interface,
405 * check to kick off any BGP interface-based neighbors,
406 * but only if this is a link-local address.
407 */
408 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6) &&
409 !list_isempty(ifc->ifp->nbr_connected))
410 bgp_start_interface_nbrs (bgp, ifc->ifp);
411 }
412
413 return 0;
414 }
415
416 static int
417 bgp_interface_address_delete (int command, struct zclient *zclient,
418 zebra_size_t length, vrf_id_t vrf_id)
419 {
420 struct connected *ifc;
421 struct bgp *bgp;
422
423 ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
424
425 if (ifc == NULL)
426 return 0;
427
428 if (bgp_debug_zebra(ifc->address))
429 {
430 char buf[PREFIX2STR_BUFFER];
431 prefix2str(ifc->address, buf, sizeof(buf));
432 zlog_debug("Rx Intf address del VRF %u IF %s addr %s",
433 vrf_id, ifc->ifp->name, buf);
434 }
435
436 if (if_is_operative (ifc->ifp))
437 {
438 bgp = bgp_lookup_by_vrf_id (vrf_id);
439 if (bgp)
440 bgp_connected_delete (bgp, ifc);
441 }
442
443 connected_free (ifc);
444
445 return 0;
446 }
447
448 static int
449 bgp_interface_nbr_address_add (int command, struct zclient *zclient,
450 zebra_size_t length, vrf_id_t vrf_id)
451 {
452 struct nbr_connected *ifc = NULL;
453 struct bgp *bgp;
454
455 ifc = zebra_interface_nbr_address_read (command, zclient->ibuf, vrf_id);
456
457 if (ifc == NULL)
458 return 0;
459
460 if (bgp_debug_zebra(ifc->address))
461 {
462 char buf[PREFIX2STR_BUFFER];
463 prefix2str(ifc->address, buf, sizeof(buf));
464 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s",
465 vrf_id, ifc->ifp->name, buf);
466 }
467
468 if (if_is_operative (ifc->ifp))
469 {
470 bgp = bgp_lookup_by_vrf_id (vrf_id);
471 if (bgp)
472 bgp_nbr_connected_add (bgp, ifc);
473 }
474
475 return 0;
476 }
477
478 static int
479 bgp_interface_nbr_address_delete (int command, struct zclient *zclient,
480 zebra_size_t length, vrf_id_t vrf_id)
481 {
482 struct nbr_connected *ifc = NULL;
483 struct bgp *bgp;
484
485 ifc = zebra_interface_nbr_address_read (command, zclient->ibuf, vrf_id);
486
487 if (ifc == NULL)
488 return 0;
489
490 if (bgp_debug_zebra(ifc->address))
491 {
492 char buf[PREFIX2STR_BUFFER];
493 prefix2str(ifc->address, buf, sizeof(buf));
494 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s",
495 vrf_id, ifc->ifp->name, buf);
496 }
497
498 if (if_is_operative (ifc->ifp))
499 {
500 bgp = bgp_lookup_by_vrf_id (vrf_id);
501 if (bgp)
502 bgp_nbr_connected_delete (bgp, ifc, 0);
503 }
504
505 nbr_connected_free (ifc);
506
507 return 0;
508 }
509
510 /* VRF update for an interface. */
511 static int
512 bgp_interface_vrf_update (int command, struct zclient *zclient, zebra_size_t length,
513 vrf_id_t vrf_id)
514 {
515 struct interface *ifp;
516 vrf_id_t new_vrf_id;
517 struct connected *c;
518 struct nbr_connected *nc;
519 struct listnode *node, *nnode;
520 struct bgp *bgp;
521
522 ifp = zebra_interface_vrf_update_read (zclient->ibuf, vrf_id, &new_vrf_id);
523 if (! ifp)
524 return 0;
525
526 if (BGP_DEBUG (zebra, ZEBRA) && ifp)
527 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u",
528 vrf_id, ifp->name, new_vrf_id);
529
530 bgp = bgp_lookup_by_vrf_id (vrf_id);
531 if (!bgp)
532 return 0;
533
534 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
535 bgp_connected_delete (bgp, c);
536
537 for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
538 bgp_nbr_connected_delete (bgp, nc, 1);
539
540 /* Fast external-failover */
541 {
542 struct peer *peer;
543
544 if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
545 return 0;
546
547 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
548 {
549 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
550 continue;
551
552 if (ifp == peer->nexthop.ifp)
553 BGP_EVENT_ADD (peer, BGP_Stop);
554 }
555 }
556
557 if_update_vrf (ifp, ifp->name, strlen (ifp->name), new_vrf_id);
558
559 bgp = bgp_lookup_by_vrf_id (new_vrf_id);
560 if (!bgp)
561 return 0;
562
563 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
564 bgp_connected_add (bgp, c);
565
566 for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
567 bgp_nbr_connected_add (bgp, nc);
568 return 0;
569 }
570
571 /* Zebra route add and delete treatment. */
572 static int
573 zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length,
574 vrf_id_t vrf_id)
575 {
576 struct stream *s;
577 struct zapi_ipv4 api;
578 struct in_addr nexthop;
579 struct prefix_ipv4 p;
580 unsigned int ifindex;
581 int i;
582 struct bgp *bgp;
583
584 bgp = bgp_lookup_by_vrf_id (vrf_id);
585 if (!bgp)
586 return 0;
587
588 s = zclient->ibuf;
589 nexthop.s_addr = 0;
590
591 /* Type, flags, message. */
592 api.type = stream_getc (s);
593 api.instance = stream_getw (s);
594 api.flags = stream_getc (s);
595 api.message = stream_getc (s);
596
597 /* IPv4 prefix. */
598 memset (&p, 0, sizeof (struct prefix_ipv4));
599 p.family = AF_INET;
600 p.prefixlen = stream_getc (s);
601 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
602
603 /* Nexthop, ifindex, distance, metric. */
604 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
605 {
606 api.nexthop_num = stream_getc (s);
607 nexthop.s_addr = stream_get_ipv4 (s);
608 }
609
610 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
611 {
612 api.ifindex_num = stream_getc (s);
613 ifindex = stream_getl (s); /* ifindex, unused */
614 }
615 else
616 {
617 ifindex = 0;
618 }
619
620 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
621 api.distance = stream_getc (s);
622
623 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
624 api.metric = stream_getl (s);
625 else
626 api.metric = 0;
627
628 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
629 api.tag = stream_getw (s);
630 else
631 api.tag = 0;
632
633 if (command == ZEBRA_REDISTRIBUTE_IPV4_ADD)
634 {
635 if (bgp_debug_zebra((struct prefix *)&p))
636 {
637 char buf[2][INET_ADDRSTRLEN];
638 zlog_debug("Rx IPv4 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
639 vrf_id,
640 zebra_route_string(api.type), api.instance,
641 inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
642 p.prefixlen,
643 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
644 api.metric,
645 api.tag);
646 }
647
648 /*
649 * The ADD message is actually an UPDATE and there is no explicit DEL
650 * for a prior redistributed route, if any. So, perform an implicit
651 * DEL processing for the same redistributed route from any other
652 * source type.
653 */
654 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
655 {
656 if (i != api.type)
657 bgp_redistribute_delete(bgp, (struct prefix *)&p, i, api.instance);
658 }
659
660 /* Now perform the add/update. */
661 bgp_redistribute_add(bgp, (struct prefix *)&p, &nexthop, NULL, ifindex,
662 api.metric, api.type, api.instance, api.tag);
663 }
664 else if (command == ZEBRA_REDISTRIBUTE_IPV4_DEL)
665 {
666 if (bgp_debug_zebra((struct prefix *)&p))
667 {
668 char buf[2][INET_ADDRSTRLEN];
669 zlog_debug("Rx IPv4 route delete VRF %u %s[%d] %s/%d "
670 "nexthop %s metric %u tag %d",
671 vrf_id,
672 zebra_route_string(api.type), api.instance,
673 inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
674 p.prefixlen,
675 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
676 api.metric,
677 api.tag);
678 }
679 bgp_redistribute_delete(bgp, (struct prefix *)&p, api.type, api.instance);
680 }
681
682 return 0;
683 }
684
685 #ifdef HAVE_IPV6
686 /* Zebra route add and delete treatment. */
687 static int
688 zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length,
689 vrf_id_t vrf_id)
690 {
691 struct stream *s;
692 struct zapi_ipv6 api;
693 struct in6_addr nexthop;
694 struct prefix_ipv6 p;
695 unsigned int ifindex;
696 int i;
697 struct bgp *bgp;
698
699 bgp = bgp_lookup_by_vrf_id (vrf_id);
700 if (!bgp)
701 return 0;
702
703 s = zclient->ibuf;
704 memset (&nexthop, 0, sizeof (struct in6_addr));
705
706 /* Type, flags, message. */
707 api.type = stream_getc (s);
708 api.instance = stream_getw (s);
709 api.flags = stream_getc (s);
710 api.message = stream_getc (s);
711
712 /* IPv6 prefix. */
713 memset (&p, 0, sizeof (struct prefix_ipv6));
714 p.family = AF_INET6;
715 p.prefixlen = stream_getc (s);
716 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
717
718 /* Nexthop, ifindex, distance, metric. */
719 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
720 {
721 api.nexthop_num = stream_getc (s);
722 stream_get (&nexthop, s, 16);
723 }
724
725 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
726 {
727 api.ifindex_num = stream_getc (s);
728 ifindex = stream_getl (s); /* ifindex, unused */
729 }
730 else
731 {
732 ifindex = 0;
733 }
734
735 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
736 api.distance = stream_getc (s);
737 else
738 api.distance = 0;
739
740 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
741 api.metric = stream_getl (s);
742 else
743 api.metric = 0;
744
745 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
746 api.tag = stream_getw (s);
747 else
748 api.tag = 0;
749
750 /* Simply ignore link-local address. */
751 if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
752 return 0;
753
754 if (command == ZEBRA_REDISTRIBUTE_IPV6_ADD)
755 {
756 if (bgp_debug_zebra((struct prefix *)&p))
757 {
758 char buf[2][INET6_ADDRSTRLEN];
759 zlog_debug("Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
760 vrf_id,
761 zebra_route_string(api.type), api.instance,
762 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
763 p.prefixlen,
764 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
765 api.metric,
766 api.tag);
767 }
768
769 /*
770 * The ADD message is actually an UPDATE and there is no explicit DEL
771 * for a prior redistributed route, if any. So, perform an implicit
772 * DEL processing for the same redistributed route from any other
773 * source type.
774 */
775 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
776 {
777 if (i != api.type)
778 bgp_redistribute_delete(bgp, (struct prefix *)&p, i, api.instance);
779 }
780
781 bgp_redistribute_add (bgp, (struct prefix *)&p, NULL, &nexthop, ifindex,
782 api.metric, api.type, api.instance, api.tag);
783 }
784 else if (command == ZEBRA_REDISTRIBUTE_IPV6_DEL)
785 {
786 if (bgp_debug_zebra((struct prefix *)&p))
787 {
788 char buf[2][INET6_ADDRSTRLEN];
789 zlog_debug("Rx IPv6 route delete VRF %u %s[%d] %s/%d "
790 "nexthop %s metric %u tag %d",
791 vrf_id,
792 zebra_route_string(api.type), api.instance,
793 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
794 p.prefixlen,
795 inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
796 api.metric,
797 api.tag);
798 }
799 bgp_redistribute_delete (bgp, (struct prefix *) &p, api.type, api.instance);
800 }
801
802 return 0;
803 }
804 #endif /* HAVE_IPV6 */
805
806 struct interface *
807 if_lookup_by_ipv4 (struct in_addr *addr, vrf_id_t vrf_id)
808 {
809 struct listnode *ifnode;
810 struct listnode *cnode;
811 struct interface *ifp;
812 struct connected *connected;
813 struct prefix_ipv4 p;
814 struct prefix *cp;
815
816 p.family = AF_INET;
817 p.prefix = *addr;
818 p.prefixlen = IPV4_MAX_BITLEN;
819
820 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
821 {
822 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
823 {
824 cp = connected->address;
825
826 if (cp->family == AF_INET)
827 if (prefix_match (cp, (struct prefix *)&p))
828 return ifp;
829 }
830 }
831 return NULL;
832 }
833
834 struct interface *
835 if_lookup_by_ipv4_exact (struct in_addr *addr, vrf_id_t vrf_id)
836 {
837 struct listnode *ifnode;
838 struct listnode *cnode;
839 struct interface *ifp;
840 struct connected *connected;
841 struct prefix *cp;
842
843 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
844 {
845 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
846 {
847 cp = connected->address;
848
849 if (cp->family == AF_INET)
850 if (IPV4_ADDR_SAME (&cp->u.prefix4, addr))
851 return ifp;
852 }
853 }
854 return NULL;
855 }
856
857 #ifdef HAVE_IPV6
858 struct interface *
859 if_lookup_by_ipv6 (struct in6_addr *addr, unsigned int ifindex, vrf_id_t vrf_id)
860 {
861 struct listnode *ifnode;
862 struct listnode *cnode;
863 struct interface *ifp;
864 struct connected *connected;
865 struct prefix_ipv6 p;
866 struct prefix *cp;
867
868 p.family = AF_INET6;
869 p.prefix = *addr;
870 p.prefixlen = IPV6_MAX_BITLEN;
871
872 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
873 {
874 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
875 {
876 cp = connected->address;
877
878 if (cp->family == AF_INET6)
879 if (prefix_match (cp, (struct prefix *)&p))
880 {
881 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6))
882 {
883 if (ifindex == ifp->ifindex)
884 return ifp;
885 }
886 else
887 return ifp;
888 }
889 }
890 }
891 return NULL;
892 }
893
894 struct interface *
895 if_lookup_by_ipv6_exact (struct in6_addr *addr, unsigned int ifindex, vrf_id_t vrf_id)
896 {
897 struct listnode *ifnode;
898 struct listnode *cnode;
899 struct interface *ifp;
900 struct connected *connected;
901 struct prefix *cp;
902
903 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
904 {
905 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
906 {
907 cp = connected->address;
908
909 if (cp->family == AF_INET6)
910 if (IPV6_ADDR_SAME (&cp->u.prefix6, addr))
911 {
912 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6))
913 {
914 if (ifindex == ifp->ifindex)
915 return ifp;
916 }
917 else
918 return ifp;
919 }
920 }
921 }
922 return NULL;
923 }
924
925 static int
926 if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
927 {
928 struct listnode *cnode;
929 struct connected *connected;
930 struct prefix *cp;
931
932 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
933 {
934 cp = connected->address;
935
936 if (cp->family == AF_INET6)
937 if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
938 {
939 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
940 return 1;
941 }
942 }
943 return 0;
944 }
945
946 static int
947 if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
948 {
949 struct listnode *cnode;
950 struct connected *connected;
951 struct prefix *cp;
952
953 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
954 {
955 cp = connected->address;
956
957 if (cp->family == AF_INET6)
958 if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
959 {
960 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
961 return 1;
962 }
963 }
964 return 0;
965 }
966 #endif /* HAVE_IPV6 */
967
968 static int
969 if_get_ipv4_address (struct interface *ifp, struct in_addr *addr)
970 {
971 struct listnode *cnode;
972 struct connected *connected;
973 struct prefix *cp;
974
975 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
976 {
977 cp = connected->address;
978 if ((cp->family == AF_INET) && !ipv4_martian(&(cp->u.prefix4)))
979 {
980 *addr = cp->u.prefix4;
981 return 1;
982 }
983 }
984 return 0;
985 }
986
987 int
988 bgp_nexthop_set (union sockunion *local, union sockunion *remote,
989 struct bgp_nexthop *nexthop, struct peer *peer)
990 {
991 int ret = 0;
992 struct interface *ifp = NULL;
993
994 memset (nexthop, 0, sizeof (struct bgp_nexthop));
995
996 if (!local)
997 return -1;
998 if (!remote)
999 return -1;
1000
1001 if (local->sa.sa_family == AF_INET)
1002 {
1003 nexthop->v4 = local->sin.sin_addr;
1004 if (peer->update_if)
1005 ifp = if_lookup_by_name_vrf (peer->update_if, peer->bgp->vrf_id);
1006 else
1007 ifp = if_lookup_by_ipv4_exact (&local->sin.sin_addr, peer->bgp->vrf_id);
1008 }
1009 #ifdef HAVE_IPV6
1010 if (local->sa.sa_family == AF_INET6)
1011 {
1012 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
1013 {
1014 if (peer->conf_if || peer->ifname)
1015 ifp = if_lookup_by_name_vrf (peer->conf_if ? peer->conf_if : peer->ifname, peer->bgp->vrf_id);
1016 }
1017 else if (peer->update_if)
1018 ifp = if_lookup_by_name_vrf (peer->update_if, peer->bgp->vrf_id);
1019 else
1020 ifp = if_lookup_by_ipv6_exact (&local->sin6.sin6_addr,
1021 local->sin6.sin6_scope_id,
1022 peer->bgp->vrf_id);
1023 }
1024 #endif /* HAVE_IPV6 */
1025
1026 if (!ifp)
1027 return -1;
1028
1029 nexthop->ifp = ifp;
1030
1031 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
1032 if (local->sa.sa_family == AF_INET)
1033 {
1034 #ifdef HAVE_IPV6
1035 /* IPv6 nexthop*/
1036 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
1037
1038 if (!ret)
1039 {
1040 /* There is no global nexthop. Use link-local address as both the
1041 * global and link-local nexthop. In this scenario, the expectation
1042 * for interop is that the network admin would use a route-map to
1043 * specify the global IPv6 nexthop.
1044 */
1045 if_get_ipv6_local (ifp, &nexthop->v6_global);
1046 memcpy (&nexthop->v6_local, &nexthop->v6_global,
1047 IPV6_MAX_BYTELEN);
1048 }
1049 else
1050 if_get_ipv6_local (ifp, &nexthop->v6_local);
1051
1052 if (if_lookup_by_ipv4 (&remote->sin.sin_addr, peer->bgp->vrf_id))
1053 peer->shared_network = 1;
1054 else
1055 peer->shared_network = 0;
1056 #endif /* HAVE_IPV6 */
1057 }
1058
1059 #ifdef HAVE_IPV6
1060 /* IPv6 connection, fetch and store IPv4 local address if any. */
1061 if (local->sa.sa_family == AF_INET6)
1062 {
1063 struct interface *direct = NULL;
1064
1065 /* IPv4 nexthop. */
1066 ret = if_get_ipv4_address(ifp, &nexthop->v4);
1067 if (!ret && peer->local_id.s_addr)
1068 nexthop->v4 = peer->local_id;
1069
1070 /* Global address*/
1071 if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
1072 {
1073 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
1074 IPV6_MAX_BYTELEN);
1075
1076 /* If directory connected set link-local address. */
1077 direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr,
1078 remote->sin6.sin6_scope_id, peer->bgp->vrf_id);
1079 if (direct)
1080 if_get_ipv6_local (ifp, &nexthop->v6_local);
1081 }
1082 else
1083 /* Link-local address. */
1084 {
1085 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
1086
1087 /* If there is no global address. Set link-local address as
1088 global. I know this break RFC specification... */
1089 /* In this scenario, the expectation for interop is that the
1090 * network admin would use a route-map to specify the global
1091 * IPv6 nexthop.
1092 */
1093 if (!ret)
1094 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
1095 IPV6_MAX_BYTELEN);
1096 /* Always set the link-local address */
1097 memcpy (&nexthop->v6_local, &local->sin6.sin6_addr,
1098 IPV6_MAX_BYTELEN);
1099 }
1100
1101 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) ||
1102 if_lookup_by_ipv6 (&remote->sin6.sin6_addr, remote->sin6.sin6_scope_id,
1103 peer->bgp->vrf_id))
1104 peer->shared_network = 1;
1105 else
1106 peer->shared_network = 0;
1107 }
1108
1109 /* KAME stack specific treatment. */
1110 #ifdef KAME
1111 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global)
1112 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global))
1113 {
1114 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0);
1115 }
1116 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local)
1117 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local))
1118 {
1119 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0);
1120 }
1121 #endif /* KAME */
1122 #endif /* HAVE_IPV6 */
1123
1124 /* If we have identified the local interface, there is no error for now. */
1125 return 0;
1126 }
1127
1128 static struct in6_addr *
1129 bgp_info_to_ipv6_nexthop (struct bgp_info *info)
1130 {
1131 struct in6_addr *nexthop = NULL;
1132
1133 /* Only global address nexthop exists. */
1134 if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL)
1135 nexthop = &info->attr->extra->mp_nexthop_global;
1136
1137 /* If both global and link-local address present. */
1138 if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1139 {
1140 /* Check if route-map is set to prefer global over link-local */
1141 if (info->attr->extra->mp_nexthop_prefer_global)
1142 nexthop = &info->attr->extra->mp_nexthop_global;
1143 else
1144 {
1145 /* Workaround for Cisco's nexthop bug. */
1146 if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global)
1147 && info->peer->su_remote->sa.sa_family == AF_INET6)
1148 nexthop = &info->peer->su_remote->sin6.sin6_addr;
1149 else
1150 nexthop = &info->attr->extra->mp_nexthop_local;
1151 }
1152 }
1153
1154 return nexthop;
1155 }
1156
1157 static int
1158 bgp_table_map_apply (struct route_map *map, struct prefix *p,
1159 struct bgp_info *info)
1160 {
1161 if (route_map_apply(map, p, RMAP_BGP, info) != RMAP_DENYMATCH)
1162 return 1;
1163
1164 if (bgp_debug_zebra(p))
1165 {
1166 if (p->family == AF_INET)
1167 {
1168 char buf[2][INET_ADDRSTRLEN];
1169 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1170 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1171 p->prefixlen,
1172 inet_ntop(AF_INET, &info->attr->nexthop, buf[1],
1173 sizeof(buf[1])));
1174 }
1175 if (p->family == AF_INET6)
1176 {
1177 char buf[2][INET6_ADDRSTRLEN];
1178 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1179 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1180 p->prefixlen,
1181 inet_ntop(AF_INET6, bgp_info_to_ipv6_nexthop(info), buf[1],
1182 sizeof(buf[1])));
1183 }
1184 }
1185 return 0;
1186 }
1187
1188 void
1189 bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp,
1190 afi_t afi, safi_t safi)
1191 {
1192 int flags;
1193 u_char distance;
1194 struct peer *peer;
1195 struct bgp_info *mpinfo;
1196 size_t oldsize, newsize;
1197 u_int32_t nhcount, metric;
1198 struct bgp_info local_info;
1199 struct bgp_info *info_cp = &local_info;
1200 u_short tag;
1201
1202 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1203 * know of this instance.
1204 */
1205 if (!bgp_install_info_to_zebra (bgp))
1206 return;
1207
1208 if ((p->family == AF_INET &&
1209 !vrf_bitmap_check (zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP], bgp->vrf_id))
1210 || (p->family == AF_INET6 &&
1211 !vrf_bitmap_check (zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP], bgp->vrf_id)))
1212 return;
1213
1214 if (bgp->main_zebra_update_hold)
1215 return;
1216
1217 flags = 0;
1218 peer = info->peer;
1219
1220 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1221 tag = info->attr->extra->tag;
1222 else
1223 tag = 0;
1224
1225 /* When we create an aggregate route we must also install a Null0 route in
1226 * the RIB */
1227 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1228 SET_FLAG (flags, ZEBRA_FLAG_BLACKHOLE);
1229
1230 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED ||
1231 info->sub_type == BGP_ROUTE_AGGREGATE)
1232 {
1233 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
1234 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1235 }
1236
1237 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1238 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1239 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1240
1241 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1242
1243 nhcount = 1 + bgp_info_mpath_count (info);
1244
1245 if (p->family == AF_INET && !BGP_ATTR_NEXTHOP_AFI_IP6(info->attr))
1246 {
1247 struct zapi_ipv4 api;
1248 struct in_addr *nexthop;
1249 char buf[2][INET_ADDRSTRLEN];
1250 int valid_nh_count = 0;
1251
1252 /* resize nexthop buffer size if necessary */
1253 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
1254 (sizeof (struct in_addr *) * nhcount))
1255 {
1256 newsize = (sizeof (struct in_addr *) * nhcount);
1257 newsize = stream_resize (bgp_nexthop_buf, newsize);
1258 if (newsize == oldsize)
1259 {
1260 zlog_err ("can't resize nexthop buffer");
1261 return;
1262 }
1263 }
1264 stream_reset (bgp_nexthop_buf);
1265 nexthop = NULL;
1266
1267 /* Metric is currently based on the best-path only. */
1268 metric = info->attr->med;
1269
1270 if (bgp->table_map[afi][safi].name)
1271 {
1272 BGP_INFO_ATTR_BUF_INIT();
1273
1274 /* Copy info and attributes, so the route-map apply doesn't modify the
1275 BGP route info. */
1276 BGP_INFO_ATTR_BUF_COPY(info, info_cp);
1277 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1278 {
1279 metric = info_cp->attr->med;
1280 nexthop = &info_cp->attr->nexthop;
1281
1282 if (info_cp->attr->extra)
1283 tag = info_cp->attr->extra->tag;
1284 }
1285 BGP_INFO_ATTR_BUF_FREE(info_cp);
1286 }
1287 else
1288 {
1289 nexthop = &info->attr->nexthop;
1290 }
1291
1292 if (nexthop)
1293 {
1294 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
1295 valid_nh_count++;
1296 }
1297
1298 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
1299 mpinfo = bgp_info_mpath_next (mpinfo))
1300 {
1301 nexthop = NULL;
1302
1303 if (bgp->table_map[afi][safi].name)
1304 {
1305 /* Copy info and attributes, so the route-map apply doesn't modify the
1306 BGP route info. */
1307 BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
1308 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1309 nexthop = &info_cp->attr->nexthop;
1310 BGP_INFO_ATTR_BUF_FREE(info_cp);
1311 }
1312 else
1313 {
1314 nexthop = &mpinfo->attr->nexthop;
1315 }
1316
1317 if (nexthop == NULL)
1318 continue;
1319
1320 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
1321 valid_nh_count++;
1322 }
1323
1324 api.vrf_id = bgp->vrf_id;
1325 api.flags = flags;
1326 api.type = ZEBRA_ROUTE_BGP;
1327 api.instance = 0;
1328 api.message = 0;
1329 api.safi = safi;
1330 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1331
1332 /* Note that this currently only applies to Null0 routes for aggregates.
1333 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
1334 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1335 * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
1336 */
1337 if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
1338 api.nexthop_num = 0;
1339 else
1340 api.nexthop_num = valid_nh_count;
1341
1342 api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf);
1343 api.ifindex_num = 0;
1344 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1345 api.metric = metric;
1346 api.tag = 0;
1347
1348 if (tag)
1349 {
1350 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
1351 api.tag = tag;
1352 }
1353
1354 distance = bgp_distance_apply (p, info, bgp);
1355
1356 if (distance)
1357 {
1358 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
1359 api.distance = distance;
1360 }
1361
1362 if (bgp_debug_zebra(p))
1363 {
1364 int i;
1365 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d"
1366 " count %d", (valid_nh_count ? "add":"delete"),
1367 bgp->vrf_id,
1368 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1369 p->prefixlen, api.metric, api.tag, api.nexthop_num);
1370 for (i = 0; i < api.nexthop_num; i++)
1371 zlog_debug(" IPv4 [nexthop %d] %s", i+1,
1372 inet_ntop(AF_INET, api.nexthop[i], buf[1], sizeof(buf[1])));
1373 }
1374
1375 zapi_ipv4_route (valid_nh_count ? ZEBRA_IPV4_ROUTE_ADD: ZEBRA_IPV4_ROUTE_DELETE,
1376 zclient, (struct prefix_ipv4 *) p, &api);
1377 }
1378 #ifdef HAVE_IPV6
1379
1380 /* We have to think about a IPv6 link-local address curse. */
1381 if (p->family == AF_INET6 ||
1382 (p->family == AF_INET && BGP_ATTR_NEXTHOP_AFI_IP6(info->attr)))
1383 {
1384 unsigned int ifindex;
1385 struct in6_addr *nexthop;
1386 struct zapi_ipv6 api;
1387 int valid_nh_count = 0;
1388 char buf[2][INET6_ADDRSTRLEN];
1389
1390 /* resize nexthop buffer size if necessary */
1391 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
1392 (sizeof (struct in6_addr *) * nhcount))
1393 {
1394 newsize = (sizeof (struct in6_addr *) * nhcount);
1395 newsize = stream_resize (bgp_nexthop_buf, newsize);
1396 if (newsize == oldsize)
1397 {
1398 zlog_err ("can't resize nexthop buffer");
1399 return;
1400 }
1401 }
1402 stream_reset (bgp_nexthop_buf);
1403
1404 /* resize ifindices buffer size if necessary */
1405 if ((oldsize = stream_get_size (bgp_ifindices_buf)) <
1406 (sizeof (unsigned int) * nhcount))
1407 {
1408 newsize = (sizeof (unsigned int) * nhcount);
1409 newsize = stream_resize (bgp_ifindices_buf, newsize);
1410 if (newsize == oldsize)
1411 {
1412 zlog_err ("can't resize nexthop buffer");
1413 return;
1414 }
1415 }
1416 stream_reset (bgp_ifindices_buf);
1417
1418 ifindex = 0;
1419 nexthop = NULL;
1420
1421 assert (info->attr->extra);
1422
1423 /* Metric is currently based on the best-path only. */
1424 metric = info->attr->med;
1425
1426 if (bgp->table_map[afi][safi].name)
1427 {
1428 BGP_INFO_ATTR_BUF_INIT();
1429
1430 /* Copy info and attributes, so the route-map apply doesn't modify the
1431 BGP route info. */
1432 BGP_INFO_ATTR_BUF_COPY(info, info_cp);
1433 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1434 {
1435 metric = info_cp->attr->med;
1436 nexthop = bgp_info_to_ipv6_nexthop(info_cp);
1437
1438 if (info_cp->attr->extra)
1439 tag = info_cp->attr->extra->tag;
1440 }
1441 BGP_INFO_ATTR_BUF_FREE(info_cp);
1442 }
1443 else
1444 {
1445 nexthop = bgp_info_to_ipv6_nexthop(info);
1446 }
1447
1448 if (nexthop)
1449 {
1450 if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1451 if (info->peer->nexthop.ifp)
1452 ifindex = info->peer->nexthop.ifp->ifindex;
1453
1454 if (!ifindex)
1455 {
1456 if (info->peer->conf_if || info->peer->ifname)
1457 ifindex = if_nametoindex (info->peer->conf_if ? info->peer->conf_if : info->peer->ifname);
1458 else if (info->peer->nexthop.ifp)
1459 ifindex = info->peer->nexthop.ifp->ifindex;
1460 }
1461 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
1462 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
1463 valid_nh_count++;
1464 }
1465
1466 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
1467 mpinfo = bgp_info_mpath_next (mpinfo))
1468 {
1469 ifindex = 0;
1470 nexthop = NULL;
1471
1472 if (bgp->table_map[afi][safi].name)
1473 {
1474 /* Copy info and attributes, so the route-map apply doesn't modify the
1475 BGP route info. */
1476 BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
1477 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1478 nexthop = bgp_info_to_ipv6_nexthop(info_cp);
1479 BGP_INFO_ATTR_BUF_FREE(info_cp);
1480 }
1481 else
1482 {
1483 nexthop = bgp_info_to_ipv6_nexthop(mpinfo);
1484 }
1485
1486 if (nexthop == NULL)
1487 continue;
1488
1489 if (mpinfo->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1490 if (mpinfo->peer->nexthop.ifp)
1491 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
1492
1493 if (!ifindex)
1494 {
1495 if (mpinfo->peer->conf_if || mpinfo->peer->ifname)
1496 ifindex = ifname2ifindex (mpinfo->peer->conf_if ? mpinfo->peer->conf_if : mpinfo->peer->ifname);
1497 else if (mpinfo->peer->nexthop.ifp)
1498 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
1499 }
1500 if (ifindex == 0)
1501 continue;
1502
1503 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
1504 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
1505 valid_nh_count++;
1506 }
1507
1508 /* Make Zebra API structure. */
1509 api.vrf_id = bgp->vrf_id;
1510 api.flags = flags;
1511 api.type = ZEBRA_ROUTE_BGP;
1512 api.instance = 0;
1513 api.message = 0;
1514 api.safi = safi;
1515 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1516
1517 /* Note that this currently only applies to Null0 routes for aggregates.
1518 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a special
1519 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1520 * do not want to also encode the :: nexthop for the aggregate route.
1521 */
1522 if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
1523 api.nexthop_num = 0;
1524 else
1525 api.nexthop_num = valid_nh_count;
1526
1527 api.nexthop = (struct in6_addr **)STREAM_DATA (bgp_nexthop_buf);
1528 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
1529 api.ifindex_num = valid_nh_count;
1530 api.ifindex = (unsigned int *)STREAM_DATA (bgp_ifindices_buf);
1531 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1532 api.metric = metric;
1533 api.tag = 0;
1534
1535 if (tag)
1536 {
1537 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
1538 api.tag = tag;
1539 }
1540
1541 if (p->family == AF_INET)
1542 {
1543 if (bgp_debug_zebra(p))
1544 {
1545 int i;
1546 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d",
1547 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1548 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1549 p->prefixlen, api.metric, api.tag);
1550 for (i = 0; i < api.nexthop_num; i++)
1551 zlog_debug(" IPv6 [nexthop %d] %s", i+1,
1552 inet_ntop(AF_INET6, api.nexthop[i], buf[1], sizeof(buf[1])));
1553 }
1554
1555 if (valid_nh_count)
1556 zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD,
1557 zclient, (struct prefix_ipv4 *) p,
1558 (struct zapi_ipv6 *)&api);
1559 else
1560 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE,
1561 zclient, (struct prefix_ipv4 *) p, (struct zapi_ipv4 *)&api);
1562 }
1563 else
1564 {
1565 if (bgp_debug_zebra(p))
1566 {
1567 int i;
1568 zlog_debug("Tx IPv6 route %s VRF %u %s/%d metric %u tag %d",
1569 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1570 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1571 p->prefixlen, api.metric, api.tag);
1572 for (i = 0; i < api.nexthop_num; i++)
1573 zlog_debug(" IPv6 [nexthop %d] %s", i+1,
1574 inet_ntop(AF_INET6, api.nexthop[i], buf[1], sizeof(buf[1])));
1575 }
1576
1577 zapi_ipv6_route (valid_nh_count ?
1578 ZEBRA_IPV6_ROUTE_ADD : ZEBRA_IPV6_ROUTE_DELETE,
1579 zclient, (struct prefix_ipv6 *) p, &api);
1580 }
1581 }
1582 #endif /* HAVE_IPV6 */
1583 }
1584
1585 /* Announce all routes of a table to zebra */
1586 void
1587 bgp_zebra_announce_table (struct bgp *bgp, afi_t afi, safi_t safi)
1588 {
1589 struct bgp_node *rn;
1590 struct bgp_table *table;
1591 struct bgp_info *ri;
1592
1593 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1594 * know of this instance.
1595 */
1596 if (!bgp_install_info_to_zebra (bgp))
1597 return;
1598
1599 table = bgp->rib[afi][safi];
1600 if (!table) return;
1601
1602 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
1603 for (ri = rn->info; ri; ri = ri->next)
1604 if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)
1605 && ri->type == ZEBRA_ROUTE_BGP
1606 && ri->sub_type == BGP_ROUTE_NORMAL)
1607 bgp_zebra_announce (&rn->p, ri, bgp, afi, safi);
1608 }
1609
1610 void
1611 bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi)
1612 {
1613 int flags;
1614 struct peer *peer;
1615
1616 peer = info->peer;
1617 assert(peer);
1618
1619 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1620 * know of this instance.
1621 */
1622 if (!bgp_install_info_to_zebra (peer->bgp))
1623 return;
1624
1625 if ((p->family == AF_INET &&
1626 !vrf_bitmap_check (zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP], peer->bgp->vrf_id))
1627 || (p->family == AF_INET6 &&
1628 !vrf_bitmap_check (zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP], peer->bgp->vrf_id)))
1629 return;
1630
1631 flags = 0;
1632
1633 if (peer->sort == BGP_PEER_IBGP)
1634 {
1635 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1636 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
1637 }
1638
1639 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1640 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1641 || bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1642 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1643
1644 if (p->family == AF_INET)
1645 {
1646 struct zapi_ipv4 api;
1647
1648 api.vrf_id = peer->bgp->vrf_id;
1649 api.flags = flags;
1650
1651 api.type = ZEBRA_ROUTE_BGP;
1652 api.instance = 0;
1653 api.message = 0;
1654 api.safi = safi;
1655 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1656 api.nexthop_num = 0;
1657 api.nexthop = NULL;
1658 api.ifindex_num = 0;
1659 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1660 api.metric = info->attr->med;
1661 api.tag = 0;
1662
1663 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1664 {
1665 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1666 api.tag = info->attr->extra->tag;
1667 }
1668
1669 if (bgp_debug_zebra(p))
1670 {
1671 char buf[2][INET_ADDRSTRLEN];
1672 zlog_debug("Tx IPv4 route delete VRF %u %s/%d metric %u tag %d",
1673 peer->bgp->vrf_id,
1674 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1675 p->prefixlen, api.metric, api.tag);
1676 }
1677
1678 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient,
1679 (struct prefix_ipv4 *) p, &api);
1680 }
1681 #ifdef HAVE_IPV6
1682 /* We have to think about a IPv6 link-local address curse. */
1683 if (p->family == AF_INET6)
1684 {
1685 struct zapi_ipv6 api;
1686
1687 assert (info->attr->extra);
1688
1689 api.vrf_id = peer->bgp->vrf_id;
1690 api.flags = flags;
1691 api.type = ZEBRA_ROUTE_BGP;
1692 api.instance = 0;
1693 api.message = 0;
1694 api.safi = safi;
1695 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1696 api.nexthop_num = 0;
1697 api.nexthop = NULL;
1698 api.ifindex_num = 0;
1699 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1700 api.metric = info->attr->med;
1701 api.tag = 0;
1702
1703 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1704 {
1705 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1706 api.tag = info->attr->extra->tag;
1707 }
1708
1709 if (bgp_debug_zebra(p))
1710 {
1711 char buf[2][INET6_ADDRSTRLEN];
1712 zlog_debug("Tx IPv6 route delete VRF %u %s/%d metric %u tag %d",
1713 peer->bgp->vrf_id,
1714 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1715 p->prefixlen, api.metric, api.tag);
1716 }
1717
1718 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient,
1719 (struct prefix_ipv6 *) p, &api);
1720 }
1721 #endif /* HAVE_IPV6 */
1722 }
1723 struct bgp_redist *
1724 bgp_redist_lookup (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
1725 {
1726 struct list *red_list;
1727 struct listnode *node;
1728 struct bgp_redist *red;
1729
1730 red_list = bgp->redist[afi][type];
1731 if (!red_list)
1732 return(NULL);
1733
1734 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1735 if (red->instance == instance)
1736 return red;
1737
1738 return NULL;
1739 }
1740
1741 struct bgp_redist *
1742 bgp_redist_add (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
1743 {
1744 struct list *red_list;
1745 struct bgp_redist *red;
1746
1747 red = bgp_redist_lookup(bgp, afi, type, instance);
1748 if (red)
1749 return red;
1750
1751 if (!bgp->redist[afi][type])
1752 bgp->redist[afi][type] = list_new();
1753
1754 red_list = bgp->redist[afi][type];
1755 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist));
1756 red->instance = instance;
1757
1758 listnode_add(red_list, red);
1759
1760 return red;
1761 }
1762
1763 static void
1764 bgp_redist_del (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
1765 {
1766 struct bgp_redist *red;
1767
1768 red = bgp_redist_lookup(bgp, afi, type, instance);
1769
1770 if (red)
1771 {
1772 listnode_delete(bgp->redist[afi][type], red);
1773 if (!bgp->redist[afi][type]->count)
1774 {
1775 list_free(bgp->redist[afi][type]);
1776 bgp->redist[afi][type] = NULL;
1777 }
1778 }
1779 }
1780
1781 /* Other routes redistribution into BGP. */
1782 int
1783 bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type, u_short instance)
1784 {
1785
1786 /* Return if already redistribute flag is set. */
1787 if (instance)
1788 {
1789 if (redist_check_instance(&zclient->mi_redist[afi][type], instance))
1790 return CMD_WARNING;
1791
1792 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1793 }
1794 else
1795 {
1796 if (vrf_bitmap_check (zclient->redist[afi][type], bgp->vrf_id))
1797 return CMD_WARNING;
1798
1799 vrf_bitmap_set (zclient->redist[afi][type], bgp->vrf_id);
1800 }
1801
1802 /* Don't try to register if we're not connected to Zebra or Zebra doesn't
1803 * know of this instance.
1804 */
1805 if (!bgp_install_info_to_zebra (bgp))
1806 return CMD_WARNING;
1807
1808 if (BGP_DEBUG (zebra, ZEBRA))
1809 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1810 bgp->vrf_id, afi,
1811 zebra_route_string(type), instance);
1812
1813 /* Send distribute add message to zebra. */
1814 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1815 instance, bgp->vrf_id);
1816
1817 return CMD_SUCCESS;
1818 }
1819
1820 int
1821 bgp_redistribute_resend (struct bgp *bgp, afi_t afi, int type, u_short instance)
1822 {
1823 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1824 * know of this instance.
1825 */
1826 if (!bgp_install_info_to_zebra (bgp))
1827 return -1;
1828
1829 if (BGP_DEBUG (zebra, ZEBRA))
1830 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1831 bgp->vrf_id, afi,
1832 zebra_route_string(type), instance);
1833
1834 /* Send distribute add message to zebra. */
1835 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1836 instance, bgp->vrf_id);
1837 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1838 instance, bgp->vrf_id);
1839
1840 return 0;
1841 }
1842
1843 /* Redistribute with route-map specification. */
1844 int
1845 bgp_redistribute_rmap_set (struct bgp_redist *red, const char *name)
1846 {
1847 if (red->rmap.name
1848 && (strcmp (red->rmap.name, name) == 0))
1849 return 0;
1850
1851 if (red->rmap.name)
1852 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1853 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1854 red->rmap.map = route_map_lookup_by_name (name);
1855
1856 return 1;
1857 }
1858
1859 /* Redistribute with metric specification. */
1860 int
1861 bgp_redistribute_metric_set (struct bgp *bgp, struct bgp_redist *red, afi_t afi,
1862 int type, u_int32_t metric)
1863 {
1864 struct bgp_node *rn;
1865 struct bgp_info *ri;
1866
1867 if (red->redist_metric_flag
1868 && red->redist_metric == metric)
1869 return 0;
1870
1871 red->redist_metric_flag = 1;
1872 red->redist_metric = metric;
1873
1874 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn; rn = bgp_route_next(rn))
1875 {
1876 for (ri = rn->info; ri; ri = ri->next)
1877 {
1878 if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE &&
1879 ri->type == type &&
1880 ri->instance == red->instance)
1881 {
1882 struct attr *old_attr;
1883 struct attr new_attr;
1884 struct attr_extra new_extra;
1885
1886 new_attr.extra = &new_extra;
1887 bgp_attr_dup (&new_attr, ri->attr);
1888 new_attr.med = red->redist_metric;
1889 old_attr = ri->attr;
1890 ri->attr = bgp_attr_intern (&new_attr);
1891 bgp_attr_unintern (&old_attr);
1892
1893 bgp_info_set_flag(rn, ri, BGP_INFO_ATTR_CHANGED);
1894 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1895 }
1896 }
1897 }
1898
1899 return 1;
1900 }
1901
1902 /* Unset redistribution. */
1903 int
1904 bgp_redistribute_unreg (struct bgp *bgp, afi_t afi, int type, u_short instance)
1905 {
1906 struct bgp_redist *red;
1907
1908 red = bgp_redist_lookup(bgp, afi, type, instance);
1909 if (!red)
1910 return CMD_SUCCESS;
1911
1912 /* Return if zebra connection is disabled. */
1913 if (instance)
1914 {
1915 if (!redist_check_instance(&zclient->mi_redist[afi][type], instance))
1916 return CMD_WARNING;
1917 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1918 }
1919 else
1920 {
1921 if (! vrf_bitmap_check (zclient->redist[afi][type], bgp->vrf_id))
1922 return CMD_WARNING;
1923 vrf_bitmap_unset (zclient->redist[afi][type], bgp->vrf_id);
1924 }
1925
1926 if (bgp_install_info_to_zebra (bgp))
1927 {
1928 /* Send distribute delete message to zebra. */
1929 if (BGP_DEBUG (zebra, ZEBRA))
1930 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1931 bgp->vrf_id, afi, zebra_route_string(type), instance);
1932 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type, instance,
1933 bgp->vrf_id);
1934 }
1935
1936 /* Withdraw redistributed routes from current BGP's routing table. */
1937 bgp_redistribute_withdraw (bgp, afi, type, instance);
1938
1939 return CMD_SUCCESS;
1940 }
1941
1942 /* Unset redistribution. */
1943 int
1944 bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type, u_short instance)
1945 {
1946 struct bgp_redist *red;
1947
1948 red = bgp_redist_lookup(bgp, afi, type, instance);
1949 if (!red)
1950 return CMD_SUCCESS;
1951
1952 bgp_redistribute_unreg(bgp, afi, type, instance);
1953
1954 /* Unset route-map. */
1955 if (red->rmap.name)
1956 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1957 red->rmap.name = NULL;
1958 red->rmap.map = NULL;
1959
1960 /* Unset metric. */
1961 red->redist_metric_flag = 0;
1962 red->redist_metric = 0;
1963
1964 bgp_redist_del(bgp, afi, type, instance);
1965
1966 return CMD_SUCCESS;
1967 }
1968
1969 /* Update redistribute vrf bitmap during triggers like
1970 restart networking or delete/add VRFs */
1971 void
1972 bgp_update_redist_vrf_bitmaps (struct bgp *bgp, vrf_id_t old_vrf_id)
1973 {
1974 int i;
1975 afi_t afi;
1976
1977 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1978 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1979 if (vrf_bitmap_check (zclient->redist[afi][i], old_vrf_id))
1980 {
1981 vrf_bitmap_unset (zclient->redist[afi][i], old_vrf_id);
1982 vrf_bitmap_set (zclient->redist[afi][i], bgp->vrf_id);
1983 }
1984 return;
1985 }
1986
1987 void
1988 bgp_zclient_reset (void)
1989 {
1990 zclient_reset (zclient);
1991 }
1992
1993 /* Register this instance with Zebra. Invoked upon connect (for
1994 * default instance) and when other VRFs are learnt (or created and
1995 * already learnt).
1996 */
1997 void
1998 bgp_zebra_instance_register (struct bgp *bgp)
1999 {
2000 /* Don't try to register if we're not connected to Zebra */
2001 if (!zclient || zclient->sock < 0)
2002 return;
2003
2004 if (BGP_DEBUG (zebra, ZEBRA))
2005 zlog_debug("Registering VRF %u", bgp->vrf_id);
2006
2007 /* Register for router-id, interfaces, redistributed routes. */
2008 zclient_send_reg_requests (zclient, bgp->vrf_id);
2009 }
2010
2011 /* Deregister this instance with Zebra. Invoked upon the instance
2012 * being deleted (default or VRF) and it is already registered.
2013 */
2014 void
2015 bgp_zebra_instance_deregister (struct bgp *bgp)
2016 {
2017 /* Don't try to deregister if we're not connected to Zebra */
2018 if (zclient->sock < 0)
2019 return;
2020
2021 if (BGP_DEBUG (zebra, ZEBRA))
2022 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
2023
2024 /* Deregister for router-id, interfaces, redistributed routes. */
2025 zclient_send_dereg_requests (zclient, bgp->vrf_id);
2026 }
2027
2028 void
2029 bgp_zebra_initiate_radv (struct bgp *bgp, struct peer *peer)
2030 {
2031 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
2032
2033 /* Don't try to initiate if we're not connected to Zebra */
2034 if (zclient->sock < 0)
2035 return;
2036
2037 if (BGP_DEBUG (zebra, ZEBRA))
2038 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id, peer->host);
2039
2040 zclient_send_interface_radv_req (zclient, bgp->vrf_id, peer->ifp, 1, ra_interval);
2041 }
2042
2043 void
2044 bgp_zebra_terminate_radv (struct bgp *bgp, struct peer *peer)
2045 {
2046 /* Don't try to terminate if we're not connected to Zebra */
2047 if (zclient->sock < 0)
2048 return;
2049
2050 if (BGP_DEBUG (zebra, ZEBRA))
2051 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id, peer->host);
2052
2053 zclient_send_interface_radv_req (zclient, bgp->vrf_id, peer->ifp, 0, 0);
2054 }
2055
2056 /* BGP has established connection with Zebra. */
2057 static void
2058 bgp_zebra_connected (struct zclient *zclient)
2059 {
2060 struct bgp *bgp;
2061
2062 /* At this point, we may or may not have BGP instances configured, but
2063 * we're only interested in the default VRF (others wouldn't have learnt
2064 * the VRF from Zebra yet.)
2065 */
2066 bgp = bgp_get_default();
2067 if (!bgp)
2068 return;
2069
2070 bgp_zebra_instance_register (bgp);
2071
2072 /* Send the client registration */
2073 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
2074
2075 /* TODO - What if we have peers and networks configured, do we have to
2076 * kick-start them?
2077 */
2078 }
2079
2080
2081 void
2082 bgp_zebra_init (struct thread_master *master)
2083 {
2084 /* Set default values. */
2085 zclient = zclient_new (master);
2086 zclient_init (zclient, ZEBRA_ROUTE_BGP, 0);
2087 zclient->zebra_connected = bgp_zebra_connected;
2088 zclient->router_id_update = bgp_router_id_update;
2089 zclient->interface_add = bgp_interface_add;
2090 zclient->interface_delete = bgp_interface_delete;
2091 zclient->interface_address_add = bgp_interface_address_add;
2092 zclient->interface_address_delete = bgp_interface_address_delete;
2093 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
2094 zclient->interface_nbr_address_delete = bgp_interface_nbr_address_delete;
2095 zclient->interface_vrf_update = bgp_interface_vrf_update;
2096 zclient->ipv4_route_add = zebra_read_ipv4;
2097 zclient->ipv4_route_delete = zebra_read_ipv4;
2098 zclient->redistribute_route_ipv4_add = zebra_read_ipv4;
2099 zclient->redistribute_route_ipv4_del = zebra_read_ipv4;
2100 zclient->interface_up = bgp_interface_up;
2101 zclient->interface_down = bgp_interface_down;
2102 zclient->ipv6_route_add = zebra_read_ipv6;
2103 zclient->ipv6_route_delete = zebra_read_ipv6;
2104 zclient->redistribute_route_ipv6_add = zebra_read_ipv6;
2105 zclient->redistribute_route_ipv6_del = zebra_read_ipv6;
2106 zclient->nexthop_update = bgp_read_nexthop_update;
2107 zclient->import_check_update = bgp_read_import_check_update;
2108
2109 bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
2110 bgp_ifindices_buf = stream_new(BGP_IFINDICES_BUF_SIZE);
2111 }
2112
2113 void
2114 bgp_zebra_destroy(void)
2115 {
2116 if (zclient == NULL)
2117 return;
2118 zclient_stop(zclient);
2119 zclient_free(zclient);
2120 zclient = NULL;
2121 }