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