David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * NETLINK Netlink attributes |
| 5 | * |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6 | * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch> |
| 7 | */ |
| 8 | |
| 9 | #include <errno.h> |
| 10 | #include "nlattr.h" |
| 11 | #include <linux/rtnetlink.h> |
| 12 | #include <string.h> |
| 13 | #include <stdio.h> |
| 14 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 15 | static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = { |
| 16 | [LIBBPF_NLA_U8] = sizeof(uint8_t), |
| 17 | [LIBBPF_NLA_U16] = sizeof(uint16_t), |
| 18 | [LIBBPF_NLA_U32] = sizeof(uint32_t), |
| 19 | [LIBBPF_NLA_U64] = sizeof(uint64_t), |
| 20 | [LIBBPF_NLA_STRING] = 1, |
| 21 | [LIBBPF_NLA_FLAG] = 0, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | }; |
| 23 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 24 | static struct nlattr *nla_next(const struct nlattr *nla, int *remaining) |
| 25 | { |
| 26 | int totlen = NLA_ALIGN(nla->nla_len); |
| 27 | |
| 28 | *remaining -= totlen; |
| 29 | return (struct nlattr *) ((char *) nla + totlen); |
| 30 | } |
| 31 | |
| 32 | static int nla_ok(const struct nlattr *nla, int remaining) |
| 33 | { |
| 34 | return remaining >= sizeof(*nla) && |
| 35 | nla->nla_len >= sizeof(*nla) && |
| 36 | nla->nla_len <= remaining; |
| 37 | } |
| 38 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 39 | static int nla_type(const struct nlattr *nla) |
| 40 | { |
| 41 | return nla->nla_type & NLA_TYPE_MASK; |
| 42 | } |
| 43 | |
| 44 | static int validate_nla(struct nlattr *nla, int maxtype, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 45 | struct libbpf_nla_policy *policy) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 46 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 47 | struct libbpf_nla_policy *pt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 48 | unsigned int minlen = 0; |
| 49 | int type = nla_type(nla); |
| 50 | |
| 51 | if (type < 0 || type > maxtype) |
| 52 | return 0; |
| 53 | |
| 54 | pt = &policy[type]; |
| 55 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 56 | if (pt->type > LIBBPF_NLA_TYPE_MAX) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 57 | return 0; |
| 58 | |
| 59 | if (pt->minlen) |
| 60 | minlen = pt->minlen; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 61 | else if (pt->type != LIBBPF_NLA_UNSPEC) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 62 | minlen = nla_attr_minlen[pt->type]; |
| 63 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 64 | if (libbpf_nla_len(nla) < minlen) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 65 | return -1; |
| 66 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 67 | if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 68 | return -1; |
| 69 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 70 | if (pt->type == LIBBPF_NLA_STRING) { |
| 71 | char *data = libbpf_nla_data(nla); |
| 72 | |
| 73 | if (data[libbpf_nla_len(nla) - 1] != '\0') |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static inline int nlmsg_len(const struct nlmsghdr *nlh) |
| 81 | { |
| 82 | return nlh->nlmsg_len - NLMSG_HDRLEN; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Create attribute index based on a stream of attributes. |
| 87 | * @arg tb Index array to be filled (maxtype+1 elements). |
| 88 | * @arg maxtype Maximum attribute type expected and accepted. |
| 89 | * @arg head Head of attribute stream. |
| 90 | * @arg len Length of attribute stream. |
| 91 | * @arg policy Attribute validation policy. |
| 92 | * |
| 93 | * Iterates over the stream of attributes and stores a pointer to each |
| 94 | * attribute in the index array using the attribute type as index to |
| 95 | * the array. Attribute with a type greater than the maximum type |
| 96 | * specified will be silently ignored in order to maintain backwards |
| 97 | * compatibility. If \a policy is not NULL, the attribute will be |
| 98 | * validated using the specified policy. |
| 99 | * |
| 100 | * @see nla_validate |
| 101 | * @return 0 on success or a negative error code. |
| 102 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 103 | int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, |
| 104 | int len, struct libbpf_nla_policy *policy) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 105 | { |
| 106 | struct nlattr *nla; |
| 107 | int rem, err; |
| 108 | |
| 109 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); |
| 110 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 111 | libbpf_nla_for_each_attr(nla, head, len, rem) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 112 | int type = nla_type(nla); |
| 113 | |
| 114 | if (type > maxtype) |
| 115 | continue; |
| 116 | |
| 117 | if (policy) { |
| 118 | err = validate_nla(nla, maxtype, policy); |
| 119 | if (err < 0) |
| 120 | goto errout; |
| 121 | } |
| 122 | |
| 123 | if (tb[type]) |
| 124 | fprintf(stderr, "Attribute of type %#x found multiple times in message, " |
| 125 | "previous attribute is being ignored.\n", type); |
| 126 | |
| 127 | tb[type] = nla; |
| 128 | } |
| 129 | |
| 130 | err = 0; |
| 131 | errout: |
| 132 | return err; |
| 133 | } |
| 134 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 135 | /** |
| 136 | * Create attribute index based on nested attribute |
| 137 | * @arg tb Index array to be filled (maxtype+1 elements). |
| 138 | * @arg maxtype Maximum attribute type expected and accepted. |
| 139 | * @arg nla Nested Attribute. |
| 140 | * @arg policy Attribute validation policy. |
| 141 | * |
| 142 | * Feeds the stream of attributes nested into the specified attribute |
| 143 | * to libbpf_nla_parse(). |
| 144 | * |
| 145 | * @see libbpf_nla_parse |
| 146 | * @return 0 on success or a negative error code. |
| 147 | */ |
| 148 | int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype, |
| 149 | struct nlattr *nla, |
| 150 | struct libbpf_nla_policy *policy) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 151 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 152 | return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla), |
| 153 | libbpf_nla_len(nla), policy); |
| 154 | } |
| 155 | |
| 156 | /* dump netlink extended ack error message */ |
| 157 | int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh) |
| 158 | { |
| 159 | struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = { |
| 160 | [NLMSGERR_ATTR_MSG] = { .type = LIBBPF_NLA_STRING }, |
| 161 | [NLMSGERR_ATTR_OFFS] = { .type = LIBBPF_NLA_U32 }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 162 | }; |
| 163 | struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr; |
| 164 | struct nlmsgerr *err; |
| 165 | char *errmsg = NULL; |
| 166 | int hlen, alen; |
| 167 | |
| 168 | /* no TLVs, nothing to do here */ |
| 169 | if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS)) |
| 170 | return 0; |
| 171 | |
| 172 | err = (struct nlmsgerr *)NLMSG_DATA(nlh); |
| 173 | hlen = sizeof(*err); |
| 174 | |
| 175 | /* if NLM_F_CAPPED is set then the inner err msg was capped */ |
| 176 | if (!(nlh->nlmsg_flags & NLM_F_CAPPED)) |
| 177 | hlen += nlmsg_len(&err->msg); |
| 178 | |
| 179 | attr = (struct nlattr *) ((void *) err + hlen); |
| 180 | alen = nlh->nlmsg_len - hlen; |
| 181 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 182 | if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen, |
| 183 | extack_policy) != 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 184 | fprintf(stderr, |
| 185 | "Failed to parse extended error attributes\n"); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | if (tb[NLMSGERR_ATTR_MSG]) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 190 | errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 191 | |
| 192 | fprintf(stderr, "Kernel error message: %s\n", errmsg); |
| 193 | |
| 194 | return 0; |
| 195 | } |