blob: 0f47f2636dbf6688550375f91eb6a445712b12c2 [file] [log] [blame]
Ole Troan298c6952018-03-08 12:30:43 +01001/*
2 * node.c: ipip packet processing
3 *
4 * Copyright (c) 2018 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 aipiped 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 <vlib/vlib.h>
19#include <vnet/ipip/ipip.h>
20#include <vnet/ip/ip6_packet.h>
21#include <vnet/mpls/mpls.h>
22#include <vnet/pg/pg.h>
23#include <vppinfra/sparse_vec.h>
24
25#define foreach_ipip_input_next \
26 _(PUNT, "error-punt") \
27 _(DROP, "error-drop") \
28 _(IP4_INPUT, "ip4-input") \
29 _(IP6_INPUT, "ip6-input")
30
31typedef enum
32{
33#define _(s, n) IPIP_INPUT_NEXT_##s,
34 foreach_ipip_input_next
35#undef _
36 IPIP_INPUT_N_NEXT,
37} ipip_input_next_t;
38
39typedef struct
40{
41 u32 tunnel_id;
42 u32 length;
43 ip46_address_t src;
44 ip46_address_t dst;
45 u8 is_ipv6;
46} ipip_rx_trace_t;
47
Filip Tehlar7a542f42019-03-05 04:50:23 -080048static u8 *
Ole Troan298c6952018-03-08 12:30:43 +010049format_ipip_rx_trace (u8 * s, va_list * args)
50{
51 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53 ipip_rx_trace_t *t = va_arg (*args, ipip_rx_trace_t *);
54
55 s = format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id,
56 clib_net_to_host_u16 (t->length), format_ip46_address, &t->src,
57 IP46_TYPE_ANY, format_ip46_address, &t->dst, IP46_TYPE_ANY);
58 return s;
59}
60
61always_inline uword
62ipip_input (vlib_main_t * vm, vlib_node_runtime_t * node,
63 vlib_frame_t * from_frame, bool is_ipv6)
64{
65 ipip_main_t *gm = &ipip_main;
66 u32 n_left_from, next_index, *from, *to_next, n_left_to_next;
67 u32 tunnel_sw_if_index = ~0;
Damjan Marion067cd622018-07-11 12:47:43 +020068 u32 thread_index = vm->thread_index;
Ole Troan298c6952018-03-08 12:30:43 +010069 u32 len;
70 vnet_interface_main_t *im = &gm->vnet_main->interface_main;
71
72 from = vlib_frame_vector_args (from_frame);
73 n_left_from = from_frame->n_vectors;
74 next_index = node->cached_next_index;
75 while (n_left_from > 0)
76 {
77 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
78
79 while (n_left_from > 0 && n_left_to_next > 0)
80 {
81 u32 bi0;
82 vlib_buffer_t *b0;
83 ip4_header_t *ip40;
84 ip6_header_t *ip60;
85 u32 next0 = IPIP_INPUT_NEXT_DROP;
Ole Troan298c6952018-03-08 12:30:43 +010086 u8 inner_protocol0;
87
88 bi0 = to_next[0] = from[0];
89 from += 1;
90 n_left_from -= 1;
91 to_next += 1;
92 n_left_to_next -= 1;
93
94 b0 = vlib_get_buffer (vm, bi0);
95
Neale Ranns14053c92019-12-29 23:55:18 +000096 ipip_tunnel_key_t key0 = {
97 .fib_index = vnet_buffer (b0)->ip.fib_index,
98 .mode = IPIP_MODE_P2P,
99 };
100
Ole Troan298c6952018-03-08 12:30:43 +0100101 if (is_ipv6)
102 {
103 ip60 = vlib_buffer_get_current (b0);
Ole Troan282093f2018-09-19 12:38:51 +0200104 /* Check for outer fragmentation */
105 if (ip60->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
106 {
107 next0 = IPIP_INPUT_NEXT_DROP;
108 b0->error = node->errors[IPIP_ERROR_FRAGMENTED_PACKET];
109 goto drop;
110 }
111
Ole Troan298c6952018-03-08 12:30:43 +0100112 vlib_buffer_advance (b0, sizeof (*ip60));
Neale Ranns14053c92019-12-29 23:55:18 +0000113 ip_set (&key0.dst, &ip60->src_address, false);
114 ip_set (&key0.src, &ip60->dst_address, false);
Ole Troan298c6952018-03-08 12:30:43 +0100115 inner_protocol0 = ip60->protocol;
Neale Ranns14053c92019-12-29 23:55:18 +0000116 key0.transport = IPIP_TRANSPORT_IP6;
Ole Troan298c6952018-03-08 12:30:43 +0100117 }
118 else
119 {
120 ip40 = vlib_buffer_get_current (b0);
Ole Troan4146c652018-08-08 22:23:19 +0200121 /* Check for outer fragmentation */
122 if (ip40->flags_and_fragment_offset &
123 clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS))
124 {
125 next0 = IPIP_INPUT_NEXT_DROP;
126 b0->error = node->errors[IPIP_ERROR_FRAGMENTED_PACKET];
127 goto drop;
128 }
Ole Troan298c6952018-03-08 12:30:43 +0100129 vlib_buffer_advance (b0, sizeof (*ip40));
Neale Ranns14053c92019-12-29 23:55:18 +0000130 ip_set (&key0.dst, &ip40->src_address, true);
131 ip_set (&key0.src, &ip40->dst_address, true);
Ole Troan298c6952018-03-08 12:30:43 +0100132 inner_protocol0 = ip40->protocol;
Neale Ranns14053c92019-12-29 23:55:18 +0000133 key0.transport = IPIP_TRANSPORT_IP4;
Ole Troan298c6952018-03-08 12:30:43 +0100134 }
135
136 /*
137 * Find tunnel. First a lookup for P2P tunnels, then a lookup
138 * for multipoint tunnels
139 */
Ole Troan298c6952018-03-08 12:30:43 +0100140 ipip_tunnel_t *t0 = ipip_tunnel_db_find (&key0);
141 if (!t0)
142 {
143 ip46_address_reset (&key0.dst);
Neale Ranns14053c92019-12-29 23:55:18 +0000144 key0.mode = IPIP_MODE_6RD;
Ole Troan298c6952018-03-08 12:30:43 +0100145 t0 = ipip_tunnel_db_find (&key0);
146 if (!t0)
147 {
148 next0 = IPIP_INPUT_NEXT_DROP;
149 b0->error = node->errors[IPIP_ERROR_NO_TUNNEL];
150 goto drop;
151 }
152 }
153 tunnel_sw_if_index = t0->sw_if_index;
154
155 len = vlib_buffer_length_in_chain (vm, b0);
156 vnet_buffer (b0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
157
158 if (inner_protocol0 == IP_PROTOCOL_IPV6)
Neale Ranns95346962019-11-25 13:04:44 +0000159 {
160 next0 = IPIP_INPUT_NEXT_IP6_INPUT;
161
Neale Ranns59ff9182019-12-29 23:55:18 +0000162 if (t0->flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
Neale Ranns95346962019-11-25 13:04:44 +0000163 {
164 if (is_ipv6)
165 ip6_set_ecn_network_order ((ip60 + 1),
166 ip6_ecn_network_order (ip60));
167 else
168 ip6_set_ecn_network_order ((ip6_header_t *) (ip40 + 1),
169 ip4_header_get_ecn (ip40));
170 }
171 }
Ole Troan298c6952018-03-08 12:30:43 +0100172 else if (inner_protocol0 == IP_PROTOCOL_IP_IN_IP)
Neale Ranns95346962019-11-25 13:04:44 +0000173 {
174 next0 = IPIP_INPUT_NEXT_IP4_INPUT;
Neale Ranns59ff9182019-12-29 23:55:18 +0000175 if (t0->flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
Neale Ranns95346962019-11-25 13:04:44 +0000176 {
177 if (is_ipv6)
178 ip4_header_set_ecn_w_chksum ((ip4_header_t *) (ip60 + 1),
179 ip6_ecn_network_order
180 (ip60));
181 else
182 ip4_header_set_ecn_w_chksum (ip40 + 1,
183 ip4_header_get_ecn (ip40));
184 }
185 }
Ole Troan298c6952018-03-08 12:30:43 +0100186
187 if (!is_ipv6 && t0->mode == IPIP_MODE_6RD
188 && t0->sixrd.security_check)
189 {
190 ip6_header_t *inner_ip60 = vlib_buffer_get_current (b0);
191 if (sixrd_get_addr_net (t0, inner_ip60->src_address.as_u64[0])
192 != ip40->src_address.as_u32)
193 {
194 next0 = IPIP_INPUT_NEXT_DROP;
195 b0->error = node->errors[IPIP_ERROR_NO_TUNNEL];
196 goto drop;
197 }
198 }
199
200 vlib_increment_combined_counter (im->combined_sw_if_counters +
201 VNET_INTERFACE_COUNTER_RX,
202 thread_index, tunnel_sw_if_index,
203 1 /* packets */ ,
204 len /* bytes */ );
205
206 drop:
207 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
208 {
209 ipip_rx_trace_t *tr =
210 vlib_add_trace (vm, node, b0, sizeof (*tr));
211 tr->tunnel_id = tunnel_sw_if_index;
212 if (is_ipv6)
213 {
214 tr->length = ip60->payload_length;
215 tr->src.ip6.as_u64[0] = ip60->src_address.as_u64[0];
216 tr->src.ip6.as_u64[1] = ip60->src_address.as_u64[1];
217 tr->dst.ip6.as_u64[0] = ip60->dst_address.as_u64[0];
218 tr->dst.ip6.as_u64[1] = ip60->dst_address.as_u64[1];
219 }
220 else
221 {
222 tr->length = ip40->length;
223 tr->src.ip4.as_u32 = ip40->src_address.as_u32;
224 tr->dst.ip4.as_u32 = ip40->dst_address.as_u32;
225 }
226 }
227
228 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
229 n_left_to_next, bi0, next0);
230 }
231
232 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
233 }
234 vlib_node_increment_counter (vm,
235 !is_ipv6 ? ipip4_input_node.index :
236 ipip6_input_node.index, IPIP_ERROR_DECAP_PKTS,
237 from_frame->n_vectors);
238 return from_frame->n_vectors;
239}
240
Filip Tehlar7a542f42019-03-05 04:50:23 -0800241VLIB_NODE_FN (ipip4_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
242 vlib_frame_t * from_frame)
Ole Troan298c6952018-03-08 12:30:43 +0100243{
244 return ipip_input (vm, node, from_frame, /* is_ip6 */ false);
245}
246
Filip Tehlar7a542f42019-03-05 04:50:23 -0800247VLIB_NODE_FN (ipip6_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
248 vlib_frame_t * from_frame)
Ole Troan298c6952018-03-08 12:30:43 +0100249{
250 return ipip_input (vm, node, from_frame, /* is_ip6 */ true);
251}
252
253static char *ipip_error_strings[] = {
254#define _(sym,string) string,
255 foreach_ipip_error
256#undef _
257};
258
259/* *INDENT-OFF* */
260VLIB_REGISTER_NODE(ipip4_input_node) = {
Ole Troan298c6952018-03-08 12:30:43 +0100261 .name = "ipip4-input",
262 /* Takes a vector of packets. */
263 .vector_size = sizeof(u32),
264 .n_errors = IPIP_N_ERROR,
265 .error_strings = ipip_error_strings,
266 .n_next_nodes = IPIP_INPUT_N_NEXT,
267 .next_nodes =
268 {
269#define _(s, n) [IPIP_INPUT_NEXT_##s] = n,
270 foreach_ipip_input_next
271#undef _
272 },
273 .format_trace = format_ipip_rx_trace,
274};
275
276VLIB_REGISTER_NODE(ipip6_input_node) = {
Ole Troan298c6952018-03-08 12:30:43 +0100277 .name = "ipip6-input",
278 /* Takes a vector of packets. */
279 .vector_size = sizeof(u32),
280 .n_errors = IPIP_N_ERROR,
281 .error_strings = ipip_error_strings,
282 .n_next_nodes = IPIP_INPUT_N_NEXT,
283 .next_nodes =
284 {
285#define _(s, n) [IPIP_INPUT_NEXT_##s] = n,
286 foreach_ipip_input_next
287#undef _
288 },
289 .format_trace = format_ipip_rx_trace,
290};
291
Ole Troan298c6952018-03-08 12:30:43 +0100292/* *INDENT-ON* */
293
294/*
295 * fd.io coding-style-patch-verification: ON
296 *
297 * Local Variables:
298 * eval: (c-set-style "gnu")
299 * End:
300 */