blob: 362805c8529a7000d72600341953c3dd3e90e538 [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 */
Dave Barach68b0fb02017-02-28 15:15:56 -050044#include <vnet/tcp/tcp_packet.h> /* for tcp_header_t */
Ed Warnickecb9cada2015-12-08 15:45:58 -070045#include <vppinfra/byte_order.h> /* for clib_net_to_host_u16 */
46
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
Ed Warnickecb9cada2015-12-08 15:45:58 -070086/* If address is a valid netmask, return length of mask. */
87always_inline uword
Neale Rannsd69f4392018-08-31 06:30:16 -040088ip4_address_netmask_length (const ip4_address_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -070089{
90 uword result = 0;
91 uword i;
92 for (i = 0; i < ARRAY_LEN (a->as_u8); i++)
93 {
94 switch (a->as_u8[i])
95 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050096 case 0xff:
97 result += 8;
98 break;
99 case 0xfe:
100 result += 7;
101 goto done;
102 case 0xfc:
103 result += 6;
104 goto done;
105 case 0xf8:
106 result += 5;
107 goto done;
108 case 0xf0:
109 result += 4;
110 goto done;
111 case 0xe0:
112 result += 3;
113 goto done;
114 case 0xc0:
115 result += 2;
116 goto done;
117 case 0x80:
118 result += 1;
119 goto done;
120 case 0x00:
121 result += 0;
122 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123 default:
124 /* Not a valid netmask mask. */
125 return ~0;
126 }
127 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500128done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 return result;
130}
131
Dave Barachd7cb1b52016-12-09 09:52:16 -0500132typedef union
133{
134 struct
135 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
137 e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
138 u8 ip_version_and_header_length;
139
140 /* Type of service. */
Neale Ranns038e1df2019-07-19 14:01:02 +0000141 ip_dscp_t tos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
143 /* Total layer 3 packet length including this header. */
144 u16 length;
145
146 /* Fragmentation ID. */
147 u16 fragment_id;
148
149 /* 3 bits of flags and 13 bits of fragment offset (in units
150 of 8 byte quantities). */
151 u16 flags_and_fragment_offset;
152#define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
153#define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
154#define IP4_HEADER_FLAG_CONGESTION (1 << 15)
155
156 /* Time to live decremented by router at each hop. */
157 u8 ttl;
158
159 /* Next level protocol packet. */
160 u8 protocol;
161
162 /* Checksum. */
163 u16 checksum;
164
165 /* Source and destination address. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500166 union
167 {
168 struct
169 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 ip4_address_t src_address, dst_address;
171 };
172 ip4_address_pair_t address_pair;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500173 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174 };
175
176 /* For checksumming we'll want to access IP header in word sized chunks. */
177 /* For 64 bit machines. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500178 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 CLIB_PACKED (struct {
180 u64 checksum_data_64[2];
181 u32 checksum_data_64_32[1];
182 });
Dave Barachd7cb1b52016-12-09 09:52:16 -0500183 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
185 /* For 32 bit machines. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500186 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 CLIB_PACKED (struct {
188 u32 checksum_data_32[5];
189 });
Dave Barachd7cb1b52016-12-09 09:52:16 -0500190 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191} ip4_header_t;
192
193/* Value of ip_version_and_header_length for packets w/o options. */
194#define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
195 ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
196
Neale Rannsc667ffd2018-06-27 18:59:03 -0700197#define IP4_ROUTER_ALERT_OPTION 20
198
Klement Sekeraf126e742019-10-10 09:46:06 +0000199always_inline u16
Neale Rannsd69f4392018-08-31 06:30:16 -0400200ip4_get_fragment_offset (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500201{
202 return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff;
203}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
Klement Sekeraf126e742019-10-10 09:46:06 +0000205always_inline u16
Neale Rannsd69f4392018-08-31 06:30:16 -0400206ip4_get_fragment_more (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500207{
208 return clib_net_to_host_u16 (i->flags_and_fragment_offset) &
209 IP4_HEADER_FLAG_MORE_FRAGMENTS;
210}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
212always_inline int
Neale Rannsd69f4392018-08-31 06:30:16 -0400213ip4_is_fragment (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500214{
215 return (i->flags_and_fragment_offset &
216 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS));
217}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
219always_inline int
Neale Rannsd69f4392018-08-31 06:30:16 -0400220ip4_is_first_fragment (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500221{
222 return (i->flags_and_fragment_offset &
223 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
224 clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
225}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
227/* Fragment offset in bytes. */
228always_inline int
Neale Rannsd69f4392018-08-31 06:30:16 -0400229ip4_get_fragment_offset_bytes (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500230{
231 return 8 * ip4_get_fragment_offset (i);
232}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
234always_inline int
Neale Rannsd69f4392018-08-31 06:30:16 -0400235ip4_header_bytes (const ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236{
237 return sizeof (u32) * (i->ip_version_and_header_length & 0xf);
238}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
240always_inline void *
241ip4_next_header (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500242{
243 return (void *) i + ip4_header_bytes (i);
244}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
246always_inline u16
247ip4_header_checksum (ip4_header_t * i)
248{
249 u16 save, csum;
250 ip_csum_t sum;
251
252 save = i->checksum;
253 i->checksum = 0;
254 sum = ip_incremental_checksum (0, i, ip4_header_bytes (i));
255 csum = ~ip_csum_fold (sum);
256
257 i->checksum = save;
258
259 /* Make checksum agree for special case where either
260 0 or 0xffff would give same 1s complement sum. */
261 if (csum == 0 && save == 0xffff)
262 csum = save;
263
264 return csum;
265}
266
Neale Ranns95346962019-11-25 13:04:44 +0000267always_inline void
268ip4_header_set_dscp (ip4_header_t * ip4, ip_dscp_t dscp)
269{
270 ip4->tos &= ~0xfc;
271 /* not masking the dscp value to save th instruction
272 * it shouldn't b necessary since the argument is an enum
273 * whose range is therefore constrained in the CP. in the
274 * DP it will have been taken from another packet, so again
275 * constrained in value */
276 ip4->tos |= dscp << IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT;
277}
278
279always_inline void
280ip4_header_set_ecn (ip4_header_t * ip4, ip_ecn_t ecn)
281{
282 ip4->tos &= ~IP_PACKET_TC_FIELD_ECN_MASK;
283 ip4->tos |= ecn;
284}
285
286always_inline void
287ip4_header_set_ecn_w_chksum (ip4_header_t * ip4, ip_ecn_t ecn)
288{
289 ip_csum_t sum = ip4->checksum;
290 u8 old = ip4->tos;
291 u8 new = (old & ~IP_PACKET_TC_FIELD_ECN_MASK) | ecn;
292
293 sum = ip_csum_update (sum, old, new, ip4_header_t, tos);
294 ip4->checksum = ip_csum_fold (sum);
295 ip4->tos = new;
296}
297
298always_inline ip_dscp_t
299ip4_header_get_dscp (const ip4_header_t * ip4)
300{
301 return (ip4->tos >> IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT);
302}
303
304always_inline ip_ecn_t
305ip4_header_get_ecn (const ip4_header_t * ip4)
306{
307 return (ip4->tos & IP_PACKET_TC_FIELD_ECN_MASK);
308}
309
310always_inline void
311ip4_header_set_df (ip4_header_t * ip4)
312{
313 ip4->flags_and_fragment_offset |=
314 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
315}
316
317always_inline void
318ip4_header_clear_df (ip4_header_t * ip4)
319{
320 ip4->flags_and_fragment_offset &=
321 ~clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
322}
323
324always_inline u8
Neale Rannse5b94dd2019-12-31 05:13:14 +0000325ip4_header_get_df (const ip4_header_t * ip4)
Neale Ranns95346962019-11-25 13:04:44 +0000326{
327 return (! !(ip4->flags_and_fragment_offset &
328 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT)));
329}
330
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331static inline uword
332ip4_header_checksum_is_valid (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500333{
334 return i->checksum == ip4_header_checksum (i);
335}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
337#define ip4_partial_header_checksum_x1(ip0,sum0) \
338do { \
339 if (BITS (ip_csum_t) > 32) \
340 { \
341 sum0 = ip0->checksum_data_64[0]; \
342 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
343 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
344 } \
345 else \
346 { \
347 sum0 = ip0->checksum_data_32[0]; \
348 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
349 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
350 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
351 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
352 } \
353} while (0)
354
355#define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
356do { \
357 if (BITS (ip_csum_t) > 32) \
358 { \
359 sum0 = ip0->checksum_data_64[0]; \
360 sum1 = ip1->checksum_data_64[0]; \
361 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
362 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
363 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
364 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
365 } \
366 else \
367 { \
368 sum0 = ip0->checksum_data_32[0]; \
369 sum1 = ip1->checksum_data_32[0]; \
370 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
371 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
372 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
373 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
374 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
375 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
376 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
377 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
378 } \
379} while (0)
380
381always_inline uword
Neale Rannsd69f4392018-08-31 06:30:16 -0400382ip4_address_is_multicast (const ip4_address_t * a)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500383{
384 return (a->data[0] & 0xf0) == 0xe0;
385}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
Neale Rannsd724e4f2020-04-02 15:02:16 +0000387always_inline uword
388ip4_address_is_global_broadcast (const ip4_address_t * a)
389{
390 return (a->as_u32) == 0xffffffff;
391}
392
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500394ip4_multicast_address_set_for_group (ip4_address_t * a,
395 ip_multicast_group_t g)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396{
Damjan Marion2c29d752015-12-18 10:26:56 +0100397 ASSERT ((u32) g < (1 << 28));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
399}
400
401always_inline void
Neale Rannsd69f4392018-08-31 06:30:16 -0400402ip4_multicast_ethernet_address (u8 * ethernet_address,
403 const ip4_address_t * a)
Eyal Baric5b13602016-11-24 19:42:43 +0200404{
Neale Rannsd69f4392018-08-31 06:30:16 -0400405 const u8 *d = a->as_u8;
Eyal Baric5b13602016-11-24 19:42:43 +0200406
407 ethernet_address[0] = 0x01;
408 ethernet_address[1] = 0x00;
409 ethernet_address[2] = 0x5e;
410 ethernet_address[3] = d[1] & 0x7f;
411 ethernet_address[4] = d[2];
412 ethernet_address[5] = d[3];
413}
414
415always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
417{
418 u32 src0, dst0;
419
420 src0 = ip0->src_address.data_u32;
421 dst0 = ip0->dst_address.data_u32;
422 ip0->src_address.data_u32 = dst0;
423 ip0->dst_address.data_u32 = src0;
424
Dave Barach68b0fb02017-02-28 15:15:56 -0500425 src0 = tcp0->src;
426 dst0 = tcp0->dst;
427 tcp0->src = dst0;
428 tcp0->dst = src0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429}
430
431always_inline void
432ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
433 tcp_header_t * tcp0, tcp_header_t * tcp1)
434{
435 u32 src0, dst0, src1, dst1;
436
437 src0 = ip0->src_address.data_u32;
438 src1 = ip1->src_address.data_u32;
439 dst0 = ip0->dst_address.data_u32;
440 dst1 = ip1->dst_address.data_u32;
441 ip0->src_address.data_u32 = dst0;
442 ip1->src_address.data_u32 = dst1;
443 ip0->dst_address.data_u32 = src0;
444 ip1->dst_address.data_u32 = src1;
445
Dave Barach68b0fb02017-02-28 15:15:56 -0500446 src0 = tcp0->src;
447 src1 = tcp1->src;
448 dst0 = tcp0->dst;
449 dst1 = tcp1->dst;
450 tcp0->src = dst0;
451 tcp1->src = dst1;
452 tcp0->dst = src0;
453 tcp1->dst = src1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454}
455
456#endif /* included_ip4_packet_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500457
458/*
459 * fd.io coding-style-patch-verification: ON
460 *
461 * Local Variables:
462 * eval: (c-set-style "gnu")
463 * End:
464 */