3 * Copyright (C) 2018 Cumulus Networks, Inc.
6 * FRR is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * FRR is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include "nexthop_group.h"
39 #include "pbr_memory.h"
40 #include "pbr_zebra.h"
41 #include "pbr_debug.h"
44 DEFINE_MTYPE_STATIC(PBRD
, PBR_INTERFACE
, "PBR Interface")
46 /* Zebra structure to hold current status. */
47 struct zclient
*zclient
;
49 struct pbr_interface
*pbr_if_new(struct interface
*ifp
)
51 struct pbr_interface
*pbr_ifp
;
56 pbr_ifp
= XCALLOC(MTYPE_PBR_INTERFACE
, sizeof(*pbr_ifp
));
62 void pbr_if_del(struct interface
*ifp
)
64 XFREE(MTYPE_PBR_INTERFACE
, ifp
->info
);
67 /* Inteface addition message from zebra. */
68 int pbr_ifp_create(struct interface
*ifp
)
70 DEBUGD(&pbr_dbg_zebra
, "%s: %s", __func__
, ifp
->name
);
75 pbr_nht_interface_update(ifp
);
76 /* Update nexthops tracked from a `set nexthop` command */
77 pbr_nht_nexthop_interface_update(ifp
);
79 pbr_map_policy_interface_update(ifp
, true);
84 int pbr_ifp_destroy(struct interface
*ifp
)
86 DEBUGD(&pbr_dbg_zebra
, "%s: %s", __func__
, ifp
->name
);
88 pbr_map_policy_interface_update(ifp
, false);
93 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
96 char buf
[PREFIX_STRLEN
];
98 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
100 DEBUGD(&pbr_dbg_zebra
, "%s: %s added %s", __func__
,
101 c
? c
->ifp
->name
: "Unknown",
102 c
? prefix2str(c
->address
, buf
, sizeof(buf
)) : "Unknown");
107 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
111 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
116 DEBUGD(&pbr_dbg_zebra
, "%s: %s deleted %pFX", __func__
, c
->ifp
->name
,
123 int pbr_ifp_up(struct interface
*ifp
)
125 DEBUGD(&pbr_dbg_zebra
, "%s: %s is up", __func__
, ifp
->name
);
127 pbr_nht_nexthop_interface_update(ifp
);
132 int pbr_ifp_down(struct interface
*ifp
)
134 DEBUGD(&pbr_dbg_zebra
, "%s: %s is down", __func__
, ifp
->name
);
136 pbr_nht_nexthop_interface_update(ifp
);
141 static int interface_vrf_update(ZAPI_CALLBACK_ARGS
)
143 struct interface
*ifp
;
146 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
150 DEBUGD(&pbr_dbg_zebra
, "%s: VRF change interface not found",
156 DEBUGD(&pbr_dbg_zebra
, "%s: %s VRF change %u -> %u", __func__
,
157 ifp
->name
, vrf_id
, new_vrf_id
);
159 if_update_to_new_vrf(ifp
, new_vrf_id
);
164 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
167 enum zapi_route_notify_owner note
;
170 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
))
174 case ZAPI_ROUTE_FAIL_INSTALL
:
175 DEBUGD(&pbr_dbg_zebra
,
176 "%s: [%pFX] Route install failure for table: %u",
177 __func__
, &p
, table_id
);
179 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
180 DEBUGD(&pbr_dbg_zebra
,
181 "%s: [%pFX] Route better admin distance won for table: %u",
182 __func__
, &p
, table_id
);
184 case ZAPI_ROUTE_INSTALLED
:
185 DEBUGD(&pbr_dbg_zebra
,
186 "%s: [%pFX] Route installed succeeded for table: %u",
187 __func__
, &p
, table_id
);
188 pbr_nht_route_installed_for_table(table_id
);
190 case ZAPI_ROUTE_REMOVED
:
191 DEBUGD(&pbr_dbg_zebra
,
192 "%s: [%pFX] Route Removed succeeded for table: %u",
193 __func__
, &p
, table_id
);
194 pbr_nht_route_removed_for_table(table_id
);
196 case ZAPI_ROUTE_REMOVE_FAIL
:
197 DEBUGD(&pbr_dbg_zebra
,
198 "%s: [%pFX] Route remove fail for table: %u", __func__
,
206 static int rule_notify_owner(ZAPI_CALLBACK_ARGS
)
208 uint32_t seqno
, priority
, unique
;
209 enum zapi_rule_notify_owner note
;
210 struct pbr_map_sequence
*pbrms
;
211 struct pbr_map_interface
*pmi
;
212 char ifname
[INTERFACE_NAMSIZ
+ 1];
215 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
220 pbrms
= pbrms_lookup_unique(unique
, ifname
, &pmi
);
222 DEBUGD(&pbr_dbg_zebra
,
223 "%s: Failure to lookup pbrms based upon %u", __func__
,
228 installed
= 1 << pmi
->install_bit
;
231 case ZAPI_RULE_FAIL_INSTALL
:
232 pbrms
->installed
&= ~installed
;
234 case ZAPI_RULE_INSTALLED
:
235 pbrms
->installed
|= installed
;
237 case ZAPI_RULE_FAIL_REMOVE
:
238 /* Don't change state on rule removal failure */
240 case ZAPI_RULE_REMOVED
:
241 pbrms
->installed
&= ~installed
;
245 DEBUGD(&pbr_dbg_zebra
, "%s: Received %s: %" PRIu64
, __func__
,
246 zapi_rule_notify_owner2str(note
), pbrms
->installed
);
248 pbr_map_final_interface_deletion(pbrms
->parent
, pmi
);
253 static void zebra_connected(struct zclient
*zclient
)
255 DEBUGD(&pbr_dbg_zebra
, "%s: Registering for fun and profit", __func__
);
256 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
259 static void route_add_helper(struct zapi_route
*api
, struct nexthop_group nhg
,
262 struct zapi_nexthop
*api_nh
;
263 struct nexthop
*nhop
;
266 api
->prefix
.family
= install_afi
;
268 DEBUGD(&pbr_dbg_zebra
, "\tEncoding %pFX", &api
->prefix
);
271 for (ALL_NEXTHOPS(nhg
, nhop
)) {
272 api_nh
= &api
->nexthops
[i
];
273 api_nh
->vrf_id
= nhop
->vrf_id
;
274 api_nh
->type
= nhop
->type
;
275 api_nh
->weight
= nhop
->weight
;
276 switch (nhop
->type
) {
277 case NEXTHOP_TYPE_IPV4
:
278 api_nh
->gate
.ipv4
= nhop
->gate
.ipv4
;
280 case NEXTHOP_TYPE_IPV4_IFINDEX
:
281 api_nh
->gate
.ipv4
= nhop
->gate
.ipv4
;
282 api_nh
->ifindex
= nhop
->ifindex
;
284 case NEXTHOP_TYPE_IFINDEX
:
285 api_nh
->ifindex
= nhop
->ifindex
;
287 case NEXTHOP_TYPE_IPV6
:
288 memcpy(&api_nh
->gate
.ipv6
, &nhop
->gate
.ipv6
, 16);
290 case NEXTHOP_TYPE_IPV6_IFINDEX
:
291 api_nh
->ifindex
= nhop
->ifindex
;
292 memcpy(&api_nh
->gate
.ipv6
, &nhop
->gate
.ipv6
, 16);
294 case NEXTHOP_TYPE_BLACKHOLE
:
295 api_nh
->bh_type
= nhop
->bh_type
;
300 api
->nexthop_num
= i
;
302 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, api
);
306 * This function assumes a default route is being
307 * installed into the appropriate tableid
309 void route_add(struct pbr_nexthop_group_cache
*pnhgc
, struct nexthop_group nhg
,
312 struct zapi_route api
;
314 DEBUGD(&pbr_dbg_zebra
, "%s for Table: %d", __func__
, pnhgc
->table_id
);
316 memset(&api
, 0, sizeof(api
));
318 api
.vrf_id
= VRF_DEFAULT
;
319 api
.type
= ZEBRA_ROUTE_PBR
;
320 api
.safi
= SAFI_UNICAST
;
322 * Sending a default route
324 api
.tableid
= pnhgc
->table_id
;
325 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
326 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
327 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
328 switch (install_afi
) {
330 route_add_helper(&api
, nhg
, AF_INET
);
331 route_add_helper(&api
, nhg
, AF_INET6
);
334 route_add_helper(&api
, nhg
, AF_INET
);
337 route_add_helper(&api
, nhg
, AF_INET6
);
340 DEBUGD(&pbr_dbg_zebra
,
341 "%s: Asked to install unsupported route type: L2VPN",
345 DEBUGD(&pbr_dbg_zebra
,
346 "%s: Asked to install unspecified route type", __func__
);
352 * This function assumes a default route is being
353 * removed from the appropriate tableid
355 void route_delete(struct pbr_nexthop_group_cache
*pnhgc
, afi_t afi
)
357 struct zapi_route api
;
359 DEBUGD(&pbr_dbg_zebra
, "%s for Table: %d", __func__
, pnhgc
->table_id
);
361 memset(&api
, 0, sizeof(api
));
362 api
.vrf_id
= VRF_DEFAULT
;
363 api
.type
= ZEBRA_ROUTE_PBR
;
364 api
.safi
= SAFI_UNICAST
;
366 api
.tableid
= pnhgc
->table_id
;
367 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
371 api
.prefix
.family
= AF_INET
;
372 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
375 api
.prefix
.family
= AF_INET6
;
376 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
379 api
.prefix
.family
= AF_INET
;
380 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
381 api
.prefix
.family
= AF_INET6
;
382 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
385 DEBUGD(&pbr_dbg_zebra
,
386 "%s: Asked to delete unsupported route type: L2VPN",
390 DEBUGD(&pbr_dbg_zebra
,
391 "%s: Asked to delete unspecified route type", __func__
);
396 static int pbr_zebra_nexthop_update(ZAPI_CALLBACK_ARGS
)
398 struct zapi_route nhr
;
401 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
402 zlog_err("Failure to decode Nexthop update message");
406 if (DEBUG_MODE_CHECK(&pbr_dbg_zebra
, DEBUG_MODE_ALL
)) {
408 DEBUGD(&pbr_dbg_zebra
, "%s: Received Nexthop update: %pFX",
409 __func__
, &nhr
.prefix
);
411 DEBUGD(&pbr_dbg_zebra
, "%s: (\tNexthops(%u)", __func__
,
414 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
415 DEBUGD(&pbr_dbg_zebra
,
416 "%s: \tType: %d: vrf: %d, ifindex: %d gate: %s",
417 __func__
, nhr
.nexthops
[i
].type
,
418 nhr
.nexthops
[i
].vrf_id
, nhr
.nexthops
[i
].ifindex
,
419 inet_ntoa(nhr
.nexthops
[i
].gate
.ipv4
));
423 pbr_nht_nexthop_update(&nhr
);
427 extern struct zebra_privs_t pbr_privs
;
429 void pbr_zebra_init(void)
431 struct zclient_options opt
= { .receive_notify
= true };
433 zclient
= zclient_new(master
, &opt
);
435 zclient_init(zclient
, ZEBRA_ROUTE_PBR
, 0, &pbr_privs
);
436 zclient
->zebra_connected
= zebra_connected
;
437 zclient
->interface_address_add
= interface_address_add
;
438 zclient
->interface_address_delete
= interface_address_delete
;
439 zclient
->interface_vrf_update
= interface_vrf_update
;
440 zclient
->route_notify_owner
= route_notify_owner
;
441 zclient
->rule_notify_owner
= rule_notify_owner
;
442 zclient
->nexthop_update
= pbr_zebra_nexthop_update
;
445 void pbr_send_rnh(struct nexthop
*nhop
, bool reg
)
451 ZEBRA_NEXTHOP_REGISTER
: ZEBRA_NEXTHOP_UNREGISTER
;
453 memset(&p
, 0, sizeof(p
));
454 switch (nhop
->type
) {
455 case NEXTHOP_TYPE_IFINDEX
:
456 case NEXTHOP_TYPE_BLACKHOLE
:
458 case NEXTHOP_TYPE_IPV4
:
459 case NEXTHOP_TYPE_IPV4_IFINDEX
:
461 p
.u
.prefix4
.s_addr
= nhop
->gate
.ipv4
.s_addr
;
464 case NEXTHOP_TYPE_IPV6
:
465 case NEXTHOP_TYPE_IPV6_IFINDEX
:
467 memcpy(&p
.u
.prefix6
, &nhop
->gate
.ipv6
, 16);
469 if (IN6_IS_ADDR_LINKLOCAL(&nhop
->gate
.ipv6
))
471 * Don't bother tracking link locals, just track their
478 if (zclient_send_rnh(zclient
, command
, &p
,
479 false, nhop
->vrf_id
) < 0) {
480 zlog_warn("%s: Failure to send nexthop to zebra", __func__
);
484 static void pbr_encode_pbr_map_sequence_prefix(struct stream
*s
,
486 unsigned char family
)
491 memset(&any
, 0, sizeof(any
));
496 stream_putc(s
, p
->family
);
497 stream_putc(s
, p
->prefixlen
);
498 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
502 pbr_encode_pbr_map_sequence_vrf(struct stream
*s
,
503 const struct pbr_map_sequence
*pbrms
,
504 const struct interface
*ifp
)
506 struct pbr_vrf
*pbr_vrf
;
508 if (pbrms
->vrf_unchanged
)
509 pbr_vrf
= pbr_vrf_lookup_by_id(ifp
->vrf_id
);
511 pbr_vrf
= pbr_vrf_lookup_by_name(pbrms
->vrf_name
);
514 DEBUGD(&pbr_dbg_zebra
, "%s: VRF not found", __func__
);
518 stream_putl(s
, pbr_vrf
->vrf
->data
.l
.table_id
);
521 static void pbr_encode_pbr_map_sequence(struct stream
*s
,
522 struct pbr_map_sequence
*pbrms
,
523 struct interface
*ifp
)
525 unsigned char family
;
529 family
= pbrms
->family
;
531 stream_putl(s
, pbrms
->seqno
);
532 stream_putl(s
, pbrms
->ruleno
);
533 stream_putl(s
, pbrms
->unique
);
534 pbr_encode_pbr_map_sequence_prefix(s
, pbrms
->src
, family
);
535 stream_putw(s
, 0); /* src port */
536 pbr_encode_pbr_map_sequence_prefix(s
, pbrms
->dst
, family
);
537 stream_putw(s
, 0); /* dst port */
538 stream_putc(s
, pbrms
->dsfield
);
539 stream_putl(s
, pbrms
->mark
);
541 if (pbrms
->vrf_unchanged
|| pbrms
->vrf_lookup
)
542 pbr_encode_pbr_map_sequence_vrf(s
, pbrms
, ifp
);
543 else if (pbrms
->nhgrp_name
)
544 stream_putl(s
, pbr_nht_get_table(pbrms
->nhgrp_name
));
546 stream_putl(s
, pbr_nht_get_table(pbrms
->internal_nhg_name
));
547 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
550 bool pbr_send_pbr_map(struct pbr_map_sequence
*pbrms
,
551 struct pbr_map_interface
*pmi
, bool install
, bool changed
)
553 struct pbr_map
*pbrm
= pbrms
->parent
;
555 uint64_t is_installed
= (uint64_t)1 << pmi
->install_bit
;
557 is_installed
&= pbrms
->installed
;
559 DEBUGD(&pbr_dbg_zebra
, "%s: for %s %d(%" PRIu64
")", __func__
,
560 pbrm
->name
, install
, is_installed
);
563 * If we are installed and asked to do so again and the config
564 * has not changed, just return.
566 * If we are not installed and asked
567 * to delete just return.
569 if (install
&& is_installed
&& !changed
)
572 if (!install
&& !is_installed
)
578 zclient_create_header(s
,
579 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
583 * We are sending one item at a time at the moment
587 DEBUGD(&pbr_dbg_zebra
, "%s: \t%s %s seq %u %d %s %u", __func__
,
588 install
? "Installing" : "Deleting", pbrm
->name
, pbrms
->seqno
,
589 install
, pmi
->ifp
->name
, pmi
->delete);
591 pbr_encode_pbr_map_sequence(s
, pbrms
, pmi
->ifp
);
593 stream_putw_at(s
, 0, stream_get_endp(s
));
595 zclient_send_message(zclient
);