From 6378ac4e3828c41dbd8efb4fdd3fd7e41b506c7e Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Tue, 26 Nov 2019 11:04:32 +0200 Subject: [PATCH] net: Verify correctness of iface_api inside L2 driver API Add build time check that guarantees that iface_api struct is the first entry inside L2 driver data. This makes sure we do not miss a case when the ordering of the fields in the struct is changed. Signed-off-by: Jukka Rissanen --- include/net/can.h | 5 +++++ include/net/dummy.h | 5 +++++ include/net/ethernet.h | 5 +++++ include/net/ieee802154_radio.h | 5 +++++ include/net/ppp.h | 5 +++++ include/net/socket_can.h | 5 +++++ include/net/wifi_mgmt.h | 5 +++++ 7 files changed, 35 insertions(+) diff --git a/include/net/can.h b/include/net/can.h index 886afa5480b..95968d2a31f 100644 --- a/include/net/can.h +++ b/include/net/can.h @@ -110,6 +110,11 @@ struct net_can_api { int (*enable)(struct device *dev, bool enable); }; +/* Make sure that the network interface API is properly setup inside + * net_can_api struct (it is the first one). + */ +BUILD_ASSERT(offsetof(struct net_can_api, iface_api) == 0); + /** @cond INTERNAL_HIDDEN */ #define CANBUS_L2_CTX_TYPE struct net_canbus_context * diff --git a/include/net/dummy.h b/include/net/dummy.h index d172da706c5..4f3344fcfed 100644 --- a/include/net/dummy.h +++ b/include/net/dummy.h @@ -33,6 +33,11 @@ struct dummy_api { int (*send)(struct device *dev, struct net_pkt *pkt); }; +/* Make sure that the network interface API is properly setup inside + * dummy API struct (it is the first one). + */ +BUILD_ASSERT(offsetof(struct dummy_api, iface_api) == 0); + #ifdef __cplusplus } #endif diff --git a/include/net/ethernet.h b/include/net/ethernet.h index 713265c77ab..505cedb8a04 100644 --- a/include/net/ethernet.h +++ b/include/net/ethernet.h @@ -265,6 +265,11 @@ struct ethernet_api { int (*send)(struct device *dev, struct net_pkt *pkt); }; +/* Make sure that the network interface API is properly setup inside + * Ethernet API struct (it is the first one). + */ +BUILD_ASSERT(offsetof(struct ethernet_api, iface_api) == 0); + /** @cond INTERNAL_HIDDEN */ struct net_eth_hdr { struct net_eth_addr dst; diff --git a/include/net/ieee802154_radio.h b/include/net/ieee802154_radio.h index f44061fe29e..6726eea6060 100644 --- a/include/net/ieee802154_radio.h +++ b/include/net/ieee802154_radio.h @@ -154,6 +154,11 @@ struct ieee802154_radio_api { #endif /* CONFIG_NET_L2_OPENTHREAD */ }; +/* Make sure that the network interface API is properly setup inside + * IEEE 802154 radio API struct (it is the first one). + */ +BUILD_ASSERT(offsetof(struct ieee802154_radio_api, iface_api) == 0); + #define IEEE802154_AR_FLAG_SET (0x20) /** diff --git a/include/net/ppp.h b/include/net/ppp.h index 138480da950..aebb8dd7561 100644 --- a/include/net/ppp.h +++ b/include/net/ppp.h @@ -62,6 +62,11 @@ struct ppp_api { #endif }; +/* Make sure that the network interface API is properly setup inside + * PPP API struct (it is the first one). + */ +BUILD_ASSERT(offsetof(struct ppp_api, iface_api) == 0); + /** * PPP protocol types. * See https://www.iana.org/assignments/ppp-numbers/ppp-numbers.xhtml diff --git a/include/net/socket_can.h b/include/net/socket_can.h index 0a8a08f878c..8aaf3d196c4 100644 --- a/include/net/socket_can.h +++ b/include/net/socket_can.h @@ -77,6 +77,11 @@ struct canbus_api { const void *optval, socklen_t *optlen); }; +/* Make sure that the network interface API is properly setup inside + * CANBUS API struct (it is the first one). + */ +BUILD_ASSERT(offsetof(struct canbus_api, iface_api) == 0); + /** * @} */ diff --git a/include/net/wifi_mgmt.h b/include/net/wifi_mgmt.h index 50fe51a1ae9..114f483395f 100644 --- a/include/net/wifi_mgmt.h +++ b/include/net/wifi_mgmt.h @@ -131,6 +131,11 @@ struct net_wifi_mgmt_offload { int (*ap_disable)(struct device *dev); }; +/* Make sure that the network interface API is properly setup inside + * Wifi mgmt offload API struct (it is the first one). + */ +BUILD_ASSERT(offsetof(struct net_wifi_mgmt_offload, iface_api) == 0); + #ifdef CONFIG_WIFI_OFFLOAD void wifi_mgmt_raise_connect_result_event(struct net_if *iface, int status);