Skip to content

Commit 9faebeb

Browse files
committed
Merge branch 'ethtool-allow-dumping-policies-to-user-space'
Jakub Kicinski says: ==================== ethtool: allow dumping policies to user space This series wires up ethtool policies to ops, so they can be dumped to user space for feature discovery. First patch wires up GET commands, and second patch wires up SETs. The policy tables are trimmed to save space and LoC. Next - take care of linking up nested policies for the header (which is the policy what we actually care about). And once header policy is linked make sure that attribute range validation for flags is done by policy, not a conditions in the code. New type of policy is needed to validate masks (patch 6). Netlink as always staying a step ahead of all the other kernel API interfaces :) v2: - merge patches 1 & 2 -> 1 - add patch 3 & 5 - remove .max_attr from struct ethnl_request_ops ==================== Reviewed-by: Johannes Berg <johannes@sipsolutions.net> Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 02da0b6 + a0de1cd commit 9faebeb

23 files changed

Lines changed: 317 additions & 414 deletions

‎include/net/netlink.h‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ enum nla_policy_validation {
200200
NLA_VALIDATE_RANGE_WARN_TOO_LONG,
201201
NLA_VALIDATE_MIN,
202202
NLA_VALIDATE_MAX,
203+
NLA_VALIDATE_MASK,
203204
NLA_VALIDATE_RANGE_PTR,
204205
NLA_VALIDATE_FUNCTION,
205206
};
@@ -317,6 +318,7 @@ struct nla_policy {
317318
u16 len;
318319
union {
319320
const u32 bitfield32_valid;
321+
const u32 mask;
320322
const char *reject_message;
321323
const struct nla_policy *nested_policy;
322324
struct netlink_range_validation *range;
@@ -362,20 +364,23 @@ struct nla_policy {
362364
#define NLA_POLICY_BITFIELD32(valid) \
363365
{ .type = NLA_BITFIELD32, .bitfield32_valid = valid }
364366

367+
#define __NLA_IS_UINT_TYPE(tp) \
368+
(tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 || tp == NLA_U64)
369+
#define __NLA_IS_SINT_TYPE(tp) \
370+
(tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64)
371+
365372
#define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
373+
#define NLA_ENSURE_UINT_TYPE(tp) \
374+
(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp)
366375
#define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp) \
367-
(__NLA_ENSURE(tp == NLA_U8 || tp == NLA_U16 || \
368-
tp == NLA_U32 || tp == NLA_U64 || \
376+
(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \
369377
tp == NLA_MSECS || \
370378
tp == NLA_BINARY) + tp)
371379
#define NLA_ENSURE_SINT_TYPE(tp) \
372-
(__NLA_ENSURE(tp == NLA_S8 || tp == NLA_S16 || \
373-
tp == NLA_S32 || tp == NLA_S64) + tp)
380+
(__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp)
374381
#define NLA_ENSURE_INT_OR_BINARY_TYPE(tp) \
375-
(__NLA_ENSURE(tp == NLA_S8 || tp == NLA_U8 || \
376-
tp == NLA_S16 || tp == NLA_U16 || \
377-
tp == NLA_S32 || tp == NLA_U32 || \
378-
tp == NLA_S64 || tp == NLA_U64 || \
382+
(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \
383+
__NLA_IS_SINT_TYPE(tp) || \
379384
tp == NLA_MSECS || \
380385
tp == NLA_BINARY) + tp)
381386
#define NLA_ENSURE_NO_VALIDATION_PTR(tp) \
@@ -415,6 +420,12 @@ struct nla_policy {
415420
.max = _max, \
416421
}
417422

423+
#define NLA_POLICY_MASK(tp, _mask) { \
424+
.type = NLA_ENSURE_UINT_TYPE(tp), \
425+
.validation_type = NLA_VALIDATE_MASK, \
426+
.mask = _mask, \
427+
}
428+
418429
#define NLA_POLICY_VALIDATE_FN(tp, fn, ...) { \
419430
.type = NLA_ENSURE_NO_VALIDATION_PTR(tp), \
420431
.validation_type = NLA_VALIDATE_FUNCTION, \

‎include/uapi/linux/netlink.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ enum netlink_attribute_type {
331331
* the index, if limited inside the nesting (U32)
332332
* @NL_POLICY_TYPE_ATTR_BITFIELD32_MASK: valid mask for the
333333
* bitfield32 type (U32)
334+
* @NL_POLICY_TYPE_ATTR_MASK: mask of valid bits for unsigned integers (U64)
334335
* @NL_POLICY_TYPE_ATTR_PAD: pad attribute for 64-bit alignment
335336
*/
336337
enum netlink_policy_type_attr {
@@ -346,6 +347,7 @@ enum netlink_policy_type_attr {
346347
NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE,
347348
NL_POLICY_TYPE_ATTR_BITFIELD32_MASK,
348349
NL_POLICY_TYPE_ATTR_PAD,
350+
NL_POLICY_TYPE_ATTR_MASK,
349351

350352
/* keep last */
351353
__NL_POLICY_TYPE_ATTR_MAX,

‎lib/nlattr.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,37 @@ static int nla_validate_int_range(const struct nla_policy *pt,
323323
}
324324
}
325325

326+
static int nla_validate_mask(const struct nla_policy *pt,
327+
const struct nlattr *nla,
328+
struct netlink_ext_ack *extack)
329+
{
330+
u64 value;
331+
332+
switch (pt->type) {
333+
case NLA_U8:
334+
value = nla_get_u8(nla);
335+
break;
336+
case NLA_U16:
337+
value = nla_get_u16(nla);
338+
break;
339+
case NLA_U32:
340+
value = nla_get_u32(nla);
341+
break;
342+
case NLA_U64:
343+
value = nla_get_u64(nla);
344+
break;
345+
default:
346+
return -EINVAL;
347+
}
348+
349+
if (value & ~(u64)pt->mask) {
350+
NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set");
351+
return -EINVAL;
352+
}
353+
354+
return 0;
355+
}
356+
326357
static int validate_nla(const struct nlattr *nla, int maxtype,
327358
const struct nla_policy *policy, unsigned int validate,
328359
struct netlink_ext_ack *extack, unsigned int depth)
@@ -503,6 +534,11 @@ static int validate_nla(const struct nlattr *nla, int maxtype,
503534
if (err)
504535
return err;
505536
break;
537+
case NLA_VALIDATE_MASK:
538+
err = nla_validate_mask(pt, nla, extack);
539+
if (err)
540+
return err;
541+
break;
506542
case NLA_VALIDATE_FUNCTION:
507543
if (pt->validate) {
508544
err = pt->validate(nla, extack);

‎net/ethtool/bitset.c‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ int ethnl_put_bitset32(struct sk_buff *skb, int attrtype, const u32 *val,
302302
return -EMSGSIZE;
303303
}
304304

305-
static const struct nla_policy bitset_policy[ETHTOOL_A_BITSET_MAX + 1] = {
306-
[ETHTOOL_A_BITSET_UNSPEC] = { .type = NLA_REJECT },
305+
static const struct nla_policy bitset_policy[] = {
307306
[ETHTOOL_A_BITSET_NOMASK] = { .type = NLA_FLAG },
308307
[ETHTOOL_A_BITSET_SIZE] = NLA_POLICY_MAX(NLA_U32,
309308
ETHNL_MAX_BITSET_SIZE),
@@ -312,8 +311,7 @@ static const struct nla_policy bitset_policy[ETHTOOL_A_BITSET_MAX + 1] = {
312311
[ETHTOOL_A_BITSET_MASK] = { .type = NLA_BINARY },
313312
};
314313

315-
static const struct nla_policy bit_policy[ETHTOOL_A_BITSET_BIT_MAX + 1] = {
316-
[ETHTOOL_A_BITSET_BIT_UNSPEC] = { .type = NLA_REJECT },
314+
static const struct nla_policy bit_policy[] = {
317315
[ETHTOOL_A_BITSET_BIT_INDEX] = { .type = NLA_U32 },
318316
[ETHTOOL_A_BITSET_BIT_NAME] = { .type = NLA_NUL_STRING },
319317
[ETHTOOL_A_BITSET_BIT_VALUE] = { .type = NLA_FLAG },
@@ -329,10 +327,10 @@ static const struct nla_policy bit_policy[ETHTOOL_A_BITSET_BIT_MAX + 1] = {
329327
*/
330328
int ethnl_bitset_is_compact(const struct nlattr *bitset, bool *compact)
331329
{
332-
struct nlattr *tb[ETHTOOL_A_BITSET_MAX + 1];
330+
struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
333331
int ret;
334332

335-
ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_MAX, bitset,
333+
ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, bitset,
336334
bitset_policy, NULL);
337335
if (ret < 0)
338336
return ret;
@@ -381,10 +379,10 @@ static int ethnl_parse_bit(unsigned int *index, bool *val, unsigned int nbits,
381379
ethnl_string_array_t names,
382380
struct netlink_ext_ack *extack)
383381
{
384-
struct nlattr *tb[ETHTOOL_A_BITSET_BIT_MAX + 1];
382+
struct nlattr *tb[ARRAY_SIZE(bit_policy)];
385383
int ret, idx;
386384

387-
ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_BIT_MAX, bit_attr,
385+
ret = nla_parse_nested(tb, ARRAY_SIZE(bit_policy) - 1, bit_attr,
388386
bit_policy, extack);
389387
if (ret < 0)
390388
return ret;
@@ -555,15 +553,15 @@ int ethnl_update_bitset32(u32 *bitmap, unsigned int nbits,
555553
const struct nlattr *attr, ethnl_string_array_t names,
556554
struct netlink_ext_ack *extack, bool *mod)
557555
{
558-
struct nlattr *tb[ETHTOOL_A_BITSET_MAX + 1];
556+
struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
559557
unsigned int change_bits;
560558
bool no_mask;
561559
int ret;
562560

563561
if (!attr)
564562
return 0;
565-
ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_MAX, attr, bitset_policy,
566-
extack);
563+
ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
564+
bitset_policy, extack);
567565
if (ret < 0)
568566
return ret;
569567

@@ -608,16 +606,16 @@ int ethnl_parse_bitset(unsigned long *val, unsigned long *mask,
608606
ethnl_string_array_t names,
609607
struct netlink_ext_ack *extack)
610608
{
611-
struct nlattr *tb[ETHTOOL_A_BITSET_MAX + 1];
609+
struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
612610
const struct nlattr *bit_attr;
613611
bool no_mask;
614612
int rem;
615613
int ret;
616614

617615
if (!attr)
618616
return 0;
619-
ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_MAX, attr, bitset_policy,
620-
extack);
617+
ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
618+
bitset_policy, extack);
621619
if (ret < 0)
622620
return ret;
623621
no_mask = tb[ETHTOOL_A_BITSET_NOMASK];

‎net/ethtool/cabletest.c‎

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
*/
1212
#define MAX_CABLE_LENGTH_CM (150 * 100)
1313

14-
static const struct nla_policy
15-
cable_test_act_policy[ETHTOOL_A_CABLE_TEST_MAX + 1] = {
16-
[ETHTOOL_A_CABLE_TEST_UNSPEC] = { .type = NLA_REJECT },
17-
[ETHTOOL_A_CABLE_TEST_HEADER] = { .type = NLA_NESTED },
14+
const struct nla_policy ethnl_cable_test_act_policy[] = {
15+
[ETHTOOL_A_CABLE_TEST_HEADER] =
16+
NLA_POLICY_NESTED(ethnl_header_policy),
1817
};
1918

2019
static int ethnl_cable_test_started(struct phy_device *phydev, u8 cmd)
@@ -56,18 +55,12 @@ static int ethnl_cable_test_started(struct phy_device *phydev, u8 cmd)
5655

5756
int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info)
5857
{
59-
struct nlattr *tb[ETHTOOL_A_CABLE_TEST_MAX + 1];
6058
struct ethnl_req_info req_info = {};
6159
const struct ethtool_phy_ops *ops;
60+
struct nlattr **tb = info->attrs;
6261
struct net_device *dev;
6362
int ret;
6463

65-
ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb,
66-
ETHTOOL_A_CABLE_TEST_MAX,
67-
cable_test_act_policy, info->extack);
68-
if (ret < 0)
69-
return ret;
70-
7164
ret = ethnl_parse_header_dev_get(&req_info,
7265
tb[ETHTOOL_A_CABLE_TEST_HEADER],
7366
genl_info_net(info), info->extack,
@@ -218,18 +211,16 @@ struct cable_test_tdr_req_info {
218211
struct ethnl_req_info base;
219212
};
220213

221-
static const struct nla_policy
222-
cable_test_tdr_act_cfg_policy[ETHTOOL_A_CABLE_TEST_TDR_CFG_MAX + 1] = {
214+
static const struct nla_policy cable_test_tdr_act_cfg_policy[] = {
223215
[ETHTOOL_A_CABLE_TEST_TDR_CFG_FIRST] = { .type = NLA_U32 },
224216
[ETHTOOL_A_CABLE_TEST_TDR_CFG_LAST] = { .type = NLA_U32 },
225217
[ETHTOOL_A_CABLE_TEST_TDR_CFG_STEP] = { .type = NLA_U32 },
226218
[ETHTOOL_A_CABLE_TEST_TDR_CFG_PAIR] = { .type = NLA_U8 },
227219
};
228220

229-
static const struct nla_policy
230-
cable_test_tdr_act_policy[ETHTOOL_A_CABLE_TEST_TDR_MAX + 1] = {
231-
[ETHTOOL_A_CABLE_TEST_TDR_UNSPEC] = { .type = NLA_REJECT },
232-
[ETHTOOL_A_CABLE_TEST_TDR_HEADER] = { .type = NLA_NESTED },
221+
const struct nla_policy ethnl_cable_test_tdr_act_policy[] = {
222+
[ETHTOOL_A_CABLE_TEST_TDR_HEADER] =
223+
NLA_POLICY_NESTED(ethnl_header_policy),
233224
[ETHTOOL_A_CABLE_TEST_TDR_CFG] = { .type = NLA_NESTED },
234225
};
235226

@@ -238,7 +229,7 @@ static int ethnl_act_cable_test_tdr_cfg(const struct nlattr *nest,
238229
struct genl_info *info,
239230
struct phy_tdr_config *cfg)
240231
{
241-
struct nlattr *tb[ETHTOOL_A_CABLE_TEST_TDR_CFG_MAX + 1];
232+
struct nlattr *tb[ARRAY_SIZE(cable_test_tdr_act_cfg_policy)];
242233
int ret;
243234

244235
cfg->first = 100;
@@ -249,8 +240,10 @@ static int ethnl_act_cable_test_tdr_cfg(const struct nlattr *nest,
249240
if (!nest)
250241
return 0;
251242

252-
ret = nla_parse_nested(tb, ETHTOOL_A_CABLE_TEST_TDR_CFG_MAX, nest,
253-
cable_test_tdr_act_cfg_policy, info->extack);
243+
ret = nla_parse_nested(tb,
244+
ARRAY_SIZE(cable_test_tdr_act_cfg_policy) - 1,
245+
nest, cable_test_tdr_act_cfg_policy,
246+
info->extack);
254247
if (ret < 0)
255248
return ret;
256249

@@ -313,19 +306,13 @@ static int ethnl_act_cable_test_tdr_cfg(const struct nlattr *nest,
313306

314307
int ethnl_act_cable_test_tdr(struct sk_buff *skb, struct genl_info *info)
315308
{
316-
struct nlattr *tb[ETHTOOL_A_CABLE_TEST_TDR_MAX + 1];
317309
struct ethnl_req_info req_info = {};
318310
const struct ethtool_phy_ops *ops;
311+
struct nlattr **tb = info->attrs;
319312
struct phy_tdr_config cfg;
320313
struct net_device *dev;
321314
int ret;
322315

323-
ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb,
324-
ETHTOOL_A_CABLE_TEST_TDR_MAX,
325-
cable_test_tdr_act_policy, info->extack);
326-
if (ret < 0)
327-
return ret;
328-
329316
ret = ethnl_parse_header_dev_get(&req_info,
330317
tb[ETHTOOL_A_CABLE_TEST_TDR_HEADER],
331318
genl_info_net(info), info->extack,

‎net/ethtool/channels.c‎

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,9 @@ struct channels_reply_data {
1717
#define CHANNELS_REPDATA(__reply_base) \
1818
container_of(__reply_base, struct channels_reply_data, base)
1919

20-
static const struct nla_policy
21-
channels_get_policy[ETHTOOL_A_CHANNELS_MAX + 1] = {
22-
[ETHTOOL_A_CHANNELS_UNSPEC] = { .type = NLA_REJECT },
23-
[ETHTOOL_A_CHANNELS_HEADER] = { .type = NLA_NESTED },
24-
[ETHTOOL_A_CHANNELS_RX_MAX] = { .type = NLA_REJECT },
25-
[ETHTOOL_A_CHANNELS_TX_MAX] = { .type = NLA_REJECT },
26-
[ETHTOOL_A_CHANNELS_OTHER_MAX] = { .type = NLA_REJECT },
27-
[ETHTOOL_A_CHANNELS_COMBINED_MAX] = { .type = NLA_REJECT },
28-
[ETHTOOL_A_CHANNELS_RX_COUNT] = { .type = NLA_REJECT },
29-
[ETHTOOL_A_CHANNELS_TX_COUNT] = { .type = NLA_REJECT },
30-
[ETHTOOL_A_CHANNELS_OTHER_COUNT] = { .type = NLA_REJECT },
31-
[ETHTOOL_A_CHANNELS_COMBINED_COUNT] = { .type = NLA_REJECT },
20+
const struct nla_policy ethnl_channels_get_policy[] = {
21+
[ETHTOOL_A_CHANNELS_HEADER] =
22+
NLA_POLICY_NESTED(ethnl_header_policy),
3223
};
3324

3425
static int channels_prepare_data(const struct ethnl_req_info *req_base,
@@ -99,10 +90,8 @@ const struct ethnl_request_ops ethnl_channels_request_ops = {
9990
.request_cmd = ETHTOOL_MSG_CHANNELS_GET,
10091
.reply_cmd = ETHTOOL_MSG_CHANNELS_GET_REPLY,
10192
.hdr_attr = ETHTOOL_A_CHANNELS_HEADER,
102-
.max_attr = ETHTOOL_A_CHANNELS_MAX,
10393
.req_info_size = sizeof(struct channels_req_info),
10494
.reply_data_size = sizeof(struct channels_reply_data),
105-
.request_policy = channels_get_policy,
10695

10796
.prepare_data = channels_prepare_data,
10897
.reply_size = channels_reply_size,
@@ -111,14 +100,9 @@ const struct ethnl_request_ops ethnl_channels_request_ops = {
111100

112101
/* CHANNELS_SET */
113102

114-
static const struct nla_policy
115-
channels_set_policy[ETHTOOL_A_CHANNELS_MAX + 1] = {
116-
[ETHTOOL_A_CHANNELS_UNSPEC] = { .type = NLA_REJECT },
117-
[ETHTOOL_A_CHANNELS_HEADER] = { .type = NLA_NESTED },
118-
[ETHTOOL_A_CHANNELS_RX_MAX] = { .type = NLA_REJECT },
119-
[ETHTOOL_A_CHANNELS_TX_MAX] = { .type = NLA_REJECT },
120-
[ETHTOOL_A_CHANNELS_OTHER_MAX] = { .type = NLA_REJECT },
121-
[ETHTOOL_A_CHANNELS_COMBINED_MAX] = { .type = NLA_REJECT },
103+
const struct nla_policy ethnl_channels_set_policy[] = {
104+
[ETHTOOL_A_CHANNELS_HEADER] =
105+
NLA_POLICY_NESTED(ethnl_header_policy),
122106
[ETHTOOL_A_CHANNELS_RX_COUNT] = { .type = NLA_U32 },
123107
[ETHTOOL_A_CHANNELS_TX_COUNT] = { .type = NLA_U32 },
124108
[ETHTOOL_A_CHANNELS_OTHER_COUNT] = { .type = NLA_U32 },
@@ -127,22 +111,17 @@ channels_set_policy[ETHTOOL_A_CHANNELS_MAX + 1] = {
127111

128112
int ethnl_set_channels(struct sk_buff *skb, struct genl_info *info)
129113
{
130-
struct nlattr *tb[ETHTOOL_A_CHANNELS_MAX + 1];
131114
unsigned int from_channel, old_total, i;
132115
bool mod = false, mod_combined = false;
133116
struct ethtool_channels channels = {};
134117
struct ethnl_req_info req_info = {};
118+
struct nlattr **tb = info->attrs;
135119
const struct nlattr *err_attr;
136120
const struct ethtool_ops *ops;
137121
struct net_device *dev;
138122
u32 max_rx_in_use = 0;
139123
int ret;
140124

141-
ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb,
142-
ETHTOOL_A_CHANNELS_MAX, channels_set_policy,
143-
info->extack);
144-
if (ret < 0)
145-
return ret;
146125
ret = ethnl_parse_header_dev_get(&req_info,
147126
tb[ETHTOOL_A_CHANNELS_HEADER],
148127
genl_info_net(info), info->extack,

0 commit comments

Comments
 (0)