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>
25 #include <freeradius-devel/libradius.h>
26 #include <freeradius-devel/udpfromto.h>
30 #define MAX_VMPS_LEN (MAX_STRING_LEN - 1)
32 /* @todo: this is a hack */
33 # define DEBUG if (fr_debug_flag && fr_log_fp) fr_printf_log
34 # define debug_pair(vp) do { if (fr_debug_flag && fr_log_fp) { \
35 vp_print(fr_log_fp, vp); \
39 * http://www.openbsd.org/cgi-bin/cvsweb/src/usr.sbin/tcpdump/print-vqp.c
41 * Some of how it works:
43 * http://www.hackingciscoexposed.com/pdf/chapter12.pdf
45 * VLAN Query Protocol (VQP)
48 * 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
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | Version | Opcode | Response Code | Data Count |
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * VQP is layered over UDP. The default destination port is 1589.
70 #define VQP_HDR_LEN (8)
71 #define VQP_VERSION (1)
72 #define VQP_MAX_ATTRIBUTES (12)
76 * Wrapper for sendto which handles sendfromto, IPv6, and all
77 * possible combinations.
79 * FIXME: This is just a copy of rad_sendto().
80 * Duplicate code is bad.
82 static int vqp_sendto(int sockfd, void *data, size_t data_len, int flags,
84 fr_ipaddr_t *src_ipaddr,
86 UNUSED fr_ipaddr_t *src_ipaddr,
88 fr_ipaddr_t *dst_ipaddr,
91 struct sockaddr_storage dst;
95 struct sockaddr_storage src;
98 if (!fr_ipaddr2sockaddr(src_ipaddr, 0, &src, &sizeof_src)) {
99 return -1; /* Unknown address family, Die Die Die! */
103 if (!fr_ipaddr2sockaddr(dst_ipaddr, dst_port, &dst, &sizeof_dst)) {
104 return -1; /* Unknown address family, Die Die Die! */
107 #ifdef WITH_UDPFROMTO
109 * Only IPv4 is supported for udpfromto.
111 * And if they don't specify a source IP address, don't
114 if ((dst_ipaddr->af == AF_INET) &&
115 (src_ipaddr->af != AF_UNSPEC)) {
116 return sendfromto(sockfd, data, data_len, flags,
117 (struct sockaddr *)&src, sizeof_src,
118 (struct sockaddr *)&dst, sizeof_dst);
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, RADIUS_PACKET *packet, 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);
148 memset(&src, 0, sizeof_src);
149 memset(&dst, 0, sizeof_dst);
152 * Get address family, etc. first, so we know if we
153 * need to do udpfromto.
155 * FIXME: udpfromto also does this, but it's not
156 * a critical problem.
158 if (getsockname(sockfd, (struct sockaddr *)&dst,
159 &sizeof_dst) < 0) return -1;
162 * Read the length of the packet, from the packet.
163 * This lets us allocate the buffer to use for
164 * reading the rest of the packet.
166 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
167 (struct sockaddr *)&src, &sizeof_src);
168 if (data_len < 0) return -1;
171 * Too little data is available, discard the packet.
174 rad_recv_discard(sockfd);
179 * Invalid version, packet type, or too many
182 } else if ((header[0] != VQP_VERSION) ||
185 (header[3] > VQP_MAX_ATTRIBUTES)) {
186 rad_recv_discard(sockfd);
190 } else { /* we got 4 bytes of data. */
192 * We don't care about the contents for now...
196 * How many attributes are in the packet.
200 if ((header[1] == 1) || (header[1] == 3)) {
201 if (len != VQP_MAX_ATTRIBUTES) {
202 rad_recv_discard(sockfd);
207 * Maximum length we support.
209 len = (12 * (4 + 4 + MAX_VMPS_LEN));
213 rad_recv_discard(sockfd);
218 * Maximum length we support.
220 len = (12 * (4 + 4 + MAX_VMPS_LEN));
226 * For now, be generous.
228 len = (12 * (4 + 4 + MAX_VMPS_LEN));
230 packet->data = talloc_array(packet, uint8_t, len);
231 if (!packet->data) return -1;
234 * Receive the packet. The OS will discard any data in the
235 * packet after "len" bytes.
237 #ifdef WITH_UDPFROMTO
238 if (dst.ss_family == AF_INET) {
239 data_len = recvfromto(sockfd, packet->data, len, flags,
240 (struct sockaddr *)&src, &sizeof_src,
241 (struct sockaddr *)&dst, &sizeof_dst);
245 * No udpfromto, OR an IPv6 socket. Fail gracefully.
247 data_len = recvfrom(sockfd, packet->data, len, flags,
248 (struct sockaddr *)&src, &sizeof_src);
253 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, &port)) {
254 return -1; /* Unknown address family, Die Die Die! */
258 fr_sockaddr2ipaddr(&dst, sizeof_dst, dst_ipaddr, &port);
262 * Different address families should never happen.
264 if (src.ss_family != dst.ss_family) {
271 RADIUS_PACKET *vqp_recv(int sockfd)
276 RADIUS_PACKET *packet;
279 * Allocate the new request data structure
281 packet = rad_alloc(NULL, false);
283 fr_strerror_printf("out of memory");
287 length = vqp_recvfrom(sockfd, packet, 0,
288 &packet->src_ipaddr, &packet->src_port,
289 &packet->dst_ipaddr, &packet->dst_port);
292 * Check for socket errors.
295 fr_strerror_printf("Error receiving packet: %s", fr_syserror(errno));
296 /* packet->data is NULL */
300 packet->data_len = length; /* unsigned vs signed */
303 * We can only receive packets formatted in a way we
304 * expect. However, we accept MORE attributes in a
305 * packet than normal implementations may send.
307 if (packet->data_len < VQP_HDR_LEN) {
308 fr_strerror_printf("VQP packet is too short");
317 for (i = 0; i < packet->data_len; i++) {
318 if ((i & 0x0f) == 0) fprintf(stderr, "%02x: ", (int) i);
319 fprintf(stderr, "%02x ", ptr[i]);
320 if ((i & 0x0f) == 0x0f) fprintf(stderr, "\n");
325 if (ptr[3] > VQP_MAX_ATTRIBUTES) {
326 fr_strerror_printf("Too many VQP attributes");
331 if (packet->data_len > VQP_HDR_LEN) {
338 length = packet->data_len - VQP_HDR_LEN;
342 fr_strerror_printf("Packet contains malformed attribute");
348 * Attributes are 4 bytes
349 * 0x00000c01 ... 0x00000c08
351 if ((ptr[0] != 0) || (ptr[1] != 0) ||
352 (ptr[2] != 0x0c) || (ptr[3] < 1) || (ptr[3] > 8)) {
353 fr_strerror_printf("Packet contains invalid attribute");
361 * We support lengths 1..253, for internal
362 * server reasons. Also, there's no reason
363 * for bigger lengths to exist... admins
364 * won't be typing in a 32K vlan name.
366 * Except for received ethernet frames...
367 * they get chopped to 253 internally.
370 ((ptr[4] != 0) || (ptr[5] > MAX_VMPS_LEN))) {
371 fr_strerror_printf("Packet contains attribute with invalid length %02x %02x", ptr[4], ptr[5]);
375 attrlen = (ptr[4] << 8) | ptr[5];
377 length -= (6 + attrlen);
381 packet->sockfd = sockfd;
385 * This is more than a bit of a hack.
387 packet->code = PW_CODE_ACCESS_REQUEST;
389 memcpy(&id, packet->data + 4, 4);
390 packet->id = ntohl(id);
393 * FIXME: Create a fake "request authenticator", to
394 * avoid duplicates? Or is the VQP sequence number
395 * adequate for this purpose?
402 * We do NOT mirror the old-style RADIUS code that does encode,
403 * sign && send in one function. For VQP, the caller MUST perform
404 * each task manually, and separately.
406 int vqp_send(RADIUS_PACKET *packet)
408 if (!packet || !packet->data || (packet->data_len < 8)) return -1;
411 * Don't print out the attributes, they were printed out
412 * when it was encoded.
416 * And send it on it's way.
418 return vqp_sendto(packet->sockfd, packet->data, packet->data_len, 0,
419 &packet->src_ipaddr, &packet->dst_ipaddr,
424 int vqp_decode(RADIUS_PACKET *packet)
427 int attribute, length;
431 if (!packet || !packet->data) return -1;
433 if (packet->data_len < VQP_HDR_LEN) return -1;
435 fr_cursor_init(&cursor, &packet->vps);
436 vp = paircreate(packet, PW_VQP_PACKET_TYPE, 0);
438 fr_strerror_printf("No memory");
441 vp->vp_integer = packet->data[1];
443 fr_cursor_insert(&cursor, vp);
445 vp = paircreate(packet, PW_VQP_ERROR_CODE, 0);
447 fr_strerror_printf("No memory");
450 vp->vp_integer = packet->data[2];
452 fr_cursor_insert(&cursor, vp);
454 vp = paircreate(packet, PW_VQP_SEQUENCE_NUMBER, 0);
456 fr_strerror_printf("No memory");
459 vp->vp_integer = packet->id; /* already set by vqp_recv */
461 fr_cursor_insert(&cursor, vp);
463 ptr = packet->data + VQP_HDR_LEN;
464 end = packet->data + packet->data_len;
467 * Note that vqp_recv() MUST ensure that the packet is
468 * formatted in a way we expect, and that vqp_recv() MUST
469 * be called before vqp_decode().
474 attribute = (ptr[2] << 8) | ptr[3];
475 length = (ptr[4] << 8) | ptr[5];
479 * Hack to get the dictionaries to work correctly.
482 vp = paircreate(packet, attribute, 0);
484 pairfree(&packet->vps);
486 fr_strerror_printf("No memory");
490 switch (vp->da->type) {
491 case PW_TYPE_IPV4_ADDR:
493 memcpy(&vp->vp_ipaddr, ptr, 4);
499 * Value doesn't match the type we have for the
500 * valuepair so we must change it's da to an
503 vp->da = dict_attrunknown(vp->da->attr, vp->da->vendor,
510 pairmemcpy(vp, ptr, length);
512 pairmemcpy(vp, ptr, 1024);
519 vp->vp_strvalue = p = talloc_array(vp, char, vp->length + 1);
521 memcpy(p, ptr, vp->length);
522 p[vp->length] = '\0';
525 vp->vp_strvalue = p = talloc_array(vp, char, 1025);
527 memcpy(p, ptr, vp->length);
528 p[vp->length] = '\0';
534 fr_cursor_insert(&cursor, vp);
538 * FIXME: Map attributes to Calling-Station-Id, etc...
545 * These are the MUST HAVE contents for a VQP packet.
547 * We don't allow the caller to give less than these, because
548 * it won't work. We don't encode more than these, because the
549 * clients will ignore it.
551 * FIXME: Be more generous? Look for CISCO + VQP attributes?
553 static int contents[5][VQP_MAX_ATTRIBUTES] = {
554 { 0, 0, 0, 0, 0, 0 },
555 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c05 }, /* Join request */
556 { 0x0c03, 0x0c08, 0, 0, 0, 0 }, /* Join Response */
557 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c08 }, /* Reconfirm */
558 { 0x0c03, 0x0c08, 0, 0, 0, 0 }
561 int vqp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
566 VALUE_PAIR *vps[VQP_MAX_ATTRIBUTES];
569 fr_strerror_printf("Failed encoding VQP");
573 if (packet->data) return 0;
575 vp = pairfind(packet->vps, PW_VQP_PACKET_TYPE, 0, TAG_ANY);
577 fr_strerror_printf("Failed to find VQP-Packet-Type in response packet");
581 code = vp->vp_integer;
582 if ((code < 1) || (code > 4)) {
583 fr_strerror_printf("Invalid value %d for VQP-Packet-Type", code);
587 length = VQP_HDR_LEN;
588 memset(vps, 0, sizeof(vps));
590 vp = pairfind(packet->vps, PW_VQP_ERROR_CODE, 0, TAG_ANY);
593 * FIXME: Map attributes from calling-station-Id, etc.
595 * Maybe do this via rlm_vqp? That's probably the
596 * best place to add the code...
600 * No error: encode attributes.
602 if (!vp) for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
603 if (!contents[code][i]) break;
605 vps[i] = pairfind(packet->vps, contents[code][i] | 0x2000, 0, TAG_ANY);
608 * FIXME: Print the name...
611 fr_strerror_printf("Failed to find VQP attribute %02x",
617 length += vps[i]->length;
620 packet->data = talloc_array(packet, uint8_t, length);
622 fr_strerror_printf("No memory");
625 packet->data_len = length;
629 ptr[0] = VQP_VERSION;
635 ptr[2] = vp->vp_integer & 0xff;
640 * The number of attributes is hard-coded.
642 if ((code == 1) || (code == 3)) {
645 ptr[3] = VQP_MAX_ATTRIBUTES;
647 sequence = htonl(packet->id);
648 memcpy(ptr + 4, &sequence, 4);
651 fr_strerror_printf("Cannot send VQP response without request");
656 * Packet Sequence Number
658 memcpy(ptr + 4, original->data + 4, 4);
668 for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
670 if (ptr >= (packet->data + packet->data_len)) break;
677 * Type. Note that we look at only the lower 8
678 * bits, as the upper 8 bits have been hacked.
679 * See also dictionary.vqp
684 ptr[3] = vp->da->attr & 0xff;
688 ptr[5] = vp->length & 0xff;
693 switch (vp->da->type) {
694 case PW_TYPE_IPV4_ADDR:
695 memcpy(ptr, &vp->vp_ipaddr, 4);
701 memcpy(ptr, vp->vp_octets, vp->length);