#225 network changes for supervisor sessions

This commit is contained in:
Mikayla Fischler
2023-06-05 17:24:00 -04:00
parent 9a5fc92c86
commit 360609df1f
7 changed files with 233 additions and 165 deletions

View File

@@ -2,6 +2,8 @@ local comms = require("scada-common.comms")
local log = require("scada-common.log")
local util = require("scada-common.util")
local config = require("supervisor.config")
local svsessions = require("supervisor.session.svsessions")
local supervisor = {}
@@ -14,29 +16,29 @@ local SCADA_MGMT_TYPE = comms.SCADA_MGMT_TYPE
-- supervisory controller communications
---@nodiscard
---@param _version string supervisor version
---@param num_reactors integer number of reactors
---@param cooling_conf table cooling configuration table
---@param modem table modem device
---@param channels sv_channel_list network channels
---@param range integer trusted device connection range
---@param fp_ok boolean if the front panel UI is running
---@diagnostic disable-next-line: unused-local
function supervisor.comms(_version, num_reactors, cooling_conf, modem, channels, range, fp_ok)
function supervisor.comms(_version, modem, fp_ok)
-- print a log message to the terminal as long as the UI isn't running
local function println(message) if not fp_ok then util.println_ts(message) end end
-- channel list
local svr_channel = channels.SVR
local plc_channel = channels.PLC
local rtu_channel = channels.RTU
local crd_channel = channels.CRD
local pkt_channel = channels.PKT
-- channel list from config
local svr_channel = config.SVR_CHANNEL
local plc_channel = config.PLC_CHANNEL
local rtu_channel = config.RTU_CHANNEL
local crd_channel = config.CRD_CHANNEL
local pkt_channel = config.PKT_CHANNEL
-- configuration data
local num_reactors = config.NUM_REACTORS
local cooling_conf = config.REACTOR_COOLING
local self = {
last_est_acks = {}
}
comms.set_trusted_range(range)
comms.set_trusted_range(config.TRUSTED_RANGE)
-- PRIVATE FUNCTIONS --
@@ -138,10 +140,7 @@ function supervisor.comms(_version, num_reactors, cooling_conf, modem, channels,
if l_chan ~= svr_channel then
log.debug("received packet on unconfigured channel " .. l_chan, true)
return
end
if r_chan == plc_channel then
elseif r_chan == plc_channel then
-- look for an associated session
local session = svsessions.find_plc_session(s_addr)
@@ -182,7 +181,7 @@ function supervisor.comms(_version, num_reactors, cooling_conf, modem, channels,
-- PLC linking request
if packet.length == 4 and type(packet.data[4]) == "number" then
local reactor_id = packet.data[4]
local plc_id = svsessions.establish_plc_session(l_chan, r_chan, reactor_id, firmware_v)
local plc_id = svsessions.establish_plc_session(s_addr, reactor_id, firmware_v)
if plc_id == false then
-- reactor already has a PLC assigned
@@ -193,8 +192,8 @@ function supervisor.comms(_version, num_reactors, cooling_conf, modem, channels,
_send_establish(packet.scada_frame, ESTABLISH_ACK.COLLISION)
else
-- got an ID; assigned to a reactor successfully
println(util.c("PLC (", firmware_v, ") [:", r_chan, "] \xbb reactor ", reactor_id, " connected"))
log.info(util.c("PLC_ESTABLISH: PLC (", firmware_v, ") [:", r_chan, "] reactor unit ", reactor_id, " PLC connected with session ID ", plc_id))
println(util.c("PLC (", firmware_v, ") [@", s_addr, "] \xbb reactor ", reactor_id, " connected"))
log.info(util.c("PLC_ESTABLISH: PLC (", firmware_v, ") [@", s_addr, "] reactor unit ", reactor_id, " PLC connected with session ID ", plc_id))
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW)
end
else
@@ -202,17 +201,19 @@ function supervisor.comms(_version, num_reactors, cooling_conf, modem, channels,
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
log.debug(util.c("illegal establish packet for device ", dev_type, " on PLC listening channel"))
log.debug(util.c("illegal establish packet for device ", dev_type, " on PLC channel"))
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
log.debug("invalid establish packet (on PLC listening channel)")
log.debug("invalid establish packet (on PLC channel)")
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
-- any other packet should be session related, discard it
log.debug(util.c(r_chan, " -> ", l_chan, ": discarding SCADA_MGMT packet without a known session"))
log.debug(util.c("discarding PLC SCADA_MGMT packet without a known session from computer ", s_addr))
end
else
log.debug(util.c("illegal packet type ", protocol, " on PLC channel"))
end
elseif r_chan == rtu_channel then
-- look for an associated session
@@ -254,33 +255,33 @@ function supervisor.comms(_version, num_reactors, cooling_conf, modem, channels,
if packet.length == 4 then
-- this is an RTU advertisement for a new session
local rtu_advert = packet.data[4]
local s_id = svsessions.establish_rtu_session(l_chan, r_chan, rtu_advert, firmware_v)
local s_id = svsessions.establish_rtu_session(s_addr, rtu_advert, firmware_v)
println(util.c("RTU (", firmware_v, ") [:", r_chan, "] \xbb connected"))
log.info(util.c("RTU_ESTABLISH: RTU (",firmware_v, ") [:", r_chan, "] connected with session ID ", s_id))
println(util.c("RTU (", firmware_v, ") [@", s_addr, "] \xbb connected"))
log.info(util.c("RTU_ESTABLISH: RTU (",firmware_v, ") [@", s_addr, "] connected with session ID ", s_id))
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW)
else
log.debug("RTU_ESTABLISH: packet length mismatch")
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
log.debug(util.c("illegal establish packet for device ", dev_type, " on RTU listening channel"))
log.debug(util.c("illegal establish packet for device ", dev_type, " on RTU channel"))
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
log.debug("invalid establish packet (on RTU listening channel)")
log.debug("invalid establish packet (on RTU channel)")
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
-- any other packet should be session related, discard it
log.debug(util.c(r_chan, " -> ", l_chan, ": discarding SCADA_MGMT packet without a known session"))
log.debug(util.c("discarding RTU SCADA_MGMT packet without a known session from computer ", s_addr))
end
else
log.debug("illegal packet type " .. protocol .. " on RTU listening channel")
log.debug(util.c("illegal packet type ", protocol, " on RTU channel"))
end
elseif r_chan == crd_channel then
-- look for an associated session
local session = svsessions.find_svctl_session(s_addr)
local session = svsessions.find_crd_session(s_addr)
if protocol == PROTOCOL.SCADA_MGMT then
---@cast packet mgmt_frame
@@ -306,45 +307,37 @@ function supervisor.comms(_version, num_reactors, cooling_conf, modem, channels,
_send_establish(packet.scada_frame, ESTABLISH_ACK.BAD_VERSION)
elseif dev_type == DEVICE_TYPE.CRDN then
-- this is an attempt to establish a new coordinator session
local s_id = svsessions.establish_coord_session(l_chan, r_chan, firmware_v)
local s_id = svsessions.establish_crd_session(s_addr, firmware_v)
if s_id ~= false then
local config = { num_reactors }
local cfg = { num_reactors }
for i = 1, #cooling_conf do
table.insert(config, cooling_conf[i].BOILERS)
table.insert(config, cooling_conf[i].TURBINES)
table.insert(cfg, cooling_conf[i].BOILERS)
table.insert(cfg, cooling_conf[i].TURBINES)
end
println(util.c("CRD (", firmware_v, ") [@", s_addr, "] \xbb connected"))
log.info(util.c("ESTABLISH: coordinator (", firmware_v, ") [@", s_addr, "] connected with session ID ", s_id))
log.info(util.c("CRD_ESTABLISH: coordinator (", firmware_v, ") [@", s_addr, "] connected with session ID ", s_id))
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW, config)
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW, cfg)
else
if last_ack ~= ESTABLISH_ACK.COLLISION then
log.info("ESTABLISH: denied new coordinator [@" .. s_addr .. "] due to already being connected to another coordinator")
log.info("CRD_ESTABLISH: denied new coordinator [@" .. s_addr .. "] due to already being connected to another coordinator")
end
_send_establish(packet.scada_frame, ESTABLISH_ACK.COLLISION)
end
elseif dev_type == DEVICE_TYPE.PKT then
-- this is an attempt to establish a new pocket diagnostic session
local s_id = svsessions.establish_pdg_session(l_chan, r_chan, firmware_v)
println(util.c("PKT (", firmware_v, ") [:", r_chan, "] \xbb connected"))
log.info(util.c("SVCTL_ESTABLISH: pocket (", firmware_v, ") [:", r_chan, "] connected with session ID ", s_id))
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW)
else
log.debug(util.c("illegal establish packet for device ", dev_type, " on SVCTL listening channel"))
log.debug(util.c("illegal establish packet for device ", dev_type, " on coordinator channel"))
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
log.debug("SVCTL_ESTABLISH: establish packet length mismatch")
log.debug("CRD_ESTABLISH: establish packet length mismatch")
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
-- any other packet should be session related, discard it
log.debug(r_chan .. " -> " .. l_chan .. ": discarding SCADA_MGMT packet without a known session")
log.debug(util.c("discarding coordinator SCADA_MGMT packet without a known session from computer ", s_addr))
end
elseif protocol == PROTOCOL.SCADA_CRDN then
---@cast packet crdn_frame
@@ -354,13 +347,70 @@ function supervisor.comms(_version, num_reactors, cooling_conf, modem, channels,
session.in_queue.push_packet(packet)
else
-- any other packet should be session related, discard it
log.debug(r_chan .. "->" .. l_chan .. ": discarding SCADA_CRDN packet without a known session")
log.debug(util.c("discarding coordinator SCADA_CRDN packet without a known session from computer ", s_addr))
end
else
log.debug("illegal packet type " .. protocol .. " on coordinator listening channel")
log.debug(util.c("illegal packet type ", protocol, " on coordinator channel"))
end
elseif r_chan == pkt_channel then
-- look for an associated session
local session = svsessions.find_pdg_session(s_addr)
if protocol == PROTOCOL.SCADA_MGMT then
---@cast packet mgmt_frame
-- SCADA management packet
if session ~= nil then
-- pass the packet onto the session handler
session.in_queue.push_packet(packet)
elseif packet.type == SCADA_MGMT_TYPE.ESTABLISH then
-- establish a new session
local last_ack = self.last_est_acks[s_addr]
-- validate packet and continue
if packet.length >= 3 and type(packet.data[1]) == "string" and type(packet.data[2]) == "string" then
local comms_v = packet.data[1]
local firmware_v = packet.data[2]
local dev_type = packet.data[3]
if comms_v ~= comms.version then
if last_ack ~= ESTABLISH_ACK.BAD_VERSION then
log.info(util.c("dropping PDG establish packet with incorrect comms version v", comms_v, " (expected v", comms.version, ")"))
end
_send_establish(packet.scada_frame, ESTABLISH_ACK.BAD_VERSION)
elseif dev_type == DEVICE_TYPE.PKT then
-- this is an attempt to establish a new pocket diagnostic session
local s_id = svsessions.establish_pdg_session(s_addr, firmware_v)
println(util.c("PKT (", firmware_v, ") [:", r_chan, "] \xbb connected"))
log.info(util.c("PDG_ESTABLISH: pocket (", firmware_v, ") [:", r_chan, "] connected with session ID ", s_id))
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW)
else
log.debug(util.c("illegal establish packet for device ", dev_type, " on pocket channel"))
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
log.debug("PDG_ESTABLISH: establish packet length mismatch")
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
end
else
-- any other packet should be session related, discard it
log.debug(util.c("discarding pocket SCADA_MGMT packet without a known session from computer ", s_addr))
end
elseif protocol == PROTOCOL.SCADA_CRDN then
---@cast packet crdn_frame
-- coordinator packet
if session ~= nil then
-- pass the packet onto the session handler
session.in_queue.push_packet(packet)
else
-- any other packet should be session related, discard it
log.debug(util.c("discarding pocket SCADA_CRDN packet without a known session from computer ", s_addr))
end
else
log.debug(util.c("illegal packet type ", protocol, " on pocket channel"))
end
else
log.debug("received packet for unknown channel " .. r_chan, true)
end