aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/ip/ip4_input.h
blob: 889b423d7003f9ad1b60adc6e2316178e83145eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * ip/ip4_input.c: IP v4 input node
 *
 * Copyright (c) 2008 Eliot Dresselhaus
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef included_ip_input_h
#define included_ip_input_h

#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>

extern char *ip4_error_strings[];

typedef enum
{
  IP4_INPUT_NEXT_DROP,
  IP4_INPUT_NEXT_PUNT,
  IP4_INPUT_NEXT_LOOKUP,
  IP4_INPUT_NEXT_LOOKUP_MULTICAST,
  IP4_INPUT_NEXT_ICMP_ERROR,
  IP4_INPUT_NEXT_REASSEMBLY,
  IP4_INPUT_N_NEXT,
} ip4_input_next_t;

static_always_inline void
check_ver_opt_csum (ip4_header_t * ip, u8 * error, int verify_checksum)
{
  if (PREDICT_FALSE (ip->ip_version_and_header_length != 0x45))
    {
      if ((ip->ip_version_and_header_length & 0xf) != 5)
	{
	  *error = IP4_ERROR_OPTIONS;
	  if (verify_checksum && ip_csum (ip, ip4_header_bytes (ip)) != 0)
	    *error = IP4_ERROR_BAD_CHECKSUM;
	}
      else
	*error = IP4_ERROR_VERSION;
    }
  else
    if (PREDICT_FALSE (verify_checksum &&
		       ip_csum (ip, sizeof (ip4_header_t)) != 0))
    *error = IP4_ERROR_BAD_CHECKSUM;
}

always_inline 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)
{
  u8 error0, error1, error2, error3;
  u32 ip_len0, cur_len0;
  u32 ip_len1, cur_len1;
  u32 ip_len2, cur_len2;
  u32 ip_len3, cur_len3;
  i32 len_diff0, len_diff1, len_diff2, len_diff3;

  error0 = error1 = error2 = error3 = IP4_ERROR_NONE;

  check_ver_opt_csum (ip[0], &error0, verify_checksum);
  check_ver_opt_csum (ip[1], &error1, verify_checksum);
  check_ver_opt_csum (ip[2], &error2, verify_checksum);
  check_ver_opt_csum (ip[3], &error3, verify_checksum);

  if (PREDICT_FALSE (ip[0]->ttl < 1))
    error0 = IP4_ERROR_TIME_EXPIRED;
  if (PREDICT_FALSE (ip[1]->ttl < 1))
    error1 = IP4_ERROR_TIME_EXPIRED;
  if (PREDICT_FALSE (ip[2]->ttl < 1))
    error2 = IP4_ERROR_TIME_EXPIRED;
  if (PREDICT_FALSE (ip[3]->ttl < 1))
    error3 = IP4_ERROR_TIME_EXPIRED;

  /* Drop fragmentation offset 1 packets. */
  error0 = ip4_get_fragment_offset (ip[0]) == 1 ?
    IP4_ERROR_FRAGMENT_OFFSET_ONE : error0;
  error1 = ip4_get_fragment_offset (ip[1]) == 1 ?
    IP4_ERROR_FRAGMENT_OFFSET_ONE : error1;
  error2 = ip4_get_fragment_offset (ip[2]) == 1 ?
    IP4_ERROR_FRAGMENT_OFFSET_ONE : error2;
  error3 = ip4_get_fragment_offset (ip[3]) == 1 ?
    IP4_ERROR_FRAGMENT_OFFSET_ONE : error3;

  /* Verify lengths. */
  ip_len0 = clib_net_to_host_u16 (ip[0]->length);
  ip_len1 = clib_net_to_host_u16 (ip[1]->length);
  ip_len2 = clib_net_to_host_u16 (ip[2]->length);
  ip_len3 = clib_net_to_host_u16 (ip[3]->length);

  /* IP length must be at least minimal IP header. */
  error0 = ip_len0 < sizeof (ip[0][0]) ? IP4_ERROR_TOO_SHORT : error0;
  error1 = ip_len1 < sizeof (ip[1][0]) ? IP4_ERROR_TOO_SHORT : error1;
  error2 = ip_len2 < sizeof (ip[2][0]) ? IP4_ERROR_TOO_SHORT : error2;
  error3 = ip_len3 < sizeof (ip[3][0]) ? IP4_ERROR_TOO_SHORT : error3;

  cur_len0 = vlib_buffer_length_in_chain (vm, p[0]);
  cur_len1 = vlib_buffer_length_in_chain (vm, p[1]);
  cur_len2 = vlib_buffer_length_in_chain (vm, p[2]);
  cur_len3 = vlib_buffer_length_in_chain (vm, p[3]);

  len_diff0 = cur_len0 - ip_len0;
  len_diff1 = cur_len1 - ip_len1;
  len_diff2 = cur_len2 - ip_len2;
  len_diff3 = cur_len3 - ip_len3;

  error0 = len_diff0 < 0 ? IP4_ERROR_BAD_LENGTH : error0;
  error1 = len_diff1 < 0 ? IP4_ERROR_BAD_LENGTH : error1;
  error2 = len_diff2 < 0 ? IP4_ERROR_BAD_LENGTH : error2;
  error3 = len_diff3 < 0 ? IP4_ERROR_BAD_LENGTH : error3;

  if (PREDICT_FALSE (error0 != IP4_ERROR_NONE))
    {
      if (error0 == IP4_ERROR_TIME_EXPIRED)
	{
	  icmp4_error_set_vnet_buffer (p[0], ICMP4_time_exceeded,
				       ICMP4_time_exceeded_ttl_exceeded_in_transit,
				       0);
	  next[0] = IP4_INPUT_NEXT_ICMP_ERROR;
	}
      else
	next[0] = error0 != IP4_ERROR_OPTIONS ?
	  IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
      p[0]->error = error_node->errors[error0];
    }
  if (PREDICT_FALSE (error1 != IP4_ERROR_NONE))
    {
      if (error1 == IP4_ERROR_TIME_EXPIRED)
	{
	  icmp4_error_set_vnet_buffer (p[1], ICMP4_time_exceeded,
				       ICMP4_time_exceeded_ttl_exceeded_in_transit,
				       0);
	  next[1] = IP4_INPUT_NEXT_ICMP_ERROR;
	}
      else
	next[1] = error1 != IP4_ERROR_OPTIONS ?
	  IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
      p[1]->error = error_node->errors[error1];
    }
  if (PREDICT_FALSE (error2 != IP4_ERROR_NONE))
    {
      if (error2 == IP4_ERROR_TIME_EXPIRED)
	{
	  icmp4_error_set_vnet_buffer (p[2], ICMP4_time_exceeded,
				       ICMP4_time_exceeded_ttl_exceeded_in_transit,
				       0);
	  next[2] = IP4_INPUT_NEXT_ICMP_ERROR;
	}
      else
	next[2] = error2 != IP4_ERROR_OPTIONS ?
	  IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
      p[2]->error = error_node->errors[error2];
    }
  if (PREDICT_FALSE (error3 != IP4_ERROR_NONE))
    {
      if (error3 == IP4_ERROR_TIME_EXPIRED)
	{
	  icmp4_error_set_vnet_buffer (p[3], ICMP4_time_exceeded,
				       ICMP4_time_exceeded_ttl_exceeded_in_transit,
				       0);
	  next[3] = IP4_INPUT_NEXT_ICMP_ERROR;
	}
      else
	next[3] = error3 != IP4_ERROR_OPTIONS ?
	  IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
      p[3]->error = error_node->errors[error3];
    }
}

always_inline 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)
{
  u8 error0, error1;
  u32 ip_len0, cur_len0;
  u32 ip_len1, cur_len1;
  i32 len_diff0, len_diff1;

  error0 = error1 = IP4_ERROR_NONE;

  check_ver_opt_csum (ip0, &error0, verify_checksum);
  check_ver_opt_csum (ip1, &error1, verify_checksum);

  if (PREDICT_FALSE (ip0->ttl < 1))
    error0 = IP4_ERROR_TIME_EXPIRED;
  if (PREDICT_FALSE (ip1->ttl < 1))
    error1 = IP4_ERROR_TIME_EXPIRED;

  /* Drop fragmentation offset 1 packets. */
  error0 = ip4_get_fragment_offset (ip0) == 1 ?
    IP4_ERROR_FRAGMENT_OFFSET_ONE : error0;
  error1 = ip4_get_fragment_offset (ip1) == 1 ?
    IP4_ERROR_FRAGMENT_OFFSET_ONE : error1;

  /* Verify lengths. */
  ip_len0 = clib_net_to_host_u16 (ip0->length);
  ip_len1 = clib_net_to_host_u16 (ip1->length);

  /* IP length must be at least minimal IP header. */
  error0 = ip_len0 < sizeof (ip0[0]) ? IP4_ERROR_TOO_SHORT : error0;
  error1 = ip_len1 < sizeof (ip1[0]) ? IP4_ERROR_TOO_SHORT : error1;

  cur_len0 = vlib_buffer_length_in_chain (vm, p0);
  cur_len1 = vlib_buffer_length_in_chain (vm, p1);

  len_diff0 = cur_len0 - ip_len0;
  len_diff1 = cur_len1 - ip_len1;

  error0 = len_diff0 < 0 ? IP4_ERROR_BAD_LENGTH : error0;
  error1 = len_diff1 < 0 ? IP4_ERROR_BAD_LENGTH : error1;

  if (PREDICT_FALSE (error0 != IP4_ERROR_NONE))
    {
      if (error0 == IP4_ERROR_TIME_EXPIRED)
	{
	  icmp4_error_set_vnet_buffer (p0, ICMP4_time_exceeded,
				       ICMP4_time_exceeded_ttl_exceeded_in_transit,
				       0);
	  *next0 = IP4_INPUT_NEXT_ICMP_ERROR;
	}
      else
	*next0 = error0 != IP4_ERROR_OPTIONS ?
	  IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
      p0->error = error_node->errors[error0];
    }
  if (PREDICT_FALSE (error1 != IP4_ERROR_NONE))
    {
      if (error1 == IP4_ERROR_TIME_EXPIRED)
	{
	  icmp4_error_set_vnet_buffer (p1, ICMP4_time_exceeded,
				       ICMP4_time_exceeded_ttl_exceeded_in_transit,
				       0);
	  *next1 = IP4_INPUT_NEXT_ICMP_ERROR;
	}
      else
	*next1 = error1 != IP4_ERROR_OPTIONS ?
	  IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
      p1->error = error_node->errors[error1];
    }

}

always_inline 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)
{
  u32 ip_len0, cur_len0;
  i32 len_diff0;
  u8 error0;

  error0 = IP4_ERROR_NONE;

  check_ver_opt_csum (ip0, &error0, verify_checksum);

  /* Punt packets with options or wrong version. */
  if (PREDICT_FALSE (ip0->ip_version_and_header_length != 0x45))
    error0 = (ip0->ip_version_and_header_length & 0xf) != 5 ?
      IP4_ERROR_OPTIONS : IP4_ERROR_VERSION;

  /* Drop fragmentation offset 1 packets. */
  error0 = ip4_get_fragment_offset (ip0) == 1 ?
    IP4_ERROR_FRAGMENT_OFFSET_ONE : error0;

  /* Verify lengths. */
  ip_len0 = clib_net_to_host_u16 (ip0->length);

  /* IP length must be at least minimal IP header. */
  error0 = ip_len0 < sizeof (ip0[0]) ? IP4_ERROR_TOO_SHORT : error0;

  cur_len0 = vlib_buffer_length_in_chain (vm, p0);

  len_diff0 = cur_len0 - ip_len0;

  error0 = len_diff0 < 0 ? IP4_ERROR_BAD_LENGTH : error0;

  if (PREDICT_FALSE (error0 != IP4_ERROR_NONE))
    {
      if (error0 == IP4_ERROR_TIME_EXPIRED)
	{
	  icmp4_error_set_vnet_buffer (p0, ICMP4_time_exceeded,
				       ICMP4_time_exceeded_ttl_exceeded_in_transit,
				       0);
	  *next0 = IP4_INPUT_NEXT_ICMP_ERROR;
	}
      else
	*next0 = error0 != IP4_ERROR_OPTIONS ?
	  IP4_INPUT_NEXT_DROP : IP4_INPUT_NEXT_PUNT;
      p0->error = error_node->errors[error0];
    }

}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */

#endif