libnl 3.7.0
nf-exp-delete.c
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/rtnetlink.h>
13
14static int quiet = 0;
15
16static 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\n"
48 );
49 exit(0);
50}
51
52int main(int argc, char *argv[])
53{
54 struct nl_sock *sock;
55 struct nfnl_exp *exp;
56 struct nl_dump_params params = {
58 .dp_fd = stdout,
59 };
60 int err, nlflags = 0;
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_TIMEOUT,
85 ARG_HELPER_NAME,
86 ARG_FLAGS,
87 };
88 static struct option long_opts[] = {
89 { "quiet", 0, 0, 'q' },
90 { "help", 0, 0, 'h' },
91 { "version", 0, 0, 'v' },
92 { "id", 1, 0, 'i' },
93 { "expect-proto", 1, 0, ARG_EXPECT_PROTO },
94 { "expect-src", 1, 0, ARG_EXPECT_SRC },
95 { "expect-sport", 1, 0, ARG_EXPECT_SPORT },
96 { "expect-dst", 1, 0, ARG_EXPECT_DST },
97 { "expect-dport", 1, 0, ARG_EXPECT_DPORT },
98 { "master-proto", 1, 0, ARG_MASTER_PROTO },
99 { "master-src", 1, 0, ARG_MASTER_SRC },
100 { "master-sport", 1, 0, ARG_MASTER_SPORT },
101 { "master-dst", 1, 0, ARG_MASTER_DST },
102 { "master-dport", 1, 0, ARG_MASTER_DPORT },
103 { "mask-proto", 1, 0, ARG_MASK_PROTO },
104 { "mask-src", 1, 0, ARG_MASK_SRC },
105 { "mask-sport", 1, 0, ARG_MASK_SPORT },
106 { "mask-dst", 1, 0, ARG_MASK_DST },
107 { "mask-dport", 1, 0, ARG_MASK_DPORT },
108 { "family", 1, 0, 'F' },
109 { "timeout", 1, 0, ARG_TIMEOUT },
110 { "helper", 1, 0, ARG_HELPER_NAME },
111 { "flags", 1, 0, ARG_FLAGS},
112 { 0, 0, 0, 0 }
113 };
114
115 c = getopt_long(argc, argv, "46f:hvi:p:F:", long_opts, &optidx);
116 if (c == -1)
117 break;
118
119 switch (c) {
120 case '?': exit(NLE_INVAL);
121 case 'q': quiet = 1; break;
122 case '4': nfnl_exp_set_family(exp, AF_INET); break;
123 case '6': nfnl_exp_set_family(exp, AF_INET6); break;
124 case 'h': print_usage(); break;
125 case 'v': nl_cli_print_version(); break;
126 case 'i': nl_cli_exp_parse_id(exp, optarg); break;
127 case ARG_EXPECT_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
128 case ARG_EXPECT_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
129 case ARG_EXPECT_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
130 case ARG_EXPECT_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
131 case ARG_EXPECT_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
132 case ARG_MASTER_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
133 case ARG_MASTER_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
134 case ARG_MASTER_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
135 case ARG_MASTER_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
136 case ARG_MASTER_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
137 case ARG_MASK_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
138 case ARG_MASK_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
139 case ARG_MASK_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
140 case ARG_MASK_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
141 case ARG_MASK_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
142 case 'F': nl_cli_exp_parse_family(exp, optarg); break;
143 case ARG_TIMEOUT: nl_cli_exp_parse_timeout(exp, optarg); break;
144 case ARG_HELPER_NAME: nl_cli_exp_parse_helper_name(exp, optarg); break;
145 case ARG_FLAGS: nl_cli_exp_parse_flags(exp, optarg); break;
146 }
147 }
148
149 sock = nl_cli_alloc_socket();
150 nl_cli_connect(sock, NETLINK_NETFILTER);
151
152 if ((err = nfnl_exp_del(sock, exp, nlflags)) < 0)
153 nl_cli_fatal(err, "Unable to delete expectation: %s", nl_geterror(err));
154
155 if (!quiet) {
156 printf("Deleted ");
157 nl_object_dump(OBJ_CAST(exp), &params);
158 }
159
160 return 0;
161}
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:287
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32