libnl: /data/src/libnl/src/nf-exp-delete.c Source File

libnl 3.12.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 "nl-default.h"
10
11#include <linux/rtnetlink.h>
12
13#include <netlink/cli/utils.h>
14#include <netlink/cli/exp.h>
15
16static int quiet = 0;
17
18static void print_usage(void)
19{
20 printf(
21 "Usage: nf-exp-list [OPTION]... [CONNTRACK ENTRY]\n"
22 "\n"
23 "Options\n"
24 " --replace Replace the address if it exists.\n"
25 " -q, --quiet Do not print informal notifications.\n"
26 " -h, --help Show this help\n"
27 " -v, --version Show versioning information\n"
28 "\n"
29 "Expectation Selection\n"
30 " -i, --id=NUM Identifier\n"
31 " --expect-proto=PROTOCOL Expectation protocol\n"
32 " --expect-src=ADDR Expectation source address\n"
33 " --expect-sport=PORT Expectation source port\n"
34 " --expect-dst=ADDR Expectation destination address\n"
35 " --expect-dport=PORT Expectation destination port\n"
36 " --master-proto=PROTOCOL Master conntrack protocol\n"
37 " --master-src=ADDR Master conntrack source address\n"
38 " --master-sport=PORT Master conntrack source port\n"
39 " --master-dst=ADDR Master conntrack destination address\n"
40 " --master-dport=PORT Master conntrack destination port\n"
41 " --mask-proto=PROTOCOL Mask protocol\n"
42 " --mask-src=ADDR Mask source address\n"
43 " --mask-sport=PORT Mask source port\n"
44 " --mask-dst=ADDR Mask destination address\n"
45 " --mask-dport=PORT Mask destination port\n"
46 " -F, --family=FAMILY Address family\n"
47 " --timeout=NUM Timeout value\n"
48 " --helper=STRING Helper Name\n"
49 " --flags Flags\n"
50 );
51 exit(0);
52}
53
54int main(int argc, char *argv[])
55{
56 struct nl_sock *sock;
57 struct nfnl_exp *exp;
58 struct nl_dump_params params = {
59 .dp_type = NL_DUMP_LINE,
60 .dp_fd = stdout,
61 };
62 int err, nlflags = 0;
63
64 exp = nl_cli_exp_alloc();
65
66 for (;;) {
67 int c, optidx = 0;
68 enum {
69 ARG_MARK = 270,
70 ARG_TCP_STATE = 271,
71 ARG_EXPECT_PROTO,
72 ARG_EXPECT_SRC,
73 ARG_EXPECT_SPORT,
74 ARG_EXPECT_DST,
75 ARG_EXPECT_DPORT,
76 ARG_MASTER_PROTO,
77 ARG_MASTER_SRC,
78 ARG_MASTER_SPORT,
79 ARG_MASTER_DST,
80 ARG_MASTER_DPORT,
81 ARG_MASK_PROTO,
82 ARG_MASK_SRC,
83 ARG_MASK_SPORT,
84 ARG_MASK_DST,
85 ARG_MASK_DPORT,
86 ARG_TIMEOUT,
87 ARG_HELPER_NAME,
88 ARG_FLAGS,
89 };
90 static struct option long_opts[] = {
91 { "quiet", 0, 0, 'q' },
92 { "help", 0, 0, 'h' },
93 { "version", 0, 0, 'v' },
94 { "id", 1, 0, 'i' },
95 { "expect-proto", 1, 0, ARG_EXPECT_PROTO },
96 { "expect-src", 1, 0, ARG_EXPECT_SRC },
97 { "expect-sport", 1, 0, ARG_EXPECT_SPORT },
98 { "expect-dst", 1, 0, ARG_EXPECT_DST },
99 { "expect-dport", 1, 0, ARG_EXPECT_DPORT },
100 { "master-proto", 1, 0, ARG_MASTER_PROTO },
101 { "master-src", 1, 0, ARG_MASTER_SRC },
102 { "master-sport", 1, 0, ARG_MASTER_SPORT },
103 { "master-dst", 1, 0, ARG_MASTER_DST },
104 { "master-dport", 1, 0, ARG_MASTER_DPORT },
105 { "mask-proto", 1, 0, ARG_MASK_PROTO },
106 { "mask-src", 1, 0, ARG_MASK_SRC },
107 { "mask-sport", 1, 0, ARG_MASK_SPORT },
108 { "mask-dst", 1, 0, ARG_MASK_DST },
109 { "mask-dport", 1, 0, ARG_MASK_DPORT },
110 { "family", 1, 0, 'F' },
111 { "timeout", 1, 0, ARG_TIMEOUT },
112 { "helper", 1, 0, ARG_HELPER_NAME },
113 { "flags", 1, 0, ARG_FLAGS},
114 { 0, 0, 0, 0 }
115 };
116
117 c = getopt_long(argc, argv, "46f:hvi:p:F:", long_opts, &optidx);
118 if (c == -1)
119 break;
120
121 switch (c) {
122 case '?': exit(NLE_INVAL);
123 case 'q': quiet = 1; break;
124 case '4': nfnl_exp_set_family(exp, AF_INET); break;
125 case '6': nfnl_exp_set_family(exp, AF_INET6); break;
126 case 'h': print_usage(); break;
127 case 'v': nl_cli_print_version(); break;
128 case 'i': nl_cli_exp_parse_id(exp, optarg); break;
129 case ARG_EXPECT_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
130 case ARG_EXPECT_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
131 case ARG_EXPECT_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
132 case ARG_EXPECT_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
133 case ARG_EXPECT_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
134 case ARG_MASTER_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
135 case ARG_MASTER_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
136 case ARG_MASTER_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
137 case ARG_MASTER_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
138 case ARG_MASTER_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
139 case ARG_MASK_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
140 case ARG_MASK_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
141 case ARG_MASK_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
142 case ARG_MASK_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
143 case ARG_MASK_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
144 case 'F': nl_cli_exp_parse_family(exp, optarg); break;
145 case ARG_TIMEOUT: nl_cli_exp_parse_timeout(exp, optarg); break;
146 case ARG_HELPER_NAME: nl_cli_exp_parse_helper_name(exp, optarg); break;
147 case ARG_FLAGS: nl_cli_exp_parse_flags(exp, optarg); break;
148 }
149 }
150
151 sock = nl_cli_alloc_socket();
152 nl_cli_connect(sock, NETLINK_NETFILTER);
153
154 if ((err = nfnl_exp_del(sock, exp, nlflags)) < 0)
155 nl_cli_fatal(err, "Unable to delete expectation: %s", nl_geterror(err));
156
157 if (!quiet) {
158 printf("Deleted ");
159 nl_object_dump(OBJ_CAST(exp), &params);
160 }
161
162 return 0;
163}
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition utils.c:71
nl_object_dump
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition object.c:294
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
nfnl_exp
Definition nl-netfilter.h:85
nl_dump_params
Dumping parameters.
Definition types.h:32