libnl  3.2.21
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 static int quiet = 0;
20 
21 static void print_usage(void)
22 {
23  printf(
24  "Usage: nf-exp-list [OPTION]... [CONNTRACK ENTRY]\n"
25  "\n"
26  "Options\n"
27  " --replace Replace the address if it exists.\n"
28  " -q, --quiet Do not print informal notifications.\n"
29  " -h, --help Show this help\n"
30  " -v, --version Show versioning information\n"
31  "\n"
32  "Expectation Selection\n"
33  " -i, --id=NUM Identifier\n"
34  " --expect-proto=PROTOCOL Expectation protocol\n"
35  " --expect-src=ADDR Expectation source address\n"
36  " --expect-sport=PORT Expectation source port\n"
37  " --expect-dst=ADDR Expectation destination address\n"
38  " --expect-dport=PORT Expectation destination port\n"
39  " --master-proto=PROTOCOL Master conntrack protocol\n"
40  " --master-src=ADDR Master conntrack source address\n"
41  " --master-sport=PORT Master conntrack source port\n"
42  " --master-dst=ADDR Master conntrack destination address\n"
43  " --master-dport=PORT Master conntrack destination port\n"
44  " --mask-proto=PROTOCOL Mask protocol\n"
45  " --mask-src=ADDR Mask source address\n"
46  " --mask-sport=PORT Mask source port\n"
47  " --mask-dst=ADDR Mask destination address\n"
48  " --mask-dport=PORT Mask destination port\n"
49  " -F, --family=FAMILY Address family\n"
50  " --timeout=NUM Timeout value\n"
51  " --helper=STRING Helper Name\n"
52  " --flags Flags (Kernel 2.6.37)\n"
53  );
54  exit(0);
55 }
56 
57 int main(int argc, char *argv[])
58 {
59  struct nl_sock *sock;
60  struct nfnl_exp *exp;
61  struct nl_dump_params params = {
63  .dp_fd = stdout,
64  };
65  int err, nlflags = NLM_F_CREATE;
66 
67  exp = nl_cli_exp_alloc();
68 
69  for (;;) {
70  int c, optidx = 0;
71  enum {
72  ARG_MARK = 270,
73  ARG_TCP_STATE = 271,
74  ARG_EXPECT_PROTO,
75  ARG_EXPECT_SRC,
76  ARG_EXPECT_SPORT,
77  ARG_EXPECT_DST,
78  ARG_EXPECT_DPORT,
79  ARG_MASTER_PROTO,
80  ARG_MASTER_SRC,
81  ARG_MASTER_SPORT,
82  ARG_MASTER_DST,
83  ARG_MASTER_DPORT,
84  ARG_MASK_PROTO,
85  ARG_MASK_SRC,
86  ARG_MASK_SPORT,
87  ARG_MASK_DST,
88  ARG_MASK_DPORT,
89  ARG_TIMEOUT,
90  ARG_HELPER_NAME,
91  ARG_REPLACE,
92  ARG_FLAGS,
93  };
94  static struct option long_opts[] = {
95  { "replace", 1, 0, ARG_REPLACE },
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 ARG_REPLACE: nlflags |= NLM_F_REPLACE; break;
129  case 'q': quiet = 1; break;
130  case '4': nfnl_exp_set_family(exp, AF_INET); break;
131  case '6': nfnl_exp_set_family(exp, AF_INET6); break;
132  case 'h': print_usage(); break;
133  case 'v': nl_cli_print_version(); break;
134  case 'i': nl_cli_exp_parse_id(exp, optarg); break;
135  case ARG_EXPECT_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
136  case ARG_EXPECT_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
137  case ARG_EXPECT_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
138  case ARG_EXPECT_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
139  case ARG_EXPECT_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_EXPECT, optarg); break;
140  case ARG_MASTER_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
141  case ARG_MASTER_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
142  case ARG_MASTER_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
143  case ARG_MASTER_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
144  case ARG_MASTER_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASTER, optarg); break;
145  case ARG_MASK_PROTO: nl_cli_exp_parse_l4protonum(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
146  case ARG_MASK_SRC: nl_cli_exp_parse_src(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
147  case ARG_MASK_SPORT: nl_cli_exp_parse_src_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
148  case ARG_MASK_DST: nl_cli_exp_parse_dst(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
149  case ARG_MASK_DPORT: nl_cli_exp_parse_dst_port(exp, NFNL_EXP_TUPLE_MASK, optarg); break;
150  case 'F': nl_cli_exp_parse_family(exp, optarg); break;
151  case ARG_TIMEOUT: nl_cli_exp_parse_timeout(exp, optarg); break;
152  case ARG_HELPER_NAME: nl_cli_exp_parse_helper_name(exp, optarg); break;
153  case ARG_FLAGS: nl_cli_exp_parse_flags(exp, optarg); break;
154  }
155  }
156 
157  sock = nl_cli_alloc_socket();
158  nl_cli_connect(sock, NETLINK_NETFILTER);
159 
160  if ((err = nfnl_exp_add(sock, exp, nlflags)) < 0)
161  nl_cli_fatal(err, "Unable to add expectation: %s", nl_geterror(err));
162 
163  if (!quiet) {
164  printf("Added ");
165  nl_object_dump(OBJ_CAST(exp), &params);
166  }
167 
168  return 0;
169 }