Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[knx] Improve handling of unknown encrypted frames #17721

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@
import org.slf4j.LoggerFactory;

import tuwien.auto.calimero.CloseEvent;
import tuwien.auto.calimero.DataUnitBuilder;
import tuwien.auto.calimero.DetachEvent;
import tuwien.auto.calimero.FrameEvent;
import tuwien.auto.calimero.GroupAddress;
import tuwien.auto.calimero.IndividualAddress;
import tuwien.auto.calimero.KNXAddress;
import tuwien.auto.calimero.KNXException;
import tuwien.auto.calimero.KNXIllegalArgumentException;
import tuwien.auto.calimero.cemi.CEMILData;
import tuwien.auto.calimero.cemi.CemiTData;
import tuwien.auto.calimero.datapoint.CommandDP;
import tuwien.auto.calimero.datapoint.Datapoint;
import tuwien.auto.calimero.device.ProcessCommunicationResponder;
Expand All @@ -60,6 +64,7 @@
import tuwien.auto.calimero.process.ProcessEvent;
import tuwien.auto.calimero.process.ProcessListener;
import tuwien.auto.calimero.secure.KnxSecureException;
import tuwien.auto.calimero.secure.SecureApplicationLayer;
import tuwien.auto.calimero.secure.Security;

/**
Expand Down Expand Up @@ -348,12 +353,13 @@ private void processEvent(String task, ProcessEvent event, ListenerNotification
if (!isHandled) {
logger.trace("Address '{}' is not configured in openHAB", destination);
final String type = switch (event.getServiceCode()) {
case 0x80 -> " GROUP_WRITE(";
case 0x40 -> " GROUP_RESPONSE(";
case 0x00 -> " GROUP_READ(";
default -> " ?(";
case 0x80 -> "GROUP_WRITE";
case 0x40 -> "GROUP_RESPONSE";
case 0x00 -> "GROUP_READ";
default -> "?";
};
final String key = destination.toString() + type + event.getASDU().length + ")";
final String key = String.format("%2d/%1d/%3d %s(%02d)", destination.getMainGroup(),
destination.getMiddleGroup(), destination.getSubGroup8(), type, event.getASDU().length);
commandExtensionData.unknownGA().compute(key, (k, v) -> v == null ? 1 : v + 1);
}
}
Expand Down Expand Up @@ -429,7 +435,38 @@ public void linkClosed(@Nullable CloseEvent closeEvent) {

@Override
public void indication(@Nullable FrameEvent e) {
// no-op
// NetworkLinkListener indication. This implementation is triggered whenever a frame is received.
// It is not necessary for OH, as we process incoming group writes via different triggers.
// However, this indication also covers encrypted data secure frames, which would typically
// be dropped silently by the Calimero library (a log message is only visible when log level for Calimero
// is set manually).

// Implementation searches for incoming data secure frames which cannot be decoded due to missing key
if (e != null) {
final var cemi = e.getFrame();
if (!(cemi instanceof CemiTData)) {
final CEMILData f = (CEMILData) cemi;
final int ctrl = f.getPayload()[0] & 0xfc;
if (ctrl == 0) {
final KNXAddress dst = f.getDestination();
if (dst instanceof GroupAddress ga) {
if (dst.getRawAddress() != 0) {
final byte[] payload = f.getPayload();
final int service = DataUnitBuilder.getAPDUService(payload);
if (service == SecureApplicationLayer.SecureService) {
if (!openhabSecurity.groupKeys().containsKey(dst)) {
logger.trace("Address '{}' cannot be decrypted, group key missing", dst);
final String key = String.format(
"%2d/%1d/%3d secure: missing group key, cannot decrypt", ga.getMainGroup(),
ga.getMiddleGroup(), ga.getSubGroup8());
commandExtensionData.unknownGA().compute(key, (k, v) -> v == null ? 1 : v + 1);
}
}
}
}
}
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void execute(String[] args, Console console) {
console.println("KNX bridge \"" + bridgeHandler.getThing().getLabel()
+ "\": group address, type, number of bytes, and number of occurrence since last reload of binding:");
for (Entry<String, Long> entry : bridgeHandler.getCommandExtensionData().unknownGA().entrySet()) {
console.println(entry.getKey() + " " + entry.getValue());
console.println(entry.getKey() + " " + entry.getValue());
}
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -85,7 +86,7 @@ public SecureRoutingConfig() {
* Helper class to carry information which can be used by the
* command line extension (openHAB console).
*/
public record CommandExtensionData(Map<String, Long> unknownGA) {
public record CommandExtensionData(SortedMap<String, Long> unknownGA) {
}

private final ScheduledExecutorService knxScheduler = ThreadPoolManager.getScheduledPool("knx");
Expand Down