1 // SPDX-License-Identifier: GPL-2.0
11 #include <arpa/inet.h>
13 #include <libmnl/libmnl.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nfnetlink.h>
16 #include <linux/netfilter/nfnetlink_queue.h>
22 unsigned int queue_num;
28 static unsigned int queue_stats[5];
29 static struct options opts;
31 static void help(const char *p)
33 printf("Usage: %s [-c|-v [-vv] ] [-t timeout] [-q queue_num] [-Qdst_queue ] [ -d ms_delay ] [-G]\n", p);
36 static int parse_attr_cb(const struct nlattr *attr, void *data)
38 const struct nlattr **tb = data;
39 int type = mnl_attr_get_type(attr);
41 /* skip unsupported attribute in user-space */
42 if (mnl_attr_type_valid(attr, NFQA_MAX) < 0)
47 case NFQA_IFINDEX_INDEV:
48 case NFQA_IFINDEX_OUTDEV:
49 case NFQA_IFINDEX_PHYSINDEV:
50 case NFQA_IFINDEX_PHYSOUTDEV:
51 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
52 perror("mnl_attr_validate");
57 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
58 sizeof(struct nfqnl_msg_packet_timestamp)) < 0) {
59 perror("mnl_attr_validate2");
64 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
65 sizeof(struct nfqnl_msg_packet_hw)) < 0) {
66 perror("mnl_attr_validate2");
77 static int queue_cb(const struct nlmsghdr *nlh, void *data)
79 struct nlattr *tb[NFQA_MAX+1] = { 0 };
80 struct nfqnl_msg_packet_hdr *ph = NULL;
85 mnl_attr_parse(nlh, sizeof(struct nfgenmsg), parse_attr_cb, tb);
86 if (tb[NFQA_PACKET_HDR]) {
87 ph = mnl_attr_get_payload(tb[NFQA_PACKET_HDR]);
88 id = ntohl(ph->packet_id);
91 printf("packet hook=%u, hwproto 0x%x",
92 ntohs(ph->hw_protocol), ph->hook);
95 fprintf(stderr, "Unknown hook %d\n", ph->hook);
99 if (opts.verbose > 0) {
100 uint32_t skbinfo = 0;
102 if (tb[NFQA_SKB_INFO])
103 skbinfo = ntohl(mnl_attr_get_u32(tb[NFQA_SKB_INFO]));
104 if (skbinfo & NFQA_SKB_CSUMNOTREADY)
105 printf(" csumnotready");
106 if (skbinfo & NFQA_SKB_GSO)
108 if (skbinfo & NFQA_SKB_CSUM_NOTVERIFIED)
109 printf(" csumnotverified");
113 if (opts.count_packets)
114 queue_stats[ph->hook]++;
117 return MNL_CB_OK + id;
120 static struct nlmsghdr *
121 nfq_build_cfg_request(char *buf, uint8_t command, int queue_num)
123 struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
124 struct nfqnl_msg_config_cmd cmd = {
126 .pf = htons(AF_INET),
128 struct nfgenmsg *nfg;
130 nlh->nlmsg_type = (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_CONFIG;
131 nlh->nlmsg_flags = NLM_F_REQUEST;
133 nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
135 nfg->nfgen_family = AF_UNSPEC;
136 nfg->version = NFNETLINK_V0;
137 nfg->res_id = htons(queue_num);
139 mnl_attr_put(nlh, NFQA_CFG_CMD, sizeof(cmd), &cmd);
144 static struct nlmsghdr *
145 nfq_build_cfg_params(char *buf, uint8_t mode, int range, int queue_num)
147 struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
148 struct nfqnl_msg_config_params params = {
149 .copy_range = htonl(range),
152 struct nfgenmsg *nfg;
154 nlh->nlmsg_type = (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_CONFIG;
155 nlh->nlmsg_flags = NLM_F_REQUEST;
157 nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
158 nfg->nfgen_family = AF_UNSPEC;
159 nfg->version = NFNETLINK_V0;
160 nfg->res_id = htons(queue_num);
162 mnl_attr_put(nlh, NFQA_CFG_PARAMS, sizeof(params), ¶ms);
167 static struct nlmsghdr *
168 nfq_build_verdict(char *buf, int id, int queue_num, uint32_t verd)
170 struct nfqnl_msg_verdict_hdr vh = {
171 .verdict = htonl(verd),
174 struct nlmsghdr *nlh;
175 struct nfgenmsg *nfg;
177 nlh = mnl_nlmsg_put_header(buf);
178 nlh->nlmsg_type = (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_VERDICT;
179 nlh->nlmsg_flags = NLM_F_REQUEST;
180 nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
181 nfg->nfgen_family = AF_UNSPEC;
182 nfg->version = NFNETLINK_V0;
183 nfg->res_id = htons(queue_num);
185 mnl_attr_put(nlh, NFQA_VERDICT_HDR, sizeof(vh), &vh);
190 static void print_stats(void)
192 unsigned int last, total;
196 last = queue_stats[0];
198 for (i = 0; i < 5; i++) {
199 printf("hook %d packets %08u\n", i, queue_stats[i]);
200 last = queue_stats[i];
204 printf("%u packets total\n", total);
207 struct mnl_socket *open_queue(void)
209 char buf[MNL_SOCKET_BUFFER_SIZE];
210 unsigned int queue_num;
211 struct mnl_socket *nl;
212 struct nlmsghdr *nlh;
216 nl = mnl_socket_open(NETLINK_NETFILTER);
218 perror("mnl_socket_open");
222 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
223 perror("mnl_socket_bind");
227 queue_num = opts.queue_num;
228 nlh = nfq_build_cfg_request(buf, NFQNL_CFG_CMD_BIND, queue_num);
230 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
231 perror("mnl_socket_sendto");
235 nlh = nfq_build_cfg_params(buf, NFQNL_COPY_PACKET, 0xFFFF, queue_num);
237 flags = opts.gso_enabled ? NFQA_CFG_F_GSO : 0;
238 flags |= NFQA_CFG_F_UID_GID;
239 mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(flags));
240 mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(flags));
242 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
243 perror("mnl_socket_sendto");
247 memset(&tv, 0, sizeof(tv));
248 tv.tv_sec = opts.timeout;
249 if (opts.timeout && setsockopt(mnl_socket_get_fd(nl),
250 SOL_SOCKET, SO_RCVTIMEO,
252 perror("setsockopt(SO_RCVTIMEO)");
259 static void sleep_ms(uint32_t delay)
261 struct timespec ts = { .tv_sec = delay / 1000 };
265 ts.tv_nsec = delay * 1000llu * 1000llu;
267 nanosleep(&ts, NULL);
270 static int mainloop(void)
272 unsigned int buflen = 64 * 1024 + MNL_SOCKET_BUFFER_SIZE;
273 struct mnl_socket *nl;
274 struct nlmsghdr *nlh;
279 buf = malloc(buflen);
286 portid = mnl_socket_get_portid(nl);
291 ret = mnl_socket_recvfrom(nl, buf, buflen);
293 if (errno == ENOBUFS || errno == EINTR)
296 if (errno == EAGAIN) {
302 perror("mnl_socket_recvfrom");
306 ret = mnl_cb_run(buf, ret, 0, portid, queue_cb, NULL);
308 perror("mnl_cb_run");
312 id = ret - MNL_CB_OK;
314 sleep_ms(opts.delay_ms);
316 nlh = nfq_build_verdict(buf, id, opts.queue_num, opts.verdict);
317 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
318 perror("mnl_socket_sendto");
323 mnl_socket_close(nl);
328 static void parse_opts(int argc, char **argv)
332 while ((c = getopt(argc, argv, "chvt:q:Q:d:G")) != -1) {
335 opts.count_packets = true;
342 opts.queue_num = atoi(optarg);
343 if (opts.queue_num > 0xffff)
347 opts.verdict = atoi(optarg);
348 if (opts.verdict > 0xffff) {
349 fprintf(stderr, "Expected destination queue number\n");
354 opts.verdict |= NF_QUEUE;
357 opts.delay_ms = atoi(optarg);
358 if (opts.delay_ms == 0) {
359 fprintf(stderr, "Expected nonzero delay (in milliseconds)\n");
364 opts.timeout = atoi(optarg);
367 opts.gso_enabled = false;
375 if (opts.verdict != NF_ACCEPT && (opts.verdict >> 16 == opts.queue_num)) {
376 fprintf(stderr, "Cannot use same destination and source queue\n");
381 int main(int argc, char *argv[])
385 opts.verdict = NF_ACCEPT;
386 opts.gso_enabled = true;
388 parse_opts(argc, argv);
391 if (opts.count_packets)