libnl  3.3.0
nf-exp-add.c
1 /*
2  * src/nf-exp-add.c Create 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 
16 #include <netlink/cli/utils.h>
17 #include <netlink/cli/exp.h>
18 
19 #include <linux/netlink.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 (Kernel 2.6.37)\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 = NLM_F_CREATE;
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_NAT_PROTO,
92  ARG_NAT_SRC,
93  ARG_NAT_SPORT,
94  ARG_NAT_DST,
95  ARG_NAT_DPORT,
96  ARG_NAT_DIR,
97  ARG_TIMEOUT,
98  ARG_HELPER_NAME,
99  ARG_REPLACE,
100  ARG_FLAGS,
101  };
102  static struct option long_opts[] = {
103  { "replace", 1, 0, ARG_REPLACE },
104  { "quiet", 0, 0, 'q' },
105  { "help", 0, 0, 'h' },
106  { "version", 0, 0, 'v' },
107  { "id", 1, 0, 'i' },
108  { "expect-proto", 1, 0, ARG_EXPECT_PROTO },
109  { "expect-src", 1, 0, ARG_EXPECT_SRC },
110  { "expect-sport", 1, 0, ARG_EXPECT_SPORT },
111  { "expect-dst", 1, 0, ARG_EXPECT_DST },
112  { "expect-dport", 1, 0, ARG_EXPECT_DPORT },
113  { "master-proto", 1, 0, ARG_MASTER_PROTO },
114  { "master-src", 1, 0, ARG_MASTER_SRC },
115  { "master-sport", 1, 0, ARG_MASTER_SPORT },
116  { "master-dst", 1, 0, ARG_MASTER_DST },
117  { "master-dport", 1, 0, ARG_MASTER_DPORT },
118  { "mask-proto", 1, 0, ARG_MASK_PROTO },
119  { "mask-src", 1, 0, ARG_MASK_SRC },
120  { "mask-sport", 1, 0, ARG_MASK_SPORT },
121  { "mask-dst", 1, 0, ARG_MASK_DST },
122  { "mask-dport", 1, 0, ARG_MASK_DPORT },
123  { "nat-proto", 1, 0, ARG_NAT_PROTO },
124  { "nat-src", 1, 0, ARG_NAT_SRC },
125  { "nat-sport", 1, 0, ARG_NAT_SPORT },
126  { "nat-dst", 1, 0, ARG_NAT_DST },
127  { "nat-dport", 1, 0, ARG_NAT_DPORT },
128  { "nat-dir", 1, 0, ARG_NAT_DIR },
129  { "family", 1, 0, 'F' },
130  { "timeout", 1, 0, ARG_TIMEOUT },
131  { "helper", 1, 0, ARG_HELPER_NAME },
132  { "flags", 1, 0, ARG_FLAGS},
133  { 0, 0, 0, 0 }
134  };
135 
136  c = getopt_long(argc, argv, "46f:hvi:p:F:", long_opts, &optidx);
137  if (c == -1)
138  break;
139 
140  switch (c) {
141  case '?': exit(NLE_INVAL);
142  case ARG_REPLACE: nlflags |= NLM_F_REPLACE; break;
143  case 'q': quiet = 1; break;
144  case '4': nfnl_exp_set_family(exp, AF_INET); break;
145  case '6': nfnl_exp_set_family(exp, AF_INET6); break;
146  case 'h': print_usage(); break;
147  case 'v': nl_cli_print_version(); break;
148  case 'i': nl_cli_exp_parse_id(exp, optarg); break;
149  case ARG_EXPECT_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
150  case ARG_EXPECT_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
151  case ARG_EXPECT_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
152  case ARG_EXPECT_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
153  case ARG_EXPECT_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
154  case ARG_MASTER_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
155  case ARG_MASTER_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
156  case ARG_MASTER_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
157  case ARG_MASTER_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
158  case ARG_MASTER_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
159  case ARG_MASK_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
160  case ARG_MASK_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
161  case ARG_MASK_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
162  case ARG_MASK_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
163  case ARG_MASK_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
164  case ARG_NAT_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
165  case ARG_NAT_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
166  case ARG_NAT_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
167  case ARG_NAT_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
168  case ARG_NAT_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_NAT, optarg); break;
169  case ARG_NAT_DIR: nl_cli_exp_parse_nat_dir(exp, optarg); break;
170  case 'F': nl_cli_exp_parse_family(exp, optarg); break;
171  case ARG_TIMEOUT: nl_cli_exp_parse_timeout(exp, optarg); break;
172  case ARG_HELPER_NAME: nl_cli_exp_parse_helper_name(exp, optarg); break;
173  case ARG_FLAGS: nl_cli_exp_parse_flags(exp, optarg); break;
174  }
175  }
176 
177  sock = nl_cli_alloc_socket();
178  nl_cli_connect(sock, NETLINK_NETFILTER);
179 
180  if ((err = nfnl_exp_add(sock, exp, nlflags)) < 0)
181  nl_cli_fatal(err, "Unable to add expectation: %s", nl_geterror(err));
182 
183  if (!quiet) {
184  printf("Added ");
185  nl_object_dump(OBJ_CAST(exp), &params);
186  }
187 
188  return 0;
189 }
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