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