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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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 *---------------------------------------------------------------------------
19 vlib_node_registration_t ip4_sixrd_node;
22 IP4_SIXRD_NEXT_IP6_LOOKUP,
35 format_sixrd_rx_trace (u8 * s, va_list * args)
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 *);
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);
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;
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 ();
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);
74 while (n_left_from > 0 && n_left_to_next > 0) {
77 u8 error0 = SIXRD_ERROR_NONE;
78 sixrd_tunnel_t *t0 = 0;
81 u32 tunnel_sw_if_index = ~0;
82 u32 next0 = IP4_SIXRD_NEXT_DROP;
84 pi0 = to_next[0] = from[0];
90 p0 = vlib_get_buffer(vm, pi0);
91 ip40 = vlib_buffer_get_current(p0);
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);
98 error0 = SIXRD_ERROR_BAD_PROTOCOL;
102 error0 = SIXRD_ERROR_NO_TUNNEL;
106 tunnel_sw_if_index = t0->sw_if_index;
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);
113 next0 = error0 == SIXRD_ERROR_NONE ? IP4_SIXRD_NEXT_IP6_LOOKUP
114 : IP4_SIXRD_NEXT_DROP;
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;
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,
134 vnet_buffer (p0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
136 p0->error = error_node->errors[error0];
138 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
139 n_left_to_next, pi0, next0);
141 vlib_put_next_frame(vm, node, next_index, n_left_to_next);
143 return frame->n_vectors;
146 static char *sixrd_error_strings[] = {
147 #define _(sym, string) string,
152 VLIB_REGISTER_NODE(ip4_sixrd_node) = {
153 .function = 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,
162 [IP4_SIXRD_NEXT_IP6_LOOKUP] = "ip6-lookup",
163 [IP4_SIXRD_NEXT_DROP] = "error-drop",