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