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