]>
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"
31 DEFINE_MTYPE_STATIC(LIB
, PREFIX
, "Prefix")
34 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
35 0xf8, 0xfc, 0xfe, 0xff};
37 static const struct in6_addr maskbytes6
[] = {
38 /* /0 */ {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}},
41 {{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00}}},
44 {{{0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00}}},
47 {{{0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00}}},
50 {{{0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00}}},
53 {{{0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00}}},
56 {{{0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00}}},
59 {{{0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00}}},
62 {{{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00}}},
65 {{{0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00}}},
68 {{{0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00}}},
71 {{{0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00}}},
74 {{{0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00}}},
77 {{{0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00}}},
80 {{{0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00}}},
83 {{{0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0x00}}},
86 {{{0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00}}},
89 {{{0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00}}},
92 {{{0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00}}},
95 {{{0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00, 0x00}}},
98 {{{0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00, 0x00}}},
101 {{{0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102 0x00, 0x00, 0x00, 0x00, 0x00}}},
104 {{{0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x00}}},
107 {{{0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 0x00, 0x00, 0x00, 0x00, 0x00}}},
110 {{{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x00}}},
113 {{{0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00}}},
116 {{{0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 0x00, 0x00, 0x00, 0x00, 0x00}}},
119 {{{0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
120 0x00, 0x00, 0x00, 0x00, 0x00}}},
122 {{{0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 0x00, 0x00, 0x00, 0x00, 0x00}}},
125 {{{0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00}}},
128 {{{0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00}}},
131 {{{0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 0x00, 0x00, 0x00, 0x00, 0x00}}},
134 {{{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00, 0x00}}},
137 {{{0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 0x00, 0x00, 0x00, 0x00, 0x00}}},
140 {{{0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
141 0x00, 0x00, 0x00, 0x00, 0x00}}},
143 {{{0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x00, 0x00}}},
146 {{{0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147 0x00, 0x00, 0x00, 0x00, 0x00}}},
149 {{{0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150 0x00, 0x00, 0x00, 0x00, 0x00}}},
152 {{{0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00}}},
155 {{{0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x00}}},
158 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0x00, 0x00}}},
161 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
162 0x00, 0x00, 0x00, 0x00, 0x00}}},
164 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
165 0x00, 0x00, 0x00, 0x00, 0x00}}},
167 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
168 0x00, 0x00, 0x00, 0x00, 0x00}}},
170 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
171 0x00, 0x00, 0x00, 0x00, 0x00}}},
173 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
174 0x00, 0x00, 0x00, 0x00, 0x00}}},
176 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
177 0x00, 0x00, 0x00, 0x00, 0x00}}},
179 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
180 0x00, 0x00, 0x00, 0x00, 0x00}}},
182 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
183 0x00, 0x00, 0x00, 0x00, 0x00}}},
185 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00,
186 0x00, 0x00, 0x00, 0x00, 0x00}}},
188 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00,
189 0x00, 0x00, 0x00, 0x00, 0x00}}},
191 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00, 0x00}}},
194 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00,
195 0x00, 0x00, 0x00, 0x00, 0x00}}},
197 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00}}},
200 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
201 0x00, 0x00, 0x00, 0x00, 0x00}}},
203 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
204 0x00, 0x00, 0x00, 0x00, 0x00}}},
206 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
207 0x00, 0x00, 0x00, 0x00, 0x00}}},
209 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
210 0x00, 0x00, 0x00, 0x00, 0x00}}},
212 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00,
213 0x00, 0x00, 0x00, 0x00, 0x00}}},
215 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00,
216 0x00, 0x00, 0x00, 0x00, 0x00}}},
218 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00,
219 0x00, 0x00, 0x00, 0x00, 0x00}}},
221 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00,
222 0x00, 0x00, 0x00, 0x00, 0x00}}},
224 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00,
225 0x00, 0x00, 0x00, 0x00, 0x00}}},
227 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x00}}},
230 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
231 0x00, 0x00, 0x00, 0x00, 0x00}}},
233 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00,
234 0x00, 0x00, 0x00, 0x00, 0x00}}},
236 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00}}},
239 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00,
240 0x00, 0x00, 0x00, 0x00, 0x00}}},
242 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00,
243 0x00, 0x00, 0x00, 0x00, 0x00}}},
245 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00,
246 0x00, 0x00, 0x00, 0x00, 0x00}}},
248 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00,
249 0x00, 0x00, 0x00, 0x00, 0x00}}},
251 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00,
252 0x00, 0x00, 0x00, 0x00, 0x00}}},
254 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
255 0x00, 0x00, 0x00, 0x00, 0x00}}},
257 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
258 0x00, 0x00, 0x00, 0x00, 0x00}}},
260 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00,
261 0x00, 0x00, 0x00, 0x00, 0x00}}},
263 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00,
264 0x00, 0x00, 0x00, 0x00, 0x00}}},
266 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00,
267 0x00, 0x00, 0x00, 0x00, 0x00}}},
269 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00,
270 0x00, 0x00, 0x00, 0x00, 0x00}}},
272 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00,
273 0x00, 0x00, 0x00, 0x00, 0x00}}},
275 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00,
276 0x00, 0x00, 0x00, 0x00, 0x00}}},
278 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
279 0x00, 0x00, 0x00, 0x00, 0x00}}},
281 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
282 0x00, 0x00, 0x00, 0x00, 0x00}}},
284 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
285 0x00, 0x00, 0x00, 0x00, 0x00}}},
287 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
288 0x00, 0x00, 0x00, 0x00, 0x00}}},
290 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
291 0x00, 0x00, 0x00, 0x00, 0x00}}},
293 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8,
294 0x00, 0x00, 0x00, 0x00, 0x00}}},
296 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
297 0x00, 0x00, 0x00, 0x00, 0x00}}},
299 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
300 0x00, 0x00, 0x00, 0x00, 0x00}}},
302 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
303 0x00, 0x00, 0x00, 0x00, 0x00}}},
305 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
306 0x80, 0x00, 0x00, 0x00, 0x00}}},
308 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
309 0xc0, 0x00, 0x00, 0x00, 0x00}}},
311 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
312 0xe0, 0x00, 0x00, 0x00, 0x00}}},
314 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
315 0xf0, 0x00, 0x00, 0x00, 0x00}}},
317 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
318 0xf8, 0x00, 0x00, 0x00, 0x00}}},
320 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
321 0xfc, 0x00, 0x00, 0x00, 0x00}}},
323 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
324 0xfe, 0x00, 0x00, 0x00, 0x00}}},
326 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
327 0xff, 0x00, 0x00, 0x00, 0x00}}},
329 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
330 0xff, 0x80, 0x00, 0x00, 0x00}}},
332 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
333 0xff, 0xc0, 0x00, 0x00, 0x00}}},
335 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
336 0xff, 0xe0, 0x00, 0x00, 0x00}}},
338 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
339 0xff, 0xf0, 0x00, 0x00, 0x00}}},
341 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
342 0xff, 0xf8, 0x00, 0x00, 0x00}}},
344 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
345 0xff, 0xfc, 0x00, 0x00, 0x00}}},
347 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
348 0xff, 0xfe, 0x00, 0x00, 0x00}}},
350 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
351 0xff, 0xff, 0x00, 0x00, 0x00}}},
353 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
354 0xff, 0xff, 0x80, 0x00, 0x00}}},
356 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
357 0xff, 0xff, 0xc0, 0x00, 0x00}}},
359 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
360 0xff, 0xff, 0xe0, 0x00, 0x00}}},
362 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
363 0xff, 0xff, 0xf0, 0x00, 0x00}}},
365 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
366 0xff, 0xff, 0xf8, 0x00, 0x00}}},
368 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
369 0xff, 0xff, 0xfc, 0x00, 0x00}}},
371 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
372 0xff, 0xff, 0xfe, 0x00, 0x00}}},
374 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
375 0xff, 0xff, 0xff, 0x00, 0x00}}},
377 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
378 0xff, 0xff, 0xff, 0x80, 0x00}}},
380 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
381 0xff, 0xff, 0xff, 0xc0, 0x00}}},
383 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
384 0xff, 0xff, 0xff, 0xe0, 0x00}}},
386 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
387 0xff, 0xff, 0xff, 0xf0, 0x00}}},
389 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
390 0xff, 0xff, 0xff, 0xf8, 0x00}}},
392 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
393 0xff, 0xff, 0xff, 0xfc, 0x00}}},
395 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
396 0xff, 0xff, 0xff, 0xfe, 0x00}}},
398 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
399 0xff, 0xff, 0xff, 0xff, 0x00}}},
401 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
402 0xff, 0xff, 0xff, 0xff, 0x80}}},
404 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
405 0xff, 0xff, 0xff, 0xff, 0xc0}}},
407 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
408 0xff, 0xff, 0xff, 0xff, 0xe0}}},
410 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
411 0xff, 0xff, 0xff, 0xff, 0xf0}}},
413 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
414 0xff, 0xff, 0xff, 0xff, 0xf8}}},
416 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
417 0xff, 0xff, 0xff, 0xff, 0xfc}}},
419 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
420 0xff, 0xff, 0xff, 0xff, 0xfe}}},
422 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
423 0xff, 0xff, 0xff, 0xff, 0xff}}}};
425 /* Number of bits in prefix type. */
430 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
432 int is_zero_mac(struct ethaddr
*mac
)
436 for (i
= 0; i
< ETH_ALEN
; i
++) {
444 unsigned int prefix_bit(const uint8_t *prefix
, const uint8_t prefixlen
)
446 unsigned int offset
= prefixlen
/ 8;
447 unsigned int shift
= 7 - (prefixlen
% 8);
449 return (prefix
[offset
] >> shift
) & 1;
452 unsigned int prefix6_bit(const struct in6_addr
*prefix
, const uint8_t prefixlen
)
454 return prefix_bit((const uint8_t *)&prefix
->s6_addr
, prefixlen
);
457 int str2family(const char *string
)
459 if (!strcmp("ipv4", string
))
461 else if (!strcmp("ipv6", string
))
463 else if (!strcmp("ethernet", string
))
465 else if (!strcmp("evpn", string
))
470 /* Address Famiy Identifier to Address Family converter. */
471 int afi2family(afi_t afi
)
475 else if (afi
== AFI_IP6
)
477 else if (afi
== AFI_L2VPN
)
479 /* NOTE: EVPN code should NOT use this interface. */
483 afi_t
family2afi(int family
)
485 if (family
== AF_INET
)
487 else if (family
== AF_INET6
)
489 else if (family
== AF_ETHERNET
|| family
== AF_EVPN
)
494 const char *afi2str(afi_t afi
)
511 const char *safi2str(safi_t safi
)
524 case SAFI_LABELED_UNICAST
:
525 return "labeled-unicast";
533 /* If n includes p prefix then return 1 else return 0. */
534 int prefix_match(const struct prefix
*n
, const struct prefix
*p
)
538 const uint8_t *np
, *pp
;
540 /* If n's prefix is longer than p's one return 0. */
541 if (n
->prefixlen
> p
->prefixlen
)
544 if (n
->family
== AF_FLOWSPEC
) {
545 /* prefixlen is unused. look at fs prefix len */
546 if (n
->u
.prefix_flowspec
.prefixlen
>
547 p
->u
.prefix_flowspec
.prefixlen
)
550 /* Set both prefix's head pointer. */
551 np
= (const uint8_t *)&n
->u
.prefix_flowspec
.ptr
;
552 pp
= (const uint8_t *)&p
->u
.prefix_flowspec
.ptr
;
554 offset
= n
->u
.prefix_flowspec
.prefixlen
;
557 if (np
[offset
] != pp
[offset
])
562 /* Set both prefix's head pointer. */
563 np
= (const uint8_t *)&n
->u
.prefix
;
564 pp
= (const uint8_t *)&p
->u
.prefix
;
566 offset
= n
->prefixlen
/ PNBBY
;
567 shift
= n
->prefixlen
% PNBBY
;
570 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
574 if (np
[offset
] != pp
[offset
])
579 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
580 int prefix_match_network_statement(const struct prefix
*n
,
581 const struct prefix
*p
)
585 const uint8_t *np
, *pp
;
587 /* Set both prefix's head pointer. */
588 np
= (const uint8_t *)&n
->u
.prefix
;
589 pp
= (const uint8_t *)&p
->u
.prefix
;
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 void prefix_copy(struct prefix
*dest
, const struct prefix
*src
)
606 dest
->family
= src
->family
;
607 dest
->prefixlen
= src
->prefixlen
;
609 if (src
->family
== AF_INET
)
610 dest
->u
.prefix4
= src
->u
.prefix4
;
611 else if (src
->family
== AF_INET6
)
612 dest
->u
.prefix6
= src
->u
.prefix6
;
613 else if (src
->family
== AF_ETHERNET
) {
614 memcpy(&dest
->u
.prefix_eth
, &src
->u
.prefix_eth
,
615 sizeof(struct ethaddr
));
616 } else if (src
->family
== AF_EVPN
) {
617 memcpy(&dest
->u
.prefix_evpn
, &src
->u
.prefix_evpn
,
618 sizeof(struct evpn_addr
));
619 } else if (src
->family
== AF_UNSPEC
) {
620 dest
->u
.lp
.id
= src
->u
.lp
.id
;
621 dest
->u
.lp
.adv_router
= src
->u
.lp
.adv_router
;
622 } else if (src
->family
== AF_FLOWSPEC
) {
626 len
= src
->u
.prefix_flowspec
.prefixlen
;
627 dest
->u
.prefix_flowspec
.prefixlen
=
628 src
->u
.prefix_flowspec
.prefixlen
;
629 dest
->family
= src
->family
;
630 temp
= XCALLOC(MTYPE_PREFIX_FLOWSPEC
, len
);
631 dest
->u
.prefix_flowspec
.ptr
= (uintptr_t)temp
;
632 memcpy((void *)dest
->u
.prefix_flowspec
.ptr
,
633 (void *)src
->u
.prefix_flowspec
.ptr
, len
);
635 zlog_err("prefix_copy(): Unknown address family %d",
642 * Return 1 if the address/netmask contained in the prefix structure
643 * is the same, and else return 0. For this routine, 'same' requires
644 * that not only the prefix length and the network part be the same,
645 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
646 * the same. Note that this routine has the same return value sense
647 * as '==' (which is different from prefix_cmp).
649 int prefix_same(const struct prefix
*p1
, const struct prefix
*p2
)
651 if ((p1
&& !p2
) || (!p1
&& p2
))
657 if (p1
->family
== p2
->family
&& p1
->prefixlen
== p2
->prefixlen
) {
658 if (p1
->family
== AF_INET
)
659 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
661 if (p1
->family
== AF_INET6
)
662 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
.s6_addr
,
663 &p2
->u
.prefix6
.s6_addr
))
665 if (p1
->family
== AF_ETHERNET
)
666 if (!memcmp(&p1
->u
.prefix_eth
, &p2
->u
.prefix_eth
,
667 sizeof(struct ethaddr
)))
669 if (p1
->family
== AF_EVPN
)
670 if (!memcmp(&p1
->u
.prefix_evpn
, &p2
->u
.prefix_evpn
,
671 sizeof(struct evpn_addr
)))
673 if (p1
->family
== AF_FLOWSPEC
) {
674 if (p1
->u
.prefix_flowspec
.prefixlen
!=
675 p2
->u
.prefix_flowspec
.prefixlen
)
677 if (!memcmp(&p1
->u
.prefix_flowspec
.ptr
,
678 &p2
->u
.prefix_flowspec
.ptr
,
679 p2
->u
.prefix_flowspec
.prefixlen
))
687 * Return 0 if the network prefixes represented by the struct prefix
688 * arguments are the same prefix, and 1 otherwise. Network prefixes
689 * are considered the same if the prefix lengths are equal and the
690 * network parts are the same. Host bits (which are considered masked
691 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
692 * 10.0.0.2/8 are considered equivalent by this routine. Note that
693 * this routine has the same return sense as strcmp (which is different
696 int prefix_cmp(const struct prefix
*p1
, const struct prefix
*p2
)
701 /* Set both prefix's head pointer. */
705 if (p1
->family
!= p2
->family
)
707 if (p1
->family
== AF_FLOWSPEC
) {
708 pp1
= (const uint8_t *)p1
->u
.prefix_flowspec
.ptr
;
709 pp2
= (const uint8_t *)p2
->u
.prefix_flowspec
.ptr
;
711 if (p1
->u
.prefix_flowspec
.prefixlen
!=
712 p2
->u
.prefix_flowspec
.prefixlen
)
715 offset
= p1
->u
.prefix_flowspec
.prefixlen
;
717 if (pp1
[offset
] != pp2
[offset
])
721 pp1
= (const uint8_t *)&p1
->u
.prefix
;
722 pp2
= (const uint8_t *)&p2
->u
.prefix
;
724 if (p1
->prefixlen
!= p2
->prefixlen
)
726 offset
= p1
->prefixlen
/ PNBBY
;
727 shift
= p1
->prefixlen
% PNBBY
;
730 if (maskbit
[shift
] & (pp1
[offset
] ^ pp2
[offset
]))
734 if (pp1
[offset
] != pp2
[offset
])
741 * Count the number of common bits in 2 prefixes. The prefix length is
742 * ignored for this function; the whole prefix is compared. If the prefix
743 * address families don't match, return -1; otherwise the return value is
744 * in range 0 ... maximum prefix length for the address family.
746 int prefix_common_bits(const struct prefix
*p1
, const struct prefix
*p2
)
752 /* Set both prefix's head pointer. */
753 const uint8_t *pp1
= (const uint8_t *)&p1
->u
.prefix
;
754 const uint8_t *pp2
= (const uint8_t *)&p2
->u
.prefix
;
756 if (p1
->family
== AF_INET
)
757 length
= IPV4_MAX_BYTELEN
;
758 if (p1
->family
== AF_INET6
)
759 length
= IPV6_MAX_BYTELEN
;
760 if (p1
->family
== AF_ETHERNET
)
762 if (p1
->family
== AF_EVPN
)
763 length
= 8 * sizeof(struct evpn_addr
);
765 if (p1
->family
!= p2
->family
|| !length
)
768 for (pos
= 0; pos
< length
; pos
++)
769 if (pp1
[pos
] != pp2
[pos
])
774 xor = pp1
[pos
] ^ pp2
[pos
];
775 for (bit
= 0; bit
< 8; bit
++)
776 if (xor&(1 << (7 - bit
)))
779 return pos
* 8 + bit
;
782 /* Return prefix family type string. */
783 const char *prefix_family_str(const struct prefix
*p
)
785 if (p
->family
== AF_INET
)
787 if (p
->family
== AF_INET6
)
789 if (p
->family
== AF_ETHERNET
)
791 if (p
->family
== AF_EVPN
)
796 /* Allocate new prefix_ipv4 structure. */
797 struct prefix_ipv4
*prefix_ipv4_new()
799 struct prefix_ipv4
*p
;
801 /* Call prefix_new to allocate a full-size struct prefix to avoid
803 where the struct prefix_ipv4 is cast to struct prefix and unallocated
804 bytes were being referenced (e.g. in structure assignments). */
805 p
= (struct prefix_ipv4
*)prefix_new();
810 /* Free prefix_ipv4 structure. */
811 void prefix_ipv4_free(struct prefix_ipv4
*p
)
813 prefix_free((struct prefix
*)p
);
816 /* When string format is invalid return 0. */
817 int str2prefix_ipv4(const char *str
, struct prefix_ipv4
*p
)
824 /* Find slash inside string. */
825 pnt
= strchr(str
, '/');
827 /* String doesn't contail slash. */
829 /* Convert string to prefix. */
830 ret
= inet_aton(str
, &p
->prefix
);
834 /* If address doesn't contain slash we assume it host address.
837 p
->prefixlen
= IPV4_MAX_BITLEN
;
841 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
842 strncpy(cp
, str
, pnt
- str
);
843 *(cp
+ (pnt
- str
)) = '\0';
844 ret
= inet_aton(cp
, &p
->prefix
);
845 XFREE(MTYPE_TMP
, cp
);
847 /* Get prefix length. */
848 plen
= (uint8_t)atoi(++pnt
);
849 if (plen
> IPV4_MAX_PREFIXLEN
)
859 /* When string format is invalid return 0. */
860 int str2prefix_eth(const char *str
, struct prefix_eth
*p
)
866 const char *str_addr
= str
;
871 if (!strcmp(str
, "any")) {
872 memset(p
, 0, sizeof(*p
));
873 p
->family
= AF_ETHERNET
;
877 /* Find slash inside string. */
878 pnt
= strchr(str
, '/');
881 /* Get prefix length. */
882 plen
= (uint8_t)atoi(++pnt
);
888 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
889 strncpy(cp
, str
, pnt
- str
);
890 *(cp
+ (pnt
- str
)) = '\0';
896 /* Convert string to prefix. */
897 if (sscanf(str_addr
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2,
903 for (i
= 0; i
< 6; ++i
) {
904 p
->eth_addr
.octet
[i
] = a
[i
] & 0xff;
907 p
->family
= AF_ETHERNET
;
910 * special case to allow old configurations to work
911 * Since all zero's is implicitly meant to allow
912 * a comparison to zero, let's assume
914 if (!slash
&& is_zero_mac(&(p
->eth_addr
)))
921 XFREE(MTYPE_TMP
, cp
);
926 /* Convert masklen into IP address's netmask (network byte order). */
927 void masklen2ip(const int masklen
, struct in_addr
*netmask
)
929 assert(masklen
>= 0 && masklen
<= IPV4_MAX_BITLEN
);
931 /* left shift is only defined for less than the size of the type.
932 * we unconditionally use long long in case the target platform
933 * has defined behaviour for << 32 (or has a 64-bit left shift) */
935 if (sizeof(unsigned long long) > 4)
936 netmask
->s_addr
= htonl(0xffffffffULL
<< (32 - masklen
));
939 htonl(masklen
? 0xffffffffU
<< (32 - masklen
) : 0);
942 /* Convert IP address's netmask into integer. We assume netmask is
943 sequential one. Argument netmask should be network byte order. */
944 uint8_t ip_masklen(struct in_addr netmask
)
946 uint32_t tmp
= ~ntohl(netmask
.s_addr
);
948 /* clz: count leading zeroes. sadly, the behaviour of this
950 * is undefined for a 0 argument, even though most CPUs give 32
952 return __builtin_clz(tmp
);
957 /* Apply mask to IPv4 prefix (network byte order). */
958 void apply_mask_ipv4(struct prefix_ipv4
*p
)
961 masklen2ip(p
->prefixlen
, &mask
);
962 p
->prefix
.s_addr
&= mask
.s_addr
;
965 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
966 int prefix_ipv4_any(const struct prefix_ipv4
*p
)
968 return (p
->prefix
.s_addr
== 0 && p
->prefixlen
== 0);
971 /* Allocate a new ip version 6 route */
972 struct prefix_ipv6
*prefix_ipv6_new(void)
974 struct prefix_ipv6
*p
;
976 /* Allocate a full-size struct prefix to avoid problems with structure
978 p
= (struct prefix_ipv6
*)prefix_new();
979 p
->family
= AF_INET6
;
983 /* Free prefix for IPv6. */
984 void prefix_ipv6_free(struct prefix_ipv6
*p
)
986 prefix_free((struct prefix
*)p
);
989 /* If given string is valid return pin6 else return NULL */
990 int str2prefix_ipv6(const char *str
, struct prefix_ipv6
*p
)
996 pnt
= strchr(str
, '/');
998 /* If string doesn't contain `/' treat it as host route. */
1000 ret
= inet_pton(AF_INET6
, str
, &p
->prefix
);
1003 p
->prefixlen
= IPV6_MAX_BITLEN
;
1007 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
1008 strncpy(cp
, str
, pnt
- str
);
1009 *(cp
+ (pnt
- str
)) = '\0';
1010 ret
= inet_pton(AF_INET6
, cp
, &p
->prefix
);
1011 XFREE(MTYPE_TMP
, cp
);
1014 plen
= (uint8_t)atoi(++pnt
);
1015 if (plen
> IPV6_MAX_BITLEN
)
1017 p
->prefixlen
= plen
;
1019 p
->family
= AF_INET6
;
1024 /* Convert struct in6_addr netmask into integer.
1025 * FIXME return uint8_t as ip_maskleni() does. */
1026 int ip6_masklen(struct in6_addr netmask
)
1032 pnt
= (unsigned char *)&netmask
;
1034 while ((*pnt
== 0xff) && len
< IPV6_MAX_BITLEN
) {
1039 if (len
< IPV6_MAX_BITLEN
) {
1049 void masklen2ip6(const int masklen
, struct in6_addr
*netmask
)
1051 assert(masklen
>= 0 && masklen
<= IPV6_MAX_BITLEN
);
1052 memcpy(netmask
, maskbytes6
+ masklen
, sizeof(struct in6_addr
));
1055 void apply_mask_ipv6(struct prefix_ipv6
*p
)
1061 index
= p
->prefixlen
/ 8;
1064 pnt
= (uint8_t *)&p
->prefix
;
1065 offset
= p
->prefixlen
% 8;
1067 pnt
[index
] &= maskbit
[offset
];
1075 void apply_mask(struct prefix
*p
)
1077 switch (p
->family
) {
1079 apply_mask_ipv4((struct prefix_ipv4
*)p
);
1082 apply_mask_ipv6((struct prefix_ipv6
*)p
);
1090 /* Utility function of convert between struct prefix <=> union sockunion.
1091 * FIXME This function isn't used anywhere. */
1092 struct prefix
*sockunion2prefix(const union sockunion
*dest
,
1093 const union sockunion
*mask
)
1095 if (dest
->sa
.sa_family
== AF_INET
) {
1096 struct prefix_ipv4
*p
;
1098 p
= prefix_ipv4_new();
1099 p
->family
= AF_INET
;
1100 p
->prefix
= dest
->sin
.sin_addr
;
1101 p
->prefixlen
= ip_masklen(mask
->sin
.sin_addr
);
1102 return (struct prefix
*)p
;
1104 if (dest
->sa
.sa_family
== AF_INET6
) {
1105 struct prefix_ipv6
*p
;
1107 p
= prefix_ipv6_new();
1108 p
->family
= AF_INET6
;
1109 p
->prefixlen
= ip6_masklen(mask
->sin6
.sin6_addr
);
1110 memcpy(&p
->prefix
, &dest
->sin6
.sin6_addr
,
1111 sizeof(struct in6_addr
));
1112 return (struct prefix
*)p
;
1117 /* Utility function of convert between struct prefix <=> union sockunion. */
1118 struct prefix
*sockunion2hostprefix(const union sockunion
*su
,
1119 struct prefix
*prefix
)
1121 if (su
->sa
.sa_family
== AF_INET
) {
1122 struct prefix_ipv4
*p
;
1124 p
= prefix
? (struct prefix_ipv4
*)prefix
: prefix_ipv4_new();
1125 p
->family
= AF_INET
;
1126 p
->prefix
= su
->sin
.sin_addr
;
1127 p
->prefixlen
= IPV4_MAX_BITLEN
;
1128 return (struct prefix
*)p
;
1130 if (su
->sa
.sa_family
== AF_INET6
) {
1131 struct prefix_ipv6
*p
;
1133 p
= prefix
? (struct prefix_ipv6
*)prefix
: prefix_ipv6_new();
1134 p
->family
= AF_INET6
;
1135 p
->prefixlen
= IPV6_MAX_BITLEN
;
1136 memcpy(&p
->prefix
, &su
->sin6
.sin6_addr
,
1137 sizeof(struct in6_addr
));
1138 return (struct prefix
*)p
;
1143 void prefix2sockunion(const struct prefix
*p
, union sockunion
*su
)
1145 memset(su
, 0, sizeof(*su
));
1147 su
->sa
.sa_family
= p
->family
;
1148 if (p
->family
== AF_INET
)
1149 su
->sin
.sin_addr
= p
->u
.prefix4
;
1150 if (p
->family
== AF_INET6
)
1151 memcpy(&su
->sin6
.sin6_addr
, &p
->u
.prefix6
,
1152 sizeof(struct in6_addr
));
1155 int prefix_blen(const struct prefix
*p
)
1157 switch (p
->family
) {
1159 return IPV4_MAX_BYTELEN
;
1162 return IPV6_MAX_BYTELEN
;
1171 /* Generic function for conversion string to struct prefix. */
1172 int str2prefix(const char *str
, struct prefix
*p
)
1176 /* First we try to convert string to struct prefix_ipv4. */
1177 ret
= str2prefix_ipv4(str
, (struct prefix_ipv4
*)p
);
1181 /* Next we try to convert string to struct prefix_ipv6. */
1182 ret
= str2prefix_ipv6(str
, (struct prefix_ipv6
*)p
);
1186 /* Next we try to convert string to struct prefix_eth. */
1187 ret
= str2prefix_eth(str
, (struct prefix_eth
*)p
);
1194 static const char *prefixevpn2str(const struct prefix
*p
, char *str
, int size
)
1197 char buf
[PREFIX2STR_BUFFER
];
1198 char buf2
[ETHER_ADDR_STRLEN
];
1200 if (p
->u
.prefix_evpn
.route_type
== 2) {
1201 if (IS_EVPN_PREFIX_IPADDR_NONE((struct prefix_evpn
*)p
))
1202 snprintf(str
, size
, "[%d]:[%s]/%d",
1203 p
->u
.prefix_evpn
.route_type
,
1204 prefix_mac2str(&p
->u
.prefix_evpn
.mac
, buf2
,
1208 family
= IS_EVPN_PREFIX_IPADDR_V4(
1209 (struct prefix_evpn
*)p
)
1212 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d",
1213 p
->u
.prefix_evpn
.route_type
,
1214 prefix_mac2str(&p
->u
.prefix_evpn
.mac
, buf2
,
1216 inet_ntop(family
, &p
->u
.prefix_evpn
.ip
.ip
.addr
,
1217 buf
, PREFIX2STR_BUFFER
),
1220 } else if (p
->u
.prefix_evpn
.route_type
== 3) {
1221 family
= IS_EVPN_PREFIX_IPADDR_V4((struct prefix_evpn
*)p
)
1224 snprintf(str
, size
, "[%d]:[%s]/%d", p
->u
.prefix_evpn
.route_type
,
1225 inet_ntop(family
, &p
->u
.prefix_evpn
.ip
.ip
.addr
, buf
,
1228 } else if (p
->u
.prefix_evpn
.route_type
== 5) {
1229 family
= IS_EVPN_PREFIX_IPADDR_V4((struct prefix_evpn
*)p
)
1232 snprintf(str
, size
, "[%d]:[%u][%s/%d]/%d",
1233 p
->u
.prefix_evpn
.route_type
, p
->u
.prefix_evpn
.eth_tag
,
1234 inet_ntop(family
, &p
->u
.prefix_evpn
.ip
.ip
.addr
, buf
,
1236 p
->u
.prefix_evpn
.ip_prefix_length
, p
->prefixlen
);
1238 sprintf(str
, "Unsupported EVPN route type %d",
1239 p
->u
.prefix_evpn
.route_type
);
1245 const char *prefix2str(union prefixconstptr pu
, char *str
, int size
)
1247 const struct prefix
*p
= pu
.p
;
1248 char buf
[PREFIX2STR_BUFFER
];
1250 switch (p
->family
) {
1253 snprintf(str
, size
, "%s/%d", inet_ntop(p
->family
, &p
->u
.prefix
,
1254 buf
, PREFIX2STR_BUFFER
),
1259 snprintf(str
, size
, "%s/%d",
1260 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
)),
1265 prefixevpn2str(p
, str
, size
);
1269 sprintf(str
, "FS prefix");
1273 sprintf(str
, "UNK prefix");
1280 struct prefix
*prefix_new()
1284 p
= XCALLOC(MTYPE_PREFIX
, sizeof *p
);
1288 /* Free prefix structure. */
1289 void prefix_free(struct prefix
*p
)
1291 XFREE(MTYPE_PREFIX
, p
);
1294 /* Utility function. Check the string only contains digit
1296 * FIXME str.[c|h] would be better place for this function. */
1297 int all_digit(const char *str
)
1299 for (; *str
!= '\0'; str
++)
1300 if (!isdigit((int)*str
))
1305 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1306 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1309 uint32_t destination
;
1311 destination
= ntohl(p
->prefix
.s_addr
);
1313 if (p
->prefixlen
== IPV4_MAX_PREFIXLEN
)
1315 /* do nothing for host routes */
1316 else if (IN_CLASSC(destination
)) {
1319 } else if (IN_CLASSB(destination
)) {
1328 in_addr_t
ipv4_network_addr(in_addr_t hostaddr
, int masklen
)
1330 struct in_addr mask
;
1332 masklen2ip(masklen
, &mask
);
1333 return hostaddr
& mask
.s_addr
;
1336 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1338 struct in_addr mask
;
1340 masklen2ip(masklen
, &mask
);
1341 return (masklen
!= IPV4_MAX_PREFIXLEN
- 1) ?
1343 (hostaddr
| ~mask
.s_addr
)
1345 /* special case for /31 */
1346 (hostaddr
^ ~mask
.s_addr
);
1349 /* Utility function to convert ipv4 netmask to prefixes
1350 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1351 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1352 int netmask_str2prefix_str(const char *net_str
, const char *mask_str
,
1355 struct in_addr network
;
1356 struct in_addr mask
;
1358 uint32_t destination
;
1361 ret
= inet_aton(net_str
, &network
);
1366 ret
= inet_aton(mask_str
, &mask
);
1370 prefixlen
= ip_masklen(mask
);
1372 destination
= ntohl(network
.s_addr
);
1374 if (network
.s_addr
== 0)
1376 else if (IN_CLASSC(destination
))
1378 else if (IN_CLASSB(destination
))
1380 else if (IN_CLASSA(destination
))
1386 sprintf(prefix_str
, "%s/%d", net_str
, prefixlen
);
1391 /* Utility function for making IPv6 address string. */
1392 const char *inet6_ntoa(struct in6_addr addr
)
1394 static char buf
[INET6_ADDRSTRLEN
];
1396 inet_ntop(AF_INET6
, &addr
, buf
, INET6_ADDRSTRLEN
);
1400 /* converts to internal representation of mac address
1401 * returns 1 on success, 0 otherwise
1402 * format accepted: AA:BB:CC:DD:EE:FF
1403 * if mac parameter is null, then check only
1405 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1413 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1416 /* error in incoming str length */
1419 /* valid mac address */
1422 for (i
= 0; i
< 6; ++i
)
1423 mac
->octet
[i
] = a
[i
] & 0xff;
1427 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1434 ptr
= (char *)XMALLOC(MTYPE_TMP
,
1435 ETHER_ADDR_STRLEN
* sizeof(char));
1437 assert(size
>= ETHER_ADDR_STRLEN
);
1440 snprintf(ptr
, (ETHER_ADDR_STRLEN
), "%02x:%02x:%02x:%02x:%02x:%02x",
1441 (uint8_t)mac
->octet
[0], (uint8_t)mac
->octet
[1],
1442 (uint8_t)mac
->octet
[2], (uint8_t)mac
->octet
[3],
1443 (uint8_t)mac
->octet
[4], (uint8_t)mac
->octet
[5]);
1447 unsigned prefix_hash_key(void *pp
)
1451 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1455 /* make sure *all* unused bits are zero,
1456 * particularly including alignment /
1457 * padding and unused prefix bytes.
1459 memset(©
, 0, sizeof(copy
));
1460 prefix_copy(©
, (struct prefix
*)pp
);
1461 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1462 copy
.u
.prefix_flowspec
.prefixlen
,
1464 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1465 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1466 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1469 /* make sure *all* unused bits are zero, particularly including
1471 * padding and unused prefix bytes. */
1472 memset(©
, 0, sizeof(copy
));
1473 prefix_copy(©
, (struct prefix
*)pp
);
1475 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),