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>
33 * http://www.openbsd.org/cgi-bin/cvsweb/src/usr.sbin/tcpdump/print-vqp.c
35 * Some of how it works:
37 * http://www.hackingciscoexposed.com/pdf/chapter12.pdf
39 * VLAN Query Protocol (VQP)
42 * 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
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Version | Opcode | Response Code | Data Count |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * VQP is layered over UDP. The default destination port is 1589.
64 #define VQP_HDR_LEN (8)
65 #define VQP_VERSION (1)
66 #define VQP_MAX_ATTRIBUTES (12)
70 * Wrapper for sendto which handles sendfromto, IPv6, and all
71 * possible combinations.
73 * FIXME: This is just a copy of rad_sendto().
74 * Duplicate code is bad.
76 static int vqp_sendto(int sockfd, void *data, size_t data_len, int flags,
77 fr_ipaddr_t *src_ipaddr, fr_ipaddr_t *dst_ipaddr,
80 struct sockaddr_storage dst;
84 struct sockaddr_storage src;
87 fr_ipaddr2sockaddr(src_ipaddr, 0, &src, &sizeof_src);
89 src_ipaddr = src_ipaddr; /* -Wunused */
92 if (!fr_ipaddr2sockaddr(dst_ipaddr, dst_port, &dst, &sizeof_dst)) {
93 return -1; /* Unknown address family, Die Die Die! */
98 * Only IPv4 is supported for udpfromto.
100 * And if they don't specify a source IP address, don't
103 if ((dst_ipaddr->af == AF_INET) ||
104 (src_ipaddr->af != AF_UNSPEC)) {
105 return sendfromto(sockfd, data, data_len, flags,
106 (struct sockaddr *)&src, sizeof_src,
107 (struct sockaddr *)&dst, sizeof_dst);
110 src_ipaddr = src_ipaddr; /* -Wunused */
114 * No udpfromto, OR an IPv6 socket, fail gracefully.
116 return sendto(sockfd, data, data_len, flags,
117 (struct sockaddr *)&dst, sizeof_dst);
121 * Wrapper for recvfrom, which handles recvfromto, IPv6, and all
122 * possible combinations.
124 * FIXME: This is copied from rad_recvfrom, with minor edits.
126 static ssize_t vqp_recvfrom(int sockfd, uint8_t **pbuf, int flags,
127 fr_ipaddr_t *src_ipaddr, uint16_t *src_port,
128 fr_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
130 struct sockaddr_storage src;
131 struct sockaddr_storage dst;
132 socklen_t sizeof_src = sizeof(src);
133 socklen_t sizeof_dst = sizeof(dst);
140 memset(&src, 0, sizeof_src);
141 memset(&dst, 0, sizeof_dst);
144 * Get address family, etc. first, so we know if we
145 * need to do udpfromto.
147 * FIXME: udpfromto also does this, but it's not
148 * a critical problem.
150 if (getsockname(sockfd, (struct sockaddr *)&dst,
151 &sizeof_dst) < 0) return -1;
154 * Read the length of the packet, from the packet.
155 * This lets us allocate the buffer to use for
156 * reading the rest of the packet.
158 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
159 (struct sockaddr *)&src, &sizeof_src);
160 if (data_len < 0) return -1;
163 * Too little data is available, discard the packet.
166 recvfrom(sockfd, header, sizeof(header), flags,
167 (struct sockaddr *)&src, &sizeof_src);
171 * Invalid version, packet type, or too many
174 } else if ((header[0] != VQP_VERSION) ||
177 (header[3] > VQP_MAX_ATTRIBUTES)) {
178 recvfrom(sockfd, header, sizeof(header), flags,
179 (struct sockaddr *)&src, &sizeof_src);
182 } else { /* we got 4 bytes of data. */
184 * We don't care about the contents for now...
188 * How many attributes are in the packet.
192 if ((header[1] == 1) || (header[1] == 3)) {
193 if (len != VQP_MAX_ATTRIBUTES) {
194 recvfrom(sockfd, header, sizeof(header), 0,
195 (struct sockaddr *)&src, &sizeof_src);
199 * Maximum length we support.
201 len = (12 * (4 + 4 + 253));
205 recvfrom(sockfd, header, sizeof(header), 0,
206 (struct sockaddr *)&src, &sizeof_src);
210 * Maximum length we support.
212 len = (12 * (4 + 4 + 253));
218 * For now, be generous.
220 len = (12 * (4 + 4 + 253));
226 * Receive the packet. The OS will discard any data in the
227 * packet after "len" bytes.
229 #ifdef WITH_UDPFROMTO
230 if (dst.ss_family == AF_INET) {
231 data_len = recvfromto(sockfd, buf, len, flags,
232 (struct sockaddr *)&src, &sizeof_src,
233 (struct sockaddr *)&dst, &sizeof_dst);
237 * No udpfromto, OR an IPv6 socket. Fail gracefully.
239 data_len = recvfrom(sockfd, buf, len, flags,
240 (struct sockaddr *)&src, &sizeof_src);
246 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, &port)) {
248 return -1; /* Unknown address family, Die Die Die! */
252 fr_sockaddr2ipaddr(&dst, sizeof_dst, dst_ipaddr, &port);
256 * Different address families should never happen.
258 if (src.ss_family != dst.ss_family) {
264 * Tell the caller about the data
271 RADIUS_PACKET *vqp_recv(int sockfd)
276 RADIUS_PACKET *packet;
279 * Allocate the new request data structure
281 if ((packet = malloc(sizeof(*packet))) == NULL) {
282 librad_log("out of memory");
285 memset(packet, 0, sizeof(*packet));
287 packet->data_len = vqp_recvfrom(sockfd, &packet->data, 0,
288 &packet->src_ipaddr, &packet->src_port,
289 &packet->dst_ipaddr, &packet->dst_port);
292 * Check for socket errors.
294 if (packet->data_len < 0) {
295 librad_log("Error receiving packet: %s", strerror(errno));
296 /* packet->data is NULL */
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 librad_log("VQP packet is too short");
317 for (i = 0; i < packet->data_len; i++) {
318 if ((i & 0x0f) == 0) fprintf(stderr, "%02x: ", i);
319 fprintf(stderr, "%02x ", ptr[i]);
320 if ((i & 0x0f) == 0x0f) fprintf(stderr, "\n");
325 if (ptr[3] > VQP_MAX_ATTRIBUTES) {
326 librad_log("Too many VQP attributes");
331 if (packet->data_len > VQP_HDR_LEN) {
338 length = packet->data_len - VQP_HDR_LEN;
342 librad_log("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 librad_log("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 if ((ptr[4] != 0) || (ptr[5] > 253)) {
367 librad_log("Packet contains attribute with invalid length %02x %02x", ptr[4], ptr[5]);
373 length -= (6 + attrlen);
377 packet->sockfd = sockfd;
381 * This is more than a bit of a hack.
383 packet->code = PW_AUTHENTICATION_REQUEST;
385 memcpy(&id, packet->data + 4, 4);
386 packet->id = ntohl(id);
389 * FIXME: Create a fake "request authenticator", to
390 * avoid duplicates? Or is the VQP sequence number
391 * adequate for this purpose?
398 * We do NOT mirror the old-style RADIUS code that does encode,
399 * sign && send in one function. For VQP, the caller MUST perform
400 * each task manually, and separately.
402 int vqp_send(RADIUS_PACKET *packet)
404 if (!packet || !packet->data || (packet->data_len < 8)) return -1;
407 * Don't print out the attributes, they were printed out
408 * when it was encoded.
412 * And send it on it's way.
414 return vqp_sendto(packet->sockfd, packet->data, packet->data_len, 0,
415 &packet->src_ipaddr, &packet->dst_ipaddr,
420 int vqp_decode(RADIUS_PACKET *packet)
423 int attribute, length;
424 VALUE_PAIR *vp, **tail;
426 if (!packet || !packet->data) return -1;
428 if (packet->data_len < VQP_HDR_LEN) return -1;
432 vp = paircreate(PW_VQP_PACKET_TYPE, PW_TYPE_OCTETS);
434 librad_log("No memory");
437 vp->lvalue = packet->data[1];
443 vp = paircreate(PW_VQP_ERROR_CODE, PW_TYPE_OCTETS);
445 librad_log("No memory");
448 vp->lvalue = packet->data[2];
454 vp = paircreate(PW_VQP_SEQUENCE_NUMBER, PW_TYPE_OCTETS);
456 librad_log("No memory");
459 vp->lvalue = packet->id; /* already set by vqp_recv */
465 ptr = packet->data + VQP_HDR_LEN;
466 end = packet->data + packet->data_len;
469 * Note that vqp_recv() MUST ensure that the packet is
470 * formatted in a way we expect, and that vqp_recv() MUST
471 * be called before vqp_decode().
474 attribute = (ptr[2] << 8) | ptr[3];
479 * Hack to get the dictionaries to work correctly.
482 vp = paircreate(attribute, PW_TYPE_OCTETS);
484 pairfree(&packet->vps);
486 librad_log("No memory");
493 memcpy(&vp->vp_ipaddr, ptr, 4);
497 vp->type = PW_TYPE_OCTETS;
503 memcpy(vp->vp_octets, ptr, length);
515 * FIXME: Map attributes to Calling-Station-Id, etc...
522 * These are the MUST HAVE contents for a VQP packet.
524 * We don't allow the caller to give less than these, because
525 * it won't work. We don't encode more than these, because the
526 * clients will ignore it.
528 * FIXME: Be more generous? Look for CISCO + VQP attributes?
530 static int contents[5][VQP_MAX_ATTRIBUTES] = {
531 { 0, 0, 0, 0, 0, 0 },
532 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c05 }, /* Join request */
533 { 0x0c03, 0x0c08, 0, 0, 0, 0 }, /* Join Response */
534 { 0x0c01, 0x0c02, 0x0c03, 0x0c04, 0x0c07, 0x0c08 }, /* Reconfirm */
535 { 0x0c03, 0x0c08, 0, 0, 0, 0 }
538 int vqp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
543 VALUE_PAIR *vps[VQP_MAX_ATTRIBUTES];
546 librad_log("Failed encoding VQP");
550 if (packet->data) return 0;
552 vp = pairfind(packet->vps, PW_VQP_PACKET_TYPE);
554 librad_log("Failed to find VQP-Packet-Type in response packet");
559 if ((code < 1) || (code > 4)) {
560 librad_log("Invalid value %d for VQP-Packet-Type", code);
564 length = VQP_HDR_LEN;
565 memset(vps, 0, sizeof(vps));
567 vp = pairfind(packet->vps, PW_VQP_ERROR_CODE);
570 * FIXME: Map attributes from calling-station-Id, etc.
572 * Maybe do this via rlm_vqp? That's probably the
573 * best place to add the code...
577 * No error: encode attributes.
579 if (!vp) for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
580 if (!contents[code][i]) break;
582 vps[i] = pairfind(packet->vps, contents[code][i] | 0x2000);
585 * FIXME: Print the name...
588 librad_log("Failed to find VQP attribute %02x",
594 length += vps[i]->length;
597 packet->data = malloc(length);
599 librad_log("No memory");
602 packet->data_len = length;
606 ptr[0] = VQP_VERSION;
612 ptr[2] = vp->lvalue & 0xff;
617 * The number of attributes is hard-coded.
619 if ((code == 1) || (code == 3)) {
622 ptr[3] = VQP_MAX_ATTRIBUTES;
624 sequence = htonl(packet->id);
625 memcpy(ptr + 4, &sequence, 4);
628 librad_log("Cannot send VQP response without request");
633 * Packet Sequence Number
635 memcpy(ptr + 4, original->data + 4, 4);
645 for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) {
652 * Type. Note that we look at only the lower 8
653 * bits, as the upper 8 bits have been hacked.
654 * See also dictionary.vqp
659 ptr[3] = vp->attribute & 0xff;
663 ptr[5] = vp->length & 0xff;
670 memcpy(ptr, &vp->vp_ipaddr, 4);
676 memcpy(ptr, vp->vp_octets, vp->length);