[RFC PATCH net-next 2/4] ethtool: Split module EEPROM attributes validation to a function
flat view
STALE1931d
REVIEWED: 1 (0M)
From: Ido Schimmel <hidden>
Date: 2021-06-23 08:01:37
Subsystem:
networking [ethtool], networking [general], the rest · Maintainers:
Andrew Lunn, Jakub Kicinski, "David S. Miller", Eric Dumazet, Paolo Abeni, Linus Torvalds
Revision rfc of 2 in this series; 1 review trailer.
Revisions (2)
- rfc current
- rfc
From: Ido Schimmel <idosch@nvidia.com> The same validation will be needed for module EEPROM write access in the subsequent patch, so split it into a function. Modify the extack messages a bit to not be specific to read access. No functional changes intended. Signed-off-by: Ido Schimmel <idosch@nvidia.com> Reviewed-by: Jiri Pirko <redacted> --- net/ethtool/eeprom.c | 58 ++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 23 deletions(-)
diff --git a/net/ethtool/eeprom.c b/net/ethtool/eeprom.c
index ed5f677f27cd..945b95e64f0d 100644
--- a/net/ethtool/eeprom.c
+++ b/net/ethtool/eeprom.c@@ -144,10 +144,41 @@ static int eeprom_prepare_data(const struct ethnl_req_info *req_base, return ret; } +static int eeprom_validate(struct nlattr **tb, struct netlink_ext_ack *extack) +{ + u32 offset = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_OFFSET]); + u32 length = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_LENGTH]); + u8 page = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_PAGE]); + + /* The following set of conditions limit the API to only access 1/2 + * EEPROM page without crossing low page boundary located at offset + * 128. For pages higher than 0, only high 128 bytes are accessible. + */ + if (page && offset < ETH_MODULE_EEPROM_PAGE_LEN) { + NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_PAGE], + "access to lower half page is allowed for page 0 only"); + return -EINVAL; + } + + if (offset < ETH_MODULE_EEPROM_PAGE_LEN && + offset + length > ETH_MODULE_EEPROM_PAGE_LEN) { + NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], + "crossing half page boundary is illegal"); + return -EINVAL; + } else if (offset + length > ETH_MODULE_EEPROM_PAGE_LEN * 2) { + NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], + "crossing page boundary is illegal"); + return -EINVAL; + } + + return 0; +} + static int eeprom_parse_request(struct ethnl_req_info *req_info, struct nlattr **tb, struct netlink_ext_ack *extack) { struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_info); + int err; if (!tb[ETHTOOL_A_MODULE_EEPROM_OFFSET] || !tb[ETHTOOL_A_MODULE_EEPROM_LENGTH] ||
@@ -155,6 +186,10 @@ static int eeprom_parse_request(struct ethnl_req_info *req_info, struct nlattr * !tb[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS]) return -EINVAL; + err = eeprom_validate(tb, extack); + if (err) + return err; + request->i2c_address = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS]); request->offset = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_OFFSET]); request->length = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_LENGTH]);
@@ -162,29 +197,6 @@ static int eeprom_parse_request(struct ethnl_req_info *req_info, struct nlattr * if (tb[ETHTOOL_A_MODULE_EEPROM_BANK]) request->bank = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_BANK]); - /* The following set of conditions limit the API to only dump 1/2 - * EEPROM page without crossing low page boundary located at offset 128. - * This means user may only request dumps of length limited to 128 from - * either low 128 bytes or high 128 bytes. - * For pages higher than 0 only high 128 bytes are accessible. - */ - if (request->page && request->offset < ETH_MODULE_EEPROM_PAGE_LEN) { - NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_PAGE], - "reading from lower half page is allowed for page 0 only"); - return -EINVAL; - } - - if (request->offset < ETH_MODULE_EEPROM_PAGE_LEN && - request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN) { - NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], - "reading cross half page boundary is illegal"); - return -EINVAL; - } else if (request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN * 2) { - NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], - "reading cross page boundary is illegal"); - return -EINVAL; - } - return 0; }
--
2.31.1