+
+ r_code = buf[0]; // request type
+ r_id = buf[1]; // radius indentifier.
+
+ if (r_code != DisconnectRequest && r_code != CoARequest)
+ {
+ LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code));
+ return;
+ }
+
+ if (!config->cluster_iam_master)
+ {
+ master_forward_dae_packet(buf, len, addr->sin_addr.s_addr, addr->sin_port);
+ return;
+ }
+
+ len = ntohs(*(uint16_t *) (buf + 2));
+
+ LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code), r_id);
+
+ // check authenticator
+ memcpy(vector, buf + 4, 16);
+ memset(buf + 4, 0, 16);
+
+ i = strlen(config->radiussecret);
+ if (i > 16) i = 16;
+
+ MD5Init(&ctx);
+ MD5Update(&ctx, buf, len);
+ MD5Update(&ctx, buf, config->radiussecret, i);
+ MD5Final(hash, &ctx);
+ if (memcmp(hash, vector, 16) != 0)
+ {
+ LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
+ return;
+ }
+
+ // unpack attributes
+ packet = buf + 20;
+ length = len - 20;
+
+ while (length > 0)
+ {
+ attribute = *packet++;
+ attribute_length = *packet++;
+ if (attribute_length < 2)
+ break;
+
+ length -= attribute_length;
+ attribute_length -= 2;
+ switch (attribute)
+ {
+ case 1: /* username */
+ len = attribute_length < MAXUSER ? attribute_length : MAXUSER - 1;
+ memcpy(username, packet, len);
+ username[len] = 0;
+ LOG(4, 0, 0, " Received DAE User-Name: %s\n", username);
+ break;
+
+ case 4: /* nas ip address */
+ nas = *(uint32_t *) packet; // net order
+ if (nas != config->bind_address)
+ error = 403; // NAS identification mismatch
+
+ LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas, 0));
+ break;
+
+ case 5: /* nas port */
+ port = ntohl(*(uint32_t *) packet);
+ if (port < 1 || port > MAXSESSION)
+ error = 404;
+
+ LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port);
+ break;
+
+ case 6: /* service type */
+ {
+ uint32_t service_type = ntohl(*(uint32_t *) packet);
+ auth_only = service_type == 8; // Authenticate only
+
+ LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type);
+ }
+ break;
+
+ case 8: /* ip address */
+ ip = *(uint32_t *) packet; // net order
+ LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip, 0));
+ break;
+
+ case 11: /* filter id */
+ LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length, packet);
+ if (!(p = memchr(packet, '.', attribute_length)))
+ {
+ error = 404; // invalid request
+ break;
+ }
+
+ len = p - packet;
+ i = find_filter(packet, len);
+ if (i < 0 || !*ip_filters[i].name)
+ {
+ error = 404;
+ break;
+ }
+
+ if (!memcmp(p, ".in", attribute_length - len))
+ fin = i + 1;
+ else if (!memcmp(p, ".out", attribute_length - len))
+ fout = i + 1;
+ else
+ error = 404;
+
+ break;
+
+ case 26: /* vendor specific */
+ if (attribute_length >= 6
+ && ntohl(*(uint32_t *) packet) == 9 // Cisco
+ && *(packet + 4) == 1 // Cisco-AVPair
+ && *(packet + 5) >= 2) // length
+ {
+ int len = *(packet + 5) - 2;
+ uint8_t *a = packet + 6;
+
+ LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len, a);
+ if (avp < sizeof(avpair)/sizeof(*avpair) - 1)
+ {
+ avpair[avp] = a;
+ avpair_len[avp++] = len;
+ }
+ }
+ break;
+ }
+
+ packet += attribute_length;
+ }
+
+ if (!error && auth_only)
+ {
+ if (fin != -1 || fout != -1 || avp)
+ error = 401; // unsupported attribute
+ else
+ error = 405; // unsupported service
+ }
+
+ if (!error && !(port || ip || *username))
+ error = 402; // missing attribute
+
+ // exact match for SID if given
+ if (!error && port)
+ {
+ s = port;
+ if (!session[s].opened)
+ error = 503; // not found
+ }
+
+ if (!error && ip)
+ {
+ // find/check session by IP
+ i = sessionbyip(ip);
+ if (!i || (s && s != i)) // not found or mismatching port
+ error = 503;
+ else
+ s = i;
+ }
+
+ if (!error && *username)
+ {
+ if (s)
+ {
+ if (strcmp(session[s].user, username))
+ error = 503;
+ }
+ else if (!(s = sessionbyuser(username)))
+ error = 503;
+ }
+
+ t = session[s].tunnel;
+
+ switch (r_code)
+ {
+ case DisconnectRequest: // Packet of Disconnect/Death
+ if (error)
+ {
+ r_code = DisconnectNAK;
+ break;
+ }
+
+ LOG(3, s, t, " DAE Disconnect %d (%s)\n", s, session[s].user);
+ r_code = DisconnectACK;
+
+ sessionshutdown(s, "Requested by PoD", 3, 0); // disconnect session
+ break;
+
+ case CoARequest: // Change of Authorization
+ if (error)
+ {
+ r_code = CoANAK;
+ break;
+ }
+
+ LOG(3, s, t, " DAE Change %d (%s)\n", s, session[s].user);
+ r_code = CoAACK;
+
+ // reset
+ {
+ struct param_radius_reset p = { &tunnel[session[s].tunnel], &session[s] };
+ run_plugins(PLUGIN_RADIUS_RESET, &p);
+ }
+
+ // apply filters
+ if (fin == -1)
+ fin = 0;
+ else
+ LOG(3, s, t, " Filter in %d (%s)\n", fin, ip_filters[fin - 1].name);
+
+ if (fout == -1)
+ fout = 0;
+ else
+ LOG(3, s, t, " Filter out %d (%s)\n", fout, ip_filters[fout - 1].name);
+
+ filter_session(s, fin, fout);
+
+ // process cisco av-pair(s)
+ for (i = 0; i < avp; i++)
+ {
+ LOG(3, s, t, " Cisco-AVPair: %.*s\n", avpair_len[i], avpair[i]);
+ handle_avpair(s, avpair[i], avpair_len[i]);
+ }
+
+ cluster_send_session(s);
+ break;
+ }
+
+ // send response
+ packet = buf;
+ *packet++ = r_code;
+ *packet++ = r_id;
+ packet += 2;
+ memset(packet, 0, 16);
+ packet += 16;
+ len = 20;
+
+ // add attributes
+ if (error)
+ {
+ // add error cause
+ *packet++ = 101;
+ *packet++ = 6;
+ *(uint32_t *) packet = htonl(error);
+ len += 6;
+ }
+
+ *((uint16_t *)(buf + 2)) = htons(len);
+
+ // make vector
+ i = strlen(config->radiussecret);
+ if (i > 16) i = 16;
+
+ MD5Init(&ctx);
+ MD5Update(&ctx, buf, len);
+ MD5Update(&ctx, config->radiussecret, i);
+ MD5Final(hash, &ctx);
+ memcpy(buf + 4, hash, 16);
+
+ LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code), r_id);
+
+ // send DAE response
+ if (sendto(daefd, buf, len, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) addr, alen) < 0)
+ LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno));