]> git.proxmox.com Git - mirror_frr.git/blame - pbrd/pbr_zebra.c
Merge pull request #4850 from lkrishnamoor/show_cli
[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. */
ef7bd2a3 62int pbr_ifp_create(struct interface *ifp)
e5c83d9b 63{
2f61710b
DS
64 DEBUGD(&pbr_dbg_zebra,
65 "%s: %s", __PRETTY_FUNCTION__, ifp->name);
66
10a00758
DS
67 if (!ifp->info)
68 pbr_if_new(ifp);
e5c83d9b 69
a106a408
RW
70 pbr_nht_nexthop_interface_update(ifp);
71
e5c83d9b
DS
72 return 0;
73}
74
3c3c3252 75int pbr_ifp_destroy(struct interface *ifp)
e5c83d9b 76{
2f61710b
DS
77 DEBUGD(&pbr_dbg_zebra,
78 "%s: %s", __PRETTY_FUNCTION__, ifp->name);
79
e5c83d9b
DS
80 return 0;
81}
82
121f9dee 83static int interface_address_add(ZAPI_CALLBACK_ARGS)
e5c83d9b 84{
2f61710b
DS
85 struct connected *c;
86 char buf[PREFIX_STRLEN];
87
121f9dee 88 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
2f61710b
DS
89
90 DEBUGD(&pbr_dbg_zebra,
964c3dba
DS
91 "%s: %s added %s", __PRETTY_FUNCTION__,
92 c ? c->ifp->name : "Unknown",
93 c ? prefix2str(c->address, buf, sizeof(buf)) : "Unknown");
e5c83d9b
DS
94
95 return 0;
96}
97
121f9dee 98static int interface_address_delete(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
99{
100 struct connected *c;
2f61710b 101 char buf[PREFIX_STRLEN];
e5c83d9b 102
121f9dee 103 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
e5c83d9b
DS
104
105 if (!c)
106 return 0;
107
2f61710b
DS
108 DEBUGD(&pbr_dbg_zebra,
109 "%s: %s deleted %s", __PRETTY_FUNCTION__, c->ifp->name,
110 prefix2str(c->address, buf, sizeof(buf)));
111
e5c83d9b
DS
112 connected_free(c);
113 return 0;
114}
115
ddbf3e60 116int pbr_ifp_up(struct interface *ifp)
e5c83d9b 117{
2f61710b
DS
118 DEBUGD(&pbr_dbg_zebra,
119 "%s: %s is up", __PRETTY_FUNCTION__, ifp->name);
e5c83d9b 120
a106a408
RW
121 pbr_nht_nexthop_interface_update(ifp);
122
e5c83d9b
DS
123 return 0;
124}
125
b0b69e59 126int pbr_ifp_down(struct interface *ifp)
e5c83d9b 127{
2f61710b
DS
128 DEBUGD(&pbr_dbg_zebra,
129 "%s: %s is down", __PRETTY_FUNCTION__, ifp->name);
e5c83d9b 130
a106a408
RW
131 pbr_nht_nexthop_interface_update(ifp);
132
e5c83d9b
DS
133 return 0;
134}
135
121f9dee 136static int route_notify_owner(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
137{
138 struct prefix p;
139 enum zapi_route_notify_owner note;
140 uint32_t table_id;
141 char buf[PREFIX_STRLEN];
142
e5c83d9b
DS
143 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note))
144 return -1;
145
2f61710b
DS
146 prefix2str(&p, buf, sizeof(buf));
147
e5c83d9b
DS
148 switch (note) {
149 case ZAPI_ROUTE_FAIL_INSTALL:
150 DEBUGD(&pbr_dbg_zebra,
151 "%s: [%s] Route install failure for table: %u",
152 __PRETTY_FUNCTION__, buf, table_id);
153 break;
154 case ZAPI_ROUTE_BETTER_ADMIN_WON:
155 DEBUGD(&pbr_dbg_zebra,
156 "%s: [%s] Route better admin distance won for table: %u",
157 __PRETTY_FUNCTION__, buf, table_id);
158 break;
159 case ZAPI_ROUTE_INSTALLED:
160 DEBUGD(&pbr_dbg_zebra,
161 "%s: [%s] Route installed succeeded for table: %u",
162 __PRETTY_FUNCTION__, buf, table_id);
163 pbr_nht_route_installed_for_table(table_id);
164 break;
165 case ZAPI_ROUTE_REMOVED:
166 DEBUGD(&pbr_dbg_zebra,
167 "%s: [%s] Route Removed succeeded for table: %u",
168 __PRETTY_FUNCTION__, buf, table_id);
169 pbr_nht_route_removed_for_table(table_id);
170 break;
171 case ZAPI_ROUTE_REMOVE_FAIL:
172 DEBUGD(&pbr_dbg_zebra,
173 "%s: [%s] Route remove fail for table: %u",
174 __PRETTY_FUNCTION__, buf, table_id);
175 break;
176 }
177
178 return 0;
179}
180
121f9dee 181static int rule_notify_owner(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
182{
183 uint32_t seqno, priority, unique;
184 enum zapi_rule_notify_owner note;
185 struct pbr_map_sequence *pbrms;
37c606ff 186 struct pbr_map_interface *pmi;
e5c83d9b 187 ifindex_t ifi;
37c606ff 188 uint64_t installed;
e5c83d9b
DS
189
190 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
191 &ifi, &note))
192 return -1;
193
37c606ff
DS
194 pmi = NULL;
195 pbrms = pbrms_lookup_unique(unique, ifi, &pmi);
e5c83d9b
DS
196 if (!pbrms) {
197 DEBUGD(&pbr_dbg_zebra,
198 "%s: Failure to lookup pbrms based upon %u",
199 __PRETTY_FUNCTION__, unique);
200 return 0;
201 }
202
37c606ff
DS
203 installed = 1 << pmi->install_bit;
204
e5c83d9b
DS
205 switch (note) {
206 case ZAPI_RULE_FAIL_INSTALL:
37c606ff 207 pbrms->installed &= ~installed;
fa0069c6
DS
208 DEBUGD(&pbr_dbg_zebra,
209 "%s: Received RULE_FAIL_INSTALL: %" PRIu64,
2f61710b 210 __PRETTY_FUNCTION__, pbrms->installed);
e5c83d9b
DS
211 break;
212 case ZAPI_RULE_INSTALLED:
37c606ff 213 pbrms->installed |= installed;
fa0069c6 214 DEBUGD(&pbr_dbg_zebra, "%s: Received RULE_INSTALLED: %" PRIu64,
2f61710b 215 __PRETTY_FUNCTION__, pbrms->installed);
e5c83d9b 216 break;
373dd3b5 217 case ZAPI_RULE_FAIL_REMOVE:
e5c83d9b 218 case ZAPI_RULE_REMOVED:
0f03639d 219 pbrms->installed &= ~installed;
fa0069c6 220 DEBUGD(&pbr_dbg_zebra, "%s: Received RULE REMOVED: %" PRIu64,
2f61710b 221 __PRETTY_FUNCTION__, pbrms->installed);
e5c83d9b
DS
222 break;
223 }
224
38e9ccde
DS
225 pbr_map_final_interface_deletion(pbrms->parent, pmi);
226
e5c83d9b
DS
227 return 0;
228}
229
230static void zebra_connected(struct zclient *zclient)
231{
2f61710b
DS
232 DEBUGD(&pbr_dbg_zebra, "%s: Registering for fun and profit",
233 __PRETTY_FUNCTION__);
e5c83d9b
DS
234 zclient_send_reg_requests(zclient, VRF_DEFAULT);
235}
236
237static void route_add_helper(struct zapi_route *api, struct nexthop_group nhg,
238 uint8_t install_afi)
239{
240 struct zapi_nexthop *api_nh;
2f61710b 241 char buf[PREFIX_STRLEN];
e5c83d9b
DS
242 struct nexthop *nhop;
243 int i;
244
245 api->prefix.family = install_afi;
246
2f61710b
DS
247 DEBUGD(&pbr_dbg_zebra, "\tEncoding %s",
248 prefix2str(&api->prefix, buf, sizeof(buf)));
249
e5c83d9b
DS
250 i = 0;
251 for (ALL_NEXTHOPS(nhg, nhop)) {
252 api_nh = &api->nexthops[i];
253 api_nh->vrf_id = nhop->vrf_id;
254 api_nh->type = nhop->type;
255 switch (nhop->type) {
256 case NEXTHOP_TYPE_IPV4:
257 api_nh->gate.ipv4 = nhop->gate.ipv4;
258 break;
259 case NEXTHOP_TYPE_IPV4_IFINDEX:
260 api_nh->gate.ipv4 = nhop->gate.ipv4;
261 api_nh->ifindex = nhop->ifindex;
262 break;
263 case NEXTHOP_TYPE_IFINDEX:
264 api_nh->ifindex = nhop->ifindex;
265 break;
266 case NEXTHOP_TYPE_IPV6:
267 memcpy(&api_nh->gate.ipv6, &nhop->gate.ipv6, 16);
268 break;
269 case NEXTHOP_TYPE_IPV6_IFINDEX:
270 api_nh->ifindex = nhop->ifindex;
271 memcpy(&api_nh->gate.ipv6, &nhop->gate.ipv6, 16);
272 break;
273 case NEXTHOP_TYPE_BLACKHOLE:
274 api_nh->bh_type = nhop->bh_type;
275 break;
276 }
277 i++;
278 }
279 api->nexthop_num = i;
280
281 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, api);
282}
283
284/*
285 * This function assumes a default route is being
286 * installed into the appropriate tableid
287 */
288void route_add(struct pbr_nexthop_group_cache *pnhgc, struct nexthop_group nhg,
289 afi_t install_afi)
290{
291 struct zapi_route api;
292
2f61710b
DS
293 DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __PRETTY_FUNCTION__,
294 pnhgc->table_id);
295
e5c83d9b
DS
296 memset(&api, 0, sizeof(api));
297
298 api.vrf_id = VRF_DEFAULT;
299 api.type = ZEBRA_ROUTE_PBR;
300 api.safi = SAFI_UNICAST;
301 /*
302 * Sending a default route
303 */
304 api.tableid = pnhgc->table_id;
305 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
306 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
307 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
308 switch (install_afi) {
309 case AFI_MAX:
310 route_add_helper(&api, nhg, AF_INET);
311 route_add_helper(&api, nhg, AF_INET6);
312 break;
313 case AFI_IP:
314 route_add_helper(&api, nhg, AF_INET);
315 break;
316 case AFI_IP6:
317 route_add_helper(&api, nhg, AF_INET6);
318 break;
319 case AFI_L2VPN:
320 DEBUGD(&pbr_dbg_zebra,
321 "%s: Asked to install unsupported route type: L2VPN",
322 __PRETTY_FUNCTION__);
323 break;
b26f891d
SW
324 case AFI_UNSPEC:
325 DEBUGD(&pbr_dbg_zebra,
326 "%s: Asked to install unspecified route type",
327 __PRETTY_FUNCTION__);
328 break;
e5c83d9b
DS
329 }
330}
331
332/*
333 * This function assumes a default route is being
334 * removed from the appropriate tableid
335 */
336void route_delete(struct pbr_nexthop_group_cache *pnhgc, afi_t afi)
337{
338 struct zapi_route api;
339
2f61710b
DS
340 DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __PRETTY_FUNCTION__,
341 pnhgc->table_id);
342
e5c83d9b
DS
343 memset(&api, 0, sizeof(api));
344 api.vrf_id = VRF_DEFAULT;
345 api.type = ZEBRA_ROUTE_PBR;
346 api.safi = SAFI_UNICAST;
347
348 api.tableid = pnhgc->table_id;
349 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
350
351 switch (afi) {
352 case AFI_IP:
353 api.prefix.family = AF_INET;
354 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
355 break;
356 case AFI_IP6:
357 api.prefix.family = AF_INET6;
358 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
359 break;
360 case AFI_MAX:
361 api.prefix.family = AF_INET;
362 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
363 api.prefix.family = AF_INET6;
364 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
365 break;
366 case AFI_L2VPN:
367 DEBUGD(&pbr_dbg_zebra,
368 "%s: Asked to delete unsupported route type: L2VPN",
369 __PRETTY_FUNCTION__);
370 break;
b26f891d
SW
371 case AFI_UNSPEC:
372 DEBUGD(&pbr_dbg_zebra,
373 "%s: Asked to delete unspecified route type",
374 __PRETTY_FUNCTION__);
375 break;
e5c83d9b 376 }
e5c83d9b
DS
377}
378
121f9dee 379static int pbr_zebra_nexthop_update(ZAPI_CALLBACK_ARGS)
e5c83d9b
DS
380{
381 struct zapi_route nhr;
382 char buf[PREFIX2STR_BUFFER];
383 uint32_t i;
384
54317f2c
A
385 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
386 zlog_warn("Failure to decode Nexthop update message");
387 return 0;
388 }
e5c83d9b
DS
389
390 if (DEBUG_MODE_CHECK(&pbr_dbg_zebra, DEBUG_MODE_ALL)) {
391
392 DEBUGD(&pbr_dbg_zebra, "%s: Received Nexthop update: %s",
393 __PRETTY_FUNCTION__,
394 prefix2str(&nhr.prefix, buf, sizeof(buf)));
395
396 DEBUGD(&pbr_dbg_zebra, "%s: (\tNexthops(%u)",
397 __PRETTY_FUNCTION__, nhr.nexthop_num);
398
399 for (i = 0; i < nhr.nexthop_num; i++) {
400 DEBUGD(&pbr_dbg_zebra,
401 "%s: \tType: %d: vrf: %d, ifindex: %d gate: %s",
402 __PRETTY_FUNCTION__, nhr.nexthops[i].type,
403 nhr.nexthops[i].vrf_id, nhr.nexthops[i].ifindex,
404 inet_ntoa(nhr.nexthops[i].gate.ipv4));
405 }
406 }
407
408 pbr_nht_nexthop_update(&nhr);
409 return 1;
410}
411
412extern struct zebra_privs_t pbr_privs;
413
414void pbr_zebra_init(void)
415{
416 struct zclient_options opt = { .receive_notify = true };
417
26f63a1e 418 zclient = zclient_new(master, &opt);
e5c83d9b
DS
419
420 zclient_init(zclient, ZEBRA_ROUTE_PBR, 0, &pbr_privs);
421 zclient->zebra_connected = zebra_connected;
e5c83d9b
DS
422 zclient->interface_address_add = interface_address_add;
423 zclient->interface_address_delete = interface_address_delete;
424 zclient->route_notify_owner = route_notify_owner;
425 zclient->rule_notify_owner = rule_notify_owner;
426 zclient->nexthop_update = pbr_zebra_nexthop_update;
427}
428
429void pbr_send_rnh(struct nexthop *nhop, bool reg)
430{
431 uint32_t command;
432 struct prefix p;
433
434 command = (reg) ?
435 ZEBRA_NEXTHOP_REGISTER : ZEBRA_NEXTHOP_UNREGISTER;
436
437 memset(&p, 0, sizeof(p));
d3765386 438 switch (nhop->type) {
e5c83d9b
DS
439 case NEXTHOP_TYPE_IFINDEX:
440 case NEXTHOP_TYPE_BLACKHOLE:
441 return;
442 case NEXTHOP_TYPE_IPV4:
443 case NEXTHOP_TYPE_IPV4_IFINDEX:
444 p.family = AF_INET;
445 p.u.prefix4.s_addr = nhop->gate.ipv4.s_addr;
446 p.prefixlen = 32;
447 break;
448 case NEXTHOP_TYPE_IPV6:
449 case NEXTHOP_TYPE_IPV6_IFINDEX:
450 p.family = AF_INET6;
451 memcpy(&p.u.prefix6, &nhop->gate.ipv6, 16);
452 p.prefixlen = 128;
cb254f41
SW
453 if (IN6_IS_ADDR_LINKLOCAL(&nhop->gate.ipv6))
454 /*
455 * Don't bother tracking link locals, just track their
456 * interface state.
457 */
458 return;
e5c83d9b
DS
459 break;
460 }
461
462 if (zclient_send_rnh(zclient, command, &p,
463 false, nhop->vrf_id) < 0) {
464 zlog_warn("%s: Failure to send nexthop to zebra",
465 __PRETTY_FUNCTION__);
466 }
467}
468
469static void pbr_encode_pbr_map_sequence_prefix(struct stream *s,
470 struct prefix *p,
49027ce8 471 unsigned char family)
e5c83d9b
DS
472{
473 struct prefix any;
474
475 if (!p) {
476 memset(&any, 0, sizeof(any));
477 any.family = family;
478 p = &any;
479 }
480
481 stream_putc(s, p->family);
482 stream_putc(s, p->prefixlen);
483 stream_put(s, &p->u.prefix, prefix_blen(p));
484}
485
486static void pbr_encode_pbr_map_sequence(struct stream *s,
487 struct pbr_map_sequence *pbrms,
488 struct interface *ifp)
489{
49027ce8 490 unsigned char family;
e5c83d9b
DS
491
492 family = AF_INET;
49027ce8
DS
493 if (pbrms->family)
494 family = pbrms->family;
e5c83d9b
DS
495
496 stream_putl(s, pbrms->seqno);
497 stream_putl(s, pbrms->ruleno);
498 stream_putl(s, pbrms->unique);
499 pbr_encode_pbr_map_sequence_prefix(s, pbrms->src, family);
500 stream_putw(s, 0); /* src port */
501 pbr_encode_pbr_map_sequence_prefix(s, pbrms->dst, family);
502 stream_putw(s, 0); /* dst port */
95a9fe02 503 stream_putl(s, pbrms->mark);
e5c83d9b
DS
504 if (pbrms->nhgrp_name)
505 stream_putl(s, pbr_nht_get_table(pbrms->nhgrp_name));
506 else if (pbrms->nhg)
507 stream_putl(s, pbr_nht_get_table(pbrms->internal_nhg_name));
508 stream_putl(s, ifp->ifindex);
509}
510
b13e5ad6
DS
511void pbr_send_pbr_map(struct pbr_map_sequence *pbrms,
512 struct pbr_map_interface *pmi, bool install)
e5c83d9b 513{
b13e5ad6 514 struct pbr_map *pbrm = pbrms->parent;
e5c83d9b 515 struct stream *s;
10a00758 516 uint64_t is_installed = (uint64_t)1 << pmi->install_bit;
37c606ff
DS
517
518 is_installed &= pbrms->installed;
e5c83d9b 519
37c606ff
DS
520 DEBUGD(&pbr_dbg_zebra, "%s: for %s %d(%" PRIu64 ")",
521 __PRETTY_FUNCTION__, pbrm->name, install, is_installed);
9b71ea4b
DS
522
523 /*
524 * If we are installed and asked to do so again
525 * just return. If we are not installed and asked
526 * and asked to delete just return;
527 */
37c606ff 528 if (install && is_installed)
9b71ea4b
DS
529 return;
530
37c606ff 531 if (!install && !is_installed)
9b71ea4b 532 return;
e5c83d9b
DS
533
534 s = zclient->obuf;
535 stream_reset(s);
536
537 zclient_create_header(s,
538 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
539 VRF_DEFAULT);
540
b13e5ad6
DS
541 /*
542 * We are sending one item at a time at the moment
543 */
544 stream_putl(s, 1);
e5c83d9b 545
b13e5ad6
DS
546 DEBUGD(&pbr_dbg_zebra, "%s: \t%s %s %d %s %u",
547 __PRETTY_FUNCTION__, install ? "Installing" : "Deleting",
548 pbrm->name, install, pmi->ifp->name, pmi->delete);
e5c83d9b 549
b13e5ad6 550 pbr_encode_pbr_map_sequence(s, pbrms, pmi->ifp);
e5c83d9b 551
e5c83d9b
DS
552 stream_putw_at(s, 0, stream_get_endp(s));
553
e5c83d9b
DS
554 zclient_send_message(zclient);
555}