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

safety: only run rx hooks on whitelisted msgs #1903

Merged
merged 57 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
2c070df
don't run rx hook on non-allowed messages
sshane Mar 3, 2025
c0bff74
better name
sshane Mar 3, 2025
eb3424c
fix toyota (bug w/ secoc)
sshane Mar 3, 2025
b6a9089
looks like honda is broken
sshane Mar 3, 2025
3382d4a
rivian is also bad! (missing EPAS_SystemStatus)
sshane Mar 3, 2025
b28ddda
misra so far
sshane Mar 3, 2025
fcbddec
nissan is fine
sshane Mar 3, 2025
f289e52
tesla is also borked
sshane Mar 3, 2025
d82214c
mazda's good
sshane Mar 3, 2025
425612b
subaru's fine
sshane Mar 3, 2025
0a9b5aa
gm broke
sshane Mar 3, 2025
ad18efc
ford's good
sshane Mar 3, 2025
ddc8290
chrysler's good
sshane Mar 3, 2025
2facb73
vw is good
sshane Mar 3, 2025
961d3cc
hyundai is broky, canfd is good
sshane Mar 3, 2025
1648ad9
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 7, 2025
7992dd0
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 7, 2025
c27ff5f
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 8, 2025
1f2ed53
Fix Rivian
sshane Mar 8, 2025
6016eff
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 11, 2025
0c6a89b
revert these
sshane Mar 11, 2025
aa21d5d
do relay malfunction check on all addresses
sshane Mar 11, 2025
a03788b
Found a Tesla bug
sshane Mar 11, 2025
51fed63
fix subaru pg
sshane Mar 11, 2025
accaeab
body
sshane Mar 11, 2025
bbf5457
rm
sshane Mar 11, 2025
cd711a3
Fix Honda
sshane Mar 11, 2025
942af4d
stash
sshane Mar 11, 2025
214064a
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 11, 2025
62d5f55
fix Hyundai
sshane Mar 11, 2025
ac94ff5
fix
sshane Mar 11, 2025
d09e708
Hyundai: buttons are used always (for interaction)
sshane Mar 11, 2025
d182243
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 11, 2025
d41d8c3
revert tesla
sshane Mar 11, 2025
2f973f6
body: we don't rx _torque_cmd_msg
sshane Mar 11, 2025
f83e217
Revert "body: we don't rx _torque_cmd_msg"
sshane Mar 11, 2025
8d7cdca
simpler
sshane Mar 11, 2025
c1ad98e
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 11, 2025
eaa762a
GM EV param for correct rxchecks
sshane Mar 11, 2025
1d4a816
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 12, 2025
a6d8a42
no need
sshane Mar 12, 2025
328d18a
might read better
sshane Mar 12, 2025
cc5a17a
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 12, 2025
ac472da
rm extras
sshane Mar 12, 2025
1f12d82
fix hyundai
sshane Mar 12, 2025
700703f
we weren't testing lfa (non-hda2), alt buttons, long
sshane Mar 12, 2025
5037164
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 12, 2025
32f71be
fix
sshane Mar 12, 2025
d8d3685
tested
sshane Mar 12, 2025
01440ce
rm
sshane Mar 12, 2025
f8e8b44
not needed
sshane Mar 12, 2025
1b431ef
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 12, 2025
dc71049
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 12, 2025
7864bfc
clean up
sshane Mar 12, 2025
dbdea15
that too
sshane Mar 12, 2025
957a07b
Merge remote-tracking branch 'upstream/master' into rx-hook-whitelist
sshane Mar 12, 2025
9442d46
.
sshane Mar 12, 2025
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
22 changes: 14 additions & 8 deletions opendbc/safety/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,16 @@ 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);
if (valid) {
bool whitelisted = get_addr_check_index(to_push, current_safety_config.rx_checks, current_safety_config.rx_checks_len) != -1;
if (valid && whitelisted) {
current_hooks->rx(to_push);
}

// the relay malfunction hook runs on all incoming rx messages
if (current_hooks->rx_relay_malfunction != NULL) {
current_hooks->rx_relay_malfunction(to_push);
}

// reset mismatches on rising edge of controls_allowed to avoid rare race condition
if (controls_allowed && !controls_allowed_prev) {
heartbeat_engaged_mismatches = 0;
Expand All @@ -228,28 +234,28 @@ static bool tx_msg_safety_check(const CANPacket_t *to_send, const CanMsg msg_lis
int bus = GET_BUS(to_send);
int length = GET_LEN(to_send);

bool allowed = false;
bool whitelisted = false;
for (int i = 0; i < len; i++) {
if ((addr == msg_list[i].addr) && (bus == msg_list[i].bus) && (length == msg_list[i].len)) {
allowed = true;
whitelisted = true;
break;
}
}
return allowed;
return whitelisted;
}

bool safety_tx_hook(CANPacket_t *to_send) {
bool allowed = tx_msg_safety_check(to_send, current_safety_config.tx_msgs, current_safety_config.tx_msgs_len);
bool whitelisted = tx_msg_safety_check(to_send, current_safety_config.tx_msgs, current_safety_config.tx_msgs_len);
if ((current_safety_mode == SAFETY_ALLOUTPUT) || (current_safety_mode == SAFETY_ELM327)) {
allowed = true;
whitelisted = true;
}

bool safety_allowed = false;
if (allowed) {
if (whitelisted) {
safety_allowed = current_hooks->tx(to_send);
}

return !relay_malfunction && allowed && safety_allowed;
return !relay_malfunction && safety_allowed;
}

int safety_fwd_hook(int bus_num, int addr) {
Expand Down
6 changes: 6 additions & 0 deletions opendbc/safety/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ static void chrysler_rx_hook(const CANPacket_t *to_push) {
if ((bus == 0) && (addr == chrysler_addrs->ESP_1)) {
brake_pressed = ((GET_BYTE(to_push, 0U) & 0xFU) >> 2U) == 1U;
}
}

static void chrysler_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
const int bus = GET_BUS(to_push);
const int addr = GET_ADDR(to_push);

generic_rx_checks((bus == 0) && (addr == chrysler_addrs->LKAS_COMMAND));
}
Expand Down Expand Up @@ -296,6 +301,7 @@ static safety_config chrysler_init(uint16_t param) {
const safety_hooks chrysler_hooks = {
.init = chrysler_init,
.rx = chrysler_rx_hook,
.rx_relay_malfunction = chrysler_rx_relay_malfunction_hook,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.rx_relay_malfunction = chrysler_rx_relay_malfunction_hook,
.stock_ecu_hook = chrysler_stock_ecu_hook,

?

.tx = chrysler_tx_hook,
.fwd = chrysler_fwd_hook,
.get_counter = chrysler_get_counter,
Expand Down
8 changes: 7 additions & 1 deletion opendbc/safety/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ static void ford_rx_hook(const CANPacket_t *to_push) {
bool cruise_engaged = (cruise_state == 4U) || (cruise_state == 5U);
pcm_cruise_check(cruise_engaged);
}
}
}

static void ford_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
if (GET_BUS(to_push) == FORD_MAIN_BUS) {
int addr = GET_ADDR(to_push);

// If steering controls messages are received on the destination bus, it's an indication
// that the relay might be malfunctioning.
Expand All @@ -194,7 +200,6 @@ static void ford_rx_hook(const CANPacket_t *to_push) {
}
generic_rx_checks(stock_ecu_detected);
}

}

static bool ford_tx_hook(const CANPacket_t *to_send) {
Expand Down Expand Up @@ -425,6 +430,7 @@ static safety_config ford_init(uint16_t param) {
const safety_hooks ford_hooks = {
.init = ford_init,
.rx = ford_rx_hook,
.rx_relay_malfunction = ford_rx_relay_malfunction_hook,
.tx = ford_tx_hook,
.fwd = ford_fwd_hook,
.get_counter = ford_get_counter,
Expand Down
7 changes: 7 additions & 0 deletions opendbc/safety/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ static void gm_rx_hook(const CANPacket_t *to_push) {
if (addr == 0xBD) {
regen_braking = (GET_BYTE(to_push, 0) >> 4) != 0U;
}
}
}

static void gm_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
if (GET_BUS(to_push) == 0U) {
int addr = GET_ADDR(to_push);

bool stock_ecu_detected = (addr == 0x180); // ASCMLKASteeringCmd

Expand Down Expand Up @@ -254,6 +260,7 @@ static safety_config gm_init(uint16_t param) {
const safety_hooks gm_hooks = {
.init = gm_init,
.rx = gm_rx_hook,
.rx_relay_malfunction = gm_rx_relay_malfunction_hook,
.tx = gm_tx_hook,
.fwd = gm_fwd_hook,
};
10 changes: 9 additions & 1 deletion opendbc/safety/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,15 @@ static void honda_rx_hook(const CANPacket_t *to_push) {
}
}
}
}

static void honda_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
int bus_rdr_car = (honda_hw == HONDA_BOSCH) ? 0 : 2; // radar bus, car side
int pt_bus = honda_get_pt_bus();

int addr = GET_ADDR(to_push);
int bus = GET_BUS(to_push);

bool stock_ecu_detected = false;

// If steering controls messages are received on the destination bus, it's an indication
Expand All @@ -187,7 +194,6 @@ static void honda_rx_hook(const CANPacket_t *to_push) {
}

generic_rx_checks(stock_ecu_detected);

}

static bool honda_tx_hook(const CANPacket_t *to_send) {
Expand Down Expand Up @@ -443,6 +449,7 @@ static int honda_bosch_fwd_hook(int bus_num, int addr) {
const safety_hooks honda_nidec_hooks = {
.init = honda_nidec_init,
.rx = honda_rx_hook,
.rx_relay_malfunction = honda_rx_relay_malfunction_hook,
.tx = honda_tx_hook,
.fwd = honda_nidec_fwd_hook,
.get_counter = honda_get_counter,
Expand All @@ -453,6 +460,7 @@ const safety_hooks honda_nidec_hooks = {
const safety_hooks honda_bosch_hooks = {
.init = honda_bosch_init,
.rx = honda_rx_hook,
.rx_relay_malfunction = honda_rx_relay_malfunction_hook,
.tx = honda_tx_hook,
.fwd = honda_bosch_fwd_hook,
.get_counter = honda_get_counter,
Expand Down
9 changes: 9 additions & 0 deletions opendbc/safety/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,14 @@ static void hyundai_rx_hook(const CANPacket_t *to_push) {
if (addr == 0x394) {
brake_pressed = ((GET_BYTE(to_push, 5) >> 5U) & 0x3U) == 0x2U;
}
}
}

static void hyundai_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);

if (bus == 0) {
bool stock_ecu_detected = (addr == 0x340);

// If openpilot is controlling longitudinal we need to ensure the radar is turned off
Expand Down Expand Up @@ -351,6 +358,7 @@ static safety_config hyundai_legacy_init(uint16_t param) {
const safety_hooks hyundai_hooks = {
.init = hyundai_init,
.rx = hyundai_rx_hook,
.rx_relay_malfunction = hyundai_rx_relay_malfunction_hook,
.tx = hyundai_tx_hook,
.fwd = hyundai_fwd_hook,
.get_counter = hyundai_get_counter,
Expand All @@ -361,6 +369,7 @@ const safety_hooks hyundai_hooks = {
const safety_hooks hyundai_legacy_hooks = {
.init = hyundai_legacy_init,
.rx = hyundai_rx_hook,
.rx_relay_malfunction = hyundai_rx_relay_malfunction_hook,
.tx = hyundai_tx_hook,
.fwd = hyundai_fwd_hook,
.get_counter = hyundai_get_counter,
Expand Down
8 changes: 8 additions & 0 deletions opendbc/safety/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,15 @@ static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) {
hyundai_common_cruise_state_check(cruise_engaged);
}
}
}

static void hyundai_canfd_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);

const int pt_bus = hyundai_canfd_lka_steering ? 1 : 0;
const int steer_addr = hyundai_canfd_lka_steering ? hyundai_canfd_get_lka_addr() : 0x12a;

bool stock_ecu_detected = (addr == steer_addr) && (bus == 0);
if (hyundai_longitudinal) {
// on LKA steering cars, ensure ADRV ECU is still knocked out
Expand Down Expand Up @@ -357,6 +364,7 @@ static safety_config hyundai_canfd_init(uint16_t param) {
const safety_hooks hyundai_canfd_hooks = {
.init = hyundai_canfd_init,
.rx = hyundai_canfd_rx_hook,
.rx_relay_malfunction = hyundai_canfd_rx_relay_malfunction_hook,
.tx = hyundai_canfd_tx_hook,
.fwd = hyundai_canfd_fwd_hook,
.get_counter = hyundai_canfd_get_counter,
Expand Down
6 changes: 6 additions & 0 deletions opendbc/safety/safety/safety_mazda.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ static void mazda_rx_hook(const CANPacket_t *to_push) {
if (addr == MAZDA_PEDALS) {
brake_pressed = (GET_BYTE(to_push, 0) & 0x10U);
}
}
}

static void mazda_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
if ((int)GET_BUS(to_push) == MAZDA_MAIN) {
int addr = GET_ADDR(to_push);
generic_rx_checks((addr == MAZDA_LKAS));
}
}
Expand Down Expand Up @@ -126,6 +131,7 @@ static safety_config mazda_init(uint16_t param) {
const safety_hooks mazda_hooks = {
.init = mazda_init,
.rx = mazda_rx_hook,
.rx_relay_malfunction = mazda_rx_relay_malfunction_hook,
.tx = mazda_tx_hook,
.fwd = mazda_fwd_hook,
};
7 changes: 7 additions & 0 deletions opendbc/safety/safety/safety_nissan.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ static void nissan_rx_hook(const CANPacket_t *to_push) {
generic_rx_checks((addr == 0x169) && (bus == 0));
}

static void nissan_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);

generic_rx_checks((addr == 0x169) && (bus == 0));
}

static bool nissan_tx_hook(const CANPacket_t *to_send) {
const AngleSteeringLimits NISSAN_STEERING_LIMITS = {
Expand Down Expand Up @@ -159,6 +165,7 @@ static safety_config nissan_init(uint16_t param) {
const safety_hooks nissan_hooks = {
.init = nissan_init,
.rx = nissan_rx_hook,
.rx_relay_malfunction = nissan_rx_relay_malfunction_hook,
.tx = nissan_tx_hook,
.fwd = nissan_fwd_hook,
};
19 changes: 14 additions & 5 deletions opendbc/safety/safety/safety_rivian.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ static void rivian_rx_hook(const CANPacket_t *to_push) {
if (addr == 0x38f) {
brake_pressed = GET_BIT(to_push, 23U);
}

generic_rx_checks(addr == 0x120); // ACM_lkaHbaCmd
if (rivian_longitudinal) {
generic_rx_checks(addr == 0x160); // ACM_longitudinalRequest
}
}

if (bus == 2) {
Expand All @@ -45,6 +40,19 @@ static void rivian_rx_hook(const CANPacket_t *to_push) {
}
}

static void rivian_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
int bus = GET_BUS(to_push);

if (bus == 0) {
int addr = GET_ADDR(to_push);

generic_rx_checks(addr == 0x120); // ACM_lkaHbaCmd
if (rivian_longitudinal) {
generic_rx_checks(addr == 0x160); // ACM_longitudinalRequest
}
}
}

static bool rivian_tx_hook(const CANPacket_t *to_send) {
const TorqueSteeringLimits RIVIAN_STEERING_LIMITS = {
.max_steer = 250,
Expand Down Expand Up @@ -156,6 +164,7 @@ static safety_config rivian_init(uint16_t param) {
const safety_hooks rivian_hooks = {
.init = rivian_init,
.rx = rivian_rx_hook,
.rx_relay_malfunction = rivian_rx_relay_malfunction_hook,
.tx = rivian_tx_hook,
.fwd = rivian_fwd_hook,
};
6 changes: 6 additions & 0 deletions opendbc/safety/safety/safety_subaru.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ static void subaru_rx_hook(const CANPacket_t *to_push) {
if ((addr == MSG_SUBARU_Throttle) && (bus == SUBARU_MAIN_BUS)) {
gas_pressed = GET_BYTE(to_push, 4) != 0U;
}
}

static void subaru_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
const int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);

generic_rx_checks((addr == MSG_SUBARU_ES_LKAS) && (bus == SUBARU_MAIN_BUS));
}
Expand Down Expand Up @@ -285,6 +290,7 @@ static safety_config subaru_init(uint16_t param) {
const safety_hooks subaru_hooks = {
.init = subaru_init,
.rx = subaru_rx_hook,
.rx_relay_malfunction = subaru_rx_relay_malfunction_hook,
.tx = subaru_tx_hook,
.fwd = subaru_fwd_hook,
.get_counter = subaru_get_counter,
Expand Down
7 changes: 6 additions & 1 deletion opendbc/safety/safety/safety_tesla.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ static void tesla_rx_hook(const CANPacket_t *to_push) {
tesla_stock_aeb = (GET_BYTE(to_push, 2) & 0x03U) == 1U;
}
}
}

static void tesla_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);

generic_rx_checks((addr == 0x488) && (bus == 0)); // DAS_steeringControl
generic_rx_checks((addr == 0x27d) && (bus == 0)); // APS_eacMonitor
Expand All @@ -63,7 +68,6 @@ static void tesla_rx_hook(const CANPacket_t *to_push) {
}
}


static bool tesla_tx_hook(const CANPacket_t *to_send) {
const AngleSteeringLimits TESLA_STEERING_LIMITS = {
.max_angle = 3600, // 360 deg, EPAS faults above this
Expand Down Expand Up @@ -209,6 +213,7 @@ static safety_config tesla_init(uint16_t param) {
const safety_hooks tesla_hooks = {
.init = tesla_init,
.rx = tesla_rx_hook,
.rx_relay_malfunction = tesla_rx_relay_malfunction_hook,
.tx = tesla_tx_hook,
.fwd = tesla_fwd_hook,
};
7 changes: 7 additions & 0 deletions opendbc/safety/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ static void toyota_rx_hook(const CANPacket_t *to_push) {

UPDATE_VEHICLE_SPEED(speed / 4.0 * 0.01 / 3.6);
}
}
}

static void toyota_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
if (GET_BUS(to_push) == 0U) {
int addr = GET_ADDR(to_push);

bool stock_ecu_detected = addr == 0x2E4; // STEERING_LKA
if (!toyota_stock_longitudinal && (addr == 0x343)) {
Expand Down Expand Up @@ -406,6 +412,7 @@ static int toyota_fwd_hook(int bus_num, int addr) {
const safety_hooks toyota_hooks = {
.init = toyota_init,
.rx = toyota_rx_hook,
.rx_relay_malfunction = toyota_rx_relay_malfunction_hook,
.tx = toyota_tx_hook,
.fwd = toyota_fwd_hook,
.get_checksum = toyota_get_checksum,
Expand Down
6 changes: 6 additions & 0 deletions opendbc/safety/safety/safety_volkswagen_mqb.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ static void volkswagen_mqb_rx_hook(const CANPacket_t *to_push) {
}

brake_pressed = volkswagen_mqb_brake_pedal_switch || volkswagen_mqb_brake_pressure_detected;
}
}

static void volkswagen_mqb_rx_relay_malfunction_hook(const CANPacket_t *to_push) {
if (GET_BUS(to_push) == 0U) {
int addr = GET_ADDR(to_push);
generic_rx_checks((addr == MSG_HCA_01));
}
}
Expand Down Expand Up @@ -240,6 +245,7 @@ static int volkswagen_mqb_fwd_hook(int bus_num, int addr) {
const safety_hooks volkswagen_mqb_hooks = {
.init = volkswagen_mqb_init,
.rx = volkswagen_mqb_rx_hook,
.rx_relay_malfunction = volkswagen_mqb_rx_relay_malfunction_hook,
.tx = volkswagen_mqb_tx_hook,
.fwd = volkswagen_mqb_fwd_hook,
.get_counter = volkswagen_mqb_meb_get_counter,
Expand Down
Loading
Loading