FD.io VPP  v20.09-64-g4f7b92f0a
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 /**
265  * convert from a IP address to a FIB prefix
266  */
267 void
269 {
270  if (addr->version == AF_IP4)
271  {
272  prefix->fp_len = 32;
273  prefix->fp_proto = FIB_PROTOCOL_IP4;
274  clib_memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
275  memcpy (&prefix->fp_addr.ip4, &addr->ip.ip4,
276  sizeof (prefix->fp_addr.ip4));
277  }
278  else
279  {
280  prefix->fp_len = 128;
281  prefix->fp_proto = FIB_PROTOCOL_IP6;
282  memcpy (&prefix->fp_addr.ip6, &addr->ip.ip6,
283  sizeof (prefix->fp_addr.ip6));
284  }
285  prefix->___fp___pad = 0;
286 }
287 
288 static void
290 {
291  u32 mask = ~0;
292 
293  ASSERT (ip4);
294 
295  if (32 <= preflen)
296  {
297  return;
298  }
299 
300  mask = pow2_mask (preflen) << (32 - preflen);
301  mask = clib_host_to_net_u32 (mask);
302  ip4->data_u32 &= mask;
303 }
304 
305 static void
306 ip_prefix_normalize_ip6 (ip6_address_t * ip6, u8 preflen)
307 {
308  u8 mask_6[16];
309  u32 *m;
310  u8 j, i0, i1;
311 
312  ASSERT (ip6);
313 
314  clib_memset (mask_6, 0, sizeof (mask_6));
315 
316  if (128 <= preflen)
317  {
318  return;
319  }
320 
321  i1 = preflen % 32;
322  i0 = preflen / 32;
323  m = (u32 *) & mask_6[0];
324 
325  for (j = 0; j < i0; j++)
326  {
327  m[j] = ~0;
328  }
329 
330  if (i1)
331  {
332  m[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
333  }
334 
335  for (j = 0; j < sizeof (mask_6); j++)
336  {
337  ip6->as_u8[j] &= mask_6[j];
338  }
339 }
340 
341 void
343 {
344  u8 preflen = ip_prefix_len (a);
345 
346  switch (ip_prefix_version (a))
347  {
348  case AF_IP4:
349  ip_prefix_normalize_ip4 (&ip_prefix_v4 (a), preflen);
350  break;
351 
352  case AF_IP6:
353  ip_prefix_normalize_ip6 (&ip_prefix_v6 (a), preflen);
354  break;
355 
356  default:
357  ASSERT (0);
358  }
359 }
360 
361 void
362 ip_prefix_copy (void *dst, void *src)
363 {
364  clib_memcpy (dst, src, sizeof (ip_prefix_t));
365 }
366 
367 int
369 {
370  int cmp = 0;
371 
372  ip_prefix_normalize (p1);
373  ip_prefix_normalize (p2);
374 
375  cmp = ip_address_cmp (&ip_prefix_addr (p1), &ip_prefix_addr (p2));
376  if (cmp == 0)
377  {
378  if (ip_prefix_len (p1) < ip_prefix_len (p2))
379  {
380  cmp = 1;
381  }
382  else
383  {
384  if (ip_prefix_len (p1) > ip_prefix_len (p2))
385  cmp = 2;
386  }
387  }
388  return cmp;
389 }
390 
391 /**
392  * convert from a LISP to a FIB prefix
393  */
394 void
396  fib_prefix_t * fib_prefix)
397 {
398  ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
399  fib_prefix->fp_len = ip_prefix->len;
400 }
401 
402 static bool
404 {
405  ip4_address_t ip4_addr, ip4_mask;
406 
407  if (ip_prefix_len (ip) > 32)
408  return (false);
409 
410  ip4_addr = ip_prefix_v4 (ip);
411  ip4_preflen_to_mask (ip_prefix_len (ip), &ip4_mask);
412 
413  return ((ip4_addr.as_u32 & ip4_mask.as_u32) == ip4_addr.as_u32);
414 }
415 
416 static bool
418 {
419  ip6_address_t ip6_addr, ip6_mask;
420 
421  if (ip_prefix_len (ip) > 128)
422  return (false);
423 
424  ip6_addr = ip_prefix_v6 (ip);
425  ip6_preflen_to_mask (ip_prefix_len (ip), &ip6_mask);
426 
427  return (((ip6_addr.as_u64[0] & ip6_mask.as_u64[0]) == ip6_addr.as_u64[0]) &&
428  ((ip6_addr.as_u64[1] & ip6_mask.as_u64[1]) == ip6_addr.as_u64[1]));
429 }
430 
431 bool
433 {
434  switch (ip_prefix_version (ip))
435  {
436  case AF_IP4:
437  return (ip4_prefix_validate (ip));
438  case AF_IP6:
439  return (ip6_prefix_validate (ip));
440  }
441  ASSERT (0);
442  return (false);
443 }
444 
445 void
447 {
448  ASSERT (preflen <= 32);
449  if (preflen == 0)
450  ip4->data_u32 = 0;
451  else
452  ip4->data_u32 &= clib_net_to_host_u32 (0xffffffff << (32 - preflen));
453 }
454 
455 void
456 ip6_address_normalize (ip6_address_t * ip6, u8 preflen)
457 {
458  ASSERT (preflen <= 128);
459  if (preflen == 0)
460  {
461  ip6->as_u64[0] = 0;
462  ip6->as_u64[1] = 0;
463  }
464  else if (preflen <= 64)
465  {
466  ip6->as_u64[0] &=
467  clib_host_to_net_u64 (0xffffffffffffffffL << (64 - preflen));
468  ip6->as_u64[1] = 0;
469  }
470  else
471  ip6->as_u64[1] &=
472  clib_host_to_net_u64 (0xffffffffffffffffL << (128 - preflen));
473 }
474 
475 void
477 {
478  if (pref_len == 0)
479  ip->as_u32 = 0;
480  else
481  ip->as_u32 = clib_host_to_net_u32 (~((1 << (32 - pref_len)) - 1));
482 }
483 
484 u32
486 {
487  if (mask->as_u32 == 0)
488  return 0;
489  return (32 - log2_first_set (clib_net_to_host_u32 (mask->as_u32)));
490 }
491 
492 void
494  ip4_address_t * res)
495 {
496  u32 not_mask;
497  not_mask = (1 << (32 - plen)) - 1;
498  res->as_u32 = clib_net_to_host_u32 (ip->as_u32) + not_mask;
499 }
500 
501 void
502 ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask)
503 {
504  if (pref_len == 0)
505  {
506  mask->as_u64[0] = 0;
507  mask->as_u64[1] = 0;
508  }
509  else if (pref_len <= 64)
510  {
511  mask->as_u64[0] =
512  clib_host_to_net_u64 (0xffffffffffffffffL << (64 - pref_len));
513  mask->as_u64[1] = 0;
514  }
515  else
516  {
517  mask->as_u64[0] = 0xffffffffffffffffL;
518  mask->as_u64[1] =
519  clib_host_to_net_u64 (0xffffffffffffffffL << (128 - pref_len));
520  }
521 }
522 
523 void
524 ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
525  ip6_address_t * res)
526 {
527  u64 not_mask;
528  if (plen == 0)
529  {
530  res->as_u64[0] = 0xffffffffffffffffL;
531  res->as_u64[1] = 0xffffffffffffffffL;
532  }
533  else if (plen <= 64)
534  {
535  not_mask = ((u64) 1 << (64 - plen)) - 1;
536  res->as_u64[0] = clib_net_to_host_u64 (ip->as_u64[0]) + not_mask;
537  res->as_u64[1] = 0xffffffffffffffffL;
538  }
539  else
540  {
541  not_mask = ((u64) 1 << (128 - plen)) - 1;
542  res->as_u64[1] = clib_net_to_host_u64 (ip->as_u64[1]) + not_mask;
543  }
544 }
545 
546 u32
547 ip6_mask_to_preflen (ip6_address_t * mask)
548 {
549  if (mask->as_u64[1] != 0)
550  return 128 - log2_first_set (clib_net_to_host_u64 (mask->as_u64[1]));
551  if (mask->as_u64[0] != 0)
552  return 64 - log2_first_set (clib_net_to_host_u64 (mask->as_u64[0]));
553  return 0;
554 }
555 
556 /*
557  * fd.io coding-style-patch-verification: ON
558  *
559  * Local Variables:
560  * eval: (c-set-style "gnu")
561  * End:
562  */
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:212
#define ip_addr_v6(_a)
Definition: ip_types.h:59
void ip_prefix_to_fib_prefix(const ip_prefix_t *ip_prefix, fib_prefix_t *fib_prefix)
convert from a LISP to a FIB prefix
Definition: ip_types.c:395
#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:403
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:432
static uword log2_first_set(uword x)
Definition: clib.h:282
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:502
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 ip4_mask_to_preflen(ip4_address_t *mask)
Definition: ip_types.c:485
u8 len
Definition: ip_types.h:83
ip46_address_t ip
Definition: ip_types.h:49
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
vl_api_prefix_t prefix
Definition: ip.api:144
u16 mask
Definition: flow_types.api:52
#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:111
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:362
static uword pow2_mask(uword x)
Definition: clib.h:237
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
Aggregate type for a prefix.
Definition: fib_types.h:203
void ip_prefix_normalize(ip_prefix_t *a)
Definition: ip_types.c:342
void ip_address_to_fib_prefix(const ip_address_t *addr, fib_prefix_t *prefix)
convert from a IP address to a FIB prefix
Definition: ip_types.c:268
unsigned int u32
Definition: types.h:88
void ip6_address_normalize(ip6_address_t *ip6, u8 preflen)
Definition: ip_types.c:456
void ip6_prefix_max_address_host_order(ip6_address_t *ip, u8 plen, ip6_address_t *res)
Definition: ip_types.c:524
u16 fp_len
The mask length.
Definition: fib_types.h:207
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:476
void ip4_prefix_max_address_host_order(ip4_address_t *ip, u8 plen, ip4_address_t *res)
Definition: ip_types.c:493
void ip_address_from_46(const ip46_address_t *nh, fib_protocol_t fproto, ip_address_t *ip)
Definition: ip_types.c:257
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:226
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:417
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:547
ip_address_t addr
Definition: ip_types.h:82
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:446
static void ip_prefix_normalize_ip4(ip4_address_t *ip4, u8 preflen)
Definition: ip_types.c:289
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:306
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:368