2 * value.c Functions to handle value_data_t
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2014 The FreeRADIUS server project
25 #include <freeradius-devel/libradius.h>
28 /** Compare two values
30 * @param[in] a_type of data to compare.
31 * @param[in] a_len of data to compare.
32 * @param[in] a Value to compare.
33 * @param[in] b_type of data to compare.
34 * @param[in] b_len of data to compare.
35 * @param[in] b Value to compare.
36 * @return -1 if a is less than b, 0 if both are equal, 1 if a is more than b, < -1 on error.
38 int value_data_cmp(PW_TYPE a_type, value_data_t const *a, size_t a_len,
39 PW_TYPE b_type, value_data_t const *b, size_t b_len)
43 if (a_type != b_type) {
44 fr_strerror_printf("Can't compare values of different types");
49 * After doing the previous check for special comparisons,
50 * do the per-type comparison here.
55 case PW_TYPE_STRING: /* We use memcmp to be \0 safe */
66 compare = memcmp(a->octets, b->octets, length);
67 if (compare != 0) break;
71 * Contents are the same. The return code
72 * is therefore the difference in lengths.
74 * i.e. "0x00" is smaller than "0x0000"
76 compare = a_len - b_len;
81 * Short-hand for simplicity.
83 #define CHECK(_type) if (a->_type < b->_type) { compare = -1; \
84 } else if (a->_type > b->_type) { compare = +1; }
86 case PW_TYPE_BOOLEAN: /* this isn't a RADIUS type, and shouldn't really ever be used */
100 case PW_TYPE_INTEGER:
108 case PW_TYPE_INTEGER64:
112 case PW_TYPE_ETHERNET:
113 compare = memcmp(a->ether, b->ether, sizeof(a->ether));
116 case PW_TYPE_IPV4_ADDR: {
117 uint32_t a_int, b_int;
119 a_int = ntohl(a->ipaddr.s_addr);
120 b_int = ntohl(b->ipaddr.s_addr);
123 } else if (a_int > b_int) {
129 case PW_TYPE_IPV6_ADDR:
130 compare = memcmp(&a->ipv6addr, &b->ipv6addr, sizeof(a->ipv6addr));
133 case PW_TYPE_IPV6_PREFIX:
134 compare = memcmp(a->ipv6prefix, b->ipv6prefix, sizeof(a->ipv6prefix));
137 case PW_TYPE_IPV4_PREFIX:
138 compare = memcmp(a->ipv4prefix, b->ipv4prefix, sizeof(a->ipv4prefix));
142 compare = memcmp(a->ifid, b->ifid, sizeof(a->ifid));
146 * Na of the types below should be in the REQUEST
148 case PW_TYPE_INVALID: /* We should never see these */
149 case PW_TYPE_COMBO_IP_ADDR: /* This should have been converted into IPADDR/IPV6ADDR */
150 case PW_TYPE_COMBO_IP_PREFIX: /* This should have been converted into IPADDR/IPV6ADDR */
152 case PW_TYPE_EXTENDED:
153 case PW_TYPE_LONG_EXTENDED:
156 case PW_TYPE_TIMEVAL:
158 fr_assert(0); /* unknown type */
162 * Do NOT add a default here, as new types are added
163 * static analysis will warn us they're not handled
169 } else if (compare < 0) {
176 * We leverage the fact that IPv4 and IPv6 prefixes both
177 * have the same format:
179 * reserved, prefix-len, data...
181 static int value_data_cidr_cmp_op(FR_TOKEN op, int bytes,
182 uint8_t a_net, uint8_t const *a,
183 uint8_t b_net, uint8_t const *b)
189 * Handle the case of netmasks being identical.
191 if (a_net == b_net) {
194 compare = memcmp(a, b, bytes);
197 * If they're identical return true for
200 if ((compare == 0) &&
201 ((op == T_OP_CMP_EQ) ||
208 * Everything else returns false.
210 * 10/8 == 24/8 --> false
211 * 10/8 <= 24/8 --> false
212 * 10/8 >= 24/8 --> false
218 * Netmasks are different. That limits the
219 * possible results, based on the operator.
229 case T_OP_LT: /* 192/8 < 192.168/16 --> false */
236 case T_OP_GT: /* 192/16 > 192.168/8 --> false */
253 * Do the check byte by byte. If the bytes are
254 * identical, it MAY be a match. If they're different,
260 * All leading bytes are identical.
262 if (common == 0) return true;
265 * Doing bitmasks takes more work.
267 if (common < 8) break;
269 if (a[i] != b[i]) return false;
277 mask <<= (8 - common);
281 if ((a[i] & mask) == ((b[i] & mask))) {
288 /** Compare two attributes using an operator
290 * @param[in] op to use in comparison.
291 * @param[in] a_type of data to compare.
292 * @param[in] a_len of data to compare.
293 * @param[in] a Value to compare.
294 * @param[in] b_type of data to compare.
295 * @param[in] b_len of data to compare.
296 * @param[in] b Value to compare.
297 * @return 1 if true, 0 if false, -1 on error.
299 int value_data_cmp_op(FR_TOKEN op,
300 PW_TYPE a_type, value_data_t const *a, size_t a_len,
301 PW_TYPE b_type, value_data_t const *b, size_t b_len)
305 if (!a || !b) return -1;
308 case PW_TYPE_IPV4_ADDR:
310 case PW_TYPE_IPV4_ADDR: /* IPv4 and IPv4 */
313 case PW_TYPE_IPV4_PREFIX: /* IPv4 and IPv4 Prefix */
314 return value_data_cidr_cmp_op(op, 4, 32, (uint8_t const *) &a->ipaddr,
315 b->ipv4prefix[1], (uint8_t const *) &b->ipv4prefix[2]);
318 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
322 case PW_TYPE_IPV4_PREFIX: /* IPv4 and IPv4 Prefix */
324 case PW_TYPE_IPV4_ADDR:
325 return value_data_cidr_cmp_op(op, 4, a->ipv4prefix[1],
326 (uint8_t const *) &a->ipv4prefix[2],
327 32, (uint8_t const *) &b->ipaddr);
329 case PW_TYPE_IPV4_PREFIX: /* IPv4 Prefix and IPv4 Prefix */
330 return value_data_cidr_cmp_op(op, 4, a->ipv4prefix[1],
331 (uint8_t const *) &a->ipv4prefix[2],
332 b->ipv4prefix[1], (uint8_t const *) &b->ipv4prefix[2]);
335 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
339 case PW_TYPE_IPV6_ADDR:
341 case PW_TYPE_IPV6_ADDR: /* IPv6 and IPv6 */
344 case PW_TYPE_IPV6_PREFIX: /* IPv6 and IPv6 Preifx */
345 return value_data_cidr_cmp_op(op, 16, 128, (uint8_t const *) &a->ipv6addr,
346 b->ipv6prefix[1], (uint8_t const *) &b->ipv6prefix[2]);
349 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
353 case PW_TYPE_IPV6_PREFIX:
355 case PW_TYPE_IPV6_ADDR: /* IPv6 Prefix and IPv6 */
356 return value_data_cidr_cmp_op(op, 16, a->ipv6prefix[1],
357 (uint8_t const *) &a->ipv6prefix[2],
358 128, (uint8_t const *) &b->ipv6addr);
360 case PW_TYPE_IPV6_PREFIX: /* IPv6 Prefix and IPv6 */
361 return value_data_cidr_cmp_op(op, 16, a->ipv6prefix[1],
362 (uint8_t const *) &a->ipv6prefix[2],
363 b->ipv6prefix[1], (uint8_t const *) &b->ipv6prefix[2]);
366 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
372 compare = value_data_cmp(a_type, a, a_len,
374 if (compare < -1) { /* comparison error */
380 * Now do the operator comparison.
384 return (compare == 0);
387 return (compare != 0);
390 return (compare < 0);
393 return (compare > 0);
396 return (compare <= 0);
399 return (compare >= 0);
406 static char const hextab[] = "0123456789abcdef";
408 /** Convert string value to a value_data_t type
410 * @param[in] ctx to alloc strings in.
411 * @param[out] dst where to write parsed value.
412 * @param[in,out] src_type of value data to create/type of value created.
413 * @param[in] src_enumv DICT_ATTR with string aliases for integer values.
414 * @param[in] src String to convert. Binary safe for variable length values if len is provided.
415 * @param[in] src_len may be < 0 in which case strlen(len) is used to determine length, else src_len
416 * should be the length of the string or sub string to parse.
417 * @param[in] quote quotation character used to drive de-escaping
418 * @return length of data written to out or -1 on parse error.
420 ssize_t value_data_from_str(TALLOC_CTX *ctx, value_data_t *dst,
421 PW_TYPE *src_type, DICT_ATTR const *src_enumv,
422 char const *src, ssize_t src_len, char quote)
431 len = (src_len < 0) ? strlen(src) : (size_t)src_len;
434 * Set size for all fixed length attributes.
436 ret = dict_attr_sizes[*src_type][1]; /* Max length */
439 * It's a variable ret src_type so we just alloc a new buffer
440 * of size len and copy.
449 buff = p = talloc_bstrndup(ctx, src, len);
452 * No de-quoting. Just copy the string.
456 dst->strvalue = buff;
461 * Do escaping for single quoted strings. Only
462 * single quotes get escaped. Everything else is
468 while (q < (buff + len)) {
470 * The quotation character is escaped.
472 if ((q[0] == '\\') &&
480 * Two backslashes get mangled to one.
482 if ((q[0] == '\\') &&
490 * Not escaped, just copy it over.
498 /* Shrink the buffer to the correct size */
499 dst->strvalue = talloc_realloc(ctx, buff, char, ret + 1);
504 * It's "string" or `string`, do all standard
508 while (q < (buff + len)) {
511 if ((c == '\\') && (q >= (buff + len))) {
512 fr_strerror_printf("Invalid escape at end of string");
518 * Fix up \X -> ... the binary form of it.
544 * \" --> ", but only inside of double quoted strings, etc.
553 * \000 --> binary zero character
561 (sscanf(q, "%3o", &x) == 1)) {
567 * Else It's not a recognised escape sequence DON'T
568 * consume the backslash. This is identical
569 * behaviour to bash and most other things that
570 * use backslash escaping.
580 dst->strvalue = talloc_realloc(ctx, buff, char, ret + 1);
585 fr_strerror_printf("Must use 'Attr-26 = ...' instead of 'Vendor-Specific = ...'");
588 /* raw octets: 0x01020304... */
589 #ifndef WITH_ASCEND_BINARY
597 * No 0x prefix, just copy verbatim.
599 if ((len < 2) || (strncasecmp(src, "0x", 2) != 0)) {
600 dst->octets = talloc_memdup(ctx, (uint8_t const *)src, len);
601 talloc_set_type(dst->octets, uint8_t);
611 if ((len & 0x01) != 0) {
612 fr_strerror_printf("Length of Hex String is not even, got %zu bytes", len);
617 p = talloc_array(ctx, uint8_t, ret);
618 if (fr_hex2bin(p, ret, src + 2, len) != (size_t)ret) {
620 fr_strerror_printf("Invalid hex data");
628 case PW_TYPE_ABINARY:
629 #ifdef WITH_ASCEND_BINARY
630 if ((len > 1) && (strncasecmp(src, "0x", 2) == 0)) {
633 if (len > ((sizeof(dst->filter) + 1) * 2)) {
634 fr_strerror_printf("Hex data is too large for ascend filter");
638 bin = fr_hex2bin((uint8_t *) &dst->filter, ret, src + 2, len - 2);
640 memset(((uint8_t *) &dst->filter) + bin, 0, ret - bin);
643 if (ascend_parse_filter(dst, src, len) < 0 ) {
644 /* Allow ascend_parse_filter's strerror to bubble up */
649 ret = sizeof(dst->filter);
653 * If Ascend binary is NOT defined,
654 * then fall through to raw octets, so that
655 * the user can at least make them by hand...
660 /* don't use this! */
662 fr_strerror_printf("Cannot parse TLV");
665 case PW_TYPE_IPV4_ADDR:
669 if (fr_pton4(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
672 * We allow v4 addresses to have a /32 suffix as some databases (PostgreSQL)
673 * print them this way.
675 if (addr.prefix != 32) {
676 fr_strerror_printf("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted "
677 "for non-prefix types", addr.prefix);
681 dst->ipaddr.s_addr = addr.ipaddr.ip4addr.s_addr;
685 case PW_TYPE_IPV4_PREFIX:
689 if (fr_pton4(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
691 dst->ipv4prefix[1] = addr.prefix;
692 memcpy(&dst->ipv4prefix[2], &addr.ipaddr.ip4addr.s_addr, sizeof(dst->ipv4prefix) - 2);
696 case PW_TYPE_IPV6_ADDR:
700 if (fr_pton6(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
703 * We allow v6 addresses to have a /128 suffix as some databases (PostgreSQL)
704 * print them this way.
706 if (addr.prefix != 128) {
707 fr_strerror_printf("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted "
708 "for non-prefix types", addr.prefix);
712 memcpy(&dst->ipv6addr, addr.ipaddr.ip6addr.s6_addr, sizeof(dst->ipv6addr));
716 case PW_TYPE_IPV6_PREFIX:
720 if (fr_pton6(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
722 dst->ipv6prefix[1] = addr.prefix;
723 memcpy(&dst->ipv6prefix[2], addr.ipaddr.ip6addr.s6_addr, sizeof(dst->ipv6prefix) - 2);
732 * It's a fixed size src_type, copy to a temporary buffer and
733 * \0 terminate if insize >= 0.
736 if (len >= sizeof(buffer)) {
737 fr_strerror_printf("Temporary buffer too small");
741 memcpy(buffer, src, src_len);
742 buffer[src_len] = '\0';
753 * Note that ALL integers are unsigned!
755 i = fr_strtoul(src, &p);
758 * Look for the named src for the given
761 if (src_enumv && *p && !is_whitespace(p)) {
762 if ((dval = dict_valbyname(src_enumv->attr, src_enumv->vendor, src)) == NULL) {
763 fr_strerror_printf("Unknown or invalid value \"%s\" for attribute %s",
764 src, src_enumv->name);
768 dst->byte = dval->value;
771 fr_strerror_printf("Byte value \"%s\" is larger than 255", src);
786 * Note that ALL integers are unsigned!
788 i = fr_strtoul(src, &p);
791 * Look for the named src for the given
794 if (src_enumv && *p && !is_whitespace(p)) {
795 if ((dval = dict_valbyname(src_enumv->attr, src_enumv->vendor, src)) == NULL) {
796 fr_strerror_printf("Unknown or invalid value \"%s\" for attribute %s",
797 src, src_enumv->name);
801 dst->ushort = dval->value;
804 fr_strerror_printf("Short value \"%s\" is larger than 65535", src);
813 case PW_TYPE_INTEGER:
819 * Note that ALL integers are unsigned!
821 i = fr_strtoul(src, &p);
824 * Look for the named src for the given
827 if (src_enumv && *p && !is_whitespace(p)) {
828 if ((dval = dict_valbyname(src_enumv->attr, src_enumv->vendor, src)) == NULL) {
829 fr_strerror_printf("Unknown or invalid value \"%s\" for attribute %s",
830 src, src_enumv->name);
834 dst->integer = dval->value;
837 * Value is always within the limits
844 case PW_TYPE_INTEGER64:
849 * Note that ALL integers are unsigned!
851 if (sscanf(src, "%" PRIu64, &i) != 1) {
852 fr_strerror_printf("Failed parsing \"%s\" as unsigned 64bit integer", src);
862 * time_t may be 64 bits, whule vp_date MUST be 32-bits. We need an
863 * intermediary variable to handle the conversions.
867 if (fr_get_time(src, &date) < 0) {
868 fr_strerror_printf("failed to parse time string \"%s\"", src);
878 if (ifid_aton(src, (void *) dst->ifid) == NULL) {
879 fr_strerror_printf("Failed to parse interface-id string \"%s\"", src);
884 case PW_TYPE_ETHERNET:
886 char const *c1, *c2, *cp;
890 * Convert things which are obviously integers to Ethernet addresses
892 * We assume the number is the bigendian representation of the
895 if (is_integer(src)) {
896 uint64_t integer = htonll(atoll(src));
898 memcpy(dst->ether, &integer, sizeof(dst->ether));
906 c2 = memchr(hextab, tolower((int) cp[0]), 16);
908 } else if ((cp[1] != '\0') && ((cp[2] == ':') || (cp[2] == '\0'))) {
909 c1 = memchr(hextab, tolower((int) cp[0]), 16);
910 c2 = memchr(hextab, tolower((int) cp[1]), 16);
912 if (*cp == ':') cp++;
916 if (!c1 || !c2 || (p_len >= sizeof(dst->ether))) {
917 fr_strerror_printf("failed to parse Ethernet address \"%s\"", src);
920 dst->ether[p_len] = ((c1-hextab)<<4) + (c2-hextab);
927 * Crazy polymorphic (IPv4/IPv6) attribute src_type for WiMAX.
929 * We try and make is saner by replacing the original
930 * da, with either an IPv4 or IPv6 da src_type.
932 * These are not dynamic da, and will have the same vendor
933 * and attribute as the original.
935 case PW_TYPE_COMBO_IP_ADDR:
937 if (inet_pton(AF_INET6, src, &dst->ipv6addr) > 0) {
938 *src_type = PW_TYPE_IPV6_ADDR;
939 ret = dict_attr_sizes[PW_TYPE_COMBO_IP_ADDR][1]; /* size of IPv6 address */
943 if (ip_hton(&ipaddr, AF_INET, src, false) < 0) {
944 fr_strerror_printf("Failed to find IPv4 address for %s", src);
948 *src_type = PW_TYPE_IPV4_ADDR;
949 dst->ipaddr.s_addr = ipaddr.ipaddr.ip4addr.s_addr;
950 ret = dict_attr_sizes[PW_TYPE_COMBO_IP_ADDR][0]; /* size of IPv4 address */
956 /* Damned code for 1 WiMAX attribute */
957 dst->sinteger = (int32_t)strtol(src, NULL, 10);
964 fr_strerror_printf("Unknown attribute type %d", *src_type);
972 /** Performs byte order reversal for types that need it
975 static void value_data_hton(value_data_t *dst, PW_TYPE type, void const *src, size_t src_len)
977 /* 8 byte integers */
979 case PW_TYPE_INTEGER64:
980 dst->integer64 = htonll(*(uint64_t const *)src);
983 /* 4 byte integers */
984 case PW_TYPE_INTEGER:
987 dst->integer = htonl(*(uint32_t const *)src);
990 /* 2 byte integers */
992 dst->ushort = htons(*(uint16_t const *)src);
998 return; /* shouldn't happen */
1001 memcpy(dst, src, src_len);
1005 /** Convert one type of value_data_t to another
1007 * @note This should be the canonical function used to convert between data types.
1009 * @param ctx to allocate buffers in (usually the same as dst)
1010 * @param dst Where to write result of casting.
1011 * @param dst_type to cast to.
1012 * @param dst_enumv Enumerated values used to converts strings to integers.
1013 * @param src_type to cast from.
1014 * @param src_enumv Enumerated values used to convert integers to strings.
1015 * @param src Input data.
1016 * @param src_len Input data len.
1017 * @return the length of data in the dst or -1 on error.
1019 ssize_t value_data_cast(TALLOC_CTX *ctx, value_data_t *dst,
1020 PW_TYPE dst_type, DICT_ATTR const *dst_enumv,
1021 PW_TYPE src_type, DICT_ATTR const *src_enumv,
1022 value_data_t const *src, size_t src_len)
1024 if (!fr_assert(dst_type != src_type)) return -1;
1027 * Deserialise a value_data_t
1029 if (src_type == PW_TYPE_STRING) {
1030 return value_data_from_str(ctx, dst, &dst_type, dst_enumv, src->strvalue, src_len, '\0');
1034 * Converts the src data to octets with no processing.
1036 if (dst_type == PW_TYPE_OCTETS) {
1037 value_data_hton(dst, src_type, src, src_len);
1038 dst->octets = talloc_memdup(ctx, dst, src_len);
1039 talloc_set_type(dst->octets, uint8_t);
1040 return talloc_array_length(dst->strvalue);
1044 * Serialise a value_data_t
1046 if (dst_type == PW_TYPE_STRING) {
1047 dst->strvalue = value_data_aprints(ctx, src_type, src_enumv, src, src_len, '\0');
1048 return talloc_array_length(dst->strvalue) - 1;
1051 if ((src_type == PW_TYPE_IFID) &&
1052 (dst_type == PW_TYPE_INTEGER64)) {
1053 memcpy(&dst->integer64, src->ifid, sizeof(src->ifid));
1054 dst->integer64 = htonll(dst->integer64);
1056 return dict_attr_sizes[dst_type][0];
1059 if ((src_type == PW_TYPE_INTEGER64) &&
1060 (dst_type == PW_TYPE_ETHERNET)) {
1064 i = htonll(src->integer64);
1065 memcpy(array, &i, 8);
1068 * For OUIs in the DB.
1070 if ((array[0] != 0) || (array[1] != 0)) return -1;
1072 memcpy(dst->ether, &array[2], 6);
1077 * For integers, we allow the casting of a SMALL type to
1078 * a larger type, but not vice-versa.
1080 if (dst_type == PW_TYPE_INTEGER64) {
1083 dst->integer64 = src->byte;
1087 dst->integer64 = src->ushort;
1090 case PW_TYPE_INTEGER:
1091 dst->integer64 = src->integer;
1095 dst->integer64 = src->date;
1098 case PW_TYPE_OCTETS:
1103 fr_strerror_printf("Invalid cast from %s to %s",
1104 fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1105 fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1113 * We can cast LONG integers to SHORTER ones, so long
1114 * as the long one is on the LHS.
1116 if (dst_type == PW_TYPE_INTEGER) {
1119 dst->integer = src->byte;
1123 dst->integer = src->ushort;
1126 case PW_TYPE_OCTETS:
1135 if (dst_type == PW_TYPE_SHORT) {
1138 dst->ushort = src->byte;
1141 case PW_TYPE_OCTETS:
1151 * We can cast integers less that < INT_MAX to signed
1153 if (dst_type == PW_TYPE_SIGNED) {
1156 dst->sinteger = src->byte;
1160 dst->sinteger = src->ushort;
1163 case PW_TYPE_INTEGER:
1164 if (src->integer > INT_MAX) {
1165 fr_strerror_printf("Invalid cast: From integer to signed. integer value %u is larger "
1166 "than max signed int and would overflow", src->integer);
1169 dst->sinteger = (int)src->integer;
1172 case PW_TYPE_INTEGER64:
1173 if (src->integer > INT_MAX) {
1174 fr_strerror_printf("Invalid cast: From integer64 to signed. integer64 value %" PRIu64
1175 " is larger than max signed int and would overflow", src->integer64);
1178 dst->sinteger = (int)src->integer64;
1181 case PW_TYPE_OCTETS:
1190 * Conversions between IPv4 addresses, IPv6 addresses, IPv4 prefixes and IPv6 prefixes
1192 * For prefix to ipaddress conversions, we assume that the host portion has already
1195 * We allow casts from v6 to v4 if the v6 address has the correct mapping prefix.
1197 * We only allow casts from prefixes to addresses if the prefix is the the length of
1198 * the address, e.g. 32 for ipv4 128 for ipv6.
1202 * 10 bytes of 0x00 2 bytes of 0xff
1204 static uint8_t const v4_v6_map[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1205 0x00, 0x00, 0x00, 0x00, 0xff, 0xff };
1208 case PW_TYPE_IPV4_ADDR:
1210 case PW_TYPE_IPV6_ADDR:
1211 if (memcmp(src->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) {
1213 fr_strerror_printf("Invalid cast from %s to %s. No IPv4-IPv6 mapping prefix",
1214 fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1215 fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1219 memcpy(&dst->ipaddr, &src->ipv6addr.s6_addr[sizeof(v4_v6_map)],
1220 sizeof(dst->ipaddr));
1223 case PW_TYPE_IPV4_PREFIX:
1224 if (src->ipv4prefix[1] != 32) {
1226 fr_strerror_printf("Invalid cast from %s to %s. Only /32 prefixes may be "
1227 "cast to IP address types",
1228 fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1229 fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1233 memcpy(&dst->ipaddr, &src->ipv4prefix[2], sizeof(dst->ipaddr));
1236 case PW_TYPE_IPV6_PREFIX:
1237 if (src->ipv6prefix[1] != 128) {
1239 fr_strerror_printf("Invalid cast from %s to %s. Only /128 prefixes may be "
1240 "cast to IP address types",
1241 fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1242 fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1245 if (memcmp(&src->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map)) != 0) {
1246 goto bad_v6_prefix_map;
1248 memcpy(&dst->ipaddr, &src->ipv6prefix[2 + sizeof(v4_v6_map)],
1249 sizeof(dst->ipaddr));
1257 case PW_TYPE_IPV6_ADDR:
1259 case PW_TYPE_IPV4_ADDR:
1260 /* Add the v4/v6 mapping prefix */
1261 memcpy(dst->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map));
1262 memcpy(&dst->ipv6addr.s6_addr[sizeof(v4_v6_map)], &src->ipaddr,
1263 sizeof(dst->ipv6addr.s6_addr) - sizeof(v4_v6_map));
1267 case PW_TYPE_IPV4_PREFIX:
1268 if (src->ipv4prefix[1] != 32) goto bad_v4_prefix_len;
1270 /* Add the v4/v6 mapping prefix */
1271 memcpy(dst->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map));
1272 memcpy(&dst->ipv6addr.s6_addr[sizeof(v4_v6_map)], &src->ipv4prefix[2],
1273 sizeof(dst->ipv6addr.s6_addr) - sizeof(v4_v6_map));
1276 case PW_TYPE_IPV6_PREFIX:
1277 if (src->ipv4prefix[1] != 128) goto bad_v6_prefix_len;
1279 memcpy(dst->ipv6addr.s6_addr, &src->ipv6prefix[2], sizeof(dst->ipv6addr.s6_addr));
1287 case PW_TYPE_IPV4_PREFIX:
1289 case PW_TYPE_IPV4_ADDR:
1290 memcpy(&dst->ipv4prefix[2], &src->ipaddr, sizeof(dst->ipv4prefix) - 2);
1291 dst->ipv4prefix[0] = 0;
1292 dst->ipv4prefix[1] = 32;
1295 case PW_TYPE_IPV6_ADDR:
1296 if (memcmp(src->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) {
1297 goto bad_v6_prefix_map;
1299 memcpy(&dst->ipv4prefix[2], &src->ipv6addr.s6_addr[sizeof(v4_v6_map)],
1300 sizeof(dst->ipv4prefix) - 2);
1301 dst->ipv4prefix[0] = 0;
1302 dst->ipv4prefix[1] = 32;
1305 case PW_TYPE_IPV6_PREFIX:
1306 if (memcmp(&src->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map)) != 0) {
1307 goto bad_v6_prefix_map;
1311 * Prefix must be >= 96 bits. If it's < 96 bytes and the
1312 * above check passed, the v6 address wasn't masked
1313 * correctly when it was packet into a value_data_t.
1315 if (!fr_assert(src->ipv6prefix[1] >= (sizeof(v4_v6_map) * 8))) return -1;
1317 memcpy(&dst->ipv4prefix[2], &src->ipv6prefix[2 + sizeof(v4_v6_map)],
1318 sizeof(dst->ipv4prefix) - 2);
1319 dst->ipv4prefix[0] = 0;
1320 dst->ipv4prefix[1] = src->ipv6prefix[1] - (sizeof(v4_v6_map) * 8);
1328 case PW_TYPE_IPV6_PREFIX:
1330 case PW_TYPE_IPV4_ADDR:
1331 /* Add the v4/v6 mapping prefix */
1332 memcpy(&dst->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map));
1333 memcpy(&dst->ipv6prefix[2 + sizeof(v4_v6_map)], &src->ipaddr,
1334 (sizeof(dst->ipv6prefix) - 2) - sizeof(v4_v6_map));
1335 dst->ipv6prefix[0] = 0;
1336 dst->ipv6prefix[1] = 128;
1339 case PW_TYPE_IPV4_PREFIX:
1340 /* Add the v4/v6 mapping prefix */
1341 memcpy(&dst->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map));
1342 memcpy(&dst->ipv6prefix[2 + sizeof(v4_v6_map)], &src->ipv4prefix[2],
1343 (sizeof(dst->ipv6prefix) - 2) - sizeof(v4_v6_map));
1344 dst->ipv6prefix[0] = 0;
1345 dst->ipv6prefix[1] = (sizeof(v4_v6_map) * 8) + src->ipv4prefix[1];
1348 case PW_TYPE_IPV6_ADDR:
1349 memcpy(&dst->ipv6prefix[2], &src->ipv6addr, sizeof(dst->ipv6prefix) - 2);
1350 dst->ipv6prefix[0] = 0;
1351 dst->ipv6prefix[1] = 128;
1366 * The attribute we've found has to have a size which is
1367 * compatible with the type of the destination cast.
1369 if ((src_len < dict_attr_sizes[dst_type][0]) ||
1370 (src_len > dict_attr_sizes[dst_type][1])) {
1371 char const *src_type_name;
1373 src_type_name = fr_int2str(dict_attr_types, src_type, "<INVALID>");
1374 fr_strerror_printf("Invalid cast from %s to %s. Length should be between %zu and %zu but is %zu",
1376 fr_int2str(dict_attr_types, dst_type, "<INVALID>"),
1377 dict_attr_sizes[dst_type][0], dict_attr_sizes[dst_type][1],
1382 if (src_type == PW_TYPE_OCTETS) {
1384 value_data_hton(dst, dst_type, src->octets, src_len);
1389 * Convert host order to network byte order.
1391 if ((dst_type == PW_TYPE_IPV4_ADDR) &&
1392 ((src_type == PW_TYPE_INTEGER) ||
1393 (src_type == PW_TYPE_DATE) ||
1394 (src_type == PW_TYPE_SIGNED))) {
1395 dst->ipaddr.s_addr = htonl(src->integer);
1397 } else if ((src_type == PW_TYPE_IPV4_ADDR) &&
1398 ((dst_type == PW_TYPE_INTEGER) ||
1399 (dst_type == PW_TYPE_DATE) ||
1400 (dst_type == PW_TYPE_SIGNED))) {
1401 dst->integer = htonl(src->ipaddr.s_addr);
1403 } else { /* they're of the same byte order */
1404 memcpy(&dst, &src, src_len);
1410 /** Copy value data verbatim duplicating any buffers
1412 * @param ctx To allocate buffers in.
1413 * @param dst Where to copy value_data to.
1414 * @param src_type Type of src.
1415 * @param src Where to copy value_data from.
1416 * @param src_len Where
1418 ssize_t value_data_copy(TALLOC_CTX *ctx, value_data_t *dst, PW_TYPE src_type,
1419 const value_data_t *src, size_t src_len)
1423 memcpy(dst, src, sizeof(*src));
1426 case PW_TYPE_STRING:
1427 dst->strvalue = talloc_bstrndup(ctx, src->strvalue, src_len);
1428 if (!dst->strvalue) return -1;
1431 case PW_TYPE_OCTETS:
1432 dst->octets = talloc_memdup(ctx, src->octets, src_len);
1433 talloc_set_type(dst->strvalue, uint8_t);
1434 if (!dst->octets) return -1;
1443 /** Print one attribute value to a string
1446 char *value_data_aprints(TALLOC_CTX *ctx,
1447 PW_TYPE type, DICT_ATTR const *enumv, value_data_t const *data,
1448 size_t inlen, char quote)
1454 case PW_TYPE_STRING:
1459 p = talloc_bstrndup(ctx, data->strvalue, inlen);
1460 if (!p) return NULL;
1461 talloc_set_type(p, char);
1465 /* Gets us the size of the buffer we need to alloc */
1466 len = fr_prints_len(data->strvalue, inlen, quote);
1467 p = talloc_array(ctx, char, len);
1468 if (!p) return NULL;
1470 ret = fr_prints(p, len, data->strvalue, inlen, quote);
1471 if (!fr_assert(ret == (len - 1))) {
1478 case PW_TYPE_INTEGER:
1491 DICT_VALUE const *dv;
1493 if (enumv && (dv = dict_valbyattr(enumv->attr, enumv->vendor, i))) {
1494 p = talloc_typed_strdup(ctx, dv->name);
1496 p = talloc_typed_asprintf(ctx, "%u", i);
1501 case PW_TYPE_SIGNED:
1502 p = talloc_typed_asprintf(ctx, "%d", data->sinteger);
1505 case PW_TYPE_INTEGER64:
1506 p = talloc_typed_asprintf(ctx, "%" PRIu64 , data->integer64);
1509 case PW_TYPE_ETHERNET:
1510 p = talloc_typed_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1511 data->ether[0], data->ether[1],
1512 data->ether[2], data->ether[3],
1513 data->ether[4], data->ether[5]);
1516 case PW_TYPE_ABINARY:
1517 #ifdef WITH_ASCEND_BINARY
1518 p = talloc_array(ctx, char, 128);
1519 if (!p) return NULL;
1520 print_abinary(p, 128, (uint8_t const *) &data->filter, inlen, 0);
1526 case PW_TYPE_OCTETS:
1527 p = talloc_array(ctx, char, 2 + 1 + inlen * 2);
1528 if (!p) return NULL;
1532 fr_bin2hex(p + 2, data->octets, inlen);
1533 p[2 + (inlen * 2)] = '\0';
1543 p = talloc_array(ctx, char, 64);
1544 strftime(p, 64, "%b %e %Y %H:%M:%S %Z",
1545 localtime_r(&t, &s_tm));
1550 * We need to use the proper inet_ntop functions for IP
1551 * addresses, else the output might not match output of
1552 * other functions, which makes testing difficult.
1554 * An example is tunnelled ipv4 in ipv6 addresses.
1556 case PW_TYPE_IPV4_ADDR:
1557 case PW_TYPE_IPV4_PREFIX:
1559 char buff[INET_ADDRSTRLEN + 4]; // + /prefix
1562 value_data_prints(buff, sizeof(buff), type, enumv, data, inlen, '\0');
1564 p = talloc_typed_strdup(ctx, buff);
1568 case PW_TYPE_IPV6_ADDR:
1569 case PW_TYPE_IPV6_PREFIX:
1571 char buff[INET6_ADDRSTRLEN + 4]; // + /prefix
1574 value_data_prints(buff, sizeof(buff), type, enumv, data, inlen, '\0');
1576 p = talloc_typed_strdup(ctx, buff);
1581 p = talloc_typed_asprintf(ctx, "%x:%x:%x:%x",
1582 (data->ifid[0] << 8) | data->ifid[1],
1583 (data->ifid[2] << 8) | data->ifid[3],
1584 (data->ifid[4] << 8) | data->ifid[5],
1585 (data->ifid[6] << 8) | data->ifid[7]);
1588 case PW_TYPE_BOOLEAN:
1589 p = talloc_typed_strdup(ctx, data->byte ? "yes" : "no");
1593 * Don't add default here
1595 case PW_TYPE_INVALID:
1596 case PW_TYPE_COMBO_IP_ADDR:
1597 case PW_TYPE_COMBO_IP_PREFIX:
1599 case PW_TYPE_EXTENDED:
1600 case PW_TYPE_LONG_EXTENDED:
1603 case PW_TYPE_TIMEVAL:
1613 /** Print the value of an attribute to a string
1615 * @note return value should be checked with is_truncated.
1616 * @note Will always \0 terminate unless outlen == 0.
1618 * @param out Where to write the printed version of the attribute value.
1619 * @param outlen Length of the output buffer.
1620 * @param type of data being printed.
1621 * @param enumv Enumerated string values for integer types.
1622 * @param data to print.
1623 * @param inlen Length of data.
1624 * @param quote char to escape in string output.
1625 * @return the number of bytes written to the out buffer, or a number >= outlen if truncation has occurred.
1627 size_t value_data_prints(char *out, size_t outlen,
1628 PW_TYPE type, DICT_ATTR const *enumv, value_data_t const *data,
1629 ssize_t inlen, char quote)
1632 char buf[1024]; /* Interim buffer to use with poorly behaved printing functions */
1633 char const *a = NULL;
1639 size_t len = 0, freespace = outlen;
1641 if (!data) return 0;
1642 if (outlen == 0) return inlen;
1649 case PW_TYPE_STRING:
1652 * Ensure that WE add the quotation marks around the string.
1655 if (freespace < 3) return inlen + 2;
1660 len = fr_prints(p, freespace, data->strvalue, inlen, quote);
1661 /* always terminate the quoted string with another quote */
1662 if (len >= (freespace - 1)) {
1663 /* Use out not p as we're operating on the entire buffer */
1664 out[outlen - 2] = (char) quote;
1665 out[outlen - 1] = '\0';
1671 *p++ = (char) quote;
1678 return fr_prints(out, outlen, data->strvalue, inlen, quote);
1680 case PW_TYPE_INTEGER:
1692 /* Normal, non-tagged attribute */
1693 if (enumv && (v = dict_valbyattr(enumv->attr, enumv->vendor, i)) != NULL) {
1697 /* should never be truncated */
1698 len = snprintf(buf, sizeof(buf), "%u", i);
1703 case PW_TYPE_INTEGER64:
1704 return snprintf(out, outlen, "%" PRIu64, data->integer64);
1709 len = strftime(buf, sizeof(buf) - 1, "%%%b %e %Y %H:%M:%S %Z%%", localtime_r(&t, &s_tm));
1710 buf[0] = (char) quote;
1711 buf[len - 1] = (char) quote;
1714 len = strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z", localtime_r(&t, &s_tm));
1719 case PW_TYPE_SIGNED: /* Damned code for 1 WiMAX attribute */
1720 len = snprintf(buf, sizeof(buf), "%d", data->sinteger);
1724 case PW_TYPE_IPV4_ADDR:
1725 a = inet_ntop(AF_INET, &(data->ipaddr), buf, sizeof(buf));
1729 case PW_TYPE_ABINARY:
1730 #ifdef WITH_ASCEND_BINARY
1731 print_abinary(buf, sizeof(buf), (uint8_t const *) data->filter, inlen, quote);
1738 case PW_TYPE_OCTETS:
1745 hexlen = (binlen * 2) + 2; /* NOT accounting for trailing NUL */
1748 * If the buffer is too small, put something into
1749 * it, and return how much we should have written
1751 * 0 + x + H + H + NUL = 5
1753 if (freespace < 5) {
1754 switch (freespace) {
1779 * The output buffer is at least 5 bytes, we haev
1780 * room for '0xHH' plus a trailing NUL byte.
1786 * Get maximum number of bytes we can encode
1787 * given freespace, ensuring we account for '0',
1788 * 'x', and the trailing NUL in the buffer.
1790 * Note that we can't have "freespace = 0" after
1791 * this, as 'freespace' has to be at least 5.
1795 if (binlen > freespace) {
1799 fr_bin2hex(out + 2, data->octets, binlen);
1804 a = ifid_ntoa(buf, sizeof(buf), data->ifid);
1808 case PW_TYPE_IPV6_ADDR:
1809 a = inet_ntop(AF_INET6, &data->ipv6addr, buf, sizeof(buf));
1813 case PW_TYPE_IPV6_PREFIX:
1815 struct in6_addr addr;
1820 memcpy(&addr, &(data->ipv6prefix[2]), sizeof(addr));
1822 a = inet_ntop(AF_INET6, &addr, buf, sizeof(buf));
1828 len += snprintf(p, sizeof(buf) - len, "/%u", (unsigned int) data->ipv6prefix[1]);
1833 case PW_TYPE_IPV4_PREFIX:
1835 struct in_addr addr;
1840 memcpy(&addr, &(data->ipv4prefix[2]), sizeof(addr));
1842 a = inet_ntop(AF_INET, &addr, buf, sizeof(buf));
1848 len += snprintf(p, sizeof(buf) - len, "/%u", (unsigned int) (data->ipv4prefix[1] & 0x3f));
1853 case PW_TYPE_ETHERNET:
1854 return snprintf(out, outlen, "%02x:%02x:%02x:%02x:%02x:%02x",
1855 data->ether[0], data->ether[1],
1856 data->ether[2], data->ether[3],
1857 data->ether[4], data->ether[5]);
1860 * Don't add default here
1862 case PW_TYPE_INVALID:
1863 case PW_TYPE_COMBO_IP_ADDR:
1864 case PW_TYPE_COMBO_IP_PREFIX:
1865 case PW_TYPE_EXTENDED:
1866 case PW_TYPE_LONG_EXTENDED:
1869 case PW_TYPE_TIMEVAL:
1870 case PW_TYPE_BOOLEAN:
1877 if (a) strlcpy(out, a, outlen);
1879 return len; /* Return the number of bytes we would of written (for truncation detection) */