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