FD.io VPP  v16.09
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/ip/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  u8 data[4];
52  /* Aliases. */
53  u8 as_u8[4];
56 
57 typedef struct {
58  /* IP address must be first for ip_interface_address_get_address() to work */
62 
63 always_inline void
65  u32 fib_index)
66 {
67  clib_memcpy (&addr_fib->ip4_addr, address, sizeof (addr_fib->ip4_addr));
68  addr_fib->fib_index = fib_index;
69 }
70 
71 /* (src,dst) pair of addresses as found in packet header. */
72 typedef struct {
75 
76 /* If address is a valid netmask, return length of mask. */
79 {
80  uword result = 0;
81  uword i;
82  for (i = 0; i < ARRAY_LEN (a->as_u8); i++)
83  {
84  switch (a->as_u8[i])
85  {
86  case 0xff: result += 8; break;
87  case 0xfe: result += 7; goto done;
88  case 0xfc: result += 6; goto done;
89  case 0xf8: result += 5; goto done;
90  case 0xf0: result += 4; goto done;
91  case 0xe0: result += 3; goto done;
92  case 0xc0: result += 2; goto done;
93  case 0x80: result += 1; goto done;
94  case 0x00: result += 0; goto done;
95  default:
96  /* Not a valid netmask mask. */
97  return ~0;
98  }
99  }
100  done:
101  return result;
102 }
103 
104 typedef union {
105  struct {
106  /* 4 bit packet length (in 32bit units) and version VVVVLLLL.
107  e.g. for packets w/ no options ip_version_and_header_length == 0x45. */
109 
110  /* Type of service. */
112 
113  /* Total layer 3 packet length including this header. */
115 
116  /* Fragmentation ID. */
118 
119  /* 3 bits of flags and 13 bits of fragment offset (in units
120  of 8 byte quantities). */
122 #define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13)
123 #define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14)
124 #define IP4_HEADER_FLAG_CONGESTION (1 << 15)
125 
126  /* Time to live decremented by router at each hop. */
128 
129  /* Next level protocol packet. */
131 
132  /* Checksum. */
134 
135  /* Source and destination address. */
136  union {
137  struct {
139  };
141  };
142  };
143 
144  /* For checksumming we'll want to access IP header in word sized chunks. */
145  /* For 64 bit machines. */
146  CLIB_PACKED (struct {
147  u64 checksum_data_64[2];
148  u32 checksum_data_64_32[1];
149  });
150 
151  /* For 32 bit machines. */
152  CLIB_PACKED (struct {
153  u32 checksum_data_32[5];
154  });
155 } ip4_header_t;
156 
157 /* Value of ip_version_and_header_length for packets w/o options. */
158 #define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \
159  ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32)))
160 
161 always_inline int
162 ip4_get_fragment_offset (ip4_header_t * i)
163 { return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff; }
164 
165 always_inline int
166 ip4_get_fragment_more (ip4_header_t * i)
167 { return clib_net_to_host_u16 (i->flags_and_fragment_offset) & IP4_HEADER_FLAG_MORE_FRAGMENTS; }
168 
169 always_inline int
170 ip4_is_fragment (ip4_header_t * i)
171 { return (i->flags_and_fragment_offset &
172  clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)); }
173 
174 always_inline int
175 ip4_is_first_fragment (ip4_header_t * i)
176 { return (i->flags_and_fragment_offset &
177  clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
178  clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS); }
179 
180 /* Fragment offset in bytes. */
181 always_inline int
183 { return 8 * ip4_get_fragment_offset (i); }
184 
185 always_inline int
186 ip4_header_bytes (ip4_header_t * i)
187 { return sizeof (u32) * (i->ip_version_and_header_length & 0xf); }
188 
189 always_inline void *
190 ip4_next_header (ip4_header_t * i)
191 { return (void *) i + ip4_header_bytes (i); }
192 
194 ip4_header_checksum (ip4_header_t * i)
195 {
196  u16 save, csum;
197  ip_csum_t sum;
198 
199  save = i->checksum;
200  i->checksum = 0;
201  sum = ip_incremental_checksum (0, i, ip4_header_bytes (i));
202  csum = ~ip_csum_fold (sum);
203 
204  i->checksum = save;
205 
206  /* Make checksum agree for special case where either
207  0 or 0xffff would give same 1s complement sum. */
208  if (csum == 0 && save == 0xffff)
209  csum = save;
210 
211  return csum;
212 }
213 
214 static inline uword
216 { return i->checksum == ip4_header_checksum (i); }
217 
218 #define ip4_partial_header_checksum_x1(ip0,sum0) \
219 do { \
220  if (BITS (ip_csum_t) > 32) \
221  { \
222  sum0 = ip0->checksum_data_64[0]; \
223  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
224  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
225  } \
226  else \
227  { \
228  sum0 = ip0->checksum_data_32[0]; \
229  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
230  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
231  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
232  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
233  } \
234 } while (0)
235 
236 #define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \
237 do { \
238  if (BITS (ip_csum_t) > 32) \
239  { \
240  sum0 = ip0->checksum_data_64[0]; \
241  sum1 = ip1->checksum_data_64[0]; \
242  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \
243  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \
244  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \
245  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \
246  } \
247  else \
248  { \
249  sum0 = ip0->checksum_data_32[0]; \
250  sum1 = ip1->checksum_data_32[0]; \
251  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \
252  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \
253  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \
254  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \
255  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \
256  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \
257  sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \
258  sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \
259  } \
260 } while (0)
261 
264 { return (a->data[0] & 0xf0) == 0xe0; }
265 
266 always_inline void
268 {
269  ASSERT ((u32) g < (1 << 28));
270  a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g);
271 }
272 
273 always_inline void
274 ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
275 {
276  u32 src0, dst0;
277 
278  src0 = ip0->src_address.data_u32;
279  dst0 = ip0->dst_address.data_u32;
280  ip0->src_address.data_u32 = dst0;
281  ip0->dst_address.data_u32 = src0;
282 
283  src0 = tcp0->ports.src;
284  dst0 = tcp0->ports.dst;
285  tcp0->ports.src = dst0;
286  tcp0->ports.dst = src0;
287 }
288 
289 always_inline void
290 ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
291  tcp_header_t * tcp0, tcp_header_t * tcp1)
292 {
293  u32 src0, dst0, src1, dst1;
294 
295  src0 = ip0->src_address.data_u32;
296  src1 = ip1->src_address.data_u32;
297  dst0 = ip0->dst_address.data_u32;
298  dst1 = ip1->dst_address.data_u32;
299  ip0->src_address.data_u32 = dst0;
300  ip1->src_address.data_u32 = dst1;
301  ip0->dst_address.data_u32 = src0;
302  ip1->dst_address.data_u32 = src1;
303 
304  src0 = tcp0->ports.src;
305  src1 = tcp1->ports.src;
306  dst0 = tcp0->ports.dst;
307  dst1 = tcp1->ports.dst;
308  tcp0->ports.src = dst0;
309  tcp1->ports.src = dst1;
310  tcp0->ports.dst = src0;
311  tcp1->ports.dst = src1;
312 }
313 
314 #endif /* included_ip4_packet_h */
ip4_address_t ip4_addr
Definition: ip4_packet.h:59
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
a
Definition: bitmap.h:516
ip4_address_t src_address
Definition: ip4_packet.h:138
static int ip4_header_bytes(ip4_header_t *i)
Definition: ip4_packet.h:186
uword ip_csum_t
Definition: ip_packet.h:86
u16 flags_and_fragment_offset
Definition: ip4_packet.h:121
static void ip4_tcp_reply_x1(ip4_header_t *ip0, tcp_header_t *tcp0)
Definition: ip4_packet.h:274
static uword ip4_header_checksum_is_valid(ip4_header_t *i)
Definition: ip4_packet.h:215
ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
Definition: ip_checksum.c:43
#define always_inline
Definition: clib.h:84
static uword ip4_address_netmask_length(ip4_address_t *a)
Definition: ip4_packet.h:78
ip4_address_t dst_address
Definition: ip4_packet.h:138
static int ip4_get_fragment_offset(ip4_header_t *i)
Definition: ip4_packet.h:162
unsigned long u64
Definition: types.h:89
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:190
static int ip4_is_fragment(ip4_header_t *i)
Definition: ip4_packet.h:170
ip4_address_pair_t address_pair
Definition: ip4_packet.h:140
static uword ip4_address_is_multicast(ip4_address_t *a)
Definition: ip4_packet.h:263
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:290
#define IP4_HEADER_FLAG_MORE_FRAGMENTS
Definition: ip4_packet.h:122
union tcp_header_t::@150 ports
ip4_address_t src
Definition: ip4_packet.h:73
#define clib_memcpy(a, b, c)
Definition: string.h:63
static int ip4_is_first_fragment(ip4_header_t *i)
Definition: ip4_packet.h:175
#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:64
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
static void ip4_multicast_address_set_for_group(ip4_address_t *a, ip_multicast_group_t g)
Definition: ip4_packet.h:267
static int ip4_get_fragment_more(ip4_header_t *i)
Definition: ip4_packet.h:166
u64 uword
Definition: types.h:112
static int ip4_get_fragment_offset_bytes(ip4_header_t *i)
Definition: ip4_packet.h:182
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
ip_multicast_group_t
Definition: ip_packet.h:77
u8 ip_version_and_header_length
Definition: ip4_packet.h:108
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:194
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:138
#define CLIB_PACKED(x)
Definition: clib.h:78