libnl  3.6.0
nf-ct-list.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/netlink.h>
12 
13 static void print_usage(void)
14 {
15  printf(
16  "Usage: nf-ct-list [OPTION]... [CONNTRACK ENTRY]\n"
17  "\n"
18  "Options\n"
19  " -f, --format=TYPE Output format { brief | details | stats }\n"
20  " -h, --help Show this help\n"
21  " -v, --version Show versioning information\n"
22  "\n"
23  "Conntrack Selection\n"
24  " -i, --id=NUM Identifier\n"
25  " -p, --proto=PROTOCOL Protocol\n"
26  " --tcp-state=STATE TCP connection state\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  " --refcnt=NUM Use counter value\n"
39  " --flags Flags\n"
40  );
41  exit(0);
42 }
43 
44 int main(int argc, char *argv[])
45 {
46  struct nl_sock *sock;
47  struct nl_cache *ct_cache;
48  struct nfnl_ct *ct;
49  struct nl_dump_params params = {
51  .dp_fd = stdout,
52  };
53 
54  ct = nl_cli_ct_alloc();
55 
56  for (;;) {
57  int c, optidx = 0;
58  enum {
59  ARG_MARK = 257,
60  ARG_TCP_STATE = 258,
61  ARG_ORIG_SRC,
62  ARG_ORIG_SPORT,
63  ARG_ORIG_DST,
64  ARG_ORIG_DPORT,
65  ARG_REPLY_SRC,
66  ARG_REPLY_SPORT,
67  ARG_REPLY_DST,
68  ARG_REPLY_DPORT,
69  ARG_TIMEOUT,
70  ARG_REFCNT,
71  ARG_FLAGS,
72  };
73  static struct option long_opts[] = {
74  { "format", 1, 0, 'f' },
75  { "help", 0, 0, 'h' },
76  { "version", 0, 0, 'v' },
77  { "id", 1, 0, 'i' },
78  { "proto", 1, 0, 'p' },
79  { "tcp-state", 1, 0, ARG_TCP_STATE },
80  { "orig-src", 1, 0, ARG_ORIG_SRC },
81  { "orig-sport", 1, 0, ARG_ORIG_SPORT },
82  { "orig-dst", 1, 0, ARG_ORIG_DST },
83  { "orig-dport", 1, 0, ARG_ORIG_DPORT },
84  { "reply-src", 1, 0, ARG_REPLY_SRC },
85  { "reply-sport", 1, 0, ARG_REPLY_SPORT },
86  { "reply-dst", 1, 0, ARG_REPLY_DST },
87  { "reply-dport", 1, 0, ARG_REPLY_DPORT },
88  { "family", 1, 0, 'F' },
89  { "mark", 1, 0, ARG_MARK },
90  { "timeout", 1, 0, ARG_TIMEOUT },
91  { "refcnt", 1, 0, ARG_REFCNT },
92  { 0, 0, 0, 0 }
93  };
94 
95  c = getopt_long(argc, argv, "46f:hvi:p:F:", long_opts, &optidx);
96  if (c == -1)
97  break;
98 
99  switch (c) {
100  case '?': exit(NLE_INVAL);
101  case '4': nfnl_ct_set_family(ct, AF_INET); break;
102  case '6': nfnl_ct_set_family(ct, AF_INET6); break;
103  case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
104  case 'h': print_usage(); break;
105  case 'v': nl_cli_print_version(); break;
106  case 'i': nl_cli_ct_parse_id(ct, optarg); break;
107  case 'p': nl_cli_ct_parse_protocol(ct, optarg); break;
108  case ARG_TCP_STATE: nl_cli_ct_parse_tcp_state(ct, optarg); break;
109  case ARG_ORIG_SRC: nl_cli_ct_parse_src(ct, 0, optarg); break;
110  case ARG_ORIG_SPORT: nl_cli_ct_parse_src_port(ct, 0, optarg); break;
111  case ARG_ORIG_DST: nl_cli_ct_parse_dst(ct, 0, optarg); break;
112  case ARG_ORIG_DPORT: nl_cli_ct_parse_dst_port(ct, 0, optarg); break;
113  case ARG_REPLY_SRC: nl_cli_ct_parse_src(ct, 1, optarg); break;
114  case ARG_REPLY_SPORT: nl_cli_ct_parse_src_port(ct, 1, optarg); break;
115  case ARG_REPLY_DST: nl_cli_ct_parse_dst(ct, 1, optarg); break;
116  case ARG_REPLY_DPORT: nl_cli_ct_parse_dst_port(ct, 1, optarg); break;
117  case 'F': nl_cli_ct_parse_family(ct, optarg); break;
118  case ARG_MARK: nl_cli_ct_parse_mark(ct, optarg); break;
119  case ARG_TIMEOUT: nl_cli_ct_parse_timeout(ct, optarg); break;
120  case ARG_REFCNT: nl_cli_ct_parse_use(ct, optarg); break;
121  case ARG_FLAGS: nl_cli_ct_parse_status(ct, optarg); break;
122  }
123  }
124 
125  sock = nl_cli_alloc_socket();
126  nl_cli_connect(sock, NETLINK_NETFILTER);
127  ct_cache = nl_cli_ct_alloc_cache(sock);
128 
129  nl_cache_dump_filter(ct_cache, &params, OBJ_CAST(ct));
130 
131  return 0;
132 }
void nl_cache_dump_filter(struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
Dump all elements of a cache (filtered).
Definition: cache.c:1211
@ 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