1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
5 * Copyright (c) 2007 Secure Computing Corporation
6 * Copyright (c) 2012 Rich Fought <rich.fought@watchguard.com>
7 */
8
9 #include <netlink/cli/utils.h>
10 #include <netlink/cli/exp.h>
11
12 #include <linux/netlink.h>
13
print_usage(void)14 static void print_usage(void)
15 {
16 printf(
17 "Usage: nf-exp-list [OPTION]... [EXPECTATION ENTRY]\n"
18 "\n"
19 "Options\n"
20 " -f, --format=TYPE Output format { brief | details }\n"
21 " -h, --help Show this help\n"
22 " -v, --version Show versioning information\n"
23 "\n"
24 "Expectation Selection\n"
25 " -i, --id=NUM Identifier\n"
26 " --expect-proto=PROTOCOL Expectation protocol\n"
27 " --expect-src=ADDR Expectation source address\n"
28 " --expect-sport=PORT Expectation source port\n"
29 " --expect-dst=ADDR Expectation destination address\n"
30 " --expect-dport=PORT Expectation destination port\n"
31 " --master-proto=PROTOCOL Master conntrack protocol\n"
32 " --master-src=ADDR Master conntrack source address\n"
33 " --master-sport=PORT Master conntrack source port\n"
34 " --master-dst=ADDR Master conntrack destination address\n"
35 " --master-dport=PORT Master conntrack destination port\n"
36 " -F, --family=FAMILY Address family\n"
37 " --timeout=NUM Timeout value\n"
38 " --helper=STRING Helper Name\n"
39 //" --flags Flags\n"
40 );
41 exit(0);
42 }
43
main(int argc,char * argv[])44 int main(int argc, char *argv[])
45 {
46 struct nl_sock *sock;
47 struct nl_cache *exp_cache;
48 struct nfnl_exp *exp;
49 struct nl_dump_params params = {
50 .dp_type = NL_DUMP_LINE,
51 .dp_fd = stdout,
52 };
53
54 exp = nl_cli_exp_alloc();
55
56 for (;;) {
57 int c, optidx = 0;
58 enum {
59 ARG_MARK = 270,
60 ARG_TCP_STATE = 271,
61 ARG_EXPECT_PROTO,
62 ARG_EXPECT_SRC,
63 ARG_EXPECT_SPORT,
64 ARG_EXPECT_DST,
65 ARG_EXPECT_DPORT,
66 ARG_MASTER_PROTO,
67 ARG_MASTER_SRC,
68 ARG_MASTER_SPORT,
69 ARG_MASTER_DST,
70 ARG_MASTER_DPORT,
71 ARG_TIMEOUT,
72 ARG_HELPER_NAME,
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 { "expect-proto", 1, 0, ARG_EXPECT_PROTO },
81 { "expect-src", 1, 0, ARG_EXPECT_SRC },
82 { "expect-sport", 1, 0, ARG_EXPECT_SPORT },
83 { "expect-dst", 1, 0, ARG_EXPECT_DST },
84 { "expect-dport", 1, 0, ARG_EXPECT_DPORT },
85 { "master-proto", 1, 0, ARG_MASTER_PROTO },
86 { "master-src", 1, 0, ARG_MASTER_SRC },
87 { "master-sport", 1, 0, ARG_MASTER_SPORT },
88 { "master-dst", 1, 0, ARG_MASTER_DST },
89 { "master-dport", 1, 0, ARG_MASTER_DPORT },
90 { "family", 1, 0, 'F' },
91 { "timeout", 1, 0, ARG_TIMEOUT },
92 { "helper", 1, 0, ARG_HELPER_NAME },
93 { "flags", 1, 0, ARG_FLAGS},
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_exp_set_family(exp, AF_INET); break;
104 case '6': nfnl_exp_set_family(exp, 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_exp_parse_id(exp, optarg); break;
109 case ARG_EXPECT_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
110 case ARG_EXPECT_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
111 case ARG_EXPECT_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
112 case ARG_EXPECT_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
113 case ARG_EXPECT_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
114 case ARG_MASTER_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
115 case ARG_MASTER_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
116 case ARG_MASTER_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
117 case ARG_MASTER_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
118 case ARG_MASTER_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
119 case 'F': nl_cli_exp_parse_family(exp, optarg); break;
120 case ARG_TIMEOUT: nl_cli_exp_parse_timeout(exp, optarg); break;
121 case ARG_HELPER_NAME: nl_cli_exp_parse_helper_name(exp, optarg); break;
122 case ARG_FLAGS: nl_cli_exp_parse_flags(exp, optarg); break;
123 }
124 }
125
126 sock = nl_cli_alloc_socket();
127 nl_cli_connect(sock, NETLINK_NETFILTER);
128 exp_cache = nl_cli_exp_alloc_cache(sock);
129
130 nl_cache_dump_filter(exp_cache, ¶ms, OBJ_CAST(exp));
131
132 return 0;
133 }
134