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