blob: 56899b73d8bd264d04f0f814358fefb07911fb64 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050043u8 *
44format_ip6_address (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070045{
Dave Barachd7cb1b52016-12-09 09:52:16 -050046 ip6_address_t *a = va_arg (*args, ip6_address_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070047 u32 max_zero_run = 0, this_zero_run = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -050048 int max_zero_run_index = -1, this_zero_run_index = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 int in_zero_run = 0, i;
50 int last_double_colon = 0;
51
52 /* Ugh, this is a pain. Scan forward looking for runs of 0's */
53 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
54 {
55 if (a->as_u16[i] == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -050056 {
57 if (in_zero_run)
58 this_zero_run++;
59 else
60 {
61 in_zero_run = 1;
62 this_zero_run = 1;
63 this_zero_run_index = i;
64 }
65 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 else
Dave Barachd7cb1b52016-12-09 09:52:16 -050067 {
68 if (in_zero_run)
69 {
70 /* offer to compress the biggest run of > 1 zero */
71 if (this_zero_run > max_zero_run && this_zero_run > 1)
72 {
73 max_zero_run_index = this_zero_run_index;
74 max_zero_run = this_zero_run;
75 }
76 }
77 in_zero_run = 0;
78 this_zero_run = 0;
79 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 }
81
82 if (in_zero_run)
83 {
84 if (this_zero_run > max_zero_run && this_zero_run > 1)
Dave Barachd7cb1b52016-12-09 09:52:16 -050085 {
86 max_zero_run_index = this_zero_run_index;
87 max_zero_run = this_zero_run;
88 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 }
Dave Barachd7cb1b52016-12-09 09:52:16 -050090
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
92 {
93 if (i == max_zero_run_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -050094 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 s = format (s, "::");
Dave Barachd7cb1b52016-12-09 09:52:16 -050096 i += max_zero_run - 1;
97 last_double_colon = 1;
98 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 else
100 {
101 s = format (s, "%s%x",
102 (last_double_colon || i == 0) ? "" : ":",
103 clib_net_to_host_u16 (a->as_u16[i]));
104 last_double_colon = 0;
105 }
106 }
107
108 return s;
109}
110
111/* Format an IP6 route destination and length. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500112u8 *
113format_ip6_address_and_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500115 ip6_address_t *a = va_arg (*args, ip6_address_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 u8 l = va_arg (*args, u32);
117 return format (s, "%U/%d", format_ip6_address, a, l);
118}
119
120/* Parse an IP6 address. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121uword
122unformat_ip6_address (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500124 ip6_address_t *result = va_arg (*args, ip6_address_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 u16 hex_quads[8];
126 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
127 uword c, n_colon, double_colon_index;
128
129 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
130 double_colon_index = ARRAY_LEN (hex_quads);
131 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
132 {
133 hex_digit = 16;
134 if (c >= '0' && c <= '9')
135 hex_digit = c - '0';
136 else if (c >= 'a' && c <= 'f')
137 hex_digit = c + 10 - 'a';
138 else if (c >= 'A' && c <= 'F')
139 hex_digit = c + 10 - 'A';
140 else if (c == ':' && n_colon < 2)
141 n_colon++;
142 else
143 {
144 unformat_put_input (input);
145 break;
146 }
147
148 /* Too many hex quads. */
149 if (n_hex_quads >= ARRAY_LEN (hex_quads))
150 return 0;
151
152 if (hex_digit < 16)
153 {
154 hex_quad = (hex_quad << 4) | hex_digit;
155
156 /* Hex quad must fit in 16 bits. */
157 if (n_hex_digits >= 4)
158 return 0;
159
160 n_colon = 0;
161 n_hex_digits++;
162 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500163
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 /* Save position of :: */
165 if (n_colon == 2)
166 {
167 /* More than one :: ? */
168 if (double_colon_index < ARRAY_LEN (hex_quads))
169 return 0;
170 double_colon_index = n_hex_quads;
171 }
172
173 if (n_colon > 0 && n_hex_digits > 0)
174 {
175 hex_quads[n_hex_quads++] = hex_quad;
176 hex_quad = 0;
177 n_hex_digits = 0;
178 }
179 }
180
181 if (n_hex_digits > 0)
182 hex_quads[n_hex_quads++] = hex_quad;
183
184 {
185 word i;
186
187 /* Expand :: to appropriate number of zero hex quads. */
188 if (double_colon_index < ARRAY_LEN (hex_quads))
189 {
190 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
191
192 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
193 hex_quads[n_zero + i] = hex_quads[i];
194
195 for (i = 0; i < n_zero; i++)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500196 {
197 ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
198 hex_quads[double_colon_index + i] = 0;
199 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
201 n_hex_quads = ARRAY_LEN (hex_quads);
202 }
203
204 /* Too few hex quads given. */
205 if (n_hex_quads < ARRAY_LEN (hex_quads))
206 return 0;
207
208 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
209 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
210
211 return 1;
212 }
213}
214
215/* Format an IP6 header. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500216u8 *
217format_ip6_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500219 ip6_header_t *ip = va_arg (*args, ip6_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220 u32 max_header_bytes = va_arg (*args, u32);
221 u32 i, ip_version, traffic_class, flow_label;
222 uword indent;
223
224 /* Nothing to do. */
225 if (max_header_bytes < sizeof (ip[0]))
226 return format (s, "IP header truncated");
227
228 indent = format_get_indent (s);
229 indent += 2;
230
231 s = format (s, "%U: %U -> %U",
232 format_ip_protocol, ip->protocol,
233 format_ip6_address, &ip->src_address,
234 format_ip6_address, &ip->dst_address);
235
236 i = clib_net_to_host_u32 (ip->ip_version_traffic_class_and_flow_label);
237 ip_version = (i >> 28);
238 traffic_class = (i >> 20) & 0xff;
239 flow_label = i & pow2_mask (20);
240
241 if (ip_version != 6)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500242 s = format (s, "\n%Uversion %d", format_white_space, indent, ip_version);
243
244 s =
245 format (s,
246 "\n%Utos 0x%02x, flow label 0x%x, hop limit %d, payload length %d",
247 format_white_space, indent, traffic_class, flow_label,
248 ip->hop_limit, clib_net_to_host_u16 (ip->payload_length));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
250 /* Recurse into next protocol layer. */
251 if (max_header_bytes != 0 && sizeof (ip[0]) < max_header_bytes)
252 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500253 ip_main_t *im = &ip_main;
254 ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
256 if (pi && pi->format_header)
257 s = format (s, "\n%U%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500258 format_white_space, indent - 2, pi->format_header,
259 /* next protocol header */ (void *) (ip + 1),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 max_header_bytes - sizeof (ip[0]));
261 }
262
263 return s;
264}
265
266/* Parse an IP6 header. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500267uword
268unformat_ip6_header (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 u8 **result = va_arg (*args, u8 **);
271 ip6_header_t *ip;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 int old_length;
273
274 /* Allocate space for IP header. */
275 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500276 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
278 old_length = vec_len (*result);
279 vec_add2 (*result, p, sizeof (ip[0]));
280 ip = p;
281 }
282
283 memset (ip, 0, sizeof (ip[0]));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500284 ip->ip_version_traffic_class_and_flow_label =
285 clib_host_to_net_u32 (6 << 28);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Dave Barachd7cb1b52016-12-09 09:52:16 -0500287 if (!unformat (input, "%U: %U -> %U",
288 unformat_ip_protocol, &ip->protocol,
289 unformat_ip6_address, &ip->src_address,
290 unformat_ip6_address, &ip->dst_address))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 return 0;
292
293 /* Parse options. */
294 while (1)
295 {
296 int i;
297
298 if (unformat (input, "tos %U", unformat_vlib_number, &i))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500299 ip->ip_version_traffic_class_and_flow_label |=
300 clib_host_to_net_u32 ((i & 0xff) << 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
302 else if (unformat (input, "hop-limit %U", unformat_vlib_number, &i))
303 ip->hop_limit = i;
304
305 /* Can't parse input: try next protocol level. */
306 else
307 break;
308 }
309
310 /* Recurse into next protocol layer. */
311 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500312 ip_main_t *im = &ip_main;
313 ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
315 if (pi && pi->unformat_header)
316 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500317 if (!unformat_user (input, pi->unformat_header, result))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 return 0;
319
320 /* Result may have moved. */
321 ip = (void *) *result + old_length;
322 }
323 }
324
Dave Barachd7cb1b52016-12-09 09:52:16 -0500325 ip->payload_length =
326 clib_host_to_net_u16 (vec_len (*result) - (old_length + sizeof (ip[0])));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
328 return 1;
329}
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100330
331/* Parse an IP46 address. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500332uword
333unformat_ip46_address (unformat_input_t * input, va_list * args)
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100334{
335 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
336 ip46_type_t type = va_arg (*args, ip46_type_t);
337 if ((type != IP46_TYPE_IP6) &&
Dave Barachd7cb1b52016-12-09 09:52:16 -0500338 unformat (input, "%U", unformat_ip4_address, &ip46->ip4))
339 {
340 ip46_address_mask_ip4 (ip46);
341 return 1;
342 }
343 else if ((type != IP46_TYPE_IP4) &&
344 unformat (input, "%U", unformat_ip6_address, &ip46->ip6))
345 {
346 return 1;
347 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100348 return 0;
349}
350
351/* Format an IP46 address. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500352u8 *
353format_ip46_address (u8 * s, va_list * args)
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100354{
355 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
Damjan Marion86be4872016-05-24 23:19:11 +0200356 ip46_type_t type = va_arg (*args, ip46_type_t);
357 int is_ip4 = 1;
358
359 switch (type)
360 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500361 case IP46_TYPE_ANY:
362 is_ip4 = ip46_address_is_ip4 (ip46);
363 break;
364 case IP46_TYPE_IP4:
365 is_ip4 = 1;
366 break;
367 case IP46_TYPE_IP6:
368 is_ip4 = 0;
369 break;
Damjan Marion86be4872016-05-24 23:19:11 +0200370 }
371
372 return is_ip4 ?
Dave Barachd7cb1b52016-12-09 09:52:16 -0500373 format (s, "%U", format_ip4_address, &ip46->ip4) :
374 format (s, "%U", format_ip6_address, &ip46->ip6);
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100375}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500376
377/*
378 * fd.io coding-style-patch-verification: ON
379 *
380 * Local Variables:
381 * eval: (c-set-style "gnu")
382 * End:
383 */