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/libradius.h>
27 #include <freeradius-devel/udpfromto.h>
28 #include <freeradius-devel/vqp.h>
32 #define MAX_VMPS_LEN (MAX_STRING_LEN - 1)
35 * http://www.openbsd.org/cgi-bin/cvsweb/src/usr.sbin/tcpdump/print-vqp.c
37 * Some of how it works:
39 * http://www.hackingciscoexposed.com/pdf/chapter12.pdf
41 * VLAN Query Protocol (VQP)
44 * 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
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Version | Opcode | Response Code | Data Count |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * VQP is layered over UDP. The default destination port is 1589.
66 #define VQP_HDR_LEN (8)
67 #define VQP_VERSION (1)
68 #define VQP_MAX_ATTRIBUTES (12)
72 * Wrapper for sendto which handles sendfromto, IPv6, and all
73 * possible combinations.
75 * FIXME: This is just a copy of rad_sendto().
76 * Duplicate code is bad.
78 static int vqp_sendto(int sockfd, void *data, size_t data_len, int flags,
79 fr_ipaddr_t *src_ipaddr, fr_ipaddr_t *dst_ipaddr,
82 struct sockaddr_storage dst;
86 struct sockaddr_storage src;
89 fr_ipaddr2sockaddr(src_ipaddr, 0, &src, &sizeof_src);
91 src_ipaddr = src_ipaddr; /* -Wunused */
94 if (!fr_ipaddr2sockaddr(dst_ipaddr, dst_port, &dst, &sizeof_dst)) {
95 return -1; /* Unknown address family, Die Die Die! */
100 * Only IPv4 is supported for udpfromto.
102 * And if they don't specify a source IP address, don't
105 if ((dst_ipaddr->af == AF_INET) &&
106 (src_ipaddr->af != AF_UNSPEC)) {
107 return sendfromto(sockfd, data, data_len, flags,
108 (struct sockaddr *)&src, sizeof_src,
109 (struct sockaddr *)&dst, sizeof_dst);
112 src_ipaddr = src_ipaddr; /* -Wunused */
116 * No udpfromto, OR an IPv6 socket, fail gracefully.
118 return sendto(sockfd, data, data_len, flags,
119 (struct sockaddr *)&dst, sizeof_dst);
123 * Wrapper for recvfrom, which handles recvfromto, IPv6, and all
124 * possible combinations.
126 * FIXME: This is copied from rad_recvfrom, with minor edits.
128 static ssize_t vqp_recvfrom(int sockfd, uint8_t **pbuf, int flags,
129 fr_ipaddr_t *src_ipaddr, uint16_t *src_port,
130 fr_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
132 struct sockaddr_storage src;
133 struct sockaddr_storage dst;
134 socklen_t sizeof_src = sizeof(src);
135 socklen_t sizeof_dst = sizeof(dst);
142 memset(&src, 0, sizeof_src);
143 memset(&dst, 0, sizeof_dst);
146 * Get address family, etc. first, so we know if we
147 * need to do udpfromto.
149 * FIXME: udpfromto also does this, but it's not
150 * a critical problem.
152 if (getsockname(sockfd, (struct sockaddr *)&dst,
153 &sizeof_dst) < 0) return -1;
156 * Read the length of the packet, from the packet.
157 * This lets us allocate the buffer to use for
158 * reading the rest of the packet.
160 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
161 (struct sockaddr *)&src, &sizeof_src);
162 if (data_len < 0) return -1;
165 * Too little data is available, discard the packet.
168 recvfrom(sockfd, header, sizeof(header), flags,
169 (struct sockaddr *)&src, &sizeof_src);
173 * Invalid version, packet type, or too many
176 } else if ((header[0] != VQP_VERSION) ||
179 (header[3] > VQP_MAX_ATTRIBUTES)) {
180 recvfrom(sockfd, header, sizeof(header), flags,
181 (struct sockaddr *)&src, &sizeof_src);
184 } else { /* we got 4 bytes of data. */
186 * We don't care about the contents for now...
190 * How many attributes are in the packet.
194 if ((header[1] == 1) || (header[1] == 3)) {
195 if (len != VQP_MAX_ATTRIBUTES) {
196 recvfrom(sockfd, header, sizeof(header), 0,
197 (struct sockaddr *)&src, &sizeof_src);
201 * Maximum length we support.
203 len = (12 * (4 + 4 + MAX_VMPS_LEN));
207 recvfrom(sockfd, header, sizeof(header), 0,
208 (struct sockaddr *)&src, &sizeof_src);
212 * Maximum length we support.
214 len = (12 * (4 + 4 + MAX_VMPS_LEN));
220 * For now, be generous.
222 len = (12 * (4 + 4 + MAX_VMPS_LEN));
228 * Receive the packet. The OS will discard any data in the
229 * packet after "len" bytes.
231 #ifdef WITH_UDPFROMTO
232 if (dst.ss_family == AF_INET) {
233 data_len = recvfromto(sockfd, buf, len, flags,
234 (struct sockaddr *)&src, &sizeof_src,
235 (struct sockaddr *)&dst, &sizeof_dst);
239 * No udpfromto, OR an IPv6 socket. Fail gracefully.
241 data_len = recvfrom(sockfd, buf, len, flags,
242 (struct sockaddr *)&src, &sizeof_src);
248 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, &port)) {
250 return -1; /* Unknown address family, Die Die Die! */
254 fr_sockaddr2ipaddr(&dst, sizeof_dst, dst_ipaddr, &port);
258 * Different address families should never happen.
260 if (src.ss_family != dst.ss_family) {
266 * Tell the caller about the data
273 RADIUS_PACKET *vqp_recv(int sockfd)
278 RADIUS_PACKET *packet;
281 * Allocate the new request data structure
283 if ((packet = malloc(sizeof(*packet))) == NULL) {
284 fr_strerror_printf("out of memory");
287 memset(packet, 0, sizeof(*packet));
289 packet->data_len = vqp_recvfrom(sockfd, &packet->data, 0,
290 &packet->src_ipaddr, &packet->src_port,
291 &packet->dst_ipaddr, &packet->dst_port);
294 * Check for socket errors.
296 if (packet->data_len < 0) {
297 fr_strerror_printf("Error receiving packet: %s", strerror(errno));
298 /* packet->data is NULL */
305 * We can only receive packets formatted in a way we
306 * expect. However, we accept MORE attributes in a
307 * packet than normal implementations may send.
309 if (packet->data_len < VQP_HDR_LEN) {
310 fr_strerror_printf("VQP packet is too short");
319 for (i = 0; i < packet->data_len; i++) {
320 if ((i & 0x0f) == 0) fprintf(stderr, "%02x: ", i);
321 fprintf(stderr, "%02x ", ptr[i]);
322 if ((i & 0x0f) == 0x0f) fprintf(stderr, "\n");
327 if (ptr[3] > VQP_MAX_ATTRIBUTES) {
328 fr_strerror_printf("Too many VQP attributes");
333 if (packet->data_len > VQP_HDR_LEN) {
340 length = packet->data_len - VQP_HDR_LEN;
344 fr_strerror_printf("Packet contains malformed attribute");
350 * Attributes are 4 bytes
351 * 0x00000c01 ... 0x00000c08
353 if ((ptr[0] != 0) || (ptr[1] != 0) ||
354 (ptr[2] != 0x0c) || (ptr[3] < 1) || (ptr[3] > 8)) {
355 fr_strerror_printf("Packet contains invalid attribute");
363 * We support lengths 1..253, for internal
364 * server reasons. Also, there's no reason
365 * for bigger lengths to exist... admins
366 * won't be typing in a 32K vlan name.
368 * Except for received ethernet frames...
369 * they get chopped to 253 internally.
372 ((ptr[4] != 0) || (ptr[5] > MAX_VMPS_LEN))) {
373 fr_strerror_printf("Packet contains attribute with invalid length %02x %02x", ptr[4], ptr[5]);
377 attrlen = (ptr[4] << 8) | ptr[5];
379 length -= (6 + attrlen);
383 packet->sockfd = sockfd;
387 * This is more than a bit of a hack.
389 packet->code = PW_AUTHENTICATION_REQUEST;
391 memcpy(&id, packet->data + 4, 4);
392 packet->id = ntohl(id);
395 * FIXME: Create a fake "request authenticator", to
396 * avoid duplicates? Or is the VQP sequence number
397 * adequate for this purpose?
404 * We do NOT mirror the old-style RADIUS code that does encode,
405 * sign && send in one function. For VQP, the caller MUST perform
406 * each task manually, and separately.
408 int vqp_send(RADIUS_PACKET *packet)
410 if (!packet || !packet->data || (packet->data_len < 8)) return -1;
413 * Don't print out the attributes, they were printed out
414 * when it was encoded.
418 * And send it on it's way.
420 return vqp_sendto(packet->sockfd, packet->data, packet->data_len, 0,
421 &packet->src_ipaddr, &packet->dst_ipaddr,
426 int vqp_decode(RADIUS_PACKET *packet)
429 int attribute, length;
430 VALUE_PAIR *vp, **tail;
432 if (!packet || !packet->data) return -1;
434 if (packet->data_len < VQP_HDR_LEN) return -1;
438 vp = paircreate(PW_VQP_PACKET_TYPE, 0, PW_TYPE_OCTETS);
440 fr_strerror_printf("No memory");
443 vp->lvalue = packet->data[1];
449 vp = paircreate(PW_VQP_ERROR_CODE, 0, PW_TYPE_OCTETS);
451 fr_strerror_printf("No memory");
454 vp->lvalue = packet->data[2];
460 vp = paircreate(PW_VQP_SEQUENCE_NUMBER, 0, PW_TYPE_OCTETS);
462 fr_strerror_printf("No memory");
465 vp->lvalue = packet->id; /* already set by vqp_recv */
471 ptr = packet->data + VQP_HDR_LEN;
472 end = packet->data + packet->data_len;
475 * Note that vqp_recv() MUST ensure that the packet is
476 * formatted in a way we expect, and that vqp_recv() MUST
477 * be called before vqp_decode().
480 attribute = (ptr[2] << 8) | ptr[3];
481 length = (ptr[4] << 8) | ptr[5];
485 * Hack to get the dictionaries to work correctly.
488 vp = paircreate(attribute, 0, PW_TYPE_OCTETS);
490 pairfree(&packet->vps);
492 fr_strerror_printf("No memory");
499 memcpy(&vp->vp_ipaddr, ptr, 4);
503 vp->type = PW_TYPE_OCTETS;
509 vp->length = (length > MAX_VMPS_LEN) ? MAX_VMPS_LEN : length;
510 memcpy(vp->vp_octets, ptr, vp->length);
511 vp->vp_octets[vp->length] = '\0';
522 * FIXME: Map attributes to Calling-Station-Id, etc...
529 * These are the MUST HAVE contents for a VQP packet.
531 * We don't allow the caller to give less than these, because
532 * it won't work. We don't encode more than these, because the
533 * clients will ignore it.
535 * FIXME: Be more generous? Look for CISCO + VQP attributes?
537 static int contents[5][VQP_MAX_ATTRIBUTES] = {
538 { 0, 0, 0, 0, 0, 0 },
539 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c05 }, /* Join request */
540 { 0x0c03, 0x0c08, 0, 0, 0, 0 }, /* Join Response */
541 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c08 }, /* Reconfirm */
542 { 0x0c03, 0x0c08, 0, 0, 0, 0 }
545 int vqp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
550 VALUE_PAIR *vps[VQP_MAX_ATTRIBUTES];
553 fr_strerror_printf("Failed encoding VQP");
557 if (packet->data) return 0;
559 vp = pairfind(packet->vps, PW_VQP_PACKET_TYPE, 0);
561 fr_strerror_printf("Failed to find VQP-Packet-Type in response packet");
566 if ((code < 1) || (code > 4)) {
567 fr_strerror_printf("Invalid value %d for VQP-Packet-Type", code);
571 length = VQP_HDR_LEN;
572 memset(vps, 0, sizeof(vps));
574 vp = pairfind(packet->vps, PW_VQP_ERROR_CODE, 0);
577 * FIXME: Map attributes from calling-station-Id, etc.
579 * Maybe do this via rlm_vqp? That's probably the
580 * best place to add the code...
584 * No error: encode attributes.
586 if (!vp) for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
587 if (!contents[code][i]) break;
589 vps[i] = pairfind(packet->vps, contents[code][i] | 0x2000, 0);
592 * FIXME: Print the name...
595 fr_strerror_printf("Failed to find VQP attribute %02x",
601 length += vps[i]->length;
604 packet->data = malloc(length);
606 fr_strerror_printf("No memory");
609 packet->data_len = length;
613 ptr[0] = VQP_VERSION;
619 ptr[2] = vp->lvalue & 0xff;
624 * The number of attributes is hard-coded.
626 if ((code == 1) || (code == 3)) {
629 ptr[3] = VQP_MAX_ATTRIBUTES;
631 sequence = htonl(packet->id);
632 memcpy(ptr + 4, &sequence, 4);
635 fr_strerror_printf("Cannot send VQP response without request");
640 * Packet Sequence Number
642 memcpy(ptr + 4, original->data + 4, 4);
652 for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
659 * Type. Note that we look at only the lower 8
660 * bits, as the upper 8 bits have been hacked.
661 * See also dictionary.vqp
666 ptr[3] = vp->attribute & 0xff;
670 ptr[5] = vp->length & 0xff;
677 memcpy(ptr, &vp->vp_ipaddr, 4);
683 memcpy(ptr, vp->vp_octets, vp->length);