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