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"
43 DEFINE_MTYPE_STATIC(PBRD
, PBR_INTERFACE
, "PBR Interface")
45 /* Zebra structure to hold current status. */
46 struct zclient
*zclient
;
48 struct pbr_interface
*pbr_if_new(struct interface
*ifp
)
50 struct pbr_interface
*pbr_ifp
;
55 pbr_ifp
= XCALLOC(MTYPE_PBR_INTERFACE
, sizeof(*pbr_ifp
));
61 /* Inteface addition message from zebra. */
62 static int interface_add(int command
, struct zclient
*zclient
,
63 zebra_size_t length
, vrf_id_t vrf_id
)
65 struct interface
*ifp
;
67 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
72 DEBUGD(&pbr_dbg_zebra
,
73 "%s: %s", __PRETTY_FUNCTION__
, ifp
->name
);
81 static int interface_delete(int command
, struct zclient
*zclient
,
82 zebra_size_t length
, vrf_id_t vrf_id
)
84 struct interface
*ifp
;
88 /* zebra_interface_state_read () updates interface structure in iflist
90 ifp
= zebra_interface_state_read(s
, vrf_id
);
95 DEBUGD(&pbr_dbg_zebra
,
96 "%s: %s", __PRETTY_FUNCTION__
, ifp
->name
);
98 if_set_index(ifp
, IFINDEX_INTERNAL
);
103 static int interface_address_add(int command
, struct zclient
*zclient
,
104 zebra_size_t length
, vrf_id_t vrf_id
)
107 char buf
[PREFIX_STRLEN
];
109 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
111 DEBUGD(&pbr_dbg_zebra
,
112 "%s: %s added %s", __PRETTY_FUNCTION__
, c
->ifp
->name
,
113 prefix2str(c
->address
, buf
, sizeof(buf
)));
118 static int interface_address_delete(int command
, struct zclient
*zclient
,
119 zebra_size_t length
, vrf_id_t vrf_id
)
122 char buf
[PREFIX_STRLEN
];
124 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
129 DEBUGD(&pbr_dbg_zebra
,
130 "%s: %s deleted %s", __PRETTY_FUNCTION__
, c
->ifp
->name
,
131 prefix2str(c
->address
, buf
, sizeof(buf
)));
137 static int interface_state_up(int command
, struct zclient
*zclient
,
138 zebra_size_t length
, vrf_id_t vrf_id
)
140 struct interface
*ifp
;
142 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
144 DEBUGD(&pbr_dbg_zebra
,
145 "%s: %s is up", __PRETTY_FUNCTION__
, ifp
->name
);
150 static int interface_state_down(int command
, struct zclient
*zclient
,
151 zebra_size_t length
, vrf_id_t vrf_id
)
153 struct interface
*ifp
;
155 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
157 DEBUGD(&pbr_dbg_zebra
,
158 "%s: %s is down", __PRETTY_FUNCTION__
, ifp
->name
);
163 static int route_notify_owner(int command
, struct zclient
*zclient
,
164 zebra_size_t length
, vrf_id_t vrf_id
)
167 enum zapi_route_notify_owner note
;
169 char buf
[PREFIX_STRLEN
];
171 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
))
174 prefix2str(&p
, buf
, sizeof(buf
));
177 case ZAPI_ROUTE_FAIL_INSTALL
:
178 DEBUGD(&pbr_dbg_zebra
,
179 "%s: [%s] Route install failure for table: %u",
180 __PRETTY_FUNCTION__
, buf
, table_id
);
182 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
183 DEBUGD(&pbr_dbg_zebra
,
184 "%s: [%s] Route better admin distance won for table: %u",
185 __PRETTY_FUNCTION__
, buf
, table_id
);
187 case ZAPI_ROUTE_INSTALLED
:
188 DEBUGD(&pbr_dbg_zebra
,
189 "%s: [%s] Route installed succeeded for table: %u",
190 __PRETTY_FUNCTION__
, buf
, table_id
);
191 pbr_nht_route_installed_for_table(table_id
);
193 case ZAPI_ROUTE_REMOVED
:
194 DEBUGD(&pbr_dbg_zebra
,
195 "%s: [%s] Route Removed succeeded for table: %u",
196 __PRETTY_FUNCTION__
, buf
, table_id
);
197 pbr_nht_route_removed_for_table(table_id
);
199 case ZAPI_ROUTE_REMOVE_FAIL
:
200 DEBUGD(&pbr_dbg_zebra
,
201 "%s: [%s] Route remove fail for table: %u",
202 __PRETTY_FUNCTION__
, buf
, table_id
);
209 static int rule_notify_owner(int command
, struct zclient
*zclient
,
210 zebra_size_t length
, vrf_id_t vrf_id
)
212 uint32_t seqno
, priority
, unique
;
213 enum zapi_rule_notify_owner note
;
214 struct pbr_map_sequence
*pbrms
;
215 struct pbr_map_interface
*pmi
;
219 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
224 pbrms
= pbrms_lookup_unique(unique
, ifi
, &pmi
);
226 DEBUGD(&pbr_dbg_zebra
,
227 "%s: Failure to lookup pbrms based upon %u",
228 __PRETTY_FUNCTION__
, unique
);
232 installed
= 1 << pmi
->install_bit
;
235 case ZAPI_RULE_FAIL_INSTALL
:
236 pbrms
->installed
&= ~installed
;
237 DEBUGD(&pbr_dbg_zebra
, "%s: Received RULE_FAIL_INSTALL: %lu",
238 __PRETTY_FUNCTION__
, pbrms
->installed
);
240 case ZAPI_RULE_INSTALLED
:
241 pbrms
->installed
|= installed
;
242 DEBUGD(&pbr_dbg_zebra
, "%s: Received RULE_INSTALLED: %lu",
243 __PRETTY_FUNCTION__
, pbrms
->installed
);
245 case ZAPI_RULE_FAIL_REMOVE
:
246 case ZAPI_RULE_REMOVED
:
247 pbrms
->installed
&= ~installed
;
248 DEBUGD(&pbr_dbg_zebra
, "%s: Received RULE REMOVED: %lu",
249 __PRETTY_FUNCTION__
, pbrms
->installed
);
253 pbr_map_final_interface_deletion(pbrms
->parent
, pmi
);
258 static void zebra_connected(struct zclient
*zclient
)
260 DEBUGD(&pbr_dbg_zebra
, "%s: Registering for fun and profit",
261 __PRETTY_FUNCTION__
);
262 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
265 static void route_add_helper(struct zapi_route
*api
, struct nexthop_group nhg
,
268 struct zapi_nexthop
*api_nh
;
269 char buf
[PREFIX_STRLEN
];
270 struct nexthop
*nhop
;
273 api
->prefix
.family
= install_afi
;
275 DEBUGD(&pbr_dbg_zebra
, "\tEncoding %s",
276 prefix2str(&api
->prefix
, buf
, sizeof(buf
)));
279 for (ALL_NEXTHOPS(nhg
, nhop
)) {
280 api_nh
= &api
->nexthops
[i
];
281 api_nh
->vrf_id
= nhop
->vrf_id
;
282 api_nh
->type
= nhop
->type
;
283 switch (nhop
->type
) {
284 case NEXTHOP_TYPE_IPV4
:
285 api_nh
->gate
.ipv4
= nhop
->gate
.ipv4
;
287 case NEXTHOP_TYPE_IPV4_IFINDEX
:
288 api_nh
->gate
.ipv4
= nhop
->gate
.ipv4
;
289 api_nh
->ifindex
= nhop
->ifindex
;
291 case NEXTHOP_TYPE_IFINDEX
:
292 api_nh
->ifindex
= nhop
->ifindex
;
294 case NEXTHOP_TYPE_IPV6
:
295 memcpy(&api_nh
->gate
.ipv6
, &nhop
->gate
.ipv6
, 16);
297 case NEXTHOP_TYPE_IPV6_IFINDEX
:
298 api_nh
->ifindex
= nhop
->ifindex
;
299 memcpy(&api_nh
->gate
.ipv6
, &nhop
->gate
.ipv6
, 16);
301 case NEXTHOP_TYPE_BLACKHOLE
:
302 api_nh
->bh_type
= nhop
->bh_type
;
307 api
->nexthop_num
= i
;
309 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, api
);
313 * This function assumes a default route is being
314 * installed into the appropriate tableid
316 void route_add(struct pbr_nexthop_group_cache
*pnhgc
, struct nexthop_group nhg
,
319 struct zapi_route api
;
321 DEBUGD(&pbr_dbg_zebra
, "%s for Table: %d", __PRETTY_FUNCTION__
,
324 memset(&api
, 0, sizeof(api
));
326 api
.vrf_id
= VRF_DEFAULT
;
327 api
.type
= ZEBRA_ROUTE_PBR
;
328 api
.safi
= SAFI_UNICAST
;
330 * Sending a default route
332 api
.tableid
= pnhgc
->table_id
;
333 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
334 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
335 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
336 switch (install_afi
) {
338 route_add_helper(&api
, nhg
, AF_INET
);
339 route_add_helper(&api
, nhg
, AF_INET6
);
342 route_add_helper(&api
, nhg
, AF_INET
);
345 route_add_helper(&api
, nhg
, AF_INET6
);
348 DEBUGD(&pbr_dbg_zebra
,
349 "%s: Asked to install unsupported route type: L2VPN",
350 __PRETTY_FUNCTION__
);
356 * This function assumes a default route is being
357 * removed from the appropriate tableid
359 void route_delete(struct pbr_nexthop_group_cache
*pnhgc
, afi_t afi
)
361 struct zapi_route api
;
363 DEBUGD(&pbr_dbg_zebra
, "%s for Table: %d", __PRETTY_FUNCTION__
,
366 memset(&api
, 0, sizeof(api
));
367 api
.vrf_id
= VRF_DEFAULT
;
368 api
.type
= ZEBRA_ROUTE_PBR
;
369 api
.safi
= SAFI_UNICAST
;
371 api
.tableid
= pnhgc
->table_id
;
372 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
376 api
.prefix
.family
= AF_INET
;
377 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
380 api
.prefix
.family
= AF_INET6
;
381 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
384 api
.prefix
.family
= AF_INET
;
385 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
386 api
.prefix
.family
= AF_INET6
;
387 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
390 DEBUGD(&pbr_dbg_zebra
,
391 "%s: Asked to delete unsupported route type: L2VPN",
392 __PRETTY_FUNCTION__
);
397 static int pbr_zebra_nexthop_update(int command
, struct zclient
*zclient
,
398 zebra_size_t length
, vrf_id_t vrf_id
)
400 struct zapi_route nhr
;
401 char buf
[PREFIX2STR_BUFFER
];
404 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
405 zlog_warn("Failure to decode Nexthop update message");
409 if (DEBUG_MODE_CHECK(&pbr_dbg_zebra
, DEBUG_MODE_ALL
)) {
411 DEBUGD(&pbr_dbg_zebra
, "%s: Received Nexthop update: %s",
413 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
)));
415 DEBUGD(&pbr_dbg_zebra
, "%s: (\tNexthops(%u)",
416 __PRETTY_FUNCTION__
, nhr
.nexthop_num
);
418 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
419 DEBUGD(&pbr_dbg_zebra
,
420 "%s: \tType: %d: vrf: %d, ifindex: %d gate: %s",
421 __PRETTY_FUNCTION__
, nhr
.nexthops
[i
].type
,
422 nhr
.nexthops
[i
].vrf_id
, nhr
.nexthops
[i
].ifindex
,
423 inet_ntoa(nhr
.nexthops
[i
].gate
.ipv4
));
427 pbr_nht_nexthop_update(&nhr
);
431 extern struct zebra_privs_t pbr_privs
;
433 void pbr_zebra_init(void)
435 struct zclient_options opt
= { .receive_notify
= true };
437 zclient
= zclient_new(master
, &opt
);
439 zclient_init(zclient
, ZEBRA_ROUTE_PBR
, 0, &pbr_privs
);
440 zclient
->zebra_connected
= zebra_connected
;
441 zclient
->interface_add
= interface_add
;
442 zclient
->interface_delete
= interface_delete
;
443 zclient
->interface_up
= interface_state_up
;
444 zclient
->interface_down
= interface_state_down
;
445 zclient
->interface_address_add
= interface_address_add
;
446 zclient
->interface_address_delete
= interface_address_delete
;
447 zclient
->route_notify_owner
= route_notify_owner
;
448 zclient
->rule_notify_owner
= rule_notify_owner
;
449 zclient
->nexthop_update
= pbr_zebra_nexthop_update
;
452 void pbr_send_rnh(struct nexthop
*nhop
, bool reg
)
458 ZEBRA_NEXTHOP_REGISTER
: ZEBRA_NEXTHOP_UNREGISTER
;
460 memset(&p
, 0, sizeof(p
));
461 switch (nhop
->type
) {
462 case NEXTHOP_TYPE_IFINDEX
:
463 case NEXTHOP_TYPE_BLACKHOLE
:
465 case NEXTHOP_TYPE_IPV4
:
466 case NEXTHOP_TYPE_IPV4_IFINDEX
:
468 p
.u
.prefix4
.s_addr
= nhop
->gate
.ipv4
.s_addr
;
471 case NEXTHOP_TYPE_IPV6
:
472 case NEXTHOP_TYPE_IPV6_IFINDEX
:
474 memcpy(&p
.u
.prefix6
, &nhop
->gate
.ipv6
, 16);
479 if (zclient_send_rnh(zclient
, command
, &p
,
480 false, nhop
->vrf_id
) < 0) {
481 zlog_warn("%s: Failure to send nexthop to zebra",
482 __PRETTY_FUNCTION__
);
486 static void pbr_encode_pbr_map_sequence_prefix(struct stream
*s
,
488 unsigned char family
)
493 memset(&any
, 0, sizeof(any
));
498 stream_putc(s
, p
->family
);
499 stream_putc(s
, p
->prefixlen
);
500 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
503 static void pbr_encode_pbr_map_sequence(struct stream
*s
,
504 struct pbr_map_sequence
*pbrms
,
505 struct interface
*ifp
)
507 unsigned char family
;
511 family
= pbrms
->family
;
513 stream_putl(s
, pbrms
->seqno
);
514 stream_putl(s
, pbrms
->ruleno
);
515 stream_putl(s
, pbrms
->unique
);
516 pbr_encode_pbr_map_sequence_prefix(s
, pbrms
->src
, family
);
517 stream_putw(s
, 0); /* src port */
518 pbr_encode_pbr_map_sequence_prefix(s
, pbrms
->dst
, family
);
519 stream_putw(s
, 0); /* dst port */
520 stream_putl(s
, 0); /* fwmark */
521 if (pbrms
->nhgrp_name
)
522 stream_putl(s
, pbr_nht_get_table(pbrms
->nhgrp_name
));
524 stream_putl(s
, pbr_nht_get_table(pbrms
->internal_nhg_name
));
525 stream_putl(s
, ifp
->ifindex
);
528 void pbr_send_pbr_map(struct pbr_map_sequence
*pbrms
,
529 struct pbr_map_interface
*pmi
, bool install
)
531 struct pbr_map
*pbrm
= pbrms
->parent
;
533 uint64_t is_installed
= (uint64_t)1 << pmi
->install_bit
;
535 is_installed
&= pbrms
->installed
;
537 DEBUGD(&pbr_dbg_zebra
, "%s: for %s %d(%" PRIu64
")",
538 __PRETTY_FUNCTION__
, pbrm
->name
, install
, is_installed
);
541 * If we are installed and asked to do so again
542 * just return. If we are not installed and asked
543 * and asked to delete just return;
545 if (install
&& is_installed
)
548 if (!install
&& !is_installed
)
554 zclient_create_header(s
,
555 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
559 * We are sending one item at a time at the moment
563 DEBUGD(&pbr_dbg_zebra
, "%s: \t%s %s %d %s %u",
564 __PRETTY_FUNCTION__
, install
? "Installing" : "Deleting",
565 pbrm
->name
, install
, pmi
->ifp
->name
, pmi
->delete);
567 pbr_encode_pbr_map_sequence(s
, pbrms
, pmi
->ifp
);
569 stream_putw_at(s
, 0, stream_get_endp(s
));
571 zclient_send_message(zclient
);