• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
nl_cli_ct_alloc(void)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 
nl_cli_ct_alloc_cache(struct nl_sock * sk)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 
nl_cli_ct_parse_family(struct nfnl_ct * ct,char * arg)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 
nl_cli_ct_parse_protocol(struct nfnl_ct * ct,char * arg)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 
nl_cli_ct_parse_mark(struct nfnl_ct * ct,char * arg)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 
nl_cli_ct_parse_timeout(struct nfnl_ct * ct,char * arg)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 
nl_cli_ct_parse_id(struct nfnl_ct * ct,char * arg)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 
nl_cli_ct_parse_use(struct nfnl_ct * ct,char * arg)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 
nl_cli_ct_parse_src(struct nfnl_ct * ct,int reply,char * arg)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 
nl_cli_ct_parse_dst(struct nfnl_ct * ct,int reply,char * arg)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 
nl_cli_ct_parse_src_port(struct nfnl_ct * ct,int reply,char * arg)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 
nl_cli_ct_parse_dst_port(struct nfnl_ct * ct,int reply,char * arg)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 
nl_cli_ct_parse_tcp_state(struct nfnl_ct * ct,char * arg)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 
nl_cli_ct_parse_status(struct nfnl_ct * ct,char * arg)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 
nl_cli_ct_parse_zone(struct nfnl_ct * ct,char * arg)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 /** @} */
163