drbd_nla.c 1.4 KB

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