Skip to content

Commit d2681e9

Browse files
jmberg-intelkuba-moo
authored andcommitted
netlink: policy: refactor per-attr policy writing
Refactor the per-attribute policy writing into a new helper function, to be used later for dumping out the policy of a rejected attribute. v2: - fix some indentation v3: - change variable order in netlink_policy_dump_write() Reviewed-by: Jakub Kicinski <[email protected]> Signed-off-by: Johannes Berg <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent c4cc0b9 commit d2681e9

File tree

1 file changed

+51
-28
lines changed

1 file changed

+51
-28
lines changed

net/netlink/policy.c

+51-28
Original file line numberDiff line numberDiff line change
@@ -196,49 +196,33 @@ bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state)
196196
return !netlink_policy_dump_finished(state);
197197
}
198198

199-
/**
200-
* netlink_policy_dump_write - write current policy dump attributes
201-
* @skb: the message skb to write to
202-
* @state: the policy dump state
203-
*
204-
* Returns: 0 on success, an error code otherwise
205-
*/
206-
int netlink_policy_dump_write(struct sk_buff *skb,
207-
struct netlink_policy_dump_state *state)
199+
static int
200+
__netlink_policy_dump_write_attr(struct netlink_policy_dump_state *state,
201+
struct sk_buff *skb,
202+
const struct nla_policy *pt,
203+
int nestattr)
208204
{
209-
const struct nla_policy *pt;
210-
struct nlattr *policy, *attr;
211205
enum netlink_attribute_type type;
212-
bool again;
213-
214-
send_attribute:
215-
again = false;
216-
217-
pt = &state->policies[state->policy_idx].policy[state->attr_idx];
206+
struct nlattr *attr;
218207

219-
policy = nla_nest_start(skb, state->policy_idx);
220-
if (!policy)
221-
return -ENOBUFS;
222-
223-
attr = nla_nest_start(skb, state->attr_idx);
208+
attr = nla_nest_start(skb, nestattr);
224209
if (!attr)
225-
goto nla_put_failure;
210+
return -ENOBUFS;
226211

227212
switch (pt->type) {
228213
default:
229214
case NLA_UNSPEC:
230215
case NLA_REJECT:
231216
/* skip - use NLA_MIN_LEN to advertise such */
232-
nla_nest_cancel(skb, policy);
233-
again = true;
234-
goto next;
217+
nla_nest_cancel(skb, attr);
218+
return -ENODATA;
235219
case NLA_NESTED:
236220
type = NL_ATTR_TYPE_NESTED;
237221
fallthrough;
238222
case NLA_NESTED_ARRAY:
239223
if (pt->type == NLA_NESTED_ARRAY)
240224
type = NL_ATTR_TYPE_NESTED_ARRAY;
241-
if (pt->nested_policy && pt->len &&
225+
if (state && pt->nested_policy && pt->len &&
242226
(nla_put_u32(skb, NL_POLICY_TYPE_ATTR_POLICY_IDX,
243227
netlink_policy_dump_get_policy_idx(state,
244228
pt->nested_policy,
@@ -349,8 +333,47 @@ int netlink_policy_dump_write(struct sk_buff *skb,
349333
if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_TYPE, type))
350334
goto nla_put_failure;
351335

352-
/* finish and move state to next attribute */
353336
nla_nest_end(skb, attr);
337+
return 0;
338+
nla_put_failure:
339+
nla_nest_cancel(skb, attr);
340+
return -ENOBUFS;
341+
}
342+
343+
/**
344+
* netlink_policy_dump_write - write current policy dump attributes
345+
* @skb: the message skb to write to
346+
* @state: the policy dump state
347+
*
348+
* Returns: 0 on success, an error code otherwise
349+
*/
350+
int netlink_policy_dump_write(struct sk_buff *skb,
351+
struct netlink_policy_dump_state *state)
352+
{
353+
const struct nla_policy *pt;
354+
struct nlattr *policy;
355+
bool again;
356+
int err;
357+
358+
send_attribute:
359+
again = false;
360+
361+
pt = &state->policies[state->policy_idx].policy[state->attr_idx];
362+
363+
policy = nla_nest_start(skb, state->policy_idx);
364+
if (!policy)
365+
return -ENOBUFS;
366+
367+
err = __netlink_policy_dump_write_attr(state, skb, pt, state->attr_idx);
368+
if (err == -ENODATA) {
369+
nla_nest_cancel(skb, policy);
370+
again = true;
371+
goto next;
372+
} else if (err) {
373+
goto nla_put_failure;
374+
}
375+
376+
/* finish and move state to next attribute */
354377
nla_nest_end(skb, policy);
355378

356379
next:

0 commit comments

Comments
 (0)