drbd_nla.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/kernel.h>
  3. #include <net/netlink.h>
  4. #include <linux/drbd_genl_api.h>
  5. #include "drbd_nla.h"
  6. static int drbd_nla_check_mandatory(int maxtype, struct nlattr *nla)
  7. {
  8. struct nlattr *head = nla_data(nla);
  9. int len = nla_len(nla);
  10. int rem;
  11. /*
  12. * validate_nla (called from nla_parse_nested) ignores attributes
  13. * beyond maxtype, and does not understand the DRBD_GENLA_F_MANDATORY flag.
  14. * In order to have it validate attributes with the DRBD_GENLA_F_MANDATORY
  15. * flag set also, check and remove that flag before calling
  16. * nla_parse_nested.
  17. */
  18. nla_for_each_attr(nla, head, len, rem) {
  19. if (nla->nla_type & DRBD_GENLA_F_MANDATORY) {
  20. nla->nla_type &= ~DRBD_GENLA_F_MANDATORY;
  21. if (nla_type(nla) > maxtype)
  22. return -EOPNOTSUPP;
  23. }
  24. }
  25. return 0;
  26. }
  27. int drbd_nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
  28. const struct nla_policy *policy)
  29. {
  30. int err;
  31. err = drbd_nla_check_mandatory(maxtype, nla);
  32. if (!err)
  33. err = nla_parse_nested_deprecated(tb, maxtype, nla, policy,
  34. NULL);
  35. return err;
  36. }
  37. struct nlattr *drbd_nla_find_nested(int maxtype, struct nlattr *nla, int attrtype)
  38. {
  39. int err;
  40. /*
  41. * If any nested attribute has the DRBD_GENLA_F_MANDATORY flag set and
  42. * we don't know about that attribute, reject all the nested
  43. * attributes.
  44. */
  45. err = drbd_nla_check_mandatory(maxtype, nla);
  46. if (err)
  47. return ERR_PTR(err);
  48. return nla_find_nested(nla, attrtype);
  49. }