blob: 3659a7a897ff102ca16af812e6bb3d9baf70cdff [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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>
Neale Ranns8d7c5022019-02-06 01:41:05 -080022#include <vnet/fib/fib_table.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
25
26u8 *
27format_ipsec_policy_action (u8 * s, va_list * args)
28{
29 u32 i = va_arg (*args, u32);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070030 char *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
32 switch (i)
33 {
34#define _(v,f,str) case IPSEC_POLICY_ACTION_##f: t = str; break;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070035 foreach_ipsec_policy_action
Ed Warnickecb9cada2015-12-08 15:45:58 -070036#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070037 default:
38 s = format (s, "unknown");
Ed Warnickecb9cada2015-12-08 15:45:58 -070039 }
40 s = format (s, "%s", t);
41 return s;
42}
43
Neale Ranns9f231d42019-03-19 10:06:00 +000044u8 *
45format_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
Ed Warnickecb9cada2015-12-08 15:45:58 -070062uword
63unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
64{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070065 u32 *r = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070067 if (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
69 foreach_ipsec_policy_action
70#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070071 else
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 return 0;
73 return 1;
74}
75
76u8 *
77format_ipsec_crypto_alg (u8 * s, va_list * args)
78{
79 u32 i = va_arg (*args, u32);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070080 u8 *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
82 switch (i)
83 {
84#define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070085 foreach_ipsec_crypto_alg
Ed Warnickecb9cada2015-12-08 15:45:58 -070086#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070087 default:
88 s = format (s, "unknown");
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 }
90 s = format (s, "%s", t);
91 return s;
92}
93
94uword
95unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
96{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070097 u32 *r = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070099 if (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
101 foreach_ipsec_crypto_alg
102#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700103 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104 return 0;
105 return 1;
106}
107
108u8 *
109format_ipsec_integ_alg (u8 * s, va_list * args)
110{
111 u32 i = va_arg (*args, u32);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700112 u8 *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
114 switch (i)
115 {
116#define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700117 foreach_ipsec_integ_alg
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700119 default:
120 s = format (s, "unknown");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 }
122 s = format (s, "%s", t);
123 return s;
124}
125
126uword
127unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
128{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700129 u32 *r = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700131 if (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
133 foreach_ipsec_integ_alg
134#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700135 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 return 0;
137 return 1;
138}
139
140u8 *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700141format_ipsec_replay_window (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142{
143 u64 w = va_arg (*args, u64);
144 u8 i;
145
146 for (i = 0; i < 64; i++)
147 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700148 s = format (s, "%u", w & (1ULL << i) ? 1 : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 }
150
151 return s;
152}
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700153
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800154u8 *
155format_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
Neale Ranns9f231d42019-03-19 10:06:00 +0000164 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);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800168 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 }
Neale Ranns231c4692019-03-18 17:11:28 +0000180
181 s = format (s, "\n local addr range %U - %U port range %u - %u",
182 format_ip46_address, &p->laddr.start, IP46_TYPE_ANY,
183 format_ip46_address, &p->laddr.stop, IP46_TYPE_ANY,
184 clib_net_to_host_u16 (p->lport.start),
185 clib_net_to_host_u16 (p->lport.stop));
186 s = format (s, "\n remote addr range %U - %U port range %u - %u",
187 format_ip46_address, &p->raddr.start, IP46_TYPE_ANY,
188 format_ip46_address, &p->raddr.stop, IP46_TYPE_ANY,
189 clib_net_to_host_u16 (p->rport.start),
190 clib_net_to_host_u16 (p->rport.stop));
191
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800192 vlib_get_combined_counter (&ipsec_spd_policy_counters, pi, &counts);
193 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
194
195 return (s);
196}
197
198u8 *
199format_ipsec_spd (u8 * s, va_list * args)
200{
201 u32 si = va_arg (*args, u32);
202 ipsec_main_t *im = &ipsec_main;
203 ipsec_spd_t *spd;
204 u32 *i;
205
206 spd = pool_elt_at_index (im->spds, si);
207
208 s = format (s, "spd %u", spd->id);
209
210#define _(v, n) \
211 s = format (s, "\n %s:", n); \
212 vec_foreach(i, spd->policies[IPSEC_SPD_POLICY_##v]) \
213 { \
214 s = format (s, "\n %U", format_ipsec_policy, *i); \
215 }
216 foreach_ipsec_spd_policy_type;
217#undef _
218
219 return (s);
220}
221
Neale Ranns8d7c5022019-02-06 01:41:05 -0800222u8 *
223format_ipsec_key (u8 * s, va_list * args)
224{
225 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
226
227 return (format (s, "%U", format_hex_bytes, key->data, key->len));
228}
229
230uword
231unformat_ipsec_key (unformat_input_t * input, va_list * args)
232{
233 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
234 u8 *data;
235
236 if (unformat (input, "%U", unformat_hex_string, &data))
237 {
238 ipsec_mk_key (key, data, vec_len (data));
239 vec_free (data);
240 }
241 else
242 return 0;
243 return 1;
244}
245
246u8 *
247format_ipsec_sa (u8 * s, va_list * args)
248{
249 u32 sai = va_arg (*args, u32);
250 ipsec_main_t *im = &ipsec_main;
Neale Rannseba31ec2019-02-17 18:04:27 +0000251 vlib_counter_t counts;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800252 u32 tx_table_id;
253 ipsec_sa_t *sa;
254
255 sa = pool_elt_at_index (im->sad, sai);
256
Kingwel Xied3d12052019-03-07 06:34:30 -0500257 s = format (s, "[%d] sa 0x%x spi %u mode %s%s protocol %s%s%s%s",
Neale Ranns8d7c5022019-02-06 01:41:05 -0800258 sai, sa->id, sa->spi,
259 sa->is_tunnel ? "tunnel" : "transport",
260 sa->is_tunnel_ip6 ? "-ip6" : "",
261 sa->protocol ? "esp" : "ah",
262 sa->udp_encap ? " udp-encap-enabled" : "",
263 sa->use_anti_replay ? " anti-replay" : "",
264 sa->use_esn ? " extended-sequence-number" : "");
265 s = format (s, "\n last-seq %u last-seq-hi %u window %U",
266 sa->last_seq, sa->last_seq_hi,
267 format_ipsec_replay_window, sa->replay_window);
268 s = format (s, "\n crypto alg %U%s%U",
269 format_ipsec_crypto_alg, sa->crypto_alg,
270 sa->crypto_alg ? " key " : "",
271 format_ipsec_key, &sa->crypto_key);
272 s = format (s, "\n integrity alg %U%s%U",
273 format_ipsec_integ_alg, sa->integ_alg,
274 sa->integ_alg ? " key " : "", format_ipsec_key, &sa->integ_key);
Neale Rannseba31ec2019-02-17 18:04:27 +0000275 vlib_get_combined_counter (&ipsec_sa_counters, sai, &counts);
276 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800277
278 if (sa->is_tunnel)
279 {
280 tx_table_id = fib_table_get_table_id (sa->tx_fib_index,
281 FIB_PROTOCOL_IP4);
282 s = format (s, "\n table-ID %d tunnel src %U dst %U",
283 tx_table_id,
284 format_ip46_address, &sa->tunnel_src_addr, IP46_TYPE_ANY,
285 format_ip46_address, &sa->tunnel_dst_addr, IP46_TYPE_ANY);
286 s = format (s, "\n resovle via fib-entry: %d", sa->fib_entry_index);
287 s = format (s, "\n stacked on:");
288 s =
289 format (s, "\n %U", format_dpo_id, &sa->dpo[IPSEC_PROTOCOL_ESP],
290 6);
291 }
292
293 return (s);
294}
295
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700296/*
297 * fd.io coding-style-patch-verification: ON
298 *
299 * Local Variables:
300 * eval: (c-set-style "gnu")
301 * End:
302 */