libbpf: Move __dump_nlmsg_t from API to implementation
[linux-2.6-microblaze.git] / tools / lib / bpf / netlink.c
1 // SPDX-License-Identifier: LGPL-2.1
2 /* Copyright (c) 2018 Facebook */
3
4 #include <stdlib.h>
5 #include <memory.h>
6 #include <unistd.h>
7 #include <linux/bpf.h>
8 #include <linux/rtnetlink.h>
9 #include <sys/socket.h>
10 #include <errno.h>
11 #include <time.h>
12
13 #include "bpf.h"
14 #include "libbpf.h"
15 #include "nlattr.h"
16
17 #ifndef SOL_NETLINK
18 #define SOL_NETLINK 270
19 #endif
20
21 typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t,
22                               void *cookie);
23
24 int bpf_netlink_open(__u32 *nl_pid)
25 {
26         struct sockaddr_nl sa;
27         socklen_t addrlen;
28         int one = 1, ret;
29         int sock;
30
31         memset(&sa, 0, sizeof(sa));
32         sa.nl_family = AF_NETLINK;
33
34         sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
35         if (sock < 0)
36                 return -errno;
37
38         if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
39                        &one, sizeof(one)) < 0) {
40                 fprintf(stderr, "Netlink error reporting not supported\n");
41         }
42
43         if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
44                 ret = -errno;
45                 goto cleanup;
46         }
47
48         addrlen = sizeof(sa);
49         if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
50                 ret = -errno;
51                 goto cleanup;
52         }
53
54         if (addrlen != sizeof(sa)) {
55                 ret = -LIBBPF_ERRNO__INTERNAL;
56                 goto cleanup;
57         }
58
59         *nl_pid = sa.nl_pid;
60         return sock;
61
62 cleanup:
63         close(sock);
64         return ret;
65 }
66
67 static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq,
68                             __dump_nlmsg_t _fn, dump_nlmsg_t fn,
69                             void *cookie)
70 {
71         bool multipart = true;
72         struct nlmsgerr *err;
73         struct nlmsghdr *nh;
74         char buf[4096];
75         int len, ret;
76
77         while (multipart) {
78                 multipart = false;
79                 len = recv(sock, buf, sizeof(buf), 0);
80                 if (len < 0) {
81                         ret = -errno;
82                         goto done;
83                 }
84
85                 if (len == 0)
86                         break;
87
88                 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
89                      nh = NLMSG_NEXT(nh, len)) {
90                         if (nh->nlmsg_pid != nl_pid) {
91                                 ret = -LIBBPF_ERRNO__WRNGPID;
92                                 goto done;
93                         }
94                         if (nh->nlmsg_seq != seq) {
95                                 ret = -LIBBPF_ERRNO__INVSEQ;
96                                 goto done;
97                         }
98                         if (nh->nlmsg_flags & NLM_F_MULTI)
99                                 multipart = true;
100                         switch (nh->nlmsg_type) {
101                         case NLMSG_ERROR:
102                                 err = (struct nlmsgerr *)NLMSG_DATA(nh);
103                                 if (!err->error)
104                                         continue;
105                                 ret = err->error;
106                                 nla_dump_errormsg(nh);
107                                 goto done;
108                         case NLMSG_DONE:
109                                 return 0;
110                         default:
111                                 break;
112                         }
113                         if (_fn) {
114                                 ret = _fn(nh, fn, cookie);
115                                 if (ret)
116                                         return ret;
117                         }
118                 }
119         }
120         ret = 0;
121 done:
122         return ret;
123 }
124
125 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
126 {
127         int sock, seq = 0, ret;
128         struct nlattr *nla, *nla_xdp;
129         struct {
130                 struct nlmsghdr  nh;
131                 struct ifinfomsg ifinfo;
132                 char             attrbuf[64];
133         } req;
134         __u32 nl_pid;
135
136         sock = bpf_netlink_open(&nl_pid);
137         if (sock < 0)
138                 return sock;
139
140         memset(&req, 0, sizeof(req));
141         req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
142         req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
143         req.nh.nlmsg_type = RTM_SETLINK;
144         req.nh.nlmsg_pid = 0;
145         req.nh.nlmsg_seq = ++seq;
146         req.ifinfo.ifi_family = AF_UNSPEC;
147         req.ifinfo.ifi_index = ifindex;
148
149         /* started nested attribute for XDP */
150         nla = (struct nlattr *)(((char *)&req)
151                                 + NLMSG_ALIGN(req.nh.nlmsg_len));
152         nla->nla_type = NLA_F_NESTED | IFLA_XDP;
153         nla->nla_len = NLA_HDRLEN;
154
155         /* add XDP fd */
156         nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
157         nla_xdp->nla_type = IFLA_XDP_FD;
158         nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
159         memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
160         nla->nla_len += nla_xdp->nla_len;
161
162         /* if user passed in any flags, add those too */
163         if (flags) {
164                 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
165                 nla_xdp->nla_type = IFLA_XDP_FLAGS;
166                 nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
167                 memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
168                 nla->nla_len += nla_xdp->nla_len;
169         }
170
171         req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
172
173         if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
174                 ret = -errno;
175                 goto cleanup;
176         }
177         ret = bpf_netlink_recv(sock, nl_pid, seq, NULL, NULL, NULL);
178
179 cleanup:
180         close(sock);
181         return ret;
182 }
183
184 static int __dump_link_nlmsg(struct nlmsghdr *nlh, dump_nlmsg_t dump_link_nlmsg,
185                              void *cookie)
186 {
187         struct nlattr *tb[IFLA_MAX + 1], *attr;
188         struct ifinfomsg *ifi = NLMSG_DATA(nlh);
189         int len;
190
191         len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
192         attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
193         if (nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
194                 return -LIBBPF_ERRNO__NLPARSE;
195
196         return dump_link_nlmsg(cookie, ifi, tb);
197 }
198
199 int nl_get_link(int sock, unsigned int nl_pid, dump_nlmsg_t dump_link_nlmsg,
200                 void *cookie)
201 {
202         struct {
203                 struct nlmsghdr nlh;
204                 struct ifinfomsg ifm;
205         } req = {
206                 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
207                 .nlh.nlmsg_type = RTM_GETLINK,
208                 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
209                 .ifm.ifi_family = AF_PACKET,
210         };
211         int seq = time(NULL);
212
213         req.nlh.nlmsg_seq = seq;
214         if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
215                 return -errno;
216
217         return bpf_netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg,
218                                 dump_link_nlmsg, cookie);
219 }
220
221 static int __dump_class_nlmsg(struct nlmsghdr *nlh,
222                               dump_nlmsg_t dump_class_nlmsg, void *cookie)
223 {
224         struct nlattr *tb[TCA_MAX + 1], *attr;
225         struct tcmsg *t = NLMSG_DATA(nlh);
226         int len;
227
228         len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
229         attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
230         if (nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
231                 return -LIBBPF_ERRNO__NLPARSE;
232
233         return dump_class_nlmsg(cookie, t, tb);
234 }
235
236 int nl_get_class(int sock, unsigned int nl_pid, int ifindex,
237                  dump_nlmsg_t dump_class_nlmsg, void *cookie)
238 {
239         struct {
240                 struct nlmsghdr nlh;
241                 struct tcmsg t;
242         } req = {
243                 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
244                 .nlh.nlmsg_type = RTM_GETTCLASS,
245                 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
246                 .t.tcm_family = AF_UNSPEC,
247                 .t.tcm_ifindex = ifindex,
248         };
249         int seq = time(NULL);
250
251         req.nlh.nlmsg_seq = seq;
252         if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
253                 return -errno;
254
255         return bpf_netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg,
256                                 dump_class_nlmsg, cookie);
257 }
258
259 static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
260                               dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
261 {
262         struct nlattr *tb[TCA_MAX + 1], *attr;
263         struct tcmsg *t = NLMSG_DATA(nlh);
264         int len;
265
266         len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
267         attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
268         if (nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
269                 return -LIBBPF_ERRNO__NLPARSE;
270
271         return dump_qdisc_nlmsg(cookie, t, tb);
272 }
273
274 int nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
275                  dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
276 {
277         struct {
278                 struct nlmsghdr nlh;
279                 struct tcmsg t;
280         } req = {
281                 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
282                 .nlh.nlmsg_type = RTM_GETQDISC,
283                 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
284                 .t.tcm_family = AF_UNSPEC,
285                 .t.tcm_ifindex = ifindex,
286         };
287         int seq = time(NULL);
288
289         req.nlh.nlmsg_seq = seq;
290         if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
291                 return -errno;
292
293         return bpf_netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg,
294                                 dump_qdisc_nlmsg, cookie);
295 }
296
297 static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
298                                dump_nlmsg_t dump_filter_nlmsg, void *cookie)
299 {
300         struct nlattr *tb[TCA_MAX + 1], *attr;
301         struct tcmsg *t = NLMSG_DATA(nlh);
302         int len;
303
304         len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
305         attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
306         if (nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
307                 return -LIBBPF_ERRNO__NLPARSE;
308
309         return dump_filter_nlmsg(cookie, t, tb);
310 }
311
312 int nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
313                   dump_nlmsg_t dump_filter_nlmsg, void *cookie)
314 {
315         struct {
316                 struct nlmsghdr nlh;
317                 struct tcmsg t;
318         } req = {
319                 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
320                 .nlh.nlmsg_type = RTM_GETTFILTER,
321                 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
322                 .t.tcm_family = AF_UNSPEC,
323                 .t.tcm_ifindex = ifindex,
324                 .t.tcm_parent = handle,
325         };
326         int seq = time(NULL);
327
328         req.nlh.nlmsg_seq = seq;
329         if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
330                 return -errno;
331
332         return bpf_netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg,
333                                 dump_filter_nlmsg, cookie);
334 }