libnl  3.7.0
ct.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
4  */
5 
6 /**
7  * @ingroup cli
8  * @defgroup cli_ct Connection Tracking
9  *
10  * @{
11  */
12 
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/ct.h>
15 
16 struct nfnl_ct *nl_cli_ct_alloc(void)
17 {
18  struct nfnl_ct *ct;
19 
20  ct = nfnl_ct_alloc();
21  if (!ct)
22  nl_cli_fatal(ENOMEM, "Unable to allocate conntrack object");
23 
24  return ct;
25 }
26 
27 struct nl_cache *nl_cli_ct_alloc_cache(struct nl_sock *sk)
28 {
29  return nl_cli_alloc_cache(sk, "conntrack", nfnl_ct_alloc_cache);
30 }
31 
32 void nl_cli_ct_parse_family(struct nfnl_ct *ct, char *arg)
33 {
34  int family;
35 
36  if ((family = nl_str2af(arg)) == AF_UNSPEC)
37  nl_cli_fatal(EINVAL,
38  "Unable to nl_cli_ct_parse family \"%s\": %s",
39  arg, nl_geterror(NLE_INVAL));
40 
41  nfnl_ct_set_family(ct, family);
42 }
43 
44 void nl_cli_ct_parse_protocol(struct nfnl_ct *ct, char *arg)
45 {
46  int proto;
47 
48  if ((proto = nl_str2ip_proto(arg)) < 0)
49  nl_cli_fatal(proto,
50  "Unable to nl_cli_ct_parse protocol \"%s\": %s",
51  arg, nl_geterror(proto));
52 
53  nfnl_ct_set_proto(ct, proto);
54 }
55 
56 void nl_cli_ct_parse_mark(struct nfnl_ct *ct, char *arg)
57 {
58  uint32_t mark = nl_cli_parse_u32(arg);
59  nfnl_ct_set_mark(ct, mark);
60 }
61 
62 void nl_cli_ct_parse_timeout(struct nfnl_ct *ct, char *arg)
63 {
64  uint32_t timeout = nl_cli_parse_u32(arg);
65  nfnl_ct_set_timeout(ct, timeout);
66 }
67 
68 void nl_cli_ct_parse_id(struct nfnl_ct *ct, char *arg)
69 {
70  uint32_t id = nl_cli_parse_u32(arg);
71  nfnl_ct_set_id(ct, id);
72 }
73 
74 void nl_cli_ct_parse_use(struct nfnl_ct *ct, char *arg)
75 {
76  uint32_t use = nl_cli_parse_u32(arg);
77  nfnl_ct_set_use(ct, use);
78 }
79 
80 void nl_cli_ct_parse_src(struct nfnl_ct *ct, int reply, char *arg)
81 {
82  int err;
83  struct nl_addr *a = nl_cli_addr_parse(arg, nfnl_ct_get_family(ct));
84  if ((err = nfnl_ct_set_src(ct, reply, a)) < 0)
85  nl_cli_fatal(err, "Unable to set source address: %s",
86  nl_geterror(err));
87 }
88 
89 void nl_cli_ct_parse_dst(struct nfnl_ct *ct, int reply, char *arg)
90 {
91  int err;
92  struct nl_addr *a = nl_cli_addr_parse(arg, nfnl_ct_get_family(ct));
93  if ((err = nfnl_ct_set_dst(ct, reply, a)) < 0)
94  nl_cli_fatal(err, "Unable to set destination address: %s",
95  nl_geterror(err));
96 }
97 
98 void nl_cli_ct_parse_src_port(struct nfnl_ct *ct, int reply, char *arg)
99 {
100  uint32_t port = nl_cli_parse_u32(arg);
101  nfnl_ct_set_src_port(ct, reply, port);
102 }
103 
104 void nl_cli_ct_parse_dst_port(struct nfnl_ct *ct, int reply, char *arg)
105 {
106  uint32_t port = nl_cli_parse_u32(arg);
107  nfnl_ct_set_dst_port(ct, reply, port);
108 }
109 
110 void nl_cli_ct_parse_tcp_state(struct nfnl_ct *ct, char *arg)
111 {
112  int state;
113 
114  if ((state = nfnl_ct_str2tcp_state(arg)) < 0)
115  nl_cli_fatal(state,
116  "Unable to nl_cli_ct_parse tcp state \"%s\": %s",
117  arg, nl_geterror(state));
118 
119  nfnl_ct_set_tcp_state(ct, state);
120 }
121 
122 void nl_cli_ct_parse_status(struct nfnl_ct *ct, char *arg)
123 {
124  int status;
125 
126  if ((status = nfnl_ct_str2status(arg)) < 0)
127  nl_cli_fatal(status,
128  "Unable to nl_cli_ct_parse flags \"%s\": %s",
129  arg, nl_geterror(status));
130 
131  nfnl_ct_set_status(ct, status);
132 }
133 
134 void nl_cli_ct_parse_zone(struct nfnl_ct *ct, char *arg)
135 {
136  uint32_t zone = nl_cli_parse_u32(arg);
137  nfnl_ct_set_zone(ct, zone);
138 }
139 
140 #if 0
141  } else if (arg_match("origicmpid")) {
142  if (argc > ++idx)
143  nfnl_ct_set_icmp_id(ct, 0, strtoul(argv[idx++], NULL, 0));
144  } else if (arg_match("origicmptype")) {
145  if (argc > ++idx)
146  nfnl_ct_set_icmp_type(ct, 0, strtoul(argv[idx++], NULL, 0));
147  } else if (arg_match("origicmpcode")) {
148  if (argc > ++idx)
149  nfnl_ct_set_icmp_code(ct, 0, strtoul(argv[idx++], NULL, 0));
150  } else if (arg_match("replyicmpid")) {
151  if (argc > ++idx)
152  nfnl_ct_set_icmp_id(ct, 1, strtoul(argv[idx++], NULL, 0));
153  } else if (arg_match("replyicmptype")) {
154  if (argc > ++idx)
155  nfnl_ct_set_icmp_type(ct, 1, strtoul(argv[idx++], NULL, 0));
156  } else if (arg_match("replyicmpcode")) {
157  if (argc > ++idx)
158  nfnl_ct_set_icmp_code(ct, 1, strtoul(argv[idx++], NULL, 0));
159  }
160 #endif
161 
162 /** @} */
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:36
int nfnl_ct_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Build a conntrack cache holding all conntrack currently in the kernel.
Definition: ct.c:669