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>
20 format_ip_address (u8 * s, va_list * args)
22 ip_address_t *a = va_arg (*args, ip_address_t *);
23 u8 ver = ip_addr_version (a);
26 return format (s, "%U", format_ip4_address, &ip_addr_v4 (a));
28 else if (ver == AF_IP6)
30 return format (s, "%U", format_ip6_address, &ip_addr_v6 (a));
34 clib_warning ("Can't format IP version %d!", ver);
40 unformat_ip_address (unformat_input_t * input, va_list * args)
42 ip_address_t *a = va_arg (*args, ip_address_t *);
44 clib_memset (a, 0, sizeof (*a));
45 if (unformat (input, "%U", unformat_ip4_address, &ip_addr_v4 (a)))
46 ip_addr_version (a) = AF_IP4;
47 else if (unformat_user (input, unformat_ip6_address, &ip_addr_v6 (a)))
48 ip_addr_version (a) = AF_IP6;
55 format_ip_prefix (u8 * s, va_list * args)
57 ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
58 return format (s, "%U/%d", format_ip_address, &ip_prefix_addr (a),
63 unformat_ip_prefix (unformat_input_t * input, va_list * args)
65 ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
66 if (unformat (input, "%U/%d", unformat_ip_address, &ip_prefix_addr (a),
69 if ((ip_prefix_version (a) == AF_IP4 && 32 < ip_prefix_len (a)) ||
70 (ip_prefix_version (a) == AF_IP6 && 128 < ip_prefix_len (a)))
72 clib_warning ("Prefix length to big: %d!", ip_prefix_len (a));
75 ip_prefix_normalize (a);
83 ip_address_size (const ip_address_t * a)
85 switch (ip_addr_version (a))
88 return sizeof (ip4_address_t);
91 return sizeof (ip6_address_t);
98 ip_address_is_zero (const ip_address_t * ip)
100 switch (ip_addr_version (ip))
103 return (ip_addr_v4 (ip).as_u32 == 0);
105 return (ip_addr_v6 (ip).as_u64[0] == 0 &&
106 ip_addr_v6 (ip).as_u64[1] == 0);
113 ip_address_cmp (const ip_address_t * ip1, const ip_address_t * ip2)
116 if (ip_addr_version (ip1) != ip_addr_version (ip2))
119 memcmp (&ip_addr_addr (ip1), &ip_addr_addr (ip2), ip_address_size (ip1));
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 dst->ip.v4 = src->ip.v4;
137 dst->version = AF_IP4;
141 clib_memcpy (dst, src, sizeof (ip_address_t));
146 ip_address_copy_addr (void *dst, const ip_address_t * src)
148 clib_memcpy (dst, src, ip_address_size (src));
152 ip_version_to_size (u8 ver)
157 return sizeof (ip4_address_t);
160 return sizeof (ip6_address_t);
167 ip_address_set (ip_address_t * dst, const void *src, u8 version)
169 clib_memcpy (dst, src, ip_version_to_size (version));
170 ip_addr_version (dst) = version;
174 ip_address_to_46 (const ip_address_t * addr, ip46_address_t * a)
176 fib_protocol_t proto;
178 proto = (AF_IP4 == ip_addr_version (addr) ?
179 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
182 case FIB_PROTOCOL_IP4:
183 ip46_address_set_ip4 (a, &addr->ip.v4);
185 case FIB_PROTOCOL_IP6:
186 a->ip6 = addr->ip.v6;
197 ip_address_from_46 (const ip46_address_t * nh,
198 fib_protocol_t fproto, ip_address_t * ip)
202 case FIB_PROTOCOL_IP4:
203 clib_memset (ip, 0, sizeof (*ip));
204 ip_address_set (ip, &nh->ip4, AF_IP4);
206 case FIB_PROTOCOL_IP6:
207 ip_address_set (ip, &nh->ip6, AF_IP6);
216 ip_prefix_normalize_ip4 (ip4_address_t * ip4, u8 preflen)
227 mask = pow2_mask (preflen) << (32 - preflen);
228 mask = clib_host_to_net_u32 (mask);
229 ip4->data_u32 &= mask;
233 ip_prefix_normalize_ip6 (ip6_address_t * ip6, u8 preflen)
241 clib_memset (mask_6, 0, sizeof (mask_6));
250 m = (u32 *) & mask_6[0];
252 for (j = 0; j < i0; j++)
259 m[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
262 for (j = 0; j < sizeof (mask_6); j++)
264 ip6->as_u8[j] &= mask_6[j];
269 ip_prefix_normalize (ip_prefix_t * a)
271 u8 preflen = ip_prefix_len (a);
273 switch (ip_prefix_version (a))
276 ip_prefix_normalize_ip4 (&ip_prefix_v4 (a), preflen);
280 ip_prefix_normalize_ip6 (&ip_prefix_v6 (a), preflen);
289 ip_prefix_copy (void *dst, void *src)
291 clib_memcpy (dst, src, sizeof (ip_prefix_t));
295 ip_prefix_cmp (ip_prefix_t * p1, ip_prefix_t * p2)
299 ip_prefix_normalize (p1);
300 ip_prefix_normalize (p2);
302 cmp = ip_address_cmp (&ip_prefix_addr (p1), &ip_prefix_addr (p2));
305 if (ip_prefix_len (p1) < ip_prefix_len (p2))
311 if (ip_prefix_len (p1) > ip_prefix_len (p2))
319 * fd.io coding-style-patch-verification: ON
322 * eval: (c-set-style "gnu")