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