libnl: /data/src/libnl/src/lib/ct.c Source File

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