blob: 269049194e603bb4929541938d2c7eb3d2c60e32 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 * ip4/packet.h: ip4 packet format
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_ip4_packet_h
41#define included_ip4_packet_h
42
43#include <vnet/ip/ip_packet.h> /* for ip_csum_t */
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#include <vppinfra/byte_order.h> /* for clib_net_to_host_u16 */
Jieqiang Wangc8833b22020-08-07 14:18:04 +000045#include <vppinfra/warnings.h> /* for WARN_OFF/WARN_ON macro */
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47/* IP4 address which can be accessed either as 4 bytes
48 or as a 32-bit number. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050049typedef union
50{
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 u8 data[4];
52 u32 data_u32;
53 /* Aliases. */
54 u8 as_u8[4];
Neale Rannsa3af3372017-03-28 03:49:52 -070055 u16 as_u16[2];
Ed Warnickecb9cada2015-12-08 15:45:58 -070056 u32 as_u32;
57} ip4_address_t;
58
Dave Barachd7cb1b52016-12-09 09:52:16 -050059typedef struct
60{
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 /* IP address must be first for ip_interface_address_get_address() to work */
62 ip4_address_t ip4_addr;
63 u32 fib_index;
64} ip4_address_fib_t;
65
66always_inline void
Neale Rannsd69f4392018-08-31 06:30:16 -040067ip4_addr_fib_init (ip4_address_fib_t * addr_fib,
68 const ip4_address_t * address, u32 fib_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
Dave Barach178cf492018-11-13 16:34:13 -050070 clib_memcpy_fast (&addr_fib->ip4_addr, address,
71 sizeof (addr_fib->ip4_addr));
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 addr_fib->fib_index = fib_index;
73}
74
75/* (src,dst) pair of addresses as found in packet header. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050076typedef struct
77{
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 ip4_address_t src, dst;
79} ip4_address_pair_t;
80
Damjan Mariona35cc142018-03-16 01:25:27 +010081typedef struct
82{
83 ip4_address_t addr, mask;
84} ip4_address_and_mask_t;
85
Dave Barachd7cb1b52016-12-09 09:52:16 -050086typedef union
87{
88 struct
89 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
91 e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
92 u8 ip_version_and_header_length;
93
94 /* Type of service. */
Neale Ranns038e1df2019-07-19 14:01:02 +000095 ip_dscp_t tos;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 /* Total layer 3 packet length including this header. */
98 u16 length;
99
100 /* Fragmentation ID. */
101 u16 fragment_id;
102
103 /* 3 bits of flags and 13 bits of fragment offset (in units
104 of 8 byte quantities). */
105 u16 flags_and_fragment_offset;
106#define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
107#define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
108#define IP4_HEADER_FLAG_CONGESTION (1 << 15)
109
110 /* Time to live decremented by router at each hop. */
111 u8 ttl;
112
113 /* Next level protocol packet. */
114 u8 protocol;
115
116 /* Checksum. */
117 u16 checksum;
118
119 /* Source and destination address. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500120 union
121 {
122 struct
123 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 ip4_address_t src_address, dst_address;
125 };
126 ip4_address_pair_t address_pair;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500127 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 };
129
130 /* For checksumming we'll want to access IP header in word sized chunks. */
131 /* For 64 bit machines. */
132 CLIB_PACKED (struct {
133 u64 checksum_data_64[2];
134 u32 checksum_data_64_32[1];
135 });
136
137 /* For 32 bit machines. */
138 CLIB_PACKED (struct {
139 u32 checksum_data_32[5];
140 });
141} ip4_header_t;
142
143/* Value of ip_version_and_header_length for packets w/o options. */
144#define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
145 ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
146
Neale Rannsc667ffd2018-06-27 18:59:03 -0700147#define IP4_ROUTER_ALERT_OPTION 20
148
Klement Sekeraf126e742019-10-10 09:46:06 +0000149always_inline u16
Neale Rannsd69f4392018-08-31 06:30:16 -0400150ip4_get_fragment_offset (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500151{
152 return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff;
153}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
Klement Sekeraf126e742019-10-10 09:46:06 +0000155always_inline u16
Neale Rannsd69f4392018-08-31 06:30:16 -0400156ip4_get_fragment_more (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500157{
158 return clib_net_to_host_u16 (i->flags_and_fragment_offset) &
159 IP4_HEADER_FLAG_MORE_FRAGMENTS;
160}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
162always_inline int
Neale Rannsd69f4392018-08-31 06:30:16 -0400163ip4_is_fragment (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500164{
165 return (i->flags_and_fragment_offset &
166 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS));
167}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
169always_inline int
Neale Rannsd69f4392018-08-31 06:30:16 -0400170ip4_is_first_fragment (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500171{
172 return (i->flags_and_fragment_offset &
173 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
174 clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
175}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
177/* Fragment offset in bytes. */
178always_inline int
Neale Rannsd69f4392018-08-31 06:30:16 -0400179ip4_get_fragment_offset_bytes (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500180{
181 return 8 * ip4_get_fragment_offset (i);
182}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
184always_inline int
Neale Rannsd69f4392018-08-31 06:30:16 -0400185ip4_header_bytes (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500186{
187 return sizeof (u32) * (i->ip_version_and_header_length & 0xf);
188}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
190always_inline void *
191ip4_next_header (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500192{
193 return (void *) i + ip4_header_bytes (i);
194}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195
Jieqiang Wangc8833b22020-08-07 14:18:04 +0000196/* Turn off array bounds check due to ip4_header_t
197 option field operations. */
198
Jieqiang Wangc8833b22020-08-07 14:18:04 +0000199WARN_OFF(array-bounds)
Jieqiang Wangc8833b22020-08-07 14:18:04 +0000200
Damjan Marion9a79a1a2020-08-31 19:54:19 +0200201static_always_inline u16
202ip4_header_checksum_inline (ip4_header_t * i, int with_checksum)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203{
Damjan Marione5f00502020-07-16 00:48:55 +0200204 int option_len = (i->ip_version_and_header_length & 0xf) - 5;
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200205 uword sum = 0;
206#if uword_bits == 64
207 u32 *iphdr = (u32 *) i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200209 sum += iphdr[0];
210 sum += iphdr[1];
Damjan Marion9a79a1a2020-08-31 19:54:19 +0200211 sum += with_checksum ? iphdr[2] : *(u16 *) (iphdr + 2);
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200212 /* skip checksum */
213 sum += iphdr[3];
214 sum += iphdr[4];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Damjan Marione5f00502020-07-16 00:48:55 +0200216 if (PREDICT_FALSE (option_len > 0))
217 switch (option_len)
218 {
219 case 10:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200220 sum += iphdr[14];
Damjan Marione5f00502020-07-16 00:48:55 +0200221 case 9:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200222 sum += iphdr[13];
Damjan Marione5f00502020-07-16 00:48:55 +0200223 case 8:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200224 sum += iphdr[12];
Damjan Marione5f00502020-07-16 00:48:55 +0200225 case 7:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200226 sum += iphdr[11];
Damjan Marione5f00502020-07-16 00:48:55 +0200227 case 6:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200228 sum += iphdr[10];
Damjan Marione5f00502020-07-16 00:48:55 +0200229 case 5:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200230 sum += iphdr[9];
Damjan Marione5f00502020-07-16 00:48:55 +0200231 case 4:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200232 sum += iphdr[8];
Damjan Marione5f00502020-07-16 00:48:55 +0200233 case 3:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200234 sum += iphdr[7];
Damjan Marione5f00502020-07-16 00:48:55 +0200235 case 2:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200236 sum += iphdr[6];
Damjan Marione5f00502020-07-16 00:48:55 +0200237 case 1:
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200238 sum += iphdr[5];
Damjan Marione5f00502020-07-16 00:48:55 +0200239 default:
240 break;
241 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200243 sum = ((u32) sum) + (sum >> 32);
244#else
245 u16 *iphdr = (u16 *) i;
246
247 sum += iphdr[0];
248 sum += iphdr[1];
249 sum += iphdr[2];
250 sum += iphdr[3];
251 sum += iphdr[4];
Damjan Marion9a79a1a2020-08-31 19:54:19 +0200252 if (with_checksum)
253 sum += iphdr[5];
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200254 sum += iphdr[6];
255 sum += iphdr[7];
256 sum += iphdr[8];
257 sum += iphdr[9];
258
259 if (PREDICT_FALSE (option_len > 0))
260 switch (option_len)
261 {
262 case 10:
263 sum += iphdr[28];
264 sum += iphdr[29];
265 case 9:
266 sum += iphdr[26];
267 sum += iphdr[27];
268 case 8:
269 sum += iphdr[24];
270 sum += iphdr[25];
271 case 7:
272 sum += iphdr[22];
273 sum += iphdr[23];
274 case 6:
275 sum += iphdr[20];
276 sum += iphdr[21];
277 case 5:
278 sum += iphdr[18];
279 sum += iphdr[19];
280 case 4:
281 sum += iphdr[16];
282 sum += iphdr[17];
283 case 3:
284 sum += iphdr[14];
285 sum += iphdr[15];
286 case 2:
287 sum += iphdr[12];
288 sum += iphdr[13];
289 case 1:
290 sum += iphdr[10];
291 sum += iphdr[11];
292 default:
293 break;
294 }
295#endif
296
Damjan Marione5f00502020-07-16 00:48:55 +0200297 sum = ((u16) sum) + (sum >> 16);
298 sum = ((u16) sum) + (sum >> 16);
Damjan Marion9a0f2a52020-07-16 16:22:48 +0200299 return ~((u16) sum);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300}
301
Jieqiang Wangc8833b22020-08-07 14:18:04 +0000302WARN_ON(array-bounds)
Jieqiang Wangc8833b22020-08-07 14:18:04 +0000303
Damjan Marion9a79a1a2020-08-31 19:54:19 +0200304always_inline u16
305ip4_header_checksum (ip4_header_t * i)
306{
307 return ip4_header_checksum_inline (i, /* with_checksum */ 0);
308}
309
Neale Ranns95346962019-11-25 13:04:44 +0000310always_inline void
311ip4_header_set_dscp (ip4_header_t * ip4, ip_dscp_t dscp)
312{
313 ip4->tos &= ~0xfc;
314 /* not masking the dscp value to save th instruction
315 * it shouldn't b necessary since the argument is an enum
316 * whose range is therefore constrained in the CP. in the
317 * DP it will have been taken from another packet, so again
318 * constrained in value */
319 ip4->tos |= dscp << IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT;
320}
321
322always_inline void
323ip4_header_set_ecn (ip4_header_t * ip4, ip_ecn_t ecn)
324{
325 ip4->tos &= ~IP_PACKET_TC_FIELD_ECN_MASK;
326 ip4->tos |= ecn;
327}
328
329always_inline void
330ip4_header_set_ecn_w_chksum (ip4_header_t * ip4, ip_ecn_t ecn)
331{
332 ip_csum_t sum = ip4->checksum;
333 u8 old = ip4->tos;
334 u8 new = (old & ~IP_PACKET_TC_FIELD_ECN_MASK) | ecn;
335
336 sum = ip_csum_update (sum, old, new, ip4_header_t, tos);
337 ip4->checksum = ip_csum_fold (sum);
338 ip4->tos = new;
339}
340
341always_inline ip_dscp_t
342ip4_header_get_dscp (const ip4_header_t * ip4)
343{
344 return (ip4->tos >> IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT);
345}
346
347always_inline ip_ecn_t
348ip4_header_get_ecn (const ip4_header_t * ip4)
349{
350 return (ip4->tos & IP_PACKET_TC_FIELD_ECN_MASK);
351}
352
Neale Rannsa91cb452021-02-04 11:02:52 +0000353always_inline u8
354ip4_header_get_ttl (const ip4_header_t *ip4)
355{
356 return (ip4->ttl);
357}
358
359always_inline void
360ip4_header_set_ttl (ip4_header_t *ip4, u8 ttl)
361{
362 ip4->ttl = ttl;
363}
364
Neale Ranns95346962019-11-25 13:04:44 +0000365always_inline void
366ip4_header_set_df (ip4_header_t * ip4)
367{
368 ip4->flags_and_fragment_offset |=
369 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
370}
371
372always_inline void
373ip4_header_clear_df (ip4_header_t * ip4)
374{
375 ip4->flags_and_fragment_offset &=
376 ~clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
377}
378
379always_inline u8
Neale Rannse5b94dd2019-12-31 05:13:14 +0000380ip4_header_get_df (const ip4_header_t * ip4)
Neale Ranns95346962019-11-25 13:04:44 +0000381{
382 return (! !(ip4->flags_and_fragment_offset &
383 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT)));
384}
385
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386static inline uword
387ip4_header_checksum_is_valid (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500388{
Damjan Marion9a79a1a2020-08-31 19:54:19 +0200389 return ip4_header_checksum_inline (i, /* with_checksum */ 1) == 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500390}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
392#define ip4_partial_header_checksum_x1(ip0,sum0) \
393do { \
394 if (BITS (ip_csum_t) > 32) \
395 { \
396 sum0 = ip0->checksum_data_64[0]; \
397 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
398 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
399 } \
400 else \
401 { \
402 sum0 = ip0->checksum_data_32[0]; \
403 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
404 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
405 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
406 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
407 } \
408} while (0)
409
410#define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
411do { \
412 if (BITS (ip_csum_t) > 32) \
413 { \
414 sum0 = ip0->checksum_data_64[0]; \
415 sum1 = ip1->checksum_data_64[0]; \
416 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
417 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
418 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
419 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
420 } \
421 else \
422 { \
423 sum0 = ip0->checksum_data_32[0]; \
424 sum1 = ip1->checksum_data_32[0]; \
425 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
426 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
427 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
428 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
429 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
430 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
431 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
432 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
433 } \
434} while (0)
435
436always_inline uword
Neale Rannsd69f4392018-08-31 06:30:16 -0400437ip4_address_is_multicast (const ip4_address_t * a)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500438{
439 return (a->data[0] & 0xf0) == 0xe0;
440}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Neale Rannsd724e4f2020-04-02 15:02:16 +0000442always_inline uword
443ip4_address_is_global_broadcast (const ip4_address_t * a)
444{
445 return (a->as_u32) == 0xffffffff;
446}
447
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500449ip4_multicast_address_set_for_group (ip4_address_t * a,
450 ip_multicast_group_t g)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451{
Damjan Marion2c29d752015-12-18 10:26:56 +0100452 ASSERT ((u32) g < (1 << 28));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
454}
455
456always_inline void
Neale Rannsd69f4392018-08-31 06:30:16 -0400457ip4_multicast_ethernet_address (u8 * ethernet_address,
458 const ip4_address_t * a)
Eyal Baric5b13602016-11-24 19:42:43 +0200459{
Neale Rannsd69f4392018-08-31 06:30:16 -0400460 const u8 *d = a->as_u8;
Eyal Baric5b13602016-11-24 19:42:43 +0200461
462 ethernet_address[0] = 0x01;
463 ethernet_address[1] = 0x00;
464 ethernet_address[2] = 0x5e;
465 ethernet_address[3] = d[1] & 0x7f;
466 ethernet_address[4] = d[2];
467 ethernet_address[5] = d[3];
468}
469
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470#endif /* included_ip4_packet_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500471
472/*
473 * fd.io coding-style-patch-verification: ON
474 *
475 * Local Variables:
476 * eval: (c-set-style "gnu")
477 * End:
478 */