ipsec: Revert "IPSEC: remove byte swap operations in DP during SPD classify"
[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 #include <vnet/ipsec/ipsec_tun.h>
26
27 u8 *
28 format_ipsec_policy_action (u8 * s, va_list * args)
29 {
30   u32 i = va_arg (*args, u32);
31   char *t = 0;
32
33   switch (i)
34     {
35 #define _(v,f,str) case IPSEC_POLICY_ACTION_##f: t = str; break;
36       foreach_ipsec_policy_action
37 #undef _
38     default:
39       s = format (s, "unknown");
40     }
41   s = format (s, "%s", t);
42   return s;
43 }
44
45 u8 *
46 format_ipsec_policy_type (u8 * s, va_list * args)
47 {
48   u32 i = va_arg (*args, u32);
49   char *t = 0;
50
51   switch (i)
52     {
53 #define _(f,str) case IPSEC_SPD_POLICY_##f: t = str; break;
54       foreach_ipsec_spd_policy_type
55 #undef _
56     default:
57       s = format (s, "unknown");
58     }
59   s = format (s, "%s", t);
60   return s;
61 }
62
63 uword
64 unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
65 {
66   u32 *r = va_arg (*args, u32 *);
67
68   if (0);
69 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
70   foreach_ipsec_policy_action
71 #undef _
72     else
73     return 0;
74   return 1;
75 }
76
77 u8 *
78 format_ipsec_crypto_alg (u8 * s, va_list * args)
79 {
80   u32 i = va_arg (*args, u32);
81   u8 *t = 0;
82
83   switch (i)
84     {
85 #define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
86       foreach_ipsec_crypto_alg
87 #undef _
88     default:
89       s = format (s, "unknown");
90     }
91   s = format (s, "%s", t);
92   return s;
93 }
94
95 uword
96 unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
97 {
98   u32 *r = va_arg (*args, u32 *);
99
100   if (0);
101 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
102   foreach_ipsec_crypto_alg
103 #undef _
104     else
105     return 0;
106   return 1;
107 }
108
109 u8 *
110 format_ipsec_integ_alg (u8 * s, va_list * args)
111 {
112   u32 i = va_arg (*args, u32);
113   u8 *t = 0;
114
115   switch (i)
116     {
117 #define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
118       foreach_ipsec_integ_alg
119 #undef _
120     default:
121       s = format (s, "unknown");
122     }
123   s = format (s, "%s", t);
124   return s;
125 }
126
127 uword
128 unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
129 {
130   u32 *r = va_arg (*args, u32 *);
131
132   if (0);
133 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
134   foreach_ipsec_integ_alg
135 #undef _
136     else
137     return 0;
138   return 1;
139 }
140
141 u8 *
142 format_ipsec_replay_window (u8 * s, va_list * args)
143 {
144   u64 w = va_arg (*args, u64);
145   u8 i;
146
147   for (i = 0; i < 64; i++)
148     {
149       s = format (s, "%u", w & (1ULL << i) ? 1 : 0);
150     }
151
152   return s;
153 }
154
155 u8 *
156 format_ipsec_policy (u8 * s, va_list * args)
157 {
158   u32 pi = va_arg (*args, u32);
159   ip46_type_t ip_type = IP46_TYPE_IP4;
160   ipsec_main_t *im = &ipsec_main;
161   ipsec_policy_t *p;
162   vlib_counter_t counts;
163
164   p = pool_elt_at_index (im->policies, pi);
165
166   s = format (s, "  [%d] priority %d action %U type %U protocol ",
167               pi, p->priority,
168               format_ipsec_policy_action, p->policy,
169               format_ipsec_policy_type, p->type);
170   if (p->protocol)
171     {
172       s = format (s, "%U", format_ip_protocol, p->protocol);
173     }
174   else
175     {
176       s = format (s, "any");
177     }
178   if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
179     {
180       s = format (s, " sa %u", p->sa_id);
181     }
182   if (p->is_ipv6)
183     {
184       ip_type = IP46_TYPE_IP6;
185     }
186
187   s = format (s, "\n     local addr range %U - %U port range %u - %u",
188               format_ip46_address, &p->laddr.start, ip_type,
189               format_ip46_address, &p->laddr.stop, ip_type,
190               p->lport.start, p->lport.stop);
191   s = format (s, "\n     remote addr range %U - %U port range %u - %u",
192               format_ip46_address, &p->raddr.start, ip_type,
193               format_ip46_address, &p->raddr.stop, ip_type,
194               p->rport.start, p->rport.stop);
195
196   vlib_get_combined_counter (&ipsec_spd_policy_counters, pi, &counts);
197   s = format (s, "\n     packets %u bytes %u", counts.packets, counts.bytes);
198
199   return (s);
200 }
201
202 u8 *
203 format_ipsec_spd (u8 * s, va_list * args)
204 {
205   u32 si = va_arg (*args, u32);
206   ipsec_main_t *im = &ipsec_main;
207   ipsec_spd_t *spd;
208   u32 *i;
209
210   if (pool_is_free_index (im->spds, si))
211     {
212       s = format (s, "No such SPD index: %d", si);
213       goto done;
214     }
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 done:
230   return (s);
231 }
232
233 u8 *
234 format_ipsec_key (u8 * s, va_list * args)
235 {
236   ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
237
238   return (format (s, "%U", format_hex_bytes, key->data, key->len));
239 }
240
241 uword
242 unformat_ipsec_key (unformat_input_t * input, va_list * args)
243 {
244   ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
245   u8 *data;
246
247   if (unformat (input, "%U", unformat_hex_string, &data))
248     {
249       ipsec_mk_key (key, data, vec_len (data));
250       vec_free (data);
251     }
252   else
253     return 0;
254   return 1;
255 }
256
257 u8 *
258 format_ipsec_sa_flags (u8 * s, va_list * args)
259 {
260   ipsec_sa_flags_t flags = va_arg (*args, int);
261
262   if (0)
263     ;
264 #define _(v, f, str) else if (flags & IPSEC_SA_FLAG_##f) s = format(s, "%s ", str);
265   foreach_ipsec_sa_flags
266 #undef _
267     return (s);
268 }
269
270 u8 *
271 format_ipsec_sa (u8 * s, va_list * args)
272 {
273   u32 sai = va_arg (*args, u32);
274   ipsec_format_flags_t flags = va_arg (*args, ipsec_format_flags_t);
275   ipsec_main_t *im = &ipsec_main;
276   vlib_counter_t counts;
277   u32 tx_table_id;
278   ipsec_sa_t *sa;
279
280   if (pool_is_free_index (im->sad, sai))
281     {
282       s = format (s, "No such SA index: %d", sai);
283       goto done;
284     }
285
286   sa = pool_elt_at_index (im->sad, sai);
287
288   s = format (s, "[%d] sa 0x%x spi %u (0x%08x) mode %s%s protocol %s %U",
289               sai, sa->id, sa->spi, sa->spi,
290               ipsec_sa_is_set_IS_TUNNEL (sa) ? "tunnel" : "transport",
291               ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ? "-ip6" : "",
292               sa->protocol ? "esp" : "ah", format_ipsec_sa_flags, sa->flags);
293
294   if (!(flags & IPSEC_FORMAT_DETAIL))
295     goto done;
296
297   s = format (s, "\n   salt 0x%x", clib_net_to_host_u32 (sa->salt));
298   s = format (s, "\n   seq %u seq-hi %u", sa->seq, sa->seq_hi);
299   s = format (s, "\n   last-seq %u last-seq-hi %u window %U",
300               sa->last_seq, sa->last_seq_hi,
301               format_ipsec_replay_window, sa->replay_window);
302   s = format (s, "\n   crypto alg %U",
303               format_ipsec_crypto_alg, sa->crypto_alg);
304   if (sa->crypto_alg)
305     s = format (s, " key %U", format_ipsec_key, &sa->crypto_key);
306   s = format (s, "\n   integrity alg %U",
307               format_ipsec_integ_alg, sa->integ_alg);
308   if (sa->integ_alg)
309     s = format (s, " key %U", format_ipsec_key, &sa->integ_key);
310
311   vlib_get_combined_counter (&ipsec_sa_counters, sai, &counts);
312   s = format (s, "\n   packets %u bytes %u", counts.packets, counts.bytes);
313
314   if (ipsec_sa_is_set_IS_TUNNEL (sa))
315     {
316       tx_table_id = fib_table_get_table_id (sa->tx_fib_index,
317                                             FIB_PROTOCOL_IP4);
318       s = format (s, "\n   table-ID %d tunnel src %U dst %U",
319                   tx_table_id,
320                   format_ip46_address, &sa->tunnel_src_addr, IP46_TYPE_ANY,
321                   format_ip46_address, &sa->tunnel_dst_addr, IP46_TYPE_ANY);
322       if (!ipsec_sa_is_set_IS_INBOUND (sa))
323         {
324           s =
325             format (s, "\n    resovle via fib-entry: %d",
326                     sa->fib_entry_index);
327           s = format (s, "\n    stacked on:");
328           s = format (s, "\n      %U", format_dpo_id, &sa->dpo, 6);
329         }
330     }
331
332 done:
333   return (s);
334 }
335
336 u8 *
337 format_ipsec_tunnel (u8 * s, va_list * args)
338 {
339   ipsec_main_t *im = &ipsec_main;
340   u32 ti = va_arg (*args, u32);
341   ipsec_tunnel_if_t *t;
342
343   if (pool_is_free_index (im->tunnel_interfaces, ti))
344     {
345       s = format (s, "No such tunnel index: %d", ti);
346       goto done;
347     }
348
349   t = pool_elt_at_index (im->tunnel_interfaces, ti);
350
351   if (t->hw_if_index == ~0)
352     goto done;
353
354   s =
355     format (s, "%U\n", format_vnet_hw_if_index_name, im->vnet_main,
356             t->hw_if_index);
357
358   s = format (s, "   out-bound sa: ");
359   s = format (s, "%U\n", format_ipsec_sa, t->output_sa_index,
360               IPSEC_FORMAT_BRIEF);
361
362   s = format (s, "    in-bound sa: ");
363   s = format (s, "%U\n", format_ipsec_sa, t->input_sa_index,
364               IPSEC_FORMAT_BRIEF);
365
366 done:
367   return (s);
368 }
369
370 u8 *
371 format_ipsec_tun_protect (u8 * s, va_list * args)
372 {
373   u32 itpi = va_arg (*args, u32);
374   ipsec_tun_protect_t *itp;
375   u32 sai;
376
377   if (pool_is_free_index (ipsec_protect_pool, itpi))
378     {
379       s = format (s, "No such tunnel index: %d", itpi);
380       goto done;
381     }
382
383   itp = pool_elt_at_index (ipsec_protect_pool, itpi);
384
385   s = format (s, "%U", format_vnet_sw_if_index_name,
386               vnet_get_main (), itp->itp_sw_if_index);
387   s = format (s, "\n output-sa:");
388   s =
389     format (s, "\n  %U", format_ipsec_sa, itp->itp_out_sa,
390             IPSEC_FORMAT_BRIEF);
391
392   s = format (s, "\n input-sa:");
393   /* *INDENT-OFF* */
394   FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
395   ({
396   s = format (s, "\n  %U", format_ipsec_sa, sai, IPSEC_FORMAT_BRIEF);
397   }));
398   /* *INDENT-ON* */
399
400 done:
401   return (s);
402 }
403
404 /*
405  * fd.io coding-style-patch-verification: ON
406  *
407  * Local Variables:
408  * eval: (c-set-style "gnu")
409  * End:
410  */