14 #include <netlink/cli/utils.h>
15 #include <netlink/cli/link.h>
16 #include <netinet/in.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nfnetlink_queue.h>
19 #include <netlink/netfilter/nfnl.h>
20 #include <netlink/netfilter/queue.h>
21 #include <netlink/netfilter/queue_msg.h>
23 static struct nl_sock *nf_sock;
25 static struct nfnl_queue *alloc_queue(
void)
27 struct nfnl_queue *queue;
29 queue = nfnl_queue_alloc();
37 static void obj_input(
struct nl_object *obj,
void *arg)
39 struct nfnl_queue_msg *msg = (
struct nfnl_queue_msg *) obj;
46 nfnl_queue_msg_set_verdict(msg, NF_ACCEPT);
48 nfnl_queue_msg_send_verdict(nf_sock, msg);
51 static int event_input(
struct nl_msg *msg,
void *arg)
53 if (nl_msg_parse(msg, &obj_input, NULL) < 0)
54 fprintf(stderr,
"<<EVENT>> Unknown message type\n");
60 int main(
int argc,
char *argv[])
62 struct nl_sock *rt_sock;
63 struct nl_cache *link_cache;
64 struct nfnl_queue *queue;
65 enum nfnl_queue_copy_mode copy_mode;
70 nf_sock = nfnl_queue_socket_alloc();
72 nl_cli_fatal(ENOBUFS,
"Unable to allocate netlink socket");
77 if ((argc > 1 && !strcasecmp(argv[1],
"-h")) || argc < 3) {
78 printf(
"Usage: nf-queue family group [ copy_mode ] "
80 printf(
"family: [ inet | inet6 | ... ] \n");
81 printf(
"group: the --queue-num arg that you gave to iptables\n");
82 printf(
"copy_mode: [ none | meta | packet ] \n");
86 nl_cli_connect(nf_sock, NETLINK_NETFILTER);
88 if ((family = nl_str2af(argv[1])) == AF_UNSPEC)
89 nl_cli_fatal(NLE_INVAL,
"Unknown family \"%s\"", argv[1]);
91 nfnl_queue_pf_unbind(nf_sock, family);
92 if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0)
96 queue = alloc_queue();
97 nfnl_queue_set_group(queue, atoi(argv[2]));
99 copy_mode = NFNL_QUEUE_COPY_PACKET;
101 copy_mode = nfnl_queue_str2copy_mode(argv[3]);
104 "Unable to parse copy mode \"%s\": %s",
105 argv[3], nl_geterror(copy_mode));
107 nfnl_queue_set_copy_mode(queue, copy_mode);
111 copy_range = atoi(argv[4]);
112 nfnl_queue_set_copy_range(queue, copy_range);
114 if ((err = nfnl_queue_create(nf_sock, queue)) < 0)
115 nl_cli_fatal(err,
"Unable to bind queue: %s", nl_geterror(err));
117 rt_sock = nl_cli_alloc_socket();
118 nl_cli_connect(rt_sock, NETLINK_ROUTE);
119 link_cache = nl_cli_link_alloc_cache(rt_sock);
125 int nffd, rtfd, maxfd, retval;
138 retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
141 if (FD_ISSET(nffd, &rfds))
143 if (FD_ISSET(rtfd, &rfds))