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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * ip4/packet.h: ip4 packet format
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #ifndef included_ip4_packet_h
41 #define included_ip4_packet_h
43 #include <vnet/ip/ip_packet.h> /* for ip_csum_t */
44 #include <vnet/ip/tcp_packet.h> /* for tcp_header_t */
45 #include <vppinfra/byte_order.h> /* for clib_net_to_host_u16 */
47 /* IP4 address which can be accessed either as 4 bytes
48 or as a 32-bit number. */
58 /* IP address must be first for ip_interface_address_get_address() to work */
59 ip4_address_t ip4_addr;
64 ip4_addr_fib_init (ip4_address_fib_t * addr_fib, ip4_address_t * address,
67 memcpy (&addr_fib->ip4_addr, address, sizeof (addr_fib->ip4_addr));
68 addr_fib->fib_index = fib_index;
71 /* (src,dst) pair of addresses as found in packet header. */
73 ip4_address_t src, dst;
76 /* If address is a valid netmask, return length of mask. */
78 ip4_address_netmask_length (ip4_address_t * a)
82 for (i = 0; i < ARRAY_LEN (a->as_u8); i++)
86 case 0xff: result += 8; break;
87 case 0xfe: result += 7; goto done;
88 case 0xfc: result += 6; goto done;
89 case 0xf8: result += 5; goto done;
90 case 0xf0: result += 4; goto done;
91 case 0xe0: result += 3; goto done;
92 case 0xc0: result += 2; goto done;
93 case 0x80: result += 1; goto done;
94 case 0x00: result += 0; goto done;
96 /* Not a valid netmask mask. */
106 /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
107 e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
108 u8 ip_version_and_header_length;
110 /* Type of service. */
113 /* Total layer 3 packet length including this header. */
116 /* Fragmentation ID. */
119 /* 3 bits of flags and 13 bits of fragment offset (in units
120 of 8 byte quantities). */
121 u16 flags_and_fragment_offset;
122 #define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
123 #define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
124 #define IP4_HEADER_FLAG_CONGESTION (1 << 15)
126 /* Time to live decremented by router at each hop. */
129 /* Next level protocol packet. */
135 /* Source and destination address. */
138 ip4_address_t src_address, dst_address;
140 ip4_address_pair_t address_pair;
144 /* For checksumming we'll want to access IP header in word sized chunks. */
145 /* For 64 bit machines. */
146 CLIB_PACKED (struct {
147 u64 checksum_data_64[2];
148 u32 checksum_data_64_32[1];
151 /* For 32 bit machines. */
152 CLIB_PACKED (struct {
153 u32 checksum_data_32[5];
157 /* Value of ip_version_and_header_length for packets w/o options. */
158 #define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
159 ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
162 ip4_get_fragment_offset (ip4_header_t * i)
163 { return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff; }
166 ip4_get_fragment_more (ip4_header_t * i)
167 { return clib_net_to_host_u16 (i->flags_and_fragment_offset) & IP4_HEADER_FLAG_MORE_FRAGMENTS; }
170 ip4_is_fragment (ip4_header_t * i)
171 { return (i->flags_and_fragment_offset &
172 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)); }
175 ip4_is_first_fragment (ip4_header_t * i)
176 { return (i->flags_and_fragment_offset &
177 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
178 clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS); }
180 /* Fragment offset in bytes. */
182 ip4_get_fragment_offset_bytes (ip4_header_t * i)
183 { return 8 * ip4_get_fragment_offset (i); }
186 ip4_header_bytes (ip4_header_t * i)
187 { return sizeof (u32) * (i->ip_version_and_header_length & 0xf); }
190 ip4_next_header (ip4_header_t * i)
191 { return (void *) i + ip4_header_bytes (i); }
194 ip4_header_checksum (ip4_header_t * i)
201 sum = ip_incremental_checksum (0, i, ip4_header_bytes (i));
202 csum = ~ip_csum_fold (sum);
206 /* Make checksum agree for special case where either
207 0 or 0xffff would give same 1s complement sum. */
208 if (csum == 0 && save == 0xffff)
215 ip4_header_checksum_is_valid (ip4_header_t * i)
216 { return i->checksum == ip4_header_checksum (i); }
218 #define ip4_partial_header_checksum_x1(ip0,sum0) \
220 if (BITS (ip_csum_t) > 32) \
222 sum0 = ip0->checksum_data_64[0]; \
223 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
224 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
228 sum0 = ip0->checksum_data_32[0]; \
229 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
230 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
231 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
232 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
236 #define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
238 if (BITS (ip_csum_t) > 32) \
240 sum0 = ip0->checksum_data_64[0]; \
241 sum1 = ip1->checksum_data_64[0]; \
242 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
243 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
244 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
245 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
249 sum0 = ip0->checksum_data_32[0]; \
250 sum1 = ip1->checksum_data_32[0]; \
251 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
252 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
253 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
254 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
255 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
256 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
257 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
258 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
263 ip4_address_is_multicast (ip4_address_t * a)
264 { return (a->data[0] & 0xf0) == 0xe0; }
267 ip4_multicast_address_set_for_group (ip4_address_t * a, ip_multicast_group_t g)
269 ASSERT ((u32) g < (1 << 28));
270 a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
274 ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
278 src0 = ip0->src_address.data_u32;
279 dst0 = ip0->dst_address.data_u32;
280 ip0->src_address.data_u32 = dst0;
281 ip0->dst_address.data_u32 = src0;
283 src0 = tcp0->ports.src;
284 dst0 = tcp0->ports.dst;
285 tcp0->ports.src = dst0;
286 tcp0->ports.dst = src0;
290 ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
291 tcp_header_t * tcp0, tcp_header_t * tcp1)
293 u32 src0, dst0, src1, dst1;
295 src0 = ip0->src_address.data_u32;
296 src1 = ip1->src_address.data_u32;
297 dst0 = ip0->dst_address.data_u32;
298 dst1 = ip1->dst_address.data_u32;
299 ip0->src_address.data_u32 = dst0;
300 ip1->src_address.data_u32 = dst1;
301 ip0->dst_address.data_u32 = src0;
302 ip1->dst_address.data_u32 = src1;
304 src0 = tcp0->ports.src;
305 src1 = tcp1->ports.src;
306 dst0 = tcp0->ports.dst;
307 dst1 = tcp1->ports.dst;
308 tcp0->ports.src = dst0;
309 tcp1->ports.src = dst1;
310 tcp0->ports.dst = src0;
311 tcp1->ports.dst = src1;
314 #endif /* included_ip4_packet_h */