FD.io VPP  v20.05-21-gb1500e9ff
Vector Packet Processing
ip_types.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 #include <vnet/ip/ip_types.h>
17 #include <vnet/ip/format.h>
18 #include <vnet/ip/ip.h>
19 
20 u8 *
21 format_ip_address (u8 * s, va_list * args)
22 {
23  ip_address_t *a = va_arg (*args, ip_address_t *);
24  u8 ver = ip_addr_version (a);
25  if (ver == AF_IP4)
26  {
27  return format (s, "%U", format_ip4_address, &ip_addr_v4 (a));
28  }
29  else if (ver == AF_IP6)
30  {
31  return format (s, "%U", format_ip6_address, &ip_addr_v6 (a));
32  }
33  else
34  {
35  clib_warning ("Can't format IP version %d!", ver);
36  return 0;
37  }
38 }
39 
40 uword
41 unformat_ip_address (unformat_input_t * input, va_list * args)
42 {
43  ip_address_t *a = va_arg (*args, ip_address_t *);
44 
45  clib_memset (a, 0, sizeof (*a));
46  if (unformat (input, "%U", unformat_ip4_address, &ip_addr_v4 (a)))
47  ip_addr_version (a) = AF_IP4;
48  else if (unformat_user (input, unformat_ip6_address, &ip_addr_v6 (a)))
49  ip_addr_version (a) = AF_IP6;
50  else
51  return 0;
52  return 1;
53 }
54 
55 u8 *
56 format_ip_prefix (u8 * s, va_list * args)
57 {
58  ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
59  return format (s, "%U/%d", format_ip_address, &ip_prefix_addr (a),
60  ip_prefix_len (a));
61 }
62 
63 uword
64 unformat_ip_prefix (unformat_input_t * input, va_list * args)
65 {
66  ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
67  if (unformat (input, "%U/%d", unformat_ip_address, &ip_prefix_addr (a),
68  &ip_prefix_len (a)))
69  {
70  if ((ip_prefix_version (a) == AF_IP4 && 32 < ip_prefix_len (a)) ||
71  (ip_prefix_version (a) == AF_IP6 && 128 < ip_prefix_len (a)))
72  {
73  clib_warning ("Prefix length to big: %d!", ip_prefix_len (a));
74  return 0;
75  }
77  }
78  else
79  return 0;
80  return 1;
81 }
82 
83 u16
85 {
86  switch (ip_addr_version (a))
87  {
88  case AF_IP4:
89  return sizeof (ip4_address_t);
90  break;
91  case AF_IP6:
92  return sizeof (ip6_address_t);
93  break;
94  }
95  return 0;
96 }
97 
98 bool
100 {
101  switch (ip_addr_version (ip))
102  {
103  case AF_IP4:
104  return (ip_addr_v4 (ip).as_u32 == 0);
105  case AF_IP6:
106  return (ip_addr_v6 (ip).as_u64[0] == 0 &&
107  ip_addr_v6 (ip).as_u64[1] == 0);
108  break;
109  }
110  return false;
111 }
112 
113 int
114 ip_address_cmp (const ip_address_t * ip1, const ip_address_t * ip2)
115 {
116  int res = 0;
117  if (ip_addr_version (ip1) != ip_addr_version (ip2))
118  return -1;
119  res = ip46_address_cmp (&ip_addr_46 (ip1), &ip_addr_46 (ip2));
120 
121  if (res < 0)
122  res = 2;
123  else if (res > 0)
124  res = 1;
125 
126  return res;
127 }
128 
129 void
131 {
132  if (AF_IP4 == ip_addr_version (src))
133  {
134  /* don't copy any garbage from the union */
135  clib_memset (dst, 0, sizeof (*dst));
136  ip_addr_v4 (dst) = ip_addr_v4 (src);
137  dst->version = AF_IP4;
138  }
139  else
140  {
141  clib_memcpy (dst, src, sizeof (ip_address_t));
142  }
143 }
144 
145 u8 *
147 {
148  switch (ip->version)
149  {
150  case AF_IP4:
151  return (u8 *) & ip_addr_v4 (ip);
152  case AF_IP6:
153  return (u8 *) & ip_addr_v6 (ip);
154  break;
155  }
156  ASSERT (0);
157  return (NULL);
158 }
159 
160 void
162 {
163  switch (src->version)
164  {
165  case AF_IP4:
166  clib_memcpy (dst, &ip_addr_v4 (src), ip_address_size (src));
167  break;
168  case AF_IP6:
169  clib_memcpy (dst, &ip_addr_v6 (src), ip_address_size (src));
170  break;
171  }
172 }
173 
174 u16
176 {
177  switch (af)
178  {
179  case AF_IP4:
180  return sizeof (ip4_address_t);
181  break;
182  case AF_IP6:
183  return sizeof (ip6_address_t);
184  break;
185  }
186  return 0;
187 }
188 
191 {
192  switch (af)
193  {
194  case AF_IP4:
195  return (VNET_LINK_IP4);
196  case AF_IP6:
197  return (VNET_LINK_IP6);
198  }
199  ASSERT (0);
200  return (VNET_LINK_IP4);
201 }
202 
203 
204 void
206 {
207  ip_addr_version (dst) = version;
208 
209  switch (version)
210  {
211  case AF_IP4:
212  ip_addr_v4 (dst) = *(ip4_address_t *) src;
213  break;
214  case AF_IP6:
215  ip_addr_v6 (dst) = *(ip6_address_t *) src;
216  break;
217  }
218 }
219 
222 {
223  switch (af)
224  {
225  case AF_IP4:
226  return (FIB_PROTOCOL_IP4);
227  case AF_IP6:
228  return (FIB_PROTOCOL_IP6);
229  }
230  ASSERT (0);
231  return (FIB_PROTOCOL_IP4);
232 }
233 
236 {
237  switch (fp)
238  {
239  case FIB_PROTOCOL_IP4:
240  return (AF_IP4);
241  case FIB_PROTOCOL_IP6:
242  return (AF_IP6);
243  case FIB_PROTOCOL_MPLS:
244  ASSERT (0);
245  }
246  return (AF_IP4);
247 }
248 
250 ip_address_to_46 (const ip_address_t * addr, ip46_address_t * a)
251 {
252  *a = ip_addr_46 (addr);
254 }
255 
256 void
257 ip_address_from_46 (const ip46_address_t * nh,
258  fib_protocol_t fproto, ip_address_t * ip)
259 {
260  ip_addr_46 (ip) = *nh;
262 }
263 
264 static void
266 {
267  u32 mask = ~0;
268 
269  ASSERT (ip4);
270 
271  if (32 <= preflen)
272  {
273  return;
274  }
275 
276  mask = pow2_mask (preflen) << (32 - preflen);
277  mask = clib_host_to_net_u32 (mask);
278  ip4->data_u32 &= mask;
279 }
280 
281 static void
282 ip_prefix_normalize_ip6 (ip6_address_t * ip6, u8 preflen)
283 {
284  u8 mask_6[16];
285  u32 *m;
286  u8 j, i0, i1;
287 
288  ASSERT (ip6);
289 
290  clib_memset (mask_6, 0, sizeof (mask_6));
291 
292  if (128 <= preflen)
293  {
294  return;
295  }
296 
297  i1 = preflen % 32;
298  i0 = preflen / 32;
299  m = (u32 *) & mask_6[0];
300 
301  for (j = 0; j < i0; j++)
302  {
303  m[j] = ~0;
304  }
305 
306  if (i1)
307  {
308  m[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
309  }
310 
311  for (j = 0; j < sizeof (mask_6); j++)
312  {
313  ip6->as_u8[j] &= mask_6[j];
314  }
315 }
316 
317 void
319 {
320  u8 preflen = ip_prefix_len (a);
321 
322  switch (ip_prefix_version (a))
323  {
324  case AF_IP4:
325  ip_prefix_normalize_ip4 (&ip_prefix_v4 (a), preflen);
326  break;
327 
328  case AF_IP6:
329  ip_prefix_normalize_ip6 (&ip_prefix_v6 (a), preflen);
330  break;
331 
332  default:
333  ASSERT (0);
334  }
335 }
336 
337 void
338 ip_prefix_copy (void *dst, void *src)
339 {
340  clib_memcpy (dst, src, sizeof (ip_prefix_t));
341 }
342 
343 int
345 {
346  int cmp = 0;
347 
348  ip_prefix_normalize (p1);
349  ip_prefix_normalize (p2);
350 
351  cmp = ip_address_cmp (&ip_prefix_addr (p1), &ip_prefix_addr (p2));
352  if (cmp == 0)
353  {
354  if (ip_prefix_len (p1) < ip_prefix_len (p2))
355  {
356  cmp = 1;
357  }
358  else
359  {
360  if (ip_prefix_len (p1) > ip_prefix_len (p2))
361  cmp = 2;
362  }
363  }
364  return cmp;
365 }
366 
367 static bool
369 {
370  ip4_address_t ip4_addr, ip4_mask;
371 
372  if (ip_prefix_len (ip) > 32)
373  return (false);
374 
375  ip4_addr = ip_prefix_v4 (ip);
376  ip4_preflen_to_mask (ip_prefix_len (ip), &ip4_mask);
377 
378  return ((ip4_addr.as_u32 & ip4_mask.as_u32) == ip4_addr.as_u32);
379 }
380 
381 static bool
383 {
384  ip6_address_t ip6_addr, ip6_mask;
385 
386  if (ip_prefix_len (ip) > 128)
387  return (false);
388 
389  ip6_addr = ip_prefix_v6 (ip);
390  ip6_preflen_to_mask (ip_prefix_len (ip), &ip6_mask);
391 
392  return (((ip6_addr.as_u64[0] & ip6_mask.as_u64[0]) == ip6_addr.as_u64[0]) &&
393  ((ip6_addr.as_u64[1] & ip6_mask.as_u64[1]) == ip6_addr.as_u64[1]));
394 }
395 
396 bool
398 {
399  switch (ip_prefix_version (ip))
400  {
401  case AF_IP4:
402  return (ip4_prefix_validate (ip));
403  case AF_IP6:
404  return (ip6_prefix_validate (ip));
405  }
406  ASSERT (0);
407  return (false);
408 }
409 
410 void
412 {
413  ASSERT (preflen <= 32);
414  if (preflen == 0)
415  ip4->data_u32 = 0;
416  else
417  ip4->data_u32 &= clib_net_to_host_u32 (0xffffffff << (32 - preflen));
418 }
419 
420 void
421 ip6_address_normalize (ip6_address_t * ip6, u8 preflen)
422 {
423  ASSERT (preflen <= 128);
424  if (preflen == 0)
425  {
426  ip6->as_u64[0] = 0;
427  ip6->as_u64[1] = 0;
428  }
429  else if (preflen <= 64)
430  {
431  ip6->as_u64[0] &=
432  clib_host_to_net_u64 (0xffffffffffffffffL << (64 - preflen));
433  ip6->as_u64[1] = 0;
434  }
435  else
436  ip6->as_u64[1] &=
437  clib_host_to_net_u64 (0xffffffffffffffffL << (128 - preflen));
438 }
439 
440 void
442 {
443  if (pref_len == 0)
444  ip->as_u32 = 0;
445  else
446  ip->as_u32 = clib_host_to_net_u32 (~((1 << (32 - pref_len)) - 1));
447 }
448 
449 u32
451 {
452  if (mask->as_u32 == 0)
453  return 0;
454  return (32 - log2_first_set (clib_net_to_host_u32 (mask->as_u32)));
455 }
456 
457 void
459  ip4_address_t * res)
460 {
461  u32 not_mask;
462  not_mask = (1 << (32 - plen)) - 1;
463  res->as_u32 = clib_net_to_host_u32 (ip->as_u32) + not_mask;
464 }
465 
466 void
467 ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask)
468 {
469  if (pref_len == 0)
470  {
471  mask->as_u64[0] = 0;
472  mask->as_u64[1] = 0;
473  }
474  else if (pref_len <= 64)
475  {
476  mask->as_u64[0] =
477  clib_host_to_net_u64 (0xffffffffffffffffL << (64 - pref_len));
478  mask->as_u64[1] = 0;
479  }
480  else
481  {
482  mask->as_u64[0] = 0xffffffffffffffffL;
483  mask->as_u64[1] =
484  clib_host_to_net_u64 (0xffffffffffffffffL << (128 - pref_len));
485  }
486 }
487 
488 void
489 ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
490  ip6_address_t * res)
491 {
492  u64 not_mask;
493  if (plen == 0)
494  {
495  res->as_u64[0] = 0xffffffffffffffffL;
496  res->as_u64[1] = 0xffffffffffffffffL;
497  }
498  else if (plen <= 64)
499  {
500  not_mask = ((u64) 1 << (64 - plen)) - 1;
501  res->as_u64[0] = clib_net_to_host_u64 (ip->as_u64[0]) + not_mask;
502  res->as_u64[1] = 0xffffffffffffffffL;
503  }
504  else
505  {
506  not_mask = ((u64) 1 << (128 - plen)) - 1;
507  res->as_u64[1] = clib_net_to_host_u64 (ip->as_u64[1]) + not_mask;
508  }
509 }
510 
511 u32
512 ip6_mask_to_preflen (ip6_address_t * mask)
513 {
514  u8 first1, first0;
515  if (mask->as_u64[0] == 0 && mask->as_u64[1] == 0)
516  return 0;
517  first1 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[1]));
518  first0 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[0]));
519 
520  if (first1 != 0)
521  return 128 - first1;
522  else
523  return 64 - first0;
524 }
525 
526 /*
527  * fd.io coding-style-patch-verification: ON
528  *
529  * Local Variables:
530  * eval: (c-set-style "gnu")
531  * End:
532  */
#define ip_addr_v6(_a)
Definition: ip_types.h:59
#define ip_addr_46(_a)
Definition: ip_types.h:57
a
Definition: bitmap.h:538
#define ip_prefix_addr(_a)
Definition: ip_types.h:87
static bool ip4_prefix_validate(const ip_prefix_t *ip)
Definition: ip_types.c:368
void ip_address_set(ip_address_t *dst, const void *src, u8 version)
Definition: ip_types.c:205
bool ip_prefix_validate(const ip_prefix_t *ip)
Definition: ip_types.c:397
static uword log2_first_set(uword x)
Definition: clib.h:274
vl_api_fib_path_nh_t nh
Definition: fib_types.api:126
u64 as_u64
Definition: bihash_doc.h:63
option version
Definition: sample.api:19
unsigned long u64
Definition: types.h:89
void ip6_preflen_to_mask(u8 pref_len, ip6_address_t *mask)
Definition: ip_types.c:467
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 ip4_mask_to_preflen(ip4_address_t *mask)
Definition: ip_types.c:450
vl_api_address_t src
Definition: gre.api:54
u16 ip_address_size(const ip_address_t *a)
Definition: ip_types.c:84
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define ip_prefix_v6(_a)
Definition: ip_types.h:91
#define ip_prefix_v4(_a)
Definition: ip_types.h:90
#define ip_addr_version(_a)
Definition: ip_types.h:60
vhost_vring_addr_t addr
Definition: vhost_user.h:254
unsigned char u8
Definition: types.h:56
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
#define clib_memcpy(d, s, n)
Definition: string.h:180
format_function_t format_ip4_address
Definition: format.h:73
unformat_function_t unformat_ip4_address
Definition: format.h:68
void ip_prefix_copy(void *dst, void *src)
Definition: ip_types.c:338
static uword pow2_mask(uword x)
Definition: clib.h:235
vl_api_ip6_address_t ip6
Definition: one.api:424
static int ip46_address_cmp(const ip46_address_t *ip46_1, const ip46_address_t *ip46_2)
Definition: ip46_address.h:80
void ip_prefix_normalize(ip_prefix_t *a)
Definition: ip_types.c:318
unsigned int u32
Definition: types.h:88
void ip6_address_normalize(ip6_address_t *ip6, u8 preflen)
Definition: ip_types.c:421
void ip6_prefix_max_address_host_order(ip6_address_t *ip, u8 plen, ip6_address_t *res)
Definition: ip_types.c:489
u16 ip_version_to_size(ip_address_family_t af)
Definition: ip_types.c:175
void ip_address_copy_addr(void *dst, const ip_address_t *src)
Definition: ip_types.c:161
void ip4_preflen_to_mask(u8 pref_len, ip4_address_t *ip)
Definition: ip_types.c:441
void ip4_prefix_max_address_host_order(ip4_address_t *ip, u8 plen, ip4_address_t *res)
Definition: ip_types.c:458
void ip_address_from_46(const ip46_address_t *nh, fib_protocol_t fproto, ip_address_t *ip)
Definition: ip_types.c:257
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
bool ip_address_is_zero(const ip_address_t *ip)
Definition: ip_types.c:99
vl_api_ip4_address_t ip4
Definition: one.api:376
uword unformat_ip_address(unformat_input_t *input, va_list *args)
Definition: ip_types.c:41
fib_protocol_t ip_address_to_46(const ip_address_t *addr, ip46_address_t *a)
Definition: ip_types.c:250
vl_api_address_t dst
Definition: gre.api:55
unformat_function_t unformat_ip6_address
Definition: format.h:89
u8 * format_ip_prefix(u8 *s, va_list *args)
Definition: ip_types.c:56
format_function_t format_ip6_address
Definition: format.h:91
vnet_link_t ip_address_family_to_link_type(ip_address_family_t af)
Definition: ip_types.c:190
#define clib_warning(format, args...)
Definition: error.h:59
u8 * format_ip_address(u8 *s, va_list *args)
Definition: ip_types.c:21
fib_protocol_t ip_address_family_to_fib_proto(ip_address_family_t af)
Definition: ip_types.c:221
#define ASSERT(truth)
#define ip_addr_v4(_a)
Definition: ip_types.h:58
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
ip_address_family_t version
Definition: ip_types.h:50
static bool ip6_prefix_validate(const ip_prefix_t *ip)
Definition: ip_types.c:382
int ip_address_cmp(const ip_address_t *ip1, const ip_address_t *ip2)
Definition: ip_types.c:114
u32 ip6_mask_to_preflen(ip6_address_t *mask)
Definition: ip_types.c:512
enum ip_address_family_t_ ip_address_family_t
vl_api_address_t ip
Definition: l2.api:501
u64 uword
Definition: types.h:112
#define ip_prefix_len(_a)
Definition: ip_types.h:89
#define ip_prefix_version(_a)
Definition: ip_types.h:88
void ip_address_copy(ip_address_t *dst, const ip_address_t *src)
Definition: ip_types.c:130
void ip4_address_normalize(ip4_address_t *ip4, u8 preflen)
Definition: ip_types.c:411
static void ip_prefix_normalize_ip4(ip4_address_t *ip4, u8 preflen)
Definition: ip_types.c:265
uword unformat_ip_prefix(unformat_input_t *input, va_list *args)
Definition: ip_types.c:64
u8 * ip_addr_bytes(ip_address_t *ip)
Definition: ip_types.c:146
static void ip_prefix_normalize_ip6(ip6_address_t *ip6, u8 preflen)
Definition: ip_types.c:282
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
ip_address_family_t ip_address_family_from_fib_proto(fib_protocol_t fp)
Definition: ip_types.c:235
int ip_prefix_cmp(ip_prefix_t *p1, ip_prefix_t *p2)
Definition: ip_types.c:344