blob: eebd5ad8bd3494ecf0cf4bfcf82b5086aaa633ba [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/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. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050043u8 *
44format_ip4_address (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070045{
Dave Barachd7cb1b52016-12-09 09:52:16 -050046 u8 *a = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070047 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. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050051u8 *
52format_ip4_address_and_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070053{
Dave Barachd7cb1b52016-12-09 09:52:16 -050054 u8 *a = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 u8 l = va_arg (*args, u32);
56 return format (s, "%U/%d", format_ip4_address, a, l);
57}
58
Damjan Mariona35cc142018-03-16 01:25:27 +010059u8 *
60format_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
Ed Warnickecb9cada2015-12-08 15:45:58 -070074/* Parse an IP4 address %d.%d.%d.%d. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050075uword
76unformat_ip4_address (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077{
Dave Barachd7cb1b52016-12-09 09:52:16 -050078 u8 *result = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 unsigned a[4];
80
Dave Barachd7cb1b52016-12-09 09:52:16 -050081 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 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
Damjan Mariona35cc142018-03-16 01:25:27 +010095uword
96unformat_ip4_address_and_mask (unformat_input_t * input, va_list * args)
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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116/* Format an IP4 header. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500117u8 *
118format_ip4_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500120 ip4_header_t *ip = va_arg (*args, ip4_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 u32 max_header_bytes = va_arg (*args, u32);
122 u32 ip_version, header_bytes;
Christophe Fontained3c008d2017-10-02 18:10:54 +0200123 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
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",
136 format_ip_protocol, ip->protocol,
137 format_ip4_address, ip->src_address.data,
138 format_ip4_address, ip->dst_address.data);
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",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500143 format_white_space, indent, ip_version, header_bytes);
144
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 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);
Klement Sekeraea5cd122019-10-23 10:16:02 +0000168 f ^= f & 0x1fff;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 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);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500176 _(MORE_FRAGMENTS);
177 _(DONT_FRAGMENT);
178 _(CONGESTION);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179#undef _
180 }
faicker.mo2bccb182018-11-14 16:51:34 +0800181 /* Fragment packet but not the first. */
182 if (o != 0)
183 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 }
185
186 /* Recurse into next protocol layer. */
187 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
188 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500189 ip_main_t *im = &ip_main;
190 ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
192 if (pi && pi->format_header)
193 s = format (s, "\n%U%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500194 format_white_space, indent - 2, pi->format_header,
195 /* next protocol header */ (void *) ip + header_bytes,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 max_header_bytes - header_bytes);
197 }
198
199 return s;
200}
201
202/* Parse an IP4 header. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500203uword
204unformat_ip4_header (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500206 u8 **result = va_arg (*args, u8 **);
207 ip4_header_t *ip;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 int old_length;
209
210 /* Allocate space for IP header. */
211 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500212 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
214 old_length = vec_len (*result);
215 vec_add2 (*result, p, sizeof (ip4_header_t));
216 ip = p;
217 }
218
Dave Barachb7b92992018-10-17 10:38:51 -0400219 clib_memset (ip, 0, sizeof (ip[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220 ip->ip_version_and_header_length = IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
221
Dave Barachd7cb1b52016-12-09 09:52:16 -0500222 if (!unformat (input, "%U: %U -> %U",
223 unformat_ip_protocol, &ip->protocol,
224 unformat_ip4_address, &ip->src_address,
225 unformat_ip4_address, &ip->dst_address))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 return 0;
227
228 /* Parse options. */
229 while (1)
230 {
231 int i, j;
232
233 if (unformat (input, "tos %U", unformat_vlib_number, &i))
234 ip->tos = i;
235
236 else if (unformat (input, "ttl %U", unformat_vlib_number, &i))
237 ip->ttl = i;
238
239 else if (unformat (input, "fragment id %U offset %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500240 unformat_vlib_number, &i, unformat_vlib_number, &j))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 {
242 ip->fragment_id = clib_host_to_net_u16 (i);
243 ip->flags_and_fragment_offset |=
244 clib_host_to_net_u16 ((i / 8) & 0x1fff);
245 }
246
247 /* Flags. */
248 else if (unformat (input, "mf") || unformat (input, "MF"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500249 ip->flags_and_fragment_offset |=
250 clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
252 else if (unformat (input, "df") || unformat (input, "DF"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500253 ip->flags_and_fragment_offset |=
254 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
256 else if (unformat (input, "ce") || unformat (input, "CE"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500257 ip->flags_and_fragment_offset |=
258 clib_host_to_net_u16 (IP4_HEADER_FLAG_CONGESTION);
259
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 /* Can't parse input: try next protocol level. */
261 else
262 break;
263 }
264
265 /* Fill in checksum. */
266 ip->checksum = ip4_header_checksum (ip);
267
268 /* Recurse into next protocol layer. */
269 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 ip_main_t *im = &ip_main;
271 ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
273 if (pi && pi->unformat_header)
274 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 if (!unformat_user (input, pi->unformat_header, result))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 return 0;
277
278 /* Result may have moved. */
279 ip = (void *) *result + old_length;
280 }
281 }
282
283 /* Fill in IP length. */
284 ip->length = clib_host_to_net_u16 (vec_len (*result) - old_length);
285
286 return 1;
287}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500288
289/*
290 * fd.io coding-style-patch-verification: ON
291 *
292 * Local Variables:
293 * eval: (c-set-style "gnu")
294 * End:
295 */