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