FD.io VPP  v21.01.1
Vector Packet Processing
ip4_input.h
Go to the documentation of this file.
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 {
57 
59 check_ver_opt_csum (ip4_header_t * ip, u8 * error, int verify_checksum)
60 {
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
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 ?
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 ?
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 ?
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 ?
198  p[3]->error = error_node->errors[error3];
199  }
200 }
201 
202 always_inline void
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 ?
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 ?
273  p1->error = error_node->errors[error1];
274  }
275 }
276 
277 always_inline void
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 ?
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
static_always_inline void check_ver_opt_csum(ip4_header_t *ip, u8 *error, int verify_checksum)
Definition: ip4_input.h:59
vlib_main_t * vm
Definition: in2out_ed.c:1580
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:470
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:402
unsigned char u8
Definition: types.h:56
#define static_always_inline
Definition: clib.h:109
static void ip4_input_check_x1(vlib_main_t *vm, vlib_node_runtime_t *error_node, vlib_buffer_t *p0, ip4_header_t *ip0, u32 *next0, int verify_checksum)
Definition: ip4_input.h:278
unsigned int u32
Definition: types.h:88
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
Definition: cJSON.c:84
unsigned short u16
Definition: types.h:57
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define always_inline
Definition: ipsec.h:28
static_always_inline u16 ip_csum(void *data, u16 n_left)
Definition: ip_packet.h:155
static u16 ip4_get_fragment_offset(const ip4_header_t *i)
Definition: ip4_packet.h:155
u8 ttl
Definition: fib_types.api:26
signed int i32
Definition: types.h:77
ip4_input_next_t
Definition: ip4_input.h:46
char const int length
Definition: cJSON.h:163
vl_api_address_t ip
Definition: l2.api:501
static void ip4_input_check_x2(vlib_main_t *vm, vlib_node_runtime_t *error_node, vlib_buffer_t *p0, vlib_buffer_t *p1, ip4_header_t *ip0, ip4_header_t *ip1, u32 *next0, u32 *next1, int verify_checksum)
Definition: ip4_input.h:203
static void ip4_input_check_x4(vlib_main_t *vm, vlib_node_runtime_t *error_node, vlib_buffer_t **p, ip4_header_t **ip, u16 *next, int verify_checksum)
Definition: ip4_input.h:79
VLIB buffer representation.
Definition: buffer.h:102
static int ip4_header_bytes(const ip4_header_t *i)
Definition: ip4_packet.h:190
u8 ip_version_and_header_length
Definition: ip4_packet.h:93
static_always_inline void icmp4_error_set_vnet_buffer(vlib_buffer_t *b, u8 type, u8 code, u32 data)
Definition: icmp4.h:51