libnl  3.4.0
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 #include <linux/netlink.h>
24 
25 static struct nl_sock *nf_sock;
26 
27 static struct nfnl_queue *alloc_queue(void)
28 {
29  struct nfnl_queue *queue;
30 
31  queue = nfnl_queue_alloc();
32  if (!queue)
33  nl_cli_fatal(ENOMEM, "Unable to allocate queue object");
34 
35  return queue;
36 }
37 
38 
39 static void obj_input(struct nl_object *obj, void *arg)
40 {
41  struct nfnl_queue_msg *msg = (struct nfnl_queue_msg *) obj;
42  struct nl_dump_params dp = {
44  .dp_fd = stdout,
45  .dp_dump_msgtype = 1,
46  };
47 
48  nfnl_queue_msg_set_verdict(msg, NF_ACCEPT);
49  nl_object_dump(obj, &dp);
50  nfnl_queue_msg_send_verdict(nf_sock, msg);
51 }
52 
53 static int event_input(struct nl_msg *msg, void *arg)
54 {
55  if (nl_msg_parse(msg, &obj_input, NULL) < 0)
56  fprintf(stderr, "<<EVENT>> Unknown message type\n");
57 
58  /* Exit nl_recvmsgs_def() and return to the main select() */
59  return NL_STOP;
60 }
61 
62 int main(int argc, char *argv[])
63 {
64  struct nl_sock *rt_sock;
65  struct nfnl_queue *queue;
66  int copy_mode;
67  uint32_t copy_range;
68  int err = 1;
69  int family;
70 
71  nf_sock = nfnl_queue_socket_alloc();
72  if (nf_sock == NULL)
73  nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
74 
76  nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
77 
78  if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) {
79  printf("Usage: nf-queue family group [ copy_mode ] "
80  "[ copy_range ]\n");
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");
84  return 2;
85  }
86 
87  nl_cli_connect(nf_sock, NETLINK_NETFILTER);
88 
89  if ((family = nl_str2af(argv[1])) == AF_UNSPEC)
90  nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\"", argv[1]);
91 
92  nfnl_queue_pf_unbind(nf_sock, family);
93  if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0)
94  nl_cli_fatal(err, "Unable to bind logger: %s",
95  nl_geterror(err));
96 
97  queue = alloc_queue();
98  nfnl_queue_set_group(queue, atoi(argv[2]));
99 
100  copy_mode = NFNL_QUEUE_COPY_PACKET;
101  if (argc > 3) {
102  copy_mode = nfnl_queue_str2copy_mode(argv[3]);
103  if (copy_mode < 0)
104  nl_cli_fatal(copy_mode,
105  "Unable to parse copy mode \"%s\": %s",
106  argv[3], nl_geterror(copy_mode));
107  }
108  nfnl_queue_set_copy_mode(queue, copy_mode);
109 
110  copy_range = 0xFFFF;
111  if (argc > 4)
112  copy_range = atoi(argv[4]);
113  nfnl_queue_set_copy_range(queue, copy_range);
114 
115  if ((err = nfnl_queue_create(nf_sock, queue)) < 0)
116  nl_cli_fatal(err, "Unable to bind queue: %s", nl_geterror(err));
117 
118  rt_sock = nl_cli_alloc_socket();
119  nl_cli_connect(rt_sock, NETLINK_ROUTE);
120  nl_cli_link_alloc_cache(rt_sock);
121 
122  nl_socket_set_buffer_size(nf_sock, 1024*127, 1024*127);
123 
124  while (1) {
125  fd_set rfds;
126  int nffd, rtfd, maxfd, retval;
127 
128  FD_ZERO(&rfds);
129 
130  maxfd = nffd = nl_socket_get_fd(nf_sock);
131  FD_SET(nffd, &rfds);
132 
133  rtfd = nl_socket_get_fd(rt_sock);
134  FD_SET(rtfd, &rfds);
135  if (maxfd < rtfd)
136  maxfd = rtfd;
137 
138  /* wait for an incoming message on the netlink socket */
139  retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
140 
141  if (retval) {
142  if (FD_ISSET(nffd, &rfds))
143  nl_recvmsgs_default(nf_sock);
144  if (FD_ISSET(rtfd, &rfds))
145  nl_recvmsgs_default(rt_sock);
146  }
147  }
148 
149  return 0;
150 }
Customized handler specified by the user.
Definition: handlers.h:83
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
Definition: socket.c:583
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:68
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:770
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:282
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:1093
Message is valid.
Definition: handlers.h:95
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:77
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:812
Dump all attributes including statistics.
Definition: types.h:24