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