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 <vppinfra/byte_order.h> /* for clib_net_to_host_u16 */
45 #include <vppinfra/warnings.h> /* for WARN_OFF/WARN_ON macro */
47 /* IP4 address which can be accessed either as 4 bytes
48 or as a 32-bit number. */
61 /* IP address must be first for ip_interface_address_get_address() to work */
62 ip4_address_t ip4_addr;
67 ip4_addr_fib_init (ip4_address_fib_t * addr_fib,
68 const ip4_address_t * address, u32 fib_index)
70 clib_memcpy_fast (&addr_fib->ip4_addr, address,
71 sizeof (addr_fib->ip4_addr));
72 addr_fib->fib_index = fib_index;
75 /* (src,dst) pair of addresses as found in packet header. */
78 ip4_address_t src, dst;
83 ip4_address_t addr, mask;
84 } ip4_address_and_mask_t;
90 /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
91 e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
92 u8 ip_version_and_header_length;
94 /* Type of service. */
97 /* Total layer 3 packet length including this header. */
100 /* Fragmentation ID. */
103 /* 3 bits of flags and 13 bits of fragment offset (in units
104 of 8 byte quantities). */
105 u16 flags_and_fragment_offset;
106 #define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
107 #define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
108 #define IP4_HEADER_FLAG_CONGESTION (1 << 15)
110 /* Time to live decremented by router at each hop. */
113 /* Next level protocol packet. */
119 /* Source and destination address. */
124 ip4_address_t src_address, dst_address;
126 ip4_address_pair_t address_pair;
130 /* For checksumming we'll want to access IP header in word sized chunks. */
131 /* For 64 bit machines. */
132 CLIB_PACKED (struct {
133 u64 checksum_data_64[2];
134 u32 checksum_data_64_32[1];
137 /* For 32 bit machines. */
138 CLIB_PACKED (struct {
139 u32 checksum_data_32[5];
143 /* Value of ip_version_and_header_length for packets w/o options. */
144 #define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
145 ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
147 #define IP4_ROUTER_ALERT_OPTION 20
150 ip4_get_fragment_offset (const ip4_header_t * i)
152 return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff;
156 ip4_get_fragment_more (const ip4_header_t * i)
158 return clib_net_to_host_u16 (i->flags_and_fragment_offset) &
159 IP4_HEADER_FLAG_MORE_FRAGMENTS;
163 ip4_is_fragment (const ip4_header_t * i)
165 return (i->flags_and_fragment_offset &
166 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS));
170 ip4_is_first_fragment (const ip4_header_t * i)
172 return (i->flags_and_fragment_offset &
173 clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
174 clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
177 /* Fragment offset in bytes. */
179 ip4_get_fragment_offset_bytes (const ip4_header_t * i)
181 return 8 * ip4_get_fragment_offset (i);
185 ip4_header_bytes (const ip4_header_t * i)
187 return sizeof (u32) * (i->ip_version_and_header_length & 0xf);
191 ip4_next_header (ip4_header_t * i)
193 return (void *) i + ip4_header_bytes (i);
196 /* Turn off array bounds check due to ip4_header_t
197 option field operations. */
199 WARN_OFF(array-bounds)
201 static_always_inline u16
202 ip4_header_checksum_inline (ip4_header_t * i, int with_checksum)
204 int option_len = (i->ip_version_and_header_length & 0xf) - 5;
207 u32 *iphdr = (u32 *) i;
211 sum += with_checksum ? iphdr[2] : *(u16 *) (iphdr + 2);
216 if (PREDICT_FALSE (option_len > 0))
243 sum = ((u32) sum) + (sum >> 32);
245 u16 *iphdr = (u16 *) i;
259 if (PREDICT_FALSE (option_len > 0))
297 sum = ((u16) sum) + (sum >> 16);
298 sum = ((u16) sum) + (sum >> 16);
302 WARN_ON(array-bounds)
305 ip4_header_checksum (ip4_header_t * i)
307 return ip4_header_checksum_inline (i, /* with_checksum */ 0);
311 ip4_header_set_dscp (ip4_header_t * ip4, ip_dscp_t dscp)
314 /* not masking the dscp value to save th instruction
315 * it shouldn't b necessary since the argument is an enum
316 * whose range is therefore constrained in the CP. in the
317 * DP it will have been taken from another packet, so again
318 * constrained in value */
319 ip4->tos |= dscp << IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT;
323 ip4_header_set_ecn (ip4_header_t * ip4, ip_ecn_t ecn)
325 ip4->tos &= ~IP_PACKET_TC_FIELD_ECN_MASK;
330 ip4_header_set_ecn_w_chksum (ip4_header_t * ip4, ip_ecn_t ecn)
332 ip_csum_t sum = ip4->checksum;
334 u8 new = (old & ~IP_PACKET_TC_FIELD_ECN_MASK) | ecn;
336 sum = ip_csum_update (sum, old, new, ip4_header_t, tos);
337 ip4->checksum = ip_csum_fold (sum);
341 always_inline ip_dscp_t
342 ip4_header_get_dscp (const ip4_header_t * ip4)
344 return (ip4->tos >> IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT);
347 always_inline ip_ecn_t
348 ip4_header_get_ecn (const ip4_header_t * ip4)
350 return (ip4->tos & IP_PACKET_TC_FIELD_ECN_MASK);
354 ip4_header_get_ttl (const ip4_header_t *ip4)
360 ip4_header_set_ttl (ip4_header_t *ip4, u8 ttl)
366 ip4_header_set_df (ip4_header_t * ip4)
368 ip4->flags_and_fragment_offset |=
369 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
373 ip4_header_clear_df (ip4_header_t * ip4)
375 ip4->flags_and_fragment_offset &=
376 ~clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
380 ip4_header_get_df (const ip4_header_t * ip4)
382 return (! !(ip4->flags_and_fragment_offset &
383 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT)));
387 ip4_header_checksum_is_valid (ip4_header_t * i)
389 return ip4_header_checksum_inline (i, /* with_checksum */ 1) == 0;
392 #define ip4_partial_header_checksum_x1(ip0,sum0) \
394 if (BITS (ip_csum_t) > 32) \
396 sum0 = ip0->checksum_data_64[0]; \
397 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
398 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
402 sum0 = ip0->checksum_data_32[0]; \
403 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
404 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
405 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
406 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
410 #define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
412 if (BITS (ip_csum_t) > 32) \
414 sum0 = ip0->checksum_data_64[0]; \
415 sum1 = ip1->checksum_data_64[0]; \
416 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
417 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
418 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
419 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
423 sum0 = ip0->checksum_data_32[0]; \
424 sum1 = ip1->checksum_data_32[0]; \
425 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
426 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
427 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
428 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
429 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
430 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
431 sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
432 sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
437 ip4_address_is_multicast (const ip4_address_t * a)
439 return (a->data[0] & 0xf0) == 0xe0;
443 ip4_address_is_global_broadcast (const ip4_address_t * a)
445 return (a->as_u32) == 0xffffffff;
449 ip4_multicast_address_set_for_group (ip4_address_t * a,
450 ip_multicast_group_t g)
452 ASSERT ((u32) g < (1 << 28));
453 a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
457 ip4_multicast_ethernet_address (u8 * ethernet_address,
458 const ip4_address_t * a)
460 const u8 *d = a->as_u8;
462 ethernet_address[0] = 0x01;
463 ethernet_address[1] = 0x00;
464 ethernet_address[2] = 0x5e;
465 ethernet_address[3] = d[1] & 0x7f;
466 ethernet_address[4] = d[2];
467 ethernet_address[5] = d[3];
470 #endif /* included_ip4_packet_h */
473 * fd.io coding-style-patch-verification: ON
476 * eval: (c-set-style "gnu")