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