2 * radius.c Functions to send/receive radius packets.
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 2000-2003,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/libradius.h>
27 #include <freeradius-devel/md5.h>
33 #include <freeradius-devel/udpfromto.h>
41 #define VP_TRACE if (fr_debug_flag) printf
43 #define VP_TRACE(_x, ...)
48 * The RFC says 4096 octets max, and most packets are less than 256.
50 #define MAX_PACKET_LEN 4096
53 * The maximum number of attributes which we allow in an incoming
54 * request. If there are more attributes than this, the request
57 * This helps to minimize the potential for a DoS, when an
58 * attacker spoofs Access-Request packets, which don't have a
59 * Message-Authenticator attribute. This means that the packet
60 * is unsigned, and the attacker can use resources on the server,
61 * even if the end request is rejected.
63 int fr_max_attributes = 0;
64 FILE *fr_log_fp = NULL;
66 typedef struct radius_packet_t {
70 uint8_t vector[AUTH_VECTOR_LEN];
74 static fr_randctx fr_rand_pool; /* across multiple calls */
75 static int fr_rand_initialized = 0;
76 static unsigned int salt_offset = 0;
78 const char *fr_packet_codes[FR_MAX_PACKET_CODE] = {
84 "Accounting-Response",
100 "Resource-Free-Request",
101 "Resource-Free-Response",
102 "Resource-Query-Request",
103 "Resource-Query-Response",
104 "Alternate-Resource-Reclaim-Request",
105 "NAS-Reboot-Request",
106 "NAS-Reboot-Response",
119 "Disconnect-Request",
129 "IP-Address-Allocate",
134 void fr_printf_log(const char *fmt, ...)
139 if ((fr_debug_flag == 0) || !fr_log_fp) {
144 vfprintf(fr_log_fp, fmt, ap);
151 static void print_hex(RADIUS_PACKET *packet)
155 if (!packet->data) return;
157 printf(" Code:\t\t%u\n", packet->data[0]);
158 printf(" Id:\t\t%u\n", packet->data[1]);
159 printf(" Length:\t%u\n", ((packet->data[2] << 8) |
161 printf(" Vector:\t");
162 for (i = 4; i < 20; i++) {
163 printf("%02x", packet->data[i]);
167 if (packet->data_len > 20) {
172 total = packet->data_len - 20;
173 ptr = packet->data + 20;
179 if (total < 2) { /* too short */
180 printf("%02x\n", *ptr);
184 if (ptr[1] > total) { /* too long */
185 for (i = 0; i < total; i++) {
186 printf("%02x ", ptr[i]);
191 printf("%02x %02x ", ptr[0], ptr[1]);
192 attrlen = ptr[1] - 2;
196 for (i = 0; i < attrlen; i++) {
197 if ((i > 0) && ((i & 0x0f) == 0x00))
199 printf("%02x ", ptr[i]);
200 if ((i & 0x0f) == 0x0f) printf("\n");
203 if ((attrlen & 0x0f) != 0x00) printf("\n");
214 * Wrapper for sendto which handles sendfromto, IPv6, and all
215 * possible combinations.
217 static int rad_sendto(int sockfd, void *data, size_t data_len, int flags,
218 fr_ipaddr_t *src_ipaddr, int src_port,
219 fr_ipaddr_t *dst_ipaddr, int dst_port)
222 struct sockaddr_storage dst;
223 socklen_t sizeof_dst;
225 #ifdef WITH_UDPFROMTO
226 struct sockaddr_storage src;
227 socklen_t sizeof_src;
229 fr_ipaddr2sockaddr(src_ipaddr, src_port, &src, &sizeof_src);
231 src_port = src_port; /* -Wunused */
234 if (!fr_ipaddr2sockaddr(dst_ipaddr, dst_port, &dst, &sizeof_dst)) {
238 #ifdef WITH_UDPFROMTO
240 * And if they don't specify a source IP address, don't
243 if (((dst_ipaddr->af == AF_INET) || (dst_ipaddr->af == AF_INET6)) &&
244 (src_ipaddr->af != AF_UNSPEC) &&
245 !fr_inaddr_any(src_ipaddr)) {
246 rcode = sendfromto(sockfd, data, data_len, flags,
247 (struct sockaddr *)&src, sizeof_src,
248 (struct sockaddr *)&dst, sizeof_dst);
252 src_ipaddr = src_ipaddr; /* -Wunused */
256 * No udpfromto, fail gracefully.
258 rcode = sendto(sockfd, data, data_len, flags,
259 (struct sockaddr *) &dst, sizeof_dst);
261 DEBUG("rad_send() failed: %s\n", strerror(errno));
268 void rad_recv_discard(int sockfd)
271 struct sockaddr_storage src;
272 socklen_t sizeof_src = sizeof(src);
274 recvfrom(sockfd, header, sizeof(header), 0,
275 (struct sockaddr *)&src, &sizeof_src);
279 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, int *src_port,
282 ssize_t data_len, packet_len;
284 struct sockaddr_storage src;
285 socklen_t sizeof_src = sizeof(src);
287 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
288 (struct sockaddr *)&src, &sizeof_src);
290 if ((errno == EAGAIN) || (errno == EINTR)) return 0;
295 * Too little data is available, discard the packet.
298 recvfrom(sockfd, header, sizeof(header), 0,
299 (struct sockaddr *)&src, &sizeof_src);
302 } else { /* we got 4 bytes of data. */
304 * See how long the packet says it is.
306 packet_len = (header[2] * 256) + header[3];
309 * The length in the packet says it's less than
310 * a RADIUS header length: discard it.
312 if (packet_len < AUTH_HDR_LEN) {
313 recvfrom(sockfd, header, sizeof(header), 0,
314 (struct sockaddr *)&src, &sizeof_src);
318 * Enforce RFC requirements, for sanity.
319 * Anything after 4k will be discarded.
321 } else if (packet_len > MAX_PACKET_LEN) {
322 recvfrom(sockfd, header, sizeof(header), 0,
323 (struct sockaddr *)&src, &sizeof_src);
329 * Convert AF. If unknown, discard packet.
331 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, src_port)) {
332 recvfrom(sockfd, header, sizeof(header), 0,
333 (struct sockaddr *)&src, &sizeof_src);
340 * The packet says it's this long, but the actual UDP
341 * size could still be smaller.
348 * wrapper for recvfrom, which handles recvfromto, IPv6, and all
349 * possible combinations.
351 static ssize_t rad_recvfrom(int sockfd, uint8_t **pbuf, int flags,
352 fr_ipaddr_t *src_ipaddr, uint16_t *src_port,
353 fr_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
355 struct sockaddr_storage src;
356 struct sockaddr_storage dst;
357 socklen_t sizeof_src = sizeof(src);
358 socklen_t sizeof_dst = sizeof(dst);
365 memset(&src, 0, sizeof_src);
366 memset(&dst, 0, sizeof_dst);
369 * Get address family, etc. first, so we know if we
370 * need to do udpfromto.
372 * FIXME: udpfromto also does this, but it's not
373 * a critical problem.
375 if (getsockname(sockfd, (struct sockaddr *)&dst,
376 &sizeof_dst) < 0) return -1;
379 * Read the length of the packet, from the packet.
380 * This lets us allocate the buffer to use for
381 * reading the rest of the packet.
383 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
384 (struct sockaddr *)&src, &sizeof_src);
386 if ((errno == EAGAIN) || (errno == EINTR)) return 0;
391 * Too little data is available, discard the packet.
394 recvfrom(sockfd, header, sizeof(header), flags,
395 (struct sockaddr *)&src, &sizeof_src);
398 } else { /* we got 4 bytes of data. */
400 * See how long the packet says it is.
402 len = (header[2] * 256) + header[3];
405 * The length in the packet says it's less than
406 * a RADIUS header length: discard it.
408 if (len < AUTH_HDR_LEN) {
409 recvfrom(sockfd, header, sizeof(header), flags,
410 (struct sockaddr *)&src, &sizeof_src);
414 * Enforce RFC requirements, for sanity.
415 * Anything after 4k will be discarded.
417 } else if (len > MAX_PACKET_LEN) {
418 recvfrom(sockfd, header, sizeof(header), flags,
419 (struct sockaddr *)&src, &sizeof_src);
428 * Receive the packet. The OS will discard any data in the
429 * packet after "len" bytes.
431 #ifdef WITH_UDPFROMTO
432 if ((dst.ss_family == AF_INET) || (dst.ss_family == AF_INET6)) {
433 data_len = recvfromto(sockfd, buf, len, flags,
434 (struct sockaddr *)&src, &sizeof_src,
435 (struct sockaddr *)&dst, &sizeof_dst);
439 * No udpfromto, fail gracefully.
441 data_len = recvfrom(sockfd, buf, len, flags,
442 (struct sockaddr *)&src, &sizeof_src);
448 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, &port)) {
450 return -1; /* Unknown address family, Die Die Die! */
454 fr_sockaddr2ipaddr(&dst, sizeof_dst, dst_ipaddr, &port);
458 * Different address families should never happen.
460 if (src.ss_family != dst.ss_family) {
466 * Tell the caller about the data
474 #define AUTH_PASS_LEN (AUTH_VECTOR_LEN)
475 /*************************************************************************
477 * Function: make_secret
479 * Purpose: Build an encrypted secret value to return in a reply
480 * packet. The secret is hidden by xoring with a MD5 digest
481 * created from the shared secret and the authentication
482 * vector. We put them into MD5 in the reverse order from
483 * that used when encrypting passwords to RADIUS.
485 *************************************************************************/
486 static void make_secret(uint8_t *digest, const uint8_t *vector,
487 const char *secret, const uint8_t *value)
492 fr_MD5Init(&context);
493 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
494 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
495 fr_MD5Final(digest, &context);
497 for ( i = 0; i < AUTH_VECTOR_LEN; i++ ) {
498 digest[i] ^= value[i];
502 #define MAX_PASS_LEN (128)
503 static void make_passwd(uint8_t *output, ssize_t *outlen,
504 const uint8_t *input, size_t inlen,
505 const char *secret, const uint8_t *vector)
507 FR_MD5_CTX context, old;
508 uint8_t digest[AUTH_VECTOR_LEN];
509 uint8_t passwd[MAX_PASS_LEN];
514 * If the length is zero, round it up.
518 if (len > MAX_PASS_LEN) len = MAX_PASS_LEN;
520 memcpy(passwd, input, len);
521 memset(passwd + len, 0, sizeof(passwd) - len);
527 else if ((len & 0x0f) != 0) {
533 fr_MD5Init(&context);
534 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
540 fr_MD5Update(&context, vector, AUTH_PASS_LEN);
542 for (n = 0; n < len; n += AUTH_PASS_LEN) {
545 fr_MD5Update(&context,
546 passwd + n - AUTH_PASS_LEN,
550 fr_MD5Final(digest, &context);
551 for (i = 0; i < AUTH_PASS_LEN; i++) {
552 passwd[i + n] ^= digest[i];
556 memcpy(output, passwd, len);
559 static void make_tunnel_passwd(uint8_t *output, ssize_t *outlen,
560 const uint8_t *input, size_t inlen, size_t room,
561 const char *secret, const uint8_t *vector)
563 FR_MD5_CTX context, old;
564 uint8_t digest[AUTH_VECTOR_LEN];
565 uint8_t passwd[MAX_STRING_LEN + AUTH_VECTOR_LEN];
572 if (room > 253) room = 253;
575 * Account for 2 bytes of the salt, and round the room
576 * available down to the nearest multiple of 16. Then,
577 * subtract one from that to account for the length byte,
578 * and the resulting number is the upper bound on the data
581 * We could short-cut this calculation just be forcing
582 * inlen to be no more than 239. It would work for all
583 * VSA's, as we don't pack multiple VSA's into one
586 * However, this calculation is more general, if a little
587 * complex. And it will work in the future for all possible
588 * kinds of weird attribute packing.
591 room -= (room & 0x0f);
594 if (inlen > room) inlen = room;
597 * Length of the encrypted data is password length plus
598 * one byte for the length of the password.
601 if ((len & 0x0f) != 0) {
605 *outlen = len + 2; /* account for the salt */
608 * Copy the password over.
610 memcpy(passwd + 3, input, inlen);
611 memset(passwd + 3 + inlen, 0, sizeof(passwd) - 3 - inlen);
614 * Generate salt. The RFC's say:
616 * The high bit of salt[0] must be set, each salt in a
617 * packet should be unique, and they should be random
619 * So, we set the high bit, add in a counter, and then
620 * add in some CSPRNG data. should be OK..
622 passwd[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
624 passwd[1] = fr_rand();
625 passwd[2] = inlen; /* length of the password string */
627 fr_MD5Init(&context);
628 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
631 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
632 fr_MD5Update(&context, &passwd[0], 2);
634 for (n = 0; n < len; n += AUTH_PASS_LEN) {
637 fr_MD5Update(&context,
638 passwd + 2 + n - AUTH_PASS_LEN,
642 fr_MD5Final(digest, &context);
644 for (i = 0; i < AUTH_PASS_LEN; i++) {
645 passwd[i + 2 + n] ^= digest[i];
648 memcpy(output, passwd, len + 2);
651 extern int fr_attr_max_tlv;
652 extern int fr_attr_shift[];
653 extern int fr_attr_mask[];
655 static int do_next_tlv(const VALUE_PAIR *vp, int nest)
657 unsigned int tlv1, tlv2;
659 if (nest > fr_attr_max_tlv) return 0;
664 * Keep encoding TLVs which have the same scope.
665 * e.g. two attributes of:
666 * ATTR.TLV1.TLV2.TLV3 = data1
667 * ATTR.TLV1.TLV2.TLV4 = data2
668 * both get put into a container of "ATTR.TLV1.TLV2"
672 * Nothing to follow, we're done.
674 if (!vp->next) return 0;
677 * Not from the same vendor, skip it.
679 if (vp->vendor != vp->next->vendor) return 0;
682 * In a different TLV space, skip it.
684 tlv1 = vp->attribute;
685 tlv2 = vp->next->attribute;
687 tlv1 &= ((1 << fr_attr_shift[nest]) - 1);
688 tlv2 &= ((1 << fr_attr_shift[nest]) - 1);
690 if (tlv1 != tlv2) return 0;
696 static ssize_t vp2data_any(const RADIUS_PACKET *packet,
697 const RADIUS_PACKET *original,
698 const char *secret, int nest,
699 const VALUE_PAIR **pvp,
700 uint8_t *start, size_t room);
702 static ssize_t vp2attr_rfc(const RADIUS_PACKET *packet,
703 const RADIUS_PACKET *original,
704 const char *secret, const VALUE_PAIR **pvp,
705 unsigned int attribute, uint8_t *ptr, size_t room);
707 static ssize_t vp2data_tlvs(const RADIUS_PACKET *packet,
708 const RADIUS_PACKET *original,
709 const char *secret, int nest,
710 const VALUE_PAIR **pvp,
711 uint8_t *start, size_t room)
714 uint8_t *ptr = start;
715 uint8_t *end = start + room;
718 if (nest > fr_attr_max_tlv) {
719 fr_strerror_printf("vp2data_tlvs: attribute nesting overflow");
725 len = vp2attr_rfc(packet, original, secret, pvp,
726 ((*pvp)->attribute >> fr_attr_shift[nest]) & fr_attr_mask[nest],
729 if (ptr > start) break;
735 if (!do_next_tlv(*pvp, nest)) break;
742 * Encodes the data portion of an attribute.
743 * Returns -1 on error, or the length of the data portion.
745 static ssize_t vp2data_any(const RADIUS_PACKET *packet,
746 const RADIUS_PACKET *original,
747 const char *secret, int nest,
748 const VALUE_PAIR **pvp,
749 uint8_t *start, size_t room)
754 uint8_t *ptr = start;
756 const VALUE_PAIR *vp = *pvp;
759 * See if we need to encode a TLV. The low portion of
760 * the attribute has already been placed into the packer.
761 * If there are still attribute bytes left, then go
762 * encode them as TLVs.
764 * If we cared about the stack, we could unroll the loop.
766 if ((nest > 0) && (nest <= fr_attr_max_tlv) &&
767 ((vp->attribute >> fr_attr_shift[nest]) != 0)) {
768 return vp2data_tlvs(packet, original, secret, nest, pvp,
773 * Set up the default sources for the data.
775 data = vp->vp_octets;
782 case PW_TYPE_IPV6ADDR:
783 case PW_TYPE_IPV6PREFIX:
784 case PW_TYPE_ABINARY:
785 /* nothing more to do */
789 len = 1; /* just in case */
790 array[0] = vp->vp_integer & 0xff;
795 len = 2; /* just in case */
796 array[0] = (vp->vp_integer >> 8) & 0xff;
797 array[1] = vp->vp_integer & 0xff;
801 case PW_TYPE_INTEGER:
802 len = 4; /* just in case */
803 lvalue = htonl(vp->vp_integer);
804 memcpy(array, &lvalue, sizeof(lvalue));
809 data = (const uint8_t *) &vp->vp_ipaddr;
810 len = 4; /* just in case */
814 * There are no tagged date attributes.
817 lvalue = htonl(vp->vp_date);
818 data = (const uint8_t *) &lvalue;
819 len = 4; /* just in case */
826 len = 4; /* just in case */
827 slvalue = htonl(vp->vp_signed);
828 memcpy(array, &slvalue, sizeof(slvalue));
835 fr_strerror_printf("ERROR: Cannot encode NULL TLV");
841 default: /* unknown type: ignore it */
842 fr_strerror_printf("ERROR: Unknown attribute type %d", vp->type);
847 * Bound the data to the calling size
849 if (len > (ssize_t) room) len = room;
852 * Encrypt the various password styles
854 * Attributes with encrypted values MUST be less than
857 switch (vp->flags.encrypt) {
858 case FLAG_ENCRYPT_USER_PASSWORD:
859 make_passwd(ptr, &len, data, len,
860 secret, packet->vector);
863 case FLAG_ENCRYPT_TUNNEL_PASSWORD:
865 if (vp->flags.has_tag) lvalue = 1;
868 * Check if there's enough room. If there isn't,
869 * we discard the attribute.
871 * This is ONLY a problem if we have multiple VSA's
872 * in one Vendor-Specific, though.
874 if (room < (18 + lvalue)) return 0;
876 switch (packet->code) {
877 case PW_AUTHENTICATION_ACK:
878 case PW_AUTHENTICATION_REJECT:
879 case PW_ACCESS_CHALLENGE:
882 fr_strerror_printf("ERROR: No request packet, cannot encrypt %s attribute in the vp.", vp->name);
886 if (lvalue) ptr[0] = vp->flags.tag;
887 make_tunnel_passwd(ptr + lvalue, &len, data, len,
889 secret, original->vector);
891 case PW_ACCOUNTING_REQUEST:
892 case PW_DISCONNECT_REQUEST:
894 ptr[0] = vp->flags.tag;
895 make_tunnel_passwd(ptr + 1, &len, data, len - 1, room,
896 secret, packet->vector);
902 * The code above ensures that this attribute
905 case FLAG_ENCRYPT_ASCEND_SECRET:
906 make_secret(ptr, packet->vector, secret, data);
907 len = AUTH_VECTOR_LEN;
912 if (vp->flags.has_tag && TAG_VALID(vp->flags.tag)) {
913 if (vp->type == PW_TYPE_STRING) {
914 if (len > ((ssize_t) (room - 1))) len = room - 1;
915 ptr[0] = vp->flags.tag;
917 } else if (vp->type == PW_TYPE_INTEGER) {
918 array[0] = vp->flags.tag;
919 } /* else it can't be any other type */
921 memcpy(ptr, data, len);
923 } /* switch over encryption flags */
926 return len + (ptr - start);;
929 static ssize_t attr_shift(const uint8_t *start, const uint8_t *end,
930 uint8_t *ptr, int hdr_len, ssize_t len,
931 int flag_offset, int vsa_offset)
933 int check_len = len - ptr[1];
934 int total = len + hdr_len;
937 * Pass 1: Check if the addition of the headers
938 * overflows the available room. If so, return
939 * what we were capable of encoding.
942 while (check_len > (255 - hdr_len)) {
944 check_len -= (255 - hdr_len);
948 * Note that this results in a number of attributes maybe
949 * being marked as "encoded", but which aren't in the
950 * packet. Oh well. The solution is to fix the
951 * "vp2data_any" function to take into account the header
954 if ((ptr + ptr[1] + total) > end) {
955 return (ptr + ptr[1]) - start;
959 * Pass 2: Now that we know there's enough room,
960 * re-arrange the data to form a set of valid
964 int sublen = 255 - ptr[1];
971 memmove(ptr + 255 + hdr_len, ptr + 255, sublen);
972 memcpy(ptr + 255, ptr, hdr_len);
974 if (vsa_offset) ptr[vsa_offset] += sublen;
975 ptr[flag_offset] |= 0x80;
979 if (vsa_offset) ptr[vsa_offset] = 3;
983 if (vsa_offset) ptr[vsa_offset] += len;
985 return (ptr + ptr[1]) - start;
990 * Encode an "extended" attribute.
992 int rad_vp2extended(const RADIUS_PACKET *packet,
993 const RADIUS_PACKET *original,
994 const char *secret, const VALUE_PAIR **pvp,
995 uint8_t *ptr, size_t room)
1000 uint8_t *start = ptr;
1001 const VALUE_PAIR *vp = *pvp;
1003 if (vp->vendor < VENDORPEC_EXTENDED) {
1004 fr_strerror_printf("rad_vp2extended called for non-extended attribute");
1008 if (room < 3) return 0;
1010 ptr[0] = vp->attribute & 0xff;
1013 if (vp->flags.extended) {
1014 ptr[2] = (vp->attribute & 0xff00) >> 8;
1016 } else if (vp->flags.extended_flags) {
1017 if (room < 4) return 0;
1020 ptr[2] = (vp->attribute & 0xff00) >> 8;
1025 * Only "flagged" attributes can be longer than one
1028 if (!vp->flags.extended_flags && (room > 255)) {
1035 if (vp->flags.evs) {
1036 uint8_t *evs = ptr + ptr[1];
1038 if (room < (size_t) (ptr[1] + 5)) return 0;
1041 * RADIUS Attribute Type is packed into the high byte
1042 * of the Vendor Id. So over-write it in the packet.
1044 * And hard-code Extended-Type to Vendor-Specific.
1046 ptr[0] = (vp->vendor >> 24) & 0xff;
1049 evs[0] = 0; /* always zero */
1050 evs[1] = (vp->vendor >> 16) & 0xff;
1051 evs[2] = (vp->vendor >> 8) & 0xff;
1052 evs[3] = vp->vendor & 0xff;
1053 evs[4] = vp->attribute & 0xff;
1060 len = vp2data_any(packet, original, secret, nest,
1061 pvp, ptr + ptr[1], room - hdr_len);
1062 if (len < 0) return len;
1065 * There may be more than 252 octets of data encoded in
1066 * the attribute. If so, move the data up in the packet,
1067 * and copy the existing header over. Set the "M" flag ONLY
1068 * after copying the rest of the data.
1070 if (vp->flags.extended_flags && (len > (255 - ptr[1]))) {
1071 return attr_shift(start, start + room, ptr, 4, len, 3, 0);
1076 return (ptr + ptr[1]) - start;
1081 * Encode a WiMAX attribute.
1083 int rad_vp2wimax(const RADIUS_PACKET *packet,
1084 const RADIUS_PACKET *original,
1085 const char *secret, const VALUE_PAIR **pvp,
1086 uint8_t *ptr, size_t room)
1091 uint8_t *start = ptr;
1092 const VALUE_PAIR *vp = *pvp;
1095 * Double-check for WiMAX format.
1097 if (!vp->flags.wimax) {
1098 fr_strerror_printf("rad_vp2wimax called for non-WIMAX VSA");
1103 * Not enough room for:
1104 * attr, len, vendor-id, vsa, vsalen, continuation
1106 if (room < 9) return 0;
1109 * Build the Vendor-Specific header
1112 ptr[0] = PW_VENDOR_SPECIFIC;
1114 lvalue = htonl(vp->vendor);
1115 memcpy(ptr + 2, &lvalue, 4);
1116 ptr[6] = (vp->attribute & fr_attr_mask[1]);
1118 ptr[8] = 0; /* continuation byte */
1122 len = vp2data_any(packet, original, secret, 1, pvp, ptr + ptr[1],
1124 if (len <= 0) return len;
1127 * There may be more than 252 octets of data encoded in
1128 * the attribute. If so, move the data up in the packet,
1129 * and copy the existing header over. Set the "C" flag
1130 * ONLY after copying the rest of the data.
1132 if (len > (255 - ptr[1])) {
1133 return attr_shift(start, start + room, ptr, hdr_len, len, 8, 7);
1139 return (ptr + ptr[1]) - start;
1143 * Encode an RFC format TLV. This could be a standard attribute,
1144 * or a TLV data type. If it's a standard attribute, then
1145 * vp->attribute == attribute. Otherwise, attribute may be
1148 static ssize_t vp2attr_rfc(const RADIUS_PACKET *packet,
1149 const RADIUS_PACKET *original,
1150 const char *secret, const VALUE_PAIR **pvp,
1151 unsigned int attribute, uint8_t *ptr, size_t room)
1155 if (room < 2) return 0;
1157 ptr[0] = attribute & 0xff;
1160 if (room > ((unsigned) 255 - ptr[1])) room = 255 - ptr[1];
1162 len = vp2data_any(packet, original, secret, 0, pvp, ptr + ptr[1], room);
1163 if (len < 0) return len;
1172 * Encode a VSA which is a TLV. If it's in the RFC format, call
1173 * vp2attr_rfc. Otherwise, encode it here.
1175 static ssize_t vp2attr_vsa(const RADIUS_PACKET *packet,
1176 const RADIUS_PACKET *original,
1177 const char *secret, const VALUE_PAIR **pvp,
1178 unsigned int attribute, unsigned int vendor,
1179 uint8_t *ptr, size_t room)
1183 const VALUE_PAIR *vp = *pvp;
1186 * Unknown vendor: RFC format.
1187 * Known vendor and RFC format: go do that.
1189 VP_TRACE("Encoding VSA %u.%u\n", vendor, attribute);
1190 dv = dict_vendorbyvalue(vendor);
1191 VP_TRACE("Flags %d %d\n", vp->flags.is_tlv, vp->flags.has_tlv);
1193 (!vp->flags.is_tlv && (dv->type == 1) && (dv->length == 1))) {
1194 VP_TRACE("Encoding RFC %u.%u\n", vendor, attribute);
1195 return vp2attr_rfc(packet, original, secret, pvp,
1196 attribute, ptr, room);
1199 if (vp->flags.is_tlv) {
1200 VP_TRACE("Encoding TLV %u.%u\n", vendor, attribute);
1201 return vp2data_tlvs(packet, original, secret, 0, pvp,
1207 fr_strerror_printf("vp2attr_vsa: Internal sanity check failed,"
1208 " type %u", (unsigned) dv->type);
1212 ptr[0] = 0; /* attr must be 24-bit */
1213 ptr[1] = (attribute >> 16) & 0xff;
1214 ptr[2] = (attribute >> 8) & 0xff;
1215 ptr[3] = attribute & 0xff;
1219 ptr[0] = (attribute >> 8) & 0xff;
1220 ptr[1] = attribute & 0xff;
1224 ptr[0] = attribute & 0xff;
1228 switch (dv->length) {
1230 fr_strerror_printf("vp2attr_vsa: Internal sanity check failed,"
1231 " length %u", (unsigned) dv->length);
1242 ptr[dv->type + dv->length - 1] = dv->type + dv->length;
1247 if (room > ((unsigned) 255 - (dv->type + dv->length))) {
1248 room = 255 - (dv->type + dv->length);
1251 len = vp2data_any(packet, original, secret, 0, pvp,
1252 ptr + dv->type + dv->length, room);
1253 if (len < 0) return len;
1255 if (dv->length) ptr[dv->type + dv->length - 1] += len;
1257 return dv->type + dv->length + len;
1262 * Encode a Vendor-Specific attribute.
1264 int rad_vp2vsa(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1265 const char *secret, const VALUE_PAIR **pvp, uint8_t *ptr,
1270 const VALUE_PAIR *vp = *pvp;
1273 * Double-check for WiMAX format.
1275 if (vp->flags.wimax) {
1276 return rad_vp2wimax(packet, original, secret, pvp,
1280 if (vp->vendor > FR_MAX_VENDOR) {
1281 fr_strerror_printf("rad_vp2vsa: Invalid arguments");
1286 * Not enough room for:
1287 * attr, len, vendor-id
1289 if (room < 6) return 0;
1292 * Build the Vendor-Specific header
1294 ptr[0] = PW_VENDOR_SPECIFIC;
1296 lvalue = htonl(vp->vendor);
1297 memcpy(ptr + 2, &lvalue, 4);
1299 if (room > ((unsigned) 255 - ptr[1])) room = 255 - ptr[1];
1301 len = vp2attr_vsa(packet, original, secret, pvp,
1302 vp->attribute, vp->vendor,
1303 ptr + ptr[1], room);
1304 if (len < 0) return len;
1313 * Encode an RFC standard attribute 1..255
1315 int rad_vp2rfc(const RADIUS_PACKET *packet,
1316 const RADIUS_PACKET *original,
1317 const char *secret, const VALUE_PAIR **pvp,
1318 uint8_t *ptr, size_t room)
1320 const VALUE_PAIR *vp = *pvp;
1322 if (vp->vendor != 0) {
1323 fr_strerror_printf("rad_vp2rfc called with VSA");
1327 if ((vp->attribute == 0) || (vp->attribute > 255)) {
1328 fr_strerror_printf("rad_vp2rfc called with non-standard attribute %u", vp->attribute);
1332 if ((vp->length == 0) &&
1333 (vp->attribute != PW_CHARGEABLE_USER_IDENTITY)) {
1337 return vp2attr_rfc(packet, original, secret, pvp, vp->attribute,
1343 * Parse a data structure into a RADIUS attribute.
1345 int rad_vp2attr(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1346 const char *secret, const VALUE_PAIR **pvp, uint8_t *start,
1349 const VALUE_PAIR *vp;
1351 if (!pvp || !*pvp || !start || (room <= 2)) return -1;
1356 * RFC format attributes take the fast path.
1358 if (vp->vendor == 0) {
1359 if (vp->attribute > 255) return 0;
1362 * Message-Authenticator is hard-coded.
1364 if (vp->attribute == PW_MESSAGE_AUTHENTICATOR) {
1365 if (room < 18) return -1;
1367 fprintf(stderr, "M-A!\n");
1368 if (room < 16) return -1;
1370 start[0] = PW_MESSAGE_AUTHENTICATOR;
1372 memset(start + 2, 0, 16);
1373 *pvp = (*pvp)->next;
1377 return rad_vp2rfc(packet, original, secret, pvp,
1381 if (vp->vendor > FR_MAX_VENDOR) {
1382 return rad_vp2extended(packet, original, secret, pvp,
1386 if (vp->flags.wimax) {
1387 return rad_vp2wimax(packet, original, secret, pvp,
1391 return rad_vp2vsa(packet, original, secret, pvp,
1399 int rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1402 radius_packet_t *hdr;
1404 uint16_t total_length;
1406 const VALUE_PAIR *reply;
1408 char ip_buffer[128];
1411 * A 4K packet, aligned on 64-bits.
1413 uint64_t data[MAX_PACKET_LEN / sizeof(uint64_t)];
1415 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
1416 what = fr_packet_codes[packet->code];
1421 DEBUG("Sending %s of id %d to %s port %d\n",
1423 inet_ntop(packet->dst_ipaddr.af,
1424 &packet->dst_ipaddr.ipaddr,
1425 ip_buffer, sizeof(ip_buffer)),
1429 * Double-check some things based on packet code.
1431 switch (packet->code) {
1432 case PW_AUTHENTICATION_ACK:
1433 case PW_AUTHENTICATION_REJECT:
1434 case PW_ACCESS_CHALLENGE:
1436 fr_strerror_printf("ERROR: Cannot sign response packet without a request packet.");
1442 * These packet vectors start off as all zero.
1444 case PW_ACCOUNTING_REQUEST:
1445 case PW_DISCONNECT_REQUEST:
1446 case PW_COA_REQUEST:
1447 memset(packet->vector, 0, sizeof(packet->vector));
1455 * Use memory on the stack, until we know how
1456 * large the packet will be.
1458 hdr = (radius_packet_t *) data;
1461 * Build standard header
1463 hdr->code = packet->code;
1464 hdr->id = packet->id;
1466 memcpy(hdr->vector, packet->vector, sizeof(hdr->vector));
1468 total_length = AUTH_HDR_LEN;
1471 * Load up the configuration values for the user
1477 * FIXME: Loop twice over the reply list. The first time,
1478 * calculate the total length of data. The second time,
1479 * allocate the memory, and fill in the VP's.
1481 * Hmm... this may be slower than just doing a small
1486 * Loop over the reply attributes for the packet.
1488 reply = packet->vps;
1491 * Ignore non-wire attributes, but allow extended
1494 if ((reply->vendor == 0) &&
1495 ((reply->attribute & 0xFFFF) >= 256) &&
1496 !reply->flags.extended && !reply->flags.extended_flags) {
1499 * Permit the admin to send BADLY formatted
1500 * attributes with a debug build.
1502 if (reply->attribute == PW_RAW_ATTRIBUTE) {
1503 memcpy(ptr, reply->vp_octets, reply->length);
1504 len = reply->length;
1505 reply = reply->next;
1509 reply = reply->next;
1514 * Set the Message-Authenticator to the correct
1515 * length and initial value.
1517 if (reply->attribute == PW_MESSAGE_AUTHENTICATOR) {
1519 * Cache the offset to the
1520 * Message-Authenticator
1522 packet->offset = total_length;
1526 * Print out ONLY the attributes which
1527 * we're sending over the wire, and print
1528 * them out BEFORE they're encrypted.
1532 len = rad_vp2attr(packet, original, secret, &reply, ptr,
1533 ((uint8_t *) data) + sizeof(data) - ptr);
1534 if (len < 0) return -1;
1537 * Failed to encode the attribute, likely because
1538 * the packet is full.
1541 (total_length > (sizeof(data) - 2 - reply->length))) {
1542 DEBUG("WARNING: Attributes are too long for packet. Discarding data past %d bytes", total_length);
1547 next: /* Used only for Raw-Attribute */
1550 total_length += len;
1551 } /* done looping over all attributes */
1554 * Fill in the rest of the fields, and copy the data over
1555 * from the local stack to the newly allocated memory.
1557 * Yes, all this 'memcpy' is slow, but it means
1558 * that we only allocate the minimum amount of
1559 * memory for a request.
1561 packet->data_len = total_length;
1562 packet->data = (uint8_t *) malloc(packet->data_len);
1563 if (!packet->data) {
1564 fr_strerror_printf("Out of memory");
1568 memcpy(packet->data, hdr, packet->data_len);
1569 hdr = (radius_packet_t *) packet->data;
1571 total_length = htons(total_length);
1572 memcpy(hdr->length, &total_length, sizeof(total_length));
1579 * Sign a previously encoded packet.
1581 int rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1584 radius_packet_t *hdr = (radius_packet_t *)packet->data;
1587 * It wasn't assigned an Id, this is bad!
1589 if (packet->id < 0) {
1590 fr_strerror_printf("ERROR: RADIUS packets must be assigned an Id.");
1594 if (!packet->data || (packet->data_len < AUTH_HDR_LEN) ||
1595 (packet->offset < 0)) {
1596 fr_strerror_printf("ERROR: You must call rad_encode() before rad_sign()");
1601 * If there's a Message-Authenticator, update it
1602 * now, BEFORE updating the authentication vector.
1604 if (packet->offset > 0) {
1605 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
1607 switch (packet->code) {
1608 case PW_ACCOUNTING_REQUEST:
1609 case PW_ACCOUNTING_RESPONSE:
1610 case PW_DISCONNECT_REQUEST:
1611 case PW_DISCONNECT_ACK:
1612 case PW_DISCONNECT_NAK:
1613 case PW_COA_REQUEST:
1616 memset(hdr->vector, 0, AUTH_VECTOR_LEN);
1619 case PW_AUTHENTICATION_ACK:
1620 case PW_AUTHENTICATION_REJECT:
1621 case PW_ACCESS_CHALLENGE:
1623 fr_strerror_printf("ERROR: Cannot sign response packet without a request packet.");
1626 memcpy(hdr->vector, original->vector,
1630 default: /* others have vector already set to zero */
1636 * Set the authentication vector to zero,
1637 * calculate the signature, and put it
1638 * into the Message-Authenticator
1641 fr_hmac_md5(packet->data, packet->data_len,
1642 (const uint8_t *) secret, strlen(secret),
1644 memcpy(packet->data + packet->offset + 2,
1645 calc_auth_vector, AUTH_VECTOR_LEN);
1648 * Copy the original request vector back
1649 * to the raw packet.
1651 memcpy(hdr->vector, packet->vector, AUTH_VECTOR_LEN);
1655 * Switch over the packet code, deciding how to
1658 switch (packet->code) {
1660 * Request packets are not signed, bur
1661 * have a random authentication vector.
1663 case PW_AUTHENTICATION_REQUEST:
1664 case PW_STATUS_SERVER:
1668 * Reply packets are signed with the
1669 * authentication vector of the request.
1676 fr_MD5Init(&context);
1677 fr_MD5Update(&context, packet->data, packet->data_len);
1678 fr_MD5Update(&context, (const uint8_t *) secret,
1680 fr_MD5Final(digest, &context);
1682 memcpy(hdr->vector, digest, AUTH_VECTOR_LEN);
1683 memcpy(packet->vector, digest, AUTH_VECTOR_LEN);
1686 }/* switch over packet codes */
1692 * Reply to the request. Also attach
1693 * reply attribute value pairs and any user message provided.
1695 int rad_send(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1700 char ip_buffer[128];
1703 * Maybe it's a fake packet. Don't send it.
1705 if (!packet || (packet->sockfd < 0)) {
1709 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
1710 what = fr_packet_codes[packet->code];
1716 * First time through, allocate room for the packet
1718 if (!packet->data) {
1720 * Encode the packet.
1722 if (rad_encode(packet, original, secret) < 0) {
1727 * Re-sign it, including updating the
1728 * Message-Authenticator.
1730 if (rad_sign(packet, original, secret) < 0) {
1735 * If packet->data points to data, then we print out
1736 * the VP list again only for debugging.
1738 } else if (fr_debug_flag) {
1739 DEBUG("Sending %s of id %d to %s port %d\n", what, packet->id,
1740 inet_ntop(packet->dst_ipaddr.af,
1741 &packet->dst_ipaddr.ipaddr,
1742 ip_buffer, sizeof(ip_buffer)),
1745 for (reply = packet->vps; reply; reply = reply->next) {
1746 if ((reply->vendor == 0) &&
1747 ((reply->attribute & 0xFFFF) > 0xff)) continue;
1753 * And send it on it's way.
1755 return rad_sendto(packet->sockfd, packet->data, packet->data_len, 0,
1756 &packet->src_ipaddr, packet->src_port,
1757 &packet->dst_ipaddr, packet->dst_port);
1761 * Do a comparison of two authentication digests by comparing
1762 * the FULL digest. Otehrwise, the server can be subject to
1763 * timing attacks that allow attackers find a valid message
1766 * http://www.cs.rice.edu/~dwallach/pub/crosby-timing2009.pdf
1768 int rad_digest_cmp(const uint8_t *a, const uint8_t *b, size_t length)
1773 for (i = 0; i < length; i++) {
1774 result |= a[i] ^ b[i];
1777 return result; /* 0 is OK, !0 is !OK, just like memcmp */
1782 * Validates the requesting client NAS. Calculates the
1783 * signature based on the clients private key.
1785 static int calc_acctdigest(RADIUS_PACKET *packet, const char *secret)
1787 uint8_t digest[AUTH_VECTOR_LEN];
1791 * Zero out the auth_vector in the received packet.
1792 * Then append the shared secret to the received packet,
1793 * and calculate the MD5 sum. This must be the same
1794 * as the original MD5 sum (packet->vector).
1796 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
1799 * MD5(packet + secret);
1801 fr_MD5Init(&context);
1802 fr_MD5Update(&context, packet->data, packet->data_len);
1803 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
1804 fr_MD5Final(digest, &context);
1807 * Return 0 if OK, 2 if not OK.
1809 if (rad_digest_cmp(digest, packet->vector, AUTH_VECTOR_LEN) != 0) return 2;
1815 * Validates the requesting client NAS. Calculates the
1816 * signature based on the clients private key.
1818 static int calc_replydigest(RADIUS_PACKET *packet, RADIUS_PACKET *original,
1821 uint8_t calc_digest[AUTH_VECTOR_LEN];
1827 if (original == NULL) {
1832 * Copy the original vector in place.
1834 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
1837 * MD5(packet + secret);
1839 fr_MD5Init(&context);
1840 fr_MD5Update(&context, packet->data, packet->data_len);
1841 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
1842 fr_MD5Final(calc_digest, &context);
1845 * Copy the packet's vector back to the packet.
1847 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
1850 * Return 0 if OK, 2 if not OK.
1852 if (rad_digest_cmp(packet->vector, calc_digest, AUTH_VECTOR_LEN) != 0) return 2;
1858 * Check if a set of RADIUS formatted TLVs are OK.
1860 int rad_tlv_ok(const uint8_t *data, size_t length,
1861 size_t dv_type, size_t dv_length)
1863 const uint8_t *end = data + length;
1865 if ((dv_length > 2) || (dv_type == 0) || (dv_type > 4)) {
1866 fr_strerror_printf("rad_tlv_ok: Invalid arguments");
1870 while (data < end) {
1873 if ((data + dv_type + dv_length) > end) {
1874 fr_strerror_printf("Attribute header overflow");
1880 if ((data[0] == 0) && (data[1] == 0) &&
1881 (data[2] == 0) && (data[3] == 0)) {
1883 fr_strerror_printf("Invalid attribute 0");
1888 fr_strerror_printf("Invalid attribute > 2^24");
1894 if ((data[1] == 0) && (data[1] == 0)) goto zero;
1898 if (data[0] == 0) goto zero;
1902 fr_strerror_printf("Internal sanity check failed");
1906 switch (dv_length) {
1911 if (data[dv_type + 1] != 0) {
1912 fr_strerror_printf("Attribute is longer than 256 octets");
1917 attrlen = data[dv_type + dv_length - 1];
1922 fr_strerror_printf("Internal sanity check failed");
1926 if (attrlen < (dv_type + dv_length)) {
1927 fr_strerror_printf("Attribute header has invalid length");
1931 if (attrlen > length) {
1932 fr_strerror_printf("Attribute overflows container");
1945 * See if the data pointed to by PTR is a valid RADIUS packet.
1947 * packet is not 'const * const' because we may update data_len,
1948 * if there's more data in the UDP packet than in the RADIUS packet.
1950 int rad_packet_ok(RADIUS_PACKET *packet, int flags)
1955 radius_packet_t *hdr;
1956 char host_ipaddr[128];
1962 * Check for packets smaller than the packet header.
1964 * RFC 2865, Section 3., subsection 'length' says:
1966 * "The minimum length is 20 ..."
1968 if (packet->data_len < AUTH_HDR_LEN) {
1969 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (received %d < minimum %d)",
1970 inet_ntop(packet->src_ipaddr.af,
1971 &packet->src_ipaddr.ipaddr,
1972 host_ipaddr, sizeof(host_ipaddr)),
1973 (int) packet->data_len, AUTH_HDR_LEN);
1978 * RFC 2865, Section 3., subsection 'length' says:
1980 * " ... and maximum length is 4096."
1982 if (packet->data_len > MAX_PACKET_LEN) {
1983 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too long (received %d > maximum %d)",
1984 inet_ntop(packet->src_ipaddr.af,
1985 &packet->src_ipaddr.ipaddr,
1986 host_ipaddr, sizeof(host_ipaddr)),
1987 (int) packet->data_len, MAX_PACKET_LEN);
1992 * Check for packets with mismatched size.
1993 * i.e. We've received 128 bytes, and the packet header
1994 * says it's 256 bytes long.
1996 totallen = (packet->data[2] << 8) | packet->data[3];
1997 hdr = (radius_packet_t *)packet->data;
2000 * Code of 0 is not understood.
2001 * Code of 16 or greate is not understood.
2003 if ((hdr->code == 0) ||
2004 (hdr->code >= FR_MAX_PACKET_CODE)) {
2005 fr_strerror_printf("WARNING: Bad RADIUS packet from host %s: unknown packet code%d ",
2006 inet_ntop(packet->src_ipaddr.af,
2007 &packet->src_ipaddr.ipaddr,
2008 host_ipaddr, sizeof(host_ipaddr)),
2014 * Message-Authenticator is required in Status-Server
2015 * packets, otherwise they can be trivially forged.
2017 if (hdr->code == PW_STATUS_SERVER) require_ma = 1;
2020 * It's also required if the caller asks for it.
2022 if (flags) require_ma = 1;
2025 * Repeat the length checks. This time, instead of
2026 * looking at the data we received, look at the value
2027 * of the 'length' field inside of the packet.
2029 * Check for packets smaller than the packet header.
2031 * RFC 2865, Section 3., subsection 'length' says:
2033 * "The minimum length is 20 ..."
2035 if (totallen < AUTH_HDR_LEN) {
2036 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (length %d < minimum %d)",
2037 inet_ntop(packet->src_ipaddr.af,
2038 &packet->src_ipaddr.ipaddr,
2039 host_ipaddr, sizeof(host_ipaddr)),
2040 totallen, AUTH_HDR_LEN);
2045 * And again, for the value of the 'length' field.
2047 * RFC 2865, Section 3., subsection 'length' says:
2049 * " ... and maximum length is 4096."
2051 if (totallen > MAX_PACKET_LEN) {
2052 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too long (length %d > maximum %d)",
2053 inet_ntop(packet->src_ipaddr.af,
2054 &packet->src_ipaddr.ipaddr,
2055 host_ipaddr, sizeof(host_ipaddr)),
2056 totallen, MAX_PACKET_LEN);
2061 * RFC 2865, Section 3., subsection 'length' says:
2063 * "If the packet is shorter than the Length field
2064 * indicates, it MUST be silently discarded."
2066 * i.e. No response to the NAS.
2068 if (packet->data_len < totallen) {
2069 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: received %d octets, packet length says %d",
2070 inet_ntop(packet->src_ipaddr.af,
2071 &packet->src_ipaddr.ipaddr,
2072 host_ipaddr, sizeof(host_ipaddr)),
2073 (int) packet->data_len, totallen);
2078 * RFC 2865, Section 3., subsection 'length' says:
2080 * "Octets outside the range of the Length field MUST be
2081 * treated as padding and ignored on reception."
2083 if (packet->data_len > totallen) {
2085 * We're shortening the packet below, but just
2086 * to be paranoid, zero out the extra data.
2088 memset(packet->data + totallen, 0, packet->data_len - totallen);
2089 packet->data_len = totallen;
2093 * Walk through the packet's attributes, ensuring that
2094 * they add up EXACTLY to the size of the packet.
2096 * If they don't, then the attributes either under-fill
2097 * or over-fill the packet. Any parsing of the packet
2098 * is impossible, and will result in unknown side effects.
2100 * This would ONLY happen with buggy RADIUS implementations,
2101 * or with an intentional attack. Either way, we do NOT want
2102 * to be vulnerable to this problem.
2105 count = totallen - AUTH_HDR_LEN;
2110 * We need at least 2 bytes to check the
2114 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: attribute header overflows the packet",
2115 inet_ntop(packet->src_ipaddr.af,
2116 &packet->src_ipaddr.ipaddr,
2117 host_ipaddr, sizeof(host_ipaddr)));
2122 * Attribute number zero is NOT defined.
2125 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: Invalid attribute 0",
2126 inet_ntop(packet->src_ipaddr.af,
2127 &packet->src_ipaddr.ipaddr,
2128 host_ipaddr, sizeof(host_ipaddr)));
2133 * Attributes are at LEAST as long as the ID & length
2134 * fields. Anything shorter is an invalid attribute.
2137 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: attribute %u too short",
2138 inet_ntop(packet->src_ipaddr.af,
2139 &packet->src_ipaddr.ipaddr,
2140 host_ipaddr, sizeof(host_ipaddr)),
2146 * If there are fewer bytes in the packet than in the
2147 * attribute, it's a bad packet.
2149 if (count < attr[1]) {
2150 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: attribute %u data overflows the packet",
2151 inet_ntop(packet->src_ipaddr.af,
2152 &packet->src_ipaddr.ipaddr,
2153 host_ipaddr, sizeof(host_ipaddr)),
2159 * Sanity check the attributes for length.
2162 default: /* don't do anything by default */
2166 * If there's an EAP-Message, we require
2167 * a Message-Authenticator.
2169 case PW_EAP_MESSAGE:
2173 case PW_MESSAGE_AUTHENTICATOR:
2174 if (attr[1] != 2 + AUTH_VECTOR_LEN) {
2175 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: Message-Authenticator has invalid length %d",
2176 inet_ntop(packet->src_ipaddr.af,
2177 &packet->src_ipaddr.ipaddr,
2178 host_ipaddr, sizeof(host_ipaddr)),
2187 * FIXME: Look up the base 255 attributes in the
2188 * dictionary, and switch over their type. For
2189 * integer/date/ip, the attribute length SHOULD
2192 count -= attr[1]; /* grab the attribute length */
2194 num_attributes++; /* seen one more attribute */
2198 * If the attributes add up to a packet, it's allowed.
2200 * If not, we complain, and throw the packet away.
2203 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: packet attributes do NOT exactly fill the packet",
2204 inet_ntop(packet->src_ipaddr.af,
2205 &packet->src_ipaddr.ipaddr,
2206 host_ipaddr, sizeof(host_ipaddr)));
2211 * If we're configured to look for a maximum number of
2212 * attributes, and we've seen more than that maximum,
2213 * then throw the packet away, as a possible DoS.
2215 if ((fr_max_attributes > 0) &&
2216 (num_attributes > fr_max_attributes)) {
2217 fr_strerror_printf("WARNING: Possible DoS attack from host %s: Too many attributes in request (received %d, max %d are allowed).",
2218 inet_ntop(packet->src_ipaddr.af,
2219 &packet->src_ipaddr.ipaddr,
2220 host_ipaddr, sizeof(host_ipaddr)),
2221 num_attributes, fr_max_attributes);
2226 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
2228 * A packet with an EAP-Message attribute MUST also have
2229 * a Message-Authenticator attribute.
2231 * A Message-Authenticator all by itself is OK, though.
2233 * Similarly, Status-Server packets MUST contain
2234 * Message-Authenticator attributes.
2236 if (require_ma && ! seen_ma) {
2237 fr_strerror_printf("WARNING: Insecure packet from host %s: Packet does not contain required Message-Authenticator attribute",
2238 inet_ntop(packet->src_ipaddr.af,
2239 &packet->src_ipaddr.ipaddr,
2240 host_ipaddr, sizeof(host_ipaddr)));
2245 * Fill RADIUS header fields
2247 packet->code = hdr->code;
2248 packet->id = hdr->id;
2249 memcpy(packet->vector, hdr->vector, AUTH_VECTOR_LEN);
2256 * Receive UDP client requests, and fill in
2257 * the basics of a RADIUS_PACKET structure.
2259 RADIUS_PACKET *rad_recv(int fd, int flags)
2262 RADIUS_PACKET *packet;
2265 * Allocate the new request data structure
2267 if ((packet = malloc(sizeof(*packet))) == NULL) {
2268 fr_strerror_printf("out of memory");
2271 memset(packet, 0, sizeof(*packet));
2274 sock_flags = MSG_PEEK;
2278 packet->data_len = rad_recvfrom(fd, &packet->data, sock_flags,
2279 &packet->src_ipaddr, &packet->src_port,
2280 &packet->dst_ipaddr, &packet->dst_port);
2283 * Check for socket errors.
2285 if (packet->data_len < 0) {
2286 fr_strerror_printf("Error receiving packet: %s", strerror(errno));
2287 /* packet->data is NULL */
2293 * If the packet is too big, then rad_recvfrom did NOT
2294 * allocate memory. Instead, it just discarded the
2297 if (packet->data_len > MAX_PACKET_LEN) {
2298 fr_strerror_printf("Discarding packet: Larger than RFC limitation of 4096 bytes.");
2299 /* packet->data is NULL */
2305 * Read no data. Continue.
2306 * This check is AFTER the MAX_PACKET_LEN check above, because
2307 * if the packet is larger than MAX_PACKET_LEN, we also have
2308 * packet->data == NULL
2310 if ((packet->data_len == 0) || !packet->data) {
2311 fr_strerror_printf("Empty packet: Socket is not ready.");
2317 * See if it's a well-formed RADIUS packet.
2319 if (!rad_packet_ok(packet, flags)) {
2325 * Remember which socket we read the packet from.
2327 packet->sockfd = fd;
2330 * FIXME: Do even more filtering by only permitting
2331 * certain IP's. The problem is that we don't know
2332 * how to do this properly for all possible clients...
2336 * Explicitely set the VP list to empty.
2340 if (fr_debug_flag) {
2341 char host_ipaddr[128];
2343 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
2344 DEBUG("rad_recv: %s packet from host %s port %d",
2345 fr_packet_codes[packet->code],
2346 inet_ntop(packet->src_ipaddr.af,
2347 &packet->src_ipaddr.ipaddr,
2348 host_ipaddr, sizeof(host_ipaddr)),
2351 DEBUG("rad_recv: Packet from host %s port %d code=%d",
2352 inet_ntop(packet->src_ipaddr.af,
2353 &packet->src_ipaddr.ipaddr,
2354 host_ipaddr, sizeof(host_ipaddr)),
2358 DEBUG(", id=%d, length=%d\n",
2359 packet->id, (int) packet->data_len);
2367 * Verify the signature of a packet.
2369 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
2376 if (!packet || !packet->data) return -1;
2379 * Before we allocate memory for the attributes, do more
2382 ptr = packet->data + AUTH_HDR_LEN;
2383 length = packet->data_len - AUTH_HDR_LEN;
2384 while (length > 0) {
2385 uint8_t msg_auth_vector[AUTH_VECTOR_LEN];
2386 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
2391 default: /* don't do anything. */
2395 * Note that more than one Message-Authenticator
2396 * attribute is invalid.
2398 case PW_MESSAGE_AUTHENTICATOR:
2399 memcpy(msg_auth_vector, &ptr[2], sizeof(msg_auth_vector));
2400 memset(&ptr[2], 0, AUTH_VECTOR_LEN);
2402 switch (packet->code) {
2406 case PW_ACCOUNTING_REQUEST:
2407 case PW_ACCOUNTING_RESPONSE:
2408 case PW_DISCONNECT_REQUEST:
2409 case PW_DISCONNECT_ACK:
2410 case PW_DISCONNECT_NAK:
2411 case PW_COA_REQUEST:
2414 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
2417 case PW_AUTHENTICATION_ACK:
2418 case PW_AUTHENTICATION_REJECT:
2419 case PW_ACCESS_CHALLENGE:
2421 fr_strerror_printf("ERROR: Cannot validate Message-Authenticator in response packet without a request packet.");
2424 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
2428 fr_hmac_md5(packet->data, packet->data_len,
2429 (const uint8_t *) secret, strlen(secret),
2431 if (rad_digest_cmp(calc_auth_vector, msg_auth_vector,
2432 sizeof(calc_auth_vector)) != 0) {
2434 fr_strerror_printf("Received packet from %s with invalid Message-Authenticator! (Shared secret is incorrect.)",
2435 inet_ntop(packet->src_ipaddr.af,
2436 &packet->src_ipaddr.ipaddr,
2437 buffer, sizeof(buffer)));
2438 /* Silently drop packet, according to RFC 3579 */
2440 } /* else the message authenticator was good */
2443 * Reinitialize Authenticators.
2445 memcpy(&ptr[2], msg_auth_vector, AUTH_VECTOR_LEN);
2446 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
2448 } /* switch over the attributes */
2452 } /* loop over the packet, sanity checking the attributes */
2455 * It looks like a RADIUS packet, but we can't validate
2458 if ((packet->code == 0) || (packet->code >= FR_MAX_PACKET_CODE)) {
2460 fr_strerror_printf("Received Unknown packet code %d "
2461 "from client %s port %d: Cannot validate signature.",
2463 inet_ntop(packet->src_ipaddr.af,
2464 &packet->src_ipaddr.ipaddr,
2465 buffer, sizeof(buffer)),
2471 * Calculate and/or verify digest.
2473 switch(packet->code) {
2477 case PW_AUTHENTICATION_REQUEST:
2478 case PW_STATUS_SERVER:
2480 * The authentication vector is random
2481 * nonsense, invented by the client.
2485 case PW_COA_REQUEST:
2486 case PW_DISCONNECT_REQUEST:
2487 case PW_ACCOUNTING_REQUEST:
2488 if (calc_acctdigest(packet, secret) > 1) {
2489 fr_strerror_printf("Received %s packet "
2490 "from client %s with invalid signature! (Shared secret is incorrect.)",
2491 fr_packet_codes[packet->code],
2492 inet_ntop(packet->src_ipaddr.af,
2493 &packet->src_ipaddr.ipaddr,
2494 buffer, sizeof(buffer)));
2499 /* Verify the reply digest */
2500 case PW_AUTHENTICATION_ACK:
2501 case PW_AUTHENTICATION_REJECT:
2502 case PW_ACCESS_CHALLENGE:
2503 case PW_ACCOUNTING_RESPONSE:
2504 case PW_DISCONNECT_ACK:
2505 case PW_DISCONNECT_NAK:
2508 rcode = calc_replydigest(packet, original, secret);
2510 fr_strerror_printf("Received %s packet "
2511 "from home server %s port %d with invalid signature! (Shared secret is incorrect.)",
2512 fr_packet_codes[packet->code],
2513 inet_ntop(packet->src_ipaddr.af,
2514 &packet->src_ipaddr.ipaddr,
2515 buffer, sizeof(buffer)),
2522 fr_strerror_printf("Received Unknown packet code %d "
2523 "from client %s port %d: Cannot validate signature",
2525 inet_ntop(packet->src_ipaddr.af,
2526 &packet->src_ipaddr.ipaddr,
2527 buffer, sizeof(buffer)),
2537 * Create a "raw" attribute from the attribute contents.
2539 static ssize_t data2vp_raw(UNUSED const RADIUS_PACKET *packet,
2540 UNUSED const RADIUS_PACKET *original,
2541 UNUSED const char *secret,
2542 unsigned int attribute, unsigned int vendor,
2543 const uint8_t *data, size_t length,
2549 * Keep the next function happy.
2551 vp = pairalloc(NULL);
2552 vp = paircreate_raw(attribute, vendor, PW_TYPE_OCTETS, vp);
2554 fr_strerror_printf("data2vp_raw: Failed creating attribute");
2558 vp->length = length;
2561 * If the data is too large, mark it as a "TLV".
2563 if (length <= sizeof(vp->vp_octets)) {
2564 memcpy(vp->vp_octets, data, length);
2566 vp->type = PW_TYPE_TLV;
2567 vp->vp_tlv = malloc(length);
2572 memcpy(vp->vp_tlv, data, length);
2581 static ssize_t data2vp_tlvs(const RADIUS_PACKET *packet,
2582 const RADIUS_PACKET *original,
2584 unsigned int attribute, unsigned int vendor,
2586 const uint8_t *start, size_t length,
2590 * Create any kind of VP from the attribute contents.
2592 * Will return -1 on error, or "length".
2594 static ssize_t data2vp_any(const RADIUS_PACKET *packet,
2595 const RADIUS_PACKET *original,
2596 const char *secret, int nest,
2597 unsigned int attribute, unsigned int vendor,
2598 const uint8_t *data, size_t length,
2601 int data_offset = 0;
2603 VALUE_PAIR *vp = NULL;
2607 * Hacks for CUI. The WiMAX spec says that it
2608 * can be zero length, even though this is
2609 * forbidden by the RADIUS specs. So... we make
2610 * a special case for it.
2612 if ((vendor == 0) &&
2613 (attribute == PW_CHARGEABLE_USER_IDENTITY)) {
2614 data = (const uint8_t *) "";
2622 da = dict_attrbyvalue(attribute, vendor);
2625 * Unknown attribute. Create it as a "raw" attribute.
2628 VP_TRACE("Not found %u.%u\n", vendor, attribute);
2630 if (vp) pairfree(&vp);
2631 return data2vp_raw(packet, original, secret,
2632 attribute, vendor, data, length, pvp);
2636 * TLVs are handled first. They can't be tagged, and
2637 * they can't be encrypted.
2639 if (da->type == PW_TYPE_TLV) {
2640 VP_TRACE("Found TLV %u.%u\n", vendor, attribute);
2641 return data2vp_tlvs(packet, original, secret,
2642 attribute, vendor, nest,
2647 * The attribute is known, and well formed. We can now
2648 * create it. The main failure from here on in is being
2657 if (vp->flags.has_tag) {
2658 if (TAG_VALID(data[0]) ||
2659 (vp->flags.encrypt == FLAG_ENCRYPT_TUNNEL_PASSWORD)) {
2661 * Tunnel passwords REQUIRE a tag, even
2662 * if don't have a valid tag.
2664 vp->flags.tag = data[0];
2666 if ((vp->type == PW_TYPE_STRING) ||
2667 (vp->type == PW_TYPE_OCTETS)) {
2668 if (length == 0) goto raw;
2675 * Copy the data to be decrypted
2677 vp->length = length - data_offset;
2678 memcpy(&vp->vp_octets[0], data + data_offset, vp->length);
2681 * Decrypt the attribute.
2683 switch (vp->flags.encrypt) {
2687 case FLAG_ENCRYPT_USER_PASSWORD:
2689 rad_pwdecode(vp->vp_strvalue,
2693 rad_pwdecode(vp->vp_strvalue,
2697 if (vp->attribute == PW_USER_PASSWORD) {
2698 vp->length = strlen(vp->vp_strvalue);
2703 * Tunnel-Password's may go ONLY
2704 * in response packets.
2706 case FLAG_ENCRYPT_TUNNEL_PASSWORD:
2707 if (!original) goto raw;
2709 if (rad_tunnel_pwdecode(vp->vp_octets, &vp->length,
2710 secret, original->vector) < 0) {
2716 * Ascend-Send-Secret
2717 * Ascend-Receive-Secret
2719 case FLAG_ENCRYPT_ASCEND_SECRET:
2723 uint8_t my_digest[AUTH_VECTOR_LEN];
2724 make_secret(my_digest,
2727 memcpy(vp->vp_strvalue, my_digest,
2729 vp->vp_strvalue[AUTH_VECTOR_LEN] = '\0';
2730 vp->length = strlen(vp->vp_strvalue);
2736 } /* switch over encryption flags */
2740 case PW_TYPE_STRING:
2741 case PW_TYPE_OCTETS:
2742 case PW_TYPE_ABINARY:
2743 /* nothing more to do */
2747 if (vp->length != 1) goto raw;
2749 vp->vp_integer = vp->vp_octets[0];
2754 if (vp->length != 2) goto raw;
2756 vp->vp_integer = (vp->vp_octets[0] << 8) | vp->vp_octets[1];
2759 case PW_TYPE_INTEGER:
2760 if (vp->length != 4) goto raw;
2762 memcpy(&vp->vp_integer, vp->vp_octets, 4);
2763 vp->vp_integer = ntohl(vp->vp_integer);
2765 if (vp->flags.has_tag) vp->vp_integer &= 0x00ffffff;
2768 * Try to get named VALUEs
2772 dval = dict_valbyattr(vp->attribute, vp->vendor,
2775 strlcpy(vp->vp_strvalue,
2777 sizeof(vp->vp_strvalue));
2783 if (vp->length != 4) goto raw;
2785 memcpy(&vp->vp_date, vp->vp_octets, 4);
2786 vp->vp_date = ntohl(vp->vp_date);
2790 case PW_TYPE_IPADDR:
2791 if (vp->length != 4) goto raw;
2793 memcpy(&vp->vp_ipaddr, vp->vp_octets, 4);
2797 * IPv6 interface ID is 8 octets long.
2800 if (vp->length != 8) goto raw;
2801 /* vp->vp_ifid == vp->vp_octets */
2805 * IPv6 addresses are 16 octets long
2807 case PW_TYPE_IPV6ADDR:
2808 if (vp->length != 16) goto raw;
2809 /* vp->vp_ipv6addr == vp->vp_octets */
2813 * IPv6 prefixes are 2 to 18 octets long.
2815 * RFC 3162: The first octet is unused.
2816 * The second is the length of the prefix
2817 * the rest are the prefix data.
2819 * The prefix length can have value 0 to 128.
2821 case PW_TYPE_IPV6PREFIX:
2822 if (vp->length < 2 || vp->length > 18) goto raw;
2823 if (vp->vp_octets[1] > 128) goto raw;
2826 * FIXME: double-check that
2827 * (vp->vp_octets[1] >> 3) matches vp->length + 2
2829 if (vp->length < 18) {
2830 memset(vp->vp_octets + vp->length, 0,
2835 case PW_TYPE_SIGNED:
2836 if (vp->length != 4) goto raw;
2839 * Overload vp_integer for ntohl, which takes
2840 * uint32_t, not int32_t
2842 memcpy(&vp->vp_integer, vp->vp_octets, 4);
2843 vp->vp_integer = ntohl(vp->vp_integer);
2844 memcpy(&vp->vp_signed, &vp->vp_integer, 4);
2849 fr_strerror_printf("data2vp_any: Internal sanity check failed");
2852 case PW_TYPE_COMBO_IP:
2853 if (vp->length == 4) {
2854 vp->type = PW_TYPE_IPADDR;
2855 memcpy(&vp->vp_ipaddr, vp->vp_octets, 4);
2858 } else if (vp->length == 16) {
2859 vp->type = PW_TYPE_IPV6ADDR;
2860 /* vp->vp_ipv6addr == vp->vp_octets */
2877 * Convert a top-level VSA to a VP.
2879 static ssize_t attr2vp_vsa(const RADIUS_PACKET *packet,
2880 const RADIUS_PACKET *original,
2881 const char *secret, unsigned int vendor,
2882 size_t dv_type, size_t dv_length,
2883 const uint8_t *data, size_t length,
2886 unsigned int attribute;
2887 ssize_t attrlen, my_len;
2890 if (length <= (dv_type + dv_length)) {
2891 fr_strerror_printf("attr2vp_vsa: Failure to call rad_tlv_ok");
2898 /* data[0] must be zero */
2899 attribute = data[1] << 16;
2900 attribute |= data[2] << 8;
2901 attribute |= data[3];
2905 attribute = data[0] << 8;
2906 attribute |= data[1];
2910 attribute = data[0];
2914 fr_strerror_printf("attr2vp_vsa: Internal sanity check failed");
2918 switch (dv_length) {
2920 /* data[dv_type] must be zero */
2921 attrlen = data[dv_type + 1];
2925 attrlen = data[dv_type];
2933 fr_strerror_printf("attr2vp_vsa: Internal sanity check failed");
2938 if (attrlen <= (ssize_t) (dv_type + dv_length)) {
2939 fr_strerror_printf("attr2vp_vsa: Failure to call rad_tlv_ok");
2944 attrlen -= (dv_type + dv_length);
2946 my_len = data2vp_any(packet, original, secret, 0,
2948 data + dv_type + dv_length, attrlen, pvp);
2949 if (my_len < 0) return my_len;
2952 if (my_len != attrlen) {
2954 fr_strerror_printf("attr2vp_vsa: Incomplete decode %d != %d",
2955 (int) my_len, (int) attrlen);
2960 return dv_type + dv_length + attrlen;
2964 * Convert one or more TLVs to VALUE_PAIRs. This function can
2965 * be called recursively...
2967 static ssize_t data2vp_tlvs(const RADIUS_PACKET *packet,
2968 const RADIUS_PACKET *original,
2970 unsigned int attribute, unsigned int vendor,
2972 const uint8_t *start, size_t length,
2975 size_t dv_type, dv_length;
2976 const uint8_t *data, *end;
2977 VALUE_PAIR *head, **last, *vp;
2982 * The default format for a VSA is the RFC recommended
2989 * Top-level TLVs can be of a weird format. TLVs
2990 * encapsulated in a TLV can only be in the RFC format.
2994 dv = dict_vendorbyvalue(vendor);
2997 dv_length = dv->length;
2998 /* dict.c enforces sane values on the above fields */
3002 if (nest >= fr_attr_max_tlv) {
3003 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed in recursion");
3008 * The VSAs do not exactly fill the data,
3009 * The *entire* TLV is malformed.
3011 if (rad_tlv_ok(data, length, dv_type, dv_length) < 0) {
3012 VP_TRACE("TLV malformed %u.%u\n", vendor, attribute);
3013 return data2vp_raw(packet, original, secret,
3014 attribute, vendor, data, length, pvp);
3017 end = data + length;
3021 while (data < end) {
3022 unsigned int my_attr;
3023 unsigned int my_len;
3026 if ((data + dv_type + dv_length) > end) {
3027 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed in tlvs: Insufficient data");
3035 my_attr = attribute;
3036 my_attr |= ((data[0] & fr_attr_mask[nest + 1])
3037 << fr_attr_shift[nest + 1]);
3040 my_attr = (data[0] << 8) | data[1];
3044 my_attr = (data[1] << 16) | (data[1] << 8) | data[3];
3048 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed");
3052 switch (dv_length) {
3059 my_len = data[dv_type + dv_length - 1];
3063 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed");
3068 if (my_len < (dv_type + dv_length)) {
3069 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed in tlvs: underflow");
3074 if ((data + my_len) > end) {
3075 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed in tlvs: overflow");
3081 my_len -= dv_type + dv_length;
3084 * If this returns > 0, it returns "my_len"
3086 if (data2vp_any(packet, original, secret, nest + 1,
3088 data + dv_type + dv_length, my_len, &vp) < 0) {
3093 data += my_len + dv_type + dv_length;
3103 return data - start;
3108 * Group "continued" attributes together, and create VPs from them.
3109 * The caller ensures that the RADIUS packet is OK, and that the
3110 * continuations have all been checked.
3112 static ssize_t data2vp_continued(const RADIUS_PACKET *packet,
3113 const RADIUS_PACKET *original,
3115 const uint8_t *start, size_t length,
3116 VALUE_PAIR **pvp, int nest,
3117 unsigned int attribute, unsigned int vendor,
3118 int first_offset, int later_offset,
3122 uint8_t *attr, *ptr;
3123 const uint8_t *data;
3125 attr = malloc(attrlen);
3127 fr_strerror_printf("Out of memory");
3138 memcpy(ptr, data + first_offset, data[1] - first_offset);
3139 ptr += data[1] - first_offset;
3140 left -= data[1] - first_offset;
3145 if (data >= (start + length)) {
3146 fr_strerror_printf("data2vp_continued: Internal sanity check failed");
3150 memcpy(ptr, data + later_offset, data[1] - later_offset);
3151 ptr += data[1] - later_offset;
3152 left -= data[1] - later_offset;
3156 left = data2vp_any(packet, original, secret, nest,
3158 attr, attrlen, pvp);
3160 if (left < 0) return left;
3162 return data - start;
3167 * Create a "raw" VALUE_PAIR from a RADIUS attribute.
3169 ssize_t rad_attr2vp_raw(const RADIUS_PACKET *packet,
3170 const RADIUS_PACKET *original,
3172 const uint8_t *data, size_t length,
3177 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3178 fr_strerror_printf("rad_attr2vp_raw: Invalid length");
3182 my_len = data2vp_raw(packet, original, secret, data[0], 0,
3183 data + 2, data[1] - 2, pvp);
3184 if (my_len < 0) return my_len;
3191 * Get the length of the data portion of all of the contiguous
3192 * continued attributes.
3194 * 0 for "no continuation"
3195 * -1 on malformed packets (continuation followed by non-wimax, etc.)
3197 static ssize_t wimax_attrlen(uint32_t vendor,
3198 const uint8_t *start, const uint8_t *end)
3201 const uint8_t *data = start;
3203 if ((data[8] & 0x80) == 0) return 0;
3204 total = data[7] - 3;
3207 while (data < end) {
3209 if ((data + 9) > end) return -1;
3211 if ((data[0] != PW_VENDOR_SPECIFIC) ||
3213 (memcmp(data + 2, &vendor, 4) != 0) ||
3214 (data[6] != start[6]) ||
3215 ((data[7] + 6) != data[1])) return -1;
3217 total += data[7] - 3;
3218 if ((data[8] & 0x80) == 0) break;
3227 * Get the length of the data portion of all of the contiguous
3228 * continued attributes.
3230 * 0 for "no continuation"
3231 * -1 on malformed packets (continuation followed by non-wimax, etc.)
3233 static ssize_t extended_attrlen(const uint8_t *start, const uint8_t *end)
3236 const uint8_t *data = start;
3238 if ((data[3] & 0x80) == 0) return 0;
3239 total = data[1] - 4;
3242 while (data < end) {
3243 if ((data + 4) > end) return -1;
3245 if ((data[0] != start[0]) ||
3247 (data[2] != start[2])) return -1;
3249 total += data[1] - 4;
3250 if ((data[3] & 0x80) == 0) break;
3259 * Create WiMAX VALUE_PAIRs from a RADIUS attribute.
3261 ssize_t rad_attr2vp_wimax(const RADIUS_PACKET *packet,
3262 const RADIUS_PACKET *original,
3264 const uint8_t *data, size_t length,
3268 unsigned int attribute;
3271 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3272 fr_strerror_printf("rad_attr2vp_wimax: Invalid length");
3276 if (data[0] != PW_VENDOR_SPECIFIC) {
3277 fr_strerror_printf("rad_attr2vp_wimax: Invalid attribute");
3282 * Not enough room for a Vendor-Id. + WiMAX header
3285 return rad_attr2vp_raw(packet, original, secret,
3289 memcpy(&lvalue, data + 2, 4);
3290 lvalue = ntohl(lvalue);
3295 if (lvalue != VENDORPEC_WIMAX) {
3298 dv = dict_vendorbyvalue(lvalue);
3299 if (!dv || !dv->flags) {
3300 fr_strerror_printf("rad_attr2vp_wimax: Not a WiMAX attribute");
3306 * The WiMAX attribute is encapsulated in a VSA. If the
3307 * WiMAX length disagrees with the VSA length, it's malformed.
3309 if ((data[7] + 6) != data[1]) {
3310 return rad_attr2vp_raw(packet, original, secret,
3314 attribute = data[6];
3317 * Attribute is continued. Do some more work.
3320 my_len = wimax_attrlen(htonl(lvalue), data, data + length);
3322 return rad_attr2vp_raw(packet, original, secret,
3326 return data2vp_continued(packet, original, secret,
3327 data, length, pvp, 0,
3332 my_len = data2vp_any(packet, original, secret, 0, attribute, lvalue,
3333 data + 9, data[1] - 9, pvp);
3334 if (my_len < 0) return my_len;
3340 * Create Vendor-Specifc VALUE_PAIRs from a RADIUS attribute.
3342 ssize_t rad_attr2vp_vsa(const RADIUS_PACKET *packet,
3343 const RADIUS_PACKET *original,
3345 const uint8_t *data, size_t length,
3348 size_t dv_type, dv_length;
3353 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3354 fr_strerror_printf("rad_attr2vp_vsa: Invalid length");
3358 if (data[0] != PW_VENDOR_SPECIFIC) {
3359 fr_strerror_printf("rad_attr2vp_vsa: Invalid attribute");
3364 * Not enough room for a Vendor-Id.
3365 * Or the high octet of the Vendor-Id is set.
3367 if ((data[1] < 6) || (data[2] != 0)) {
3368 return rad_attr2vp_raw(packet, original, secret,
3372 memcpy(&lvalue, data + 2, 4);
3373 lvalue = ntohl(lvalue);
3376 * WiMAX gets its own set of magic.
3378 if (lvalue == VENDORPEC_WIMAX) {
3380 return rad_attr2vp_wimax(packet, original, secret,
3384 dv_type = dv_length = 1;
3385 dv = dict_vendorbyvalue(lvalue);
3388 dv_length = dv->length;
3390 if (dv->flags) goto wimax;
3394 * Attribute is not in the correct form.
3396 if (rad_tlv_ok(data + 6, data[1] - 6, dv_type, dv_length) < 0) {
3397 return rad_attr2vp_raw(packet, original, secret,
3401 my_len = attr2vp_vsa(packet, original, secret,
3402 lvalue, dv_type, dv_length,
3403 data + 6, data[1] - 6, pvp);
3404 if (my_len < 0) return my_len;
3407 if (my_len != (data[1] - 6)) {
3409 fr_strerror_printf("rad_attr2vp_vsa: Incomplete decode");
3418 * Create an "extended" VALUE_PAIR from a RADIUS attribute.
3420 ssize_t rad_attr2vp_extended(const RADIUS_PACKET *packet,
3421 const RADIUS_PACKET *original,
3423 const uint8_t *start, size_t length,
3426 unsigned int attribute;
3429 unsigned int vendor = VENDORPEC_EXTENDED;
3430 size_t data_len = length;
3431 const uint8_t *data;
3436 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3437 fr_strerror_printf("rad_attr2vp_extended: Invalid length");
3441 da = dict_attrbyvalue(data[0], vendor);
3443 (!da->flags.extended && !da->flags.extended_flags)) {
3444 fr_strerror_printf("rad_attr2vp_extended: Attribute is not extended format");
3451 * No Extended-Type. It's a raw attribute.
3452 * Also, if there's no data following the Extended-Type,
3453 * it's a raw attribute.
3457 return rad_attr2vp_raw(packet, original, secret, start,
3462 * The attribute is "241.1", for example. Go look that
3463 * up to see what type it is.
3465 attribute = data[0];
3466 attribute |= (data[2] << fr_attr_shift[1]);
3468 da = dict_attrbyvalue(attribute, vendor);
3471 vendor = VENDORPEC_EXTENDED;
3474 if (data[1] < length) data_len = data[1];
3480 * If there's supposed to be a flag octet. If not, it's
3481 * a raw attribute. If the flag is set, it's supposed to
3484 if (da->flags.extended_flags) {
3485 if (data_len == 0) goto raw;
3487 continued = ((data[0] & 0x80) != 0);
3493 * Extended VSAs have 4 octets of
3494 * Vendor-Id followed by one octet of
3497 if (da->flags.evs) {
3498 if (data_len < 5) goto raw;
3501 * Vendor Ids can only be 24-bit.
3503 if (data[0] != 0) goto raw;
3505 vendor = ((data[1] << 16) |
3510 * Pack the *encapsulating* attribute number into
3511 * the vendor id. This number should be >= 241.
3513 vendor |= start[0] * FR_MAX_VENDOR;
3517 * Over-write the attribute with the
3520 attribute = data[4];
3526 int first_offset = 4;
3529 if (vendor != VENDORPEC_EXTENDED) first_offset += 5;
3531 my_len = extended_attrlen(start, start + length);
3532 if (my_len < 0) goto raw;
3534 if (vendor != VENDORPEC_EXTENDED) my_len -= 5;
3536 return data2vp_continued(packet, original, secret,
3537 start, length, pvp, shift,
3539 first_offset, 4, my_len);
3542 if (data2vp_any(packet, original, secret, shift,
3543 attribute, vendor, data, data_len, pvp) < 0) {
3547 return (data + data_len) - start;
3552 * Create a "standard" RFC VALUE_PAIR from the given data.
3554 ssize_t rad_attr2vp_rfc(const RADIUS_PACKET *packet,
3555 const RADIUS_PACKET *original,
3557 const uint8_t *data, size_t length,
3560 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3561 fr_strerror_printf("rad_attr2vp_rfc: Insufficient data");
3565 if (data2vp_any(packet, original, secret, 0,
3566 data[0], 0, data + 2, data[1] - 2, pvp) < 0) {
3574 * Create a "normal" VALUE_PAIR from the given data.
3576 ssize_t rad_attr2vp(const RADIUS_PACKET *packet,
3577 const RADIUS_PACKET *original,
3579 const uint8_t *data, size_t length,
3582 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3583 fr_strerror_printf("rad_attr2vp: Insufficient data");
3588 * VSAs get their own handler.
3590 if (data[0] == PW_VENDOR_SPECIFIC) {
3591 return rad_attr2vp_vsa(packet, original, secret,
3596 * Extended attribute format gets their own handler.
3598 if (dict_attrbyvalue(data[0], VENDORPEC_EXTENDED) != NULL) {
3599 return rad_attr2vp_extended(packet, original, secret,
3603 return rad_attr2vp_rfc(packet, original, secret, data, length, pvp);
3608 * Calculate/check digest, and decode radius attributes.
3610 * -1 on decoding error
3613 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original,
3619 radius_packet_t *hdr;
3620 VALUE_PAIR *head, **tail, *vp;
3623 * Extract attribute-value pairs
3625 hdr = (radius_packet_t *)packet->data;
3627 packet_length = packet->data_len - AUTH_HDR_LEN;
3634 * Loop over the attributes, decoding them into VPs.
3636 while (packet_length > 0) {
3640 * This may return many VPs
3642 my_len = rad_attr2vp(packet, original, secret,
3643 ptr, packet_length, &vp);
3658 * VSA's may not have been counted properly in
3659 * rad_packet_ok() above, as it is hard to count
3660 * then without using the dictionary. We
3661 * therefore enforce the limits here, too.
3663 if ((fr_max_attributes > 0) &&
3664 (num_attributes > fr_max_attributes)) {
3665 char host_ipaddr[128];
3668 fr_strerror_printf("WARNING: Possible DoS attack from host %s: Too many attributes in request (received %d, max %d are allowed).",
3669 inet_ntop(packet->src_ipaddr.af,
3670 &packet->src_ipaddr.ipaddr,
3671 host_ipaddr, sizeof(host_ipaddr)),
3672 num_attributes, fr_max_attributes);
3677 packet_length -= my_len;
3681 * Merge information from the outside world into our
3684 fr_rand_seed(packet->data, AUTH_HDR_LEN);
3687 * There may be VP's already in the packet. Don't
3688 * destroy them. Instead, add the decoded attributes to
3689 * the tail of the list.
3691 for (tail = &packet->vps; *tail != NULL; tail = &((*tail)->next)) {
3703 * We assume that the passwd buffer passed is big enough.
3704 * RFC2138 says the password is max 128 chars, so the size
3705 * of the passwd buffer must be at least 129 characters.
3706 * Preferably it's just MAX_STRING_LEN.
3708 * int *pwlen is updated to the new length of the encrypted
3709 * password - a multiple of 16 bytes.
3711 int rad_pwencode(char *passwd, size_t *pwlen, const char *secret,
3712 const uint8_t *vector)
3714 FR_MD5_CTX context, old;
3715 uint8_t digest[AUTH_VECTOR_LEN];
3716 int i, n, secretlen;
3720 * RFC maximum is 128 bytes.
3722 * If length is zero, pad it out with zeros.
3724 * If the length isn't aligned to 16 bytes,
3725 * zero out the extra data.
3729 if (len > 128) len = 128;
3732 memset(passwd, 0, AUTH_PASS_LEN);
3733 len = AUTH_PASS_LEN;
3734 } else if ((len % AUTH_PASS_LEN) != 0) {
3735 memset(&passwd[len], 0, AUTH_PASS_LEN - (len % AUTH_PASS_LEN));
3736 len += AUTH_PASS_LEN - (len % AUTH_PASS_LEN);
3741 * Use the secret to setup the decryption digest
3743 secretlen = strlen(secret);
3745 fr_MD5Init(&context);
3746 fr_MD5Update(&context, (const uint8_t *) secret, secretlen);
3747 old = context; /* save intermediate work */
3750 * Encrypt it in place. Don't bother checking
3751 * len, as we've ensured above that it's OK.
3753 for (n = 0; n < len; n += AUTH_PASS_LEN) {
3755 fr_MD5Update(&context, vector, AUTH_PASS_LEN);
3756 fr_MD5Final(digest, &context);
3759 fr_MD5Update(&context,
3760 (uint8_t *) passwd + n - AUTH_PASS_LEN,
3762 fr_MD5Final(digest, &context);
3765 for (i = 0; i < AUTH_PASS_LEN; i++) {
3766 passwd[i + n] ^= digest[i];
3776 int rad_pwdecode(char *passwd, size_t pwlen, const char *secret,
3777 const uint8_t *vector)
3779 FR_MD5_CTX context, old;
3780 uint8_t digest[AUTH_VECTOR_LEN];
3782 size_t n, secretlen;
3785 * The RFC's say that the maximum is 128.
3786 * The buffer we're putting it into above is 254, so
3787 * we don't need to do any length checking.
3789 if (pwlen > 128) pwlen = 128;
3794 if (pwlen == 0) goto done;
3797 * Use the secret to setup the decryption digest
3799 secretlen = strlen(secret);
3801 fr_MD5Init(&context);
3802 fr_MD5Update(&context, (const uint8_t *) secret, secretlen);
3803 old = context; /* save intermediate work */
3806 * The inverse of the code above.
3808 for (n = 0; n < pwlen; n += AUTH_PASS_LEN) {
3810 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
3811 fr_MD5Final(digest, &context);
3814 if (pwlen > AUTH_PASS_LEN) {
3815 fr_MD5Update(&context, (uint8_t *) passwd,
3819 fr_MD5Final(digest, &context);
3822 if (pwlen > (n + AUTH_PASS_LEN)) {
3823 fr_MD5Update(&context, (uint8_t *) passwd + n,
3828 for (i = 0; i < AUTH_PASS_LEN; i++) {
3829 passwd[i + n] ^= digest[i];
3834 passwd[pwlen] = '\0';
3835 return strlen(passwd);
3840 * Encode Tunnel-Password attributes when sending them out on the wire.
3842 * int *pwlen is updated to the new length of the encrypted
3843 * password - a multiple of 16 bytes.
3845 * This is per RFC-2868 which adds a two char SALT to the initial intermediate
3848 int rad_tunnel_pwencode(char *passwd, size_t *pwlen, const char *secret,
3849 const uint8_t *vector)
3851 uint8_t buffer[AUTH_VECTOR_LEN + MAX_STRING_LEN + 3];
3852 unsigned char digest[AUTH_VECTOR_LEN];
3854 int i, n, secretlen;
3859 if (len > 127) len = 127;
3862 * Shift the password 3 positions right to place a salt and original
3863 * length, tag will be added automatically on packet send
3865 for (n=len ; n>=0 ; n--) passwd[n+3] = passwd[n];
3869 * save original password length as first password character;
3876 * Generate salt. The RFC's say:
3878 * The high bit of salt[0] must be set, each salt in a
3879 * packet should be unique, and they should be random
3881 * So, we set the high bit, add in a counter, and then
3882 * add in some CSPRNG data. should be OK..
3884 salt[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
3885 (fr_rand() & 0x07));
3886 salt[1] = fr_rand();
3889 * Padd password to multiple of AUTH_PASS_LEN bytes.
3891 n = len % AUTH_PASS_LEN;
3893 n = AUTH_PASS_LEN - n;
3894 for (; n > 0; n--, len++)
3897 /* set new password length */
3901 * Use the secret to setup the decryption digest
3903 secretlen = strlen(secret);
3904 memcpy(buffer, secret, secretlen);
3906 for (n2 = 0; n2 < len; n2+=AUTH_PASS_LEN) {
3908 memcpy(buffer + secretlen, vector, AUTH_VECTOR_LEN);
3909 memcpy(buffer + secretlen + AUTH_VECTOR_LEN, salt, 2);
3910 fr_md5_calc(digest, buffer, secretlen + AUTH_VECTOR_LEN + 2);
3912 memcpy(buffer + secretlen, passwd + n2 - AUTH_PASS_LEN, AUTH_PASS_LEN);
3913 fr_md5_calc(digest, buffer, secretlen + AUTH_PASS_LEN);
3916 for (i = 0; i < AUTH_PASS_LEN; i++) {
3917 passwd[i + n2] ^= digest[i];
3925 * Decode Tunnel-Password encrypted attributes.
3927 * Defined in RFC-2868, this uses a two char SALT along with the
3928 * initial intermediate value, to differentiate it from the
3931 int rad_tunnel_pwdecode(uint8_t *passwd, size_t *pwlen, const char *secret,
3932 const uint8_t *vector)
3934 FR_MD5_CTX context, old;
3935 uint8_t digest[AUTH_VECTOR_LEN];
3937 unsigned i, n, len, reallen;
3942 * We need at least a salt.
3945 fr_strerror_printf("tunnel password is too short");
3950 * There's a salt, but no password. Or, there's a salt
3951 * and a 'data_len' octet. It's wrong, but at least we
3952 * can figure out what it means: the password is empty.
3954 * Note that this means we ignore the 'data_len' field,
3955 * if the attribute length tells us that there's no
3956 * more data. So the 'data_len' field may be wrong,
3965 len -= 2; /* discount the salt */
3968 * Use the secret to setup the decryption digest
3970 secretlen = strlen(secret);
3972 fr_MD5Init(&context);
3973 fr_MD5Update(&context, (const uint8_t *) secret, secretlen);
3974 old = context; /* save intermediate work */
3977 * Set up the initial key:
3979 * b(1) = MD5(secret + vector + salt)
3981 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
3982 fr_MD5Update(&context, passwd, 2);
3985 for (n = 0; n < len; n += AUTH_PASS_LEN) {
3989 fr_MD5Final(digest, &context);
3994 * A quick check: decrypt the first octet
3995 * of the password, which is the
3996 * 'data_len' field. Ensure it's sane.
3998 reallen = passwd[2] ^ digest[0];
3999 if (reallen >= len) {
4000 fr_strerror_printf("tunnel password is too long for the attribute");
4004 fr_MD5Update(&context, passwd + 2, AUTH_PASS_LEN);
4008 fr_MD5Final(digest, &context);
4011 fr_MD5Update(&context, passwd + n + 2, AUTH_PASS_LEN);
4014 for (i = base; i < AUTH_PASS_LEN; i++) {
4015 passwd[n + i - 1] = passwd[n + i + 2] ^ digest[i];
4020 * See make_tunnel_password, above.
4022 if (reallen > 239) reallen = 239;
4025 passwd[reallen] = 0;
4031 * Encode a CHAP password
4033 * FIXME: might not work with Ascend because
4034 * we use vp->length, and Ascend gear likes
4035 * to send an extra '\0' in the string!
4037 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output, int id,
4038 VALUE_PAIR *password)
4042 uint8_t string[MAX_STRING_LEN * 2 + 1];
4043 VALUE_PAIR *challenge;
4046 * Sanity check the input parameters
4048 if ((packet == NULL) || (password == NULL)) {
4053 * Note that the password VP can be EITHER
4054 * a User-Password attribute (from a check-item list),
4055 * or a CHAP-Password attribute (the client asking
4056 * the library to encode it).
4064 memcpy(ptr, password->vp_strvalue, password->length);
4065 ptr += password->length;
4066 i += password->length;
4069 * Use Chap-Challenge pair if present,
4070 * Request-Authenticator otherwise.
4072 challenge = pairfind(packet->vps, PW_CHAP_CHALLENGE, 0);
4074 memcpy(ptr, challenge->vp_strvalue, challenge->length);
4075 i += challenge->length;
4077 memcpy(ptr, packet->vector, AUTH_VECTOR_LEN);
4078 i += AUTH_VECTOR_LEN;
4082 fr_md5_calc((uint8_t *)output + 1, (uint8_t *)string, i);
4089 * Seed the random number generator.
4091 * May be called any number of times.
4093 void fr_rand_seed(const void *data, size_t size)
4098 * Ensure that the pool is initialized.
4100 if (!fr_rand_initialized) {
4103 memset(&fr_rand_pool, 0, sizeof(fr_rand_pool));
4105 fd = open("/dev/urandom", O_RDONLY);
4111 while (total < sizeof(fr_rand_pool.randrsl)) {
4112 this = read(fd, fr_rand_pool.randrsl,
4113 sizeof(fr_rand_pool.randrsl) - total);
4114 if ((this < 0) && (errno != EINTR)) break;
4115 if (this > 0) total += this;
4119 fr_rand_pool.randrsl[0] = fd;
4120 fr_rand_pool.randrsl[1] = time(NULL);
4121 fr_rand_pool.randrsl[2] = errno;
4124 fr_randinit(&fr_rand_pool, 1);
4125 fr_rand_pool.randcnt = 0;
4126 fr_rand_initialized = 1;
4132 * Hash the user data
4135 if (!hash) hash = fr_rand();
4136 hash = fr_hash_update(data, size, hash);
4138 fr_rand_pool.randmem[fr_rand_pool.randcnt] ^= hash;
4143 * Return a 32-bit random number.
4145 uint32_t fr_rand(void)
4150 * Ensure that the pool is initialized.
4152 if (!fr_rand_initialized) {
4153 fr_rand_seed(NULL, 0);
4156 num = fr_rand_pool.randrsl[fr_rand_pool.randcnt++];
4157 if (fr_rand_pool.randcnt >= 256) {
4158 fr_rand_pool.randcnt = 0;
4159 fr_isaac(&fr_rand_pool);
4167 * Allocate a new RADIUS_PACKET
4169 RADIUS_PACKET *rad_alloc(int newvector)
4173 if ((rp = malloc(sizeof(RADIUS_PACKET))) == NULL) {
4174 fr_strerror_printf("out of memory");
4177 memset(rp, 0, sizeof(*rp));
4183 uint32_t hash, base;
4186 * Don't expose the actual contents of the random
4190 for (i = 0; i < AUTH_VECTOR_LEN; i += sizeof(uint32_t)) {
4191 hash = fr_rand() ^ base;
4192 memcpy(rp->vector + i, &hash, sizeof(hash));
4195 fr_rand(); /* stir the pool again */
4200 RADIUS_PACKET *rad_alloc_reply(RADIUS_PACKET *packet)
4202 RADIUS_PACKET *reply;
4204 if (!packet) return NULL;
4206 reply = rad_alloc(0);
4207 if (!reply) return NULL;
4210 * Initialize the fields from the request.
4212 reply->sockfd = packet->sockfd;
4213 reply->dst_ipaddr = packet->src_ipaddr;
4214 reply->src_ipaddr = packet->dst_ipaddr;
4215 reply->dst_port = packet->src_port;
4216 reply->src_port = packet->dst_port;
4217 reply->id = packet->id;
4218 reply->code = 0; /* UNKNOWN code */
4219 memcpy(reply->vector, packet->vector,
4220 sizeof(reply->vector));
4223 reply->data_len = 0;
4230 * Free a RADIUS_PACKET
4232 void rad_free(RADIUS_PACKET **radius_packet_ptr)
4234 RADIUS_PACKET *radius_packet;
4236 if (!radius_packet_ptr || !*radius_packet_ptr) return;
4237 radius_packet = *radius_packet_ptr;
4239 free(radius_packet->data);
4241 pairfree(&radius_packet->vps);
4243 free(radius_packet);
4245 *radius_packet_ptr = NULL;