Initial commit of vpp code.
[vpp.git] / vnet / vnet / ipsec / ipsec_input.c
1 /*
2  * decap.c : IPSec tunnel decapsulation
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
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24
25 #define foreach_ipsec_input_next                \
26 _(DROP, "error-drop")                           \
27 _(ESP_DECRYPT, "esp-decrypt")
28
29 #define _(v, s) IPSEC_INPUT_NEXT_##v,
30 typedef enum {
31   foreach_ipsec_input_next
32 #undef _
33   IPSEC_INPUT_N_NEXT,
34 } ipsec_input_next_t;
35
36
37 #define foreach_ipsec_input_error                   \
38  _(RX_PKTS, "IPSEC pkts received")                  \
39  _(DECRYPTION_FAILED, "IPSEC decryption failed")
40
41
42 typedef enum {
43 #define _(sym,str) IPSEC_INPUT_ERROR_##sym,
44   foreach_ipsec_input_error
45 #undef _
46   IPSEC_INPUT_N_ERROR,
47 } ipsec_input_error_t;
48
49 static char * ipsec_input_error_strings[] = {
50 #define _(sym,string) string,
51   foreach_ipsec_input_error
52 #undef _
53 };
54
55 vlib_node_registration_t ipsec_input_node;
56
57 typedef struct {
58   u32 tunnel_index;
59   u32 spi;
60   u32 seq;
61 } ipsec_input_trace_t;
62
63 /* packet trace format function */
64 static u8 * format_ipsec_input_trace (u8 * s, va_list * args)
65 {
66   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
67   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
68   ipsec_input_trace_t * t = va_arg (*args, ipsec_input_trace_t *);
69
70   if (t->tunnel_index != ~0)
71   {
72     s = format (s, "esp: tunnel %u spi %u seq %u", t->tunnel_index, t->spi, t->seq);
73   }
74   else
75   {
76     s = format (s, "esp: no tunnel spi %u seq %u",t->spi, t->seq);
77   }
78   return s;
79 }
80
81 always_inline ipsec_policy_t *
82 ipsec_input_protect_policy_match(ipsec_spd_t * spd, u32 sa, u32 da, u32 spi)
83 {
84   ipsec_main_t *im = &ipsec_main;
85   ipsec_policy_t * p;
86   ipsec_sa_t * s;
87   u32 * i;
88
89   vec_foreach(i, spd->ipv4_inbound_protect_policy_indices)
90     {
91       p = pool_elt_at_index(spd->policies, *i);
92       s = pool_elt_at_index(im->sad, p->sa_index);
93
94       if (spi != s->spi)
95         continue;
96
97       if (s->is_tunnel)
98         {
99            if (da != clib_net_to_host_u32(s->tunnel_dst_addr.ip4.as_u32))
100                continue;
101
102            if (sa != clib_net_to_host_u32(s->tunnel_src_addr.ip4.as_u32))
103                continue;
104
105            return p;
106         }
107
108       if (da < clib_net_to_host_u32(p->laddr.start.ip4.as_u32))
109         continue;
110
111       if (da > clib_net_to_host_u32(p->laddr.stop.ip4.as_u32))
112         continue;
113
114       if (sa < clib_net_to_host_u32(p->raddr.start.ip4.as_u32))
115         continue;
116
117       if (sa > clib_net_to_host_u32(p->raddr.stop.ip4.as_u32))
118         continue;
119
120       return p;
121     }
122     return 0;
123 }
124
125 always_inline uword
126 ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la, ip6_address_t * ua)
127 {
128   if ((memcmp(a->as_u64, la->as_u64, 2 * sizeof(u64)) >= 0) &&
129       (memcmp(a->as_u64, ua->as_u64, 2 * sizeof(u64)) <= 0))
130     return 1;
131   return 0;
132 }
133
134 always_inline ipsec_policy_t *
135 ipsec_input_ip6_protect_policy_match (ipsec_spd_t * spd,
136                                       ip6_address_t * sa,
137                                       ip6_address_t * da,
138                                       u32 spi)
139 {
140   ipsec_main_t *im = &ipsec_main;
141   ipsec_policy_t * p;
142   ipsec_sa_t * s;
143   u32 * i;
144
145   vec_foreach(i, spd->ipv6_inbound_protect_policy_indices)
146     {
147       p = pool_elt_at_index(spd->policies, *i);
148       s = pool_elt_at_index(im->sad, p->sa_index);
149
150       if (spi != s->spi)
151         continue;
152
153       if (s->is_tunnel)
154         {
155           if (!ip6_address_is_equal(sa, &s->tunnel_src_addr.ip6))
156             continue;
157
158           if (!ip6_address_is_equal(da, &s->tunnel_dst_addr.ip6))
159             continue;
160
161           return p;
162         }
163
164       if (!ip6_addr_match_range(sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
165         continue;
166
167       if (!ip6_addr_match_range(da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
168         continue;
169
170       return p;
171     }
172     return 0;
173 }
174
175 static uword
176 ipsec_input_ip4_node_fn (vlib_main_t * vm,
177                          vlib_node_runtime_t * node,
178                          vlib_frame_t * from_frame)
179 {
180   ip4_main_t * i4m = &ip4_main;
181   ip_lookup_main_t * lm = &i4m->lookup_main;
182   ip_config_main_t * cm = &lm->rx_config_mains[VNET_UNICAST];
183   u32 n_left_from, *from, next_index, *to_next;
184   ipsec_main_t *im = &ipsec_main;
185
186   from = vlib_frame_vector_args (from_frame);
187   n_left_from = from_frame->n_vectors;
188
189   next_index = node->cached_next_index;
190
191   while (n_left_from > 0)
192     {
193       u32 n_left_to_next;
194
195       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
196
197       while (n_left_from > 0 && n_left_to_next > 0)
198         {
199           u32 bi0, next0;
200           vlib_buffer_t * b0;
201           ip4_header_t *ip0;
202           esp_header_t *esp0;
203           ip4_ipsec_config_t * c0;
204           u32 tunnel_index0 = ~0;
205           ipsec_spd_t * spd0;
206
207           bi0 = to_next[0] = from[0];
208           from += 1;
209           n_left_from -= 1;
210           to_next +=1;
211           n_left_to_next -= 1;
212
213           b0 = vlib_get_buffer (vm, bi0);
214           c0 = vnet_get_config_data (&cm->config_main,
215                                      &vnet_buffer (b0)->ip.current_config_index,
216                                      &next0, sizeof (c0[0]));
217
218           spd0 = pool_elt_at_index(im->spds, c0->spd_index);
219
220           ip0 = vlib_buffer_get_current (b0);
221           esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
222
223           if (PREDICT_TRUE(ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
224             {
225 #if 0
226               clib_warning("packet received from %U to %U spi %u size %u spd_id %u",
227                            format_ip4_address, ip0->src_address.as_u8,
228                            format_ip4_address, ip0->dst_address.as_u8,
229                            clib_net_to_host_u32(esp0->spi),
230                            clib_net_to_host_u16(ip0->length),
231                            spd0->id);
232 #endif
233               ipsec_policy_t * p0;
234               p0 = ipsec_input_protect_policy_match(spd0,
235                          clib_net_to_host_u32(ip0->src_address.as_u32),
236                          clib_net_to_host_u32(ip0->dst_address.as_u32),
237                          clib_net_to_host_u32(esp0->spi));
238
239               if (PREDICT_TRUE(p0 != 0))
240                 {
241                   p0->counter.packets++;
242                   p0->counter.bytes += clib_net_to_host_u16(ip0->length);
243                   vnet_buffer(b0)->output_features.ipsec_sad_index = p0->sa_index;
244                   next0 = IPSEC_INPUT_NEXT_ESP_DECRYPT;
245                   vlib_buffer_advance(b0, ip4_header_bytes (ip0));
246                   goto trace0;
247                 }
248             }
249
250           /* FIXME bypass and discard */
251
252 trace0:
253           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) {
254             ipsec_input_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
255             tr->tunnel_index = tunnel_index0;
256             tr->spi = clib_host_to_net_u32(esp0->spi);
257             tr->seq = clib_host_to_net_u32(esp0->seq);
258           }
259
260                 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
261                                           to_next, n_left_to_next, bi0, next0);
262         }
263       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
264     }
265   vlib_node_increment_counter (vm, ipsec_input_ip4_node.index,
266                                IPSEC_INPUT_ERROR_RX_PKTS,
267                                from_frame->n_vectors);
268
269   return from_frame->n_vectors;
270 }
271
272
273 VLIB_REGISTER_NODE (ipsec_input_ip4_node) = {
274   .function = ipsec_input_ip4_node_fn,
275   .name = "ipsec-input-ip4",
276   .vector_size = sizeof (u32),
277   .format_trace = format_ipsec_input_trace,
278   .type = VLIB_NODE_TYPE_INTERNAL,
279
280   .n_errors = ARRAY_LEN(ipsec_input_error_strings),
281   .error_strings = ipsec_input_error_strings,
282
283   .n_next_nodes = IPSEC_INPUT_N_NEXT,
284   .next_nodes = {
285 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
286     foreach_ipsec_input_next
287 #undef _
288   },
289 };
290
291
292 static uword
293 ipsec_input_ip6_node_fn (vlib_main_t * vm,
294                          vlib_node_runtime_t * node,
295                          vlib_frame_t * from_frame)
296 {
297   ip6_main_t * i6m = &ip6_main;
298   ip_lookup_main_t * lm = &i6m->lookup_main;
299   ip_config_main_t * cm = &lm->rx_config_mains[VNET_UNICAST];
300   u32 n_left_from, *from, next_index, *to_next;
301   ipsec_main_t *im = &ipsec_main;
302
303   from = vlib_frame_vector_args (from_frame);
304   n_left_from = from_frame->n_vectors;
305
306   next_index = node->cached_next_index;
307
308   while (n_left_from > 0)
309     {
310       u32 n_left_to_next;
311
312       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
313
314       while (n_left_from > 0 && n_left_to_next > 0)
315         {
316           u32 bi0, next0;
317           vlib_buffer_t * b0;
318           ip6_header_t *ip0;
319           esp_header_t *esp0;
320           ip4_ipsec_config_t * c0;
321           u32 tunnel_index0 = ~0;
322           ipsec_spd_t * spd0;
323           u32 header_size = sizeof(ip0[0]);
324
325           bi0 = to_next[0] = from[0];
326           from += 1;
327           n_left_from -= 1;
328           to_next +=1;
329           n_left_to_next -= 1;
330
331           b0 = vlib_get_buffer (vm, bi0);
332           c0 = vnet_get_config_data (&cm->config_main,
333                                      &vnet_buffer (b0)->ip.current_config_index,
334                                      &next0, sizeof (c0[0]));
335
336           spd0 = pool_elt_at_index(im->spds, c0->spd_index);
337
338           ip0 = vlib_buffer_get_current (b0);
339           esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
340
341           if (PREDICT_TRUE(ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
342             {
343 #if 0
344               clib_warning("packet received from %U to %U spi %u size %u spd_id %u",
345                            format_ip6_address, &ip0->src_address,
346                            format_ip6_address, &ip0->dst_address,
347                            clib_net_to_host_u32(esp0->spi),
348                            clib_net_to_host_u16(ip0->payload_length) + header_size,
349                            spd0->id);
350 #endif
351               ipsec_policy_t * p0;
352               p0 = ipsec_input_ip6_protect_policy_match(spd0,
353                          &ip0->src_address,
354                          &ip0->dst_address,
355                          clib_net_to_host_u32(esp0->spi));
356
357               if (PREDICT_TRUE(p0 != 0))
358                 {
359                   p0->counter.packets++;
360                   p0->counter.bytes += clib_net_to_host_u16(ip0->payload_length);
361                   p0->counter.bytes += header_size;
362                   vnet_buffer(b0)->output_features.ipsec_sad_index = p0->sa_index;
363                   next0 = IPSEC_INPUT_NEXT_ESP_DECRYPT;
364                   vlib_buffer_advance(b0, header_size);
365                   goto trace0;
366                 }
367             }
368
369 trace0:
370           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) {
371             ipsec_input_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
372             tr->tunnel_index = tunnel_index0;
373             tr->spi = clib_host_to_net_u32(esp0->spi);
374             tr->seq = clib_host_to_net_u32(esp0->seq);
375           }
376
377           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
378                                            n_left_to_next, bi0, next0);
379         }
380       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
381     }
382   vlib_node_increment_counter (vm, ipsec_input_ip6_node.index,
383                                IPSEC_INPUT_ERROR_RX_PKTS,
384                                from_frame->n_vectors);
385
386   return from_frame->n_vectors;
387 }
388
389
390 VLIB_REGISTER_NODE (ipsec_input_ip6_node) = {
391   .function = ipsec_input_ip6_node_fn,
392   .name = "ipsec-input-ip6",
393   .vector_size = sizeof (u32),
394   .format_trace = format_ipsec_input_trace,
395   .type = VLIB_NODE_TYPE_INTERNAL,
396
397   .n_errors = ARRAY_LEN(ipsec_input_error_strings),
398   .error_strings = ipsec_input_error_strings,
399
400   .n_next_nodes = IPSEC_INPUT_N_NEXT,
401   .next_nodes = {
402 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
403     foreach_ipsec_input_next
404 #undef _
405   },
406 };