This source file includes following definitions.
- nla_next
- nla_ok
- nla_type
- validate_nla
- nlmsg_len
- libbpf_nla_parse
- libbpf_nla_parse_nested
- libbpf_nla_dump_errormsg
1
2
3
4
5
6
7
8
9 #include <errno.h>
10 #include "nlattr.h"
11 #include <linux/rtnetlink.h>
12 #include <string.h>
13 #include <stdio.h>
14
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,
22 };
23
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
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,
45 struct libbpf_nla_policy *policy)
46 {
47 struct libbpf_nla_policy *pt;
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
56 if (pt->type > LIBBPF_NLA_TYPE_MAX)
57 return 0;
58
59 if (pt->minlen)
60 minlen = pt->minlen;
61 else if (pt->type != LIBBPF_NLA_UNSPEC)
62 minlen = nla_attr_minlen[pt->type];
63
64 if (libbpf_nla_len(nla) < minlen)
65 return -1;
66
67 if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
68 return -1;
69
70 if (pt->type == LIBBPF_NLA_STRING) {
71 char *data = libbpf_nla_data(nla);
72
73 if (data[libbpf_nla_len(nla) - 1] != '\0')
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
104 int len, struct libbpf_nla_policy *policy)
105 {
106 struct nlattr *nla;
107 int rem, err;
108
109 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
110
111 libbpf_nla_for_each_attr(nla, head, len, rem) {
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148 int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
149 struct nlattr *nla,
150 struct libbpf_nla_policy *policy)
151 {
152 return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla),
153 libbpf_nla_len(nla), policy);
154 }
155
156
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 },
162 };
163 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr;
164 struct nlmsgerr *err;
165 char *errmsg = NULL;
166 int hlen, alen;
167
168
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
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
182 if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen,
183 extack_policy) != 0) {
184 fprintf(stderr,
185 "Failed to parse extended error attributes\n");
186 return 0;
187 }
188
189 if (tb[NLMSGERR_ATTR_MSG])
190 errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]);
191
192 fprintf(stderr, "Kernel error message: %s\n", errmsg);
193
194 return 0;
195 }