]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vrf.c
Merge pull request #4337 from donaldsharp/routemap_coverity
[mirror_frr.git] / zebra / zebra_vrf.c
1 /*
2 * Copyright (C) 2016 CumulusNetworks
3 * Donald Sharp
4 *
5 * This file is part of Quagga
6 *
7 * Quagga is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * Quagga is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #include <zebra.h>
22
23 #include "log.h"
24 #include "linklist.h"
25 #include "command.h"
26 #include "memory.h"
27 #include "srcdest_table.h"
28 #include "vrf.h"
29 #include "vty.h"
30
31 #include "zebra/zebra_router.h"
32 #include "zebra/rtadv.h"
33 #include "zebra/debug.h"
34 #include "zebra/zapi_msg.h"
35 #include "zebra/rib.h"
36 #include "zebra/zebra_vrf.h"
37 #include "zebra/zebra_rnh.h"
38 #include "zebra/router-id.h"
39 #include "zebra/zebra_memory.h"
40 #include "zebra/interface.h"
41 #include "zebra/zebra_mpls.h"
42 #include "zebra/zebra_vxlan.h"
43 #include "zebra/zebra_netns_notify.h"
44 #include "zebra/zebra_routemap.h"
45
46 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
47 safi_t safi);
48 static void zebra_rnhtable_node_cleanup(struct route_table *table,
49 struct route_node *node);
50
51 DEFINE_MTYPE_STATIC(ZEBRA, OTHER_TABLE, "Other Table");
52
53 /* VRF information update. */
54 static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
55 {
56 struct listnode *node, *nnode;
57 struct zserv *client;
58
59 if (IS_ZEBRA_DEBUG_EVENT)
60 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
61
62 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
63 zsend_vrf_add(client, zvrf);
64 }
65
66 static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
67 {
68 struct listnode *node, *nnode;
69 struct zserv *client;
70
71 if (IS_ZEBRA_DEBUG_EVENT)
72 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
73
74 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
75 zsend_vrf_delete(client, zvrf);
76 }
77
78 void zebra_vrf_update_all(struct zserv *client)
79 {
80 struct vrf *vrf;
81
82 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
83 if (vrf->vrf_id != VRF_UNKNOWN)
84 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
85 }
86 }
87
88 /* Callback upon creating a new VRF. */
89 static int zebra_vrf_new(struct vrf *vrf)
90 {
91 struct zebra_vrf *zvrf;
92
93 if (IS_ZEBRA_DEBUG_EVENT)
94 zlog_info("VRF %s created, id %u", vrf->name, vrf->vrf_id);
95
96 zvrf = zebra_vrf_alloc();
97 vrf->info = zvrf;
98 zvrf->vrf = vrf;
99
100 otable_init(&zvrf->other_tables);
101
102 router_id_init(zvrf);
103 return 0;
104 }
105
106 /* Callback upon enabling a VRF. */
107 static int zebra_vrf_enable(struct vrf *vrf)
108 {
109 struct zebra_vrf *zvrf = vrf->info;
110 struct route_table *table;
111 afi_t afi;
112 safi_t safi;
113
114 assert(zvrf);
115 if (IS_ZEBRA_DEBUG_EVENT)
116 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
117 zvrf_id(zvrf));
118
119 if (vrf_is_backend_netns())
120 zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
121 else
122 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
123 #if defined(HAVE_RTADV)
124 rtadv_init(zvrf);
125 #endif
126
127 /* Inform clients that the VRF is now active. This is an
128 * add for the clients.
129 */
130
131 zebra_vrf_add_update(zvrf);
132 /* Allocate tables */
133 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
134 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
135 zebra_vrf_table_create(zvrf, afi, safi);
136
137 table = route_table_init();
138 table->cleanup = zebra_rnhtable_node_cleanup;
139 zvrf->rnh_table[afi] = table;
140
141 table = route_table_init();
142 table->cleanup = zebra_rnhtable_node_cleanup;
143 zvrf->import_check_table[afi] = table;
144 }
145
146 /* Kick off any VxLAN-EVPN processing. */
147 zebra_vxlan_vrf_enable(zvrf);
148
149 return 0;
150 }
151
152 /* Callback upon disabling a VRF. */
153 static int zebra_vrf_disable(struct vrf *vrf)
154 {
155 struct zebra_vrf *zvrf = vrf->info;
156 struct interface *ifp;
157 afi_t afi;
158 safi_t safi;
159 unsigned i;
160
161 assert(zvrf);
162 if (IS_ZEBRA_DEBUG_EVENT)
163 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
164 zvrf_id(zvrf));
165
166 /* Stop any VxLAN-EVPN processing. */
167 zebra_vxlan_vrf_disable(zvrf);
168
169 #if defined(HAVE_RTADV)
170 rtadv_terminate(zvrf);
171 #endif
172
173 /* Inform clients that the VRF is now inactive. This is a
174 * delete for the clients.
175 */
176 zebra_vrf_delete_update(zvrf);
177
178 /* If asked to retain routes, there's nothing more to do. */
179 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
180 return 0;
181
182 /* Remove all routes. */
183 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
184 route_table_finish(zvrf->rnh_table[afi]);
185 zvrf->rnh_table[afi] = NULL;
186 route_table_finish(zvrf->import_check_table[afi]);
187 zvrf->import_check_table[afi] = NULL;
188
189 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
190 rib_close_table(zvrf->table[afi][safi]);
191 }
192
193 /* Cleanup Vxlan, MPLS and PW tables. */
194 zebra_vxlan_cleanup_tables(zvrf);
195 zebra_mpls_cleanup_tables(zvrf);
196 zebra_pw_exit(zvrf);
197
198 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
199 */
200 FOR_ALL_INTERFACES (vrf, ifp)
201 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
202
203 /* clean-up work queues */
204 for (i = 0; i < MQ_SIZE; i++) {
205 struct listnode *lnode, *nnode;
206 struct route_node *rnode;
207 rib_dest_t *dest;
208
209 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
210 rnode)) {
211 dest = rib_dest_from_rnode(rnode);
212 if (dest && rib_dest_vrf(dest) == zvrf) {
213 route_unlock_node(rnode);
214 list_delete_node(zrouter.mq->subq[i], lnode);
215 zrouter.mq->size--;
216 }
217 }
218 }
219
220 /* Cleanup (free) routing tables and NHT tables. */
221 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
222 /*
223 * Set the table pointer to NULL as that
224 * we no-longer need a copy of it, nor do we
225 * own this data, the zebra_router structure
226 * owns these tables. Once we've cleaned up the
227 * table, see rib_close_table above
228 * we no-longer need this pointer.
229 */
230 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
231 zebra_router_release_table(zvrf, zvrf->table_id, afi,
232 safi);
233 zvrf->table[afi][safi] = NULL;
234 }
235 }
236
237 return 0;
238 }
239
240 static int zebra_vrf_delete(struct vrf *vrf)
241 {
242 struct zebra_vrf *zvrf = vrf->info;
243 struct other_route_table *otable;
244 struct route_table *table;
245 afi_t afi;
246 safi_t safi;
247 unsigned i;
248
249 assert(zvrf);
250 if (IS_ZEBRA_DEBUG_EVENT)
251 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
252 zvrf_id(zvrf));
253
254 /* clean-up work queues */
255 for (i = 0; i < MQ_SIZE; i++) {
256 struct listnode *lnode, *nnode;
257 struct route_node *rnode;
258 rib_dest_t *dest;
259
260 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
261 rnode)) {
262 dest = rib_dest_from_rnode(rnode);
263 if (dest && rib_dest_vrf(dest) == zvrf) {
264 route_unlock_node(rnode);
265 list_delete_node(zrouter.mq->subq[i], lnode);
266 zrouter.mq->size--;
267 }
268 }
269 }
270
271 /* Free Vxlan and MPLS. */
272 zebra_vxlan_close_tables(zvrf);
273 zebra_mpls_close_tables(zvrf);
274
275 /* release allocated memory */
276 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
277 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
278 table = zvrf->table[afi][safi];
279 if (table) {
280 zebra_router_release_table(zvrf, zvrf->table_id,
281 afi, safi);
282 zvrf->table[afi][safi] = NULL;
283 }
284 }
285
286 if (zvrf->rnh_table[afi])
287 route_table_finish(zvrf->rnh_table[afi]);
288 if (zvrf->import_check_table[afi])
289 route_table_finish(zvrf->import_check_table[afi]);
290 }
291
292 otable = otable_pop(&zvrf->other_tables);
293 while (otable) {
294 zebra_router_release_table(zvrf, otable->table_id,
295 otable->afi, otable->safi);
296 XFREE(MTYPE_OTHER_TABLE, otable);
297
298 otable = otable_pop(&zvrf->other_tables);
299 }
300
301 /* Cleanup EVPN states for vrf */
302 zebra_vxlan_vrf_delete(zvrf);
303
304 list_delete_all_node(zvrf->rid_all_sorted_list);
305 list_delete_all_node(zvrf->rid_lo_sorted_list);
306
307 otable_fini(&zvrf->other_tables);
308 XFREE(MTYPE_ZEBRA_VRF, zvrf);
309 vrf->info = NULL;
310
311 return 0;
312 }
313
314 static int zebra_vrf_update(struct vrf *vrf)
315 {
316 struct zebra_vrf *zvrf = vrf->info;
317
318 assert(zvrf);
319 if (IS_ZEBRA_DEBUG_EVENT)
320 zlog_debug("VRF %s id %u, name updated", vrf->name,
321 zvrf_id(zvrf));
322 zebra_vrf_add_update(zvrf);
323 return 0;
324 }
325
326
327 /* Return if this VRF has any FRR configuration or not.
328 * IMPORTANT: This function needs to be updated when additional configuration
329 * is added for a VRF.
330 */
331 int zebra_vrf_has_config(struct zebra_vrf *zvrf)
332 {
333 /* EVPN L3-VNI? */
334 if (zvrf->l3vni)
335 return 1;
336
337 return 0;
338 }
339
340 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
341 * NOTE: Table-id is relevant on two modes:
342 * - case VRF backend is default : on default VRF only
343 * - case VRF backend is netns : on all VRFs
344 */
345 struct route_table *zebra_vrf_table_with_table_id(afi_t afi, safi_t safi,
346 vrf_id_t vrf_id,
347 uint32_t table_id)
348 {
349 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
350 struct other_route_table ort, *otable;
351 struct route_table *table;
352
353 if (!zvrf)
354 return NULL;
355
356 if (afi >= AFI_MAX || safi >= SAFI_MAX)
357 return NULL;
358
359 if (table_id == zvrf->table_id)
360 return zebra_vrf_table(afi, safi, vrf_id);
361
362 ort.afi = afi;
363 ort.safi = safi;
364 ort.table_id = table_id;
365 otable = otable_find(&zvrf->other_tables, &ort);
366 if (otable)
367 return otable->table;
368
369 table = zebra_router_get_table(zvrf, table_id, afi, safi);
370
371 otable = XCALLOC(MTYPE_OTHER_TABLE, sizeof(*otable));
372 otable->afi = afi;
373 otable->safi = safi;
374 otable->table_id = table_id;
375 otable->table = table;
376 otable_add(&zvrf->other_tables, otable);
377
378 return table;
379 }
380
381 void zebra_rtable_node_cleanup(struct route_table *table,
382 struct route_node *node)
383 {
384 struct route_entry *re, *next;
385
386 RNODE_FOREACH_RE_SAFE (node, re, next) {
387 rib_unlink(node, re);
388 }
389
390 if (node->info) {
391 rib_dest_t *dest = node->info;
392
393 rnh_list_fini(&dest->nht);
394 XFREE(MTYPE_RIB_DEST, node->info);
395 }
396 }
397
398 static void zebra_rnhtable_node_cleanup(struct route_table *table,
399 struct route_node *node)
400 {
401 if (node->info)
402 zebra_free_rnh(node->info);
403 }
404
405 /*
406 * Create a routing table for the specific AFI/SAFI in the given VRF.
407 */
408 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
409 safi_t safi)
410 {
411 struct route_node *rn;
412 struct prefix p;
413
414 assert(!zvrf->table[afi][safi]);
415
416 zvrf->table[afi][safi] =
417 zebra_router_get_table(zvrf, zvrf->table_id, afi, safi);
418
419 memset(&p, 0, sizeof(p));
420 p.family = afi2family(afi);
421
422 rn = srcdest_rnode_get(zvrf->table[afi][safi], &p, NULL);
423 zebra_rib_create_dest(rn);
424 }
425
426 /* Allocate new zebra VRF. */
427 struct zebra_vrf *zebra_vrf_alloc(void)
428 {
429 struct zebra_vrf *zvrf;
430
431 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
432
433 zebra_vxlan_init_tables(zvrf);
434 zebra_mpls_init_tables(zvrf);
435 zebra_pw_init(zvrf);
436 zvrf->table_id = RT_TABLE_MAIN;
437 /* by default table ID is default one */
438 return zvrf;
439 }
440
441 /* Lookup VRF by identifier. */
442 struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
443 {
444 return vrf_info_lookup(vrf_id);
445 }
446
447 /* Lookup VRF by name. */
448 struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
449 {
450 struct vrf *vrf;
451
452 if (!name)
453 name = VRF_DEFAULT_NAME;
454
455 vrf = vrf_lookup_by_name(name);
456 if (vrf)
457 return ((struct zebra_vrf *)vrf->info);
458
459 return NULL;
460 }
461
462 /* Lookup the routing table in an enabled VRF. */
463 struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
464 {
465 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
466
467 if (!zvrf)
468 return NULL;
469
470 if (afi >= AFI_MAX || safi >= SAFI_MAX)
471 return NULL;
472
473 return zvrf->table[afi][safi];
474 }
475
476 static int vrf_config_write(struct vty *vty)
477 {
478 struct vrf *vrf;
479 struct zebra_vrf *zvrf;
480
481 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
482 zvrf = vrf->info;
483
484 if (!zvrf)
485 continue;
486
487 if (zvrf_id(zvrf) == VRF_DEFAULT) {
488 if (zvrf->l3vni)
489 vty_out(vty, "vni %u\n", zvrf->l3vni);
490 } else {
491 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
492 if (zvrf->l3vni)
493 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
494 is_l3vni_for_prefix_routes_only(
495 zvrf->l3vni)
496 ? " prefix-routes-only"
497 : "");
498 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
499 }
500
501 zebra_routemap_config_write_protocol(vty, zvrf);
502
503 if (zvrf_id(zvrf) != VRF_DEFAULT)
504 vty_endframe(vty, " exit-vrf\n!\n");
505 else
506 vty_out(vty, "!\n");
507 }
508 return 0;
509 }
510
511 /* Zebra VRF initialization. */
512 void zebra_vrf_init(void)
513 {
514 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
515 zebra_vrf_delete, zebra_vrf_update);
516
517 vrf_cmd_init(vrf_config_write, &zserv_privs);
518 }