2 * decap.c : IPSec tunnel support
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
24 #include <vnet/ipsec/ipsec.h>
27 format_ipsec_policy_action (u8 * s, va_list * args)
29 u32 i = va_arg (*args, u32);
34 #define _(v,f,str) case IPSEC_POLICY_ACTION_##f: t = str; break;
35 foreach_ipsec_policy_action
38 s = format (s, "unknown");
40 s = format (s, "%s", t);
45 unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
47 u32 *r = va_arg (*args, u32 *);
50 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
51 foreach_ipsec_policy_action
59 format_ipsec_crypto_alg (u8 * s, va_list * args)
61 u32 i = va_arg (*args, u32);
66 #define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
67 foreach_ipsec_crypto_alg
70 s = format (s, "unknown");
72 s = format (s, "%s", t);
77 unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
79 u32 *r = va_arg (*args, u32 *);
82 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
83 foreach_ipsec_crypto_alg
91 format_ipsec_integ_alg (u8 * s, va_list * args)
93 u32 i = va_arg (*args, u32);
98 #define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
99 foreach_ipsec_integ_alg
102 s = format (s, "unknown");
104 s = format (s, "%s", t);
109 unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
111 u32 *r = va_arg (*args, u32 *);
114 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
115 foreach_ipsec_integ_alg
123 format_ipsec_replay_window (u8 * s, va_list * args)
125 u64 w = va_arg (*args, u64);
128 for (i = 0; i < 64; i++)
130 s = format (s, "%u", w & (1ULL << i) ? 1 : 0);
137 format_ipsec_policy (u8 * s, va_list * args)
139 u32 pi = va_arg (*args, u32);
140 ipsec_main_t *im = &ipsec_main;
142 vlib_counter_t counts;
144 p = pool_elt_at_index (im->policies, pi);
146 s = format (s, " [%d] priority %d action %U protocol ",
147 pi, p->priority, format_ipsec_policy_action, p->policy);
150 s = format (s, "%U", format_ip_protocol, p->protocol);
154 s = format (s, "any");
156 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
158 s = format (s, " sa %u", p->sa_id);
162 s = format (s, "\n local addr range %U - %U port range %u - %u",
163 format_ip6_address, &p->laddr.start.ip6,
164 format_ip6_address, &p->laddr.stop.ip6,
165 p->lport.start, p->lport.stop);
166 s = format (s, "\n remote addr range %U - %U port range %u - %u",
167 format_ip6_address, &p->raddr.start.ip6,
168 format_ip6_address, &p->raddr.stop.ip6,
169 p->rport.start, p->rport.stop);
173 s = format (s, "\n local addr range %U - %U port range %u - %u",
174 format_ip4_address, &p->laddr.start.ip4,
175 format_ip4_address, &p->laddr.stop.ip4,
176 p->lport.start, p->lport.stop);
177 s = format (s, "\n remote addr range %U - %U port range %u - %u",
178 format_ip4_address, &p->raddr.start.ip4,
179 format_ip4_address, &p->raddr.stop.ip4,
180 p->rport.start, p->rport.stop);
182 vlib_get_combined_counter (&ipsec_spd_policy_counters, pi, &counts);
183 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
189 format_ipsec_spd (u8 * s, va_list * args)
191 u32 si = va_arg (*args, u32);
192 ipsec_main_t *im = &ipsec_main;
196 spd = pool_elt_at_index (im->spds, si);
198 s = format (s, "spd %u", spd->id);
201 s = format (s, "\n %s:", n); \
202 vec_foreach(i, spd->policies[IPSEC_SPD_POLICY_##v]) \
204 s = format (s, "\n %U", format_ipsec_policy, *i); \
206 foreach_ipsec_spd_policy_type;
213 format_ipsec_key (u8 * s, va_list * args)
215 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
217 return (format (s, "%U", format_hex_bytes, key->data, key->len));
221 unformat_ipsec_key (unformat_input_t * input, va_list * args)
223 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
226 if (unformat (input, "%U", unformat_hex_string, &data))
228 ipsec_mk_key (key, data, vec_len (data));
237 format_ipsec_sa (u8 * s, va_list * args)
239 u32 sai = va_arg (*args, u32);
240 ipsec_main_t *im = &ipsec_main;
244 sa = pool_elt_at_index (im->sad, sai);
246 s = format (s, "[%d] sa %u spi %u mode %s%s protocol %s%s%s%s",
247 sai, sa->id, sa->spi,
248 sa->is_tunnel ? "tunnel" : "transport",
249 sa->is_tunnel_ip6 ? "-ip6" : "",
250 sa->protocol ? "esp" : "ah",
251 sa->udp_encap ? " udp-encap-enabled" : "",
252 sa->use_anti_replay ? " anti-replay" : "",
253 sa->use_esn ? " extended-sequence-number" : "");
254 s = format (s, "\n last-seq %u last-seq-hi %u window %U",
255 sa->last_seq, sa->last_seq_hi,
256 format_ipsec_replay_window, sa->replay_window);
257 s = format (s, "\n crypto alg %U%s%U",
258 format_ipsec_crypto_alg, sa->crypto_alg,
259 sa->crypto_alg ? " key " : "",
260 format_ipsec_key, &sa->crypto_key);
261 s = format (s, "\n integrity alg %U%s%U",
262 format_ipsec_integ_alg, sa->integ_alg,
263 sa->integ_alg ? " key " : "", format_ipsec_key, &sa->integ_key);
267 tx_table_id = fib_table_get_table_id (sa->tx_fib_index,
269 s = format (s, "\n table-ID %d tunnel src %U dst %U",
271 format_ip46_address, &sa->tunnel_src_addr, IP46_TYPE_ANY,
272 format_ip46_address, &sa->tunnel_dst_addr, IP46_TYPE_ANY);
273 s = format (s, "\n resovle via fib-entry: %d", sa->fib_entry_index);
274 s = format (s, "\n stacked on:");
276 format (s, "\n %U", format_dpo_id, &sa->dpo[IPSEC_PROTOCOL_ESP],
284 * fd.io coding-style-patch-verification: ON
287 * eval: (c-set-style "gnu")