68edc0fa9189827365b92370e6750d419a8b78f1
[vpp.git] / vnet / vnet / ip / ip4_input.c
1 /*
2  * Copyright (c) 2015 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  * ip/ip4_input.c: IP v4 input node
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vnet/ip/ip.h>
41 #include <vnet/ethernet/ethernet.h>
42 #include <vnet/ppp/ppp.h>
43 #include <vnet/hdlc/hdlc.h>
44
45 typedef struct {
46   u8 packet_data[64];
47 } ip4_input_trace_t;
48
49 static u8 * format_ip4_input_trace (u8 * s, va_list * va)
50 {
51   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
52   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
53   ip4_input_trace_t * t = va_arg (*va, ip4_input_trace_t *);
54
55   s = format (s, "%U",
56               format_ip4_header,
57               t->packet_data, sizeof (t->packet_data));
58
59   return s;
60 }
61
62 typedef enum {
63   IP4_INPUT_NEXT_DROP,
64   IP4_INPUT_NEXT_PUNT,
65   IP4_INPUT_NEXT_LOOKUP,
66   IP4_INPUT_NEXT_LOOKUP_MULTICAST,
67   IP4_INPUT_NEXT_TTL_EXPIRE,
68   IP4_INPUT_N_NEXT,
69 } ip4_input_next_t;
70
71 /* Validate IP v4 packets and pass them either to forwarding code
72    or drop/punt exception packets. */
73 always_inline uword
74 ip4_input_inline (vlib_main_t * vm,
75                   vlib_node_runtime_t * node,
76                   vlib_frame_t * frame,
77                   int verify_checksum)
78 {
79   ip4_main_t * im = &ip4_main;
80   vnet_main_t * vnm = vnet_get_main();
81   ip_lookup_main_t * lm = &im->lookup_main;
82   u32 n_left_from, * from, * to_next;
83   ip4_input_next_t next_index;
84   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
85   vlib_simple_counter_main_t * cm;
86   u32 cpu_index = os_get_cpu_number();
87
88   from = vlib_frame_vector_args (frame);
89   n_left_from = frame->n_vectors;
90   next_index = node->cached_next_index;
91
92   if (node->flags & VLIB_NODE_FLAG_TRACE)
93     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
94                                    /* stride */ 1,
95                                    sizeof (ip4_input_trace_t));
96
97   cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
98                          VNET_INTERFACE_COUNTER_IP4);
99
100   while (n_left_from > 0)
101     {
102       u32 n_left_to_next;
103
104       vlib_get_next_frame (vm, node, next_index,
105                            to_next, n_left_to_next);
106
107       while (n_left_from >= 4 && n_left_to_next >= 2)
108         {
109           vlib_buffer_t * p0, * p1;
110           ip4_header_t * ip0, * ip1;
111           ip_config_main_t * cm0, * cm1;
112           u32 sw_if_index0, pi0, ip_len0, cur_len0, next0;
113           u32 sw_if_index1, pi1, ip_len1, cur_len1, next1;
114           i32 len_diff0, len_diff1;
115           u8 error0, error1, cast0, cast1;
116
117           /* Prefetch next iteration. */
118           {
119             vlib_buffer_t * p2, * p3;
120
121             p2 = vlib_get_buffer (vm, from[2]);
122             p3 = vlib_get_buffer (vm, from[3]);
123
124             vlib_prefetch_buffer_header (p2, LOAD);
125             vlib_prefetch_buffer_header (p3, LOAD);
126
127             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
128             CLIB_PREFETCH (p3->data, sizeof (ip1[0]), LOAD);
129           }
130
131           to_next[0] = pi0 = from[0];
132           to_next[1] = pi1 = from[1];
133           from += 2;
134           to_next += 2;
135           n_left_from -= 2;
136           n_left_to_next -= 2;
137
138           p0 = vlib_get_buffer (vm, pi0);
139           p1 = vlib_get_buffer (vm, pi1);
140
141           ip0 = vlib_buffer_get_current (p0);
142           ip1 = vlib_buffer_get_current (p1);
143
144           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
145           sw_if_index1 = vnet_buffer (p1)->sw_if_index[VLIB_RX];
146
147           cast0 = ip4_address_is_multicast (&ip0->dst_address) ? VNET_MULTICAST : VNET_UNICAST;
148           cast1 = ip4_address_is_multicast (&ip1->dst_address) ? VNET_MULTICAST : VNET_UNICAST;
149
150           cm0 = lm->rx_config_mains + cast0;
151           cm1 = lm->rx_config_mains + cast1;
152
153           vnet_buffer (p0)->ip.current_config_index = vec_elt (cm0->config_index_by_sw_if_index, sw_if_index0);
154           vnet_buffer (p1)->ip.current_config_index = vec_elt (cm1->config_index_by_sw_if_index, sw_if_index1);
155
156           vnet_buffer (p0)->ip.adj_index[VLIB_RX] = ~0;
157           vnet_buffer (p1)->ip.adj_index[VLIB_RX] = ~0;
158
159           vnet_get_config_data (&cm0->config_main,
160                                 &vnet_buffer (p0)->ip.current_config_index,
161                                 &next0,
162                                 /* # bytes of config data */ 0);
163           vnet_get_config_data (&cm1->config_main,
164                                 &vnet_buffer (p1)->ip.current_config_index,
165                                 &next1,
166                                 /* # bytes of config data */ 0);
167
168           vlib_increment_simple_counter (cm, cpu_index, sw_if_index0, 1);
169           vlib_increment_simple_counter (cm, cpu_index, sw_if_index1, 1);
170
171           error0 = error1 = IP4_ERROR_NONE;
172
173           /* Punt packets with options. */
174           error0 = (ip0->ip_version_and_header_length & 0xf) != 5 ? IP4_ERROR_OPTIONS : error0;
175           error1 = (ip1->ip_version_and_header_length & 0xf) != 5 ? IP4_ERROR_OPTIONS : error1;
176
177           /* Version != 4?  Drop it. */
178           error0 = (ip0->ip_version_and_header_length >> 4) != 4 ? IP4_ERROR_VERSION : error0;
179           error1 = (ip1->ip_version_and_header_length >> 4) != 4 ? IP4_ERROR_VERSION : error1;
180
181           /* Verify header checksum. */
182           if (verify_checksum)
183             {
184               ip_csum_t sum0, sum1;
185
186               ip4_partial_header_checksum_x1 (ip0, sum0);
187               ip4_partial_header_checksum_x1 (ip1, sum1);
188
189               error0 = 0xffff != ip_csum_fold (sum0) ? IP4_ERROR_BAD_CHECKSUM : error0;
190               error1 = 0xffff != ip_csum_fold (sum1) ? IP4_ERROR_BAD_CHECKSUM : error1;
191             }
192
193           /* Drop fragmentation offset 1 packets. */
194           error0 = ip4_get_fragment_offset (ip0) == 1 ? IP4_ERROR_FRAGMENT_OFFSET_ONE : error0;
195           error1 = ip4_get_fragment_offset (ip1) == 1 ? IP4_ERROR_FRAGMENT_OFFSET_ONE : error1;
196
197           /* TTL <= 1? Drop it. */
198           error0 = (ip0->ttl <= 1 && cast0 == VNET_UNICAST) ? IP4_ERROR_TIME_EXPIRED : error0;
199           error1 = (ip1->ttl <= 1 && cast1 == VNET_UNICAST) ? IP4_ERROR_TIME_EXPIRED : error1;
200
201           /* Verify lengths. */
202           ip_len0 = clib_net_to_host_u16 (ip0->length);
203           ip_len1 = clib_net_to_host_u16 (ip1->length);
204
205           /* IP length must be at least minimal IP header. */
206           error0 = ip_len0 < sizeof (ip0[0]) ? IP4_ERROR_TOO_SHORT : error0;
207           error1 = ip_len1 < sizeof (ip1[0]) ? IP4_ERROR_TOO_SHORT : error1;
208
209           cur_len0 = vlib_buffer_length_in_chain (vm, p0);
210           cur_len1 = vlib_buffer_length_in_chain (vm, p1);
211
212           len_diff0 = cur_len0 - ip_len0;
213           len_diff1 = cur_len1 - ip_len1;
214
215           error0 = len_diff0 < 0 ? IP4_ERROR_BAD_LENGTH : error0;
216           error1 = len_diff1 < 0 ? IP4_ERROR_BAD_LENGTH : error1;
217
218           p0->error = error_node->errors[error0];
219           p1->error = error_node->errors[error1];
220
221       if (PREDICT_FALSE(error0 != IP4_ERROR_NONE))
222         {
223           next0 = (error0 != IP4_ERROR_OPTIONS
224                    ? (error0 == IP4_ERROR_TIME_EXPIRED
225                       ? IP4_INPUT_NEXT_TTL_EXPIRE
226                       : IP4_INPUT_NEXT_DROP)
227                    : IP4_INPUT_NEXT_PUNT);
228         }
229       if (PREDICT_FALSE(error1 != IP4_ERROR_NONE))
230         {
231           next1 = (error1 != IP4_ERROR_OPTIONS
232                    ? (error1 == IP4_ERROR_TIME_EXPIRED
233                       ? IP4_INPUT_NEXT_TTL_EXPIRE
234                       : IP4_INPUT_NEXT_DROP)
235                    : IP4_INPUT_NEXT_PUNT);
236         }
237
238           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
239                                            to_next, n_left_to_next,
240                                            pi0, pi1, next0, next1);
241         }    
242       while (n_left_from > 0 && n_left_to_next > 0)
243         {
244           vlib_buffer_t * p0;
245           ip4_header_t * ip0;
246           ip_config_main_t * cm0;
247           u32 sw_if_index0, pi0, ip_len0, cur_len0, next0;
248           i32 len_diff0;
249           u8 error0, cast0;
250
251           pi0 = from[0];
252           to_next[0] = pi0;
253           from += 1;
254           to_next += 1;
255           n_left_from -= 1;
256           n_left_to_next -= 1;
257
258           p0 = vlib_get_buffer (vm, pi0);
259           ip0 = vlib_buffer_get_current (p0);
260
261           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
262
263           cast0 = ip4_address_is_multicast (&ip0->dst_address) ? VNET_MULTICAST : VNET_UNICAST;
264           cm0 = lm->rx_config_mains + cast0;
265           vnet_buffer (p0)->ip.current_config_index = vec_elt (cm0->config_index_by_sw_if_index, sw_if_index0);
266           vnet_buffer (p0)->ip.adj_index[VLIB_RX] = ~0;
267           vnet_get_config_data (&cm0->config_main,
268                                 &vnet_buffer (p0)->ip.current_config_index,
269                                 &next0,
270                                 /* # bytes of config data */ 0);
271
272           vlib_increment_simple_counter (cm, cpu_index, sw_if_index0, 1);
273
274           error0 = IP4_ERROR_NONE;
275
276           /* Punt packets with options. */
277           error0 = (ip0->ip_version_and_header_length & 0xf) != 5 ? IP4_ERROR_OPTIONS : error0;
278
279           /* Version != 4?  Drop it. */
280           error0 = (ip0->ip_version_and_header_length >> 4) != 4 ? IP4_ERROR_VERSION : error0;
281
282           /* Verify header checksum. */
283           if (verify_checksum)
284             {
285               ip_csum_t sum0;
286
287               ip4_partial_header_checksum_x1 (ip0, sum0);
288               error0 = 0xffff != ip_csum_fold (sum0) ? IP4_ERROR_BAD_CHECKSUM : error0;
289             }
290
291           /* Drop fragmentation offset 1 packets. */
292           error0 = ip4_get_fragment_offset (ip0) == 1 ? IP4_ERROR_FRAGMENT_OFFSET_ONE : error0;
293
294           /* TTL <= 1? Drop it. */
295           error0 = (ip0->ttl <= 1 && cast0 == VNET_UNICAST) ? IP4_ERROR_TIME_EXPIRED : error0;
296
297           /* Verify lengths. */
298           ip_len0 = clib_net_to_host_u16 (ip0->length);
299
300           /* IP length must be at least minimal IP header. */
301           error0 = ip_len0 < sizeof (ip0[0]) ? IP4_ERROR_TOO_SHORT : error0;
302
303           cur_len0 = vlib_buffer_length_in_chain (vm, p0);
304           len_diff0 = cur_len0 - ip_len0;
305           error0 = len_diff0 < 0 ? IP4_ERROR_BAD_LENGTH : error0;
306
307           p0->error = error_node->errors[error0];
308       if (PREDICT_FALSE(error0 != IP4_ERROR_NONE))
309         {
310           next0 = (error0 != IP4_ERROR_OPTIONS
311                    ? (error0 == IP4_ERROR_TIME_EXPIRED
312                       ? IP4_INPUT_NEXT_TTL_EXPIRE
313                       : IP4_INPUT_NEXT_DROP)
314                    : IP4_INPUT_NEXT_PUNT);
315         }
316
317           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
318                                            to_next, n_left_to_next,
319                                            pi0, next0);
320         }
321
322       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
323     }
324
325   return frame->n_vectors;
326 }
327
328 static uword
329 ip4_input (vlib_main_t * vm,
330            vlib_node_runtime_t * node,
331            vlib_frame_t * frame)
332 {
333   return ip4_input_inline (vm, node, frame, /* verify_checksum */ 1);
334 }
335
336 static uword
337 ip4_input_no_checksum (vlib_main_t * vm,
338                        vlib_node_runtime_t * node,
339                        vlib_frame_t * frame)
340 {
341   return ip4_input_inline (vm, node, frame, /* verify_checksum */ 0);
342 }
343
344 static char * ip4_error_strings[] = {
345 #define _(sym,string) string,
346   foreach_ip4_error
347 #undef _
348 };
349
350 VLIB_REGISTER_NODE (ip4_input_node) = {
351   .function = ip4_input,
352   .name = "ip4-input",
353   .vector_size = sizeof (u32),
354
355   .n_errors = IP4_N_ERROR,
356   .error_strings = ip4_error_strings,
357
358   .n_next_nodes = IP4_INPUT_N_NEXT,
359   .next_nodes = {
360     [IP4_INPUT_NEXT_DROP] = "error-drop",
361     [IP4_INPUT_NEXT_PUNT] = "error-punt",
362     [IP4_INPUT_NEXT_LOOKUP] = "ip4-lookup",
363     [IP4_INPUT_NEXT_LOOKUP_MULTICAST] = "ip4-lookup-multicast",
364     [IP4_INPUT_NEXT_TTL_EXPIRE] = "ip4-icmp-ttl-expire",
365   },
366
367   .format_buffer = format_ip4_header,
368   .format_trace = format_ip4_input_trace,
369 };
370
371 VLIB_REGISTER_NODE (ip4_input_no_checksum_node,static) = {
372   .function = ip4_input_no_checksum,
373   .name = "ip4-input-no-checksum",
374   .vector_size = sizeof (u32),
375
376   .n_next_nodes = IP4_INPUT_N_NEXT,
377   .next_nodes = {
378     [IP4_INPUT_NEXT_DROP] = "error-drop",
379     [IP4_INPUT_NEXT_PUNT] = "error-punt",
380     [IP4_INPUT_NEXT_LOOKUP] = "ip4-lookup",
381     [IP4_INPUT_NEXT_LOOKUP_MULTICAST] = "ip4-lookup-multicast",
382     [IP4_INPUT_NEXT_TTL_EXPIRE] = "ip4-icmp-ttl-expire",
383   },
384
385   .format_buffer = format_ip4_header,
386   .format_trace = format_ip4_input_trace,
387 };
388
389 static clib_error_t * ip4_init (vlib_main_t * vm)
390 {
391   clib_error_t * error;
392
393   ethernet_register_input_type (vm, ETHERNET_TYPE_IP4,
394                                 ip4_input_node.index);
395   ppp_register_input_protocol (vm, PPP_PROTOCOL_ip4,
396                                ip4_input_node.index);
397   hdlc_register_input_protocol (vm, HDLC_PROTOCOL_ip4,
398                                 ip4_input_node.index);
399
400   {
401     pg_node_t * pn;
402     pn = pg_get_node (ip4_input_node.index);
403     pn->unformat_edit = unformat_pg_ip4_header;
404     pn = pg_get_node (ip4_input_no_checksum_node.index);
405     pn->unformat_edit = unformat_pg_ip4_header;
406   }
407
408   if ((error = vlib_call_init_function (vm, ip4_cli_init)))
409     return error;
410
411   if ((error = vlib_call_init_function (vm, ip4_source_check_init)))
412     return error;
413
414   /* Set flow hash to something non-zero. */
415   ip4_main.flow_hash_seed = 0xdeadbeef;
416
417   /* Default TTL for packets we generate. */
418   ip4_main.host_config.ttl = 64;
419
420   return error;
421 }
422
423 VLIB_INIT_FUNCTION (ip4_init);