2 * vqp.c Functions to send/receive VQP 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 2007 Alan DeKok <aland@deployingradius.com>
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/udpfromto.h>
32 # define debug_pair(vp) do { if (fr_debug_flag && fr_log_fp) { \
33 fputc('\t', fr_log_fp); \
34 vp_print(fr_log_fp, vp); \
35 fputc('\n', fr_log_fp); \
39 #define MAX_VMPS_LEN (MAX_STRING_LEN - 1)
42 * http://www.openbsd.org/cgi-bin/cvsweb/src/usr.sbin/tcpdump/print-vqp.c
44 * Some of how it works:
46 * http://www.hackingciscoexposed.com/pdf/chapter12.pdf
48 * VLAN Query Protocol (VQP)
51 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | Version | Opcode | Response Code | Data Count |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 * VQP is layered over UDP. The default destination port is 1589.
73 #define VQP_HDR_LEN (8)
74 #define VQP_VERSION (1)
75 #define VQP_MAX_ATTRIBUTES (12)
79 * Wrapper for sendto which handles sendfromto, IPv6, and all
80 * possible combinations.
82 * FIXME: This is just a copy of rad_sendto().
83 * Duplicate code is bad.
85 static int vqp_sendto(int sockfd, void *data, size_t data_len, int flags,
86 fr_ipaddr_t *src_ipaddr, fr_ipaddr_t *dst_ipaddr,
89 struct sockaddr_storage dst;
93 struct sockaddr_storage src;
96 fr_ipaddr2sockaddr(src_ipaddr, 0, &src, &sizeof_src);
98 src_ipaddr = src_ipaddr; /* -Wunused */
101 if (!fr_ipaddr2sockaddr(dst_ipaddr, dst_port, &dst, &sizeof_dst)) {
102 return -1; /* Unknown address family, Die Die Die! */
105 #ifdef WITH_UDPFROMTO
107 * Only IPv4 is supported for udpfromto.
109 * And if they don't specify a source IP address, don't
112 if ((dst_ipaddr->af == AF_INET) ||
113 (src_ipaddr->af != AF_UNSPEC)) {
114 return sendfromto(sockfd, data, data_len, flags,
115 (struct sockaddr *)&src, sizeof_src,
116 (struct sockaddr *)&dst, sizeof_dst);
119 src_ipaddr = src_ipaddr; /* -Wunused */
123 * No udpfromto, OR an IPv6 socket, fail gracefully.
125 return sendto(sockfd, data, data_len, flags,
126 (struct sockaddr *)&dst, sizeof_dst);
130 * Wrapper for recvfrom, which handles recvfromto, IPv6, and all
131 * possible combinations.
133 * FIXME: This is copied from rad_recvfrom, with minor edits.
135 static ssize_t vqp_recvfrom(int sockfd, uint8_t **pbuf, int flags,
136 fr_ipaddr_t *src_ipaddr, uint16_t *src_port,
137 fr_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
139 struct sockaddr_storage src;
140 struct sockaddr_storage dst;
141 socklen_t sizeof_src = sizeof(src);
142 socklen_t sizeof_dst = sizeof(dst);
149 memset(&src, 0, sizeof_src);
150 memset(&dst, 0, sizeof_dst);
153 * Get address family, etc. first, so we know if we
154 * need to do udpfromto.
156 * FIXME: udpfromto also does this, but it's not
157 * a critical problem.
159 if (getsockname(sockfd, (struct sockaddr *)&dst,
160 &sizeof_dst) < 0) return -1;
163 * Read the length of the packet, from the packet.
164 * This lets us allocate the buffer to use for
165 * reading the rest of the packet.
167 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
168 (struct sockaddr *)&src, &sizeof_src);
169 if (data_len < 0) return -1;
172 * Too little data is available, discard the packet.
175 recvfrom(sockfd, header, sizeof(header), flags,
176 (struct sockaddr *)&src, &sizeof_src);
180 * Invalid version, packet type, or too many
183 } else if ((header[0] != VQP_VERSION) ||
186 (header[3] > VQP_MAX_ATTRIBUTES)) {
187 recvfrom(sockfd, header, sizeof(header), flags,
188 (struct sockaddr *)&src, &sizeof_src);
191 } else { /* we got 4 bytes of data. */
193 * We don't care about the contents for now...
197 * How many attributes are in the packet.
201 if ((header[1] == 1) || (header[1] == 3)) {
202 if (len != VQP_MAX_ATTRIBUTES) {
203 recvfrom(sockfd, header, sizeof(header), 0,
204 (struct sockaddr *)&src, &sizeof_src);
208 * Maximum length we support.
210 len = (12 * (4 + 4 + MAX_VMPS_LEN));
214 recvfrom(sockfd, header, sizeof(header), 0,
215 (struct sockaddr *)&src, &sizeof_src);
219 * Maximum length we support.
221 len = (12 * (4 + 4 + MAX_VMPS_LEN));
227 * For now, be generous.
229 len = (12 * (4 + 4 + MAX_VMPS_LEN));
235 * Receive the packet. The OS will discard any data in the
236 * packet after "len" bytes.
238 #ifdef WITH_UDPFROMTO
239 if (dst.ss_family == AF_INET) {
240 data_len = recvfromto(sockfd, buf, len, flags,
241 (struct sockaddr *)&src, &sizeof_src,
242 (struct sockaddr *)&dst, &sizeof_dst);
246 * No udpfromto, OR an IPv6 socket. Fail gracefully.
248 data_len = recvfrom(sockfd, buf, len, flags,
249 (struct sockaddr *)&src, &sizeof_src);
255 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, &port)) {
257 return -1; /* Unknown address family, Die Die Die! */
261 fr_sockaddr2ipaddr(&dst, sizeof_dst, dst_ipaddr, &port);
265 * Different address families should never happen.
267 if (src.ss_family != dst.ss_family) {
273 * Tell the caller about the data
280 RADIUS_PACKET *vqp_recv(int sockfd)
285 RADIUS_PACKET *packet;
288 * Allocate the new request data structure
290 if ((packet = malloc(sizeof(*packet))) == NULL) {
291 fr_strerror_printf("out of memory");
294 memset(packet, 0, sizeof(*packet));
296 packet->data_len = vqp_recvfrom(sockfd, &packet->data, 0,
297 &packet->src_ipaddr, &packet->src_port,
298 &packet->dst_ipaddr, &packet->dst_port);
301 * Check for socket errors.
303 if (packet->data_len < 0) {
304 fr_strerror_printf("Error receiving packet: %s", strerror(errno));
305 /* packet->data is NULL */
312 * We can only receive packets formatted in a way we
313 * expect. However, we accept MORE attributes in a
314 * packet than normal implementations may send.
316 if (packet->data_len < VQP_HDR_LEN) {
317 fr_strerror_printf("VQP packet is too short");
326 for (i = 0; i < packet->data_len; i++) {
327 if ((i & 0x0f) == 0) fprintf(stderr, "%02x: ", i);
328 fprintf(stderr, "%02x ", ptr[i]);
329 if ((i & 0x0f) == 0x0f) fprintf(stderr, "\n");
334 if (ptr[3] > VQP_MAX_ATTRIBUTES) {
335 fr_strerror_printf("Too many VQP attributes");
340 if (packet->data_len > VQP_HDR_LEN) {
347 length = packet->data_len - VQP_HDR_LEN;
351 fr_strerror_printf("Packet contains malformed attribute");
357 * Attributes are 4 bytes
358 * 0x00000c01 ... 0x00000c08
360 if ((ptr[0] != 0) || (ptr[1] != 0) ||
361 (ptr[2] != 0x0c) || (ptr[3] < 1) || (ptr[3] > 8)) {
362 fr_strerror_printf("Packet contains invalid attribute");
370 * We support lengths 1..253, for internal
371 * server reasons. Also, there's no reason
372 * for bigger lengths to exist... admins
373 * won't be typing in a 32K vlan name.
375 * Except for received ethernet frames...
376 * they get chopped to 253 internally.
379 ((ptr[4] != 0) || (ptr[5] > MAX_VMPS_LEN))) {
380 fr_strerror_printf("Packet contains attribute with invalid length %02x %02x", ptr[4], ptr[5]);
384 attrlen = (ptr[4] << 8) | ptr[5];
386 length -= (6 + attrlen);
390 packet->sockfd = sockfd;
394 * This is more than a bit of a hack.
396 packet->code = PW_AUTHENTICATION_REQUEST;
398 memcpy(&id, packet->data + 4, 4);
399 packet->id = ntohl(id);
402 * FIXME: Create a fake "request authenticator", to
403 * avoid duplicates? Or is the VQP sequence number
404 * adequate for this purpose?
411 * We do NOT mirror the old-style RADIUS code that does encode,
412 * sign && send in one function. For VQP, the caller MUST perform
413 * each task manually, and separately.
415 int vqp_send(RADIUS_PACKET *packet)
417 if (!packet || !packet->data || (packet->data_len < 8)) return -1;
420 * Don't print out the attributes, they were printed out
421 * when it was encoded.
425 * And send it on it's way.
427 return vqp_sendto(packet->sockfd, packet->data, packet->data_len, 0,
428 &packet->src_ipaddr, &packet->dst_ipaddr,
433 int vqp_decode(RADIUS_PACKET *packet)
436 int attribute, length;
437 VALUE_PAIR *vp, **tail;
439 if (!packet || !packet->data) return -1;
441 if (packet->data_len < VQP_HDR_LEN) return -1;
445 vp = paircreate(PW_VQP_PACKET_TYPE, PW_TYPE_OCTETS);
447 fr_strerror_printf("No memory");
450 vp->lvalue = packet->data[1];
456 vp = paircreate(PW_VQP_ERROR_CODE, PW_TYPE_OCTETS);
458 fr_strerror_printf("No memory");
461 vp->lvalue = packet->data[2];
467 vp = paircreate(PW_VQP_SEQUENCE_NUMBER, PW_TYPE_OCTETS);
469 fr_strerror_printf("No memory");
472 vp->lvalue = packet->id; /* already set by vqp_recv */
478 ptr = packet->data + VQP_HDR_LEN;
479 end = packet->data + packet->data_len;
482 * Note that vqp_recv() MUST ensure that the packet is
483 * formatted in a way we expect, and that vqp_recv() MUST
484 * be called before vqp_decode().
487 attribute = (ptr[2] << 8) | ptr[3];
488 length = (ptr[4] << 8) | ptr[5];
492 * Hack to get the dictionaries to work correctly.
495 vp = paircreate(attribute, PW_TYPE_OCTETS);
497 pairfree(&packet->vps);
499 fr_strerror_printf("No memory");
506 memcpy(&vp->vp_ipaddr, ptr, 4);
510 vp->type = PW_TYPE_OCTETS;
516 vp->length = (length > MAX_VMPS_LEN) ? MAX_VMPS_LEN : length;
517 memcpy(vp->vp_octets, ptr, vp->length);
518 vp->vp_octets[vp->length] = '\0';
529 * FIXME: Map attributes to Calling-Station-Id, etc...
536 * These are the MUST HAVE contents for a VQP packet.
538 * We don't allow the caller to give less than these, because
539 * it won't work. We don't encode more than these, because the
540 * clients will ignore it.
542 * FIXME: Be more generous? Look for CISCO + VQP attributes?
544 static int contents[5][VQP_MAX_ATTRIBUTES] = {
545 { 0, 0, 0, 0, 0, 0 },
546 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c05 }, /* Join request */
547 { 0x0c03, 0x0c08, 0, 0, 0, 0 }, /* Join Response */
548 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c08 }, /* Reconfirm */
549 { 0x0c03, 0x0c08, 0, 0, 0, 0 }
552 int vqp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
557 VALUE_PAIR *vps[VQP_MAX_ATTRIBUTES];
560 fr_strerror_printf("Failed encoding VQP");
564 if (packet->data) return 0;
566 vp = pairfind(packet->vps, PW_VQP_PACKET_TYPE);
568 fr_strerror_printf("Failed to find VQP-Packet-Type in response packet");
573 if ((code < 1) || (code > 4)) {
574 fr_strerror_printf("Invalid value %d for VQP-Packet-Type", code);
578 length = VQP_HDR_LEN;
579 memset(vps, 0, sizeof(vps));
581 vp = pairfind(packet->vps, PW_VQP_ERROR_CODE);
584 * FIXME: Map attributes from calling-station-Id, etc.
586 * Maybe do this via rlm_vqp? That's probably the
587 * best place to add the code...
591 * No error: encode attributes.
593 if (!vp) for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
594 if (!contents[code][i]) break;
596 vps[i] = pairfind(packet->vps, contents[code][i] | 0x2000);
599 * FIXME: Print the name...
602 fr_strerror_printf("Failed to find VQP attribute %02x",
608 length += vps[i]->length;
611 packet->data = malloc(length);
613 fr_strerror_printf("No memory");
616 packet->data_len = length;
620 ptr[0] = VQP_VERSION;
626 ptr[2] = vp->lvalue & 0xff;
631 * The number of attributes is hard-coded.
633 if ((code == 1) || (code == 3)) {
636 ptr[3] = VQP_MAX_ATTRIBUTES;
638 sequence = htonl(packet->id);
639 memcpy(ptr + 4, &sequence, 4);
642 fr_strerror_printf("Cannot send VQP response without request");
647 * Packet Sequence Number
649 memcpy(ptr + 4, original->data + 4, 4);
659 for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
666 * Type. Note that we look at only the lower 8
667 * bits, as the upper 8 bits have been hacked.
668 * See also dictionary.vqp
673 ptr[3] = vp->attribute & 0xff;
677 ptr[5] = vp->length & 0xff;
684 memcpy(ptr, &vp->vp_ipaddr, 4);
690 memcpy(ptr, vp->vp_octets, vp->length);