drbd_nla.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "drbd_wrappers.h"
  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(tb, maxtype, nla, policy);
  34. return err;
  35. }
  36. struct nlattr *drbd_nla_find_nested(int maxtype, struct nlattr *nla, int attrtype)
  37. {
  38. int err;
  39. /*
  40. * If any nested attribute has the DRBD_GENLA_F_MANDATORY flag set and
  41. * we don't know about that attribute, reject all the nested
  42. * attributes.
  43. */
  44. err = drbd_nla_check_mandatory(maxtype, nla);
  45. if (err)
  46. return ERR_PTR(err);
  47. return nla_find_nested(nla, attrtype);
  48. }