FD.io VPP  v16.12-rc0-308-g931be3a
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 * format_ip4_address (u8 * s, va_list * args)
44 {
45  u8 * a = va_arg (*args, u8 *);
46  return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
47 }
48 
49 /* Format an IP4 route destination and length. */
50 u8 * format_ip4_address_and_length (u8 * s, va_list * args)
51 {
52  u8 * a = va_arg (*args, u8 *);
53  u8 l = va_arg (*args, u32);
54  return format (s, "%U/%d", format_ip4_address, a, l);
55 }
56 
57 /* Parse an IP4 address %d.%d.%d.%d. */
58 uword unformat_ip4_address (unformat_input_t * input, va_list * args)
59 {
60  u8 * result = va_arg (*args, u8 *);
61  unsigned a[4];
62 
63  if (! unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
64  return 0;
65 
66  if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
67  return 0;
68 
69  result[0] = a[0];
70  result[1] = a[1];
71  result[2] = a[2];
72  result[3] = a[3];
73 
74  return 1;
75 }
76 
77 /* Format an IP4 header. */
78 u8 * format_ip4_header (u8 * s, va_list * args)
79 {
80  ip4_header_t * ip = va_arg (*args, ip4_header_t *);
81  u32 max_header_bytes = va_arg (*args, u32);
82  u32 ip_version, header_bytes;
83  uword indent;
84 
85  /* Nothing to do. */
86  if (max_header_bytes < sizeof (ip[0]))
87  return format (s, "IP header truncated");
88 
89  indent = format_get_indent (s);
90  indent += 2;
91 
92  ip_version = (ip->ip_version_and_header_length >> 4);
93  header_bytes = (ip->ip_version_and_header_length & 0xf) * sizeof (u32);
94 
95  s = format (s, "%U: %U -> %U",
99 
100  /* Show IP version and header length only with unexpected values. */
101  if (ip_version != 4 || header_bytes != sizeof (ip4_header_t))
102  s = format (s, "\n%Uversion %d, header length %d",
103  format_white_space, indent,
104  ip_version, header_bytes);
105 
106  s = format (s, "\n%Utos 0x%02x, ttl %d, length %d, checksum 0x%04x",
107  format_white_space, indent,
108  ip->tos, ip->ttl,
109  clib_net_to_host_u16 (ip->length),
110  clib_net_to_host_u16 (ip->checksum));
111 
112  /* Check and report invalid checksums. */
113  {
114  u16 c = ip4_header_checksum (ip);
115  if (c != ip->checksum)
116  s = format (s, " (should be 0x%04x)", clib_net_to_host_u16 (c));
117  }
118 
119  {
120  u32 f = clib_net_to_host_u16 (ip->flags_and_fragment_offset);
121  u32 o;
122 
123  s = format (s, "\n%Ufragment id 0x%04x",
124  format_white_space, indent,
125  clib_net_to_host_u16 (ip->fragment_id));
126 
127  /* Fragment offset. */
128  o = 8 * (f & 0x1fff);
129  f ^= o;
130  if (o != 0)
131  s = format (s, " offset %d", o);
132 
133  if (f != 0)
134  {
135  s = format (s, ", flags ");
136 #define _(l) if (f & IP4_HEADER_FLAG_##l) s = format (s, #l);
137  _ (MORE_FRAGMENTS);
138  _ (DONT_FRAGMENT);
139  _ (CONGESTION);
140 #undef _
141  }
142  }
143 
144  /* Recurse into next protocol layer. */
145  if (max_header_bytes != 0 && header_bytes < max_header_bytes)
146  {
147  ip_main_t * im = &ip_main;
149 
150  if (pi && pi->format_header)
151  s = format (s, "\n%U%U",
152  format_white_space, indent - 2,
153  pi->format_header,
154  /* next protocol header */ (void*) ip + header_bytes,
155  max_header_bytes - header_bytes);
156  }
157 
158  return s;
159 }
160 
161 /* Parse an IP4 header. */
162 uword unformat_ip4_header (unformat_input_t * input, va_list * args)
163 {
164  u8 ** result = va_arg (*args, u8 **);
165  ip4_header_t * ip;
166  int old_length;
167 
168  /* Allocate space for IP header. */
169  {
170  void * p;
171 
172  old_length = vec_len (*result);
173  vec_add2 (*result, p, sizeof (ip4_header_t));
174  ip = p;
175  }
176 
177  memset (ip, 0, sizeof (ip[0]));
179 
180  if (! unformat (input, "%U: %U -> %U",
184  return 0;
185 
186  /* Parse options. */
187  while (1)
188  {
189  int i, j;
190 
191  if (unformat (input, "tos %U", unformat_vlib_number, &i))
192  ip->tos = i;
193 
194  else if (unformat (input, "ttl %U", unformat_vlib_number, &i))
195  ip->ttl = i;
196 
197  else if (unformat (input, "fragment id %U offset %U",
200  {
201  ip->fragment_id = clib_host_to_net_u16 (i);
203  clib_host_to_net_u16 ((i / 8) & 0x1fff);
204  }
205 
206  /* Flags. */
207  else if (unformat (input, "mf") || unformat (input, "MF"))
208  ip->flags_and_fragment_offset |= clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
209 
210  else if (unformat (input, "df") || unformat (input, "DF"))
211  ip->flags_and_fragment_offset |= clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
212 
213  else if (unformat (input, "ce") || unformat (input, "CE"))
214  ip->flags_and_fragment_offset |= clib_host_to_net_u16 (IP4_HEADER_FLAG_CONGESTION);
215 
216  /* Can't parse input: try next protocol level. */
217  else
218  break;
219  }
220 
221  /* Fill in checksum. */
222  ip->checksum = ip4_header_checksum (ip);
223 
224  /* Recurse into next protocol layer. */
225  {
226  ip_main_t * im = &ip_main;
228 
229  if (pi && pi->unformat_header)
230  {
231  if (! unformat_user (input, pi->unformat_header, result))
232  return 0;
233 
234  /* Result may have moved. */
235  ip = (void *) *result + old_length;
236  }
237  }
238 
239  /* Fill in IP length. */
240  ip->length = clib_host_to_net_u16 (vec_len (*result) - old_length);
241 
242  return 1;
243 }
uword unformat_vlib_number(unformat_input_t *input, va_list *args)
Definition: format.c:148
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
a
Definition: bitmap.h:516
ip4_address_t src_address
Definition: ip4_packet.h:138
u8 * format_ip4_header(u8 *s, va_list *args)
Definition: ip4_format.c:78
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:521
u16 flags_and_fragment_offset
Definition: ip4_packet.h:121
Definition: ip.h:109
uword unformat_ip4_header(unformat_input_t *input, va_list *args)
Definition: ip4_format.c:162
ip4_address_t dst_address
Definition: ip4_packet.h:138
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
format_function_t format_ip_protocol
Definition: format.h:45
static uword format_get_indent(u8 *s)
Definition: format.h:72
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:82
#define IP4_HEADER_FLAG_MORE_FRAGMENTS
Definition: ip4_packet.h:122
svmdb_client_t * c
unformat_function_t unformat_ip_protocol
Definition: format.h:46
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:58
unsigned int u32
Definition: types.h:88
unformat_function_t * unformat_header
Definition: ip.h:85
u8 * format_ip4_address(u8 *s, va_list *args)
Definition: ip4_format.c:43
#define IP4_HEADER_FLAG_CONGESTION
Definition: ip4_packet.h:124
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
#define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS
Definition: ip4_packet.h:158
#define IP4_HEADER_FLAG_DONT_FRAGMENT
Definition: ip4_packet.h:123
u8 ip_version_and_header_length
Definition: ip4_packet.h:108
struct _unformat_input_t unformat_input_t
u8 * format_ip4_address_and_length(u8 *s, va_list *args)
Definition: ip4_format.c:50
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:194