blob: c1852fc3ff24bf16a8b8225ed69f077d53cf891b [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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199always_inline int
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
205always_inline int
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
267static inline uword
268ip4_header_checksum_is_valid (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500269{
270 return i->checksum == ip4_header_checksum (i);
271}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
273#define ip4_partial_header_checksum_x1(ip0,sum0) \
274do { \
275 if (BITS (ip_csum_t) > 32) \
276 { \
277 sum0 = ip0->checksum_data_64[0]; \
278 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
279 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
280 } \
281 else \
282 { \
283 sum0 = ip0->checksum_data_32[0]; \
284 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
285 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
286 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
287 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
288 } \
289} while (0)
290
291#define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
292do { \
293 if (BITS (ip_csum_t) > 32) \
294 { \
295 sum0 = ip0->checksum_data_64[0]; \
296 sum1 = ip1->checksum_data_64[0]; \
297 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
298 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
299 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
300 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
301 } \
302 else \
303 { \
304 sum0 = ip0->checksum_data_32[0]; \
305 sum1 = ip1->checksum_data_32[0]; \
306 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
307 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
308 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
309 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
310 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
311 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
312 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
313 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
314 } \
315} while (0)
316
317always_inline uword
Neale Rannsd69f4392018-08-31 06:30:16 -0400318ip4_address_is_multicast (const ip4_address_t * a)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500319{
320 return (a->data[0] & 0xf0) == 0xe0;
321}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322
323always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500324ip4_multicast_address_set_for_group (ip4_address_t * a,
325 ip_multicast_group_t g)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326{
Damjan Marion2c29d752015-12-18 10:26:56 +0100327 ASSERT ((u32) g < (1 << 28));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
329}
330
331always_inline void
Neale Rannsd69f4392018-08-31 06:30:16 -0400332ip4_multicast_ethernet_address (u8 * ethernet_address,
333 const ip4_address_t * a)
Eyal Baric5b13602016-11-24 19:42:43 +0200334{
Neale Rannsd69f4392018-08-31 06:30:16 -0400335 const u8 *d = a->as_u8;
Eyal Baric5b13602016-11-24 19:42:43 +0200336
337 ethernet_address[0] = 0x01;
338 ethernet_address[1] = 0x00;
339 ethernet_address[2] = 0x5e;
340 ethernet_address[3] = d[1] & 0x7f;
341 ethernet_address[4] = d[2];
342 ethernet_address[5] = d[3];
343}
344
345always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
347{
348 u32 src0, dst0;
349
350 src0 = ip0->src_address.data_u32;
351 dst0 = ip0->dst_address.data_u32;
352 ip0->src_address.data_u32 = dst0;
353 ip0->dst_address.data_u32 = src0;
354
Dave Barach68b0fb02017-02-28 15:15:56 -0500355 src0 = tcp0->src;
356 dst0 = tcp0->dst;
357 tcp0->src = dst0;
358 tcp0->dst = src0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359}
360
361always_inline void
362ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
363 tcp_header_t * tcp0, tcp_header_t * tcp1)
364{
365 u32 src0, dst0, src1, dst1;
366
367 src0 = ip0->src_address.data_u32;
368 src1 = ip1->src_address.data_u32;
369 dst0 = ip0->dst_address.data_u32;
370 dst1 = ip1->dst_address.data_u32;
371 ip0->src_address.data_u32 = dst0;
372 ip1->src_address.data_u32 = dst1;
373 ip0->dst_address.data_u32 = src0;
374 ip1->dst_address.data_u32 = src1;
375
Dave Barach68b0fb02017-02-28 15:15:56 -0500376 src0 = tcp0->src;
377 src1 = tcp1->src;
378 dst0 = tcp0->dst;
379 dst1 = tcp1->dst;
380 tcp0->src = dst0;
381 tcp1->src = dst1;
382 tcp0->dst = src0;
383 tcp1->dst = src1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384}
385
386#endif /* included_ip4_packet_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500387
388/*
389 * fd.io coding-style-patch-verification: ON
390 *
391 * Local Variables:
392 * eval: (c-set-style "gnu")
393 * End:
394 */