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