• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <netlink/cli/utils.h>
9 #include <netlink/cli/link.h>
10 #include <netlink/netfilter/nfnl.h>
11 #include <netlink/netfilter/log.h>
12 
13 #include <linux/netfilter/nfnetlink_log.h>
14 #include <linux/netlink.h>
15 
alloc_log(void)16 static struct nfnl_log *alloc_log(void)
17 {
18 	struct nfnl_log *log;
19 
20 	log = nfnl_log_alloc();
21 	if (!log)
22 		nl_cli_fatal(ENOMEM, "Unable to allocate log object");
23 
24 	return log;
25 }
26 
obj_input(struct nl_object * obj,void * arg)27 static void obj_input(struct nl_object *obj, void *arg)
28 {
29 	struct nl_dump_params dp = {
30 		.dp_type = NL_DUMP_STATS,
31 		.dp_fd = stdout,
32 		.dp_dump_msgtype = 1,
33 	};
34 
35 	nl_object_dump(obj, &dp);
36 }
37 
event_input(struct nl_msg * msg,void * arg)38 static int event_input(struct nl_msg *msg, void *arg)
39 {
40 	if (nl_msg_parse(msg, &obj_input, NULL) < 0)
41 		fprintf(stderr, "<<EVENT>> Unknown message type\n");
42 
43 	/* Exit nl_recvmsgs_def() and return to the main select() */
44 	return NL_STOP;
45 }
46 
main(int argc,char * argv[])47 int main(int argc, char *argv[])
48 {
49 	struct nl_sock *nf_sock;
50 	struct nl_sock *rt_sock;
51 	struct nfnl_log *log;
52 	int copy_mode;
53 	uint32_t copy_range;
54 	int err;
55 	int family;
56 
57 	nf_sock = nl_cli_alloc_socket();
58 	nl_socket_disable_seq_check(nf_sock);
59 	nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
60 
61 	if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) {
62 		printf("Usage: nf-log family group [ copy_mode ] "
63 		       "[copy_range] \n");
64 		return 2;
65 	}
66 
67 	nl_cli_connect(nf_sock, NETLINK_NETFILTER);
68 
69 	family = nl_str2af(argv[1]);
70 	if (family == AF_UNSPEC)
71 		nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\": %s",
72 			     argv[1], nl_geterror(family));
73 
74 	nfnl_log_pf_unbind(nf_sock, family);
75 	if ((err = nfnl_log_pf_bind(nf_sock, family)) < 0)
76 		nl_cli_fatal(err, "Unable to bind logger: %s",
77 			     nl_geterror(err));
78 
79 	log = alloc_log();
80 	nfnl_log_set_group(log, atoi(argv[2]));
81 
82 	copy_mode = NFNL_LOG_COPY_META;
83 	if (argc > 3) {
84 		copy_mode = nfnl_log_str2copy_mode(argv[3]);
85 		if (copy_mode < 0)
86 			nl_cli_fatal(copy_mode,
87 				     "Unable to parse copy mode \"%s\": %s",
88 				     argv[3], nl_geterror(copy_mode));
89 	}
90 	nfnl_log_set_copy_mode(log, copy_mode);
91 
92 	copy_range = 0xFFFF;
93 	if (argc > 4)
94 		copy_range = atoi(argv[4]);
95 	nfnl_log_set_copy_range(log, copy_range);
96 
97 	if ((err = nfnl_log_create(nf_sock, log)) < 0)
98 		nl_cli_fatal(err, "Unable to bind instance: %s",
99 			     nl_geterror(err));
100 
101 	{
102 		struct nl_dump_params dp = {
103 			.dp_type = NL_DUMP_STATS,
104 			.dp_fd = stdout,
105 			.dp_dump_msgtype = 1,
106 		};
107 
108 		printf("log params: ");
109 		nl_object_dump((struct nl_object *) log, &dp);
110 	}
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 	while (1) {
117 		fd_set rfds;
118 		int nffd, rtfd, maxfd, retval;
119 
120 		FD_ZERO(&rfds);
121 
122 		maxfd = nffd = nl_socket_get_fd(nf_sock);
123 		FD_SET(nffd, &rfds);
124 
125 		rtfd = nl_socket_get_fd(rt_sock);
126 		FD_SET(rtfd, &rfds);
127 		if (maxfd < rtfd)
128 			maxfd = rtfd;
129 
130 		/* wait for an incoming message on the netlink nf_socket */
131 		retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
132 
133 		if (retval) {
134 			if (FD_ISSET(nffd, &rfds))
135 				nl_recvmsgs_default(nf_sock);
136 			if (FD_ISSET(rtfd, &rfds))
137 				nl_recvmsgs_default(rt_sock);
138 		}
139 	}
140 
141 	return 0;
142 }
143