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