]>
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")
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 uint8_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 uint8_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
);
974 /* clz: count leading zeroes. sadly, the behaviour of this
976 * is undefined for a 0 argument, even though most CPUs give 32
978 return __builtin_clz(tmp
);
983 /* Apply mask to IPv4 prefix (network byte order). */
984 void apply_mask_ipv4(struct prefix_ipv4
*p
)
987 masklen2ip(p
->prefixlen
, &mask
);
988 p
->prefix
.s_addr
&= mask
.s_addr
;
991 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
992 int prefix_ipv4_any(const struct prefix_ipv4
*p
)
994 return (p
->prefix
.s_addr
== 0 && p
->prefixlen
== 0);
997 /* Allocate a new ip version 6 route */
998 struct prefix_ipv6
*prefix_ipv6_new(void)
1000 struct prefix_ipv6
*p
;
1002 /* Allocate a full-size struct prefix to avoid problems with structure
1004 p
= (struct prefix_ipv6
*)prefix_new();
1005 p
->family
= AF_INET6
;
1009 /* Free prefix for IPv6. */
1010 void prefix_ipv6_free(struct prefix_ipv6
*p
)
1012 prefix_free((struct prefix
*)p
);
1015 /* If given string is valid return pin6 else return NULL */
1016 int str2prefix_ipv6(const char *str
, struct prefix_ipv6
*p
)
1022 pnt
= strchr(str
, '/');
1024 /* If string doesn't contain `/' treat it as host route. */
1026 ret
= inet_pton(AF_INET6
, str
, &p
->prefix
);
1029 p
->prefixlen
= IPV6_MAX_BITLEN
;
1033 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
1034 strncpy(cp
, str
, pnt
- str
);
1035 *(cp
+ (pnt
- str
)) = '\0';
1036 ret
= inet_pton(AF_INET6
, cp
, &p
->prefix
);
1037 XFREE(MTYPE_TMP
, cp
);
1040 plen
= (uint8_t)atoi(++pnt
);
1041 if (plen
> IPV6_MAX_BITLEN
)
1043 p
->prefixlen
= plen
;
1045 p
->family
= AF_INET6
;
1050 /* Convert struct in6_addr netmask into integer.
1051 * FIXME return uint8_t as ip_maskleni() does. */
1052 int ip6_masklen(struct in6_addr netmask
)
1058 pnt
= (unsigned char *)&netmask
;
1060 while ((*pnt
== 0xff) && len
< IPV6_MAX_BITLEN
) {
1065 if (len
< IPV6_MAX_BITLEN
) {
1075 void masklen2ip6(const int masklen
, struct in6_addr
*netmask
)
1077 assert(masklen
>= 0 && masklen
<= IPV6_MAX_BITLEN
);
1078 memcpy(netmask
, maskbytes6
+ masklen
, sizeof(struct in6_addr
));
1081 void apply_mask_ipv6(struct prefix_ipv6
*p
)
1087 index
= p
->prefixlen
/ 8;
1090 pnt
= (uint8_t *)&p
->prefix
;
1091 offset
= p
->prefixlen
% 8;
1093 pnt
[index
] &= maskbit
[offset
];
1101 void apply_mask(struct prefix
*p
)
1103 switch (p
->family
) {
1105 apply_mask_ipv4((struct prefix_ipv4
*)p
);
1108 apply_mask_ipv6((struct prefix_ipv6
*)p
);
1116 /* Utility function of convert between struct prefix <=> union sockunion.
1117 * FIXME This function isn't used anywhere. */
1118 struct prefix
*sockunion2prefix(const union sockunion
*dest
,
1119 const union sockunion
*mask
)
1121 if (dest
->sa
.sa_family
== AF_INET
) {
1122 struct prefix_ipv4
*p
;
1124 p
= prefix_ipv4_new();
1125 p
->family
= AF_INET
;
1126 p
->prefix
= dest
->sin
.sin_addr
;
1127 p
->prefixlen
= ip_masklen(mask
->sin
.sin_addr
);
1128 return (struct prefix
*)p
;
1130 if (dest
->sa
.sa_family
== AF_INET6
) {
1131 struct prefix_ipv6
*p
;
1133 p
= prefix_ipv6_new();
1134 p
->family
= AF_INET6
;
1135 p
->prefixlen
= ip6_masklen(mask
->sin6
.sin6_addr
);
1136 memcpy(&p
->prefix
, &dest
->sin6
.sin6_addr
,
1137 sizeof(struct in6_addr
));
1138 return (struct prefix
*)p
;
1143 /* Utility function of convert between struct prefix <=> union sockunion. */
1144 struct prefix
*sockunion2hostprefix(const union sockunion
*su
,
1145 struct prefix
*prefix
)
1147 if (su
->sa
.sa_family
== AF_INET
) {
1148 struct prefix_ipv4
*p
;
1150 p
= prefix
? (struct prefix_ipv4
*)prefix
: prefix_ipv4_new();
1151 p
->family
= AF_INET
;
1152 p
->prefix
= su
->sin
.sin_addr
;
1153 p
->prefixlen
= IPV4_MAX_BITLEN
;
1154 return (struct prefix
*)p
;
1156 if (su
->sa
.sa_family
== AF_INET6
) {
1157 struct prefix_ipv6
*p
;
1159 p
= prefix
? (struct prefix_ipv6
*)prefix
: prefix_ipv6_new();
1160 p
->family
= AF_INET6
;
1161 p
->prefixlen
= IPV6_MAX_BITLEN
;
1162 memcpy(&p
->prefix
, &su
->sin6
.sin6_addr
,
1163 sizeof(struct in6_addr
));
1164 return (struct prefix
*)p
;
1169 void prefix2sockunion(const struct prefix
*p
, union sockunion
*su
)
1171 memset(su
, 0, sizeof(*su
));
1173 su
->sa
.sa_family
= p
->family
;
1174 if (p
->family
== AF_INET
)
1175 su
->sin
.sin_addr
= p
->u
.prefix4
;
1176 if (p
->family
== AF_INET6
)
1177 memcpy(&su
->sin6
.sin6_addr
, &p
->u
.prefix6
,
1178 sizeof(struct in6_addr
));
1181 int prefix_blen(const struct prefix
*p
)
1183 switch (p
->family
) {
1185 return IPV4_MAX_BYTELEN
;
1188 return IPV6_MAX_BYTELEN
;
1197 /* Generic function for conversion string to struct prefix. */
1198 int str2prefix(const char *str
, struct prefix
*p
)
1205 /* First we try to convert string to struct prefix_ipv4. */
1206 ret
= str2prefix_ipv4(str
, (struct prefix_ipv4
*)p
);
1210 /* Next we try to convert string to struct prefix_ipv6. */
1211 ret
= str2prefix_ipv6(str
, (struct prefix_ipv6
*)p
);
1215 /* Next we try to convert string to struct prefix_eth. */
1216 ret
= str2prefix_eth(str
, (struct prefix_eth
*)p
);
1223 static const char *prefixevpn_ead2str(const struct prefix_evpn
*p
, char *str
,
1226 snprintf(str
, size
, "Unsupported EVPN prefix");
1230 static const char *prefixevpn_macip2str(const struct prefix_evpn
*p
, char *str
,
1234 char buf
[PREFIX2STR_BUFFER
];
1235 char buf2
[ETHER_ADDR_STRLEN
];
1237 if (is_evpn_prefix_ipaddr_none(p
))
1238 snprintf(str
, size
, "[%d]:[%s]/%d",
1239 p
->prefix
.route_type
,
1240 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
1241 buf2
, sizeof(buf2
)),
1244 family
= is_evpn_prefix_ipaddr_v4(p
)
1247 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d",
1248 p
->prefix
.route_type
,
1249 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
1250 buf2
, sizeof(buf2
)),
1252 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
1253 buf
, PREFIX2STR_BUFFER
),
1259 static const char *prefixevpn_imet2str(const struct prefix_evpn
*p
, char *str
,
1263 char buf
[PREFIX2STR_BUFFER
];
1265 family
= is_evpn_prefix_ipaddr_v4(p
)
1268 snprintf(str
, size
, "[%d]:[%s]/%d", p
->prefix
.route_type
,
1270 &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf
,
1276 static const char *prefixevpn_es2str(const struct prefix_evpn
*p
, char *str
,
1279 char buf
[ESI_STR_LEN
];
1281 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d", p
->prefix
.route_type
,
1282 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1283 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
),
1288 static const char *prefixevpn_prefix2str(const struct prefix_evpn
*p
, char *str
,
1292 char buf
[PREFIX2STR_BUFFER
];
1294 family
= is_evpn_prefix_ipaddr_v4(p
)
1297 snprintf(str
, size
, "[%d]:[%u][%s/%d]/%d",
1298 p
->prefix
.route_type
,
1299 p
->prefix
.prefix_addr
.eth_tag
,
1301 &p
->prefix
.prefix_addr
.ip
.ip
.addr
, buf
,
1303 p
->prefix
.prefix_addr
.ip_prefix_length
,
1308 static const char *prefixevpn2str(const struct prefix_evpn
*p
, char *str
,
1311 switch (p
->prefix
.route_type
) {
1313 return prefixevpn_ead2str(p
, str
, size
);
1315 return prefixevpn_macip2str(p
, str
, size
);
1317 return prefixevpn_imet2str(p
, str
, size
);
1319 return prefixevpn_es2str(p
, str
, size
);
1321 return prefixevpn_prefix2str(p
, str
, size
);
1323 snprintf(str
, size
, "Unsupported EVPN prefix");
1329 const char *prefix2str(union prefixconstptr pu
, char *str
, int size
)
1331 const struct prefix
*p
= pu
.p
;
1332 char buf
[PREFIX2STR_BUFFER
];
1334 switch (p
->family
) {
1337 snprintf(str
, size
, "%s/%d", inet_ntop(p
->family
, &p
->u
.prefix
,
1338 buf
, PREFIX2STR_BUFFER
),
1343 snprintf(str
, size
, "%s/%d",
1344 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
)),
1349 prefixevpn2str((const struct prefix_evpn
*)p
, str
, size
);
1353 sprintf(str
, "FS prefix");
1357 sprintf(str
, "UNK prefix");
1364 struct prefix
*prefix_new()
1368 p
= XCALLOC(MTYPE_PREFIX
, sizeof *p
);
1372 /* Free prefix structure. */
1373 void prefix_free(struct prefix
*p
)
1375 XFREE(MTYPE_PREFIX
, p
);
1378 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1379 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1382 uint32_t destination
;
1384 destination
= ntohl(p
->prefix
.s_addr
);
1386 if (p
->prefixlen
== IPV4_MAX_PREFIXLEN
)
1388 /* do nothing for host routes */
1389 else if (IN_CLASSC(destination
)) {
1392 } else if (IN_CLASSB(destination
)) {
1401 in_addr_t
ipv4_network_addr(in_addr_t hostaddr
, int masklen
)
1403 struct in_addr mask
;
1405 masklen2ip(masklen
, &mask
);
1406 return hostaddr
& mask
.s_addr
;
1409 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1411 struct in_addr mask
;
1413 masklen2ip(masklen
, &mask
);
1414 return (masklen
!= IPV4_MAX_PREFIXLEN
- 1) ?
1416 (hostaddr
| ~mask
.s_addr
)
1418 /* special case for /31 */
1419 (hostaddr
^ ~mask
.s_addr
);
1422 /* Utility function to convert ipv4 netmask to prefixes
1423 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1424 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1425 int netmask_str2prefix_str(const char *net_str
, const char *mask_str
,
1428 struct in_addr network
;
1429 struct in_addr mask
;
1431 uint32_t destination
;
1434 ret
= inet_aton(net_str
, &network
);
1439 ret
= inet_aton(mask_str
, &mask
);
1443 prefixlen
= ip_masklen(mask
);
1445 destination
= ntohl(network
.s_addr
);
1447 if (network
.s_addr
== 0)
1449 else if (IN_CLASSC(destination
))
1451 else if (IN_CLASSB(destination
))
1453 else if (IN_CLASSA(destination
))
1459 sprintf(prefix_str
, "%s/%d", net_str
, prefixlen
);
1464 /* Utility function for making IPv6 address string. */
1465 const char *inet6_ntoa(struct in6_addr addr
)
1467 static char buf
[INET6_ADDRSTRLEN
];
1469 inet_ntop(AF_INET6
, &addr
, buf
, INET6_ADDRSTRLEN
);
1473 /* converts to internal representation of mac address
1474 * returns 1 on success, 0 otherwise
1475 * format accepted: AA:BB:CC:DD:EE:FF
1476 * if mac parameter is null, then check only
1478 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1486 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1489 /* error in incoming str length */
1492 /* valid mac address */
1495 for (i
= 0; i
< 6; ++i
)
1496 mac
->octet
[i
] = a
[i
] & 0xff;
1500 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1507 ptr
= (char *)XMALLOC(MTYPE_TMP
,
1508 ETHER_ADDR_STRLEN
* sizeof(char));
1510 assert(size
>= ETHER_ADDR_STRLEN
);
1513 snprintf(ptr
, (ETHER_ADDR_STRLEN
), "%02x:%02x:%02x:%02x:%02x:%02x",
1514 (uint8_t)mac
->octet
[0], (uint8_t)mac
->octet
[1],
1515 (uint8_t)mac
->octet
[2], (uint8_t)mac
->octet
[3],
1516 (uint8_t)mac
->octet
[4], (uint8_t)mac
->octet
[5]);
1520 unsigned prefix_hash_key(void *pp
)
1524 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1528 /* make sure *all* unused bits are zero,
1529 * particularly including alignment /
1530 * padding and unused prefix bytes.
1532 memset(©
, 0, sizeof(copy
));
1533 prefix_copy(©
, (struct prefix
*)pp
);
1534 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1535 copy
.u
.prefix_flowspec
.prefixlen
,
1537 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1538 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1539 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1542 /* make sure *all* unused bits are zero, particularly including
1544 * padding and unused prefix bytes. */
1545 memset(©
, 0, sizeof(copy
));
1546 prefix_copy(©
, (struct prefix
*)pp
);
1548 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),
1552 /* converts to internal representation of esi
1553 * returns 1 on success, 0 otherwise
1554 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1555 * if esi parameter is null, then check only
1557 int str_to_esi(const char *str
, esi_t
*esi
)
1560 unsigned int a
[ESI_BYTES
];
1565 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1566 a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1567 a
+ 4, a
+ 5, a
+ 6, a
+ 7,
1570 /* error in incoming str length */
1577 for (i
= 0; i
< ESI_BYTES
; ++i
)
1578 esi
->val
[i
] = a
[i
] & 0xff;
1582 char *esi_to_str(const esi_t
*esi
, char *buf
, int size
)
1589 ptr
= (char *)XMALLOC(MTYPE_TMP
,
1590 ESI_STR_LEN
* sizeof(char));
1592 assert(size
>= ESI_STR_LEN
);
1596 snprintf(ptr
, ESI_STR_LEN
,
1597 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1598 esi
->val
[0], esi
->val
[1], esi
->val
[2],
1599 esi
->val
[3], esi
->val
[4], esi
->val
[5],
1600 esi
->val
[6], esi
->val
[7], esi
->val
[8],