]> git.proxmox.com Git - mirror_frr.git/blob - pbrd/pbr_zebra.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / pbrd / pbr_zebra.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Zebra connect code.
4 * Copyright (C) 2018 Cumulus Networks, Inc.
5 * Donald Sharp
6 */
7 #include <zebra.h>
8
9 #include "thread.h"
10 #include "command.h"
11 #include "network.h"
12 #include "prefix.h"
13 #include "routemap.h"
14 #include "table.h"
15 #include "stream.h"
16 #include "memory.h"
17 #include "zclient.h"
18 #include "filter.h"
19 #include "plist.h"
20 #include "log.h"
21 #include "nexthop.h"
22 #include "nexthop_group.h"
23
24 #include "pbr_nht.h"
25 #include "pbr_map.h"
26 #include "pbr_memory.h"
27 #include "pbr_zebra.h"
28 #include "pbr_debug.h"
29 #include "pbr_vrf.h"
30
31 DEFINE_MTYPE_STATIC(PBRD, PBR_INTERFACE, "PBR Interface");
32
33 /* Zebra structure to hold current status. */
34 struct zclient *zclient;
35
36 struct pbr_interface *pbr_if_new(struct interface *ifp)
37 {
38 struct pbr_interface *pbr_ifp;
39
40 assert(ifp);
41 assert(!ifp->info);
42
43 pbr_ifp = XCALLOC(MTYPE_PBR_INTERFACE, sizeof(*pbr_ifp));
44
45 ifp->info = pbr_ifp;
46 return pbr_ifp;
47 }
48
49 void pbr_if_del(struct interface *ifp)
50 {
51 XFREE(MTYPE_PBR_INTERFACE, ifp->info);
52 }
53
54 /* Interface addition message from zebra. */
55 int pbr_ifp_create(struct interface *ifp)
56 {
57 DEBUGD(&pbr_dbg_zebra, "%s: %s", __func__, ifp->name);
58
59 if (!ifp->info)
60 pbr_if_new(ifp);
61
62 pbr_nht_interface_update(ifp);
63 /* Update nexthops tracked from a `set nexthop` command */
64 pbr_nht_nexthop_interface_update(ifp);
65
66 pbr_map_policy_interface_update(ifp, true);
67
68 return 0;
69 }
70
71 int pbr_ifp_destroy(struct interface *ifp)
72 {
73 DEBUGD(&pbr_dbg_zebra, "%s: %s", __func__, ifp->name);
74
75 pbr_map_policy_interface_update(ifp, false);
76
77 return 0;
78 }
79
80 static int interface_address_add(ZAPI_CALLBACK_ARGS)
81 {
82 struct connected *c;
83 char buf[PREFIX_STRLEN];
84
85 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
86
87 DEBUGD(&pbr_dbg_zebra, "%s: %s added %s", __func__,
88 c ? c->ifp->name : "Unknown",
89 c ? prefix2str(c->address, buf, sizeof(buf)) : "Unknown");
90
91 return 0;
92 }
93
94 static int interface_address_delete(ZAPI_CALLBACK_ARGS)
95 {
96 struct connected *c;
97
98 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
99
100 if (!c)
101 return 0;
102
103 DEBUGD(&pbr_dbg_zebra, "%s: %s deleted %pFX", __func__, c->ifp->name,
104 c->address);
105
106 connected_free(&c);
107 return 0;
108 }
109
110 int pbr_ifp_up(struct interface *ifp)
111 {
112 DEBUGD(&pbr_dbg_zebra, "%s: %s is up", __func__, ifp->name);
113
114 pbr_nht_nexthop_interface_update(ifp);
115
116 return 0;
117 }
118
119 int pbr_ifp_down(struct interface *ifp)
120 {
121 DEBUGD(&pbr_dbg_zebra, "%s: %s is down", __func__, ifp->name);
122
123 pbr_nht_nexthop_interface_update(ifp);
124
125 return 0;
126 }
127
128 static int interface_vrf_update(ZAPI_CALLBACK_ARGS)
129 {
130 struct interface *ifp;
131 vrf_id_t new_vrf_id;
132
133 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
134 &new_vrf_id);
135
136 if (!ifp) {
137 DEBUGD(&pbr_dbg_zebra, "%s: VRF change interface not found",
138 __func__);
139
140 return 0;
141 }
142
143 DEBUGD(&pbr_dbg_zebra, "%s: %s VRF change %u -> %u", __func__,
144 ifp->name, vrf_id, new_vrf_id);
145
146 if_update_to_new_vrf(ifp, new_vrf_id);
147
148 return 0;
149 }
150
151 static int route_notify_owner(ZAPI_CALLBACK_ARGS)
152 {
153 struct prefix p;
154 enum zapi_route_notify_owner note;
155 uint32_t table_id;
156
157 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note,
158 NULL, NULL))
159 return -1;
160
161 switch (note) {
162 case ZAPI_ROUTE_FAIL_INSTALL:
163 DEBUGD(&pbr_dbg_zebra,
164 "%s: [%pFX] Route install failure for table: %u",
165 __func__, &p, table_id);
166 break;
167 case ZAPI_ROUTE_BETTER_ADMIN_WON:
168 DEBUGD(&pbr_dbg_zebra,
169 "%s: [%pFX] Route better admin distance won for table: %u",
170 __func__, &p, table_id);
171 break;
172 case ZAPI_ROUTE_INSTALLED:
173 DEBUGD(&pbr_dbg_zebra,
174 "%s: [%pFX] Route installed succeeded for table: %u",
175 __func__, &p, table_id);
176 pbr_nht_route_installed_for_table(table_id);
177 break;
178 case ZAPI_ROUTE_REMOVED:
179 DEBUGD(&pbr_dbg_zebra,
180 "%s: [%pFX] Route Removed succeeded for table: %u",
181 __func__, &p, table_id);
182 pbr_nht_route_removed_for_table(table_id);
183 break;
184 case ZAPI_ROUTE_REMOVE_FAIL:
185 DEBUGD(&pbr_dbg_zebra,
186 "%s: [%pFX] Route remove fail for table: %u", __func__,
187 &p, table_id);
188 break;
189 }
190
191 return 0;
192 }
193
194 static int rule_notify_owner(ZAPI_CALLBACK_ARGS)
195 {
196 uint32_t seqno, priority, unique;
197 enum zapi_rule_notify_owner note;
198 struct pbr_map_sequence *pbrms;
199 struct pbr_map_interface *pmi;
200 char ifname[INTERFACE_NAMSIZ + 1];
201 uint64_t installed;
202
203 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
204 ifname, &note))
205 return -1;
206
207 pmi = NULL;
208 pbrms = pbrms_lookup_unique(unique, ifname, &pmi);
209 if (!pbrms) {
210 DEBUGD(&pbr_dbg_zebra,
211 "%s: Failure to lookup pbrms based upon %u", __func__,
212 unique);
213 return 0;
214 }
215
216 installed = 1 << pmi->install_bit;
217
218 switch (note) {
219 case ZAPI_RULE_FAIL_INSTALL:
220 pbrms->installed &= ~installed;
221 break;
222 case ZAPI_RULE_INSTALLED:
223 pbrms->installed |= installed;
224 break;
225 case ZAPI_RULE_FAIL_REMOVE:
226 /* Don't change state on rule removal failure */
227 break;
228 case ZAPI_RULE_REMOVED:
229 pbrms->installed &= ~installed;
230 break;
231 }
232
233 DEBUGD(&pbr_dbg_zebra, "%s: Received %s: %" PRIu64, __func__,
234 zapi_rule_notify_owner2str(note), pbrms->installed);
235
236 pbr_map_final_interface_deletion(pbrms->parent, pmi);
237
238 return 0;
239 }
240
241 static void zebra_connected(struct zclient *zclient)
242 {
243 DEBUGD(&pbr_dbg_zebra, "%s: Registering for fun and profit", __func__);
244 zclient_send_reg_requests(zclient, VRF_DEFAULT);
245 }
246
247 static void route_add_helper(struct zapi_route *api, struct nexthop_group nhg,
248 uint8_t install_afi)
249 {
250 struct zapi_nexthop *api_nh;
251 struct nexthop *nhop;
252 int i;
253
254 api->prefix.family = install_afi;
255
256 DEBUGD(&pbr_dbg_zebra, " Encoding %pFX", &api->prefix);
257
258 i = 0;
259 for (ALL_NEXTHOPS(nhg, nhop)) {
260 api_nh = &api->nexthops[i];
261 api_nh->vrf_id = nhop->vrf_id;
262 api_nh->type = nhop->type;
263 api_nh->weight = nhop->weight;
264 switch (nhop->type) {
265 case NEXTHOP_TYPE_IPV4:
266 api_nh->gate.ipv4 = nhop->gate.ipv4;
267 break;
268 case NEXTHOP_TYPE_IPV4_IFINDEX:
269 api_nh->gate.ipv4 = nhop->gate.ipv4;
270 api_nh->ifindex = nhop->ifindex;
271 break;
272 case NEXTHOP_TYPE_IFINDEX:
273 api_nh->ifindex = nhop->ifindex;
274 break;
275 case NEXTHOP_TYPE_IPV6:
276 memcpy(&api_nh->gate.ipv6, &nhop->gate.ipv6,
277 IPV6_MAX_BYTELEN);
278 break;
279 case NEXTHOP_TYPE_IPV6_IFINDEX:
280 api_nh->ifindex = nhop->ifindex;
281 memcpy(&api_nh->gate.ipv6, &nhop->gate.ipv6,
282 IPV6_MAX_BYTELEN);
283 break;
284 case NEXTHOP_TYPE_BLACKHOLE:
285 api_nh->bh_type = nhop->bh_type;
286 break;
287 }
288 i++;
289 }
290 api->nexthop_num = i;
291
292 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, api);
293 }
294
295 /*
296 * This function assumes a default route is being
297 * installed into the appropriate tableid
298 */
299 void route_add(struct pbr_nexthop_group_cache *pnhgc, struct nexthop_group nhg,
300 afi_t install_afi)
301 {
302 struct zapi_route api;
303
304 DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __func__, pnhgc->table_id);
305
306 memset(&api, 0, sizeof(api));
307
308 api.vrf_id = VRF_DEFAULT;
309 api.type = ZEBRA_ROUTE_PBR;
310 api.safi = SAFI_UNICAST;
311 /*
312 * Sending a default route
313 */
314 api.tableid = pnhgc->table_id;
315 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
316 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
317 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
318 switch (install_afi) {
319 case AFI_MAX:
320 route_add_helper(&api, nhg, AF_INET);
321 route_add_helper(&api, nhg, AF_INET6);
322 break;
323 case AFI_IP:
324 route_add_helper(&api, nhg, AF_INET);
325 break;
326 case AFI_IP6:
327 route_add_helper(&api, nhg, AF_INET6);
328 break;
329 case AFI_L2VPN:
330 DEBUGD(&pbr_dbg_zebra,
331 "%s: Asked to install unsupported route type: L2VPN",
332 __func__);
333 break;
334 case AFI_UNSPEC:
335 DEBUGD(&pbr_dbg_zebra,
336 "%s: Asked to install unspecified route type", __func__);
337 break;
338 }
339 }
340
341 /*
342 * This function assumes a default route is being
343 * removed from the appropriate tableid
344 */
345 void route_delete(struct pbr_nexthop_group_cache *pnhgc, afi_t afi)
346 {
347 struct zapi_route api;
348
349 DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __func__, pnhgc->table_id);
350
351 memset(&api, 0, sizeof(api));
352 api.vrf_id = VRF_DEFAULT;
353 api.type = ZEBRA_ROUTE_PBR;
354 api.safi = SAFI_UNICAST;
355
356 api.tableid = pnhgc->table_id;
357 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
358
359 switch (afi) {
360 case AFI_IP:
361 api.prefix.family = AF_INET;
362 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
363 break;
364 case AFI_IP6:
365 api.prefix.family = AF_INET6;
366 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
367 break;
368 case AFI_MAX:
369 api.prefix.family = AF_INET;
370 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
371 api.prefix.family = AF_INET6;
372 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
373 break;
374 case AFI_L2VPN:
375 DEBUGD(&pbr_dbg_zebra,
376 "%s: Asked to delete unsupported route type: L2VPN",
377 __func__);
378 break;
379 case AFI_UNSPEC:
380 DEBUGD(&pbr_dbg_zebra,
381 "%s: Asked to delete unspecified route type", __func__);
382 break;
383 }
384 }
385
386 static int pbr_zebra_nexthop_update(ZAPI_CALLBACK_ARGS)
387 {
388 struct zapi_route nhr;
389 struct prefix matched;
390 uint32_t i;
391
392 if (!zapi_nexthop_update_decode(zclient->ibuf, &matched, &nhr)) {
393 zlog_err("Failure to decode Nexthop update message");
394 return 0;
395 }
396
397 if (DEBUG_MODE_CHECK(&pbr_dbg_zebra, DEBUG_MODE_ALL)) {
398
399 DEBUGD(&pbr_dbg_zebra,
400 "%s: Received Nexthop update: %pFX against %pFX",
401 __func__, &matched, &nhr.prefix);
402
403 DEBUGD(&pbr_dbg_zebra, "%s: (Nexthops(%u)", __func__,
404 nhr.nexthop_num);
405
406 for (i = 0; i < nhr.nexthop_num; i++) {
407 DEBUGD(&pbr_dbg_zebra,
408 "%s: Type: %d: vrf: %d, ifindex: %d gate: %pI4",
409 __func__, nhr.nexthops[i].type,
410 nhr.nexthops[i].vrf_id, nhr.nexthops[i].ifindex,
411 &nhr.nexthops[i].gate.ipv4);
412 }
413 }
414
415 nhr.prefix = matched;
416 pbr_nht_nexthop_update(&nhr);
417 return 1;
418 }
419
420 extern struct zebra_privs_t pbr_privs;
421
422 static zclient_handler *const pbr_handlers[] = {
423 [ZEBRA_INTERFACE_ADDRESS_ADD] = interface_address_add,
424 [ZEBRA_INTERFACE_ADDRESS_DELETE] = interface_address_delete,
425 [ZEBRA_INTERFACE_VRF_UPDATE] = interface_vrf_update,
426 [ZEBRA_ROUTE_NOTIFY_OWNER] = route_notify_owner,
427 [ZEBRA_RULE_NOTIFY_OWNER] = rule_notify_owner,
428 [ZEBRA_NEXTHOP_UPDATE] = pbr_zebra_nexthop_update,
429 };
430
431 void pbr_zebra_init(void)
432 {
433 struct zclient_options opt = { .receive_notify = true };
434
435 zclient = zclient_new(master, &opt, pbr_handlers,
436 array_size(pbr_handlers));
437
438 zclient_init(zclient, ZEBRA_ROUTE_PBR, 0, &pbr_privs);
439 zclient->zebra_connected = zebra_connected;
440 }
441
442 void pbr_send_rnh(struct nexthop *nhop, bool reg)
443 {
444 uint32_t command;
445 struct prefix p;
446
447 command = (reg) ?
448 ZEBRA_NEXTHOP_REGISTER : ZEBRA_NEXTHOP_UNREGISTER;
449
450 memset(&p, 0, sizeof(p));
451 switch (nhop->type) {
452 case NEXTHOP_TYPE_IFINDEX:
453 case NEXTHOP_TYPE_BLACKHOLE:
454 return;
455 case NEXTHOP_TYPE_IPV4:
456 case NEXTHOP_TYPE_IPV4_IFINDEX:
457 p.family = AF_INET;
458 p.u.prefix4.s_addr = nhop->gate.ipv4.s_addr;
459 p.prefixlen = IPV4_MAX_BITLEN;
460 break;
461 case NEXTHOP_TYPE_IPV6:
462 case NEXTHOP_TYPE_IPV6_IFINDEX:
463 p.family = AF_INET6;
464 memcpy(&p.u.prefix6, &nhop->gate.ipv6, IPV6_MAX_BYTELEN);
465 p.prefixlen = IPV6_MAX_BITLEN;
466 if (IN6_IS_ADDR_LINKLOCAL(&nhop->gate.ipv6))
467 /*
468 * Don't bother tracking link locals, just track their
469 * interface state.
470 */
471 return;
472 break;
473 }
474
475 if (zclient_send_rnh(zclient, command, &p, SAFI_UNICAST, false, false,
476 nhop->vrf_id)
477 == ZCLIENT_SEND_FAILURE) {
478 zlog_warn("%s: Failure to send nexthop to zebra", __func__);
479 }
480 }
481
482 static void pbr_encode_pbr_map_sequence_prefix(struct stream *s,
483 struct prefix *p,
484 unsigned char family)
485 {
486 struct prefix any;
487
488 if (!p) {
489 memset(&any, 0, sizeof(any));
490 any.family = family;
491 p = &any;
492 }
493
494 stream_putc(s, p->family);
495 stream_putc(s, p->prefixlen);
496 stream_put(s, &p->u.prefix, prefix_blen(p));
497 }
498
499 static void
500 pbr_encode_pbr_map_sequence_vrf(struct stream *s,
501 const struct pbr_map_sequence *pbrms,
502 const struct interface *ifp)
503 {
504 struct pbr_vrf *pbr_vrf;
505
506 if (pbrms->vrf_unchanged)
507 pbr_vrf = ifp->vrf->info;
508 else
509 pbr_vrf = pbr_vrf_lookup_by_name(pbrms->vrf_name);
510
511 if (!pbr_vrf) {
512 DEBUGD(&pbr_dbg_zebra, "%s: VRF not found", __func__);
513 return;
514 }
515
516 stream_putl(s, pbr_vrf->vrf->data.l.table_id);
517 }
518
519 static void pbr_encode_pbr_map_sequence(struct stream *s,
520 struct pbr_map_sequence *pbrms,
521 struct interface *ifp)
522 {
523 unsigned char family;
524
525 family = AF_INET;
526 if (pbrms->family)
527 family = pbrms->family;
528
529 stream_putl(s, pbrms->seqno);
530 stream_putl(s, pbrms->ruleno);
531 stream_putl(s, pbrms->unique);
532 stream_putc(s, pbrms->ip_proto); /* The ip_proto */
533 pbr_encode_pbr_map_sequence_prefix(s, pbrms->src, family);
534 stream_putw(s, pbrms->src_prt);
535 pbr_encode_pbr_map_sequence_prefix(s, pbrms->dst, family);
536 stream_putw(s, pbrms->dst_prt);
537 stream_putc(s, pbrms->dsfield);
538 stream_putl(s, pbrms->mark);
539
540 stream_putl(s, pbrms->action_queue_id);
541
542 stream_putw(s, pbrms->action_vlan_id);
543 stream_putw(s, pbrms->action_vlan_flags);
544 stream_putw(s, pbrms->action_pcp);
545
546 if (pbrms->vrf_unchanged || pbrms->vrf_lookup)
547 pbr_encode_pbr_map_sequence_vrf(s, pbrms, ifp);
548 else if (pbrms->nhgrp_name)
549 stream_putl(s, pbr_nht_get_table(pbrms->nhgrp_name));
550 else if (pbrms->nhg)
551 stream_putl(s, pbr_nht_get_table(pbrms->internal_nhg_name));
552 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
553 }
554
555 bool pbr_send_pbr_map(struct pbr_map_sequence *pbrms,
556 struct pbr_map_interface *pmi, bool install, bool changed)
557 {
558 struct pbr_map *pbrm = pbrms->parent;
559 struct stream *s;
560 uint64_t is_installed = (uint64_t)1 << pmi->install_bit;
561
562 is_installed &= pbrms->installed;
563
564 DEBUGD(&pbr_dbg_zebra, "%s: for %s %d(%" PRIu64 ")", __func__,
565 pbrm->name, install, is_installed);
566
567 /*
568 * If we are installed and asked to do so again and the config
569 * has not changed, just return.
570 *
571 * If we are not installed and asked
572 * to delete just return.
573 */
574 if (install && is_installed && !changed)
575 return false;
576
577 if (!install && !is_installed)
578 return false;
579
580 s = zclient->obuf;
581 stream_reset(s);
582
583 zclient_create_header(s,
584 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
585 VRF_DEFAULT);
586
587 /*
588 * We are sending one item at a time at the moment
589 */
590 stream_putl(s, 1);
591
592 DEBUGD(&pbr_dbg_zebra, "%s: %s %s seq %u %d %s %u", __func__,
593 install ? "Installing" : "Deleting", pbrm->name, pbrms->seqno,
594 install, pmi->ifp->name, pmi->delete);
595
596 pbr_encode_pbr_map_sequence(s, pbrms, pmi->ifp);
597
598 stream_putw_at(s, 0, stream_get_endp(s));
599
600 zclient_send_message(zclient);
601
602 return true;
603 }