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/tcp/tcp_packet.h> /* for tcp_header_t */
45 #include <vppinfra/byte_order.h> /* for clib_net_to_host_u16 */
46 #include <vppinfra/warnings.h> /* for WARN_OFF/WARN_ON macro */
48 /* IP4 address which can be accessed either as 4 bytes
49 or as a 32-bit number. */
62 /* IP address must be first for ip_interface_address_get_address() to work */
63 ip4_address_t ip4_addr;
68 ip4_addr_fib_init (ip4_address_fib_t * addr_fib,
69 const ip4_address_t * address, u32 fib_index)
71 clib_memcpy_fast (&addr_fib->ip4_addr, address,
72 sizeof (addr_fib->ip4_addr));
73 addr_fib->fib_index = fib_index;
76 /* (src,dst) pair of addresses as found in packet header. */
79 ip4_address_t src, dst;
84 ip4_address_t addr, mask;
85 } ip4_address_and_mask_t;
91 /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
92 e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
93 u8 ip_version_and_header_length;
95 /* Type of service. */
98 /* Total layer 3 packet length including this header. */
101 /* Fragmentation ID. */
104 /* 3 bits of flags and 13 bits of fragment offset (in units
105 of 8 byte quantities). */
106 u16 flags_and_fragment_offset;
107 #define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
108 #define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
109 #define IP4_HEADER_FLAG_CONGESTION (1 << 15)
111 /* Time to live decremented by router at each hop. */
114 /* Next level protocol packet. */
120 /* Source and destination address. */
125 ip4_address_t src_address, dst_address;
127 ip4_address_pair_t address_pair;
131 /* For checksumming we'll want to access IP header in word sized chunks. */
132 /* For 64 bit machines. */
134 CLIB_PACKED (struct {
135 u64 checksum_data_64[2];
136 u32 checksum_data_64_32[1];
140 /* For 32 bit machines. */
142 CLIB_PACKED (struct {
143 u32 checksum_data_32[5];
148 /* Value of ip_version_and_header_length for packets w/o options. */
149 #define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
150 ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
152 #define IP4_ROUTER_ALERT_OPTION 20
155 ip4_get_fragment_offset (const ip4_header_t * i)
157 return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff;
161 ip4_get_fragment_more (const ip4_header_t * i)
163 return clib_net_to_host_u16 (i->flags_and_fragment_offset) &
164 IP4_HEADER_FLAG_MORE_FRAGMENTS;
168 ip4_is_fragment (const ip4_header_t * i)
170 return (i->flags_and_fragment_offset &
171 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS));
175 ip4_is_first_fragment (const ip4_header_t * i)
177 return (i->flags_and_fragment_offset &
178 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
179 clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
182 /* Fragment offset in bytes. */
184 ip4_get_fragment_offset_bytes (const ip4_header_t * i)
186 return 8 * ip4_get_fragment_offset (i);
190 ip4_header_bytes (const ip4_header_t * i)
192 return sizeof (u32) * (i->ip_version_and_header_length & 0xf);
196 ip4_next_header (ip4_header_t * i)
198 return (void *) i + ip4_header_bytes (i);
201 /* Turn off array bounds check due to ip4_header_t
202 option field operations. */
205 WARN_OFF(array-bounds)
209 ip4_header_checksum (ip4_header_t * i)
211 int option_len = (i->ip_version_and_header_length & 0xf) - 5;
214 u32 *iphdr = (u32 *) i;
218 sum += *(u16 *) (iphdr + 2);
223 if (PREDICT_FALSE (option_len > 0))
250 sum = ((u32) sum) + (sum >> 32);
252 u16 *iphdr = (u16 *) i;
265 if (PREDICT_FALSE (option_len > 0))
303 sum = ((u16) sum) + (sum >> 16);
304 sum = ((u16) sum) + (sum >> 16);
309 WARN_ON(array-bounds)
313 ip4_header_set_dscp (ip4_header_t * ip4, ip_dscp_t dscp)
316 /* not masking the dscp value to save th instruction
317 * it shouldn't b necessary since the argument is an enum
318 * whose range is therefore constrained in the CP. in the
319 * DP it will have been taken from another packet, so again
320 * constrained in value */
321 ip4->tos |= dscp << IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT;
325 ip4_header_set_ecn (ip4_header_t * ip4, ip_ecn_t ecn)
327 ip4->tos &= ~IP_PACKET_TC_FIELD_ECN_MASK;
332 ip4_header_set_ecn_w_chksum (ip4_header_t * ip4, ip_ecn_t ecn)
334 ip_csum_t sum = ip4->checksum;
336 u8 new = (old & ~IP_PACKET_TC_FIELD_ECN_MASK) | ecn;
338 sum = ip_csum_update (sum, old, new, ip4_header_t, tos);
339 ip4->checksum = ip_csum_fold (sum);
343 always_inline ip_dscp_t
344 ip4_header_get_dscp (const ip4_header_t * ip4)
346 return (ip4->tos >> IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT);
349 always_inline ip_ecn_t
350 ip4_header_get_ecn (const ip4_header_t * ip4)
352 return (ip4->tos & IP_PACKET_TC_FIELD_ECN_MASK);
356 ip4_header_set_df (ip4_header_t * ip4)
358 ip4->flags_and_fragment_offset |=
359 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
363 ip4_header_clear_df (ip4_header_t * ip4)
365 ip4->flags_and_fragment_offset &=
366 ~clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
370 ip4_header_get_df (const ip4_header_t * ip4)
372 return (! !(ip4->flags_and_fragment_offset &
373 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT)));
377 ip4_header_checksum_is_valid (ip4_header_t * i)
379 return i->checksum == ip4_header_checksum (i);
382 #define ip4_partial_header_checksum_x1(ip0,sum0) \
384 if (BITS (ip_csum_t) > 32) \
386 sum0 = ip0->checksum_data_64[0]; \
387 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
388 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
392 sum0 = ip0->checksum_data_32[0]; \
393 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
394 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
395 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
396 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
400 #define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
402 if (BITS (ip_csum_t) > 32) \
404 sum0 = ip0->checksum_data_64[0]; \
405 sum1 = ip1->checksum_data_64[0]; \
406 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
407 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
408 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
409 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
413 sum0 = ip0->checksum_data_32[0]; \
414 sum1 = ip1->checksum_data_32[0]; \
415 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
416 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
417 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
418 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
419 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
420 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
421 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
422 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
427 ip4_address_is_multicast (const ip4_address_t * a)
429 return (a->data[0] & 0xf0) == 0xe0;
433 ip4_address_is_global_broadcast (const ip4_address_t * a)
435 return (a->as_u32) == 0xffffffff;
439 ip4_multicast_address_set_for_group (ip4_address_t * a,
440 ip_multicast_group_t g)
442 ASSERT ((u32) g < (1 << 28));
443 a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
447 ip4_multicast_ethernet_address (u8 * ethernet_address,
448 const ip4_address_t * a)
450 const u8 *d = a->as_u8;
452 ethernet_address[0] = 0x01;
453 ethernet_address[1] = 0x00;
454 ethernet_address[2] = 0x5e;
455 ethernet_address[3] = d[1] & 0x7f;
456 ethernet_address[4] = d[2];
457 ethernet_address[5] = d[3];
461 ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
465 src0 = ip0->src_address.data_u32;
466 dst0 = ip0->dst_address.data_u32;
467 ip0->src_address.data_u32 = dst0;
468 ip0->dst_address.data_u32 = src0;
477 ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
478 tcp_header_t * tcp0, tcp_header_t * tcp1)
480 u32 src0, dst0, src1, dst1;
482 src0 = ip0->src_address.data_u32;
483 src1 = ip1->src_address.data_u32;
484 dst0 = ip0->dst_address.data_u32;
485 dst1 = ip1->dst_address.data_u32;
486 ip0->src_address.data_u32 = dst0;
487 ip1->src_address.data_u32 = dst1;
488 ip0->dst_address.data_u32 = src0;
489 ip1->dst_address.data_u32 = src1;
501 #endif /* included_ip4_packet_h */
504 * fd.io coding-style-patch-verification: ON
507 * eval: (c-set-style "gnu")