5 #include <netinet/in.h>
6 #include <linux/netfilter.h> /* for NF_ACCEPT */
8 #include <libnfnetlink_queue/libnfnetlink_queue.h>
10 /* returns packet id */
11 static u_int32_t print_pkt (struct nfattr *tb[])
14 struct nfqnl_msg_packet_hdr *ph;
17 unsigned int datalength;
20 ph = nfqnl_get_msg_packet_hdr(tb);
22 id = ntohl(ph->packet_id);
23 printf("hw_protocol=0x%04x hook=%u id=%u ",
24 ntohs(ph->hw_protocol), ph->hook, id);
27 mark = nfqnl_get_nfmark(tb);
29 printf("mark=%u ", mark);
31 ifi = nfqnl_get_indev(tb);
33 printf("indev=%u ", ifi);
35 ifi = nfqnl_get_outdev(tb);
37 printf("outdev=%u ", ifi);
39 ret = nfqnl_get_payload(tb, &data, &datalength);
41 printf("payload_len=%d ", datalength);
49 static int cb(struct nfqnl_q_handle *qh, struct nfgenmsg *nfmsg,
50 struct nfattr *nfa[], void *data)
52 u_int32_t id = print_pkt(nfa);
53 printf("entering callback\n");
54 return nfqnl_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
57 int main(int argc, char **argv)
59 struct nfqnl_handle *h;
60 struct nfqnl_q_handle *qh;
61 struct nfnl_handle *nh;
66 printf("opening library handle\n");
69 fprintf(stderr, "error during nfqnl_open()\n");
73 printf("unbinding existing nf_queue handler for AF_INET (if any)\n");
74 if (nfqnl_unbind_pf(h, AF_INET) < 0) {
75 fprintf(stderr, "error during nfqnl_unbind_pf()\n");
79 printf("binding nfnetlink_queue as nf_queue handler for AF_INET\n");
80 if (nfqnl_bind_pf(h, AF_INET) < 0) {
81 fprintf(stderr, "error during nfqnl_bind_pf()\n");
85 printf("binding this socket to queue '0'\n");
86 qh = nfqnl_create_queue(h, 0, &cb, NULL);
88 fprintf(stderr, "error during nfqnl_create_queue()\n");
92 printf("setting copy_packet mode\n");
93 if (nfqnl_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
94 fprintf(stderr, "can't set packet_copy mode\n");
101 while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
102 printf("pkt received\n");
103 nfqnl_handle_packet(h, buf, rv);
106 printf("unbinding from queue 0\n");
107 nfqnl_destroy_queue(qh);
110 /* normally, applications SHOULD NOT issue this command, since
111 * it detaches other programs/sockets from AF_INET, too ! */
112 printf("unbinding from AF_INET\n");
113 nfqnl_unbind_pf(h, AF_INET);
116 printf("closing library handle\n");