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