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 #include <linux/netlink.h> 25 static struct nl_sock *nf_sock;
27 static struct nfnl_queue *alloc_queue(
void)
29 struct nfnl_queue *queue;
31 queue = nfnl_queue_alloc();
39 static void obj_input(
struct nl_object *obj,
void *arg)
41 struct nfnl_queue_msg *msg = (
struct nfnl_queue_msg *) obj;
48 nfnl_queue_msg_set_verdict(msg, NF_ACCEPT);
50 nfnl_queue_msg_send_verdict(nf_sock, msg);
53 static int event_input(
struct nl_msg *msg,
void *arg)
55 if (nl_msg_parse(msg, &obj_input, NULL) < 0)
56 fprintf(stderr,
"<<EVENT>> Unknown message type\n");
62 int main(
int argc,
char *argv[])
64 struct nl_sock *rt_sock;
65 struct nfnl_queue *queue;
71 nf_sock = nfnl_queue_socket_alloc();
73 nl_cli_fatal(ENOBUFS,
"Unable to allocate netlink socket");
78 if ((argc > 1 && !strcasecmp(argv[1],
"-h")) || argc < 3) {
79 printf(
"Usage: nf-queue family group [ copy_mode ] " 81 printf(
"family: [ inet | inet6 | ... ] \n");
82 printf(
"group: the --queue-num arg that you gave to iptables\n");
83 printf(
"copy_mode: [ none | meta | packet ] \n");
87 nl_cli_connect(nf_sock, NETLINK_NETFILTER);
89 if ((family = nl_str2af(argv[1])) == AF_UNSPEC)
90 nl_cli_fatal(NLE_INVAL,
"Unknown family \"%s\"", argv[1]);
92 nfnl_queue_pf_unbind(nf_sock, family);
93 if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0)
97 queue = alloc_queue();
98 nfnl_queue_set_group(queue, atoi(argv[2]));
100 copy_mode = NFNL_QUEUE_COPY_PACKET;
102 copy_mode = nfnl_queue_str2copy_mode(argv[3]);
105 "Unable to parse copy mode \"%s\": %s",
106 argv[3], nl_geterror(copy_mode));
108 nfnl_queue_set_copy_mode(queue, copy_mode);
112 copy_range = atoi(argv[4]);
113 nfnl_queue_set_copy_range(queue, copy_range);
115 if ((err = nfnl_queue_create(nf_sock, queue)) < 0)
116 nl_cli_fatal(err,
"Unable to bind queue: %s", nl_geterror(err));
118 rt_sock = nl_cli_alloc_socket();
119 nl_cli_connect(rt_sock, NETLINK_ROUTE);
120 nl_cli_link_alloc_cache(rt_sock);
126 int nffd, rtfd, maxfd, retval;
139 retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
142 if (FD_ISSET(nffd, &rfds))
144 if (FD_ISSET(rtfd, &rfds))
Customized handler specified by the user.
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Stop parsing altogether and discard remaining messages.
int nl_socket_modify_cb(struct nl_sock *sk, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Modify the callback handler associated with the socket.
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
int nl_recvmsgs_default(struct nl_sock *sk)
Receive a set of message from a netlink socket using handlers in nl_sock.
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
Set socket buffer size of netlink socket.
Dump all attributes including statistics.