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