Blame view

net/netlink/policy.c 11.2 KB
d07dcf9aa   Johannes Berg   netlink: add infr...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  // SPDX-License-Identifier: GPL-2.0
  /*
   * NETLINK      Policy advertisement to userspace
   *
   * 		Authors:	Johannes Berg <johannes@sipsolutions.net>
   *
   * Copyright 2019 Intel Corporation
   */
  
  #include <linux/kernel.h>
  #include <linux/errno.h>
  #include <linux/types.h>
  #include <net/netlink.h>
  
  #define INITIAL_POLICIES_ALLOC	10
adc848450   Jakub Kicinski   genetlink: add a ...
16
  struct netlink_policy_dump_state {
d07dcf9aa   Johannes Berg   netlink: add infr...
17
18
19
20
21
22
23
24
  	unsigned int policy_idx;
  	unsigned int attr_idx;
  	unsigned int n_alloc;
  	struct {
  		const struct nla_policy *policy;
  		unsigned int maxtype;
  	} policies[];
  };
adc848450   Jakub Kicinski   genetlink: add a ...
25
  static int add_policy(struct netlink_policy_dump_state **statep,
d07dcf9aa   Johannes Berg   netlink: add infr...
26
27
28
  		      const struct nla_policy *policy,
  		      unsigned int maxtype)
  {
adc848450   Jakub Kicinski   genetlink: add a ...
29
  	struct netlink_policy_dump_state *state = *statep;
d07dcf9aa   Johannes Berg   netlink: add infr...
30
31
32
33
34
35
  	unsigned int n_alloc, i;
  
  	if (!policy || !maxtype)
  		return 0;
  
  	for (i = 0; i < state->n_alloc; i++) {
899b07c57   Johannes Berg   netlink: compare ...
36
37
  		if (state->policies[i].policy == policy &&
  		    state->policies[i].maxtype == maxtype)
d07dcf9aa   Johannes Berg   netlink: add infr...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  			return 0;
  
  		if (!state->policies[i].policy) {
  			state->policies[i].policy = policy;
  			state->policies[i].maxtype = maxtype;
  			return 0;
  		}
  	}
  
  	n_alloc = state->n_alloc + INITIAL_POLICIES_ALLOC;
  	state = krealloc(state, struct_size(state, policies, n_alloc),
  			 GFP_KERNEL);
  	if (!state)
  		return -ENOMEM;
d1fb55592   Johannes Berg   netlink: fix stat...
52
53
  	memset(&state->policies[state->n_alloc], 0,
  	       flex_array_size(state, policies, n_alloc - state->n_alloc));
d07dcf9aa   Johannes Berg   netlink: add infr...
54
55
56
57
58
59
60
  	state->policies[state->n_alloc].policy = policy;
  	state->policies[state->n_alloc].maxtype = maxtype;
  	state->n_alloc = n_alloc;
  	*statep = state;
  
  	return 0;
  }
04a351a62   Johannes Berg   netlink: rework p...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  /**
   * netlink_policy_dump_get_policy_idx - retrieve policy index
   * @state: the policy dump state
   * @policy: the policy to find
   * @maxtype: the policy's maxattr
   *
   * Returns: the index of the given policy in the dump state
   *
   * Call this to find a policy index when you've added multiple and e.g.
   * need to tell userspace which command has which policy (by index).
   *
   * Note: this will WARN and return 0 if the policy isn't found, which
   *	 means it wasn't added in the first place, which would be an
   *	 internal consistency bug.
   */
  int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
  				       const struct nla_policy *policy,
  				       unsigned int maxtype)
d07dcf9aa   Johannes Berg   netlink: add infr...
79
80
  {
  	unsigned int i;
04a351a62   Johannes Berg   netlink: rework p...
81
82
  	if (WARN_ON(!policy || !maxtype))
                  return 0;
d07dcf9aa   Johannes Berg   netlink: add infr...
83
  	for (i = 0; i < state->n_alloc; i++) {
899b07c57   Johannes Berg   netlink: compare ...
84
85
  		if (state->policies[i].policy == policy &&
  		    state->policies[i].maxtype == maxtype)
d07dcf9aa   Johannes Berg   netlink: add infr...
86
87
  			return i;
  	}
04a351a62   Johannes Berg   netlink: rework p...
88
89
  	WARN_ON(1);
  	return 0;
d07dcf9aa   Johannes Berg   netlink: add infr...
90
  }
04a351a62   Johannes Berg   netlink: rework p...
91
  static struct netlink_policy_dump_state *alloc_state(void)
d07dcf9aa   Johannes Berg   netlink: add infr...
92
  {
adc848450   Jakub Kicinski   genetlink: add a ...
93
  	struct netlink_policy_dump_state *state;
04a351a62   Johannes Berg   netlink: rework p...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  
  	state = kzalloc(struct_size(state, policies, INITIAL_POLICIES_ALLOC),
  			GFP_KERNEL);
  	if (!state)
  		return ERR_PTR(-ENOMEM);
  	state->n_alloc = INITIAL_POLICIES_ALLOC;
  
  	return state;
  }
  
  /**
   * netlink_policy_dump_add_policy - add a policy to the dump
   * @pstate: state to add to, may be reallocated, must be %NULL the first time
   * @policy: the new policy to add to the dump
   * @maxtype: the new policy's max attr type
   *
   * Returns: 0 on success, a negative error code otherwise.
   *
   * Call this to allocate a policy dump state, and to add policies to it. This
   * should be called from the dump start() callback.
   *
   * Note: on failures, any previously allocated state is freed.
   */
  int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
  				   const struct nla_policy *policy,
  				   unsigned int maxtype)
  {
  	struct netlink_policy_dump_state *state = *pstate;
d07dcf9aa   Johannes Berg   netlink: add infr...
122
123
  	unsigned int policy_idx;
  	int err;
04a351a62   Johannes Berg   netlink: rework p...
124
125
126
127
128
  	if (!state) {
  		state = alloc_state();
  		if (IS_ERR(state))
  			return PTR_ERR(state);
  	}
d07dcf9aa   Johannes Berg   netlink: add infr...
129
130
131
132
133
  
  	/*
  	 * walk the policies and nested ones first, and build
  	 * a linear list of them.
  	 */
d07dcf9aa   Johannes Berg   netlink: add infr...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  	err = add_policy(&state, policy, maxtype);
  	if (err)
  		return err;
  
  	for (policy_idx = 0;
  	     policy_idx < state->n_alloc && state->policies[policy_idx].policy;
  	     policy_idx++) {
  		const struct nla_policy *policy;
  		unsigned int type;
  
  		policy = state->policies[policy_idx].policy;
  
  		for (type = 0;
  		     type <= state->policies[policy_idx].maxtype;
  		     type++) {
  			switch (policy[type].type) {
  			case NLA_NESTED:
  			case NLA_NESTED_ARRAY:
  				err = add_policy(&state,
  						 policy[type].nested_policy,
  						 policy[type].len);
  				if (err)
  					return err;
  				break;
  			default:
  				break;
  			}
  		}
  	}
04a351a62   Johannes Berg   netlink: rework p...
163
  	*pstate = state;
d07dcf9aa   Johannes Berg   netlink: add infr...
164
165
  	return 0;
  }
adc848450   Jakub Kicinski   genetlink: add a ...
166
167
  static bool
  netlink_policy_dump_finished(struct netlink_policy_dump_state *state)
d07dcf9aa   Johannes Berg   netlink: add infr...
168
169
170
171
  {
  	return state->policy_idx >= state->n_alloc ||
  	       !state->policies[state->policy_idx].policy;
  }
04a351a62   Johannes Berg   netlink: rework p...
172
173
174
175
176
177
178
179
  /**
   * netlink_policy_dump_loop - dumping loop indicator
   * @state: the policy dump state
   *
   * Returns: %true if the dump continues, %false otherwise
   *
   * Note: this frees the dump state when finishing
   */
adc848450   Jakub Kicinski   genetlink: add a ...
180
  bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state)
d07dcf9aa   Johannes Berg   netlink: add infr...
181
  {
949ca6b82   Johannes Berg   netlink: fix poli...
182
  	return !netlink_policy_dump_finished(state);
d07dcf9aa   Johannes Berg   netlink: add infr...
183
  }
44f3625bc   Johannes Berg   netlink: export p...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt)
  {
  	/* nested + type */
  	int common = 2 * nla_attr_size(sizeof(u32));
  
  	switch (pt->type) {
  	case NLA_UNSPEC:
  	case NLA_REJECT:
  		/* these actually don't need any space */
  		return 0;
  	case NLA_NESTED:
  	case NLA_NESTED_ARRAY:
  		/* common, policy idx, policy maxattr */
  		return common + 2 * nla_attr_size(sizeof(u32));
  	case NLA_U8:
  	case NLA_U16:
  	case NLA_U32:
  	case NLA_U64:
  	case NLA_MSECS:
  	case NLA_S8:
  	case NLA_S16:
  	case NLA_S32:
  	case NLA_S64:
  		/* maximum is common, u64 min/max with padding */
  		return common +
  		       2 * (nla_attr_size(0) + nla_attr_size(sizeof(u64)));
  	case NLA_BITFIELD32:
  		return common + nla_attr_size(sizeof(u32));
  	case NLA_STRING:
  	case NLA_NUL_STRING:
  	case NLA_BINARY:
  		/* maximum is common, u32 min-length/max-length */
  		return common + 2 * nla_attr_size(sizeof(u32));
  	case NLA_FLAG:
  		return common;
  	}
  
  	/* this should then cause a warning later */
  	return 0;
  }
d2681e93b   Johannes Berg   netlink: policy: ...
224
225
226
227
228
  static int
  __netlink_policy_dump_write_attr(struct netlink_policy_dump_state *state,
  				 struct sk_buff *skb,
  				 const struct nla_policy *pt,
  				 int nestattr)
d07dcf9aa   Johannes Berg   netlink: add infr...
229
  {
44f3625bc   Johannes Berg   netlink: export p...
230
  	int estimate = netlink_policy_dump_attr_size_estimate(pt);
d07dcf9aa   Johannes Berg   netlink: add infr...
231
  	enum netlink_attribute_type type;
d2681e93b   Johannes Berg   netlink: policy: ...
232
  	struct nlattr *attr;
d07dcf9aa   Johannes Berg   netlink: add infr...
233

d2681e93b   Johannes Berg   netlink: policy: ...
234
  	attr = nla_nest_start(skb, nestattr);
d07dcf9aa   Johannes Berg   netlink: add infr...
235
  	if (!attr)
d2681e93b   Johannes Berg   netlink: policy: ...
236
  		return -ENOBUFS;
d07dcf9aa   Johannes Berg   netlink: add infr...
237
238
239
240
241
242
  
  	switch (pt->type) {
  	default:
  	case NLA_UNSPEC:
  	case NLA_REJECT:
  		/* skip - use NLA_MIN_LEN to advertise such */
d2681e93b   Johannes Berg   netlink: policy: ...
243
244
  		nla_nest_cancel(skb, attr);
  		return -ENODATA;
d07dcf9aa   Johannes Berg   netlink: add infr...
245
246
  	case NLA_NESTED:
  		type = NL_ATTR_TYPE_NESTED;
df561f668   Gustavo A. R. Silva   treewide: Use fal...
247
  		fallthrough;
d07dcf9aa   Johannes Berg   netlink: add infr...
248
249
250
  	case NLA_NESTED_ARRAY:
  		if (pt->type == NLA_NESTED_ARRAY)
  			type = NL_ATTR_TYPE_NESTED_ARRAY;
d2681e93b   Johannes Berg   netlink: policy: ...
251
  		if (state && pt->nested_policy && pt->len &&
d07dcf9aa   Johannes Berg   netlink: add infr...
252
  		    (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_POLICY_IDX,
04a351a62   Johannes Berg   netlink: rework p...
253
254
255
  				 netlink_policy_dump_get_policy_idx(state,
  								    pt->nested_policy,
  								    pt->len)) ||
d07dcf9aa   Johannes Berg   netlink: add infr...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  		     nla_put_u32(skb, NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE,
  				 pt->len)))
  			goto nla_put_failure;
  		break;
  	case NLA_U8:
  	case NLA_U16:
  	case NLA_U32:
  	case NLA_U64:
  	case NLA_MSECS: {
  		struct netlink_range_validation range;
  
  		if (pt->type == NLA_U8)
  			type = NL_ATTR_TYPE_U8;
  		else if (pt->type == NLA_U16)
  			type = NL_ATTR_TYPE_U16;
  		else if (pt->type == NLA_U32)
  			type = NL_ATTR_TYPE_U32;
  		else
  			type = NL_ATTR_TYPE_U64;
bdbb4e29d   Jakub Kicinski   netlink: add mask...
275
276
277
278
279
280
281
  		if (pt->validation_type == NLA_VALIDATE_MASK) {
  			if (nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MASK,
  					      pt->mask,
  					      NL_POLICY_TYPE_ATTR_PAD))
  				goto nla_put_failure;
  			break;
  		}
d07dcf9aa   Johannes Berg   netlink: add infr...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  		nla_get_range_unsigned(pt, &range);
  
  		if (nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MIN_VALUE_U,
  				      range.min, NL_POLICY_TYPE_ATTR_PAD) ||
  		    nla_put_u64_64bit(skb, NL_POLICY_TYPE_ATTR_MAX_VALUE_U,
  				      range.max, NL_POLICY_TYPE_ATTR_PAD))
  			goto nla_put_failure;
  		break;
  	}
  	case NLA_S8:
  	case NLA_S16:
  	case NLA_S32:
  	case NLA_S64: {
  		struct netlink_range_validation_signed range;
  
  		if (pt->type == NLA_S8)
  			type = NL_ATTR_TYPE_S8;
  		else if (pt->type == NLA_S16)
  			type = NL_ATTR_TYPE_S16;
  		else if (pt->type == NLA_S32)
  			type = NL_ATTR_TYPE_S32;
  		else
  			type = NL_ATTR_TYPE_S64;
  
  		nla_get_range_signed(pt, &range);
  
  		if (nla_put_s64(skb, NL_POLICY_TYPE_ATTR_MIN_VALUE_S,
  				range.min, NL_POLICY_TYPE_ATTR_PAD) ||
  		    nla_put_s64(skb, NL_POLICY_TYPE_ATTR_MAX_VALUE_S,
  				range.max, NL_POLICY_TYPE_ATTR_PAD))
  			goto nla_put_failure;
  		break;
  	}
  	case NLA_BITFIELD32:
  		type = NL_ATTR_TYPE_BITFIELD32;
  		if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_BITFIELD32_MASK,
  				pt->bitfield32_valid))
  			goto nla_put_failure;
  		break;
d07dcf9aa   Johannes Berg   netlink: add infr...
321
322
323
324
325
326
327
328
329
  	case NLA_STRING:
  	case NLA_NUL_STRING:
  	case NLA_BINARY:
  		if (pt->type == NLA_STRING)
  			type = NL_ATTR_TYPE_STRING;
  		else if (pt->type == NLA_NUL_STRING)
  			type = NL_ATTR_TYPE_NUL_STRING;
  		else
  			type = NL_ATTR_TYPE_BINARY;
8aa26c575   Johannes Berg   netlink: make NLA...
330

c30a3c957   Johannes Berg   netlink: policy: ...
331
332
  		if (pt->validation_type == NLA_VALIDATE_RANGE ||
  		    pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG) {
8aa26c575   Johannes Berg   netlink: make NLA...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  			struct netlink_range_validation range;
  
  			nla_get_range_unsigned(pt, &range);
  
  			if (range.min &&
  			    nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MIN_LENGTH,
  					range.min))
  				goto nla_put_failure;
  
  			if (range.max < U16_MAX &&
  			    nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MAX_LENGTH,
  					range.max))
  				goto nla_put_failure;
  		} else if (pt->len &&
  			   nla_put_u32(skb, NL_POLICY_TYPE_ATTR_MAX_LENGTH,
  				       pt->len)) {
d07dcf9aa   Johannes Berg   netlink: add infr...
349
  			goto nla_put_failure;
8aa26c575   Johannes Berg   netlink: make NLA...
350
  		}
d07dcf9aa   Johannes Berg   netlink: add infr...
351
352
353
354
355
356
357
358
  		break;
  	case NLA_FLAG:
  		type = NL_ATTR_TYPE_FLAG;
  		break;
  	}
  
  	if (nla_put_u32(skb, NL_POLICY_TYPE_ATTR_TYPE, type))
  		goto nla_put_failure;
d07dcf9aa   Johannes Berg   netlink: add infr...
359
  	nla_nest_end(skb, attr);
44f3625bc   Johannes Berg   netlink: export p...
360
  	WARN_ON(attr->nla_len > estimate);
d2681e93b   Johannes Berg   netlink: policy: ...
361
362
363
364
365
366
367
  	return 0;
  nla_put_failure:
  	nla_nest_cancel(skb, attr);
  	return -ENOBUFS;
  }
  
  /**
44f3625bc   Johannes Berg   netlink: export p...
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
   * netlink_policy_dump_write_attr - write a given attribute policy
   * @skb: the message skb to write to
   * @pt: the attribute's policy
   * @nestattr: the nested attribute ID to use
   *
   * Returns: 0 on success, an error code otherwise; -%ENODATA is
   *	    special, indicating that there's no policy data and
   *	    the attribute is generally rejected.
   */
  int netlink_policy_dump_write_attr(struct sk_buff *skb,
  				   const struct nla_policy *pt,
  				   int nestattr)
  {
  	return __netlink_policy_dump_write_attr(NULL, skb, pt, nestattr);
  }
  
  /**
d2681e93b   Johannes Berg   netlink: policy: ...
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
   * netlink_policy_dump_write - write current policy dump attributes
   * @skb: the message skb to write to
   * @state: the policy dump state
   *
   * Returns: 0 on success, an error code otherwise
   */
  int netlink_policy_dump_write(struct sk_buff *skb,
  			      struct netlink_policy_dump_state *state)
  {
  	const struct nla_policy *pt;
  	struct nlattr *policy;
  	bool again;
  	int err;
  
  send_attribute:
  	again = false;
  
  	pt = &state->policies[state->policy_idx].policy[state->attr_idx];
  
  	policy = nla_nest_start(skb, state->policy_idx);
  	if (!policy)
  		return -ENOBUFS;
  
  	err = __netlink_policy_dump_write_attr(state, skb, pt, state->attr_idx);
  	if (err == -ENODATA) {
  		nla_nest_cancel(skb, policy);
  		again = true;
  		goto next;
  	} else if (err) {
  		goto nla_put_failure;
  	}
  
  	/* finish and move state to next attribute */
d07dcf9aa   Johannes Berg   netlink: add infr...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  	nla_nest_end(skb, policy);
  
  next:
  	state->attr_idx += 1;
  	if (state->attr_idx > state->policies[state->policy_idx].maxtype) {
  		state->attr_idx = 0;
  		state->policy_idx++;
  	}
  
  	if (again) {
  		if (netlink_policy_dump_finished(state))
  			return -ENODATA;
  		goto send_attribute;
  	}
  
  	return 0;
  
  nla_put_failure:
  	nla_nest_cancel(skb, policy);
  	return -ENOBUFS;
  }
949ca6b82   Johannes Berg   netlink: fix poli...
439

04a351a62   Johannes Berg   netlink: rework p...
440
441
442
443
444
445
  /**
   * netlink_policy_dump_free - free policy dump state
   * @state: the policy dump state to free
   *
   * Call this from the done() method to ensure dump state is freed.
   */
adc848450   Jakub Kicinski   genetlink: add a ...
446
  void netlink_policy_dump_free(struct netlink_policy_dump_state *state)
949ca6b82   Johannes Berg   netlink: fix poli...
447
  {
949ca6b82   Johannes Berg   netlink: fix poli...
448
449
  	kfree(state);
  }