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