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