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);
150 void rad_print_hex(RADIUS_PACKET *packet)
154 if (!packet->data) return;
156 printf(" Code:\t\t%u\n", packet->data[0]);
157 printf(" Id:\t\t%u\n", packet->data[1]);
158 printf(" Length:\t%u\n", ((packet->data[2] << 8) |
160 printf(" Vector:\t");
161 for (i = 4; i < 20; i++) {
162 printf("%02x", packet->data[i]);
166 if (packet->data_len > 20) {
171 total = packet->data_len - 20;
172 ptr = packet->data + 20;
178 if (total < 2) { /* too short */
179 printf("%02x\n", *ptr);
183 if (ptr[1] > total) { /* too long */
184 for (i = 0; i < total; i++) {
185 printf("%02x ", ptr[i]);
190 printf("%02x %02x ", ptr[0], ptr[1]);
191 attrlen = ptr[1] - 2;
195 for (i = 0; i < attrlen; i++) {
196 if ((i > 0) && ((i & 0x0f) == 0x00))
198 printf("%02x ", ptr[i]);
199 if ((i & 0x0f) == 0x0f) printf("\n");
202 if ((attrlen & 0x0f) != 0x00) printf("\n");
212 * Wrapper for sendto which handles sendfromto, IPv6, and all
213 * possible combinations.
215 static int rad_sendto(int sockfd, void *data, size_t data_len, int flags,
216 fr_ipaddr_t *src_ipaddr, int src_port,
217 fr_ipaddr_t *dst_ipaddr, int dst_port)
220 struct sockaddr_storage dst;
221 socklen_t sizeof_dst;
223 #ifdef WITH_UDPFROMTO
224 struct sockaddr_storage src;
225 socklen_t sizeof_src;
227 fr_ipaddr2sockaddr(src_ipaddr, src_port, &src, &sizeof_src);
229 src_port = src_port; /* -Wunused */
232 if (!fr_ipaddr2sockaddr(dst_ipaddr, dst_port, &dst, &sizeof_dst)) {
236 #ifdef WITH_UDPFROMTO
238 * And if they don't specify a source IP address, don't
241 if (((dst_ipaddr->af == AF_INET) || (dst_ipaddr->af == AF_INET6)) &&
242 (src_ipaddr->af != AF_UNSPEC) &&
243 !fr_inaddr_any(src_ipaddr)) {
244 rcode = sendfromto(sockfd, data, data_len, flags,
245 (struct sockaddr *)&src, sizeof_src,
246 (struct sockaddr *)&dst, sizeof_dst);
250 src_ipaddr = src_ipaddr; /* -Wunused */
254 * No udpfromto, fail gracefully.
256 rcode = sendto(sockfd, data, data_len, flags,
257 (struct sockaddr *) &dst, sizeof_dst);
259 DEBUG("rad_send() failed: %s\n", strerror(errno));
266 void rad_recv_discard(int sockfd)
269 struct sockaddr_storage src;
270 socklen_t sizeof_src = sizeof(src);
272 recvfrom(sockfd, header, sizeof(header), 0,
273 (struct sockaddr *)&src, &sizeof_src);
277 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, int *src_port,
280 ssize_t data_len, packet_len;
282 struct sockaddr_storage src;
283 socklen_t sizeof_src = sizeof(src);
285 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
286 (struct sockaddr *)&src, &sizeof_src);
288 if ((errno == EAGAIN) || (errno == EINTR)) return 0;
293 * Too little data is available, discard the packet.
296 recvfrom(sockfd, header, sizeof(header), 0,
297 (struct sockaddr *)&src, &sizeof_src);
300 } else { /* we got 4 bytes of data. */
302 * See how long the packet says it is.
304 packet_len = (header[2] * 256) + header[3];
307 * The length in the packet says it's less than
308 * a RADIUS header length: discard it.
310 if (packet_len < AUTH_HDR_LEN) {
311 recvfrom(sockfd, header, sizeof(header), 0,
312 (struct sockaddr *)&src, &sizeof_src);
316 * Enforce RFC requirements, for sanity.
317 * Anything after 4k will be discarded.
319 } else if (packet_len > MAX_PACKET_LEN) {
320 recvfrom(sockfd, header, sizeof(header), 0,
321 (struct sockaddr *)&src, &sizeof_src);
327 * Convert AF. If unknown, discard packet.
329 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, src_port)) {
330 recvfrom(sockfd, header, sizeof(header), 0,
331 (struct sockaddr *)&src, &sizeof_src);
338 * The packet says it's this long, but the actual UDP
339 * size could still be smaller.
346 * wrapper for recvfrom, which handles recvfromto, IPv6, and all
347 * possible combinations.
349 static ssize_t rad_recvfrom(int sockfd, uint8_t **pbuf, int flags,
350 fr_ipaddr_t *src_ipaddr, uint16_t *src_port,
351 fr_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
353 struct sockaddr_storage src;
354 struct sockaddr_storage dst;
355 socklen_t sizeof_src = sizeof(src);
356 socklen_t sizeof_dst = sizeof(dst);
363 memset(&src, 0, sizeof_src);
364 memset(&dst, 0, sizeof_dst);
367 * Get address family, etc. first, so we know if we
368 * need to do udpfromto.
370 * FIXME: udpfromto also does this, but it's not
371 * a critical problem.
373 if (getsockname(sockfd, (struct sockaddr *)&dst,
374 &sizeof_dst) < 0) return -1;
377 * Read the length of the packet, from the packet.
378 * This lets us allocate the buffer to use for
379 * reading the rest of the packet.
381 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
382 (struct sockaddr *)&src, &sizeof_src);
384 if ((errno == EAGAIN) || (errno == EINTR)) return 0;
389 * Too little data is available, discard the packet.
392 recvfrom(sockfd, header, sizeof(header), flags,
393 (struct sockaddr *)&src, &sizeof_src);
396 } else { /* we got 4 bytes of data. */
398 * See how long the packet says it is.
400 len = (header[2] * 256) + header[3];
403 * The length in the packet says it's less than
404 * a RADIUS header length: discard it.
406 if (len < AUTH_HDR_LEN) {
407 recvfrom(sockfd, header, sizeof(header), flags,
408 (struct sockaddr *)&src, &sizeof_src);
412 * Enforce RFC requirements, for sanity.
413 * Anything after 4k will be discarded.
415 } else if (len > MAX_PACKET_LEN) {
416 recvfrom(sockfd, header, sizeof(header), flags,
417 (struct sockaddr *)&src, &sizeof_src);
426 * Receive the packet. The OS will discard any data in the
427 * packet after "len" bytes.
429 #ifdef WITH_UDPFROMTO
430 if ((dst.ss_family == AF_INET) || (dst.ss_family == AF_INET6)) {
431 data_len = recvfromto(sockfd, buf, len, flags,
432 (struct sockaddr *)&src, &sizeof_src,
433 (struct sockaddr *)&dst, &sizeof_dst);
437 * No udpfromto, fail gracefully.
439 data_len = recvfrom(sockfd, buf, len, flags,
440 (struct sockaddr *)&src, &sizeof_src);
446 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, &port)) {
448 return -1; /* Unknown address family, Die Die Die! */
452 fr_sockaddr2ipaddr(&dst, sizeof_dst, dst_ipaddr, &port);
456 * Different address families should never happen.
458 if (src.ss_family != dst.ss_family) {
464 * Tell the caller about the data
472 #define AUTH_PASS_LEN (AUTH_VECTOR_LEN)
473 /*************************************************************************
475 * Function: make_secret
477 * Purpose: Build an encrypted secret value to return in a reply
478 * packet. The secret is hidden by xoring with a MD5 digest
479 * created from the shared secret and the authentication
480 * vector. We put them into MD5 in the reverse order from
481 * that used when encrypting passwords to RADIUS.
483 *************************************************************************/
484 static void make_secret(uint8_t *digest, const uint8_t *vector,
485 const char *secret, const uint8_t *value)
490 fr_MD5Init(&context);
491 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
492 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
493 fr_MD5Final(digest, &context);
495 for ( i = 0; i < AUTH_VECTOR_LEN; i++ ) {
496 digest[i] ^= value[i];
500 #define MAX_PASS_LEN (128)
501 static void make_passwd(uint8_t *output, ssize_t *outlen,
502 const uint8_t *input, size_t inlen,
503 const char *secret, const uint8_t *vector)
505 FR_MD5_CTX context, old;
506 uint8_t digest[AUTH_VECTOR_LEN];
507 uint8_t passwd[MAX_PASS_LEN];
512 * If the length is zero, round it up.
516 if (len > MAX_PASS_LEN) len = MAX_PASS_LEN;
518 memcpy(passwd, input, len);
519 memset(passwd + len, 0, sizeof(passwd) - len);
525 else if ((len & 0x0f) != 0) {
531 fr_MD5Init(&context);
532 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
538 fr_MD5Update(&context, vector, AUTH_PASS_LEN);
540 for (n = 0; n < len; n += AUTH_PASS_LEN) {
543 fr_MD5Update(&context,
544 passwd + n - AUTH_PASS_LEN,
548 fr_MD5Final(digest, &context);
549 for (i = 0; i < AUTH_PASS_LEN; i++) {
550 passwd[i + n] ^= digest[i];
554 memcpy(output, passwd, len);
557 static void make_tunnel_passwd(uint8_t *output, ssize_t *outlen,
558 const uint8_t *input, size_t inlen, size_t room,
559 const char *secret, const uint8_t *vector)
561 FR_MD5_CTX context, old;
562 uint8_t digest[AUTH_VECTOR_LEN];
563 uint8_t passwd[MAX_STRING_LEN + AUTH_VECTOR_LEN];
570 if (room > 253) room = 253;
573 * Account for 2 bytes of the salt, and round the room
574 * available down to the nearest multiple of 16. Then,
575 * subtract one from that to account for the length byte,
576 * and the resulting number is the upper bound on the data
579 * We could short-cut this calculation just be forcing
580 * inlen to be no more than 239. It would work for all
581 * VSA's, as we don't pack multiple VSA's into one
584 * However, this calculation is more general, if a little
585 * complex. And it will work in the future for all possible
586 * kinds of weird attribute packing.
589 room -= (room & 0x0f);
592 if (inlen > room) inlen = room;
595 * Length of the encrypted data is password length plus
596 * one byte for the length of the password.
599 if ((len & 0x0f) != 0) {
603 *outlen = len + 2; /* account for the salt */
606 * Copy the password over.
608 memcpy(passwd + 3, input, inlen);
609 memset(passwd + 3 + inlen, 0, sizeof(passwd) - 3 - inlen);
612 * Generate salt. The RFC's say:
614 * The high bit of salt[0] must be set, each salt in a
615 * packet should be unique, and they should be random
617 * So, we set the high bit, add in a counter, and then
618 * add in some CSPRNG data. should be OK..
620 passwd[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
622 passwd[1] = fr_rand();
623 passwd[2] = inlen; /* length of the password string */
625 fr_MD5Init(&context);
626 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
629 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
630 fr_MD5Update(&context, &passwd[0], 2);
632 for (n = 0; n < len; n += AUTH_PASS_LEN) {
635 fr_MD5Update(&context,
636 passwd + 2 + n - AUTH_PASS_LEN,
640 fr_MD5Final(digest, &context);
642 for (i = 0; i < AUTH_PASS_LEN; i++) {
643 passwd[i + 2 + n] ^= digest[i];
646 memcpy(output, passwd, len + 2);
649 extern int fr_attr_max_tlv;
650 extern int fr_attr_shift[];
651 extern int fr_attr_mask[];
653 static int do_next_tlv(const VALUE_PAIR *vp, int nest)
655 unsigned int tlv1, tlv2;
657 if (nest > fr_attr_max_tlv) return 0;
662 * Keep encoding TLVs which have the same scope.
663 * e.g. two attributes of:
664 * ATTR.TLV1.TLV2.TLV3 = data1
665 * ATTR.TLV1.TLV2.TLV4 = data2
666 * both get put into a container of "ATTR.TLV1.TLV2"
670 * Nothing to follow, we're done.
672 if (!vp->next) return 0;
675 * Not from the same vendor, skip it.
677 if (vp->vendor != vp->next->vendor) return 0;
680 * In a different TLV space, skip it.
682 tlv1 = vp->attribute;
683 tlv2 = vp->next->attribute;
685 tlv1 &= ((1 << fr_attr_shift[nest]) - 1);
686 tlv2 &= ((1 << fr_attr_shift[nest]) - 1);
688 if (tlv1 != tlv2) return 0;
694 static ssize_t vp2data_any(const RADIUS_PACKET *packet,
695 const RADIUS_PACKET *original,
696 const char *secret, int nest,
697 const VALUE_PAIR **pvp,
698 uint8_t *start, size_t room);
700 static ssize_t vp2attr_rfc(const RADIUS_PACKET *packet,
701 const RADIUS_PACKET *original,
702 const char *secret, const VALUE_PAIR **pvp,
703 unsigned int attribute, uint8_t *ptr, size_t room);
705 static ssize_t vp2data_tlvs(const RADIUS_PACKET *packet,
706 const RADIUS_PACKET *original,
707 const char *secret, int nest,
708 const VALUE_PAIR **pvp,
709 uint8_t *start, size_t room)
712 uint8_t *ptr = start;
713 uint8_t *end = start + room;
716 if (nest > fr_attr_max_tlv) {
717 fr_strerror_printf("vp2data_tlvs: attribute nesting overflow");
723 len = vp2attr_rfc(packet, original, secret, pvp,
724 ((*pvp)->attribute >> fr_attr_shift[nest]) & fr_attr_mask[nest],
727 if (ptr > start) break;
733 if (!do_next_tlv(*pvp, nest)) break;
740 * Encodes the data portion of an attribute.
741 * Returns -1 on error, or the length of the data portion.
743 static ssize_t vp2data_any(const RADIUS_PACKET *packet,
744 const RADIUS_PACKET *original,
745 const char *secret, int nest,
746 const VALUE_PAIR **pvp,
747 uint8_t *start, size_t room)
752 uint8_t *ptr = start;
754 const VALUE_PAIR *vp = *pvp;
757 * See if we need to encode a TLV. The low portion of
758 * the attribute has already been placed into the packer.
759 * If there are still attribute bytes left, then go
760 * encode them as TLVs.
762 * If we cared about the stack, we could unroll the loop.
764 if ((nest > 0) && (nest <= fr_attr_max_tlv) &&
765 ((vp->attribute >> fr_attr_shift[nest]) != 0)) {
766 return vp2data_tlvs(packet, original, secret, nest, pvp,
771 * Set up the default sources for the data.
773 data = vp->vp_octets;
780 case PW_TYPE_IPV6ADDR:
781 case PW_TYPE_IPV6PREFIX:
782 case PW_TYPE_ABINARY:
783 /* nothing more to do */
787 len = 1; /* just in case */
788 array[0] = vp->vp_integer & 0xff;
793 len = 2; /* just in case */
794 array[0] = (vp->vp_integer >> 8) & 0xff;
795 array[1] = vp->vp_integer & 0xff;
799 case PW_TYPE_INTEGER:
800 len = 4; /* just in case */
801 lvalue = htonl(vp->vp_integer);
802 memcpy(array, &lvalue, sizeof(lvalue));
807 data = (const uint8_t *) &vp->vp_ipaddr;
808 len = 4; /* just in case */
812 * There are no tagged date attributes.
815 lvalue = htonl(vp->vp_date);
816 data = (const uint8_t *) &lvalue;
817 len = 4; /* just in case */
824 len = 4; /* just in case */
825 slvalue = htonl(vp->vp_signed);
826 memcpy(array, &slvalue, sizeof(slvalue));
833 fr_strerror_printf("ERROR: Cannot encode NULL TLV");
839 default: /* unknown type: ignore it */
840 fr_strerror_printf("ERROR: Unknown attribute type %d", vp->type);
845 * Bound the data to the calling size
847 if (len > (ssize_t) room) len = room;
850 * Encrypt the various password styles
852 * Attributes with encrypted values MUST be less than
855 switch (vp->flags.encrypt) {
856 case FLAG_ENCRYPT_USER_PASSWORD:
857 make_passwd(ptr, &len, data, len,
858 secret, packet->vector);
861 case FLAG_ENCRYPT_TUNNEL_PASSWORD:
863 if (vp->flags.has_tag) lvalue = 1;
866 * Check if there's enough room. If there isn't,
867 * we discard the attribute.
869 * This is ONLY a problem if we have multiple VSA's
870 * in one Vendor-Specific, though.
872 if (room < (18 + lvalue)) return 0;
874 switch (packet->code) {
875 case PW_AUTHENTICATION_ACK:
876 case PW_AUTHENTICATION_REJECT:
877 case PW_ACCESS_CHALLENGE:
880 fr_strerror_printf("ERROR: No request packet, cannot encrypt %s attribute in the vp.", vp->name);
884 if (lvalue) ptr[0] = vp->flags.tag;
885 make_tunnel_passwd(ptr + lvalue, &len, data, len,
887 secret, original->vector);
889 case PW_ACCOUNTING_REQUEST:
890 case PW_DISCONNECT_REQUEST:
892 ptr[0] = vp->flags.tag;
893 make_tunnel_passwd(ptr + 1, &len, data, len - 1, room,
894 secret, packet->vector);
900 * The code above ensures that this attribute
903 case FLAG_ENCRYPT_ASCEND_SECRET:
904 make_secret(ptr, packet->vector, secret, data);
905 len = AUTH_VECTOR_LEN;
910 if (vp->flags.has_tag && TAG_VALID(vp->flags.tag)) {
911 if (vp->type == PW_TYPE_STRING) {
912 if (len > ((ssize_t) (room - 1))) len = room - 1;
913 ptr[0] = vp->flags.tag;
915 } else if (vp->type == PW_TYPE_INTEGER) {
916 array[0] = vp->flags.tag;
917 } /* else it can't be any other type */
919 memcpy(ptr, data, len);
921 } /* switch over encryption flags */
924 return len + (ptr - start);;
927 static ssize_t attr_shift(const uint8_t *start, const uint8_t *end,
928 uint8_t *ptr, int hdr_len, ssize_t len,
929 int flag_offset, int vsa_offset)
931 int check_len = len - ptr[1];
932 int total = len + hdr_len;
935 * Pass 1: Check if the addition of the headers
936 * overflows the available room. If so, return
937 * what we were capable of encoding.
940 while (check_len > (255 - hdr_len)) {
942 check_len -= (255 - hdr_len);
946 * Note that this results in a number of attributes maybe
947 * being marked as "encoded", but which aren't in the
948 * packet. Oh well. The solution is to fix the
949 * "vp2data_any" function to take into account the header
952 if ((ptr + ptr[1] + total) > end) {
953 return (ptr + ptr[1]) - start;
957 * Pass 2: Now that we know there's enough room,
958 * re-arrange the data to form a set of valid
962 int sublen = 255 - ptr[1];
969 memmove(ptr + 255 + hdr_len, ptr + 255, sublen);
970 memcpy(ptr + 255, ptr, hdr_len);
972 if (vsa_offset) ptr[vsa_offset] += sublen;
973 ptr[flag_offset] |= 0x80;
977 if (vsa_offset) ptr[vsa_offset] = 3;
981 if (vsa_offset) ptr[vsa_offset] += len;
983 return (ptr + ptr[1]) - start;
988 * Encode an "extended" attribute.
990 int rad_vp2extended(const RADIUS_PACKET *packet,
991 const RADIUS_PACKET *original,
992 const char *secret, const VALUE_PAIR **pvp,
993 uint8_t *ptr, size_t room)
998 uint8_t *start = ptr;
999 const VALUE_PAIR *vp = *pvp;
1001 if (vp->vendor < VENDORPEC_EXTENDED) {
1002 fr_strerror_printf("rad_vp2extended called for non-extended attribute");
1006 if (room < 3) return 0;
1008 ptr[0] = vp->attribute & 0xff;
1011 if (vp->flags.extended) {
1012 ptr[2] = (vp->attribute & 0xff00) >> 8;
1014 } else if (vp->flags.extended_flags) {
1015 if (room < 4) return 0;
1018 ptr[2] = (vp->attribute & 0xff00) >> 8;
1023 * Only "flagged" attributes can be longer than one
1026 if (!vp->flags.extended_flags && (room > 255)) {
1033 if (vp->flags.evs) {
1034 uint8_t *evs = ptr + ptr[1];
1036 if (room < (size_t) (ptr[1] + 5)) return 0;
1039 * RADIUS Attribute Type is packed into the high byte
1040 * of the Vendor Id. So over-write it in the packet.
1042 * And hard-code Extended-Type to Vendor-Specific.
1044 ptr[0] = (vp->vendor >> 24) & 0xff;
1047 evs[0] = 0; /* always zero */
1048 evs[1] = (vp->vendor >> 16) & 0xff;
1049 evs[2] = (vp->vendor >> 8) & 0xff;
1050 evs[3] = vp->vendor & 0xff;
1051 evs[4] = vp->attribute & 0xff;
1058 len = vp2data_any(packet, original, secret, nest,
1059 pvp, ptr + ptr[1], room - hdr_len);
1060 if (len < 0) return len;
1063 * There may be more than 252 octets of data encoded in
1064 * the attribute. If so, move the data up in the packet,
1065 * and copy the existing header over. Set the "M" flag ONLY
1066 * after copying the rest of the data.
1068 if (vp->flags.extended_flags && (len > (255 - ptr[1]))) {
1069 return attr_shift(start, start + room, ptr, 4, len, 3, 0);
1074 return (ptr + ptr[1]) - start;
1079 * Encode a WiMAX attribute.
1081 int rad_vp2wimax(const RADIUS_PACKET *packet,
1082 const RADIUS_PACKET *original,
1083 const char *secret, const VALUE_PAIR **pvp,
1084 uint8_t *ptr, size_t room)
1089 uint8_t *start = ptr;
1090 const VALUE_PAIR *vp = *pvp;
1093 * Double-check for WiMAX format.
1095 if (!vp->flags.wimax) {
1096 fr_strerror_printf("rad_vp2wimax called for non-WIMAX VSA");
1101 * Not enough room for:
1102 * attr, len, vendor-id, vsa, vsalen, continuation
1104 if (room < 9) return 0;
1107 * Build the Vendor-Specific header
1110 ptr[0] = PW_VENDOR_SPECIFIC;
1112 lvalue = htonl(vp->vendor);
1113 memcpy(ptr + 2, &lvalue, 4);
1114 ptr[6] = (vp->attribute & fr_attr_mask[1]);
1116 ptr[8] = 0; /* continuation byte */
1120 len = vp2data_any(packet, original, secret, 1, pvp, ptr + ptr[1],
1122 if (len <= 0) return len;
1125 * There may be more than 252 octets of data encoded in
1126 * the attribute. If so, move the data up in the packet,
1127 * and copy the existing header over. Set the "C" flag
1128 * ONLY after copying the rest of the data.
1130 if (len > (255 - ptr[1])) {
1131 return attr_shift(start, start + room, ptr, hdr_len, len, 8, 7);
1137 return (ptr + ptr[1]) - start;
1141 * Encode an RFC format TLV. This could be a standard attribute,
1142 * or a TLV data type. If it's a standard attribute, then
1143 * vp->attribute == attribute. Otherwise, attribute may be
1146 static ssize_t vp2attr_rfc(const RADIUS_PACKET *packet,
1147 const RADIUS_PACKET *original,
1148 const char *secret, const VALUE_PAIR **pvp,
1149 unsigned int attribute, uint8_t *ptr, size_t room)
1153 if (room < 2) return 0;
1155 ptr[0] = attribute & 0xff;
1158 if (room > ((unsigned) 255 - ptr[1])) room = 255 - ptr[1];
1160 len = vp2data_any(packet, original, secret, 0, pvp, ptr + ptr[1], room);
1161 if (len < 0) return len;
1170 * Encode a VSA which is a TLV. If it's in the RFC format, call
1171 * vp2attr_rfc. Otherwise, encode it here.
1173 static ssize_t vp2attr_vsa(const RADIUS_PACKET *packet,
1174 const RADIUS_PACKET *original,
1175 const char *secret, const VALUE_PAIR **pvp,
1176 unsigned int attribute, unsigned int vendor,
1177 uint8_t *ptr, size_t room)
1181 const VALUE_PAIR *vp = *pvp;
1184 * Unknown vendor: RFC format.
1185 * Known vendor and RFC format: go do that.
1187 VP_TRACE("Encoding VSA %u.%u\n", vendor, attribute);
1188 dv = dict_vendorbyvalue(vendor);
1189 VP_TRACE("Flags %d %d\n", vp->flags.is_tlv, vp->flags.has_tlv);
1191 (!vp->flags.is_tlv && (dv->type == 1) && (dv->length == 1))) {
1192 VP_TRACE("Encoding RFC %u.%u\n", vendor, attribute);
1193 return vp2attr_rfc(packet, original, secret, pvp,
1194 attribute, ptr, room);
1197 if (vp->flags.is_tlv) {
1198 VP_TRACE("Encoding TLV %u.%u\n", vendor, attribute);
1199 return vp2data_tlvs(packet, original, secret, 0, pvp,
1205 fr_strerror_printf("vp2attr_vsa: Internal sanity check failed,"
1206 " type %u", (unsigned) dv->type);
1210 ptr[0] = 0; /* attr must be 24-bit */
1211 ptr[1] = (attribute >> 16) & 0xff;
1212 ptr[2] = (attribute >> 8) & 0xff;
1213 ptr[3] = attribute & 0xff;
1217 ptr[0] = (attribute >> 8) & 0xff;
1218 ptr[1] = attribute & 0xff;
1222 ptr[0] = attribute & 0xff;
1226 switch (dv->length) {
1228 fr_strerror_printf("vp2attr_vsa: Internal sanity check failed,"
1229 " length %u", (unsigned) dv->length);
1240 ptr[dv->type + dv->length - 1] = dv->type + dv->length;
1245 if (room > ((unsigned) 255 - (dv->type + dv->length))) {
1246 room = 255 - (dv->type + dv->length);
1249 len = vp2data_any(packet, original, secret, 0, pvp,
1250 ptr + dv->type + dv->length, room);
1251 if (len < 0) return len;
1253 if (dv->length) ptr[dv->type + dv->length - 1] += len;
1255 return dv->type + dv->length + len;
1260 * Encode a Vendor-Specific attribute.
1262 int rad_vp2vsa(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1263 const char *secret, const VALUE_PAIR **pvp, uint8_t *ptr,
1268 const VALUE_PAIR *vp = *pvp;
1271 * Double-check for WiMAX format.
1273 if (vp->flags.wimax) {
1274 return rad_vp2wimax(packet, original, secret, pvp,
1278 if (vp->vendor > FR_MAX_VENDOR) {
1279 fr_strerror_printf("rad_vp2vsa: Invalid arguments");
1284 * Not enough room for:
1285 * attr, len, vendor-id
1287 if (room < 6) return 0;
1290 * Build the Vendor-Specific header
1292 ptr[0] = PW_VENDOR_SPECIFIC;
1294 lvalue = htonl(vp->vendor);
1295 memcpy(ptr + 2, &lvalue, 4);
1297 if (room > ((unsigned) 255 - ptr[1])) room = 255 - ptr[1];
1299 len = vp2attr_vsa(packet, original, secret, pvp,
1300 vp->attribute, vp->vendor,
1301 ptr + ptr[1], room);
1302 if (len < 0) return len;
1311 * Encode an RFC standard attribute 1..255
1313 int rad_vp2rfc(const RADIUS_PACKET *packet,
1314 const RADIUS_PACKET *original,
1315 const char *secret, const VALUE_PAIR **pvp,
1316 uint8_t *ptr, size_t room)
1318 const VALUE_PAIR *vp = *pvp;
1320 if (vp->vendor != 0) {
1321 fr_strerror_printf("rad_vp2rfc called with VSA");
1325 if ((vp->attribute == 0) || (vp->attribute > 255)) {
1326 fr_strerror_printf("rad_vp2rfc called with non-standard attribute %u", vp->attribute);
1330 if ((vp->length == 0) &&
1331 (vp->attribute != PW_CHARGEABLE_USER_IDENTITY)) {
1335 return vp2attr_rfc(packet, original, secret, pvp, vp->attribute,
1341 * Parse a data structure into a RADIUS attribute.
1343 int rad_vp2attr(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1344 const char *secret, const VALUE_PAIR **pvp, uint8_t *start,
1347 const VALUE_PAIR *vp;
1349 if (!pvp || !*pvp || !start || (room <= 2)) return -1;
1354 * RFC format attributes take the fast path.
1356 if (vp->vendor == 0) {
1357 if (vp->attribute > 255) return 0;
1360 * Message-Authenticator is hard-coded.
1362 if (vp->attribute == PW_MESSAGE_AUTHENTICATOR) {
1363 if (room < 18) return -1;
1365 fprintf(stderr, "M-A!\n");
1366 if (room < 16) return -1;
1368 start[0] = PW_MESSAGE_AUTHENTICATOR;
1370 memset(start + 2, 0, 16);
1371 *pvp = (*pvp)->next;
1375 return rad_vp2rfc(packet, original, secret, pvp,
1379 if (vp->vendor > FR_MAX_VENDOR) {
1380 return rad_vp2extended(packet, original, secret, pvp,
1384 if (vp->flags.wimax) {
1385 return rad_vp2wimax(packet, original, secret, pvp,
1389 return rad_vp2vsa(packet, original, secret, pvp,
1397 int rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1400 radius_packet_t *hdr;
1402 uint16_t total_length;
1404 const VALUE_PAIR *reply;
1406 char ip_buffer[128];
1409 * A 4K packet, aligned on 64-bits.
1411 uint64_t data[MAX_PACKET_LEN / sizeof(uint64_t)];
1413 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
1414 what = fr_packet_codes[packet->code];
1419 DEBUG("Sending %s of id %d to %s port %d\n",
1421 inet_ntop(packet->dst_ipaddr.af,
1422 &packet->dst_ipaddr.ipaddr,
1423 ip_buffer, sizeof(ip_buffer)),
1427 * Double-check some things based on packet code.
1429 switch (packet->code) {
1430 case PW_AUTHENTICATION_ACK:
1431 case PW_AUTHENTICATION_REJECT:
1432 case PW_ACCESS_CHALLENGE:
1434 fr_strerror_printf("ERROR: Cannot sign response packet without a request packet.");
1440 * These packet vectors start off as all zero.
1442 case PW_ACCOUNTING_REQUEST:
1443 case PW_DISCONNECT_REQUEST:
1444 case PW_COA_REQUEST:
1445 memset(packet->vector, 0, sizeof(packet->vector));
1453 * Use memory on the stack, until we know how
1454 * large the packet will be.
1456 hdr = (radius_packet_t *) data;
1459 * Build standard header
1461 hdr->code = packet->code;
1462 hdr->id = packet->id;
1464 memcpy(hdr->vector, packet->vector, sizeof(hdr->vector));
1466 total_length = AUTH_HDR_LEN;
1469 * Load up the configuration values for the user
1475 * FIXME: Loop twice over the reply list. The first time,
1476 * calculate the total length of data. The second time,
1477 * allocate the memory, and fill in the VP's.
1479 * Hmm... this may be slower than just doing a small
1484 * Loop over the reply attributes for the packet.
1486 reply = packet->vps;
1489 * Ignore non-wire attributes, but allow extended
1492 if ((reply->vendor == 0) &&
1493 ((reply->attribute & 0xFFFF) >= 256) &&
1494 !reply->flags.extended && !reply->flags.extended_flags) {
1497 * Permit the admin to send BADLY formatted
1498 * attributes with a debug build.
1500 if (reply->attribute == PW_RAW_ATTRIBUTE) {
1501 memcpy(ptr, reply->vp_octets, reply->length);
1502 len = reply->length;
1503 reply = reply->next;
1507 reply = reply->next;
1512 * Set the Message-Authenticator to the correct
1513 * length and initial value.
1515 if (reply->attribute == PW_MESSAGE_AUTHENTICATOR) {
1517 * Cache the offset to the
1518 * Message-Authenticator
1520 packet->offset = total_length;
1524 * Print out ONLY the attributes which
1525 * we're sending over the wire, and print
1526 * them out BEFORE they're encrypted.
1530 len = rad_vp2attr(packet, original, secret, &reply, ptr,
1531 ((uint8_t *) data) + sizeof(data) - ptr);
1532 if (len < 0) return -1;
1535 * Failed to encode the attribute, likely because
1536 * the packet is full.
1539 (total_length > (sizeof(data) - 2 - reply->length))) {
1540 DEBUG("WARNING: Attributes are too long for packet. Discarding data past %d bytes", total_length);
1545 next: /* Used only for Raw-Attribute */
1548 total_length += len;
1549 } /* done looping over all attributes */
1552 * Fill in the rest of the fields, and copy the data over
1553 * from the local stack to the newly allocated memory.
1555 * Yes, all this 'memcpy' is slow, but it means
1556 * that we only allocate the minimum amount of
1557 * memory for a request.
1559 packet->data_len = total_length;
1560 packet->data = (uint8_t *) malloc(packet->data_len);
1561 if (!packet->data) {
1562 fr_strerror_printf("Out of memory");
1566 memcpy(packet->data, hdr, packet->data_len);
1567 hdr = (radius_packet_t *) packet->data;
1569 total_length = htons(total_length);
1570 memcpy(hdr->length, &total_length, sizeof(total_length));
1577 * Sign a previously encoded packet.
1579 int rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1582 radius_packet_t *hdr = (radius_packet_t *)packet->data;
1585 * It wasn't assigned an Id, this is bad!
1587 if (packet->id < 0) {
1588 fr_strerror_printf("ERROR: RADIUS packets must be assigned an Id.");
1592 if (!packet->data || (packet->data_len < AUTH_HDR_LEN) ||
1593 (packet->offset < 0)) {
1594 fr_strerror_printf("ERROR: You must call rad_encode() before rad_sign()");
1599 * If there's a Message-Authenticator, update it
1600 * now, BEFORE updating the authentication vector.
1602 if (packet->offset > 0) {
1603 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
1605 switch (packet->code) {
1606 case PW_ACCOUNTING_REQUEST:
1607 case PW_ACCOUNTING_RESPONSE:
1608 case PW_DISCONNECT_REQUEST:
1609 case PW_DISCONNECT_ACK:
1610 case PW_DISCONNECT_NAK:
1611 case PW_COA_REQUEST:
1614 memset(hdr->vector, 0, AUTH_VECTOR_LEN);
1617 case PW_AUTHENTICATION_ACK:
1618 case PW_AUTHENTICATION_REJECT:
1619 case PW_ACCESS_CHALLENGE:
1621 fr_strerror_printf("ERROR: Cannot sign response packet without a request packet.");
1624 memcpy(hdr->vector, original->vector,
1628 default: /* others have vector already set to zero */
1634 * Set the authentication vector to zero,
1635 * calculate the signature, and put it
1636 * into the Message-Authenticator
1639 fr_hmac_md5(packet->data, packet->data_len,
1640 (const uint8_t *) secret, strlen(secret),
1642 memcpy(packet->data + packet->offset + 2,
1643 calc_auth_vector, AUTH_VECTOR_LEN);
1646 * Copy the original request vector back
1647 * to the raw packet.
1649 memcpy(hdr->vector, packet->vector, AUTH_VECTOR_LEN);
1653 * Switch over the packet code, deciding how to
1656 switch (packet->code) {
1658 * Request packets are not signed, bur
1659 * have a random authentication vector.
1661 case PW_AUTHENTICATION_REQUEST:
1662 case PW_STATUS_SERVER:
1666 * Reply packets are signed with the
1667 * authentication vector of the request.
1674 fr_MD5Init(&context);
1675 fr_MD5Update(&context, packet->data, packet->data_len);
1676 fr_MD5Update(&context, (const uint8_t *) secret,
1678 fr_MD5Final(digest, &context);
1680 memcpy(hdr->vector, digest, AUTH_VECTOR_LEN);
1681 memcpy(packet->vector, digest, AUTH_VECTOR_LEN);
1684 }/* switch over packet codes */
1690 * Reply to the request. Also attach
1691 * reply attribute value pairs and any user message provided.
1693 int rad_send(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1698 char ip_buffer[128];
1701 * Maybe it's a fake packet. Don't send it.
1703 if (!packet || (packet->sockfd < 0)) {
1707 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
1708 what = fr_packet_codes[packet->code];
1714 * First time through, allocate room for the packet
1716 if (!packet->data) {
1718 * Encode the packet.
1720 if (rad_encode(packet, original, secret) < 0) {
1725 * Re-sign it, including updating the
1726 * Message-Authenticator.
1728 if (rad_sign(packet, original, secret) < 0) {
1733 * If packet->data points to data, then we print out
1734 * the VP list again only for debugging.
1736 } else if (fr_debug_flag) {
1737 DEBUG("Sending %s of id %d to %s port %d\n", what, packet->id,
1738 inet_ntop(packet->dst_ipaddr.af,
1739 &packet->dst_ipaddr.ipaddr,
1740 ip_buffer, sizeof(ip_buffer)),
1743 for (reply = packet->vps; reply; reply = reply->next) {
1744 if ((reply->vendor == 0) &&
1745 ((reply->attribute & 0xFFFF) > 0xff)) continue;
1751 * And send it on it's way.
1753 return rad_sendto(packet->sockfd, packet->data, packet->data_len, 0,
1754 &packet->src_ipaddr, packet->src_port,
1755 &packet->dst_ipaddr, packet->dst_port);
1759 * Do a comparison of two authentication digests by comparing
1760 * the FULL digest. Otehrwise, the server can be subject to
1761 * timing attacks that allow attackers find a valid message
1764 * http://www.cs.rice.edu/~dwallach/pub/crosby-timing2009.pdf
1766 int rad_digest_cmp(const uint8_t *a, const uint8_t *b, size_t length)
1771 for (i = 0; i < length; i++) {
1772 result |= a[i] ^ b[i];
1775 return result; /* 0 is OK, !0 is !OK, just like memcmp */
1780 * Validates the requesting client NAS. Calculates the
1781 * signature based on the clients private key.
1783 static int calc_acctdigest(RADIUS_PACKET *packet, const char *secret)
1785 uint8_t digest[AUTH_VECTOR_LEN];
1789 * Zero out the auth_vector in the received packet.
1790 * Then append the shared secret to the received packet,
1791 * and calculate the MD5 sum. This must be the same
1792 * as the original MD5 sum (packet->vector).
1794 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
1797 * MD5(packet + secret);
1799 fr_MD5Init(&context);
1800 fr_MD5Update(&context, packet->data, packet->data_len);
1801 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
1802 fr_MD5Final(digest, &context);
1805 * Return 0 if OK, 2 if not OK.
1807 if (rad_digest_cmp(digest, packet->vector, AUTH_VECTOR_LEN) != 0) return 2;
1813 * Validates the requesting client NAS. Calculates the
1814 * signature based on the clients private key.
1816 static int calc_replydigest(RADIUS_PACKET *packet, RADIUS_PACKET *original,
1819 uint8_t calc_digest[AUTH_VECTOR_LEN];
1825 if (original == NULL) {
1830 * Copy the original vector in place.
1832 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
1835 * MD5(packet + secret);
1837 fr_MD5Init(&context);
1838 fr_MD5Update(&context, packet->data, packet->data_len);
1839 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
1840 fr_MD5Final(calc_digest, &context);
1843 * Copy the packet's vector back to the packet.
1845 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
1848 * Return 0 if OK, 2 if not OK.
1850 if (rad_digest_cmp(packet->vector, calc_digest, AUTH_VECTOR_LEN) != 0) return 2;
1856 * Check if a set of RADIUS formatted TLVs are OK.
1858 int rad_tlv_ok(const uint8_t *data, size_t length,
1859 size_t dv_type, size_t dv_length)
1861 const uint8_t *end = data + length;
1863 if ((dv_length > 2) || (dv_type == 0) || (dv_type > 4)) {
1864 fr_strerror_printf("rad_tlv_ok: Invalid arguments");
1868 while (data < end) {
1871 if ((data + dv_type + dv_length) > end) {
1872 fr_strerror_printf("Attribute header overflow");
1878 if ((data[0] == 0) && (data[1] == 0) &&
1879 (data[2] == 0) && (data[3] == 0)) {
1881 fr_strerror_printf("Invalid attribute 0");
1886 fr_strerror_printf("Invalid attribute > 2^24");
1892 if ((data[1] == 0) && (data[1] == 0)) goto zero;
1896 if (data[0] == 0) goto zero;
1900 fr_strerror_printf("Internal sanity check failed");
1904 switch (dv_length) {
1909 if (data[dv_type + 1] != 0) {
1910 fr_strerror_printf("Attribute is longer than 256 octets");
1915 attrlen = data[dv_type + dv_length - 1];
1920 fr_strerror_printf("Internal sanity check failed");
1924 if (attrlen < (dv_type + dv_length)) {
1925 fr_strerror_printf("Attribute header has invalid length");
1929 if (attrlen > length) {
1930 fr_strerror_printf("Attribute overflows container");
1943 * See if the data pointed to by PTR is a valid RADIUS packet.
1945 * packet is not 'const * const' because we may update data_len,
1946 * if there's more data in the UDP packet than in the RADIUS packet.
1948 int rad_packet_ok(RADIUS_PACKET *packet, int flags)
1953 radius_packet_t *hdr;
1954 char host_ipaddr[128];
1960 * Check for packets smaller than the packet header.
1962 * RFC 2865, Section 3., subsection 'length' says:
1964 * "The minimum length is 20 ..."
1966 if (packet->data_len < AUTH_HDR_LEN) {
1967 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (received %d < minimum %d)",
1968 inet_ntop(packet->src_ipaddr.af,
1969 &packet->src_ipaddr.ipaddr,
1970 host_ipaddr, sizeof(host_ipaddr)),
1971 (int) packet->data_len, AUTH_HDR_LEN);
1976 * RFC 2865, Section 3., subsection 'length' says:
1978 * " ... and maximum length is 4096."
1980 if (packet->data_len > MAX_PACKET_LEN) {
1981 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too long (received %d > maximum %d)",
1982 inet_ntop(packet->src_ipaddr.af,
1983 &packet->src_ipaddr.ipaddr,
1984 host_ipaddr, sizeof(host_ipaddr)),
1985 (int) packet->data_len, MAX_PACKET_LEN);
1990 * Check for packets with mismatched size.
1991 * i.e. We've received 128 bytes, and the packet header
1992 * says it's 256 bytes long.
1994 totallen = (packet->data[2] << 8) | packet->data[3];
1995 hdr = (radius_packet_t *)packet->data;
1998 * Code of 0 is not understood.
1999 * Code of 16 or greate is not understood.
2001 if ((hdr->code == 0) ||
2002 (hdr->code >= FR_MAX_PACKET_CODE)) {
2003 fr_strerror_printf("WARNING: Bad RADIUS packet from host %s: unknown packet code%d ",
2004 inet_ntop(packet->src_ipaddr.af,
2005 &packet->src_ipaddr.ipaddr,
2006 host_ipaddr, sizeof(host_ipaddr)),
2012 * Message-Authenticator is required in Status-Server
2013 * packets, otherwise they can be trivially forged.
2015 if (hdr->code == PW_STATUS_SERVER) require_ma = 1;
2018 * It's also required if the caller asks for it.
2020 if (flags) require_ma = 1;
2023 * Repeat the length checks. This time, instead of
2024 * looking at the data we received, look at the value
2025 * of the 'length' field inside of the packet.
2027 * Check for packets smaller than the packet header.
2029 * RFC 2865, Section 3., subsection 'length' says:
2031 * "The minimum length is 20 ..."
2033 if (totallen < AUTH_HDR_LEN) {
2034 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (length %d < minimum %d)",
2035 inet_ntop(packet->src_ipaddr.af,
2036 &packet->src_ipaddr.ipaddr,
2037 host_ipaddr, sizeof(host_ipaddr)),
2038 totallen, AUTH_HDR_LEN);
2043 * And again, for the value of the 'length' field.
2045 * RFC 2865, Section 3., subsection 'length' says:
2047 * " ... and maximum length is 4096."
2049 if (totallen > MAX_PACKET_LEN) {
2050 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too long (length %d > maximum %d)",
2051 inet_ntop(packet->src_ipaddr.af,
2052 &packet->src_ipaddr.ipaddr,
2053 host_ipaddr, sizeof(host_ipaddr)),
2054 totallen, MAX_PACKET_LEN);
2059 * RFC 2865, Section 3., subsection 'length' says:
2061 * "If the packet is shorter than the Length field
2062 * indicates, it MUST be silently discarded."
2064 * i.e. No response to the NAS.
2066 if (packet->data_len < totallen) {
2067 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: received %d octets, packet length says %d",
2068 inet_ntop(packet->src_ipaddr.af,
2069 &packet->src_ipaddr.ipaddr,
2070 host_ipaddr, sizeof(host_ipaddr)),
2071 (int) packet->data_len, totallen);
2076 * RFC 2865, Section 3., subsection 'length' says:
2078 * "Octets outside the range of the Length field MUST be
2079 * treated as padding and ignored on reception."
2081 if (packet->data_len > totallen) {
2083 * We're shortening the packet below, but just
2084 * to be paranoid, zero out the extra data.
2086 memset(packet->data + totallen, 0, packet->data_len - totallen);
2087 packet->data_len = totallen;
2091 * Walk through the packet's attributes, ensuring that
2092 * they add up EXACTLY to the size of the packet.
2094 * If they don't, then the attributes either under-fill
2095 * or over-fill the packet. Any parsing of the packet
2096 * is impossible, and will result in unknown side effects.
2098 * This would ONLY happen with buggy RADIUS implementations,
2099 * or with an intentional attack. Either way, we do NOT want
2100 * to be vulnerable to this problem.
2103 count = totallen - AUTH_HDR_LEN;
2108 * We need at least 2 bytes to check the
2112 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: attribute header overflows the packet",
2113 inet_ntop(packet->src_ipaddr.af,
2114 &packet->src_ipaddr.ipaddr,
2115 host_ipaddr, sizeof(host_ipaddr)));
2120 * Attribute number zero is NOT defined.
2123 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: Invalid attribute 0",
2124 inet_ntop(packet->src_ipaddr.af,
2125 &packet->src_ipaddr.ipaddr,
2126 host_ipaddr, sizeof(host_ipaddr)));
2131 * Attributes are at LEAST as long as the ID & length
2132 * fields. Anything shorter is an invalid attribute.
2135 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: attribute %u too short",
2136 inet_ntop(packet->src_ipaddr.af,
2137 &packet->src_ipaddr.ipaddr,
2138 host_ipaddr, sizeof(host_ipaddr)),
2144 * If there are fewer bytes in the packet than in the
2145 * attribute, it's a bad packet.
2147 if (count < attr[1]) {
2148 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: attribute %u data overflows the packet",
2149 inet_ntop(packet->src_ipaddr.af,
2150 &packet->src_ipaddr.ipaddr,
2151 host_ipaddr, sizeof(host_ipaddr)),
2157 * Sanity check the attributes for length.
2160 default: /* don't do anything by default */
2164 * If there's an EAP-Message, we require
2165 * a Message-Authenticator.
2167 case PW_EAP_MESSAGE:
2171 case PW_MESSAGE_AUTHENTICATOR:
2172 if (attr[1] != 2 + AUTH_VECTOR_LEN) {
2173 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: Message-Authenticator has invalid length %d",
2174 inet_ntop(packet->src_ipaddr.af,
2175 &packet->src_ipaddr.ipaddr,
2176 host_ipaddr, sizeof(host_ipaddr)),
2185 * FIXME: Look up the base 255 attributes in the
2186 * dictionary, and switch over their type. For
2187 * integer/date/ip, the attribute length SHOULD
2190 count -= attr[1]; /* grab the attribute length */
2192 num_attributes++; /* seen one more attribute */
2196 * If the attributes add up to a packet, it's allowed.
2198 * If not, we complain, and throw the packet away.
2201 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: packet attributes do NOT exactly fill the packet",
2202 inet_ntop(packet->src_ipaddr.af,
2203 &packet->src_ipaddr.ipaddr,
2204 host_ipaddr, sizeof(host_ipaddr)));
2209 * If we're configured to look for a maximum number of
2210 * attributes, and we've seen more than that maximum,
2211 * then throw the packet away, as a possible DoS.
2213 if ((fr_max_attributes > 0) &&
2214 (num_attributes > fr_max_attributes)) {
2215 fr_strerror_printf("WARNING: Possible DoS attack from host %s: Too many attributes in request (received %d, max %d are allowed).",
2216 inet_ntop(packet->src_ipaddr.af,
2217 &packet->src_ipaddr.ipaddr,
2218 host_ipaddr, sizeof(host_ipaddr)),
2219 num_attributes, fr_max_attributes);
2224 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
2226 * A packet with an EAP-Message attribute MUST also have
2227 * a Message-Authenticator attribute.
2229 * A Message-Authenticator all by itself is OK, though.
2231 * Similarly, Status-Server packets MUST contain
2232 * Message-Authenticator attributes.
2234 if (require_ma && ! seen_ma) {
2235 fr_strerror_printf("WARNING: Insecure packet from host %s: Packet does not contain required Message-Authenticator attribute",
2236 inet_ntop(packet->src_ipaddr.af,
2237 &packet->src_ipaddr.ipaddr,
2238 host_ipaddr, sizeof(host_ipaddr)));
2243 * Fill RADIUS header fields
2245 packet->code = hdr->code;
2246 packet->id = hdr->id;
2247 memcpy(packet->vector, hdr->vector, AUTH_VECTOR_LEN);
2254 * Receive UDP client requests, and fill in
2255 * the basics of a RADIUS_PACKET structure.
2257 RADIUS_PACKET *rad_recv(int fd, int flags)
2260 RADIUS_PACKET *packet;
2263 * Allocate the new request data structure
2265 if ((packet = malloc(sizeof(*packet))) == NULL) {
2266 fr_strerror_printf("out of memory");
2269 memset(packet, 0, sizeof(*packet));
2272 sock_flags = MSG_PEEK;
2276 packet->data_len = rad_recvfrom(fd, &packet->data, sock_flags,
2277 &packet->src_ipaddr, &packet->src_port,
2278 &packet->dst_ipaddr, &packet->dst_port);
2281 * Check for socket errors.
2283 if (packet->data_len < 0) {
2284 fr_strerror_printf("Error receiving packet: %s", strerror(errno));
2285 /* packet->data is NULL */
2291 * If the packet is too big, then rad_recvfrom did NOT
2292 * allocate memory. Instead, it just discarded the
2295 if (packet->data_len > MAX_PACKET_LEN) {
2296 fr_strerror_printf("Discarding packet: Larger than RFC limitation of 4096 bytes.");
2297 /* packet->data is NULL */
2303 * Read no data. Continue.
2304 * This check is AFTER the MAX_PACKET_LEN check above, because
2305 * if the packet is larger than MAX_PACKET_LEN, we also have
2306 * packet->data == NULL
2308 if ((packet->data_len == 0) || !packet->data) {
2309 fr_strerror_printf("Empty packet: Socket is not ready.");
2315 * See if it's a well-formed RADIUS packet.
2317 if (!rad_packet_ok(packet, flags)) {
2323 * Remember which socket we read the packet from.
2325 packet->sockfd = fd;
2328 * FIXME: Do even more filtering by only permitting
2329 * certain IP's. The problem is that we don't know
2330 * how to do this properly for all possible clients...
2334 * Explicitely set the VP list to empty.
2338 if (fr_debug_flag) {
2339 char host_ipaddr[128];
2341 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
2342 DEBUG("rad_recv: %s packet from host %s port %d",
2343 fr_packet_codes[packet->code],
2344 inet_ntop(packet->src_ipaddr.af,
2345 &packet->src_ipaddr.ipaddr,
2346 host_ipaddr, sizeof(host_ipaddr)),
2349 DEBUG("rad_recv: Packet from host %s port %d code=%d",
2350 inet_ntop(packet->src_ipaddr.af,
2351 &packet->src_ipaddr.ipaddr,
2352 host_ipaddr, sizeof(host_ipaddr)),
2356 DEBUG(", id=%d, length=%d\n",
2357 packet->id, (int) packet->data_len);
2365 * Verify the signature of a packet.
2367 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
2374 if (!packet || !packet->data) return -1;
2377 * Before we allocate memory for the attributes, do more
2380 ptr = packet->data + AUTH_HDR_LEN;
2381 length = packet->data_len - AUTH_HDR_LEN;
2382 while (length > 0) {
2383 uint8_t msg_auth_vector[AUTH_VECTOR_LEN];
2384 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
2389 default: /* don't do anything. */
2393 * Note that more than one Message-Authenticator
2394 * attribute is invalid.
2396 case PW_MESSAGE_AUTHENTICATOR:
2397 memcpy(msg_auth_vector, &ptr[2], sizeof(msg_auth_vector));
2398 memset(&ptr[2], 0, AUTH_VECTOR_LEN);
2400 switch (packet->code) {
2404 case PW_ACCOUNTING_REQUEST:
2405 case PW_ACCOUNTING_RESPONSE:
2406 case PW_DISCONNECT_REQUEST:
2407 case PW_DISCONNECT_ACK:
2408 case PW_DISCONNECT_NAK:
2409 case PW_COA_REQUEST:
2412 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
2415 case PW_AUTHENTICATION_ACK:
2416 case PW_AUTHENTICATION_REJECT:
2417 case PW_ACCESS_CHALLENGE:
2419 fr_strerror_printf("ERROR: Cannot validate Message-Authenticator in response packet without a request packet.");
2422 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
2426 fr_hmac_md5(packet->data, packet->data_len,
2427 (const uint8_t *) secret, strlen(secret),
2429 if (rad_digest_cmp(calc_auth_vector, msg_auth_vector,
2430 sizeof(calc_auth_vector)) != 0) {
2432 fr_strerror_printf("Received packet from %s with invalid Message-Authenticator! (Shared secret is incorrect.)",
2433 inet_ntop(packet->src_ipaddr.af,
2434 &packet->src_ipaddr.ipaddr,
2435 buffer, sizeof(buffer)));
2436 /* Silently drop packet, according to RFC 3579 */
2438 } /* else the message authenticator was good */
2441 * Reinitialize Authenticators.
2443 memcpy(&ptr[2], msg_auth_vector, AUTH_VECTOR_LEN);
2444 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
2446 } /* switch over the attributes */
2450 } /* loop over the packet, sanity checking the attributes */
2453 * It looks like a RADIUS packet, but we can't validate
2456 if ((packet->code == 0) || (packet->code >= FR_MAX_PACKET_CODE)) {
2458 fr_strerror_printf("Received Unknown packet code %d "
2459 "from client %s port %d: Cannot validate signature.",
2461 inet_ntop(packet->src_ipaddr.af,
2462 &packet->src_ipaddr.ipaddr,
2463 buffer, sizeof(buffer)),
2469 * Calculate and/or verify digest.
2471 switch(packet->code) {
2475 case PW_AUTHENTICATION_REQUEST:
2476 case PW_STATUS_SERVER:
2478 * The authentication vector is random
2479 * nonsense, invented by the client.
2483 case PW_COA_REQUEST:
2484 case PW_DISCONNECT_REQUEST:
2485 case PW_ACCOUNTING_REQUEST:
2486 if (calc_acctdigest(packet, secret) > 1) {
2487 fr_strerror_printf("Received %s packet "
2488 "from client %s with invalid signature! (Shared secret is incorrect.)",
2489 fr_packet_codes[packet->code],
2490 inet_ntop(packet->src_ipaddr.af,
2491 &packet->src_ipaddr.ipaddr,
2492 buffer, sizeof(buffer)));
2497 /* Verify the reply digest */
2498 case PW_AUTHENTICATION_ACK:
2499 case PW_AUTHENTICATION_REJECT:
2500 case PW_ACCESS_CHALLENGE:
2501 case PW_ACCOUNTING_RESPONSE:
2502 case PW_DISCONNECT_ACK:
2503 case PW_DISCONNECT_NAK:
2506 rcode = calc_replydigest(packet, original, secret);
2508 fr_strerror_printf("Received %s packet "
2509 "from home server %s port %d with invalid signature! (Shared secret is incorrect.)",
2510 fr_packet_codes[packet->code],
2511 inet_ntop(packet->src_ipaddr.af,
2512 &packet->src_ipaddr.ipaddr,
2513 buffer, sizeof(buffer)),
2520 fr_strerror_printf("Received Unknown packet code %d "
2521 "from client %s port %d: Cannot validate signature",
2523 inet_ntop(packet->src_ipaddr.af,
2524 &packet->src_ipaddr.ipaddr,
2525 buffer, sizeof(buffer)),
2535 * Create a "raw" attribute from the attribute contents.
2537 static ssize_t data2vp_raw(UNUSED const RADIUS_PACKET *packet,
2538 UNUSED const RADIUS_PACKET *original,
2539 UNUSED const char *secret,
2540 unsigned int attribute, unsigned int vendor,
2541 const uint8_t *data, size_t length,
2547 * Keep the next function happy.
2549 vp = pairalloc(NULL);
2550 vp = paircreate_raw(attribute, vendor, PW_TYPE_OCTETS, vp);
2552 fr_strerror_printf("data2vp_raw: Failed creating attribute");
2556 vp->length = length;
2559 * If the data is too large, mark it as a "TLV".
2561 if (length <= sizeof(vp->vp_octets)) {
2562 memcpy(vp->vp_octets, data, length);
2564 vp->type = PW_TYPE_TLV;
2565 vp->vp_tlv = malloc(length);
2570 memcpy(vp->vp_tlv, data, length);
2579 static ssize_t data2vp_tlvs(const RADIUS_PACKET *packet,
2580 const RADIUS_PACKET *original,
2582 unsigned int attribute, unsigned int vendor,
2584 const uint8_t *start, size_t length,
2588 * Create any kind of VP from the attribute contents.
2590 * Will return -1 on error, or "length".
2592 static ssize_t data2vp_any(const RADIUS_PACKET *packet,
2593 const RADIUS_PACKET *original,
2594 const char *secret, int nest,
2595 unsigned int attribute, unsigned int vendor,
2596 const uint8_t *data, size_t length,
2599 int data_offset = 0;
2601 VALUE_PAIR *vp = NULL;
2605 * Hacks for CUI. The WiMAX spec says that it
2606 * can be zero length, even though this is
2607 * forbidden by the RADIUS specs. So... we make
2608 * a special case for it.
2610 if ((vendor == 0) &&
2611 (attribute == PW_CHARGEABLE_USER_IDENTITY)) {
2612 data = (const uint8_t *) "";
2620 da = dict_attrbyvalue(attribute, vendor);
2623 * Unknown attribute. Create it as a "raw" attribute.
2626 VP_TRACE("Not found %u.%u\n", vendor, attribute);
2628 if (vp) pairfree(&vp);
2629 return data2vp_raw(packet, original, secret,
2630 attribute, vendor, data, length, pvp);
2634 * TLVs are handled first. They can't be tagged, and
2635 * they can't be encrypted.
2637 if (da->type == PW_TYPE_TLV) {
2638 VP_TRACE("Found TLV %u.%u\n", vendor, attribute);
2639 return data2vp_tlvs(packet, original, secret,
2640 attribute, vendor, nest,
2645 * The attribute is known, and well formed. We can now
2646 * create it. The main failure from here on in is being
2655 if (vp->flags.has_tag) {
2656 if (TAG_VALID(data[0]) ||
2657 (vp->flags.encrypt == FLAG_ENCRYPT_TUNNEL_PASSWORD)) {
2659 * Tunnel passwords REQUIRE a tag, even
2660 * if don't have a valid tag.
2662 vp->flags.tag = data[0];
2664 if ((vp->type == PW_TYPE_STRING) ||
2665 (vp->type == PW_TYPE_OCTETS)) {
2666 if (length == 0) goto raw;
2673 * Copy the data to be decrypted
2675 vp->length = length - data_offset;
2676 memcpy(&vp->vp_octets[0], data + data_offset, vp->length);
2679 * Decrypt the attribute.
2681 switch (vp->flags.encrypt) {
2685 case FLAG_ENCRYPT_USER_PASSWORD:
2687 rad_pwdecode(vp->vp_strvalue,
2691 rad_pwdecode(vp->vp_strvalue,
2695 if (vp->attribute == PW_USER_PASSWORD) {
2696 vp->length = strlen(vp->vp_strvalue);
2701 * Tunnel-Password's may go ONLY
2702 * in response packets.
2704 case FLAG_ENCRYPT_TUNNEL_PASSWORD:
2705 if (!original) goto raw;
2707 if (rad_tunnel_pwdecode(vp->vp_octets, &vp->length,
2708 secret, original->vector) < 0) {
2714 * Ascend-Send-Secret
2715 * Ascend-Receive-Secret
2717 case FLAG_ENCRYPT_ASCEND_SECRET:
2721 uint8_t my_digest[AUTH_VECTOR_LEN];
2722 make_secret(my_digest,
2725 memcpy(vp->vp_strvalue, my_digest,
2727 vp->vp_strvalue[AUTH_VECTOR_LEN] = '\0';
2728 vp->length = strlen(vp->vp_strvalue);
2734 } /* switch over encryption flags */
2738 case PW_TYPE_STRING:
2739 case PW_TYPE_OCTETS:
2740 case PW_TYPE_ABINARY:
2741 /* nothing more to do */
2745 if (vp->length != 1) goto raw;
2747 vp->vp_integer = vp->vp_octets[0];
2752 if (vp->length != 2) goto raw;
2754 vp->vp_integer = (vp->vp_octets[0] << 8) | vp->vp_octets[1];
2757 case PW_TYPE_INTEGER:
2758 if (vp->length != 4) goto raw;
2760 memcpy(&vp->vp_integer, vp->vp_octets, 4);
2761 vp->vp_integer = ntohl(vp->vp_integer);
2763 if (vp->flags.has_tag) vp->vp_integer &= 0x00ffffff;
2766 * Try to get named VALUEs
2770 dval = dict_valbyattr(vp->attribute, vp->vendor,
2773 strlcpy(vp->vp_strvalue,
2775 sizeof(vp->vp_strvalue));
2781 if (vp->length != 4) goto raw;
2783 memcpy(&vp->vp_date, vp->vp_octets, 4);
2784 vp->vp_date = ntohl(vp->vp_date);
2788 case PW_TYPE_IPADDR:
2789 if (vp->length != 4) goto raw;
2791 memcpy(&vp->vp_ipaddr, vp->vp_octets, 4);
2795 * IPv6 interface ID is 8 octets long.
2798 if (vp->length != 8) goto raw;
2799 /* vp->vp_ifid == vp->vp_octets */
2803 * IPv6 addresses are 16 octets long
2805 case PW_TYPE_IPV6ADDR:
2806 if (vp->length != 16) goto raw;
2807 /* vp->vp_ipv6addr == vp->vp_octets */
2811 * IPv6 prefixes are 2 to 18 octets long.
2813 * RFC 3162: The first octet is unused.
2814 * The second is the length of the prefix
2815 * the rest are the prefix data.
2817 * The prefix length can have value 0 to 128.
2819 case PW_TYPE_IPV6PREFIX:
2820 if (vp->length < 2 || vp->length > 18) goto raw;
2821 if (vp->vp_octets[1] > 128) goto raw;
2824 * FIXME: double-check that
2825 * (vp->vp_octets[1] >> 3) matches vp->length + 2
2827 if (vp->length < 18) {
2828 memset(vp->vp_octets + vp->length, 0,
2833 case PW_TYPE_SIGNED:
2834 if (vp->length != 4) goto raw;
2837 * Overload vp_integer for ntohl, which takes
2838 * uint32_t, not int32_t
2840 memcpy(&vp->vp_integer, vp->vp_octets, 4);
2841 vp->vp_integer = ntohl(vp->vp_integer);
2842 memcpy(&vp->vp_signed, &vp->vp_integer, 4);
2847 fr_strerror_printf("data2vp_any: Internal sanity check failed");
2850 case PW_TYPE_COMBO_IP:
2851 if (vp->length == 4) {
2852 vp->type = PW_TYPE_IPADDR;
2853 memcpy(&vp->vp_ipaddr, vp->vp_octets, 4);
2856 } else if (vp->length == 16) {
2857 vp->type = PW_TYPE_IPV6ADDR;
2858 /* vp->vp_ipv6addr == vp->vp_octets */
2875 * Convert a top-level VSA to a VP.
2877 static ssize_t attr2vp_vsa(const RADIUS_PACKET *packet,
2878 const RADIUS_PACKET *original,
2879 const char *secret, unsigned int vendor,
2880 size_t dv_type, size_t dv_length,
2881 const uint8_t *data, size_t length,
2884 unsigned int attribute;
2885 ssize_t attrlen, my_len;
2888 if (length <= (dv_type + dv_length)) {
2889 fr_strerror_printf("attr2vp_vsa: Failure to call rad_tlv_ok");
2896 /* data[0] must be zero */
2897 attribute = data[1] << 16;
2898 attribute |= data[2] << 8;
2899 attribute |= data[3];
2903 attribute = data[0] << 8;
2904 attribute |= data[1];
2908 attribute = data[0];
2912 fr_strerror_printf("attr2vp_vsa: Internal sanity check failed");
2916 switch (dv_length) {
2918 /* data[dv_type] must be zero */
2919 attrlen = data[dv_type + 1];
2923 attrlen = data[dv_type];
2931 fr_strerror_printf("attr2vp_vsa: Internal sanity check failed");
2936 if (attrlen <= (ssize_t) (dv_type + dv_length)) {
2937 fr_strerror_printf("attr2vp_vsa: Failure to call rad_tlv_ok");
2942 attrlen -= (dv_type + dv_length);
2944 my_len = data2vp_any(packet, original, secret, 0,
2946 data + dv_type + dv_length, attrlen, pvp);
2947 if (my_len < 0) return my_len;
2950 if (my_len != attrlen) {
2952 fr_strerror_printf("attr2vp_vsa: Incomplete decode %d != %d",
2953 (int) my_len, (int) attrlen);
2958 return dv_type + dv_length + attrlen;
2962 * Convert one or more TLVs to VALUE_PAIRs. This function can
2963 * be called recursively...
2965 static ssize_t data2vp_tlvs(const RADIUS_PACKET *packet,
2966 const RADIUS_PACKET *original,
2968 unsigned int attribute, unsigned int vendor,
2970 const uint8_t *start, size_t length,
2973 size_t dv_type, dv_length;
2974 const uint8_t *data, *end;
2975 VALUE_PAIR *head, **last, *vp;
2980 * The default format for a VSA is the RFC recommended
2987 * Top-level TLVs can be of a weird format. TLVs
2988 * encapsulated in a TLV can only be in the RFC format.
2992 dv = dict_vendorbyvalue(vendor);
2995 dv_length = dv->length;
2996 /* dict.c enforces sane values on the above fields */
3000 if (nest >= fr_attr_max_tlv) {
3001 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed in recursion");
3006 * The VSAs do not exactly fill the data,
3007 * The *entire* TLV is malformed.
3009 if (rad_tlv_ok(data, length, dv_type, dv_length) < 0) {
3010 VP_TRACE("TLV malformed %u.%u\n", vendor, attribute);
3011 return data2vp_raw(packet, original, secret,
3012 attribute, vendor, data, length, pvp);
3015 end = data + length;
3019 while (data < end) {
3020 unsigned int my_attr;
3021 unsigned int my_len;
3024 if ((data + dv_type + dv_length) > end) {
3025 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed in tlvs: Insufficient data");
3033 my_attr = attribute;
3034 my_attr |= ((data[0] & fr_attr_mask[nest + 1])
3035 << fr_attr_shift[nest + 1]);
3038 my_attr = (data[0] << 8) | data[1];
3042 my_attr = (data[1] << 16) | (data[1] << 8) | data[3];
3046 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed");
3050 switch (dv_length) {
3057 my_len = data[dv_type + dv_length - 1];
3061 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed");
3066 if (my_len < (dv_type + dv_length)) {
3067 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed in tlvs: underflow");
3072 if ((data + my_len) > end) {
3073 fr_strerror_printf("data2vp_tlvs: Internal sanity check failed in tlvs: overflow");
3079 my_len -= dv_type + dv_length;
3082 * If this returns > 0, it returns "my_len"
3084 if (data2vp_any(packet, original, secret, nest + 1,
3086 data + dv_type + dv_length, my_len, &vp) < 0) {
3091 data += my_len + dv_type + dv_length;
3101 return data - start;
3106 * Group "continued" attributes together, and create VPs from them.
3107 * The caller ensures that the RADIUS packet is OK, and that the
3108 * continuations have all been checked.
3110 static ssize_t data2vp_continued(const RADIUS_PACKET *packet,
3111 const RADIUS_PACKET *original,
3113 const uint8_t *start, size_t length,
3114 VALUE_PAIR **pvp, int nest,
3115 unsigned int attribute, unsigned int vendor,
3116 int first_offset, int later_offset,
3120 uint8_t *attr, *ptr;
3121 const uint8_t *data;
3123 attr = malloc(attrlen);
3125 fr_strerror_printf("Out of memory");
3136 memcpy(ptr, data + first_offset, data[1] - first_offset);
3137 ptr += data[1] - first_offset;
3138 left -= data[1] - first_offset;
3143 if (data >= (start + length)) {
3144 fr_strerror_printf("data2vp_continued: Internal sanity check failed");
3148 memcpy(ptr, data + later_offset, data[1] - later_offset);
3149 ptr += data[1] - later_offset;
3150 left -= data[1] - later_offset;
3154 left = data2vp_any(packet, original, secret, nest,
3156 attr, attrlen, pvp);
3158 if (left < 0) return left;
3160 return data - start;
3165 * Create a "raw" VALUE_PAIR from a RADIUS attribute.
3167 ssize_t rad_attr2vp_raw(const RADIUS_PACKET *packet,
3168 const RADIUS_PACKET *original,
3170 const uint8_t *data, size_t length,
3175 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3176 fr_strerror_printf("rad_attr2vp_raw: Invalid length");
3180 my_len = data2vp_raw(packet, original, secret, data[0], 0,
3181 data + 2, data[1] - 2, pvp);
3182 if (my_len < 0) return my_len;
3189 * Get the length of the data portion of all of the contiguous
3190 * continued attributes.
3192 * 0 for "no continuation"
3193 * -1 on malformed packets (continuation followed by non-wimax, etc.)
3195 static ssize_t wimax_attrlen(uint32_t vendor,
3196 const uint8_t *start, const uint8_t *end)
3199 const uint8_t *data = start;
3201 if ((data[8] & 0x80) == 0) return 0;
3202 total = data[7] - 3;
3205 while (data < end) {
3207 if ((data + 9) > end) return -1;
3209 if ((data[0] != PW_VENDOR_SPECIFIC) ||
3211 (memcmp(data + 2, &vendor, 4) != 0) ||
3212 (data[6] != start[6]) ||
3213 ((data[7] + 6) != data[1])) return -1;
3215 total += data[7] - 3;
3216 if ((data[8] & 0x80) == 0) break;
3225 * Get the length of the data portion of all of the contiguous
3226 * continued attributes.
3228 * 0 for "no continuation"
3229 * -1 on malformed packets (continuation followed by non-wimax, etc.)
3231 static ssize_t extended_attrlen(const uint8_t *start, const uint8_t *end)
3234 const uint8_t *data = start;
3236 if ((data[3] & 0x80) == 0) return 0;
3237 total = data[1] - 4;
3240 while (data < end) {
3241 if ((data + 4) > end) return -1;
3243 if ((data[0] != start[0]) ||
3245 (data[2] != start[2])) return -1;
3247 total += data[1] - 4;
3248 if ((data[3] & 0x80) == 0) break;
3257 * Create WiMAX VALUE_PAIRs from a RADIUS attribute.
3259 ssize_t rad_attr2vp_wimax(const RADIUS_PACKET *packet,
3260 const RADIUS_PACKET *original,
3262 const uint8_t *data, size_t length,
3266 unsigned int attribute;
3269 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3270 fr_strerror_printf("rad_attr2vp_wimax: Invalid length");
3274 if (data[0] != PW_VENDOR_SPECIFIC) {
3275 fr_strerror_printf("rad_attr2vp_wimax: Invalid attribute");
3280 * Not enough room for a Vendor-Id. + WiMAX header
3283 return rad_attr2vp_raw(packet, original, secret,
3287 memcpy(&lvalue, data + 2, 4);
3288 lvalue = ntohl(lvalue);
3293 if (lvalue != VENDORPEC_WIMAX) {
3296 dv = dict_vendorbyvalue(lvalue);
3297 if (!dv || !dv->flags) {
3298 fr_strerror_printf("rad_attr2vp_wimax: Not a WiMAX attribute");
3304 * The WiMAX attribute is encapsulated in a VSA. If the
3305 * WiMAX length disagrees with the VSA length, it's malformed.
3307 if ((data[7] + 6) != data[1]) {
3308 return rad_attr2vp_raw(packet, original, secret,
3312 attribute = data[6];
3315 * Attribute is continued. Do some more work.
3318 my_len = wimax_attrlen(htonl(lvalue), data, data + length);
3320 return rad_attr2vp_raw(packet, original, secret,
3324 return data2vp_continued(packet, original, secret,
3325 data, length, pvp, 0,
3330 my_len = data2vp_any(packet, original, secret, 0, attribute, lvalue,
3331 data + 9, data[1] - 9, pvp);
3332 if (my_len < 0) return my_len;
3338 * Create Vendor-Specifc VALUE_PAIRs from a RADIUS attribute.
3340 ssize_t rad_attr2vp_vsa(const RADIUS_PACKET *packet,
3341 const RADIUS_PACKET *original,
3343 const uint8_t *data, size_t length,
3346 size_t dv_type, dv_length;
3351 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3352 fr_strerror_printf("rad_attr2vp_vsa: Invalid length");
3356 if (data[0] != PW_VENDOR_SPECIFIC) {
3357 fr_strerror_printf("rad_attr2vp_vsa: Invalid attribute");
3362 * Not enough room for a Vendor-Id.
3363 * Or the high octet of the Vendor-Id is set.
3365 if ((data[1] < 6) || (data[2] != 0)) {
3366 return rad_attr2vp_raw(packet, original, secret,
3370 memcpy(&lvalue, data + 2, 4);
3371 lvalue = ntohl(lvalue);
3374 * WiMAX gets its own set of magic.
3376 if (lvalue == VENDORPEC_WIMAX) {
3378 return rad_attr2vp_wimax(packet, original, secret,
3382 dv_type = dv_length = 1;
3383 dv = dict_vendorbyvalue(lvalue);
3386 dv_length = dv->length;
3388 if (dv->flags) goto wimax;
3392 * Attribute is not in the correct form.
3394 if (rad_tlv_ok(data + 6, data[1] - 6, dv_type, dv_length) < 0) {
3395 return rad_attr2vp_raw(packet, original, secret,
3399 my_len = attr2vp_vsa(packet, original, secret,
3400 lvalue, dv_type, dv_length,
3401 data + 6, data[1] - 6, pvp);
3402 if (my_len < 0) return my_len;
3405 if (my_len != (data[1] - 6)) {
3407 fr_strerror_printf("rad_attr2vp_vsa: Incomplete decode");
3416 * Create an "extended" VALUE_PAIR from a RADIUS attribute.
3418 ssize_t rad_attr2vp_extended(const RADIUS_PACKET *packet,
3419 const RADIUS_PACKET *original,
3421 const uint8_t *start, size_t length,
3424 unsigned int attribute;
3427 unsigned int vendor = VENDORPEC_EXTENDED;
3428 size_t data_len = length;
3429 const uint8_t *data;
3434 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3435 fr_strerror_printf("rad_attr2vp_extended: Invalid length");
3439 da = dict_attrbyvalue(data[0], vendor);
3441 (!da->flags.extended && !da->flags.extended_flags)) {
3442 fr_strerror_printf("rad_attr2vp_extended: Attribute is not extended format");
3449 * No Extended-Type. It's a raw attribute.
3450 * Also, if there's no data following the Extended-Type,
3451 * it's a raw attribute.
3455 return rad_attr2vp_raw(packet, original, secret, start,
3460 * The attribute is "241.1", for example. Go look that
3461 * up to see what type it is.
3463 attribute = data[0];
3464 attribute |= (data[2] << fr_attr_shift[1]);
3466 da = dict_attrbyvalue(attribute, vendor);
3469 vendor = VENDORPEC_EXTENDED;
3472 if (data[1] < length) data_len = data[1];
3478 * If there's supposed to be a flag octet. If not, it's
3479 * a raw attribute. If the flag is set, it's supposed to
3482 if (da->flags.extended_flags) {
3483 if (data_len == 0) goto raw;
3485 continued = ((data[0] & 0x80) != 0);
3491 * Extended VSAs have 4 octets of
3492 * Vendor-Id followed by one octet of
3495 if (da->flags.evs) {
3496 if (data_len < 5) goto raw;
3499 * Vendor Ids can only be 24-bit.
3501 if (data[0] != 0) goto raw;
3503 vendor = ((data[1] << 16) |
3508 * Pack the *encapsulating* attribute number into
3509 * the vendor id. This number should be >= 241.
3511 vendor |= start[0] * FR_MAX_VENDOR;
3515 * Over-write the attribute with the
3518 attribute = data[4];
3524 int first_offset = 4;
3527 if (vendor != VENDORPEC_EXTENDED) first_offset += 5;
3529 my_len = extended_attrlen(start, start + length);
3530 if (my_len < 0) goto raw;
3532 if (vendor != VENDORPEC_EXTENDED) my_len -= 5;
3534 return data2vp_continued(packet, original, secret,
3535 start, length, pvp, shift,
3537 first_offset, 4, my_len);
3540 if (data2vp_any(packet, original, secret, shift,
3541 attribute, vendor, data, data_len, pvp) < 0) {
3545 return (data + data_len) - start;
3550 * Create a "standard" RFC VALUE_PAIR from the given data.
3552 ssize_t rad_attr2vp_rfc(const RADIUS_PACKET *packet,
3553 const RADIUS_PACKET *original,
3555 const uint8_t *data, size_t length,
3558 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3559 fr_strerror_printf("rad_attr2vp_rfc: Insufficient data");
3563 if (data2vp_any(packet, original, secret, 0,
3564 data[0], 0, data + 2, data[1] - 2, pvp) < 0) {
3572 * Create a "normal" VALUE_PAIR from the given data.
3574 ssize_t rad_attr2vp(const RADIUS_PACKET *packet,
3575 const RADIUS_PACKET *original,
3577 const uint8_t *data, size_t length,
3580 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3581 fr_strerror_printf("rad_attr2vp: Insufficient data");
3586 * VSAs get their own handler.
3588 if (data[0] == PW_VENDOR_SPECIFIC) {
3589 return rad_attr2vp_vsa(packet, original, secret,
3594 * Extended attribute format gets their own handler.
3596 if (dict_attrbyvalue(data[0], VENDORPEC_EXTENDED) != NULL) {
3597 return rad_attr2vp_extended(packet, original, secret,
3601 return rad_attr2vp_rfc(packet, original, secret, data, length, pvp);
3606 * Calculate/check digest, and decode radius attributes.
3608 * -1 on decoding error
3611 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original,
3617 radius_packet_t *hdr;
3618 VALUE_PAIR *head, **tail, *vp;
3621 * Extract attribute-value pairs
3623 hdr = (radius_packet_t *)packet->data;
3625 packet_length = packet->data_len - AUTH_HDR_LEN;
3632 * Loop over the attributes, decoding them into VPs.
3634 while (packet_length > 0) {
3638 * This may return many VPs
3640 my_len = rad_attr2vp(packet, original, secret,
3641 ptr, packet_length, &vp);
3656 * VSA's may not have been counted properly in
3657 * rad_packet_ok() above, as it is hard to count
3658 * then without using the dictionary. We
3659 * therefore enforce the limits here, too.
3661 if ((fr_max_attributes > 0) &&
3662 (num_attributes > fr_max_attributes)) {
3663 char host_ipaddr[128];
3666 fr_strerror_printf("WARNING: Possible DoS attack from host %s: Too many attributes in request (received %d, max %d are allowed).",
3667 inet_ntop(packet->src_ipaddr.af,
3668 &packet->src_ipaddr.ipaddr,
3669 host_ipaddr, sizeof(host_ipaddr)),
3670 num_attributes, fr_max_attributes);
3675 packet_length -= my_len;
3679 * Merge information from the outside world into our
3682 fr_rand_seed(packet->data, AUTH_HDR_LEN);
3685 * There may be VP's already in the packet. Don't
3686 * destroy them. Instead, add the decoded attributes to
3687 * the tail of the list.
3689 for (tail = &packet->vps; *tail != NULL; tail = &((*tail)->next)) {
3701 * We assume that the passwd buffer passed is big enough.
3702 * RFC2138 says the password is max 128 chars, so the size
3703 * of the passwd buffer must be at least 129 characters.
3704 * Preferably it's just MAX_STRING_LEN.
3706 * int *pwlen is updated to the new length of the encrypted
3707 * password - a multiple of 16 bytes.
3709 int rad_pwencode(char *passwd, size_t *pwlen, const char *secret,
3710 const uint8_t *vector)
3712 FR_MD5_CTX context, old;
3713 uint8_t digest[AUTH_VECTOR_LEN];
3714 int i, n, secretlen;
3718 * RFC maximum is 128 bytes.
3720 * If length is zero, pad it out with zeros.
3722 * If the length isn't aligned to 16 bytes,
3723 * zero out the extra data.
3727 if (len > 128) len = 128;
3730 memset(passwd, 0, AUTH_PASS_LEN);
3731 len = AUTH_PASS_LEN;
3732 } else if ((len % AUTH_PASS_LEN) != 0) {
3733 memset(&passwd[len], 0, AUTH_PASS_LEN - (len % AUTH_PASS_LEN));
3734 len += AUTH_PASS_LEN - (len % AUTH_PASS_LEN);
3739 * Use the secret to setup the decryption digest
3741 secretlen = strlen(secret);
3743 fr_MD5Init(&context);
3744 fr_MD5Update(&context, (const uint8_t *) secret, secretlen);
3745 old = context; /* save intermediate work */
3748 * Encrypt it in place. Don't bother checking
3749 * len, as we've ensured above that it's OK.
3751 for (n = 0; n < len; n += AUTH_PASS_LEN) {
3753 fr_MD5Update(&context, vector, AUTH_PASS_LEN);
3754 fr_MD5Final(digest, &context);
3757 fr_MD5Update(&context,
3758 (uint8_t *) passwd + n - AUTH_PASS_LEN,
3760 fr_MD5Final(digest, &context);
3763 for (i = 0; i < AUTH_PASS_LEN; i++) {
3764 passwd[i + n] ^= digest[i];
3774 int rad_pwdecode(char *passwd, size_t pwlen, const char *secret,
3775 const uint8_t *vector)
3777 FR_MD5_CTX context, old;
3778 uint8_t digest[AUTH_VECTOR_LEN];
3780 size_t n, secretlen;
3783 * The RFC's say that the maximum is 128.
3784 * The buffer we're putting it into above is 254, so
3785 * we don't need to do any length checking.
3787 if (pwlen > 128) pwlen = 128;
3792 if (pwlen == 0) goto done;
3795 * Use the secret to setup the decryption digest
3797 secretlen = strlen(secret);
3799 fr_MD5Init(&context);
3800 fr_MD5Update(&context, (const uint8_t *) secret, secretlen);
3801 old = context; /* save intermediate work */
3804 * The inverse of the code above.
3806 for (n = 0; n < pwlen; n += AUTH_PASS_LEN) {
3808 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
3809 fr_MD5Final(digest, &context);
3812 if (pwlen > AUTH_PASS_LEN) {
3813 fr_MD5Update(&context, (uint8_t *) passwd,
3817 fr_MD5Final(digest, &context);
3820 if (pwlen > (n + AUTH_PASS_LEN)) {
3821 fr_MD5Update(&context, (uint8_t *) passwd + n,
3826 for (i = 0; i < AUTH_PASS_LEN; i++) {
3827 passwd[i + n] ^= digest[i];
3832 passwd[pwlen] = '\0';
3833 return strlen(passwd);
3838 * Encode Tunnel-Password attributes when sending them out on the wire.
3840 * int *pwlen is updated to the new length of the encrypted
3841 * password - a multiple of 16 bytes.
3843 * This is per RFC-2868 which adds a two char SALT to the initial intermediate
3846 int rad_tunnel_pwencode(char *passwd, size_t *pwlen, const char *secret,
3847 const uint8_t *vector)
3849 uint8_t buffer[AUTH_VECTOR_LEN + MAX_STRING_LEN + 3];
3850 unsigned char digest[AUTH_VECTOR_LEN];
3852 int i, n, secretlen;
3857 if (len > 127) len = 127;
3860 * Shift the password 3 positions right to place a salt and original
3861 * length, tag will be added automatically on packet send
3863 for (n=len ; n>=0 ; n--) passwd[n+3] = passwd[n];
3867 * save original password length as first password character;
3874 * Generate salt. The RFC's say:
3876 * The high bit of salt[0] must be set, each salt in a
3877 * packet should be unique, and they should be random
3879 * So, we set the high bit, add in a counter, and then
3880 * add in some CSPRNG data. should be OK..
3882 salt[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
3883 (fr_rand() & 0x07));
3884 salt[1] = fr_rand();
3887 * Padd password to multiple of AUTH_PASS_LEN bytes.
3889 n = len % AUTH_PASS_LEN;
3891 n = AUTH_PASS_LEN - n;
3892 for (; n > 0; n--, len++)
3895 /* set new password length */
3899 * Use the secret to setup the decryption digest
3901 secretlen = strlen(secret);
3902 memcpy(buffer, secret, secretlen);
3904 for (n2 = 0; n2 < len; n2+=AUTH_PASS_LEN) {
3906 memcpy(buffer + secretlen, vector, AUTH_VECTOR_LEN);
3907 memcpy(buffer + secretlen + AUTH_VECTOR_LEN, salt, 2);
3908 fr_md5_calc(digest, buffer, secretlen + AUTH_VECTOR_LEN + 2);
3910 memcpy(buffer + secretlen, passwd + n2 - AUTH_PASS_LEN, AUTH_PASS_LEN);
3911 fr_md5_calc(digest, buffer, secretlen + AUTH_PASS_LEN);
3914 for (i = 0; i < AUTH_PASS_LEN; i++) {
3915 passwd[i + n2] ^= digest[i];
3923 * Decode Tunnel-Password encrypted attributes.
3925 * Defined in RFC-2868, this uses a two char SALT along with the
3926 * initial intermediate value, to differentiate it from the
3929 int rad_tunnel_pwdecode(uint8_t *passwd, size_t *pwlen, const char *secret,
3930 const uint8_t *vector)
3932 FR_MD5_CTX context, old;
3933 uint8_t digest[AUTH_VECTOR_LEN];
3935 unsigned i, n, len, reallen;
3940 * We need at least a salt.
3943 fr_strerror_printf("tunnel password is too short");
3948 * There's a salt, but no password. Or, there's a salt
3949 * and a 'data_len' octet. It's wrong, but at least we
3950 * can figure out what it means: the password is empty.
3952 * Note that this means we ignore the 'data_len' field,
3953 * if the attribute length tells us that there's no
3954 * more data. So the 'data_len' field may be wrong,
3963 len -= 2; /* discount the salt */
3966 * Use the secret to setup the decryption digest
3968 secretlen = strlen(secret);
3970 fr_MD5Init(&context);
3971 fr_MD5Update(&context, (const uint8_t *) secret, secretlen);
3972 old = context; /* save intermediate work */
3975 * Set up the initial key:
3977 * b(1) = MD5(secret + vector + salt)
3979 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
3980 fr_MD5Update(&context, passwd, 2);
3983 for (n = 0; n < len; n += AUTH_PASS_LEN) {
3987 fr_MD5Final(digest, &context);
3992 * A quick check: decrypt the first octet
3993 * of the password, which is the
3994 * 'data_len' field. Ensure it's sane.
3996 reallen = passwd[2] ^ digest[0];
3997 if (reallen >= len) {
3998 fr_strerror_printf("tunnel password is too long for the attribute");
4002 fr_MD5Update(&context, passwd + 2, AUTH_PASS_LEN);
4006 fr_MD5Final(digest, &context);
4009 fr_MD5Update(&context, passwd + n + 2, AUTH_PASS_LEN);
4012 for (i = base; i < AUTH_PASS_LEN; i++) {
4013 passwd[n + i - 1] = passwd[n + i + 2] ^ digest[i];
4018 * See make_tunnel_password, above.
4020 if (reallen > 239) reallen = 239;
4023 passwd[reallen] = 0;
4029 * Encode a CHAP password
4031 * FIXME: might not work with Ascend because
4032 * we use vp->length, and Ascend gear likes
4033 * to send an extra '\0' in the string!
4035 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output, int id,
4036 VALUE_PAIR *password)
4040 uint8_t string[MAX_STRING_LEN * 2 + 1];
4041 VALUE_PAIR *challenge;
4044 * Sanity check the input parameters
4046 if ((packet == NULL) || (password == NULL)) {
4051 * Note that the password VP can be EITHER
4052 * a User-Password attribute (from a check-item list),
4053 * or a CHAP-Password attribute (the client asking
4054 * the library to encode it).
4062 memcpy(ptr, password->vp_strvalue, password->length);
4063 ptr += password->length;
4064 i += password->length;
4067 * Use Chap-Challenge pair if present,
4068 * Request-Authenticator otherwise.
4070 challenge = pairfind(packet->vps, PW_CHAP_CHALLENGE, 0);
4072 memcpy(ptr, challenge->vp_strvalue, challenge->length);
4073 i += challenge->length;
4075 memcpy(ptr, packet->vector, AUTH_VECTOR_LEN);
4076 i += AUTH_VECTOR_LEN;
4080 fr_md5_calc((uint8_t *)output + 1, (uint8_t *)string, i);
4087 * Seed the random number generator.
4089 * May be called any number of times.
4091 void fr_rand_seed(const void *data, size_t size)
4096 * Ensure that the pool is initialized.
4098 if (!fr_rand_initialized) {
4101 memset(&fr_rand_pool, 0, sizeof(fr_rand_pool));
4103 fd = open("/dev/urandom", O_RDONLY);
4109 while (total < sizeof(fr_rand_pool.randrsl)) {
4110 this = read(fd, fr_rand_pool.randrsl,
4111 sizeof(fr_rand_pool.randrsl) - total);
4112 if ((this < 0) && (errno != EINTR)) break;
4113 if (this > 0) total += this;
4117 fr_rand_pool.randrsl[0] = fd;
4118 fr_rand_pool.randrsl[1] = time(NULL);
4119 fr_rand_pool.randrsl[2] = errno;
4122 fr_randinit(&fr_rand_pool, 1);
4123 fr_rand_pool.randcnt = 0;
4124 fr_rand_initialized = 1;
4130 * Hash the user data
4133 if (!hash) hash = fr_rand();
4134 hash = fr_hash_update(data, size, hash);
4136 fr_rand_pool.randmem[fr_rand_pool.randcnt] ^= hash;
4141 * Return a 32-bit random number.
4143 uint32_t fr_rand(void)
4148 * Ensure that the pool is initialized.
4150 if (!fr_rand_initialized) {
4151 fr_rand_seed(NULL, 0);
4154 num = fr_rand_pool.randrsl[fr_rand_pool.randcnt++];
4155 if (fr_rand_pool.randcnt >= 256) {
4156 fr_rand_pool.randcnt = 0;
4157 fr_isaac(&fr_rand_pool);
4165 * Allocate a new RADIUS_PACKET
4167 RADIUS_PACKET *rad_alloc(int newvector)
4171 if ((rp = malloc(sizeof(RADIUS_PACKET))) == NULL) {
4172 fr_strerror_printf("out of memory");
4175 memset(rp, 0, sizeof(*rp));
4181 uint32_t hash, base;
4184 * Don't expose the actual contents of the random
4188 for (i = 0; i < AUTH_VECTOR_LEN; i += sizeof(uint32_t)) {
4189 hash = fr_rand() ^ base;
4190 memcpy(rp->vector + i, &hash, sizeof(hash));
4193 fr_rand(); /* stir the pool again */
4198 RADIUS_PACKET *rad_alloc_reply(RADIUS_PACKET *packet)
4200 RADIUS_PACKET *reply;
4202 if (!packet) return NULL;
4204 reply = rad_alloc(0);
4205 if (!reply) return NULL;
4208 * Initialize the fields from the request.
4210 reply->sockfd = packet->sockfd;
4211 reply->dst_ipaddr = packet->src_ipaddr;
4212 reply->src_ipaddr = packet->dst_ipaddr;
4213 reply->dst_port = packet->src_port;
4214 reply->src_port = packet->dst_port;
4215 reply->id = packet->id;
4216 reply->code = 0; /* UNKNOWN code */
4217 memcpy(reply->vector, packet->vector,
4218 sizeof(reply->vector));
4221 reply->data_len = 0;
4228 * Free a RADIUS_PACKET
4230 void rad_free(RADIUS_PACKET **radius_packet_ptr)
4232 RADIUS_PACKET *radius_packet;
4234 if (!radius_packet_ptr || !*radius_packet_ptr) return;
4235 radius_packet = *radius_packet_ptr;
4237 free(radius_packet->data);
4239 pairfree(&radius_packet->vps);
4241 free(radius_packet);
4243 *radius_packet_ptr = NULL;