1 // SPDX-License-Identifier: GPL-2.0
3 * NETLINK Policy advertisement to userspace
7 * Copyright 2019 Intel Corporation
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <net/netlink.h>
15 #define INITIAL_POLICIES_ALLOC 10
17 struct nl_policy_dump {
18 unsigned int policy_idx;
19 unsigned int attr_idx;
22 const struct nla_policy *policy;
27 static int add_policy(struct nl_policy_dump **statep,
28 const struct nla_policy *policy,
31 struct nl_policy_dump *state = *statep;
32 unsigned int n_alloc, i;
34 if (!policy || !maxtype)
37 for (i = 0; i < state->n_alloc; i++) {
38 if (state->policies[i].policy == policy)
41 if (!state->policies[i].policy) {
42 state->policies[i].policy = policy;
43 state->policies[i].maxtype = maxtype;
48 n_alloc = state->n_alloc + INITIAL_POLICIES_ALLOC;
49 state = krealloc(state, struct_size(state, policies, n_alloc),
54 memset(&state->policies[state->n_alloc], 0,
55 flex_array_size(state, policies, n_alloc - state->n_alloc));
57 state->policies[state->n_alloc].policy = policy;
58 state->policies[state->n_alloc].maxtype = maxtype;
59 state->n_alloc = n_alloc;
65 static unsigned int get_policy_idx(struct nl_policy_dump *state,
66 const struct nla_policy *policy)
70 for (i = 0; i < state->n_alloc; i++) {
71 if (state->policies[i].policy == policy)
79 int netlink_policy_dump_start(const struct nla_policy *policy,
81 unsigned long *_state)
83 struct nl_policy_dump *state;
84 unsigned int policy_idx;
91 * walk the policies and nested ones first, and build
92 * a linear list of them.
95 state = kzalloc(struct_size(state, policies, INITIAL_POLICIES_ALLOC),
99 state->n_alloc = INITIAL_POLICIES_ALLOC;
101 err = add_policy(&state, policy, maxtype);
106 policy_idx < state->n_alloc && state->policies[policy_idx].policy;
108 const struct nla_policy *policy;
111 policy = state->policies[policy_idx].policy;
114 type <= state->policies[policy_idx].maxtype;
116 switch (policy[type].type) {
118 case NLA_NESTED_ARRAY:
119 err = add_policy(&state,
120 policy[type].nested_policy,
131 *_state = (unsigned long)state;
136 static bool netlink_policy_dump_finished(struct nl_policy_dump *state)
138 return state->policy_idx >= state->n_alloc ||
139 !state->policies[state->policy_idx].policy;
142 bool netlink_policy_dump_loop(unsigned long _state)
144 struct nl_policy_dump *state = (void *)_state;
146 return !netlink_policy_dump_finished(state);
149 int netlink_policy_dump_write(struct sk_buff *skb, unsigned long _state)
151 struct nl_policy_dump *state = (void *)_state;
152 const struct nla_policy *pt;
153 struct nlattr *policy, *attr;
154 enum netlink_attribute_type type;
160 pt = &state->policies[state->policy_idx].policy[state->attr_idx];
162 policy = nla_nest_start(skb, state->policy_idx);
166 attr = nla_nest_start(skb, state->attr_idx);
168 goto nla_put_failure;
174 /* skip - use NLA_MIN_LEN to advertise such */
175 nla_nest_cancel(skb, policy);
179 type = NL_ATTR_TYPE_NESTED;
181 case NLA_NESTED_ARRAY:
182 if (pt->type == NLA_NESTED_ARRAY)
183 type = NL_ATTR_TYPE_NESTED_ARRAY;
184 if (pt->nested_policy && pt->len &&
185 (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_POLICY_IDX,
186 get_policy_idx(state, pt->nested_policy)) ||
187 nla_put_u32(skb, NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE,
189 goto nla_put_failure;
196 struct netlink_range_validation range;
198 if (pt->type == NLA_U8)
199 type = NL_ATTR_TYPE_U8;
200 else if (pt->type == NLA_U16)
201 type = NL_ATTR_TYPE_U16;
202 else if (pt->type == NLA_U32)
203 type = NL_ATTR_TYPE_U32;
205 type = NL_ATTR_TYPE_U64;
207 nla_get_range_unsigned(pt, &range);
209 if (nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MIN_VALUE_U,
210 range.min, NL_POLICY_TYPE_ATTR_PAD) ||
211 nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MAX_VALUE_U,
212 range.max, NL_POLICY_TYPE_ATTR_PAD))
213 goto nla_put_failure;
220 struct netlink_range_validation_signed range;
222 if (pt->type == NLA_S8)
223 type = NL_ATTR_TYPE_S8;
224 else if (pt->type == NLA_S16)
225 type = NL_ATTR_TYPE_S16;
226 else if (pt->type == NLA_S32)
227 type = NL_ATTR_TYPE_S32;
229 type = NL_ATTR_TYPE_S64;
231 nla_get_range_signed(pt, &range);
233 if (nla_put_s64(skb, NL_POLICY_TYPE_ATTR_MIN_VALUE_S,
234 range.min, NL_POLICY_TYPE_ATTR_PAD) ||
235 nla_put_s64(skb, NL_POLICY_TYPE_ATTR_MAX_VALUE_S,
236 range.max, NL_POLICY_TYPE_ATTR_PAD))
237 goto nla_put_failure;
241 type = NL_ATTR_TYPE_BITFIELD32;
242 if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_BITFIELD32_MASK,
243 pt->bitfield32_valid))
244 goto nla_put_failure;
247 type = NL_ATTR_TYPE_BINARY;
248 if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MIN_LENGTH, pt->len) ||
249 nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MAX_LENGTH, pt->len))
250 goto nla_put_failure;
255 if (pt->type == NLA_STRING)
256 type = NL_ATTR_TYPE_STRING;
257 else if (pt->type == NLA_NUL_STRING)
258 type = NL_ATTR_TYPE_NUL_STRING;
260 type = NL_ATTR_TYPE_BINARY;
261 if (pt->len && nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MAX_LENGTH,
263 goto nla_put_failure;
266 type = NL_ATTR_TYPE_BINARY;
267 if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MIN_LENGTH, pt->len))
268 goto nla_put_failure;
271 type = NL_ATTR_TYPE_FLAG;
275 if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_TYPE, type))
276 goto nla_put_failure;
278 /* finish and move state to next attribute */
279 nla_nest_end(skb, attr);
280 nla_nest_end(skb, policy);
283 state->attr_idx += 1;
284 if (state->attr_idx > state->policies[state->policy_idx].maxtype) {
290 if (netlink_policy_dump_finished(state))
298 nla_nest_cancel(skb, policy);
302 void netlink_policy_dump_free(unsigned long _state)
304 struct nl_policy_dump *state = (void *)_state;