Coverage Report

Created: 2025-11-11 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/frr/bgpd/bgp_zebra.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/* zebra client
3
 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4
 */
5
6
#include <zebra.h>
7
8
#include "command.h"
9
#include "stream.h"
10
#include "network.h"
11
#include "prefix.h"
12
#include "log.h"
13
#include "sockunion.h"
14
#include "zclient.h"
15
#include "routemap.h"
16
#include "frrevent.h"
17
#include "queue.h"
18
#include "memory.h"
19
#include "lib/json.h"
20
#include "lib/bfd.h"
21
#include "lib/route_opaque.h"
22
#include "filter.h"
23
#include "mpls.h"
24
#include "vxlan.h"
25
#include "pbr.h"
26
27
#include "bgpd/bgpd.h"
28
#include "bgpd/bgp_route.h"
29
#include "bgpd/bgp_attr.h"
30
#include "bgpd/bgp_aspath.h"
31
#include "bgpd/bgp_nexthop.h"
32
#include "bgpd/bgp_zebra.h"
33
#include "bgpd/bgp_fsm.h"
34
#include "bgpd/bgp_debug.h"
35
#include "bgpd/bgp_errors.h"
36
#include "bgpd/bgp_mpath.h"
37
#include "bgpd/bgp_nexthop.h"
38
#include "bgpd/bgp_nht.h"
39
#include "bgpd/bgp_bfd.h"
40
#include "bgpd/bgp_label.h"
41
#ifdef ENABLE_BGP_VNC
42
#include "bgpd/rfapi/rfapi_backend.h"
43
#include "bgpd/rfapi/vnc_export_bgp.h"
44
#endif
45
#include "bgpd/bgp_evpn.h"
46
#include "bgpd/bgp_mplsvpn.h"
47
#include "bgpd/bgp_labelpool.h"
48
#include "bgpd/bgp_pbr.h"
49
#include "bgpd/bgp_evpn_private.h"
50
#include "bgpd/bgp_evpn_mh.h"
51
#include "bgpd/bgp_mac.h"
52
#include "bgpd/bgp_trace.h"
53
#include "bgpd/bgp_community.h"
54
#include "bgpd/bgp_lcommunity.h"
55
56
/* All information about zebra. */
57
struct zclient *zclient = NULL;
58
59
/* hook to indicate vrf status change for SNMP */
60
DEFINE_HOOK(bgp_vrf_status_changed, (struct bgp *bgp, struct interface *ifp),
61
      (bgp, ifp));
62
63
2
DEFINE_MTYPE_STATIC(BGPD, BGP_IF_INFO, "BGP interface context");
64
2
65
2
/* Can we install into zebra? */
66
2
static inline bool bgp_install_info_to_zebra(struct bgp *bgp)
67
2
{
68
0
  if (zclient->sock <= 0)
69
0
    return false;
70
71
0
  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
72
0
    zlog_debug(
73
0
      "%s: No zebra instance to talk to, not installing information",
74
0
      __func__);
75
0
    return false;
76
0
  }
77
78
0
  return true;
79
0
}
80
81
int zclient_num_connects;
82
83
/* Router-id update message from zebra. */
84
static int bgp_router_id_update(ZAPI_CALLBACK_ARGS)
85
0
{
86
0
  struct prefix router_id;
87
88
0
  zebra_router_id_update_read(zclient->ibuf, &router_id);
89
90
0
  if (BGP_DEBUG(zebra, ZEBRA))
91
0
    zlog_debug("Rx Router Id update VRF %u Id %pFX", vrf_id,
92
0
         &router_id);
93
94
0
  bgp_router_id_zebra_bump(vrf_id, &router_id);
95
0
  return 0;
96
0
}
97
98
/* Nexthop update message from zebra. */
99
static int bgp_read_nexthop_update(ZAPI_CALLBACK_ARGS)
100
0
{
101
0
  bgp_parse_nexthop_update(cmd, vrf_id);
102
0
  return 0;
103
0
}
104
105
/* Set or clear interface on which unnumbered neighbor is configured. This
106
 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
107
 * interface.
108
 */
109
static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
110
              struct interface *upd_ifp)
111
0
{
112
0
  struct listnode *node, *nnode;
113
0
  struct peer *peer;
114
115
0
  for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
116
0
    if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
117
0
      if (upd_ifp) {
118
0
        peer->ifp = upd_ifp;
119
0
        bgp_zebra_initiate_radv(bgp, peer);
120
0
      } else {
121
0
        bgp_zebra_terminate_radv(bgp, peer);
122
0
        peer->ifp = upd_ifp;
123
0
      }
124
0
    }
125
0
  }
126
0
}
127
128
static int bgp_read_fec_update(ZAPI_CALLBACK_ARGS)
129
0
{
130
0
  bgp_parse_fec_update();
131
0
  return 0;
132
0
}
133
134
static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
135
0
{
136
0
  struct listnode *node, *nnode;
137
0
  struct peer *peer;
138
139
0
  for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
140
0
    if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
141
0
        && !peer_established(peer)) {
142
0
      if (peer_active(peer))
143
0
        BGP_EVENT_ADD(peer, BGP_Stop);
144
0
      BGP_EVENT_ADD(peer, BGP_Start);
145
0
    }
146
0
  }
147
0
}
148
149
static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
150
0
{
151
0
  struct listnode *node;
152
0
  struct connected *connected;
153
0
  struct interface *ifp;
154
0
  struct prefix *p;
155
156
  /* Kick-off the FSM for any relevant peers only if there is a
157
   * valid local address on the interface.
158
   */
159
0
  ifp = ifc->ifp;
160
0
  for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
161
0
    p = connected->address;
162
0
    if (p->family == AF_INET6
163
0
        && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
164
0
      break;
165
0
  }
166
0
  if (!connected)
167
0
    return;
168
169
0
  bgp_start_interface_nbrs(bgp, ifp);
170
0
}
171
172
static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
173
             int del)
174
0
{
175
0
  struct listnode *node, *nnode;
176
0
  struct peer *peer;
177
0
  struct interface *ifp;
178
179
0
  for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
180
0
    if (peer->conf_if
181
0
        && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
182
0
      peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
183
0
      BGP_EVENT_ADD(peer, BGP_Stop);
184
0
    }
185
0
  }
186
  /* Free neighbor also, if we're asked to. */
187
0
  if (del) {
188
0
    ifp = ifc->ifp;
189
0
    listnode_delete(ifp->nbr_connected, ifc);
190
0
    nbr_connected_free(ifc);
191
0
  }
192
0
}
193
194
static int bgp_ifp_destroy(struct interface *ifp)
195
0
{
196
0
  struct bgp *bgp;
197
198
0
  bgp = ifp->vrf->info;
199
200
0
  if (BGP_DEBUG(zebra, ZEBRA))
201
0
    zlog_debug("Rx Intf del VRF %u IF %s", ifp->vrf->vrf_id,
202
0
         ifp->name);
203
204
0
  if (bgp) {
205
0
    bgp_update_interface_nbrs(bgp, ifp, NULL);
206
0
    hook_call(bgp_vrf_status_changed, bgp, ifp);
207
0
  }
208
209
0
  bgp_mac_del_mac_entry(ifp);
210
211
0
  return 0;
212
0
}
213
214
static int bgp_ifp_up(struct interface *ifp)
215
0
{
216
0
  struct connected *c;
217
0
  struct nbr_connected *nc;
218
0
  struct listnode *node, *nnode;
219
0
  struct bgp *bgp;
220
221
0
  bgp = ifp->vrf->info;
222
223
0
  bgp_mac_add_mac_entry(ifp);
224
225
0
  if (BGP_DEBUG(zebra, ZEBRA))
226
0
    zlog_debug("Rx Intf up VRF %u IF %s", ifp->vrf->vrf_id,
227
0
         ifp->name);
228
229
0
  if (!bgp)
230
0
    return 0;
231
232
0
  for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
233
0
    bgp_connected_add(bgp, c);
234
235
0
  for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
236
0
    bgp_nbr_connected_add(bgp, nc);
237
238
0
  hook_call(bgp_vrf_status_changed, bgp, ifp);
239
0
  bgp_nht_ifp_up(ifp);
240
241
0
  return 0;
242
0
}
243
244
static int bgp_ifp_down(struct interface *ifp)
245
0
{
246
0
  struct connected *c;
247
0
  struct nbr_connected *nc;
248
0
  struct listnode *node, *nnode;
249
0
  struct bgp *bgp;
250
0
  struct peer *peer;
251
252
0
  bgp = ifp->vrf->info;
253
254
0
  bgp_mac_del_mac_entry(ifp);
255
256
0
  if (BGP_DEBUG(zebra, ZEBRA))
257
0
    zlog_debug("Rx Intf down VRF %u IF %s", ifp->vrf->vrf_id,
258
0
         ifp->name);
259
260
0
  if (!bgp)
261
0
    return 0;
262
263
0
  for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
264
0
    bgp_connected_delete(bgp, c);
265
266
0
  for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
267
0
    bgp_nbr_connected_delete(bgp, nc, 1);
268
269
  /* Fast external-failover */
270
0
  if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
271
272
0
    for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
273
      /* Take down directly connected peers. */
274
0
      if ((peer->ttl != BGP_DEFAULT_TTL)
275
0
          && (peer->gtsm_hops != BGP_GTSM_HOPS_CONNECTED))
276
0
        continue;
277
278
0
      if (ifp == peer->nexthop.ifp) {
279
0
        BGP_EVENT_ADD(peer, BGP_Stop);
280
0
        peer->last_reset = PEER_DOWN_IF_DOWN;
281
0
      }
282
0
    }
283
0
  }
284
285
0
  hook_call(bgp_vrf_status_changed, bgp, ifp);
286
0
  bgp_nht_ifp_down(ifp);
287
288
0
  return 0;
289
0
}
290
291
static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS)
292
0
{
293
0
  struct connected *ifc;
294
0
  struct bgp *bgp;
295
0
  struct peer *peer;
296
0
  struct prefix *addr;
297
0
  struct listnode *node, *nnode;
298
0
  afi_t afi;
299
0
  safi_t safi;
300
301
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
302
303
0
  ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
304
305
0
  if (ifc == NULL)
306
0
    return 0;
307
308
0
  if (bgp_debug_zebra(ifc->address))
309
0
    zlog_debug("Rx Intf address add VRF %u IF %s addr %pFX", vrf_id,
310
0
         ifc->ifp->name, ifc->address);
311
312
0
  if (!bgp)
313
0
    return 0;
314
315
0
  if (if_is_operative(ifc->ifp)) {
316
0
    bgp_connected_add(bgp, ifc);
317
318
    /* If we have learnt of any neighbors on this interface,
319
     * check to kick off any BGP interface-based neighbors,
320
     * but only if this is a link-local address.
321
     */
322
0
    if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
323
0
        && !list_isempty(ifc->ifp->nbr_connected))
324
0
      bgp_start_interface_nbrs(bgp, ifc->ifp);
325
0
    else {
326
0
      addr = ifc->address;
327
328
0
      for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
329
0
        if (addr->family == AF_INET)
330
0
          continue;
331
332
        /*
333
         * If the Peer's interface name matches the
334
         * interface name for which BGP received the
335
         * update and if the received interface address
336
         * is a globalV6 and if the peer is currently
337
         * using a v4-mapped-v6 addr or a link local
338
         * address, then copy the Rxed global v6 addr
339
         * into peer's v6_global and send updates out
340
         * with new nexthop addr.
341
         */
342
0
        if ((peer->conf_if &&
343
0
             (strcmp(peer->conf_if, ifc->ifp->name) ==
344
0
              0)) &&
345
0
            !IN6_IS_ADDR_LINKLOCAL(&addr->u.prefix6) &&
346
0
            ((IS_MAPPED_IPV6(
347
0
               &peer->nexthop.v6_global)) ||
348
0
             IN6_IS_ADDR_LINKLOCAL(
349
0
               &peer->nexthop.v6_global))) {
350
351
0
          if (bgp_debug_zebra(ifc->address)) {
352
0
            zlog_debug(
353
0
              "Update peer %pBP's current intf addr %pI6 and send updates",
354
0
              peer,
355
0
              &peer->nexthop
356
0
                 .v6_global);
357
0
          }
358
0
          memcpy(&peer->nexthop.v6_global,
359
0
                 &addr->u.prefix6,
360
0
                 IPV6_MAX_BYTELEN);
361
0
          FOREACH_AFI_SAFI (afi, safi)
362
0
            bgp_announce_route(peer, afi,
363
0
                   safi, true);
364
0
        }
365
0
      }
366
0
    }
367
0
  }
368
369
0
  return 0;
370
0
}
371
372
static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS)
373
0
{
374
0
  struct listnode *node, *nnode;
375
0
  struct connected *ifc;
376
0
  struct peer *peer;
377
0
  struct bgp *bgp;
378
0
  struct prefix *addr;
379
380
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
381
382
0
  ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
383
384
0
  if (ifc == NULL)
385
0
    return 0;
386
387
0
  if (bgp_debug_zebra(ifc->address))
388
0
    zlog_debug("Rx Intf address del VRF %u IF %s addr %pFX", vrf_id,
389
0
         ifc->ifp->name, ifc->address);
390
391
0
  if (bgp && if_is_operative(ifc->ifp)) {
392
0
    bgp_connected_delete(bgp, ifc);
393
0
  }
394
395
0
  addr = ifc->address;
396
397
0
  if (bgp) {
398
    /*
399
     * When we are using the v6 global as part of the peering
400
     * nexthops and we are removing it, then we need to
401
     * clear the peer data saved for that nexthop and
402
     * cause a re-announcement of the route.  Since
403
     * we do not want the peering to bounce.
404
     */
405
0
    for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
406
0
      afi_t afi;
407
0
      safi_t safi;
408
409
0
      if (addr->family == AF_INET)
410
0
        continue;
411
412
0
      if (!IN6_IS_ADDR_LINKLOCAL(&addr->u.prefix6)
413
0
          && memcmp(&peer->nexthop.v6_global,
414
0
              &addr->u.prefix6, 16)
415
0
               == 0) {
416
0
        memset(&peer->nexthop.v6_global, 0, 16);
417
0
        FOREACH_AFI_SAFI (afi, safi)
418
0
          bgp_announce_route(peer, afi, safi,
419
0
                 true);
420
0
      }
421
0
    }
422
0
  }
423
424
0
  connected_free(&ifc);
425
426
0
  return 0;
427
0
}
428
429
static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS)
430
0
{
431
0
  struct nbr_connected *ifc = NULL;
432
0
  struct bgp *bgp;
433
434
0
  ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id);
435
436
0
  if (ifc == NULL)
437
0
    return 0;
438
439
0
  if (bgp_debug_zebra(ifc->address))
440
0
    zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %pFX",
441
0
         vrf_id, ifc->ifp->name, ifc->address);
442
443
0
  if (if_is_operative(ifc->ifp)) {
444
0
    bgp = bgp_lookup_by_vrf_id(vrf_id);
445
0
    if (bgp)
446
0
      bgp_nbr_connected_add(bgp, ifc);
447
0
  }
448
449
0
  return 0;
450
0
}
451
452
static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS)
453
0
{
454
0
  struct nbr_connected *ifc = NULL;
455
0
  struct bgp *bgp;
456
457
0
  ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id);
458
459
0
  if (ifc == NULL)
460
0
    return 0;
461
462
0
  if (bgp_debug_zebra(ifc->address))
463
0
    zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %pFX",
464
0
         vrf_id, ifc->ifp->name, ifc->address);
465
466
0
  if (if_is_operative(ifc->ifp)) {
467
0
    bgp = bgp_lookup_by_vrf_id(vrf_id);
468
0
    if (bgp)
469
0
      bgp_nbr_connected_delete(bgp, ifc, 0);
470
0
  }
471
472
0
  nbr_connected_free(ifc);
473
474
0
  return 0;
475
0
}
476
477
/* VRF update for an interface. */
478
static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS)
479
0
{
480
0
  struct interface *ifp;
481
0
  vrf_id_t new_vrf_id;
482
0
  struct connected *c;
483
0
  struct nbr_connected *nc;
484
0
  struct listnode *node, *nnode;
485
0
  struct bgp *bgp;
486
0
  struct peer *peer;
487
488
0
  ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
489
0
                &new_vrf_id);
490
0
  if (!ifp)
491
0
    return 0;
492
493
0
  if (BGP_DEBUG(zebra, ZEBRA))
494
0
    zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id,
495
0
         ifp->name, new_vrf_id);
496
497
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
498
499
0
  if (bgp) {
500
0
    for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
501
0
      bgp_connected_delete(bgp, c);
502
503
0
    for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
504
0
      bgp_nbr_connected_delete(bgp, nc, 1);
505
506
    /* Fast external-failover */
507
0
    if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
508
0
      for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
509
0
        if ((peer->ttl != BGP_DEFAULT_TTL)
510
0
            && (peer->gtsm_hops
511
0
          != BGP_GTSM_HOPS_CONNECTED))
512
0
          continue;
513
514
0
        if (ifp == peer->nexthop.ifp)
515
0
          BGP_EVENT_ADD(peer, BGP_Stop);
516
0
      }
517
0
    }
518
0
  }
519
520
0
  if_update_to_new_vrf(ifp, new_vrf_id);
521
522
0
  bgp = bgp_lookup_by_vrf_id(new_vrf_id);
523
0
  if (!bgp)
524
0
    return 0;
525
526
0
  for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
527
0
    bgp_connected_add(bgp, c);
528
529
0
  for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
530
0
    bgp_nbr_connected_add(bgp, nc);
531
532
0
  hook_call(bgp_vrf_status_changed, bgp, ifp);
533
0
  return 0;
534
0
}
535
536
/* Zebra route add and delete treatment. */
537
static int zebra_read_route(ZAPI_CALLBACK_ARGS)
538
0
{
539
0
  enum nexthop_types_t nhtype;
540
0
  enum blackhole_type bhtype = BLACKHOLE_UNSPEC;
541
0
  struct zapi_route api;
542
0
  union g_addr nexthop = {};
543
0
  ifindex_t ifindex;
544
0
  int add, i;
545
0
  struct bgp *bgp;
546
547
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
548
0
  if (!bgp)
549
0
    return 0;
550
551
0
  if (zapi_route_decode(zclient->ibuf, &api) < 0)
552
0
    return -1;
553
554
  /* we completely ignore srcdest routes for now. */
555
0
  if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
556
0
    return 0;
557
558
  /* ignore link-local address. */
559
0
  if (api.prefix.family == AF_INET6
560
0
      && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
561
0
    return 0;
562
563
0
  ifindex = api.nexthops[0].ifindex;
564
0
  nhtype = api.nexthops[0].type;
565
566
  /* api_nh structure has union of gate and bh_type */
567
0
  if (nhtype == NEXTHOP_TYPE_BLACKHOLE) {
568
    /* bh_type is only applicable if NEXTHOP_TYPE_BLACKHOLE*/
569
0
    bhtype = api.nexthops[0].bh_type;
570
0
  } else
571
0
    nexthop = api.nexthops[0].gate;
572
573
0
  add = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
574
0
  if (add) {
575
    /*
576
     * The ADD message is actually an UPDATE and there is no
577
     * explicit DEL
578
     * for a prior redistributed route, if any. So, perform an
579
     * implicit
580
     * DEL processing for the same redistributed route from any
581
     * other
582
     * source type.
583
     */
584
0
    for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
585
0
      if (i != api.type)
586
0
        bgp_redistribute_delete(bgp, &api.prefix, i,
587
0
              api.instance);
588
0
    }
589
590
    /* Now perform the add/update. */
591
0
    bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
592
0
             nhtype, bhtype, api.distance, api.metric,
593
0
             api.type, api.instance, api.tag);
594
0
  } else {
595
0
    bgp_redistribute_delete(bgp, &api.prefix, api.type,
596
0
          api.instance);
597
0
  }
598
599
0
  if (bgp_debug_zebra(&api.prefix)) {
600
0
    char buf[PREFIX_STRLEN];
601
602
0
    if (add) {
603
0
      inet_ntop(api.prefix.family, &nexthop, buf,
604
0
          sizeof(buf));
605
0
      zlog_debug(
606
0
        "Rx route ADD VRF %u %s[%d] %pFX nexthop %s (type %d if %u) metric %u distance %u tag %" ROUTE_TAG_PRI,
607
0
        vrf_id, zebra_route_string(api.type),
608
0
        api.instance, &api.prefix, buf, nhtype, ifindex,
609
0
        api.metric, api.distance, api.tag);
610
0
    } else {
611
0
      zlog_debug("Rx route DEL VRF %u %s[%d] %pFX", vrf_id,
612
0
           zebra_route_string(api.type), api.instance,
613
0
           &api.prefix);
614
0
    }
615
0
  }
616
617
0
  return 0;
618
0
}
619
620
struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
621
0
{
622
0
  struct vrf *vrf;
623
0
  struct listnode *cnode;
624
0
  struct interface *ifp;
625
0
  struct connected *connected;
626
0
  struct prefix_ipv4 p;
627
0
  struct prefix *cp;
628
629
0
  vrf = vrf_lookup_by_id(vrf_id);
630
0
  if (!vrf)
631
0
    return NULL;
632
633
0
  p.family = AF_INET;
634
0
  p.prefix = *addr;
635
0
  p.prefixlen = IPV4_MAX_BITLEN;
636
637
0
  FOR_ALL_INTERFACES (vrf, ifp) {
638
0
    for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
639
0
      cp = connected->address;
640
641
0
      if (cp->family == AF_INET)
642
0
        if (prefix_match(cp, (struct prefix *)&p))
643
0
          return ifp;
644
0
    }
645
0
  }
646
0
  return NULL;
647
0
}
648
649
struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
650
0
{
651
0
  struct vrf *vrf;
652
0
  struct listnode *cnode;
653
0
  struct interface *ifp;
654
0
  struct connected *connected;
655
0
  struct prefix *cp;
656
657
0
  vrf = vrf_lookup_by_id(vrf_id);
658
0
  if (!vrf)
659
0
    return NULL;
660
661
0
  FOR_ALL_INTERFACES (vrf, ifp) {
662
0
    for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
663
0
      cp = connected->address;
664
665
0
      if (cp->family == AF_INET)
666
0
        if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
667
0
          return ifp;
668
0
    }
669
0
  }
670
0
  return NULL;
671
0
}
672
673
struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
674
            vrf_id_t vrf_id)
675
0
{
676
0
  struct vrf *vrf;
677
0
  struct listnode *cnode;
678
0
  struct interface *ifp;
679
0
  struct connected *connected;
680
0
  struct prefix_ipv6 p;
681
0
  struct prefix *cp;
682
683
0
  vrf = vrf_lookup_by_id(vrf_id);
684
0
  if (!vrf)
685
0
    return NULL;
686
687
0
  p.family = AF_INET6;
688
0
  p.prefix = *addr;
689
0
  p.prefixlen = IPV6_MAX_BITLEN;
690
691
0
  FOR_ALL_INTERFACES (vrf, ifp) {
692
0
    for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
693
0
      cp = connected->address;
694
695
0
      if (cp->family == AF_INET6)
696
0
        if (prefix_match(cp, (struct prefix *)&p)) {
697
0
          if (IN6_IS_ADDR_LINKLOCAL(
698
0
                &cp->u.prefix6)) {
699
0
            if (ifindex == ifp->ifindex)
700
0
              return ifp;
701
0
          } else
702
0
            return ifp;
703
0
        }
704
0
    }
705
0
  }
706
0
  return NULL;
707
0
}
708
709
struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
710
            ifindex_t ifindex, vrf_id_t vrf_id)
711
0
{
712
0
  struct vrf *vrf;
713
0
  struct listnode *cnode;
714
0
  struct interface *ifp;
715
0
  struct connected *connected;
716
0
  struct prefix *cp;
717
718
0
  vrf = vrf_lookup_by_id(vrf_id);
719
0
  if (!vrf)
720
0
    return NULL;
721
722
0
  FOR_ALL_INTERFACES (vrf, ifp) {
723
0
    for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
724
0
      cp = connected->address;
725
726
0
      if (cp->family == AF_INET6)
727
0
        if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
728
0
          if (IN6_IS_ADDR_LINKLOCAL(
729
0
                &cp->u.prefix6)) {
730
0
            if (ifindex == ifp->ifindex)
731
0
              return ifp;
732
0
          } else
733
0
            return ifp;
734
0
        }
735
0
    }
736
0
  }
737
0
  return NULL;
738
0
}
739
740
static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
741
0
{
742
0
  struct listnode *cnode;
743
0
  struct connected *connected;
744
0
  struct prefix *cp;
745
746
0
  for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
747
0
    cp = connected->address;
748
749
0
    if (cp->family == AF_INET6)
750
0
      if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
751
0
        memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
752
0
        return 1;
753
0
      }
754
0
  }
755
0
  return 0;
756
0
}
757
758
static bool if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
759
0
{
760
0
  struct listnode *cnode;
761
0
  struct connected *connected;
762
0
  struct prefix *cp;
763
764
0
  for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
765
0
    cp = connected->address;
766
767
0
    if (cp->family == AF_INET6)
768
0
      if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
769
0
        memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
770
0
        return true;
771
0
      }
772
0
  }
773
0
  return false;
774
0
}
775
776
static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
777
0
{
778
0
  struct listnode *cnode;
779
0
  struct connected *connected;
780
0
  struct prefix *cp;
781
782
0
  for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
783
0
    cp = connected->address;
784
0
    if ((cp->family == AF_INET)
785
0
        && !ipv4_martian(&(cp->u.prefix4))) {
786
0
      *addr = cp->u.prefix4;
787
0
      return 1;
788
0
    }
789
0
  }
790
0
  return 0;
791
0
}
792
793
794
bool bgp_zebra_nexthop_set(union sockunion *local, union sockunion *remote,
795
         struct bgp_nexthop *nexthop, struct peer *peer)
796
0
{
797
0
  int ret = 0;
798
0
  struct interface *ifp = NULL;
799
0
  bool v6_ll_avail = true;
800
801
0
  memset(nexthop, 0, sizeof(struct bgp_nexthop));
802
803
0
  if (!local)
804
0
    return false;
805
0
  if (!remote)
806
0
    return false;
807
808
0
  if (local->sa.sa_family == AF_INET) {
809
0
    nexthop->v4 = local->sin.sin_addr;
810
0
    if (peer->update_if)
811
0
      ifp = if_lookup_by_name(peer->update_if,
812
0
            peer->bgp->vrf_id);
813
0
    else
814
0
      ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
815
0
                  peer->bgp->vrf_id);
816
0
  }
817
0
  if (local->sa.sa_family == AF_INET6) {
818
0
    memcpy(&nexthop->v6_global, &local->sin6.sin6_addr, IPV6_MAX_BYTELEN);
819
0
    if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
820
0
      if (peer->conf_if || peer->ifname)
821
0
        ifp = if_lookup_by_name(peer->conf_if
822
0
                ? peer->conf_if
823
0
                : peer->ifname,
824
0
              peer->bgp->vrf_id);
825
0
      else if (peer->update_if)
826
0
        ifp = if_lookup_by_name(peer->update_if,
827
0
              peer->bgp->vrf_id);
828
0
    } else if (peer->update_if)
829
0
      ifp = if_lookup_by_name(peer->update_if,
830
0
            peer->bgp->vrf_id);
831
0
    else
832
0
      ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
833
0
                  local->sin6.sin6_scope_id,
834
0
                  peer->bgp->vrf_id);
835
0
  }
836
837
  /* Handle peerings via loopbacks. For instance, peer between
838
   * 127.0.0.1 and 127.0.0.2. In short, allow peering with self
839
   * via 127.0.0.0/8.
840
   */
841
0
  if (!ifp && cmd_allow_reserved_ranges_get())
842
0
    ifp = if_get_vrf_loopback(peer->bgp->vrf_id);
843
844
0
  if (!ifp) {
845
    /*
846
     * BGP views do not currently get proper data
847
     * from zebra( when attached ) to be able to
848
     * properly resolve nexthops, so give this
849
     * instance type a pass.
850
     */
851
0
    if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
852
0
      return true;
853
    /*
854
     * If we have no interface data but we have established
855
     * some connection w/ zebra than something has gone
856
     * terribly terribly wrong here, so say this failed
857
     * If we do not any zebra connection then not
858
     * having a ifp pointer is ok.
859
     */
860
0
    return zclient_num_connects ? false : true;
861
0
  }
862
863
0
  nexthop->ifp = ifp;
864
865
  /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
866
0
  if (local->sa.sa_family == AF_INET) {
867
    /* IPv6 nexthop*/
868
0
    ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
869
870
0
    if (!ret) {
871
      /* There is no global nexthop. Use link-local address as
872
       * both the
873
       * global and link-local nexthop. In this scenario, the
874
       * expectation
875
       * for interop is that the network admin would use a
876
       * route-map to
877
       * specify the global IPv6 nexthop.
878
       */
879
0
      v6_ll_avail =
880
0
        if_get_ipv6_local(ifp, &nexthop->v6_global);
881
0
      memcpy(&nexthop->v6_local, &nexthop->v6_global,
882
0
             IPV6_MAX_BYTELEN);
883
0
    } else
884
0
      v6_ll_avail =
885
0
        if_get_ipv6_local(ifp, &nexthop->v6_local);
886
887
    /*
888
     * If we are a v4 connection and we are not doing unnumbered
889
     * not having a v6 LL address is ok
890
     */
891
0
    if (!v6_ll_avail && !peer->conf_if)
892
0
      v6_ll_avail = true;
893
0
    if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
894
0
      peer->shared_network = 1;
895
0
    else
896
0
      peer->shared_network = 0;
897
0
  }
898
899
  /* IPv6 connection, fetch and store IPv4 local address if any. */
900
0
  if (local->sa.sa_family == AF_INET6) {
901
0
    struct interface *direct = NULL;
902
903
    /* IPv4 nexthop. */
904
0
    ret = if_get_ipv4_address(ifp, &nexthop->v4);
905
0
    if (!ret && peer->local_id.s_addr != INADDR_ANY)
906
0
      nexthop->v4 = peer->local_id;
907
908
    /* Global address*/
909
0
    if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
910
0
      memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
911
0
             IPV6_MAX_BYTELEN);
912
913
      /* If directly connected set link-local address. */
914
0
      direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
915
0
               remote->sin6.sin6_scope_id,
916
0
               peer->bgp->vrf_id);
917
0
      if (direct)
918
0
        v6_ll_avail = if_get_ipv6_local(
919
0
          ifp, &nexthop->v6_local);
920
      /*
921
       * It's fine to not have a v6 LL when using
922
       * update-source loopback/vrf
923
       */
924
0
      if (!v6_ll_avail && if_is_loopback(ifp))
925
0
        v6_ll_avail = true;
926
0
      else if (!v6_ll_avail) {
927
0
        flog_warn(
928
0
          EC_BGP_NO_LL_ADDRESS_AVAILABLE,
929
0
          "Interface: %s does not have a v6 LL address associated with it, waiting until one is created for it",
930
0
          ifp->name);
931
0
      }
932
0
    } else
933
    /* Link-local address. */
934
0
    {
935
0
      ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
936
937
      /* If there is no global address.  Set link-local
938
         address as
939
         global.  I know this break RFC specification... */
940
      /* In this scenario, the expectation for interop is that
941
       * the
942
       * network admin would use a route-map to specify the
943
       * global
944
       * IPv6 nexthop.
945
       */
946
0
      if (!ret)
947
0
        memcpy(&nexthop->v6_global,
948
0
               &local->sin6.sin6_addr,
949
0
               IPV6_MAX_BYTELEN);
950
      /* Always set the link-local address */
951
0
      memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
952
0
             IPV6_MAX_BYTELEN);
953
0
    }
954
955
0
    if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
956
0
        || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
957
0
           remote->sin6.sin6_scope_id,
958
0
           peer->bgp->vrf_id))
959
0
      peer->shared_network = 1;
960
0
    else
961
0
      peer->shared_network = 0;
962
0
  }
963
964
/* KAME stack specific treatment.  */
965
#ifdef KAME
966
  if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
967
      && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
968
    SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
969
  }
970
  if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
971
      && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
972
    SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
973
  }
974
#endif /* KAME */
975
976
  /* If we have identified the local interface, there is no error for now.
977
   */
978
0
  return v6_ll_avail;
979
0
}
980
981
static struct in6_addr *
982
bgp_path_info_to_ipv6_nexthop(struct bgp_path_info *path, ifindex_t *ifindex)
983
0
{
984
0
  struct in6_addr *nexthop = NULL;
985
986
  /* Only global address nexthop exists. */
987
0
  if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL
988
0
      || path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV6_GLOBAL) {
989
0
    nexthop = &path->attr->mp_nexthop_global;
990
0
    if (IN6_IS_ADDR_LINKLOCAL(nexthop))
991
0
      *ifindex = path->attr->nh_ifindex;
992
0
  }
993
994
  /* If both global and link-local address present. */
995
0
  if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
996
0
      || path->attr->mp_nexthop_len
997
0
           == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
998
    /* Check if route-map is set to prefer global over link-local */
999
0
    if (path->attr->mp_nexthop_prefer_global) {
1000
0
      nexthop = &path->attr->mp_nexthop_global;
1001
0
      if (IN6_IS_ADDR_LINKLOCAL(nexthop))
1002
0
        *ifindex = path->attr->nh_ifindex;
1003
0
    } else {
1004
      /* Workaround for Cisco's nexthop bug.  */
1005
0
      if (IN6_IS_ADDR_UNSPECIFIED(
1006
0
            &path->attr->mp_nexthop_global)
1007
0
          && path->peer->su_remote
1008
0
          && path->peer->su_remote->sa.sa_family
1009
0
               == AF_INET6) {
1010
0
        nexthop =
1011
0
          &path->peer->su_remote->sin6.sin6_addr;
1012
0
        if (IN6_IS_ADDR_LINKLOCAL(nexthop))
1013
0
          *ifindex = path->peer->nexthop.ifp
1014
0
                 ->ifindex;
1015
0
      } else {
1016
0
        nexthop = &path->attr->mp_nexthop_local;
1017
0
        if (IN6_IS_ADDR_LINKLOCAL(nexthop))
1018
0
          *ifindex = path->attr->nh_lla_ifindex;
1019
0
      }
1020
0
    }
1021
0
  }
1022
1023
0
  return nexthop;
1024
0
}
1025
1026
static bool bgp_table_map_apply(struct route_map *map, const struct prefix *p,
1027
        struct bgp_path_info *path)
1028
0
{
1029
0
  route_map_result_t ret;
1030
1031
0
  ret = route_map_apply(map, p, path);
1032
0
  bgp_attr_flush(path->attr);
1033
1034
0
  if (ret != RMAP_DENYMATCH)
1035
0
    return true;
1036
1037
0
  if (bgp_debug_zebra(p)) {
1038
0
    if (p->family == AF_INET) {
1039
0
      zlog_debug(
1040
0
        "Zebra rmap deny: IPv4 route %pFX nexthop %pI4",
1041
0
        p, &path->attr->nexthop);
1042
0
    }
1043
0
    if (p->family == AF_INET6) {
1044
0
      ifindex_t ifindex;
1045
0
      struct in6_addr *nexthop;
1046
1047
0
      nexthop = bgp_path_info_to_ipv6_nexthop(path, &ifindex);
1048
0
      zlog_debug(
1049
0
        "Zebra rmap deny: IPv6 route %pFX nexthop %pI6",
1050
0
        p, nexthop);
1051
0
    }
1052
0
  }
1053
0
  return false;
1054
0
}
1055
1056
static struct event *bgp_tm_thread_connect;
1057
static bool bgp_tm_status_connected;
1058
static bool bgp_tm_chunk_obtained;
1059
0
#define BGP_FLOWSPEC_TABLE_CHUNK 100000
1060
static uint32_t bgp_tm_min, bgp_tm_max, bgp_tm_chunk_size;
1061
struct bgp *bgp_tm_bgp;
1062
1063
static void bgp_zebra_tm_connect(struct event *t)
1064
0
{
1065
0
  struct zclient *zclient;
1066
0
  int delay = 10, ret = 0;
1067
0
1068
0
  zclient = EVENT_ARG(t);
1069
0
  if (bgp_tm_status_connected && zclient->sock > 0)
1070
0
    delay = 60;
1071
0
  else {
1072
0
    bgp_tm_status_connected = false;
1073
0
    ret = tm_table_manager_connect(zclient);
1074
0
  }
1075
0
  if (ret < 0) {
1076
0
    zlog_info("Error connecting to table manager!");
1077
0
    bgp_tm_status_connected = false;
1078
0
  } else {
1079
0
    if (!bgp_tm_status_connected)
1080
0
      zlog_debug("Connecting to table manager. Success");
1081
0
    bgp_tm_status_connected = true;
1082
0
    if (!bgp_tm_chunk_obtained) {
1083
0
      if (bgp_zebra_get_table_range(bgp_tm_chunk_size,
1084
0
                  &bgp_tm_min,
1085
0
                  &bgp_tm_max) >= 0) {
1086
0
        bgp_tm_chunk_obtained = true;
1087
0
        /* parse non installed entries */
1088
0
        bgp_zebra_announce_table(bgp_tm_bgp, AFI_IP, SAFI_FLOWSPEC);
1089
0
      }
1090
0
    }
1091
0
  }
1092
0
  event_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1093
0
      &bgp_tm_thread_connect);
1094
0
}
1095
1096
bool bgp_zebra_tm_chunk_obtained(void)
1097
0
{
1098
0
  return bgp_tm_chunk_obtained;
1099
0
}
1100
1101
uint32_t bgp_zebra_tm_get_id(void)
1102
0
{
1103
0
  static int table_id;
1104
1105
0
  if (!bgp_tm_chunk_obtained)
1106
0
    return ++table_id;
1107
0
  return bgp_tm_min++;
1108
0
}
1109
1110
void bgp_zebra_init_tm_connect(struct bgp *bgp)
1111
0
{
1112
0
  int delay = 1;
1113
1114
  /* if already set, do nothing
1115
   */
1116
0
  if (bgp_tm_thread_connect != NULL)
1117
0
    return;
1118
0
  bgp_tm_status_connected = false;
1119
0
  bgp_tm_chunk_obtained = false;
1120
0
  bgp_tm_min = bgp_tm_max = 0;
1121
0
  bgp_tm_chunk_size = BGP_FLOWSPEC_TABLE_CHUNK;
1122
0
  bgp_tm_bgp = bgp;
1123
0
  event_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1124
0
      &bgp_tm_thread_connect);
1125
0
}
1126
1127
int bgp_zebra_get_table_range(uint32_t chunk_size,
1128
            uint32_t *start, uint32_t *end)
1129
0
{
1130
0
  int ret;
1131
1132
0
  if (!bgp_tm_status_connected)
1133
0
    return -1;
1134
0
  ret = tm_get_table_chunk(zclient, chunk_size, start, end);
1135
0
  if (ret < 0) {
1136
0
    flog_err(EC_BGP_TABLE_CHUNK,
1137
0
       "BGP: Error getting table chunk %u", chunk_size);
1138
0
    return -1;
1139
0
  }
1140
0
  zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1141
0
     chunk_size, *start, *end);
1142
0
  return 0;
1143
0
}
1144
1145
static bool update_ipv4nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1146
              struct in_addr *nexthop,
1147
              struct attr *attr, bool is_evpn,
1148
              struct zapi_nexthop *api_nh)
1149
0
{
1150
0
  api_nh->gate.ipv4 = *nexthop;
1151
0
  api_nh->vrf_id = nh_bgp->vrf_id;
1152
1153
  /* Need to set fields appropriately for EVPN routes imported into
1154
   * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1155
   * connected routes leaked into a VRF.
1156
   */
1157
0
  if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1158
0
    api_nh->type = attr->nh_type;
1159
0
    api_nh->bh_type = attr->bh_type;
1160
0
  } else if (is_evpn) {
1161
    /*
1162
     * If the nexthop is EVPN overlay index gateway IP,
1163
     * treat the nexthop as NEXTHOP_TYPE_IPV4
1164
     * Else, mark the nexthop as onlink.
1165
     */
1166
0
    if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1167
0
      api_nh->type = NEXTHOP_TYPE_IPV4;
1168
0
    else {
1169
0
      api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1170
0
      SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN);
1171
0
      SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1172
0
      api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1173
0
    }
1174
0
  } else if (nh_othervrf && api_nh->gate.ipv4.s_addr == INADDR_ANY) {
1175
0
    api_nh->type = NEXTHOP_TYPE_IFINDEX;
1176
0
    api_nh->ifindex = attr->nh_ifindex;
1177
0
  } else
1178
0
    api_nh->type = NEXTHOP_TYPE_IPV4;
1179
1180
0
  return true;
1181
0
}
1182
1183
static bool update_ipv6nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1184
              struct in6_addr *nexthop,
1185
              ifindex_t ifindex,
1186
              struct bgp_path_info *pi,
1187
              struct bgp_path_info *best_pi,
1188
              bool is_evpn,
1189
              struct zapi_nexthop *api_nh)
1190
0
{
1191
0
  struct attr *attr;
1192
1193
0
  attr = pi->attr;
1194
0
  api_nh->vrf_id = nh_bgp->vrf_id;
1195
1196
0
  if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1197
0
    api_nh->type = attr->nh_type;
1198
0
    api_nh->bh_type = attr->bh_type;
1199
0
  } else if (is_evpn) {
1200
    /*
1201
     * If the nexthop is EVPN overlay index gateway IP,
1202
     * treat the nexthop as NEXTHOP_TYPE_IPV4
1203
     * Else, mark the nexthop as onlink.
1204
     */
1205
0
    if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1206
0
      api_nh->type = NEXTHOP_TYPE_IPV6;
1207
0
    else {
1208
0
      api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1209
0
      SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN);
1210
0
      SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1211
0
      api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1212
0
    }
1213
0
  } else if (nh_othervrf) {
1214
0
    if (IN6_IS_ADDR_UNSPECIFIED(nexthop)) {
1215
0
      api_nh->type = NEXTHOP_TYPE_IFINDEX;
1216
0
      api_nh->ifindex = attr->nh_ifindex;
1217
0
    } else if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1218
0
      if (ifindex == 0)
1219
0
        return false;
1220
0
      api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1221
0
      api_nh->ifindex = ifindex;
1222
0
    } else {
1223
0
      api_nh->type = NEXTHOP_TYPE_IPV6;
1224
0
      api_nh->ifindex = 0;
1225
0
    }
1226
0
  } else {
1227
0
    if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1228
0
      if (pi == best_pi
1229
0
          && attr->mp_nexthop_len
1230
0
               == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1231
0
        if (pi->peer->nexthop.ifp)
1232
0
          ifindex =
1233
0
            pi->peer->nexthop.ifp->ifindex;
1234
0
      if (!ifindex) {
1235
0
        if (pi->peer->conf_if)
1236
0
          ifindex = pi->peer->ifp->ifindex;
1237
0
        else if (pi->peer->ifname)
1238
0
          ifindex = ifname2ifindex(
1239
0
            pi->peer->ifname,
1240
0
            pi->peer->bgp->vrf_id);
1241
0
        else if (pi->peer->nexthop.ifp)
1242
0
          ifindex =
1243
0
            pi->peer->nexthop.ifp->ifindex;
1244
0
      }
1245
1246
0
      if (ifindex == 0)
1247
0
        return false;
1248
0
      api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1249
0
      api_nh->ifindex = ifindex;
1250
0
    } else {
1251
0
      api_nh->type = NEXTHOP_TYPE_IPV6;
1252
0
      api_nh->ifindex = 0;
1253
0
    }
1254
0
  }
1255
  /* api_nh structure has union of gate and bh_type */
1256
0
  if (nexthop && api_nh->type != NEXTHOP_TYPE_BLACKHOLE)
1257
0
    api_nh->gate.ipv6 = *nexthop;
1258
1259
0
  return true;
1260
0
}
1261
1262
static bool bgp_zebra_use_nhop_weighted(struct bgp *bgp, struct attr *attr,
1263
          uint64_t tot_bw, uint32_t *nh_weight)
1264
0
{
1265
0
  uint32_t bw;
1266
0
  uint64_t tmp;
1267
1268
0
  bw = attr->link_bw;
1269
  /* zero link-bandwidth and link-bandwidth not present are treated
1270
   * as the same situation.
1271
   */
1272
0
  if (!bw) {
1273
    /* the only situations should be if we're either told
1274
     * to skip or use default weight.
1275
     */
1276
0
    if (bgp->lb_handling == BGP_LINK_BW_SKIP_MISSING)
1277
0
      return false;
1278
0
    *nh_weight = BGP_ZEBRA_DEFAULT_NHOP_WEIGHT;
1279
0
  } else {
1280
0
    tmp = (uint64_t)bw * 100;
1281
0
    *nh_weight = ((uint32_t)(tmp / tot_bw));
1282
0
  }
1283
1284
0
  return true;
1285
0
}
1286
1287
void bgp_zebra_announce(struct bgp_dest *dest, const struct prefix *p,
1288
      struct bgp_path_info *info, struct bgp *bgp, afi_t afi,
1289
      safi_t safi)
1290
0
{
1291
0
  struct zapi_route api = { 0 };
1292
0
  struct zapi_nexthop *api_nh;
1293
0
  int nh_family;
1294
0
  unsigned int valid_nh_count = 0;
1295
0
  bool allow_recursion = false;
1296
0
  uint8_t distance;
1297
0
  struct peer *peer;
1298
0
  struct bgp_path_info *mpinfo;
1299
0
  struct bgp *bgp_orig;
1300
0
  uint32_t metric;
1301
0
  struct attr local_attr;
1302
0
  struct bgp_path_info local_info;
1303
0
  struct bgp_path_info *mpinfo_cp = &local_info;
1304
0
  route_tag_t tag;
1305
0
  struct bgp_sid_info *sid_info;
1306
0
  mpls_label_t *labels;
1307
0
  uint32_t num_labels = 0;
1308
0
  mpls_label_t nh_label;
1309
0
  int nh_othervrf = 0;
1310
0
  bool nh_updated = false;
1311
0
  bool do_wt_ecmp;
1312
0
  uint64_t cum_bw = 0;
1313
0
  uint32_t nhg_id = 0;
1314
0
  bool is_add;
1315
0
  uint32_t ttl = 0;
1316
0
  uint32_t bos = 0;
1317
0
  uint32_t exp = 0;
1318
1319
  /*
1320
   * BGP is installing this route and bgp has been configured
1321
   * to suppress announcements until the route has been installed
1322
   * let's set the fact that we expect this route to be installed
1323
   */
1324
0
  if (BGP_SUPPRESS_FIB_ENABLED(bgp))
1325
0
    SET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
1326
1327
  /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1328
   * know of this instance.
1329
   */
1330
0
  if (!bgp_install_info_to_zebra(bgp))
1331
0
    return;
1332
1333
0
  if (bgp->main_zebra_update_hold)
1334
0
    return;
1335
1336
0
  if (safi == SAFI_FLOWSPEC) {
1337
0
    bgp_pbr_update_entry(bgp, bgp_dest_get_prefix(dest), info, afi,
1338
0
             safi, true);
1339
0
    return;
1340
0
  }
1341
1342
  /*
1343
   * vrf leaking support (will have only one nexthop)
1344
   */
1345
0
  if (info->extra && info->extra->bgp_orig)
1346
0
    nh_othervrf = 1;
1347
1348
  /* Make Zebra API structure. */
1349
0
  api.vrf_id = bgp->vrf_id;
1350
0
  api.type = ZEBRA_ROUTE_BGP;
1351
0
  api.safi = safi;
1352
0
  api.prefix = *p;
1353
0
  SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1354
1355
0
  peer = info->peer;
1356
1357
0
  if (info->type == ZEBRA_ROUTE_BGP
1358
0
      && info->sub_type == BGP_ROUTE_IMPORTED) {
1359
1360
    /* Obtain peer from parent */
1361
0
    if (info->extra && info->extra->parent)
1362
0
      peer = ((struct bgp_path_info *)(info->extra->parent))
1363
0
               ->peer;
1364
0
  }
1365
1366
0
  tag = info->attr->tag;
1367
1368
0
  if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1369
0
      || info->sub_type == BGP_ROUTE_AGGREGATE) {
1370
0
    SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1371
0
    SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1372
0
  }
1373
1374
0
  if ((peer->sort == BGP_PEER_EBGP && peer->ttl != BGP_DEFAULT_TTL)
1375
0
      || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1376
0
      || CHECK_FLAG(bgp->flags, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1377
1378
0
    allow_recursion = true;
1379
1380
0
  if (info->attr->rmap_table_id) {
1381
0
    SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
1382
0
    api.tableid = info->attr->rmap_table_id;
1383
0
  }
1384
1385
0
  if (CHECK_FLAG(info->attr->flag, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR)))
1386
0
    SET_FLAG(api.message, ZAPI_MESSAGE_SRTE);
1387
1388
  /* Metric is currently based on the best-path only */
1389
0
  metric = info->attr->med;
1390
1391
  /* Determine if we're doing weighted ECMP or not */
1392
0
  do_wt_ecmp = bgp_path_info_mpath_chkwtd(bgp, info);
1393
0
  if (do_wt_ecmp)
1394
0
    cum_bw = bgp_path_info_mpath_cumbw(info);
1395
1396
  /* EVPN MAC-IP routes are installed with a L3 NHG id */
1397
0
  if (bgp_evpn_path_es_use_nhg(bgp, info, &nhg_id)) {
1398
0
    mpinfo = NULL;
1399
0
    api.nhgid = nhg_id;
1400
0
    if (nhg_id)
1401
0
      SET_FLAG(api.message, ZAPI_MESSAGE_NHG);
1402
0
  } else {
1403
0
    mpinfo = info;
1404
0
  }
1405
1406
0
  for (; mpinfo; mpinfo = bgp_path_info_mpath_next(mpinfo)) {
1407
0
    labels = NULL;
1408
0
    num_labels = 0;
1409
0
    uint32_t nh_weight;
1410
0
    bool is_evpn;
1411
0
    bool is_parent_evpn;
1412
1413
0
    if (valid_nh_count >= multipath_num)
1414
0
      break;
1415
1416
0
    *mpinfo_cp = *mpinfo;
1417
0
    nh_weight = 0;
1418
1419
    /* Get nexthop address-family */
1420
0
    if (p->family == AF_INET &&
1421
0
        !BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp->attr))
1422
0
      nh_family = AF_INET;
1423
0
    else if (p->family == AF_INET6 ||
1424
0
       (p->family == AF_INET &&
1425
0
        BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp->attr)))
1426
0
      nh_family = AF_INET6;
1427
0
    else
1428
0
      continue;
1429
1430
    /* If processing for weighted ECMP, determine the next hop's
1431
     * weight. Based on user setting, we may skip the next hop
1432
     * in some situations.
1433
     */
1434
0
    if (do_wt_ecmp) {
1435
0
      if (!bgp_zebra_use_nhop_weighted(bgp, mpinfo->attr,
1436
0
               cum_bw, &nh_weight))
1437
0
        continue;
1438
0
    }
1439
0
    api_nh = &api.nexthops[valid_nh_count];
1440
1441
0
    if (CHECK_FLAG(info->attr->flag,
1442
0
             ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR)))
1443
0
      api_nh->srte_color = info->attr->srte_color;
1444
1445
0
    if (bgp_debug_zebra(&api.prefix)) {
1446
0
      if (mpinfo->extra) {
1447
0
        zlog_debug("%s: p=%pFX, bgp_is_valid_label: %d",
1448
0
             __func__, p,
1449
0
             bgp_is_valid_label(
1450
0
               &mpinfo->extra->label[0]));
1451
0
      } else {
1452
0
        zlog_debug(
1453
0
          "%s: p=%pFX, extra is NULL, no label",
1454
0
          __func__, p);
1455
0
      }
1456
0
    }
1457
1458
0
    if (bgp->table_map[afi][safi].name) {
1459
      /* Copy info and attributes, so the route-map
1460
         apply doesn't modify the BGP route info. */
1461
0
      local_attr = *mpinfo->attr;
1462
0
      mpinfo_cp->attr = &local_attr;
1463
0
      if (!bgp_table_map_apply(bgp->table_map[afi][safi].map,
1464
0
             p, mpinfo_cp))
1465
0
        continue;
1466
1467
      /* metric/tag is only allowed to be
1468
       * overridden on 1st nexthop */
1469
0
      if (mpinfo == info) {
1470
0
        metric = mpinfo_cp->attr->med;
1471
0
        tag = mpinfo_cp->attr->tag;
1472
0
      }
1473
0
    }
1474
1475
0
    BGP_ORIGINAL_UPDATE(bgp_orig, mpinfo, bgp);
1476
1477
0
    is_parent_evpn = is_route_parent_evpn(mpinfo);
1478
1479
0
    if (nh_family == AF_INET) {
1480
0
      nh_updated = update_ipv4nh_for_route_install(
1481
0
        nh_othervrf, bgp_orig,
1482
0
        &mpinfo_cp->attr->nexthop, mpinfo_cp->attr,
1483
0
        is_parent_evpn, api_nh);
1484
0
    } else {
1485
0
      ifindex_t ifindex = IFINDEX_INTERNAL;
1486
0
      struct in6_addr *nexthop;
1487
1488
0
      nexthop = bgp_path_info_to_ipv6_nexthop(mpinfo_cp,
1489
0
                &ifindex);
1490
1491
0
      if (!nexthop)
1492
0
        nh_updated = update_ipv4nh_for_route_install(
1493
0
          nh_othervrf, bgp_orig,
1494
0
          &mpinfo_cp->attr->nexthop,
1495
0
          mpinfo_cp->attr, is_parent_evpn,
1496
0
          api_nh);
1497
0
      else
1498
0
        nh_updated = update_ipv6nh_for_route_install(
1499
0
          nh_othervrf, bgp_orig, nexthop, ifindex,
1500
0
          mpinfo, info, is_parent_evpn, api_nh);
1501
0
    }
1502
1503
0
    is_evpn = !!CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN);
1504
1505
    /* Did we get proper nexthop info to update zebra? */
1506
0
    if (!nh_updated)
1507
0
      continue;
1508
1509
    /* Allow recursion if it is a multipath group with both
1510
     * eBGP and iBGP paths.
1511
     */
1512
0
    if (!allow_recursion
1513
0
        && CHECK_FLAG(bgp->flags, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX)
1514
0
        && (mpinfo->peer->sort == BGP_PEER_IBGP
1515
0
      || mpinfo->peer->sort == BGP_PEER_CONFED))
1516
0
      allow_recursion = true;
1517
1518
0
    if (mpinfo->extra) {
1519
0
      labels = mpinfo->extra->label;
1520
0
      num_labels = mpinfo->extra->num_labels;
1521
0
    }
1522
1523
0
    if (labels && (num_labels > 0) &&
1524
0
        (is_evpn || bgp_is_valid_label(&labels[0]))) {
1525
0
      enum lsp_types_t nh_label_type = ZEBRA_LSP_NONE;
1526
1527
0
      if (is_evpn) {
1528
0
        nh_label = *bgp_evpn_path_info_labels_get_l3vni(
1529
0
          labels, num_labels);
1530
0
        nh_label_type = ZEBRA_LSP_EVPN;
1531
0
      } else {
1532
0
        mpls_lse_decode(labels[0], &nh_label, &ttl,
1533
0
            &exp, &bos);
1534
0
      }
1535
1536
0
      SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL);
1537
0
      api_nh->label_num = 1;
1538
0
      api_nh->label_type = nh_label_type;
1539
0
      api_nh->labels[0] = nh_label;
1540
0
    }
1541
1542
0
    if (is_evpn
1543
0
        && mpinfo->attr->evpn_overlay.type
1544
0
             != OVERLAY_INDEX_GATEWAY_IP)
1545
0
      memcpy(&api_nh->rmac, &(mpinfo->attr->rmac),
1546
0
             sizeof(struct ethaddr));
1547
1548
0
    api_nh->weight = nh_weight;
1549
1550
0
    if (mpinfo->extra && !is_evpn &&
1551
0
        bgp_is_valid_label(&labels[0]) &&
1552
0
        !sid_zero(&mpinfo->extra->sid[0].sid)) {
1553
0
      sid_info = &mpinfo->extra->sid[0];
1554
1555
0
      memcpy(&api_nh->seg6_segs, &sid_info->sid,
1556
0
             sizeof(api_nh->seg6_segs));
1557
1558
0
      if (sid_info->transposition_len != 0) {
1559
0
        mpls_lse_decode(labels[0], &nh_label, &ttl,
1560
0
            &exp, &bos);
1561
1562
0
        if (nh_label < MPLS_LABEL_UNRESERVED_MIN) {
1563
0
          if (bgp_debug_zebra(&api.prefix))
1564
0
            zlog_debug(
1565
0
              "skip invalid SRv6 routes: transposition scheme is used, but label is too small");
1566
0
          continue;
1567
0
        }
1568
1569
0
        transpose_sid(&api_nh->seg6_segs, nh_label,
1570
0
                sid_info->transposition_offset,
1571
0
                sid_info->transposition_len);
1572
0
      }
1573
1574
0
      SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6);
1575
0
    }
1576
1577
0
    valid_nh_count++;
1578
0
  }
1579
1580
0
  is_add = (valid_nh_count || nhg_id) ? true : false;
1581
1582
0
  if (is_add && CHECK_FLAG(bm->flags, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA)) {
1583
0
    struct bgp_zebra_opaque bzo = {};
1584
0
    const char *reason =
1585
0
      bgp_path_selection_reason2str(dest->reason);
1586
1587
0
    strlcpy(bzo.aspath, info->attr->aspath->str,
1588
0
      sizeof(bzo.aspath));
1589
1590
0
    if (info->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
1591
0
      strlcpy(bzo.community,
1592
0
        bgp_attr_get_community(info->attr)->str,
1593
0
        sizeof(bzo.community));
1594
1595
0
    if (info->attr->flag
1596
0
        & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES))
1597
0
      strlcpy(bzo.lcommunity,
1598
0
        bgp_attr_get_lcommunity(info->attr)->str,
1599
0
        sizeof(bzo.lcommunity));
1600
1601
0
    strlcpy(bzo.selection_reason, reason,
1602
0
      sizeof(bzo.selection_reason));
1603
1604
0
    SET_FLAG(api.message, ZAPI_MESSAGE_OPAQUE);
1605
0
    api.opaque.length = MIN(sizeof(struct bgp_zebra_opaque),
1606
0
          ZAPI_MESSAGE_OPAQUE_LENGTH);
1607
0
    memcpy(api.opaque.data, &bzo, api.opaque.length);
1608
0
  }
1609
1610
0
  if (allow_recursion)
1611
0
    SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1612
1613
  /*
1614
   * When we create an aggregate route we must also
1615
   * install a Null0 route in the RIB, so overwrite
1616
   * what was written into api with a blackhole route
1617
   */
1618
0
  if (info->sub_type == BGP_ROUTE_AGGREGATE)
1619
0
    zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
1620
0
  else
1621
0
    api.nexthop_num = valid_nh_count;
1622
1623
0
  SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1624
0
  api.metric = metric;
1625
1626
0
  if (tag) {
1627
0
    SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1628
0
    api.tag = tag;
1629
0
  }
1630
1631
0
  distance = bgp_distance_apply(p, info, afi, safi, bgp);
1632
0
  if (distance) {
1633
0
    SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1634
0
    api.distance = distance;
1635
0
  }
1636
1637
0
  if (bgp_debug_zebra(p)) {
1638
0
    char nh_buf[INET6_ADDRSTRLEN];
1639
0
    char eth_buf[ETHER_ADDR_STRLEN + 7] = {'\0'};
1640
0
    char buf1[ETHER_ADDR_STRLEN];
1641
0
    char label_buf[20];
1642
0
    char sid_buf[20];
1643
0
    char segs_buf[256];
1644
0
    int i;
1645
1646
0
    zlog_debug(
1647
0
      "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI
1648
0
      " count %d nhg %d",
1649
0
      is_add ? "add" : "delete", bgp->vrf_id, &api.prefix,
1650
0
      api.metric, api.tag, api.nexthop_num, nhg_id);
1651
0
    for (i = 0; i < api.nexthop_num; i++) {
1652
0
      api_nh = &api.nexthops[i];
1653
1654
0
      switch (api_nh->type) {
1655
0
      case NEXTHOP_TYPE_IFINDEX:
1656
0
        nh_buf[0] = '\0';
1657
0
        break;
1658
0
      case NEXTHOP_TYPE_IPV4:
1659
0
      case NEXTHOP_TYPE_IPV4_IFINDEX:
1660
0
        nh_family = AF_INET;
1661
0
        inet_ntop(nh_family, &api_nh->gate, nh_buf,
1662
0
            sizeof(nh_buf));
1663
0
        break;
1664
0
      case NEXTHOP_TYPE_IPV6:
1665
0
      case NEXTHOP_TYPE_IPV6_IFINDEX:
1666
0
        nh_family = AF_INET6;
1667
0
        inet_ntop(nh_family, &api_nh->gate, nh_buf,
1668
0
            sizeof(nh_buf));
1669
0
        break;
1670
0
      case NEXTHOP_TYPE_BLACKHOLE:
1671
0
        strlcpy(nh_buf, "blackhole", sizeof(nh_buf));
1672
0
        break;
1673
0
      default:
1674
        /* Note: add new nexthop case */
1675
0
        assert(0);
1676
0
        break;
1677
0
      }
1678
1679
0
      label_buf[0] = '\0';
1680
0
      eth_buf[0] = '\0';
1681
0
      segs_buf[0] = '\0';
1682
0
      if (CHECK_FLAG(api_nh->flags,
1683
0
               ZAPI_NEXTHOP_FLAG_LABEL) &&
1684
0
          !CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN))
1685
0
        snprintf(label_buf, sizeof(label_buf),
1686
0
          "label %u", api_nh->labels[0]);
1687
0
      if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6) &&
1688
0
          !CHECK_FLAG(api_nh->flags,
1689
0
          ZAPI_NEXTHOP_FLAG_EVPN)) {
1690
0
        inet_ntop(AF_INET6, &api_nh->seg6_segs,
1691
0
            sid_buf, sizeof(sid_buf));
1692
0
        snprintf(segs_buf, sizeof(segs_buf), "segs %s",
1693
0
           sid_buf);
1694
0
      }
1695
0
      if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN) &&
1696
0
          !is_zero_mac(&api_nh->rmac))
1697
0
        snprintf(eth_buf, sizeof(eth_buf), " RMAC %s",
1698
0
           prefix_mac2str(&api_nh->rmac,
1699
0
              buf1, sizeof(buf1)));
1700
0
      zlog_debug("  nhop [%d]: %s if %u VRF %u wt %u %s %s %s",
1701
0
           i + 1, nh_buf, api_nh->ifindex,
1702
0
           api_nh->vrf_id, api_nh->weight,
1703
0
           label_buf, segs_buf, eth_buf);
1704
0
    }
1705
1706
0
    int recursion_flag = 0;
1707
1708
0
    if (CHECK_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION))
1709
0
      recursion_flag = 1;
1710
1711
0
    zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)",
1712
0
         __func__, p, (recursion_flag ? "" : "NOT "));
1713
0
  }
1714
0
  zclient_route_send(is_add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
1715
0
         zclient, &api);
1716
0
}
1717
1718
/* Announce all routes of a table to zebra */
1719
void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1720
0
{
1721
0
  struct bgp_dest *dest;
1722
0
  struct bgp_table *table;
1723
0
  struct bgp_path_info *pi;
1724
1725
  /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1726
   * know of this instance.
1727
   */
1728
0
  if (!bgp_install_info_to_zebra(bgp))
1729
0
    return;
1730
1731
0
  table = bgp->rib[afi][safi];
1732
0
  if (!table)
1733
0
    return;
1734
1735
0
  for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1736
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
1737
0
      if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1738
1739
0
          (pi->type == ZEBRA_ROUTE_BGP
1740
0
           && (pi->sub_type == BGP_ROUTE_NORMAL
1741
0
         || pi->sub_type == BGP_ROUTE_IMPORTED)))
1742
1743
0
        bgp_zebra_announce(dest,
1744
0
               bgp_dest_get_prefix(dest),
1745
0
               pi, bgp, afi, safi);
1746
0
}
1747
1748
/* Announce routes of any bgp subtype of a table to zebra */
1749
void bgp_zebra_announce_table_all_subtypes(struct bgp *bgp, afi_t afi,
1750
             safi_t safi)
1751
0
{
1752
0
  struct bgp_dest *dest;
1753
0
  struct bgp_table *table;
1754
0
  struct bgp_path_info *pi;
1755
1756
0
  if (!bgp_install_info_to_zebra(bgp))
1757
0
    return;
1758
1759
0
  table = bgp->rib[afi][safi];
1760
0
  if (!table)
1761
0
    return;
1762
1763
0
  for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1764
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
1765
0
      if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1766
0
          pi->type == ZEBRA_ROUTE_BGP)
1767
0
        bgp_zebra_announce(dest,
1768
0
               bgp_dest_get_prefix(dest),
1769
0
               pi, bgp, afi, safi);
1770
0
}
1771
1772
void bgp_zebra_withdraw(const struct prefix *p, struct bgp_path_info *info,
1773
      struct bgp *bgp, safi_t safi)
1774
0
{
1775
0
  struct zapi_route api;
1776
0
  struct peer *peer;
1777
1778
  /*
1779
   * If we are withdrawing the route, we don't need to have this
1780
   * flag set.  So unset it.
1781
   */
1782
0
  UNSET_FLAG(info->net->flags, BGP_NODE_FIB_INSTALL_PENDING);
1783
1784
  /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1785
   * know of this instance.
1786
   */
1787
0
  if (!bgp_install_info_to_zebra(bgp))
1788
0
    return;
1789
1790
0
  if (safi == SAFI_FLOWSPEC) {
1791
0
    peer = info->peer;
1792
0
    bgp_pbr_update_entry(peer->bgp, p, info, AFI_IP, safi, false);
1793
0
    return;
1794
0
  }
1795
1796
0
  memset(&api, 0, sizeof(api));
1797
0
  api.vrf_id = bgp->vrf_id;
1798
0
  api.type = ZEBRA_ROUTE_BGP;
1799
0
  api.safi = safi;
1800
0
  api.prefix = *p;
1801
1802
0
  if (info->attr->rmap_table_id) {
1803
0
    SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
1804
0
    api.tableid = info->attr->rmap_table_id;
1805
0
  }
1806
1807
0
  if (bgp_debug_zebra(p))
1808
0
    zlog_debug("Tx route delete VRF %u %pFX", bgp->vrf_id,
1809
0
         &api.prefix);
1810
1811
0
  zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
1812
0
}
1813
1814
/* Withdraw all entries in a BGP instances RIB table from Zebra */
1815
void bgp_zebra_withdraw_table_all_subtypes(struct bgp *bgp, afi_t afi, safi_t safi)
1816
0
{
1817
0
  struct bgp_dest *dest;
1818
0
  struct bgp_table *table;
1819
0
  struct bgp_path_info *pi;
1820
1821
0
  if (!bgp_install_info_to_zebra(bgp))
1822
0
    return;
1823
1824
0
  table = bgp->rib[afi][safi];
1825
0
  if (!table)
1826
0
    return;
1827
1828
0
  for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
1829
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
1830
0
      if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
1831
0
          && (pi->type == ZEBRA_ROUTE_BGP))
1832
0
        bgp_zebra_withdraw(bgp_dest_get_prefix(dest),
1833
0
               pi, bgp, safi);
1834
0
    }
1835
0
  }
1836
0
}
1837
1838
struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type,
1839
             unsigned short instance)
1840
0
{
1841
0
  struct list *red_list;
1842
0
  struct listnode *node;
1843
0
  struct bgp_redist *red;
1844
1845
0
  red_list = bgp->redist[afi][type];
1846
0
  if (!red_list)
1847
0
    return (NULL);
1848
1849
0
  for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1850
0
    if (red->instance == instance)
1851
0
      return red;
1852
1853
0
  return NULL;
1854
0
}
1855
1856
struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type,
1857
          unsigned short instance)
1858
0
{
1859
0
  struct list *red_list;
1860
0
  struct bgp_redist *red;
1861
1862
0
  red = bgp_redist_lookup(bgp, afi, type, instance);
1863
0
  if (red)
1864
0
    return red;
1865
1866
0
  if (!bgp->redist[afi][type])
1867
0
    bgp->redist[afi][type] = list_new();
1868
1869
0
  red_list = bgp->redist[afi][type];
1870
0
  red = XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist));
1871
0
  red->instance = instance;
1872
1873
0
  listnode_add(red_list, red);
1874
1875
0
  return red;
1876
0
}
1877
1878
static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type,
1879
         unsigned short instance)
1880
0
{
1881
0
  struct bgp_redist *red;
1882
1883
0
  red = bgp_redist_lookup(bgp, afi, type, instance);
1884
1885
0
  if (red) {
1886
0
    listnode_delete(bgp->redist[afi][type], red);
1887
0
    XFREE(MTYPE_BGP_REDIST, red);
1888
0
    if (!bgp->redist[afi][type]->count)
1889
0
      list_delete(&bgp->redist[afi][type]);
1890
0
  }
1891
0
}
1892
1893
/* Other routes redistribution into BGP. */
1894
int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type,
1895
       unsigned short instance, bool changed)
1896
0
{
1897
  /* If redistribute options are changed call
1898
   * bgp_redistribute_unreg() to reset the option and withdraw
1899
   * the routes
1900
   */
1901
0
  if (changed)
1902
0
    bgp_redistribute_unreg(bgp, afi, type, instance);
1903
1904
  /* Return if already redistribute flag is set. */
1905
0
  if (instance) {
1906
0
    if (redist_check_instance(&zclient->mi_redist[afi][type],
1907
0
            instance))
1908
0
      return CMD_WARNING;
1909
1910
0
    redist_add_instance(&zclient->mi_redist[afi][type], instance);
1911
0
  } else {
1912
0
    if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1913
0
      return CMD_WARNING;
1914
1915
0
#ifdef ENABLE_BGP_VNC
1916
0
    if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
1917
0
      vnc_export_bgp_enable(
1918
0
        bgp, afi); /* only enables if mode bits cfg'd */
1919
0
    }
1920
0
#endif
1921
1922
0
    vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1923
0
  }
1924
1925
  /*
1926
   * Don't try to register if we're not connected to Zebra or Zebra
1927
   * doesn't know of this instance.
1928
   *
1929
   * When we come up later well resend if needed.
1930
   */
1931
0
  if (!bgp_install_info_to_zebra(bgp))
1932
0
    return CMD_SUCCESS;
1933
1934
0
  if (BGP_DEBUG(zebra, ZEBRA))
1935
0
    zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1936
0
         bgp->vrf_id, afi, zebra_route_string(type),
1937
0
         instance);
1938
1939
  /* Send distribute add message to zebra. */
1940
0
  zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1941
0
        instance, bgp->vrf_id);
1942
1943
0
  return CMD_SUCCESS;
1944
0
}
1945
1946
int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1947
          unsigned short instance)
1948
0
{
1949
  /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1950
   * know of this instance.
1951
   */
1952
0
  if (!bgp_install_info_to_zebra(bgp))
1953
0
    return -1;
1954
1955
0
  if (BGP_DEBUG(zebra, ZEBRA))
1956
0
    zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1957
0
         bgp->vrf_id, afi, zebra_route_string(type),
1958
0
         instance);
1959
1960
  /* Send distribute add message to zebra. */
1961
0
  zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1962
0
        instance, bgp->vrf_id);
1963
0
  zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1964
0
        instance, bgp->vrf_id);
1965
1966
0
  return 0;
1967
0
}
1968
1969
/* Redistribute with route-map specification.  */
1970
bool bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name,
1971
             struct route_map *route_map)
1972
0
{
1973
0
  if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1974
0
    return false;
1975
1976
0
  XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1977
  /* Decrement the count for existing routemap and
1978
   * increment the count for new route map.
1979
   */
1980
0
  route_map_counter_decrement(red->rmap.map);
1981
0
  red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1982
0
  red->rmap.map = route_map;
1983
0
  route_map_counter_increment(red->rmap.map);
1984
1985
0
  return true;
1986
0
}
1987
1988
/* Redistribute with metric specification.  */
1989
bool bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1990
         afi_t afi, int type, uint32_t metric)
1991
0
{
1992
0
  struct bgp_dest *dest;
1993
0
  struct bgp_path_info *pi;
1994
1995
0
  if (red->redist_metric_flag && red->redist_metric == metric)
1996
0
    return false;
1997
1998
0
  red->redist_metric_flag = 1;
1999
0
  red->redist_metric = metric;
2000
2001
0
  for (dest = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); dest;
2002
0
       dest = bgp_route_next(dest)) {
2003
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2004
0
      if (pi->sub_type == BGP_ROUTE_REDISTRIBUTE
2005
0
          && pi->type == type
2006
0
          && pi->instance == red->instance) {
2007
0
        struct attr *old_attr;
2008
0
        struct attr new_attr;
2009
2010
0
        new_attr = *pi->attr;
2011
0
        new_attr.med = red->redist_metric;
2012
0
        old_attr = pi->attr;
2013
0
        pi->attr = bgp_attr_intern(&new_attr);
2014
0
        bgp_attr_unintern(&old_attr);
2015
2016
0
        bgp_path_info_set_flag(dest, pi,
2017
0
                   BGP_PATH_ATTR_CHANGED);
2018
0
        bgp_process(bgp, dest, afi, SAFI_UNICAST);
2019
0
      }
2020
0
    }
2021
0
  }
2022
2023
0
  return true;
2024
0
}
2025
2026
/* Unset redistribution.  */
2027
int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
2028
         unsigned short instance)
2029
0
{
2030
0
  struct bgp_redist *red;
2031
2032
0
  red = bgp_redist_lookup(bgp, afi, type, instance);
2033
0
  if (!red)
2034
0
    return CMD_SUCCESS;
2035
2036
  /* Return if zebra connection is disabled. */
2037
0
  if (instance) {
2038
0
    if (!redist_check_instance(&zclient->mi_redist[afi][type],
2039
0
             instance))
2040
0
      return CMD_WARNING;
2041
0
    redist_del_instance(&zclient->mi_redist[afi][type], instance);
2042
0
  } else {
2043
0
    if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
2044
0
      return CMD_WARNING;
2045
0
    vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
2046
0
  }
2047
2048
0
  if (bgp_install_info_to_zebra(bgp)) {
2049
    /* Send distribute delete message to zebra. */
2050
0
    if (BGP_DEBUG(zebra, ZEBRA))
2051
0
      zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
2052
0
           bgp->vrf_id, afi, zebra_route_string(type),
2053
0
           instance);
2054
0
    zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
2055
0
          type, instance, bgp->vrf_id);
2056
0
  }
2057
2058
  /* Withdraw redistributed routes from current BGP's routing table. */
2059
0
  bgp_redistribute_withdraw(bgp, afi, type, instance);
2060
2061
0
  return CMD_SUCCESS;
2062
0
}
2063
2064
/* Unset redistribution.  */
2065
int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
2066
         unsigned short instance)
2067
0
{
2068
0
  struct bgp_redist *red;
2069
2070
/*
2071
 * vnc and vpn->vrf checks must be before red check because
2072
 * they operate within bgpd irrespective of zebra connection
2073
 * status. red lookup fails if there is no zebra connection.
2074
 */
2075
0
#ifdef ENABLE_BGP_VNC
2076
0
  if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
2077
0
    vnc_export_bgp_disable(bgp, afi);
2078
0
  }
2079
0
#endif
2080
2081
0
  red = bgp_redist_lookup(bgp, afi, type, instance);
2082
0
  if (!red)
2083
0
    return CMD_SUCCESS;
2084
2085
0
  bgp_redistribute_unreg(bgp, afi, type, instance);
2086
2087
  /* Unset route-map. */
2088
0
  XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
2089
0
  route_map_counter_decrement(red->rmap.map);
2090
0
  red->rmap.map = NULL;
2091
2092
  /* Unset metric. */
2093
0
  red->redist_metric_flag = 0;
2094
0
  red->redist_metric = 0;
2095
2096
0
  bgp_redist_del(bgp, afi, type, instance);
2097
2098
0
  return CMD_SUCCESS;
2099
0
}
2100
2101
void bgp_redistribute_redo(struct bgp *bgp)
2102
0
{
2103
0
  afi_t afi;
2104
0
  int i;
2105
0
  struct list *red_list;
2106
0
  struct listnode *node;
2107
0
  struct bgp_redist *red;
2108
2109
0
  for (afi = AFI_IP; afi < AFI_MAX; afi++) {
2110
0
    for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
2111
2112
0
      red_list = bgp->redist[afi][i];
2113
0
      if (!red_list)
2114
0
        continue;
2115
2116
0
      for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
2117
0
        bgp_redistribute_resend(bgp, afi, i,
2118
0
              red->instance);
2119
0
      }
2120
0
    }
2121
0
  }
2122
0
}
2123
2124
void bgp_zclient_reset(void)
2125
0
{
2126
0
  zclient_reset(zclient);
2127
0
}
2128
2129
/* Register this instance with Zebra. Invoked upon connect (for
2130
 * default instance) and when other VRFs are learnt (or created and
2131
 * already learnt).
2132
 */
2133
void bgp_zebra_instance_register(struct bgp *bgp)
2134
1
{
2135
  /* Don't try to register if we're not connected to Zebra */
2136
1
  if (!zclient || zclient->sock < 0)
2137
1
    return;
2138
2139
0
  if (BGP_DEBUG(zebra, ZEBRA))
2140
0
    zlog_debug("Registering VRF %u", bgp->vrf_id);
2141
2142
  /* Register for router-id, interfaces, redistributed routes. */
2143
0
  zclient_send_reg_requests(zclient, bgp->vrf_id);
2144
2145
  /* For EVPN instance, register to learn about VNIs, if appropriate. */
2146
0
  if (bgp->advertise_all_vni)
2147
0
    bgp_zebra_advertise_all_vni(bgp, 1);
2148
2149
0
  bgp_nht_register_nexthops(bgp);
2150
0
}
2151
2152
/* Deregister this instance with Zebra. Invoked upon the instance
2153
 * being deleted (default or VRF) and it is already registered.
2154
 */
2155
void bgp_zebra_instance_deregister(struct bgp *bgp)
2156
0
{
2157
  /* Don't try to deregister if we're not connected to Zebra */
2158
0
  if (zclient->sock < 0)
2159
0
    return;
2160
2161
0
  if (BGP_DEBUG(zebra, ZEBRA))
2162
0
    zlog_debug("Deregistering VRF %u", bgp->vrf_id);
2163
2164
  /* For EVPN instance, unregister learning about VNIs, if appropriate. */
2165
0
  if (bgp->advertise_all_vni)
2166
0
    bgp_zebra_advertise_all_vni(bgp, 0);
2167
2168
  /* Deregister for router-id, interfaces, redistributed routes. */
2169
0
  zclient_send_dereg_requests(zclient, bgp->vrf_id);
2170
0
}
2171
2172
void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
2173
0
{
2174
0
  uint32_t ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
2175
2176
  /* Don't try to initiate if we're not connected to Zebra */
2177
0
  if (zclient->sock < 0)
2178
0
    return;
2179
2180
0
  if (BGP_DEBUG(zebra, ZEBRA))
2181
0
    zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
2182
0
         peer->host);
2183
2184
  /*
2185
   * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
2186
   * If we don't have an ifp pointer, call function to find the
2187
   * ifps for a numbered enhe peer to turn RAs on.
2188
   */
2189
0
  peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2190
0
                peer->ifp, 1, ra_interval)
2191
0
      : bgp_nht_reg_enhe_cap_intfs(peer);
2192
0
}
2193
2194
void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
2195
0
{
2196
  /* Don't try to terminate if we're not connected to Zebra */
2197
0
  if (zclient->sock < 0)
2198
0
    return;
2199
2200
0
  if (BGP_DEBUG(zebra, ZEBRA))
2201
0
    zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
2202
0
         peer->host);
2203
2204
  /*
2205
   * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2206
   * If we don't have an ifp pointer, call function to find the
2207
   * ifps for a numbered enhe peer to turn RAs off.
2208
   */
2209
0
  peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2210
0
                peer->ifp, 0, 0)
2211
0
      : bgp_nht_dereg_enhe_cap_intfs(peer);
2212
0
}
2213
2214
int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
2215
0
{
2216
0
  struct stream *s = NULL;
2217
2218
  /* Check socket. */
2219
0
  if (!zclient || zclient->sock < 0)
2220
0
    return 0;
2221
2222
  /* Don't try to register if Zebra doesn't know of this instance. */
2223
0
  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2224
0
    if (BGP_DEBUG(zebra, ZEBRA))
2225
0
      zlog_debug(
2226
0
        "%s: No zebra instance to talk to, cannot advertise subnet",
2227
0
        __func__);
2228
0
    return 0;
2229
0
  }
2230
2231
0
  s = zclient->obuf;
2232
0
  stream_reset(s);
2233
2234
0
  zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
2235
0
  stream_putc(s, advertise);
2236
0
  stream_put3(s, vni);
2237
0
  stream_putw_at(s, 0, stream_get_endp(s));
2238
2239
0
  return zclient_send_message(zclient);
2240
0
}
2241
2242
int bgp_zebra_advertise_svi_macip(struct bgp *bgp, int advertise, vni_t vni)
2243
0
{
2244
0
  struct stream *s = NULL;
2245
2246
  /* Check socket. */
2247
0
  if (!zclient || zclient->sock < 0)
2248
0
    return 0;
2249
2250
  /* Don't try to register if Zebra doesn't know of this instance. */
2251
0
  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2252
0
    return 0;
2253
2254
0
  s = zclient->obuf;
2255
0
  stream_reset(s);
2256
2257
0
  zclient_create_header(s, ZEBRA_ADVERTISE_SVI_MACIP, bgp->vrf_id);
2258
0
  stream_putc(s, advertise);
2259
0
  stream_putl(s, vni);
2260
0
  stream_putw_at(s, 0, stream_get_endp(s));
2261
2262
0
  return zclient_send_message(zclient);
2263
0
}
2264
2265
int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
2266
0
{
2267
0
  struct stream *s = NULL;
2268
2269
  /* Check socket. */
2270
0
  if (!zclient || zclient->sock < 0)
2271
0
    return 0;
2272
2273
  /* Don't try to register if Zebra doesn't know of this instance. */
2274
0
  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2275
0
    if (BGP_DEBUG(zebra, ZEBRA))
2276
0
      zlog_debug(
2277
0
        "%s: No zebra instance to talk to, not installing gw_macip",
2278
0
        __func__);
2279
0
    return 0;
2280
0
  }
2281
2282
0
  s = zclient->obuf;
2283
0
  stream_reset(s);
2284
2285
0
  zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
2286
0
  stream_putc(s, advertise);
2287
0
  stream_putl(s, vni);
2288
0
  stream_putw_at(s, 0, stream_get_endp(s));
2289
2290
0
  return zclient_send_message(zclient);
2291
0
}
2292
2293
int bgp_zebra_vxlan_flood_control(struct bgp *bgp,
2294
          enum vxlan_flood_control flood_ctrl)
2295
0
{
2296
0
  struct stream *s;
2297
2298
  /* Check socket. */
2299
0
  if (!zclient || zclient->sock < 0)
2300
0
    return 0;
2301
2302
  /* Don't try to register if Zebra doesn't know of this instance. */
2303
0
  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2304
0
    if (BGP_DEBUG(zebra, ZEBRA))
2305
0
      zlog_debug(
2306
0
        "%s: No zebra instance to talk to, not installing all vni",
2307
0
        __func__);
2308
0
    return 0;
2309
0
  }
2310
2311
0
  s = zclient->obuf;
2312
0
  stream_reset(s);
2313
2314
0
  zclient_create_header(s, ZEBRA_VXLAN_FLOOD_CONTROL, bgp->vrf_id);
2315
0
  stream_putc(s, flood_ctrl);
2316
0
  stream_putw_at(s, 0, stream_get_endp(s));
2317
2318
0
  return zclient_send_message(zclient);
2319
0
}
2320
2321
int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
2322
0
{
2323
0
  struct stream *s;
2324
2325
  /* Check socket. */
2326
0
  if (!zclient || zclient->sock < 0)
2327
0
    return 0;
2328
2329
  /* Don't try to register if Zebra doesn't know of this instance. */
2330
0
  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2331
0
    return 0;
2332
2333
0
  s = zclient->obuf;
2334
0
  stream_reset(s);
2335
2336
0
  zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
2337
0
  stream_putc(s, advertise);
2338
  /* Also inform current BUM handling setting. This is really
2339
   * relevant only when 'advertise' is set.
2340
   */
2341
0
  stream_putc(s, bgp->vxlan_flood_ctrl);
2342
0
  stream_putw_at(s, 0, stream_get_endp(s));
2343
2344
0
  return zclient_send_message(zclient);
2345
0
}
2346
2347
int bgp_zebra_dup_addr_detection(struct bgp *bgp)
2348
1
{
2349
1
  struct stream *s;
2350
2351
  /* Check socket. */
2352
1
  if (!zclient || zclient->sock < 0)
2353
1
    return 0;
2354
2355
  /* Don't try to register if Zebra doesn't know of this instance. */
2356
0
  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2357
0
    return 0;
2358
2359
0
  if (BGP_DEBUG(zebra, ZEBRA))
2360
0
    zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2361
0
         bgp->evpn_info->dup_addr_detect ?
2362
0
         "enable" : "disable",
2363
0
         bgp->evpn_info->dad_max_moves,
2364
0
         bgp->evpn_info->dad_time,
2365
0
         bgp->evpn_info->dad_freeze ?
2366
0
         "enable" : "disable",
2367
0
         bgp->evpn_info->dad_freeze_time);
2368
2369
0
  s = zclient->obuf;
2370
0
  stream_reset(s);
2371
0
  zclient_create_header(s, ZEBRA_DUPLICATE_ADDR_DETECTION,
2372
0
            bgp->vrf_id);
2373
0
  stream_putl(s, bgp->evpn_info->dup_addr_detect);
2374
0
  stream_putl(s, bgp->evpn_info->dad_time);
2375
0
  stream_putl(s, bgp->evpn_info->dad_max_moves);
2376
0
  stream_putl(s, bgp->evpn_info->dad_freeze);
2377
0
  stream_putl(s, bgp->evpn_info->dad_freeze_time);
2378
0
  stream_putw_at(s, 0, stream_get_endp(s));
2379
2380
0
  return zclient_send_message(zclient);
2381
0
}
2382
2383
static int rule_notify_owner(ZAPI_CALLBACK_ARGS)
2384
0
{
2385
0
  uint32_t seqno, priority, unique;
2386
0
  enum zapi_rule_notify_owner note;
2387
0
  struct bgp_pbr_action *bgp_pbra;
2388
0
  struct bgp_pbr_rule *bgp_pbr = NULL;
2389
0
  char ifname[INTERFACE_NAMSIZ + 1];
2390
2391
0
  if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
2392
0
             ifname, &note))
2393
0
    return -1;
2394
2395
0
  bgp_pbra = bgp_pbr_action_rule_lookup(vrf_id, unique);
2396
0
  if (!bgp_pbra) {
2397
    /* look in bgp pbr rule */
2398
0
    bgp_pbr = bgp_pbr_rule_lookup(vrf_id, unique);
2399
0
    if (!bgp_pbr && note != ZAPI_RULE_REMOVED) {
2400
0
      if (BGP_DEBUG(zebra, ZEBRA))
2401
0
        zlog_debug("%s: Fail to look BGP rule (%u)",
2402
0
             __func__, unique);
2403
0
      return 0;
2404
0
    }
2405
0
  }
2406
2407
0
  switch (note) {
2408
0
  case ZAPI_RULE_FAIL_INSTALL:
2409
0
    if (BGP_DEBUG(zebra, ZEBRA))
2410
0
      zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__);
2411
0
    if (bgp_pbra) {
2412
0
      bgp_pbra->installed = false;
2413
0
      bgp_pbra->install_in_progress = false;
2414
0
    } else {
2415
0
      bgp_pbr->installed = false;
2416
0
      bgp_pbr->install_in_progress = false;
2417
0
    }
2418
0
    break;
2419
0
  case ZAPI_RULE_INSTALLED:
2420
0
    if (bgp_pbra) {
2421
0
      bgp_pbra->installed = true;
2422
0
      bgp_pbra->install_in_progress = false;
2423
0
    } else {
2424
0
      struct bgp_path_info *path;
2425
0
      struct bgp_path_info_extra *extra;
2426
2427
0
      bgp_pbr->installed = true;
2428
0
      bgp_pbr->install_in_progress = false;
2429
0
      bgp_pbr->action->refcnt++;
2430
      /* link bgp_info to bgp_pbr */
2431
0
      path = (struct bgp_path_info *)bgp_pbr->path;
2432
0
      extra = bgp_path_info_extra_get(path);
2433
0
      listnode_add_force(&extra->bgp_fs_iprule,
2434
0
             bgp_pbr);
2435
0
    }
2436
0
    if (BGP_DEBUG(zebra, ZEBRA))
2437
0
      zlog_debug("%s: Received RULE_INSTALLED", __func__);
2438
0
    break;
2439
0
  case ZAPI_RULE_FAIL_REMOVE:
2440
0
  case ZAPI_RULE_REMOVED:
2441
0
    if (BGP_DEBUG(zebra, ZEBRA))
2442
0
      zlog_debug("%s: Received RULE REMOVED", __func__);
2443
0
    break;
2444
0
  }
2445
2446
0
  return 0;
2447
0
}
2448
2449
static int ipset_notify_owner(ZAPI_CALLBACK_ARGS)
2450
0
{
2451
0
  uint32_t unique;
2452
0
  enum zapi_ipset_notify_owner note;
2453
0
  struct bgp_pbr_match *bgp_pbim;
2454
2455
0
  if (!zapi_ipset_notify_decode(zclient->ibuf,
2456
0
              &unique,
2457
0
              &note))
2458
0
    return -1;
2459
2460
0
  bgp_pbim = bgp_pbr_match_ipset_lookup(vrf_id, unique);
2461
0
  if (!bgp_pbim) {
2462
0
    if (BGP_DEBUG(zebra, ZEBRA))
2463
0
      zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2464
0
           __func__, note, unique);
2465
0
    return 0;
2466
0
  }
2467
2468
0
  switch (note) {
2469
0
  case ZAPI_IPSET_FAIL_INSTALL:
2470
0
    if (BGP_DEBUG(zebra, ZEBRA))
2471
0
      zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__);
2472
0
    bgp_pbim->installed = false;
2473
0
    bgp_pbim->install_in_progress = false;
2474
0
    break;
2475
0
  case ZAPI_IPSET_INSTALLED:
2476
0
    bgp_pbim->installed = true;
2477
0
    bgp_pbim->install_in_progress = false;
2478
0
    if (BGP_DEBUG(zebra, ZEBRA))
2479
0
      zlog_debug("%s: Received IPSET_INSTALLED", __func__);
2480
0
    break;
2481
0
  case ZAPI_IPSET_FAIL_REMOVE:
2482
0
  case ZAPI_IPSET_REMOVED:
2483
0
    if (BGP_DEBUG(zebra, ZEBRA))
2484
0
      zlog_debug("%s: Received IPSET REMOVED", __func__);
2485
0
    break;
2486
0
  }
2487
2488
0
  return 0;
2489
0
}
2490
2491
static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS)
2492
0
{
2493
0
  uint32_t unique;
2494
0
  char ipset_name[ZEBRA_IPSET_NAME_SIZE];
2495
0
  enum zapi_ipset_entry_notify_owner note;
2496
0
  struct bgp_pbr_match_entry *bgp_pbime;
2497
2498
0
  if (!zapi_ipset_entry_notify_decode(
2499
0
        zclient->ibuf,
2500
0
        &unique,
2501
0
        ipset_name,
2502
0
        &note))
2503
0
    return -1;
2504
0
  bgp_pbime = bgp_pbr_match_ipset_entry_lookup(vrf_id,
2505
0
                 ipset_name,
2506
0
                 unique);
2507
0
  if (!bgp_pbime) {
2508
0
    if (BGP_DEBUG(zebra, ZEBRA))
2509
0
      zlog_debug(
2510
0
        "%s: Fail to look BGP match entry (%u, ID %u)",
2511
0
        __func__, note, unique);
2512
0
    return 0;
2513
0
  }
2514
2515
0
  switch (note) {
2516
0
  case ZAPI_IPSET_ENTRY_FAIL_INSTALL:
2517
0
    if (BGP_DEBUG(zebra, ZEBRA))
2518
0
      zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2519
0
           __func__);
2520
0
    bgp_pbime->installed = false;
2521
0
    bgp_pbime->install_in_progress = false;
2522
0
    break;
2523
0
  case ZAPI_IPSET_ENTRY_INSTALLED:
2524
0
    {
2525
0
    struct bgp_path_info *path;
2526
0
    struct bgp_path_info_extra *extra;
2527
2528
0
    bgp_pbime->installed = true;
2529
0
    bgp_pbime->install_in_progress = false;
2530
0
    if (BGP_DEBUG(zebra, ZEBRA))
2531
0
      zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2532
0
           __func__);
2533
    /* link bgp_path_info to bpme */
2534
0
    path = (struct bgp_path_info *)bgp_pbime->path;
2535
0
    extra = bgp_path_info_extra_get(path);
2536
0
    listnode_add_force(&extra->bgp_fs_pbr, bgp_pbime);
2537
0
    }
2538
0
    break;
2539
0
  case ZAPI_IPSET_ENTRY_FAIL_REMOVE:
2540
0
  case ZAPI_IPSET_ENTRY_REMOVED:
2541
0
    if (BGP_DEBUG(zebra, ZEBRA))
2542
0
      zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2543
0
           __func__);
2544
0
    break;
2545
0
  }
2546
0
  return 0;
2547
0
}
2548
2549
static int iptable_notify_owner(ZAPI_CALLBACK_ARGS)
2550
0
{
2551
0
  uint32_t unique;
2552
0
  enum zapi_iptable_notify_owner note;
2553
0
  struct bgp_pbr_match *bgpm;
2554
2555
0
  if (!zapi_iptable_notify_decode(
2556
0
          zclient->ibuf,
2557
0
          &unique,
2558
0
          &note))
2559
0
    return -1;
2560
0
  bgpm = bgp_pbr_match_iptable_lookup(vrf_id, unique);
2561
0
  if (!bgpm) {
2562
0
    if (BGP_DEBUG(zebra, ZEBRA))
2563
0
      zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2564
0
           __func__, note, unique);
2565
0
    return 0;
2566
0
  }
2567
0
  switch (note) {
2568
0
  case ZAPI_IPTABLE_FAIL_INSTALL:
2569
0
    if (BGP_DEBUG(zebra, ZEBRA))
2570
0
      zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2571
0
           __func__);
2572
0
    bgpm->installed_in_iptable = false;
2573
0
    bgpm->install_iptable_in_progress = false;
2574
0
    break;
2575
0
  case ZAPI_IPTABLE_INSTALLED:
2576
0
    bgpm->installed_in_iptable = true;
2577
0
    bgpm->install_iptable_in_progress = false;
2578
0
    if (BGP_DEBUG(zebra, ZEBRA))
2579
0
      zlog_debug("%s: Received IPTABLE_INSTALLED", __func__);
2580
0
    bgpm->action->refcnt++;
2581
0
    break;
2582
0
  case ZAPI_IPTABLE_FAIL_REMOVE:
2583
0
  case ZAPI_IPTABLE_REMOVED:
2584
0
    if (BGP_DEBUG(zebra, ZEBRA))
2585
0
      zlog_debug("%s: Received IPTABLE REMOVED", __func__);
2586
0
    break;
2587
0
  }
2588
0
  return 0;
2589
0
}
2590
2591
/* Process route notification messages from RIB */
2592
static int bgp_zebra_route_notify_owner(int command, struct zclient *zclient,
2593
          zebra_size_t length, vrf_id_t vrf_id)
2594
0
{
2595
0
  struct prefix p;
2596
0
  enum zapi_route_notify_owner note;
2597
0
  uint32_t table_id;
2598
0
  afi_t afi;
2599
0
  safi_t safi;
2600
0
  struct bgp_dest *dest;
2601
0
  struct bgp *bgp;
2602
0
  struct bgp_path_info *pi, *new_select;
2603
2604
0
  if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note,
2605
0
              &afi, &safi)) {
2606
0
    zlog_err("%s : error in msg decode", __func__);
2607
0
    return -1;
2608
0
  }
2609
2610
  /* Get the bgp instance */
2611
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
2612
0
  if (!bgp) {
2613
0
    flog_err(EC_BGP_INVALID_BGP_INSTANCE,
2614
0
       "%s : bgp instance not found vrf %d", __func__,
2615
0
       vrf_id);
2616
0
    return -1;
2617
0
  }
2618
2619
  /* Find the bgp route node */
2620
0
  dest = bgp_safi_node_lookup(bgp->rib[afi][safi], safi, &p,
2621
0
            &bgp->vrf_prd);
2622
0
  if (!dest)
2623
0
    return -1;
2624
2625
0
  switch (note) {
2626
0
  case ZAPI_ROUTE_INSTALLED:
2627
0
    new_select = NULL;
2628
    /* Clear the flags so that route can be processed */
2629
0
    UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2630
0
    SET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2631
0
    if (BGP_DEBUG(zebra, ZEBRA))
2632
0
      zlog_debug("route %pRN : INSTALLED", dest);
2633
    /* Find the best route */
2634
0
    for (pi = dest->info; pi; pi = pi->next) {
2635
      /* Process aggregate route */
2636
0
      bgp_aggregate_increment(bgp, &p, pi, afi, safi);
2637
0
      if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2638
0
        new_select = pi;
2639
0
    }
2640
    /* Advertise the route */
2641
0
    if (new_select)
2642
0
      group_announce_route(bgp, afi, safi, dest, new_select);
2643
0
    else {
2644
0
      flog_err(EC_BGP_INVALID_ROUTE,
2645
0
         "selected route %pRN not found", dest);
2646
2647
0
      bgp_dest_unlock_node(dest);
2648
0
      return -1;
2649
0
    }
2650
0
    break;
2651
0
  case ZAPI_ROUTE_REMOVED:
2652
    /* Route deleted from dataplane, reset the installed flag
2653
     * so that route can be reinstalled when client sends
2654
     * route add later
2655
     */
2656
0
    UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2657
0
    if (BGP_DEBUG(zebra, ZEBRA))
2658
0
      zlog_debug("route %pRN: Removed from Fib", dest);
2659
0
    break;
2660
0
  case ZAPI_ROUTE_FAIL_INSTALL:
2661
0
    new_select = NULL;
2662
0
    if (BGP_DEBUG(zebra, ZEBRA))
2663
0
      zlog_debug("route: %pRN Failed to Install into Fib",
2664
0
           dest);
2665
0
    UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2666
0
    UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2667
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2668
0
      if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2669
0
        new_select = pi;
2670
0
    }
2671
0
    if (new_select)
2672
0
      group_announce_route(bgp, afi, safi, dest, new_select);
2673
    /* Error will be logged by zebra module */
2674
0
    break;
2675
0
  case ZAPI_ROUTE_BETTER_ADMIN_WON:
2676
0
    if (BGP_DEBUG(zebra, ZEBRA))
2677
0
      zlog_debug("route: %pRN removed due to better admin won",
2678
0
           dest);
2679
0
    new_select = NULL;
2680
0
    UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2681
0
    UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2682
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2683
0
      bgp_aggregate_decrement(bgp, &p, pi, afi, safi);
2684
0
      if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2685
0
        new_select = pi;
2686
0
    }
2687
0
    if (new_select)
2688
0
      group_announce_route(bgp, afi, safi, dest, new_select);
2689
    /* No action required */
2690
0
    break;
2691
0
  case ZAPI_ROUTE_REMOVE_FAIL:
2692
0
    zlog_warn("%s: Route %pRN failure to remove",
2693
0
        __func__, dest);
2694
0
    break;
2695
0
  }
2696
2697
0
  bgp_dest_unlock_node(dest);
2698
0
  return 0;
2699
0
}
2700
2701
/* this function is used to forge ip rule,
2702
 * - either for iptable/ipset using fwmark id
2703
 * - or for sample ip rule cmd
2704
 */
2705
static void bgp_encode_pbr_rule_action(struct stream *s,
2706
               struct bgp_pbr_action *pbra,
2707
               struct bgp_pbr_rule *pbr)
2708
0
{
2709
0
  struct prefix pfx;
2710
0
  uint8_t fam = AF_INET;
2711
0
  char ifname[INTERFACE_NAMSIZ];
2712
2713
0
  if (pbra->nh.type == NEXTHOP_TYPE_IPV6)
2714
0
    fam = AF_INET6;
2715
0
  stream_putl(s, 0); /* seqno unused */
2716
0
  if (pbr)
2717
0
    stream_putl(s, pbr->priority);
2718
0
  else
2719
0
    stream_putl(s, 0);
2720
  /* ruleno unused - priority change
2721
   * ruleno permits distinguishing various FS PBR entries
2722
   * - FS PBR entries based on ipset/iptables
2723
   * - FS PBR entries based on iprule
2724
   * the latter may contain default routing information injected by FS
2725
   */
2726
0
  if (pbr)
2727
0
    stream_putl(s, pbr->unique);
2728
0
  else
2729
0
    stream_putl(s, pbra->unique);
2730
0
  stream_putc(s, 0); /* ip protocol being used */
2731
0
  if (pbr && pbr->flags & MATCH_IP_SRC_SET)
2732
0
    memcpy(&pfx, &(pbr->src), sizeof(struct prefix));
2733
0
  else {
2734
0
    memset(&pfx, 0, sizeof(pfx));
2735
0
    pfx.family = fam;
2736
0
  }
2737
0
  stream_putc(s, pfx.family);
2738
0
  stream_putc(s, pfx.prefixlen);
2739
0
  stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
2740
2741
0
  stream_putw(s, 0);  /* src port */
2742
2743
0
  if (pbr && pbr->flags & MATCH_IP_DST_SET)
2744
0
    memcpy(&pfx, &(pbr->dst), sizeof(struct prefix));
2745
0
  else {
2746
0
    memset(&pfx, 0, sizeof(pfx));
2747
0
    pfx.family = fam;
2748
0
  }
2749
0
  stream_putc(s, pfx.family);
2750
0
  stream_putc(s, pfx.prefixlen);
2751
0
  stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
2752
2753
0
  stream_putw(s, 0);  /* dst port */
2754
0
  stream_putc(s, 0);  /* dsfield */
2755
  /* if pbr present, fwmark is not used */
2756
0
  if (pbr)
2757
0
    stream_putl(s, 0);
2758
0
  else
2759
0
    stream_putl(s, pbra->fwmark);  /* fwmark */
2760
2761
0
  stream_putl(s, 0); /* queue id */
2762
0
  stream_putw(s, 0); /* vlan_id */
2763
0
  stream_putw(s, 0); /* vlan_flags */
2764
0
  stream_putw(s, 0); /* pcp */
2765
2766
0
  stream_putl(s, pbra->table_id);
2767
2768
0
  memset(ifname, 0, sizeof(ifname));
2769
0
  stream_put(s, ifname, INTERFACE_NAMSIZ); /* ifname unused */
2770
0
}
2771
2772
static void bgp_encode_pbr_ipset_match(struct stream *s,
2773
          struct bgp_pbr_match *pbim)
2774
0
{
2775
0
  stream_putl(s, pbim->unique);
2776
0
  stream_putl(s, pbim->type);
2777
0
  stream_putc(s, pbim->family);
2778
0
  stream_put(s, pbim->ipset_name,
2779
0
       ZEBRA_IPSET_NAME_SIZE);
2780
0
}
2781
2782
static void bgp_encode_pbr_ipset_entry_match(struct stream *s,
2783
          struct bgp_pbr_match_entry *pbime)
2784
0
{
2785
0
  stream_putl(s, pbime->unique);
2786
  /* check that back pointer is not null */
2787
0
  stream_put(s, pbime->backpointer->ipset_name,
2788
0
       ZEBRA_IPSET_NAME_SIZE);
2789
2790
0
  stream_putc(s, pbime->src.family);
2791
0
  stream_putc(s, pbime->src.prefixlen);
2792
0
  stream_put(s, &pbime->src.u.prefix, prefix_blen(&pbime->src));
2793
2794
0
  stream_putc(s, pbime->dst.family);
2795
0
  stream_putc(s, pbime->dst.prefixlen);
2796
0
  stream_put(s, &pbime->dst.u.prefix, prefix_blen(&pbime->dst));
2797
2798
0
  stream_putw(s, pbime->src_port_min);
2799
0
  stream_putw(s, pbime->src_port_max);
2800
0
  stream_putw(s, pbime->dst_port_min);
2801
0
  stream_putw(s, pbime->dst_port_max);
2802
0
  stream_putc(s, pbime->proto);
2803
0
}
2804
2805
static void bgp_encode_pbr_iptable_match(struct stream *s,
2806
           struct bgp_pbr_action *bpa,
2807
           struct bgp_pbr_match *pbm)
2808
0
{
2809
0
  stream_putl(s, pbm->unique2);
2810
2811
0
  stream_putl(s, pbm->type);
2812
2813
0
  stream_putl(s, pbm->flags);
2814
2815
  /* TODO: correlate with what is contained
2816
   * into bgp_pbr_action.
2817
   * currently only forward supported
2818
   */
2819
0
  if (bpa->nh.type == NEXTHOP_TYPE_BLACKHOLE)
2820
0
    stream_putl(s, ZEBRA_IPTABLES_DROP);
2821
0
  else
2822
0
    stream_putl(s, ZEBRA_IPTABLES_FORWARD);
2823
0
  stream_putl(s, bpa->fwmark);
2824
0
  stream_put(s, pbm->ipset_name,
2825
0
       ZEBRA_IPSET_NAME_SIZE);
2826
0
  stream_putc(s, pbm->family);
2827
0
  stream_putw(s, pbm->pkt_len_min);
2828
0
  stream_putw(s, pbm->pkt_len_max);
2829
0
  stream_putw(s, pbm->tcp_flags);
2830
0
  stream_putw(s, pbm->tcp_mask_flags);
2831
0
  stream_putc(s, pbm->dscp_value);
2832
0
  stream_putc(s, pbm->fragment);
2833
0
  stream_putc(s, pbm->protocol);
2834
0
  stream_putw(s, pbm->flow_label);
2835
0
}
2836
2837
/* BGP has established connection with Zebra. */
2838
static void bgp_zebra_connected(struct zclient *zclient)
2839
0
{
2840
0
  struct bgp *bgp;
2841
2842
0
  zclient_num_connects++; /* increment even if not responding */
2843
2844
  /* Send the client registration */
2845
0
  bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2846
2847
  /* At this point, we may or may not have BGP instances configured, but
2848
   * we're only interested in the default VRF (others wouldn't have learnt
2849
   * the VRF from Zebra yet.)
2850
   */
2851
0
  bgp = bgp_get_default();
2852
0
  if (!bgp)
2853
0
    return;
2854
2855
0
  bgp_zebra_instance_register(bgp);
2856
2857
  /* tell label pool that zebra is connected */
2858
0
  bgp_lp_event_zebra_up();
2859
2860
  /* TODO - What if we have peers and networks configured, do we have to
2861
   * kick-start them?
2862
   */
2863
0
  BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp, bgp->peer);
2864
0
}
2865
2866
static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS)
2867
0
{
2868
0
  esi_t esi;
2869
0
  struct bgp *bgp = NULL;
2870
0
  struct stream *s = NULL;
2871
0
  char buf[ESI_STR_LEN];
2872
0
  struct in_addr originator_ip;
2873
0
  uint8_t active;
2874
0
  uint8_t bypass;
2875
0
  uint16_t df_pref;
2876
2877
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
2878
0
  if (!bgp)
2879
0
    return 0;
2880
2881
0
  s = zclient->ibuf;
2882
0
  stream_get(&esi, s, sizeof(esi_t));
2883
0
  originator_ip.s_addr = stream_get_ipv4(s);
2884
0
  active = stream_getc(s);
2885
0
  df_pref = stream_getw(s);
2886
0
  bypass = stream_getc(s);
2887
2888
0
  if (BGP_DEBUG(zebra, ZEBRA))
2889
0
    zlog_debug(
2890
0
      "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s",
2891
0
      esi_to_str(&esi, buf, sizeof(buf)), &originator_ip,
2892
0
      active, df_pref, bypass ? "bypass" : "");
2893
2894
0
  frrtrace(5, frr_bgp, evpn_mh_local_es_add_zrecv, &esi, originator_ip,
2895
0
     active, bypass, df_pref);
2896
2897
0
  bgp_evpn_local_es_add(bgp, &esi, originator_ip, active, df_pref,
2898
0
            !!bypass);
2899
2900
0
  return 0;
2901
0
}
2902
2903
static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS)
2904
0
{
2905
0
  esi_t esi;
2906
0
  struct bgp *bgp = NULL;
2907
0
  struct stream *s = NULL;
2908
0
  char buf[ESI_STR_LEN];
2909
2910
0
  memset(&esi, 0, sizeof(esi_t));
2911
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
2912
0
  if (!bgp)
2913
0
    return 0;
2914
2915
0
  s = zclient->ibuf;
2916
0
  stream_get(&esi, s, sizeof(esi_t));
2917
2918
0
  if (BGP_DEBUG(zebra, ZEBRA))
2919
0
    zlog_debug("Rx del ESI %s",
2920
0
        esi_to_str(&esi, buf, sizeof(buf)));
2921
2922
0
  frrtrace(1, frr_bgp, evpn_mh_local_es_del_zrecv, &esi);
2923
2924
0
  bgp_evpn_local_es_del(bgp, &esi);
2925
2926
0
  return 0;
2927
0
}
2928
2929
static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS)
2930
0
{
2931
0
  esi_t esi;
2932
0
  vni_t vni;
2933
0
  struct bgp *bgp;
2934
0
  struct stream *s;
2935
0
  char buf[ESI_STR_LEN];
2936
2937
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
2938
0
  if (!bgp)
2939
0
    return 0;
2940
2941
0
  s = zclient->ibuf;
2942
0
  stream_get(&esi, s, sizeof(esi_t));
2943
0
  vni = stream_getl(s);
2944
2945
0
  if (BGP_DEBUG(zebra, ZEBRA))
2946
0
    zlog_debug("Rx %s ESI %s VNI %u",
2947
0
         (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
2948
0
         esi_to_str(&esi, buf, sizeof(buf)), vni);
2949
2950
0
  if (cmd == ZEBRA_LOCAL_ES_EVI_ADD) {
2951
0
    frrtrace(2, frr_bgp, evpn_mh_local_es_evi_add_zrecv, &esi, vni);
2952
2953
0
    bgp_evpn_local_es_evi_add(bgp, &esi, vni);
2954
0
  } else {
2955
0
    frrtrace(2, frr_bgp, evpn_mh_local_es_evi_del_zrecv, &esi, vni);
2956
2957
0
    bgp_evpn_local_es_evi_del(bgp, &esi, vni);
2958
0
  }
2959
2960
0
  return 0;
2961
0
}
2962
2963
static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS)
2964
0
{
2965
0
  int filter = 0;
2966
0
  vni_t l3vni = 0;
2967
0
  struct ethaddr svi_rmac, vrr_rmac = {.octet = {0} };
2968
0
  struct in_addr originator_ip;
2969
0
  struct stream *s;
2970
0
  ifindex_t svi_ifindex;
2971
0
  bool is_anycast_mac = false;
2972
2973
0
  memset(&svi_rmac, 0, sizeof(svi_rmac));
2974
0
  memset(&originator_ip, 0, sizeof(originator_ip));
2975
0
  s = zclient->ibuf;
2976
0
  l3vni = stream_getl(s);
2977
0
  if (cmd == ZEBRA_L3VNI_ADD) {
2978
0
    stream_get(&svi_rmac, s, sizeof(struct ethaddr));
2979
0
    originator_ip.s_addr = stream_get_ipv4(s);
2980
0
    stream_get(&filter, s, sizeof(int));
2981
0
    svi_ifindex = stream_getl(s);
2982
0
    stream_get(&vrr_rmac, s, sizeof(struct ethaddr));
2983
0
    is_anycast_mac = stream_getl(s);
2984
2985
0
    if (BGP_DEBUG(zebra, ZEBRA))
2986
0
      zlog_debug(
2987
0
        "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u",
2988
0
        vrf_id_to_name(vrf_id), l3vni, &svi_rmac,
2989
0
        &vrr_rmac,
2990
0
        filter ? "prefix-routes-only" : "none",
2991
0
        svi_ifindex);
2992
2993
0
    frrtrace(8, frr_bgp, evpn_local_l3vni_add_zrecv, l3vni, vrf_id,
2994
0
       &svi_rmac, &vrr_rmac, filter, originator_ip,
2995
0
       svi_ifindex, is_anycast_mac);
2996
2997
0
    bgp_evpn_local_l3vni_add(l3vni, vrf_id, &svi_rmac, &vrr_rmac,
2998
0
           originator_ip, filter, svi_ifindex,
2999
0
           is_anycast_mac);
3000
0
  } else {
3001
0
    if (BGP_DEBUG(zebra, ZEBRA))
3002
0
      zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
3003
0
           vrf_id_to_name(vrf_id), l3vni);
3004
3005
0
    frrtrace(2, frr_bgp, evpn_local_l3vni_del_zrecv, l3vni, vrf_id);
3006
3007
0
    bgp_evpn_local_l3vni_del(l3vni, vrf_id);
3008
0
  }
3009
3010
0
  return 0;
3011
0
}
3012
3013
static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS)
3014
0
{
3015
0
  struct stream *s;
3016
0
  vni_t vni;
3017
0
  struct bgp *bgp;
3018
0
  struct in_addr vtep_ip = {INADDR_ANY};
3019
0
  vrf_id_t tenant_vrf_id = VRF_DEFAULT;
3020
0
  struct in_addr mcast_grp = {INADDR_ANY};
3021
0
  ifindex_t svi_ifindex = 0;
3022
3023
0
  s = zclient->ibuf;
3024
0
  vni = stream_getl(s);
3025
0
  if (cmd == ZEBRA_VNI_ADD) {
3026
0
    vtep_ip.s_addr = stream_get_ipv4(s);
3027
0
    stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
3028
0
    mcast_grp.s_addr = stream_get_ipv4(s);
3029
0
    stream_get(&svi_ifindex, s, sizeof(ifindex_t));
3030
0
  }
3031
3032
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
3033
0
  if (!bgp)
3034
0
    return 0;
3035
3036
0
  if (BGP_DEBUG(zebra, ZEBRA))
3037
0
    zlog_debug(
3038
0
      "Rx VNI %s VRF %s VNI %u tenant-vrf %s SVI ifindex %u",
3039
0
      (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
3040
0
      vrf_id_to_name(vrf_id), vni,
3041
0
      vrf_id_to_name(tenant_vrf_id), svi_ifindex);
3042
3043
0
  if (cmd == ZEBRA_VNI_ADD) {
3044
0
    frrtrace(4, frr_bgp, evpn_local_vni_add_zrecv, vni, vtep_ip,
3045
0
       tenant_vrf_id, mcast_grp);
3046
3047
0
    return bgp_evpn_local_vni_add(
3048
0
      bgp, vni,
3049
0
      vtep_ip.s_addr != INADDR_ANY ? vtep_ip : bgp->router_id,
3050
0
      tenant_vrf_id, mcast_grp, svi_ifindex);
3051
0
  } else {
3052
0
    frrtrace(1, frr_bgp, evpn_local_vni_del_zrecv, vni);
3053
3054
0
    return bgp_evpn_local_vni_del(bgp, vni);
3055
0
  }
3056
0
}
3057
3058
static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS)
3059
0
{
3060
0
  struct stream *s;
3061
0
  vni_t vni;
3062
0
  struct bgp *bgp;
3063
0
  struct ethaddr mac;
3064
0
  struct ipaddr ip;
3065
0
  int ipa_len;
3066
0
  uint8_t flags = 0;
3067
0
  uint32_t seqnum = 0;
3068
0
  int state = 0;
3069
0
  char buf2[ESI_STR_LEN];
3070
0
  esi_t esi;
3071
3072
0
  memset(&ip, 0, sizeof(ip));
3073
0
  s = zclient->ibuf;
3074
0
  vni = stream_getl(s);
3075
0
  stream_get(&mac.octet, s, ETH_ALEN);
3076
0
  ipa_len = stream_getl(s);
3077
0
  if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
3078
0
      && ipa_len != IPV6_MAX_BYTELEN) {
3079
0
    flog_err(EC_BGP_MACIP_LEN,
3080
0
       "%u:Recv MACIP %s with invalid IP addr length %d",
3081
0
       vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del",
3082
0
       ipa_len);
3083
0
    return -1;
3084
0
  }
3085
3086
0
  if (ipa_len) {
3087
0
    ip.ipa_type =
3088
0
      (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
3089
0
    stream_get(&ip.ip.addr, s, ipa_len);
3090
0
  }
3091
0
  if (cmd == ZEBRA_MACIP_ADD) {
3092
0
    flags = stream_getc(s);
3093
0
    seqnum = stream_getl(s);
3094
0
    stream_get(&esi, s, sizeof(esi_t));
3095
0
  } else {
3096
0
    state = stream_getl(s);
3097
0
    memset(&esi, 0, sizeof(esi_t));
3098
0
  }
3099
3100
0
  bgp = bgp_lookup_by_vrf_id(vrf_id);
3101
0
  if (!bgp)
3102
0
    return 0;
3103
3104
0
  if (BGP_DEBUG(zebra, ZEBRA))
3105
0
    zlog_debug(
3106
0
      "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s",
3107
0
      vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags,
3108
0
      &mac, &ip, vni, seqnum, state,
3109
0
      esi_to_str(&esi, buf2, sizeof(buf2)));
3110
3111
0
  if (cmd == ZEBRA_MACIP_ADD) {
3112
0
    frrtrace(6, frr_bgp, evpn_local_macip_add_zrecv, vni, &mac, &ip,
3113
0
       flags, seqnum, &esi);
3114
3115
0
    return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip,
3116
0
            flags, seqnum, &esi);
3117
0
  } else {
3118
0
    frrtrace(4, frr_bgp, evpn_local_macip_del_zrecv, vni, &mac, &ip,
3119
0
       state);
3120
3121
0
    return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip, state);
3122
0
  }
3123
0
}
3124
3125
static int bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS)
3126
0
{
3127
0
  struct stream *s = NULL;
3128
0
  struct bgp *bgp_vrf = NULL;
3129
0
  struct prefix p;
3130
3131
0
  memset(&p, 0, sizeof(p));
3132
0
  s = zclient->ibuf;
3133
0
  stream_get(&p, s, sizeof(struct prefix));
3134
3135
0
  bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
3136
0
  if (!bgp_vrf)
3137
0
    return 0;
3138
3139
0
  if (BGP_DEBUG(zebra, ZEBRA))
3140
0
    zlog_debug("Recv prefix %pFX %s on vrf %s", &p,
3141
0
         (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
3142
0
         vrf_id_to_name(vrf_id));
3143
3144
0
  if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
3145
3146
0
    if (p.family == AF_INET)
3147
0
      bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3148
0
                   AFI_IP, SAFI_UNICAST);
3149
0
    else
3150
0
      bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3151
0
                   AFI_IP6, SAFI_UNICAST);
3152
3153
0
  } else {
3154
0
    if (p.family == AF_INET)
3155
0
      bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP,
3156
0
                  SAFI_UNICAST);
3157
0
    else
3158
0
      bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP6,
3159
0
                  SAFI_UNICAST);
3160
0
  }
3161
0
  return 0;
3162
0
}
3163
3164
static int bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS)
3165
0
{
3166
0
  struct stream *s = NULL;
3167
0
  uint8_t response_keep;
3168
0
  uint32_t first;
3169
0
  uint32_t last;
3170
0
  uint8_t proto;
3171
0
  unsigned short instance;
3172
3173
0
  s = zclient->ibuf;
3174
0
  STREAM_GETC(s, proto);
3175
0
  STREAM_GETW(s, instance);
3176
0
  STREAM_GETC(s, response_keep);
3177
0
  STREAM_GETL(s, first);
3178
0
  STREAM_GETL(s, last);
3179
3180
0
  if (zclient->redist_default != proto) {
3181
0
    flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong proto %u",
3182
0
       proto);
3183
0
    return 0;
3184
0
  }
3185
0
  if (zclient->instance != instance) {
3186
0
    flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong instance %u",
3187
0
       proto);
3188
0
    return 0;
3189
0
  }
3190
3191
0
  if (first > last ||
3192
0
    first < MPLS_LABEL_UNRESERVED_MIN ||
3193
0
    last > MPLS_LABEL_UNRESERVED_MAX) {
3194
3195
0
    flog_err(EC_BGP_LM_ERROR, "%s: Invalid Label chunk: %u - %u",
3196
0
       __func__, first, last);
3197
0
    return 0;
3198
0
  }
3199
0
  if (BGP_DEBUG(zebra, ZEBRA)) {
3200
0
    zlog_debug("Label Chunk assign: %u - %u (%u) ",
3201
0
      first, last, response_keep);
3202
0
  }
3203
3204
0
  bgp_lp_event_chunk(response_keep, first, last);
3205
3206
0
  return 0;
3207
3208
0
stream_failure:   /* for STREAM_GETX */
3209
0
  return -1;
3210
0
}
3211
3212
extern struct zebra_privs_t bgpd_privs;
3213
3214
static int bgp_ifp_create(struct interface *ifp)
3215
0
{
3216
0
  struct bgp *bgp;
3217
3218
0
  if (BGP_DEBUG(zebra, ZEBRA))
3219
0
    zlog_debug("Rx Intf add VRF %u IF %s", ifp->vrf->vrf_id,
3220
0
         ifp->name);
3221
3222
0
  bgp = ifp->vrf->info;
3223
0
  if (!bgp)
3224
0
    return 0;
3225
3226
0
  bgp_mac_add_mac_entry(ifp);
3227
3228
0
  bgp_update_interface_nbrs(bgp, ifp, ifp);
3229
0
  hook_call(bgp_vrf_status_changed, bgp, ifp);
3230
0
  return 0;
3231
0
}
3232
3233
static int bgp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS)
3234
0
{
3235
0
  struct stream *s = NULL;
3236
0
  struct bgp *bgp = bgp_get_default();
3237
0
  struct listnode *node;
3238
0
  struct srv6_locator_chunk *c;
3239
0
  struct srv6_locator_chunk *chunk = srv6_locator_chunk_alloc();
3240
3241
0
  s = zclient->ibuf;
3242
0
  zapi_srv6_locator_chunk_decode(s, chunk);
3243
3244
0
  if (strcmp(bgp->srv6_locator_name, chunk->locator_name) != 0) {
3245
0
    zlog_err("%s: Locator name unmatch %s:%s", __func__,
3246
0
       bgp->srv6_locator_name, chunk->locator_name);
3247
0
    srv6_locator_chunk_free(&chunk);
3248
0
    return 0;
3249
0
  }
3250
3251
0
  for (ALL_LIST_ELEMENTS_RO(bgp->srv6_locator_chunks, node, c)) {
3252
0
    if (!prefix_cmp(&c->prefix, &chunk->prefix)) {
3253
0
      srv6_locator_chunk_free(&chunk);
3254
0
      return 0;
3255
0
    }
3256
0
  }
3257
3258
0
  listnode_add(bgp->srv6_locator_chunks, chunk);
3259
0
  vpn_leak_postchange_all();
3260
0
  return 0;
3261
0
}
3262
3263
static int bgp_zebra_process_srv6_locator_add(ZAPI_CALLBACK_ARGS)
3264
0
{
3265
0
  struct srv6_locator loc = {};
3266
0
  struct bgp *bgp = bgp_get_default();
3267
0
  const char *loc_name = bgp->srv6_locator_name;
3268
3269
0
  if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3270
0
    return -1;
3271
3272
0
  if (!bgp || !bgp->srv6_enabled)
3273
0
    return 0;
3274
3275
0
  if (bgp_zebra_srv6_manager_get_locator_chunk(loc_name) < 0)
3276
0
    return -1;
3277
3278
0
  return 0;
3279
0
}
3280
3281
static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
3282
0
{
3283
0
  struct srv6_locator loc = {};
3284
0
  struct bgp *bgp = bgp_get_default();
3285
0
  struct listnode *node, *nnode;
3286
0
  struct srv6_locator_chunk *chunk, *tovpn_sid_locator;
3287
0
  struct bgp_srv6_function *func;
3288
0
  struct bgp *bgp_vrf;
3289
0
  struct in6_addr *tovpn_sid;
3290
0
  struct prefix_ipv6 tmp_prefi;
3291
3292
0
  if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3293
0
    return -1;
3294
3295
  // refresh chunks
3296
0
  for (ALL_LIST_ELEMENTS(bgp->srv6_locator_chunks, node, nnode, chunk))
3297
0
    if (prefix_match((struct prefix *)&loc.prefix,
3298
0
         (struct prefix *)&chunk->prefix)) {
3299
0
      listnode_delete(bgp->srv6_locator_chunks, chunk);
3300
0
      srv6_locator_chunk_free(&chunk);
3301
0
    }
3302
3303
  // refresh functions
3304
0
  for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) {
3305
0
    tmp_prefi.family = AF_INET6;
3306
0
    tmp_prefi.prefixlen = 128;
3307
0
    tmp_prefi.prefix = func->sid;
3308
0
    if (prefix_match((struct prefix *)&loc.prefix,
3309
0
         (struct prefix *)&tmp_prefi)) {
3310
0
      listnode_delete(bgp->srv6_functions, func);
3311
0
      XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
3312
0
    }
3313
0
  }
3314
3315
  // refresh tovpn_sid
3316
0
  for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
3317
0
    if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF)
3318
0
      continue;
3319
3320
    // refresh vpnv4 tovpn_sid
3321
0
    tovpn_sid = bgp_vrf->vpn_policy[AFI_IP].tovpn_sid;
3322
0
    if (tovpn_sid) {
3323
0
      tmp_prefi.family = AF_INET6;
3324
0
      tmp_prefi.prefixlen = 128;
3325
0
      tmp_prefi.prefix = *tovpn_sid;
3326
0
      if (prefix_match((struct prefix *)&loc.prefix,
3327
0
           (struct prefix *)&tmp_prefi))
3328
0
        XFREE(MTYPE_BGP_SRV6_SID,
3329
0
              bgp_vrf->vpn_policy[AFI_IP].tovpn_sid);
3330
0
    }
3331
3332
    // refresh vpnv6 tovpn_sid
3333
0
    tovpn_sid = bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid;
3334
0
    if (tovpn_sid) {
3335
0
      tmp_prefi.family = AF_INET6;
3336
0
      tmp_prefi.prefixlen = 128;
3337
0
      tmp_prefi.prefix = *tovpn_sid;
3338
0
      if (prefix_match((struct prefix *)&loc.prefix,
3339
0
           (struct prefix *)&tmp_prefi))
3340
0
        XFREE(MTYPE_BGP_SRV6_SID,
3341
0
              bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid);
3342
0
    }
3343
3344
    /* refresh per-vrf tovpn_sid */
3345
0
    tovpn_sid = bgp_vrf->tovpn_sid;
3346
0
    if (tovpn_sid) {
3347
0
      tmp_prefi.family = AF_INET6;
3348
0
      tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
3349
0
      tmp_prefi.prefix = *tovpn_sid;
3350
0
      if (prefix_match((struct prefix *)&loc.prefix,
3351
0
           (struct prefix *)&tmp_prefi))
3352
0
        XFREE(MTYPE_BGP_SRV6_SID, bgp_vrf->tovpn_sid);
3353
0
    }
3354
0
  }
3355
3356
0
  vpn_leak_postchange_all();
3357
3358
  /* refresh tovpn_sid_locator */
3359
0
  for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
3360
0
    if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF)
3361
0
      continue;
3362
3363
    /* refresh vpnv4 tovpn_sid_locator */
3364
0
    tovpn_sid_locator =
3365
0
      bgp_vrf->vpn_policy[AFI_IP].tovpn_sid_locator;
3366
0
    if (tovpn_sid_locator) {
3367
0
      tmp_prefi.family = AF_INET6;
3368
0
      tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
3369
0
      tmp_prefi.prefix = tovpn_sid_locator->prefix.prefix;
3370
0
      if (prefix_match((struct prefix *)&loc.prefix,
3371
0
           (struct prefix *)&tmp_prefi))
3372
0
        srv6_locator_chunk_free(
3373
0
          &bgp_vrf->vpn_policy[AFI_IP]
3374
0
             .tovpn_sid_locator);
3375
0
    }
3376
3377
    /* refresh vpnv6 tovpn_sid_locator */
3378
0
    tovpn_sid_locator =
3379
0
      bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid_locator;
3380
0
    if (tovpn_sid_locator) {
3381
0
      tmp_prefi.family = AF_INET6;
3382
0
      tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
3383
0
      tmp_prefi.prefix = tovpn_sid_locator->prefix.prefix;
3384
0
      if (prefix_match((struct prefix *)&loc.prefix,
3385
0
           (struct prefix *)&tmp_prefi))
3386
0
        srv6_locator_chunk_free(
3387
0
          &bgp_vrf->vpn_policy[AFI_IP6]
3388
0
             .tovpn_sid_locator);
3389
0
    }
3390
3391
    /* refresh per-vrf tovpn_sid_locator */
3392
0
    tovpn_sid_locator = bgp_vrf->tovpn_sid_locator;
3393
0
    if (tovpn_sid_locator) {
3394
0
      tmp_prefi.family = AF_INET6;
3395
0
      tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
3396
0
      tmp_prefi.prefix = tovpn_sid_locator->prefix.prefix;
3397
0
      if (prefix_match((struct prefix *)&loc.prefix,
3398
0
           (struct prefix *)&tmp_prefi))
3399
0
        srv6_locator_chunk_free(
3400
0
          &bgp_vrf->tovpn_sid_locator);
3401
0
    }
3402
0
  }
3403
3404
0
  return 0;
3405
0
}
3406
3407
static zclient_handler *const bgp_handlers[] = {
3408
  [ZEBRA_ROUTER_ID_UPDATE] = bgp_router_id_update,
3409
  [ZEBRA_INTERFACE_ADDRESS_ADD] = bgp_interface_address_add,
3410
  [ZEBRA_INTERFACE_ADDRESS_DELETE] = bgp_interface_address_delete,
3411
  [ZEBRA_INTERFACE_NBR_ADDRESS_ADD] = bgp_interface_nbr_address_add,
3412
  [ZEBRA_INTERFACE_NBR_ADDRESS_DELETE] = bgp_interface_nbr_address_delete,
3413
  [ZEBRA_INTERFACE_VRF_UPDATE] = bgp_interface_vrf_update,
3414
  [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = zebra_read_route,
3415
  [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = zebra_read_route,
3416
  [ZEBRA_NEXTHOP_UPDATE] = bgp_read_nexthop_update,
3417
  [ZEBRA_FEC_UPDATE] = bgp_read_fec_update,
3418
  [ZEBRA_LOCAL_ES_ADD] = bgp_zebra_process_local_es_add,
3419
  [ZEBRA_LOCAL_ES_DEL] = bgp_zebra_process_local_es_del,
3420
  [ZEBRA_VNI_ADD] = bgp_zebra_process_local_vni,
3421
  [ZEBRA_LOCAL_ES_EVI_ADD] = bgp_zebra_process_local_es_evi,
3422
  [ZEBRA_LOCAL_ES_EVI_DEL] = bgp_zebra_process_local_es_evi,
3423
  [ZEBRA_VNI_DEL] = bgp_zebra_process_local_vni,
3424
  [ZEBRA_MACIP_ADD] = bgp_zebra_process_local_macip,
3425
  [ZEBRA_MACIP_DEL] = bgp_zebra_process_local_macip,
3426
  [ZEBRA_L3VNI_ADD] = bgp_zebra_process_local_l3vni,
3427
  [ZEBRA_L3VNI_DEL] = bgp_zebra_process_local_l3vni,
3428
  [ZEBRA_IP_PREFIX_ROUTE_ADD] = bgp_zebra_process_local_ip_prefix,
3429
  [ZEBRA_IP_PREFIX_ROUTE_DEL] = bgp_zebra_process_local_ip_prefix,
3430
  [ZEBRA_GET_LABEL_CHUNK] = bgp_zebra_process_label_chunk,
3431
  [ZEBRA_RULE_NOTIFY_OWNER] = rule_notify_owner,
3432
  [ZEBRA_IPSET_NOTIFY_OWNER] = ipset_notify_owner,
3433
  [ZEBRA_IPSET_ENTRY_NOTIFY_OWNER] = ipset_entry_notify_owner,
3434
  [ZEBRA_IPTABLE_NOTIFY_OWNER] = iptable_notify_owner,
3435
  [ZEBRA_ROUTE_NOTIFY_OWNER] = bgp_zebra_route_notify_owner,
3436
  [ZEBRA_SRV6_LOCATOR_ADD] = bgp_zebra_process_srv6_locator_add,
3437
  [ZEBRA_SRV6_LOCATOR_DELETE] = bgp_zebra_process_srv6_locator_delete,
3438
  [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK] =
3439
    bgp_zebra_process_srv6_locator_chunk,
3440
};
3441
3442
static int bgp_if_new_hook(struct interface *ifp)
3443
0
{
3444
0
  struct bgp_interface *iifp;
3445
3446
0
  if (ifp->info)
3447
0
    return 0;
3448
0
  iifp = XCALLOC(MTYPE_BGP_IF_INFO, sizeof(struct bgp_interface));
3449
0
  ifp->info = iifp;
3450
3451
0
  return 0;
3452
0
}
3453
3454
static int bgp_if_delete_hook(struct interface *ifp)
3455
0
{
3456
0
  XFREE(MTYPE_BGP_IF_INFO, ifp->info);
3457
0
  return 0;
3458
0
}
3459
3460
void bgp_if_init(void)
3461
0
{
3462
  /* Initialize Zebra interface data structure. */
3463
0
  hook_register_prio(if_add, 0, bgp_if_new_hook);
3464
0
  hook_register_prio(if_del, 0, bgp_if_delete_hook);
3465
0
}
3466
3467
void bgp_zebra_init(struct event_loop *master, unsigned short instance)
3468
0
{
3469
0
  zclient_num_connects = 0;
3470
3471
0
  if_zapi_callbacks(bgp_ifp_create, bgp_ifp_up,
3472
0
        bgp_ifp_down, bgp_ifp_destroy);
3473
3474
  /* Set default values. */
3475
0
  zclient = zclient_new(master, &zclient_options_default, bgp_handlers,
3476
0
            array_size(bgp_handlers));
3477
0
  zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
3478
0
  zclient->zebra_connected = bgp_zebra_connected;
3479
0
  zclient->instance = instance;
3480
0
}
3481
3482
void bgp_zebra_destroy(void)
3483
0
{
3484
0
  if (zclient == NULL)
3485
0
    return;
3486
0
  zclient_stop(zclient);
3487
0
  zclient_free(zclient);
3488
0
  zclient = NULL;
3489
0
}
3490
3491
int bgp_zebra_num_connects(void)
3492
0
{
3493
0
  return zclient_num_connects;
3494
0
}
3495
3496
void bgp_send_pbr_rule_action(struct bgp_pbr_action *pbra,
3497
            struct bgp_pbr_rule *pbr,
3498
            bool install)
3499
0
{
3500
0
  struct stream *s;
3501
3502
0
  if (pbra->install_in_progress && !pbr)
3503
0
    return;
3504
0
  if (pbr && pbr->install_in_progress)
3505
0
    return;
3506
0
  if (BGP_DEBUG(zebra, ZEBRA)) {
3507
0
    if (pbr)
3508
0
      zlog_debug("%s: table %d (ip rule) %d", __func__,
3509
0
           pbra->table_id, install);
3510
0
    else
3511
0
      zlog_debug("%s: table %d fwmark %d %d", __func__,
3512
0
           pbra->table_id, pbra->fwmark, install);
3513
0
  }
3514
0
  s = zclient->obuf;
3515
0
  stream_reset(s);
3516
3517
0
  zclient_create_header(s,
3518
0
            install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
3519
0
            VRF_DEFAULT);
3520
0
  stream_putl(s, 1); /* send one pbr action */
3521
3522
0
  bgp_encode_pbr_rule_action(s, pbra, pbr);
3523
3524
0
  stream_putw_at(s, 0, stream_get_endp(s));
3525
0
  if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE)
3526
0
      && install) {
3527
0
    if (!pbr)
3528
0
      pbra->install_in_progress = true;
3529
0
    else
3530
0
      pbr->install_in_progress = true;
3531
0
  }
3532
0
}
3533
3534
void bgp_send_pbr_ipset_match(struct bgp_pbr_match *pbrim, bool install)
3535
0
{
3536
0
  struct stream *s;
3537
3538
0
  if (pbrim->install_in_progress)
3539
0
    return;
3540
0
  if (BGP_DEBUG(zebra, ZEBRA))
3541
0
    zlog_debug("%s: name %s type %d %d, ID %u", __func__,
3542
0
         pbrim->ipset_name, pbrim->type, install,
3543
0
         pbrim->unique);
3544
0
  s = zclient->obuf;
3545
0
  stream_reset(s);
3546
3547
0
  zclient_create_header(s,
3548
0
            install ? ZEBRA_IPSET_CREATE :
3549
0
            ZEBRA_IPSET_DESTROY,
3550
0
            VRF_DEFAULT);
3551
3552
0
  stream_putl(s, 1); /* send one pbr action */
3553
3554
0
  bgp_encode_pbr_ipset_match(s, pbrim);
3555
3556
0
  stream_putw_at(s, 0, stream_get_endp(s));
3557
0
  if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
3558
0
    pbrim->install_in_progress = true;
3559
0
}
3560
3561
void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry *pbrime,
3562
            bool install)
3563
0
{
3564
0
  struct stream *s;
3565
3566
0
  if (pbrime->install_in_progress)
3567
0
    return;
3568
0
  if (BGP_DEBUG(zebra, ZEBRA))
3569
0
    zlog_debug("%s: name %s %d %d, ID %u", __func__,
3570
0
         pbrime->backpointer->ipset_name, pbrime->unique,
3571
0
         install, pbrime->unique);
3572
0
  s = zclient->obuf;
3573
0
  stream_reset(s);
3574
3575
0
  zclient_create_header(s,
3576
0
            install ? ZEBRA_IPSET_ENTRY_ADD :
3577
0
            ZEBRA_IPSET_ENTRY_DELETE,
3578
0
            VRF_DEFAULT);
3579
3580
0
  stream_putl(s, 1); /* send one pbr action */
3581
3582
0
  bgp_encode_pbr_ipset_entry_match(s, pbrime);
3583
3584
0
  stream_putw_at(s, 0, stream_get_endp(s));
3585
0
  if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
3586
0
    pbrime->install_in_progress = true;
3587
0
}
3588
3589
static void bgp_encode_pbr_interface_list(struct bgp *bgp, struct stream *s,
3590
            uint8_t family)
3591
0
{
3592
0
  struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3593
0
  struct bgp_pbr_interface_head *head;
3594
0
  struct bgp_pbr_interface *pbr_if;
3595
0
  struct interface *ifp;
3596
3597
0
  if (!bgp_pbr_cfg)
3598
0
    return;
3599
0
  if (family == AF_INET)
3600
0
    head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3601
0
  else
3602
0
    head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
3603
0
  RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
3604
0
    ifp = if_lookup_by_name(pbr_if->name, bgp->vrf_id);
3605
0
    if (ifp)
3606
0
      stream_putl(s, ifp->ifindex);
3607
0
  }
3608
0
}
3609
3610
static int bgp_pbr_get_ifnumber(struct bgp *bgp, uint8_t family)
3611
0
{
3612
0
  struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3613
0
  struct bgp_pbr_interface_head *head;
3614
0
  struct bgp_pbr_interface *pbr_if;
3615
0
  int cnt = 0;
3616
3617
0
  if (!bgp_pbr_cfg)
3618
0
    return 0;
3619
0
  if (family == AF_INET)
3620
0
    head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3621
0
  else
3622
0
    head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
3623
0
  RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
3624
0
    if (if_lookup_by_name(pbr_if->name, bgp->vrf_id))
3625
0
      cnt++;
3626
0
  }
3627
0
  return cnt;
3628
0
}
3629
3630
void bgp_send_pbr_iptable(struct bgp_pbr_action *pba,
3631
        struct bgp_pbr_match *pbm,
3632
        bool install)
3633
0
{
3634
0
  struct stream *s;
3635
0
  int ret = 0;
3636
0
  int nb_interface;
3637
3638
0
  if (pbm->install_iptable_in_progress)
3639
0
    return;
3640
0
  if (BGP_DEBUG(zebra, ZEBRA))
3641
0
    zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__,
3642
0
         pbm->ipset_name, pbm->type, pba->fwmark, install,
3643
0
         pbm->unique2);
3644
0
  s = zclient->obuf;
3645
0
  stream_reset(s);
3646
3647
0
  zclient_create_header(s,
3648
0
            install ? ZEBRA_IPTABLE_ADD :
3649
0
            ZEBRA_IPTABLE_DELETE,
3650
0
            VRF_DEFAULT);
3651
3652
0
  bgp_encode_pbr_iptable_match(s, pba, pbm);
3653
0
  nb_interface = bgp_pbr_get_ifnumber(pba->bgp, pbm->family);
3654
0
  stream_putl(s, nb_interface);
3655
0
  if (nb_interface)
3656
0
    bgp_encode_pbr_interface_list(pba->bgp, s, pbm->family);
3657
0
  stream_putw_at(s, 0, stream_get_endp(s));
3658
0
  ret = zclient_send_message(zclient);
3659
0
  if (install) {
3660
0
    if (ret != ZCLIENT_SEND_FAILURE)
3661
0
      pba->refcnt++;
3662
0
    else
3663
0
      pbm->install_iptable_in_progress = true;
3664
0
  }
3665
0
}
3666
3667
/* inject in table <table_id> a default route to:
3668
 * - if nexthop IP is present : to this nexthop
3669
 * - if vrf is different from local : to the matching VRF
3670
 */
3671
void bgp_zebra_announce_default(struct bgp *bgp, struct nexthop *nh,
3672
        afi_t afi, uint32_t table_id, bool announce)
3673
0
{
3674
0
  struct zapi_nexthop *api_nh;
3675
0
  struct zapi_route api;
3676
0
  struct prefix p;
3677
3678
0
  if (!nh || (nh->type != NEXTHOP_TYPE_IPV4
3679
0
        && nh->type != NEXTHOP_TYPE_IPV6)
3680
0
      || nh->vrf_id == VRF_UNKNOWN)
3681
0
    return;
3682
3683
  /* in vrf-lite, no default route has to be announced
3684
   * the table id of vrf is directly used to divert traffic
3685
   */
3686
0
  if (!vrf_is_backend_netns() && bgp->vrf_id != nh->vrf_id)
3687
0
    return;
3688
3689
0
  memset(&p, 0, sizeof(p));
3690
0
  if (afi != AFI_IP && afi != AFI_IP6)
3691
0
    return;
3692
0
  p.family = afi2family(afi);
3693
0
  memset(&api, 0, sizeof(api));
3694
0
  api.vrf_id = bgp->vrf_id;
3695
0
  api.type = ZEBRA_ROUTE_BGP;
3696
0
  api.safi = SAFI_UNICAST;
3697
0
  api.prefix = p;
3698
0
  api.tableid = table_id;
3699
0
  api.nexthop_num = 1;
3700
0
  SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
3701
0
  SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
3702
0
  api_nh = &api.nexthops[0];
3703
3704
0
  api.distance = ZEBRA_EBGP_DISTANCE_DEFAULT;
3705
0
  SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
3706
3707
  /* redirect IP */
3708
0
  if (afi == AFI_IP && nh->gate.ipv4.s_addr != INADDR_ANY) {
3709
0
    api_nh->vrf_id = nh->vrf_id;
3710
0
    api_nh->gate.ipv4 = nh->gate.ipv4;
3711
0
    api_nh->type = NEXTHOP_TYPE_IPV4;
3712
3713
0
    if (BGP_DEBUG(zebra, ZEBRA))
3714
0
      zlog_debug(
3715
0
        "BGP: %s default route to %pI4 table %d (redirect IP)",
3716
0
        announce ? "adding" : "withdrawing",
3717
0
        &nh->gate.ipv4, table_id);
3718
3719
0
    zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3720
0
           : ZEBRA_ROUTE_DELETE,
3721
0
           zclient, &api);
3722
0
  } else if (afi == AFI_IP6 &&
3723
0
       memcmp(&nh->gate.ipv6,
3724
0
        &in6addr_any, sizeof(struct in6_addr))) {
3725
0
    api_nh->vrf_id = nh->vrf_id;
3726
0
    memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6,
3727
0
           sizeof(struct in6_addr));
3728
0
    api_nh->type = NEXTHOP_TYPE_IPV6;
3729
3730
0
    if (BGP_DEBUG(zebra, ZEBRA))
3731
0
      zlog_debug(
3732
0
        "BGP: %s default route to %pI6 table %d (redirect IP)",
3733
0
        announce ? "adding" : "withdrawing",
3734
0
        &nh->gate.ipv6, table_id);
3735
3736
0
    zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3737
0
           : ZEBRA_ROUTE_DELETE,
3738
0
           zclient, &api);
3739
0
  } else if (nh->vrf_id != bgp->vrf_id) {
3740
0
    struct vrf *vrf;
3741
0
    struct interface *ifp;
3742
3743
0
    vrf = vrf_lookup_by_id(nh->vrf_id);
3744
0
    if (!vrf)
3745
0
      return;
3746
    /* create default route with interface <VRF>
3747
     * with nexthop-vrf <VRF>
3748
     */
3749
0
    ifp = if_lookup_by_name_vrf(vrf->name, vrf);
3750
0
    if (!ifp)
3751
0
      return;
3752
0
    api_nh->vrf_id = nh->vrf_id;
3753
0
    api_nh->type = NEXTHOP_TYPE_IFINDEX;
3754
0
    api_nh->ifindex = ifp->ifindex;
3755
0
    if (BGP_DEBUG(zebra, ZEBRA))
3756
0
      zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3757
0
          announce ? "adding" : "withdrawing",
3758
0
          vrf->name, table_id);
3759
0
    zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3760
0
           : ZEBRA_ROUTE_DELETE,
3761
0
           zclient, &api);
3762
0
    return;
3763
0
  }
3764
0
}
3765
3766
/* Send capabilities to RIB */
3767
int bgp_zebra_send_capabilities(struct bgp *bgp, bool disable)
3768
0
{
3769
0
  struct zapi_cap api;
3770
0
  int ret = BGP_GR_SUCCESS;
3771
3772
0
  if (BGP_DEBUG(zebra, ZEBRA))
3773
0
    zlog_debug("%s: Sending %sable for %s", __func__,
3774
0
         disable ? "dis" : "en", bgp->name_pretty);
3775
3776
0
  if (zclient == NULL) {
3777
0
    if (BGP_DEBUG(zebra, ZEBRA))
3778
0
      zlog_debug("%s: %s zclient invalid", __func__,
3779
0
           bgp->name_pretty);
3780
0
    return BGP_GR_FAILURE;
3781
0
  }
3782
3783
  /* Check if the client is connected */
3784
0
  if ((zclient->sock < 0) || (zclient->t_connect)) {
3785
0
    if (BGP_DEBUG(zebra, ZEBRA))
3786
0
      zlog_debug("%s: %s client not connected", __func__,
3787
0
           bgp->name_pretty);
3788
0
    return BGP_GR_FAILURE;
3789
0
  }
3790
3791
  /* Check if capability is already sent. If the flag force is set
3792
   * send the capability since this can be initial bgp configuration
3793
   */
3794
0
  memset(&api, 0, sizeof(api));
3795
0
  if (disable) {
3796
0
    api.cap = ZEBRA_CLIENT_GR_DISABLE;
3797
0
    api.vrf_id = bgp->vrf_id;
3798
0
  } else {
3799
0
    api.cap = ZEBRA_CLIENT_GR_CAPABILITIES;
3800
0
    api.stale_removal_time = bgp->rib_stale_time;
3801
0
    api.vrf_id = bgp->vrf_id;
3802
0
  }
3803
3804
0
  if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3805
0
      == ZCLIENT_SEND_FAILURE) {
3806
0
    zlog_err("%s: %s error sending capability", __func__,
3807
0
       bgp->name_pretty);
3808
0
    ret = BGP_GR_FAILURE;
3809
0
  } else {
3810
0
    if (disable)
3811
0
      bgp->present_zebra_gr_state = ZEBRA_GR_DISABLE;
3812
0
    else
3813
0
      bgp->present_zebra_gr_state = ZEBRA_GR_ENABLE;
3814
3815
0
    if (BGP_DEBUG(zebra, ZEBRA))
3816
0
      zlog_debug("%s: %s send capabilty success", __func__,
3817
0
           bgp->name_pretty);
3818
0
    ret = BGP_GR_SUCCESS;
3819
0
  }
3820
0
  return ret;
3821
0
}
3822
3823
/* Send route update pesding or completed status to RIB for the
3824
 * specific AFI, SAFI
3825
 */
3826
int bgp_zebra_update(struct bgp *bgp, afi_t afi, safi_t safi,
3827
         enum zserv_client_capabilities type)
3828
0
{
3829
0
  struct zapi_cap api = {0};
3830
3831
0
  if (BGP_DEBUG(zebra, ZEBRA))
3832
0
    zlog_debug("%s: %s afi: %u safi: %u Command %s", __func__,
3833
0
         bgp->name_pretty, afi, safi,
3834
0
         zserv_gr_client_cap_string(type));
3835
3836
0
  if (zclient == NULL) {
3837
0
    if (BGP_DEBUG(zebra, ZEBRA))
3838
0
      zlog_debug("%s: %s zclient == NULL, invalid", __func__,
3839
0
           bgp->name_pretty);
3840
0
    return BGP_GR_FAILURE;
3841
0
  }
3842
3843
  /* Check if the client is connected */
3844
0
  if ((zclient->sock < 0) || (zclient->t_connect)) {
3845
0
    if (BGP_DEBUG(zebra, ZEBRA))
3846
0
      zlog_debug("%s: %s client not connected", __func__,
3847
0
           bgp->name_pretty);
3848
0
    return BGP_GR_FAILURE;
3849
0
  }
3850
3851
0
  api.afi = afi;
3852
0
  api.safi = safi;
3853
0
  api.vrf_id = bgp->vrf_id;
3854
0
  api.cap = type;
3855
3856
0
  if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3857
0
      == ZCLIENT_SEND_FAILURE) {
3858
0
    if (BGP_DEBUG(zebra, ZEBRA))
3859
0
      zlog_debug("%s: %s error sending capability", __func__,
3860
0
           bgp->name_pretty);
3861
0
    return BGP_GR_FAILURE;
3862
0
  }
3863
0
  return BGP_GR_SUCCESS;
3864
0
}
3865
3866
3867
/* Send RIB stale timer update */
3868
int bgp_zebra_stale_timer_update(struct bgp *bgp)
3869
0
{
3870
0
  struct zapi_cap api;
3871
3872
0
  if (BGP_DEBUG(zebra, ZEBRA))
3873
0
    zlog_debug("%s: %s Timer Update to %u", __func__,
3874
0
         bgp->name_pretty, bgp->rib_stale_time);
3875
3876
0
  if (zclient == NULL) {
3877
0
    if (BGP_DEBUG(zebra, ZEBRA))
3878
0
      zlog_debug("zclient invalid");
3879
0
    return BGP_GR_FAILURE;
3880
0
  }
3881
3882
  /* Check if the client is connected */
3883
0
  if ((zclient->sock < 0) || (zclient->t_connect)) {
3884
0
    if (BGP_DEBUG(zebra, ZEBRA))
3885
0
      zlog_debug("%s: %s client not connected", __func__,
3886
0
           bgp->name_pretty);
3887
0
    return BGP_GR_FAILURE;
3888
0
  }
3889
3890
0
  memset(&api, 0, sizeof(api));
3891
0
  api.cap = ZEBRA_CLIENT_RIB_STALE_TIME;
3892
0
  api.stale_removal_time = bgp->rib_stale_time;
3893
0
  api.vrf_id = bgp->vrf_id;
3894
0
  if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3895
0
      == ZCLIENT_SEND_FAILURE) {
3896
0
    if (BGP_DEBUG(zebra, ZEBRA))
3897
0
      zlog_debug("%s: %s error sending capability", __func__,
3898
0
           bgp->name_pretty);
3899
0
    return BGP_GR_FAILURE;
3900
0
  }
3901
3902
0
  return BGP_GR_SUCCESS;
3903
0
}
3904
3905
int bgp_zebra_srv6_manager_get_locator_chunk(const char *name)
3906
0
{
3907
0
  return srv6_manager_get_locator_chunk(zclient, name);
3908
0
}
3909
3910
int bgp_zebra_srv6_manager_release_locator_chunk(const char *name)
3911
0
{
3912
0
  return srv6_manager_release_locator_chunk(zclient, name);
3913
0
}
3914
3915
void bgp_zebra_send_nexthop_label(int cmd, mpls_label_t label,
3916
          ifindex_t ifindex, vrf_id_t vrf_id,
3917
          enum lsp_types_t ltype, struct prefix *p)
3918
0
{
3919
0
  struct zapi_labels zl = {};
3920
0
  struct zapi_nexthop *znh;
3921
3922
0
  zl.type = ltype;
3923
0
  zl.local_label = label;
3924
0
  zl.nexthop_num = 1;
3925
0
  znh = &zl.nexthops[0];
3926
0
  if (p->family == AF_INET)
3927
0
    IPV4_ADDR_COPY(&znh->gate.ipv4, &p->u.prefix4);
3928
0
  else
3929
0
    IPV6_ADDR_COPY(&znh->gate.ipv6, &p->u.prefix6);
3930
0
  if (ifindex == IFINDEX_INTERNAL)
3931
0
    znh->type = (p->family == AF_INET) ? NEXTHOP_TYPE_IPV4
3932
0
               : NEXTHOP_TYPE_IPV6;
3933
0
  else
3934
0
    znh->type = (p->family == AF_INET) ? NEXTHOP_TYPE_IPV4_IFINDEX
3935
0
               : NEXTHOP_TYPE_IPV6_IFINDEX;
3936
0
  znh->ifindex = ifindex;
3937
0
  znh->vrf_id = vrf_id;
3938
0
  znh->label_num = 0;
3939
3940
  /* vrf_id is DEFAULT_VRF */
3941
0
  zebra_send_mpls_labels(zclient, cmd, &zl);
3942
0
}