socket_test.sh: Don't hard code debug image with gdb.
[vpp.git] / src / plugins / sixrd / ip4_sixrd.c
1 /*---------------------------------------------------------------------------
2  * Copyright (c) 2009-2014 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *---------------------------------------------------------------------------
15  */
16
17 #include "sixrd.h"
18
19 vlib_node_registration_t ip4_sixrd_node;
20
21 typedef enum {
22   IP4_SIXRD_NEXT_IP6_LOOKUP,
23   IP4_SIXRD_NEXT_DROP,
24   IP4_SIXRD_N_NEXT,
25 } ip4_sixrd_next_t;
26
27 typedef struct {
28   u32 tunnel_id;
29   u32 length;
30   ip4_address_t src;
31   ip4_address_t dst;
32 } sixrd_rx_trace_t;
33
34 u8 *
35 format_sixrd_rx_trace (u8 * s, va_list * args)
36 {
37   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
38   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
39   sixrd_rx_trace_t *t = va_arg (*args, sixrd_rx_trace_t *);
40
41   s = format (s, "6RD: tunnel %d len %d src %U dst %U",
42               t->tunnel_id, clib_net_to_host_u16 (t->length),
43               format_ip4_address, &t->src, format_ip4_address, &t->dst);
44   return s;
45 }
46
47 /*
48  * ip4_sixrd_sec_check
49  */
50 static_always_inline void
51 ip4_sixrd_sec_check(sixrd_tunnel_t *t, ip4_address_t sa4,
52                     ip6_address_t sa6, u8 *error) {
53   if (PREDICT_FALSE(sixrd_get_addr_net(t, sa6.as_u64[0]) != sa4.as_u32))
54     *error = SIXRD_ERROR_SEC_CHECK;
55 }
56
57 /*
58  * ip4_sixrd
59  */
60 uword ip4_sixrd(vlib_main_t *vm, vlib_node_runtime_t *node,
61                 vlib_frame_t *frame) {
62   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
63   vlib_node_runtime_t *error_node = vlib_node_get_runtime(vm, ip4_sixrd_node.index);
64   vnet_interface_main_t * im = &vnet_get_main()->interface_main;
65   u32 thread_index = vlib_get_thread_index ();
66
67   from = vlib_frame_vector_args(frame);
68   n_left_from = frame->n_vectors;
69   next_index = node->cached_next_index;
70   while (n_left_from > 0) {
71     vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
72
73     /* Single loop */
74     while (n_left_from > 0 && n_left_to_next > 0) {
75       u32 pi0;
76       vlib_buffer_t *p0;
77       u8 error0 = SIXRD_ERROR_NONE;
78       sixrd_tunnel_t *t0 = 0;
79       ip4_header_t *ip40;
80       ip6_header_t *ip60;
81       u32 tunnel_sw_if_index = ~0;
82       u32 next0 = IP4_SIXRD_NEXT_DROP;
83
84       pi0 = to_next[0] = from[0];
85       from += 1;
86       n_left_from -= 1;
87       to_next += 1;
88       n_left_to_next -= 1;
89
90       p0 = vlib_get_buffer(vm, pi0);
91       ip40 = vlib_buffer_get_current(p0);
92
93       /* Throw away anything that isn't IP in IP. */
94       if (PREDICT_TRUE(ip40->protocol == IP_PROTOCOL_IPV6 && clib_net_to_host_u16(ip40->length) >= 60)) {
95         vlib_buffer_advance(p0, sizeof(ip4_header_t));
96         t0 = ip4_sixrd_get_tunnel(vnet_buffer(p0)->ip.adj_index[VLIB_TX], (ip4_address_t *)&ip40->dst_address, &error0);
97       } else {
98         error0 = SIXRD_ERROR_BAD_PROTOCOL;
99       }
100
101       if (!t0) {
102         error0 = SIXRD_ERROR_NO_TUNNEL;
103         goto error;
104       }
105
106       tunnel_sw_if_index = t0->sw_if_index;
107
108       /* SIXRD inbound security check */
109       if (t0->security_check) {
110         ip60 = vlib_buffer_get_current(p0);
111         ip4_sixrd_sec_check(t0, ip40->src_address, ip60->src_address, &error0);
112       }
113       next0 = error0 == SIXRD_ERROR_NONE ? IP4_SIXRD_NEXT_IP6_LOOKUP
114                                          : IP4_SIXRD_NEXT_DROP;
115
116       if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) {
117         sixrd_rx_trace_t *tr = vlib_add_trace(vm, node, p0, sizeof(*tr));
118         tr->tunnel_id = tunnel_sw_if_index;
119         tr->length = ip40->length;
120         tr->src.as_u32 = ip40->src_address.as_u32;
121         tr->dst.as_u32 = ip40->dst_address.as_u32;
122       }
123
124       error:
125       if (PREDICT_TRUE(error0 == SIXRD_ERROR_NONE)) {
126         u32 len = vlib_buffer_length_in_chain (vm, p0);
127         vlib_increment_combined_counter (im->combined_sw_if_counters
128                                          + VNET_INTERFACE_COUNTER_RX,
129                                          thread_index,
130                                          tunnel_sw_if_index,
131                                          1 /* packets */ ,
132                                          len /* bytes */ );
133
134         vnet_buffer (p0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
135       } else {
136         p0->error = error_node->errors[error0];
137       }
138       vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
139                                       n_left_to_next, pi0, next0);
140     }
141     vlib_put_next_frame(vm, node, next_index, n_left_to_next);
142   }
143   return frame->n_vectors;
144 }
145
146 static char *sixrd_error_strings[] = {
147 #define _(sym, string) string,
148     foreach_sixrd_error
149 #undef _
150 };
151
152 VLIB_REGISTER_NODE(ip4_sixrd_node) = {
153   .function = ip4_sixrd,
154   .name = "ip4-sixrd",
155   .vector_size = sizeof(u32),
156   .format_trace = format_sixrd_rx_trace,
157   .n_errors = SIXRD_N_ERROR,
158   .error_strings = sixrd_error_strings,
159   .n_next_nodes = IP4_SIXRD_N_NEXT,
160   .next_nodes =
161   {
162     [IP4_SIXRD_NEXT_IP6_LOOKUP] = "ip6-lookup",
163     [IP4_SIXRD_NEXT_DROP] = "error-drop",
164   },
165 };