^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * NETLINK Netlink attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors: Thomas Graf <tgraf@suug.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* For these data types, attribute length should be exactly the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * size. However, to maintain compatibility with broken commands, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * attribute length does not match the expected size a warning is emitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * to the user that the command is sending invalid data and needs to be fixed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) [NLA_U8] = sizeof(u8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) [NLA_U16] = sizeof(u16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) [NLA_U32] = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) [NLA_U64] = sizeof(u64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) [NLA_S8] = sizeof(s8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) [NLA_S16] = sizeof(s16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) [NLA_S32] = sizeof(s32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) [NLA_S64] = sizeof(s64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) [NLA_U8] = sizeof(u8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) [NLA_U16] = sizeof(u16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) [NLA_U32] = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) [NLA_U64] = sizeof(u64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) [NLA_MSECS] = sizeof(u64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) [NLA_NESTED] = NLA_HDRLEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) [NLA_S8] = sizeof(s8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) [NLA_S16] = sizeof(s16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) [NLA_S32] = sizeof(s32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) [NLA_S64] = sizeof(s64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Nested policies might refer back to the original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * policy in some cases, and userspace could try to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * abuse that and recurse by nesting in the right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * ways. Limit recursion to avoid this problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define MAX_POLICY_RECURSION_DEPTH 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) const struct nla_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) unsigned int validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct netlink_ext_ack *extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct nlattr **tb, unsigned int depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int validate_nla_bitfield32(const struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const u32 valid_flags_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const struct nla_bitfield32 *bf = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!valid_flags_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*disallow invalid bit selector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (bf->selector & ~valid_flags_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /*disallow invalid bit values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (bf->value & ~valid_flags_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /*disallow valid bit values that are not selected*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (bf->value & ~bf->selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) const struct nla_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct netlink_ext_ack *extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned int validate, unsigned int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) const struct nlattr *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) nla_for_each_attr(entry, head, len, rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (nla_len(entry) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (nla_len(entry) < NLA_HDRLEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) NL_SET_ERR_MSG_ATTR_POL(extack, entry, policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) "Array element too short");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ret = __nla_validate_parse(nla_data(entry), nla_len(entry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) maxtype, policy, validate, extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) NULL, depth + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) void nla_get_range_unsigned(const struct nla_policy *pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct netlink_range_validation *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) (pt->min < 0 || pt->max < 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) range->min = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) switch (pt->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) case NLA_U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) range->max = U8_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) case NLA_U16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) case NLA_BINARY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) range->max = U16_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) case NLA_U32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) range->max = U32_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) case NLA_U64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) case NLA_MSECS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) range->max = U64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) switch (pt->validation_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) case NLA_VALIDATE_RANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) range->min = pt->min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) range->max = pt->max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) case NLA_VALIDATE_RANGE_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) *range = *pt->range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) case NLA_VALIDATE_MIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) range->min = pt->min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) case NLA_VALIDATE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) range->max = pt->max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int nla_validate_range_unsigned(const struct nla_policy *pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) const struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct netlink_ext_ack *extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned int validate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct netlink_range_validation range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) u64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) switch (pt->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) case NLA_U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) value = nla_get_u8(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) case NLA_U16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) value = nla_get_u16(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) case NLA_U32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) value = nla_get_u32(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) case NLA_U64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) case NLA_MSECS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) value = nla_get_u64(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) case NLA_BINARY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) value = nla_len(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) nla_get_range_unsigned(pt, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) pt->type == NLA_BINARY && value > range.max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) current->comm, pt->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (validate & NL_VALIDATE_STRICT_ATTRS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) "invalid attribute length");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* this assumes min <= max (don't validate against min) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (value < range.min || value > range.max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) bool binary = pt->type == NLA_BINARY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (binary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) "binary attribute size out of range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) "integer out of range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) void nla_get_range_signed(const struct nla_policy *pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct netlink_range_validation_signed *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) switch (pt->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) case NLA_S8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) range->min = S8_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) range->max = S8_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) case NLA_S16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) range->min = S16_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) range->max = S16_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) case NLA_S32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) range->min = S32_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) range->max = S32_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) case NLA_S64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) range->min = S64_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) range->max = S64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) switch (pt->validation_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) case NLA_VALIDATE_RANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) range->min = pt->min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) range->max = pt->max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) case NLA_VALIDATE_RANGE_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *range = *pt->range_signed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) case NLA_VALIDATE_MIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) range->min = pt->min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) case NLA_VALIDATE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) range->max = pt->max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static int nla_validate_int_range_signed(const struct nla_policy *pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) const struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) struct netlink_range_validation_signed range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) s64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) switch (pt->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) case NLA_S8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) value = nla_get_s8(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) case NLA_S16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) value = nla_get_s16(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) case NLA_S32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) value = nla_get_s32(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) case NLA_S64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) value = nla_get_s64(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) nla_get_range_signed(pt, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (value < range.min || value > range.max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) "integer out of range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int nla_validate_int_range(const struct nla_policy *pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) const struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct netlink_ext_ack *extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) unsigned int validate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) switch (pt->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) case NLA_U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) case NLA_U16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) case NLA_U32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) case NLA_U64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) case NLA_MSECS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) case NLA_BINARY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return nla_validate_range_unsigned(pt, nla, extack, validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) case NLA_S8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) case NLA_S16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) case NLA_S32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) case NLA_S64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return nla_validate_int_range_signed(pt, nla, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int nla_validate_mask(const struct nla_policy *pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) const struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) u64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) switch (pt->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) case NLA_U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) value = nla_get_u8(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) case NLA_U16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) value = nla_get_u16(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) case NLA_U32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) value = nla_get_u32(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) case NLA_U64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) value = nla_get_u64(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (value & ~(u64)pt->mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static int validate_nla(const struct nlattr *nla, int maxtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) const struct nla_policy *policy, unsigned int validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct netlink_ext_ack *extack, unsigned int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) u16 strict_start_type = policy[0].strict_start_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) const struct nla_policy *pt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int err = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (strict_start_type && type >= strict_start_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) validate |= NL_VALIDATE_STRICT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (type <= 0 || type > maxtype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) pt = &policy[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) BUG_ON(pt->type > NLA_TYPE_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) current->comm, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (validate & NL_VALIDATE_STRICT_ATTRS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) "invalid attribute length");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (validate & NL_VALIDATE_NESTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) !(nla->nla_type & NLA_F_NESTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) "NLA_F_NESTED is missing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) "NLA_F_NESTED not expected");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) switch (pt->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) case NLA_REJECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (extack && pt->reject_message) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) NL_SET_BAD_ATTR(extack, nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) extack->_msg = pt->reject_message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) case NLA_FLAG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (attrlen > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) case NLA_BITFIELD32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (attrlen != sizeof(struct nla_bitfield32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) err = validate_nla_bitfield32(nla, pt->bitfield32_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) case NLA_NUL_STRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (pt->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) minlen = min_t(int, attrlen, pt->len + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) minlen = attrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) case NLA_STRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (attrlen < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (pt->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) char *buf = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (buf[attrlen - 1] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) attrlen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (attrlen > pt->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) case NLA_BINARY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (pt->len && attrlen > pt->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) case NLA_NESTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) /* a nested attributes is allowed to be empty; if its not,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * it must have a size of at least NLA_HDRLEN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (attrlen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (attrlen < NLA_HDRLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (pt->nested_policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) err = __nla_validate_parse(nla_data(nla), nla_len(nla),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) pt->len, pt->nested_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) validate, extack, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) depth + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * return directly to preserve the inner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * error message/attribute pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) case NLA_NESTED_ARRAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* a nested array attribute is allowed to be empty; if its not,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * it must have a size of at least NLA_HDRLEN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (attrlen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (attrlen < NLA_HDRLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (pt->nested_policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) err = nla_validate_array(nla_data(nla), nla_len(nla),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) pt->len, pt->nested_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) extack, validate, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * return directly to preserve the inner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * error message/attribute pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) case NLA_UNSPEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (validate & NL_VALIDATE_UNSPEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) NL_SET_ERR_MSG_ATTR(extack, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) "Unsupported attribute");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (attrlen < pt->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (pt->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) minlen = pt->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) minlen = nla_attr_minlen[pt->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (attrlen < minlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /* further validation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) switch (pt->validation_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) case NLA_VALIDATE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) /* nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) case NLA_VALIDATE_RANGE_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) case NLA_VALIDATE_RANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) case NLA_VALIDATE_MIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) case NLA_VALIDATE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) err = nla_validate_int_range(pt, nla, extack, validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) case NLA_VALIDATE_MASK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) err = nla_validate_mask(pt, nla, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) case NLA_VALIDATE_FUNCTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (pt->validate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) err = pt->validate(nla, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) "Attribute failed policy validation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) const struct nla_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) unsigned int validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) struct netlink_ext_ack *extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) struct nlattr **tb, unsigned int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) const struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) int rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (depth >= MAX_POLICY_RECURSION_DEPTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) "allowed policy recursion depth exceeded");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) nla_for_each_attr(nla, head, len, rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) u16 type = nla_type(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (type == 0 || type > maxtype) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (validate & NL_VALIDATE_MAXTYPE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) NL_SET_ERR_MSG_ATTR(extack, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) "Unknown attribute type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) int err = validate_nla(nla, maxtype, policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) validate, extack, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) tb[type] = (struct nlattr *)nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (unlikely(rem > 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) rem, current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (validate & NL_VALIDATE_TRAILING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * __nla_validate - Validate a stream of attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) * @head: head of attribute stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * @len: length of attribute stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * @maxtype: maximum attribute type to be expected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * @policy: validation policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * @validate: validation strictness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * @extack: extended ACK report struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * Validates all attributes in the specified attribute stream against the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) * specified policy. Validation depends on the validate flags passed, see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * &enum netlink_validation for more details on that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * See documenation of struct nla_policy for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * Returns 0 on success or a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) int __nla_validate(const struct nlattr *head, int len, int maxtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) const struct nla_policy *policy, unsigned int validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) return __nla_validate_parse(head, len, maxtype, policy, validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) extack, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) EXPORT_SYMBOL(__nla_validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * nla_policy_len - Determin the max. length of a policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * @policy: policy to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * @n: number of policies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * Determines the max. length of the policy. It is currently used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * to allocated Netlink buffers roughly the size of the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * Returns 0 on success or a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) nla_policy_len(const struct nla_policy *p, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) int i, len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) for (i = 0; i < n; i++, p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (p->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) len += nla_total_size(p->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) else if (nla_attr_len[p->type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) len += nla_total_size(nla_attr_len[p->type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) else if (nla_attr_minlen[p->type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) len += nla_total_size(nla_attr_minlen[p->type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) EXPORT_SYMBOL(nla_policy_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * __nla_parse - Parse a stream of attributes into a tb buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * @tb: destination array with maxtype+1 elements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) * @maxtype: maximum attribute type to be expected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * @head: head of attribute stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * @len: length of attribute stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) * @policy: validation policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * @validate: validation strictness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * @extack: extended ACK pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * Parses a stream of attributes and stores a pointer to each attribute in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * the tb array accessible via the attribute type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * Validation is controlled by the @validate parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * Returns 0 on success or a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) int __nla_parse(struct nlattr **tb, int maxtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) const struct nlattr *head, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) const struct nla_policy *policy, unsigned int validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) return __nla_validate_parse(head, len, maxtype, policy, validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) extack, tb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) EXPORT_SYMBOL(__nla_parse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * nla_find - Find a specific attribute in a stream of attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * @head: head of attribute stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * @len: length of attribute stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * @attrtype: type of attribute to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * Returns the first attribute in the stream matching the specified type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) const struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) int rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) nla_for_each_attr(nla, head, len, rem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (nla_type(nla) == attrtype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return (struct nlattr *)nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) EXPORT_SYMBOL(nla_find);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * nla_strlcpy - Copy string attribute payload into a sized buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * @dst: where to copy the string to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * @nla: attribute to copy the string from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * @dstsize: size of destination buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * Copies at most dstsize - 1 bytes into the destination buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * The result is always a valid NUL-terminated string. Unlike
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * strlcpy the destination buffer is always padded out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * Returns the length of the source buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) size_t srclen = nla_len(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) char *src = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (srclen > 0 && src[srclen - 1] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) srclen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (dstsize > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) memset(dst, 0, dstsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) memcpy(dst, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) return srclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) EXPORT_SYMBOL(nla_strlcpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) * nla_strdup - Copy string attribute payload into a newly allocated buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) * @nla: attribute to copy the string from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) * @flags: the type of memory to allocate (see kmalloc).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) * Returns a pointer to the allocated buffer or NULL on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) char *nla_strdup(const struct nlattr *nla, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) size_t srclen = nla_len(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) char *src = nla_data(nla), *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) if (srclen > 0 && src[srclen - 1] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) srclen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) dst = kmalloc(srclen + 1, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (dst != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) memcpy(dst, src, srclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) dst[srclen] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) EXPORT_SYMBOL(nla_strdup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * nla_memcpy - Copy a netlink attribute into another memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * @dest: where to copy to memcpy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * @src: netlink attribute to copy from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * @count: size of the destination area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * Note: The number of bytes copied is limited by the length of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * attribute's payload. memcpy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * Returns the number of bytes copied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) int nla_memcpy(void *dest, const struct nlattr *src, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) int minlen = min_t(int, count, nla_len(src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) memcpy(dest, nla_data(src), minlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (count > minlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) memset(dest + minlen, 0, count - minlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return minlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) EXPORT_SYMBOL(nla_memcpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * nla_memcmp - Compare an attribute with sized memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * @nla: netlink attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) * @data: memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * @size: size of memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) int nla_memcmp(const struct nlattr *nla, const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) int d = nla_len(nla) - size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) if (d == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) d = memcmp(nla_data(nla), data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) EXPORT_SYMBOL(nla_memcmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * nla_strcmp - Compare a string attribute against a string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * @nla: netlink string attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * @str: another string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) int nla_strcmp(const struct nlattr *nla, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) int len = strlen(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) char *buf = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) int attrlen = nla_len(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) int d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) while (attrlen > 0 && buf[attrlen - 1] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) attrlen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) d = attrlen - len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if (d == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) d = memcmp(nla_data(nla), str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) EXPORT_SYMBOL(nla_strcmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) #ifdef CONFIG_NET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * __nla_reserve - reserve room for attribute on the skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * @skb: socket buffer to reserve room on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * @attrtype: attribute type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * Adds a netlink attribute header to a socket buffer and reserves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) * room for the payload but does not copy it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) * The caller is responsible to ensure that the skb provides enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) * tailroom for the attribute header and payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) nla = skb_put(skb, nla_total_size(attrlen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) nla->nla_type = attrtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) nla->nla_len = nla_attr_size(attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) return nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) EXPORT_SYMBOL(__nla_reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * __nla_reserve_64bit - reserve room for attribute on the skb and align it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) * @skb: socket buffer to reserve room on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) * @attrtype: attribute type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * @padattr: attribute type for the padding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * Adds a netlink attribute header to a socket buffer and reserves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * room for the payload but does not copy it. It also ensure that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * attribute will have a 64-bit aligned nla_data() area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * The caller is responsible to ensure that the skb provides enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * tailroom for the attribute header and payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) int attrlen, int padattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) nla_align_64bit(skb, padattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) return __nla_reserve(skb, attrtype, attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) EXPORT_SYMBOL(__nla_reserve_64bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * __nla_reserve_nohdr - reserve room for attribute without header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * @skb: socket buffer to reserve room on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) * Reserves room for attribute payload without a header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * The caller is responsible to ensure that the skb provides enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * tailroom for the payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) return skb_put_zero(skb, NLA_ALIGN(attrlen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) EXPORT_SYMBOL(__nla_reserve_nohdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * nla_reserve - reserve room for attribute on the skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * @skb: socket buffer to reserve room on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) * @attrtype: attribute type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) * Adds a netlink attribute header to a socket buffer and reserves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * room for the payload but does not copy it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) * Returns NULL if the tailroom of the skb is insufficient to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) * the attribute header and payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return __nla_reserve(skb, attrtype, attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) EXPORT_SYMBOL(nla_reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) * nla_reserve_64bit - reserve room for attribute on the skb and align it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) * @skb: socket buffer to reserve room on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) * @attrtype: attribute type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) * @padattr: attribute type for the padding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) * Adds a netlink attribute header to a socket buffer and reserves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) * room for the payload but does not copy it. It also ensure that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) * attribute will have a 64-bit aligned nla_data() area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) * Returns NULL if the tailroom of the skb is insufficient to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) * the attribute header and payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) int padattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) if (nla_need_padding_for_64bit(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) len = nla_total_size_64bit(attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) len = nla_total_size(attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (unlikely(skb_tailroom(skb) < len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) EXPORT_SYMBOL(nla_reserve_64bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * nla_reserve_nohdr - reserve room for attribute without header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) * @skb: socket buffer to reserve room on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * Reserves room for attribute payload without a header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * Returns NULL if the tailroom of the skb is insufficient to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) * the attribute payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) return __nla_reserve_nohdr(skb, attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) EXPORT_SYMBOL(nla_reserve_nohdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * __nla_put - Add a netlink attribute to a socket buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) * @skb: socket buffer to add attribute to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) * @attrtype: attribute type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) * @data: head of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) * The caller is responsible to ensure that the skb provides enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) * tailroom for the attribute header and payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) nla = __nla_reserve(skb, attrtype, attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) memcpy(nla_data(nla), data, attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) EXPORT_SYMBOL(__nla_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) * @skb: socket buffer to add attribute to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * @attrtype: attribute type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) * @data: head of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * @padattr: attribute type for the padding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) * The caller is responsible to ensure that the skb provides enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * tailroom for the attribute header and payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) const void *data, int padattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) memcpy(nla_data(nla), data, attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) EXPORT_SYMBOL(__nla_put_64bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * __nla_put_nohdr - Add a netlink attribute without header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * @skb: socket buffer to add attribute to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) * @data: head of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) * The caller is responsible to ensure that the skb provides enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) * tailroom for the attribute payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) void *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) start = __nla_reserve_nohdr(skb, attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) memcpy(start, data, attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) EXPORT_SYMBOL(__nla_put_nohdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * nla_put - Add a netlink attribute to a socket buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * @skb: socket buffer to add attribute to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * @attrtype: attribute type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) * @data: head of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) * the attribute header and payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) __nla_put(skb, attrtype, attrlen, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) EXPORT_SYMBOL(nla_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) * @skb: socket buffer to add attribute to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * @attrtype: attribute type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) * @data: head of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) * @padattr: attribute type for the padding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * the attribute header and payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) const void *data, int padattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (nla_need_padding_for_64bit(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) len = nla_total_size_64bit(attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) len = nla_total_size(attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) if (unlikely(skb_tailroom(skb) < len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) EXPORT_SYMBOL(nla_put_64bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) * nla_put_nohdr - Add a netlink attribute without header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * @skb: socket buffer to add attribute to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) * @data: head of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) * the attribute payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) __nla_put_nohdr(skb, attrlen, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) EXPORT_SYMBOL(nla_put_nohdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) * nla_append - Add a netlink attribute without header or padding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) * @skb: socket buffer to add attribute to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) * @attrlen: length of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) * @data: head of attribute payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * the attribute payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) int nla_append(struct sk_buff *skb, int attrlen, const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) skb_put_data(skb, data, attrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) EXPORT_SYMBOL(nla_append);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) #endif