xref: /aosp_15_r20/external/libnl/src/nf-exp-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  * Copyright (c) 2012 Rich Fought <[email protected]>
7  */
8 
9 #include "nl-default.h"
10 
11 #include <linux/netlink.h>
12 
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/exp.h>
15 
print_usage(void)16 static void print_usage(void)
17 {
18 	printf(
19 	"Usage: nf-exp-list [OPTION]... [EXPECTATION ENTRY]\n"
20 	"\n"
21 	"Options\n"
22 	" -f, --format=TYPE     Output format { brief | details }\n"
23 	" -h, --help            Show this help\n"
24 	" -v, --version         Show versioning information\n"
25 	"\n"
26 	"Expectation Selection\n"
27 	" -i, --id=NUM                Identifier\n"
28 	"     --expect-proto=PROTOCOL Expectation protocol\n"
29 	"     --expect-src=ADDR       Expectation source address\n"
30 	"     --expect-sport=PORT     Expectation source port\n"
31 	"     --expect-dst=ADDR       Expectation destination address\n"
32 	"     --expect-dport=PORT     Expectation destination port\n"
33 	"     --master-proto=PROTOCOL Master conntrack protocol\n"
34 	"     --master-src=ADDR       Master conntrack source address\n"
35 	"     --master-sport=PORT     Master conntrack source port\n"
36 	"     --master-dst=ADDR       Master conntrack destination address\n"
37 	"     --master-dport=PORT     Master conntrack destination port\n"
38 	" -F, --family=FAMILY         Address family\n"
39 	"     --timeout=NUM           Timeout value\n"
40 	"     --helper=STRING         Helper Name\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 *exp_cache;
50 	struct nfnl_exp *exp;
51 	struct nl_dump_params params = {
52 		.dp_type = NL_DUMP_LINE,
53 		.dp_fd = stdout,
54 	};
55 
56 	exp = nl_cli_exp_alloc();
57 
58 	for (;;) {
59 		int c, optidx = 0;
60 		enum {
61 			ARG_MARK = 270,
62 			ARG_TCP_STATE = 271,
63 			ARG_EXPECT_PROTO,
64 			ARG_EXPECT_SRC,
65 			ARG_EXPECT_SPORT,
66 			ARG_EXPECT_DST,
67 			ARG_EXPECT_DPORT,
68 			ARG_MASTER_PROTO,
69 			ARG_MASTER_SRC,
70 			ARG_MASTER_SPORT,
71 			ARG_MASTER_DST,
72 			ARG_MASTER_DPORT,
73 			ARG_TIMEOUT,
74 			ARG_HELPER_NAME,
75 			ARG_FLAGS,
76 		};
77 		static struct option long_opts[] = {
78 			{ "format", 1, 0, 'f' },
79 			{ "help", 0, 0, 'h' },
80 			{ "version", 0, 0, 'v' },
81 			{ "id", 1, 0, 'i' },
82 			{ "expect-proto", 1, 0, ARG_EXPECT_PROTO },
83 			{ "expect-src", 1, 0, ARG_EXPECT_SRC },
84 			{ "expect-sport", 1, 0, ARG_EXPECT_SPORT },
85 			{ "expect-dst", 1, 0, ARG_EXPECT_DST },
86 			{ "expect-dport", 1, 0, ARG_EXPECT_DPORT },
87 			{ "master-proto", 1, 0, ARG_MASTER_PROTO },
88 			{ "master-src", 1, 0, ARG_MASTER_SRC },
89 			{ "master-sport", 1, 0, ARG_MASTER_SPORT },
90 			{ "master-dst", 1, 0, ARG_MASTER_DST },
91 			{ "master-dport", 1, 0, ARG_MASTER_DPORT },
92 			{ "family", 1, 0, 'F' },
93 			{ "timeout", 1, 0, ARG_TIMEOUT },
94 			{ "helper", 1, 0, ARG_HELPER_NAME },
95 			{ "flags", 1, 0, ARG_FLAGS},
96 			{ 0, 0, 0, 0 }
97 		};
98 
99 		c = getopt_long(argc, argv, "46f:hvi:p:F:", long_opts, &optidx);
100 		if (c == -1)
101 			break;
102 
103 		switch (c) {
104 		case '?': exit(NLE_INVAL);
105 		case '4': nfnl_exp_set_family(exp, AF_INET); break;
106 		case '6': nfnl_exp_set_family(exp, AF_INET6); break;
107 		case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
108 		case 'h': print_usage(); break;
109 		case 'v': nl_cli_print_version(); break;
110 		case 'i': nl_cli_exp_parse_id(exp, optarg); break;
111 		case ARG_EXPECT_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
112 		case ARG_EXPECT_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
113 		case ARG_EXPECT_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
114 		case ARG_EXPECT_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
115 		case ARG_EXPECT_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
116 		case ARG_MASTER_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
117 		case ARG_MASTER_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
118 		case ARG_MASTER_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
119 		case ARG_MASTER_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
120 		case ARG_MASTER_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
121 		case 'F': nl_cli_exp_parse_family(exp, optarg); break;
122 		case ARG_TIMEOUT: nl_cli_exp_parse_timeout(exp, optarg); break;
123 		case ARG_HELPER_NAME: nl_cli_exp_parse_helper_name(exp, optarg); break;
124 		case ARG_FLAGS: nl_cli_exp_parse_flags(exp, optarg); break;
125 		}
126 	}
127 
128 	sock = nl_cli_alloc_socket();
129 	nl_cli_connect(sock, NETLINK_NETFILTER);
130 	exp_cache = nl_cli_exp_alloc_cache(sock);
131 
132 	nl_cache_dump_filter(exp_cache, &params, OBJ_CAST(exp));
133 
134 	return 0;
135 }
136