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/vqp.h>
30 * http://www.openbsd.org/cgi-bin/cvsweb/src/usr.sbin/tcpdump/print-vqp.c
32 * Some of how it works:
34 * http://www.hackingciscoexposed.com/pdf/chapter12.pdf
36 * VLAN Query Protocol (VQP)
39 * 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
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Version | Opcode | Response Code | Data Count |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * VQP is layered over UDP. The default destination port is 1589.
61 #define VQP_HDR_LEN (8)
62 #define VQP_VERSION (1)
63 #define VQP_MAX_ATTRIBUTES (12)
67 * Wrapper for sendto which handles sendfromto, IPv6, and all
68 * possible combinations.
70 * FIXME: This is just a copy of rad_sendto().
71 * Duplicate code is bad.
73 static int vqp_sendto(int sockfd, void *data, size_t data_len, int flags,
74 lrad_ipaddr_t *src_ipaddr, lrad_ipaddr_t *dst_ipaddr,
77 struct sockaddr_storage dst;
78 socklen_t sizeof_dst = sizeof(dst);
81 struct sockaddr_storage src;
82 socklen_t sizeof_src = sizeof(src);
84 memset(&src, 0, sizeof(src));
86 memset(&dst, 0, sizeof(dst));
91 if (dst_ipaddr->af == AF_INET) {
92 struct sockaddr_in *s4;
94 s4 = (struct sockaddr_in *)&dst;
95 sizeof_dst = sizeof(struct sockaddr_in);
97 s4->sin_family = AF_INET;
98 s4->sin_addr = dst_ipaddr->ipaddr.ip4addr;
99 s4->sin_port = htons(dst_port);
101 #ifdef WITH_UDPFROMTO
102 s4 = (struct sockaddr_in *)&src;
103 sizeof_src = sizeof(struct sockaddr_in);
105 s4->sin_family = AF_INET;
106 s4->sin_addr = src_ipaddr->ipaddr.ip4addr;
110 * IPv6 MAY be supported.
112 #ifdef HAVE_STRUCT_SOCKADDR_IN6
113 } else if (dst_ipaddr->af == AF_INET6) {
114 struct sockaddr_in6 *s6;
116 s6 = (struct sockaddr_in6 *)&dst;
117 sizeof_dst = sizeof(struct sockaddr_in6);
119 s6->sin6_family = AF_INET6;
120 s6->sin6_addr = dst_ipaddr->ipaddr.ip6addr;
121 s6->sin6_port = htons(dst_port);
123 #ifdef WITH_UDPFROMTO
124 return -1; /* UDPFROMTO && IPv6 are not supported */
126 s6 = (struct sockaddr_in6 *)&src;
127 sizeof_src = sizeof(struct sockaddr_in6);
129 s6->sin6_family = AF_INET6;
130 s6->sin6_addr = src_ipaddr->ipaddr.ip6addr;
132 #endif /* WITH_UDPFROMTO */
133 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
134 } else return -1; /* Unknown address family, Die Die Die! */
136 #ifdef WITH_UDPFROMTO
138 * Only IPv4 is supported for udpfromto.
140 * And if they don't specify a source IP address, don't
143 if ((dst_ipaddr->af == AF_INET) ||
144 (src_ipaddr->af != AF_UNSPEC)) {
145 return sendfromto(sockfd, data, data_len, flags,
146 (struct sockaddr *)&src, sizeof_src,
147 (struct sockaddr *)&dst, sizeof_dst);
150 src_ipaddr = src_ipaddr; /* -Wunused */
154 * No udpfromto, OR an IPv6 socket, fail gracefully.
156 return sendto(sockfd, data, data_len, flags,
157 (struct sockaddr *)&dst, sizeof_dst);
161 * Wrapper for recvfrom, which handles recvfromto, IPv6, and all
162 * possible combinations.
164 * FIXME: This is copied from rad_recvfrom, with minor edits.
166 static ssize_t vqp_recvfrom(int sockfd, uint8_t **pbuf, int flags,
167 lrad_ipaddr_t *src_ipaddr, uint16_t *src_port,
168 lrad_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
170 struct sockaddr_storage src;
171 struct sockaddr_storage dst;
172 socklen_t sizeof_src = sizeof(src);
173 socklen_t sizeof_dst = sizeof(dst);
179 memset(&src, 0, sizeof_src);
180 memset(&dst, 0, sizeof_dst);
183 * Get address family, etc. first, so we know if we
184 * need to do udpfromto.
186 * FIXME: udpfromto also does this, but it's not
187 * a critical problem.
189 if (getsockname(sockfd, (struct sockaddr *)&dst,
190 &sizeof_dst) < 0) return -1;
193 * Read the length of the packet, from the packet.
194 * This lets us allocate the buffer to use for
195 * reading the rest of the packet.
197 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
198 (struct sockaddr *)&src, &sizeof_src);
199 if (data_len < 0) return -1;
202 * Too little data is available, discard the packet.
205 recvfrom(sockfd, header, sizeof(header), flags,
206 (struct sockaddr *)&src, &sizeof_src);
210 * Invalid version, packet type, or too many
213 } else if ((header[0] != VQP_VERSION) ||
216 (header[3] > VQP_MAX_ATTRIBUTES)) {
217 recvfrom(sockfd, header, sizeof(header), flags,
218 (struct sockaddr *)&src, &sizeof_src);
221 } else { /* we got 4 bytes of data. */
223 * We don't care about the contents for now...
227 * How many attributes are in the packet.
231 if ((header[1] == 1) || (header[1] == 3)) {
232 if (len != VQP_MAX_ATTRIBUTES) {
233 recvfrom(sockfd, header, sizeof(header), 0,
234 (struct sockaddr *)&src, &sizeof_src);
238 * Maximum length we support.
240 len = (12 * (4 + 4 + 253));
244 recvfrom(sockfd, header, sizeof(header), 0,
245 (struct sockaddr *)&src, &sizeof_src);
249 * Maximum length we support.
251 len = (12 * (4 + 4 + 253));
257 * For now, be generous.
259 len = (12 * (4 + 4 + 253));
265 * Receive the packet. The OS will discard any data in the
266 * packet after "len" bytes.
268 #ifdef WITH_UDPFROMTO
269 if (dst.ss_family == AF_INET) {
270 data_len = recvfromto(sockfd, buf, len, flags,
271 (struct sockaddr *)&src, &sizeof_src,
272 (struct sockaddr *)&dst, &sizeof_dst);
276 * No udpfromto, OR an IPv6 socket. Fail gracefully.
278 data_len = recvfrom(sockfd, buf, len, flags,
279 (struct sockaddr *)&src, &sizeof_src);
286 * Check address families, and update src/dst ports, etc.
288 if (src.ss_family == AF_INET) {
289 struct sockaddr_in *s4;
291 s4 = (struct sockaddr_in *)&src;
292 src_ipaddr->af = AF_INET;
293 src_ipaddr->ipaddr.ip4addr = s4->sin_addr;
294 *src_port = ntohs(s4->sin_port);
296 s4 = (struct sockaddr_in *)&dst;
297 dst_ipaddr->af = AF_INET;
298 dst_ipaddr->ipaddr.ip4addr = s4->sin_addr;
299 *dst_port = ntohs(s4->sin_port);
301 #ifdef HAVE_STRUCT_SOCKADDR_IN6
302 } else if (src.ss_family == AF_INET6) {
303 struct sockaddr_in6 *s6;
305 s6 = (struct sockaddr_in6 *)&src;
306 src_ipaddr->af = AF_INET6;
307 src_ipaddr->ipaddr.ip6addr = s6->sin6_addr;
308 *src_port = ntohs(s6->sin6_port);
310 s6 = (struct sockaddr_in6 *)&dst;
311 dst_ipaddr->af = AF_INET6;
312 dst_ipaddr->ipaddr.ip6addr = s6->sin6_addr;
313 *dst_port = ntohs(s6->sin6_port);
317 return -1; /* Unknown address family, Die Die Die! */
321 * Different address families should never happen.
323 if (src.ss_family != dst.ss_family) {
329 * Tell the caller about the data
336 RADIUS_PACKET *vqp_recv(int sockfd)
341 RADIUS_PACKET *packet;
344 * Allocate the new request data structure
346 if ((packet = malloc(sizeof(*packet))) == NULL) {
347 librad_log("out of memory");
350 memset(packet, 0, sizeof(*packet));
352 packet->data_len = vqp_recvfrom(sockfd, &packet->data, 0,
353 &packet->src_ipaddr, &packet->src_port,
354 &packet->dst_ipaddr, &packet->dst_port);
357 * Check for socket errors.
359 if (packet->data_len < 0) {
360 librad_log("Error receiving packet: %s", strerror(errno));
361 /* packet->data is NULL */
368 * We can only receive packets formatted in a way we
369 * expect. However, we accept MORE attributes in a
370 * packet than normal implementations may send.
372 if (packet->data_len < VQP_HDR_LEN) {
373 librad_log("VQP packet is too short");
382 for (i = 0; i < packet->data_len; i++) {
383 if ((i & 0x0f) == 0) fprintf(stderr, "%02x: ", i);
384 fprintf(stderr, "%02x ", ptr[i]);
385 if ((i & 0x0f) == 0x0f) fprintf(stderr, "\n");
390 if (ptr[3] > VQP_MAX_ATTRIBUTES) {
391 librad_log("Too many VQP attributes");
396 if (packet->data_len > VQP_HDR_LEN) {
403 length = packet->data_len - VQP_HDR_LEN;
407 librad_log("Packet contains malformed attribute");
413 * Attributes are 4 bytes
414 * 0x00000c01 ... 0x00000c08
416 if ((ptr[0] != 0) || (ptr[1] != 0) ||
417 (ptr[2] != 0x0c) || (ptr[3] < 1) || (ptr[3] > 8)) {
418 librad_log("Packet contains invalid attribute");
426 * We support lengths 1..253, for internal
427 * server reasons. Also, there's no reason
428 * for bigger lengths to exist... admins
429 * won't be typing in a 32K vlan name.
431 if ((ptr[4] != 0) || (ptr[5] > 253)) {
432 librad_log("Packet contains attribute with invalid length %02x %02x", ptr[4], ptr[5]);
438 length -= (6 + attrlen);
442 packet->sockfd = sockfd;
446 * This is more than a bit of a hack.
448 packet->code = PW_AUTHENTICATION_REQUEST;
450 memcpy(&id, packet->data + 4, 4);
451 packet->id = ntohl(id);
457 * We do NOT mirror the old-style RADIUS code that does encode,
458 * sign && send in one function. For VQP, the caller MUST perform
459 * each task manually, and separately.
461 int vqp_send(RADIUS_PACKET *packet)
463 if (!packet || !packet->data || (packet->data_len < 8)) return -1;
466 * Don't print out the attributes, they were printed out
467 * when it was encoded.
471 * And send it on it's way.
473 return vqp_sendto(packet->sockfd, packet->data, packet->data_len, 0,
474 &packet->src_ipaddr, &packet->dst_ipaddr,
479 int vqp_decode(RADIUS_PACKET *packet)
482 int attribute, length;
483 VALUE_PAIR *vp, **tail;
485 if (!packet || !packet->data) return -1;
487 if (packet->data_len < VQP_HDR_LEN) return -1;
491 vp = paircreate(PW_VQP_PACKET_TYPE, PW_TYPE_OCTETS);
493 librad_log("No memory");
496 vp->lvalue = packet->data[1];
502 vp = paircreate(PW_VQP_ERROR_CODE, PW_TYPE_OCTETS);
504 librad_log("No memory");
507 vp->lvalue = packet->data[2];
513 vp = paircreate(PW_VQP_SEQUENCE_NUMBER, PW_TYPE_OCTETS);
515 librad_log("No memory");
518 vp->lvalue = packet->id; /* already set by vqp_recv */
524 ptr = packet->data + VQP_HDR_LEN;
525 end = packet->data + packet->data_len;
528 * Note that vqp_recv() MUST ensure that the packet is
529 * formatted in a way we expect, and that vqp_recv() MUST
530 * be called before vqp_decode().
533 attribute = (ptr[2] << 8) | ptr[3];
538 * Hack to get the dictionaries to work correctly.
541 vp = paircreate(attribute, PW_TYPE_OCTETS);
543 pairfree(&packet->vps);
545 librad_log("No memory");
552 memcpy(&vp->vp_ipaddr, ptr, 4);
556 vp->type = PW_TYPE_OCTETS;
562 memcpy(vp->vp_octets, ptr, length);
574 * FIXME: Map attributes to Calling-Station-Id, etc...
581 * These are the MUST HAVE contents for a VQP packet.
583 * We don't allow the caller to give less than these, because
584 * it won't work. We don't encode more than these, because the
585 * clients will ignore it.
587 * FIXME: Be more generous? Look for CISCO + VQP attributes?
589 static int contents[5][VQP_MAX_ATTRIBUTES] = {
590 { 0, 0, 0, 0, 0, 0 },
591 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c05 }, /* Join request */
592 { 0x0c03, 0x0c08, 0, 0, 0, 0 }, /* Join Response */
593 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c08 }, /* Reconfirm */
594 { 0x0c03, 0x0c08, 0, 0, 0, 0 }
597 int vqp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
602 VALUE_PAIR *vps[VQP_MAX_ATTRIBUTES];
605 librad_log("Failed encoding VQP");
609 if (packet->data) return 0;
611 vp = pairfind(packet->vps, PW_VQP_PACKET_TYPE);
613 librad_log("Failed to find VQP-Packet-Type in response packet");
618 if ((code < 1) || (code > 4)) {
619 librad_log("Invalid value %d for VQP-Packet-Type", code);
623 length = VQP_HDR_LEN;
624 memset(vps, 0, sizeof(vps));
626 vp = pairfind(packet->vps, PW_VQP_ERROR_CODE);
629 * FIXME: Map attributes from calling-station-Id, etc.
631 * Maybe do this via rlm_vqp? That's probably the
632 * best place to add the code...
636 * No error: encode attributes.
638 if (!vp) for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
639 if (!contents[code][i]) break;
641 vps[i] = pairfind(packet->vps, contents[code][i] | 0x2000);
644 * FIXME: Print the name...
647 librad_log("Failed to find VQP attribute %02x",
653 length += vps[i]->length;
656 packet->data = malloc(length);
658 librad_log("No memory");
661 packet->data_len = length;
665 ptr[0] = VQP_VERSION;
671 ptr[2] = vp->lvalue & 0xff;
676 * The number of attributes is hard-coded.
678 if ((code == 1) || (code == 3)) {
681 ptr[3] = VQP_MAX_ATTRIBUTES;
683 sequence = htonl(packet->id);
684 memcpy(ptr + 4, &sequence, 4);
687 librad_log("Cannot send VQP response without request");
692 * Packet Sequence Number
694 memcpy(ptr + 4, original->data + 4, 4);
704 for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
711 * Type. Note that we look at only the lower 8
712 * bits, as the upper 8 bits have been hacked.
713 * See also dictionary.vqp
718 ptr[3] = vp->attribute & 0xff;
722 ptr[5] = vp->length & 0xff;
729 memcpy(ptr, &vp->vp_ipaddr, 4);
735 memcpy(ptr, vp->vp_octets, vp->length);