blob: ca1938f651a023d5ea6dad52572bcb2120a48d51 [file] [log] [blame]
Neale Ranns59f71132020-04-08 12:19:38 +00001/*
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:
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.h>
17
18/**
19 * @file
20 * @brief IP prefix management on interfaces
21 */
22
23u32
24ip_interface_address_find (ip_lookup_main_t * lm,
25 void *addr_fib, u32 address_length)
26{
27 uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib);
28
29 if (p)
30 return (p[0]);
31
32 return (~0);
33}
34
35clib_error_t *
36ip_interface_address_add (ip_lookup_main_t * lm,
37 u32 sw_if_index,
38 void *addr_fib,
39 u32 address_length, u32 * result_if_address_index)
40{
41 vnet_main_t *vnm = vnet_get_main ();
42 ip_interface_address_t *a, *prev;
43 u32 pi; /* previous index */
44 u32 ai;
45 u32 hi; /* head index */
46
47 /* Verify given length. */
48 if ((address_length == 0) ||
49 (lm->is_ip6 && address_length > 128) ||
50 (!lm->is_ip6 && address_length > 32))
51 {
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);
57 }
58
59 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
60 sw_if_index, ~0);
61
62 pool_get_zero (lm->if_address_pool, a);
63
64 ai = a - lm->if_address_pool;
65 hi = pi = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
66
67 prev = 0;
68 while (pi != (u32) ~ 0)
69 {
70 prev = pool_elt_at_index (lm->if_address_pool, pi);
71 pi = prev->next_this_sw_interface;
72 }
73 pi = prev ? prev - lm->if_address_pool : (u32) ~ 0;
74
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;
79 a->flags = 0;
80 a->prev_this_sw_interface = pi;
81 a->next_this_sw_interface = ~0;
82 if (prev)
83 prev->next_this_sw_interface = ai;
84
85 lm->if_address_pool_index_by_sw_if_index[sw_if_index] =
86 (hi != ~0) ? hi : ai;
87
88 *result_if_address_index = ai;
89
90 return (NULL);
91}
92
yedgdbd366b2020-05-14 10:51:53 +080093clib_error_t *
Neale Ranns59f71132020-04-08 12:19:38 +000094ip_interface_address_del (ip_lookup_main_t * lm,
yedgdbd366b2020-05-14 10:51:53 +080095 vnet_main_t * vnm,
96 u32 address_index, void *addr_fib,
97 u32 address_length, u32 sw_if_index)
Neale Ranns59f71132020-04-08 12:19:38 +000098{
99 ip_interface_address_t *a, *prev, *next;
100
101 a = pool_elt_at_index (lm->if_address_pool, address_index);
102
yedgdbd366b2020-05-14 10:51:53 +0800103 if (a->sw_if_index != sw_if_index)
104 {
105 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
106 return clib_error_create ("%U not found for interface %U",
107 lm->format_address_and_length,
108 addr_fib, address_length,
109 format_vnet_sw_if_index_name,
110 vnet_get_main (), sw_if_index);
111 }
112
Neale Ranns59f71132020-04-08 12:19:38 +0000113 if (a->prev_this_sw_interface != ~0)
114 {
115 prev = pool_elt_at_index (lm->if_address_pool,
116 a->prev_this_sw_interface);
117 prev->next_this_sw_interface = a->next_this_sw_interface;
118 }
119 if (a->next_this_sw_interface != ~0)
120 {
121 next = pool_elt_at_index (lm->if_address_pool,
122 a->next_this_sw_interface);
123 next->prev_this_sw_interface = a->prev_this_sw_interface;
124
125 if (a->prev_this_sw_interface == ~0)
126 lm->if_address_pool_index_by_sw_if_index[a->sw_if_index] =
127 a->next_this_sw_interface;
128 }
129
130 if ((a->next_this_sw_interface == ~0) && (a->prev_this_sw_interface == ~0))
131 lm->if_address_pool_index_by_sw_if_index[a->sw_if_index] = ~0;
132
133 mhash_unset (&lm->address_to_if_address_index, addr_fib,
134 /* old_value */ 0);
135 pool_put (lm->if_address_pool, a);
yedgdbd366b2020-05-14 10:51:53 +0800136 return NULL;
Neale Ranns59f71132020-04-08 12:19:38 +0000137}
138
139u8
140ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4)
141{
142 ip_interface_address_t *ia = 0;
143
144 if (is_ip4)
145 {
146 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
147 ip4_address_t *ip4;
Neale Ranns59f71132020-04-08 12:19:38 +0000148 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
149 ({
150 ip4 = ip_interface_address_get_address (lm4, ia);
151 if (ip4_address_compare (ip4, &ip->ip4) == 0)
152 return 1;
153 }));
Neale Ranns59f71132020-04-08 12:19:38 +0000154 }
155 else
156 {
157 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
158 ip6_address_t *ip6;
Neale Ranns59f71132020-04-08 12:19:38 +0000159 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
160 ({
161 ip6 = ip_interface_address_get_address (lm6, ia);
162 if (ip6_address_compare (ip6, &ip->ip6) == 0)
163 return 1;
164 }));
Neale Ranns59f71132020-04-08 12:19:38 +0000165 }
166 return 0;
167}
168
169void *
170ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
171{
172 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
173 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
174 ip_interface_address_t *ia = 0;
175
176 if (is_ip4)
177 {
Neale Ranns59f71132020-04-08 12:19:38 +0000178 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
179 ({
180 return ip_interface_address_get_address (lm4, ia);
181 }));
Neale Ranns59f71132020-04-08 12:19:38 +0000182 }
183 else
184 {
Neale Ranns59f71132020-04-08 12:19:38 +0000185 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
186 ({
187 ip6_address_t *rv;
188 rv = ip_interface_address_get_address (lm6, ia);
189 /* Trying to use a link-local ip6 src address is a fool's errand */
190 if (!ip6_address_is_link_local_unicast (rv))
191 return rv;
192 }));
Neale Ranns59f71132020-04-08 12:19:38 +0000193 }
194
195 return 0;
196}
197
Stanislav Zaikin3bad8b62022-04-25 19:11:36 +0200198walk_rc_t
199ip_interface_address_mark_one_interface (vnet_main_t *vnm,
200 vnet_sw_interface_t *si, void *ctx)
Neale Ranns59f71132020-04-08 12:19:38 +0000201{
202 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
203 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
204 ip_interface_address_t *ia = 0;
205
Neale Ranns59f71132020-04-08 12:19:38 +0000206 foreach_ip_interface_address (lm4, ia, si->sw_if_index, 1 /* unnumbered */ ,
207 ({
208 ia->flags |= IP_INTERFACE_ADDRESS_FLAG_STALE;
209 }));
210 foreach_ip_interface_address (lm6, ia, si->sw_if_index, 1 /* unnumbered */ ,
211 ({
212 ia->flags |= IP_INTERFACE_ADDRESS_FLAG_STALE;
213 }));
Neale Ranns59f71132020-04-08 12:19:38 +0000214
215 return (WALK_CONTINUE);
216}
217
218void
219ip_interface_address_mark (void)
220{
221 vnet_sw_interface_walk (vnet_get_main (),
222 ip_interface_address_mark_one_interface, NULL);
223}
224
225static walk_rc_t
226ip_interface_address_sweep_one_interface (vnet_main_t * vnm,
227 vnet_sw_interface_t * si, void *ctx)
228{
229 vlib_main_t *vm = vlib_get_main ();
230 ip4_address_t *ip4_addrs = 0;
231 ip6_address_t *ip6_addrs = 0;
232 ip4_main_t *im4 = &ip4_main;
233 ip6_main_t *im6 = &ip6_main;
234 ip_interface_address_t *ia;
235 u32 *ip6_masks = 0;
236 u32 *ip4_masks = 0;
237 int i;
238
Neale Ranns59f71132020-04-08 12:19:38 +0000239 foreach_ip_interface_address (&im4->lookup_main, ia, si->sw_if_index, 1,
240 ({
241 if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE)
242 {
243 ip4_address_t * x = (ip4_address_t *)
244 ip_interface_address_get_address (&im4->lookup_main, ia);
245 vec_add1 (ip4_addrs, x[0]);
246 vec_add1 (ip4_masks, ia->address_length);
247 }
248 }));
249
250 foreach_ip_interface_address (&im6->lookup_main, ia, si->sw_if_index, 1,
251 ({
252 if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE)
253 {
254 ip6_address_t * x = (ip6_address_t *)
255 ip_interface_address_get_address (&im6->lookup_main, ia);
256 vec_add1 (ip6_addrs, x[0]);
257 vec_add1 (ip6_masks, ia->address_length);
258 }
259 }));
Neale Ranns59f71132020-04-08 12:19:38 +0000260
261 for (i = 0; i < vec_len (ip4_addrs); i++)
262 ip4_add_del_interface_address (vm, si->sw_if_index, &ip4_addrs[i],
263 ip4_masks[i], 1 /* is_del */ );
264 for (i = 0; i < vec_len (ip6_addrs); i++)
265 ip6_add_del_interface_address (vm, si->sw_if_index, &ip6_addrs[i],
266 ip6_masks[i], 1 /* is_del */ );
267
268 vec_free (ip4_addrs);
269 vec_free (ip4_masks);
270 vec_free (ip6_addrs);
271 vec_free (ip6_masks);
272
273 return (WALK_CONTINUE);
274}
275
276void
277ip_interface_address_sweep (void)
278{
279 vnet_sw_interface_walk (vnet_get_main (),
280 ip_interface_address_sweep_one_interface, NULL);
281}
282
283/*
284 * fd.io coding-style-patch-verification: ON
285 *
286 * Local Variables:
287 * eval: (c-set-style "gnu")
288 * End:
289 */