889b423d7003f9ad1b60adc6e2316178e83145eb
[vpp.git] / src / vnet / ip / ip4_input.h
1 /*
2  * Copyright (c) 2017 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 #ifndef included_ip_input_h
41 #define included_ip_input_h
42
43 #include <vnet/ip/ip.h>
44 #include <vnet/ethernet/ethernet.h>
45
46 extern char *ip4_error_strings[];
47
48 typedef enum
49 {
50   IP4_INPUT_NEXT_DROP,
51   IP4_INPUT_NEXT_PUNT,
52   IP4_INPUT_NEXT_LOOKUP,
53   IP4_INPUT_NEXT_LOOKUP_MULTICAST,
54   IP4_INPUT_NEXT_ICMP_ERROR,
55   IP4_INPUT_NEXT_REASSEMBLY,
56   IP4_INPUT_N_NEXT,
57 } ip4_input_next_t;
58
59 static_always_inline void
60 check_ver_opt_csum (ip4_header_t * ip, u8 * error, int verify_checksum)
61 {
62   if (PREDICT_FALSE (ip->ip_version_and_header_length != 0x45))
63     {
64       if ((ip->ip_version_and_header_length & 0xf) != 5)
65         {
66           *error = IP4_ERROR_OPTIONS;
67           if (verify_checksum && ip_csum (ip, ip4_header_bytes (ip)) != 0)
68             *error = IP4_ERROR_BAD_CHECKSUM;
69         }
70       else
71         *error = IP4_ERROR_VERSION;
72     }
73   else
74     if (PREDICT_FALSE (verify_checksum &&
75                        ip_csum (ip, sizeof (ip4_header_t)) != 0))
76     *error = IP4_ERROR_BAD_CHECKSUM;
77 }
78
79 always_inline void
80 ip4_input_check_x4 (vlib_main_t * vm,
81                     vlib_node_runtime_t * error_node,
82                     vlib_buffer_t ** p, ip4_header_t ** ip,
83                     u16 * next, int verify_checksum)
84 {
85   u8 error0, error1, error2, error3;
86   u32 ip_len0, cur_len0;
87   u32 ip_len1, cur_len1;
88   u32 ip_len2, cur_len2;
89   u32 ip_len3, cur_len3;
90   i32 len_diff0, len_diff1, len_diff2, len_diff3;
91
92   error0 = error1 = error2 = error3 = IP4_ERROR_NONE;
93
94   check_ver_opt_csum (ip[0], &error0, verify_checksum);
95   check_ver_opt_csum (ip[1], &error1, verify_checksum);
96   check_ver_opt_csum (ip[2], &error2, verify_checksum);
97   check_ver_opt_csum (ip[3], &error3, verify_checksum);
98
99   if (PREDICT_FALSE (ip[0]->ttl < 1))
100     error0 = IP4_ERROR_TIME_EXPIRED;
101   if (PREDICT_FALSE (ip[1]->ttl < 1))
102     error1 = IP4_ERROR_TIME_EXPIRED;
103   if (PREDICT_FALSE (ip[2]->ttl < 1))
104     error2 = IP4_ERROR_TIME_EXPIRED;
105   if (PREDICT_FALSE (ip[3]->ttl < 1))
106     error3 = IP4_ERROR_TIME_EXPIRED;
107
108   /* Drop fragmentation offset 1 packets. */
109   error0 = ip4_get_fragment_offset (ip[0]) == 1 ?
110     IP4_ERROR_FRAGMENT_OFFSET_ONE : error0;
111   error1 = ip4_get_fragment_offset (ip[1]) == 1 ?
112     IP4_ERROR_FRAGMENT_OFFSET_ONE : error1;
113   error2 = ip4_get_fragment_offset (ip[2]) == 1 ?
114     IP4_ERROR_FRAGMENT_OFFSET_ONE : error2;
115   error3 = ip4_get_fragment_offset (ip[3]) == 1 ?
116     IP4_ERROR_FRAGMENT_OFFSET_ONE : error3;
117
118   /* Verify lengths. */
119   ip_len0 = clib_net_to_host_u16 (ip[0]->length);
120   ip_len1 = clib_net_to_host_u16 (ip[1]->length);
121   ip_len2 = clib_net_to_host_u16 (ip[2]->length);
122   ip_len3 = clib_net_to_host_u16 (ip[3]->length);
123
124   /* IP length must be at least minimal IP header. */
125   error0 = ip_len0 < sizeof (ip[0][0]) ? IP4_ERROR_TOO_SHORT : error0;
126   error1 = ip_len1 < sizeof (ip[1][0]) ? IP4_ERROR_TOO_SHORT : error1;
127   error2 = ip_len2 < sizeof (ip[2][0]) ? IP4_ERROR_TOO_SHORT : error2;
128   error3 = ip_len3 < sizeof (ip[3][0]) ? IP4_ERROR_TOO_SHORT : error3;
129
130   cur_len0 = vlib_buffer_length_in_chain (vm, p[0]);
131   cur_len1 = vlib_buffer_length_in_chain (vm, p[1]);
132   cur_len2 = vlib_buffer_length_in_chain (vm, p[2]);
133   cur_len3 = vlib_buffer_length_in_chain (vm, p[3]);
134
135   len_diff0 = cur_len0 - ip_len0;
136   len_diff1 = cur_len1 - ip_len1;
137   len_diff2 = cur_len2 - ip_len2;
138   len_diff3 = cur_len3 - ip_len3;
139
140   error0 = len_diff0 < 0 ? IP4_ERROR_BAD_LENGTH : error0;
141   error1 = len_diff1 < 0 ? IP4_ERROR_BAD_LENGTH : error1;
142   error2 = len_diff2 < 0 ? IP4_ERROR_BAD_LENGTH : error2;
143   error3 = len_diff3 < 0 ? IP4_ERROR_BAD_LENGTH : error3;
144
145   if (PREDICT_FALSE (error0 != IP4_ERROR_NONE))
146     {
147       if (error0 == IP4_ERROR_TIME_EXPIRED)
148         {
149           icmp4_error_set_vnet_buffer (p[0], ICMP4_time_exceeded,
150                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
151                                        0);
152           next[0] = IP4_INPUT_NEXT_ICMP_ERROR;
153         }
154       else
155         next[0] = error0 != IP4_ERROR_OPTIONS ?
156           IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
157       p[0]->error = error_node->errors[error0];
158     }
159   if (PREDICT_FALSE (error1 != IP4_ERROR_NONE))
160     {
161       if (error1 == IP4_ERROR_TIME_EXPIRED)
162         {
163           icmp4_error_set_vnet_buffer (p[1], ICMP4_time_exceeded,
164                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
165                                        0);
166           next[1] = IP4_INPUT_NEXT_ICMP_ERROR;
167         }
168       else
169         next[1] = error1 != IP4_ERROR_OPTIONS ?
170           IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
171       p[1]->error = error_node->errors[error1];
172     }
173   if (PREDICT_FALSE (error2 != IP4_ERROR_NONE))
174     {
175       if (error2 == IP4_ERROR_TIME_EXPIRED)
176         {
177           icmp4_error_set_vnet_buffer (p[2], ICMP4_time_exceeded,
178                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
179                                        0);
180           next[2] = IP4_INPUT_NEXT_ICMP_ERROR;
181         }
182       else
183         next[2] = error2 != IP4_ERROR_OPTIONS ?
184           IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
185       p[2]->error = error_node->errors[error2];
186     }
187   if (PREDICT_FALSE (error3 != IP4_ERROR_NONE))
188     {
189       if (error3 == IP4_ERROR_TIME_EXPIRED)
190         {
191           icmp4_error_set_vnet_buffer (p[3], ICMP4_time_exceeded,
192                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
193                                        0);
194           next[3] = IP4_INPUT_NEXT_ICMP_ERROR;
195         }
196       else
197         next[3] = error3 != IP4_ERROR_OPTIONS ?
198           IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
199       p[3]->error = error_node->errors[error3];
200     }
201 }
202
203 always_inline void
204 ip4_input_check_x2 (vlib_main_t * vm,
205                     vlib_node_runtime_t * error_node,
206                     vlib_buffer_t * p0, vlib_buffer_t * p1,
207                     ip4_header_t * ip0, ip4_header_t * ip1,
208                     u32 * next0, u32 * next1, int verify_checksum)
209 {
210   u8 error0, error1;
211   u32 ip_len0, cur_len0;
212   u32 ip_len1, cur_len1;
213   i32 len_diff0, len_diff1;
214
215   error0 = error1 = IP4_ERROR_NONE;
216
217   check_ver_opt_csum (ip0, &error0, verify_checksum);
218   check_ver_opt_csum (ip1, &error1, verify_checksum);
219
220   if (PREDICT_FALSE (ip0->ttl < 1))
221     error0 = IP4_ERROR_TIME_EXPIRED;
222   if (PREDICT_FALSE (ip1->ttl < 1))
223     error1 = IP4_ERROR_TIME_EXPIRED;
224
225   /* Drop fragmentation offset 1 packets. */
226   error0 = ip4_get_fragment_offset (ip0) == 1 ?
227     IP4_ERROR_FRAGMENT_OFFSET_ONE : error0;
228   error1 = ip4_get_fragment_offset (ip1) == 1 ?
229     IP4_ERROR_FRAGMENT_OFFSET_ONE : error1;
230
231   /* Verify lengths. */
232   ip_len0 = clib_net_to_host_u16 (ip0->length);
233   ip_len1 = clib_net_to_host_u16 (ip1->length);
234
235   /* IP length must be at least minimal IP header. */
236   error0 = ip_len0 < sizeof (ip0[0]) ? IP4_ERROR_TOO_SHORT : error0;
237   error1 = ip_len1 < sizeof (ip1[0]) ? IP4_ERROR_TOO_SHORT : error1;
238
239   cur_len0 = vlib_buffer_length_in_chain (vm, p0);
240   cur_len1 = vlib_buffer_length_in_chain (vm, p1);
241
242   len_diff0 = cur_len0 - ip_len0;
243   len_diff1 = cur_len1 - ip_len1;
244
245   error0 = len_diff0 < 0 ? IP4_ERROR_BAD_LENGTH : error0;
246   error1 = len_diff1 < 0 ? IP4_ERROR_BAD_LENGTH : error1;
247
248   if (PREDICT_FALSE (error0 != IP4_ERROR_NONE))
249     {
250       if (error0 == IP4_ERROR_TIME_EXPIRED)
251         {
252           icmp4_error_set_vnet_buffer (p0, ICMP4_time_exceeded,
253                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
254                                        0);
255           *next0 = IP4_INPUT_NEXT_ICMP_ERROR;
256         }
257       else
258         *next0 = error0 != IP4_ERROR_OPTIONS ?
259           IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
260       p0->error = error_node->errors[error0];
261     }
262   if (PREDICT_FALSE (error1 != IP4_ERROR_NONE))
263     {
264       if (error1 == IP4_ERROR_TIME_EXPIRED)
265         {
266           icmp4_error_set_vnet_buffer (p1, ICMP4_time_exceeded,
267                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
268                                        0);
269           *next1 = IP4_INPUT_NEXT_ICMP_ERROR;
270         }
271       else
272         *next1 = error1 != IP4_ERROR_OPTIONS ?
273           IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
274       p1->error = error_node->errors[error1];
275     }
276
277 }
278
279 always_inline void
280 ip4_input_check_x1 (vlib_main_t * vm,
281                     vlib_node_runtime_t * error_node,
282                     vlib_buffer_t * p0,
283                     ip4_header_t * ip0, u32 * next0, int verify_checksum)
284 {
285   u32 ip_len0, cur_len0;
286   i32 len_diff0;
287   u8 error0;
288
289   error0 = IP4_ERROR_NONE;
290
291   check_ver_opt_csum (ip0, &error0, verify_checksum);
292
293   /* Punt packets with options or wrong version. */
294   if (PREDICT_FALSE (ip0->ip_version_and_header_length != 0x45))
295     error0 = (ip0->ip_version_and_header_length & 0xf) != 5 ?
296       IP4_ERROR_OPTIONS : IP4_ERROR_VERSION;
297
298   /* Drop fragmentation offset 1 packets. */
299   error0 = ip4_get_fragment_offset (ip0) == 1 ?
300     IP4_ERROR_FRAGMENT_OFFSET_ONE : error0;
301
302   /* Verify lengths. */
303   ip_len0 = clib_net_to_host_u16 (ip0->length);
304
305   /* IP length must be at least minimal IP header. */
306   error0 = ip_len0 < sizeof (ip0[0]) ? IP4_ERROR_TOO_SHORT : error0;
307
308   cur_len0 = vlib_buffer_length_in_chain (vm, p0);
309
310   len_diff0 = cur_len0 - ip_len0;
311
312   error0 = len_diff0 < 0 ? IP4_ERROR_BAD_LENGTH : error0;
313
314   if (PREDICT_FALSE (error0 != IP4_ERROR_NONE))
315     {
316       if (error0 == IP4_ERROR_TIME_EXPIRED)
317         {
318           icmp4_error_set_vnet_buffer (p0, ICMP4_time_exceeded,
319                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
320                                        0);
321           *next0 = IP4_INPUT_NEXT_ICMP_ERROR;
322         }
323       else
324         *next0 = error0 != IP4_ERROR_OPTIONS ?
325           IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
326       p0->error = error_node->errors[error0];
327     }
328
329 }
330
331 /*
332  * fd.io coding-style-patch-verification: ON
333  *
334  * Local Variables:
335  * eval: (c-set-style "gnu")
336  * End:
337  */
338
339 #endif