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