IPSEC: tidy the policy types
[vpp.git] / src / vnet / ipsec / ipsec_format.c
1 /*
2  * decap.c : IPSec tunnel support
3  *
4  * Copyright (c) 2015 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/interface.h>
22 #include <vnet/fib/fib_table.h>
23
24 #include <vnet/ipsec/ipsec.h>
25
26 u8 *
27 format_ipsec_policy_action (u8 * s, va_list * args)
28 {
29   u32 i = va_arg (*args, u32);
30   char *t = 0;
31
32   switch (i)
33     {
34 #define _(v,f,str) case IPSEC_POLICY_ACTION_##f: t = str; break;
35       foreach_ipsec_policy_action
36 #undef _
37     default:
38       s = format (s, "unknown");
39     }
40   s = format (s, "%s", t);
41   return s;
42 }
43
44 u8 *
45 format_ipsec_policy_type (u8 * s, va_list * args)
46 {
47   u32 i = va_arg (*args, u32);
48   char *t = 0;
49
50   switch (i)
51     {
52 #define _(f,str) case IPSEC_SPD_POLICY_##f: t = str; break;
53       foreach_ipsec_spd_policy_type
54 #undef _
55     default:
56       s = format (s, "unknown");
57     }
58   s = format (s, "%s", t);
59   return s;
60 }
61
62 uword
63 unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
64 {
65   u32 *r = va_arg (*args, u32 *);
66
67   if (0);
68 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
69   foreach_ipsec_policy_action
70 #undef _
71     else
72     return 0;
73   return 1;
74 }
75
76 u8 *
77 format_ipsec_crypto_alg (u8 * s, va_list * args)
78 {
79   u32 i = va_arg (*args, u32);
80   u8 *t = 0;
81
82   switch (i)
83     {
84 #define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
85       foreach_ipsec_crypto_alg
86 #undef _
87     default:
88       s = format (s, "unknown");
89     }
90   s = format (s, "%s", t);
91   return s;
92 }
93
94 uword
95 unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
96 {
97   u32 *r = va_arg (*args, u32 *);
98
99   if (0);
100 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
101   foreach_ipsec_crypto_alg
102 #undef _
103     else
104     return 0;
105   return 1;
106 }
107
108 u8 *
109 format_ipsec_integ_alg (u8 * s, va_list * args)
110 {
111   u32 i = va_arg (*args, u32);
112   u8 *t = 0;
113
114   switch (i)
115     {
116 #define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
117       foreach_ipsec_integ_alg
118 #undef _
119     default:
120       s = format (s, "unknown");
121     }
122   s = format (s, "%s", t);
123   return s;
124 }
125
126 uword
127 unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
128 {
129   u32 *r = va_arg (*args, u32 *);
130
131   if (0);
132 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
133   foreach_ipsec_integ_alg
134 #undef _
135     else
136     return 0;
137   return 1;
138 }
139
140 u8 *
141 format_ipsec_replay_window (u8 * s, va_list * args)
142 {
143   u64 w = va_arg (*args, u64);
144   u8 i;
145
146   for (i = 0; i < 64; i++)
147     {
148       s = format (s, "%u", w & (1ULL << i) ? 1 : 0);
149     }
150
151   return s;
152 }
153
154 u8 *
155 format_ipsec_policy (u8 * s, va_list * args)
156 {
157   u32 pi = va_arg (*args, u32);
158   ipsec_main_t *im = &ipsec_main;
159   ipsec_policy_t *p;
160   vlib_counter_t counts;
161
162   p = pool_elt_at_index (im->policies, pi);
163
164   s = format (s, "  [%d] priority %d action %U type %U protocol ",
165               pi, p->priority,
166               format_ipsec_policy_action, p->policy,
167               format_ipsec_policy_type, p->type);
168   if (p->protocol)
169     {
170       s = format (s, "%U", format_ip_protocol, p->protocol);
171     }
172   else
173     {
174       s = format (s, "any");
175     }
176   if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
177     {
178       s = format (s, " sa %u", p->sa_id);
179     }
180   if (p->is_ipv6)
181     {
182       s = format (s, "\n     local addr range %U - %U port range %u - %u",
183                   format_ip6_address, &p->laddr.start.ip6,
184                   format_ip6_address, &p->laddr.stop.ip6,
185                   p->lport.start, p->lport.stop);
186       s = format (s, "\n     remote addr range %U - %U port range %u - %u",
187                   format_ip6_address, &p->raddr.start.ip6,
188                   format_ip6_address, &p->raddr.stop.ip6,
189                   p->rport.start, p->rport.stop);
190     }
191   else
192     {
193       s = format (s, "\n     local addr range %U - %U port range %u - %u",
194                   format_ip4_address, &p->laddr.start.ip4,
195                   format_ip4_address, &p->laddr.stop.ip4,
196                   p->lport.start, p->lport.stop);
197       s = format (s, "\n     remote addr range %U - %U port range %u - %u",
198                   format_ip4_address, &p->raddr.start.ip4,
199                   format_ip4_address, &p->raddr.stop.ip4,
200                   p->rport.start, p->rport.stop);
201     }
202   vlib_get_combined_counter (&ipsec_spd_policy_counters, pi, &counts);
203   s = format (s, "\n     packets %u bytes %u", counts.packets, counts.bytes);
204
205   return (s);
206 }
207
208 u8 *
209 format_ipsec_spd (u8 * s, va_list * args)
210 {
211   u32 si = va_arg (*args, u32);
212   ipsec_main_t *im = &ipsec_main;
213   ipsec_spd_t *spd;
214   u32 *i;
215
216   spd = pool_elt_at_index (im->spds, si);
217
218   s = format (s, "spd %u", spd->id);
219
220 #define _(v, n)                                                 \
221   s = format (s, "\n %s:", n);                                  \
222   vec_foreach(i, spd->policies[IPSEC_SPD_POLICY_##v])           \
223   {                                                             \
224     s = format (s, "\n %U", format_ipsec_policy, *i);           \
225   }
226   foreach_ipsec_spd_policy_type;
227 #undef _
228
229   return (s);
230 }
231
232 u8 *
233 format_ipsec_key (u8 * s, va_list * args)
234 {
235   ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
236
237   return (format (s, "%U", format_hex_bytes, key->data, key->len));
238 }
239
240 uword
241 unformat_ipsec_key (unformat_input_t * input, va_list * args)
242 {
243   ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
244   u8 *data;
245
246   if (unformat (input, "%U", unformat_hex_string, &data))
247     {
248       ipsec_mk_key (key, data, vec_len (data));
249       vec_free (data);
250     }
251   else
252     return 0;
253   return 1;
254 }
255
256 u8 *
257 format_ipsec_sa (u8 * s, va_list * args)
258 {
259   u32 sai = va_arg (*args, u32);
260   ipsec_main_t *im = &ipsec_main;
261   vlib_counter_t counts;
262   u32 tx_table_id;
263   ipsec_sa_t *sa;
264
265   sa = pool_elt_at_index (im->sad, sai);
266
267   s = format (s, "[%d] sa 0x%x spi %u mode %s%s protocol %s%s%s%s",
268               sai, sa->id, sa->spi,
269               sa->is_tunnel ? "tunnel" : "transport",
270               sa->is_tunnel_ip6 ? "-ip6" : "",
271               sa->protocol ? "esp" : "ah",
272               sa->udp_encap ? " udp-encap-enabled" : "",
273               sa->use_anti_replay ? " anti-replay" : "",
274               sa->use_esn ? " extended-sequence-number" : "");
275   s = format (s, "\n   last-seq %u last-seq-hi %u  window %U",
276               sa->last_seq, sa->last_seq_hi,
277               format_ipsec_replay_window, sa->replay_window);
278   s = format (s, "\n   crypto alg %U%s%U",
279               format_ipsec_crypto_alg, sa->crypto_alg,
280               sa->crypto_alg ? " key " : "",
281               format_ipsec_key, &sa->crypto_key);
282   s = format (s, "\n   integrity alg %U%s%U",
283               format_ipsec_integ_alg, sa->integ_alg,
284               sa->integ_alg ? " key " : "", format_ipsec_key, &sa->integ_key);
285   vlib_get_combined_counter (&ipsec_sa_counters, sai, &counts);
286   s = format (s, "\n   packets %u bytes %u", counts.packets, counts.bytes);
287
288   if (sa->is_tunnel)
289     {
290       tx_table_id = fib_table_get_table_id (sa->tx_fib_index,
291                                             FIB_PROTOCOL_IP4);
292       s = format (s, "\n   table-ID %d tunnel src %U dst %U",
293                   tx_table_id,
294                   format_ip46_address, &sa->tunnel_src_addr, IP46_TYPE_ANY,
295                   format_ip46_address, &sa->tunnel_dst_addr, IP46_TYPE_ANY);
296       s = format (s, "\n    resovle via fib-entry: %d", sa->fib_entry_index);
297       s = format (s, "\n    stacked on:");
298       s =
299         format (s, "\n      %U", format_dpo_id, &sa->dpo[IPSEC_PROTOCOL_ESP],
300                 6);
301     }
302
303   return (s);
304 }
305
306 /*
307  * fd.io coding-style-patch-verification: ON
308  *
309  * Local Variables:
310  * eval: (c-set-style "gnu")
311  * End:
312  */