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