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