]>
git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
0203301562f5df3382b8233423df420c1c0f891d
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")
35 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
36 0xf8, 0xfc, 0xfe, 0xff};
38 static const struct in6_addr maskbytes6
[] = {
39 /* /0 */ {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}},
42 {{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00}}},
45 {{{0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00}}},
48 {{{0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00}}},
51 {{{0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00}}},
54 {{{0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00}}},
57 {{{0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00}}},
60 {{{0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00}}},
63 {{{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00}}},
66 {{{0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00}}},
69 {{{0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00}}},
72 {{{0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00}}},
75 {{{0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00}}},
78 {{{0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00}}},
81 {{{0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x00}}},
84 {{{0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x00, 0x00}}},
87 {{{0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00, 0x00}}},
90 {{{0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00}}},
93 {{{0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94 0x00, 0x00, 0x00, 0x00, 0x00}}},
96 {{{0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x00, 0x00}}},
99 {{{0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0x00, 0x00, 0x00, 0x00, 0x00}}},
102 {{{0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00, 0x00}}},
105 {{{0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
106 0x00, 0x00, 0x00, 0x00, 0x00}}},
108 {{{0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109 0x00, 0x00, 0x00, 0x00, 0x00}}},
111 {{{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112 0x00, 0x00, 0x00, 0x00, 0x00}}},
114 {{{0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00}}},
117 {{{0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 0x00, 0x00, 0x00, 0x00, 0x00}}},
120 {{{0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121 0x00, 0x00, 0x00, 0x00, 0x00}}},
123 {{{0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00, 0x00}}},
126 {{{0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00, 0x00}}},
129 {{{0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 0x00, 0x00, 0x00, 0x00, 0x00}}},
132 {{{0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133 0x00, 0x00, 0x00, 0x00, 0x00}}},
135 {{{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00, 0x00}}},
138 {{{0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139 0x00, 0x00, 0x00, 0x00, 0x00}}},
141 {{{0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142 0x00, 0x00, 0x00, 0x00, 0x00}}},
144 {{{0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 0x00, 0x00, 0x00, 0x00, 0x00}}},
147 {{{0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148 0x00, 0x00, 0x00, 0x00, 0x00}}},
150 {{{0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151 0x00, 0x00, 0x00, 0x00, 0x00}}},
153 {{{0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154 0x00, 0x00, 0x00, 0x00, 0x00}}},
156 {{{0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x00}}},
159 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160 0x00, 0x00, 0x00, 0x00, 0x00}}},
162 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
163 0x00, 0x00, 0x00, 0x00, 0x00}}},
165 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
166 0x00, 0x00, 0x00, 0x00, 0x00}}},
168 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
169 0x00, 0x00, 0x00, 0x00, 0x00}}},
171 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
172 0x00, 0x00, 0x00, 0x00, 0x00}}},
174 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
175 0x00, 0x00, 0x00, 0x00, 0x00}}},
177 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
178 0x00, 0x00, 0x00, 0x00, 0x00}}},
180 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
181 0x00, 0x00, 0x00, 0x00, 0x00}}},
183 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
184 0x00, 0x00, 0x00, 0x00, 0x00}}},
186 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00,
187 0x00, 0x00, 0x00, 0x00, 0x00}}},
189 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00}}},
192 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
193 0x00, 0x00, 0x00, 0x00, 0x00}}},
195 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00,
196 0x00, 0x00, 0x00, 0x00, 0x00}}},
198 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
199 0x00, 0x00, 0x00, 0x00, 0x00}}},
201 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
202 0x00, 0x00, 0x00, 0x00, 0x00}}},
204 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
205 0x00, 0x00, 0x00, 0x00, 0x00}}},
207 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
208 0x00, 0x00, 0x00, 0x00, 0x00}}},
210 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
211 0x00, 0x00, 0x00, 0x00, 0x00}}},
213 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0x00, 0x00, 0x00}}},
216 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00,
217 0x00, 0x00, 0x00, 0x00, 0x00}}},
219 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00,
220 0x00, 0x00, 0x00, 0x00, 0x00}}},
222 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00,
223 0x00, 0x00, 0x00, 0x00, 0x00}}},
225 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00,
226 0x00, 0x00, 0x00, 0x00, 0x00}}},
228 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00,
229 0x00, 0x00, 0x00, 0x00, 0x00}}},
231 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
232 0x00, 0x00, 0x00, 0x00, 0x00}}},
234 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00,
235 0x00, 0x00, 0x00, 0x00, 0x00}}},
237 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
238 0x00, 0x00, 0x00, 0x00, 0x00}}},
240 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00,
241 0x00, 0x00, 0x00, 0x00, 0x00}}},
243 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00,
244 0x00, 0x00, 0x00, 0x00, 0x00}}},
246 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00,
247 0x00, 0x00, 0x00, 0x00, 0x00}}},
249 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00,
250 0x00, 0x00, 0x00, 0x00, 0x00}}},
252 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00,
253 0x00, 0x00, 0x00, 0x00, 0x00}}},
255 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
256 0x00, 0x00, 0x00, 0x00, 0x00}}},
258 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
259 0x00, 0x00, 0x00, 0x00, 0x00}}},
261 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00,
262 0x00, 0x00, 0x00, 0x00, 0x00}}},
264 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00,
265 0x00, 0x00, 0x00, 0x00, 0x00}}},
267 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00,
268 0x00, 0x00, 0x00, 0x00, 0x00}}},
270 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00,
271 0x00, 0x00, 0x00, 0x00, 0x00}}},
273 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00,
274 0x00, 0x00, 0x00, 0x00, 0x00}}},
276 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00,
277 0x00, 0x00, 0x00, 0x00, 0x00}}},
279 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
280 0x00, 0x00, 0x00, 0x00, 0x00}}},
282 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
283 0x00, 0x00, 0x00, 0x00, 0x00}}},
285 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
286 0x00, 0x00, 0x00, 0x00, 0x00}}},
288 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
289 0x00, 0x00, 0x00, 0x00, 0x00}}},
291 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
292 0x00, 0x00, 0x00, 0x00, 0x00}}},
294 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8,
295 0x00, 0x00, 0x00, 0x00, 0x00}}},
297 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
298 0x00, 0x00, 0x00, 0x00, 0x00}}},
300 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
301 0x00, 0x00, 0x00, 0x00, 0x00}}},
303 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
304 0x00, 0x00, 0x00, 0x00, 0x00}}},
306 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
307 0x80, 0x00, 0x00, 0x00, 0x00}}},
309 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
310 0xc0, 0x00, 0x00, 0x00, 0x00}}},
312 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
313 0xe0, 0x00, 0x00, 0x00, 0x00}}},
315 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
316 0xf0, 0x00, 0x00, 0x00, 0x00}}},
318 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
319 0xf8, 0x00, 0x00, 0x00, 0x00}}},
321 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
322 0xfc, 0x00, 0x00, 0x00, 0x00}}},
324 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
325 0xfe, 0x00, 0x00, 0x00, 0x00}}},
327 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
328 0xff, 0x00, 0x00, 0x00, 0x00}}},
330 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
331 0xff, 0x80, 0x00, 0x00, 0x00}}},
333 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
334 0xff, 0xc0, 0x00, 0x00, 0x00}}},
336 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
337 0xff, 0xe0, 0x00, 0x00, 0x00}}},
339 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
340 0xff, 0xf0, 0x00, 0x00, 0x00}}},
342 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
343 0xff, 0xf8, 0x00, 0x00, 0x00}}},
345 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
346 0xff, 0xfc, 0x00, 0x00, 0x00}}},
348 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
349 0xff, 0xfe, 0x00, 0x00, 0x00}}},
351 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
352 0xff, 0xff, 0x00, 0x00, 0x00}}},
354 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
355 0xff, 0xff, 0x80, 0x00, 0x00}}},
357 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
358 0xff, 0xff, 0xc0, 0x00, 0x00}}},
360 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
361 0xff, 0xff, 0xe0, 0x00, 0x00}}},
363 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
364 0xff, 0xff, 0xf0, 0x00, 0x00}}},
366 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
367 0xff, 0xff, 0xf8, 0x00, 0x00}}},
369 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
370 0xff, 0xff, 0xfc, 0x00, 0x00}}},
372 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
373 0xff, 0xff, 0xfe, 0x00, 0x00}}},
375 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
376 0xff, 0xff, 0xff, 0x00, 0x00}}},
378 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
379 0xff, 0xff, 0xff, 0x80, 0x00}}},
381 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
382 0xff, 0xff, 0xff, 0xc0, 0x00}}},
384 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
385 0xff, 0xff, 0xff, 0xe0, 0x00}}},
387 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
388 0xff, 0xff, 0xff, 0xf0, 0x00}}},
390 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
391 0xff, 0xff, 0xff, 0xf8, 0x00}}},
393 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
394 0xff, 0xff, 0xff, 0xfc, 0x00}}},
396 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
397 0xff, 0xff, 0xff, 0xfe, 0x00}}},
399 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
400 0xff, 0xff, 0xff, 0xff, 0x00}}},
402 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
403 0xff, 0xff, 0xff, 0xff, 0x80}}},
405 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
406 0xff, 0xff, 0xff, 0xff, 0xc0}}},
408 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
409 0xff, 0xff, 0xff, 0xff, 0xe0}}},
411 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
412 0xff, 0xff, 0xff, 0xff, 0xf0}}},
414 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
415 0xff, 0xff, 0xff, 0xff, 0xf8}}},
417 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
418 0xff, 0xff, 0xff, 0xff, 0xfc}}},
420 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
421 0xff, 0xff, 0xff, 0xff, 0xfe}}},
423 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
424 0xff, 0xff, 0xff, 0xff, 0xff}}}};
426 /* Number of bits in prefix type. */
431 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
433 void prefix_hexdump(const struct prefix
*p
)
435 char buf
[PREFIX_STRLEN
];
437 zlog_debug("prefix: %s",
438 prefix2str(p
, buf
, sizeof(buf
)));
439 zlog_hexdump(p
, sizeof(struct prefix
));
442 int is_zero_mac(struct ethaddr
*mac
)
446 for (i
= 0; i
< ETH_ALEN
; i
++) {
454 unsigned int prefix_bit(const uint8_t *prefix
, const uint16_t prefixlen
)
456 unsigned int offset
= prefixlen
/ 8;
457 unsigned int shift
= 7 - (prefixlen
% 8);
459 return (prefix
[offset
] >> shift
) & 1;
462 unsigned int prefix6_bit(const struct in6_addr
*prefix
, const uint16_t prefixlen
)
464 return prefix_bit((const uint8_t *)&prefix
->s6_addr
, prefixlen
);
467 int str2family(const char *string
)
469 if (!strcmp("ipv4", string
))
471 else if (!strcmp("ipv6", string
))
473 else if (!strcmp("ethernet", string
))
475 else if (!strcmp("evpn", string
))
480 const char *family2str(int family
)
495 /* Address Famiy Identifier to Address Family converter. */
496 int afi2family(afi_t afi
)
500 else if (afi
== AFI_IP6
)
502 else if (afi
== AFI_L2VPN
)
504 /* NOTE: EVPN code should NOT use this interface. */
508 afi_t
family2afi(int family
)
510 if (family
== AF_INET
)
512 else if (family
== AF_INET6
)
514 else if (family
== AF_ETHERNET
|| family
== AF_EVPN
)
519 const char *afi2str(afi_t afi
)
536 const char *safi2str(safi_t safi
)
549 case SAFI_LABELED_UNICAST
:
550 return "labeled-unicast";
558 /* If n includes p prefix then return 1 else return 0. */
559 int prefix_match(const struct prefix
*n
, const struct prefix
*p
)
563 const uint8_t *np
, *pp
;
565 /* If n's prefix is longer than p's one return 0. */
566 if (n
->prefixlen
> p
->prefixlen
)
569 if (n
->family
== AF_FLOWSPEC
) {
570 /* prefixlen is unused. look at fs prefix len */
571 if (n
->u
.prefix_flowspec
.prefixlen
>
572 p
->u
.prefix_flowspec
.prefixlen
)
575 /* Set both prefix's head pointer. */
576 np
= (const uint8_t *)&n
->u
.prefix_flowspec
.ptr
;
577 pp
= (const uint8_t *)&p
->u
.prefix_flowspec
.ptr
;
579 offset
= n
->u
.prefix_flowspec
.prefixlen
;
582 if (np
[offset
] != pp
[offset
])
587 /* Set both prefix's head pointer. */
591 offset
= n
->prefixlen
/ PNBBY
;
592 shift
= n
->prefixlen
% PNBBY
;
595 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
599 if (np
[offset
] != pp
[offset
])
604 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
605 int prefix_match_network_statement(const struct prefix
*n
,
606 const struct prefix
*p
)
610 const uint8_t *np
, *pp
;
612 /* Set both prefix's head pointer. */
616 offset
= n
->prefixlen
/ PNBBY
;
617 shift
= n
->prefixlen
% PNBBY
;
620 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
624 if (np
[offset
] != pp
[offset
])
629 void prefix_copy(struct prefix
*dest
, const struct prefix
*src
)
631 dest
->family
= src
->family
;
632 dest
->prefixlen
= src
->prefixlen
;
634 if (src
->family
== AF_INET
)
635 dest
->u
.prefix4
= src
->u
.prefix4
;
636 else if (src
->family
== AF_INET6
)
637 dest
->u
.prefix6
= src
->u
.prefix6
;
638 else if (src
->family
== AF_ETHERNET
) {
639 memcpy(&dest
->u
.prefix_eth
, &src
->u
.prefix_eth
,
640 sizeof(struct ethaddr
));
641 } else if (src
->family
== AF_EVPN
) {
642 memcpy(&dest
->u
.prefix_evpn
, &src
->u
.prefix_evpn
,
643 sizeof(struct evpn_addr
));
644 } else if (src
->family
== AF_UNSPEC
) {
645 dest
->u
.lp
.id
= src
->u
.lp
.id
;
646 dest
->u
.lp
.adv_router
= src
->u
.lp
.adv_router
;
647 } else if (src
->family
== AF_FLOWSPEC
) {
651 len
= src
->u
.prefix_flowspec
.prefixlen
;
652 dest
->u
.prefix_flowspec
.prefixlen
=
653 src
->u
.prefix_flowspec
.prefixlen
;
654 dest
->family
= src
->family
;
655 temp
= XCALLOC(MTYPE_PREFIX_FLOWSPEC
, len
);
656 dest
->u
.prefix_flowspec
.ptr
= (uintptr_t)temp
;
657 memcpy((void *)dest
->u
.prefix_flowspec
.ptr
,
658 (void *)src
->u
.prefix_flowspec
.ptr
, len
);
660 flog_err(EC_LIB_DEVELOPMENT
,
661 "prefix_copy(): Unknown address family %d",
668 * Return 1 if the address/netmask contained in the prefix structure
669 * is the same, and else return 0. For this routine, 'same' requires
670 * that not only the prefix length and the network part be the same,
671 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
672 * the same. Note that this routine has the same return value sense
673 * as '==' (which is different from prefix_cmp).
675 int prefix_same(const struct prefix
*p1
, const struct prefix
*p2
)
677 if ((p1
&& !p2
) || (!p1
&& p2
))
683 if (p1
->family
== p2
->family
&& p1
->prefixlen
== p2
->prefixlen
) {
684 if (p1
->family
== AF_INET
)
685 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
687 if (p1
->family
== AF_INET6
)
688 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
.s6_addr
,
689 &p2
->u
.prefix6
.s6_addr
))
691 if (p1
->family
== AF_ETHERNET
)
692 if (!memcmp(&p1
->u
.prefix_eth
, &p2
->u
.prefix_eth
,
693 sizeof(struct ethaddr
)))
695 if (p1
->family
== AF_EVPN
)
696 if (!memcmp(&p1
->u
.prefix_evpn
, &p2
->u
.prefix_evpn
,
697 sizeof(struct evpn_addr
)))
699 if (p1
->family
== AF_FLOWSPEC
) {
700 if (p1
->u
.prefix_flowspec
.prefixlen
!=
701 p2
->u
.prefix_flowspec
.prefixlen
)
703 if (!memcmp(&p1
->u
.prefix_flowspec
.ptr
,
704 &p2
->u
.prefix_flowspec
.ptr
,
705 p2
->u
.prefix_flowspec
.prefixlen
))
713 * Return 0 if the network prefixes represented by the struct prefix
714 * arguments are the same prefix, and 1 otherwise. Network prefixes
715 * are considered the same if the prefix lengths are equal and the
716 * network parts are the same. Host bits (which are considered masked
717 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
718 * 10.0.0.2/8 are considered equivalent by this routine. Note that
719 * this routine has the same return sense as strcmp (which is different
722 int prefix_cmp(const struct prefix
*p1
, const struct prefix
*p2
)
727 /* Set both prefix's head pointer. */
731 if (p1
->family
!= p2
->family
)
733 if (p1
->family
== AF_FLOWSPEC
) {
734 pp1
= (const uint8_t *)p1
->u
.prefix_flowspec
.ptr
;
735 pp2
= (const uint8_t *)p2
->u
.prefix_flowspec
.ptr
;
737 if (p1
->u
.prefix_flowspec
.prefixlen
!=
738 p2
->u
.prefix_flowspec
.prefixlen
)
741 offset
= p1
->u
.prefix_flowspec
.prefixlen
;
743 if (pp1
[offset
] != pp2
[offset
])
750 if (p1
->prefixlen
!= p2
->prefixlen
)
752 offset
= p1
->prefixlen
/ PNBBY
;
753 shift
= p1
->prefixlen
% PNBBY
;
756 if (maskbit
[shift
] & (pp1
[offset
] ^ pp2
[offset
]))
760 if (pp1
[offset
] != pp2
[offset
])
767 * Count the number of common bits in 2 prefixes. The prefix length is
768 * ignored for this function; the whole prefix is compared. If the prefix
769 * address families don't match, return -1; otherwise the return value is
770 * in range 0 ... maximum prefix length for the address family.
772 int prefix_common_bits(const struct prefix
*p1
, const struct prefix
*p2
)
778 /* Set both prefix's head pointer. */
779 const uint8_t *pp1
= p1
->u
.val
;
780 const uint8_t *pp2
= p2
->u
.val
;
782 if (p1
->family
== AF_INET
)
783 length
= IPV4_MAX_BYTELEN
;
784 if (p1
->family
== AF_INET6
)
785 length
= IPV6_MAX_BYTELEN
;
786 if (p1
->family
== AF_ETHERNET
)
788 if (p1
->family
== AF_EVPN
)
789 length
= 8 * sizeof(struct evpn_addr
);
791 if (p1
->family
!= p2
->family
|| !length
)
794 for (pos
= 0; pos
< length
; pos
++)
795 if (pp1
[pos
] != pp2
[pos
])
800 xor = pp1
[pos
] ^ pp2
[pos
];
801 for (bit
= 0; bit
< 8; bit
++)
802 if (xor&(1 << (7 - bit
)))
805 return pos
* 8 + bit
;
808 /* Return prefix family type string. */
809 const char *prefix_family_str(const struct prefix
*p
)
811 if (p
->family
== AF_INET
)
813 if (p
->family
== AF_INET6
)
815 if (p
->family
== AF_ETHERNET
)
817 if (p
->family
== AF_EVPN
)
822 /* Allocate new prefix_ipv4 structure. */
823 struct prefix_ipv4
*prefix_ipv4_new()
825 struct prefix_ipv4
*p
;
827 /* Call prefix_new to allocate a full-size struct prefix to avoid
829 where the struct prefix_ipv4 is cast to struct prefix and unallocated
830 bytes were being referenced (e.g. in structure assignments). */
831 p
= (struct prefix_ipv4
*)prefix_new();
836 /* Free prefix_ipv4 structure. */
837 void prefix_ipv4_free(struct prefix_ipv4
*p
)
839 prefix_free((struct prefix
*)p
);
842 /* When string format is invalid return 0. */
843 int str2prefix_ipv4(const char *str
, struct prefix_ipv4
*p
)
850 /* Find slash inside string. */
851 pnt
= strchr(str
, '/');
853 /* String doesn't contail slash. */
855 /* Convert string to prefix. */
856 ret
= inet_pton(AF_INET
, str
, &p
->prefix
);
860 /* If address doesn't contain slash we assume it host address.
863 p
->prefixlen
= IPV4_MAX_BITLEN
;
867 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
868 strncpy(cp
, str
, pnt
- str
);
869 *(cp
+ (pnt
- str
)) = '\0';
870 ret
= inet_aton(cp
, &p
->prefix
);
871 XFREE(MTYPE_TMP
, cp
);
873 /* Get prefix length. */
874 plen
= (uint8_t)atoi(++pnt
);
875 if (plen
> IPV4_MAX_PREFIXLEN
)
885 /* When string format is invalid return 0. */
886 int str2prefix_eth(const char *str
, struct prefix_eth
*p
)
892 const char *str_addr
= str
;
897 if (!strcmp(str
, "any")) {
898 memset(p
, 0, sizeof(*p
));
899 p
->family
= AF_ETHERNET
;
903 /* Find slash inside string. */
904 pnt
= strchr(str
, '/');
907 /* Get prefix length. */
908 plen
= (uint8_t)atoi(++pnt
);
914 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
915 strncpy(cp
, str
, pnt
- str
);
916 *(cp
+ (pnt
- str
)) = '\0';
922 /* Convert string to prefix. */
923 if (sscanf(str_addr
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2,
929 for (i
= 0; i
< 6; ++i
) {
930 p
->eth_addr
.octet
[i
] = a
[i
] & 0xff;
933 p
->family
= AF_ETHERNET
;
936 * special case to allow old configurations to work
937 * Since all zero's is implicitly meant to allow
938 * a comparison to zero, let's assume
940 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 strncpy(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 struct prefix
*prefix_new()
1366 p
= XCALLOC(MTYPE_PREFIX
, sizeof *p
);
1370 /* Free prefix structure. */
1371 void prefix_free(struct prefix
*p
)
1373 XFREE(MTYPE_PREFIX
, p
);
1376 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1377 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1380 uint32_t destination
;
1382 destination
= ntohl(p
->prefix
.s_addr
);
1384 if (p
->prefixlen
== IPV4_MAX_PREFIXLEN
)
1386 /* do nothing for host routes */
1387 else if (IN_CLASSC(destination
)) {
1390 } else if (IN_CLASSB(destination
)) {
1399 in_addr_t
ipv4_network_addr(in_addr_t hostaddr
, int masklen
)
1401 struct in_addr mask
;
1403 masklen2ip(masklen
, &mask
);
1404 return hostaddr
& mask
.s_addr
;
1407 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1409 struct in_addr mask
;
1411 masklen2ip(masklen
, &mask
);
1412 return (masklen
!= IPV4_MAX_PREFIXLEN
- 1) ?
1414 (hostaddr
| ~mask
.s_addr
)
1416 /* special case for /31 */
1417 (hostaddr
^ ~mask
.s_addr
);
1420 /* Utility function to convert ipv4 netmask to prefixes
1421 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1422 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1423 int netmask_str2prefix_str(const char *net_str
, const char *mask_str
,
1426 struct in_addr network
;
1427 struct in_addr mask
;
1429 uint32_t destination
;
1432 ret
= inet_aton(net_str
, &network
);
1437 ret
= inet_aton(mask_str
, &mask
);
1441 prefixlen
= ip_masklen(mask
);
1443 destination
= ntohl(network
.s_addr
);
1445 if (network
.s_addr
== 0)
1447 else if (IN_CLASSC(destination
))
1449 else if (IN_CLASSB(destination
))
1451 else if (IN_CLASSA(destination
))
1457 sprintf(prefix_str
, "%s/%d", net_str
, prefixlen
);
1462 /* Utility function for making IPv6 address string. */
1463 const char *inet6_ntoa(struct in6_addr addr
)
1465 static char buf
[INET6_ADDRSTRLEN
];
1467 inet_ntop(AF_INET6
, &addr
, buf
, INET6_ADDRSTRLEN
);
1471 /* converts to internal representation of mac address
1472 * returns 1 on success, 0 otherwise
1473 * format accepted: AA:BB:CC:DD:EE:FF
1474 * if mac parameter is null, then check only
1476 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1484 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1487 /* error in incoming str length */
1490 /* valid mac address */
1493 for (i
= 0; i
< 6; ++i
)
1494 mac
->octet
[i
] = a
[i
] & 0xff;
1498 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1505 ptr
= (char *)XMALLOC(MTYPE_TMP
,
1506 ETHER_ADDR_STRLEN
* sizeof(char));
1508 assert(size
>= ETHER_ADDR_STRLEN
);
1511 snprintf(ptr
, (ETHER_ADDR_STRLEN
), "%02x:%02x:%02x:%02x:%02x:%02x",
1512 (uint8_t)mac
->octet
[0], (uint8_t)mac
->octet
[1],
1513 (uint8_t)mac
->octet
[2], (uint8_t)mac
->octet
[3],
1514 (uint8_t)mac
->octet
[4], (uint8_t)mac
->octet
[5]);
1518 unsigned prefix_hash_key(void *pp
)
1522 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1526 /* make sure *all* unused bits are zero,
1527 * particularly including alignment /
1528 * padding and unused prefix bytes.
1530 memset(©
, 0, sizeof(copy
));
1531 prefix_copy(©
, (struct prefix
*)pp
);
1532 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1533 copy
.u
.prefix_flowspec
.prefixlen
,
1535 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1536 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1537 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1540 /* make sure *all* unused bits are zero, particularly including
1542 * padding and unused prefix bytes. */
1543 memset(©
, 0, sizeof(copy
));
1544 prefix_copy(©
, (struct prefix
*)pp
);
1546 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),
1550 /* converts to internal representation of esi
1551 * returns 1 on success, 0 otherwise
1552 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1553 * if esi parameter is null, then check only
1555 int str_to_esi(const char *str
, esi_t
*esi
)
1558 unsigned int a
[ESI_BYTES
];
1563 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1564 a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1565 a
+ 4, a
+ 5, a
+ 6, a
+ 7,
1568 /* error in incoming str length */
1575 for (i
= 0; i
< ESI_BYTES
; ++i
)
1576 esi
->val
[i
] = a
[i
] & 0xff;
1580 char *esi_to_str(const esi_t
*esi
, char *buf
, int size
)
1587 ptr
= (char *)XMALLOC(MTYPE_TMP
,
1588 ESI_STR_LEN
* sizeof(char));
1590 assert(size
>= ESI_STR_LEN
);
1594 snprintf(ptr
, ESI_STR_LEN
,
1595 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1596 esi
->val
[0], esi
->val
[1], esi
->val
[2],
1597 esi
->val
[3], esi
->val
[4], esi
->val
[5],
1598 esi
->val
[6], esi
->val
[7], esi
->val
[8],