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