]> git.proxmox.com Git - mirror_frr.git/blob - pbrd/pbr_zebra.c
Merge pull request #5653 from slankdev/slankdev-bgpd-support-prefix-sid-srv6-l3vpn
[mirror_frr.git] / pbrd / pbr_zebra.c
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 #include "pbr_vrf.h"
43
44 DEFINE_MTYPE_STATIC(PBRD, PBR_INTERFACE, "PBR Interface")
45
46 /* Zebra structure to hold current status. */
47 struct zclient *zclient;
48
49 struct pbr_interface *pbr_if_new(struct interface *ifp)
50 {
51 struct pbr_interface *pbr_ifp;
52
53 zassert(ifp);
54 zassert(!ifp->info);
55
56 pbr_ifp = XCALLOC(MTYPE_PBR_INTERFACE, sizeof(*pbr_ifp));
57
58 ifp->info = pbr_ifp;
59 return pbr_ifp;
60 }
61
62 /* Inteface addition message from zebra. */
63 int pbr_ifp_create(struct interface *ifp)
64 {
65 DEBUGD(&pbr_dbg_zebra,
66 "%s: %s", __PRETTY_FUNCTION__, ifp->name);
67
68 if (!ifp->info)
69 pbr_if_new(ifp);
70
71 /* Update nexthops tracked from a `set nexthop` command */
72 pbr_nht_nexthop_interface_update(ifp);
73
74 pbr_map_policy_interface_update(ifp, true);
75
76 return 0;
77 }
78
79 int pbr_ifp_destroy(struct interface *ifp)
80 {
81 DEBUGD(&pbr_dbg_zebra,
82 "%s: %s", __PRETTY_FUNCTION__, ifp->name);
83
84 pbr_map_policy_interface_update(ifp, false);
85
86 return 0;
87 }
88
89 static int interface_address_add(ZAPI_CALLBACK_ARGS)
90 {
91 struct connected *c;
92 char buf[PREFIX_STRLEN];
93
94 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
95
96 DEBUGD(&pbr_dbg_zebra,
97 "%s: %s added %s", __PRETTY_FUNCTION__,
98 c ? c->ifp->name : "Unknown",
99 c ? prefix2str(c->address, buf, sizeof(buf)) : "Unknown");
100
101 return 0;
102 }
103
104 static int interface_address_delete(ZAPI_CALLBACK_ARGS)
105 {
106 struct connected *c;
107 char buf[PREFIX_STRLEN];
108
109 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
110
111 if (!c)
112 return 0;
113
114 DEBUGD(&pbr_dbg_zebra,
115 "%s: %s deleted %s", __PRETTY_FUNCTION__, c->ifp->name,
116 prefix2str(c->address, buf, sizeof(buf)));
117
118 connected_free(&c);
119 return 0;
120 }
121
122 int pbr_ifp_up(struct interface *ifp)
123 {
124 DEBUGD(&pbr_dbg_zebra,
125 "%s: %s is up", __PRETTY_FUNCTION__, ifp->name);
126
127 pbr_nht_nexthop_interface_update(ifp);
128
129 return 0;
130 }
131
132 int pbr_ifp_down(struct interface *ifp)
133 {
134 DEBUGD(&pbr_dbg_zebra,
135 "%s: %s is down", __PRETTY_FUNCTION__, ifp->name);
136
137 pbr_nht_nexthop_interface_update(ifp);
138
139 return 0;
140 }
141
142 static int interface_vrf_update(ZAPI_CALLBACK_ARGS)
143 {
144 struct interface *ifp;
145 vrf_id_t new_vrf_id;
146
147 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
148 &new_vrf_id);
149
150 if (!ifp) {
151 DEBUGD(&pbr_dbg_zebra, "%s: VRF change interface not found",
152 __func__);
153
154 return 0;
155 }
156
157 DEBUGD(&pbr_dbg_zebra, "%s: %s VRF change %u -> %u", __func__,
158 ifp->name, vrf_id, new_vrf_id);
159
160 if_update_to_new_vrf(ifp, new_vrf_id);
161
162 return 0;
163 }
164
165 static int route_notify_owner(ZAPI_CALLBACK_ARGS)
166 {
167 struct prefix p;
168 enum zapi_route_notify_owner note;
169 uint32_t table_id;
170 char buf[PREFIX_STRLEN];
171
172 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note))
173 return -1;
174
175 prefix2str(&p, buf, sizeof(buf));
176
177 switch (note) {
178 case ZAPI_ROUTE_FAIL_INSTALL:
179 DEBUGD(&pbr_dbg_zebra,
180 "%s: [%s] Route install failure for table: %u",
181 __PRETTY_FUNCTION__, buf, table_id);
182 break;
183 case ZAPI_ROUTE_BETTER_ADMIN_WON:
184 DEBUGD(&pbr_dbg_zebra,
185 "%s: [%s] Route better admin distance won for table: %u",
186 __PRETTY_FUNCTION__, buf, table_id);
187 break;
188 case ZAPI_ROUTE_INSTALLED:
189 DEBUGD(&pbr_dbg_zebra,
190 "%s: [%s] Route installed succeeded for table: %u",
191 __PRETTY_FUNCTION__, buf, table_id);
192 pbr_nht_route_installed_for_table(table_id);
193 break;
194 case ZAPI_ROUTE_REMOVED:
195 DEBUGD(&pbr_dbg_zebra,
196 "%s: [%s] Route Removed succeeded for table: %u",
197 __PRETTY_FUNCTION__, buf, table_id);
198 pbr_nht_route_removed_for_table(table_id);
199 break;
200 case ZAPI_ROUTE_REMOVE_FAIL:
201 DEBUGD(&pbr_dbg_zebra,
202 "%s: [%s] Route remove fail for table: %u",
203 __PRETTY_FUNCTION__, buf, table_id);
204 break;
205 }
206
207 return 0;
208 }
209
210 static int rule_notify_owner(ZAPI_CALLBACK_ARGS)
211 {
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;
216 ifindex_t ifi;
217 uint64_t installed;
218
219 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
220 &ifi, &note))
221 return -1;
222
223 pmi = NULL;
224 pbrms = pbrms_lookup_unique(unique, ifi, &pmi);
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
232 installed = 1 << pmi->install_bit;
233
234 switch (note) {
235 case ZAPI_RULE_FAIL_INSTALL:
236 pbrms->installed &= ~installed;
237 break;
238 case ZAPI_RULE_INSTALLED:
239 pbrms->installed |= installed;
240 break;
241 case ZAPI_RULE_FAIL_REMOVE:
242 /* Don't change state on rule removal failure */
243 break;
244 case ZAPI_RULE_REMOVED:
245 pbrms->installed &= ~installed;
246 break;
247 }
248
249 DEBUGD(&pbr_dbg_zebra, "%s: Received %s: %" PRIu64, __func__,
250 zapi_rule_notify_owner2str(note), pbrms->installed);
251
252 pbr_map_final_interface_deletion(pbrms->parent, pmi);
253
254 return 0;
255 }
256
257 static void zebra_connected(struct zclient *zclient)
258 {
259 DEBUGD(&pbr_dbg_zebra, "%s: Registering for fun and profit",
260 __PRETTY_FUNCTION__);
261 zclient_send_reg_requests(zclient, VRF_DEFAULT);
262 }
263
264 static void route_add_helper(struct zapi_route *api, struct nexthop_group nhg,
265 uint8_t install_afi)
266 {
267 struct zapi_nexthop *api_nh;
268 char buf[PREFIX_STRLEN];
269 struct nexthop *nhop;
270 int i;
271
272 api->prefix.family = install_afi;
273
274 DEBUGD(&pbr_dbg_zebra, "\tEncoding %s",
275 prefix2str(&api->prefix, buf, sizeof(buf)));
276
277 i = 0;
278 for (ALL_NEXTHOPS(nhg, nhop)) {
279 api_nh = &api->nexthops[i];
280 api_nh->vrf_id = nhop->vrf_id;
281 api_nh->type = nhop->type;
282 api_nh->weight = nhop->weight;
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 */
316 void route_add(struct pbr_nexthop_group_cache *pnhgc, struct nexthop_group nhg,
317 afi_t install_afi)
318 {
319 struct zapi_route api;
320
321 DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __PRETTY_FUNCTION__,
322 pnhgc->table_id);
323
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;
352 case AFI_UNSPEC:
353 DEBUGD(&pbr_dbg_zebra,
354 "%s: Asked to install unspecified route type",
355 __PRETTY_FUNCTION__);
356 break;
357 }
358 }
359
360 /*
361 * This function assumes a default route is being
362 * removed from the appropriate tableid
363 */
364 void route_delete(struct pbr_nexthop_group_cache *pnhgc, afi_t afi)
365 {
366 struct zapi_route api;
367
368 DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __PRETTY_FUNCTION__,
369 pnhgc->table_id);
370
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;
399 case AFI_UNSPEC:
400 DEBUGD(&pbr_dbg_zebra,
401 "%s: Asked to delete unspecified route type",
402 __PRETTY_FUNCTION__);
403 break;
404 }
405 }
406
407 static int pbr_zebra_nexthop_update(ZAPI_CALLBACK_ARGS)
408 {
409 struct zapi_route nhr;
410 char buf[PREFIX2STR_BUFFER];
411 uint32_t i;
412
413 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
414 zlog_warn("Failure to decode Nexthop update message");
415 return 0;
416 }
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
440 extern struct zebra_privs_t pbr_privs;
441
442 void pbr_zebra_init(void)
443 {
444 struct zclient_options opt = { .receive_notify = true };
445
446 zclient = zclient_new(master, &opt);
447
448 zclient_init(zclient, ZEBRA_ROUTE_PBR, 0, &pbr_privs);
449 zclient->zebra_connected = zebra_connected;
450 zclient->interface_address_add = interface_address_add;
451 zclient->interface_address_delete = interface_address_delete;
452 zclient->interface_vrf_update = interface_vrf_update;
453 zclient->route_notify_owner = route_notify_owner;
454 zclient->rule_notify_owner = rule_notify_owner;
455 zclient->nexthop_update = pbr_zebra_nexthop_update;
456 }
457
458 void pbr_send_rnh(struct nexthop *nhop, bool reg)
459 {
460 uint32_t command;
461 struct prefix p;
462
463 command = (reg) ?
464 ZEBRA_NEXTHOP_REGISTER : ZEBRA_NEXTHOP_UNREGISTER;
465
466 memset(&p, 0, sizeof(p));
467 switch (nhop->type) {
468 case NEXTHOP_TYPE_IFINDEX:
469 case NEXTHOP_TYPE_BLACKHOLE:
470 return;
471 case NEXTHOP_TYPE_IPV4:
472 case NEXTHOP_TYPE_IPV4_IFINDEX:
473 p.family = AF_INET;
474 p.u.prefix4.s_addr = nhop->gate.ipv4.s_addr;
475 p.prefixlen = 32;
476 break;
477 case NEXTHOP_TYPE_IPV6:
478 case NEXTHOP_TYPE_IPV6_IFINDEX:
479 p.family = AF_INET6;
480 memcpy(&p.u.prefix6, &nhop->gate.ipv6, 16);
481 p.prefixlen = 128;
482 if (IN6_IS_ADDR_LINKLOCAL(&nhop->gate.ipv6))
483 /*
484 * Don't bother tracking link locals, just track their
485 * interface state.
486 */
487 return;
488 break;
489 }
490
491 if (zclient_send_rnh(zclient, command, &p,
492 false, nhop->vrf_id) < 0) {
493 zlog_warn("%s: Failure to send nexthop to zebra",
494 __PRETTY_FUNCTION__);
495 }
496 }
497
498 static void pbr_encode_pbr_map_sequence_prefix(struct stream *s,
499 struct prefix *p,
500 unsigned char family)
501 {
502 struct prefix any;
503
504 if (!p) {
505 memset(&any, 0, sizeof(any));
506 any.family = family;
507 p = &any;
508 }
509
510 stream_putc(s, p->family);
511 stream_putc(s, p->prefixlen);
512 stream_put(s, &p->u.prefix, prefix_blen(p));
513 }
514
515 static void
516 pbr_encode_pbr_map_sequence_vrf(struct stream *s,
517 const struct pbr_map_sequence *pbrms,
518 const struct interface *ifp)
519 {
520 struct pbr_vrf *pbr_vrf;
521
522 if (pbrms->vrf_unchanged)
523 pbr_vrf = pbr_vrf_lookup_by_id(ifp->vrf_id);
524 else
525 pbr_vrf = pbr_vrf_lookup_by_name(pbrms->vrf_name);
526
527 if (!pbr_vrf) {
528 DEBUGD(&pbr_dbg_zebra, "%s: VRF not found", __func__);
529 return;
530 }
531
532 stream_putl(s, pbr_vrf->vrf->data.l.table_id);
533 }
534
535 static void pbr_encode_pbr_map_sequence(struct stream *s,
536 struct pbr_map_sequence *pbrms,
537 struct interface *ifp)
538 {
539 unsigned char family;
540
541 family = AF_INET;
542 if (pbrms->family)
543 family = pbrms->family;
544
545 stream_putl(s, pbrms->seqno);
546 stream_putl(s, pbrms->ruleno);
547 stream_putl(s, pbrms->unique);
548 pbr_encode_pbr_map_sequence_prefix(s, pbrms->src, family);
549 stream_putw(s, 0); /* src port */
550 pbr_encode_pbr_map_sequence_prefix(s, pbrms->dst, family);
551 stream_putw(s, 0); /* dst port */
552 stream_putl(s, pbrms->mark);
553
554 if (pbrms->vrf_unchanged || pbrms->vrf_lookup)
555 pbr_encode_pbr_map_sequence_vrf(s, pbrms, ifp);
556 else if (pbrms->nhgrp_name)
557 stream_putl(s, pbr_nht_get_table(pbrms->nhgrp_name));
558 else if (pbrms->nhg)
559 stream_putl(s, pbr_nht_get_table(pbrms->internal_nhg_name));
560 stream_putl(s, ifp->ifindex);
561 }
562
563 void pbr_send_pbr_map(struct pbr_map_sequence *pbrms,
564 struct pbr_map_interface *pmi, bool install)
565 {
566 struct pbr_map *pbrm = pbrms->parent;
567 struct stream *s;
568 uint64_t is_installed = (uint64_t)1 << pmi->install_bit;
569
570 is_installed &= pbrms->installed;
571
572 DEBUGD(&pbr_dbg_zebra, "%s: for %s %d(%" PRIu64 ")",
573 __PRETTY_FUNCTION__, pbrm->name, install, is_installed);
574
575 /*
576 * If we are installed and asked to do so again
577 * just return. If we are not installed and asked
578 * and asked to delete just return;
579 */
580 if (install && is_installed)
581 return;
582
583 if (!install && !is_installed)
584 return;
585
586 s = zclient->obuf;
587 stream_reset(s);
588
589 zclient_create_header(s,
590 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
591 VRF_DEFAULT);
592
593 /*
594 * We are sending one item at a time at the moment
595 */
596 stream_putl(s, 1);
597
598 DEBUGD(&pbr_dbg_zebra, "%s: \t%s %s %d %s %u",
599 __PRETTY_FUNCTION__, install ? "Installing" : "Deleting",
600 pbrm->name, install, pmi->ifp->name, pmi->delete);
601
602 pbr_encode_pbr_map_sequence(s, pbrms, pmi->ifp);
603
604 stream_putw_at(s, 0, stream_get_endp(s));
605
606 zclient_send_message(zclient);
607 }