Skip to content

Instantly share code, notes, and snippets.

@sbz
Last active May 25, 2018 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sbz/11148971 to your computer and use it in GitHub Desktop.
Save sbz/11148971 to your computer and use it in GitHub Desktop.
pf: ioctl get rules call debugging
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#ifdef __FreeBSD__
#include <sys/endian.h>
#endif
#include <net/if.h>
#include <net/pfvar.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void
breakpoint(void) {
asm volatile("int $3");
}
int
main(void) {
int dev;
int rc;
char *pf_device = "/dev/pf";
struct pfioc_rule pr;
dev = open(pf_device, O_RDONLY);
if (dev == -1)
errx(1, "open: can't open device %s", pf_device);
memset(&pr, 0, sizeof(pr));
if (ioctl(dev, DIOCGETRULES, &pr)) {
errx(1, "ioctl: DIOCGETRULES");
}
#ifdef NDEBUG
breakpoint();
#endif
printf("%x\n", pr);
printf("GETRULES: %lu\n", DIOCGETRULES);
return 0;
}
@sbz
Copy link
Author

sbz commented Apr 21, 2014

  • Compile it with:
% gcc -o pgr pf_getrules.c -g -D NDEBUG
  • Run debugger in privilege mode because /dev/pf is owned by root:
% sudo gdb -q ./pgr
(gdb) r
Starting program: /tmp/pgr 

Program received signal SIGTRAP, Trace/breakpoint trap.
breakpoint () at pgr.c:23
23  }
(gdb) list
18  #include <unistd.h>
19  
20  static void
21  breakpoint(void) {
22      asm volatile("int $3");
23  }
24  
25  int
26  main(void) {
27      int dev;
(gdb) n
main () at pgrc.c:46
46      printf("%x\n", pr);
(gdb) print pr
$1 = {action = 0, ticket = 4, pool_ticket = 0, nr = 16, anchor = '\0' <repeats 1023 times>, anchor_call = '\0' <repeats 1023 times>, rule = {src = {addr = {v = {a = {addr = {pfa = {v4 = {s_addr = 0}, v6 = {__u6_addr = {__u6_addr8 = '\0' <repeats 15 times>, __u6_addr16 = {
                      0, 0, 0, 0, 0, 0, 0, 0}, __u6_addr32 = {0, 0, 0, 0}}}, addr8 = '\0' <repeats 15 times>, addr16 = {0, 0, 0, 0, 0, 0, 0, 0}, addr32 = {0, 0, 0, 0}}}, mask = {pfa = {v4 = {s_addr = 0}, v6 = {__u6_addr = {__u6_addr8 = '\0' <repeats 15 times>, 
                    __u6_addr16 = {0, 0, 0, 0, 0, 0, 0, 0}, __u6_addr32 = {0, 0, 0, 0}}}, addr8 = '\0' <repeats 15 times>, addr16 = {0, 0, 0, 0, 0, 0, 0, 0}, addr32 = {0, 0, 0, 0}}}}, ifname = '\0' <repeats 15 times>, tblname = '\0' <repeats 31 times>}, p = {dyn = 0x0, 
          tbl = 0x0, dyncnt = 0, tblcnt = 0}, type = 0 '\0', iflags = 0 '\0'}, port = {0, 0}, neg = 0 '\0', port_op = 0 '\0'}, dst = {addr = {v = {a = {addr = {pfa = {v4 = {s_addr = 0}, v6 = {__u6_addr = {__u6_addr8 = '\0' <repeats 15 times>, __u6_addr16 = {0, 0, 0, 0, 
                      0, 0, 0, 0}, __u6_addr32 = {0, 0, 0, 0}}}, addr8 = '\0' <repeats 15 times>, addr16 = {0, 0, 0, 0, 0, 0, 0, 0}, addr32 = {0, 0, 0, 0}}}, mask = {pfa = {v4 = {s_addr = 0}, v6 = {__u6_addr = {__u6_addr8 = '\0' <repeats 15 times>, __u6_addr16 = {0, 0, 
                      0, 0, 0, 0, 0, 0}, __u6_addr32 = {0, 0, 0, 0}}}, addr8 = '\0' <repeats 15 times>, addr16 = {0, 0, 0, 0, 0, 0, 0, 0}, addr32 = {0, 0, 0, 0}}}}, ifname = '\0' <repeats 15 times>, tblname = '\0' <repeats 31 times>}, p = {dyn = 0x0, tbl = 0x0, 
          dyncnt = 0, tblcnt = 0}, type = 0 '\0', iflags = 0 '\0'}, port = {0, 0}, neg = 0 '\0', port_op = 0 '\0'}, skip = {{ptr = 0x0, nr = 0}, {ptr = 0x0, nr = 0}, {ptr = 0x0, nr = 0}, {ptr = 0x0, nr = 0}, {ptr = 0x0, nr = 0}, {ptr = 0x0, nr = 0}, {ptr = 0x0, nr = 0}, {
        ptr = 0x0, nr = 0}}, label = '\0' <repeats 63 times>, ifname = '\0' <repeats 15 times>, qname = '\0' <repeats 63 times>, pqname = '\0' <repeats 63 times>, tagname = '\0' <repeats 63 times>, match_tagname = '\0' <repeats 63 times>, 
    overload_tblname = '\0' <repeats 31 times>, entries = {tqe_next = 0x0, tqe_prev = 0x0}, rpool = {list = {tqh_first = 0x0, tqh_last = 0x0}, cur = 0x0, key = {pfk = {key8 = '\0' <repeats 15 times>, key16 = {0, 0, 0, 0, 0, 0, 0, 0}, key32 = {0, 0, 0, 0}}}, counter = {
        pfa = {v4 = {s_addr = 0}, v6 = {__u6_addr = {__u6_addr8 = '\0' <repeats 15 times>, __u6_addr16 = {0, 0, 0, 0, 0, 0, 0, 0}, __u6_addr32 = {0, 0, 0, 0}}}, addr8 = '\0' <repeats 15 times>, addr16 = {0, 0, 0, 0, 0, 0, 0, 0}, addr32 = {0, 0, 0, 0}}}, tblidx = 0, 
      proxy_port = {0, 0}, opts = 0 '\0'}, evaluations = 0, packets = {0, 0}, bytes = {0, 0}, kif = 0x0, anchor = 0x0, overload_tbl = 0x0, os_fingerprint = 0, rtableid = 0, timeout = {0 <repeats 20 times>}, states_cur = 0, states_tot = 0, max_states = 0, src_nodes = 0, 
    max_src_nodes = 0, max_src_states = 0, spare1 = 0, max_src_conn = 0, max_src_conn_rate = {limit = 0, seconds = 0}, qid = 0, pqid = 0, rt_listid = 0, nr = 0, prob = 0, cuid = 0, cpid = 0, return_icmp = 0, return_icmp6 = 0, max_mss = 0, tag = 0, match_tag = 0, 
    spare2 = 0, uid = {uid = {0, 0}, op = 0 '\0'}, gid = {gid = {0, 0}, op = 0 '\0'}, rule_flag = 0, action = 0 '\0', direction = 0 '\0', log = 0 '\0', logif = 0 '\0', quick = 0 '\0', ifnot = 0 '\0', match_tag_not = 0 '\0', natpass = 0 '\0', keep_state = 0 '\0', 
    af = 0 '\0', proto = 0 '\0', type = 0 '\0', code = 0 '\0', flags = 0 '\0', flagset = 0 '\0', min_ttl = 0 '\0', allow_opts = 0 '\0', rt = 0 '\0', return_ttl = 0 '\0', tos = 0 '\0', set_tos = 0 '\0', anchor_relative = 0 '\0', anchor_wildcard = 0 '\0', flush = 0 '\0', 
    divert = {addr = {pfa = {v4 = {s_addr = 0}, v6 = {__u6_addr = {__u6_addr8 = '\0' <repeats 15 times>, __u6_addr16 = {0, 0, 0, 0, 0, 0, 0, 0}, __u6_addr32 = {0, 0, 0, 0}}}, addr8 = '\0' <repeats 15 times>, addr16 = {0, 0, 0, 0, 0, 0, 0, 0}, addr32 = {0, 0, 0, 0}}}, 
      port = 0}}}
(gdb) c
Continuing.
ffffda48
GETRULES: 3417850886

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment