• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/lib/exp.c		CLI Expectation 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  * Copyright (c) 2012 Rich Fought <rich.fought@watchguard.com>
12  */
13 
14 /**
15  * @ingroup cli
16  * @defgroup cli_exp Expectation Tracking
17  *
18  * @{
19  */
20 
21 #include <netlink/cli/utils.h>
22 #include <netlink/cli/exp.h>
23 
nl_cli_exp_alloc(void)24 struct nfnl_exp *nl_cli_exp_alloc(void)
25 {
26 	struct nfnl_exp *exp;
27 
28 	exp = nfnl_exp_alloc();
29 	if (!exp)
30 		nl_cli_fatal(ENOMEM, "Unable to allocate expectation object");
31 
32 	return exp;
33 }
34 
nl_cli_exp_alloc_cache(struct nl_sock * sk)35 struct nl_cache *nl_cli_exp_alloc_cache(struct nl_sock *sk)
36 {
37 	return nl_cli_alloc_cache(sk, "expectation", nfnl_exp_alloc_cache);
38 }
39 
nl_cli_exp_parse_family(struct nfnl_exp * exp,char * arg)40 void nl_cli_exp_parse_family(struct nfnl_exp *exp, char *arg)
41 {
42 	int family;
43 
44 	if ((family = nl_str2af(arg)) == AF_UNSPEC)
45 		nl_cli_fatal(EINVAL,
46 			     "Unable to nl_cli_exp_parse family \"%s\": %s",
47 			     arg, nl_geterror(NLE_INVAL));
48 
49 	nfnl_exp_set_family(exp, family);
50 }
51 
nl_cli_exp_parse_timeout(struct nfnl_exp * exp,char * arg)52 void nl_cli_exp_parse_timeout(struct nfnl_exp *exp, char *arg)
53 {
54 	uint32_t timeout = nl_cli_parse_u32(arg);
55 	nfnl_exp_set_timeout(exp, timeout);
56 }
57 
nl_cli_exp_parse_id(struct nfnl_exp * exp,char * arg)58 void nl_cli_exp_parse_id(struct nfnl_exp *exp, char *arg)
59 {
60 	uint32_t id = nl_cli_parse_u32(arg);
61 	nfnl_exp_set_id(exp, id);
62 }
63 
nl_cli_exp_parse_helper_name(struct nfnl_exp * exp,char * arg)64 void nl_cli_exp_parse_helper_name(struct nfnl_exp *exp, char *arg)
65 {
66 	nfnl_exp_set_helper_name(exp, arg);
67 }
68 
nl_cli_exp_parse_zone(struct nfnl_exp * exp,char * arg)69 void nl_cli_exp_parse_zone(struct nfnl_exp *exp, char *arg)
70 {
71 	uint32_t zone = nl_cli_parse_u32(arg);
72 	nfnl_exp_set_zone(exp, zone);
73 }
74 
nl_cli_exp_parse_flags(struct nfnl_exp * exp,char * arg)75 void nl_cli_exp_parse_flags(struct nfnl_exp *exp, char *arg)
76 {
77 	uint32_t flags = nl_cli_parse_u32(arg);
78 	nfnl_exp_set_flags(exp, flags);
79 }
80 
nl_cli_exp_parse_class(struct nfnl_exp * exp,char * arg)81 void nl_cli_exp_parse_class(struct nfnl_exp *exp, char *arg)
82 {
83 	uint32_t class = nl_cli_parse_u32(arg);
84 	nfnl_exp_set_class(exp, class);
85 }
86 
nl_cli_exp_parse_nat_dir(struct nfnl_exp * exp,char * arg)87 void nl_cli_exp_parse_nat_dir(struct nfnl_exp *exp, char *arg)
88 {
89 	uint32_t nat_dir = nl_cli_parse_u32(arg);
90 	nfnl_exp_set_nat_dir(exp, nat_dir);
91 }
92 
nl_cli_exp_parse_fn(struct nfnl_exp * exp,char * arg)93 void nl_cli_exp_parse_fn(struct nfnl_exp *exp, char *arg)
94 {
95 	nfnl_exp_set_fn(exp, arg);
96 }
97 
nl_cli_exp_parse_src(struct nfnl_exp * exp,int tuple,char * arg)98 void nl_cli_exp_parse_src(struct nfnl_exp *exp, int tuple, char *arg)
99 {
100 	int err;
101 	struct nl_addr *a = nl_cli_addr_parse(arg, nfnl_exp_get_family(exp));
102 	if ((err = nfnl_exp_set_src(exp, tuple, a)) < 0)
103 		nl_cli_fatal(err, "Unable to set source address: %s",
104 			     nl_geterror(err));
105 }
106 
nl_cli_exp_parse_dst(struct nfnl_exp * exp,int tuple,char * arg)107 void nl_cli_exp_parse_dst(struct nfnl_exp *exp, int tuple, char *arg)
108 {
109 	int err;
110 	struct nl_addr *a = nl_cli_addr_parse(arg, nfnl_exp_get_family(exp));
111 	if ((err = nfnl_exp_set_dst(exp, tuple, a)) < 0)
112 		nl_cli_fatal(err, "Unable to set destination address: %s",
113 			     nl_geterror(err));
114 }
115 
nl_cli_exp_parse_l4protonum(struct nfnl_exp * exp,int tuple,char * arg)116 void nl_cli_exp_parse_l4protonum(struct nfnl_exp *exp, int tuple, char *arg)
117 {
118 	int l4protonum;
119 
120 	if ((l4protonum = nl_str2ip_proto(arg)) < 0)
121 		nl_cli_fatal(l4protonum,
122 			"Unable to nl_cli_exp_parse protocol \"%s\": %s",
123 			arg, nl_geterror(l4protonum));
124 
125 	nfnl_exp_set_l4protonum(exp, tuple, l4protonum);
126 }
127 
nl_cli_exp_parse_src_port(struct nfnl_exp * exp,int tuple,char * arg)128 void nl_cli_exp_parse_src_port(struct nfnl_exp *exp, int tuple, char *arg)
129 {
130 	uint32_t sport = nl_cli_parse_u32(arg);
131 	uint16_t dport = nfnl_exp_get_dst_port(exp, tuple);
132 	nfnl_exp_set_ports(exp, tuple, sport, dport);
133 }
134 
nl_cli_exp_parse_dst_port(struct nfnl_exp * exp,int tuple,char * arg)135 void nl_cli_exp_parse_dst_port(struct nfnl_exp *exp, int tuple, char *arg)
136 {
137 	uint32_t dport = nl_cli_parse_u32(arg);
138 	uint16_t sport = nfnl_exp_get_src_port(exp, tuple);
139 	nfnl_exp_set_ports(exp, tuple, sport, dport);
140 }
141 
nl_cli_exp_parse_icmp_id(struct nfnl_exp * exp,int tuple,char * arg)142 void nl_cli_exp_parse_icmp_id(struct nfnl_exp *exp, int tuple, char *arg)
143 {
144 	uint32_t id = nl_cli_parse_u32(arg);
145 	uint8_t type = nfnl_exp_get_icmp_type(exp, tuple);
146 	uint8_t code = nfnl_exp_get_icmp_code(exp, tuple);
147 	nfnl_exp_set_icmp(exp, tuple, id, type, code);
148 }
149 
nl_cli_exp_parse_icmp_type(struct nfnl_exp * exp,int tuple,char * arg)150 void nl_cli_exp_parse_icmp_type(struct nfnl_exp *exp, int tuple, char *arg)
151 {
152 	uint32_t type = nl_cli_parse_u32(arg);
153 	uint16_t id = nfnl_exp_get_icmp_id(exp, tuple);
154 	uint8_t code = nfnl_exp_get_icmp_code(exp, tuple);
155 	nfnl_exp_set_icmp(exp, tuple, id, type, code);
156 }
157 
nl_cli_exp_parse_icmp_code(struct nfnl_exp * exp,int tuple,char * arg)158 void nl_cli_exp_parse_icmp_code(struct nfnl_exp *exp, int tuple, char *arg)
159 {
160 	uint32_t code = nl_cli_parse_u32(arg);
161 	uint16_t id = nfnl_exp_get_icmp_id(exp, tuple);
162 	uint8_t type = nfnl_exp_get_icmp_type(exp, tuple);
163 	nfnl_exp_set_icmp(exp, tuple, id, type, code);
164 }
165 
166 /** @} */
167