2 * src/nf-queue.c Monitor netfilter queue events
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net>
10 * Copyright (c) 2010 Karl Hiramoto <karl@hiramoto.org>
14 #include <netlink/cli/utils.h>
15 #include <netlink/cli/link.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink_queue.h>
18 #include <netlink/netfilter/nfnl.h>
19 #include <netlink/netfilter/queue.h>
20 #include <netlink/netfilter/queue_msg.h>
22 static struct nl_sock *nf_sock;
24 static struct nfnl_queue *alloc_queue(void)
26 struct nfnl_queue *queue;
28 queue = nfnl_queue_alloc();
30 nl_cli_fatal(ENOMEM, "Unable to allocate queue object");
36 static void obj_input(struct nl_object *obj, void *arg)
38 struct nfnl_queue_msg *msg = (struct nfnl_queue_msg *) obj;
39 struct nl_dump_params dp = {
40 .dp_type = NL_DUMP_STATS,
44 uint32_t packet_id = nfnl_queue_msg_get_packetid(msg);
45 static uint32_t next_packet_id = 0;
46 struct nfnl_queue_msg *lost_msg = NULL;
50 if (packet_id > next_packet_id) {
51 printf("Warning: %d Out of order packets. Queue or socket overload \n", packet_id - next_packet_id);
52 group = nfnl_queue_msg_get_group(msg);
53 family = nfnl_queue_msg_get_family(msg);
54 lost_msg = nfnl_queue_msg_alloc();
57 nfnl_queue_msg_set_group(lost_msg, group);
58 nfnl_queue_msg_set_family(lost_msg, family);
59 nfnl_queue_msg_set_packetid(lost_msg, next_packet_id);
60 nfnl_queue_msg_set_verdict(lost_msg, NF_ACCEPT);
61 nfnl_queue_msg_send_verdict(nf_sock, lost_msg);
63 } while (packet_id > next_packet_id);
65 nfnl_queue_msg_put(lost_msg);
68 next_packet_id = packet_id + 1;
69 nfnl_queue_msg_set_verdict(msg, NF_ACCEPT);
70 nl_object_dump(obj, &dp);
71 nfnl_queue_msg_send_verdict(nf_sock, msg);
74 static int event_input(struct nl_msg *msg, void *arg)
76 if (nl_msg_parse(msg, &obj_input, NULL) < 0)
77 fprintf(stderr, "<<EVENT>> Unknown message type\n");
79 /* Exit nl_recvmsgs_def() and return to the main select() */
83 int main(int argc, char *argv[])
85 struct nl_sock *rt_sock;
86 struct nl_cache *link_cache;
87 struct nfnl_queue *queue;
88 enum nfnl_queue_copy_mode copy_mode;
93 nf_sock = nfnl_queue_socket_alloc();
95 nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
97 nl_socket_disable_seq_check(nf_sock);
98 nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
100 if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) {
101 printf("Usage: nf-queue family group [ copy_mode ] "
103 printf("family: [ inet | inet6 | ... ] \n");
104 printf("group: the --queue-num arg that you gave to iptables\n");
105 printf("copy_mode: [ none | meta | packet ] \n");
109 nl_cli_connect(nf_sock, NETLINK_NETFILTER);
111 if ((family = nl_str2af(argv[1])) == AF_UNSPEC)
112 nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\"", argv[1]);
114 nfnl_queue_pf_unbind(nf_sock, family);
115 if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0)
116 nl_cli_fatal(err, "Unable to bind logger: %s",
119 queue = alloc_queue();
120 nfnl_queue_set_group(queue, atoi(argv[2]));
122 copy_mode = NFNL_QUEUE_COPY_PACKET;
124 copy_mode = nfnl_queue_str2copy_mode(argv[3]);
126 nl_cli_fatal(copy_mode,
127 "Unable to parse copy mode \"%s\": %s",
128 argv[3], nl_geterror(copy_mode));
130 nfnl_queue_set_copy_mode(queue, copy_mode);
134 copy_range = atoi(argv[4]);
135 nfnl_queue_set_copy_range(queue, copy_range);
137 if ((err = nfnl_queue_create(nf_sock, queue)) < 0)
138 nl_cli_fatal(err, "Unable to bind queue: %s", nl_geterror(err));
140 rt_sock = nl_cli_alloc_socket();
141 nl_cli_connect(rt_sock, NETLINK_ROUTE);
142 link_cache = nl_cli_link_alloc_cache(rt_sock);
144 nl_socket_set_buffer_size(nf_sock, 1024*127, 1024*127);
148 int nffd, rtfd, maxfd, retval;
152 maxfd = nffd = nl_socket_get_fd(nf_sock);
155 rtfd = nl_socket_get_fd(rt_sock);
160 /* wait for an incoming message on the netlink socket */
161 retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
164 if (FD_ISSET(nffd, &rfds))
165 nl_recvmsgs_default(nf_sock);
166 if (FD_ISSET(rtfd, &rfds))
167 nl_recvmsgs_default(rt_sock);