blob: 2f0c75e4924e9dcb6b40b78cc6283b421991a3ce [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
67ip4_addr_fib_init (ip4_address_fib_t * addr_fib, ip4_address_t * address,
68 u32 fib_index)
69{
Damjan Marionf1213b82016-03-13 02:22:06 +010070 clib_memcpy (&addr_fib->ip4_addr, address, sizeof (addr_fib->ip4_addr));
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 addr_fib->fib_index = fib_index;
72}
73
74/* (src,dst) pair of addresses as found in packet header. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050075typedef struct
76{
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 ip4_address_t src, dst;
78} ip4_address_pair_t;
79
Damjan Mariona35cc142018-03-16 01:25:27 +010080typedef struct
81{
82 ip4_address_t addr, mask;
83} ip4_address_and_mask_t;
84
Ed Warnickecb9cada2015-12-08 15:45:58 -070085/* If address is a valid netmask, return length of mask. */
86always_inline uword
87ip4_address_netmask_length (ip4_address_t * a)
88{
89 uword result = 0;
90 uword i;
91 for (i = 0; i < ARRAY_LEN (a->as_u8); i++)
92 {
93 switch (a->as_u8[i])
94 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050095 case 0xff:
96 result += 8;
97 break;
98 case 0xfe:
99 result += 7;
100 goto done;
101 case 0xfc:
102 result += 6;
103 goto done;
104 case 0xf8:
105 result += 5;
106 goto done;
107 case 0xf0:
108 result += 4;
109 goto done;
110 case 0xe0:
111 result += 3;
112 goto done;
113 case 0xc0:
114 result += 2;
115 goto done;
116 case 0x80:
117 result += 1;
118 goto done;
119 case 0x00:
120 result += 0;
121 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 default:
123 /* Not a valid netmask mask. */
124 return ~0;
125 }
126 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500127done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 return result;
129}
130
Dave Barachd7cb1b52016-12-09 09:52:16 -0500131typedef union
132{
133 struct
134 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
136 e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
137 u8 ip_version_and_header_length;
138
139 /* Type of service. */
140 u8 tos;
141
142 /* Total layer 3 packet length including this header. */
143 u16 length;
144
145 /* Fragmentation ID. */
146 u16 fragment_id;
147
148 /* 3 bits of flags and 13 bits of fragment offset (in units
149 of 8 byte quantities). */
150 u16 flags_and_fragment_offset;
151#define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
152#define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
153#define IP4_HEADER_FLAG_CONGESTION (1 << 15)
154
155 /* Time to live decremented by router at each hop. */
156 u8 ttl;
157
158 /* Next level protocol packet. */
159 u8 protocol;
160
161 /* Checksum. */
162 u16 checksum;
163
164 /* Source and destination address. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500165 union
166 {
167 struct
168 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 ip4_address_t src_address, dst_address;
170 };
171 ip4_address_pair_t address_pair;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500172 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 };
174
175 /* For checksumming we'll want to access IP header in word sized chunks. */
176 /* For 64 bit machines. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500177 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 CLIB_PACKED (struct {
179 u64 checksum_data_64[2];
180 u32 checksum_data_64_32[1];
181 });
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
184 /* For 32 bit machines. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500185 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 CLIB_PACKED (struct {
187 u32 checksum_data_32[5];
188 });
Dave Barachd7cb1b52016-12-09 09:52:16 -0500189 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190} ip4_header_t;
191
192/* Value of ip_version_and_header_length for packets w/o options. */
193#define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
194 ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
195
196always_inline int
197ip4_get_fragment_offset (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500198{
199 return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff;
200}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
202always_inline int
203ip4_get_fragment_more (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500204{
205 return clib_net_to_host_u16 (i->flags_and_fragment_offset) &
206 IP4_HEADER_FLAG_MORE_FRAGMENTS;
207}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
209always_inline int
210ip4_is_fragment (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500211{
212 return (i->flags_and_fragment_offset &
213 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS));
214}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
216always_inline int
217ip4_is_first_fragment (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500218{
219 return (i->flags_and_fragment_offset &
220 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
221 clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
222}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
224/* Fragment offset in bytes. */
225always_inline int
226ip4_get_fragment_offset_bytes (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500227{
228 return 8 * ip4_get_fragment_offset (i);
229}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
231always_inline int
232ip4_header_bytes (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233{
234 return sizeof (u32) * (i->ip_version_and_header_length & 0xf);
235}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
237always_inline void *
238ip4_next_header (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500239{
240 return (void *) i + ip4_header_bytes (i);
241}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
243always_inline u16
244ip4_header_checksum (ip4_header_t * i)
245{
246 u16 save, csum;
247 ip_csum_t sum;
248
249 save = i->checksum;
250 i->checksum = 0;
251 sum = ip_incremental_checksum (0, i, ip4_header_bytes (i));
252 csum = ~ip_csum_fold (sum);
253
254 i->checksum = save;
255
256 /* Make checksum agree for special case where either
257 0 or 0xffff would give same 1s complement sum. */
258 if (csum == 0 && save == 0xffff)
259 csum = save;
260
261 return csum;
262}
263
264static inline uword
265ip4_header_checksum_is_valid (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500266{
267 return i->checksum == ip4_header_checksum (i);
268}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
270#define ip4_partial_header_checksum_x1(ip0,sum0) \
271do { \
272 if (BITS (ip_csum_t) > 32) \
273 { \
274 sum0 = ip0->checksum_data_64[0]; \
275 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
276 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
277 } \
278 else \
279 { \
280 sum0 = ip0->checksum_data_32[0]; \
281 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
282 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
283 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
284 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
285 } \
286} while (0)
287
288#define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
289do { \
290 if (BITS (ip_csum_t) > 32) \
291 { \
292 sum0 = ip0->checksum_data_64[0]; \
293 sum1 = ip1->checksum_data_64[0]; \
294 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
295 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
296 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
297 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
298 } \
299 else \
300 { \
301 sum0 = ip0->checksum_data_32[0]; \
302 sum1 = ip1->checksum_data_32[0]; \
303 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
304 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
305 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
306 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
307 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
308 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
309 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
310 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
311 } \
312} while (0)
313
314always_inline uword
315ip4_address_is_multicast (ip4_address_t * a)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500316{
317 return (a->data[0] & 0xf0) == 0xe0;
318}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
320always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500321ip4_multicast_address_set_for_group (ip4_address_t * a,
322 ip_multicast_group_t g)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323{
Damjan Marion2c29d752015-12-18 10:26:56 +0100324 ASSERT ((u32) g < (1 << 28));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
326}
327
328always_inline void
Eyal Baric5b13602016-11-24 19:42:43 +0200329ip4_multicast_ethernet_address (u8 * ethernet_address, ip4_address_t * a)
330{
331 u8 *d = a->as_u8;
332
333 ethernet_address[0] = 0x01;
334 ethernet_address[1] = 0x00;
335 ethernet_address[2] = 0x5e;
336 ethernet_address[3] = d[1] & 0x7f;
337 ethernet_address[4] = d[2];
338 ethernet_address[5] = d[3];
339}
340
341always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
343{
344 u32 src0, dst0;
345
346 src0 = ip0->src_address.data_u32;
347 dst0 = ip0->dst_address.data_u32;
348 ip0->src_address.data_u32 = dst0;
349 ip0->dst_address.data_u32 = src0;
350
Dave Barach68b0fb02017-02-28 15:15:56 -0500351 src0 = tcp0->src;
352 dst0 = tcp0->dst;
353 tcp0->src = dst0;
354 tcp0->dst = src0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355}
356
357always_inline void
358ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
359 tcp_header_t * tcp0, tcp_header_t * tcp1)
360{
361 u32 src0, dst0, src1, dst1;
362
363 src0 = ip0->src_address.data_u32;
364 src1 = ip1->src_address.data_u32;
365 dst0 = ip0->dst_address.data_u32;
366 dst1 = ip1->dst_address.data_u32;
367 ip0->src_address.data_u32 = dst0;
368 ip1->src_address.data_u32 = dst1;
369 ip0->dst_address.data_u32 = src0;
370 ip1->dst_address.data_u32 = src1;
371
Dave Barach68b0fb02017-02-28 15:15:56 -0500372 src0 = tcp0->src;
373 src1 = tcp1->src;
374 dst0 = tcp0->dst;
375 dst1 = tcp1->dst;
376 tcp0->src = dst0;
377 tcp1->src = dst1;
378 tcp0->dst = src0;
379 tcp1->dst = src1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380}
381
382#endif /* included_ip4_packet_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500383
384/*
385 * fd.io coding-style-patch-verification: ON
386 *
387 * Local Variables:
388 * eval: (c-set-style "gnu")
389 * End:
390 */