]>
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"
33 DEFINE_MTYPE_STATIC(LIB
, PREFIX
, "Prefix")
34 DEFINE_MTYPE_STATIC(LIB
, PREFIX_FLOWSPEC
, "Prefix Flowspec")
37 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
38 0xf8, 0xfc, 0xfe, 0xff};
40 static const struct in6_addr maskbytes6
[] = {
41 /* /0 */ {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}},
44 {{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00}}},
47 {{{0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00}}},
50 {{{0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00}}},
53 {{{0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00}}},
56 {{{0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00}}},
59 {{{0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00}}},
62 {{{0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00}}},
65 {{{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00}}},
68 {{{0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00}}},
71 {{{0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00}}},
74 {{{0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00}}},
77 {{{0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00}}},
80 {{{0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00}}},
83 {{{0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0x00}}},
86 {{{0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00}}},
89 {{{0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00}}},
92 {{{0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00}}},
95 {{{0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00, 0x00}}},
98 {{{0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00, 0x00}}},
101 {{{0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102 0x00, 0x00, 0x00, 0x00, 0x00}}},
104 {{{0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x00}}},
107 {{{0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 0x00, 0x00, 0x00, 0x00, 0x00}}},
110 {{{0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x00}}},
113 {{{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00}}},
116 {{{0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 0x00, 0x00, 0x00, 0x00, 0x00}}},
119 {{{0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
120 0x00, 0x00, 0x00, 0x00, 0x00}}},
122 {{{0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 0x00, 0x00, 0x00, 0x00, 0x00}}},
125 {{{0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00}}},
128 {{{0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00}}},
131 {{{0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 0x00, 0x00, 0x00, 0x00, 0x00}}},
134 {{{0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00, 0x00}}},
137 {{{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 0x00, 0x00, 0x00, 0x00, 0x00}}},
140 {{{0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
141 0x00, 0x00, 0x00, 0x00, 0x00}}},
143 {{{0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x00, 0x00}}},
146 {{{0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147 0x00, 0x00, 0x00, 0x00, 0x00}}},
149 {{{0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150 0x00, 0x00, 0x00, 0x00, 0x00}}},
152 {{{0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00}}},
155 {{{0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x00}}},
158 {{{0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0x00, 0x00}}},
161 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162 0x00, 0x00, 0x00, 0x00, 0x00}}},
164 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
165 0x00, 0x00, 0x00, 0x00, 0x00}}},
167 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
168 0x00, 0x00, 0x00, 0x00, 0x00}}},
170 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
171 0x00, 0x00, 0x00, 0x00, 0x00}}},
173 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
174 0x00, 0x00, 0x00, 0x00, 0x00}}},
176 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
177 0x00, 0x00, 0x00, 0x00, 0x00}}},
179 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
180 0x00, 0x00, 0x00, 0x00, 0x00}}},
182 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
183 0x00, 0x00, 0x00, 0x00, 0x00}}},
185 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
186 0x00, 0x00, 0x00, 0x00, 0x00}}},
188 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00,
189 0x00, 0x00, 0x00, 0x00, 0x00}}},
191 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00, 0x00}}},
194 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
195 0x00, 0x00, 0x00, 0x00, 0x00}}},
197 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00}}},
200 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
201 0x00, 0x00, 0x00, 0x00, 0x00}}},
203 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
204 0x00, 0x00, 0x00, 0x00, 0x00}}},
206 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
207 0x00, 0x00, 0x00, 0x00, 0x00}}},
209 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
210 0x00, 0x00, 0x00, 0x00, 0x00}}},
212 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
213 0x00, 0x00, 0x00, 0x00, 0x00}}},
215 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00,
216 0x00, 0x00, 0x00, 0x00, 0x00}}},
218 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00,
219 0x00, 0x00, 0x00, 0x00, 0x00}}},
221 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00,
222 0x00, 0x00, 0x00, 0x00, 0x00}}},
224 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00,
225 0x00, 0x00, 0x00, 0x00, 0x00}}},
227 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x00}}},
230 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00,
231 0x00, 0x00, 0x00, 0x00, 0x00}}},
233 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
234 0x00, 0x00, 0x00, 0x00, 0x00}}},
236 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00}}},
239 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
240 0x00, 0x00, 0x00, 0x00, 0x00}}},
242 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00,
243 0x00, 0x00, 0x00, 0x00, 0x00}}},
245 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00,
246 0x00, 0x00, 0x00, 0x00, 0x00}}},
248 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00,
249 0x00, 0x00, 0x00, 0x00, 0x00}}},
251 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00,
252 0x00, 0x00, 0x00, 0x00, 0x00}}},
254 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00,
255 0x00, 0x00, 0x00, 0x00, 0x00}}},
257 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
258 0x00, 0x00, 0x00, 0x00, 0x00}}},
260 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
261 0x00, 0x00, 0x00, 0x00, 0x00}}},
263 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00,
264 0x00, 0x00, 0x00, 0x00, 0x00}}},
266 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00,
267 0x00, 0x00, 0x00, 0x00, 0x00}}},
269 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00,
270 0x00, 0x00, 0x00, 0x00, 0x00}}},
272 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00,
273 0x00, 0x00, 0x00, 0x00, 0x00}}},
275 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00,
276 0x00, 0x00, 0x00, 0x00, 0x00}}},
278 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00,
279 0x00, 0x00, 0x00, 0x00, 0x00}}},
281 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
282 0x00, 0x00, 0x00, 0x00, 0x00}}},
284 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
285 0x00, 0x00, 0x00, 0x00, 0x00}}},
287 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
288 0x00, 0x00, 0x00, 0x00, 0x00}}},
290 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
291 0x00, 0x00, 0x00, 0x00, 0x00}}},
293 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
294 0x00, 0x00, 0x00, 0x00, 0x00}}},
296 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8,
297 0x00, 0x00, 0x00, 0x00, 0x00}}},
299 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
300 0x00, 0x00, 0x00, 0x00, 0x00}}},
302 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
303 0x00, 0x00, 0x00, 0x00, 0x00}}},
305 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
306 0x00, 0x00, 0x00, 0x00, 0x00}}},
308 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
309 0x80, 0x00, 0x00, 0x00, 0x00}}},
311 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
312 0xc0, 0x00, 0x00, 0x00, 0x00}}},
314 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
315 0xe0, 0x00, 0x00, 0x00, 0x00}}},
317 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
318 0xf0, 0x00, 0x00, 0x00, 0x00}}},
320 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
321 0xf8, 0x00, 0x00, 0x00, 0x00}}},
323 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
324 0xfc, 0x00, 0x00, 0x00, 0x00}}},
326 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
327 0xfe, 0x00, 0x00, 0x00, 0x00}}},
329 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
330 0xff, 0x00, 0x00, 0x00, 0x00}}},
332 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
333 0xff, 0x80, 0x00, 0x00, 0x00}}},
335 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
336 0xff, 0xc0, 0x00, 0x00, 0x00}}},
338 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
339 0xff, 0xe0, 0x00, 0x00, 0x00}}},
341 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
342 0xff, 0xf0, 0x00, 0x00, 0x00}}},
344 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
345 0xff, 0xf8, 0x00, 0x00, 0x00}}},
347 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
348 0xff, 0xfc, 0x00, 0x00, 0x00}}},
350 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
351 0xff, 0xfe, 0x00, 0x00, 0x00}}},
353 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
354 0xff, 0xff, 0x00, 0x00, 0x00}}},
356 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
357 0xff, 0xff, 0x80, 0x00, 0x00}}},
359 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
360 0xff, 0xff, 0xc0, 0x00, 0x00}}},
362 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
363 0xff, 0xff, 0xe0, 0x00, 0x00}}},
365 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
366 0xff, 0xff, 0xf0, 0x00, 0x00}}},
368 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
369 0xff, 0xff, 0xf8, 0x00, 0x00}}},
371 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
372 0xff, 0xff, 0xfc, 0x00, 0x00}}},
374 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
375 0xff, 0xff, 0xfe, 0x00, 0x00}}},
377 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
378 0xff, 0xff, 0xff, 0x00, 0x00}}},
380 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
381 0xff, 0xff, 0xff, 0x80, 0x00}}},
383 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
384 0xff, 0xff, 0xff, 0xc0, 0x00}}},
386 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
387 0xff, 0xff, 0xff, 0xe0, 0x00}}},
389 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
390 0xff, 0xff, 0xff, 0xf0, 0x00}}},
392 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
393 0xff, 0xff, 0xff, 0xf8, 0x00}}},
395 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
396 0xff, 0xff, 0xff, 0xfc, 0x00}}},
398 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
399 0xff, 0xff, 0xff, 0xfe, 0x00}}},
401 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
402 0xff, 0xff, 0xff, 0xff, 0x00}}},
404 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
405 0xff, 0xff, 0xff, 0xff, 0x80}}},
407 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
408 0xff, 0xff, 0xff, 0xff, 0xc0}}},
410 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
411 0xff, 0xff, 0xff, 0xff, 0xe0}}},
413 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
414 0xff, 0xff, 0xff, 0xff, 0xf0}}},
416 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
417 0xff, 0xff, 0xff, 0xff, 0xf8}}},
419 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
420 0xff, 0xff, 0xff, 0xff, 0xfc}}},
422 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
423 0xff, 0xff, 0xff, 0xff, 0xfe}}},
425 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
426 0xff, 0xff, 0xff, 0xff, 0xff}}}};
428 /* Number of bits in prefix type. */
433 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
435 void prefix_hexdump(const struct prefix
*p
)
437 char buf
[PREFIX_STRLEN
];
439 zlog_debug("prefix: %s",
440 prefix2str(p
, buf
, sizeof(buf
)));
441 zlog_hexdump(p
, sizeof(struct prefix
));
444 int is_zero_mac(const struct ethaddr
*mac
)
448 for (i
= 0; i
< ETH_ALEN
; i
++) {
456 unsigned int prefix_bit(const uint8_t *prefix
, const uint16_t prefixlen
)
458 unsigned int offset
= prefixlen
/ 8;
459 unsigned int shift
= 7 - (prefixlen
% 8);
461 return (prefix
[offset
] >> shift
) & 1;
464 unsigned int prefix6_bit(const struct in6_addr
*prefix
, const uint16_t prefixlen
)
466 return prefix_bit((const uint8_t *)&prefix
->s6_addr
, prefixlen
);
469 int str2family(const char *string
)
471 if (!strcmp("ipv4", string
))
473 else if (!strcmp("ipv6", string
))
475 else if (!strcmp("ethernet", string
))
477 else if (!strcmp("evpn", string
))
482 const char *family2str(int family
)
497 /* Address Famiy Identifier to Address Family converter. */
498 int afi2family(afi_t afi
)
502 else if (afi
== AFI_IP6
)
504 else if (afi
== AFI_L2VPN
)
506 /* NOTE: EVPN code should NOT use this interface. */
510 afi_t
family2afi(int family
)
512 if (family
== AF_INET
)
514 else if (family
== AF_INET6
)
516 else if (family
== AF_ETHERNET
|| family
== AF_EVPN
)
521 const char *afi2str(afi_t afi
)
538 const char *safi2str(safi_t safi
)
551 case SAFI_LABELED_UNICAST
:
552 return "labeled-unicast";
560 /* If n includes p prefix then return 1 else return 0. */
561 int prefix_match(const struct prefix
*n
, const struct prefix
*p
)
565 const uint8_t *np
, *pp
;
567 /* If n's prefix is longer than p's one return 0. */
568 if (n
->prefixlen
> p
->prefixlen
)
571 if (n
->family
== AF_FLOWSPEC
) {
572 /* prefixlen is unused. look at fs prefix len */
573 if (n
->u
.prefix_flowspec
.prefixlen
>
574 p
->u
.prefix_flowspec
.prefixlen
)
577 /* Set both prefix's head pointer. */
578 np
= (const uint8_t *)&n
->u
.prefix_flowspec
.ptr
;
579 pp
= (const uint8_t *)&p
->u
.prefix_flowspec
.ptr
;
581 offset
= n
->u
.prefix_flowspec
.prefixlen
;
584 if (np
[offset
] != pp
[offset
])
589 /* Set both prefix's head pointer. */
593 offset
= n
->prefixlen
/ PNBBY
;
594 shift
= n
->prefixlen
% PNBBY
;
597 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
601 if (np
[offset
] != pp
[offset
])
608 * n is a type5 evpn prefix. This function tries to see if there is an
609 * ip-prefix within n which matches prefix p
610 * If n includes p prefix then return 1 else return 0.
612 int evpn_type5_prefix_match(const struct prefix
*n
, const struct prefix
*p
)
617 const uint8_t *np
, *pp
;
618 struct prefix_evpn
*evp
;
620 if (n
->family
!= AF_EVPN
)
623 evp
= (struct prefix_evpn
*)n
;
626 if ((evp
->prefix
.route_type
!= 5) ||
627 (p
->family
== AF_INET6
&& !is_evpn_prefix_ipaddr_v6(evp
)) ||
628 (p
->family
== AF_INET
&& !is_evpn_prefix_ipaddr_v4(evp
)) ||
629 (is_evpn_prefix_ipaddr_none(evp
)))
632 prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
633 np
= &evp
->prefix
.prefix_addr
.ip
.ip
.addr
;
635 /* If n's prefix is longer than p's one return 0. */
636 if (prefixlen
> p
->prefixlen
)
639 offset
= prefixlen
/ PNBBY
;
640 shift
= prefixlen
% PNBBY
;
643 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
647 if (np
[offset
] != pp
[offset
])
653 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
654 int prefix_match_network_statement(const struct prefix
*n
,
655 const struct prefix
*p
)
659 const uint8_t *np
, *pp
;
661 /* Set both prefix's head pointer. */
665 offset
= n
->prefixlen
/ PNBBY
;
666 shift
= n
->prefixlen
% PNBBY
;
669 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
673 if (np
[offset
] != pp
[offset
])
678 #ifdef __clang_analyzer__
679 #undef prefix_copy /* cf. prefix.h */
682 void prefix_copy(union prefixptr udest
, union prefixconstptr usrc
)
684 struct prefix
*dest
= udest
.p
;
685 const struct prefix
*src
= usrc
.p
;
687 dest
->family
= src
->family
;
688 dest
->prefixlen
= src
->prefixlen
;
690 if (src
->family
== AF_INET
)
691 dest
->u
.prefix4
= src
->u
.prefix4
;
692 else if (src
->family
== AF_INET6
)
693 dest
->u
.prefix6
= src
->u
.prefix6
;
694 else if (src
->family
== AF_ETHERNET
) {
695 memcpy(&dest
->u
.prefix_eth
, &src
->u
.prefix_eth
,
696 sizeof(struct ethaddr
));
697 } else if (src
->family
== AF_EVPN
) {
698 memcpy(&dest
->u
.prefix_evpn
, &src
->u
.prefix_evpn
,
699 sizeof(struct evpn_addr
));
700 } else if (src
->family
== AF_UNSPEC
) {
701 dest
->u
.lp
.id
= src
->u
.lp
.id
;
702 dest
->u
.lp
.adv_router
= src
->u
.lp
.adv_router
;
703 } else if (src
->family
== AF_FLOWSPEC
) {
707 len
= src
->u
.prefix_flowspec
.prefixlen
;
708 dest
->u
.prefix_flowspec
.prefixlen
=
709 src
->u
.prefix_flowspec
.prefixlen
;
710 dest
->family
= src
->family
;
711 temp
= XCALLOC(MTYPE_PREFIX_FLOWSPEC
, len
);
712 dest
->u
.prefix_flowspec
.ptr
= (uintptr_t)temp
;
713 memcpy((void *)dest
->u
.prefix_flowspec
.ptr
,
714 (void *)src
->u
.prefix_flowspec
.ptr
, len
);
716 flog_err(EC_LIB_DEVELOPMENT
,
717 "prefix_copy(): Unknown address family %d",
724 * Return 1 if the address/netmask contained in the prefix structure
725 * is the same, and else return 0. For this routine, 'same' requires
726 * that not only the prefix length and the network part be the same,
727 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
728 * the same. Note that this routine has the same return value sense
729 * as '==' (which is different from prefix_cmp).
731 int prefix_same(union prefixconstptr up1
, union prefixconstptr up2
)
733 const struct prefix
*p1
= up1
.p
;
734 const struct prefix
*p2
= up2
.p
;
736 if ((p1
&& !p2
) || (!p1
&& p2
))
742 if (p1
->family
== p2
->family
&& p1
->prefixlen
== p2
->prefixlen
) {
743 if (p1
->family
== AF_INET
)
744 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
746 if (p1
->family
== AF_INET6
)
747 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
.s6_addr
,
748 &p2
->u
.prefix6
.s6_addr
))
750 if (p1
->family
== AF_ETHERNET
)
751 if (!memcmp(&p1
->u
.prefix_eth
, &p2
->u
.prefix_eth
,
752 sizeof(struct ethaddr
)))
754 if (p1
->family
== AF_EVPN
)
755 if (!memcmp(&p1
->u
.prefix_evpn
, &p2
->u
.prefix_evpn
,
756 sizeof(struct evpn_addr
)))
758 if (p1
->family
== AF_FLOWSPEC
) {
759 if (p1
->u
.prefix_flowspec
.prefixlen
!=
760 p2
->u
.prefix_flowspec
.prefixlen
)
762 if (!memcmp(&p1
->u
.prefix_flowspec
.ptr
,
763 &p2
->u
.prefix_flowspec
.ptr
,
764 p2
->u
.prefix_flowspec
.prefixlen
))
772 * Return -1/0/1 comparing the prefixes in a way that gives a full/linear
775 * Network prefixes are considered the same if the prefix lengths are equal
776 * and the network parts are the same. Host bits (which are considered masked
777 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
778 * 10.0.0.2/8 are considered equivalent by this routine. Note that
779 * this routine has the same return sense as strcmp (which is different
782 int prefix_cmp(union prefixconstptr up1
, union prefixconstptr up2
)
784 const struct prefix
*p1
= up1
.p
;
785 const struct prefix
*p2
= up2
.p
;
790 /* Set both prefix's head pointer. */
794 if (p1
->family
!= p2
->family
)
795 return numcmp(p1
->family
, p2
->family
);
796 if (p1
->family
== AF_FLOWSPEC
) {
797 pp1
= (const uint8_t *)p1
->u
.prefix_flowspec
.ptr
;
798 pp2
= (const uint8_t *)p2
->u
.prefix_flowspec
.ptr
;
800 if (p1
->u
.prefix_flowspec
.prefixlen
!=
801 p2
->u
.prefix_flowspec
.prefixlen
)
802 return numcmp(p1
->u
.prefix_flowspec
.prefixlen
,
803 p2
->u
.prefix_flowspec
.prefixlen
);
805 offset
= p1
->u
.prefix_flowspec
.prefixlen
;
807 if (pp1
[offset
] != pp2
[offset
])
808 return numcmp(pp1
[offset
], pp2
[offset
]);
814 if (p1
->prefixlen
!= p2
->prefixlen
)
815 return numcmp(p1
->prefixlen
, p2
->prefixlen
);
816 offset
= p1
->prefixlen
/ PNBBY
;
817 shift
= p1
->prefixlen
% PNBBY
;
819 i
= memcmp(pp1
, pp2
, offset
);
824 * At this point offset was the same, if we have shift
825 * that means we still have data to compare, if shift is
826 * 0 then we are at the end of the data structure
827 * and should just return, as that we will be accessing
828 * memory beyond the end of the party zone
831 return numcmp(pp1
[offset
] & maskbit
[shift
],
832 pp2
[offset
] & maskbit
[shift
]);
838 * Count the number of common bits in 2 prefixes. The prefix length is
839 * ignored for this function; the whole prefix is compared. If the prefix
840 * address families don't match, return -1; otherwise the return value is
841 * in range 0 ... maximum prefix length for the address family.
843 int prefix_common_bits(const struct prefix
*p1
, const struct prefix
*p2
)
849 /* Set both prefix's head pointer. */
850 const uint8_t *pp1
= p1
->u
.val
;
851 const uint8_t *pp2
= p2
->u
.val
;
853 if (p1
->family
== AF_INET
)
854 length
= IPV4_MAX_BYTELEN
;
855 if (p1
->family
== AF_INET6
)
856 length
= IPV6_MAX_BYTELEN
;
857 if (p1
->family
== AF_ETHERNET
)
859 if (p1
->family
== AF_EVPN
)
860 length
= 8 * sizeof(struct evpn_addr
);
862 if (p1
->family
!= p2
->family
|| !length
)
865 for (pos
= 0; pos
< length
; pos
++)
866 if (pp1
[pos
] != pp2
[pos
])
871 xor = pp1
[pos
] ^ pp2
[pos
];
872 for (bit
= 0; bit
< 8; bit
++)
873 if (xor&(1 << (7 - bit
)))
876 return pos
* 8 + bit
;
879 /* Return prefix family type string. */
880 const char *prefix_family_str(const struct prefix
*p
)
882 if (p
->family
== AF_INET
)
884 if (p
->family
== AF_INET6
)
886 if (p
->family
== AF_ETHERNET
)
888 if (p
->family
== AF_EVPN
)
893 /* Allocate new prefix_ipv4 structure. */
894 struct prefix_ipv4
*prefix_ipv4_new(void)
896 struct prefix_ipv4
*p
;
898 /* Call prefix_new to allocate a full-size struct prefix to avoid
900 where the struct prefix_ipv4 is cast to struct prefix and unallocated
901 bytes were being referenced (e.g. in structure assignments). */
902 p
= (struct prefix_ipv4
*)prefix_new();
907 /* Free prefix_ipv4 structure. */
908 void prefix_ipv4_free(struct prefix_ipv4
**p
)
910 prefix_free((struct prefix
**)p
);
913 /* If given string is valid return 1 else return 0 */
914 int str2prefix_ipv4(const char *str
, struct prefix_ipv4
*p
)
921 /* Find slash inside string. */
922 pnt
= strchr(str
, '/');
924 /* String doesn't contail slash. */
926 /* Convert string to prefix. */
927 ret
= inet_pton(AF_INET
, str
, &p
->prefix
);
931 /* If address doesn't contain slash we assume it host address.
934 p
->prefixlen
= IPV4_MAX_BITLEN
;
938 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
939 memcpy(cp
, str
, pnt
- str
);
940 *(cp
+ (pnt
- str
)) = '\0';
941 ret
= inet_pton(AF_INET
, cp
, &p
->prefix
);
942 XFREE(MTYPE_TMP
, cp
);
946 /* Get prefix length. */
947 plen
= (uint8_t)atoi(++pnt
);
948 if (plen
> IPV4_MAX_PREFIXLEN
)
958 /* When string format is invalid return 0. */
959 int str2prefix_eth(const char *str
, struct prefix_eth
*p
)
965 const char *str_addr
= str
;
970 if (!strcmp(str
, "any")) {
971 memset(p
, 0, sizeof(*p
));
972 p
->family
= AF_ETHERNET
;
976 /* Find slash inside string. */
977 pnt
= strchr(str
, '/');
980 /* Get prefix length. */
981 plen
= (uint8_t)atoi(++pnt
);
987 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
988 memcpy(cp
, str
, pnt
- str
);
989 *(cp
+ (pnt
- str
)) = '\0';
995 /* Convert string to prefix. */
996 if (sscanf(str_addr
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2,
1002 for (i
= 0; i
< 6; ++i
) {
1003 p
->eth_addr
.octet
[i
] = a
[i
] & 0xff;
1005 p
->prefixlen
= plen
;
1006 p
->family
= AF_ETHERNET
;
1009 * special case to allow old configurations to work
1010 * Since all zero's is implicitly meant to allow
1011 * a comparison to zero, let's assume
1013 if (!slash
&& is_zero_mac(&(p
->eth_addr
)))
1019 XFREE(MTYPE_TMP
, cp
);
1024 /* Convert masklen into IP address's netmask (network byte order). */
1025 void masklen2ip(const int masklen
, struct in_addr
*netmask
)
1027 assert(masklen
>= 0 && masklen
<= IPV4_MAX_BITLEN
);
1029 /* left shift is only defined for less than the size of the type.
1030 * we unconditionally use long long in case the target platform
1031 * has defined behaviour for << 32 (or has a 64-bit left shift) */
1033 if (sizeof(unsigned long long) > 4)
1034 netmask
->s_addr
= htonl(0xffffffffULL
<< (32 - masklen
));
1037 htonl(masklen
? 0xffffffffU
<< (32 - masklen
) : 0);
1040 /* Convert IP address's netmask into integer. We assume netmask is
1041 * sequential one. Argument netmask should be network byte order. */
1042 uint8_t ip_masklen(struct in_addr netmask
)
1044 uint32_t tmp
= ~ntohl(netmask
.s_addr
);
1047 * clz: count leading zeroes. sadly, the behaviour of this builtin is
1048 * undefined for a 0 argument, even though most CPUs give 32
1050 return tmp
? __builtin_clz(tmp
) : 32;
1053 /* Apply mask to IPv4 prefix (network byte order). */
1054 void apply_mask_ipv4(struct prefix_ipv4
*p
)
1056 struct in_addr mask
;
1057 masklen2ip(p
->prefixlen
, &mask
);
1058 p
->prefix
.s_addr
&= mask
.s_addr
;
1061 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
1062 int prefix_ipv4_any(const struct prefix_ipv4
*p
)
1064 return (p
->prefix
.s_addr
== 0 && p
->prefixlen
== 0);
1067 /* Allocate a new ip version 6 route */
1068 struct prefix_ipv6
*prefix_ipv6_new(void)
1070 struct prefix_ipv6
*p
;
1072 /* Allocate a full-size struct prefix to avoid problems with structure
1074 p
= (struct prefix_ipv6
*)prefix_new();
1075 p
->family
= AF_INET6
;
1079 /* Free prefix for IPv6. */
1080 void prefix_ipv6_free(struct prefix_ipv6
**p
)
1082 prefix_free((struct prefix
**)p
);
1085 /* If given string is valid return 1 else return 0 */
1086 int str2prefix_ipv6(const char *str
, struct prefix_ipv6
*p
)
1092 pnt
= strchr(str
, '/');
1094 /* If string doesn't contain `/' treat it as host route. */
1096 ret
= inet_pton(AF_INET6
, str
, &p
->prefix
);
1099 p
->prefixlen
= IPV6_MAX_BITLEN
;
1103 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
1104 memcpy(cp
, str
, pnt
- str
);
1105 *(cp
+ (pnt
- str
)) = '\0';
1106 ret
= inet_pton(AF_INET6
, cp
, &p
->prefix
);
1107 XFREE(MTYPE_TMP
, cp
);
1110 plen
= (uint8_t)atoi(++pnt
);
1111 if (plen
> IPV6_MAX_BITLEN
)
1113 p
->prefixlen
= plen
;
1115 p
->family
= AF_INET6
;
1120 /* Convert struct in6_addr netmask into integer.
1121 * FIXME return uint8_t as ip_maskleni() does. */
1122 int ip6_masklen(struct in6_addr netmask
)
1128 pnt
= (unsigned char *)&netmask
;
1130 while ((*pnt
== 0xff) && len
< IPV6_MAX_BITLEN
) {
1135 if (len
< IPV6_MAX_BITLEN
) {
1145 void masklen2ip6(const int masklen
, struct in6_addr
*netmask
)
1147 assert(masklen
>= 0 && masklen
<= IPV6_MAX_BITLEN
);
1148 memcpy(netmask
, maskbytes6
+ masklen
, sizeof(struct in6_addr
));
1151 void apply_mask_ipv6(struct prefix_ipv6
*p
)
1157 index
= p
->prefixlen
/ 8;
1160 pnt
= (uint8_t *)&p
->prefix
;
1161 offset
= p
->prefixlen
% 8;
1163 pnt
[index
] &= maskbit
[offset
];
1171 void apply_mask(struct prefix
*p
)
1173 switch (p
->family
) {
1175 apply_mask_ipv4((struct prefix_ipv4
*)p
);
1178 apply_mask_ipv6((struct prefix_ipv6
*)p
);
1186 /* Utility function of convert between struct prefix <=> union sockunion.
1187 * FIXME This function isn't used anywhere. */
1188 struct prefix
*sockunion2prefix(const union sockunion
*dest
,
1189 const union sockunion
*mask
)
1191 if (dest
->sa
.sa_family
== AF_INET
) {
1192 struct prefix_ipv4
*p
;
1194 p
= prefix_ipv4_new();
1195 p
->family
= AF_INET
;
1196 p
->prefix
= dest
->sin
.sin_addr
;
1197 p
->prefixlen
= ip_masklen(mask
->sin
.sin_addr
);
1198 return (struct prefix
*)p
;
1200 if (dest
->sa
.sa_family
== AF_INET6
) {
1201 struct prefix_ipv6
*p
;
1203 p
= prefix_ipv6_new();
1204 p
->family
= AF_INET6
;
1205 p
->prefixlen
= ip6_masklen(mask
->sin6
.sin6_addr
);
1206 memcpy(&p
->prefix
, &dest
->sin6
.sin6_addr
,
1207 sizeof(struct in6_addr
));
1208 return (struct prefix
*)p
;
1213 /* Utility function of convert between struct prefix <=> union sockunion. */
1214 struct prefix
*sockunion2hostprefix(const union sockunion
*su
,
1215 struct prefix
*prefix
)
1217 if (su
->sa
.sa_family
== AF_INET
) {
1218 struct prefix_ipv4
*p
;
1220 p
= prefix
? (struct prefix_ipv4
*)prefix
: prefix_ipv4_new();
1221 p
->family
= AF_INET
;
1222 p
->prefix
= su
->sin
.sin_addr
;
1223 p
->prefixlen
= IPV4_MAX_BITLEN
;
1224 return (struct prefix
*)p
;
1226 if (su
->sa
.sa_family
== AF_INET6
) {
1227 struct prefix_ipv6
*p
;
1229 p
= prefix
? (struct prefix_ipv6
*)prefix
: prefix_ipv6_new();
1230 p
->family
= AF_INET6
;
1231 p
->prefixlen
= IPV6_MAX_BITLEN
;
1232 memcpy(&p
->prefix
, &su
->sin6
.sin6_addr
,
1233 sizeof(struct in6_addr
));
1234 return (struct prefix
*)p
;
1239 void prefix2sockunion(const struct prefix
*p
, union sockunion
*su
)
1241 memset(su
, 0, sizeof(*su
));
1243 su
->sa
.sa_family
= p
->family
;
1244 if (p
->family
== AF_INET
)
1245 su
->sin
.sin_addr
= p
->u
.prefix4
;
1246 if (p
->family
== AF_INET6
)
1247 memcpy(&su
->sin6
.sin6_addr
, &p
->u
.prefix6
,
1248 sizeof(struct in6_addr
));
1251 int prefix_blen(const struct prefix
*p
)
1253 switch (p
->family
) {
1255 return IPV4_MAX_BYTELEN
;
1258 return IPV6_MAX_BYTELEN
;
1267 /* Generic function for conversion string to struct prefix. */
1268 int str2prefix(const char *str
, struct prefix
*p
)
1275 /* First we try to convert string to struct prefix_ipv4. */
1276 ret
= str2prefix_ipv4(str
, (struct prefix_ipv4
*)p
);
1280 /* Next we try to convert string to struct prefix_ipv6. */
1281 ret
= str2prefix_ipv6(str
, (struct prefix_ipv6
*)p
);
1285 /* Next we try to convert string to struct prefix_eth. */
1286 ret
= str2prefix_eth(str
, (struct prefix_eth
*)p
);
1293 static const char *prefixevpn_ead2str(const struct prefix_evpn
*p
, char *str
,
1296 snprintf(str
, size
, "Unsupported EVPN prefix");
1300 static const char *prefixevpn_macip2str(const struct prefix_evpn
*p
, char *str
,
1304 char buf
[PREFIX2STR_BUFFER
];
1305 char buf2
[ETHER_ADDR_STRLEN
];
1307 if (is_evpn_prefix_ipaddr_none(p
))
1308 snprintf(str
, size
, "[%d]:[%s]/%d",
1309 p
->prefix
.route_type
,
1310 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
1311 buf2
, sizeof(buf2
)),
1314 family
= is_evpn_prefix_ipaddr_v4(p
)
1317 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d",
1318 p
->prefix
.route_type
,
1319 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
1320 buf2
, sizeof(buf2
)),
1322 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
1323 buf
, PREFIX2STR_BUFFER
),
1329 static const char *prefixevpn_imet2str(const struct prefix_evpn
*p
, char *str
,
1333 char buf
[PREFIX2STR_BUFFER
];
1335 family
= is_evpn_prefix_ipaddr_v4(p
)
1338 snprintf(str
, size
, "[%d]:[%s]/%d", p
->prefix
.route_type
,
1340 &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf
,
1346 static const char *prefixevpn_es2str(const struct prefix_evpn
*p
, char *str
,
1349 char buf
[ESI_STR_LEN
];
1351 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d", p
->prefix
.route_type
,
1352 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1353 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
),
1358 static const char *prefixevpn_prefix2str(const struct prefix_evpn
*p
, char *str
,
1362 char buf
[PREFIX2STR_BUFFER
];
1364 family
= is_evpn_prefix_ipaddr_v4(p
)
1367 snprintf(str
, size
, "[%d]:[%u][%s/%d]/%d",
1368 p
->prefix
.route_type
,
1369 p
->prefix
.prefix_addr
.eth_tag
,
1371 &p
->prefix
.prefix_addr
.ip
.ip
.addr
, buf
,
1373 p
->prefix
.prefix_addr
.ip_prefix_length
,
1378 static const char *prefixevpn2str(const struct prefix_evpn
*p
, char *str
,
1381 switch (p
->prefix
.route_type
) {
1383 return prefixevpn_ead2str(p
, str
, size
);
1385 return prefixevpn_macip2str(p
, str
, size
);
1387 return prefixevpn_imet2str(p
, str
, size
);
1389 return prefixevpn_es2str(p
, str
, size
);
1391 return prefixevpn_prefix2str(p
, str
, size
);
1393 snprintf(str
, size
, "Unsupported EVPN prefix");
1399 const char *prefix2str(union prefixconstptr pu
, char *str
, int size
)
1401 const struct prefix
*p
= pu
.p
;
1402 char buf
[PREFIX2STR_BUFFER
];
1403 int byte
, tmp
, a
, b
;
1407 switch (p
->family
) {
1410 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, sizeof(buf
));
1413 byte
= p
->prefixlen
;
1414 if ((tmp
= p
->prefixlen
- 100) >= 0) {
1425 strlcpy(str
, buf
, size
);
1429 snprintf(str
, size
, "%s/%d",
1430 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
)),
1435 prefixevpn2str((const struct prefix_evpn
*)p
, str
, size
);
1439 strlcpy(str
, "FS prefix", size
);
1443 strlcpy(str
, "UNK prefix", size
);
1450 void prefix_mcast_inet4_dump(const char *onfail
, struct in_addr addr
,
1451 char *buf
, int buf_size
)
1453 int save_errno
= errno
;
1455 if (addr
.s_addr
== INADDR_ANY
)
1456 strlcpy(buf
, "*", buf_size
);
1458 if (!inet_ntop(AF_INET
, &addr
, buf
, buf_size
)) {
1460 snprintf(buf
, buf_size
, "%s", onfail
);
1467 const char *prefix_sg2str(const struct prefix_sg
*sg
, char *sg_str
)
1469 char src_str
[INET_ADDRSTRLEN
];
1470 char grp_str
[INET_ADDRSTRLEN
];
1472 prefix_mcast_inet4_dump("<src?>", sg
->src
, src_str
, sizeof(src_str
));
1473 prefix_mcast_inet4_dump("<grp?>", sg
->grp
, grp_str
, sizeof(grp_str
));
1474 snprintf(sg_str
, PREFIX_SG_STR_LEN
, "(%s,%s)", src_str
, grp_str
);
1479 struct prefix
*prefix_new(void)
1483 p
= XCALLOC(MTYPE_PREFIX
, sizeof *p
);
1487 void prefix_free_lists(void *arg
)
1489 struct prefix
*p
= arg
;
1494 /* Free prefix structure. */
1495 void prefix_free(struct prefix
**p
)
1497 XFREE(MTYPE_PREFIX
, *p
);
1501 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1502 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1505 uint32_t destination
;
1507 destination
= ntohl(p
->prefix
.s_addr
);
1509 if (p
->prefixlen
== IPV4_MAX_PREFIXLEN
)
1511 /* do nothing for host routes */
1512 else if (IN_CLASSC(destination
)) {
1515 } else if (IN_CLASSB(destination
)) {
1524 in_addr_t
ipv4_network_addr(in_addr_t hostaddr
, int masklen
)
1526 struct in_addr mask
;
1528 masklen2ip(masklen
, &mask
);
1529 return hostaddr
& mask
.s_addr
;
1532 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1534 struct in_addr mask
;
1536 masklen2ip(masklen
, &mask
);
1537 return (masklen
!= IPV4_MAX_PREFIXLEN
- 1) ?
1539 (hostaddr
| ~mask
.s_addr
)
1541 /* special case for /31 */
1542 (hostaddr
^ ~mask
.s_addr
);
1545 /* Utility function to convert ipv4 netmask to prefixes
1546 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1547 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1548 int netmask_str2prefix_str(const char *net_str
, const char *mask_str
,
1551 struct in_addr network
;
1552 struct in_addr mask
;
1554 uint32_t destination
;
1557 ret
= inet_aton(net_str
, &network
);
1562 ret
= inet_aton(mask_str
, &mask
);
1566 prefixlen
= ip_masklen(mask
);
1568 destination
= ntohl(network
.s_addr
);
1570 if (network
.s_addr
== 0)
1572 else if (IN_CLASSC(destination
))
1574 else if (IN_CLASSB(destination
))
1576 else if (IN_CLASSA(destination
))
1582 sprintf(prefix_str
, "%s/%d", net_str
, prefixlen
);
1587 /* Utility function for making IPv6 address string. */
1588 const char *inet6_ntoa(struct in6_addr addr
)
1590 static char buf
[INET6_ADDRSTRLEN
];
1592 inet_ntop(AF_INET6
, &addr
, buf
, INET6_ADDRSTRLEN
);
1596 /* converts to internal representation of mac address
1597 * returns 1 on success, 0 otherwise
1598 * format accepted: AA:BB:CC:DD:EE:FF
1599 * if mac parameter is null, then check only
1601 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1609 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1612 /* error in incoming str length */
1615 /* valid mac address */
1618 for (i
= 0; i
< 6; ++i
)
1619 mac
->octet
[i
] = a
[i
] & 0xff;
1623 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1630 ptr
= XMALLOC(MTYPE_TMP
, ETHER_ADDR_STRLEN
* sizeof(char));
1632 assert(size
>= ETHER_ADDR_STRLEN
);
1635 snprintf(ptr
, (ETHER_ADDR_STRLEN
), "%02x:%02x:%02x:%02x:%02x:%02x",
1636 (uint8_t)mac
->octet
[0], (uint8_t)mac
->octet
[1],
1637 (uint8_t)mac
->octet
[2], (uint8_t)mac
->octet
[3],
1638 (uint8_t)mac
->octet
[4], (uint8_t)mac
->octet
[5]);
1642 unsigned prefix_hash_key(const void *pp
)
1646 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1650 /* make sure *all* unused bits are zero,
1651 * particularly including alignment /
1652 * padding and unused prefix bytes.
1654 memset(©
, 0, sizeof(copy
));
1655 prefix_copy(©
, (struct prefix
*)pp
);
1656 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1657 copy
.u
.prefix_flowspec
.prefixlen
,
1659 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1660 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1661 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1664 /* make sure *all* unused bits are zero, particularly including
1666 * padding and unused prefix bytes. */
1667 memset(©
, 0, sizeof(copy
));
1668 prefix_copy(©
, (struct prefix
*)pp
);
1670 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),
1674 /* converts to internal representation of esi
1675 * returns 1 on success, 0 otherwise
1676 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1677 * if esi parameter is null, then check only
1679 int str_to_esi(const char *str
, esi_t
*esi
)
1682 unsigned int a
[ESI_BYTES
];
1687 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1688 a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1689 a
+ 4, a
+ 5, a
+ 6, a
+ 7,
1692 /* error in incoming str length */
1699 for (i
= 0; i
< ESI_BYTES
; ++i
)
1700 esi
->val
[i
] = a
[i
] & 0xff;
1704 char *esi_to_str(const esi_t
*esi
, char *buf
, int size
)
1711 ptr
= XMALLOC(MTYPE_TMP
, ESI_STR_LEN
* sizeof(char));
1713 assert(size
>= ESI_STR_LEN
);
1717 snprintf(ptr
, ESI_STR_LEN
,
1718 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1719 esi
->val
[0], esi
->val
[1], esi
->val
[2],
1720 esi
->val
[3], esi
->val
[4], esi
->val
[5],
1721 esi
->val
[6], esi
->val
[7], esi
->val
[8],
1726 printfrr_ext_autoreg_p("I4", printfrr_i4
)
1727 static ssize_t
printfrr_i4(char *buf
, size_t bsz
, const char *fmt
,
1728 int prec
, const void *ptr
)
1730 inet_ntop(AF_INET
, ptr
, buf
, bsz
);
1734 printfrr_ext_autoreg_p("I6", printfrr_i6
)
1735 static ssize_t
printfrr_i6(char *buf
, size_t bsz
, const char *fmt
,
1736 int prec
, const void *ptr
)
1738 inet_ntop(AF_INET6
, ptr
, buf
, bsz
);
1742 printfrr_ext_autoreg_p("FX", printfrr_pfx
)
1743 static ssize_t
printfrr_pfx(char *buf
, size_t bsz
, const char *fmt
,
1744 int prec
, const void *ptr
)
1746 prefix2str(ptr
, buf
, bsz
);
1750 printfrr_ext_autoreg_p("SG4", printfrr_psg
)
1751 static ssize_t
printfrr_psg(char *buf
, size_t bsz
, const char *fmt
,
1752 int prec
, const void *ptr
)
1754 const struct prefix_sg
*sg
= ptr
;
1755 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= bsz
- 1 };
1757 if (sg
->src
.s_addr
== INADDR_ANY
)
1758 bprintfrr(&fb
, "(*,");
1760 bprintfrr(&fb
, "(%pI4,", &sg
->src
);
1762 if (sg
->grp
.s_addr
== INADDR_ANY
)
1763 bprintfrr(&fb
, "*)");
1765 bprintfrr(&fb
, "%pI4)", &sg
->grp
);