libnl  3.2.27
nf-queue.c
1 /*
2  * src/nf-queue.c Monitor netfilter queue events
3  *
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
7  * of the License.
8  *
9  * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net>
10  * Copyright (c) 2010 Karl Hiramoto <karl@hiramoto.org>
11  */
12 
13 
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>
22 
23 static struct nl_sock *nf_sock;
24 
25 static struct nfnl_queue *alloc_queue(void)
26 {
27  struct nfnl_queue *queue;
28 
29  queue = nfnl_queue_alloc();
30  if (!queue)
31  nl_cli_fatal(ENOMEM, "Unable to allocate queue object");
32 
33  return queue;
34 }
35 
36 
37 static void obj_input(struct nl_object *obj, void *arg)
38 {
39  struct nfnl_queue_msg *msg = (struct nfnl_queue_msg *) obj;
40  struct nl_dump_params dp = {
42  .dp_fd = stdout,
43  .dp_dump_msgtype = 1,
44  };
45 
46  nfnl_queue_msg_set_verdict(msg, NF_ACCEPT);
47  nl_object_dump(obj, &dp);
48  nfnl_queue_msg_send_verdict(nf_sock, msg);
49 }
50 
51 static int event_input(struct nl_msg *msg, void *arg)
52 {
53  if (nl_msg_parse(msg, &obj_input, NULL) < 0)
54  fprintf(stderr, "<<EVENT>> Unknown message type\n");
55 
56  /* Exit nl_recvmsgs_def() and return to the main select() */
57  return NL_STOP;
58 }
59 
60 int main(int argc, char *argv[])
61 {
62  struct nl_sock *rt_sock;
63  struct nfnl_queue *queue;
64  int copy_mode;
65  uint32_t copy_range;
66  int err = 1;
67  int family;
68 
69  nf_sock = nfnl_queue_socket_alloc();
70  if (nf_sock == NULL)
71  nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
72 
74  nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
75 
76  if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) {
77  printf("Usage: nf-queue family group [ copy_mode ] "
78  "[ copy_range ]\n");
79  printf("family: [ inet | inet6 | ... ] \n");
80  printf("group: the --queue-num arg that you gave to iptables\n");
81  printf("copy_mode: [ none | meta | packet ] \n");
82  return 2;
83  }
84 
85  nl_cli_connect(nf_sock, NETLINK_NETFILTER);
86 
87  if ((family = nl_str2af(argv[1])) == AF_UNSPEC)
88  nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\"", argv[1]);
89 
90  nfnl_queue_pf_unbind(nf_sock, family);
91  if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0)
92  nl_cli_fatal(err, "Unable to bind logger: %s",
93  nl_geterror(err));
94 
95  queue = alloc_queue();
96  nfnl_queue_set_group(queue, atoi(argv[2]));
97 
98  copy_mode = NFNL_QUEUE_COPY_PACKET;
99  if (argc > 3) {
100  copy_mode = nfnl_queue_str2copy_mode(argv[3]);
101  if (copy_mode < 0)
102  nl_cli_fatal(copy_mode,
103  "Unable to parse copy mode \"%s\": %s",
104  argv[3], nl_geterror(copy_mode));
105  }
106  nfnl_queue_set_copy_mode(queue, copy_mode);
107 
108  copy_range = 0xFFFF;
109  if (argc > 4)
110  copy_range = atoi(argv[4]);
111  nfnl_queue_set_copy_range(queue, copy_range);
112 
113  if ((err = nfnl_queue_create(nf_sock, queue)) < 0)
114  nl_cli_fatal(err, "Unable to bind queue: %s", nl_geterror(err));
115 
116  rt_sock = nl_cli_alloc_socket();
117  nl_cli_connect(rt_sock, NETLINK_ROUTE);
118  nl_cli_link_alloc_cache(rt_sock);
119 
120  nl_socket_set_buffer_size(nf_sock, 1024*127, 1024*127);
121 
122  while (1) {
123  fd_set rfds;
124  int nffd, rtfd, maxfd, retval;
125 
126  FD_ZERO(&rfds);
127 
128  maxfd = nffd = nl_socket_get_fd(nf_sock);
129  FD_SET(nffd, &rfds);
130 
131  rtfd = nl_socket_get_fd(rt_sock);
132  FD_SET(rtfd, &rfds);
133  if (maxfd < rtfd)
134  maxfd = rtfd;
135 
136  /* wait for an incoming message on the netlink socket */
137  retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
138 
139  if (retval) {
140  if (FD_ISSET(nffd, &rfds))
141  nl_recvmsgs_default(nf_sock);
142  if (FD_ISSET(rtfd, &rfds))
143  nl_recvmsgs_default(rt_sock);
144  }
145  }
146 
147  return 0;
148 }
Customized handler specified by the user.
Definition: handlers.h:80
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
Definition: socket.c:586
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
Stop parsing altogether and discard remaining messages.
Definition: handlers.h:65
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.
Definition: socket.c:771
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:288
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
Definition: socket.c:281
int nl_recvmsgs_default(struct nl_sock *sk)
Receive a set of message from a netlink socket using handlers in nl_sock.
Definition: nl.c:1098
Message is valid.
Definition: handlers.h:92
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:70
Dumping parameters.
Definition: types.h:33
int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
Set socket buffer size of netlink socket.
Definition: socket.c:813
Dump all attributes including statistics.
Definition: types.h:24