libnl  3.4.0
nf-ct-add.c
1 /*
2  * src/nf-ct-list.c List Conntrack Entries
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  */
13 
14 #include <netlink/cli/utils.h>
15 #include <netlink/cli/ct.h>
16 
17 #include <linux/rtnetlink.h>
18 
19 static int quiet = 0;
20 
21 static void print_usage(void)
22 {
23  printf(
24  "Usage: nf-ct-add [OPTION]... [CONNTRACK ENTRY]\n"
25  "\n"
26  "Options\n"
27  " -q, --quiet Do not print informal notifications.\n"
28  " -h, --help Show this help\n"
29  " -v, --version Show versioning information\n"
30  "\n"
31  "Conntrack Selection\n"
32  " -p, --proto=PROTOCOL Protocol\n"
33  " --orig-src=ADDR Original source address\n"
34  " --orig-sport=PORT Original source port\n"
35  " --orig-dst=ADDR Original destination address\n"
36  " --orig-dport=PORT Original destination port\n"
37  " --reply-src=ADDR Reply source address\n"
38  " --reply-sport=PORT Reply source port\n"
39  " --reply-dst=ADDR Reply destination address\n"
40  " --reply-dport=PORT Reply destination port\n"
41  " -F, --family=FAMILY Address family\n"
42  " --mark=NUM Mark value\n"
43  " --timeout=NUM Timeout value\n"
44  " --status Bitset representing status of connection.\n"
45  " --zone=NUM Zone value\n"
46  );
47  exit(0);
48 }
49 
50 int main(int argc, char *argv[])
51 {
52  struct nl_sock *sock;
53  struct nfnl_ct *ct;
54  struct nl_dump_params params = {
56  .dp_fd = stdout,
57  };
58  int err, nlflags = NLM_F_CREATE;
59 
60  ct = nl_cli_ct_alloc();
61 
62  for (;;) {
63  int c, optidx = 0;
64  enum {
65  ARG_ORIG_SRC = 257,
66  ARG_ORIG_SPORT = 258,
67  ARG_ORIG_DST,
68  ARG_ORIG_DPORT,
69  ARG_REPLY_SRC,
70  ARG_REPLY_SPORT,
71  ARG_REPLY_DST,
72  ARG_REPLY_DPORT,
73  ARG_MARK,
74  ARG_TIMEOUT,
75  ARG_STATUS,
76  ARG_ZONE,
77  };
78  static struct option long_opts[] = {
79  { "quiet", 0, 0, 'q' },
80  { "help", 0, 0, 'h' },
81  { "version", 0, 0, 'v' },
82  { "proto", 1, 0, 'p' },
83  { "orig-src", 1, 0, ARG_ORIG_SRC },
84  { "orig-sport", 1, 0, ARG_ORIG_SPORT },
85  { "orig-dst", 1, 0, ARG_ORIG_DST },
86  { "orig-dport", 1, 0, ARG_ORIG_DPORT },
87  { "reply-src", 1, 0, ARG_REPLY_SRC },
88  { "reply-sport", 1, 0, ARG_REPLY_SPORT },
89  { "reply-dst", 1, 0, ARG_REPLY_DST },
90  { "reply-dport", 1, 0, ARG_REPLY_DPORT },
91  { "family", 1, 0, 'F' },
92  { "mark", 1, 0, ARG_MARK },
93  { "timeout", 1, 0, ARG_TIMEOUT },
94  { "status", 1, 0, ARG_STATUS },
95  { "zone", 1, 0, ARG_ZONE },
96  { 0, 0, 0, 0 }
97  };
98 
99  c = getopt_long(argc, argv, "46q:hv:p:F:", long_opts, &optidx);
100  if (c == -1)
101  break;
102 
103  switch (c) {
104  case '?': exit(NLE_INVAL);
105  case 'q': quiet = 1; break;
106  case '4': nfnl_ct_set_family(ct, AF_INET); break;
107  case '6': nfnl_ct_set_family(ct, AF_INET6); break;
108  case 'h': print_usage(); break;
109  case 'v': nl_cli_print_version(); break;
110  case 'p': nl_cli_ct_parse_protocol(ct, optarg); break;
111  case ARG_ORIG_SRC: nl_cli_ct_parse_src(ct, 0, optarg); break;
112  case ARG_ORIG_SPORT: nl_cli_ct_parse_src_port(ct, 0, optarg); break;
113  case ARG_ORIG_DST: nl_cli_ct_parse_dst(ct, 0, optarg); break;
114  case ARG_ORIG_DPORT: nl_cli_ct_parse_dst_port(ct, 0, optarg); break;
115  case ARG_REPLY_SRC: nl_cli_ct_parse_src(ct, 1, optarg); break;
116  case ARG_REPLY_SPORT: nl_cli_ct_parse_src_port(ct, 1, optarg); break;
117  case ARG_REPLY_DST: nl_cli_ct_parse_dst(ct, 1, optarg); break;
118  case ARG_REPLY_DPORT: nl_cli_ct_parse_dst_port(ct, 1, optarg); break;
119  case 'F': nl_cli_ct_parse_family(ct, optarg); break;
120  case ARG_MARK: nl_cli_ct_parse_mark(ct, optarg); break;
121  case ARG_TIMEOUT: nl_cli_ct_parse_timeout(ct, optarg); break;
122  case ARG_STATUS: nl_cli_ct_parse_status(ct, optarg); break;
123  case ARG_ZONE: nl_cli_ct_parse_zone(ct, optarg); break;
124  }
125  }
126 
127  if (!quiet) {
128  printf("Adding ");
129  nl_object_dump(OBJ_CAST(ct), &params);
130  }
131 
132  sock = nl_cli_alloc_socket();
133  nl_cli_connect(sock, NETLINK_NETFILTER);
134 
135  if ((err = nfnl_ct_add(sock, ct, nlflags)) < 0)
136  nl_cli_fatal(err, "Unable to add conntrack: %s", nl_geterror(err));
137 
138  if (!quiet) {
139  printf("Added ");
140  nl_object_dump(OBJ_CAST(ct), &params);
141  }
142 
143  return 0;
144 }
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:77
Dumping parameters.
Definition: types.h:33