• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nf-monitor.c     Monitor netfilter events
4  *
5  *	This library is free software; you can redistribute it and/or
6  *	modify it under the terms of the GNU Lesser General Public
7  *	License as published by the Free Software Foundation version 2.1
8  *	of the License.
9  *
10  * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
11  * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
12  * Copyright (c) 2007 Secure Computing Corporation
13  */
14 
15 #include <netlink/cli/utils.h>
16 #include <netlink/netfilter/nfnl.h>
17 
18 #include <linux/netlink.h>
19 #include <linux/netfilter/nfnetlink.h>
20 
obj_input(struct nl_object * obj,void * arg)21 static void obj_input(struct nl_object *obj, void *arg)
22 {
23 	struct nl_dump_params dp = {
24 		.dp_type = NL_DUMP_STATS,
25 		.dp_fd = stdout,
26 		.dp_dump_msgtype = 1,
27 	};
28 
29 	nl_object_dump(obj, &dp);
30 }
31 
event_input(struct nl_msg * msg,void * arg)32 static int event_input(struct nl_msg *msg, void *arg)
33 {
34 	if (nl_msg_parse(msg, &obj_input, NULL) < 0)
35 		fprintf(stderr, "<<EVENT>> Unknown message type\n");
36 
37 	/* Exit nl_recvmsgs_def() and return to the main select() */
38 	return NL_STOP;
39 }
40 
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 	struct nl_sock *sock;
44 	int err;
45 	int i, idx;
46 
47 	static const struct {
48 		enum nfnetlink_groups gr_id;
49 		const char* gr_name;
50 	} groups[] = {
51 		{ NFNLGRP_CONNTRACK_NEW, "ct-new" },
52 		{ NFNLGRP_CONNTRACK_UPDATE, "ct-update" },
53 		{ NFNLGRP_CONNTRACK_DESTROY, "ct-destroy" },
54 		{ NFNLGRP_NONE, NULL }
55 	};
56 
57 	sock = nl_cli_alloc_socket();
58 	nl_socket_disable_seq_check(sock);
59 	nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
60 
61 	if (argc > 1 && !strcasecmp(argv[1], "-h")) {
62 		printf("Usage: nf-monitor [<groups>]\n");
63 
64 		printf("Known groups:");
65 		for (i = 0; groups[i].gr_id != NFNLGRP_NONE; i++)
66 			printf(" %s", groups[i].gr_name);
67 		printf("\n");
68 		return 2;
69 	}
70 
71 	nl_cli_connect(sock, NETLINK_NETFILTER);
72 
73 	for (idx = 1; argc > idx; idx++) {
74 		for (i = 0; groups[i].gr_id != NFNLGRP_NONE; i++) {
75 			if (strcmp(argv[idx], groups[i].gr_name))
76 				continue;
77 
78 			err = nl_socket_add_membership(sock, groups[i].gr_id);
79 			if (err < 0)
80 				nl_cli_fatal(err,
81 					     "Unable to add membership: %s",
82 					     nl_geterror(err));
83 			break;
84 		}
85 
86 		if (groups[i].gr_id == NFNLGRP_NONE)
87 			nl_cli_fatal(NLE_OBJ_NOTFOUND, "Unknown group: \"%s\"",
88 				     argv[idx]);
89 	}
90 
91 	while (1) {
92 		fd_set rfds;
93 		int fd, retval;
94 
95 		fd = nl_socket_get_fd(sock);
96 
97 		FD_ZERO(&rfds);
98 		FD_SET(fd, &rfds);
99 		/* wait for an incoming message on the netlink socket */
100 		retval = select(fd+1, &rfds, NULL, NULL, NULL);
101 
102 		if (retval) {
103 			/* FD_ISSET(fd, &rfds) will be true */
104 			nl_recvmsgs_default(sock);
105 		}
106 	}
107 
108 	return 0;
109 }
110