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