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_cmp (const ip_address_t * ip1, const ip_address_t * ip2)
101 if (ip_addr_version (ip1) != ip_addr_version (ip2))
104 memcmp (&ip_addr_addr (ip1), &ip_addr_addr (ip2), ip_address_size (ip1));
115 ip_address_copy (ip_address_t * dst, const ip_address_t * src)
117 if (AF_IP4 == ip_addr_version (src))
119 /* don't copy any garbage from the union */
120 clib_memset (dst, 0, sizeof (*dst));
121 dst->ip.v4 = src->ip.v4;
122 dst->version = AF_IP4;
126 clib_memcpy (dst, src, sizeof (ip_address_t));
131 ip_address_copy_addr (void *dst, const ip_address_t * src)
133 clib_memcpy (dst, src, ip_address_size (src));
137 ip_version_to_size (u8 ver)
142 return sizeof (ip4_address_t);
145 return sizeof (ip6_address_t);
152 ip_address_set (ip_address_t * dst, const void *src, u8 version)
154 clib_memcpy (dst, src, ip_version_to_size (version));
155 ip_addr_version (dst) = version;
159 ip_address_to_46 (const ip_address_t * addr,
160 ip46_address_t * a, fib_protocol_t * proto)
162 *proto = (AF_IP4 == ip_addr_version (addr) ?
163 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
166 case FIB_PROTOCOL_IP4:
167 ip46_address_set_ip4 (a, &addr->ip.v4);
169 case FIB_PROTOCOL_IP6:
170 a->ip6 = addr->ip.v6;
179 ip_prefix_normalize_ip4 (ip4_address_t * ip4, u8 preflen)
190 mask = pow2_mask (preflen) << (32 - preflen);
191 mask = clib_host_to_net_u32 (mask);
192 ip4->data_u32 &= mask;
196 ip_prefix_normalize_ip6 (ip6_address_t * ip6, u8 preflen)
204 clib_memset (mask_6, 0, sizeof (mask_6));
213 m = (u32 *) & mask_6[0];
215 for (j = 0; j < i0; j++)
222 m[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
225 for (j = 0; j < sizeof (mask_6); j++)
227 ip6->as_u8[j] &= mask_6[j];
232 ip_prefix_normalize (ip_prefix_t * a)
234 u8 preflen = ip_prefix_len (a);
236 switch (ip_prefix_version (a))
239 ip_prefix_normalize_ip4 (&ip_prefix_v4 (a), preflen);
243 ip_prefix_normalize_ip6 (&ip_prefix_v6 (a), preflen);
252 ip_prefix_copy (void *dst, void *src)
254 clib_memcpy (dst, src, sizeof (ip_prefix_t));
258 ip_prefix_cmp (ip_prefix_t * p1, ip_prefix_t * p2)
262 ip_prefix_normalize (p1);
263 ip_prefix_normalize (p2);
265 cmp = ip_address_cmp (&ip_prefix_addr (p1), &ip_prefix_addr (p2));
268 if (ip_prefix_len (p1) < ip_prefix_len (p2))
274 if (ip_prefix_len (p1) > ip_prefix_len (p2))
282 * fd.io coding-style-patch-verification: ON
285 * eval: (c-set-style "gnu")