xref: /openbmc/linux/net/netlink/policy.c (revision bdbb4e29)
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 		if (pt->validation_type == NLA_VALIDATE_MASK) {
267 			if (nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MASK,
268 					      pt->mask,
269 					      NL_POLICY_TYPE_ATTR_PAD))
270 				goto nla_put_failure;
271 			break;
272 		}
273 
274 		nla_get_range_unsigned(pt, &range);
275 
276 		if (nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MIN_VALUE_U,
277 				      range.min, NL_POLICY_TYPE_ATTR_PAD) ||
278 		    nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MAX_VALUE_U,
279 				      range.max, NL_POLICY_TYPE_ATTR_PAD))
280 			goto nla_put_failure;
281 		break;
282 	}
283 	case NLA_S8:
284 	case NLA_S16:
285 	case NLA_S32:
286 	case NLA_S64: {
287 		struct netlink_range_validation_signed range;
288 
289 		if (pt->type == NLA_S8)
290 			type = NL_ATTR_TYPE_S8;
291 		else if (pt->type == NLA_S16)
292 			type = NL_ATTR_TYPE_S16;
293 		else if (pt->type == NLA_S32)
294 			type = NL_ATTR_TYPE_S32;
295 		else
296 			type = NL_ATTR_TYPE_S64;
297 
298 		nla_get_range_signed(pt, &range);
299 
300 		if (nla_put_s64(skb, NL_POLICY_TYPE_ATTR_MIN_VALUE_S,
301 				range.min, NL_POLICY_TYPE_ATTR_PAD) ||
302 		    nla_put_s64(skb, NL_POLICY_TYPE_ATTR_MAX_VALUE_S,
303 				range.max, NL_POLICY_TYPE_ATTR_PAD))
304 			goto nla_put_failure;
305 		break;
306 	}
307 	case NLA_BITFIELD32:
308 		type = NL_ATTR_TYPE_BITFIELD32;
309 		if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_BITFIELD32_MASK,
310 				pt->bitfield32_valid))
311 			goto nla_put_failure;
312 		break;
313 	case NLA_STRING:
314 	case NLA_NUL_STRING:
315 	case NLA_BINARY:
316 		if (pt->type == NLA_STRING)
317 			type = NL_ATTR_TYPE_STRING;
318 		else if (pt->type == NLA_NUL_STRING)
319 			type = NL_ATTR_TYPE_NUL_STRING;
320 		else
321 			type = NL_ATTR_TYPE_BINARY;
322 
323 		if (pt->validation_type == NLA_VALIDATE_RANGE ||
324 		    pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG) {
325 			struct netlink_range_validation range;
326 
327 			nla_get_range_unsigned(pt, &range);
328 
329 			if (range.min &&
330 			    nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MIN_LENGTH,
331 					range.min))
332 				goto nla_put_failure;
333 
334 			if (range.max < U16_MAX &&
335 			    nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MAX_LENGTH,
336 					range.max))
337 				goto nla_put_failure;
338 		} else if (pt->len &&
339 			   nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MAX_LENGTH,
340 				       pt->len)) {
341 			goto nla_put_failure;
342 		}
343 		break;
344 	case NLA_FLAG:
345 		type = NL_ATTR_TYPE_FLAG;
346 		break;
347 	}
348 
349 	if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_TYPE, type))
350 		goto nla_put_failure;
351 
352 	/* finish and move state to next attribute */
353 	nla_nest_end(skb, attr);
354 	nla_nest_end(skb, policy);
355 
356 next:
357 	state->attr_idx += 1;
358 	if (state->attr_idx > state->policies[state->policy_idx].maxtype) {
359 		state->attr_idx = 0;
360 		state->policy_idx++;
361 	}
362 
363 	if (again) {
364 		if (netlink_policy_dump_finished(state))
365 			return -ENODATA;
366 		goto send_attribute;
367 	}
368 
369 	return 0;
370 
371 nla_put_failure:
372 	nla_nest_cancel(skb, policy);
373 	return -ENOBUFS;
374 }
375 
376 /**
377  * netlink_policy_dump_free - free policy dump state
378  * @state: the policy dump state to free
379  *
380  * Call this from the done() method to ensure dump state is freed.
381  */
382 void netlink_policy_dump_free(struct netlink_policy_dump_state *state)
383 {
384 	kfree(state);
385 }
386