• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 static int quiet = 0;
15 
print_usage(void)16 static void print_usage(void)
17 {
18 	printf(
19 	"Usage: nf-exp-list [OPTION]... [CONNTRACK ENTRY]\n"
20 	"\n"
21 	"Options\n"
22 	"     --replace             Replace the address if it exists.\n"
23 	" -q, --quiet               Do not print informal notifications.\n"
24 	" -h, --help            Show this help\n"
25 	" -v, --version         Show versioning information\n"
26 	"\n"
27 	"Expectation Selection\n"
28 	" -i, --id=NUM                Identifier\n"
29 	"     --expect-proto=PROTOCOL Expectation protocol\n"
30 	"     --expect-src=ADDR       Expectation source address\n"
31 	"     --expect-sport=PORT     Expectation source port\n"
32 	"     --expect-dst=ADDR       Expectation destination address\n"
33 	"     --expect-dport=PORT     Expectation destination port\n"
34 	"     --master-proto=PROTOCOL Master conntrack protocol\n"
35 	"     --master-src=ADDR       Master conntrack source address\n"
36 	"     --master-sport=PORT     Master conntrack source port\n"
37 	"     --master-dst=ADDR       Master conntrack destination address\n"
38 	"     --master-dport=PORT     Master conntrack destination port\n"
39 	"     --mask-proto=PROTOCOL   Mask protocol\n"
40 	"     --mask-src=ADDR         Mask source address\n"
41 	"     --mask-sport=PORT       Mask source port\n"
42 	"     --mask-dst=ADDR         Mask destination address\n"
43 	"     --mask-dport=PORT       Mask destination port\n"
44 	" -F, --family=FAMILY         Address family\n"
45 	"     --timeout=NUM           Timeout value\n"
46 	"     --helper=STRING         Helper Name\n"
47 	"     --flags                 Flags (Kernel 2.6.37)\n"
48 	);
49 	exit(0);
50 }
51 
main(int argc,char * argv[])52 int main(int argc, char *argv[])
53 {
54 	struct nl_sock *sock;
55 	struct nfnl_exp *exp;
56 	struct nl_dump_params params = {
57 		.dp_type = NL_DUMP_LINE,
58 		.dp_fd = stdout,
59 	};
60 	int err, nlflags = NLM_F_CREATE;
61 
62 	exp = nl_cli_exp_alloc();
63 
64 	for (;;) {
65 		int c, optidx = 0;
66 		enum {
67 			ARG_MARK = 270,
68 			ARG_TCP_STATE = 271,
69 			ARG_EXPECT_PROTO,
70 			ARG_EXPECT_SRC,
71 			ARG_EXPECT_SPORT,
72 			ARG_EXPECT_DST,
73 			ARG_EXPECT_DPORT,
74 			ARG_MASTER_PROTO,
75 			ARG_MASTER_SRC,
76 			ARG_MASTER_SPORT,
77 			ARG_MASTER_DST,
78 			ARG_MASTER_DPORT,
79 			ARG_MASK_PROTO,
80 			ARG_MASK_SRC,
81 			ARG_MASK_SPORT,
82 			ARG_MASK_DST,
83 			ARG_MASK_DPORT,
84 			ARG_NAT_PROTO,
85 			ARG_NAT_SRC,
86 			ARG_NAT_SPORT,
87 			ARG_NAT_DST,
88 			ARG_NAT_DPORT,
89 			ARG_NAT_DIR,
90 			ARG_TIMEOUT,
91 			ARG_HELPER_NAME,
92 			ARG_REPLACE,
93 			ARG_FLAGS,
94 		};
95 		static struct option long_opts[] = {
96 			{ "replace", 1, 0, ARG_REPLACE },
97 			{ "quiet", 0, 0, 'q' },
98 			{ "help", 0, 0, 'h' },
99 			{ "version", 0, 0, 'v' },
100 			{ "id", 1, 0, 'i' },
101 			{ "expect-proto", 1, 0, ARG_EXPECT_PROTO },
102 			{ "expect-src", 1, 0, ARG_EXPECT_SRC },
103 			{ "expect-sport", 1, 0, ARG_EXPECT_SPORT },
104 			{ "expect-dst", 1, 0, ARG_EXPECT_DST },
105 			{ "expect-dport", 1, 0, ARG_EXPECT_DPORT },
106 			{ "master-proto", 1, 0, ARG_MASTER_PROTO },
107 			{ "master-src", 1, 0, ARG_MASTER_SRC },
108 			{ "master-sport", 1, 0, ARG_MASTER_SPORT },
109 			{ "master-dst", 1, 0, ARG_MASTER_DST },
110 			{ "master-dport", 1, 0, ARG_MASTER_DPORT },
111 			{ "mask-proto", 1, 0, ARG_MASK_PROTO },
112 			{ "mask-src", 1, 0, ARG_MASK_SRC },
113 			{ "mask-sport", 1, 0, ARG_MASK_SPORT },
114 			{ "mask-dst", 1, 0, ARG_MASK_DST },
115 			{ "mask-dport", 1, 0, ARG_MASK_DPORT },
116 			{ "nat-proto", 1, 0, ARG_NAT_PROTO },
117 			{ "nat-src", 1, 0, ARG_NAT_SRC },
118 			{ "nat-sport", 1, 0, ARG_NAT_SPORT },
119 			{ "nat-dst", 1, 0, ARG_NAT_DST },
120 			{ "nat-dport", 1, 0, ARG_NAT_DPORT },
121 			{ "nat-dir", 1, 0, ARG_NAT_DIR },
122 			{ "family", 1, 0, 'F' },
123 			{ "timeout", 1, 0, ARG_TIMEOUT },
124 			{ "helper", 1, 0, ARG_HELPER_NAME },
125 			{ "flags", 1, 0, ARG_FLAGS},
126 			{ 0, 0, 0, 0 }
127 		};
128 
129 		c = getopt_long(argc, argv, "46f:hvi:p:F:", long_opts, &optidx);
130 		if (c == -1)
131 			break;
132 
133 		switch (c) {
134 		case '?': exit(NLE_INVAL);
135 		case ARG_REPLACE: nlflags |= NLM_F_REPLACE; break;
136 		case 'q': quiet = 1; break;
137 		case '4': nfnl_exp_set_family(exp, AF_INET); break;
138 		case '6': nfnl_exp_set_family(exp, AF_INET6); break;
139 		case 'h': print_usage(); break;
140 		case 'v': nl_cli_print_version(); break;
141 		case 'i': nl_cli_exp_parse_id(exp, optarg); break;
142 		case ARG_EXPECT_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
143 		case ARG_EXPECT_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
144 		case ARG_EXPECT_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
145 		case ARG_EXPECT_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
146 		case ARG_EXPECT_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
147 		case ARG_MASTER_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
148 		case ARG_MASTER_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
149 		case ARG_MASTER_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
150 		case ARG_MASTER_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
151 		case ARG_MASTER_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
152 		case ARG_MASK_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
153 		case ARG_MASK_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
154 		case ARG_MASK_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
155 		case ARG_MASK_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
156 		case ARG_MASK_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
157 		case ARG_NAT_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
158 		case ARG_NAT_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
159 		case ARG_NAT_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
160 		case ARG_NAT_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
161 		case ARG_NAT_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
162 		case ARG_NAT_DIR: nl_cli_exp_parse_nat_dir(exp, optarg); break;
163 		case 'F': nl_cli_exp_parse_family(exp, optarg); break;
164 		case ARG_TIMEOUT: nl_cli_exp_parse_timeout(exp, optarg); break;
165 		case ARG_HELPER_NAME: nl_cli_exp_parse_helper_name(exp, optarg); break;
166 		case ARG_FLAGS: nl_cli_exp_parse_flags(exp, optarg); break;
167 		}
168 	}
169 
170 	sock = nl_cli_alloc_socket();
171 	nl_cli_connect(sock, NETLINK_NETFILTER);
172 
173 	if ((err = nfnl_exp_add(sock, exp, nlflags)) < 0)
174 		nl_cli_fatal(err, "Unable to add expectation: %s", nl_geterror(err));
175 
176 	if (!quiet) {
177 		printf("Added ");
178 		nl_object_dump(OBJ_CAST(exp), &params);
179 	}
180 
181 	return 0;
182 }
183