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