]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_zebra.c
Merge remote-tracking branch 'origin/master' into pim_lib_work2
[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
718e3744 701/* Zebra route add and delete treatment. */
94f2b392 702static int
7076bb2f
FL
703zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length,
704 vrf_id_t vrf_id)
718e3744 705{
706 struct stream *s;
707 struct zapi_ipv6 api;
718e3744 708 struct in6_addr nexthop;
709 struct prefix_ipv6 p;
bc413143 710 unsigned int ifindex;
d289687f 711 int i;
6aeb9e78
DS
712 struct bgp *bgp;
713
714 bgp = bgp_lookup_by_vrf_id (vrf_id);
715 if (!bgp)
716 return 0;
718e3744 717
718 s = zclient->ibuf;
718e3744 719 memset (&nexthop, 0, sizeof (struct in6_addr));
720
721 /* Type, flags, message. */
722 api.type = stream_getc (s);
7c8ff89e 723 api.instance = stream_getw (s);
0fc452dc 724 api.flags = stream_getl (s);
718e3744 725 api.message = stream_getc (s);
726
727 /* IPv6 prefix. */
728 memset (&p, 0, sizeof (struct prefix_ipv6));
729 p.family = AF_INET6;
d9178828 730 p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, stream_getc (s));
718e3744 731 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
732
733 /* Nexthop, ifindex, distance, metric. */
734 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
735 {
736 api.nexthop_num = stream_getc (s);
737 stream_get (&nexthop, s, 16);
738 }
0d9551dc 739
718e3744 740 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
741 {
742 api.ifindex_num = stream_getc (s);
bc413143 743 ifindex = stream_getl (s); /* ifindex, unused */
718e3744 744 }
ffd0c037
DS
745 else
746 {
747 ifindex = 0;
748 }
0d9551dc 749
718e3744 750 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
751 api.distance = stream_getc (s);
752 else
753 api.distance = 0;
0d9551dc 754
718e3744 755 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
756 api.metric = stream_getl (s);
757 else
758 api.metric = 0;
759
0d9551dc 760 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
dc9ffce8 761 api.tag = stream_getl (s);
0d9551dc
DS
762 else
763 api.tag = 0;
764
718e3744 765 /* Simply ignore link-local address. */
766 if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
767 return 0;
768
5048fe14 769 if (command == ZEBRA_REDISTRIBUTE_IPV6_ADD)
a39275d7 770 {
ffd0c037 771 if (bgp_debug_zebra((struct prefix *)&p))
a39275d7 772 {
f04a80a5 773 char buf[2][INET6_ADDRSTRLEN];
dc9ffce8 774 zlog_debug("Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %"ROUTE_TAG_PRI,
ad4cbda1 775 vrf_id,
7c8ff89e 776 zebra_route_string(api.type), api.instance,
f04a80a5
SH
777 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
778 p.prefixlen,
779 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
0d9551dc
DS
780 api.metric,
781 api.tag);
a39275d7 782 }
d289687f 783
784 /*
785 * The ADD message is actually an UPDATE and there is no explicit DEL
786 * for a prior redistributed route, if any. So, perform an implicit
787 * DEL processing for the same redistributed route from any other
788 * source type.
789 */
790 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
791 {
792 if (i != api.type)
6aeb9e78 793 bgp_redistribute_delete(bgp, (struct prefix *)&p, i, api.instance);
d289687f 794 }
795
6aeb9e78 796 bgp_redistribute_add (bgp, (struct prefix *)&p, NULL, &nexthop, ifindex,
7c8ff89e 797 api.metric, api.type, api.instance, api.tag);
a39275d7 798 }
5048fe14 799 else if (command == ZEBRA_REDISTRIBUTE_IPV6_DEL)
a39275d7 800 {
ffd0c037 801 if (bgp_debug_zebra((struct prefix *)&p))
a39275d7 802 {
f04a80a5 803 char buf[2][INET6_ADDRSTRLEN];
ad4cbda1 804 zlog_debug("Rx IPv6 route delete VRF %u %s[%d] %s/%d "
dc9ffce8 805 "nexthop %s metric %u tag %"ROUTE_TAG_PRI,
ad4cbda1 806 vrf_id,
7c8ff89e 807 zebra_route_string(api.type), api.instance,
f04a80a5
SH
808 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
809 p.prefixlen,
810 inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
0d9551dc
DS
811 api.metric,
812 api.tag);
a39275d7 813 }
6aeb9e78 814 bgp_redistribute_delete (bgp, (struct prefix *) &p, api.type, api.instance);
a39275d7 815 }
718e3744 816
817 return 0;
818}
6b0655a2 819
718e3744 820struct interface *
6aeb9e78 821if_lookup_by_ipv4 (struct in_addr *addr, vrf_id_t vrf_id)
718e3744 822{
52dc7ee6 823 struct listnode *ifnode;
824 struct listnode *cnode;
718e3744 825 struct interface *ifp;
826 struct connected *connected;
827 struct prefix_ipv4 p;
828 struct prefix *cp;
829
830 p.family = AF_INET;
831 p.prefix = *addr;
832 p.prefixlen = IPV4_MAX_BITLEN;
833
6aeb9e78 834 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
718e3744 835 {
1eb8ef25 836 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
718e3744 837 {
718e3744 838 cp = connected->address;
839
840 if (cp->family == AF_INET)
841 if (prefix_match (cp, (struct prefix *)&p))
842 return ifp;
843 }
844 }
845 return NULL;
846}
847
848struct interface *
6aeb9e78 849if_lookup_by_ipv4_exact (struct in_addr *addr, vrf_id_t vrf_id)
718e3744 850{
52dc7ee6 851 struct listnode *ifnode;
852 struct listnode *cnode;
718e3744 853 struct interface *ifp;
854 struct connected *connected;
855 struct prefix *cp;
856
6aeb9e78 857 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
718e3744 858 {
1eb8ef25 859 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
718e3744 860 {
718e3744 861 cp = connected->address;
862
863 if (cp->family == AF_INET)
864 if (IPV4_ADDR_SAME (&cp->u.prefix4, addr))
865 return ifp;
866 }
867 }
868 return NULL;
869}
870
718e3744 871struct interface *
b892f1dd 872if_lookup_by_ipv6 (struct in6_addr *addr, ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 873{
52dc7ee6 874 struct listnode *ifnode;
875 struct listnode *cnode;
718e3744 876 struct interface *ifp;
877 struct connected *connected;
878 struct prefix_ipv6 p;
879 struct prefix *cp;
880
881 p.family = AF_INET6;
882 p.prefix = *addr;
883 p.prefixlen = IPV6_MAX_BITLEN;
884
6aeb9e78 885 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
718e3744 886 {
1eb8ef25 887 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
718e3744 888 {
718e3744 889 cp = connected->address;
890
891 if (cp->family == AF_INET6)
892 if (prefix_match (cp, (struct prefix *)&p))
f2345335 893 {
2bb913f5 894 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6))
f2345335
DS
895 {
896 if (ifindex == ifp->ifindex)
897 return ifp;
898 }
899 else
900 return ifp;
901 }
718e3744 902 }
903 }
904 return NULL;
905}
906
907struct interface *
b892f1dd 908if_lookup_by_ipv6_exact (struct in6_addr *addr, ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 909{
52dc7ee6 910 struct listnode *ifnode;
911 struct listnode *cnode;
718e3744 912 struct interface *ifp;
913 struct connected *connected;
914 struct prefix *cp;
915
6aeb9e78 916 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
718e3744 917 {
1eb8ef25 918 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
718e3744 919 {
718e3744 920 cp = connected->address;
921
922 if (cp->family == AF_INET6)
923 if (IPV6_ADDR_SAME (&cp->u.prefix6, addr))
f2345335
DS
924 {
925 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6))
926 {
927 if (ifindex == ifp->ifindex)
928 return ifp;
929 }
930 else
931 return ifp;
932 }
718e3744 933 }
934 }
935 return NULL;
936}
937
94f2b392 938static int
718e3744 939if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
940{
52dc7ee6 941 struct listnode *cnode;
718e3744 942 struct connected *connected;
943 struct prefix *cp;
944
1eb8ef25 945 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
718e3744 946 {
718e3744 947 cp = connected->address;
948
949 if (cp->family == AF_INET6)
950 if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
951 {
952 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
953 return 1;
954 }
955 }
956 return 0;
957}
958
94f2b392 959static int
718e3744 960if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
961{
52dc7ee6 962 struct listnode *cnode;
718e3744 963 struct connected *connected;
964 struct prefix *cp;
965
1eb8ef25 966 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
718e3744 967 {
718e3744 968 cp = connected->address;
969
970 if (cp->family == AF_INET6)
971 if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
972 {
973 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
974 return 1;
975 }
976 }
977 return 0;
978}
718e3744 979
6ee06fa9
PM
980static int
981if_get_ipv4_address (struct interface *ifp, struct in_addr *addr)
982{
983 struct listnode *cnode;
984 struct connected *connected;
985 struct prefix *cp;
986
987 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
988 {
989 cp = connected->address;
990 if ((cp->family == AF_INET) && !ipv4_martian(&(cp->u.prefix4)))
991 {
992 *addr = cp->u.prefix4;
993 return 1;
994 }
995 }
996 return 0;
997}
998
718e3744 999int
1000bgp_nexthop_set (union sockunion *local, union sockunion *remote,
1001 struct bgp_nexthop *nexthop, struct peer *peer)
1002{
1003 int ret = 0;
1004 struct interface *ifp = NULL;
1005
1006 memset (nexthop, 0, sizeof (struct bgp_nexthop));
1007
1008 if (!local)
1009 return -1;
1010 if (!remote)
1011 return -1;
1012
1013 if (local->sa.sa_family == AF_INET)
1014 {
1015 nexthop->v4 = local->sin.sin_addr;
2fb2f5cf 1016 if (peer->update_if)
6aeb9e78 1017 ifp = if_lookup_by_name_vrf (peer->update_if, peer->bgp->vrf_id);
2fb2f5cf 1018 else
6aeb9e78 1019 ifp = if_lookup_by_ipv4_exact (&local->sin.sin_addr, peer->bgp->vrf_id);
718e3744 1020 }
718e3744 1021 if (local->sa.sa_family == AF_INET6)
1022 {
1023 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
1024 {
a80beece 1025 if (peer->conf_if || peer->ifname)
322e5964 1026 ifp = if_lookup_by_name_vrf (peer->conf_if ? peer->conf_if : peer->ifname, peer->bgp->vrf_id);
718e3744 1027 }
2fb2f5cf 1028 else if (peer->update_if)
6aeb9e78 1029 ifp = if_lookup_by_name_vrf (peer->update_if, peer->bgp->vrf_id);
718e3744 1030 else
f2345335 1031 ifp = if_lookup_by_ipv6_exact (&local->sin6.sin6_addr,
6aeb9e78
DS
1032 local->sin6.sin6_scope_id,
1033 peer->bgp->vrf_id);
718e3744 1034 }
718e3744 1035
1036 if (!ifp)
1037 return -1;
1038
1039 nexthop->ifp = ifp;
1040
e33a4880 1041 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
718e3744 1042 if (local->sa.sa_family == AF_INET)
1043 {
718e3744 1044 /* IPv6 nexthop*/
1045 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
1046
718e3744 1047 if (!ret)
003c1ba0 1048 {
1049 /* There is no global nexthop. Use link-local address as both the
1050 * global and link-local nexthop. In this scenario, the expectation
1051 * for interop is that the network admin would use a route-map to
1052 * specify the global IPv6 nexthop.
1053 */
1054 if_get_ipv6_local (ifp, &nexthop->v6_global);
1055 memcpy (&nexthop->v6_local, &nexthop->v6_global,
1056 IPV6_MAX_BYTELEN);
1057 }
718e3744 1058 else
1059 if_get_ipv6_local (ifp, &nexthop->v6_local);
003c1ba0 1060
6aeb9e78 1061 if (if_lookup_by_ipv4 (&remote->sin.sin_addr, peer->bgp->vrf_id))
003c1ba0 1062 peer->shared_network = 1;
1063 else
1064 peer->shared_network = 0;
718e3744 1065 }
1066
e33a4880 1067 /* IPv6 connection, fetch and store IPv4 local address if any. */
718e3744 1068 if (local->sa.sa_family == AF_INET6)
1069 {
1070 struct interface *direct = NULL;
1071
6ee06fa9
PM
1072 /* IPv4 nexthop. */
1073 ret = if_get_ipv4_address(ifp, &nexthop->v4);
1074 if (!ret && peer->local_id.s_addr)
718e3744 1075 nexthop->v4 = peer->local_id;
1076
1077 /* Global address*/
1078 if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
1079 {
1080 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
1081 IPV6_MAX_BYTELEN);
1082
1083 /* If directory connected set link-local address. */
f2345335 1084 direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr,
6aeb9e78 1085 remote->sin6.sin6_scope_id, peer->bgp->vrf_id);
718e3744 1086 if (direct)
1087 if_get_ipv6_local (ifp, &nexthop->v6_local);
1088 }
1089 else
1090 /* Link-local address. */
1091 {
1092 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
1093
1094 /* If there is no global address. Set link-local address as
1095 global. I know this break RFC specification... */
b2b926d5
DS
1096 /* In this scenario, the expectation for interop is that the
1097 * network admin would use a route-map to specify the global
1098 * IPv6 nexthop.
1099 */
718e3744 1100 if (!ret)
1101 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
1102 IPV6_MAX_BYTELEN);
b2b926d5
DS
1103 /* Always set the link-local address */
1104 memcpy (&nexthop->v6_local, &local->sin6.sin6_addr,
1105 IPV6_MAX_BYTELEN);
718e3744 1106 }
718e3744 1107
003c1ba0 1108 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) ||
6aeb9e78
DS
1109 if_lookup_by_ipv6 (&remote->sin6.sin6_addr, remote->sin6.sin6_scope_id,
1110 peer->bgp->vrf_id))
003c1ba0 1111 peer->shared_network = 1;
1112 else
1113 peer->shared_network = 0;
1114 }
718e3744 1115
1116 /* KAME stack specific treatment. */
1117#ifdef KAME
1118 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global)
1119 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global))
1120 {
1121 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0);
1122 }
1123 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local)
1124 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local))
1125 {
1126 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0);
1127 }
1128#endif /* KAME */
e33a4880 1129
1130 /* If we have identified the local interface, there is no error for now. */
1131 return 0;
718e3744 1132}
1133
73ac8160
DS
1134static struct in6_addr *
1135bgp_info_to_ipv6_nexthop (struct bgp_info *info)
1136{
1137 struct in6_addr *nexthop = NULL;
1138
1139 /* Only global address nexthop exists. */
801a9bcc 1140 if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL)
73ac8160
DS
1141 nexthop = &info->attr->extra->mp_nexthop_global;
1142
1143 /* If both global and link-local address present. */
801a9bcc 1144 if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
73ac8160 1145 {
161995ea
DS
1146 /* Check if route-map is set to prefer global over link-local */
1147 if (info->attr->extra->mp_nexthop_prefer_global)
1148 nexthop = &info->attr->extra->mp_nexthop_global;
73ac8160 1149 else
161995ea
DS
1150 {
1151 /* Workaround for Cisco's nexthop bug. */
1152 if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global)
1153 && info->peer->su_remote->sa.sa_family == AF_INET6)
1154 nexthop = &info->peer->su_remote->sin6.sin6_addr;
1155 else
1156 nexthop = &info->attr->extra->mp_nexthop_local;
1157 }
73ac8160
DS
1158 }
1159
1160 return nexthop;
1161}
1162
1163static int
1164bgp_table_map_apply (struct route_map *map, struct prefix *p,
1165 struct bgp_info *info)
1166{
1167 if (route_map_apply(map, p, RMAP_BGP, info) != RMAP_DENYMATCH)
1168 return 1;
1169
16286195 1170 if (bgp_debug_zebra(p))
73ac8160
DS
1171 {
1172 if (p->family == AF_INET)
1173 {
1174 char buf[2][INET_ADDRSTRLEN];
1175 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1176 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1177 p->prefixlen,
1178 inet_ntop(AF_INET, &info->attr->nexthop, buf[1],
1179 sizeof(buf[1])));
1180 }
1181 if (p->family == AF_INET6)
1182 {
1183 char buf[2][INET6_ADDRSTRLEN];
1184 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1185 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1186 p->prefixlen,
1187 inet_ntop(AF_INET6, bgp_info_to_ipv6_nexthop(info), buf[1],
1188 sizeof(buf[1])));
1189 }
1190 }
1191 return 0;
1192}
1193
718e3744 1194void
73ac8160
DS
1195bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp,
1196 afi_t afi, safi_t safi)
718e3744 1197{
0fc452dc 1198 u_int32_t flags;
718e3744 1199 u_char distance;
1200 struct peer *peer;
8196f13d
JB
1201 struct bgp_info *mpinfo;
1202 size_t oldsize, newsize;
73ac8160
DS
1203 u_int32_t nhcount, metric;
1204 struct bgp_info local_info;
1205 struct bgp_info *info_cp = &local_info;
dc9ffce8 1206 route_tag_t tag;
718e3744 1207
ad4cbda1 1208 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1209 * know of this instance.
1210 */
1211 if (!bgp_install_info_to_zebra (bgp))
718e3744 1212 return;
1213
7076bb2f 1214 if ((p->family == AF_INET &&
6aeb9e78 1215 !vrf_bitmap_check (zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP], bgp->vrf_id))
7076bb2f 1216 || (p->family == AF_INET6 &&
6aeb9e78 1217 !vrf_bitmap_check (zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP], bgp->vrf_id)))
718e3744 1218 return;
1219
4a16ae86
DS
1220 if (bgp->main_zebra_update_hold)
1221 return;
1222
718e3744 1223 flags = 0;
1224 peer = info->peer;
1225
0d9551dc
DS
1226 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1227 tag = info->attr->extra->tag;
1228 else
1229 tag = 0;
1230
f992e2a9
DS
1231 /* When we create an aggregate route we must also install a Null0 route in
1232 * the RIB */
1233 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1234 SET_FLAG (flags, ZEBRA_FLAG_BLACKHOLE);
1235
1236 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED ||
1237 info->sub_type == BGP_ROUTE_AGGREGATE)
718e3744 1238 {
1239 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
1240 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1241 }
1242
6d85b15b 1243 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
907f92c8
DS
1244 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1245 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1246
718e3744 1247 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1248
431aa9f9 1249 nhcount = 1 + bgp_info_mpath_count (info);
8196f13d 1250
8a92a8a0 1251 if (p->family == AF_INET && !BGP_ATTR_NEXTHOP_AFI_IP6(info->attr))
718e3744 1252 {
1253 struct zapi_ipv4 api;
1254 struct in_addr *nexthop;
73ac8160
DS
1255 char buf[2][INET_ADDRSTRLEN];
1256 int valid_nh_count = 0;
718e3744 1257
431aa9f9
DS
1258 /* resize nexthop buffer size if necessary */
1259 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
1260 (sizeof (struct in_addr *) * nhcount))
1261 {
1262 newsize = (sizeof (struct in_addr *) * nhcount);
1263 newsize = stream_resize (bgp_nexthop_buf, newsize);
1264 if (newsize == oldsize)
1265 {
1266 zlog_err ("can't resize nexthop buffer");
1267 return;
1268 }
1269 }
1270 stream_reset (bgp_nexthop_buf);
73ac8160
DS
1271 nexthop = NULL;
1272
1273 /* Metric is currently based on the best-path only. */
1274 metric = info->attr->med;
1275
1276 if (bgp->table_map[afi][safi].name)
1277 {
1278 BGP_INFO_ATTR_BUF_INIT();
1279
1280 /* Copy info and attributes, so the route-map apply doesn't modify the
1281 BGP route info. */
1282 BGP_INFO_ATTR_BUF_COPY(info, info_cp);
1283 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1284 {
1285 metric = info_cp->attr->med;
1286 nexthop = &info_cp->attr->nexthop;
0d9551dc
DS
1287
1288 if (info_cp->attr->extra)
1289 tag = info_cp->attr->extra->tag;
73ac8160
DS
1290 }
1291 BGP_INFO_ATTR_BUF_FREE(info_cp);
1292 }
1293 else
1294 {
1295 nexthop = &info->attr->nexthop;
1296 }
1297
1298 if (nexthop)
1299 {
1300 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
1301 valid_nh_count++;
1302 }
431aa9f9 1303
8196f13d 1304 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
73ac8160
DS
1305 mpinfo = bgp_info_mpath_next (mpinfo))
1306 {
1307 nexthop = NULL;
1308
1309 if (bgp->table_map[afi][safi].name)
1310 {
1311 /* Copy info and attributes, so the route-map apply doesn't modify the
1312 BGP route info. */
1313 BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
1314 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1315 nexthop = &info_cp->attr->nexthop;
1316 BGP_INFO_ATTR_BUF_FREE(info_cp);
1317 }
1318 else
1319 {
1320 nexthop = &mpinfo->attr->nexthop;
1321 }
1322
1323 if (nexthop == NULL)
1324 continue;
1325
1326 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
1327 valid_nh_count++;
1328 }
718e3744 1329
6aeb9e78 1330 api.vrf_id = bgp->vrf_id;
73ac8160 1331 api.flags = flags;
718e3744 1332 api.type = ZEBRA_ROUTE_BGP;
7c8ff89e 1333 api.instance = 0;
718e3744 1334 api.message = 0;
5a616c08 1335 api.safi = safi;
718e3744 1336 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
f992e2a9
DS
1337
1338 /* Note that this currently only applies to Null0 routes for aggregates.
1339 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
1340 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1341 * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
1342 */
1343 if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
1344 api.nexthop_num = 0;
1345 else
1346 api.nexthop_num = valid_nh_count;
1347
8196f13d 1348 api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf);
718e3744 1349 api.ifindex_num = 0;
1350 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
73ac8160 1351 api.metric = metric;
d2921c7a 1352 api.tag = 0;
718e3744 1353
0d9551dc
DS
1354 if (tag)
1355 {
1356 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
1357 api.tag = tag;
1358 }
1359
734b349e 1360 distance = bgp_distance_apply (p, info, afi, safi, bgp);
718e3744 1361 if (distance)
1362 {
1363 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
1364 api.distance = distance;
1365 }
a39275d7 1366
16286195 1367 if (bgp_debug_zebra(p))
73ac8160
DS
1368 {
1369 int i;
dc9ffce8 1370 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI
73ac8160 1371 " count %d", (valid_nh_count ? "add":"delete"),
ad4cbda1 1372 bgp->vrf_id,
73ac8160 1373 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
0d9551dc 1374 p->prefixlen, api.metric, api.tag, api.nexthop_num);
73ac8160
DS
1375 for (i = 0; i < api.nexthop_num; i++)
1376 zlog_debug(" IPv4 [nexthop %d] %s", i+1,
1377 inet_ntop(AF_INET, api.nexthop[i], buf[1], sizeof(buf[1])));
1378 }
a39275d7 1379
73ac8160
DS
1380 zapi_ipv4_route (valid_nh_count ? ZEBRA_IPV4_ROUTE_ADD: ZEBRA_IPV4_ROUTE_DELETE,
1381 zclient, (struct prefix_ipv4 *) p, &api);
718e3744 1382 }
431aa9f9 1383
718e3744 1384 /* We have to think about a IPv6 link-local address curse. */
8a92a8a0
DS
1385 if (p->family == AF_INET6 ||
1386 (p->family == AF_INET && BGP_ATTR_NEXTHOP_AFI_IP6(info->attr)))
718e3744 1387 {
b892f1dd 1388 ifindex_t ifindex;
718e3744 1389 struct in6_addr *nexthop;
1390 struct zapi_ipv6 api;
431aa9f9 1391 int valid_nh_count = 0;
73ac8160 1392 char buf[2][INET6_ADDRSTRLEN];
431aa9f9
DS
1393
1394 /* resize nexthop buffer size if necessary */
1395 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
1396 (sizeof (struct in6_addr *) * nhcount))
1397 {
1398 newsize = (sizeof (struct in6_addr *) * nhcount);
1399 newsize = stream_resize (bgp_nexthop_buf, newsize);
1400 if (newsize == oldsize)
1401 {
1402 zlog_err ("can't resize nexthop buffer");
1403 return;
1404 }
1405 }
1406 stream_reset (bgp_nexthop_buf);
1407
1408 /* resize ifindices buffer size if necessary */
1409 if ((oldsize = stream_get_size (bgp_ifindices_buf)) <
1410 (sizeof (unsigned int) * nhcount))
1411 {
1412 newsize = (sizeof (unsigned int) * nhcount);
1413 newsize = stream_resize (bgp_ifindices_buf, newsize);
1414 if (newsize == oldsize)
1415 {
1416 zlog_err ("can't resize nexthop buffer");
1417 return;
1418 }
1419 }
1420 stream_reset (bgp_ifindices_buf);
718e3744 1421
1422 ifindex = 0;
1423 nexthop = NULL;
431aa9f9 1424
fb982c25 1425 assert (info->attr->extra);
718e3744 1426
73ac8160
DS
1427 /* Metric is currently based on the best-path only. */
1428 metric = info->attr->med;
1429
1430 if (bgp->table_map[afi][safi].name)
1431 {
1432 BGP_INFO_ATTR_BUF_INIT();
718e3744 1433
73ac8160
DS
1434 /* Copy info and attributes, so the route-map apply doesn't modify the
1435 BGP route info. */
1436 BGP_INFO_ATTR_BUF_COPY(info, info_cp);
1437 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1438 {
1439 metric = info_cp->attr->med;
1440 nexthop = bgp_info_to_ipv6_nexthop(info_cp);
0d9551dc
DS
1441
1442 if (info_cp->attr->extra)
1443 tag = info_cp->attr->extra->tag;
73ac8160
DS
1444 }
1445 BGP_INFO_ATTR_BUF_FREE(info_cp);
1446 }
1447 else
1448 {
1449 nexthop = bgp_info_to_ipv6_nexthop(info);
1450 }
718e3744 1451
73ac8160
DS
1452 if (nexthop)
1453 {
801a9bcc 1454 if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
73ac8160
DS
1455 if (info->peer->nexthop.ifp)
1456 ifindex = info->peer->nexthop.ifp->ifindex;
1457
1458 if (!ifindex)
ffd0c037
DS
1459 {
1460 if (info->peer->conf_if || info->peer->ifname)
1461 ifindex = if_nametoindex (info->peer->conf_if ? info->peer->conf_if : info->peer->ifname);
1462 else if (info->peer->nexthop.ifp)
1463 ifindex = info->peer->nexthop.ifp->ifindex;
1464 }
73ac8160
DS
1465 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
1466 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
1467 valid_nh_count++;
1468 }
431aa9f9
DS
1469
1470 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
1471 mpinfo = bgp_info_mpath_next (mpinfo))
73ac8160
DS
1472 {
1473 ifindex = 0;
1474 nexthop = NULL;
000830bd 1475
73ac8160 1476 if (bgp->table_map[afi][safi].name)
431aa9f9 1477 {
73ac8160
DS
1478 /* Copy info and attributes, so the route-map apply doesn't modify the
1479 BGP route info. */
1480 BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
1481 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1482 nexthop = bgp_info_to_ipv6_nexthop(info_cp);
1483 BGP_INFO_ATTR_BUF_FREE(info_cp);
1484 }
1485 else
1486 {
1487 nexthop = bgp_info_to_ipv6_nexthop(mpinfo);
431aa9f9 1488 }
431aa9f9 1489
73ac8160
DS
1490 if (nexthop == NULL)
1491 continue;
1492
801a9bcc 1493 if (mpinfo->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
73ac8160
DS
1494 if (mpinfo->peer->nexthop.ifp)
1495 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
000830bd
DS
1496
1497 if (!ifindex)
ffd0c037
DS
1498 {
1499 if (mpinfo->peer->conf_if || mpinfo->peer->ifname)
322e5964 1500 ifindex = ifname2ifindex (mpinfo->peer->conf_if ? mpinfo->peer->conf_if : mpinfo->peer->ifname);
ffd0c037
DS
1501 else if (mpinfo->peer->nexthop.ifp)
1502 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
1503 }
73ac8160
DS
1504 if (ifindex == 0)
1505 continue;
431aa9f9
DS
1506
1507 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
1508 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
1509 valid_nh_count++;
73ac8160 1510 }
718e3744 1511
1512 /* Make Zebra API structure. */
6aeb9e78 1513 api.vrf_id = bgp->vrf_id;
718e3744 1514 api.flags = flags;
1515 api.type = ZEBRA_ROUTE_BGP;
7c8ff89e 1516 api.instance = 0;
718e3744 1517 api.message = 0;
c7ec179a 1518 api.safi = safi;
718e3744 1519 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
f992e2a9
DS
1520
1521 /* Note that this currently only applies to Null0 routes for aggregates.
1522 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a special
1523 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1524 * do not want to also encode the :: nexthop for the aggregate route.
1525 */
1526 if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
1527 api.nexthop_num = 0;
1528 else
1529 api.nexthop_num = valid_nh_count;
1530
431aa9f9 1531 api.nexthop = (struct in6_addr **)STREAM_DATA (bgp_nexthop_buf);
718e3744 1532 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
431aa9f9 1533 api.ifindex_num = valid_nh_count;
b892f1dd 1534 api.ifindex = (ifindex_t *)STREAM_DATA (bgp_ifindices_buf);
718e3744 1535 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
73ac8160 1536 api.metric = metric;
d2921c7a 1537 api.tag = 0;
718e3744 1538
0d9551dc
DS
1539 if (tag)
1540 {
1541 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
1542 api.tag = tag;
1543 }
1544
734b349e
MZ
1545 distance = bgp_distance_apply (p, info, afi, safi, bgp);
1546 if (distance)
1547 {
1548 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
1549 api.distance = distance;
1550 }
1551
8a92a8a0 1552 if (p->family == AF_INET)
73ac8160 1553 {
8a92a8a0
DS
1554 if (bgp_debug_zebra(p))
1555 {
1556 int i;
dc9ffce8 1557 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI,
ad4cbda1 1558 valid_nh_count ? "add" : "delete", bgp->vrf_id,
8a92a8a0
DS
1559 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1560 p->prefixlen, api.metric, api.tag);
1561 for (i = 0; i < api.nexthop_num; i++)
1562 zlog_debug(" IPv6 [nexthop %d] %s", i+1,
1563 inet_ntop(AF_INET6, api.nexthop[i], buf[1], sizeof(buf[1])));
1564 }
1565
1566 if (valid_nh_count)
1567 zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD,
88177fe3
DS
1568 zclient, (struct prefix_ipv4 *) p,
1569 (struct zapi_ipv6 *)&api);
8a92a8a0
DS
1570 else
1571 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE,
88177fe3 1572 zclient, (struct prefix_ipv4 *) p, (struct zapi_ipv4 *)&api);
73ac8160 1573 }
8a92a8a0
DS
1574 else
1575 {
1576 if (bgp_debug_zebra(p))
1577 {
1578 int i;
dc9ffce8 1579 zlog_debug("Tx IPv6 route %s VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI,
ad4cbda1 1580 valid_nh_count ? "add" : "delete", bgp->vrf_id,
8a92a8a0
DS
1581 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1582 p->prefixlen, api.metric, api.tag);
1583 for (i = 0; i < api.nexthop_num; i++)
1584 zlog_debug(" IPv6 [nexthop %d] %s", i+1,
1585 inet_ntop(AF_INET6, api.nexthop[i], buf[1], sizeof(buf[1])));
1586 }
a39275d7 1587
8a92a8a0
DS
1588 zapi_ipv6_route (valid_nh_count ?
1589 ZEBRA_IPV6_ROUTE_ADD : ZEBRA_IPV6_ROUTE_DELETE,
1590 zclient, (struct prefix_ipv6 *) p, &api);
1591 }
718e3744 1592 }
718e3744 1593}
1594
73ac8160
DS
1595/* Announce all routes of a table to zebra */
1596void
1597bgp_zebra_announce_table (struct bgp *bgp, afi_t afi, safi_t safi)
1598{
1599 struct bgp_node *rn;
1600 struct bgp_table *table;
1601 struct bgp_info *ri;
1602
ad4cbda1 1603 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1604 * know of this instance.
1605 */
1606 if (!bgp_install_info_to_zebra (bgp))
1607 return;
1608
73ac8160 1609 table = bgp->rib[afi][safi];
4a16ae86 1610 if (!table) return;
73ac8160
DS
1611
1612 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
1613 for (ri = rn->info; ri; ri = ri->next)
1614 if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)
1615 && ri->type == ZEBRA_ROUTE_BGP
1616 && ri->sub_type == BGP_ROUTE_NORMAL)
1617 bgp_zebra_announce (&rn->p, ri, bgp, afi, safi);
1618}
1619
718e3744 1620void
5a616c08 1621bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi)
718e3744 1622{
0fc452dc 1623 u_int32_t flags;
718e3744 1624 struct peer *peer;
1625
6aeb9e78 1626 peer = info->peer;
ad4cbda1 1627 assert(peer);
1628
1629 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1630 * know of this instance.
1631 */
1632 if (!bgp_install_info_to_zebra (peer->bgp))
1633 return;
6aeb9e78 1634
7076bb2f 1635 if ((p->family == AF_INET &&
6aeb9e78 1636 !vrf_bitmap_check (zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP], peer->bgp->vrf_id))
7076bb2f 1637 || (p->family == AF_INET6 &&
6aeb9e78 1638 !vrf_bitmap_check (zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP], peer->bgp->vrf_id)))
718e3744 1639 return;
1640
718e3744 1641 flags = 0;
1642
6d85b15b 1643 if (peer->sort == BGP_PEER_IBGP)
718e3744 1644 {
1645 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1646 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
1647 }
1648
6d85b15b 1649 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
907f92c8
DS
1650 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1651 || bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
718e3744 1652 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1653
1654 if (p->family == AF_INET)
1655 {
1656 struct zapi_ipv4 api;
718e3744 1657
6aeb9e78 1658 api.vrf_id = peer->bgp->vrf_id;
718e3744 1659 api.flags = flags;
718e3744 1660
1661 api.type = ZEBRA_ROUTE_BGP;
7c8ff89e 1662 api.instance = 0;
718e3744 1663 api.message = 0;
5a616c08 1664 api.safi = safi;
718e3744 1665 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
76145957
DS
1666 api.nexthop_num = 0;
1667 api.nexthop = NULL;
718e3744 1668 api.ifindex_num = 0;
1669 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1670 api.metric = info->attr->med;
d2921c7a 1671 api.tag = 0;
718e3744 1672
0d9551dc
DS
1673 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1674 {
1675 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1676 api.tag = info->attr->extra->tag;
1677 }
1678
16286195 1679 if (bgp_debug_zebra(p))
a39275d7
AS
1680 {
1681 char buf[2][INET_ADDRSTRLEN];
dc9ffce8 1682 zlog_debug("Tx IPv4 route delete VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI,
ad4cbda1 1683 peer->bgp->vrf_id,
a39275d7 1684 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
fbf288a5 1685 p->prefixlen, api.metric, api.tag);
a39275d7
AS
1686 }
1687
0a589359 1688 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient,
1689 (struct prefix_ipv4 *) p, &api);
718e3744 1690 }
718e3744 1691 /* We have to think about a IPv6 link-local address curse. */
1692 if (p->family == AF_INET6)
1693 {
1694 struct zapi_ipv6 api;
fb982c25
PJ
1695
1696 assert (info->attr->extra);
322e5964 1697
6aeb9e78 1698 api.vrf_id = peer->bgp->vrf_id;
718e3744 1699 api.flags = flags;
1700 api.type = ZEBRA_ROUTE_BGP;
7c8ff89e 1701 api.instance = 0;
718e3744 1702 api.message = 0;
c7ec179a 1703 api.safi = safi;
718e3744 1704 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
fbf288a5
DS
1705 api.nexthop_num = 0;
1706 api.nexthop = NULL;
1707 api.ifindex_num = 0;
718e3744 1708 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1709 api.metric = info->attr->med;
d2921c7a 1710 api.tag = 0;
718e3744 1711
0d9551dc
DS
1712 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1713 {
1714 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1715 api.tag = info->attr->extra->tag;
1716 }
1717
16286195 1718 if (bgp_debug_zebra(p))
a39275d7
AS
1719 {
1720 char buf[2][INET6_ADDRSTRLEN];
dc9ffce8 1721 zlog_debug("Tx IPv6 route delete VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI,
ad4cbda1 1722 peer->bgp->vrf_id,
a39275d7 1723 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
fbf288a5 1724 p->prefixlen, api.metric, api.tag);
a39275d7
AS
1725 }
1726
0a589359 1727 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient,
1728 (struct prefix_ipv6 *) p, &api);
718e3744 1729 }
718e3744 1730}
56c1f7d8 1731
7c8ff89e
DS
1732struct bgp_redist *
1733bgp_redist_lookup (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
1734{
1735 struct list *red_list;
1736 struct listnode *node;
1737 struct bgp_redist *red;
1738
1739 red_list = bgp->redist[afi][type];
1740 if (!red_list)
1741 return(NULL);
1742
1743 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1744 if (red->instance == instance)
1745 return red;
1746
1747 return NULL;
1748}
1749
1750struct bgp_redist *
1751bgp_redist_add (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
1752{
1753 struct list *red_list;
1754 struct bgp_redist *red;
1755
1756 red = bgp_redist_lookup(bgp, afi, type, instance);
1757 if (red)
1758 return red;
1759
1760 if (!bgp->redist[afi][type])
1761 bgp->redist[afi][type] = list_new();
1762
1763 red_list = bgp->redist[afi][type];
6e919709 1764 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist));
7c8ff89e
DS
1765 red->instance = instance;
1766
1767 listnode_add(red_list, red);
1768
1769 return red;
1770}
1771
1772static void
1773bgp_redist_del (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
1774{
1775 struct bgp_redist *red;
1776
1777 red = bgp_redist_lookup(bgp, afi, type, instance);
1778
1779 if (red)
1780 {
1781 listnode_delete(bgp->redist[afi][type], red);
37d361e7 1782 XFREE (MTYPE_BGP_REDIST, red);
7c8ff89e
DS
1783 if (!bgp->redist[afi][type]->count)
1784 {
1785 list_free(bgp->redist[afi][type]);
1786 bgp->redist[afi][type] = NULL;
1787 }
1788 }
1789}
6b0655a2 1790
718e3744 1791/* Other routes redistribution into BGP. */
1792int
6aeb9e78 1793bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type, u_short instance)
718e3744 1794{
718e3744 1795
1796 /* Return if already redistribute flag is set. */
7076bb2f
FL
1797 if (instance)
1798 {
1799 if (redist_check_instance(&zclient->mi_redist[afi][type], instance))
1800 return CMD_WARNING;
718e3744 1801
7076bb2f
FL
1802 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1803 }
1804 else
1805 {
6aeb9e78 1806 if (vrf_bitmap_check (zclient->redist[afi][type], bgp->vrf_id))
7076bb2f
FL
1807 return CMD_WARNING;
1808
65efcfce
LB
1809#if ENABLE_BGP_VNC
1810 if (bgp->vrf_id == VRF_DEFAULT &&
1811 type == ZEBRA_ROUTE_VNC_DIRECT) {
1812 vnc_export_bgp_enable(bgp, afi); /* only enables if mode bits cfg'd */
1813 }
1814#endif
1815
6aeb9e78 1816 vrf_bitmap_set (zclient->redist[afi][type], bgp->vrf_id);
7076bb2f 1817 }
718e3744 1818
ad4cbda1 1819 /* Don't try to register if we're not connected to Zebra or Zebra doesn't
1820 * know of this instance.
1821 */
1822 if (!bgp_install_info_to_zebra (bgp))
718e3744 1823 return CMD_WARNING;
a39275d7 1824
16286195 1825 if (BGP_DEBUG (zebra, ZEBRA))
ad4cbda1 1826 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1827 bgp->vrf_id, afi,
8bb0831e 1828 zebra_route_string(type), instance);
518f0eb1 1829
718e3744 1830 /* Send distribute add message to zebra. */
7076bb2f 1831 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
6aeb9e78 1832 instance, bgp->vrf_id);
718e3744 1833
1834 return CMD_SUCCESS;
1835}
1836
518f0eb1 1837int
7c8ff89e 1838bgp_redistribute_resend (struct bgp *bgp, afi_t afi, int type, u_short instance)
518f0eb1 1839{
ad4cbda1 1840 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1841 * know of this instance.
1842 */
1843 if (!bgp_install_info_to_zebra (bgp))
518f0eb1
DS
1844 return -1;
1845
16286195 1846 if (BGP_DEBUG (zebra, ZEBRA))
ad4cbda1 1847 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1848 bgp->vrf_id, afi,
8bb0831e 1849 zebra_route_string(type), instance);
518f0eb1
DS
1850
1851 /* Send distribute add message to zebra. */
7076bb2f 1852 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
9585bba7 1853 instance, bgp->vrf_id);
7076bb2f 1854 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
9585bba7 1855 instance, bgp->vrf_id);
518f0eb1
DS
1856
1857 return 0;
1858}
1859
718e3744 1860/* Redistribute with route-map specification. */
1861int
7c8ff89e 1862bgp_redistribute_rmap_set (struct bgp_redist *red, const char *name)
718e3744 1863{
7c8ff89e
DS
1864 if (red->rmap.name
1865 && (strcmp (red->rmap.name, name) == 0))
718e3744 1866 return 0;
1867
7c8ff89e 1868 if (red->rmap.name)
6e919709
DS
1869 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1870 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
7c8ff89e 1871 red->rmap.map = route_map_lookup_by_name (name);
718e3744 1872
1873 return 1;
1874}
1875
1876/* Redistribute with metric specification. */
1877int
caf958b4
DS
1878bgp_redistribute_metric_set (struct bgp *bgp, struct bgp_redist *red, afi_t afi,
1879 int type, u_int32_t metric)
718e3744 1880{
caf958b4
DS
1881 struct bgp_node *rn;
1882 struct bgp_info *ri;
1883
7c8ff89e
DS
1884 if (red->redist_metric_flag
1885 && red->redist_metric == metric)
718e3744 1886 return 0;
1887
7c8ff89e
DS
1888 red->redist_metric_flag = 1;
1889 red->redist_metric = metric;
718e3744 1890
711093b5 1891 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn; rn = bgp_route_next(rn))
1892 {
1893 for (ri = rn->info; ri; ri = ri->next)
1894 {
1895 if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE &&
1896 ri->type == type &&
1897 ri->instance == red->instance)
1898 {
1899 struct attr *old_attr;
1900 struct attr new_attr;
1901 struct attr_extra new_extra;
1902
1903 new_attr.extra = &new_extra;
1904 bgp_attr_dup (&new_attr, ri->attr);
1905 new_attr.med = red->redist_metric;
1906 old_attr = ri->attr;
1907 ri->attr = bgp_attr_intern (&new_attr);
1908 bgp_attr_unintern (&old_attr);
1909
1910 bgp_info_set_flag(rn, ri, BGP_INFO_ATTR_CHANGED);
1911 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1912 }
1913 }
caf958b4 1914 }
caf958b4 1915
718e3744 1916 return 1;
1917}
1918
1919/* Unset redistribution. */
1920int
6aeb9e78 1921bgp_redistribute_unreg (struct bgp *bgp, afi_t afi, int type, u_short instance)
718e3744 1922{
7c8ff89e
DS
1923 struct bgp_redist *red;
1924
1925 red = bgp_redist_lookup(bgp, afi, type, instance);
1926 if (!red)
1927 return CMD_SUCCESS;
718e3744 1928
718e3744 1929 /* Return if zebra connection is disabled. */
7076bb2f
FL
1930 if (instance)
1931 {
1932 if (!redist_check_instance(&zclient->mi_redist[afi][type], instance))
1933 return CMD_WARNING;
1934 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1935 }
1936 else
1937 {
6aeb9e78 1938 if (! vrf_bitmap_check (zclient->redist[afi][type], bgp->vrf_id))
7076bb2f 1939 return CMD_WARNING;
6aeb9e78 1940 vrf_bitmap_unset (zclient->redist[afi][type], bgp->vrf_id);
7076bb2f 1941 }
718e3744 1942
65efcfce
LB
1943#if ENABLE_BGP_VNC
1944 if (bgp->vrf_id == VRF_DEFAULT &&
1945 type == ZEBRA_ROUTE_VNC_DIRECT) {
1946 vnc_export_bgp_disable(bgp, afi);
1947 }
1948#endif
1949
ad4cbda1 1950 if (bgp_install_info_to_zebra (bgp))
a39275d7
AS
1951 {
1952 /* Send distribute delete message to zebra. */
16286195 1953 if (BGP_DEBUG (zebra, ZEBRA))
ad4cbda1 1954 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1955 bgp->vrf_id, afi, zebra_route_string(type), instance);
7076bb2f 1956 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type, instance,
6aeb9e78 1957 bgp->vrf_id);
a39275d7 1958 }
718e3744 1959
1960 /* Withdraw redistributed routes from current BGP's routing table. */
7c8ff89e 1961 bgp_redistribute_withdraw (bgp, afi, type, instance);
718e3744 1962
1963 return CMD_SUCCESS;
1964}
1965
6aeb9e78
DS
1966/* Unset redistribution. */
1967int
1968bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type, u_short instance)
1969{
1970 struct bgp_redist *red;
1971
1972 red = bgp_redist_lookup(bgp, afi, type, instance);
1973 if (!red)
1974 return CMD_SUCCESS;
1975
1976 bgp_redistribute_unreg(bgp, afi, type, instance);
1977
1978 /* Unset route-map. */
1979 if (red->rmap.name)
1980 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1981 red->rmap.name = NULL;
1982 red->rmap.map = NULL;
1983
1984 /* Unset metric. */
1985 red->redist_metric_flag = 0;
1986 red->redist_metric = 0;
1987
1988 bgp_redist_del(bgp, afi, type, instance);
1989
1990 return CMD_SUCCESS;
1991}
1992
eb117f29
SK
1993/* Update redistribute vrf bitmap during triggers like
1994 restart networking or delete/add VRFs */
1995void
1996bgp_update_redist_vrf_bitmaps (struct bgp *bgp, vrf_id_t old_vrf_id)
1997{
1998 int i;
1999 afi_t afi;
2000
2001 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2002 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2003 if (vrf_bitmap_check (zclient->redist[afi][i], old_vrf_id))
2004 {
2005 vrf_bitmap_unset (zclient->redist[afi][i], old_vrf_id);
2006 vrf_bitmap_set (zclient->redist[afi][i], bgp->vrf_id);
2007 }
2008 return;
2009}
2010
718e3744 2011void
94f2b392 2012bgp_zclient_reset (void)
718e3744 2013{
2014 zclient_reset (zclient);
2015}
2016
ad4cbda1 2017/* Register this instance with Zebra. Invoked upon connect (for
2018 * default instance) and when other VRFs are learnt (or created and
2019 * already learnt).
2020 */
2021void
2022bgp_zebra_instance_register (struct bgp *bgp)
2023{
2024 /* Don't try to register if we're not connected to Zebra */
4c2620da 2025 if (!zclient || zclient->sock < 0)
ad4cbda1 2026 return;
2027
2028 if (BGP_DEBUG (zebra, ZEBRA))
2029 zlog_debug("Registering VRF %u", bgp->vrf_id);
2030
2031 /* Register for router-id, interfaces, redistributed routes. */
2032 zclient_send_reg_requests (zclient, bgp->vrf_id);
2033}
2034
2035/* Deregister this instance with Zebra. Invoked upon the instance
2036 * being deleted (default or VRF) and it is already registered.
2037 */
2038void
2039bgp_zebra_instance_deregister (struct bgp *bgp)
2040{
2041 /* Don't try to deregister if we're not connected to Zebra */
2042 if (zclient->sock < 0)
2043 return;
2044
2045 if (BGP_DEBUG (zebra, ZEBRA))
2046 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
2047
2048 /* Deregister for router-id, interfaces, redistributed routes. */
2049 zclient_send_dereg_requests (zclient, bgp->vrf_id);
2050}
2051
4a04e5f7 2052void
2053bgp_zebra_initiate_radv (struct bgp *bgp, struct peer *peer)
2054{
5c81b96a 2055 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
2056
4a04e5f7 2057 /* Don't try to initiate if we're not connected to Zebra */
2058 if (zclient->sock < 0)
2059 return;
2060
2061 if (BGP_DEBUG (zebra, ZEBRA))
2062 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id, peer->host);
2063
5c81b96a 2064 zclient_send_interface_radv_req (zclient, bgp->vrf_id, peer->ifp, 1, ra_interval);
4a04e5f7 2065}
2066
2067void
2068bgp_zebra_terminate_radv (struct bgp *bgp, struct peer *peer)
2069{
2070 /* Don't try to terminate if we're not connected to Zebra */
2071 if (zclient->sock < 0)
2072 return;
2073
2074 if (BGP_DEBUG (zebra, ZEBRA))
2075 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id, peer->host);
2076
5c81b96a 2077 zclient_send_interface_radv_req (zclient, bgp->vrf_id, peer->ifp, 0, 0);
4a04e5f7 2078}
2079
ad4cbda1 2080/* BGP has established connection with Zebra. */
7076bb2f
FL
2081static void
2082bgp_zebra_connected (struct zclient *zclient)
2083{
6aeb9e78 2084 struct bgp *bgp;
7076bb2f 2085
afbb1c59
LB
2086 zclient_num_connects++; /* increment even if not responding */
2087
ad4cbda1 2088 /* At this point, we may or may not have BGP instances configured, but
2089 * we're only interested in the default VRF (others wouldn't have learnt
2090 * the VRF from Zebra yet.)
2091 */
2092 bgp = bgp_get_default();
2093 if (!bgp)
2094 return;
2095
2096 bgp_zebra_instance_register (bgp);
2097
2376c3f2 2098 /* Send the client registration */
2099 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
2100
ad4cbda1 2101 /* TODO - What if we have peers and networks configured, do we have to
2102 * kick-start them?
2103 */
7076bb2f
FL
2104}
2105
6aeb9e78 2106
718e3744 2107void
4140ca4d 2108bgp_zebra_init (struct thread_master *master)
718e3744 2109{
afbb1c59
LB
2110 zclient_num_connects = 0;
2111
718e3744 2112 /* Set default values. */
4140ca4d 2113 zclient = zclient_new (master);
7c8ff89e 2114 zclient_init (zclient, ZEBRA_ROUTE_BGP, 0);
7076bb2f 2115 zclient->zebra_connected = bgp_zebra_connected;
18a6dce6 2116 zclient->router_id_update = bgp_router_id_update;
718e3744 2117 zclient->interface_add = bgp_interface_add;
2118 zclient->interface_delete = bgp_interface_delete;
2119 zclient->interface_address_add = bgp_interface_address_add;
2120 zclient->interface_address_delete = bgp_interface_address_delete;
a80beece
DS
2121 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
2122 zclient->interface_nbr_address_delete = bgp_interface_nbr_address_delete;
bfcd43b2 2123 zclient->interface_vrf_update = bgp_interface_vrf_update;
5048fe14 2124 zclient->redistribute_route_ipv4_add = zebra_read_ipv4;
2125 zclient->redistribute_route_ipv4_del = zebra_read_ipv4;
718e3744 2126 zclient->interface_up = bgp_interface_up;
2127 zclient->interface_down = bgp_interface_down;
5048fe14 2128 zclient->redistribute_route_ipv6_add = zebra_read_ipv6;
2129 zclient->redistribute_route_ipv6_del = zebra_read_ipv6;
fb018d25 2130 zclient->nexthop_update = bgp_read_nexthop_update;
078430f6 2131 zclient->import_check_update = bgp_read_import_check_update;
718e3744 2132
8196f13d 2133 bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
431aa9f9 2134 bgp_ifindices_buf = stream_new(BGP_IFINDICES_BUF_SIZE);
718e3744 2135}
bb86c601
LB
2136
2137void
2138bgp_zebra_destroy(void)
2139{
2140 if (zclient == NULL)
2141 return;
2142 zclient_stop(zclient);
2143 zclient_free(zclient);
2144 zclient = NULL;
2145}
afbb1c59
LB
2146
2147int
2148bgp_zebra_num_connects(void)
2149{
2150 return zclient_num_connects;
2151}