3 * Copyright (C) 2014 6WIND S.A.
5 * This file is part of GNU Zebra.
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
43 #define CLONE_NEWNET 0x40000000 /* New network namespace (lo, device, names sockets, etc) */
47 static inline int setns(int fd, int nstype)
50 return syscall(__NR_setns, fd, nstype);
56 #endif /* HAVE_SETNS */
58 #define VRF_RUN_DIR "/var/run/netns"
62 #define VRF_DEFAULT_NAME "/proc/self/ns/net"
63 static int have_netns_enabled = -1;
65 #else /* !HAVE_NETNS */
67 #define VRF_DEFAULT_NAME "Default-IP-Routing-Table"
69 #endif /* HAVE_NETNS */
71 static int have_netns(void)
74 if (have_netns_enabled < 0)
76 int fd = open (VRF_DEFAULT_NAME, O_RDONLY);
79 have_netns_enabled = 0;
82 have_netns_enabled = 1;
86 return have_netns_enabled;
94 /* Identifier, same as the vector index */
101 /* Master list of interfaces belonging to this VRF */
108 /* Holding VRF hooks */
111 int (*vrf_new_hook) (vrf_id_t, void **);
112 int (*vrf_delete_hook) (vrf_id_t, void **);
113 int (*vrf_enable_hook) (vrf_id_t, void **);
114 int (*vrf_disable_hook) (vrf_id_t, void **);
118 struct route_table *vrf_table = NULL;
120 static int vrf_is_enabled (struct vrf *vrf);
121 static int vrf_enable (struct vrf *vrf);
122 static void vrf_disable (struct vrf *vrf);
125 /* Build the table key */
127 vrf_build_key (vrf_id_t vrf_id, struct prefix *p)
130 p->prefixlen = IPV4_MAX_BITLEN;
131 p->u.prefix4.s_addr = vrf_id;
134 /* Get a VRF. If not found, create one. */
136 vrf_get (vrf_id_t vrf_id)
139 struct route_node *rn;
142 vrf_build_key (vrf_id, &p);
143 rn = route_node_get (vrf_table, &p);
146 vrf = (struct vrf *)rn->info;
147 route_unlock_node (rn); /* get */
151 vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf));
152 vrf->vrf_id = vrf_id;
156 /* Initialize interfaces. */
157 if_init (vrf_id, &vrf->iflist);
159 zlog_info ("VRF %u is created.", vrf_id);
161 if (vrf_master.vrf_new_hook)
162 (*vrf_master.vrf_new_hook) (vrf_id, &vrf->info);
167 /* Delete a VRF. This is called in vrf_terminate(). */
169 vrf_delete (struct vrf *vrf)
171 zlog_info ("VRF %u is to be deleted.", vrf->vrf_id);
175 if (vrf_master.vrf_delete_hook)
176 (*vrf_master.vrf_delete_hook) (vrf->vrf_id, &vrf->info);
178 if_terminate (vrf->vrf_id, &vrf->iflist);
181 XFREE (MTYPE_VRF_NAME, vrf->name);
183 XFREE (MTYPE_VRF, vrf);
186 /* Look up a VRF by identifier. */
188 vrf_lookup (vrf_id_t vrf_id)
191 struct route_node *rn;
192 struct vrf *vrf = NULL;
194 vrf_build_key (vrf_id, &p);
195 rn = route_node_lookup (vrf_table, &p);
198 vrf = (struct vrf *)rn->info;
199 route_unlock_node (rn); /* lookup */
205 * Check whether the VRF is enabled - that is, whether the VRF
206 * is ready to allocate resources. Currently there's only one
207 * type of resource: socket.
210 vrf_is_enabled (struct vrf *vrf)
213 return vrf && vrf->fd >= 0;
215 return vrf && vrf->fd == -2 && vrf->vrf_id == VRF_DEFAULT;
219 * Enable a VRF - that is, let the VRF be ready to use.
220 * The VRF_ENABLE_HOOK callback will be called to inform
221 * that they can allocate resources in this VRF.
223 * RETURN: 1 - enabled successfully; otherwise, 0.
226 vrf_enable (struct vrf *vrf)
229 if (!vrf_is_enabled (vrf))
232 vrf->fd = open (vrf->name, O_RDONLY);
234 vrf->fd = -2; /* Remember that vrf_enable_hook has been called */
238 if (!vrf_is_enabled (vrf))
240 zlog_err ("Can not enable VRF %u: %s!",
241 vrf->vrf_id, safe_strerror (errno));
246 zlog_info ("VRF %u is associated with NETNS %s.",
247 vrf->vrf_id, vrf->name);
249 zlog_info ("VRF %u is enabled.", vrf->vrf_id);
250 if (vrf_master.vrf_enable_hook)
251 (*vrf_master.vrf_enable_hook) (vrf->vrf_id, &vrf->info);
258 * Disable a VRF - that is, let the VRF be unusable.
259 * The VRF_DELETE_HOOK callback will be called to inform
260 * that they must release the resources in the VRF.
263 vrf_disable (struct vrf *vrf)
265 if (vrf_is_enabled (vrf))
267 zlog_info ("VRF %u is to be disabled.", vrf->vrf_id);
269 if (vrf_master.vrf_disable_hook)
270 (*vrf_master.vrf_disable_hook) (vrf->vrf_id, &vrf->info);
280 /* Add a VRF hook. Please add hooks before calling vrf_init(). */
282 vrf_add_hook (int type, int (*func)(vrf_id_t, void **))
286 vrf_master.vrf_new_hook = func;
288 case VRF_DELETE_HOOK:
289 vrf_master.vrf_delete_hook = func;
291 case VRF_ENABLE_HOOK:
292 vrf_master.vrf_enable_hook = func;
294 case VRF_DISABLE_HOOK:
295 vrf_master.vrf_disable_hook = func;
302 /* Return the iterator of the first VRF. */
306 struct route_node *rn;
308 for (rn = route_top (vrf_table); rn; rn = route_next (rn))
311 route_unlock_node (rn); /* top/next */
312 return (vrf_iter_t)rn;
314 return VRF_ITER_INVALID;
317 /* Return the next VRF iterator to the given iterator. */
319 vrf_next (vrf_iter_t iter)
321 struct route_node *rn = NULL;
323 /* Lock it first because route_next() will unlock it. */
324 if (iter != VRF_ITER_INVALID)
325 rn = route_next (route_lock_node ((struct route_node *)iter));
327 for (; rn; rn = route_next (rn))
330 route_unlock_node (rn); /* next */
331 return (vrf_iter_t)rn;
333 return VRF_ITER_INVALID;
336 /* Return the VRF iterator of the given VRF ID. If it does not exist,
337 * the iterator of the next existing VRF is returned. */
339 vrf_iterator (vrf_id_t vrf_id)
342 struct route_node *rn;
344 vrf_build_key (vrf_id, &p);
345 rn = route_node_get (vrf_table, &p);
348 /* OK, the VRF exists. */
349 route_unlock_node (rn); /* get */
350 return (vrf_iter_t)rn;
353 /* Find the next VRF. */
354 for (rn = route_next (rn); rn; rn = route_next (rn))
357 route_unlock_node (rn); /* next */
358 return (vrf_iter_t)rn;
361 return VRF_ITER_INVALID;
364 /* Obtain the VRF ID from the given VRF iterator. */
366 vrf_iter2id (vrf_iter_t iter)
368 struct route_node *rn = (struct route_node *) iter;
369 return (rn && rn->info) ? ((struct vrf *)rn->info)->vrf_id : VRF_DEFAULT;
372 /* Obtain the data pointer from the given VRF iterator. */
374 vrf_iter2info (vrf_iter_t iter)
376 struct route_node *rn = (struct route_node *) iter;
377 return (rn && rn->info) ? ((struct vrf *)rn->info)->info : NULL;
380 /* Obtain the interface list from the given VRF iterator. */
382 vrf_iter2iflist (vrf_iter_t iter)
384 struct route_node *rn = (struct route_node *) iter;
385 return (rn && rn->info) ? ((struct vrf *)rn->info)->iflist : NULL;
388 /* Get the data pointer of the specified VRF. If not found, create one. */
390 vrf_info_get (vrf_id_t vrf_id)
392 struct vrf *vrf = vrf_get (vrf_id);
396 /* Look up the data pointer of the specified VRF. */
398 vrf_info_lookup (vrf_id_t vrf_id)
400 struct vrf *vrf = vrf_lookup (vrf_id);
401 return vrf ? vrf->info : NULL;
404 /* Look up the interface list in a VRF. */
406 vrf_iflist (vrf_id_t vrf_id)
408 struct vrf * vrf = vrf_lookup (vrf_id);
409 return vrf ? vrf->iflist : NULL;
412 /* Get the interface list of the specified VRF. Create one if not find. */
414 vrf_iflist_get (vrf_id_t vrf_id)
416 struct vrf * vrf = vrf_get (vrf_id);
424 #define VRF_BITMAP_NUM_OF_GROUPS 8
425 #define VRF_BITMAP_NUM_OF_BITS_IN_GROUP \
426 (UINT16_MAX / VRF_BITMAP_NUM_OF_GROUPS)
427 #define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
428 (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
430 #define VRF_BITMAP_GROUP(_id) \
431 ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
432 #define VRF_BITMAP_BIT_OFFSET(_id) \
433 ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
435 #define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) \
436 ((_bit_offset) / CHAR_BIT)
437 #define VRF_BITMAP_FLAG(_bit_offset) \
438 (((u_char)1) << ((_bit_offset) % CHAR_BIT))
442 u_char *groups[VRF_BITMAP_NUM_OF_GROUPS];
446 vrf_bitmap_init (void)
448 return (vrf_bitmap_t) XCALLOC (MTYPE_VRF_BITMAP, sizeof (struct vrf_bitmap));
452 vrf_bitmap_free (vrf_bitmap_t bmap)
454 struct vrf_bitmap *bm = (struct vrf_bitmap *) bmap;
457 if (bmap == VRF_BITMAP_NULL)
460 for (i = 0; i < VRF_BITMAP_NUM_OF_GROUPS; i++)
462 XFREE (MTYPE_VRF_BITMAP, bm->groups[i]);
464 XFREE (MTYPE_VRF_BITMAP, bm);
468 vrf_bitmap_set (vrf_bitmap_t bmap, vrf_id_t vrf_id)
470 struct vrf_bitmap *bm = (struct vrf_bitmap *) bmap;
471 u_char group = VRF_BITMAP_GROUP (vrf_id);
472 u_char offset = VRF_BITMAP_BIT_OFFSET (vrf_id);
474 if (bmap == VRF_BITMAP_NULL)
477 if (bm->groups[group] == NULL)
478 bm->groups[group] = XCALLOC (MTYPE_VRF_BITMAP,
479 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP);
481 SET_FLAG (bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP (offset)],
482 VRF_BITMAP_FLAG (offset));
486 vrf_bitmap_unset (vrf_bitmap_t bmap, vrf_id_t vrf_id)
488 struct vrf_bitmap *bm = (struct vrf_bitmap *) bmap;
489 u_char group = VRF_BITMAP_GROUP (vrf_id);
490 u_char offset = VRF_BITMAP_BIT_OFFSET (vrf_id);
492 if (bmap == VRF_BITMAP_NULL || bm->groups[group] == NULL)
495 UNSET_FLAG (bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP (offset)],
496 VRF_BITMAP_FLAG (offset));
500 vrf_bitmap_check (vrf_bitmap_t bmap, vrf_id_t vrf_id)
502 struct vrf_bitmap *bm = (struct vrf_bitmap *) bmap;
503 u_char group = VRF_BITMAP_GROUP (vrf_id);
504 u_char offset = VRF_BITMAP_BIT_OFFSET (vrf_id);
506 if (bmap == VRF_BITMAP_NULL || bm->groups[group] == NULL)
509 return CHECK_FLAG (bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP (offset)],
510 VRF_BITMAP_FLAG (offset)) ? 1 : 0;
514 * VRF realization with NETNS
518 vrf_netns_pathname (struct vty *vty, const char *name)
520 static char pathname[PATH_MAX];
523 if (name[0] == '/') /* absolute pathname */
524 result = realpath (name, pathname);
525 else /* relevant pathname */
527 char tmp_name[PATH_MAX];
528 snprintf (tmp_name, PATH_MAX, "%s/%s", VRF_RUN_DIR, name);
529 result = realpath (tmp_name, pathname);
534 vty_out (vty, "Invalid pathname: %s%s", safe_strerror (errno),
543 "vrf <1-65535> netns NAME",
545 "Specify the VRF identifier\n"
546 "Associate with a NETNS\n"
547 "The file name in " VRF_RUN_DIR ", or a full pathname\n")
549 vrf_id_t vrf_id = VRF_DEFAULT;
550 struct vrf *vrf = NULL;
551 char *pathname = vrf_netns_pathname (vty, argv[1]);
556 VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]);
557 vrf = vrf_get (vrf_id);
559 if (vrf->name && strcmp (vrf->name, pathname) != 0)
561 vty_out (vty, "VRF %u is already configured with NETNS %s%s",
562 vrf->vrf_id, vrf->name, VTY_NEWLINE);
567 vrf->name = XSTRDUP (MTYPE_VRF_NAME, pathname);
569 if (!vrf_enable (vrf))
571 vty_out (vty, "Can not associate VRF %u with NETNS %s%s",
572 vrf->vrf_id, vrf->name, VTY_NEWLINE);
581 "no vrf <1-65535> netns NAME",
584 "Specify the VRF identifier\n"
585 "Associate with a NETNS\n"
586 "The file name in " VRF_RUN_DIR ", or a full pathname\n")
588 vrf_id_t vrf_id = VRF_DEFAULT;
589 struct vrf *vrf = NULL;
590 char *pathname = vrf_netns_pathname (vty, argv[1]);
595 VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]);
596 vrf = vrf_lookup (vrf_id);
600 vty_out (vty, "VRF %u is not found%s", vrf_id, VTY_NEWLINE);
604 if (vrf->name && strcmp (vrf->name, pathname) != 0)
606 vty_out (vty, "Incorrect NETNS file name%s", VTY_NEWLINE);
614 XFREE (MTYPE_VRF_NAME, vrf->name);
622 static struct cmd_node vrf_node =
625 "", /* VRF node has no interface. */
629 /* VRF configuration write function. */
631 vrf_config_write (struct vty *vty)
633 struct route_node *rn;
637 for (rn = route_top (vrf_table); rn; rn = route_next (rn))
638 if ((vrf = rn->info) != NULL &&
639 vrf->vrf_id != VRF_DEFAULT && vrf->name)
641 vty_out (vty, "vrf %u netns %s%s", vrf->vrf_id, vrf->name, VTY_NEWLINE);
648 /* Initialize VRF module. */
652 struct vrf *default_vrf;
654 /* Allocate VRF table. */
655 vrf_table = route_table_init ();
657 /* The default VRF always exists. */
658 default_vrf = vrf_get (VRF_DEFAULT);
661 zlog_err ("vrf_init: failed to create the default VRF!");
665 /* Set the default VRF name. */
666 default_vrf->name = XSTRDUP (MTYPE_VRF_NAME, VRF_DEFAULT_NAME);
668 /* Enable the default VRF. */
669 if (!vrf_enable (default_vrf))
671 zlog_err ("vrf_init: failed to enable the default VRF!");
677 /* Install VRF commands. */
678 install_node (&vrf_node, vrf_config_write);
679 install_element (CONFIG_NODE, &vrf_netns_cmd);
680 install_element (CONFIG_NODE, &no_vrf_netns_cmd);
684 /* Terminate VRF module. */
688 struct route_node *rn;
691 for (rn = route_top (vrf_table); rn; rn = route_next (rn))
692 if ((vrf = rn->info) != NULL)
695 route_table_finish (vrf_table);
699 /* Create a socket for the VRF. */
701 vrf_socket (int domain, int type, int protocol, vrf_id_t vrf_id)
703 struct vrf *vrf = vrf_lookup (vrf_id);
706 if (!vrf_is_enabled (vrf))
714 ret = (vrf_id != VRF_DEFAULT) ? setns (vrf->fd, CLONE_NEWNET) : 0;
717 ret = socket (domain, type, protocol);
718 if (vrf_id != VRF_DEFAULT)
719 setns (vrf_lookup (VRF_DEFAULT)->fd, CLONE_NEWNET);
723 ret = socket (domain, type, protocol);