libnl: /data/src/libnl/src/nf-queue.c Source File

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