uq-its-ss / bacnet-device

Node library for implementing a BACnet device
6 stars 3 forks source link

IAm response directed to BBMD instead of caller device #3

Open gcibeira opened 1 year ago

gcibeira commented 1 year ago

Hi everyone! I'm working in developing a BBMD using the bacstack library. I'm using a virtual network made with virtualbox, with 2 subnet linked with a router.

bacnet lab network (1)

Device1 is my BBMD implementation. Device2 is a simple bacnet client which call a WhoIs() on the broadcast address. Device3 is our bacnet-device made with this repository.

When a "WhoIs" request arrives from the BBMD, the response is directed to the BBMD tiself, instead of the caller device. I've solved it changeing the sender address to the "forwardedFrom" address before responding (line 210 in bacnet-device.js file):

if (msg.header.sender.forwardedFrom) {
                // Message came from a BBMD, so need to reply as if we're a BBMD.
                //msg.header.sender.forwardedFrom = this.ip;

                // Actually it seems like responses should be returned direct to the
                // caller by the device, bypassing the BBMD.
                msg.header.sender.address = msg.header.sender.forwardedFrom;
                msg.header.sender.forwardedFrom = null;
            }

The problem now is that the caller device sees the router's IP instead of the responding device, as seen in the wireshark capture

Captura de pantalla 2022-12-29 171607

I'm not sure if it's ok, or it could be a problem when binding device instance with IP address.

gcibeira commented 1 year ago

So, after some research, I found 2 things:

  1. It's not clear if a device must respond a "WhoIs" request with a unicast messagge to the caller, or a broadcast messagge as well. Most device implementations I've found respond with a broadcast (see traffic captures). If that's the case, we may change the iAmResponse() call with a broadcast (line 215 in bacnet-device.js file).

  2. It is ok for a device to see the IP of the router when receive a response from another subnet. It's a normal behavior of NAT. But now I wonder how can a device initiate a unicast conversation after a discovery in another subnet.

Cheers!

References:

Wireshark bacnet traffic captures: http://kargs.net/captures/ https://wiki.wireshark.org/uploads/__moin_import__/attachments/Protocols/bacnet/bacnet-stack-services.cap

BBMD with NAT: http://www.bacnetwiki.com/wiki/index.php?title=Using_BACnet_over_the_Internet https://www.ashrae.org/File%20Library/Technical%20Resources/Standards%20and%20Guidelines/Standards%20Addenda/135-2008/Add-135-2008o_Final.pdf