blob: ee26d01328ee682bc34f2bb955c717eb080f07e6 [file] [log] [blame]
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001// 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
Jakub Kicinskiadc84842020-10-02 14:49:55 -070017struct netlink_policy_dump_state {
Johannes Bergd07dcf9a2020-04-30 22:13:12 +020018 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
Jakub Kicinskiadc84842020-10-02 14:49:55 -070027static int add_policy(struct netlink_policy_dump_state **statep,
Johannes Bergd07dcf9a2020-04-30 22:13:12 +020028 const struct nla_policy *policy,
29 unsigned int maxtype)
30{
Jakub Kicinskiadc84842020-10-02 14:49:55 -070031 struct netlink_policy_dump_state *state = *statep;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +020032 unsigned int n_alloc, i;
33
34 if (!policy || !maxtype)
35 return 0;
36
37 for (i = 0; i < state->n_alloc; i++) {
Johannes Berg899b07c2020-10-03 10:44:42 +020038 if (state->policies[i].policy == policy &&
39 state->policies[i].maxtype == maxtype)
Johannes Bergd07dcf9a2020-04-30 22:13:12 +020040 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
Johannes Bergd1fb5552020-08-19 21:52:38 +020055 memset(&state->policies[state->n_alloc], 0,
56 flex_array_size(state, policies, n_alloc - state->n_alloc));
57
Johannes Bergd07dcf9a2020-04-30 22:13:12 +020058 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
Johannes Berg04a351a2020-10-03 10:44:43 +020066/**
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 */
81int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
82 const struct nla_policy *policy,
83 unsigned int maxtype)
Johannes Bergd07dcf9a2020-04-30 22:13:12 +020084{
85 unsigned int i;
86
Johannes Berg04a351a2020-10-03 10:44:43 +020087 if (WARN_ON(!policy || !maxtype))
88 return 0;
89
Johannes Bergd07dcf9a2020-04-30 22:13:12 +020090 for (i = 0; i < state->n_alloc; i++) {
Johannes Berg899b07c2020-10-03 10:44:42 +020091 if (state->policies[i].policy == policy &&
92 state->policies[i].maxtype == maxtype)
Johannes Bergd07dcf9a2020-04-30 22:13:12 +020093 return i;
94 }
95
Johannes Berg04a351a2020-10-03 10:44:43 +020096 WARN_ON(1);
97 return 0;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +020098}
99
Johannes Berg04a351a2020-10-03 10:44:43 +0200100static struct netlink_policy_dump_state *alloc_state(void)
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200101{
Jakub Kicinskiadc84842020-10-02 14:49:55 -0700102 struct netlink_policy_dump_state *state;
Johannes Berg04a351a2020-10-03 10:44:43 +0200103
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 */
126int 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;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200131 unsigned int policy_idx;
132 int err;
133
Johannes Berg04a351a2020-10-03 10:44:43 +0200134 if (!state) {
135 state = alloc_state();
136 if (IS_ERR(state))
137 return PTR_ERR(state);
138 }
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200139
140 /*
141 * walk the policies and nested ones first, and build
142 * a linear list of them.
143 */
144
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200145 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
Johannes Berg04a351a2020-10-03 10:44:43 +0200175 *pstate = state;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200176 return 0;
177}
178
Jakub Kicinskiadc84842020-10-02 14:49:55 -0700179static bool
180netlink_policy_dump_finished(struct netlink_policy_dump_state *state)
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200181{
182 return state->policy_idx >= state->n_alloc ||
183 !state->policies[state->policy_idx].policy;
184}
185
Johannes Berg04a351a2020-10-03 10:44:43 +0200186/**
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 */
Jakub Kicinskiadc84842020-10-02 14:49:55 -0700194bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state)
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200195{
Johannes Berg949ca6b2020-10-02 09:46:04 +0200196 return !netlink_policy_dump_finished(state);
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200197}
198
Johannes Berg04a351a2020-10-03 10:44:43 +0200199/**
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 */
Jakub Kicinskiadc84842020-10-02 14:49:55 -0700206int netlink_policy_dump_write(struct sk_buff *skb,
207 struct netlink_policy_dump_state *state)
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200208{
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200209 const struct nla_policy *pt;
210 struct nlattr *policy, *attr;
211 enum netlink_attribute_type type;
212 bool again;
213
214send_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;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500237 fallthrough;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200238 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,
Johannes Berg04a351a2020-10-03 10:44:43 +0200243 netlink_policy_dump_get_policy_idx(state,
244 pt->nested_policy,
245 pt->len)) ||
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200246 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
Jakub Kicinskibdbb4e22020-10-05 15:07:38 -0700266 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
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200274 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;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200313 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;
Johannes Berg8aa26c52020-08-18 10:17:33 +0200322
Johannes Bergc30a3c92020-08-31 20:28:05 +0200323 if (pt->validation_type == NLA_VALIDATE_RANGE ||
324 pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG) {
Johannes Berg8aa26c52020-08-18 10:17:33 +0200325 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)) {
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200341 goto nla_put_failure;
Johannes Berg8aa26c52020-08-18 10:17:33 +0200342 }
Johannes Bergd07dcf9a2020-04-30 22:13:12 +0200343 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
356next:
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
371nla_put_failure:
372 nla_nest_cancel(skb, policy);
373 return -ENOBUFS;
374}
Johannes Berg949ca6b2020-10-02 09:46:04 +0200375
Johannes Berg04a351a2020-10-03 10:44:43 +0200376/**
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 */
Jakub Kicinskiadc84842020-10-02 14:49:55 -0700382void netlink_policy_dump_free(struct netlink_policy_dump_state *state)
Johannes Berg949ca6b2020-10-02 09:46:04 +0200383{
Johannes Berg949ca6b2020-10-02 09:46:04 +0200384 kfree(state);
385}