blob: 1ff9fbdba13f5831f563f29fb0d5f0dcc5bb4834 [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
80/* If address is a valid netmask, return length of mask. */
81always_inline uword
82ip4_address_netmask_length (ip4_address_t * a)
83{
84 uword result = 0;
85 uword i;
86 for (i = 0; i < ARRAY_LEN (a->as_u8); i++)
87 {
88 switch (a->as_u8[i])
89 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050090 case 0xff:
91 result += 8;
92 break;
93 case 0xfe:
94 result += 7;
95 goto done;
96 case 0xfc:
97 result += 6;
98 goto done;
99 case 0xf8:
100 result += 5;
101 goto done;
102 case 0xf0:
103 result += 4;
104 goto done;
105 case 0xe0:
106 result += 3;
107 goto done;
108 case 0xc0:
109 result += 2;
110 goto done;
111 case 0x80:
112 result += 1;
113 goto done;
114 case 0x00:
115 result += 0;
116 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 default:
118 /* Not a valid netmask mask. */
119 return ~0;
120 }
121 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500122done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123 return result;
124}
125
Dave Barachd7cb1b52016-12-09 09:52:16 -0500126typedef union
127{
128 struct
129 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
131 e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
132 u8 ip_version_and_header_length;
133
134 /* Type of service. */
135 u8 tos;
136
137 /* Total layer 3 packet length including this header. */
138 u16 length;
139
140 /* Fragmentation ID. */
141 u16 fragment_id;
142
143 /* 3 bits of flags and 13 bits of fragment offset (in units
144 of 8 byte quantities). */
145 u16 flags_and_fragment_offset;
146#define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
147#define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
148#define IP4_HEADER_FLAG_CONGESTION (1 << 15)
149
150 /* Time to live decremented by router at each hop. */
151 u8 ttl;
152
153 /* Next level protocol packet. */
154 u8 protocol;
155
156 /* Checksum. */
157 u16 checksum;
158
159 /* Source and destination address. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500160 union
161 {
162 struct
163 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 ip4_address_t src_address, dst_address;
165 };
166 ip4_address_pair_t address_pair;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500167 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 };
169
170 /* For checksumming we'll want to access IP header in word sized chunks. */
171 /* For 64 bit machines. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500172 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 CLIB_PACKED (struct {
174 u64 checksum_data_64[2];
175 u32 checksum_data_64_32[1];
176 });
Dave Barachd7cb1b52016-12-09 09:52:16 -0500177 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
179 /* For 32 bit machines. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500180 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 CLIB_PACKED (struct {
182 u32 checksum_data_32[5];
183 });
Dave Barachd7cb1b52016-12-09 09:52:16 -0500184 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185} ip4_header_t;
186
187/* Value of ip_version_and_header_length for packets w/o options. */
188#define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
189 ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
190
191always_inline int
192ip4_get_fragment_offset (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500193{
194 return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff;
195}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
197always_inline int
198ip4_get_fragment_more (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500199{
200 return clib_net_to_host_u16 (i->flags_and_fragment_offset) &
201 IP4_HEADER_FLAG_MORE_FRAGMENTS;
202}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
204always_inline int
205ip4_is_fragment (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500206{
207 return (i->flags_and_fragment_offset &
208 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS));
209}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
211always_inline int
212ip4_is_first_fragment (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500213{
214 return (i->flags_and_fragment_offset &
215 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
216 clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
217}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
219/* Fragment offset in bytes. */
220always_inline int
221ip4_get_fragment_offset_bytes (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500222{
223 return 8 * ip4_get_fragment_offset (i);
224}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
226always_inline int
227ip4_header_bytes (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228{
229 return sizeof (u32) * (i->ip_version_and_header_length & 0xf);
230}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231
232always_inline void *
233ip4_next_header (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500234{
235 return (void *) i + ip4_header_bytes (i);
236}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
238always_inline u16
239ip4_header_checksum (ip4_header_t * i)
240{
241 u16 save, csum;
242 ip_csum_t sum;
243
244 save = i->checksum;
245 i->checksum = 0;
246 sum = ip_incremental_checksum (0, i, ip4_header_bytes (i));
247 csum = ~ip_csum_fold (sum);
248
249 i->checksum = save;
250
251 /* Make checksum agree for special case where either
252 0 or 0xffff would give same 1s complement sum. */
253 if (csum == 0 && save == 0xffff)
254 csum = save;
255
256 return csum;
257}
258
259static inline uword
260ip4_header_checksum_is_valid (ip4_header_t * i)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500261{
262 return i->checksum == ip4_header_checksum (i);
263}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
265#define ip4_partial_header_checksum_x1(ip0,sum0) \
266do { \
267 if (BITS (ip_csum_t) > 32) \
268 { \
269 sum0 = ip0->checksum_data_64[0]; \
270 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
271 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
272 } \
273 else \
274 { \
275 sum0 = ip0->checksum_data_32[0]; \
276 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
277 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
278 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
279 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
280 } \
281} while (0)
282
283#define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
284do { \
285 if (BITS (ip_csum_t) > 32) \
286 { \
287 sum0 = ip0->checksum_data_64[0]; \
288 sum1 = ip1->checksum_data_64[0]; \
289 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
290 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
291 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
292 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
293 } \
294 else \
295 { \
296 sum0 = ip0->checksum_data_32[0]; \
297 sum1 = ip1->checksum_data_32[0]; \
298 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
299 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
300 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
301 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
302 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
303 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
304 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
305 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
306 } \
307} while (0)
308
309always_inline uword
310ip4_address_is_multicast (ip4_address_t * a)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500311{
312 return (a->data[0] & 0xf0) == 0xe0;
313}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
315always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500316ip4_multicast_address_set_for_group (ip4_address_t * a,
317 ip_multicast_group_t g)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318{
Damjan Marion2c29d752015-12-18 10:26:56 +0100319 ASSERT ((u32) g < (1 << 28));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
321}
322
323always_inline void
Eyal Baric5b13602016-11-24 19:42:43 +0200324ip4_multicast_ethernet_address (u8 * ethernet_address, ip4_address_t * a)
325{
326 u8 *d = a->as_u8;
327
328 ethernet_address[0] = 0x01;
329 ethernet_address[1] = 0x00;
330 ethernet_address[2] = 0x5e;
331 ethernet_address[3] = d[1] & 0x7f;
332 ethernet_address[4] = d[2];
333 ethernet_address[5] = d[3];
334}
335
336always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
338{
339 u32 src0, dst0;
340
341 src0 = ip0->src_address.data_u32;
342 dst0 = ip0->dst_address.data_u32;
343 ip0->src_address.data_u32 = dst0;
344 ip0->dst_address.data_u32 = src0;
345
Dave Barach68b0fb02017-02-28 15:15:56 -0500346 src0 = tcp0->src;
347 dst0 = tcp0->dst;
348 tcp0->src = dst0;
349 tcp0->dst = src0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350}
351
352always_inline void
353ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
354 tcp_header_t * tcp0, tcp_header_t * tcp1)
355{
356 u32 src0, dst0, src1, dst1;
357
358 src0 = ip0->src_address.data_u32;
359 src1 = ip1->src_address.data_u32;
360 dst0 = ip0->dst_address.data_u32;
361 dst1 = ip1->dst_address.data_u32;
362 ip0->src_address.data_u32 = dst0;
363 ip1->src_address.data_u32 = dst1;
364 ip0->dst_address.data_u32 = src0;
365 ip1->dst_address.data_u32 = src1;
366
Dave Barach68b0fb02017-02-28 15:15:56 -0500367 src0 = tcp0->src;
368 src1 = tcp1->src;
369 dst0 = tcp0->dst;
370 dst1 = tcp1->dst;
371 tcp0->src = dst0;
372 tcp1->src = dst1;
373 tcp0->dst = src0;
374 tcp1->dst = src1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375}
376
377#endif /* included_ip4_packet_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500378
379/*
380 * fd.io coding-style-patch-verification: ON
381 *
382 * Local Variables:
383 * eval: (c-set-style "gnu")
384 * End:
385 */