libnl 3.9.0
nf-log.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
4 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
5 * Copyright (c) 2007 Secure Computing Corporation
6 */
7
8#include "nl-default.h"
9
10#include <linux/netfilter/nfnetlink_log.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/log.h>
17
18static struct nfnl_log *alloc_log(void)
19{
20 struct nfnl_log *log;
21
22 log = nfnl_log_alloc();
23 if (!log)
24 nl_cli_fatal(ENOMEM, "Unable to allocate log object");
25
26 return log;
27}
28
29static void obj_input(struct nl_object *obj, void *arg)
30{
31 struct nl_dump_params dp = {
33 .dp_fd = stdout,
34 .dp_dump_msgtype = 1,
35 };
36
37 nl_object_dump(obj, &dp);
38}
39
40static int event_input(struct nl_msg *msg, void *arg)
41{
42 if (nl_msg_parse(msg, &obj_input, NULL) < 0)
43 fprintf(stderr, "<<EVENT>> Unknown message type\n");
44
45 /* Exit nl_recvmsgs_def() and return to the main select() */
46 return NL_STOP;
47}
48
49int main(int argc, char *argv[])
50{
51 struct nl_sock *nf_sock;
52 struct nl_sock *rt_sock;
53 struct nfnl_log *log;
54 int copy_mode;
55 uint32_t copy_range;
56 int err;
57 int family;
58
59 nf_sock = nl_cli_alloc_socket();
61 nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
62
63 if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) {
64 printf("Usage: nf-log family group [ copy_mode ] "
65 "[copy_range] \n");
66 return 2;
67 }
68
69 nl_cli_connect(nf_sock, NETLINK_NETFILTER);
70
71 family = nl_str2af(argv[1]);
72 if (family == AF_UNSPEC)
73 nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\": %s",
74 argv[1], nl_geterror(family));
75
76 nfnl_log_pf_unbind(nf_sock, family);
77 if ((err = nfnl_log_pf_bind(nf_sock, family)) < 0)
78 nl_cli_fatal(err, "Unable to bind logger: %s",
79 nl_geterror(err));
80
81 log = alloc_log();
82 nfnl_log_set_group(log, atoi(argv[2]));
83
84 copy_mode = NFNL_LOG_COPY_META;
85 if (argc > 3) {
86 copy_mode = nfnl_log_str2copy_mode(argv[3]);
87 if (copy_mode < 0)
88 nl_cli_fatal(copy_mode,
89 "Unable to parse copy mode \"%s\": %s",
90 argv[3], nl_geterror(copy_mode));
91 }
92 nfnl_log_set_copy_mode(log, copy_mode);
93
94 copy_range = 0xFFFF;
95 if (argc > 4)
96 copy_range = atoi(argv[4]);
97 nfnl_log_set_copy_range(log, copy_range);
98
99 if ((err = nfnl_log_create(nf_sock, log)) < 0)
100 nl_cli_fatal(err, "Unable to bind instance: %s",
101 nl_geterror(err));
102
103 {
104 struct nl_dump_params dp = {
106 .dp_fd = stdout,
107 .dp_dump_msgtype = 1,
108 };
109
110 printf("log params: ");
111 nl_object_dump((struct nl_object *) log, &dp);
112 }
113
114 rt_sock = nl_cli_alloc_socket();
115 nl_cli_connect(rt_sock, NETLINK_ROUTE);
116 nl_cli_link_alloc_cache(rt_sock);
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 nf_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
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:294
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:1092
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
Definition socket.c:603
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
Definition socket.c:302
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:790
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition types.h:22
Dumping parameters.
Definition types.h:32
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition types.h:36