libnl  3.2.24-rc1
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 nl_cache *link_cache;
64  struct nfnl_queue *queue;
65  enum nfnl_queue_copy_mode copy_mode;
66  uint32_t copy_range;
67  int err = 1;
68  int family;
69 
70  nf_sock = nfnl_queue_socket_alloc();
71  if (nf_sock == NULL)
72  nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
73 
75  nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
76 
77  if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) {
78  printf("Usage: nf-queue family group [ copy_mode ] "
79  "[ copy_range ]\n");
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");
83  return 2;
84  }
85 
86  nl_cli_connect(nf_sock, NETLINK_NETFILTER);
87 
88  if ((family = nl_str2af(argv[1])) == AF_UNSPEC)
89  nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\"", argv[1]);
90 
91  nfnl_queue_pf_unbind(nf_sock, family);
92  if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0)
93  nl_cli_fatal(err, "Unable to bind logger: %s",
94  nl_geterror(err));
95 
96  queue = alloc_queue();
97  nfnl_queue_set_group(queue, atoi(argv[2]));
98 
99  copy_mode = NFNL_QUEUE_COPY_PACKET;
100  if (argc > 3) {
101  copy_mode = nfnl_queue_str2copy_mode(argv[3]);
102  if (copy_mode < 0)
103  nl_cli_fatal(copy_mode,
104  "Unable to parse copy mode \"%s\": %s",
105  argv[3], nl_geterror(copy_mode));
106  }
107  nfnl_queue_set_copy_mode(queue, copy_mode);
108 
109  copy_range = 0xFFFF;
110  if (argc > 4)
111  copy_range = atoi(argv[4]);
112  nfnl_queue_set_copy_range(queue, copy_range);
113 
114  if ((err = nfnl_queue_create(nf_sock, queue)) < 0)
115  nl_cli_fatal(err, "Unable to bind queue: %s", nl_geterror(err));
116 
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);
120 
121  nl_socket_set_buffer_size(nf_sock, 1024*127, 1024*127);
122 
123  while (1) {
124  fd_set rfds;
125  int nffd, rtfd, maxfd, retval;
126 
127  FD_ZERO(&rfds);
128 
129  maxfd = nffd = nl_socket_get_fd(nf_sock);
130  FD_SET(nffd, &rfds);
131 
132  rtfd = nl_socket_get_fd(rt_sock);
133  FD_SET(rtfd, &rfds);
134  if (maxfd < rtfd)
135  maxfd = rtfd;
136 
137  /* wait for an incoming message on the netlink socket */
138  retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
139 
140  if (retval) {
141  if (FD_ISSET(nffd, &rfds))
142  nl_recvmsgs_default(nf_sock);
143  if (FD_ISSET(rtfd, &rfds))
144  nl_recvmsgs_default(rt_sock);
145  }
146  }
147 
148  return 0;
149 }