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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * ip/ip6_format.c: ip6 formatting
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/ip/ip.h>
42 /* Format an IP6 address. */
43 u8 * format_ip6_address (u8 * s, va_list * args)
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;
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++)
54 if (a->as_u16[i] == 0)
62 this_zero_run_index = i;
69 /* offer to compress the biggest run of > 1 zero */
70 if (this_zero_run > max_zero_run && this_zero_run > 1)
72 max_zero_run_index = this_zero_run_index;
73 max_zero_run = this_zero_run;
83 if (this_zero_run > max_zero_run && this_zero_run > 1)
85 max_zero_run_index = this_zero_run_index;
86 max_zero_run = this_zero_run;
90 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
92 if (i == max_zero_run_index)
95 i += max_zero_run - 1;
96 last_double_colon = 1;
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;
110 /* Format an IP6 route destination and length. */
111 u8 * format_ip6_address_and_length (u8 * s, va_list * args)
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);
118 /* Parse an IP6 address. */
119 uword unformat_ip6_address (unformat_input_t * input, va_list * args)
121 ip6_address_t * result = va_arg (*args, ip6_address_t *);
123 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
124 uword c, n_colon, double_colon_index;
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)
131 if (c >= '0' && c <= '9')
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)
141 unformat_put_input (input);
145 /* Too many hex quads. */
146 if (n_hex_quads >= ARRAY_LEN (hex_quads))
151 hex_quad = (hex_quad << 4) | hex_digit;
153 /* Hex quad must fit in 16 bits. */
154 if (n_hex_digits >= 4)
161 /* Save position of :: */
164 /* More than one :: ? */
165 if (double_colon_index < ARRAY_LEN (hex_quads))
167 double_colon_index = n_hex_quads;
170 if (n_colon > 0 && n_hex_digits > 0)
172 hex_quads[n_hex_quads++] = hex_quad;
178 if (n_hex_digits > 0)
179 hex_quads[n_hex_quads++] = hex_quad;
184 /* Expand :: to appropriate number of zero hex quads. */
185 if (double_colon_index < ARRAY_LEN (hex_quads))
187 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
189 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
190 hex_quads[n_zero + i] = hex_quads[i];
192 for (i = 0; i < n_zero; i++)
194 ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
195 hex_quads[double_colon_index + i] = 0;
198 n_hex_quads = ARRAY_LEN (hex_quads);
201 /* Too few hex quads given. */
202 if (n_hex_quads < ARRAY_LEN (hex_quads))
205 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
206 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
212 /* Format an IP6 header. */
213 u8 * format_ip6_header (u8 * s, va_list * args)
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;
221 if (max_header_bytes < sizeof (ip[0]))
222 return format (s, "IP header truncated");
224 indent = format_get_indent (s);
227 s = format (s, "%U: %U -> %U",
228 format_ip_protocol, ip->protocol,
229 format_ip6_address, &ip->src_address,
230 format_ip6_address, &ip->dst_address);
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);
238 s = format (s, "\n%Uversion %d",
239 format_white_space, indent, ip_version);
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));
246 /* Recurse into next protocol layer. */
247 if (max_header_bytes != 0 && sizeof (ip[0]) < max_header_bytes)
249 ip_main_t * im = &ip_main;
250 ip_protocol_info_t * pi = ip_get_protocol_info (im, ip->protocol);
252 if (pi && pi->format_header)
253 s = format (s, "\n%U%U",
254 format_white_space, indent - 2,
256 /* next protocol header */ (void*) (ip + 1),
257 max_header_bytes - sizeof (ip[0]));
263 /* Parse an IP6 header. */
264 uword unformat_ip6_header (unformat_input_t * input, va_list * args)
266 u8 ** result = va_arg (*args, u8 **);
270 /* Allocate space for IP header. */
274 old_length = vec_len (*result);
275 vec_add2 (*result, p, sizeof (ip[0]));
279 memset (ip, 0, sizeof (ip[0]));
280 ip->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (6 << 28);
282 if (! unformat (input, "%U: %U -> %U",
283 unformat_ip_protocol, &ip->protocol,
284 unformat_ip6_address, &ip->src_address,
285 unformat_ip6_address, &ip->dst_address))
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);
296 else if (unformat (input, "hop-limit %U", unformat_vlib_number, &i))
299 /* Can't parse input: try next protocol level. */
304 /* Recurse into next protocol layer. */
306 ip_main_t * im = &ip_main;
307 ip_protocol_info_t * pi = ip_get_protocol_info (im, ip->protocol);
309 if (pi && pi->unformat_header)
311 if (! unformat_user (input, pi->unformat_header, result))
314 /* Result may have moved. */
315 ip = (void *) *result + old_length;
319 ip->payload_length = clib_host_to_net_u16 (vec_len (*result) - (old_length + sizeof (ip[0])));
324 /* Parse an IP46 address. */
325 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
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);
333 } else if ((type != IP46_TYPE_IP4) &&
334 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
340 /* Format an IP46 address. */
341 u8 * format_ip46_address (u8 * s, va_list * args)
343 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
344 ip46_type_t type = va_arg (*args, ip46_type_t);
350 is_ip4 = ip46_address_is_ip4(ip46);
361 format(s, "%U", format_ip4_address, &ip46->ip4):
362 format(s, "%U", format_ip6_address, &ip46->ip6);