FD.io VPP  v16.06
Vector Packet Processing
ip6_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/ip6_format.c: ip6 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 IP6 address. */
43 u8 * format_ip6_address (u8 * s, va_list * args)
44 {
45  ip6_address_t * a = va_arg (*args, ip6_address_t *);
46  u32 max_zero_run = 0, this_zero_run = 0;
47  int max_zero_run_index = -1, this_zero_run_index=0;
48  int in_zero_run = 0, i;
49  int last_double_colon = 0;
50 
51  /* Ugh, this is a pain. Scan forward looking for runs of 0's */
52  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
53  {
54  if (a->as_u16[i] == 0)
55  {
56  if (in_zero_run)
57  this_zero_run++;
58  else
59  {
60  in_zero_run = 1;
61  this_zero_run =1;
62  this_zero_run_index = i;
63  }
64  }
65  else
66  {
67  if (in_zero_run)
68  {
69  /* offer to compress the biggest run of > 1 zero */
70  if (this_zero_run > max_zero_run && this_zero_run > 1)
71  {
72  max_zero_run_index = this_zero_run_index;
73  max_zero_run = this_zero_run;
74  }
75  }
76  in_zero_run = 0;
77  this_zero_run = 0;
78  }
79  }
80 
81  if (in_zero_run)
82  {
83  if (this_zero_run > max_zero_run && this_zero_run > 1)
84  {
85  max_zero_run_index = this_zero_run_index;
86  max_zero_run = this_zero_run;
87  }
88  }
89 
90  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
91  {
92  if (i == max_zero_run_index)
93  {
94  s = format (s, "::");
95  i += max_zero_run - 1;
96  last_double_colon = 1;
97  }
98  else
99  {
100  s = format (s, "%s%x",
101  (last_double_colon || i == 0) ? "" : ":",
102  clib_net_to_host_u16 (a->as_u16[i]));
103  last_double_colon = 0;
104  }
105  }
106 
107  return s;
108 }
109 
110 /* Format an IP6 route destination and length. */
111 u8 * format_ip6_address_and_length (u8 * s, va_list * args)
112 {
113  ip6_address_t * a = va_arg (*args, ip6_address_t *);
114  u8 l = va_arg (*args, u32);
115  return format (s, "%U/%d", format_ip6_address, a, l);
116 }
117 
118 /* Parse an IP6 address. */
119 uword unformat_ip6_address (unformat_input_t * input, va_list * args)
120 {
121  ip6_address_t * result = va_arg (*args, ip6_address_t *);
122  u16 hex_quads[8];
123  uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
124  uword c, n_colon, double_colon_index;
125 
126  n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
127  double_colon_index = ARRAY_LEN (hex_quads);
128  while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
129  {
130  hex_digit = 16;
131  if (c >= '0' && c <= '9')
132  hex_digit = c - '0';
133  else if (c >= 'a' && c <= 'f')
134  hex_digit = c + 10 - 'a';
135  else if (c >= 'A' && c <= 'F')
136  hex_digit = c + 10 - 'A';
137  else if (c == ':' && n_colon < 2)
138  n_colon++;
139  else
140  {
141  unformat_put_input (input);
142  break;
143  }
144 
145  /* Too many hex quads. */
146  if (n_hex_quads >= ARRAY_LEN (hex_quads))
147  return 0;
148 
149  if (hex_digit < 16)
150  {
151  hex_quad = (hex_quad << 4) | hex_digit;
152 
153  /* Hex quad must fit in 16 bits. */
154  if (n_hex_digits >= 4)
155  return 0;
156 
157  n_colon = 0;
158  n_hex_digits++;
159  }
160 
161  /* Save position of :: */
162  if (n_colon == 2)
163  {
164  /* More than one :: ? */
165  if (double_colon_index < ARRAY_LEN (hex_quads))
166  return 0;
167  double_colon_index = n_hex_quads;
168  }
169 
170  if (n_colon > 0 && n_hex_digits > 0)
171  {
172  hex_quads[n_hex_quads++] = hex_quad;
173  hex_quad = 0;
174  n_hex_digits = 0;
175  }
176  }
177 
178  if (n_hex_digits > 0)
179  hex_quads[n_hex_quads++] = hex_quad;
180 
181  {
182  word i;
183 
184  /* Expand :: to appropriate number of zero hex quads. */
185  if (double_colon_index < ARRAY_LEN (hex_quads))
186  {
187  word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
188 
189  for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
190  hex_quads[n_zero + i] = hex_quads[i];
191 
192  for (i = 0; i < n_zero; i++)
193  {
194  ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
195  hex_quads[double_colon_index + i] = 0;
196  }
197 
198  n_hex_quads = ARRAY_LEN (hex_quads);
199  }
200 
201  /* Too few hex quads given. */
202  if (n_hex_quads < ARRAY_LEN (hex_quads))
203  return 0;
204 
205  for (i = 0; i < ARRAY_LEN (hex_quads); i++)
206  result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
207 
208  return 1;
209  }
210 }
211 
212 /* Format an IP6 header. */
213 u8 * format_ip6_header (u8 * s, va_list * args)
214 {
215  ip6_header_t * ip = va_arg (*args, ip6_header_t *);
216  u32 max_header_bytes = va_arg (*args, u32);
217  u32 i, ip_version, traffic_class, flow_label;
218  uword indent;
219 
220  /* Nothing to do. */
221  if (max_header_bytes < sizeof (ip[0]))
222  return format (s, "IP header truncated");
223 
224  indent = format_get_indent (s);
225  indent += 2;
226 
227  s = format (s, "%U: %U -> %U",
231 
232  i = clib_net_to_host_u32 (ip->ip_version_traffic_class_and_flow_label);
233  ip_version = (i >> 28);
234  traffic_class = (i >> 20) & 0xff;
235  flow_label = i & pow2_mask (20);
236 
237  if (ip_version != 6)
238  s = format (s, "\n%Uversion %d",
239  format_white_space, indent, ip_version);
240 
241  s = format (s, "\n%Utos 0x%02x, flow label 0x%x, hop limit %d, payload length %d",
242  format_white_space, indent,
243  traffic_class, flow_label, ip->hop_limit,
244  clib_net_to_host_u16 (ip->payload_length));
245 
246  /* Recurse into next protocol layer. */
247  if (max_header_bytes != 0 && sizeof (ip[0]) < max_header_bytes)
248  {
249  ip_main_t * im = &ip_main;
251 
252  if (pi && pi->format_header)
253  s = format (s, "\n%U%U",
254  format_white_space, indent - 2,
255  pi->format_header,
256  /* next protocol header */ (void*) (ip + 1),
257  max_header_bytes - sizeof (ip[0]));
258  }
259 
260  return s;
261 }
262 
263 /* Parse an IP6 header. */
264 uword unformat_ip6_header (unformat_input_t * input, va_list * args)
265 {
266  u8 ** result = va_arg (*args, u8 **);
267  ip6_header_t * ip;
268  int old_length;
269 
270  /* Allocate space for IP header. */
271  {
272  void * p;
273 
274  old_length = vec_len (*result);
275  vec_add2 (*result, p, sizeof (ip[0]));
276  ip = p;
277  }
278 
279  memset (ip, 0, sizeof (ip[0]));
280  ip->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (6 << 28);
281 
282  if (! unformat (input, "%U: %U -> %U",
286  return 0;
287 
288  /* Parse options. */
289  while (1)
290  {
291  int i;
292 
293  if (unformat (input, "tos %U", unformat_vlib_number, &i))
294  ip->ip_version_traffic_class_and_flow_label |= clib_host_to_net_u32 ((i & 0xff) << 20);
295 
296  else if (unformat (input, "hop-limit %U", unformat_vlib_number, &i))
297  ip->hop_limit = i;
298 
299  /* Can't parse input: try next protocol level. */
300  else
301  break;
302  }
303 
304  /* Recurse into next protocol layer. */
305  {
306  ip_main_t * im = &ip_main;
308 
309  if (pi && pi->unformat_header)
310  {
311  if (! unformat_user (input, pi->unformat_header, result))
312  return 0;
313 
314  /* Result may have moved. */
315  ip = (void *) *result + old_length;
316  }
317  }
318 
319  ip->payload_length = clib_host_to_net_u16 (vec_len (*result) - (old_length + sizeof (ip[0])));
320 
321  return 1;
322 }
323 
324 /* Parse an IP46 address. */
326 {
327  ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
328  ip46_type_t type = va_arg (*args, ip46_type_t);
329  if ((type != IP46_TYPE_IP6) &&
330  unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
331  ip46_address_mask_ip4(ip46);
332  return 1;
333  } else if ((type != IP46_TYPE_IP4) &&
334  unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
335  return 1;
336  }
337  return 0;
338 }
339 
340 /* Format an IP46 address. */
341 u8 * format_ip46_address (u8 * s, va_list * args)
342 {
343  ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
344  return ip46_address_is_ip4(ip46)?
345  format(s, "%U", format_ip4_address, &ip46->ip4):
346  format(s, "%U", format_ip6_address, &ip46->ip6);
347 }
uword unformat_vlib_number(unformat_input_t *input, va_list *args)
Definition: format.c:130
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
a
Definition: bitmap.h:393
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
u8 * format_ip6_header(u8 *s, va_list *args)
Definition: ip6_format.c:213
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
Definition: ip.h:108
format_function_t format_ip4_address
Definition: format.h:71
ip6_address_t src_address
Definition: ip6_packet.h:293
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:107
#define ip46_address_mask_ip4(ip46)
Definition: ip6_packet.h:70
uword unformat_ip46_address(unformat_input_t *input, va_list *args)
Definition: ip6_format.c:325
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
format_function_t format_ip_protocol
Definition: format.h:45
uword unformat_ip6_header(unformat_input_t *input, va_list *args)
Definition: ip6_format.c:264
unformat_function_t unformat_ip4_address
Definition: format.h:68
static ip_protocol_info_t * ip_get_protocol_info(ip_main_t *im, u32 protocol)
Definition: ip.h:135
format_function_t * format_header
Definition: ip.h:81
ip46_type_t
Definition: format.h:56
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:69
unformat_function_t unformat_ip_protocol
Definition: format.h:46
ip_main_t ip_main
Definition: ip_init.c:42
uword unformat_ip6_address(unformat_input_t *input, va_list *args)
Definition: ip6_format.c:119
#define ARRAY_LEN(x)
Definition: clib.h:59
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
always_inline uword unformat_get_input(unformat_input_t *input)
Definition: format.h:190
u8 * format_ip6_address_and_length(u8 *s, va_list *args)
Definition: ip6_format.c:111
unformat_function_t * unformat_header
Definition: ip.h:84
always_inline uword format_get_indent(u8 *s)
Definition: format.h:72
u8 * format_ip6_address(u8 *s, va_list *args)
Definition: ip6_format.c:43
u64 uword
Definition: types.h:112
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:280
unsigned short u16
Definition: types.h:57
u16 payload_length
Definition: ip6_packet.h:284
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
u8 * format_ip46_address(u8 *s, va_list *args)
Definition: ip6_format.c:341
always_inline void unformat_put_input(unformat_input_t *input)
Definition: format.h:203
u16 as_u16[8]
Definition: ip6_packet.h:48
struct _unformat_input_t unformat_input_t
always_inline uword pow2_mask(uword x)
Definition: clib.h:242
ip6_address_t dst_address
Definition: ip6_packet.h:293