]> git.proxmox.com Git - mirror_frr.git/blame - pbrd/pbr_zebra.c
*: Add infrastructure to support zapi interface callbacks
[mirror_frr.git] / pbrd / pbr_zebra.c
CommitLineData
e5c83d9b
DS
1/*
2 * Zebra connect code.
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
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
9 * later version.
10 *
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.
15 *
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
19 */
20#include <zebra.h>
21
22#include "thread.h"
23#include "command.h"
24#include "network.h"
25#include "prefix.h"
26#include "routemap.h"
27#include "table.h"
28#include "stream.h"
29#include "memory.h"
30#include "zclient.h"
31#include "filter.h"
32#include "plist.h"
33#include "log.h"
34#include "nexthop.h"
35#include "nexthop_group.h"
36
37#include "pbr_nht.h"
38#include "pbr_map.h"
39#include "pbr_memory.h"
40#include "pbr_zebra.h"
41#include "pbr_debug.h"
42
43DEFINE_MTYPE_STATIC(PBRD, PBR_INTERFACE, "PBR Interface")
44
45/* Zebra structure to hold current status. */
d3765386 46struct zclient *zclient;
e5c83d9b 47
b13e5ad6 48struct pbr_interface *pbr_if_new(struct interface *ifp)
e5c83d9b
DS
49{
50 struct pbr_interface *pbr_ifp;
51
52 zassert(ifp);
53 zassert(!ifp->info);
54
55 pbr_ifp = XCALLOC(MTYPE_PBR_INTERFACE, sizeof(*pbr_ifp));
56
10a00758
DS
57 ifp->info = pbr_ifp;
58 return pbr_ifp;
e5c83d9b
DS
59}
60
61/* Inteface addition message from zebra. */
121f9dee 62static int interface_add(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
63{
64 struct interface *ifp;
65
66 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
67
68 if (!ifp)
69 return 0;
70
2f61710b
DS
71 DEBUGD(&pbr_dbg_zebra,
72 "%s: %s", __PRETTY_FUNCTION__, ifp->name);
73
10a00758
DS
74 if (!ifp->info)
75 pbr_if_new(ifp);
e5c83d9b 76
a106a408
RW
77 pbr_nht_nexthop_interface_update(ifp);
78
e5c83d9b
DS
79 return 0;
80}
81
121f9dee 82static int interface_delete(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
83{
84 struct interface *ifp;
85 struct stream *s;
86
87 s = zclient->ibuf;
88 /* zebra_interface_state_read () updates interface structure in iflist
89 */
90 ifp = zebra_interface_state_read(s, vrf_id);
91
92 if (ifp == NULL)
93 return 0;
94
2f61710b
DS
95 DEBUGD(&pbr_dbg_zebra,
96 "%s: %s", __PRETTY_FUNCTION__, ifp->name);
97
e5c83d9b
DS
98 if_set_index(ifp, IFINDEX_INTERNAL);
99
100 return 0;
101}
102
121f9dee 103static int interface_address_add(ZAPI_CALLBACK_ARGS)
e5c83d9b 104{
2f61710b
DS
105 struct connected *c;
106 char buf[PREFIX_STRLEN];
107
121f9dee 108 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
2f61710b
DS
109
110 DEBUGD(&pbr_dbg_zebra,
964c3dba
DS
111 "%s: %s added %s", __PRETTY_FUNCTION__,
112 c ? c->ifp->name : "Unknown",
113 c ? prefix2str(c->address, buf, sizeof(buf)) : "Unknown");
e5c83d9b
DS
114
115 return 0;
116}
117
121f9dee 118static int interface_address_delete(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
119{
120 struct connected *c;
2f61710b 121 char buf[PREFIX_STRLEN];
e5c83d9b 122
121f9dee 123 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
e5c83d9b
DS
124
125 if (!c)
126 return 0;
127
2f61710b
DS
128 DEBUGD(&pbr_dbg_zebra,
129 "%s: %s deleted %s", __PRETTY_FUNCTION__, c->ifp->name,
130 prefix2str(c->address, buf, sizeof(buf)));
131
e5c83d9b
DS
132 connected_free(c);
133 return 0;
134}
135
121f9dee 136static int interface_state_up(ZAPI_CALLBACK_ARGS)
e5c83d9b 137{
2f61710b
DS
138 struct interface *ifp;
139
140 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
e5c83d9b 141
2f61710b
DS
142 DEBUGD(&pbr_dbg_zebra,
143 "%s: %s is up", __PRETTY_FUNCTION__, ifp->name);
e5c83d9b 144
a106a408
RW
145 pbr_nht_nexthop_interface_update(ifp);
146
e5c83d9b
DS
147 return 0;
148}
149
121f9dee 150static int interface_state_down(ZAPI_CALLBACK_ARGS)
e5c83d9b 151{
2f61710b 152 struct interface *ifp;
e5c83d9b 153
2f61710b
DS
154 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
155
156 DEBUGD(&pbr_dbg_zebra,
157 "%s: %s is down", __PRETTY_FUNCTION__, ifp->name);
e5c83d9b 158
a106a408
RW
159 pbr_nht_nexthop_interface_update(ifp);
160
e5c83d9b
DS
161 return 0;
162}
163
121f9dee 164static int route_notify_owner(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
165{
166 struct prefix p;
167 enum zapi_route_notify_owner note;
168 uint32_t table_id;
169 char buf[PREFIX_STRLEN];
170
e5c83d9b
DS
171 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note))
172 return -1;
173
2f61710b
DS
174 prefix2str(&p, buf, sizeof(buf));
175
e5c83d9b
DS
176 switch (note) {
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);
181 break;
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);
186 break;
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);
192 break;
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);
198 break;
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);
203 break;
204 }
205
206 return 0;
207}
208
121f9dee 209static int rule_notify_owner(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
210{
211 uint32_t seqno, priority, unique;
212 enum zapi_rule_notify_owner note;
213 struct pbr_map_sequence *pbrms;
37c606ff 214 struct pbr_map_interface *pmi;
e5c83d9b 215 ifindex_t ifi;
37c606ff 216 uint64_t installed;
e5c83d9b
DS
217
218 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
219 &ifi, &note))
220 return -1;
221
37c606ff
DS
222 pmi = NULL;
223 pbrms = pbrms_lookup_unique(unique, ifi, &pmi);
e5c83d9b
DS
224 if (!pbrms) {
225 DEBUGD(&pbr_dbg_zebra,
226 "%s: Failure to lookup pbrms based upon %u",
227 __PRETTY_FUNCTION__, unique);
228 return 0;
229 }
230
37c606ff
DS
231 installed = 1 << pmi->install_bit;
232
e5c83d9b
DS
233 switch (note) {
234 case ZAPI_RULE_FAIL_INSTALL:
37c606ff 235 pbrms->installed &= ~installed;
fa0069c6
DS
236 DEBUGD(&pbr_dbg_zebra,
237 "%s: Received RULE_FAIL_INSTALL: %" PRIu64,
2f61710b 238 __PRETTY_FUNCTION__, pbrms->installed);
e5c83d9b
DS
239 break;
240 case ZAPI_RULE_INSTALLED:
37c606ff 241 pbrms->installed |= installed;
fa0069c6 242 DEBUGD(&pbr_dbg_zebra, "%s: Received RULE_INSTALLED: %" PRIu64,
2f61710b 243 __PRETTY_FUNCTION__, pbrms->installed);
e5c83d9b 244 break;
373dd3b5 245 case ZAPI_RULE_FAIL_REMOVE:
e5c83d9b 246 case ZAPI_RULE_REMOVED:
0f03639d 247 pbrms->installed &= ~installed;
fa0069c6 248 DEBUGD(&pbr_dbg_zebra, "%s: Received RULE REMOVED: %" PRIu64,
2f61710b 249 __PRETTY_FUNCTION__, pbrms->installed);
e5c83d9b
DS
250 break;
251 }
252
38e9ccde
DS
253 pbr_map_final_interface_deletion(pbrms->parent, pmi);
254
e5c83d9b
DS
255 return 0;
256}
257
258static void zebra_connected(struct zclient *zclient)
259{
2f61710b
DS
260 DEBUGD(&pbr_dbg_zebra, "%s: Registering for fun and profit",
261 __PRETTY_FUNCTION__);
e5c83d9b
DS
262 zclient_send_reg_requests(zclient, VRF_DEFAULT);
263}
264
265static void route_add_helper(struct zapi_route *api, struct nexthop_group nhg,
266 uint8_t install_afi)
267{
268 struct zapi_nexthop *api_nh;
2f61710b 269 char buf[PREFIX_STRLEN];
e5c83d9b
DS
270 struct nexthop *nhop;
271 int i;
272
273 api->prefix.family = install_afi;
274
2f61710b
DS
275 DEBUGD(&pbr_dbg_zebra, "\tEncoding %s",
276 prefix2str(&api->prefix, buf, sizeof(buf)));
277
e5c83d9b
DS
278 i = 0;
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;
286 break;
287 case NEXTHOP_TYPE_IPV4_IFINDEX:
288 api_nh->gate.ipv4 = nhop->gate.ipv4;
289 api_nh->ifindex = nhop->ifindex;
290 break;
291 case NEXTHOP_TYPE_IFINDEX:
292 api_nh->ifindex = nhop->ifindex;
293 break;
294 case NEXTHOP_TYPE_IPV6:
295 memcpy(&api_nh->gate.ipv6, &nhop->gate.ipv6, 16);
296 break;
297 case NEXTHOP_TYPE_IPV6_IFINDEX:
298 api_nh->ifindex = nhop->ifindex;
299 memcpy(&api_nh->gate.ipv6, &nhop->gate.ipv6, 16);
300 break;
301 case NEXTHOP_TYPE_BLACKHOLE:
302 api_nh->bh_type = nhop->bh_type;
303 break;
304 }
305 i++;
306 }
307 api->nexthop_num = i;
308
309 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, api);
310}
311
312/*
313 * This function assumes a default route is being
314 * installed into the appropriate tableid
315 */
316void route_add(struct pbr_nexthop_group_cache *pnhgc, struct nexthop_group nhg,
317 afi_t install_afi)
318{
319 struct zapi_route api;
320
2f61710b
DS
321 DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __PRETTY_FUNCTION__,
322 pnhgc->table_id);
323
e5c83d9b
DS
324 memset(&api, 0, sizeof(api));
325
326 api.vrf_id = VRF_DEFAULT;
327 api.type = ZEBRA_ROUTE_PBR;
328 api.safi = SAFI_UNICAST;
329 /*
330 * Sending a default route
331 */
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) {
337 case AFI_MAX:
338 route_add_helper(&api, nhg, AF_INET);
339 route_add_helper(&api, nhg, AF_INET6);
340 break;
341 case AFI_IP:
342 route_add_helper(&api, nhg, AF_INET);
343 break;
344 case AFI_IP6:
345 route_add_helper(&api, nhg, AF_INET6);
346 break;
347 case AFI_L2VPN:
348 DEBUGD(&pbr_dbg_zebra,
349 "%s: Asked to install unsupported route type: L2VPN",
350 __PRETTY_FUNCTION__);
351 break;
b26f891d
SW
352 case AFI_UNSPEC:
353 DEBUGD(&pbr_dbg_zebra,
354 "%s: Asked to install unspecified route type",
355 __PRETTY_FUNCTION__);
356 break;
e5c83d9b
DS
357 }
358}
359
360/*
361 * This function assumes a default route is being
362 * removed from the appropriate tableid
363 */
364void route_delete(struct pbr_nexthop_group_cache *pnhgc, afi_t afi)
365{
366 struct zapi_route api;
367
2f61710b
DS
368 DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __PRETTY_FUNCTION__,
369 pnhgc->table_id);
370
e5c83d9b
DS
371 memset(&api, 0, sizeof(api));
372 api.vrf_id = VRF_DEFAULT;
373 api.type = ZEBRA_ROUTE_PBR;
374 api.safi = SAFI_UNICAST;
375
376 api.tableid = pnhgc->table_id;
377 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
378
379 switch (afi) {
380 case AFI_IP:
381 api.prefix.family = AF_INET;
382 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
383 break;
384 case AFI_IP6:
385 api.prefix.family = AF_INET6;
386 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
387 break;
388 case AFI_MAX:
389 api.prefix.family = AF_INET;
390 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
391 api.prefix.family = AF_INET6;
392 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
393 break;
394 case AFI_L2VPN:
395 DEBUGD(&pbr_dbg_zebra,
396 "%s: Asked to delete unsupported route type: L2VPN",
397 __PRETTY_FUNCTION__);
398 break;
b26f891d
SW
399 case AFI_UNSPEC:
400 DEBUGD(&pbr_dbg_zebra,
401 "%s: Asked to delete unspecified route type",
402 __PRETTY_FUNCTION__);
403 break;
e5c83d9b 404 }
e5c83d9b
DS
405}
406
121f9dee 407static int pbr_zebra_nexthop_update(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
408{
409 struct zapi_route nhr;
410 char buf[PREFIX2STR_BUFFER];
411 uint32_t i;
412
54317f2c
A
413 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
414 zlog_warn("Failure to decode Nexthop update message");
415 return 0;
416 }
e5c83d9b
DS
417
418 if (DEBUG_MODE_CHECK(&pbr_dbg_zebra, DEBUG_MODE_ALL)) {
419
420 DEBUGD(&pbr_dbg_zebra, "%s: Received Nexthop update: %s",
421 __PRETTY_FUNCTION__,
422 prefix2str(&nhr.prefix, buf, sizeof(buf)));
423
424 DEBUGD(&pbr_dbg_zebra, "%s: (\tNexthops(%u)",
425 __PRETTY_FUNCTION__, nhr.nexthop_num);
426
427 for (i = 0; i < nhr.nexthop_num; i++) {
428 DEBUGD(&pbr_dbg_zebra,
429 "%s: \tType: %d: vrf: %d, ifindex: %d gate: %s",
430 __PRETTY_FUNCTION__, nhr.nexthops[i].type,
431 nhr.nexthops[i].vrf_id, nhr.nexthops[i].ifindex,
432 inet_ntoa(nhr.nexthops[i].gate.ipv4));
433 }
434 }
435
436 pbr_nht_nexthop_update(&nhr);
437 return 1;
438}
439
440extern struct zebra_privs_t pbr_privs;
441
442void pbr_zebra_init(void)
443{
444 struct zclient_options opt = { .receive_notify = true };
445
26f63a1e 446 zclient = zclient_new(master, &opt);
e5c83d9b
DS
447
448 zclient_init(zclient, ZEBRA_ROUTE_PBR, 0, &pbr_privs);
449 zclient->zebra_connected = zebra_connected;
450 zclient->interface_add = interface_add;
451 zclient->interface_delete = interface_delete;
452 zclient->interface_up = interface_state_up;
453 zclient->interface_down = interface_state_down;
454 zclient->interface_address_add = interface_address_add;
455 zclient->interface_address_delete = interface_address_delete;
456 zclient->route_notify_owner = route_notify_owner;
457 zclient->rule_notify_owner = rule_notify_owner;
458 zclient->nexthop_update = pbr_zebra_nexthop_update;
459}
460
461void pbr_send_rnh(struct nexthop *nhop, bool reg)
462{
463 uint32_t command;
464 struct prefix p;
465
466 command = (reg) ?
467 ZEBRA_NEXTHOP_REGISTER : ZEBRA_NEXTHOP_UNREGISTER;
468
469 memset(&p, 0, sizeof(p));
d3765386 470 switch (nhop->type) {
e5c83d9b
DS
471 case NEXTHOP_TYPE_IFINDEX:
472 case NEXTHOP_TYPE_BLACKHOLE:
473 return;
474 case NEXTHOP_TYPE_IPV4:
475 case NEXTHOP_TYPE_IPV4_IFINDEX:
476 p.family = AF_INET;
477 p.u.prefix4.s_addr = nhop->gate.ipv4.s_addr;
478 p.prefixlen = 32;
479 break;
480 case NEXTHOP_TYPE_IPV6:
481 case NEXTHOP_TYPE_IPV6_IFINDEX:
482 p.family = AF_INET6;
483 memcpy(&p.u.prefix6, &nhop->gate.ipv6, 16);
484 p.prefixlen = 128;
485 break;
486 }
487
488 if (zclient_send_rnh(zclient, command, &p,
489 false, nhop->vrf_id) < 0) {
490 zlog_warn("%s: Failure to send nexthop to zebra",
491 __PRETTY_FUNCTION__);
492 }
493}
494
495static void pbr_encode_pbr_map_sequence_prefix(struct stream *s,
496 struct prefix *p,
49027ce8 497 unsigned char family)
e5c83d9b
DS
498{
499 struct prefix any;
500
501 if (!p) {
502 memset(&any, 0, sizeof(any));
503 any.family = family;
504 p = &any;
505 }
506
507 stream_putc(s, p->family);
508 stream_putc(s, p->prefixlen);
509 stream_put(s, &p->u.prefix, prefix_blen(p));
510}
511
512static void pbr_encode_pbr_map_sequence(struct stream *s,
513 struct pbr_map_sequence *pbrms,
514 struct interface *ifp)
515{
49027ce8 516 unsigned char family;
e5c83d9b
DS
517
518 family = AF_INET;
49027ce8
DS
519 if (pbrms->family)
520 family = pbrms->family;
e5c83d9b
DS
521
522 stream_putl(s, pbrms->seqno);
523 stream_putl(s, pbrms->ruleno);
524 stream_putl(s, pbrms->unique);
525 pbr_encode_pbr_map_sequence_prefix(s, pbrms->src, family);
526 stream_putw(s, 0); /* src port */
527 pbr_encode_pbr_map_sequence_prefix(s, pbrms->dst, family);
528 stream_putw(s, 0); /* dst port */
95a9fe02 529 stream_putl(s, pbrms->mark);
e5c83d9b
DS
530 if (pbrms->nhgrp_name)
531 stream_putl(s, pbr_nht_get_table(pbrms->nhgrp_name));
532 else if (pbrms->nhg)
533 stream_putl(s, pbr_nht_get_table(pbrms->internal_nhg_name));
534 stream_putl(s, ifp->ifindex);
535}
536
b13e5ad6
DS
537void pbr_send_pbr_map(struct pbr_map_sequence *pbrms,
538 struct pbr_map_interface *pmi, bool install)
e5c83d9b 539{
b13e5ad6 540 struct pbr_map *pbrm = pbrms->parent;
e5c83d9b 541 struct stream *s;
10a00758 542 uint64_t is_installed = (uint64_t)1 << pmi->install_bit;
37c606ff
DS
543
544 is_installed &= pbrms->installed;
e5c83d9b 545
37c606ff
DS
546 DEBUGD(&pbr_dbg_zebra, "%s: for %s %d(%" PRIu64 ")",
547 __PRETTY_FUNCTION__, pbrm->name, install, is_installed);
9b71ea4b
DS
548
549 /*
550 * If we are installed and asked to do so again
551 * just return. If we are not installed and asked
552 * and asked to delete just return;
553 */
37c606ff 554 if (install && is_installed)
9b71ea4b
DS
555 return;
556
37c606ff 557 if (!install && !is_installed)
9b71ea4b 558 return;
e5c83d9b
DS
559
560 s = zclient->obuf;
561 stream_reset(s);
562
563 zclient_create_header(s,
564 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
565 VRF_DEFAULT);
566
b13e5ad6
DS
567 /*
568 * We are sending one item at a time at the moment
569 */
570 stream_putl(s, 1);
e5c83d9b 571
b13e5ad6
DS
572 DEBUGD(&pbr_dbg_zebra, "%s: \t%s %s %d %s %u",
573 __PRETTY_FUNCTION__, install ? "Installing" : "Deleting",
574 pbrm->name, install, pmi->ifp->name, pmi->delete);
e5c83d9b 575
b13e5ad6 576 pbr_encode_pbr_map_sequence(s, pbrms, pmi->ifp);
e5c83d9b 577
e5c83d9b
DS
578 stream_putw_at(s, 0, stream_get_endp(s));
579
e5c83d9b
DS
580 zclient_send_message(zclient);
581}
138c5a74
DS
582
583int pbr_ifp_create(struct interface *ifp)
584{
585 return 0;
586}
587
588int pbr_ifp_up(struct interface *ifp)
589{
590 return 0;
591}
592
593int pbr_ifp_down(struct interface *ifp)
594{
595 return 0;
596}
597
598int pbr_ifp_destroy(struct interface *ifp)
599{
600 return 0;
601}