FD.io VPP  v18.07.1-19-g511ce25
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  u32 fib_index)
69 {
70  clib_memcpy (&addr_fib->ip4_addr, address, sizeof (addr_fib->ip4_addr));
71  addr_fib->fib_index = fib_index;
72 }
73 
74 /* (src,dst) pair of addresses as found in packet header. */
75 typedef struct
76 {
79 
80 typedef struct
81 {
84 
85 /* If address is a valid netmask, return length of mask. */
88 {
89  uword result = 0;
90  uword i;
91  for (i = 0; i < ARRAY_LEN (a->as_u8); i++)
92  {
93  switch (a->as_u8[i])
94  {
95  case 0xff:
96  result += 8;
97  break;
98  case 0xfe:
99  result += 7;
100  goto done;
101  case 0xfc:
102  result += 6;
103  goto done;
104  case 0xf8:
105  result += 5;
106  goto done;
107  case 0xf0:
108  result += 4;
109  goto done;
110  case 0xe0:
111  result += 3;
112  goto done;
113  case 0xc0:
114  result += 2;
115  goto done;
116  case 0x80:
117  result += 1;
118  goto done;
119  case 0x00:
120  result += 0;
121  goto done;
122  default:
123  /* Not a valid netmask mask. */
124  return ~0;
125  }
126  }
127 done:
128  return result;
129 }
130 
131 typedef union
132 {
133  struct
134  {
135  /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
136  e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
138 
139  /* Type of service. */
141 
142  /* Total layer 3 packet length including this header. */
144 
145  /* Fragmentation ID. */
147 
148  /* 3 bits of flags and 13 bits of fragment offset (in units
149  of 8 byte quantities). */
151 #define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
152 #define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
153 #define IP4_HEADER_FLAG_CONGESTION (1 << 15)
154 
155  /* Time to live decremented by router at each hop. */
157 
158  /* Next level protocol packet. */
160 
161  /* Checksum. */
163 
164  /* Source and destination address. */
165  union
166  {
167  struct
168  {
170  };
172  };
173  };
174 
175  /* For checksumming we'll want to access IP header in word sized chunks. */
176  /* For 64 bit machines. */
177  /* *INDENT-OFF* */
178  CLIB_PACKED (struct {
179  u64 checksum_data_64[2];
180  u32 checksum_data_64_32[1];
181  });
182  /* *INDENT-ON* */
183 
184  /* For 32 bit machines. */
185  /* *INDENT-OFF* */
186  CLIB_PACKED (struct {
187  u32 checksum_data_32[5];
188  });
189  /* *INDENT-ON* */
190 } ip4_header_t;
191 
192 /* Value of ip_version_and_header_length for packets w/o options. */
193 #define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
194  ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
195 
196 #define IP4_ROUTER_ALERT_OPTION 20
197 
198 always_inline int
199 ip4_get_fragment_offset (ip4_header_t * i)
200 {
201  return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff;
202 }
203 
204 always_inline int
205 ip4_get_fragment_more (ip4_header_t * i)
206 {
207  return clib_net_to_host_u16 (i->flags_and_fragment_offset) &
209 }
210 
211 always_inline int
212 ip4_is_fragment (ip4_header_t * i)
213 {
214  return (i->flags_and_fragment_offset &
215  clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS));
216 }
217 
218 always_inline int
219 ip4_is_first_fragment (ip4_header_t * i)
220 {
221  return (i->flags_and_fragment_offset &
222  clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
223  clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
224 }
225 
226 /* Fragment offset in bytes. */
227 always_inline int
229 {
230  return 8 * ip4_get_fragment_offset (i);
231 }
232 
233 always_inline int
234 ip4_header_bytes (ip4_header_t * i)
235 {
236  return sizeof (u32) * (i->ip_version_and_header_length & 0xf);
237 }
238 
239 always_inline void *
240 ip4_next_header (ip4_header_t * i)
241 {
242  return (void *) i + ip4_header_bytes (i);
243 }
244 
246 ip4_header_checksum (ip4_header_t * i)
247 {
248  u16 save, csum;
249  ip_csum_t sum;
250 
251  save = i->checksum;
252  i->checksum = 0;
253  sum = ip_incremental_checksum (0, i, ip4_header_bytes (i));
254  csum = ~ip_csum_fold (sum);
255 
256  i->checksum = save;
257 
258  /* Make checksum agree for special case where either
259  0 or 0xffff would give same 1s complement sum. */
260  if (csum == 0 && save == 0xffff)
261  csum = save;
262 
263  return csum;
264 }
265 
266 static inline uword
268 {
269  return i->checksum == ip4_header_checksum (i);
270 }
271 
272 #define ip4_partial_header_checksum_x1(ip0,sum0) \
273 do { \
274  if (BITS (ip_csum_t) > 32) \
275  { \
276  sum0 = ip0->checksum_data_64[0]; \
277  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
278  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
279  } \
280  else \
281  { \
282  sum0 = ip0->checksum_data_32[0]; \
283  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
284  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
285  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
286  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
287  } \
288 } while (0)
289 
290 #define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
291 do { \
292  if (BITS (ip_csum_t) > 32) \
293  { \
294  sum0 = ip0->checksum_data_64[0]; \
295  sum1 = ip1->checksum_data_64[0]; \
296  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
297  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
298  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
299  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
300  } \
301  else \
302  { \
303  sum0 = ip0->checksum_data_32[0]; \
304  sum1 = ip1->checksum_data_32[0]; \
305  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
306  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
307  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
308  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
309  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
310  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
311  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
312  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
313  } \
314 } while (0)
315 
318 {
319  return (a->data[0] & 0xf0) == 0xe0;
320 }
321 
322 always_inline void
325 {
326  ASSERT ((u32) g < (1 << 28));
327  a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
328 }
329 
330 always_inline void
332 {
333  u8 *d = a->as_u8;
334 
335  ethernet_address[0] = 0x01;
336  ethernet_address[1] = 0x00;
337  ethernet_address[2] = 0x5e;
338  ethernet_address[3] = d[1] & 0x7f;
339  ethernet_address[4] = d[2];
340  ethernet_address[5] = d[3];
341 }
342 
343 always_inline void
344 ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
345 {
346  u32 src0, dst0;
347 
348  src0 = ip0->src_address.data_u32;
349  dst0 = ip0->dst_address.data_u32;
350  ip0->src_address.data_u32 = dst0;
351  ip0->dst_address.data_u32 = src0;
352 
353  src0 = tcp0->src;
354  dst0 = tcp0->dst;
355  tcp0->src = dst0;
356  tcp0->dst = src0;
357 }
358 
359 always_inline void
360 ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
361  tcp_header_t * tcp0, tcp_header_t * tcp1)
362 {
363  u32 src0, dst0, src1, dst1;
364 
365  src0 = ip0->src_address.data_u32;
366  src1 = ip1->src_address.data_u32;
367  dst0 = ip0->dst_address.data_u32;
368  dst1 = ip1->dst_address.data_u32;
369  ip0->src_address.data_u32 = dst0;
370  ip1->src_address.data_u32 = dst1;
371  ip0->dst_address.data_u32 = src0;
372  ip1->dst_address.data_u32 = src1;
373 
374  src0 = tcp0->src;
375  src1 = tcp1->src;
376  dst0 = tcp0->dst;
377  dst1 = tcp1->dst;
378  tcp0->src = dst0;
379  tcp1->src = dst1;
380  tcp0->dst = src0;
381  tcp1->dst = src1;
382 }
383 
384 #endif /* included_ip4_packet_h */
385 
386 /*
387  * fd.io coding-style-patch-verification: ON
388  *
389  * Local Variables:
390  * eval: (c-set-style "gnu")
391  * End:
392  */
ip4_address_t ip4_addr
Definition: ip4_packet.h:62
typedef address
Definition: ip_types.api:35
a
Definition: bitmap.h:538
ip4_address_t src_address
Definition: ip4_packet.h:169
unsigned long u64
Definition: types.h:89
static int ip4_header_bytes(ip4_header_t *i)
Definition: ip4_packet.h:234
int i
uword ip_csum_t
Definition: ip_packet.h:181
u16 flags_and_fragment_offset
Definition: ip4_packet.h:150
struct _tcp_header tcp_header_t
vhost_vring_addr_t addr
Definition: vhost_user.h:116
static void ip4_tcp_reply_x1(ip4_header_t *ip0, tcp_header_t *tcp0)
Definition: ip4_packet.h:344
unsigned char u8
Definition: types.h:56
static uword ip4_header_checksum_is_valid(ip4_header_t *i)
Definition: ip4_packet.h:267
#define always_inline
Definition: clib.h:92
static uword ip4_address_netmask_length(ip4_address_t *a)
Definition: ip4_packet.h:87
ip4_address_t dst_address
Definition: ip4_packet.h:169
static int ip4_get_fragment_offset(ip4_header_t *i)
Definition: ip4_packet.h:199
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:240
unsigned int u32
Definition: types.h:88
static int ip4_is_fragment(ip4_header_t *i)
Definition: ip4_packet.h:212
unsigned short u16
Definition: types.h:57
ip4_address_pair_t address_pair
Definition: ip4_packet.h:171
static uword ip4_address_is_multicast(ip4_address_t *a)
Definition: ip4_packet.h:317
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:360
#define IP4_HEADER_FLAG_MORE_FRAGMENTS
Definition: ip4_packet.h:151
static void ip4_multicast_ethernet_address(u8 *ethernet_address, ip4_address_t *a)
Definition: ip4_packet.h:331
ip4_address_t src
Definition: ip4_packet.h:77
static ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
Definition: ip_packet.h:254
#define clib_memcpy(a, b, c)
Definition: string.h:75
static int ip4_is_first_fragment(ip4_header_t *i)
Definition: ip4_packet.h:219
#define ARRAY_LEN(x)
Definition: clib.h:59
static void ip4_addr_fib_init(ip4_address_fib_t *addr_fib, ip4_address_t *address, u32 fib_index)
Definition: ip4_packet.h:67
#define ASSERT(truth)
static void ip4_multicast_address_set_for_group(ip4_address_t *a, ip_multicast_group_t g)
Definition: ip4_packet.h:323
static int ip4_get_fragment_more(ip4_header_t *i)
Definition: ip4_packet.h:205
static int ip4_get_fragment_offset_bytes(ip4_header_t *i)
Definition: ip4_packet.h:228
u64 uword
Definition: types.h:112
ip4_address_t mask
Definition: ip4_packet.h:82
ip_multicast_group_t
Definition: ip_packet.h:80
u8 ip_version_and_header_length
Definition: ip4_packet.h:137
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:246
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:237
#define CLIB_PACKED(x)
Definition: clib.h:78