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