|
3 | 3 |
|
4 | 4 | #include <errno.h>
|
5 | 5 | #include <linux/err.h>
|
| 6 | +#include <linux/netfilter.h> |
| 7 | +#include <linux/netfilter_arp.h> |
6 | 8 | #include <net/if.h>
|
7 | 9 | #include <stdio.h>
|
8 | 10 | #include <unistd.h>
|
@@ -135,6 +137,18 @@ static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
|
135 | 137 | }
|
136 | 138 | }
|
137 | 139 |
|
| 140 | +void netfilter_dump_json(const struct bpf_link_info *info, json_writer_t *wtr) |
| 141 | +{ |
| 142 | + jsonw_uint_field(json_wtr, "pf", |
| 143 | + info->netfilter.pf); |
| 144 | + jsonw_uint_field(json_wtr, "hook", |
| 145 | + info->netfilter.hooknum); |
| 146 | + jsonw_int_field(json_wtr, "prio", |
| 147 | + info->netfilter.priority); |
| 148 | + jsonw_uint_field(json_wtr, "flags", |
| 149 | + info->netfilter.flags); |
| 150 | +} |
| 151 | + |
138 | 152 | static int get_prog_info(int prog_id, struct bpf_prog_info *info)
|
139 | 153 | {
|
140 | 154 | __u32 len = sizeof(*info);
|
@@ -195,6 +209,10 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
|
195 | 209 | info->netns.netns_ino);
|
196 | 210 | show_link_attach_type_json(info->netns.attach_type, json_wtr);
|
197 | 211 | break;
|
| 212 | + case BPF_LINK_TYPE_NETFILTER: |
| 213 | + netfilter_dump_json(info, json_wtr); |
| 214 | + break; |
| 215 | + |
198 | 216 | default:
|
199 | 217 | break;
|
200 | 218 | }
|
@@ -263,6 +281,68 @@ static void show_iter_plain(struct bpf_link_info *info)
|
263 | 281 | }
|
264 | 282 | }
|
265 | 283 |
|
| 284 | +static const char * const pf2name[] = { |
| 285 | + [NFPROTO_INET] = "inet", |
| 286 | + [NFPROTO_IPV4] = "ip", |
| 287 | + [NFPROTO_ARP] = "arp", |
| 288 | + [NFPROTO_NETDEV] = "netdev", |
| 289 | + [NFPROTO_BRIDGE] = "bridge", |
| 290 | + [NFPROTO_IPV6] = "ip6", |
| 291 | +}; |
| 292 | + |
| 293 | +static const char * const inethook2name[] = { |
| 294 | + [NF_INET_PRE_ROUTING] = "prerouting", |
| 295 | + [NF_INET_LOCAL_IN] = "input", |
| 296 | + [NF_INET_FORWARD] = "forward", |
| 297 | + [NF_INET_LOCAL_OUT] = "output", |
| 298 | + [NF_INET_POST_ROUTING] = "postrouting", |
| 299 | +}; |
| 300 | + |
| 301 | +static const char * const arphook2name[] = { |
| 302 | + [NF_ARP_IN] = "input", |
| 303 | + [NF_ARP_OUT] = "output", |
| 304 | +}; |
| 305 | + |
| 306 | +void netfilter_dump_plain(const struct bpf_link_info *info) |
| 307 | +{ |
| 308 | + const char *hookname = NULL, *pfname = NULL; |
| 309 | + unsigned int hook = info->netfilter.hooknum; |
| 310 | + unsigned int pf = info->netfilter.pf; |
| 311 | + |
| 312 | + if (pf < ARRAY_SIZE(pf2name)) |
| 313 | + pfname = pf2name[pf]; |
| 314 | + |
| 315 | + switch (pf) { |
| 316 | + case NFPROTO_BRIDGE: /* bridge shares numbers with enum nf_inet_hooks */ |
| 317 | + case NFPROTO_IPV4: |
| 318 | + case NFPROTO_IPV6: |
| 319 | + case NFPROTO_INET: |
| 320 | + if (hook < ARRAY_SIZE(inethook2name)) |
| 321 | + hookname = inethook2name[hook]; |
| 322 | + break; |
| 323 | + case NFPROTO_ARP: |
| 324 | + if (hook < ARRAY_SIZE(arphook2name)) |
| 325 | + hookname = arphook2name[hook]; |
| 326 | + default: |
| 327 | + break; |
| 328 | + } |
| 329 | + |
| 330 | + if (pfname) |
| 331 | + printf("\n\t%s", pfname); |
| 332 | + else |
| 333 | + printf("\n\tpf: %d", pf); |
| 334 | + |
| 335 | + if (hookname) |
| 336 | + printf(" %s", hookname); |
| 337 | + else |
| 338 | + printf(", hook %u,", hook); |
| 339 | + |
| 340 | + printf(" prio %d", info->netfilter.priority); |
| 341 | + |
| 342 | + if (info->netfilter.flags) |
| 343 | + printf(" flags 0x%x", info->netfilter.flags); |
| 344 | +} |
| 345 | + |
266 | 346 | static int show_link_close_plain(int fd, struct bpf_link_info *info)
|
267 | 347 | {
|
268 | 348 | struct bpf_prog_info prog_info;
|
@@ -301,6 +381,9 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
|
301 | 381 | printf("\n\tnetns_ino %u ", info->netns.netns_ino);
|
302 | 382 | show_link_attach_type_plain(info->netns.attach_type);
|
303 | 383 | break;
|
| 384 | + case BPF_LINK_TYPE_NETFILTER: |
| 385 | + netfilter_dump_plain(info); |
| 386 | + break; |
304 | 387 | default:
|
305 | 388 | break;
|
306 | 389 | }
|
|
0 commit comments