.. | .. |
---|
1 | | -// SPDX-License-Identifier: LGPL-2.1 |
---|
| 1 | +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
---|
2 | 2 | |
---|
3 | 3 | /* |
---|
4 | 4 | * NETLINK Netlink attributes |
---|
5 | | - * |
---|
6 | | - * This library is free software; you can redistribute it and/or |
---|
7 | | - * modify it under the terms of the GNU Lesser General Public |
---|
8 | | - * License as published by the Free Software Foundation version 2.1 |
---|
9 | | - * of the License. |
---|
10 | 5 | * |
---|
11 | 6 | * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch> |
---|
12 | 7 | */ |
---|
13 | 8 | |
---|
14 | 9 | #include <errno.h> |
---|
15 | | -#include "nlattr.h" |
---|
16 | | -#include <linux/rtnetlink.h> |
---|
17 | 10 | #include <string.h> |
---|
18 | 11 | #include <stdio.h> |
---|
| 12 | +#include <linux/rtnetlink.h> |
---|
| 13 | +#include "nlattr.h" |
---|
| 14 | +#include "libbpf_internal.h" |
---|
19 | 15 | |
---|
20 | | -static uint16_t nla_attr_minlen[NLA_TYPE_MAX+1] = { |
---|
21 | | - [NLA_U8] = sizeof(uint8_t), |
---|
22 | | - [NLA_U16] = sizeof(uint16_t), |
---|
23 | | - [NLA_U32] = sizeof(uint32_t), |
---|
24 | | - [NLA_U64] = sizeof(uint64_t), |
---|
25 | | - [NLA_STRING] = 1, |
---|
26 | | - [NLA_FLAG] = 0, |
---|
| 16 | +static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = { |
---|
| 17 | + [LIBBPF_NLA_U8] = sizeof(uint8_t), |
---|
| 18 | + [LIBBPF_NLA_U16] = sizeof(uint16_t), |
---|
| 19 | + [LIBBPF_NLA_U32] = sizeof(uint32_t), |
---|
| 20 | + [LIBBPF_NLA_U64] = sizeof(uint64_t), |
---|
| 21 | + [LIBBPF_NLA_STRING] = 1, |
---|
| 22 | + [LIBBPF_NLA_FLAG] = 0, |
---|
27 | 23 | }; |
---|
28 | | - |
---|
29 | | -static int nla_len(const struct nlattr *nla) |
---|
30 | | -{ |
---|
31 | | - return nla->nla_len - NLA_HDRLEN; |
---|
32 | | -} |
---|
33 | 24 | |
---|
34 | 25 | static struct nlattr *nla_next(const struct nlattr *nla, int *remaining) |
---|
35 | 26 | { |
---|
.. | .. |
---|
46 | 37 | nla->nla_len <= remaining; |
---|
47 | 38 | } |
---|
48 | 39 | |
---|
49 | | -static void *nla_data(const struct nlattr *nla) |
---|
50 | | -{ |
---|
51 | | - return (char *) nla + NLA_HDRLEN; |
---|
52 | | -} |
---|
53 | | - |
---|
54 | 40 | static int nla_type(const struct nlattr *nla) |
---|
55 | 41 | { |
---|
56 | 42 | return nla->nla_type & NLA_TYPE_MASK; |
---|
57 | 43 | } |
---|
58 | 44 | |
---|
59 | 45 | static int validate_nla(struct nlattr *nla, int maxtype, |
---|
60 | | - struct nla_policy *policy) |
---|
| 46 | + struct libbpf_nla_policy *policy) |
---|
61 | 47 | { |
---|
62 | | - struct nla_policy *pt; |
---|
| 48 | + struct libbpf_nla_policy *pt; |
---|
63 | 49 | unsigned int minlen = 0; |
---|
64 | 50 | int type = nla_type(nla); |
---|
65 | 51 | |
---|
.. | .. |
---|
68 | 54 | |
---|
69 | 55 | pt = &policy[type]; |
---|
70 | 56 | |
---|
71 | | - if (pt->type > NLA_TYPE_MAX) |
---|
| 57 | + if (pt->type > LIBBPF_NLA_TYPE_MAX) |
---|
72 | 58 | return 0; |
---|
73 | 59 | |
---|
74 | 60 | if (pt->minlen) |
---|
75 | 61 | minlen = pt->minlen; |
---|
76 | | - else if (pt->type != NLA_UNSPEC) |
---|
| 62 | + else if (pt->type != LIBBPF_NLA_UNSPEC) |
---|
77 | 63 | minlen = nla_attr_minlen[pt->type]; |
---|
78 | 64 | |
---|
79 | | - if (nla_len(nla) < minlen) |
---|
| 65 | + if (libbpf_nla_len(nla) < minlen) |
---|
80 | 66 | return -1; |
---|
81 | 67 | |
---|
82 | | - if (pt->maxlen && nla_len(nla) > pt->maxlen) |
---|
| 68 | + if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen) |
---|
83 | 69 | return -1; |
---|
84 | 70 | |
---|
85 | | - if (pt->type == NLA_STRING) { |
---|
86 | | - char *data = nla_data(nla); |
---|
87 | | - if (data[nla_len(nla) - 1] != '\0') |
---|
| 71 | + if (pt->type == LIBBPF_NLA_STRING) { |
---|
| 72 | + char *data = libbpf_nla_data(nla); |
---|
| 73 | + |
---|
| 74 | + if (data[libbpf_nla_len(nla) - 1] != '\0') |
---|
88 | 75 | return -1; |
---|
89 | 76 | } |
---|
90 | 77 | |
---|
.. | .. |
---|
114 | 101 | * @see nla_validate |
---|
115 | 102 | * @return 0 on success or a negative error code. |
---|
116 | 103 | */ |
---|
117 | | -static int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, |
---|
118 | | - struct nla_policy *policy) |
---|
| 104 | +int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, |
---|
| 105 | + int len, struct libbpf_nla_policy *policy) |
---|
119 | 106 | { |
---|
120 | 107 | struct nlattr *nla; |
---|
121 | 108 | int rem, err; |
---|
122 | 109 | |
---|
123 | 110 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); |
---|
124 | 111 | |
---|
125 | | - nla_for_each_attr(nla, head, len, rem) { |
---|
| 112 | + libbpf_nla_for_each_attr(nla, head, len, rem) { |
---|
126 | 113 | int type = nla_type(nla); |
---|
127 | 114 | |
---|
128 | 115 | if (type > maxtype) |
---|
.. | .. |
---|
135 | 122 | } |
---|
136 | 123 | |
---|
137 | 124 | if (tb[type]) |
---|
138 | | - fprintf(stderr, "Attribute of type %#x found multiple times in message, " |
---|
139 | | - "previous attribute is being ignored.\n", type); |
---|
| 125 | + pr_warn("Attribute of type %#x found multiple times in message, " |
---|
| 126 | + "previous attribute is being ignored.\n", type); |
---|
140 | 127 | |
---|
141 | 128 | tb[type] = nla; |
---|
142 | 129 | } |
---|
.. | .. |
---|
146 | 133 | return err; |
---|
147 | 134 | } |
---|
148 | 135 | |
---|
149 | | -/* dump netlink extended ack error message */ |
---|
150 | | -int nla_dump_errormsg(struct nlmsghdr *nlh) |
---|
| 136 | +/** |
---|
| 137 | + * Create attribute index based on nested attribute |
---|
| 138 | + * @arg tb Index array to be filled (maxtype+1 elements). |
---|
| 139 | + * @arg maxtype Maximum attribute type expected and accepted. |
---|
| 140 | + * @arg nla Nested Attribute. |
---|
| 141 | + * @arg policy Attribute validation policy. |
---|
| 142 | + * |
---|
| 143 | + * Feeds the stream of attributes nested into the specified attribute |
---|
| 144 | + * to libbpf_nla_parse(). |
---|
| 145 | + * |
---|
| 146 | + * @see libbpf_nla_parse |
---|
| 147 | + * @return 0 on success or a negative error code. |
---|
| 148 | + */ |
---|
| 149 | +int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype, |
---|
| 150 | + struct nlattr *nla, |
---|
| 151 | + struct libbpf_nla_policy *policy) |
---|
151 | 152 | { |
---|
152 | | - struct nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = { |
---|
153 | | - [NLMSGERR_ATTR_MSG] = { .type = NLA_STRING }, |
---|
154 | | - [NLMSGERR_ATTR_OFFS] = { .type = NLA_U32 }, |
---|
| 153 | + return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla), |
---|
| 154 | + libbpf_nla_len(nla), policy); |
---|
| 155 | +} |
---|
| 156 | + |
---|
| 157 | +/* dump netlink extended ack error message */ |
---|
| 158 | +int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh) |
---|
| 159 | +{ |
---|
| 160 | + struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = { |
---|
| 161 | + [NLMSGERR_ATTR_MSG] = { .type = LIBBPF_NLA_STRING }, |
---|
| 162 | + [NLMSGERR_ATTR_OFFS] = { .type = LIBBPF_NLA_U32 }, |
---|
155 | 163 | }; |
---|
156 | 164 | struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr; |
---|
157 | 165 | struct nlmsgerr *err; |
---|
.. | .. |
---|
170 | 178 | hlen += nlmsg_len(&err->msg); |
---|
171 | 179 | |
---|
172 | 180 | attr = (struct nlattr *) ((void *) err + hlen); |
---|
173 | | - alen = nlh->nlmsg_len - hlen; |
---|
| 181 | + alen = (void *)nlh + nlh->nlmsg_len - (void *)attr; |
---|
174 | 182 | |
---|
175 | | - if (nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen, extack_policy) != 0) { |
---|
176 | | - fprintf(stderr, |
---|
177 | | - "Failed to parse extended error attributes\n"); |
---|
| 183 | + if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen, |
---|
| 184 | + extack_policy) != 0) { |
---|
| 185 | + pr_warn("Failed to parse extended error attributes\n"); |
---|
178 | 186 | return 0; |
---|
179 | 187 | } |
---|
180 | 188 | |
---|
181 | 189 | if (tb[NLMSGERR_ATTR_MSG]) |
---|
182 | | - errmsg = (char *) nla_data(tb[NLMSGERR_ATTR_MSG]); |
---|
| 190 | + errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]); |
---|
183 | 191 | |
---|
184 | | - fprintf(stderr, "Kernel error message: %s\n", errmsg); |
---|
| 192 | + pr_warn("Kernel error message: %s\n", errmsg); |
---|
185 | 193 | |
---|
186 | 194 | return 0; |
---|
187 | 195 | } |
---|