fix BACnet/IP on OS to bind broadcast to specific port (#489)

* Fixes #346 by binding the broadcast socket to the port specific broadcast address rather than INADDR_ANY or INADDR_BROADCAST. Added additional compile and environment options for those who might need them. 

* Changed the define USE_CLASSADDR to BACNET_IP_BROADCAST_USE_CLASSADDR to help manage namespaces.

* Added BACNET_IP_BROADCAST_BIND_ADDR environment variable to override the default broadcast binding address.

---------

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2023-09-10 10:24:18 -05:00
committed by GitHub
parent 0001f85f29
commit 55b8b3d93c
18 changed files with 231 additions and 58 deletions
+19 -15
View File
@@ -715,14 +715,14 @@ int bvlc_bbmd_disabled_handler(BACNET_IP_ADDRESS *addr,
if (function_len) {
if (bbmd_address_match_self(&fwd_address)) {
/* ignore forwards from my IPv4 address */
debug_print_string("Forwarded-NPDU is me!");
debug_print_string("Dropped Forwarded-NPDU from me!");
break;
}
bvlc_ip_address_to_bacnet_local(src, &fwd_address);
offset = header_len + function_len - npdu_len;
debug_print_npdu("Forwarded-NPDU", offset, npdu_len);
} else {
debug_print_string("Forwarded-NPDU: Unable to decode!");
debug_print_string("Dropped Forwarded-NPDU: Malformed!");
}
break;
case BVLC_REGISTER_FOREIGN_DEVICE:
@@ -751,7 +751,8 @@ int bvlc_bbmd_disabled_handler(BACNET_IP_ADDRESS *addr,
debug_print_bip("Received Original-Unicast-NPDU", addr);
if (bbmd_address_match_self(addr)) {
/* ignore messages from my IPv4 address */
debug_print_string("Original-Unicast-NPDU is me!");
debug_print_string(
"Dropped Original-Unicast-NPDU from me!");
break;
}
function_len = bvlc_decode_original_unicast(
@@ -762,14 +763,15 @@ int bvlc_bbmd_disabled_handler(BACNET_IP_ADDRESS *addr,
debug_print_npdu("Original-Unicast-NPDU", offset, npdu_len);
} else {
debug_print_string(
"Original-Unicast-NPDU: Unable to decode!");
"Dropped Original-Unicast-NPDU: Malformed!");
}
break;
case BVLC_ORIGINAL_BROADCAST_NPDU:
debug_print_bip("Received Original-Broadcast-NPDU", addr);
if (bbmd_address_match_self(addr)) {
/* ignore messages from my IPv4 address */
debug_print_string("Original-Broadcast-NPDU is me!");
debug_print_string(
"Dropped Original-Broadcast-NPDU from me!");
break;
}
function_len = bvlc_decode_original_broadcast(
@@ -784,15 +786,15 @@ int bvlc_bbmd_disabled_handler(BACNET_IP_ADDRESS *addr,
npdu = &mtu[offset];
if (npdu_confirmed_service(npdu, npdu_len)) {
offset = 0;
debug_print_string("Original-Broadcast-NPDU: "
"Confirmed Service! Discard!");
debug_print_string("Dropped Original-Broadcast-NPDU: "
"Confirmed Service!");
} else {
debug_print_npdu(
"Original-Broadcast-NPDU", offset, npdu_len);
}
} else {
debug_print_string(
"Original-Broadcast-NPDU: Unable to decode!");
"Dropped Original-Broadcast-NPDU: Malformed!");
}
break;
case BVLC_SECURE_BVLL:
@@ -922,7 +924,7 @@ int bvlc_bbmd_enabled_handler(BACNET_IP_ADDRESS *addr,
if (function_len) {
if (bbmd_address_match_self(&fwd_address)) {
/* ignore forwards from my IPv4 address */
debug_print_string("Forwarded-NPDU is me!");
debug_print_string("Dropped Forwarded-NPDU from me!");
break;
}
if (bbmd_bdt_member_mask_is_unicast(addr)) {
@@ -1057,7 +1059,7 @@ int bvlc_bbmd_enabled_handler(BACNET_IP_ADDRESS *addr,
debug_print_bip("Received Original-Unicast-NPDU", addr);
if (bbmd_address_match_self(addr)) {
/* ignore messages from my IPv4 address */
debug_print_string("Original-Unicast-NPDU is me!");
debug_print_string("Dropped Original-Unicast-NPDU from me!");
break;
}
function_len =
@@ -1069,14 +1071,14 @@ int bvlc_bbmd_enabled_handler(BACNET_IP_ADDRESS *addr,
debug_print_npdu("Original-Unicast-NPDU", offset, npdu_len);
} else {
debug_print_string(
"Original-Broadcast-NPDU: Unable to decode!");
"Dropped Original-Broadcast-NPDU: Malformed!");
}
break;
case BVLC_ORIGINAL_BROADCAST_NPDU:
debug_print_bip("Received Original-Broadcast-NPDU", addr);
if (bbmd_address_match_self(addr)) {
/* ignore messages from my IPv4 address */
debug_print_string("Original-Broadcast-NPDU is me!");
debug_print_string("Dropped Original-Broadcast-NPDU from me!");
break;
}
function_len = bvlc_decode_original_broadcast(
@@ -1105,8 +1107,8 @@ int bvlc_bbmd_enabled_handler(BACNET_IP_ADDRESS *addr,
network layer. */
if (npdu_confirmed_service(npdu, npdu_len)) {
offset = 0;
debug_print_string("Original-Broadcast-NPDU: "
"Confirmed Service! Discard!");
debug_print_string("Dropped Original-Broadcast-NPDU: "
"Confirmed Service!");
} else {
(void)bbmd_fdt_forward_npdu(addr, npdu, npdu_len, true);
(void)bbmd_bdt_forward_npdu(addr, npdu, npdu_len, true);
@@ -1115,7 +1117,7 @@ int bvlc_bbmd_enabled_handler(BACNET_IP_ADDRESS *addr,
}
} else {
debug_print_string(
"Original-Broadcast-NPDU: Unable to decode!");
"Dropped Original-Broadcast-NPDU: Malformed!");
}
break;
case BVLC_SECURE_BVLL:
@@ -1168,12 +1170,14 @@ int bvlc_broadcast_handler(BACNET_IP_ADDRESS *addr,
uint16_t message_length = 0;
int header_len = 0;
debug_print_bip("Received Broadcast", addr);
header_len =
bvlc_decode_header(npdu, npdu_len, &message_type, &message_length);
if (header_len == 4) {
switch (message_type) {
case BVLC_ORIGINAL_UNICAST_NPDU:
/* drop unicast when sent as a broadcast */
debug_print_bip("Dropped BVLC (Original Unicast)", addr);
break;
default:
offset = bvlc_handler(addr, src, npdu, npdu_len);
+1 -1
View File
@@ -1154,7 +1154,7 @@ bool Binary_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data)
break;
}
/* not using len at this time */
len = len;
(void)len;
return status;
}
+2 -1
View File
@@ -414,4 +414,5 @@ bool tsm_invoke_id_failed(uint8_t invokeID)
return status;
}
#endif
#endif
-4
View File
@@ -44,10 +44,6 @@
/* optional configuration for BACnet/IP datalink layer */
#if (defined(BACDL_BIP) || defined(BACDL_ALL))
/* other BIP defines (define as 1 to enable):
USE_INADDR - uses INADDR_BROADCAST for broadcast and binds using INADDR_ANY
USE_CLASSADDR = uses IN_CLASSx_HOST where x=A,B,C or D for broadcast
*/
#if !defined(BBMD_ENABLED)
#define BBMD_ENABLED 1
#endif
+2 -1
View File
@@ -90,4 +90,5 @@ int create_object_ack_encode(
}
#endif /* __cplusplus */
#endif
#endif
+4
View File
@@ -122,6 +122,10 @@ extern "C" {
BACNET_STACK_EXPORT
int bip_get_broadcast_socket(void);
BACNET_STACK_EXPORT
int bip_set_broadcast_binding(
const char *ip4_broadcast);
#ifdef __cplusplus
}
#endif /* __cplusplus */
+5
View File
@@ -524,6 +524,7 @@ void dlenv_maintenance_timer(uint16_t elapsed_seconds)
* - BACNET_BDT_MASK_1 - dotted IPv4 mask of the BBMD table
* entry 1..128 (optional)
* - BACNET_IP_NAT_ADDR - dotted IPv4 address of the public facing router
* - BACNET_IP_BROADCAST_BIND_ADDR - dotted IPv4 address to bind broadcasts
* - BACDL_MSTP: (BACnet MS/TP)
* - BACNET_MAX_INFO_FRAMES
* - BACNET_MAX_MASTER
@@ -592,6 +593,10 @@ void dlenv_init(void)
bip_set_port(0xBAC0);
}
}
pEnv = getenv("BACNET_IP_BROADCAST_BIND_ADDR");
if (pEnv) {
bip_set_broadcast_binding(pEnv);
}
pEnv = getenv("BACNET_IP_NAT_ADDR");
if (pEnv) {
if (bip_get_addr_by_name(pEnv, &addr)) {
+2 -1
View File
@@ -53,4 +53,5 @@ int delete_object_decode_service_request(
}
#endif /* __cplusplus */
#endif
#endif
+2 -1
View File
@@ -80,4 +80,5 @@ int list_element_error_ack_decode(
}
#endif /* __cplusplus */
#endif
#endif