FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
ip4_format.c
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  * ip/ip4_format.c: ip4 formatting
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 #include <vnet/ip/ip.h>
41 
42 /* Format an IP4 address. */
43 u8 *
44 format_ip4_address (u8 * s, va_list * args)
45 {
46  u8 *a = va_arg (*args, u8 *);
47  return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
48 }
49 
50 /* Format an IP4 route destination and length. */
51 u8 *
52 format_ip4_address_and_length (u8 * s, va_list * args)
53 {
54  u8 *a = va_arg (*args, u8 *);
55  u8 l = va_arg (*args, u32);
56  return format (s, "%U/%d", format_ip4_address, a, l);
57 }
58 
59 u8 *
60 format_ip4_address_and_mask (u8 * s, va_list * args)
61 {
62  ip4_address_and_mask_t *am = va_arg (*args, ip4_address_and_mask_t *);
63 
64  if (am->addr.as_u32 == 0 && am->mask.as_u32 == 0)
65  return format (s, "any");
66 
67  if (am->mask.as_u32 == ~0)
68  return format (s, "%U", format_ip4_address, &am->addr);
69 
70  return format (s, "%U/%U", format_ip4_address, &am->addr,
71  format_ip4_address, &am->mask);
72 }
73 
74 /* Parse an IP4 address %d.%d.%d.%d. */
75 uword
76 unformat_ip4_address (unformat_input_t * input, va_list * args)
77 {
78  u8 *result = va_arg (*args, u8 *);
79  unsigned a[4];
80 
81  if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
82  return 0;
83 
84  if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
85  return 0;
86 
87  result[0] = a[0];
88  result[1] = a[1];
89  result[2] = a[2];
90  result[3] = a[3];
91 
92  return 1;
93 }
94 
95 uword
97 {
98  ip4_address_and_mask_t *am = va_arg (*args, ip4_address_and_mask_t *);
99  u32 addr = 0, mask = 0;
100 
101  if (unformat (input, "any"))
102  ;
103  else if (unformat (input, "%U/%U", unformat_ip4_address, &addr,
104  unformat_ip4_address, &mask))
105  ;
106  else if (unformat (input, "%U", unformat_ip4_address, &addr))
107  mask = ~0;
108  else
109  return 0;
110 
111  am->addr.as_u32 = addr;
112  am->mask.as_u32 = mask;
113  return 1;
114 }
115 
116 /* Format an IP4 header. */
117 u8 *
118 format_ip4_header (u8 * s, va_list * args)
119 {
120  ip4_header_t *ip = va_arg (*args, ip4_header_t *);
121  u32 max_header_bytes = va_arg (*args, u32);
122  u32 ip_version, header_bytes;
123  u32 indent;
124 
125  /* Nothing to do. */
126  if (max_header_bytes < sizeof (ip[0]))
127  return format (s, "IP header truncated");
128 
129  indent = format_get_indent (s);
130  indent += 2;
131 
132  ip_version = (ip->ip_version_and_header_length >> 4);
133  header_bytes = (ip->ip_version_and_header_length & 0xf) * sizeof (u32);
134 
135  s = format (s, "%U: %U -> %U",
139 
140  /* Show IP version and header length only with unexpected values. */
141  if (ip_version != 4 || header_bytes != sizeof (ip4_header_t))
142  s = format (s, "\n%Uversion %d, header length %d",
143  format_white_space, indent, ip_version, header_bytes);
144 
145  s = format (s, "\n%Utos 0x%02x, ttl %d, length %d, checksum 0x%04x",
146  format_white_space, indent,
147  ip->tos, ip->ttl,
148  clib_net_to_host_u16 (ip->length),
149  clib_net_to_host_u16 (ip->checksum));
150 
151  /* Check and report invalid checksums. */
152  {
153  u16 c = ip4_header_checksum (ip);
154  if (c != ip->checksum)
155  s = format (s, " (should be 0x%04x)", clib_net_to_host_u16 (c));
156  }
157 
158  {
159  u32 f = clib_net_to_host_u16 (ip->flags_and_fragment_offset);
160  u32 o;
161 
162  s = format (s, "\n%Ufragment id 0x%04x",
163  format_white_space, indent,
164  clib_net_to_host_u16 (ip->fragment_id));
165 
166  /* Fragment offset. */
167  o = 8 * (f & 0x1fff);
168  f ^= o;
169  if (o != 0)
170  s = format (s, " offset %d", o);
171 
172  if (f != 0)
173  {
174  s = format (s, ", flags ");
175 #define _(l) if (f & IP4_HEADER_FLAG_##l) s = format (s, #l);
176  _(MORE_FRAGMENTS);
177  _(DONT_FRAGMENT);
178  _(CONGESTION);
179 #undef _
180  }
181  }
182 
183  /* Recurse into next protocol layer. */
184  if (max_header_bytes != 0 && header_bytes < max_header_bytes)
185  {
186  ip_main_t *im = &ip_main;
188 
189  if (pi && pi->format_header)
190  s = format (s, "\n%U%U",
191  format_white_space, indent - 2, pi->format_header,
192  /* next protocol header */ (void *) ip + header_bytes,
193  max_header_bytes - header_bytes);
194  }
195 
196  return s;
197 }
198 
199 /* Parse an IP4 header. */
200 uword
201 unformat_ip4_header (unformat_input_t * input, va_list * args)
202 {
203  u8 **result = va_arg (*args, u8 **);
204  ip4_header_t *ip;
205  int old_length;
206 
207  /* Allocate space for IP header. */
208  {
209  void *p;
210 
211  old_length = vec_len (*result);
212  vec_add2 (*result, p, sizeof (ip4_header_t));
213  ip = p;
214  }
215 
216  clib_memset (ip, 0, sizeof (ip[0]));
218 
219  if (!unformat (input, "%U: %U -> %U",
223  return 0;
224 
225  /* Parse options. */
226  while (1)
227  {
228  int i, j;
229 
230  if (unformat (input, "tos %U", unformat_vlib_number, &i))
231  ip->tos = i;
232 
233  else if (unformat (input, "ttl %U", unformat_vlib_number, &i))
234  ip->ttl = i;
235 
236  else if (unformat (input, "fragment id %U offset %U",
238  {
239  ip->fragment_id = clib_host_to_net_u16 (i);
241  clib_host_to_net_u16 ((i / 8) & 0x1fff);
242  }
243 
244  /* Flags. */
245  else if (unformat (input, "mf") || unformat (input, "MF"))
247  clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
248 
249  else if (unformat (input, "df") || unformat (input, "DF"))
251  clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
252 
253  else if (unformat (input, "ce") || unformat (input, "CE"))
255  clib_host_to_net_u16 (IP4_HEADER_FLAG_CONGESTION);
256 
257  /* Can't parse input: try next protocol level. */
258  else
259  break;
260  }
261 
262  /* Fill in checksum. */
263  ip->checksum = ip4_header_checksum (ip);
264 
265  /* Recurse into next protocol layer. */
266  {
267  ip_main_t *im = &ip_main;
269 
270  if (pi && pi->unformat_header)
271  {
272  if (!unformat_user (input, pi->unformat_header, result))
273  return 0;
274 
275  /* Result may have moved. */
276  ip = (void *) *result + old_length;
277  }
278  }
279 
280  /* Fill in IP length. */
281  ip->length = clib_host_to_net_u16 (vec_len (*result) - old_length);
282 
283  return 1;
284 }
285 
286 /*
287  * fd.io coding-style-patch-verification: ON
288  *
289  * Local Variables:
290  * eval: (c-set-style "gnu")
291  * End:
292  */
format_function_t format_ip_protocol
Definition: format.h:45
unformat_function_t unformat_ip_protocol
Definition: format.h:46
uword unformat_ip4_address_and_mask(unformat_input_t *input, va_list *args)
Definition: ip4_format.c:96
a
Definition: bitmap.h:538
ip4_address_t src_address
Definition: ip4_packet.h:170
u8 * format_ip4_header(u8 *s, va_list *args)
Definition: ip4_format.c:118
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:564
int i
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
u16 flags_and_fragment_offset
Definition: ip4_packet.h:151
Definition: ip.h:109
vhost_vring_addr_t addr
Definition: vhost_user.h:121
unsigned char u8
Definition: types.h:56
uword unformat_vlib_number(unformat_input_t *input, va_list *args)
Definition: format.c:148
uword unformat_ip4_header(unformat_input_t *input, va_list *args)
Definition: ip4_format.c:201
u8 * format_ip4_address_and_mask(u8 *s, va_list *args)
Definition: ip4_format.c:60
ip4_address_t dst_address
Definition: ip4_packet.h:170
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
unsigned int u32
Definition: types.h:88
static ip_protocol_info_t * ip_get_protocol_info(ip_main_t *im, u32 protocol)
Definition: ip.h:136
format_function_t * format_header
Definition: ip.h:81
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define IP4_HEADER_FLAG_MORE_FRAGMENTS
Definition: ip4_packet.h:152
svmdb_client_t * c
ip_main_t ip_main
Definition: ip_init.c:42
uword unformat_ip4_address(unformat_input_t *input, va_list *args)
Definition: ip4_format.c:76
ip4_address_t addr
Definition: ip4_packet.h:83
unformat_function_t * unformat_header
Definition: ip.h:84
u8 * format_ip4_address(u8 *s, va_list *args)
Definition: ip4_format.c:44
#define IP4_HEADER_FLAG_CONGESTION
Definition: ip4_packet.h:154
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
ip4_address_t mask
Definition: ip4_packet.h:83
#define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS
Definition: ip4_packet.h:194
#define IP4_HEADER_FLAG_DONT_FRAGMENT
Definition: ip4_packet.h:153
u8 ip_version_and_header_length
Definition: ip4_packet.h:138
u8 * format_ip4_address_and_length(u8 *s, va_list *args)
Definition: ip4_format.c:52
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:247
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972