plugins types FEATURE ipv4/v6-prefix LYB value support
diff --git a/src/common.c b/src/common.c
index 9c0d15a..25e19f1 100644
--- a/src/common.c
+++ b/src/common.c
@@ -19,6 +19,7 @@
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
+#include <inttypes.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -66,6 +67,44 @@
     }
 }
 
+#define LY_OVERFLOW_ADD(MAX, X, Y) ((X > MAX - Y) ? 1 : 0)
+
+#define LY_OVERFLOW_MUL(MAX, X, Y) ((X > MAX / Y) ? 1 : 0)
+
+LY_ERR
+ly_strntou8(const char *nptr, size_t len, uint8_t *ret)
+{
+    uint8_t num = 0, dig, dec_pow;
+
+    if (len > 3) {
+        /* overflow for sure */
+        return LY_EDENIED;
+    }
+
+    dec_pow = 1;
+    for ( ; len && isdigit(nptr[len - 1]); --len) {
+        dig = nptr[len - 1] - 48;
+
+        if (LY_OVERFLOW_MUL(UINT8_MAX, dig, dec_pow)) {
+            return LY_EDENIED;
+        }
+        dig *= dec_pow;
+
+        if (LY_OVERFLOW_ADD(UINT8_MAX, num, dig)) {
+            return LY_EDENIED;
+        }
+        num += dig;
+
+        dec_pow *= 10;
+    }
+
+    if (len) {
+        return LY_EVALID;
+    }
+    *ret = num;
+    return LY_SUCCESS;
+}
+
 uint32_t
 ly_value_prefix_next(const char *str_begin, const char *str_end, ly_bool *is_prefix, const char **str_next)
 {
diff --git a/src/common.h b/src/common.h
index 6e174c7..7a08f97 100644
--- a/src/common.h
+++ b/src/common.h
@@ -385,6 +385,19 @@
 int ly_strncmp(const char *refstr, const char *str, size_t str_len);
 
 /**
+ * @brief Similar functionality to strtoul() except number length in the string
+ * must be specified and the whole number must be parsed for success.
+ *
+ * @param[in] nptr Number string.
+ * @param[in] len Number string length starting at @p nptr.
+ * @param[out] ret Parsed number.
+ * @return LY_EDENIED on overflow.
+ * @return LY_EVALID on encountering a non-digit character.
+ * @return LY_SUCCESS on success.
+ */
+LY_ERR ly_strntou8(const char *nptr, size_t len, uint8_t *ret);
+
+/**
  * @brief Similar to strlen(3) but accepts NULL and returns 0.
  *
  * @param[in] s String to examine.
diff --git a/src/plugins.c b/src/plugins.c
index 694bcc9..f0800ed 100644
--- a/src/plugins.c
+++ b/src/plugins.c
@@ -55,7 +55,8 @@
  * ietf-inet-types
  */
 extern const struct lyplg_type_record plugins_ip_address[];
-extern const struct lyplg_type_record plugins_ip_prefix[];
+extern const struct lyplg_type_record plugins_ipv4_prefix[];
+extern const struct lyplg_type_record plugins_ipv6_prefix[];
 
 /*
  * ietf-yang-types
@@ -420,7 +421,8 @@
 
     /* ietf-inet-types */
     LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ip_address), error);
-    LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ip_prefix), error);
+    LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv4_prefix), error);
+    LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv6_prefix), error);
 
     /* ietf-yang-types */
     LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_date_and_time), error);
diff --git a/src/plugins_types/ip_prefix.c b/src/plugins_types/ip_prefix.c
deleted file mode 100644
index d6f596c..0000000
--- a/src/plugins_types/ip_prefix.c
+++ /dev/null
@@ -1,307 +0,0 @@
-/**
- * @file ip_prefix.c
- * @author Michal Vasko <mvasko@cesnet.cz>
- * @brief ietf-inet-types ip-prefix type plugin.
- *
- * Copyright (c) 2019-2021 CESNET, z.s.p.o.
- *
- * This source code is licensed under BSD 3-Clause License (the "License").
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     https://opensource.org/licenses/BSD-3-Clause
- */
-
-#define _GNU_SOURCE /* asprintf, strdup */
-#include <sys/cdefs.h>
-
-#include "plugins_types.h"
-
-#include <arpa/inet.h>
-#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
-#include <netinet/in.h>
-#include <sys/socket.h>
-#endif
-#include <assert.h>
-#include <errno.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "libyang.h"
-
-#include "common.h"
-#include "compat.h"
-
-/**
- * @brief Canonize an ipv4-prefix value.
- *
- * @param[in] ipv4_prefix Prefix to canonize.
- * @param[out] canonical Canonical format of @p ipv4_prefix.
- * @param[out] err Error structure on error.
- * @return LY_ERR value.
- */
-static LY_ERR
-canonize_ipv4_prefix(const char *ipv4_prefix, char **canonical, struct ly_err_item **err)
-{
-    LY_ERR ret;
-    const char *pref_str;
-    char *ptr, *result = NULL;
-    uint32_t pref, addr_bin, i, mask;
-
-    *canonical = NULL;
-    *err = NULL;
-
-    pref_str = strchr(ipv4_prefix, '/');
-    if (!pref_str) {
-        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid IPv4 prefix \"%s\".", ipv4_prefix);
-        goto error;
-    }
-
-    /* learn prefix */
-    pref = strtoul(pref_str + 1, &ptr, 10);
-    if (ptr[0] || (pref > 32)) {
-        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid IPv4 prefix \"%s\".", ipv4_prefix);
-        goto error;
-    }
-
-    result = malloc(INET_ADDRSTRLEN + 3);
-    if (!result) {
-        ret = LY_EMEM;
-        goto error;
-    }
-
-    /* copy just the network prefix */
-    strncpy(result, ipv4_prefix, pref_str - ipv4_prefix);
-    result[pref_str - ipv4_prefix] = '\0';
-
-    /* convert it to binary form */
-    if (inet_pton(AF_INET, result, (void *)&addr_bin) != 1) {
-        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", result);
-        goto error;
-    }
-
-    /* zero host bits */
-    mask = 0;
-    for (i = 0; i < 32; ++i) {
-        mask <<= 1;
-        if (pref > i) {
-            mask |= 1;
-        }
-    }
-    mask = htonl(mask);
-    addr_bin &= mask;
-
-    /* convert back to string */
-    if (!inet_ntop(AF_INET, (void *)&addr_bin, result, INET_ADDRSTRLEN)) {
-        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address (%s).", strerror(errno));
-        goto error;
-    }
-
-    /* add the prefix */
-    strcat(result, pref_str);
-
-    *canonical = result;
-    return LY_SUCCESS;
-
-error:
-    free(result);
-    return ret;
-}
-
-/**
- * @brief Canonize an ipv6-prefix value.
- *
- * @param[in] ipv6_prefix Prefix to canonize.
- * @param[out] canonical Canonical format of @p ipv6_prefix.
- * @param[out] err Error structure on error.
- * @return LY_ERR value.
- */
-static LY_ERR
-canonize_ipv6_prefix(const char *ipv4_prefix, char **canonical, struct ly_err_item **err)
-{
-    LY_ERR ret;
-    const char *pref_str;
-    char *ptr, *result = NULL;
-    unsigned long int pref, i, j;
-
-    union {
-        struct in6_addr s;
-        uint32_t a[4];
-    } addr_bin;
-    uint32_t mask;
-
-    *canonical = NULL;
-    *err = NULL;
-
-    pref_str = strchr(ipv4_prefix, '/');
-    if (!pref_str) {
-        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid IPv6 prefix \"%s\".", ipv4_prefix);
-        goto error;
-    }
-
-    /* learn prefix */
-    pref = strtoul(pref_str + 1, &ptr, 10);
-    if (ptr[0] || (pref > 128)) {
-        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid IPv6 prefix \"%s\".", ipv4_prefix);
-        goto error;
-    }
-
-    result = malloc(INET6_ADDRSTRLEN + 4);
-    if (!result) {
-        ret = LY_EMEM;
-        goto error;
-    }
-
-    /* copy just the network prefix */
-    strncpy(result, ipv4_prefix, pref_str - ipv4_prefix);
-    result[pref_str - ipv4_prefix] = '\0';
-
-    /* convert it to binary form */
-    if (inet_pton(AF_INET6, result, (void *)&addr_bin.s) != 1) {
-        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address \"%s\".", result);
-        goto error;
-    }
-
-    /* zero host bits */
-    for (i = 0; i < 4; ++i) {
-        mask = 0;
-        for (j = 0; j < 32; ++j) {
-            mask <<= 1;
-            if (pref > (i * 32) + j) {
-                mask |= 1;
-            }
-        }
-        mask = htonl(mask);
-        addr_bin.a[i] &= mask;
-    }
-
-    /* convert back to string */
-    if (!inet_ntop(AF_INET6, (void *)&addr_bin.s, result, INET6_ADDRSTRLEN)) {
-        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address (%s).", strerror(errno));
-        goto error;
-    }
-
-    /* add the prefix */
-    strcat(result, pref_str);
-
-    *canonical = result;
-    return LY_SUCCESS;
-
-error:
-    free(result);
-    return ret;
-}
-
-/**
- * @brief Validate, canonize and store value of the ietf-inet-types ipv4-prefix type.
- * Implementation of the ::lyplg_type_store_clb.
- */
-static LY_ERR
-lyplg_type_store_ipv4_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
-        uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
-        struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
-{
-    LY_ERR ret = LY_SUCCESS;
-    char *canonical;
-
-    /* store as a string */
-    ret = lyplg_type_store_string(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node,
-            storage, unres, err);
-    LY_CHECK_RET(ret);
-
-    /* canonize */
-    ret = canonize_ipv4_prefix(storage->_canonical, &canonical, err);
-    LY_CHECK_GOTO(ret, cleanup);
-
-    if (strcmp(canonical, storage->_canonical)) {
-        /* some conversion took place, update the value */
-        lydict_remove(ctx, storage->_canonical);
-        storage->_canonical = NULL;
-        LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, canonical, &storage->_canonical), cleanup);
-    } else {
-        free(canonical);
-    }
-
-cleanup:
-    if (ret) {
-        type->plugin->free(ctx, storage);
-    }
-    return ret;
-}
-
-/**
- * @brief Validate, canonize and store value of the ietf-inet-types ipv6-prefix type.
- * Implementation of the ::lyplg_type_store_clb.
- */
-static LY_ERR
-lyplg_type_store_ipv6_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
-        uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
-        struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
-{
-    LY_ERR ret = LY_SUCCESS;
-    char *canonical;
-
-    /* store as a string */
-    ret = lyplg_type_store_string(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node,
-            storage, unres, err);
-    LY_CHECK_RET(ret);
-
-    /* canonize */
-    ret = canonize_ipv6_prefix(storage->_canonical, &canonical, err);
-    LY_CHECK_GOTO(ret, cleanup);
-
-    if (strcmp(canonical, storage->_canonical)) {
-        /* some conversion took place, update the value */
-        lydict_remove(ctx, storage->_canonical);
-        storage->_canonical = NULL;
-        LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, canonical, &storage->_canonical), cleanup);
-    } else {
-        free(canonical);
-    }
-
-cleanup:
-    if (ret) {
-        type->plugin->free(ctx, storage);
-    }
-    return ret;
-}
-
-/**
- * @brief Plugin information for ip-prefix type implementation.
- *
- * Note that external plugins are supposed to use:
- *
- *   LYPLG_TYPES = {
- */
-const struct lyplg_type_record plugins_ip_prefix[] = {
-    {
-        .module = "ietf-inet-types",
-        .revision = "2013-07-15",
-        .name = "ipv4-prefix",
-
-        .plugin.id = "libyang 2 - ipv4-prefix, version 1",
-        .plugin.store = lyplg_type_store_ipv4_prefix,
-        .plugin.validate = NULL,
-        .plugin.compare = lyplg_type_compare_simple,
-        .plugin.print = lyplg_type_print_simple,
-        .plugin.hash = lyplg_type_hash_simple,
-        .plugin.duplicate = lyplg_type_dup_simple,
-        .plugin.free = lyplg_type_free_simple
-    },
-    {
-        .module = "ietf-inet-types",
-        .revision = "2013-07-15",
-        .name = "ipv6-prefix",
-
-        .plugin.id = "libyang 2 - ipv6-prefix, version 1",
-        .plugin.store = lyplg_type_store_ipv6_prefix,
-        .plugin.validate = NULL,
-        .plugin.compare = lyplg_type_compare_simple,
-        .plugin.print = lyplg_type_print_simple,
-        .plugin.hash = lyplg_type_hash_simple,
-        .plugin.duplicate = lyplg_type_dup_simple,
-        .plugin.free = lyplg_type_free_simple
-    },
-    {0}
-};
diff --git a/src/plugins_types/ipv4_prefix.c b/src/plugins_types/ipv4_prefix.c
new file mode 100644
index 0000000..65ec68e
--- /dev/null
+++ b/src/plugins_types/ipv4_prefix.c
@@ -0,0 +1,358 @@
+/**
+ * @file ipv4_prefix.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief ietf-inet-types ipv4-prefix type plugin.
+ *
+ * Copyright (c) 2019-2021 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#define _GNU_SOURCE /* asprintf, strdup */
+#include <sys/cdefs.h>
+
+#include "plugins_types.h"
+
+#include <arpa/inet.h>
+#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
+#include <netinet/in.h>
+#include <sys/socket.h>
+#endif
+#include <assert.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libyang.h"
+
+#include "common.h"
+#include "compat.h"
+
+/**
+ * @page howtoDataLYB LYB Binary Format
+ * @subsection howtoDataLYBTypesIPv4Prefix ipv4-prefix (ietf-inet-types)
+ *
+ * | Size (B) | Mandatory | Type | Meaning |
+ * | :------  | :-------: | :--: | :-----: |
+ * | 4 | yes | `struct in_addr *` | IPv4 address in network-byte order |
+ * | 1 | yes | `uint8_t *` | prefix length up to 32 |
+ */
+
+/**
+ * @brief Stored value structure for ipv4-prefix
+ */
+struct lyd_value_ipv4_prefix {
+    struct in_addr addr;
+    uint8_t prefix;
+};
+
+static void lyplg_type_free_ipv4_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
+
+/**
+ * @brief Convert IP address with a prefix in string to a binary network-byte order value.
+ *
+ * @param[in] value String to convert.
+ * @param[in] value_len Length of @p value.
+ * @param[in,out] addr Allocated address value to fill.
+ * @param[out] prefix Prefix length.
+ * @param[out] err Error information on error.
+ * @return LY_ERR value.
+ */
+static LY_ERR
+ipv4prefix_str2ip(const char *value, size_t value_len, struct in_addr *addr, uint8_t *prefix, struct ly_err_item **err)
+{
+    LY_ERR ret = LY_SUCCESS;
+    const char *pref_str;
+    char *mask_str = NULL;
+
+    /* it passed the pattern validation */
+    pref_str = ly_strnchr(value, '/', value_len);
+    ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
+
+    /* get just the network prefix */
+    mask_str = strndup(value, pref_str - value);
+    LY_CHECK_ERR_GOTO(!mask_str, ret = LY_EMEM, cleanup);
+
+    /* convert it to netword-byte order */
+    if (inet_pton(AF_INET, mask_str, addr) != 1) {
+        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", mask_str);
+        goto cleanup;
+    }
+
+cleanup:
+    free(mask_str);
+    return ret;
+}
+
+/**
+ * @brief Zero host-portion of the IP address.
+ *
+ * @param[in,out] addr IP address.
+ * @param[in] prefix Prefix length.
+ */
+static void
+ipv4prefix_zero_host(struct in_addr *addr, uint8_t prefix)
+{
+    uint32_t i, mask;
+
+    /* zero host bits */
+    mask = 0;
+    for (i = 0; i < 32; ++i) {
+        mask <<= 1;
+        if (prefix > i) {
+            mask |= 1;
+        }
+    }
+    mask = htonl(mask);
+    addr->s_addr &= mask;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_store_clb for the ipv4-prefix ietf-inet-types type.
+ */
+static LY_ERR
+lyplg_type_store_ipv4_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
+        uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
+        const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
+        struct ly_err_item **err)
+{
+    LY_ERR ret = LY_SUCCESS;
+    struct lysc_type_str *type_str = (struct lysc_type_str *)type;
+    struct lyd_value_ipv4_prefix *val;
+
+    /* clear storage */
+    memset(storage, 0, sizeof *storage);
+
+    if (format == LY_VALUE_LYB) {
+        /* validation */
+        if (value_len != 5) {
+            ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-prefix value size %zu (expected 5).",
+                    value_len);
+            goto cleanup;
+        }
+        if (((uint8_t *)value)[4] > 32) {
+            ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-prefix prefix length %" PRIu8 ".",
+                    ((uint8_t *)value)[4]);
+            goto cleanup;
+        }
+
+        /* store/allocate value */
+        if (options & LYPLG_TYPE_STORE_DYNAMIC) {
+            val = (void *)value;
+            options &= ~LYPLG_TYPE_STORE_DYNAMIC;
+        } else {
+            val = malloc(sizeof *val);
+            LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
+            memcpy(val, value, value_len);
+        }
+
+        /* init storage */
+        storage->_canonical = NULL;
+        storage->ptr = val;
+        storage->realtype = type;
+
+        /* zero host */
+        ipv4prefix_zero_host(&val->addr, val->prefix);
+
+        /* success */
+        goto cleanup;
+    }
+
+    /* check hints */
+    ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
+    LY_CHECK_GOTO(ret, cleanup);
+
+    /* length restriction of the string */
+    if (type_str->length) {
+        /* value_len is in bytes, but we need number of characters here */
+        ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
+        LY_CHECK_GOTO(ret, cleanup);
+    }
+
+    /* pattern restrictions */
+    ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
+    LY_CHECK_GOTO(ret, cleanup);
+
+    /* allocate the value */
+    val = malloc(sizeof *val);
+    LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
+
+    /* init storage */
+    storage->_canonical = NULL;
+    storage->ptr = val;
+    storage->realtype = type;
+
+    /* get the mask in network-byte order */
+    ret = ipv4prefix_str2ip(value, value_len, &val->addr, &val->prefix, err);
+    LY_CHECK_GOTO(ret, cleanup);
+
+    /* zero host */
+    ipv4prefix_zero_host(&val->addr, val->prefix);
+
+    if (format == LY_VALUE_CANON) {
+        /* store canonical value */
+        if (options & LYPLG_TYPE_STORE_DYNAMIC) {
+            ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
+            options &= ~LYPLG_TYPE_STORE_DYNAMIC;
+            LY_CHECK_GOTO(ret, cleanup);
+        } else {
+            ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
+            LY_CHECK_GOTO(ret, cleanup);
+        }
+    }
+
+cleanup:
+    if (options & LYPLG_TYPE_STORE_DYNAMIC) {
+        free((void *)value);
+    }
+
+    if (ret) {
+        lyplg_type_free_ipv4_prefix(ctx, storage);
+    }
+    return ret;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_compare_clb for the ietf-inet-types ipv4-prefix type.
+ */
+static LY_ERR
+lyplg_type_compare_ipv4_prefix(const struct lyd_value *val1, const struct lyd_value *val2)
+{
+    struct lyd_value_ipv4_prefix *v1 = val1->ptr, *v2 = val2->ptr;
+
+    if (val1->realtype != val2->realtype) {
+        return LY_ENOT;
+    }
+
+    if (memcmp(v1, v2, sizeof *v1)) {
+        return LY_ENOT;
+    }
+    return LY_SUCCESS;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_compare_clb for the ietf-inet-types ipv4-prefix type.
+ */
+static const void *
+lyplg_type_print_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
+        void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
+{
+    struct lyd_value_ipv4_prefix *val = value->ptr;
+    char *ret;
+
+    if (format == LY_VALUE_LYB) {
+        *dynamic = 0;
+        if (value_len) {
+            *value_len = sizeof *val;
+        }
+        return val;
+    }
+
+    /* generate canonical value if not already */
+    if (!value->_canonical) {
+        /* IPv4 mask + '/' + prefix */
+        ret = malloc(INET_ADDRSTRLEN + 3);
+        LY_CHECK_RET(!ret, NULL);
+
+        /* convert back to string */
+        if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
+            free(ret);
+            return NULL;
+        }
+
+        /* add the prefix */
+        sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
+
+        /* store it */
+        if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
+            LOGMEM(ctx);
+            return NULL;
+        }
+    }
+
+    /* use the cached canonical value */
+    if (dynamic) {
+        *dynamic = 0;
+    }
+    if (value_len) {
+        *value_len = strlen(value->_canonical);
+    }
+    return value->_canonical;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_hash_clb for the ietf-inet-types ipv4-prefix type.
+ */
+static const void *
+lyplg_type_hash_ipv4_prefix(const struct lyd_value *value, ly_bool *dynamic, size_t *key_len)
+{
+    struct lyd_value_ipv4_prefix *val = value->ptr;
+
+    *dynamic = 0;
+    *key_len = sizeof *val;
+    return val;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_dup_clb for the ietf-inet-types ipv4-prefix type.
+ */
+static LY_ERR
+lyplg_type_dup_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
+{
+    LY_ERR ret;
+    struct lyd_value_ipv4_prefix *orig_val = original->ptr, *dup_val;
+
+    ret = lydict_insert(ctx, original->_canonical, ly_strlen(original->_canonical), &dup->_canonical);
+    LY_CHECK_RET(ret);
+
+    dup_val = malloc(sizeof *dup_val);
+    if (!dup_val) {
+        lydict_remove(ctx, dup->_canonical);
+        return LY_EMEM;
+    }
+    memcpy(dup_val, orig_val, sizeof *orig_val);
+
+    dup->ptr = dup_val;
+    dup->realtype = original->realtype;
+    return LY_SUCCESS;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_free_clb for the ietf-inet-types ipv4-prefix type.
+ */
+static void
+lyplg_type_free_ipv4_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
+{
+    lydict_remove(ctx, value->_canonical);
+    free(value->ptr);
+}
+
+/**
+ * @brief Plugin information for ipv4-prefix type implementation.
+ *
+ * Note that external plugins are supposed to use:
+ *
+ *   LYPLG_TYPES = {
+ */
+const struct lyplg_type_record plugins_ipv4_prefix[] = {
+    {
+        .module = "ietf-inet-types",
+        .revision = "2013-07-15",
+        .name = "ipv4-prefix",
+
+        .plugin.id = "libyang 2 - ipv4-prefix, version 1",
+        .plugin.store = lyplg_type_store_ipv4_prefix,
+        .plugin.validate = NULL,
+        .plugin.compare = lyplg_type_compare_ipv4_prefix,
+        .plugin.print = lyplg_type_print_ipv4_prefix,
+        .plugin.hash = lyplg_type_hash_ipv4_prefix,
+        .plugin.duplicate = lyplg_type_dup_ipv4_prefix,
+        .plugin.free = lyplg_type_free_ipv4_prefix
+    },
+    {0}
+};
diff --git a/src/plugins_types/ipv6_prefix.c b/src/plugins_types/ipv6_prefix.c
new file mode 100644
index 0000000..7484061
--- /dev/null
+++ b/src/plugins_types/ipv6_prefix.c
@@ -0,0 +1,360 @@
+/**
+ * @file ipv6_prefix.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief ietf-inet-types ipv6-prefix type plugin.
+ *
+ * Copyright (c) 2019-2021 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#define _GNU_SOURCE /* asprintf, strdup */
+#include <sys/cdefs.h>
+
+#include "plugins_types.h"
+
+#include <arpa/inet.h>
+#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
+#include <netinet/in.h>
+#include <sys/socket.h>
+#endif
+#include <assert.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libyang.h"
+
+#include "common.h"
+#include "compat.h"
+
+/**
+ * @page howtoDataLYB LYB Binary Format
+ * @subsection howtoDataLYBTypesIPv6Prefix ipv6-prefix (ietf-inet-types)
+ *
+ * | Size (B) | Mandatory | Type | Meaning |
+ * | :------  | :-------: | :--: | :-----: |
+ * | 16 | yes | `struct in6_addr *` | IPv6 address in network-byte order |
+ * | 1 | yes | `uint8_t *` | prefix length up to 128 |
+ */
+
+/**
+ * @brief Stored value structure for ipv6-prefix
+ */
+struct lyd_value_ipv6_prefix {
+    struct in6_addr addr;
+    uint8_t prefix;
+};
+
+static void lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
+
+/**
+ * @brief Convert IP address with a prefix in string to a binary network-byte order value.
+ *
+ * @param[in] value String to convert.
+ * @param[in] value_len Length of @p value.
+ * @param[in,out] addr Allocated address value to fill.
+ * @param[out] prefix Prefix length.
+ * @param[out] err Error information on error.
+ * @return LY_ERR value.
+ */
+static LY_ERR
+ipv6prefix_str2ip(const char *value, size_t value_len, struct in6_addr *addr, uint8_t *prefix, struct ly_err_item **err)
+{
+    LY_ERR ret = LY_SUCCESS;
+    const char *pref_str;
+    char *mask_str = NULL;
+
+    /* it passed the pattern validation */
+    pref_str = ly_strnchr(value, '/', value_len);
+    ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
+
+    /* get just the network prefix */
+    mask_str = strndup(value, pref_str - value);
+    LY_CHECK_ERR_GOTO(!mask_str, ret = LY_EMEM, cleanup);
+
+    /* convert it to netword-byte order */
+    if (inet_pton(AF_INET6, mask_str, addr) != 1) {
+        ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address \"%s\".", mask_str);
+        goto cleanup;
+    }
+
+cleanup:
+    free(mask_str);
+    return ret;
+}
+
+/**
+ * @brief Zero host-portion of the IP address.
+ *
+ * @param[in,out] addr IP address.
+ * @param[in] prefix Prefix length.
+ */
+static void
+ipv6prefix_zero_host(struct in6_addr *addr, uint8_t prefix)
+{
+    uint32_t i, j, mask;
+
+    /* zero host bits */
+    for (i = 0; i < 4; ++i) {
+        mask = 0;
+        for (j = 0; j < 32; ++j) {
+            mask <<= 1;
+            if (prefix > (i * 32) + j) {
+                mask |= 1;
+            }
+        }
+        mask = htonl(mask);
+        ((uint32_t *)addr->s6_addr)[i] &= mask;
+    }
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_store_clb for the ipv6-prefix ietf-inet-types type.
+ */
+static LY_ERR
+lyplg_type_store_ipv6_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
+        uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
+        const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
+        struct ly_err_item **err)
+{
+    LY_ERR ret = LY_SUCCESS;
+    struct lysc_type_str *type_str = (struct lysc_type_str *)type;
+    struct lyd_value_ipv6_prefix *val;
+
+    /* clear storage */
+    memset(storage, 0, sizeof *storage);
+
+    if (format == LY_VALUE_LYB) {
+        /* validation */
+        if (value_len != 17) {
+            ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-prefix value size %zu (expected 17).",
+                    value_len);
+            goto cleanup;
+        }
+        if (((uint8_t *)value)[16] > 128) {
+            ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-prefix prefix length %" PRIu8 ".",
+                    ((uint8_t *)value)[16]);
+            goto cleanup;
+        }
+
+        /* store/allocate value */
+        if (options & LYPLG_TYPE_STORE_DYNAMIC) {
+            val = (void *)value;
+            options &= ~LYPLG_TYPE_STORE_DYNAMIC;
+        } else {
+            val = malloc(sizeof *val);
+            LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
+            memcpy(val, value, value_len);
+        }
+
+        /* init storage */
+        storage->_canonical = NULL;
+        storage->ptr = val;
+        storage->realtype = type;
+
+        /* zero host */
+        ipv6prefix_zero_host(&val->addr, val->prefix);
+
+        /* success */
+        goto cleanup;
+    }
+
+    /* check hints */
+    ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
+    LY_CHECK_GOTO(ret, cleanup);
+
+    /* length restriction of the string */
+    if (type_str->length) {
+        /* value_len is in bytes, but we need number of characters here */
+        ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
+        LY_CHECK_GOTO(ret, cleanup);
+    }
+
+    /* pattern restrictions */
+    ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
+    LY_CHECK_GOTO(ret, cleanup);
+
+    /* allocate the value */
+    val = malloc(sizeof *val);
+    LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
+
+    /* init storage */
+    storage->_canonical = NULL;
+    storage->ptr = val;
+    storage->realtype = type;
+
+    /* get the mask in network-byte order */
+    ret = ipv6prefix_str2ip(value, value_len, &val->addr, &val->prefix, err);
+    LY_CHECK_GOTO(ret, cleanup);
+
+    /* zero host */
+    ipv6prefix_zero_host(&val->addr, val->prefix);
+
+    if (format == LY_VALUE_CANON) {
+        /* store canonical value */
+        if (options & LYPLG_TYPE_STORE_DYNAMIC) {
+            ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
+            options &= ~LYPLG_TYPE_STORE_DYNAMIC;
+            LY_CHECK_GOTO(ret, cleanup);
+        } else {
+            ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
+            LY_CHECK_GOTO(ret, cleanup);
+        }
+    }
+
+cleanup:
+    if (options & LYPLG_TYPE_STORE_DYNAMIC) {
+        free((void *)value);
+    }
+
+    if (ret) {
+        lyplg_type_free_ipv6_prefix(ctx, storage);
+    }
+    return ret;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_compare_clb for the ietf-inet-types ipv6-prefix type.
+ */
+static LY_ERR
+lyplg_type_compare_ipv6_prefix(const struct lyd_value *val1, const struct lyd_value *val2)
+{
+    struct lyd_value_ipv6_prefix *v1 = val1->ptr, *v2 = val2->ptr;
+
+    if (val1->realtype != val2->realtype) {
+        return LY_ENOT;
+    }
+
+    if (memcmp(v1, v2, sizeof *v1)) {
+        return LY_ENOT;
+    }
+    return LY_SUCCESS;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_compare_clb for the ietf-inet-types ipv6-prefix type.
+ */
+static const void *
+lyplg_type_print_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
+        void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
+{
+    struct lyd_value_ipv6_prefix *val = value->ptr;
+    char *ret;
+
+    if (format == LY_VALUE_LYB) {
+        *dynamic = 0;
+        if (value_len) {
+            *value_len = sizeof *val;
+        }
+        return val;
+    }
+
+    /* generate canonical value if not already */
+    if (!value->_canonical) {
+        /* IPv6 mask + '/' + prefix */
+        ret = malloc(INET6_ADDRSTRLEN + 4);
+        LY_CHECK_RET(!ret, NULL);
+
+        /* convert back to string */
+        if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
+            free(ret);
+            return NULL;
+        }
+
+        /* add the prefix */
+        sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
+
+        /* store it */
+        if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
+            LOGMEM(ctx);
+            return NULL;
+        }
+    }
+
+    /* use the cached canonical value */
+    if (dynamic) {
+        *dynamic = 0;
+    }
+    if (value_len) {
+        *value_len = strlen(value->_canonical);
+    }
+    return value->_canonical;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_hash_clb for the ietf-inet-types ipv6-prefix type.
+ */
+static const void *
+lyplg_type_hash_ipv6_prefix(const struct lyd_value *value, ly_bool *dynamic, size_t *key_len)
+{
+    struct lyd_value_ipv6_prefix *val = value->ptr;
+
+    *dynamic = 0;
+    *key_len = sizeof *val;
+    return val;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_dup_clb for the ietf-inet-types ipv6-prefix type.
+ */
+static LY_ERR
+lyplg_type_dup_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
+{
+    LY_ERR ret;
+    struct lyd_value_ipv6_prefix *orig_val = original->ptr, *dup_val;
+
+    ret = lydict_insert(ctx, original->_canonical, ly_strlen(original->_canonical), &dup->_canonical);
+    LY_CHECK_RET(ret);
+
+    dup_val = malloc(sizeof *dup_val);
+    if (!dup_val) {
+        lydict_remove(ctx, dup->_canonical);
+        return LY_EMEM;
+    }
+    memcpy(dup_val, orig_val, sizeof *orig_val);
+
+    dup->ptr = dup_val;
+    dup->realtype = original->realtype;
+    return LY_SUCCESS;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_free_clb for the ietf-inet-types ipv6-prefix type.
+ */
+static void
+lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
+{
+    lydict_remove(ctx, value->_canonical);
+    free(value->ptr);
+}
+
+/**
+ * @brief Plugin information for ipv6-prefix type implementation.
+ *
+ * Note that external plugins are supposed to use:
+ *
+ *   LYPLG_TYPES = {
+ */
+const struct lyplg_type_record plugins_ipv6_prefix[] = {
+    {
+        .module = "ietf-inet-types",
+        .revision = "2013-07-15",
+        .name = "ipv6-prefix",
+
+        .plugin.id = "libyang 2 - ipv6-prefix, version 1",
+        .plugin.store = lyplg_type_store_ipv6_prefix,
+        .plugin.validate = NULL,
+        .plugin.compare = lyplg_type_compare_ipv6_prefix,
+        .plugin.print = lyplg_type_print_ipv6_prefix,
+        .plugin.hash = lyplg_type_hash_ipv6_prefix,
+        .plugin.duplicate = lyplg_type_dup_ipv6_prefix,
+        .plugin.free = lyplg_type_free_ipv6_prefix
+    },
+    {0}
+};