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:
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 #include <vnet/ip/ip_types.h>
17 #include <vnet/ip/format.h>
18 #include <vnet/ip/ip.h>
21 format_ip_address (u8 * s, va_list * args)
23 ip_address_t *a = va_arg (*args, ip_address_t *);
24 u8 ver = ip_addr_version (a);
27 return format (s, "%U", format_ip4_address, &ip_addr_v4 (a));
29 else if (ver == AF_IP6)
31 return format (s, "%U", format_ip6_address, &ip_addr_v6 (a));
35 clib_warning ("Can't format IP version %d!", ver);
41 unformat_ip_address (unformat_input_t * input, va_list * args)
43 ip_address_t *a = va_arg (*args, ip_address_t *);
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;
56 format_ip_prefix (u8 * s, va_list * args)
58 ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
59 return format (s, "%U/%d", format_ip_address, &ip_prefix_addr (a),
64 unformat_ip_prefix (unformat_input_t * input, va_list * args)
66 ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
67 if (unformat (input, "%U/%d", unformat_ip_address, &ip_prefix_addr (a),
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)))
73 clib_warning ("Prefix length to big: %d!", ip_prefix_len (a));
76 ip_prefix_normalize (a);
84 ip_address_size (const ip_address_t * a)
86 switch (ip_addr_version (a))
89 return sizeof (ip4_address_t);
92 return sizeof (ip6_address_t);
99 ip_address_is_zero (const ip_address_t * ip)
101 switch (ip_addr_version (ip))
104 return (ip_addr_v4 (ip).as_u32 == 0);
106 return (ip_addr_v6 (ip).as_u64[0] == 0 &&
107 ip_addr_v6 (ip).as_u64[1] == 0);
114 ip_address_cmp (const ip_address_t * ip1, const ip_address_t * ip2)
117 if (ip_addr_version (ip1) != ip_addr_version (ip2))
119 res = ip46_address_cmp (&ip_addr_46 (ip1), &ip_addr_46 (ip2));
130 ip_address_copy (ip_address_t * dst, const ip_address_t * src)
132 if (AF_IP4 == ip_addr_version (src))
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;
141 clib_memcpy (dst, src, sizeof (ip_address_t));
146 ip_addr_bytes (ip_address_t * ip)
151 return (u8 *) & ip_addr_v4 (ip);
153 return (u8 *) & ip_addr_v6 (ip);
161 ip_address_copy_addr (void *dst, const ip_address_t * src)
163 switch (src->version)
166 clib_memcpy (dst, &ip_addr_v4 (src), ip_address_size (src));
169 clib_memcpy (dst, &ip_addr_v6 (src), ip_address_size (src));
175 ip_version_to_size (ip_address_family_t af)
180 return sizeof (ip4_address_t);
183 return sizeof (ip6_address_t);
190 ip_address_family_to_link_type (ip_address_family_t af)
195 return (VNET_LINK_IP4);
197 return (VNET_LINK_IP6);
200 return (VNET_LINK_IP4);
205 ip_address_set (ip_address_t * dst, const void *src, u8 version)
207 ip_addr_version (dst) = version;
212 ip_addr_v4 (dst) = *(ip4_address_t *) src;
215 ip_addr_v6 (dst) = *(ip6_address_t *) src;
221 ip_address_family_to_fib_proto (ip_address_family_t af)
226 return (FIB_PROTOCOL_IP4);
228 return (FIB_PROTOCOL_IP6);
231 return (FIB_PROTOCOL_IP4);
235 ip_address_family_from_fib_proto (fib_protocol_t fp)
239 case FIB_PROTOCOL_IP4:
241 case FIB_PROTOCOL_IP6:
243 case FIB_PROTOCOL_MPLS:
250 ip_address_to_46 (const ip_address_t * addr, ip46_address_t * a)
252 *a = ip_addr_46 (addr);
253 return (ip_address_family_to_fib_proto (ip_addr_version (addr)));
257 ip_address_from_46 (const ip46_address_t * nh,
258 fib_protocol_t fproto, ip_address_t * ip)
260 ip_addr_46 (ip) = *nh;
261 ip_addr_version (ip) = ip_address_family_from_fib_proto (fproto);
265 * convert from a IP address to a FIB prefix
268 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
270 if (addr->version == AF_IP4)
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));
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));
285 prefix->___fp___pad = 0;
289 ip_prefix_normalize_ip4 (ip4_address_t * ip4, u8 preflen)
300 mask = pow2_mask (preflen) << (32 - preflen);
301 mask = clib_host_to_net_u32 (mask);
302 ip4->data_u32 &= mask;
306 ip_prefix_normalize_ip6 (ip6_address_t * ip6, u8 preflen)
314 clib_memset (mask_6, 0, sizeof (mask_6));
323 m = (u32 *) & mask_6[0];
325 for (j = 0; j < i0; j++)
332 m[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
335 for (j = 0; j < sizeof (mask_6); j++)
337 ip6->as_u8[j] &= mask_6[j];
342 ip_prefix_normalize (ip_prefix_t * a)
344 u8 preflen = ip_prefix_len (a);
346 switch (ip_prefix_version (a))
349 ip_prefix_normalize_ip4 (&ip_prefix_v4 (a), preflen);
353 ip_prefix_normalize_ip6 (&ip_prefix_v6 (a), preflen);
362 ip_prefix_copy (void *dst, void *src)
364 clib_memcpy (dst, src, sizeof (ip_prefix_t));
368 ip_prefix_cmp (ip_prefix_t * p1, ip_prefix_t * p2)
372 ip_prefix_normalize (p1);
373 ip_prefix_normalize (p2);
375 cmp = ip_address_cmp (&ip_prefix_addr (p1), &ip_prefix_addr (p2));
378 if (ip_prefix_len (p1) < ip_prefix_len (p2))
384 if (ip_prefix_len (p1) > ip_prefix_len (p2))
392 * convert from a LISP to a FIB prefix
395 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
396 fib_prefix_t * fib_prefix)
398 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
399 fib_prefix->fp_len = ip_prefix->len;
403 ip4_prefix_validate (const ip_prefix_t * ip)
405 ip4_address_t ip4_addr, ip4_mask;
407 if (ip_prefix_len (ip) > 32)
410 ip4_addr = ip_prefix_v4 (ip);
411 ip4_preflen_to_mask (ip_prefix_len (ip), &ip4_mask);
413 return ((ip4_addr.as_u32 & ip4_mask.as_u32) == ip4_addr.as_u32);
417 ip6_prefix_validate (const ip_prefix_t * ip)
419 ip6_address_t ip6_addr, ip6_mask;
421 if (ip_prefix_len (ip) > 128)
424 ip6_addr = ip_prefix_v6 (ip);
425 ip6_preflen_to_mask (ip_prefix_len (ip), &ip6_mask);
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]));
432 ip_prefix_validate (const ip_prefix_t * ip)
434 switch (ip_prefix_version (ip))
437 return (ip4_prefix_validate (ip));
439 return (ip6_prefix_validate (ip));
446 ip4_address_normalize (ip4_address_t * ip4, u8 preflen)
448 ASSERT (preflen <= 32);
452 ip4->data_u32 &= clib_net_to_host_u32 (0xffffffff << (32 - preflen));
456 ip6_address_normalize (ip6_address_t * ip6, u8 preflen)
458 ASSERT (preflen <= 128);
464 else if (preflen <= 64)
467 clib_host_to_net_u64 (0xffffffffffffffffL << (64 - preflen));
472 clib_host_to_net_u64 (0xffffffffffffffffL << (128 - preflen));
476 ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip)
481 ip->as_u32 = clib_host_to_net_u32 (~((1 << (32 - pref_len)) - 1));
485 ip4_mask_to_preflen (ip4_address_t * mask)
487 if (mask->as_u32 == 0)
489 return (32 - log2_first_set (clib_net_to_host_u32 (mask->as_u32)));
493 ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
497 not_mask = (1 << (32 - plen)) - 1;
498 res->as_u32 = clib_net_to_host_u32 (ip->as_u32) + not_mask;
502 ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask)
509 else if (pref_len <= 64)
512 clib_host_to_net_u64 (0xffffffffffffffffL << (64 - pref_len));
517 mask->as_u64[0] = 0xffffffffffffffffL;
519 clib_host_to_net_u64 (0xffffffffffffffffL << (128 - pref_len));
524 ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
530 res->as_u64[0] = 0xffffffffffffffffL;
531 res->as_u64[1] = 0xffffffffffffffffL;
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;
541 not_mask = ((u64) 1 << (128 - plen)) - 1;
542 res->as_u64[1] = clib_net_to_host_u64 (ip->as_u64[1]) + not_mask;
547 ip6_mask_to_preflen (ip6_address_t * mask)
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]));
557 * fd.io coding-style-patch-verification: ON
560 * eval: (c-set-style "gnu")