]> git.proxmox.com Git - mirror_frr.git/blame - lib/vrf.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / vrf.c
CommitLineData
b72ede27
FL
1/*
2 * VRF functions.
3 * Copyright (C) 2014 6WIND S.A.
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra 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 *
896014f4
DL
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
b72ede27
FL
20 */
21
22#include <zebra.h>
23
4691b65a
PG
24/* for basename */
25#include <libgen.h>
26
6a69b354 27#include "if.h"
b72ede27 28#include "vrf.h"
7922fc65 29#include "vrf_int.h"
b72ede27
FL
30#include "prefix.h"
31#include "table.h"
32#include "log.h"
33#include "memory.h"
19dc275e 34#include "command.h"
b95c1883 35#include "ns.h"
3bc34908 36#include "privs.h"
98cbbaea 37#include "nexthop_group.h"
b66d022e 38#include "lib_errors.h"
19dc275e 39
ec31f30d
PG
40/* default VRF ID value used when VRF backend is not NETNS */
41#define VRF_DEFAULT_INTERNAL 0
dd114702 42#define VRF_DEFAULT_NAME_INTERNAL "default"
ec31f30d 43
d62a17ae 44DEFINE_MTYPE_STATIC(LIB, VRF, "VRF")
4a1ab8e4
DL
45DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map")
46
e80e7cce
DL
47DEFINE_QOBJ_TYPE(vrf)
48
d62a17ae 49static __inline int vrf_id_compare(const struct vrf *, const struct vrf *);
50static __inline int vrf_name_compare(const struct vrf *, const struct vrf *);
1a1a7065 51
d62a17ae 52RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare);
53RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare);
1a1a7065 54
d62a17ae 55struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id);
56struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name);
1a1a7065 57
78dd30b2 58static int vrf_backend;
3bc34908 59static struct zebra_privs_t *vrf_daemon_privs;
c200f5e1 60static char vrf_default_name[VRF_NAMSIZ] = VRF_DEFAULT_NAME_INTERNAL;
78dd30b2 61
19dc275e
DS
62/*
63 * Turn on/off debug code
64 * for vrf.
65 */
66int debug_vrf = 0;
b72ede27 67
b72ede27 68/* Holding VRF hooks */
d62a17ae 69struct vrf_master {
70 int (*vrf_new_hook)(struct vrf *);
71 int (*vrf_delete_hook)(struct vrf *);
72 int (*vrf_enable_hook)(struct vrf *);
73 int (*vrf_disable_hook)(struct vrf *);
ecbc5a37 74 int (*vrf_update_name_hook)(struct vrf *vrf);
d62a17ae 75} vrf_master = {
76 0,
77};
b72ede27 78
d62a17ae 79static int vrf_is_enabled(struct vrf *vrf);
e5bf3e1e 80
216b18ef 81/* VRF list existance check by name. */
d62a17ae 82struct vrf *vrf_lookup_by_name(const char *name)
216b18ef 83{
d62a17ae 84 struct vrf vrf;
85 strlcpy(vrf.name, name, sizeof(vrf.name));
86 return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf));
216b18ef 87}
216b18ef 88
d62a17ae 89static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b)
b72ede27 90{
d62a17ae 91 return (a->vrf_id - b->vrf_id);
216b18ef
DS
92}
93
d62a17ae 94static int vrf_name_compare(const struct vrf *a, const struct vrf *b)
b72ede27 95{
d62a17ae 96 return strcmp(a->name, b->name);
b72ede27
FL
97}
98
e26aedbe
PG
99/* if ns_id is different and not VRF_UNKNOWN,
100 * then update vrf identifier, and enable VRF
101 */
102static void vrf_update_vrf_id(ns_id_t ns_id, void *opaqueptr)
103{
104 ns_id_t vrf_id = (vrf_id_t)ns_id;
105 vrf_id_t old_vrf_id;
106 struct vrf *vrf = (struct vrf *)opaqueptr;
107
108 if (!vrf)
109 return;
110 old_vrf_id = vrf->vrf_id;
111 if (vrf_id == vrf->vrf_id)
112 return;
113 if (vrf->vrf_id != VRF_UNKNOWN)
114 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
115 vrf->vrf_id = vrf_id;
116 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
117 if (old_vrf_id == VRF_UNKNOWN)
118 vrf_enable((struct vrf *)vrf);
119}
120
ce1be369
PG
121int vrf_switch_to_netns(vrf_id_t vrf_id)
122{
123 char *name;
124 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
125
ce1be369 126 /* VRF is default VRF. silently ignore */
e26aedbe 127 if (!vrf || vrf->vrf_id == VRF_DEFAULT)
9dff1132 128 return 1; /* 1 = default */
e26aedbe
PG
129 /* VRF has no NETNS backend. silently ignore */
130 if (vrf->data.l.netns_name[0] == '\0')
9dff1132 131 return 2; /* 2 = no netns */
ce1be369
PG
132 name = ns_netns_pathname(NULL, vrf->data.l.netns_name);
133 if (debug_vrf)
134 zlog_debug("VRF_SWITCH: %s(%u)", name, vrf->vrf_id);
135 return ns_switch_to_netns(name);
136}
137
138int vrf_switchback_to_initial(void)
139{
140 int ret = ns_switchback_to_initial();
141
142 if (ret == 0 && debug_vrf)
143 zlog_debug("VRF_SWITCHBACK");
144 return ret;
145}
146
216b18ef 147/* Get a VRF. If not found, create one.
34f8e6af
DS
148 * Arg:
149 * name - The name of the vrf. May be NULL if unknown.
150 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
216b18ef 151 * Description: Please note that this routine can be called with just the name
34f8e6af
DS
152 * and 0 vrf-id
153 */
d62a17ae 154struct vrf *vrf_get(vrf_id_t vrf_id, const char *name)
155{
156 struct vrf *vrf = NULL;
157 int new = 0;
158
159 if (debug_vrf)
996c9314
LB
160 zlog_debug("VRF_GET: %s(%u)", name == NULL ? "(NULL)" : name,
161 vrf_id);
d62a17ae 162
163 /* Nothing to see, move along here */
164 if (!name && vrf_id == VRF_UNKNOWN)
165 return NULL;
166
0c2bac38
PG
167 /* attempt to find already available VRF
168 */
169 if (name)
170 vrf = vrf_lookup_by_name(name);
dd114702
PG
171 if (vrf && vrf_id != VRF_UNKNOWN
172 && vrf->vrf_id != VRF_UNKNOWN
173 && vrf->vrf_id != vrf_id) {
174 zlog_debug("VRF_GET: avoid %s creation(%u), same name exists (%u)",
175 name, vrf_id, vrf->vrf_id);
176 return NULL;
177 }
d62a17ae 178 /* Try to find VRF both by ID and name */
0c2bac38 179 if (!vrf && vrf_id != VRF_UNKNOWN)
d62a17ae 180 vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 181
182 if (vrf == NULL) {
183 vrf = XCALLOC(MTYPE_VRF, sizeof(struct vrf));
184 vrf->vrf_id = VRF_UNKNOWN;
d62a17ae 185 QOBJ_REG(vrf, vrf);
186 new = 1;
187
188 if (debug_vrf)
189 zlog_debug("VRF(%u) %s is created.", vrf_id,
190 (name) ? name : "(NULL)");
191 }
192
193 /* Set identifier */
194 if (vrf_id != VRF_UNKNOWN && vrf->vrf_id == VRF_UNKNOWN) {
195 vrf->vrf_id = vrf_id;
196 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
197 }
198
199 /* Set name */
200 if (name && vrf->name[0] != '\0' && strcmp(name, vrf->name)) {
201 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
202 strlcpy(vrf->name, name, sizeof(vrf->name));
203 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
204 } else if (name && vrf->name[0] == '\0') {
205 strlcpy(vrf->name, name, sizeof(vrf->name));
206 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
207 }
d62a17ae 208 if (new &&vrf_master.vrf_new_hook)
209 (*vrf_master.vrf_new_hook)(vrf);
210
211 return vrf;
b72ede27
FL
212}
213
84915b0a 214/* Delete a VRF. This is called when the underlying VRF goes away, a
215 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
216 */
d62a17ae 217void vrf_delete(struct vrf *vrf)
b72ede27 218{
d62a17ae 219 if (debug_vrf)
220 zlog_debug("VRF %u is to be deleted.", vrf->vrf_id);
b72ede27 221
d62a17ae 222 if (vrf_is_enabled(vrf))
223 vrf_disable(vrf);
e5bf3e1e 224
84915b0a 225 /* If the VRF is user configured, it'll stick around, just remove
226 * the ID mapping. Interfaces assigned to this VRF should've been
227 * removed already as part of the VRF going down.
228 */
229 if (vrf_is_user_cfged(vrf)) {
230 if (vrf->vrf_id != VRF_UNKNOWN) {
231 /* Delete any VRF interfaces - should be only
232 * the VRF itself, other interfaces should've
233 * been moved out of the VRF.
234 */
235 if_terminate(vrf);
236 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
237 vrf->vrf_id = VRF_UNKNOWN;
238 }
239 return;
240 }
241
d62a17ae 242 if (vrf_master.vrf_delete_hook)
243 (*vrf_master.vrf_delete_hook)(vrf);
216b18ef 244
d62a17ae 245 QOBJ_UNREG(vrf);
f4e14fdb 246 if_terminate(vrf);
b72ede27 247
d62a17ae 248 if (vrf->vrf_id != VRF_UNKNOWN)
249 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
250 if (vrf->name[0] != '\0')
251 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
b72ede27 252
d62a17ae 253 XFREE(MTYPE_VRF, vrf);
b72ede27
FL
254}
255
256/* Look up a VRF by identifier. */
d62a17ae 257struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id)
b72ede27 258{
d62a17ae 259 struct vrf vrf;
260 vrf.vrf_id = vrf_id;
261 return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf));
b72ede27
FL
262}
263
e5bf3e1e
FL
264/*
265 * Enable a VRF - that is, let the VRF be ready to use.
266 * The VRF_ENABLE_HOOK callback will be called to inform
267 * that they can allocate resources in this VRF.
268 *
269 * RETURN: 1 - enabled successfully; otherwise, 0.
270 */
d62a17ae 271int vrf_enable(struct vrf *vrf)
e5bf3e1e 272{
d62a17ae 273 if (vrf_is_enabled(vrf))
274 return 1;
05e8e11e 275
d62a17ae 276 if (debug_vrf)
277 zlog_debug("VRF %u is enabled.", vrf->vrf_id);
e5bf3e1e 278
d62a17ae 279 SET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 280
d62a17ae 281 if (vrf_master.vrf_enable_hook)
282 (*vrf_master.vrf_enable_hook)(vrf);
e5bf3e1e 283
98cbbaea
DS
284 /*
285 * If we have any nexthop group entries that
286 * are awaiting vrf initialization then
287 * let's let people know about it
288 */
289 nexthop_group_enable_vrf(vrf);
290
d62a17ae 291 return 1;
e5bf3e1e
FL
292}
293
294/*
295 * Disable a VRF - that is, let the VRF be unusable.
296 * The VRF_DELETE_HOOK callback will be called to inform
297 * that they must release the resources in the VRF.
298 */
697d3ec7 299void vrf_disable(struct vrf *vrf)
e5bf3e1e 300{
d62a17ae 301 if (!vrf_is_enabled(vrf))
302 return;
a647bfa8 303
d62a17ae 304 UNSET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 305
d62a17ae 306 if (debug_vrf)
307 zlog_debug("VRF %u is to be disabled.", vrf->vrf_id);
e5bf3e1e 308
d62a17ae 309 /* Till now, nothing to be done for the default VRF. */
310 // Pending: see why this statement.
e74f14fc 311
d62a17ae 312 if (vrf_master.vrf_disable_hook)
313 (*vrf_master.vrf_disable_hook)(vrf);
e5bf3e1e
FL
314}
315
b7cfce93
MK
316const char *vrf_id_to_name(vrf_id_t vrf_id)
317{
318 struct vrf *vrf;
319
320 vrf = vrf_lookup_by_id(vrf_id);
321 if (vrf)
322 return vrf->name;
323
181c08c6 324 return "n/a";
b7cfce93
MK
325}
326
d62a17ae 327vrf_id_t vrf_name_to_id(const char *name)
216b18ef 328{
d62a17ae 329 struct vrf *vrf;
330 vrf_id_t vrf_id = VRF_DEFAULT; // Pending: need a way to return invalid
331 // id/ routine not used.
216b18ef 332
2569910b
PG
333 if (!name)
334 return vrf_id;
d62a17ae 335 vrf = vrf_lookup_by_name(name);
336 if (vrf)
337 vrf_id = vrf->vrf_id;
216b18ef 338
d62a17ae 339 return vrf_id;
216b18ef
DS
340}
341
b72ede27 342/* Get the data pointer of the specified VRF. If not found, create one. */
d62a17ae 343void *vrf_info_get(vrf_id_t vrf_id)
b72ede27 344{
d62a17ae 345 struct vrf *vrf = vrf_get(vrf_id, NULL);
346 return vrf->info;
b72ede27
FL
347}
348
349/* Look up the data pointer of the specified VRF. */
d62a17ae 350void *vrf_info_lookup(vrf_id_t vrf_id)
b72ede27 351{
d62a17ae 352 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
353 return vrf ? vrf->info : NULL;
b72ede27
FL
354}
355
7076bb2f 356/*
4a8bf858 357 * VRF hash for storing set or not.
7076bb2f 358 */
4a8bf858
DS
359struct vrf_bit_set {
360 vrf_id_t vrf_id;
361 bool set;
362};
7076bb2f 363
4a8bf858
DS
364static unsigned int vrf_hash_bitmap_key(void *data)
365{
366 struct vrf_bit_set *bit = data;
d62a17ae 367
4a8bf858
DS
368 return bit->vrf_id;
369}
d62a17ae 370
74df8d6d 371static bool vrf_hash_bitmap_cmp(const void *a, const void *b)
4a8bf858
DS
372{
373 const struct vrf_bit_set *bit1 = a;
374 const struct vrf_bit_set *bit2 = b;
d62a17ae 375
4a8bf858
DS
376 return bit1->vrf_id == bit2->vrf_id;
377}
378
379static void *vrf_hash_bitmap_alloc(void *data)
380{
381 struct vrf_bit_set *copy = data;
382 struct vrf_bit_set *bit;
383
384 bit = XMALLOC(MTYPE_VRF_BITMAP, sizeof(*bit));
385 bit->vrf_id = copy->vrf_id;
386
387 return bit;
388}
389
390static void vrf_hash_bitmap_free(void *data)
391{
392 struct vrf_bit_set *bit = data;
393
394 XFREE(MTYPE_VRF_BITMAP, bit);
395}
7076bb2f 396
d62a17ae 397vrf_bitmap_t vrf_bitmap_init(void)
7076bb2f 398{
4a8bf858
DS
399 return hash_create_size(32, vrf_hash_bitmap_key, vrf_hash_bitmap_cmp,
400 "VRF BIT HASH");
7076bb2f
FL
401}
402
d62a17ae 403void vrf_bitmap_free(vrf_bitmap_t bmap)
7076bb2f 404{
4a8bf858 405 struct hash *vrf_hash = bmap;
7076bb2f 406
4a8bf858 407 if (vrf_hash == NULL)
d62a17ae 408 return;
7076bb2f 409
4a8bf858
DS
410 hash_clean(vrf_hash, vrf_hash_bitmap_free);
411 hash_free(vrf_hash);
7076bb2f
FL
412}
413
d62a17ae 414void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 415{
4a8bf858
DS
416 struct vrf_bit_set lookup = { .vrf_id = vrf_id };
417 struct hash *vrf_hash = bmap;
418 struct vrf_bit_set *bit;
7076bb2f 419
4a8bf858 420 if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN)
d62a17ae 421 return;
7076bb2f 422
4a8bf858
DS
423 bit = hash_get(vrf_hash, &lookup, vrf_hash_bitmap_alloc);
424 bit->set = true;
7076bb2f
FL
425}
426
d62a17ae 427void vrf_bitmap_unset(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 428{
4a8bf858
DS
429 struct vrf_bit_set lookup = { .vrf_id = vrf_id };
430 struct hash *vrf_hash = bmap;
431 struct vrf_bit_set *bit;
7076bb2f 432
4a8bf858 433 if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN)
d62a17ae 434 return;
7076bb2f 435
4a8bf858
DS
436 bit = hash_get(vrf_hash, &lookup, vrf_hash_bitmap_alloc);
437 bit->set = false;
7076bb2f
FL
438}
439
d62a17ae 440int vrf_bitmap_check(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 441{
4a8bf858
DS
442 struct vrf_bit_set lookup = { .vrf_id = vrf_id };
443 struct hash *vrf_hash = bmap;
444 struct vrf_bit_set *bit;
7076bb2f 445
4a8bf858 446 if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN)
d62a17ae 447 return 0;
7076bb2f 448
4a8bf858
DS
449 bit = hash_lookup(vrf_hash, &lookup);
450 if (bit)
451 return bit->set;
452
453 return 0;
7076bb2f
FL
454}
455
d62a17ae 456static void vrf_autocomplete(vector comps, struct cmd_token *token)
d617d5fe 457{
d62a17ae 458 struct vrf *vrf = NULL;
d617d5fe 459
723001fc
PG
460 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
461 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
d617d5fe
DS
462}
463
464static const struct cmd_variable_handler vrf_var_handlers[] = {
d62a17ae 465 {
466 .varname = "vrf",
467 .completions = vrf_autocomplete,
468 },
469 {.completions = NULL},
d617d5fe
DS
470};
471
b72ede27 472/* Initialize VRF module. */
d62a17ae 473void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *),
ecbc5a37
PG
474 int (*disable)(struct vrf *), int (*delete)(struct vrf *),
475 int ((*update)(struct vrf *)))
d62a17ae 476{
477 struct vrf *default_vrf;
478
e26aedbe
PG
479 /* initialise NS, in case VRF backend if NETNS */
480 ns_init();
d62a17ae 481 if (debug_vrf)
482 zlog_debug("%s: Initializing VRF subsystem",
483 __PRETTY_FUNCTION__);
484
485 vrf_master.vrf_new_hook = create;
486 vrf_master.vrf_enable_hook = enable;
487 vrf_master.vrf_disable_hook = disable;
488 vrf_master.vrf_delete_hook = delete;
ecbc5a37 489 vrf_master.vrf_update_name_hook = update;
d62a17ae 490
491 /* The default VRF always exists. */
eb6934d5 492 default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME);
d62a17ae 493 if (!default_vrf) {
450971aa 494 flog_err(EC_LIB_VRF_START,
1c50c1c0 495 "vrf_init: failed to create the default VRF!");
d62a17ae 496 exit(1);
497 }
20c87e98
TC
498 if (vrf_is_backend_netns()) {
499 struct ns *ns;
500
fdafe17e 501 strlcpy(default_vrf->data.l.netns_name,
eb6934d5 502 VRF_DEFAULT_NAME, NS_NAMSIZ);
20c87e98 503 ns = ns_lookup(ns_get_default_id());
c3568c4d
TC
504 ns->vrf_ctxt = default_vrf;
505 default_vrf->ns_ctxt = ns;
20c87e98 506 }
d62a17ae 507
508 /* Enable the default VRF. */
509 if (!vrf_enable(default_vrf)) {
450971aa 510 flog_err(EC_LIB_VRF_START,
1c50c1c0 511 "vrf_init: failed to enable the default VRF!");
d62a17ae 512 exit(1);
513 }
514
515 cmd_variable_handler_register(vrf_var_handlers);
b72ede27
FL
516}
517
518/* Terminate VRF module. */
d62a17ae 519void vrf_terminate(void)
b72ede27 520{
d62a17ae 521 struct vrf *vrf;
b72ede27 522
d62a17ae 523 if (debug_vrf)
524 zlog_debug("%s: Shutting down vrf subsystem",
525 __PRETTY_FUNCTION__);
19dc275e 526
55cd0f61
DS
527 while (!RB_EMPTY(vrf_id_head, &vrfs_by_id)) {
528 vrf = RB_ROOT(vrf_id_head, &vrfs_by_id);
529
65c3a7c4 530 /* Clear configured flag and invoke delete. */
531 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
d62a17ae 532 vrf_delete(vrf);
65c3a7c4 533 }
55cd0f61
DS
534
535 while (!RB_EMPTY(vrf_name_head, &vrfs_by_name)) {
536 vrf = RB_ROOT(vrf_name_head, &vrfs_by_name);
537
65c3a7c4 538 /* Clear configured flag and invoke delete. */
539 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
d62a17ae 540 vrf_delete(vrf);
65c3a7c4 541 }
b72ede27
FL
542}
543
9dff1132
LB
544static int vrf_default_accepts_vrf(int type)
545{
546 const char *fname = NULL;
547 char buf[32] = {0x0};
548 int ret = 0;
549 FILE *fd = NULL;
550
551 /*
552 * TCP & UDP services running in the default VRF context (ie., not bound
553 * to any VRF device) can work across all VRF domains by enabling the
554 * tcp_l3mdev_accept and udp_l3mdev_accept sysctl options:
555 * sysctl -w net.ipv4.tcp_l3mdev_accept=1
556 * sysctl -w net.ipv4.udp_l3mdev_accept=1
557 */
558 if (type == SOCK_STREAM)
559 fname = "/proc/sys/net/ipv4/tcp_l3mdev_accept";
560 else if (type == SOCK_DGRAM)
561 fname = "/proc/sys/net/ipv4/udp_l3mdev_accept";
562 else
563 return ret;
564 fd = fopen(fname, "r");
565 if (fd == NULL)
566 return ret;
567 fgets(buf, 32, fd);
568 ret = atoi(buf);
569 fclose(fd);
570 return ret;
571}
572
e5bf3e1e 573/* Create a socket for the VRF. */
0f4977c6
PG
574int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id,
575 char *interfacename)
e5bf3e1e 576{
2e0d2b3d 577 int ret, save_errno, ret2;
e5bf3e1e 578
2e0d2b3d
PG
579 ret = vrf_switch_to_netns(vrf_id);
580 if (ret < 0)
450971aa 581 flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)",
09c866e3 582 __func__, vrf_id, safe_strerror(errno));
b66d022e 583
9dff1132
LB
584 if (ret > 0 && interfacename && vrf_default_accepts_vrf(type)) {
585 zlog_err("VRF socket not used since net.ipv4.%s_l3mdev_accept != 0",
586 (type == SOCK_STREAM ? "tcp" : "udp"));
587 errno = EEXIST; /* not sure if this is the best error... */
588 return -2;
589 }
b66d022e 590
d62a17ae 591 ret = socket(domain, type, protocol);
2e0d2b3d
PG
592 save_errno = errno;
593 ret2 = vrf_switchback_to_initial();
594 if (ret2 < 0)
450971aa 595 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
596 "%s: Can't switchback from VRF %u (%s)", __func__,
597 vrf_id, safe_strerror(errno));
2e0d2b3d 598 errno = save_errno;
0f4977c6
PG
599 if (ret <= 0)
600 return ret;
601 ret2 = vrf_bind(vrf_id, ret, interfacename);
602 if (ret2 < 0) {
603 close(ret);
604 ret = ret2;
605 }
d62a17ae 606 return ret;
e5bf3e1e
FL
607}
608
78dd30b2
PG
609int vrf_is_backend_netns(void)
610{
611 return (vrf_backend == VRF_BACKEND_NETNS);
612}
613
614int vrf_get_backend(void)
615{
616 return vrf_backend;
617}
618
619void vrf_configure_backend(int vrf_backend_netns)
620{
621 vrf_backend = vrf_backend_netns;
622}
623
03aff2d8
PG
624int vrf_handler_create(struct vty *vty, const char *vrfname,
625 struct vrf **vrf)
f30c50b9 626{
d62a17ae 627 struct vrf *vrfp;
f30c50b9 628
d62a17ae 629 if (strlen(vrfname) > VRF_NAMSIZ) {
697d3ec7
PG
630 if (vty)
631 vty_out(vty,
996c9314
LB
632 "%% VRF name %s invalid: length exceeds %d bytes\n",
633 vrfname, VRF_NAMSIZ);
697d3ec7 634 else
0351a28f 635 flog_warn(
450971aa 636 EC_LIB_VRF_LENGTH,
996c9314
LB
637 "%% VRF name %s invalid: length exceeds %d bytes\n",
638 vrfname, VRF_NAMSIZ);
d62a17ae 639 return CMD_WARNING_CONFIG_FAILED;
640 }
f30c50b9 641
d62a17ae 642 vrfp = vrf_get(VRF_UNKNOWN, vrfname);
f30c50b9 643
697d3ec7
PG
644 if (vty)
645 VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
f30c50b9 646
697d3ec7
PG
647 if (vrf)
648 *vrf = vrfp;
d62a17ae 649 return CMD_SUCCESS;
f30c50b9
RW
650}
651
996c9314 652int vrf_netns_handler_create(struct vty *vty, struct vrf *vrf, char *pathname,
03aff2d8 653 ns_id_t ns_id, ns_id_t internal_ns_id)
e26aedbe
PG
654{
655 struct ns *ns = NULL;
656
657 if (!vrf)
658 return CMD_WARNING_CONFIG_FAILED;
659 if (vrf->vrf_id != VRF_UNKNOWN && vrf->ns_ctxt == NULL) {
660 if (vty)
661 vty_out(vty,
662 "VRF %u is already configured with VRF %s\n",
663 vrf->vrf_id, vrf->name);
664 else
0351a28f 665 zlog_info("VRF %u is already configured with VRF %s\n",
e26aedbe
PG
666 vrf->vrf_id, vrf->name);
667 return CMD_WARNING_CONFIG_FAILED;
668 }
669 if (vrf->ns_ctxt != NULL) {
996c9314 670 ns = (struct ns *)vrf->ns_ctxt;
2e1cc436 671 if (!strcmp(ns->name, pathname)) {
e26aedbe
PG
672 if (vty)
673 vty_out(vty,
996c9314
LB
674 "VRF %u already configured with NETNS %s\n",
675 vrf->vrf_id, ns->name);
e26aedbe 676 else
0351a28f 677 zlog_info(
ade6974d
QY
678 "VRF %u already configured with NETNS %s",
679 vrf->vrf_id, ns->name);
e26aedbe
PG
680 return CMD_WARNING_CONFIG_FAILED;
681 }
682 }
683 ns = ns_lookup_name(pathname);
684 if (ns && ns->vrf_ctxt) {
685 struct vrf *vrf2 = (struct vrf *)ns->vrf_ctxt;
686
687 if (vrf2 == vrf)
688 return CMD_SUCCESS;
689 if (vty)
996c9314
LB
690 vty_out(vty,
691 "NS %s is already configured"
e26aedbe 692 " with VRF %u(%s)\n",
996c9314 693 ns->name, vrf2->vrf_id, vrf2->name);
e26aedbe 694 else
0351a28f 695 zlog_info("NS %s is already configured with VRF %u(%s)",
e26aedbe
PG
696 ns->name, vrf2->vrf_id, vrf2->name);
697 return CMD_WARNING_CONFIG_FAILED;
698 }
699 ns = ns_get_created(ns, pathname, ns_id);
03aff2d8 700 ns->internal_ns_id = internal_ns_id;
e26aedbe
PG
701 ns->vrf_ctxt = (void *)vrf;
702 vrf->ns_ctxt = (void *)ns;
703 /* update VRF netns NAME */
2e1cc436 704 strlcpy(vrf->data.l.netns_name, basename(pathname), NS_NAMSIZ);
e26aedbe
PG
705
706 if (!ns_enable(ns, vrf_update_vrf_id)) {
707 if (vty)
708 vty_out(vty, "Can not associate NS %u with NETNS %s\n",
996c9314 709 ns->ns_id, ns->name);
e26aedbe 710 else
0351a28f 711 zlog_info("Can not associate NS %u with NETNS %s",
e26aedbe
PG
712 ns->ns_id, ns->name);
713 return CMD_WARNING_CONFIG_FAILED;
714 }
715
716 return CMD_SUCCESS;
717}
718
3d4c0b49 719int vrf_is_mapped_on_netns(struct vrf *vrf)
ce1be369 720{
ce1be369
PG
721 if (!vrf || vrf->data.l.netns_name[0] == '\0')
722 return 0;
ce1be369
PG
723 return 1;
724}
725
697d3ec7 726/* vrf CLI commands */
16d6ea59
QY
727DEFUN_NOSH(vrf_exit,
728 vrf_exit_cmd,
729 "exit-vrf",
730 "Exit current mode and down to previous mode\n")
731{
732 /* We have to set vrf context to default vrf */
733 VTY_PUSH_CONTEXT(VRF_NODE, vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME));
734 vty->node = CONFIG_NODE;
735 return CMD_SUCCESS;
736}
737
697d3ec7
PG
738DEFUN_NOSH (vrf,
739 vrf_cmd,
740 "vrf NAME",
741 "Select a VRF to configure\n"
742 "VRF's name\n")
743{
744 int idx_name = 1;
745 const char *vrfname = argv[idx_name]->arg;
746
747 return vrf_handler_create(vty, vrfname, NULL);
748}
749
34c46274
RW
750DEFUN (no_vrf,
751 no_vrf_cmd,
752 "no vrf NAME",
753 NO_STR
754 "Delete a pseudo VRF's configuration\n"
755 "VRF's name\n")
f30c50b9 756{
d62a17ae 757 const char *vrfname = argv[2]->arg;
53dc2b05 758
d62a17ae 759 struct vrf *vrfp;
f30c50b9 760
d62a17ae 761 vrfp = vrf_lookup_by_name(vrfname);
f30c50b9 762
d62a17ae 763 if (vrfp == NULL) {
764 vty_out(vty, "%% VRF %s does not exist\n", vrfname);
765 return CMD_WARNING_CONFIG_FAILED;
766 }
f30c50b9 767
d62a17ae 768 if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
769 vty_out(vty, "%% Only inactive VRFs can be deleted\n");
770 return CMD_WARNING_CONFIG_FAILED;
771 }
f30c50b9 772
84915b0a 773 /* Clear configured flag and invoke delete. */
774 UNSET_FLAG(vrfp->status, VRF_CONFIGURED);
d62a17ae 775 vrf_delete(vrfp);
f30c50b9 776
d62a17ae 777 return CMD_SUCCESS;
f30c50b9
RW
778}
779
53dc2b05 780
d62a17ae 781struct cmd_node vrf_node = {VRF_NODE, "%s(config-vrf)# ", 1};
7ddcfca4 782
34c46274 783DEFUN_NOSH (vrf_netns,
4a541e8c
PG
784 vrf_netns_cmd,
785 "netns NAME",
786 "Attach VRF to a Namespace\n"
787 "The file name in " NS_RUN_DIR ", or a full pathname\n")
e26aedbe 788{
3bc34908 789 int idx_name = 1, ret;
e26aedbe
PG
790 char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg);
791
792 VTY_DECLVAR_CONTEXT(vrf, vrf);
793
794 if (!pathname)
795 return CMD_WARNING_CONFIG_FAILED;
3bc34908 796
6bb30c2c
DL
797 frr_elevate_privs(vrf_daemon_privs) {
798 ret = vrf_netns_handler_create(vty, vrf, pathname,
799 NS_UNKNOWN, NS_UNKNOWN);
800 }
3bc34908 801 return ret;
e26aedbe
PG
802}
803
34c46274 804DEFUN_NOSH (no_vrf_netns,
e26aedbe
PG
805 no_vrf_netns_cmd,
806 "no netns [NAME]",
807 NO_STR
808 "Detach VRF from a Namespace\n"
809 "The file name in " NS_RUN_DIR ", or a full pathname\n")
810{
811 struct ns *ns = NULL;
812
813 VTY_DECLVAR_CONTEXT(vrf, vrf);
814
815 if (!vrf_is_backend_netns()) {
816 vty_out(vty, "VRF backend is not Netns. Aborting\n");
817 return CMD_WARNING_CONFIG_FAILED;
818 }
819 if (!vrf->ns_ctxt) {
820 vty_out(vty, "VRF %s(%u) is not configured with NetNS\n",
821 vrf->name, vrf->vrf_id);
822 return CMD_WARNING_CONFIG_FAILED;
823 }
824
825 ns = (struct ns *)vrf->ns_ctxt;
826
827 ns->vrf_ctxt = NULL;
828 vrf_disable(vrf);
829 /* vrf ID from VRF is necessary for Zebra
830 * so that propagate to other clients is done
831 */
832 ns_delete(ns);
833 vrf->ns_ctxt = NULL;
834 return CMD_SUCCESS;
835}
836
19dc275e
DS
837/*
838 * Debug CLI for vrf's
839 */
840DEFUN (vrf_debug,
841 vrf_debug_cmd,
842 "debug vrf",
843 DEBUG_STR
844 "VRF Debugging\n")
845{
d62a17ae 846 debug_vrf = 1;
19dc275e 847
d62a17ae 848 return CMD_SUCCESS;
19dc275e
DS
849}
850
851DEFUN (no_vrf_debug,
852 no_vrf_debug_cmd,
853 "no debug vrf",
854 NO_STR
855 DEBUG_STR
856 "VRF Debugging\n")
857{
d62a17ae 858 debug_vrf = 0;
19dc275e 859
d62a17ae 860 return CMD_SUCCESS;
19dc275e
DS
861}
862
d62a17ae 863static int vrf_write_host(struct vty *vty)
19dc275e 864{
d62a17ae 865 if (debug_vrf)
866 vty_out(vty, "debug vrf\n");
19dc275e 867
d62a17ae 868 return 1;
19dc275e
DS
869}
870
d62a17ae 871static struct cmd_node vrf_debug_node = {VRF_DEBUG_NODE, "", 1};
19dc275e 872
d62a17ae 873void vrf_install_commands(void)
19dc275e 874{
d62a17ae 875 install_node(&vrf_debug_node, vrf_write_host);
19dc275e 876
d62a17ae 877 install_element(CONFIG_NODE, &vrf_debug_cmd);
878 install_element(ENABLE_NODE, &vrf_debug_cmd);
879 install_element(CONFIG_NODE, &no_vrf_debug_cmd);
880 install_element(ENABLE_NODE, &no_vrf_debug_cmd);
19dc275e 881}
53dc2b05 882
3bc34908
PG
883void vrf_cmd_init(int (*writefunc)(struct vty *vty),
884 struct zebra_privs_t *daemon_privs)
7ddcfca4 885{
d62a17ae 886 install_element(CONFIG_NODE, &vrf_cmd);
887 install_element(CONFIG_NODE, &no_vrf_cmd);
888 install_node(&vrf_node, writefunc);
889 install_default(VRF_NODE);
16d6ea59 890 install_element(VRF_NODE, &vrf_exit_cmd);
e26aedbe
PG
891 if (vrf_is_backend_netns() && ns_have_netns()) {
892 /* Install NS commands. */
3bc34908 893 vrf_daemon_privs = daemon_privs;
e26aedbe
PG
894 install_element(VRF_NODE, &vrf_netns_cmd);
895 install_element(VRF_NODE, &no_vrf_netns_cmd);
896 }
19dc275e 897}
ec31f30d 898
4fe52e76 899void vrf_set_default_name(const char *default_name, bool force)
ec31f30d 900{
c200f5e1 901 struct vrf *def_vrf;
dd114702 902 struct vrf *vrf_with_default_name = NULL;
4fe52e76 903 static bool def_vrf_forced;
ec31f30d 904
c200f5e1
PG
905 def_vrf = vrf_lookup_by_id(VRF_DEFAULT);
906 assert(default_name);
4fe52e76
PG
907 if (def_vrf && !force && def_vrf_forced) {
908 zlog_debug("VRF: %s, avoid changing name to %s, previously forced (%u)",
909 def_vrf->name, default_name,
910 def_vrf->vrf_id);
911 return;
912 }
c200f5e1
PG
913 if (vrf_with_default_name && vrf_with_default_name != def_vrf) {
914 /* vrf name already used by an other VRF */
915 zlog_debug("VRF: %s, avoid changing name to %s, same name exists (%u)",
916 vrf_with_default_name->name, default_name,
917 vrf_with_default_name->vrf_id);
918 return;
919 }
920 snprintf(vrf_default_name, VRF_NAMSIZ, "%s", default_name);
921 if (def_vrf) {
4fe52e76
PG
922 if (force)
923 def_vrf_forced = true;
c200f5e1
PG
924 RB_REMOVE(vrf_name_head, &vrfs_by_name, def_vrf);
925 strlcpy(def_vrf->data.l.netns_name,
926 vrf_default_name, NS_NAMSIZ);
927 strlcpy(def_vrf->name, vrf_default_name, sizeof(def_vrf->name));
928 RB_INSERT(vrf_name_head, &vrfs_by_name, def_vrf);
ecbc5a37
PG
929 if (vrf_master.vrf_update_name_hook)
930 (*vrf_master.vrf_update_name_hook)(def_vrf);
c200f5e1
PG
931 }
932}
933
934const char *vrf_get_default_name(void)
935{
936 return vrf_default_name;
937}
938
939vrf_id_t vrf_get_default_id(void)
940{
03aff2d8
PG
941 /* backend netns is only known by zebra
942 * for other daemons, we return VRF_DEFAULT_INTERNAL
943 */
ec31f30d
PG
944 if (vrf_is_backend_netns())
945 return ns_get_default_id();
946 else
947 return VRF_DEFAULT_INTERNAL;
948}
2e0d2b3d 949
0f4977c6
PG
950int vrf_bind(vrf_id_t vrf_id, int fd, char *name)
951{
952 int ret = 0;
953
954 if (fd < 0 || name == NULL)
955 return fd;
3d4c0b49 956 if (vrf_is_mapped_on_netns(vrf_lookup_by_id(vrf_id)))
0f4977c6
PG
957 return fd;
958#ifdef SO_BINDTODEVICE
c9c70dd1 959 ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name)+1);
0f4977c6 960 if (ret < 0)
996c9314
LB
961 zlog_debug("bind to interface %s failed, errno=%d", name,
962 errno);
0f4977c6
PG
963#endif /* SO_BINDTODEVICE */
964 return ret;
965}
2e0d2b3d 966int vrf_getaddrinfo(const char *node, const char *service,
996c9314
LB
967 const struct addrinfo *hints, struct addrinfo **res,
968 vrf_id_t vrf_id)
2e0d2b3d
PG
969{
970 int ret, ret2, save_errno;
971
972 ret = vrf_switch_to_netns(vrf_id);
973 if (ret < 0)
450971aa 974 flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)",
09c866e3 975 __func__, vrf_id, safe_strerror(errno));
2e0d2b3d
PG
976 ret = getaddrinfo(node, service, hints, res);
977 save_errno = errno;
978 ret2 = vrf_switchback_to_initial();
979 if (ret2 < 0)
450971aa 980 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
981 "%s: Can't switchback from VRF %u (%s)", __func__,
982 vrf_id, safe_strerror(errno));
2e0d2b3d
PG
983 errno = save_errno;
984 return ret;
985}
986
516d7591
PG
987int vrf_ioctl(vrf_id_t vrf_id, int d, unsigned long request, char *params)
988{
989 int ret, saved_errno, rc;
990
991 ret = vrf_switch_to_netns(vrf_id);
992 if (ret < 0) {
450971aa 993 flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)",
09c866e3 994 __func__, vrf_id, safe_strerror(errno));
516d7591
PG
995 return 0;
996 }
997 rc = ioctl(d, request, params);
998 saved_errno = errno;
999 ret = vrf_switchback_to_initial();
1000 if (ret < 0)
450971aa 1001 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
1002 "%s: Can't switchback from VRF %u (%s)", __func__,
1003 vrf_id, safe_strerror(errno));
516d7591
PG
1004 errno = saved_errno;
1005 return rc;
1006}
1007
0f4977c6
PG
1008int vrf_sockunion_socket(const union sockunion *su, vrf_id_t vrf_id,
1009 char *interfacename)
2e0d2b3d
PG
1010{
1011 int ret, save_errno, ret2;
1012
1013 ret = vrf_switch_to_netns(vrf_id);
1014 if (ret < 0)
450971aa 1015 flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)",
09c866e3 1016 __func__, vrf_id, safe_strerror(errno));
2e0d2b3d
PG
1017 ret = sockunion_socket(su);
1018 save_errno = errno;
1019 ret2 = vrf_switchback_to_initial();
1020 if (ret2 < 0)
450971aa 1021 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
1022 "%s: Can't switchback from VRF %u (%s)", __func__,
1023 vrf_id, safe_strerror(errno));
2e0d2b3d 1024 errno = save_errno;
0f4977c6
PG
1025
1026 if (ret <= 0)
1027 return ret;
1028 ret2 = vrf_bind(vrf_id, ret, interfacename);
1029 if (ret2 < 0) {
1030 close(ret);
1031 ret = ret2;
1032 }
2e0d2b3d
PG
1033 return ret;
1034}
0b014ea6
PG
1035
1036vrf_id_t vrf_generate_id(void)
1037{
1038 static int vrf_id_local;
1039
1040 return ++vrf_id_local;
1041}