FD.io VPP  v19.01.3-6-g70449b9b9
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 
199 always_inline int
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 
205 always_inline int
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 static inline uword
269 {
270  return i->checksum == ip4_header_checksum (i);
271 }
272 
273 #define ip4_partial_header_checksum_x1(ip0,sum0) \
274 do { \
275  if (BITS (ip_csum_t) > 32) \
276  { \
277  sum0 = ip0->checksum_data_64[0]; \
278  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
279  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
280  } \
281  else \
282  { \
283  sum0 = ip0->checksum_data_32[0]; \
284  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
285  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
286  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
287  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
288  } \
289 } while (0)
290 
291 #define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
292 do { \
293  if (BITS (ip_csum_t) > 32) \
294  { \
295  sum0 = ip0->checksum_data_64[0]; \
296  sum1 = ip1->checksum_data_64[0]; \
297  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
298  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
299  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
300  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
301  } \
302  else \
303  { \
304  sum0 = ip0->checksum_data_32[0]; \
305  sum1 = ip1->checksum_data_32[0]; \
306  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
307  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
308  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
309  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
310  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
311  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
312  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
313  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
314  } \
315 } while (0)
316 
319 {
320  return (a->data[0] & 0xf0) == 0xe0;
321 }
322 
323 always_inline void
326 {
327  ASSERT ((u32) g < (1 << 28));
328  a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
329 }
330 
331 always_inline void
332 ip4_multicast_ethernet_address (u8 * ethernet_address,
333  const ip4_address_t * a)
334 {
335  const u8 *d = a->as_u8;
336 
337  ethernet_address[0] = 0x01;
338  ethernet_address[1] = 0x00;
339  ethernet_address[2] = 0x5e;
340  ethernet_address[3] = d[1] & 0x7f;
341  ethernet_address[4] = d[2];
342  ethernet_address[5] = d[3];
343 }
344 
345 always_inline void
346 ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
347 {
348  u32 src0, dst0;
349 
350  src0 = ip0->src_address.data_u32;
351  dst0 = ip0->dst_address.data_u32;
352  ip0->src_address.data_u32 = dst0;
353  ip0->dst_address.data_u32 = src0;
354 
355  src0 = tcp0->src;
356  dst0 = tcp0->dst;
357  tcp0->src = dst0;
358  tcp0->dst = src0;
359 }
360 
361 always_inline void
362 ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
363  tcp_header_t * tcp0, tcp_header_t * tcp1)
364 {
365  u32 src0, dst0, src1, dst1;
366 
367  src0 = ip0->src_address.data_u32;
368  src1 = ip1->src_address.data_u32;
369  dst0 = ip0->dst_address.data_u32;
370  dst1 = ip1->dst_address.data_u32;
371  ip0->src_address.data_u32 = dst0;
372  ip1->src_address.data_u32 = dst1;
373  ip0->dst_address.data_u32 = src0;
374  ip1->dst_address.data_u32 = src1;
375 
376  src0 = tcp0->src;
377  src1 = tcp1->src;
378  dst0 = tcp0->dst;
379  dst1 = tcp1->dst;
380  tcp0->src = dst0;
381  tcp1->src = dst1;
382  tcp0->dst = src0;
383  tcp1->dst = src1;
384 }
385 
386 #endif /* included_ip4_packet_h */
387 
388 /*
389  * fd.io coding-style-patch-verification: ON
390  *
391  * Local Variables:
392  * eval: (c-set-style "gnu")
393  * End:
394  */
ip4_address_t ip4_addr
Definition: ip4_packet.h:62
typedef address
Definition: ip_types.api:30
a
Definition: bitmap.h:538
ip4_address_t src_address
Definition: ip4_packet.h:170
unsigned long u64
Definition: types.h:89
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
int i
uword ip_csum_t
Definition: ip_packet.h:181
u16 flags_and_fragment_offset
Definition: ip4_packet.h:151
static uword ip4_address_is_multicast(const ip4_address_t *a)
Definition: ip4_packet.h:318
struct _tcp_header tcp_header_t
vhost_vring_addr_t addr
Definition: vhost_user.h:121
static void ip4_tcp_reply_x1(ip4_header_t *ip0, tcp_header_t *tcp0)
Definition: ip4_packet.h:346
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 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:268
#define always_inline
Definition: clib.h:98
ip4_address_t dst_address
Definition: ip4_packet.h:170
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:241
unsigned int u32
Definition: types.h:88
unsigned short u16
Definition: types.h:57
static int ip4_get_fragment_offset(const ip4_header_t *i)
Definition: ip4_packet.h:200
ip4_address_pair_t address_pair
Definition: ip4_packet.h:172
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:362
#define IP4_HEADER_FLAG_MORE_FRAGMENTS
Definition: ip4_packet.h:152
vl_api_address_t dst
Definition: vxlan_gbp.api:33
static void ip4_multicast_ethernet_address(u8 *ethernet_address, const ip4_address_t *a)
Definition: ip4_packet.h:332
ip4_address_t src
Definition: ip4_packet.h:78
static ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
Definition: ip_packet.h:254
#define ARRAY_LEN(x)
Definition: clib.h:62
static int ip4_get_fragment_more(const ip4_header_t *i)
Definition: ip4_packet.h:206
#define ASSERT(truth)
static void ip4_multicast_address_set_for_group(ip4_address_t *a, ip_multicast_group_t g)
Definition: ip4_packet.h:324
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
ip4_address_t mask
Definition: ip4_packet.h:83
ip_multicast_group_t
Definition: ip_packet.h:80
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
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:237
#define CLIB_PACKED(x)
Definition: clib.h:81