xref: /aosp_15_r20/external/libnl/src/nf-log.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2008 Thomas Graf <[email protected]>
4  * Copyright (c) 2007 Philip Craig <[email protected]>
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 
alloc_log(void)18 static 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 
obj_input(struct nl_object * obj,void * arg)29 static void obj_input(struct nl_object *obj, void *arg)
30 {
31 	struct nl_dump_params dp = {
32 		.dp_type = NL_DUMP_STATS,
33 		.dp_fd = stdout,
34 		.dp_dump_msgtype = 1,
35 	};
36 
37 	nl_object_dump(obj, &dp);
38 }
39 
event_input(struct nl_msg * msg,void * arg)40 static 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 
main(int argc,char * argv[])49 int 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();
60 	nl_socket_disable_seq_check(nf_sock);
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 = {
105 			.dp_type = NL_DUMP_STATS,
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 }
145