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