xref: /openbmc/linux/net/netlink/policy.c (revision 04a351a6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NETLINK      Policy advertisement to userspace
4  *
5  * 		Authors:	Johannes Berg <johannes@sipsolutions.net>
6  *
7  * Copyright 2019 Intel Corporation
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <net/netlink.h>
14 
15 #define INITIAL_POLICIES_ALLOC	10
16 
17 struct netlink_policy_dump_state {
18 	unsigned int policy_idx;
19 	unsigned int attr_idx;
20 	unsigned int n_alloc;
21 	struct {
22 		const struct nla_policy *policy;
23 		unsigned int maxtype;
24 	} policies[];
25 };
26 
27 static int add_policy(struct netlink_policy_dump_state **statep,
28 		      const struct nla_policy *policy,
29 		      unsigned int maxtype)
30 {
31 	struct netlink_policy_dump_state *state = *statep;
32 	unsigned int n_alloc, i;
33 
34 	if (!policy || !maxtype)
35 		return 0;
36 
37 	for (i = 0; i < state->n_alloc; i++) {
38 		if (state->policies[i].policy == policy &&
39 		    state->policies[i].maxtype == maxtype)
40 			return 0;
41 
42 		if (!state->policies[i].policy) {
43 			state->policies[i].policy = policy;
44 			state->policies[i].maxtype = maxtype;
45 			return 0;
46 		}
47 	}
48 
49 	n_alloc = state->n_alloc + INITIAL_POLICIES_ALLOC;
50 	state = krealloc(state, struct_size(state, policies, n_alloc),
51 			 GFP_KERNEL);
52 	if (!state)
53 		return -ENOMEM;
54 
55 	memset(&state->policies[state->n_alloc], 0,
56 	       flex_array_size(state, policies, n_alloc - state->n_alloc));
57 
58 	state->policies[state->n_alloc].policy = policy;
59 	state->policies[state->n_alloc].maxtype = maxtype;
60 	state->n_alloc = n_alloc;
61 	*statep = state;
62 
63 	return 0;
64 }
65 
66 /**
67  * netlink_policy_dump_get_policy_idx - retrieve policy index
68  * @state: the policy dump state
69  * @policy: the policy to find
70  * @maxtype: the policy's maxattr
71  *
72  * Returns: the index of the given policy in the dump state
73  *
74  * Call this to find a policy index when you've added multiple and e.g.
75  * need to tell userspace which command has which policy (by index).
76  *
77  * Note: this will WARN and return 0 if the policy isn't found, which
78  *	 means it wasn't added in the first place, which would be an
79  *	 internal consistency bug.
80  */
81 int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
82 				       const struct nla_policy *policy,
83 				       unsigned int maxtype)
84 {
85 	unsigned int i;
86 
87 	if (WARN_ON(!policy || !maxtype))
88                 return 0;
89 
90 	for (i = 0; i < state->n_alloc; i++) {
91 		if (state->policies[i].policy == policy &&
92 		    state->policies[i].maxtype == maxtype)
93 			return i;
94 	}
95 
96 	WARN_ON(1);
97 	return 0;
98 }
99 
100 static struct netlink_policy_dump_state *alloc_state(void)
101 {
102 	struct netlink_policy_dump_state *state;
103 
104 	state = kzalloc(struct_size(state, policies, INITIAL_POLICIES_ALLOC),
105 			GFP_KERNEL);
106 	if (!state)
107 		return ERR_PTR(-ENOMEM);
108 	state->n_alloc = INITIAL_POLICIES_ALLOC;
109 
110 	return state;
111 }
112 
113 /**
114  * netlink_policy_dump_add_policy - add a policy to the dump
115  * @pstate: state to add to, may be reallocated, must be %NULL the first time
116  * @policy: the new policy to add to the dump
117  * @maxtype: the new policy's max attr type
118  *
119  * Returns: 0 on success, a negative error code otherwise.
120  *
121  * Call this to allocate a policy dump state, and to add policies to it. This
122  * should be called from the dump start() callback.
123  *
124  * Note: on failures, any previously allocated state is freed.
125  */
126 int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
127 				   const struct nla_policy *policy,
128 				   unsigned int maxtype)
129 {
130 	struct netlink_policy_dump_state *state = *pstate;
131 	unsigned int policy_idx;
132 	int err;
133 
134 	if (!state) {
135 		state = alloc_state();
136 		if (IS_ERR(state))
137 			return PTR_ERR(state);
138 	}
139 
140 	/*
141 	 * walk the policies and nested ones first, and build
142 	 * a linear list of them.
143 	 */
144 
145 	err = add_policy(&state, policy, maxtype);
146 	if (err)
147 		return err;
148 
149 	for (policy_idx = 0;
150 	     policy_idx < state->n_alloc && state->policies[policy_idx].policy;
151 	     policy_idx++) {
152 		const struct nla_policy *policy;
153 		unsigned int type;
154 
155 		policy = state->policies[policy_idx].policy;
156 
157 		for (type = 0;
158 		     type <= state->policies[policy_idx].maxtype;
159 		     type++) {
160 			switch (policy[type].type) {
161 			case NLA_NESTED:
162 			case NLA_NESTED_ARRAY:
163 				err = add_policy(&state,
164 						 policy[type].nested_policy,
165 						 policy[type].len);
166 				if (err)
167 					return err;
168 				break;
169 			default:
170 				break;
171 			}
172 		}
173 	}
174 
175 	*pstate = state;
176 	return 0;
177 }
178 
179 static bool
180 netlink_policy_dump_finished(struct netlink_policy_dump_state *state)
181 {
182 	return state->policy_idx >= state->n_alloc ||
183 	       !state->policies[state->policy_idx].policy;
184 }
185 
186 /**
187  * netlink_policy_dump_loop - dumping loop indicator
188  * @state: the policy dump state
189  *
190  * Returns: %true if the dump continues, %false otherwise
191  *
192  * Note: this frees the dump state when finishing
193  */
194 bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state)
195 {
196 	return !netlink_policy_dump_finished(state);
197 }
198 
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)
208 {
209 	const struct nla_policy *pt;
210 	struct nlattr *policy, *attr;
211 	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];
218 
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);
224 	if (!attr)
225 		goto nla_put_failure;
226 
227 	switch (pt->type) {
228 	default:
229 	case NLA_UNSPEC:
230 	case NLA_REJECT:
231 		/* skip - use NLA_MIN_LEN to advertise such */
232 		nla_nest_cancel(skb, policy);
233 		again = true;
234 		goto next;
235 	case NLA_NESTED:
236 		type = NL_ATTR_TYPE_NESTED;
237 		fallthrough;
238 	case NLA_NESTED_ARRAY:
239 		if (pt->type == NLA_NESTED_ARRAY)
240 			type = NL_ATTR_TYPE_NESTED_ARRAY;
241 		if (pt->nested_policy && pt->len &&
242 		    (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_POLICY_IDX,
243 				 netlink_policy_dump_get_policy_idx(state,
244 								    pt->nested_policy,
245 								    pt->len)) ||
246 		     nla_put_u32(skb, NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE,
247 				 pt->len)))
248 			goto nla_put_failure;
249 		break;
250 	case NLA_U8:
251 	case NLA_U16:
252 	case NLA_U32:
253 	case NLA_U64:
254 	case NLA_MSECS: {
255 		struct netlink_range_validation range;
256 
257 		if (pt->type == NLA_U8)
258 			type = NL_ATTR_TYPE_U8;
259 		else if (pt->type == NLA_U16)
260 			type = NL_ATTR_TYPE_U16;
261 		else if (pt->type == NLA_U32)
262 			type = NL_ATTR_TYPE_U32;
263 		else
264 			type = NL_ATTR_TYPE_U64;
265 
266 		nla_get_range_unsigned(pt, &range);
267 
268 		if (nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MIN_VALUE_U,
269 				      range.min, NL_POLICY_TYPE_ATTR_PAD) ||
270 		    nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MAX_VALUE_U,
271 				      range.max, NL_POLICY_TYPE_ATTR_PAD))
272 			goto nla_put_failure;
273 		break;
274 	}
275 	case NLA_S8:
276 	case NLA_S16:
277 	case NLA_S32:
278 	case NLA_S64: {
279 		struct netlink_range_validation_signed range;
280 
281 		if (pt->type == NLA_S8)
282 			type = NL_ATTR_TYPE_S8;
283 		else if (pt->type == NLA_S16)
284 			type = NL_ATTR_TYPE_S16;
285 		else if (pt->type == NLA_S32)
286 			type = NL_ATTR_TYPE_S32;
287 		else
288 			type = NL_ATTR_TYPE_S64;
289 
290 		nla_get_range_signed(pt, &range);
291 
292 		if (nla_put_s64(skb, NL_POLICY_TYPE_ATTR_MIN_VALUE_S,
293 				range.min, NL_POLICY_TYPE_ATTR_PAD) ||
294 		    nla_put_s64(skb, NL_POLICY_TYPE_ATTR_MAX_VALUE_S,
295 				range.max, NL_POLICY_TYPE_ATTR_PAD))
296 			goto nla_put_failure;
297 		break;
298 	}
299 	case NLA_BITFIELD32:
300 		type = NL_ATTR_TYPE_BITFIELD32;
301 		if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_BITFIELD32_MASK,
302 				pt->bitfield32_valid))
303 			goto nla_put_failure;
304 		break;
305 	case NLA_STRING:
306 	case NLA_NUL_STRING:
307 	case NLA_BINARY:
308 		if (pt->type == NLA_STRING)
309 			type = NL_ATTR_TYPE_STRING;
310 		else if (pt->type == NLA_NUL_STRING)
311 			type = NL_ATTR_TYPE_NUL_STRING;
312 		else
313 			type = NL_ATTR_TYPE_BINARY;
314 
315 		if (pt->validation_type == NLA_VALIDATE_RANGE ||
316 		    pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG) {
317 			struct netlink_range_validation range;
318 
319 			nla_get_range_unsigned(pt, &range);
320 
321 			if (range.min &&
322 			    nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MIN_LENGTH,
323 					range.min))
324 				goto nla_put_failure;
325 
326 			if (range.max < U16_MAX &&
327 			    nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MAX_LENGTH,
328 					range.max))
329 				goto nla_put_failure;
330 		} else if (pt->len &&
331 			   nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MAX_LENGTH,
332 				       pt->len)) {
333 			goto nla_put_failure;
334 		}
335 		break;
336 	case NLA_FLAG:
337 		type = NL_ATTR_TYPE_FLAG;
338 		break;
339 	}
340 
341 	if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_TYPE, type))
342 		goto nla_put_failure;
343 
344 	/* finish and move state to next attribute */
345 	nla_nest_end(skb, attr);
346 	nla_nest_end(skb, policy);
347 
348 next:
349 	state->attr_idx += 1;
350 	if (state->attr_idx > state->policies[state->policy_idx].maxtype) {
351 		state->attr_idx = 0;
352 		state->policy_idx++;
353 	}
354 
355 	if (again) {
356 		if (netlink_policy_dump_finished(state))
357 			return -ENODATA;
358 		goto send_attribute;
359 	}
360 
361 	return 0;
362 
363 nla_put_failure:
364 	nla_nest_cancel(skb, policy);
365 	return -ENOBUFS;
366 }
367 
368 /**
369  * netlink_policy_dump_free - free policy dump state
370  * @state: the policy dump state to free
371  *
372  * Call this from the done() method to ensure dump state is freed.
373  */
374 void netlink_policy_dump_free(struct netlink_policy_dump_state *state)
375 {
376 	kfree(state);
377 }
378