libnl  3.6.0
nf-ct-add.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4  * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
5  * Copyright (c) 2007 Secure Computing Corporation
6  */
7 
8 #include <netlink/cli/utils.h>
9 #include <netlink/cli/ct.h>
10 
11 #include <linux/rtnetlink.h>
12 
13 static int quiet = 0;
14 
15 static void print_usage(void)
16 {
17  printf(
18  "Usage: nf-ct-add [OPTION]... [CONNTRACK ENTRY]\n"
19  "\n"
20  "Options\n"
21  " -q, --quiet Do not print informal notifications.\n"
22  " -h, --help Show this help\n"
23  " -v, --version Show versioning information\n"
24  "\n"
25  "Conntrack Selection\n"
26  " -p, --proto=PROTOCOL Protocol\n"
27  " --orig-src=ADDR Original source address\n"
28  " --orig-sport=PORT Original source port\n"
29  " --orig-dst=ADDR Original destination address\n"
30  " --orig-dport=PORT Original destination port\n"
31  " --reply-src=ADDR Reply source address\n"
32  " --reply-sport=PORT Reply source port\n"
33  " --reply-dst=ADDR Reply destination address\n"
34  " --reply-dport=PORT Reply destination port\n"
35  " -F, --family=FAMILY Address family\n"
36  " --mark=NUM Mark value\n"
37  " --timeout=NUM Timeout value\n"
38  " --status Bitset representing status of connection.\n"
39  " --zone=NUM Zone value\n"
40  );
41  exit(0);
42 }
43 
44 int main(int argc, char *argv[])
45 {
46  struct nl_sock *sock;
47  struct nfnl_ct *ct;
48  struct nl_dump_params params = {
50  .dp_fd = stdout,
51  };
52  int err, nlflags = NLM_F_CREATE;
53 
54  ct = nl_cli_ct_alloc();
55 
56  for (;;) {
57  int c, optidx = 0;
58  enum {
59  ARG_ORIG_SRC = 257,
60  ARG_ORIG_SPORT = 258,
61  ARG_ORIG_DST,
62  ARG_ORIG_DPORT,
63  ARG_REPLY_SRC,
64  ARG_REPLY_SPORT,
65  ARG_REPLY_DST,
66  ARG_REPLY_DPORT,
67  ARG_MARK,
68  ARG_TIMEOUT,
69  ARG_STATUS,
70  ARG_ZONE,
71  };
72  static struct option long_opts[] = {
73  { "quiet", 0, 0, 'q' },
74  { "help", 0, 0, 'h' },
75  { "version", 0, 0, 'v' },
76  { "proto", 1, 0, 'p' },
77  { "orig-src", 1, 0, ARG_ORIG_SRC },
78  { "orig-sport", 1, 0, ARG_ORIG_SPORT },
79  { "orig-dst", 1, 0, ARG_ORIG_DST },
80  { "orig-dport", 1, 0, ARG_ORIG_DPORT },
81  { "reply-src", 1, 0, ARG_REPLY_SRC },
82  { "reply-sport", 1, 0, ARG_REPLY_SPORT },
83  { "reply-dst", 1, 0, ARG_REPLY_DST },
84  { "reply-dport", 1, 0, ARG_REPLY_DPORT },
85  { "family", 1, 0, 'F' },
86  { "mark", 1, 0, ARG_MARK },
87  { "timeout", 1, 0, ARG_TIMEOUT },
88  { "status", 1, 0, ARG_STATUS },
89  { "zone", 1, 0, ARG_ZONE },
90  { 0, 0, 0, 0 }
91  };
92 
93  c = getopt_long(argc, argv, "46q:hv:p:F:", long_opts, &optidx);
94  if (c == -1)
95  break;
96 
97  switch (c) {
98  case '?': exit(NLE_INVAL);
99  case 'q': quiet = 1; break;
100  case '4': nfnl_ct_set_family(ct, AF_INET); break;
101  case '6': nfnl_ct_set_family(ct, AF_INET6); break;
102  case 'h': print_usage(); break;
103  case 'v': nl_cli_print_version(); break;
104  case 'p': nl_cli_ct_parse_protocol(ct, optarg); break;
105  case ARG_ORIG_SRC: nl_cli_ct_parse_src(ct, 0, optarg); break;
106  case ARG_ORIG_SPORT: nl_cli_ct_parse_src_port(ct, 0, optarg); break;
107  case ARG_ORIG_DST: nl_cli_ct_parse_dst(ct, 0, optarg); break;
108  case ARG_ORIG_DPORT: nl_cli_ct_parse_dst_port(ct, 0, optarg); break;
109  case ARG_REPLY_SRC: nl_cli_ct_parse_src(ct, 1, optarg); break;
110  case ARG_REPLY_SPORT: nl_cli_ct_parse_src_port(ct, 1, optarg); break;
111  case ARG_REPLY_DST: nl_cli_ct_parse_dst(ct, 1, optarg); break;
112  case ARG_REPLY_DPORT: nl_cli_ct_parse_dst_port(ct, 1, optarg); break;
113  case 'F': nl_cli_ct_parse_family(ct, optarg); break;
114  case ARG_MARK: nl_cli_ct_parse_mark(ct, optarg); break;
115  case ARG_TIMEOUT: nl_cli_ct_parse_timeout(ct, optarg); break;
116  case ARG_STATUS: nl_cli_ct_parse_status(ct, optarg); break;
117  case ARG_ZONE: nl_cli_ct_parse_zone(ct, optarg); break;
118  }
119  }
120 
121  if (!quiet) {
122  printf("Adding ");
123  nl_object_dump(OBJ_CAST(ct), &params);
124  }
125 
126  sock = nl_cli_alloc_socket();
127  nl_cli_connect(sock, NETLINK_NETFILTER);
128 
129  if ((err = nfnl_ct_add(sock, ct, nlflags)) < 0)
130  nl_cli_fatal(err, "Unable to add conntrack: %s", nl_geterror(err));
131 
132  if (!quiet) {
133  printf("Added ");
134  nl_object_dump(OBJ_CAST(ct), &params);
135  }
136 
137  return 0;
138 }
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:287
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32