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