2 * Copyright (c) 2020 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.h>
20 * @brief IP prefix management on interfaces
24 ip_interface_address_find (ip_lookup_main_t * lm,
25 void *addr_fib, u32 address_length)
27 uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib);
36 ip_interface_address_add (ip_lookup_main_t * lm,
39 u32 address_length, u32 * result_if_address_index)
41 vnet_main_t *vnm = vnet_get_main ();
42 ip_interface_address_t *a, *prev;
43 u32 pi; /* previous index */
45 u32 hi; /* head index */
47 /* Verify given length. */
48 if ((address_length == 0) ||
49 (lm->is_ip6 && address_length > 128) ||
50 (!lm->is_ip6 && address_length > 32))
52 vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
53 return clib_error_create
54 ("%U wrong length for interface %U",
55 lm->format_address_and_length, addr_fib,
56 address_length, format_vnet_sw_if_index_name, vnm, sw_if_index);
59 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
62 pool_get_zero (lm->if_address_pool, a);
64 ai = a - lm->if_address_pool;
65 hi = pi = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
68 while (pi != (u32) ~ 0)
70 prev = pool_elt_at_index (lm->if_address_pool, pi);
71 pi = prev->next_this_sw_interface;
73 pi = prev ? prev - lm->if_address_pool : (u32) ~ 0;
75 a->address_key = mhash_set (&lm->address_to_if_address_index,
76 addr_fib, ai, /* old_value */ 0);
77 a->address_length = address_length;
78 a->sw_if_index = sw_if_index;
80 a->prev_this_sw_interface = pi;
81 a->next_this_sw_interface = ~0;
83 prev->next_this_sw_interface = ai;
85 lm->if_address_pool_index_by_sw_if_index[sw_if_index] =
88 *result_if_address_index = ai;
94 ip_interface_address_del (ip_lookup_main_t * lm,
95 u32 address_index, void *addr_fib)
97 ip_interface_address_t *a, *prev, *next;
99 a = pool_elt_at_index (lm->if_address_pool, address_index);
101 if (a->prev_this_sw_interface != ~0)
103 prev = pool_elt_at_index (lm->if_address_pool,
104 a->prev_this_sw_interface);
105 prev->next_this_sw_interface = a->next_this_sw_interface;
107 if (a->next_this_sw_interface != ~0)
109 next = pool_elt_at_index (lm->if_address_pool,
110 a->next_this_sw_interface);
111 next->prev_this_sw_interface = a->prev_this_sw_interface;
113 if (a->prev_this_sw_interface == ~0)
114 lm->if_address_pool_index_by_sw_if_index[a->sw_if_index] =
115 a->next_this_sw_interface;
118 if ((a->next_this_sw_interface == ~0) && (a->prev_this_sw_interface == ~0))
119 lm->if_address_pool_index_by_sw_if_index[a->sw_if_index] = ~0;
121 mhash_unset (&lm->address_to_if_address_index, addr_fib,
123 pool_put (lm->if_address_pool, a);
127 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4)
129 ip_interface_address_t *ia = 0;
133 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
136 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
138 ip4 = ip_interface_address_get_address (lm4, ia);
139 if (ip4_address_compare (ip4, &ip->ip4) == 0)
146 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
149 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
151 ip6 = ip_interface_address_get_address (lm6, ia);
152 if (ip6_address_compare (ip6, &ip->ip6) == 0)
161 ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
163 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
164 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
165 ip_interface_address_t *ia = 0;
170 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
172 return ip_interface_address_get_address (lm4, ia);
179 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
182 rv = ip_interface_address_get_address (lm6, ia);
183 /* Trying to use a link-local ip6 src address is a fool's errand */
184 if (!ip6_address_is_link_local_unicast (rv))
194 ip_interface_address_mark_one_interface (vnet_main_t * vnm,
195 vnet_sw_interface_t * si, void *ctx)
197 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
198 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
199 ip_interface_address_t *ia = 0;
202 foreach_ip_interface_address (lm4, ia, si->sw_if_index, 1 /* unnumbered */ ,
204 ia->flags |= IP_INTERFACE_ADDRESS_FLAG_STALE;
206 foreach_ip_interface_address (lm6, ia, si->sw_if_index, 1 /* unnumbered */ ,
208 ia->flags |= IP_INTERFACE_ADDRESS_FLAG_STALE;
212 return (WALK_CONTINUE);
216 ip_interface_address_mark (void)
218 vnet_sw_interface_walk (vnet_get_main (),
219 ip_interface_address_mark_one_interface, NULL);
223 ip_interface_address_sweep_one_interface (vnet_main_t * vnm,
224 vnet_sw_interface_t * si, void *ctx)
226 vlib_main_t *vm = vlib_get_main ();
227 ip4_address_t *ip4_addrs = 0;
228 ip6_address_t *ip6_addrs = 0;
229 ip4_main_t *im4 = &ip4_main;
230 ip6_main_t *im6 = &ip6_main;
231 ip_interface_address_t *ia;
237 foreach_ip_interface_address (&im4->lookup_main, ia, si->sw_if_index, 1,
239 if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE)
241 ip4_address_t * x = (ip4_address_t *)
242 ip_interface_address_get_address (&im4->lookup_main, ia);
243 vec_add1 (ip4_addrs, x[0]);
244 vec_add1 (ip4_masks, ia->address_length);
248 foreach_ip_interface_address (&im6->lookup_main, ia, si->sw_if_index, 1,
250 if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE)
252 ip6_address_t * x = (ip6_address_t *)
253 ip_interface_address_get_address (&im6->lookup_main, ia);
254 vec_add1 (ip6_addrs, x[0]);
255 vec_add1 (ip6_masks, ia->address_length);
260 for (i = 0; i < vec_len (ip4_addrs); i++)
261 ip4_add_del_interface_address (vm, si->sw_if_index, &ip4_addrs[i],
262 ip4_masks[i], 1 /* is_del */ );
263 for (i = 0; i < vec_len (ip6_addrs); i++)
264 ip6_add_del_interface_address (vm, si->sw_if_index, &ip6_addrs[i],
265 ip6_masks[i], 1 /* is_del */ );
267 vec_free (ip4_addrs);
268 vec_free (ip4_masks);
269 vec_free (ip6_addrs);
270 vec_free (ip6_masks);
272 return (WALK_CONTINUE);
276 ip_interface_address_sweep (void)
278 vnet_sw_interface_walk (vnet_get_main (),
279 ip_interface_address_sweep_one_interface, NULL);
283 * fd.io coding-style-patch-verification: ON
286 * eval: (c-set-style "gnu")