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