policy.c 11 KB

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