1 // SPDX-License-Identifier: (GPL-2.0-only OR B 1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 // Copyright (C) 2018 Facebook 2 // Copyright (C) 2018 Facebook 3 3 4 #ifndef _GNU_SOURCE 4 #ifndef _GNU_SOURCE 5 #define _GNU_SOURCE 5 #define _GNU_SOURCE 6 #endif 6 #endif 7 #include <errno.h> 7 #include <errno.h> 8 #include <fcntl.h> 8 #include <fcntl.h> 9 #include <stdlib.h> 9 #include <stdlib.h> 10 #include <string.h> 10 #include <string.h> 11 #include <time.h> 11 #include <time.h> 12 #include <unistd.h> 12 #include <unistd.h> 13 #include <bpf/bpf.h> 13 #include <bpf/bpf.h> 14 #include <bpf/libbpf.h> 14 #include <bpf/libbpf.h> 15 #include <net/if.h> 15 #include <net/if.h> 16 #include <linux/rtnetlink.h> 16 #include <linux/rtnetlink.h> 17 #include <linux/socket.h> 17 #include <linux/socket.h> 18 #include <linux/tc_act/tc_bpf.h> 18 #include <linux/tc_act/tc_bpf.h> 19 #include <sys/socket.h> 19 #include <sys/socket.h> 20 #include <sys/stat.h> 20 #include <sys/stat.h> 21 #include <sys/types.h> 21 #include <sys/types.h> 22 22 23 #include "bpf/nlattr.h" 23 #include "bpf/nlattr.h" 24 #include "main.h" 24 #include "main.h" 25 #include "netlink_dumper.h" 25 #include "netlink_dumper.h" 26 26 27 #ifndef SOL_NETLINK 27 #ifndef SOL_NETLINK 28 #define SOL_NETLINK 270 28 #define SOL_NETLINK 270 29 #endif 29 #endif 30 30 31 struct ip_devname_ifindex { 31 struct ip_devname_ifindex { 32 char devname[64]; 32 char devname[64]; 33 int ifindex; 33 int ifindex; 34 }; 34 }; 35 35 36 struct bpf_netdev_t { 36 struct bpf_netdev_t { 37 struct ip_devname_ifindex *devices; 37 struct ip_devname_ifindex *devices; 38 int used_len; 38 int used_len; 39 int array_len; 39 int array_len; 40 int filter_idx; 40 int filter_idx; 41 }; 41 }; 42 42 43 struct tc_kind_handle { 43 struct tc_kind_handle { 44 char kind[64]; 44 char kind[64]; 45 int handle; 45 int handle; 46 }; 46 }; 47 47 48 struct bpf_tcinfo_t { 48 struct bpf_tcinfo_t { 49 struct tc_kind_handle *handle_array; 49 struct tc_kind_handle *handle_array; 50 int used_len; 50 int used_len; 51 int array_len; 51 int array_len; 52 bool is_qdisc; 52 bool is_qdisc; 53 }; 53 }; 54 54 55 struct bpf_filter_t { 55 struct bpf_filter_t { 56 const char *kind; 56 const char *kind; 57 const char *devname; 57 const char *devname; 58 int ifindex; 58 int ifindex; 59 }; 59 }; 60 60 61 struct bpf_attach_info { 61 struct bpf_attach_info { 62 __u32 flow_dissector_id; 62 __u32 flow_dissector_id; 63 }; 63 }; 64 64 65 enum net_attach_type { 65 enum net_attach_type { 66 NET_ATTACH_TYPE_XDP, 66 NET_ATTACH_TYPE_XDP, 67 NET_ATTACH_TYPE_XDP_GENERIC, 67 NET_ATTACH_TYPE_XDP_GENERIC, 68 NET_ATTACH_TYPE_XDP_DRIVER, 68 NET_ATTACH_TYPE_XDP_DRIVER, 69 NET_ATTACH_TYPE_XDP_OFFLOAD, 69 NET_ATTACH_TYPE_XDP_OFFLOAD, 70 NET_ATTACH_TYPE_TCX_INGRESS, 70 NET_ATTACH_TYPE_TCX_INGRESS, 71 NET_ATTACH_TYPE_TCX_EGRESS, 71 NET_ATTACH_TYPE_TCX_EGRESS, 72 }; 72 }; 73 73 74 static const char * const attach_type_strings[ 74 static const char * const attach_type_strings[] = { 75 [NET_ATTACH_TYPE_XDP] = "xdp 75 [NET_ATTACH_TYPE_XDP] = "xdp", 76 [NET_ATTACH_TYPE_XDP_GENERIC] = "xdp 76 [NET_ATTACH_TYPE_XDP_GENERIC] = "xdpgeneric", 77 [NET_ATTACH_TYPE_XDP_DRIVER] = "xdp 77 [NET_ATTACH_TYPE_XDP_DRIVER] = "xdpdrv", 78 [NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdp 78 [NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdpoffload", 79 [NET_ATTACH_TYPE_TCX_INGRESS] = "tcx 79 [NET_ATTACH_TYPE_TCX_INGRESS] = "tcx_ingress", 80 [NET_ATTACH_TYPE_TCX_EGRESS] = "tcx 80 [NET_ATTACH_TYPE_TCX_EGRESS] = "tcx_egress", 81 }; 81 }; 82 82 83 static const char * const attach_loc_strings[] 83 static const char * const attach_loc_strings[] = { 84 [BPF_TCX_INGRESS] = "tcx 84 [BPF_TCX_INGRESS] = "tcx/ingress", 85 [BPF_TCX_EGRESS] = "tcx 85 [BPF_TCX_EGRESS] = "tcx/egress", 86 [BPF_NETKIT_PRIMARY] = "net 86 [BPF_NETKIT_PRIMARY] = "netkit/primary", 87 [BPF_NETKIT_PEER] = "net 87 [BPF_NETKIT_PEER] = "netkit/peer", 88 }; 88 }; 89 89 90 const size_t net_attach_type_size = ARRAY_SIZE 90 const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings); 91 91 92 static enum net_attach_type parse_attach_type( 92 static enum net_attach_type parse_attach_type(const char *str) 93 { 93 { 94 enum net_attach_type type; 94 enum net_attach_type type; 95 95 96 for (type = 0; type < net_attach_type_ 96 for (type = 0; type < net_attach_type_size; type++) { 97 if (attach_type_strings[type] 97 if (attach_type_strings[type] && 98 is_prefix(str, attach_type 98 is_prefix(str, attach_type_strings[type])) 99 return type; 99 return type; 100 } 100 } 101 101 102 return net_attach_type_size; 102 return net_attach_type_size; 103 } 103 } 104 104 105 typedef int (*dump_nlmsg_t)(void *cookie, void 105 typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); 106 106 107 typedef int (*__dump_nlmsg_t)(struct nlmsghdr 107 typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, void *cookie); 108 108 109 static int netlink_open(__u32 *nl_pid) 109 static int netlink_open(__u32 *nl_pid) 110 { 110 { 111 struct sockaddr_nl sa; 111 struct sockaddr_nl sa; 112 socklen_t addrlen; 112 socklen_t addrlen; 113 int one = 1, ret; 113 int one = 1, ret; 114 int sock; 114 int sock; 115 115 116 memset(&sa, 0, sizeof(sa)); 116 memset(&sa, 0, sizeof(sa)); 117 sa.nl_family = AF_NETLINK; 117 sa.nl_family = AF_NETLINK; 118 118 119 sock = socket(AF_NETLINK, SOCK_RAW, NE 119 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); 120 if (sock < 0) 120 if (sock < 0) 121 return -errno; 121 return -errno; 122 122 123 if (setsockopt(sock, SOL_NETLINK, NETL 123 if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK, 124 &one, sizeof(one)) < 0) 124 &one, sizeof(one)) < 0) { 125 p_err("Netlink error reporting 125 p_err("Netlink error reporting not supported"); 126 } 126 } 127 127 128 if (bind(sock, (struct sockaddr *)&sa, 128 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) { 129 ret = -errno; 129 ret = -errno; 130 goto cleanup; 130 goto cleanup; 131 } 131 } 132 132 133 addrlen = sizeof(sa); 133 addrlen = sizeof(sa); 134 if (getsockname(sock, (struct sockaddr 134 if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) { 135 ret = -errno; 135 ret = -errno; 136 goto cleanup; 136 goto cleanup; 137 } 137 } 138 138 139 if (addrlen != sizeof(sa)) { 139 if (addrlen != sizeof(sa)) { 140 ret = -LIBBPF_ERRNO__INTERNAL; 140 ret = -LIBBPF_ERRNO__INTERNAL; 141 goto cleanup; 141 goto cleanup; 142 } 142 } 143 143 144 *nl_pid = sa.nl_pid; 144 *nl_pid = sa.nl_pid; 145 return sock; 145 return sock; 146 146 147 cleanup: 147 cleanup: 148 close(sock); 148 close(sock); 149 return ret; 149 return ret; 150 } 150 } 151 151 152 static int netlink_recv(int sock, __u32 nl_pid 152 static int netlink_recv(int sock, __u32 nl_pid, __u32 seq, 153 __dump_nlmsg_t _fn 153 __dump_nlmsg_t _fn, dump_nlmsg_t fn, 154 void *cookie) 154 void *cookie) 155 { 155 { 156 bool multipart = true; 156 bool multipart = true; 157 struct nlmsgerr *err; 157 struct nlmsgerr *err; 158 struct nlmsghdr *nh; 158 struct nlmsghdr *nh; 159 char buf[4096]; 159 char buf[4096]; 160 int len, ret; 160 int len, ret; 161 161 162 while (multipart) { 162 while (multipart) { 163 multipart = false; 163 multipart = false; 164 len = recv(sock, buf, sizeof(b 164 len = recv(sock, buf, sizeof(buf), 0); 165 if (len < 0) { 165 if (len < 0) { 166 ret = -errno; 166 ret = -errno; 167 goto done; 167 goto done; 168 } 168 } 169 169 170 if (len == 0) 170 if (len == 0) 171 break; 171 break; 172 172 173 for (nh = (struct nlmsghdr *)b 173 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, (unsigned int)len); 174 nh = NLMSG_NEXT(nh, len)) 174 nh = NLMSG_NEXT(nh, len)) { 175 if (nh->nlmsg_pid != n 175 if (nh->nlmsg_pid != nl_pid) { 176 ret = -LIBBPF_ 176 ret = -LIBBPF_ERRNO__WRNGPID; 177 goto done; 177 goto done; 178 } 178 } 179 if (nh->nlmsg_seq != s 179 if (nh->nlmsg_seq != seq) { 180 ret = -LIBBPF_ 180 ret = -LIBBPF_ERRNO__INVSEQ; 181 goto done; 181 goto done; 182 } 182 } 183 if (nh->nlmsg_flags & 183 if (nh->nlmsg_flags & NLM_F_MULTI) 184 multipart = tr 184 multipart = true; 185 switch (nh->nlmsg_type 185 switch (nh->nlmsg_type) { 186 case NLMSG_ERROR: 186 case NLMSG_ERROR: 187 err = (struct 187 err = (struct nlmsgerr *)NLMSG_DATA(nh); 188 if (!err->erro 188 if (!err->error) 189 contin 189 continue; 190 ret = err->err 190 ret = err->error; 191 libbpf_nla_dum 191 libbpf_nla_dump_errormsg(nh); 192 goto done; 192 goto done; 193 case NLMSG_DONE: 193 case NLMSG_DONE: 194 return 0; 194 return 0; 195 default: 195 default: 196 break; 196 break; 197 } 197 } 198 if (_fn) { 198 if (_fn) { 199 ret = _fn(nh, 199 ret = _fn(nh, fn, cookie); 200 if (ret) 200 if (ret) 201 return 201 return ret; 202 } 202 } 203 } 203 } 204 } 204 } 205 ret = 0; 205 ret = 0; 206 done: 206 done: 207 return ret; 207 return ret; 208 } 208 } 209 209 210 static int __dump_class_nlmsg(struct nlmsghdr 210 static int __dump_class_nlmsg(struct nlmsghdr *nlh, 211 dump_nlmsg_t dum 211 dump_nlmsg_t dump_class_nlmsg, 212 void *cookie) 212 void *cookie) 213 { 213 { 214 struct nlattr *tb[TCA_MAX + 1], *attr; 214 struct nlattr *tb[TCA_MAX + 1], *attr; 215 struct tcmsg *t = NLMSG_DATA(nlh); 215 struct tcmsg *t = NLMSG_DATA(nlh); 216 int len; 216 int len; 217 217 218 len = nlh->nlmsg_len - NLMSG_LENGTH(si 218 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 219 attr = (struct nlattr *) ((void *) t + 219 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 220 if (libbpf_nla_parse(tb, TCA_MAX, attr 220 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 221 return -LIBBPF_ERRNO__NLPARSE; 221 return -LIBBPF_ERRNO__NLPARSE; 222 222 223 return dump_class_nlmsg(cookie, t, tb) 223 return dump_class_nlmsg(cookie, t, tb); 224 } 224 } 225 225 226 static int netlink_get_class(int sock, unsigne 226 static int netlink_get_class(int sock, unsigned int nl_pid, int ifindex, 227 dump_nlmsg_t dump 227 dump_nlmsg_t dump_class_nlmsg, void *cookie) 228 { 228 { 229 struct { 229 struct { 230 struct nlmsghdr nlh; 230 struct nlmsghdr nlh; 231 struct tcmsg t; 231 struct tcmsg t; 232 } req = { 232 } req = { 233 .nlh.nlmsg_len = NLMSG_LENGTH( 233 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), 234 .nlh.nlmsg_type = RTM_GETTCLAS 234 .nlh.nlmsg_type = RTM_GETTCLASS, 235 .nlh.nlmsg_flags = NLM_F_DUMP 235 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 236 .t.tcm_family = AF_UNSPEC, 236 .t.tcm_family = AF_UNSPEC, 237 .t.tcm_ifindex = ifindex, 237 .t.tcm_ifindex = ifindex, 238 }; 238 }; 239 int seq = time(NULL); 239 int seq = time(NULL); 240 240 241 req.nlh.nlmsg_seq = seq; 241 req.nlh.nlmsg_seq = seq; 242 if (send(sock, &req, req.nlh.nlmsg_len 242 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0) 243 return -errno; 243 return -errno; 244 244 245 return netlink_recv(sock, nl_pid, seq, 245 return netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg, 246 dump_class_nlmsg, 246 dump_class_nlmsg, cookie); 247 } 247 } 248 248 249 static int __dump_qdisc_nlmsg(struct nlmsghdr 249 static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh, 250 dump_nlmsg_t dum 250 dump_nlmsg_t dump_qdisc_nlmsg, 251 void *cookie) 251 void *cookie) 252 { 252 { 253 struct nlattr *tb[TCA_MAX + 1], *attr; 253 struct nlattr *tb[TCA_MAX + 1], *attr; 254 struct tcmsg *t = NLMSG_DATA(nlh); 254 struct tcmsg *t = NLMSG_DATA(nlh); 255 int len; 255 int len; 256 256 257 len = nlh->nlmsg_len - NLMSG_LENGTH(si 257 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 258 attr = (struct nlattr *) ((void *) t + 258 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 259 if (libbpf_nla_parse(tb, TCA_MAX, attr 259 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 260 return -LIBBPF_ERRNO__NLPARSE; 260 return -LIBBPF_ERRNO__NLPARSE; 261 261 262 return dump_qdisc_nlmsg(cookie, t, tb) 262 return dump_qdisc_nlmsg(cookie, t, tb); 263 } 263 } 264 264 265 static int netlink_get_qdisc(int sock, unsigne 265 static int netlink_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 266 dump_nlmsg_t dump 266 dump_nlmsg_t dump_qdisc_nlmsg, void *cookie) 267 { 267 { 268 struct { 268 struct { 269 struct nlmsghdr nlh; 269 struct nlmsghdr nlh; 270 struct tcmsg t; 270 struct tcmsg t; 271 } req = { 271 } req = { 272 .nlh.nlmsg_len = NLMSG_LENGTH( 272 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), 273 .nlh.nlmsg_type = RTM_GETQDISC 273 .nlh.nlmsg_type = RTM_GETQDISC, 274 .nlh.nlmsg_flags = NLM_F_DUMP 274 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 275 .t.tcm_family = AF_UNSPEC, 275 .t.tcm_family = AF_UNSPEC, 276 .t.tcm_ifindex = ifindex, 276 .t.tcm_ifindex = ifindex, 277 }; 277 }; 278 int seq = time(NULL); 278 int seq = time(NULL); 279 279 280 req.nlh.nlmsg_seq = seq; 280 req.nlh.nlmsg_seq = seq; 281 if (send(sock, &req, req.nlh.nlmsg_len 281 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0) 282 return -errno; 282 return -errno; 283 283 284 return netlink_recv(sock, nl_pid, seq, 284 return netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg, 285 dump_qdisc_nlmsg, 285 dump_qdisc_nlmsg, cookie); 286 } 286 } 287 287 288 static int __dump_filter_nlmsg(struct nlmsghdr 288 static int __dump_filter_nlmsg(struct nlmsghdr *nlh, 289 dump_nlmsg_t du 289 dump_nlmsg_t dump_filter_nlmsg, 290 void *cookie) 290 void *cookie) 291 { 291 { 292 struct nlattr *tb[TCA_MAX + 1], *attr; 292 struct nlattr *tb[TCA_MAX + 1], *attr; 293 struct tcmsg *t = NLMSG_DATA(nlh); 293 struct tcmsg *t = NLMSG_DATA(nlh); 294 int len; 294 int len; 295 295 296 len = nlh->nlmsg_len - NLMSG_LENGTH(si 296 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 297 attr = (struct nlattr *) ((void *) t + 297 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 298 if (libbpf_nla_parse(tb, TCA_MAX, attr 298 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 299 return -LIBBPF_ERRNO__NLPARSE; 299 return -LIBBPF_ERRNO__NLPARSE; 300 300 301 return dump_filter_nlmsg(cookie, t, tb 301 return dump_filter_nlmsg(cookie, t, tb); 302 } 302 } 303 303 304 static int netlink_get_filter(int sock, unsign 304 static int netlink_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 305 dump_nlmsg_t dum 305 dump_nlmsg_t dump_filter_nlmsg, void *cookie) 306 { 306 { 307 struct { 307 struct { 308 struct nlmsghdr nlh; 308 struct nlmsghdr nlh; 309 struct tcmsg t; 309 struct tcmsg t; 310 } req = { 310 } req = { 311 .nlh.nlmsg_len = NLMSG_LENGTH( 311 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), 312 .nlh.nlmsg_type = RTM_GETTFILT 312 .nlh.nlmsg_type = RTM_GETTFILTER, 313 .nlh.nlmsg_flags = NLM_F_DUMP 313 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 314 .t.tcm_family = AF_UNSPEC, 314 .t.tcm_family = AF_UNSPEC, 315 .t.tcm_ifindex = ifindex, 315 .t.tcm_ifindex = ifindex, 316 .t.tcm_parent = handle, 316 .t.tcm_parent = handle, 317 }; 317 }; 318 int seq = time(NULL); 318 int seq = time(NULL); 319 319 320 req.nlh.nlmsg_seq = seq; 320 req.nlh.nlmsg_seq = seq; 321 if (send(sock, &req, req.nlh.nlmsg_len 321 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0) 322 return -errno; 322 return -errno; 323 323 324 return netlink_recv(sock, nl_pid, seq, 324 return netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg, 325 dump_filter_nlmsg, 325 dump_filter_nlmsg, cookie); 326 } 326 } 327 327 328 static int __dump_link_nlmsg(struct nlmsghdr * 328 static int __dump_link_nlmsg(struct nlmsghdr *nlh, 329 dump_nlmsg_t dump 329 dump_nlmsg_t dump_link_nlmsg, void *cookie) 330 { 330 { 331 struct nlattr *tb[IFLA_MAX + 1], *attr 331 struct nlattr *tb[IFLA_MAX + 1], *attr; 332 struct ifinfomsg *ifi = NLMSG_DATA(nlh 332 struct ifinfomsg *ifi = NLMSG_DATA(nlh); 333 int len; 333 int len; 334 334 335 len = nlh->nlmsg_len - NLMSG_LENGTH(si 335 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); 336 attr = (struct nlattr *) ((void *) ifi 336 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi))); 337 if (libbpf_nla_parse(tb, IFLA_MAX, att 337 if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0) 338 return -LIBBPF_ERRNO__NLPARSE; 338 return -LIBBPF_ERRNO__NLPARSE; 339 339 340 return dump_link_nlmsg(cookie, ifi, tb 340 return dump_link_nlmsg(cookie, ifi, tb); 341 } 341 } 342 342 343 static int netlink_get_link(int sock, unsigned 343 static int netlink_get_link(int sock, unsigned int nl_pid, 344 dump_nlmsg_t dump_ 344 dump_nlmsg_t dump_link_nlmsg, void *cookie) 345 { 345 { 346 struct { 346 struct { 347 struct nlmsghdr nlh; 347 struct nlmsghdr nlh; 348 struct ifinfomsg ifm; 348 struct ifinfomsg ifm; 349 } req = { 349 } req = { 350 .nlh.nlmsg_len = NLMSG_LENGTH( 350 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 351 .nlh.nlmsg_type = RTM_GETLINK, 351 .nlh.nlmsg_type = RTM_GETLINK, 352 .nlh.nlmsg_flags = NLM_F_DUMP 352 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 353 .ifm.ifi_family = AF_PACKET, 353 .ifm.ifi_family = AF_PACKET, 354 }; 354 }; 355 int seq = time(NULL); 355 int seq = time(NULL); 356 356 357 req.nlh.nlmsg_seq = seq; 357 req.nlh.nlmsg_seq = seq; 358 if (send(sock, &req, req.nlh.nlmsg_len 358 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0) 359 return -errno; 359 return -errno; 360 360 361 return netlink_recv(sock, nl_pid, seq, 361 return netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg, 362 dump_link_nlmsg, c 362 dump_link_nlmsg, cookie); 363 } 363 } 364 364 365 static int dump_link_nlmsg(void *cookie, void 365 static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb) 366 { 366 { 367 struct bpf_netdev_t *netinfo = cookie; 367 struct bpf_netdev_t *netinfo = cookie; 368 struct ifinfomsg *ifinfo = msg; 368 struct ifinfomsg *ifinfo = msg; 369 369 370 if (netinfo->filter_idx > 0 && netinfo 370 if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index) 371 return 0; 371 return 0; 372 372 373 if (netinfo->used_len == netinfo->arra 373 if (netinfo->used_len == netinfo->array_len) { 374 netinfo->devices = realloc(net 374 netinfo->devices = realloc(netinfo->devices, 375 (netinfo->array_len + 375 (netinfo->array_len + 16) * 376 sizeof(struct ip_devna 376 sizeof(struct ip_devname_ifindex)); 377 if (!netinfo->devices) 377 if (!netinfo->devices) 378 return -ENOMEM; 378 return -ENOMEM; 379 379 380 netinfo->array_len += 16; 380 netinfo->array_len += 16; 381 } 381 } 382 netinfo->devices[netinfo->used_len].if 382 netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index; 383 snprintf(netinfo->devices[netinfo->use 383 snprintf(netinfo->devices[netinfo->used_len].devname, 384 sizeof(netinfo->devices[netin 384 sizeof(netinfo->devices[netinfo->used_len].devname), 385 "%s", 385 "%s", 386 tb[IFLA_IFNAME] 386 tb[IFLA_IFNAME] 387 ? libbpf_nla_getattr_ 387 ? libbpf_nla_getattr_str(tb[IFLA_IFNAME]) 388 : ""); 388 : ""); 389 netinfo->used_len++; 389 netinfo->used_len++; 390 390 391 return do_xdp_dump(ifinfo, tb); 391 return do_xdp_dump(ifinfo, tb); 392 } 392 } 393 393 394 static int dump_class_qdisc_nlmsg(void *cookie 394 static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb) 395 { 395 { 396 struct bpf_tcinfo_t *tcinfo = cookie; 396 struct bpf_tcinfo_t *tcinfo = cookie; 397 struct tcmsg *info = msg; 397 struct tcmsg *info = msg; 398 398 399 if (tcinfo->is_qdisc) { 399 if (tcinfo->is_qdisc) { 400 /* skip clsact qdisc */ 400 /* skip clsact qdisc */ 401 if (tb[TCA_KIND] && 401 if (tb[TCA_KIND] && 402 strcmp(libbpf_nla_data(tb[ 402 strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0) 403 return 0; 403 return 0; 404 if (info->tcm_handle == 0) 404 if (info->tcm_handle == 0) 405 return 0; 405 return 0; 406 } 406 } 407 407 408 if (tcinfo->used_len == tcinfo->array_ 408 if (tcinfo->used_len == tcinfo->array_len) { 409 tcinfo->handle_array = realloc 409 tcinfo->handle_array = realloc(tcinfo->handle_array, 410 (tcinfo->array_len + 1 410 (tcinfo->array_len + 16) * sizeof(struct tc_kind_handle)); 411 if (!tcinfo->handle_array) 411 if (!tcinfo->handle_array) 412 return -ENOMEM; 412 return -ENOMEM; 413 413 414 tcinfo->array_len += 16; 414 tcinfo->array_len += 16; 415 } 415 } 416 tcinfo->handle_array[tcinfo->used_len] 416 tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle; 417 snprintf(tcinfo->handle_array[tcinfo-> 417 snprintf(tcinfo->handle_array[tcinfo->used_len].kind, 418 sizeof(tcinfo->handle_array[t 418 sizeof(tcinfo->handle_array[tcinfo->used_len].kind), 419 "%s", 419 "%s", 420 tb[TCA_KIND] 420 tb[TCA_KIND] 421 ? libbpf_nla_getattr_ 421 ? libbpf_nla_getattr_str(tb[TCA_KIND]) 422 : "unknown"); 422 : "unknown"); 423 tcinfo->used_len++; 423 tcinfo->used_len++; 424 424 425 return 0; 425 return 0; 426 } 426 } 427 427 428 static int dump_filter_nlmsg(void *cookie, voi 428 static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb) 429 { 429 { 430 const struct bpf_filter_t *filter_info 430 const struct bpf_filter_t *filter_info = cookie; 431 431 432 return do_filter_dump((struct tcmsg *) 432 return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind, 433 filter_info->dev 433 filter_info->devname, filter_info->ifindex); 434 } 434 } 435 435 436 static int __show_dev_tc_bpf_name(__u32 id, ch 436 static int __show_dev_tc_bpf_name(__u32 id, char *name, size_t len) 437 { 437 { 438 struct bpf_prog_info info = {}; 438 struct bpf_prog_info info = {}; 439 __u32 ilen = sizeof(info); 439 __u32 ilen = sizeof(info); 440 int fd, ret; 440 int fd, ret; 441 441 442 fd = bpf_prog_get_fd_by_id(id); 442 fd = bpf_prog_get_fd_by_id(id); 443 if (fd < 0) 443 if (fd < 0) 444 return fd; 444 return fd; 445 ret = bpf_obj_get_info_by_fd(fd, &info 445 ret = bpf_obj_get_info_by_fd(fd, &info, &ilen); 446 if (ret < 0) 446 if (ret < 0) 447 goto out; 447 goto out; 448 ret = -ENOENT; 448 ret = -ENOENT; 449 if (info.name[0]) { 449 if (info.name[0]) { 450 get_prog_full_name(&info, fd, 450 get_prog_full_name(&info, fd, name, len); 451 ret = 0; 451 ret = 0; 452 } 452 } 453 out: 453 out: 454 close(fd); 454 close(fd); 455 return ret; 455 return ret; 456 } 456 } 457 457 458 static void __show_dev_tc_bpf(const struct ip_ 458 static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev, 459 const enum bpf_a 459 const enum bpf_attach_type loc) 460 { 460 { 461 __u32 prog_flags[64] = {}, link_flags[ 461 __u32 prog_flags[64] = {}, link_flags[64] = {}, i, j; 462 __u32 prog_ids[64] = {}, link_ids[64] 462 __u32 prog_ids[64] = {}, link_ids[64] = {}; 463 LIBBPF_OPTS(bpf_prog_query_opts, optq) 463 LIBBPF_OPTS(bpf_prog_query_opts, optq); 464 char prog_name[MAX_PROG_FULL_NAME]; 464 char prog_name[MAX_PROG_FULL_NAME]; 465 int ret; 465 int ret; 466 466 467 optq.prog_ids = prog_ids; 467 optq.prog_ids = prog_ids; 468 optq.prog_attach_flags = prog_flags; 468 optq.prog_attach_flags = prog_flags; 469 optq.link_ids = link_ids; 469 optq.link_ids = link_ids; 470 optq.link_attach_flags = link_flags; 470 optq.link_attach_flags = link_flags; 471 optq.count = ARRAY_SIZE(prog_ids); 471 optq.count = ARRAY_SIZE(prog_ids); 472 472 473 ret = bpf_prog_query_opts(dev->ifindex 473 ret = bpf_prog_query_opts(dev->ifindex, loc, &optq); 474 if (ret) 474 if (ret) 475 return; 475 return; 476 for (i = 0; i < optq.count; i++) { 476 for (i = 0; i < optq.count; i++) { 477 NET_START_OBJECT; 477 NET_START_OBJECT; 478 NET_DUMP_STR("devname", "%s", 478 NET_DUMP_STR("devname", "%s", dev->devname); 479 NET_DUMP_UINT("ifindex", "(%u) 479 NET_DUMP_UINT("ifindex", "(%u)", dev->ifindex); 480 NET_DUMP_STR("kind", " %s", at 480 NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]); 481 ret = __show_dev_tc_bpf_name(p 481 ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name, 482 s 482 sizeof(prog_name)); 483 if (!ret) 483 if (!ret) 484 NET_DUMP_STR("name", " 484 NET_DUMP_STR("name", " %s", prog_name); 485 NET_DUMP_UINT("prog_id", " pro 485 NET_DUMP_UINT("prog_id", " prog_id %u ", prog_ids[i]); 486 if (prog_flags[i] || json_outp 486 if (prog_flags[i] || json_output) { 487 NET_START_ARRAY("prog_ 487 NET_START_ARRAY("prog_flags", "%s "); 488 for (j = 0; prog_flags 488 for (j = 0; prog_flags[i] && j < 32; j++) { 489 if (!(prog_fla 489 if (!(prog_flags[i] & (1U << j))) 490 contin 490 continue; 491 NET_DUMP_UINT_ 491 NET_DUMP_UINT_ONLY(1U << j); 492 } 492 } 493 NET_END_ARRAY(""); 493 NET_END_ARRAY(""); 494 } 494 } 495 if (link_ids[i] || json_output 495 if (link_ids[i] || json_output) { 496 NET_DUMP_UINT("link_id 496 NET_DUMP_UINT("link_id", "link_id %u ", link_ids[i]); 497 if (link_flags[i] || j 497 if (link_flags[i] || json_output) { 498 NET_START_ARRA 498 NET_START_ARRAY("link_flags", "%s "); 499 for (j = 0; li 499 for (j = 0; link_flags[i] && j < 32; j++) { 500 if (!( 500 if (!(link_flags[i] & (1U << j))) 501 501 continue; 502 NET_DU 502 NET_DUMP_UINT_ONLY(1U << j); 503 } 503 } 504 NET_END_ARRAY( 504 NET_END_ARRAY(""); 505 } 505 } 506 } 506 } 507 NET_END_OBJECT_FINAL; 507 NET_END_OBJECT_FINAL; 508 } 508 } 509 } 509 } 510 510 511 static void show_dev_tc_bpf(struct ip_devname_ 511 static void show_dev_tc_bpf(struct ip_devname_ifindex *dev) 512 { 512 { 513 __show_dev_tc_bpf(dev, BPF_TCX_INGRESS 513 __show_dev_tc_bpf(dev, BPF_TCX_INGRESS); 514 __show_dev_tc_bpf(dev, BPF_TCX_EGRESS) 514 __show_dev_tc_bpf(dev, BPF_TCX_EGRESS); 515 515 516 __show_dev_tc_bpf(dev, BPF_NETKIT_PRIM 516 __show_dev_tc_bpf(dev, BPF_NETKIT_PRIMARY); 517 __show_dev_tc_bpf(dev, BPF_NETKIT_PEER 517 __show_dev_tc_bpf(dev, BPF_NETKIT_PEER); 518 } 518 } 519 519 520 static int show_dev_tc_bpf_classic(int sock, u 520 static int show_dev_tc_bpf_classic(int sock, unsigned int nl_pid, 521 struct ip_d 521 struct ip_devname_ifindex *dev) 522 { 522 { 523 struct bpf_filter_t filter_info; 523 struct bpf_filter_t filter_info; 524 struct bpf_tcinfo_t tcinfo; 524 struct bpf_tcinfo_t tcinfo; 525 int i, handle, ret = 0; 525 int i, handle, ret = 0; 526 526 527 tcinfo.handle_array = NULL; 527 tcinfo.handle_array = NULL; 528 tcinfo.used_len = 0; 528 tcinfo.used_len = 0; 529 tcinfo.array_len = 0; 529 tcinfo.array_len = 0; 530 530 531 tcinfo.is_qdisc = false; 531 tcinfo.is_qdisc = false; 532 ret = netlink_get_class(sock, nl_pid, 532 ret = netlink_get_class(sock, nl_pid, dev->ifindex, 533 dump_class_qdi 533 dump_class_qdisc_nlmsg, &tcinfo); 534 if (ret) 534 if (ret) 535 goto out; 535 goto out; 536 536 537 tcinfo.is_qdisc = true; 537 tcinfo.is_qdisc = true; 538 ret = netlink_get_qdisc(sock, nl_pid, 538 ret = netlink_get_qdisc(sock, nl_pid, dev->ifindex, 539 dump_class_qdi 539 dump_class_qdisc_nlmsg, &tcinfo); 540 if (ret) 540 if (ret) 541 goto out; 541 goto out; 542 542 543 filter_info.devname = dev->devname; 543 filter_info.devname = dev->devname; 544 filter_info.ifindex = dev->ifindex; 544 filter_info.ifindex = dev->ifindex; 545 for (i = 0; i < tcinfo.used_len; i++) 545 for (i = 0; i < tcinfo.used_len; i++) { 546 filter_info.kind = tcinfo.hand 546 filter_info.kind = tcinfo.handle_array[i].kind; 547 ret = netlink_get_filter(sock, 547 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, 548 tcinf 548 tcinfo.handle_array[i].handle, 549 dump_ 549 dump_filter_nlmsg, &filter_info); 550 if (ret) 550 if (ret) 551 goto out; 551 goto out; 552 } 552 } 553 553 554 /* root, ingress and egress handle */ 554 /* root, ingress and egress handle */ 555 handle = TC_H_ROOT; 555 handle = TC_H_ROOT; 556 filter_info.kind = "root"; 556 filter_info.kind = "root"; 557 ret = netlink_get_filter(sock, nl_pid, 557 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle, 558 dump_filter_n 558 dump_filter_nlmsg, &filter_info); 559 if (ret) 559 if (ret) 560 goto out; 560 goto out; 561 561 562 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_M 562 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS); 563 filter_info.kind = "clsact/ingress"; 563 filter_info.kind = "clsact/ingress"; 564 ret = netlink_get_filter(sock, nl_pid, 564 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle, 565 dump_filter_n 565 dump_filter_nlmsg, &filter_info); 566 if (ret) 566 if (ret) 567 goto out; 567 goto out; 568 568 569 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_M 569 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS); 570 filter_info.kind = "clsact/egress"; 570 filter_info.kind = "clsact/egress"; 571 ret = netlink_get_filter(sock, nl_pid, 571 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle, 572 dump_filter_n 572 dump_filter_nlmsg, &filter_info); 573 if (ret) 573 if (ret) 574 goto out; 574 goto out; 575 575 576 out: 576 out: 577 free(tcinfo.handle_array); 577 free(tcinfo.handle_array); 578 return 0; 578 return 0; 579 } 579 } 580 580 581 static int query_flow_dissector(struct bpf_att 581 static int query_flow_dissector(struct bpf_attach_info *attach_info) 582 { 582 { 583 __u32 attach_flags; 583 __u32 attach_flags; 584 __u32 prog_ids[1]; 584 __u32 prog_ids[1]; 585 __u32 prog_cnt; 585 __u32 prog_cnt; 586 int err; 586 int err; 587 int fd; 587 int fd; 588 588 589 fd = open("/proc/self/ns/net", O_RDONL 589 fd = open("/proc/self/ns/net", O_RDONLY); 590 if (fd < 0) { 590 if (fd < 0) { 591 p_err("can't open /proc/self/n 591 p_err("can't open /proc/self/ns/net: %s", 592 strerror(errno)); 592 strerror(errno)); 593 return -1; 593 return -1; 594 } 594 } 595 prog_cnt = ARRAY_SIZE(prog_ids); 595 prog_cnt = ARRAY_SIZE(prog_ids); 596 err = bpf_prog_query(fd, BPF_FLOW_DISS 596 err = bpf_prog_query(fd, BPF_FLOW_DISSECTOR, 0, 597 &attach_flags, pr 597 &attach_flags, prog_ids, &prog_cnt); 598 close(fd); 598 close(fd); 599 if (err) { 599 if (err) { 600 if (errno == EINVAL) { 600 if (errno == EINVAL) { 601 /* Older kernel's don' 601 /* Older kernel's don't support querying 602 * flow dissector prog 602 * flow dissector programs. 603 */ 603 */ 604 errno = 0; 604 errno = 0; 605 return 0; 605 return 0; 606 } 606 } 607 p_err("can't query prog: %s", 607 p_err("can't query prog: %s", strerror(errno)); 608 return -1; 608 return -1; 609 } 609 } 610 610 611 if (prog_cnt == 1) 611 if (prog_cnt == 1) 612 attach_info->flow_dissector_id 612 attach_info->flow_dissector_id = prog_ids[0]; 613 613 614 return 0; 614 return 0; 615 } 615 } 616 616 617 static int net_parse_dev(int *argc, char ***ar 617 static int net_parse_dev(int *argc, char ***argv) 618 { 618 { 619 int ifindex; 619 int ifindex; 620 620 621 if (is_prefix(**argv, "dev")) { 621 if (is_prefix(**argv, "dev")) { 622 NEXT_ARGP(); 622 NEXT_ARGP(); 623 623 624 ifindex = if_nametoindex(**arg 624 ifindex = if_nametoindex(**argv); 625 if (!ifindex) 625 if (!ifindex) 626 p_err("invalid devname 626 p_err("invalid devname %s", **argv); 627 627 628 NEXT_ARGP(); 628 NEXT_ARGP(); 629 } else { 629 } else { 630 p_err("expected 'dev', got: '% 630 p_err("expected 'dev', got: '%s'?", **argv); 631 return -1; 631 return -1; 632 } 632 } 633 633 634 return ifindex; 634 return ifindex; 635 } 635 } 636 636 637 static int do_attach_detach_xdp(int progfd, en 637 static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type, 638 int ifindex, b 638 int ifindex, bool overwrite) 639 { 639 { 640 __u32 flags = 0; 640 __u32 flags = 0; 641 641 642 if (!overwrite) 642 if (!overwrite) 643 flags = XDP_FLAGS_UPDATE_IF_NO 643 flags = XDP_FLAGS_UPDATE_IF_NOEXIST; 644 if (attach_type == NET_ATTACH_TYPE_XDP 644 if (attach_type == NET_ATTACH_TYPE_XDP_GENERIC) 645 flags |= XDP_FLAGS_SKB_MODE; 645 flags |= XDP_FLAGS_SKB_MODE; 646 if (attach_type == NET_ATTACH_TYPE_XDP 646 if (attach_type == NET_ATTACH_TYPE_XDP_DRIVER) 647 flags |= XDP_FLAGS_DRV_MODE; 647 flags |= XDP_FLAGS_DRV_MODE; 648 if (attach_type == NET_ATTACH_TYPE_XDP 648 if (attach_type == NET_ATTACH_TYPE_XDP_OFFLOAD) 649 flags |= XDP_FLAGS_HW_MODE; 649 flags |= XDP_FLAGS_HW_MODE; 650 650 651 return bpf_xdp_attach(ifindex, progfd, 651 return bpf_xdp_attach(ifindex, progfd, flags, NULL); 652 } 652 } 653 653 654 static int get_tcx_type(enum net_attach_type a 654 static int get_tcx_type(enum net_attach_type attach_type) 655 { 655 { 656 switch (attach_type) { 656 switch (attach_type) { 657 case NET_ATTACH_TYPE_TCX_INGRESS: 657 case NET_ATTACH_TYPE_TCX_INGRESS: 658 return BPF_TCX_INGRESS; 658 return BPF_TCX_INGRESS; 659 case NET_ATTACH_TYPE_TCX_EGRESS: 659 case NET_ATTACH_TYPE_TCX_EGRESS: 660 return BPF_TCX_EGRESS; 660 return BPF_TCX_EGRESS; 661 default: 661 default: 662 return -1; 662 return -1; 663 } 663 } 664 } 664 } 665 665 666 static int do_attach_tcx(int progfd, enum net_ 666 static int do_attach_tcx(int progfd, enum net_attach_type attach_type, int ifindex) 667 { 667 { 668 int type = get_tcx_type(attach_type); 668 int type = get_tcx_type(attach_type); 669 669 670 return bpf_prog_attach(progfd, ifindex 670 return bpf_prog_attach(progfd, ifindex, type, 0); 671 } 671 } 672 672 673 static int do_detach_tcx(int targetfd, enum ne 673 static int do_detach_tcx(int targetfd, enum net_attach_type attach_type) 674 { 674 { 675 int type = get_tcx_type(attach_type); 675 int type = get_tcx_type(attach_type); 676 676 677 return bpf_prog_detach(targetfd, type) 677 return bpf_prog_detach(targetfd, type); 678 } 678 } 679 679 680 static int do_attach(int argc, char **argv) 680 static int do_attach(int argc, char **argv) 681 { 681 { 682 enum net_attach_type attach_type; 682 enum net_attach_type attach_type; 683 int progfd, ifindex, err = 0; 683 int progfd, ifindex, err = 0; 684 bool overwrite = false; 684 bool overwrite = false; 685 685 686 /* parse attach args */ 686 /* parse attach args */ 687 if (!REQ_ARGS(5)) 687 if (!REQ_ARGS(5)) 688 return -EINVAL; 688 return -EINVAL; 689 689 690 attach_type = parse_attach_type(*argv) 690 attach_type = parse_attach_type(*argv); 691 if (attach_type == net_attach_type_siz 691 if (attach_type == net_attach_type_size) { 692 p_err("invalid net attach/deta 692 p_err("invalid net attach/detach type: %s", *argv); 693 return -EINVAL; 693 return -EINVAL; 694 } 694 } 695 NEXT_ARG(); 695 NEXT_ARG(); 696 696 697 progfd = prog_parse_fd(&argc, &argv); 697 progfd = prog_parse_fd(&argc, &argv); 698 if (progfd < 0) 698 if (progfd < 0) 699 return -EINVAL; 699 return -EINVAL; 700 700 701 ifindex = net_parse_dev(&argc, &argv); 701 ifindex = net_parse_dev(&argc, &argv); 702 if (ifindex < 1) { 702 if (ifindex < 1) { 703 err = -EINVAL; 703 err = -EINVAL; 704 goto cleanup; 704 goto cleanup; 705 } 705 } 706 706 707 if (argc) { 707 if (argc) { 708 if (is_prefix(*argv, "overwrit 708 if (is_prefix(*argv, "overwrite")) { 709 overwrite = true; 709 overwrite = true; 710 } else { 710 } else { 711 p_err("expected 'overw 711 p_err("expected 'overwrite', got: '%s'?", *argv); 712 err = -EINVAL; 712 err = -EINVAL; 713 goto cleanup; 713 goto cleanup; 714 } 714 } 715 } 715 } 716 716 717 switch (attach_type) { 717 switch (attach_type) { 718 /* attach xdp prog */ 718 /* attach xdp prog */ 719 case NET_ATTACH_TYPE_XDP: 719 case NET_ATTACH_TYPE_XDP: 720 case NET_ATTACH_TYPE_XDP_GENERIC: 720 case NET_ATTACH_TYPE_XDP_GENERIC: 721 case NET_ATTACH_TYPE_XDP_DRIVER: 721 case NET_ATTACH_TYPE_XDP_DRIVER: 722 case NET_ATTACH_TYPE_XDP_OFFLOAD: 722 case NET_ATTACH_TYPE_XDP_OFFLOAD: 723 err = do_attach_detach_xdp(pro 723 err = do_attach_detach_xdp(progfd, attach_type, ifindex, overwrite); 724 break; 724 break; 725 /* attach tcx prog */ 725 /* attach tcx prog */ 726 case NET_ATTACH_TYPE_TCX_INGRESS: 726 case NET_ATTACH_TYPE_TCX_INGRESS: 727 case NET_ATTACH_TYPE_TCX_EGRESS: 727 case NET_ATTACH_TYPE_TCX_EGRESS: 728 err = do_attach_tcx(progfd, at 728 err = do_attach_tcx(progfd, attach_type, ifindex); 729 break; 729 break; 730 default: 730 default: 731 break; 731 break; 732 } 732 } 733 733 734 if (err) { 734 if (err) { 735 p_err("interface %s attach fai 735 p_err("interface %s attach failed: %s", 736 attach_type_strings[atta 736 attach_type_strings[attach_type], strerror(-err)); 737 goto cleanup; 737 goto cleanup; 738 } 738 } 739 739 740 if (json_output) 740 if (json_output) 741 jsonw_null(json_wtr); 741 jsonw_null(json_wtr); 742 cleanup: 742 cleanup: 743 close(progfd); 743 close(progfd); 744 return err; 744 return err; 745 } 745 } 746 746 747 static int do_detach(int argc, char **argv) 747 static int do_detach(int argc, char **argv) 748 { 748 { 749 enum net_attach_type attach_type; 749 enum net_attach_type attach_type; 750 int progfd, ifindex, err = 0; 750 int progfd, ifindex, err = 0; 751 751 752 /* parse detach args */ 752 /* parse detach args */ 753 if (!REQ_ARGS(3)) 753 if (!REQ_ARGS(3)) 754 return -EINVAL; 754 return -EINVAL; 755 755 756 attach_type = parse_attach_type(*argv) 756 attach_type = parse_attach_type(*argv); 757 if (attach_type == net_attach_type_siz 757 if (attach_type == net_attach_type_size) { 758 p_err("invalid net attach/deta 758 p_err("invalid net attach/detach type: %s", *argv); 759 return -EINVAL; 759 return -EINVAL; 760 } 760 } 761 NEXT_ARG(); 761 NEXT_ARG(); 762 762 763 ifindex = net_parse_dev(&argc, &argv); 763 ifindex = net_parse_dev(&argc, &argv); 764 if (ifindex < 1) 764 if (ifindex < 1) 765 return -EINVAL; 765 return -EINVAL; 766 766 767 switch (attach_type) { 767 switch (attach_type) { 768 /* detach xdp prog */ 768 /* detach xdp prog */ 769 case NET_ATTACH_TYPE_XDP: 769 case NET_ATTACH_TYPE_XDP: 770 case NET_ATTACH_TYPE_XDP_GENERIC: 770 case NET_ATTACH_TYPE_XDP_GENERIC: 771 case NET_ATTACH_TYPE_XDP_DRIVER: 771 case NET_ATTACH_TYPE_XDP_DRIVER: 772 case NET_ATTACH_TYPE_XDP_OFFLOAD: 772 case NET_ATTACH_TYPE_XDP_OFFLOAD: 773 progfd = -1; 773 progfd = -1; 774 err = do_attach_detach_xdp(pro 774 err = do_attach_detach_xdp(progfd, attach_type, ifindex, NULL); 775 break; 775 break; 776 /* detach tcx prog */ 776 /* detach tcx prog */ 777 case NET_ATTACH_TYPE_TCX_INGRESS: 777 case NET_ATTACH_TYPE_TCX_INGRESS: 778 case NET_ATTACH_TYPE_TCX_EGRESS: 778 case NET_ATTACH_TYPE_TCX_EGRESS: 779 err = do_detach_tcx(ifindex, a 779 err = do_detach_tcx(ifindex, attach_type); 780 break; 780 break; 781 default: 781 default: 782 break; 782 break; 783 } 783 } 784 784 785 if (err < 0) { 785 if (err < 0) { 786 p_err("interface %s detach fai 786 p_err("interface %s detach failed: %s", 787 attach_type_strings[atta 787 attach_type_strings[attach_type], strerror(-err)); 788 return err; 788 return err; 789 } 789 } 790 790 791 if (json_output) 791 if (json_output) 792 jsonw_null(json_wtr); 792 jsonw_null(json_wtr); 793 793 794 return 0; 794 return 0; 795 } 795 } 796 796 797 static int netfilter_link_compar(const void *a 797 static int netfilter_link_compar(const void *a, const void *b) 798 { 798 { 799 const struct bpf_link_info *nfa = a; 799 const struct bpf_link_info *nfa = a; 800 const struct bpf_link_info *nfb = b; 800 const struct bpf_link_info *nfb = b; 801 int delta; 801 int delta; 802 802 803 delta = nfa->netfilter.pf - nfb->netfi 803 delta = nfa->netfilter.pf - nfb->netfilter.pf; 804 if (delta) 804 if (delta) 805 return delta; 805 return delta; 806 806 807 delta = nfa->netfilter.hooknum - nfb-> 807 delta = nfa->netfilter.hooknum - nfb->netfilter.hooknum; 808 if (delta) 808 if (delta) 809 return delta; 809 return delta; 810 810 811 if (nfa->netfilter.priority < nfb->net 811 if (nfa->netfilter.priority < nfb->netfilter.priority) 812 return -1; 812 return -1; 813 if (nfa->netfilter.priority > nfb->net 813 if (nfa->netfilter.priority > nfb->netfilter.priority) 814 return 1; 814 return 1; 815 815 816 return nfa->netfilter.flags - nfb->net 816 return nfa->netfilter.flags - nfb->netfilter.flags; 817 } 817 } 818 818 819 static void show_link_netfilter(void) 819 static void show_link_netfilter(void) 820 { 820 { 821 unsigned int nf_link_len = 0, nf_link_ 821 unsigned int nf_link_len = 0, nf_link_count = 0; 822 struct bpf_link_info *nf_link_info = N 822 struct bpf_link_info *nf_link_info = NULL; 823 __u32 id = 0; 823 __u32 id = 0; 824 824 825 while (true) { 825 while (true) { 826 struct bpf_link_info info; 826 struct bpf_link_info info; 827 int fd, err; 827 int fd, err; 828 __u32 len; 828 __u32 len; 829 829 830 err = bpf_link_get_next_id(id, 830 err = bpf_link_get_next_id(id, &id); 831 if (err) { 831 if (err) { 832 if (errno == ENOENT) 832 if (errno == ENOENT) 833 break; 833 break; 834 p_err("can't get next 834 p_err("can't get next link: %s (id %d)", strerror(errno), id); 835 break; 835 break; 836 } 836 } 837 837 838 fd = bpf_link_get_fd_by_id(id) 838 fd = bpf_link_get_fd_by_id(id); 839 if (fd < 0) { 839 if (fd < 0) { 840 p_err("can't get link 840 p_err("can't get link by id (%u): %s", id, strerror(errno)); 841 continue; 841 continue; 842 } 842 } 843 843 844 memset(&info, 0, sizeof(info)) 844 memset(&info, 0, sizeof(info)); 845 len = sizeof(info); 845 len = sizeof(info); 846 846 847 err = bpf_link_get_info_by_fd( 847 err = bpf_link_get_info_by_fd(fd, &info, &len); 848 848 849 close(fd); 849 close(fd); 850 850 851 if (err) { 851 if (err) { 852 p_err("can't get link 852 p_err("can't get link info for fd %d: %s", fd, strerror(errno)); 853 continue; 853 continue; 854 } 854 } 855 855 856 if (info.type != BPF_LINK_TYPE 856 if (info.type != BPF_LINK_TYPE_NETFILTER) 857 continue; 857 continue; 858 858 859 if (nf_link_count >= nf_link_l 859 if (nf_link_count >= nf_link_len) { 860 static const unsigned 860 static const unsigned int max_link_count = INT_MAX / sizeof(info); 861 struct bpf_link_info * 861 struct bpf_link_info *expand; 862 862 863 if (nf_link_count > ma 863 if (nf_link_count > max_link_count) { 864 p_err("cannot 864 p_err("cannot handle more than %u links\n", max_link_count); 865 break; 865 break; 866 } 866 } 867 867 868 nf_link_len += 16; 868 nf_link_len += 16; 869 869 870 expand = realloc(nf_li 870 expand = realloc(nf_link_info, nf_link_len * sizeof(info)); 871 if (!expand) { 871 if (!expand) { 872 p_err("realloc 872 p_err("realloc: %s", strerror(errno)); 873 break; 873 break; 874 } 874 } 875 875 876 nf_link_info = expand; 876 nf_link_info = expand; 877 } 877 } 878 878 879 nf_link_info[nf_link_count] = 879 nf_link_info[nf_link_count] = info; 880 nf_link_count++; 880 nf_link_count++; 881 } 881 } 882 882 883 if (!nf_link_info) 883 if (!nf_link_info) 884 return; 884 return; 885 885 886 qsort(nf_link_info, nf_link_count, siz 886 qsort(nf_link_info, nf_link_count, sizeof(*nf_link_info), netfilter_link_compar); 887 887 888 for (id = 0; id < nf_link_count; id++) 888 for (id = 0; id < nf_link_count; id++) { 889 NET_START_OBJECT; 889 NET_START_OBJECT; 890 if (json_output) 890 if (json_output) 891 netfilter_dump_json(&n 891 netfilter_dump_json(&nf_link_info[id], json_wtr); 892 else 892 else 893 netfilter_dump_plain(& 893 netfilter_dump_plain(&nf_link_info[id]); 894 894 895 NET_DUMP_UINT("id", " prog_id 895 NET_DUMP_UINT("id", " prog_id %u", nf_link_info[id].prog_id); 896 NET_END_OBJECT; 896 NET_END_OBJECT; 897 } 897 } 898 898 899 free(nf_link_info); 899 free(nf_link_info); 900 } 900 } 901 901 902 static int do_show(int argc, char **argv) 902 static int do_show(int argc, char **argv) 903 { 903 { 904 struct bpf_attach_info attach_info = { 904 struct bpf_attach_info attach_info = {}; 905 int i, sock, ret, filter_idx = -1; 905 int i, sock, ret, filter_idx = -1; 906 struct bpf_netdev_t dev_array; 906 struct bpf_netdev_t dev_array; 907 unsigned int nl_pid = 0; 907 unsigned int nl_pid = 0; 908 char err_buf[256]; 908 char err_buf[256]; 909 909 910 if (argc == 2) { 910 if (argc == 2) { 911 filter_idx = net_parse_dev(&ar 911 filter_idx = net_parse_dev(&argc, &argv); 912 if (filter_idx < 1) 912 if (filter_idx < 1) 913 return -1; 913 return -1; 914 } else if (argc != 0) { 914 } else if (argc != 0) { 915 usage(); 915 usage(); 916 } 916 } 917 917 918 ret = query_flow_dissector(&attach_inf 918 ret = query_flow_dissector(&attach_info); 919 if (ret) 919 if (ret) 920 return -1; 920 return -1; 921 921 922 sock = netlink_open(&nl_pid); 922 sock = netlink_open(&nl_pid); 923 if (sock < 0) { 923 if (sock < 0) { 924 fprintf(stderr, "failed to ope 924 fprintf(stderr, "failed to open netlink sock\n"); 925 return -1; 925 return -1; 926 } 926 } 927 927 928 dev_array.devices = NULL; 928 dev_array.devices = NULL; 929 dev_array.used_len = 0; 929 dev_array.used_len = 0; 930 dev_array.array_len = 0; 930 dev_array.array_len = 0; 931 dev_array.filter_idx = filter_idx; 931 dev_array.filter_idx = filter_idx; 932 932 933 if (json_output) 933 if (json_output) 934 jsonw_start_array(json_wtr); 934 jsonw_start_array(json_wtr); 935 NET_START_OBJECT; 935 NET_START_OBJECT; 936 NET_START_ARRAY("xdp", "%s:\n"); 936 NET_START_ARRAY("xdp", "%s:\n"); 937 ret = netlink_get_link(sock, nl_pid, d 937 ret = netlink_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array); 938 NET_END_ARRAY("\n"); 938 NET_END_ARRAY("\n"); 939 939 940 if (!ret) { 940 if (!ret) { 941 NET_START_ARRAY("tc", "%s:\n") 941 NET_START_ARRAY("tc", "%s:\n"); 942 for (i = 0; i < dev_array.used 942 for (i = 0; i < dev_array.used_len; i++) { 943 show_dev_tc_bpf(&dev_a 943 show_dev_tc_bpf(&dev_array.devices[i]); 944 ret = show_dev_tc_bpf_ 944 ret = show_dev_tc_bpf_classic(sock, nl_pid, 945 945 &dev_array.devices[i]); 946 if (ret) 946 if (ret) 947 break; 947 break; 948 } 948 } 949 NET_END_ARRAY("\n"); 949 NET_END_ARRAY("\n"); 950 } 950 } 951 951 952 NET_START_ARRAY("flow_dissector", "%s: 952 NET_START_ARRAY("flow_dissector", "%s:\n"); 953 if (attach_info.flow_dissector_id > 0) 953 if (attach_info.flow_dissector_id > 0) 954 NET_DUMP_UINT("id", "id %u", a 954 NET_DUMP_UINT("id", "id %u", attach_info.flow_dissector_id); 955 NET_END_ARRAY("\n"); 955 NET_END_ARRAY("\n"); 956 956 957 NET_START_ARRAY("netfilter", "%s:\n"); 957 NET_START_ARRAY("netfilter", "%s:\n"); 958 show_link_netfilter(); 958 show_link_netfilter(); 959 NET_END_ARRAY("\n"); 959 NET_END_ARRAY("\n"); 960 960 961 NET_END_OBJECT; 961 NET_END_OBJECT; 962 if (json_output) 962 if (json_output) 963 jsonw_end_array(json_wtr); 963 jsonw_end_array(json_wtr); 964 964 965 if (ret) { 965 if (ret) { 966 if (json_output) 966 if (json_output) 967 jsonw_null(json_wtr); 967 jsonw_null(json_wtr); 968 libbpf_strerror(ret, err_buf, 968 libbpf_strerror(ret, err_buf, sizeof(err_buf)); 969 fprintf(stderr, "Error: %s\n", 969 fprintf(stderr, "Error: %s\n", err_buf); 970 } 970 } 971 free(dev_array.devices); 971 free(dev_array.devices); 972 close(sock); 972 close(sock); 973 return ret; 973 return ret; 974 } 974 } 975 975 976 static int do_help(int argc, char **argv) 976 static int do_help(int argc, char **argv) 977 { 977 { 978 if (json_output) { 978 if (json_output) { 979 jsonw_null(json_wtr); 979 jsonw_null(json_wtr); 980 return 0; 980 return 0; 981 } 981 } 982 982 983 fprintf(stderr, 983 fprintf(stderr, 984 "Usage: %1$s %2$s { show | lis 984 "Usage: %1$s %2$s { show | list } [dev <devname>]\n" 985 " %1$s %2$s attach ATTAC 985 " %1$s %2$s attach ATTACH_TYPE PROG dev <devname> [ overwrite ]\n" 986 " %1$s %2$s detach ATTAC 986 " %1$s %2$s detach ATTACH_TYPE dev <devname>\n" 987 " %1$s %2$s help\n" 987 " %1$s %2$s help\n" 988 "\n" 988 "\n" 989 " " HELP_SPEC_PROGRAM "\ 989 " " HELP_SPEC_PROGRAM "\n" 990 " ATTACH_TYPE := { xdp | 990 " ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload | tcx_ingress\n" 991 " | tcx 991 " | tcx_egress }\n" 992 " " HELP_SPEC_OPTIONS " 992 " " HELP_SPEC_OPTIONS " }\n" 993 "\n" 993 "\n" 994 "Note: Only xdp, tcx, tc, netk 994 "Note: Only xdp, tcx, tc, netkit, flow_dissector and netfilter attachments\n" 995 " are currently supported 995 " are currently supported.\n" 996 " For progs attached to c 996 " For progs attached to cgroups, use \"bpftool cgroup\"\n" 997 " to dump program attachm 997 " to dump program attachments. For program types\n" 998 " sk_{filter,skb,msg,reus 998 " sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n" 999 " consult iproute2.\n" 999 " consult iproute2.\n" 1000 "", 1000 "", 1001 bin_name, argv[-2]); 1001 bin_name, argv[-2]); 1002 1002 1003 return 0; 1003 return 0; 1004 } 1004 } 1005 1005 1006 static const struct cmd cmds[] = { 1006 static const struct cmd cmds[] = { 1007 { "show", do_show }, 1007 { "show", do_show }, 1008 { "list", do_show }, 1008 { "list", do_show }, 1009 { "attach", do_attach }, 1009 { "attach", do_attach }, 1010 { "detach", do_detach }, 1010 { "detach", do_detach }, 1011 { "help", do_help }, 1011 { "help", do_help }, 1012 { 0 } 1012 { 0 } 1013 }; 1013 }; 1014 1014 1015 int do_net(int argc, char **argv) 1015 int do_net(int argc, char **argv) 1016 { 1016 { 1017 return cmd_select(cmds, argc, argv, d 1017 return cmd_select(cmds, argc, argv, do_help); 1018 } 1018 } 1019 1019
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.