Skip to content

Commit 1968165

Browse files
jukkarnashif
authored andcommitted
net: gptp: Do not update clock if time diff is < 0
The time difference calculation did not check if the result value would be < 0 which means really large value when converted to unsigned. Fixes #20100 Signed-off-by: Jukka Rissanen <[email protected]>
1 parent fc7916a commit 1968165

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

subsys/net/l2/ethernet/gptp/gptp_mi.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,13 @@ static void gptp_update_local_port_clock(void)
739739
key = irq_lock();
740740
ptp_clock_get(clk, &tm);
741741

742+
if (second_diff < 0 && tm.second < -second_diff) {
743+
NET_DBG("Do not set local clock because %lu < %ld",
744+
(unsigned long int)tm.second,
745+
(long int)-second_diff);
746+
goto skip_clock_set;
747+
}
748+
742749
tm.second += second_diff;
743750

744751
if (nanosecond_diff < 0 &&
@@ -756,7 +763,18 @@ static void gptp_update_local_port_clock(void)
756763
tm.nanosecond -= NSEC_PER_SEC;
757764
}
758765

766+
/* This prints too much data normally but can be enabled to see
767+
* what time we are setting to the local clock.
768+
*/
769+
if (0) {
770+
NET_INFO("Set local clock %lu.%lu",
771+
(unsigned long int)tm.second,
772+
(unsigned long int)tm.nanosecond);
773+
}
774+
759775
ptp_clock_set(clk, &tm);
776+
777+
skip_clock_set:
760778
irq_unlock(key);
761779
} else {
762780
if (nanosecond_diff < -200) {

0 commit comments

Comments
 (0)