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

rx hooks: check address whitelisted #1732

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
74 changes: 48 additions & 26 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,32 @@ safety_config current_safety_config;
bool safety_rx_hook(const CANPacket_t *to_push) {
bool controls_allowed_prev = controls_allowed;

bool valid = rx_msg_safety_check(to_push, &current_safety_config, current_hooks);
bool valid = true;
int index = get_addr_check_index(to_push, current_safety_config.rx_checks,
current_safety_config.rx_checks_len);
if (index != -1) {
bool valid = rx_msg_safety_check(to_push, &current_safety_config, current_hooks);
if (valid) {
current_hooks->rx(to_push);
if (valid) {
current_hooks->rx(to_push);
}
}

// check relay malfunction and common rx logic
bool stock_ecu_detected = false;
print("relay_addrs_len: "); puth(current_safety_config.relay_addrs_len); print("\n");
for (int i = 0; i < current_safety_config.relay_addrs_len; i++) {
print("addr: "); puth(GET_ADDR(to_push)); print("\n");
if ((GET_ADDR(to_push) == current_safety_config.relay_addrs[i].addr) && (GET_BUS(to_push) == current_safety_config.relay_addrs[i].bus)) { // &&
// (GET_LEN(to_push) == current_safety_config.relay_addrs[i].len)) {
print("stock ecu!\n");
stock_ecu_detected = true;
break;
}
}
generic_rx_checks(stock_ecu_detected);

// TODO check if this should go in or out of index if
// reset mismatches on rising edge of controls_allowed to avoid rare race condition
if (controls_allowed && !controls_allowed_prev) {
heartbeat_engaged_mismatches = 0;
Expand Down Expand Up @@ -220,37 +241,34 @@ void update_addr_timestamp(RxCheck addr_list[], int index) {
}
}

bool rx_msg_safety_check(const CANPacket_t *to_push,
bool rx_msg_safety_check(const CANPacket_t *to_push, int index,
const safety_config *cfg,
const safety_hooks *safety_hooks) {

int index = get_addr_check_index(to_push, cfg->rx_checks, cfg->rx_checks_len);
update_addr_timestamp(cfg->rx_checks, index);

if (index != -1) {
// checksum check
if ((safety_hooks->get_checksum != NULL) && (safety_hooks->compute_checksum != NULL) && cfg->rx_checks[index].msg[cfg->rx_checks[index].status.index].check_checksum) {
uint32_t checksum = safety_hooks->get_checksum(to_push);
uint32_t checksum_comp = safety_hooks->compute_checksum(to_push);
cfg->rx_checks[index].status.valid_checksum = checksum_comp == checksum;
} else {
cfg->rx_checks[index].status.valid_checksum = true;
}
// checksum check
if ((safety_hooks->get_checksum != NULL) && (safety_hooks->compute_checksum != NULL) && cfg->rx_checks[index].msg[cfg->rx_checks[index].status.index].check_checksum) {
uint32_t checksum = safety_hooks->get_checksum(to_push);
uint32_t checksum_comp = safety_hooks->compute_checksum(to_push);
cfg->rx_checks[index].status.valid_checksum = checksum_comp == checksum;
} else {
cfg->rx_checks[index].status.valid_checksum = true;
}

// counter check (max_counter == 0 means skip check)
if ((safety_hooks->get_counter != NULL) && (cfg->rx_checks[index].msg[cfg->rx_checks[index].status.index].max_counter > 0U)) {
uint8_t counter = safety_hooks->get_counter(to_push);
update_counter(cfg->rx_checks, index, counter);
} else {
cfg->rx_checks[index].status.wrong_counters = 0U;
}
// counter check (max_counter == 0 means skip check)
if ((safety_hooks->get_counter != NULL) && (cfg->rx_checks[index].msg[cfg->rx_checks[index].status.index].max_counter > 0U)) {
uint8_t counter = safety_hooks->get_counter(to_push);
update_counter(cfg->rx_checks, index, counter);
} else {
cfg->rx_checks[index].status.wrong_counters = 0U;
}

// quality flag check
if ((safety_hooks->get_quality_flag_valid != NULL) && cfg->rx_checks[index].msg[cfg->rx_checks[index].status.index].quality_flag) {
cfg->rx_checks[index].status.valid_quality_flag = safety_hooks->get_quality_flag_valid(to_push);
} else {
cfg->rx_checks[index].status.valid_quality_flag = true;
}
// quality flag check
if ((safety_hooks->get_quality_flag_valid != NULL) && cfg->rx_checks[index].msg[cfg->rx_checks[index].status.index].quality_flag) {
cfg->rx_checks[index].status.valid_quality_flag = safety_hooks->get_quality_flag_valid(to_push);
} else {
cfg->rx_checks[index].status.valid_quality_flag = true;
}
return is_msg_valid(cfg->rx_checks, index);
}
Expand Down Expand Up @@ -362,6 +380,8 @@ int set_safety_hooks(uint16_t mode, uint16_t param) {
current_safety_config.rx_checks_len = 0;
current_safety_config.tx_msgs = NULL;
current_safety_config.tx_msgs_len = 0;
current_safety_config.relay_addrs = NULL;
current_safety_config.relay_addrs_len = 0;

int set_status = -1; // not set
int hook_config_count = sizeof(safety_hook_registry) / sizeof(safety_hook_config);
Expand All @@ -379,6 +399,8 @@ int set_safety_hooks(uint16_t mode, uint16_t param) {
current_safety_config.rx_checks_len = cfg.rx_checks_len;
current_safety_config.tx_msgs = cfg.tx_msgs;
current_safety_config.tx_msgs_len = cfg.tx_msgs_len;
current_safety_config.relay_addrs = cfg.relay_addrs;
current_safety_config.relay_addrs_len = cfg.relay_addrs_len;
// reset all dynamic fields in addr struct
for (int j = 0; j < current_safety_config.rx_checks_len; j++) {
current_safety_config.rx_checks[j].status = (RxStatus){0};
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void default_rx_hook(const CANPacket_t *to_push) {

static safety_config nooutput_init(uint16_t param) {
UNUSED(param);
return (safety_config){NULL, 0, NULL, 0};
return (safety_config){NULL, 0, NULL, 0, NULL, 0};
}

static bool nooutput_tx_hook(const CANPacket_t *to_send) {
Expand Down Expand Up @@ -36,7 +36,7 @@ bool alloutput_passthrough = false;
static safety_config alloutput_init(uint16_t param) {
controls_allowed = true;
alloutput_passthrough = GET_FLAG(param, ALLOUTPUT_PARAM_PASSTHROUGH);
return (safety_config){NULL, 0, NULL, 0};
return (safety_config){NULL, 0, NULL, 0, NULL, 0};
}

static bool alloutput_tx_hook(const CANPacket_t *to_send) {
Expand Down
12 changes: 6 additions & 6 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ RxCheck toyota_lta_interceptor_rx_checks[] = {
{.msg = {{0x201, 0, 6, .check_checksum = false, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
};

RelayAddr toyota_relay_addrs[] = {{0x2E4, 0, 5}};

// safety param flags
// first byte is for EPS factor, second is for flags
const uint32_t TOYOTA_PARAM_OFFSET = 8U;
Expand Down Expand Up @@ -208,8 +210,6 @@ static void toyota_rx_hook(const CANPacket_t *to_push) {
// TODO: remove this, only left in for gas_interceptor_prev test
gas_interceptor_prev = gas_interceptor;
}

generic_rx_checks((addr == 0x2E4));
}
}

Expand Down Expand Up @@ -344,11 +344,11 @@ static safety_config toyota_init(uint16_t param) {

safety_config ret;
if (toyota_lta) {
ret = enable_gas_interceptor ? BUILD_SAFETY_CFG(toyota_lta_interceptor_rx_checks, TOYOTA_INTERCEPTOR_TX_MSGS) : \
BUILD_SAFETY_CFG(toyota_lta_rx_checks, TOYOTA_TX_MSGS);
ret = enable_gas_interceptor ? BUILD_SAFETY_CFG(toyota_lta_interceptor_rx_checks, TOYOTA_INTERCEPTOR_TX_MSGS, toyota_relay_addrs) : \
BUILD_SAFETY_CFG(toyota_lta_rx_checks, TOYOTA_TX_MSGS, toyota_relay_addrs);
} else {
ret = enable_gas_interceptor ? BUILD_SAFETY_CFG(toyota_lka_interceptor_rx_checks, TOYOTA_INTERCEPTOR_TX_MSGS) : \
BUILD_SAFETY_CFG(toyota_lka_rx_checks, TOYOTA_TX_MSGS);
ret = enable_gas_interceptor ? BUILD_SAFETY_CFG(toyota_lka_interceptor_rx_checks, TOYOTA_INTERCEPTOR_TX_MSGS, toyota_relay_addrs) : \
BUILD_SAFETY_CFG(toyota_lka_rx_checks, TOYOTA_TX_MSGS, toyota_relay_addrs);
}
return ret;
}
Expand Down
17 changes: 14 additions & 3 deletions board/safety_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#define GET_BYTE(msg, b) ((msg)->data[(b)])
#define GET_FLAG(value, mask) (((__typeof__(mask))(value) & (mask)) == (mask))

#define BUILD_SAFETY_CFG(rx, tx) ((safety_config){(rx), (sizeof((rx)) / sizeof((rx)[0])), \
(tx), (sizeof((tx)) / sizeof((tx)[0]))})
#define BUILD_SAFETY_CFG(rx, tx, relay_addr) ((safety_config){(rx), (sizeof((rx)) / sizeof((rx)[0])), \
(tx), (sizeof((tx)) / sizeof((tx)[0])), \
(relay_addr), (sizeof((relay_addr)) / sizeof((relay_addr)[0]))})
#define SET_RX_CHECKS(rx, config) ((config).rx_checks = (rx), \
(config).rx_checks_len = sizeof((rx)) / sizeof((rx)[0]))
#define SET_TX_MSGS(tx, config) ((config).tx_msgs = (tx), \
Expand Down Expand Up @@ -137,11 +138,21 @@ typedef struct {
RxStatus status;
} RxCheck;

// TODO: check counter/checksum, etc like normal rx addr checks?
// just make this a flag with current rx checks?
typedef struct {
const int addr;
const int bus;
const int len;
} RelayAddr;

typedef struct {
RxCheck *rx_checks;
int rx_checks_len;
const CanMsg *tx_msgs;
int tx_msgs_len;
RelayAddr *relay_addrs;
int relay_addrs_len;
} safety_config;

typedef uint32_t (*get_checksum_t)(const CANPacket_t *to_push);
Expand Down Expand Up @@ -190,7 +201,7 @@ int get_addr_check_index(const CANPacket_t *to_push, RxCheck addr_list[], const
void update_counter(RxCheck addr_list[], int index, uint8_t counter);
void update_addr_timestamp(RxCheck addr_list[], int index);
bool is_msg_valid(RxCheck addr_list[], int index);
bool rx_msg_safety_check(const CANPacket_t *to_push,
bool rx_msg_safety_check(const CANPacket_t *to_push, int index,
const safety_config *cfg,
const safety_hooks *safety_hooks);
void generic_rx_checks(bool stock_ecu_detected);
Expand Down
Loading