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