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