]>
git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
2 * Prefix related functions.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "sockunion.h"
30 #include "lib_errors.h"
32 DEFINE_MTYPE_STATIC(LIB
, PREFIX
, "Prefix")
33 DEFINE_MTYPE_STATIC(LIB
, PREFIX_FLOWSPEC
, "Prefix Flowspec")
36 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
37 0xf8, 0xfc, 0xfe, 0xff};
39 static const struct in6_addr maskbytes6
[] = {
40 /* /0 */ {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}},
43 {{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00}}},
46 {{{0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00}}},
49 {{{0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00, 0x00}}},
52 {{{0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00}}},
55 {{{0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00}}},
58 {{{0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00}}},
61 {{{0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00}}},
64 {{{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00}}},
67 {{{0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00}}},
70 {{{0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00}}},
73 {{{0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00}}},
76 {{{0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00}}},
79 {{{0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00}}},
82 {{{0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x00}}},
85 {{{0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00, 0x00}}},
88 {{{0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00}}},
91 {{{0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92 0x00, 0x00, 0x00, 0x00, 0x00}}},
94 {{{0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00, 0x00}}},
97 {{{0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00}}},
100 {{{0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00}}},
103 {{{0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00, 0x00}}},
106 {{{0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107 0x00, 0x00, 0x00, 0x00, 0x00}}},
109 {{{0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110 0x00, 0x00, 0x00, 0x00, 0x00}}},
112 {{{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00}}},
115 {{{0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
116 0x00, 0x00, 0x00, 0x00, 0x00}}},
118 {{{0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
119 0x00, 0x00, 0x00, 0x00, 0x00}}},
121 {{{0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122 0x00, 0x00, 0x00, 0x00, 0x00}}},
124 {{{0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00}}},
127 {{{0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 0x00, 0x00, 0x00, 0x00, 0x00}}},
130 {{{0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x00, 0x00}}},
133 {{{0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 0x00, 0x00, 0x00, 0x00, 0x00}}},
136 {{{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137 0x00, 0x00, 0x00, 0x00, 0x00}}},
139 {{{0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
140 0x00, 0x00, 0x00, 0x00, 0x00}}},
142 {{{0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143 0x00, 0x00, 0x00, 0x00, 0x00}}},
145 {{{0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146 0x00, 0x00, 0x00, 0x00, 0x00}}},
148 {{{0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149 0x00, 0x00, 0x00, 0x00, 0x00}}},
151 {{{0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152 0x00, 0x00, 0x00, 0x00, 0x00}}},
154 {{{0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155 0x00, 0x00, 0x00, 0x00, 0x00}}},
157 {{{0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
158 0x00, 0x00, 0x00, 0x00, 0x00}}},
160 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161 0x00, 0x00, 0x00, 0x00, 0x00}}},
163 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
164 0x00, 0x00, 0x00, 0x00, 0x00}}},
166 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
167 0x00, 0x00, 0x00, 0x00, 0x00}}},
169 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
170 0x00, 0x00, 0x00, 0x00, 0x00}}},
172 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
173 0x00, 0x00, 0x00, 0x00, 0x00}}},
175 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
176 0x00, 0x00, 0x00, 0x00, 0x00}}},
178 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
179 0x00, 0x00, 0x00, 0x00, 0x00}}},
181 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
182 0x00, 0x00, 0x00, 0x00, 0x00}}},
184 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
185 0x00, 0x00, 0x00, 0x00, 0x00}}},
187 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00,
188 0x00, 0x00, 0x00, 0x00, 0x00}}},
190 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00,
191 0x00, 0x00, 0x00, 0x00, 0x00}}},
193 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
194 0x00, 0x00, 0x00, 0x00, 0x00}}},
196 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00,
197 0x00, 0x00, 0x00, 0x00, 0x00}}},
199 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
200 0x00, 0x00, 0x00, 0x00, 0x00}}},
202 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
203 0x00, 0x00, 0x00, 0x00, 0x00}}},
205 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
206 0x00, 0x00, 0x00, 0x00, 0x00}}},
208 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
209 0x00, 0x00, 0x00, 0x00, 0x00}}},
211 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
212 0x00, 0x00, 0x00, 0x00, 0x00}}},
214 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00,
215 0x00, 0x00, 0x00, 0x00, 0x00}}},
217 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00,
218 0x00, 0x00, 0x00, 0x00, 0x00}}},
220 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00,
221 0x00, 0x00, 0x00, 0x00, 0x00}}},
223 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00,
224 0x00, 0x00, 0x00, 0x00, 0x00}}},
226 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00,
227 0x00, 0x00, 0x00, 0x00, 0x00}}},
229 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00,
230 0x00, 0x00, 0x00, 0x00, 0x00}}},
232 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
233 0x00, 0x00, 0x00, 0x00, 0x00}}},
235 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00,
236 0x00, 0x00, 0x00, 0x00, 0x00}}},
238 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
239 0x00, 0x00, 0x00, 0x00, 0x00}}},
241 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00,
242 0x00, 0x00, 0x00, 0x00, 0x00}}},
244 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00,
245 0x00, 0x00, 0x00, 0x00, 0x00}}},
247 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00,
248 0x00, 0x00, 0x00, 0x00, 0x00}}},
250 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00,
251 0x00, 0x00, 0x00, 0x00, 0x00}}},
253 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00,
254 0x00, 0x00, 0x00, 0x00, 0x00}}},
256 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
257 0x00, 0x00, 0x00, 0x00, 0x00}}},
259 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
260 0x00, 0x00, 0x00, 0x00, 0x00}}},
262 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00,
263 0x00, 0x00, 0x00, 0x00, 0x00}}},
265 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00,
266 0x00, 0x00, 0x00, 0x00, 0x00}}},
268 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00,
269 0x00, 0x00, 0x00, 0x00, 0x00}}},
271 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00,
272 0x00, 0x00, 0x00, 0x00, 0x00}}},
274 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00,
275 0x00, 0x00, 0x00, 0x00, 0x00}}},
277 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00,
278 0x00, 0x00, 0x00, 0x00, 0x00}}},
280 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
281 0x00, 0x00, 0x00, 0x00, 0x00}}},
283 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
284 0x00, 0x00, 0x00, 0x00, 0x00}}},
286 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
287 0x00, 0x00, 0x00, 0x00, 0x00}}},
289 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
290 0x00, 0x00, 0x00, 0x00, 0x00}}},
292 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
293 0x00, 0x00, 0x00, 0x00, 0x00}}},
295 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8,
296 0x00, 0x00, 0x00, 0x00, 0x00}}},
298 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
299 0x00, 0x00, 0x00, 0x00, 0x00}}},
301 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
302 0x00, 0x00, 0x00, 0x00, 0x00}}},
304 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
305 0x00, 0x00, 0x00, 0x00, 0x00}}},
307 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
308 0x80, 0x00, 0x00, 0x00, 0x00}}},
310 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
311 0xc0, 0x00, 0x00, 0x00, 0x00}}},
313 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
314 0xe0, 0x00, 0x00, 0x00, 0x00}}},
316 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
317 0xf0, 0x00, 0x00, 0x00, 0x00}}},
319 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
320 0xf8, 0x00, 0x00, 0x00, 0x00}}},
322 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
323 0xfc, 0x00, 0x00, 0x00, 0x00}}},
325 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
326 0xfe, 0x00, 0x00, 0x00, 0x00}}},
328 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
329 0xff, 0x00, 0x00, 0x00, 0x00}}},
331 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
332 0xff, 0x80, 0x00, 0x00, 0x00}}},
334 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
335 0xff, 0xc0, 0x00, 0x00, 0x00}}},
337 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
338 0xff, 0xe0, 0x00, 0x00, 0x00}}},
340 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
341 0xff, 0xf0, 0x00, 0x00, 0x00}}},
343 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
344 0xff, 0xf8, 0x00, 0x00, 0x00}}},
346 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
347 0xff, 0xfc, 0x00, 0x00, 0x00}}},
349 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
350 0xff, 0xfe, 0x00, 0x00, 0x00}}},
352 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
353 0xff, 0xff, 0x00, 0x00, 0x00}}},
355 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
356 0xff, 0xff, 0x80, 0x00, 0x00}}},
358 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
359 0xff, 0xff, 0xc0, 0x00, 0x00}}},
361 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
362 0xff, 0xff, 0xe0, 0x00, 0x00}}},
364 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
365 0xff, 0xff, 0xf0, 0x00, 0x00}}},
367 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
368 0xff, 0xff, 0xf8, 0x00, 0x00}}},
370 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
371 0xff, 0xff, 0xfc, 0x00, 0x00}}},
373 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
374 0xff, 0xff, 0xfe, 0x00, 0x00}}},
376 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
377 0xff, 0xff, 0xff, 0x00, 0x00}}},
379 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
380 0xff, 0xff, 0xff, 0x80, 0x00}}},
382 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
383 0xff, 0xff, 0xff, 0xc0, 0x00}}},
385 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
386 0xff, 0xff, 0xff, 0xe0, 0x00}}},
388 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
389 0xff, 0xff, 0xff, 0xf0, 0x00}}},
391 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
392 0xff, 0xff, 0xff, 0xf8, 0x00}}},
394 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
395 0xff, 0xff, 0xff, 0xfc, 0x00}}},
397 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
398 0xff, 0xff, 0xff, 0xfe, 0x00}}},
400 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
401 0xff, 0xff, 0xff, 0xff, 0x00}}},
403 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
404 0xff, 0xff, 0xff, 0xff, 0x80}}},
406 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
407 0xff, 0xff, 0xff, 0xff, 0xc0}}},
409 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
410 0xff, 0xff, 0xff, 0xff, 0xe0}}},
412 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
413 0xff, 0xff, 0xff, 0xff, 0xf0}}},
415 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
416 0xff, 0xff, 0xff, 0xff, 0xf8}}},
418 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
419 0xff, 0xff, 0xff, 0xff, 0xfc}}},
421 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
422 0xff, 0xff, 0xff, 0xff, 0xfe}}},
424 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
425 0xff, 0xff, 0xff, 0xff, 0xff}}}};
427 /* Number of bits in prefix type. */
432 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
434 void prefix_hexdump(const struct prefix
*p
)
436 char buf
[PREFIX_STRLEN
];
438 zlog_debug("prefix: %s",
439 prefix2str(p
, buf
, sizeof(buf
)));
440 zlog_hexdump(p
, sizeof(struct prefix
));
443 int is_zero_mac(struct ethaddr
*mac
)
447 for (i
= 0; i
< ETH_ALEN
; i
++) {
455 unsigned int prefix_bit(const uint8_t *prefix
, const uint16_t prefixlen
)
457 unsigned int offset
= prefixlen
/ 8;
458 unsigned int shift
= 7 - (prefixlen
% 8);
460 return (prefix
[offset
] >> shift
) & 1;
463 unsigned int prefix6_bit(const struct in6_addr
*prefix
, const uint16_t prefixlen
)
465 return prefix_bit((const uint8_t *)&prefix
->s6_addr
, prefixlen
);
468 int str2family(const char *string
)
470 if (!strcmp("ipv4", string
))
472 else if (!strcmp("ipv6", string
))
474 else if (!strcmp("ethernet", string
))
476 else if (!strcmp("evpn", string
))
481 const char *family2str(int family
)
496 /* Address Famiy Identifier to Address Family converter. */
497 int afi2family(afi_t afi
)
501 else if (afi
== AFI_IP6
)
503 else if (afi
== AFI_L2VPN
)
505 /* NOTE: EVPN code should NOT use this interface. */
509 afi_t
family2afi(int family
)
511 if (family
== AF_INET
)
513 else if (family
== AF_INET6
)
515 else if (family
== AF_ETHERNET
|| family
== AF_EVPN
)
520 const char *afi2str(afi_t afi
)
537 const char *safi2str(safi_t safi
)
550 case SAFI_LABELED_UNICAST
:
551 return "labeled-unicast";
559 /* If n includes p prefix then return 1 else return 0. */
560 int prefix_match(const struct prefix
*n
, const struct prefix
*p
)
564 const uint8_t *np
, *pp
;
566 /* If n's prefix is longer than p's one return 0. */
567 if (n
->prefixlen
> p
->prefixlen
)
570 if (n
->family
== AF_FLOWSPEC
) {
571 /* prefixlen is unused. look at fs prefix len */
572 if (n
->u
.prefix_flowspec
.prefixlen
>
573 p
->u
.prefix_flowspec
.prefixlen
)
576 /* Set both prefix's head pointer. */
577 np
= (const uint8_t *)&n
->u
.prefix_flowspec
.ptr
;
578 pp
= (const uint8_t *)&p
->u
.prefix_flowspec
.ptr
;
580 offset
= n
->u
.prefix_flowspec
.prefixlen
;
583 if (np
[offset
] != pp
[offset
])
588 /* Set both prefix's head pointer. */
592 offset
= n
->prefixlen
/ PNBBY
;
593 shift
= n
->prefixlen
% PNBBY
;
596 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
600 if (np
[offset
] != pp
[offset
])
605 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
606 int prefix_match_network_statement(const struct prefix
*n
,
607 const struct prefix
*p
)
611 const uint8_t *np
, *pp
;
613 /* Set both prefix's head pointer. */
617 offset
= n
->prefixlen
/ PNBBY
;
618 shift
= n
->prefixlen
% PNBBY
;
621 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
625 if (np
[offset
] != pp
[offset
])
630 void prefix_copy(struct prefix
*dest
, const struct prefix
*src
)
632 dest
->family
= src
->family
;
633 dest
->prefixlen
= src
->prefixlen
;
635 if (src
->family
== AF_INET
)
636 dest
->u
.prefix4
= src
->u
.prefix4
;
637 else if (src
->family
== AF_INET6
)
638 dest
->u
.prefix6
= src
->u
.prefix6
;
639 else if (src
->family
== AF_ETHERNET
) {
640 memcpy(&dest
->u
.prefix_eth
, &src
->u
.prefix_eth
,
641 sizeof(struct ethaddr
));
642 } else if (src
->family
== AF_EVPN
) {
643 memcpy(&dest
->u
.prefix_evpn
, &src
->u
.prefix_evpn
,
644 sizeof(struct evpn_addr
));
645 } else if (src
->family
== AF_UNSPEC
) {
646 dest
->u
.lp
.id
= src
->u
.lp
.id
;
647 dest
->u
.lp
.adv_router
= src
->u
.lp
.adv_router
;
648 } else if (src
->family
== AF_FLOWSPEC
) {
652 len
= src
->u
.prefix_flowspec
.prefixlen
;
653 dest
->u
.prefix_flowspec
.prefixlen
=
654 src
->u
.prefix_flowspec
.prefixlen
;
655 dest
->family
= src
->family
;
656 temp
= XCALLOC(MTYPE_PREFIX_FLOWSPEC
, len
);
657 dest
->u
.prefix_flowspec
.ptr
= (uintptr_t)temp
;
658 memcpy((void *)dest
->u
.prefix_flowspec
.ptr
,
659 (void *)src
->u
.prefix_flowspec
.ptr
, len
);
661 flog_err(EC_LIB_DEVELOPMENT
,
662 "prefix_copy(): Unknown address family %d",
669 * Return 1 if the address/netmask contained in the prefix structure
670 * is the same, and else return 0. For this routine, 'same' requires
671 * that not only the prefix length and the network part be the same,
672 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
673 * the same. Note that this routine has the same return value sense
674 * as '==' (which is different from prefix_cmp).
676 int prefix_same(const struct prefix
*p1
, const struct prefix
*p2
)
678 if ((p1
&& !p2
) || (!p1
&& p2
))
684 if (p1
->family
== p2
->family
&& p1
->prefixlen
== p2
->prefixlen
) {
685 if (p1
->family
== AF_INET
)
686 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
688 if (p1
->family
== AF_INET6
)
689 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
.s6_addr
,
690 &p2
->u
.prefix6
.s6_addr
))
692 if (p1
->family
== AF_ETHERNET
)
693 if (!memcmp(&p1
->u
.prefix_eth
, &p2
->u
.prefix_eth
,
694 sizeof(struct ethaddr
)))
696 if (p1
->family
== AF_EVPN
)
697 if (!memcmp(&p1
->u
.prefix_evpn
, &p2
->u
.prefix_evpn
,
698 sizeof(struct evpn_addr
)))
700 if (p1
->family
== AF_FLOWSPEC
) {
701 if (p1
->u
.prefix_flowspec
.prefixlen
!=
702 p2
->u
.prefix_flowspec
.prefixlen
)
704 if (!memcmp(&p1
->u
.prefix_flowspec
.ptr
,
705 &p2
->u
.prefix_flowspec
.ptr
,
706 p2
->u
.prefix_flowspec
.prefixlen
))
714 * Return 0 if the network prefixes represented by the struct prefix
715 * arguments are the same prefix, and 1 otherwise. Network prefixes
716 * are considered the same if the prefix lengths are equal and the
717 * network parts are the same. Host bits (which are considered masked
718 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
719 * 10.0.0.2/8 are considered equivalent by this routine. Note that
720 * this routine has the same return sense as strcmp (which is different
723 int prefix_cmp(const struct prefix
*p1
, const struct prefix
*p2
)
728 /* Set both prefix's head pointer. */
732 if (p1
->family
!= p2
->family
)
734 if (p1
->family
== AF_FLOWSPEC
) {
735 pp1
= (const uint8_t *)p1
->u
.prefix_flowspec
.ptr
;
736 pp2
= (const uint8_t *)p2
->u
.prefix_flowspec
.ptr
;
738 if (p1
->u
.prefix_flowspec
.prefixlen
!=
739 p2
->u
.prefix_flowspec
.prefixlen
)
742 offset
= p1
->u
.prefix_flowspec
.prefixlen
;
744 if (pp1
[offset
] != pp2
[offset
])
751 if (p1
->prefixlen
!= p2
->prefixlen
)
753 offset
= p1
->prefixlen
/ PNBBY
;
754 shift
= p1
->prefixlen
% PNBBY
;
757 if (maskbit
[shift
] & (pp1
[offset
] ^ pp2
[offset
]))
761 if (pp1
[offset
] != pp2
[offset
])
768 * Count the number of common bits in 2 prefixes. The prefix length is
769 * ignored for this function; the whole prefix is compared. If the prefix
770 * address families don't match, return -1; otherwise the return value is
771 * in range 0 ... maximum prefix length for the address family.
773 int prefix_common_bits(const struct prefix
*p1
, const struct prefix
*p2
)
779 /* Set both prefix's head pointer. */
780 const uint8_t *pp1
= p1
->u
.val
;
781 const uint8_t *pp2
= p2
->u
.val
;
783 if (p1
->family
== AF_INET
)
784 length
= IPV4_MAX_BYTELEN
;
785 if (p1
->family
== AF_INET6
)
786 length
= IPV6_MAX_BYTELEN
;
787 if (p1
->family
== AF_ETHERNET
)
789 if (p1
->family
== AF_EVPN
)
790 length
= 8 * sizeof(struct evpn_addr
);
792 if (p1
->family
!= p2
->family
|| !length
)
795 for (pos
= 0; pos
< length
; pos
++)
796 if (pp1
[pos
] != pp2
[pos
])
801 xor = pp1
[pos
] ^ pp2
[pos
];
802 for (bit
= 0; bit
< 8; bit
++)
803 if (xor&(1 << (7 - bit
)))
806 return pos
* 8 + bit
;
809 /* Return prefix family type string. */
810 const char *prefix_family_str(const struct prefix
*p
)
812 if (p
->family
== AF_INET
)
814 if (p
->family
== AF_INET6
)
816 if (p
->family
== AF_ETHERNET
)
818 if (p
->family
== AF_EVPN
)
823 /* Allocate new prefix_ipv4 structure. */
824 struct prefix_ipv4
*prefix_ipv4_new(void)
826 struct prefix_ipv4
*p
;
828 /* Call prefix_new to allocate a full-size struct prefix to avoid
830 where the struct prefix_ipv4 is cast to struct prefix and unallocated
831 bytes were being referenced (e.g. in structure assignments). */
832 p
= (struct prefix_ipv4
*)prefix_new();
837 /* Free prefix_ipv4 structure. */
838 void prefix_ipv4_free(struct prefix_ipv4
*p
)
840 prefix_free((struct prefix
*)p
);
843 /* When string format is invalid return 0. */
844 int str2prefix_ipv4(const char *str
, struct prefix_ipv4
*p
)
851 /* Find slash inside string. */
852 pnt
= strchr(str
, '/');
854 /* String doesn't contail slash. */
856 /* Convert string to prefix. */
857 ret
= inet_pton(AF_INET
, str
, &p
->prefix
);
861 /* If address doesn't contain slash we assume it host address.
864 p
->prefixlen
= IPV4_MAX_BITLEN
;
868 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
869 memcpy(cp
, str
, pnt
- str
);
870 *(cp
+ (pnt
- str
)) = '\0';
871 ret
= inet_aton(cp
, &p
->prefix
);
872 XFREE(MTYPE_TMP
, cp
);
874 /* Get prefix length. */
875 plen
= (uint8_t)atoi(++pnt
);
876 if (plen
> IPV4_MAX_PREFIXLEN
)
886 /* When string format is invalid return 0. */
887 int str2prefix_eth(const char *str
, struct prefix_eth
*p
)
893 const char *str_addr
= str
;
898 if (!strcmp(str
, "any")) {
899 memset(p
, 0, sizeof(*p
));
900 p
->family
= AF_ETHERNET
;
904 /* Find slash inside string. */
905 pnt
= strchr(str
, '/');
908 /* Get prefix length. */
909 plen
= (uint8_t)atoi(++pnt
);
915 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
916 memcpy(cp
, str
, pnt
- str
);
917 *(cp
+ (pnt
- str
)) = '\0';
923 /* Convert string to prefix. */
924 if (sscanf(str_addr
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2,
930 for (i
= 0; i
< 6; ++i
) {
931 p
->eth_addr
.octet
[i
] = a
[i
] & 0xff;
934 p
->family
= AF_ETHERNET
;
937 * special case to allow old configurations to work
938 * Since all zero's is implicitly meant to allow
939 * a comparison to zero, let's assume
941 if (!slash
&& is_zero_mac(&(p
->eth_addr
)))
947 XFREE(MTYPE_TMP
, cp
);
952 /* Convert masklen into IP address's netmask (network byte order). */
953 void masklen2ip(const int masklen
, struct in_addr
*netmask
)
955 assert(masklen
>= 0 && masklen
<= IPV4_MAX_BITLEN
);
957 /* left shift is only defined for less than the size of the type.
958 * we unconditionally use long long in case the target platform
959 * has defined behaviour for << 32 (or has a 64-bit left shift) */
961 if (sizeof(unsigned long long) > 4)
962 netmask
->s_addr
= htonl(0xffffffffULL
<< (32 - masklen
));
965 htonl(masklen
? 0xffffffffU
<< (32 - masklen
) : 0);
968 /* Convert IP address's netmask into integer. We assume netmask is
969 * sequential one. Argument netmask should be network byte order. */
970 uint8_t ip_masklen(struct in_addr netmask
)
972 uint32_t tmp
= ~ntohl(netmask
.s_addr
);
975 * clz: count leading zeroes. sadly, the behaviour of this builtin is
976 * undefined for a 0 argument, even though most CPUs give 32
978 return tmp
? __builtin_clz(tmp
) : 32;
981 /* Apply mask to IPv4 prefix (network byte order). */
982 void apply_mask_ipv4(struct prefix_ipv4
*p
)
985 masklen2ip(p
->prefixlen
, &mask
);
986 p
->prefix
.s_addr
&= mask
.s_addr
;
989 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
990 int prefix_ipv4_any(const struct prefix_ipv4
*p
)
992 return (p
->prefix
.s_addr
== 0 && p
->prefixlen
== 0);
995 /* Allocate a new ip version 6 route */
996 struct prefix_ipv6
*prefix_ipv6_new(void)
998 struct prefix_ipv6
*p
;
1000 /* Allocate a full-size struct prefix to avoid problems with structure
1002 p
= (struct prefix_ipv6
*)prefix_new();
1003 p
->family
= AF_INET6
;
1007 /* Free prefix for IPv6. */
1008 void prefix_ipv6_free(struct prefix_ipv6
*p
)
1010 prefix_free((struct prefix
*)p
);
1013 /* If given string is valid return pin6 else return NULL */
1014 int str2prefix_ipv6(const char *str
, struct prefix_ipv6
*p
)
1020 pnt
= strchr(str
, '/');
1022 /* If string doesn't contain `/' treat it as host route. */
1024 ret
= inet_pton(AF_INET6
, str
, &p
->prefix
);
1027 p
->prefixlen
= IPV6_MAX_BITLEN
;
1031 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
1032 memcpy(cp
, str
, pnt
- str
);
1033 *(cp
+ (pnt
- str
)) = '\0';
1034 ret
= inet_pton(AF_INET6
, cp
, &p
->prefix
);
1035 XFREE(MTYPE_TMP
, cp
);
1038 plen
= (uint8_t)atoi(++pnt
);
1039 if (plen
> IPV6_MAX_BITLEN
)
1041 p
->prefixlen
= plen
;
1043 p
->family
= AF_INET6
;
1048 /* Convert struct in6_addr netmask into integer.
1049 * FIXME return uint8_t as ip_maskleni() does. */
1050 int ip6_masklen(struct in6_addr netmask
)
1056 pnt
= (unsigned char *)&netmask
;
1058 while ((*pnt
== 0xff) && len
< IPV6_MAX_BITLEN
) {
1063 if (len
< IPV6_MAX_BITLEN
) {
1073 void masklen2ip6(const int masklen
, struct in6_addr
*netmask
)
1075 assert(masklen
>= 0 && masklen
<= IPV6_MAX_BITLEN
);
1076 memcpy(netmask
, maskbytes6
+ masklen
, sizeof(struct in6_addr
));
1079 void apply_mask_ipv6(struct prefix_ipv6
*p
)
1085 index
= p
->prefixlen
/ 8;
1088 pnt
= (uint8_t *)&p
->prefix
;
1089 offset
= p
->prefixlen
% 8;
1091 pnt
[index
] &= maskbit
[offset
];
1099 void apply_mask(struct prefix
*p
)
1101 switch (p
->family
) {
1103 apply_mask_ipv4((struct prefix_ipv4
*)p
);
1106 apply_mask_ipv6((struct prefix_ipv6
*)p
);
1114 /* Utility function of convert between struct prefix <=> union sockunion.
1115 * FIXME This function isn't used anywhere. */
1116 struct prefix
*sockunion2prefix(const union sockunion
*dest
,
1117 const union sockunion
*mask
)
1119 if (dest
->sa
.sa_family
== AF_INET
) {
1120 struct prefix_ipv4
*p
;
1122 p
= prefix_ipv4_new();
1123 p
->family
= AF_INET
;
1124 p
->prefix
= dest
->sin
.sin_addr
;
1125 p
->prefixlen
= ip_masklen(mask
->sin
.sin_addr
);
1126 return (struct prefix
*)p
;
1128 if (dest
->sa
.sa_family
== AF_INET6
) {
1129 struct prefix_ipv6
*p
;
1131 p
= prefix_ipv6_new();
1132 p
->family
= AF_INET6
;
1133 p
->prefixlen
= ip6_masklen(mask
->sin6
.sin6_addr
);
1134 memcpy(&p
->prefix
, &dest
->sin6
.sin6_addr
,
1135 sizeof(struct in6_addr
));
1136 return (struct prefix
*)p
;
1141 /* Utility function of convert between struct prefix <=> union sockunion. */
1142 struct prefix
*sockunion2hostprefix(const union sockunion
*su
,
1143 struct prefix
*prefix
)
1145 if (su
->sa
.sa_family
== AF_INET
) {
1146 struct prefix_ipv4
*p
;
1148 p
= prefix
? (struct prefix_ipv4
*)prefix
: prefix_ipv4_new();
1149 p
->family
= AF_INET
;
1150 p
->prefix
= su
->sin
.sin_addr
;
1151 p
->prefixlen
= IPV4_MAX_BITLEN
;
1152 return (struct prefix
*)p
;
1154 if (su
->sa
.sa_family
== AF_INET6
) {
1155 struct prefix_ipv6
*p
;
1157 p
= prefix
? (struct prefix_ipv6
*)prefix
: prefix_ipv6_new();
1158 p
->family
= AF_INET6
;
1159 p
->prefixlen
= IPV6_MAX_BITLEN
;
1160 memcpy(&p
->prefix
, &su
->sin6
.sin6_addr
,
1161 sizeof(struct in6_addr
));
1162 return (struct prefix
*)p
;
1167 void prefix2sockunion(const struct prefix
*p
, union sockunion
*su
)
1169 memset(su
, 0, sizeof(*su
));
1171 su
->sa
.sa_family
= p
->family
;
1172 if (p
->family
== AF_INET
)
1173 su
->sin
.sin_addr
= p
->u
.prefix4
;
1174 if (p
->family
== AF_INET6
)
1175 memcpy(&su
->sin6
.sin6_addr
, &p
->u
.prefix6
,
1176 sizeof(struct in6_addr
));
1179 int prefix_blen(const struct prefix
*p
)
1181 switch (p
->family
) {
1183 return IPV4_MAX_BYTELEN
;
1186 return IPV6_MAX_BYTELEN
;
1195 /* Generic function for conversion string to struct prefix. */
1196 int str2prefix(const char *str
, struct prefix
*p
)
1203 /* First we try to convert string to struct prefix_ipv4. */
1204 ret
= str2prefix_ipv4(str
, (struct prefix_ipv4
*)p
);
1208 /* Next we try to convert string to struct prefix_ipv6. */
1209 ret
= str2prefix_ipv6(str
, (struct prefix_ipv6
*)p
);
1213 /* Next we try to convert string to struct prefix_eth. */
1214 ret
= str2prefix_eth(str
, (struct prefix_eth
*)p
);
1221 static const char *prefixevpn_ead2str(const struct prefix_evpn
*p
, char *str
,
1224 snprintf(str
, size
, "Unsupported EVPN prefix");
1228 static const char *prefixevpn_macip2str(const struct prefix_evpn
*p
, char *str
,
1232 char buf
[PREFIX2STR_BUFFER
];
1233 char buf2
[ETHER_ADDR_STRLEN
];
1235 if (is_evpn_prefix_ipaddr_none(p
))
1236 snprintf(str
, size
, "[%d]:[%s]/%d",
1237 p
->prefix
.route_type
,
1238 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
1239 buf2
, sizeof(buf2
)),
1242 family
= is_evpn_prefix_ipaddr_v4(p
)
1245 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d",
1246 p
->prefix
.route_type
,
1247 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
1248 buf2
, sizeof(buf2
)),
1250 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
1251 buf
, PREFIX2STR_BUFFER
),
1257 static const char *prefixevpn_imet2str(const struct prefix_evpn
*p
, char *str
,
1261 char buf
[PREFIX2STR_BUFFER
];
1263 family
= is_evpn_prefix_ipaddr_v4(p
)
1266 snprintf(str
, size
, "[%d]:[%s]/%d", p
->prefix
.route_type
,
1268 &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf
,
1274 static const char *prefixevpn_es2str(const struct prefix_evpn
*p
, char *str
,
1277 char buf
[ESI_STR_LEN
];
1279 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d", p
->prefix
.route_type
,
1280 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1281 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
),
1286 static const char *prefixevpn_prefix2str(const struct prefix_evpn
*p
, char *str
,
1290 char buf
[PREFIX2STR_BUFFER
];
1292 family
= is_evpn_prefix_ipaddr_v4(p
)
1295 snprintf(str
, size
, "[%d]:[%u][%s/%d]/%d",
1296 p
->prefix
.route_type
,
1297 p
->prefix
.prefix_addr
.eth_tag
,
1299 &p
->prefix
.prefix_addr
.ip
.ip
.addr
, buf
,
1301 p
->prefix
.prefix_addr
.ip_prefix_length
,
1306 static const char *prefixevpn2str(const struct prefix_evpn
*p
, char *str
,
1309 switch (p
->prefix
.route_type
) {
1311 return prefixevpn_ead2str(p
, str
, size
);
1313 return prefixevpn_macip2str(p
, str
, size
);
1315 return prefixevpn_imet2str(p
, str
, size
);
1317 return prefixevpn_es2str(p
, str
, size
);
1319 return prefixevpn_prefix2str(p
, str
, size
);
1321 snprintf(str
, size
, "Unsupported EVPN prefix");
1327 const char *prefix2str(union prefixconstptr pu
, char *str
, int size
)
1329 const struct prefix
*p
= pu
.p
;
1330 char buf
[PREFIX2STR_BUFFER
];
1332 switch (p
->family
) {
1335 snprintf(str
, size
, "%s/%d", inet_ntop(p
->family
, &p
->u
.prefix
,
1336 buf
, PREFIX2STR_BUFFER
),
1341 snprintf(str
, size
, "%s/%d",
1342 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
)),
1347 prefixevpn2str((const struct prefix_evpn
*)p
, str
, size
);
1351 sprintf(str
, "FS prefix");
1355 sprintf(str
, "UNK prefix");
1362 void prefix_mcast_inet4_dump(const char *onfail
, struct in_addr addr
,
1363 char *buf
, int buf_size
)
1365 int save_errno
= errno
;
1367 if (addr
.s_addr
== INADDR_ANY
)
1370 if (!inet_ntop(AF_INET
, &addr
, buf
, buf_size
)) {
1372 snprintf(buf
, buf_size
, "%s", onfail
);
1379 const char *prefix_sg2str(const struct prefix_sg
*sg
, char *sg_str
)
1381 char src_str
[INET_ADDRSTRLEN
];
1382 char grp_str
[INET_ADDRSTRLEN
];
1384 prefix_mcast_inet4_dump("<src?>", sg
->src
, src_str
, sizeof(src_str
));
1385 prefix_mcast_inet4_dump("<grp?>", sg
->grp
, grp_str
, sizeof(grp_str
));
1386 snprintf(sg_str
, PREFIX_SG_STR_LEN
, "(%s,%s)", src_str
, grp_str
);
1391 struct prefix
*prefix_new(void)
1395 p
= XCALLOC(MTYPE_PREFIX
, sizeof *p
);
1399 /* Free prefix structure. */
1400 void prefix_free(struct prefix
*p
)
1402 XFREE(MTYPE_PREFIX
, p
);
1405 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1406 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1409 uint32_t destination
;
1411 destination
= ntohl(p
->prefix
.s_addr
);
1413 if (p
->prefixlen
== IPV4_MAX_PREFIXLEN
)
1415 /* do nothing for host routes */
1416 else if (IN_CLASSC(destination
)) {
1419 } else if (IN_CLASSB(destination
)) {
1428 in_addr_t
ipv4_network_addr(in_addr_t hostaddr
, int masklen
)
1430 struct in_addr mask
;
1432 masklen2ip(masklen
, &mask
);
1433 return hostaddr
& mask
.s_addr
;
1436 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1438 struct in_addr mask
;
1440 masklen2ip(masklen
, &mask
);
1441 return (masklen
!= IPV4_MAX_PREFIXLEN
- 1) ?
1443 (hostaddr
| ~mask
.s_addr
)
1445 /* special case for /31 */
1446 (hostaddr
^ ~mask
.s_addr
);
1449 /* Utility function to convert ipv4 netmask to prefixes
1450 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1451 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1452 int netmask_str2prefix_str(const char *net_str
, const char *mask_str
,
1455 struct in_addr network
;
1456 struct in_addr mask
;
1458 uint32_t destination
;
1461 ret
= inet_aton(net_str
, &network
);
1466 ret
= inet_aton(mask_str
, &mask
);
1470 prefixlen
= ip_masklen(mask
);
1472 destination
= ntohl(network
.s_addr
);
1474 if (network
.s_addr
== 0)
1476 else if (IN_CLASSC(destination
))
1478 else if (IN_CLASSB(destination
))
1480 else if (IN_CLASSA(destination
))
1486 sprintf(prefix_str
, "%s/%d", net_str
, prefixlen
);
1491 /* Utility function for making IPv6 address string. */
1492 const char *inet6_ntoa(struct in6_addr addr
)
1494 static char buf
[INET6_ADDRSTRLEN
];
1496 inet_ntop(AF_INET6
, &addr
, buf
, INET6_ADDRSTRLEN
);
1500 /* converts to internal representation of mac address
1501 * returns 1 on success, 0 otherwise
1502 * format accepted: AA:BB:CC:DD:EE:FF
1503 * if mac parameter is null, then check only
1505 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1513 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1516 /* error in incoming str length */
1519 /* valid mac address */
1522 for (i
= 0; i
< 6; ++i
)
1523 mac
->octet
[i
] = a
[i
] & 0xff;
1527 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1534 ptr
= XMALLOC(MTYPE_TMP
, ETHER_ADDR_STRLEN
* sizeof(char));
1536 assert(size
>= ETHER_ADDR_STRLEN
);
1539 snprintf(ptr
, (ETHER_ADDR_STRLEN
), "%02x:%02x:%02x:%02x:%02x:%02x",
1540 (uint8_t)mac
->octet
[0], (uint8_t)mac
->octet
[1],
1541 (uint8_t)mac
->octet
[2], (uint8_t)mac
->octet
[3],
1542 (uint8_t)mac
->octet
[4], (uint8_t)mac
->octet
[5]);
1546 unsigned prefix_hash_key(void *pp
)
1550 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1554 /* make sure *all* unused bits are zero,
1555 * particularly including alignment /
1556 * padding and unused prefix bytes.
1558 memset(©
, 0, sizeof(copy
));
1559 prefix_copy(©
, (struct prefix
*)pp
);
1560 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1561 copy
.u
.prefix_flowspec
.prefixlen
,
1563 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1564 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1565 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1568 /* make sure *all* unused bits are zero, particularly including
1570 * padding and unused prefix bytes. */
1571 memset(©
, 0, sizeof(copy
));
1572 prefix_copy(©
, (struct prefix
*)pp
);
1574 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),
1578 /* converts to internal representation of esi
1579 * returns 1 on success, 0 otherwise
1580 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1581 * if esi parameter is null, then check only
1583 int str_to_esi(const char *str
, esi_t
*esi
)
1586 unsigned int a
[ESI_BYTES
];
1591 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1592 a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1593 a
+ 4, a
+ 5, a
+ 6, a
+ 7,
1596 /* error in incoming str length */
1603 for (i
= 0; i
< ESI_BYTES
; ++i
)
1604 esi
->val
[i
] = a
[i
] & 0xff;
1608 char *esi_to_str(const esi_t
*esi
, char *buf
, int size
)
1615 ptr
= XMALLOC(MTYPE_TMP
, ESI_STR_LEN
* sizeof(char));
1617 assert(size
>= ESI_STR_LEN
);
1621 snprintf(ptr
, ESI_STR_LEN
,
1622 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1623 esi
->val
[0], esi
->val
[1], esi
->val
[2],
1624 esi
->val
[3], esi
->val
[4], esi
->val
[5],
1625 esi
->val
[6], esi
->val
[7], esi
->val
[8],