1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2018 Cumulus Networks, Inc.
22 #include "nexthop_group.h"
26 #include "pbr_memory.h"
27 #include "pbr_zebra.h"
28 #include "pbr_debug.h"
31 DEFINE_MTYPE_STATIC(PBRD
, PBR_INTERFACE
, "PBR Interface");
33 /* Zebra structure to hold current status. */
34 struct zclient
*zclient
;
36 struct pbr_interface
*pbr_if_new(struct interface
*ifp
)
38 struct pbr_interface
*pbr_ifp
;
43 pbr_ifp
= XCALLOC(MTYPE_PBR_INTERFACE
, sizeof(*pbr_ifp
));
49 void pbr_if_del(struct interface
*ifp
)
51 XFREE(MTYPE_PBR_INTERFACE
, ifp
->info
);
54 /* Interface addition message from zebra. */
55 int pbr_ifp_create(struct interface
*ifp
)
57 DEBUGD(&pbr_dbg_zebra
, "%s: %s", __func__
, ifp
->name
);
62 pbr_nht_interface_update(ifp
);
63 /* Update nexthops tracked from a `set nexthop` command */
64 pbr_nht_nexthop_interface_update(ifp
);
66 pbr_map_policy_interface_update(ifp
, true);
71 int pbr_ifp_destroy(struct interface
*ifp
)
73 DEBUGD(&pbr_dbg_zebra
, "%s: %s", __func__
, ifp
->name
);
75 pbr_map_policy_interface_update(ifp
, false);
80 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
83 char buf
[PREFIX_STRLEN
];
85 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
87 DEBUGD(&pbr_dbg_zebra
, "%s: %s added %s", __func__
,
88 c
? c
->ifp
->name
: "Unknown",
89 c
? prefix2str(c
->address
, buf
, sizeof(buf
)) : "Unknown");
94 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
98 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
103 DEBUGD(&pbr_dbg_zebra
, "%s: %s deleted %pFX", __func__
, c
->ifp
->name
,
110 int pbr_ifp_up(struct interface
*ifp
)
112 DEBUGD(&pbr_dbg_zebra
, "%s: %s is up", __func__
, ifp
->name
);
114 pbr_nht_nexthop_interface_update(ifp
);
119 int pbr_ifp_down(struct interface
*ifp
)
121 DEBUGD(&pbr_dbg_zebra
, "%s: %s is down", __func__
, ifp
->name
);
123 pbr_nht_nexthop_interface_update(ifp
);
128 static int interface_vrf_update(ZAPI_CALLBACK_ARGS
)
130 struct interface
*ifp
;
133 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
137 DEBUGD(&pbr_dbg_zebra
, "%s: VRF change interface not found",
143 DEBUGD(&pbr_dbg_zebra
, "%s: %s VRF change %u -> %u", __func__
,
144 ifp
->name
, vrf_id
, new_vrf_id
);
146 if_update_to_new_vrf(ifp
, new_vrf_id
);
151 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
154 enum zapi_route_notify_owner note
;
157 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
,
162 case ZAPI_ROUTE_FAIL_INSTALL
:
163 DEBUGD(&pbr_dbg_zebra
,
164 "%s: [%pFX] Route install failure for table: %u",
165 __func__
, &p
, table_id
);
167 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
168 DEBUGD(&pbr_dbg_zebra
,
169 "%s: [%pFX] Route better admin distance won for table: %u",
170 __func__
, &p
, table_id
);
172 case ZAPI_ROUTE_INSTALLED
:
173 DEBUGD(&pbr_dbg_zebra
,
174 "%s: [%pFX] Route installed succeeded for table: %u",
175 __func__
, &p
, table_id
);
176 pbr_nht_route_installed_for_table(table_id
);
178 case ZAPI_ROUTE_REMOVED
:
179 DEBUGD(&pbr_dbg_zebra
,
180 "%s: [%pFX] Route Removed succeeded for table: %u",
181 __func__
, &p
, table_id
);
182 pbr_nht_route_removed_for_table(table_id
);
184 case ZAPI_ROUTE_REMOVE_FAIL
:
185 DEBUGD(&pbr_dbg_zebra
,
186 "%s: [%pFX] Route remove fail for table: %u", __func__
,
194 static int rule_notify_owner(ZAPI_CALLBACK_ARGS
)
196 uint32_t seqno
, priority
, unique
;
197 enum zapi_rule_notify_owner note
;
198 struct pbr_map_sequence
*pbrms
;
199 struct pbr_map_interface
*pmi
;
200 char ifname
[INTERFACE_NAMSIZ
+ 1];
203 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
208 pbrms
= pbrms_lookup_unique(unique
, ifname
, &pmi
);
210 DEBUGD(&pbr_dbg_zebra
,
211 "%s: Failure to lookup pbrms based upon %u", __func__
,
216 installed
= 1 << pmi
->install_bit
;
219 case ZAPI_RULE_FAIL_INSTALL
:
220 pbrms
->installed
&= ~installed
;
222 case ZAPI_RULE_INSTALLED
:
223 pbrms
->installed
|= installed
;
225 case ZAPI_RULE_FAIL_REMOVE
:
226 /* Don't change state on rule removal failure */
228 case ZAPI_RULE_REMOVED
:
229 pbrms
->installed
&= ~installed
;
233 DEBUGD(&pbr_dbg_zebra
, "%s: Received %s: %" PRIu64
, __func__
,
234 zapi_rule_notify_owner2str(note
), pbrms
->installed
);
236 pbr_map_final_interface_deletion(pbrms
->parent
, pmi
);
241 static void zebra_connected(struct zclient
*zclient
)
243 DEBUGD(&pbr_dbg_zebra
, "%s: Registering for fun and profit", __func__
);
244 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
247 static void route_add_helper(struct zapi_route
*api
, struct nexthop_group nhg
,
250 struct zapi_nexthop
*api_nh
;
251 struct nexthop
*nhop
;
254 api
->prefix
.family
= install_afi
;
256 DEBUGD(&pbr_dbg_zebra
, " Encoding %pFX", &api
->prefix
);
259 for (ALL_NEXTHOPS(nhg
, nhop
)) {
260 api_nh
= &api
->nexthops
[i
];
261 api_nh
->vrf_id
= nhop
->vrf_id
;
262 api_nh
->type
= nhop
->type
;
263 api_nh
->weight
= nhop
->weight
;
264 switch (nhop
->type
) {
265 case NEXTHOP_TYPE_IPV4
:
266 api_nh
->gate
.ipv4
= nhop
->gate
.ipv4
;
268 case NEXTHOP_TYPE_IPV4_IFINDEX
:
269 api_nh
->gate
.ipv4
= nhop
->gate
.ipv4
;
270 api_nh
->ifindex
= nhop
->ifindex
;
272 case NEXTHOP_TYPE_IFINDEX
:
273 api_nh
->ifindex
= nhop
->ifindex
;
275 case NEXTHOP_TYPE_IPV6
:
276 memcpy(&api_nh
->gate
.ipv6
, &nhop
->gate
.ipv6
,
279 case NEXTHOP_TYPE_IPV6_IFINDEX
:
280 api_nh
->ifindex
= nhop
->ifindex
;
281 memcpy(&api_nh
->gate
.ipv6
, &nhop
->gate
.ipv6
,
284 case NEXTHOP_TYPE_BLACKHOLE
:
285 api_nh
->bh_type
= nhop
->bh_type
;
290 api
->nexthop_num
= i
;
292 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, api
);
296 * This function assumes a default route is being
297 * installed into the appropriate tableid
299 void route_add(struct pbr_nexthop_group_cache
*pnhgc
, struct nexthop_group nhg
,
302 struct zapi_route api
;
304 DEBUGD(&pbr_dbg_zebra
, "%s for Table: %d", __func__
, pnhgc
->table_id
);
306 memset(&api
, 0, sizeof(api
));
308 api
.vrf_id
= VRF_DEFAULT
;
309 api
.type
= ZEBRA_ROUTE_PBR
;
310 api
.safi
= SAFI_UNICAST
;
312 * Sending a default route
314 api
.tableid
= pnhgc
->table_id
;
315 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
316 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
317 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
318 switch (install_afi
) {
320 route_add_helper(&api
, nhg
, AF_INET
);
321 route_add_helper(&api
, nhg
, AF_INET6
);
324 route_add_helper(&api
, nhg
, AF_INET
);
327 route_add_helper(&api
, nhg
, AF_INET6
);
330 DEBUGD(&pbr_dbg_zebra
,
331 "%s: Asked to install unsupported route type: L2VPN",
335 DEBUGD(&pbr_dbg_zebra
,
336 "%s: Asked to install unspecified route type", __func__
);
342 * This function assumes a default route is being
343 * removed from the appropriate tableid
345 void route_delete(struct pbr_nexthop_group_cache
*pnhgc
, afi_t afi
)
347 struct zapi_route api
;
349 DEBUGD(&pbr_dbg_zebra
, "%s for Table: %d", __func__
, pnhgc
->table_id
);
351 memset(&api
, 0, sizeof(api
));
352 api
.vrf_id
= VRF_DEFAULT
;
353 api
.type
= ZEBRA_ROUTE_PBR
;
354 api
.safi
= SAFI_UNICAST
;
356 api
.tableid
= pnhgc
->table_id
;
357 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
361 api
.prefix
.family
= AF_INET
;
362 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
365 api
.prefix
.family
= AF_INET6
;
366 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
369 api
.prefix
.family
= AF_INET
;
370 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
371 api
.prefix
.family
= AF_INET6
;
372 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
375 DEBUGD(&pbr_dbg_zebra
,
376 "%s: Asked to delete unsupported route type: L2VPN",
380 DEBUGD(&pbr_dbg_zebra
,
381 "%s: Asked to delete unspecified route type", __func__
);
386 static int pbr_zebra_nexthop_update(ZAPI_CALLBACK_ARGS
)
388 struct zapi_route nhr
;
389 struct prefix matched
;
392 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &matched
, &nhr
)) {
393 zlog_err("Failure to decode Nexthop update message");
397 if (DEBUG_MODE_CHECK(&pbr_dbg_zebra
, DEBUG_MODE_ALL
)) {
399 DEBUGD(&pbr_dbg_zebra
,
400 "%s: Received Nexthop update: %pFX against %pFX",
401 __func__
, &matched
, &nhr
.prefix
);
403 DEBUGD(&pbr_dbg_zebra
, "%s: (Nexthops(%u)", __func__
,
406 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
407 DEBUGD(&pbr_dbg_zebra
,
408 "%s: Type: %d: vrf: %d, ifindex: %d gate: %pI4",
409 __func__
, nhr
.nexthops
[i
].type
,
410 nhr
.nexthops
[i
].vrf_id
, nhr
.nexthops
[i
].ifindex
,
411 &nhr
.nexthops
[i
].gate
.ipv4
);
415 nhr
.prefix
= matched
;
416 pbr_nht_nexthop_update(&nhr
);
420 extern struct zebra_privs_t pbr_privs
;
422 static zclient_handler
*const pbr_handlers
[] = {
423 [ZEBRA_INTERFACE_ADDRESS_ADD
] = interface_address_add
,
424 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = interface_address_delete
,
425 [ZEBRA_INTERFACE_VRF_UPDATE
] = interface_vrf_update
,
426 [ZEBRA_ROUTE_NOTIFY_OWNER
] = route_notify_owner
,
427 [ZEBRA_RULE_NOTIFY_OWNER
] = rule_notify_owner
,
428 [ZEBRA_NEXTHOP_UPDATE
] = pbr_zebra_nexthop_update
,
431 void pbr_zebra_init(void)
433 struct zclient_options opt
= { .receive_notify
= true };
435 zclient
= zclient_new(master
, &opt
, pbr_handlers
,
436 array_size(pbr_handlers
));
438 zclient_init(zclient
, ZEBRA_ROUTE_PBR
, 0, &pbr_privs
);
439 zclient
->zebra_connected
= zebra_connected
;
442 void pbr_send_rnh(struct nexthop
*nhop
, bool reg
)
448 ZEBRA_NEXTHOP_REGISTER
: ZEBRA_NEXTHOP_UNREGISTER
;
450 memset(&p
, 0, sizeof(p
));
451 switch (nhop
->type
) {
452 case NEXTHOP_TYPE_IFINDEX
:
453 case NEXTHOP_TYPE_BLACKHOLE
:
455 case NEXTHOP_TYPE_IPV4
:
456 case NEXTHOP_TYPE_IPV4_IFINDEX
:
458 p
.u
.prefix4
.s_addr
= nhop
->gate
.ipv4
.s_addr
;
459 p
.prefixlen
= IPV4_MAX_BITLEN
;
461 case NEXTHOP_TYPE_IPV6
:
462 case NEXTHOP_TYPE_IPV6_IFINDEX
:
464 memcpy(&p
.u
.prefix6
, &nhop
->gate
.ipv6
, IPV6_MAX_BYTELEN
);
465 p
.prefixlen
= IPV6_MAX_BITLEN
;
466 if (IN6_IS_ADDR_LINKLOCAL(&nhop
->gate
.ipv6
))
468 * Don't bother tracking link locals, just track their
475 if (zclient_send_rnh(zclient
, command
, &p
, SAFI_UNICAST
, false, false,
477 == ZCLIENT_SEND_FAILURE
) {
478 zlog_warn("%s: Failure to send nexthop to zebra", __func__
);
482 static void pbr_encode_pbr_map_sequence_prefix(struct stream
*s
,
484 unsigned char family
)
489 memset(&any
, 0, sizeof(any
));
494 stream_putc(s
, p
->family
);
495 stream_putc(s
, p
->prefixlen
);
496 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
500 pbr_encode_pbr_map_sequence_vrf(struct stream
*s
,
501 const struct pbr_map_sequence
*pbrms
,
502 const struct interface
*ifp
)
504 struct pbr_vrf
*pbr_vrf
;
506 if (pbrms
->vrf_unchanged
)
507 pbr_vrf
= ifp
->vrf
->info
;
509 pbr_vrf
= pbr_vrf_lookup_by_name(pbrms
->vrf_name
);
512 DEBUGD(&pbr_dbg_zebra
, "%s: VRF not found", __func__
);
516 stream_putl(s
, pbr_vrf
->vrf
->data
.l
.table_id
);
519 static void pbr_encode_pbr_map_sequence(struct stream
*s
,
520 struct pbr_map_sequence
*pbrms
,
521 struct interface
*ifp
)
523 unsigned char family
;
527 family
= pbrms
->family
;
529 stream_putl(s
, pbrms
->seqno
);
530 stream_putl(s
, pbrms
->ruleno
);
531 stream_putl(s
, pbrms
->unique
);
532 stream_putc(s
, pbrms
->ip_proto
); /* The ip_proto */
533 pbr_encode_pbr_map_sequence_prefix(s
, pbrms
->src
, family
);
534 stream_putw(s
, pbrms
->src_prt
);
535 pbr_encode_pbr_map_sequence_prefix(s
, pbrms
->dst
, family
);
536 stream_putw(s
, pbrms
->dst_prt
);
537 stream_putc(s
, pbrms
->dsfield
);
538 stream_putl(s
, pbrms
->mark
);
540 stream_putl(s
, pbrms
->action_queue_id
);
542 stream_putw(s
, pbrms
->action_vlan_id
);
543 stream_putw(s
, pbrms
->action_vlan_flags
);
544 stream_putw(s
, pbrms
->action_pcp
);
546 if (pbrms
->vrf_unchanged
|| pbrms
->vrf_lookup
)
547 pbr_encode_pbr_map_sequence_vrf(s
, pbrms
, ifp
);
548 else if (pbrms
->nhgrp_name
)
549 stream_putl(s
, pbr_nht_get_table(pbrms
->nhgrp_name
));
551 stream_putl(s
, pbr_nht_get_table(pbrms
->internal_nhg_name
));
552 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
555 bool pbr_send_pbr_map(struct pbr_map_sequence
*pbrms
,
556 struct pbr_map_interface
*pmi
, bool install
, bool changed
)
558 struct pbr_map
*pbrm
= pbrms
->parent
;
560 uint64_t is_installed
= (uint64_t)1 << pmi
->install_bit
;
562 is_installed
&= pbrms
->installed
;
564 DEBUGD(&pbr_dbg_zebra
, "%s: for %s %d(%" PRIu64
")", __func__
,
565 pbrm
->name
, install
, is_installed
);
568 * If we are installed and asked to do so again and the config
569 * has not changed, just return.
571 * If we are not installed and asked
572 * to delete just return.
574 if (install
&& is_installed
&& !changed
)
577 if (!install
&& !is_installed
)
583 zclient_create_header(s
,
584 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
588 * We are sending one item at a time at the moment
592 DEBUGD(&pbr_dbg_zebra
, "%s: %s %s seq %u %d %s %u", __func__
,
593 install
? "Installing" : "Deleting", pbrm
->name
, pbrms
->seqno
,
594 install
, pmi
->ifp
->name
, pmi
->delete);
596 pbr_encode_pbr_map_sequence(s
, pbrms
, pmi
->ifp
);
598 stream_putw_at(s
, 0, stream_get_endp(s
));
600 zclient_send_message(zclient
);