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 if (!fr_ipaddr2sockaddr(src_ipaddr, 0, &src, &sizeof_src)) {
90 return -1; /* Unknown address family, Die Die Die! */
93 src_ipaddr = src_ipaddr; /* -Wunused */
96 if (!fr_ipaddr2sockaddr(dst_ipaddr, dst_port, &dst, &sizeof_dst)) {
97 return -1; /* Unknown address family, Die Die Die! */
100 #ifdef WITH_UDPFROMTO
102 * Only IPv4 is supported for udpfromto.
104 * And if they don't specify a source IP address, don't
107 if ((dst_ipaddr->af == AF_INET) &&
108 (src_ipaddr->af != AF_UNSPEC)) {
109 return sendfromto(sockfd, data, data_len, flags,
110 (struct sockaddr *)&src, sizeof_src,
111 (struct sockaddr *)&dst, sizeof_dst);
114 src_ipaddr = src_ipaddr; /* -Wunused */
118 * No udpfromto, OR an IPv6 socket, fail gracefully.
120 return sendto(sockfd, data, data_len, flags,
121 (struct sockaddr *)&dst, sizeof_dst);
125 * Wrapper for recvfrom, which handles recvfromto, IPv6, and all
126 * possible combinations.
128 * FIXME: This is copied from rad_recvfrom, with minor edits.
130 static ssize_t vqp_recvfrom(int sockfd, uint8_t **pbuf, int flags,
131 fr_ipaddr_t *src_ipaddr, uint16_t *src_port,
132 fr_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
134 struct sockaddr_storage src;
135 struct sockaddr_storage dst;
136 socklen_t sizeof_src = sizeof(src);
137 socklen_t sizeof_dst = sizeof(dst);
144 memset(&src, 0, sizeof_src);
145 memset(&dst, 0, sizeof_dst);
148 * Get address family, etc. first, so we know if we
149 * need to do udpfromto.
151 * FIXME: udpfromto also does this, but it's not
152 * a critical problem.
154 if (getsockname(sockfd, (struct sockaddr *)&dst,
155 &sizeof_dst) < 0) return -1;
158 * Read the length of the packet, from the packet.
159 * This lets us allocate the buffer to use for
160 * reading the rest of the packet.
162 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
163 (struct sockaddr *)&src, &sizeof_src);
164 if (data_len < 0) return -1;
167 * Too little data is available, discard the packet.
170 rad_recv_discard(sockfd);
175 * Invalid version, packet type, or too many
178 } else if ((header[0] != VQP_VERSION) ||
181 (header[3] > VQP_MAX_ATTRIBUTES)) {
182 rad_recv_discard(sockfd);
186 } else { /* we got 4 bytes of data. */
188 * We don't care about the contents for now...
192 * How many attributes are in the packet.
196 if ((header[1] == 1) || (header[1] == 3)) {
197 if (len != VQP_MAX_ATTRIBUTES) {
198 rad_recv_discard(sockfd);
203 * Maximum length we support.
205 len = (12 * (4 + 4 + MAX_VMPS_LEN));
209 rad_recv_discard(sockfd);
214 * Maximum length we support.
216 len = (12 * (4 + 4 + MAX_VMPS_LEN));
222 * For now, be generous.
224 len = (12 * (4 + 4 + MAX_VMPS_LEN));
230 * Receive the packet. The OS will discard any data in the
231 * packet after "len" bytes.
233 #ifdef WITH_UDPFROMTO
234 if (dst.ss_family == AF_INET) {
235 data_len = recvfromto(sockfd, buf, len, flags,
236 (struct sockaddr *)&src, &sizeof_src,
237 (struct sockaddr *)&dst, &sizeof_dst);
241 * No udpfromto, OR an IPv6 socket. Fail gracefully.
243 data_len = recvfrom(sockfd, buf, len, flags,
244 (struct sockaddr *)&src, &sizeof_src);
250 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, &port)) {
252 return -1; /* Unknown address family, Die Die Die! */
256 fr_sockaddr2ipaddr(&dst, sizeof_dst, dst_ipaddr, &port);
260 * Different address families should never happen.
262 if (src.ss_family != dst.ss_family) {
268 * Tell the caller about the data
275 RADIUS_PACKET *vqp_recv(int sockfd)
280 RADIUS_PACKET *packet;
283 * Allocate the new request data structure
285 packet = rad_alloc(NULL, 0);
287 fr_strerror_printf("out of memory");
291 length = vqp_recvfrom(sockfd, &packet->data, 0,
292 &packet->src_ipaddr, &packet->src_port,
293 &packet->dst_ipaddr, &packet->dst_port);
296 * Check for socket errors.
299 fr_strerror_printf("Error receiving packet: %s", strerror(errno));
300 /* packet->data is NULL */
304 packet->data_len = length; /* unsigned vs signed */
307 * We can only receive packets formatted in a way we
308 * expect. However, we accept MORE attributes in a
309 * packet than normal implementations may send.
311 if (packet->data_len < VQP_HDR_LEN) {
312 fr_strerror_printf("VQP packet is too short");
321 for (i = 0; i < packet->data_len; i++) {
322 if ((i & 0x0f) == 0) fprintf(stderr, "%02x: ", (int) i);
323 fprintf(stderr, "%02x ", ptr[i]);
324 if ((i & 0x0f) == 0x0f) fprintf(stderr, "\n");
329 if (ptr[3] > VQP_MAX_ATTRIBUTES) {
330 fr_strerror_printf("Too many VQP attributes");
335 if (packet->data_len > VQP_HDR_LEN) {
342 length = packet->data_len - VQP_HDR_LEN;
346 fr_strerror_printf("Packet contains malformed attribute");
352 * Attributes are 4 bytes
353 * 0x00000c01 ... 0x00000c08
355 if ((ptr[0] != 0) || (ptr[1] != 0) ||
356 (ptr[2] != 0x0c) || (ptr[3] < 1) || (ptr[3] > 8)) {
357 fr_strerror_printf("Packet contains invalid attribute");
365 * We support lengths 1..253, for internal
366 * server reasons. Also, there's no reason
367 * for bigger lengths to exist... admins
368 * won't be typing in a 32K vlan name.
370 * Except for received ethernet frames...
371 * they get chopped to 253 internally.
374 ((ptr[4] != 0) || (ptr[5] > MAX_VMPS_LEN))) {
375 fr_strerror_printf("Packet contains attribute with invalid length %02x %02x", ptr[4], ptr[5]);
379 attrlen = (ptr[4] << 8) | ptr[5];
381 length -= (6 + attrlen);
385 packet->sockfd = sockfd;
389 * This is more than a bit of a hack.
391 packet->code = PW_AUTHENTICATION_REQUEST;
393 memcpy(&id, packet->data + 4, 4);
394 packet->id = ntohl(id);
397 * FIXME: Create a fake "request authenticator", to
398 * avoid duplicates? Or is the VQP sequence number
399 * adequate for this purpose?
406 * We do NOT mirror the old-style RADIUS code that does encode,
407 * sign && send in one function. For VQP, the caller MUST perform
408 * each task manually, and separately.
410 int vqp_send(RADIUS_PACKET *packet)
412 if (!packet || !packet->data || (packet->data_len < 8)) return -1;
415 * Don't print out the attributes, they were printed out
416 * when it was encoded.
420 * And send it on it's way.
422 return vqp_sendto(packet->sockfd, packet->data, packet->data_len, 0,
423 &packet->src_ipaddr, &packet->dst_ipaddr,
428 int vqp_decode(RADIUS_PACKET *packet)
431 int attribute, length;
432 VALUE_PAIR *vp, **tail;
434 if (!packet || !packet->data) return -1;
436 if (packet->data_len < VQP_HDR_LEN) return -1;
440 vp = paircreate(PW_VQP_PACKET_TYPE, 0);
442 fr_strerror_printf("No memory");
445 vp->vp_integer = packet->data[1];
451 vp = paircreate(PW_VQP_ERROR_CODE, 0);
453 fr_strerror_printf("No memory");
456 vp->vp_integer = packet->data[2];
462 vp = paircreate(PW_VQP_SEQUENCE_NUMBER, 0);
464 fr_strerror_printf("No memory");
467 vp->vp_integer = packet->id; /* already set by vqp_recv */
473 ptr = packet->data + VQP_HDR_LEN;
474 end = packet->data + packet->data_len;
477 * Note that vqp_recv() MUST ensure that the packet is
478 * formatted in a way we expect, and that vqp_recv() MUST
479 * be called before vqp_decode().
482 attribute = (ptr[2] << 8) | ptr[3];
483 length = (ptr[4] << 8) | ptr[5];
487 * Hack to get the dictionaries to work correctly.
490 vp = paircreate(attribute, 0);
492 pairfree(&packet->vps);
494 fr_strerror_printf("No memory");
498 switch (vp->da->type) {
501 memcpy(&vp->vp_ipaddr, ptr, 4);
507 * Value doesn't match the type we have for the
508 * valuepair so we must change it's da to an
511 vp->da = dict_attrunknown(vp->da->attr, vp->da->vendor,
518 vp->length = (length > MAX_VMPS_LEN) ? MAX_VMPS_LEN : length;
519 memcpy(vp->vp_octets, ptr, vp->length);
520 vp->vp_octets[vp->length] = '\0';
531 * FIXME: Map attributes to Calling-Station-Id, etc...
538 * These are the MUST HAVE contents for a VQP packet.
540 * We don't allow the caller to give less than these, because
541 * it won't work. We don't encode more than these, because the
542 * clients will ignore it.
544 * FIXME: Be more generous? Look for CISCO + VQP attributes?
546 static int contents[5][VQP_MAX_ATTRIBUTES] = {
547 { 0, 0, 0, 0, 0, 0 },
548 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c05 }, /* Join request */
549 { 0x0c03, 0x0c08, 0, 0, 0, 0 }, /* Join Response */
550 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c08 }, /* Reconfirm */
551 { 0x0c03, 0x0c08, 0, 0, 0, 0 }
554 int vqp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
559 VALUE_PAIR *vps[VQP_MAX_ATTRIBUTES];
562 fr_strerror_printf("Failed encoding VQP");
566 if (packet->data) return 0;
568 vp = pairfind(packet->vps, PW_VQP_PACKET_TYPE, 0, TAG_ANY);
570 fr_strerror_printf("Failed to find VQP-Packet-Type in response packet");
574 code = vp->vp_integer;
575 if ((code < 1) || (code > 4)) {
576 fr_strerror_printf("Invalid value %d for VQP-Packet-Type", code);
580 length = VQP_HDR_LEN;
581 memset(vps, 0, sizeof(vps));
583 vp = pairfind(packet->vps, PW_VQP_ERROR_CODE, 0, TAG_ANY);
586 * FIXME: Map attributes from calling-station-Id, etc.
588 * Maybe do this via rlm_vqp? That's probably the
589 * best place to add the code...
593 * No error: encode attributes.
595 if (!vp) for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
596 if (!contents[code][i]) break;
598 vps[i] = pairfind(packet->vps, contents[code][i] | 0x2000, 0, TAG_ANY);
601 * FIXME: Print the name...
604 fr_strerror_printf("Failed to find VQP attribute %02x",
610 length += vps[i]->length;
613 packet->data = malloc(length);
615 fr_strerror_printf("No memory");
618 packet->data_len = length;
622 ptr[0] = VQP_VERSION;
628 ptr[2] = vp->vp_integer & 0xff;
633 * The number of attributes is hard-coded.
635 if ((code == 1) || (code == 3)) {
638 ptr[3] = VQP_MAX_ATTRIBUTES;
640 sequence = htonl(packet->id);
641 memcpy(ptr + 4, &sequence, 4);
644 fr_strerror_printf("Cannot send VQP response without request");
649 * Packet Sequence Number
651 memcpy(ptr + 4, original->data + 4, 4);
661 for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
663 if (ptr >= (packet->data + packet->data_len)) break;
670 * Type. Note that we look at only the lower 8
671 * bits, as the upper 8 bits have been hacked.
672 * See also dictionary.vqp
677 ptr[3] = vp->da->attr & 0xff;
681 ptr[5] = vp->length & 0xff;
686 switch (vp->da->type) {
688 memcpy(ptr, &vp->vp_ipaddr, 4);
694 memcpy(ptr, vp->vp_octets, vp->length);