FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
ip4_packet.h
Go to the documentation of this file.
1 /*
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 */
44 #include <vnet/tcp/tcp_packet.h> /* for tcp_header_t */
45 #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. */
49 typedef union
50 {
51  u8 data[4];
53  /* Aliases. */
54  u8 as_u8[4];
55  u16 as_u16[2];
58 
59 typedef struct
60 {
61  /* IP address must be first for ip_interface_address_get_address() to work */
65 
66 always_inline void
68  const ip4_address_t * address, u32 fib_index)
69 {
70  clib_memcpy_fast (&addr_fib->ip4_addr, address,
71  sizeof (addr_fib->ip4_addr));
72  addr_fib->fib_index = fib_index;
73 }
74 
75 /* (src,dst) pair of addresses as found in packet header. */
76 typedef struct
77 {
80 
81 typedef struct
82 {
85 
86 /* If address is a valid netmask, return length of mask. */
89 {
90  uword result = 0;
91  uword i;
92  for (i = 0; i < ARRAY_LEN (a->as_u8); i++)
93  {
94  switch (a->as_u8[i])
95  {
96  case 0xff:
97  result += 8;
98  break;
99  case 0xfe:
100  result += 7;
101  goto done;
102  case 0xfc:
103  result += 6;
104  goto done;
105  case 0xf8:
106  result += 5;
107  goto done;
108  case 0xf0:
109  result += 4;
110  goto done;
111  case 0xe0:
112  result += 3;
113  goto done;
114  case 0xc0:
115  result += 2;
116  goto done;
117  case 0x80:
118  result += 1;
119  goto done;
120  case 0x00:
121  result += 0;
122  goto done;
123  default:
124  /* Not a valid netmask mask. */
125  return ~0;
126  }
127  }
128 done:
129  return result;
130 }
131 
132 typedef union
133 {
134  struct
135  {
136  /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
137  e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
139 
140  /* Type of service. */
142 
143  /* Total layer 3 packet length including this header. */
145 
146  /* Fragmentation ID. */
148 
149  /* 3 bits of flags and 13 bits of fragment offset (in units
150  of 8 byte quantities). */
152 #define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
153 #define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
154 #define IP4_HEADER_FLAG_CONGESTION (1 << 15)
155 
156  /* Time to live decremented by router at each hop. */
158 
159  /* Next level protocol packet. */
161 
162  /* Checksum. */
164 
165  /* Source and destination address. */
166  union
167  {
168  struct
169  {
171  };
173  };
174  };
175 
176  /* For checksumming we'll want to access IP header in word sized chunks. */
177  /* For 64 bit machines. */
178  /* *INDENT-OFF* */
179  CLIB_PACKED (struct {
180  u64 checksum_data_64[2];
181  u32 checksum_data_64_32[1];
182  });
183  /* *INDENT-ON* */
184 
185  /* For 32 bit machines. */
186  /* *INDENT-OFF* */
187  CLIB_PACKED (struct {
188  u32 checksum_data_32[5];
189  });
190  /* *INDENT-ON* */
191 } ip4_header_t;
192 
193 /* Value of ip_version_and_header_length for packets w/o options. */
194 #define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
195  ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
196 
197 #define IP4_ROUTER_ALERT_OPTION 20
198 
200 ip4_get_fragment_offset (const ip4_header_t * i)
201 {
202  return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff;
203 }
204 
206 ip4_get_fragment_more (const ip4_header_t * i)
207 {
208  return clib_net_to_host_u16 (i->flags_and_fragment_offset) &
210 }
211 
212 always_inline int
213 ip4_is_fragment (const ip4_header_t * i)
214 {
215  return (i->flags_and_fragment_offset &
216  clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS));
217 }
218 
219 always_inline int
220 ip4_is_first_fragment (const ip4_header_t * i)
221 {
222  return (i->flags_and_fragment_offset &
223  clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
224  clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
225 }
226 
227 /* Fragment offset in bytes. */
228 always_inline int
229 ip4_get_fragment_offset_bytes (const ip4_header_t * i)
230 {
231  return 8 * ip4_get_fragment_offset (i);
232 }
233 
234 always_inline int
235 ip4_header_bytes (const ip4_header_t * i)
236 {
237  return sizeof (u32) * (i->ip_version_and_header_length & 0xf);
238 }
239 
240 always_inline void *
241 ip4_next_header (ip4_header_t * i)
242 {
243  return (void *) i + ip4_header_bytes (i);
244 }
245 
247 ip4_header_checksum (ip4_header_t * i)
248 {
249  u16 save, csum;
250  ip_csum_t sum;
251 
252  save = i->checksum;
253  i->checksum = 0;
254  sum = ip_incremental_checksum (0, i, ip4_header_bytes (i));
255  csum = ~ip_csum_fold (sum);
256 
257  i->checksum = save;
258 
259  /* Make checksum agree for special case where either
260  0 or 0xffff would give same 1s complement sum. */
261  if (csum == 0 && save == 0xffff)
262  csum = save;
263 
264  return csum;
265 }
266 
267 always_inline void
269 {
270  ip4->tos &= ~0xfc;
271  /* not masking the dscp value to save th instruction
272  * it shouldn't b necessary since the argument is an enum
273  * whose range is therefore constrained in the CP. in the
274  * DP it will have been taken from another packet, so again
275  * constrained in value */
276  ip4->tos |= dscp << IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT;
277 }
278 
279 always_inline void
280 ip4_header_set_ecn (ip4_header_t * ip4, ip_ecn_t ecn)
281 {
283  ip4->tos |= ecn;
284 }
285 
286 always_inline void
288 {
289  ip_csum_t sum = ip4->checksum;
290  u8 old = ip4->tos;
291  u8 new = (old & ~IP_PACKET_TC_FIELD_ECN_MASK) | ecn;
292 
293  sum = ip_csum_update (sum, old, new, ip4_header_t, tos);
294  ip4->checksum = ip_csum_fold (sum);
295  ip4->tos = new;
296 }
297 
299 ip4_header_get_dscp (const ip4_header_t * ip4)
300 {
301  return (ip4->tos >> IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT);
302 }
303 
305 ip4_header_get_ecn (const ip4_header_t * ip4)
306 {
307  return (ip4->tos & IP_PACKET_TC_FIELD_ECN_MASK);
308 }
309 
310 always_inline void
311 ip4_header_set_df (ip4_header_t * ip4)
312 {
314  clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
315 }
316 
317 always_inline void
318 ip4_header_clear_df (ip4_header_t * ip4)
319 {
321  ~clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
322 }
323 
325 ip4_header_get_df (ip4_header_t * ip4)
326 {
327  return (! !(ip4->flags_and_fragment_offset &
328  clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT)));
329 }
330 
331 static inline uword
333 {
334  return i->checksum == ip4_header_checksum (i);
335 }
336 
337 #define ip4_partial_header_checksum_x1(ip0,sum0) \
338 do { \
339  if (BITS (ip_csum_t) > 32) \
340  { \
341  sum0 = ip0->checksum_data_64[0]; \
342  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
343  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
344  } \
345  else \
346  { \
347  sum0 = ip0->checksum_data_32[0]; \
348  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
349  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
350  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
351  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
352  } \
353 } while (0)
354 
355 #define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
356 do { \
357  if (BITS (ip_csum_t) > 32) \
358  { \
359  sum0 = ip0->checksum_data_64[0]; \
360  sum1 = ip1->checksum_data_64[0]; \
361  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
362  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
363  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
364  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
365  } \
366  else \
367  { \
368  sum0 = ip0->checksum_data_32[0]; \
369  sum1 = ip1->checksum_data_32[0]; \
370  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
371  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
372  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
373  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
374  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
375  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
376  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
377  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
378  } \
379 } while (0)
380 
383 {
384  return (a->data[0] & 0xf0) == 0xe0;
385 }
386 
387 always_inline void
390 {
391  ASSERT ((u32) g < (1 << 28));
392  a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
393 }
394 
395 always_inline void
396 ip4_multicast_ethernet_address (u8 * ethernet_address,
397  const ip4_address_t * a)
398 {
399  const u8 *d = a->as_u8;
400 
401  ethernet_address[0] = 0x01;
402  ethernet_address[1] = 0x00;
403  ethernet_address[2] = 0x5e;
404  ethernet_address[3] = d[1] & 0x7f;
405  ethernet_address[4] = d[2];
406  ethernet_address[5] = d[3];
407 }
408 
409 always_inline void
410 ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
411 {
412  u32 src0, dst0;
413 
414  src0 = ip0->src_address.data_u32;
415  dst0 = ip0->dst_address.data_u32;
416  ip0->src_address.data_u32 = dst0;
417  ip0->dst_address.data_u32 = src0;
418 
419  src0 = tcp0->src;
420  dst0 = tcp0->dst;
421  tcp0->src = dst0;
422  tcp0->dst = src0;
423 }
424 
425 always_inline void
426 ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
427  tcp_header_t * tcp0, tcp_header_t * tcp1)
428 {
429  u32 src0, dst0, src1, dst1;
430 
431  src0 = ip0->src_address.data_u32;
432  src1 = ip1->src_address.data_u32;
433  dst0 = ip0->dst_address.data_u32;
434  dst1 = ip1->dst_address.data_u32;
435  ip0->src_address.data_u32 = dst0;
436  ip1->src_address.data_u32 = dst1;
437  ip0->dst_address.data_u32 = src0;
438  ip1->dst_address.data_u32 = src1;
439 
440  src0 = tcp0->src;
441  src1 = tcp1->src;
442  dst0 = tcp0->dst;
443  dst1 = tcp1->dst;
444  tcp0->src = dst0;
445  tcp1->src = dst1;
446  tcp0->dst = src0;
447  tcp1->dst = src1;
448 }
449 
450 #endif /* included_ip4_packet_h */
451 
452 /*
453  * fd.io coding-style-patch-verification: ON
454  *
455  * Local Variables:
456  * eval: (c-set-style "gnu")
457  * End:
458  */
ip4_address_t ip4_addr
Definition: ip4_packet.h:62
a
Definition: bitmap.h:538
ip4_address_t src_address
Definition: ip4_packet.h:170
static ip_dscp_t ip4_header_get_dscp(const ip4_header_t *ip4)
Definition: ip4_packet.h:299
unsigned long u64
Definition: types.h:89
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static void ip4_header_set_ecn_w_chksum(ip4_header_t *ip4, ip_ecn_t ecn)
Definition: ip4_packet.h:287
int i
enum ip_ecn_t_ ip_ecn_t
uword ip_csum_t
Definition: ip_packet.h:244
static void ip4_header_set_dscp(ip4_header_t *ip4, ip_dscp_t dscp)
Definition: ip4_packet.h:268
u16 flags_and_fragment_offset
Definition: ip4_packet.h:151
#define IP_PACKET_TC_FIELD_ECN_MASK
Definition: ip_packet.h:128
static uword ip4_address_is_multicast(const ip4_address_t *a)
Definition: ip4_packet.h:382
struct _tcp_header tcp_header_t
vhost_vring_addr_t addr
Definition: vhost_user.h:147
static void ip4_header_clear_df(ip4_header_t *ip4)
Definition: ip4_packet.h:318
static void ip4_tcp_reply_x1(ip4_header_t *ip0, tcp_header_t *tcp0)
Definition: ip4_packet.h:410
unsigned char u8
Definition: types.h:56
static int ip4_get_fragment_offset_bytes(const ip4_header_t *i)
Definition: ip4_packet.h:229
static u8 ip4_header_get_df(ip4_header_t *ip4)
Definition: ip4_packet.h:325
static int ip4_is_fragment(const ip4_header_t *i)
Definition: ip4_packet.h:213
static uword ip4_header_checksum_is_valid(ip4_header_t *i)
Definition: ip4_packet.h:332
static u16 ip4_get_fragment_more(const ip4_header_t *i)
Definition: ip4_packet.h:206
ip4_address_t dst_address
Definition: ip4_packet.h:170
#define IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT
IP DSCP bit shift The ECN occupies the 2 least significant bits of the TC field.
Definition: ip_packet.h:127
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:241
unsigned int u32
Definition: types.h:88
vl_api_ip_dscp_t dscp
Definition: dhcp.api:163
static void ip4_header_set_ecn(ip4_header_t *ip4, ip_ecn_t ecn)
Definition: ip4_packet.h:280
static void ip4_header_set_df(ip4_header_t *ip4)
Definition: ip4_packet.h:311
unsigned short u16
Definition: types.h:57
ip4_address_pair_t address_pair
Definition: ip4_packet.h:172
#define always_inline
Definition: ipsec.h:28
static void ip4_tcp_reply_x2(ip4_header_t *ip0, ip4_header_t *ip1, tcp_header_t *tcp0, tcp_header_t *tcp1)
Definition: ip4_packet.h:426
vl_api_address_t dst
Definition: gre.api:61
#define IP4_HEADER_FLAG_MORE_FRAGMENTS
Definition: ip4_packet.h:152
static void ip4_multicast_ethernet_address(u8 *ethernet_address, const ip4_address_t *a)
Definition: ip4_packet.h:396
static u16 ip4_get_fragment_offset(const ip4_header_t *i)
Definition: ip4_packet.h:200
ip4_address_t src
Definition: ip4_packet.h:78
#define ARRAY_LEN(x)
Definition: clib.h:62
#define ASSERT(truth)
manual_print typedef address
Definition: ip_types.api:84
u8 data[128]
Definition: ipsec_types.api:87
enum ip_dscp_t_ ip_dscp_t
ip_dscp_t tos
Definition: ip4_packet.h:141
static void ip4_multicast_address_set_for_group(ip4_address_t *a, ip_multicast_group_t g)
Definition: ip4_packet.h:388
static void ip4_addr_fib_init(ip4_address_fib_t *addr_fib, const ip4_address_t *address, u32 fib_index)
Definition: ip4_packet.h:67
static int ip4_is_first_fragment(const ip4_header_t *i)
Definition: ip4_packet.h:220
u64 uword
Definition: types.h:112
#define ip_csum_update(sum, old, new, type, field)
Definition: ip_packet.h:294
static ip_ecn_t ip4_header_get_ecn(const ip4_header_t *ip4)
Definition: ip4_packet.h:305
ip4_address_t mask
Definition: ip4_packet.h:83
ip_multicast_group_t
Definition: ip_packet.h:80
#define IP4_HEADER_FLAG_DONT_FRAGMENT
Definition: ip4_packet.h:153
static uword ip4_address_netmask_length(const ip4_address_t *a)
Definition: ip4_packet.h:88
static int ip4_header_bytes(const ip4_header_t *i)
Definition: ip4_packet.h:235
u8 ip_version_and_header_length
Definition: ip4_packet.h:138
u32 ip4
Definition: one.api:440
static ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
Definition: ip_packet.h:318
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:247
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:300
#define CLIB_PACKED(x)
Definition: clib.h:81