• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nf-ct-list.c     List Conntrack Entries
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-2009 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/cli/ct.h>
17 
18 #include <linux/netlink.h>
19 
print_usage(void)20 static void print_usage(void)
21 {
22 	printf(
23 	"Usage: nf-ct-list [OPTION]... [CONNTRACK ENTRY]\n"
24 	"\n"
25 	"Options\n"
26 	" -f, --format=TYPE     Output format { brief | details | stats }\n"
27 	" -h, --help            Show this help\n"
28 	" -v, --version         Show versioning information\n"
29 	"\n"
30 	"Conntrack Selection\n"
31 	" -i, --id=NUM            Identifier\n"
32 	" -p, --proto=PROTOCOL    Protocol\n"
33 	"     --tcp-state=STATE   TCP connection state\n"
34 	"     --orig-src=ADDR     Original source address\n"
35 	"     --orig-sport=PORT   Original source port\n"
36 	"     --orig-dst=ADDR     Original destination address\n"
37 	"     --orig-dport=PORT   Original destination port\n"
38 	"     --reply-src=ADDR    Reply source address\n"
39 	"     --reply-sport=PORT  Reply source port\n"
40 	"     --reply-dst=ADDR    Reply destination address\n"
41 	"     --reply-dport=PORT  Reply destination port\n"
42 	" -F, --family=FAMILY     Address family\n"
43 	"     --mark=NUM          Mark value\n"
44 	"     --timeout=NUM       Timeout value\n"
45 	"     --refcnt=NUM        Use counter value\n"
46 	"     --flags             Flags\n"
47 	);
48 	exit(0);
49 }
50 
main(int argc,char * argv[])51 int main(int argc, char *argv[])
52 {
53 	struct nl_sock *sock;
54 	struct nl_cache *ct_cache;
55 	struct nfnl_ct *ct;
56 	struct nl_dump_params params = {
57 		.dp_type = NL_DUMP_LINE,
58 		.dp_fd = stdout,
59 	};
60 
61 	ct = nl_cli_ct_alloc();
62 
63 	for (;;) {
64 		int c, optidx = 0;
65 		enum {
66 			ARG_MARK = 257,
67 			ARG_TCP_STATE = 258,
68 			ARG_ORIG_SRC,
69 			ARG_ORIG_SPORT,
70 			ARG_ORIG_DST,
71 			ARG_ORIG_DPORT,
72 			ARG_REPLY_SRC,
73 			ARG_REPLY_SPORT,
74 			ARG_REPLY_DST,
75 			ARG_REPLY_DPORT,
76 			ARG_TIMEOUT,
77 			ARG_REFCNT,
78 			ARG_FLAGS,
79 		};
80 		static struct option long_opts[] = {
81 			{ "format", 1, 0, 'f' },
82 			{ "help", 0, 0, 'h' },
83 			{ "version", 0, 0, 'v' },
84 			{ "id", 1, 0, 'i' },
85 			{ "proto", 1, 0, 'p' },
86 			{ "tcp-state", 1, 0, ARG_TCP_STATE },
87 			{ "orig-src", 1, 0, ARG_ORIG_SRC },
88 			{ "orig-sport", 1, 0, ARG_ORIG_SPORT },
89 			{ "orig-dst", 1, 0, ARG_ORIG_DST },
90 			{ "orig-dport", 1, 0, ARG_ORIG_DPORT },
91 			{ "reply-src", 1, 0, ARG_REPLY_SRC },
92 			{ "reply-sport", 1, 0, ARG_REPLY_SPORT },
93 			{ "reply-dst", 1, 0, ARG_REPLY_DST },
94 			{ "reply-dport", 1, 0, ARG_REPLY_DPORT },
95 			{ "family", 1, 0, 'F' },
96 			{ "mark", 1, 0, ARG_MARK },
97 			{ "timeout", 1, 0, ARG_TIMEOUT },
98 			{ "refcnt", 1, 0, ARG_REFCNT },
99 			{ 0, 0, 0, 0 }
100 		};
101 
102 		c = getopt_long(argc, argv, "46f:hvi:p:F:", long_opts, &optidx);
103 		if (c == -1)
104 			break;
105 
106 		switch (c) {
107 		case '?': exit(NLE_INVAL);
108 		case '4': nfnl_ct_set_family(ct, AF_INET); break;
109 		case '6': nfnl_ct_set_family(ct, AF_INET6); break;
110 		case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
111 		case 'h': print_usage(); break;
112 		case 'v': nl_cli_print_version(); break;
113 		case 'i': nl_cli_ct_parse_id(ct, optarg); break;
114 		case 'p': nl_cli_ct_parse_protocol(ct, optarg); break;
115 		case ARG_TCP_STATE: nl_cli_ct_parse_tcp_state(ct, optarg); break;
116 		case ARG_ORIG_SRC: nl_cli_ct_parse_src(ct, 0, optarg); break;
117 		case ARG_ORIG_SPORT: nl_cli_ct_parse_src_port(ct, 0, optarg); break;
118 		case ARG_ORIG_DST: nl_cli_ct_parse_dst(ct, 0, optarg); break;
119 		case ARG_ORIG_DPORT: nl_cli_ct_parse_dst_port(ct, 0, optarg); break;
120 		case ARG_REPLY_SRC: nl_cli_ct_parse_src(ct, 1, optarg); break;
121 		case ARG_REPLY_SPORT: nl_cli_ct_parse_src_port(ct, 1, optarg); break;
122 		case ARG_REPLY_DST: nl_cli_ct_parse_dst(ct, 1, optarg); break;
123 		case ARG_REPLY_DPORT: nl_cli_ct_parse_dst_port(ct, 1, optarg); break;
124 		case 'F': nl_cli_ct_parse_family(ct, optarg); break;
125 		case ARG_MARK: nl_cli_ct_parse_mark(ct, optarg); break;
126 		case ARG_TIMEOUT: nl_cli_ct_parse_timeout(ct, optarg); break;
127 		case ARG_REFCNT: nl_cli_ct_parse_use(ct, optarg); break;
128 		case ARG_FLAGS: nl_cli_ct_parse_status(ct, optarg); break;
129 		}
130 	}
131 
132 	sock = nl_cli_alloc_socket();
133 	nl_cli_connect(sock, NETLINK_NETFILTER);
134 	ct_cache = nl_cli_ct_alloc_cache(sock);
135 
136 	nl_cache_dump_filter(ct_cache, &params, OBJ_CAST(ct));
137 
138 	return 0;
139 }
140