Initial commit of vpp code.
[vpp.git] / vnet / vnet / ipsec / ipsec_output.c
1 /*
2  * ipsec_output.c : IPSec output node
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
24
25 #define foreach_ipsec_output_next                \
26 _(DROP, "error-drop")                            \
27 _(ESP_ENCRYPT, "esp-encrypt")
28
29 #define _(v, s) IPSEC_OUTPUT_NEXT_##v,
30 typedef enum {
31   foreach_intf_output_feat
32   foreach_ipsec_output_next
33 #undef _
34   IPSEC_OUTPUT_N_NEXT,
35 } ipsec_output_next_t;
36
37
38 #define foreach_ipsec_output_error                   \
39  _(RX_PKTS, "IPSec pkts received")                   \
40  _(POLICY_DISCARD, "IPSec policy discard")           \
41  _(POLICY_NO_MATCH, "IPSec policy (no match)")       \
42  _(POLICY_PROTECT, "IPSec policy protect")           \
43  _(POLICY_BYPASS, "IPSec policy bypass")             \
44  _(ENCAPS_FAILED, "IPSec encapsulation failed")
45
46
47 typedef enum {
48 #define _(sym,str) IPSEC_OUTPUT_ERROR_##sym,
49   foreach_ipsec_output_error
50 #undef _
51   IPSEC_DECAP_N_ERROR,
52 } ipsec_output_error_t;
53
54 static char * ipsec_output_error_strings[] = {
55 #define _(sym,string) string,
56   foreach_ipsec_output_error
57 #undef _
58 };
59
60 vlib_node_registration_t ipsec_output_node;
61
62 typedef struct {
63   u32 spd_id;
64 } ipsec_output_trace_t;
65
66 /* packet trace format function */
67 static u8 * format_ipsec_output_trace (u8 * s, va_list * args)
68 {
69   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
71   ipsec_output_trace_t * t = va_arg (*args, ipsec_output_trace_t *);
72
73   if (t->spd_id != ~0)
74   {
75     s = format (s, "spd %u ", t->spd_id);
76   }
77   else
78   {
79     s = format (s, "no spd");
80   }
81   return s;
82 }
83
84 always_inline intf_output_feat_t
85 get_next_intf_output_feature_and_reset_bit(vlib_buffer_t *b)
86 {
87   u32 next_feature;
88   count_trailing_zeros(next_feature, vnet_buffer(b)->output_features.bitmap);
89   if (next_feature != INTF_OUTPUT_FEAT_DONE)
90     vnet_buffer(b)->output_features.bitmap &= ~(1 << next_feature);
91   return next_feature;
92 }
93
94 always_inline ipsec_policy_t *
95 ipsec_output_policy_match(ipsec_spd_t * spd, u8 pr, u32 la, u32 ra, u16 lp, u16 rp)
96 {
97   ipsec_policy_t * p;
98   u32 * i;
99
100   vec_foreach(i, spd->ipv4_outbound_policies)
101     {
102       p = pool_elt_at_index(spd->policies, *i);
103       if (PREDICT_FALSE(p->protocol && (p->protocol != pr)))
104         continue;
105
106       if (la < clib_net_to_host_u32(p->laddr.start.ip4.as_u32))
107         continue;
108
109       if (la > clib_net_to_host_u32(p->laddr.stop.ip4.as_u32))
110         continue;
111
112       if (ra < clib_net_to_host_u32(p->raddr.start.ip4.as_u32))
113         continue;
114
115       if (ra > clib_net_to_host_u32(p->raddr.stop.ip4.as_u32))
116         continue;
117
118       if (PREDICT_FALSE((pr != IP_PROTOCOL_TCP) && (pr != IP_PROTOCOL_UDP)))
119         return p;
120
121       if (lp < p->lport.start)
122         continue;
123
124       if (lp > p->lport.stop)
125         continue;
126
127       if (rp < p->rport.start)
128         continue;
129
130       if (rp > p->rport.stop)
131         continue;
132
133       return p;
134     }
135     return 0;
136 }
137
138 always_inline uword
139 ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la, ip6_address_t * ua)
140 {
141   if ((memcmp(a->as_u64, la->as_u64, 2 * sizeof(u64)) >= 0) &&
142       (memcmp(a->as_u64, ua->as_u64, 2 * sizeof(u64)) <= 0))
143     return 1;
144   return 0;
145 }
146
147 always_inline ipsec_policy_t *
148 ipsec_output_ip6_policy_match (ipsec_spd_t * spd,
149                                ip6_address_t * sa,
150                                ip6_address_t * da,
151                                u16 lp,
152                                u16 rp,
153                                u8 pr)
154 {
155   ipsec_policy_t * p;
156   u32 * i;
157
158   vec_foreach(i, spd->ipv6_outbound_policies)
159     {
160       p = pool_elt_at_index(spd->policies, *i);
161       if (PREDICT_FALSE(p->protocol && (p->protocol != pr)))
162         continue;
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       if (PREDICT_FALSE((pr != IP_PROTOCOL_TCP) && (pr != IP_PROTOCOL_UDP)))
171         return p;
172
173       if (lp < p->lport.start)
174         continue;
175
176       if (lp > p->lport.stop)
177         continue;
178
179       if (rp < p->rport.start)
180         continue;
181
182       if (rp > p->rport.stop)
183         continue;
184
185      return p;
186     }
187
188   return 0;
189 }
190 static uword
191 ipsec_output_node_fn (vlib_main_t * vm,
192          vlib_node_runtime_t * node,
193          vlib_frame_t * from_frame)
194 {
195   ipsec_main_t *im = &ipsec_main;
196   vnet_main_t * vnm = im->vnet_main;
197
198   u32 * from, * to_next = 0;
199   u32 n_left_from, sw_if_index0, last_sw_if_index = (u32) ~0;
200   u32 next_node_index = (u32)~0, last_next_node_index = (u32) ~0;
201   vlib_frame_t *f = 0;
202   u32 spd_index0 = ~0;
203   ipsec_spd_t * spd0 = 0;
204   u64 nc_protect = 0, nc_bypass = 0, nc_discard = 0, nc_nomatch = 0;
205
206   from = vlib_frame_vector_args (from_frame);
207   n_left_from = from_frame->n_vectors;
208
209   while (n_left_from > 0)
210     {
211       u32 bi0;
212       vlib_buffer_t * b0;
213       ipsec_policy_t * p0;
214       ip4_header_t * ip0;
215       ip6_header_t * ip6_0 = 0;
216       udp_header_t * udp0;
217       u8 is_ipv6 = 0;
218
219       bi0 = from[0];
220       b0 = vlib_get_buffer (vm, bi0);
221       sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
222
223
224       ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
225                               sizeof(ethernet_header_t));
226
227       /* just forward non ipv4 packets */
228       if (PREDICT_FALSE((ip0->ip_version_and_header_length & 0xF0 ) != 0x40))
229       {
230         /* ipv6 packets */
231         if (PREDICT_TRUE((ip0->ip_version_and_header_length & 0xF0 ) == 0x60))
232           {
233             is_ipv6 = 1;
234             ip6_0 = (ip6_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
235                                       sizeof(ethernet_header_t));
236           }
237         else
238           {
239             next_node_index = get_next_output_feature_node_index(vnm, b0);
240             goto dispatch0;
241           }
242       }
243
244       /* lookup for SPD only if sw_if_index is changed */
245       if (PREDICT_FALSE(last_sw_if_index != sw_if_index0))
246         {
247            uword * p = hash_get (im->spd_index_by_sw_if_index, sw_if_index0);
248            ASSERT(p);
249            spd_index0 = p[0];
250            spd0 = pool_elt_at_index(im->spds, spd_index0);
251            last_sw_if_index = sw_if_index0;
252         }
253
254      if (is_ipv6)
255         {
256           udp0 = ip6_next_header(ip6_0);
257 #if 0
258           clib_warning("packet received from %U port %u to %U port %u spd_id %u",
259                        format_ip6_address, &ip6_0->src_address,
260                        clib_net_to_host_u16(udp0->src_port),
261                        format_ip6_address, &ip6_0->dst_address,
262                        clib_net_to_host_u16(udp0->dst_port),
263                        spd0->id);
264 #endif
265
266           p0 = ipsec_output_ip6_policy_match(spd0,
267                      &ip6_0->src_address,
268                      &ip6_0->dst_address,
269                      clib_net_to_host_u16(udp0->src_port),
270                      clib_net_to_host_u16(udp0->dst_port),
271                      ip6_0->protocol);
272         }
273       else
274         {
275           udp0 = (udp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
276
277 #if 0
278           clib_warning("packet received from %U to %U port %u",
279                        format_ip4_address, ip0->src_address.as_u8,
280                        format_ip4_address, ip0->dst_address.as_u8,
281                        clib_net_to_host_u16(udp0->dst_port));
282           clib_warning("sw_if_index0 %u spd_index0 %u spd_id %u",
283                        sw_if_index0, spd_index0, spd0->id);
284 #endif
285
286           p0 = ipsec_output_policy_match(spd0, ip0->protocol,
287                      clib_net_to_host_u32(ip0->src_address.as_u32),
288                      clib_net_to_host_u32(ip0->dst_address.as_u32),
289                      clib_net_to_host_u16(udp0->src_port),
290                      clib_net_to_host_u16(udp0->dst_port));
291         }
292
293       if (PREDICT_TRUE(p0 != NULL))
294         {
295           if (p0->policy == IPSEC_POLICY_ACTION_PROTECT)
296             {
297               nc_protect++;
298               next_node_index = im->esp_encrypt_node_index;
299               vnet_buffer(b0)->output_features.ipsec_sad_index = p0->sa_index;
300               vlib_buffer_advance(b0, sizeof(ethernet_header_t));
301               p0->counter.packets++;
302               if (is_ipv6)
303                 {
304                   p0->counter.bytes += clib_net_to_host_u16(ip6_0->payload_length);
305                   p0->counter.bytes += sizeof(ip6_header_t);
306                 }
307               else
308                 {
309                   p0->counter.bytes += clib_net_to_host_u16(ip0->length);
310                 }
311             }
312           else if (p0->policy == IPSEC_POLICY_ACTION_BYPASS)
313             {
314               nc_bypass++;
315               next_node_index = get_next_output_feature_node_index(vnm, b0);
316               p0->counter.packets++;
317               if (is_ipv6)
318                 {
319                   p0->counter.bytes += clib_net_to_host_u16(ip6_0->payload_length);
320                   p0->counter.bytes += sizeof(ip6_header_t);
321                 }
322               else
323                 {
324                   p0->counter.bytes += clib_net_to_host_u16(ip0->length);
325                 }
326             }
327           else
328             {
329               nc_discard++;
330               p0->counter.packets++;
331               if (is_ipv6)
332                 {
333                   p0->counter.bytes += clib_net_to_host_u16(ip6_0->payload_length);
334                   p0->counter.bytes += sizeof(ip6_header_t);
335                 }
336               else
337                 {
338                   p0->counter.bytes += clib_net_to_host_u16(ip0->length);
339                 }
340               next_node_index = im->error_drop_node_index;
341             }
342         }
343       else
344         {
345           nc_nomatch++;
346           next_node_index = im->error_drop_node_index;
347         }
348
349 dispatch0:
350       from += 1;
351       n_left_from -= 1;
352
353       if (PREDICT_FALSE((last_next_node_index != next_node_index)))
354         {
355           /* if this is not 1st frame */
356           if (f)
357             vlib_put_frame_to_node (vm, last_next_node_index, f);
358
359           last_next_node_index = next_node_index;
360
361           f = vlib_get_frame_to_node(vm, next_node_index);
362           to_next = vlib_frame_vector_args (f);
363         }
364
365       to_next[0] = bi0;
366       to_next+=1;
367       f->n_vectors++;
368
369       if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) {
370             ipsec_output_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
371             if (spd0)
372               tr->spd_id = spd0->id;
373       }
374     }
375
376   vlib_put_frame_to_node (vm, next_node_index, f);
377   vlib_node_increment_counter (vm, ipsec_output_node.index,
378                                IPSEC_OUTPUT_ERROR_POLICY_PROTECT, nc_protect);
379   vlib_node_increment_counter (vm, ipsec_output_node.index,
380                                IPSEC_OUTPUT_ERROR_POLICY_BYPASS, nc_bypass);
381   vlib_node_increment_counter (vm, ipsec_output_node.index,
382                                IPSEC_OUTPUT_ERROR_POLICY_DISCARD, nc_discard);
383   vlib_node_increment_counter (vm, ipsec_output_node.index,
384                                IPSEC_OUTPUT_ERROR_POLICY_NO_MATCH, nc_nomatch);
385   return from_frame->n_vectors;
386 }
387
388 VLIB_REGISTER_NODE (ipsec_output_node) = {
389   .function = ipsec_output_node_fn,
390   .name = "ipsec-output",
391   .vector_size = sizeof (u32),
392   .format_trace = format_ipsec_output_trace,
393   .type = VLIB_NODE_TYPE_INTERNAL,
394
395   .n_errors = ARRAY_LEN(ipsec_output_error_strings),
396   .error_strings = ipsec_output_error_strings,
397
398   .n_next_nodes = IPSEC_OUTPUT_N_NEXT,
399   .next_nodes = {
400 #define _(s,n) [IPSEC_OUTPUT_NEXT_##s] = n,
401     foreach_intf_output_feat
402     foreach_ipsec_output_next
403 #undef _
404   },
405 };