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 *---------------------------------------------------------------------------
18 static vlib_node_registration_t ip4_sixrd_node;
21 IP4_SIXRD_NEXT_IP6_LOOKUP,
29 static_always_inline void
30 ip4_sixrd_sec_check (sixrd_domain_t *d, ip4_address_t sa4, ip6_address_t sa6, u8 *error)
32 u32 a = sixrd_get_addr(d, sa6.as_u64[0]);
33 clib_warning("Security check: %U %U", format_ip4_address, &a, format_ip4_address, &sa4);
34 if (PREDICT_FALSE(sixrd_get_addr(d, sa6.as_u64[0]) != sa4.as_u32))
35 *error = SIXRD_ERROR_SEC_CHECK;
42 ip4_sixrd (vlib_main_t *vm,
43 vlib_node_runtime_t *node,
46 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
47 vlib_node_runtime_t *error_node = vlib_node_get_runtime(vm, ip4_sixrd_node.index);
50 from = vlib_frame_vector_args(frame);
51 n_left_from = frame->n_vectors;
52 next_index = node->cached_next_index;
53 while (n_left_from > 0) {
54 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
57 while (n_left_from > 0 && n_left_to_next > 0) {
60 u8 error0 = SIXRD_ERROR_NONE;
61 sixrd_domain_t *d0 = 0;
64 u32 sixrd_domain_index0 = ~0;
67 pi0 = to_next[0] = from[0];
73 p0 = vlib_get_buffer(vm, pi0);
74 ip40 = vlib_buffer_get_current(p0);
76 /* Throw away anything that isn't IP in IP. */
77 if (PREDICT_TRUE(ip40->protocol == IP_PROTOCOL_IPV6 && clib_net_to_host_u16(ip40->length) >= 60)) {
78 vlib_buffer_advance(p0, sizeof(ip4_header_t));
79 ip60 = vlib_buffer_get_current(p0);
80 d0 = ip4_sixrd_get_domain(vnet_buffer(p0)->ip.adj_index[VLIB_TX], (ip6_address_t *)&ip60->src_address,
81 &sixrd_domain_index0, &error0);
83 error0 = SIXRD_ERROR_BAD_PROTOCOL;
86 /* SIXRD inbound security check */
87 ip4_sixrd_sec_check(d0, ip40->src_address, ip60->src_address, &error0);
90 next0 = error0 == SIXRD_ERROR_NONE ? IP4_SIXRD_NEXT_IP6_LOOKUP : IP4_SIXRD_NEXT_DROP;
92 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) {
93 sixrd_trace_t *tr = vlib_add_trace(vm, node, p0, sizeof(*tr));
94 tr->sixrd_domain_index = sixrd_domain_index0;
97 p0->error = error_node->errors[error0];
98 if (PREDICT_TRUE(error0 == SIXRD_ERROR_NONE)) decap++;
99 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, pi0, next0);
102 vlib_put_next_frame(vm, node, next_index, n_left_to_next);
104 vlib_node_increment_counter(vm, ip4_sixrd_node.index, SIXRD_ERROR_DECAPSULATED, decap);
106 return frame->n_vectors;
109 static char *sixrd_error_strings[] = {
110 #define _(sym,string) string,
115 VLIB_REGISTER_NODE(ip4_sixrd_node,static) = {
116 .function = ip4_sixrd,
118 .vector_size = sizeof(u32),
119 .format_trace = format_sixrd_trace,
120 .n_errors = SIXRD_N_ERROR,
121 .error_strings = sixrd_error_strings,
122 .n_next_nodes = IP4_SIXRD_N_NEXT,
124 [IP4_SIXRD_NEXT_IP6_LOOKUP] = "ip6-lookup",
125 [IP4_SIXRD_NEXT_DROP] = "error-drop",