]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
0203301562f5df3382b8233423df420c1c0f891d
[mirror_frr.git] / lib / prefix.c
1 /*
2 * Prefix related functions.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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
10 * later version.
11 *
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.
16 *
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
20 */
21
22 #include <zebra.h>
23
24 #include "prefix.h"
25 #include "vty.h"
26 #include "sockunion.h"
27 #include "memory.h"
28 #include "log.h"
29 #include "jhash.h"
30 #include "lib_errors.h"
31
32 DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix")
33
34 /* Maskbit. */
35 static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
36 0xf8, 0xfc, 0xfe, 0xff};
37
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}}},
41 /* /1 */
42 {{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00}}},
44 /* /2 */
45 {{{0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00}}},
47 /* /3 */
48 {{{0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00}}},
50 /* /4 */
51 {{{0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00}}},
53 /* /5 */
54 {{{0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00}}},
56 /* /6 */
57 {{{0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00}}},
59 /* /7 */
60 {{{0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00}}},
62 /* /8 */
63 {{{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00}}},
65 /* /9 */
66 {{{0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00}}},
68 /* /10 */
69 {{{0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00}}},
71 /* /11 */
72 {{{0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00}}},
74 /* /12 */
75 {{{0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00}}},
77 /* /13 */
78 {{{0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00}}},
80 /* /14 */
81 {{{0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x00}}},
83 /* /15 */
84 {{{0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x00, 0x00}}},
86 /* /16 */
87 {{{0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00, 0x00}}},
89 /* /17 */
90 {{{0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00}}},
92 /* /18 */
93 {{{0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94 0x00, 0x00, 0x00, 0x00, 0x00}}},
95 /* /19 */
96 {{{0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x00, 0x00}}},
98 /* /20 */
99 {{{0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0x00, 0x00, 0x00, 0x00, 0x00}}},
101 /* /21 */
102 {{{0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00, 0x00}}},
104 /* /22 */
105 {{{0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
106 0x00, 0x00, 0x00, 0x00, 0x00}}},
107 /* /23 */
108 {{{0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109 0x00, 0x00, 0x00, 0x00, 0x00}}},
110 /* /24 */
111 {{{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112 0x00, 0x00, 0x00, 0x00, 0x00}}},
113 /* /25 */
114 {{{0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00}}},
116 /* /26 */
117 {{{0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 0x00, 0x00, 0x00, 0x00, 0x00}}},
119 /* /27 */
120 {{{0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121 0x00, 0x00, 0x00, 0x00, 0x00}}},
122 /* /28 */
123 {{{0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00, 0x00}}},
125 /* /29 */
126 {{{0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00, 0x00}}},
128 /* /30 */
129 {{{0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 0x00, 0x00, 0x00, 0x00, 0x00}}},
131 /* /31 */
132 {{{0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133 0x00, 0x00, 0x00, 0x00, 0x00}}},
134 /* /32 */
135 {{{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00, 0x00}}},
137 /* /33 */
138 {{{0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139 0x00, 0x00, 0x00, 0x00, 0x00}}},
140 /* /34 */
141 {{{0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142 0x00, 0x00, 0x00, 0x00, 0x00}}},
143 /* /35 */
144 {{{0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 0x00, 0x00, 0x00, 0x00, 0x00}}},
146 /* /36 */
147 {{{0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148 0x00, 0x00, 0x00, 0x00, 0x00}}},
149 /* /37 */
150 {{{0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151 0x00, 0x00, 0x00, 0x00, 0x00}}},
152 /* /38 */
153 {{{0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154 0x00, 0x00, 0x00, 0x00, 0x00}}},
155 /* /39 */
156 {{{0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x00}}},
158 /* /40 */
159 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160 0x00, 0x00, 0x00, 0x00, 0x00}}},
161 /* /41 */
162 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
163 0x00, 0x00, 0x00, 0x00, 0x00}}},
164 /* /42 */
165 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
166 0x00, 0x00, 0x00, 0x00, 0x00}}},
167 /* /43 */
168 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
169 0x00, 0x00, 0x00, 0x00, 0x00}}},
170 /* /44 */
171 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
172 0x00, 0x00, 0x00, 0x00, 0x00}}},
173 /* /45 */
174 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
175 0x00, 0x00, 0x00, 0x00, 0x00}}},
176 /* /46 */
177 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
178 0x00, 0x00, 0x00, 0x00, 0x00}}},
179 /* /47 */
180 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
181 0x00, 0x00, 0x00, 0x00, 0x00}}},
182 /* /48 */
183 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
184 0x00, 0x00, 0x00, 0x00, 0x00}}},
185 /* /49 */
186 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00,
187 0x00, 0x00, 0x00, 0x00, 0x00}}},
188 /* /50 */
189 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00}}},
191 /* /51 */
192 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
193 0x00, 0x00, 0x00, 0x00, 0x00}}},
194 /* /52 */
195 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00,
196 0x00, 0x00, 0x00, 0x00, 0x00}}},
197 /* /53 */
198 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
199 0x00, 0x00, 0x00, 0x00, 0x00}}},
200 /* /54 */
201 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
202 0x00, 0x00, 0x00, 0x00, 0x00}}},
203 /* /55 */
204 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
205 0x00, 0x00, 0x00, 0x00, 0x00}}},
206 /* /56 */
207 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
208 0x00, 0x00, 0x00, 0x00, 0x00}}},
209 /* /57 */
210 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
211 0x00, 0x00, 0x00, 0x00, 0x00}}},
212 /* /58 */
213 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0x00, 0x00, 0x00}}},
215 /* /59 */
216 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00,
217 0x00, 0x00, 0x00, 0x00, 0x00}}},
218 /* /60 */
219 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00,
220 0x00, 0x00, 0x00, 0x00, 0x00}}},
221 /* /61 */
222 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00,
223 0x00, 0x00, 0x00, 0x00, 0x00}}},
224 /* /62 */
225 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00,
226 0x00, 0x00, 0x00, 0x00, 0x00}}},
227 /* /63 */
228 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00,
229 0x00, 0x00, 0x00, 0x00, 0x00}}},
230 /* /64 */
231 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
232 0x00, 0x00, 0x00, 0x00, 0x00}}},
233 /* /65 */
234 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00,
235 0x00, 0x00, 0x00, 0x00, 0x00}}},
236 /* /66 */
237 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
238 0x00, 0x00, 0x00, 0x00, 0x00}}},
239 /* /67 */
240 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00,
241 0x00, 0x00, 0x00, 0x00, 0x00}}},
242 /* /68 */
243 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00,
244 0x00, 0x00, 0x00, 0x00, 0x00}}},
245 /* /69 */
246 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00,
247 0x00, 0x00, 0x00, 0x00, 0x00}}},
248 /* /70 */
249 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00,
250 0x00, 0x00, 0x00, 0x00, 0x00}}},
251 /* /71 */
252 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00,
253 0x00, 0x00, 0x00, 0x00, 0x00}}},
254 /* /72 */
255 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
256 0x00, 0x00, 0x00, 0x00, 0x00}}},
257 /* /73 */
258 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
259 0x00, 0x00, 0x00, 0x00, 0x00}}},
260 /* /74 */
261 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00,
262 0x00, 0x00, 0x00, 0x00, 0x00}}},
263 /* /75 */
264 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00,
265 0x00, 0x00, 0x00, 0x00, 0x00}}},
266 /* /76 */
267 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00,
268 0x00, 0x00, 0x00, 0x00, 0x00}}},
269 /* /77 */
270 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00,
271 0x00, 0x00, 0x00, 0x00, 0x00}}},
272 /* /78 */
273 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00,
274 0x00, 0x00, 0x00, 0x00, 0x00}}},
275 /* /79 */
276 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00,
277 0x00, 0x00, 0x00, 0x00, 0x00}}},
278 /* /80 */
279 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
280 0x00, 0x00, 0x00, 0x00, 0x00}}},
281 /* /81 */
282 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
283 0x00, 0x00, 0x00, 0x00, 0x00}}},
284 /* /82 */
285 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
286 0x00, 0x00, 0x00, 0x00, 0x00}}},
287 /* /83 */
288 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
289 0x00, 0x00, 0x00, 0x00, 0x00}}},
290 /* /84 */
291 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
292 0x00, 0x00, 0x00, 0x00, 0x00}}},
293 /* /85 */
294 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8,
295 0x00, 0x00, 0x00, 0x00, 0x00}}},
296 /* /86 */
297 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
298 0x00, 0x00, 0x00, 0x00, 0x00}}},
299 /* /87 */
300 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
301 0x00, 0x00, 0x00, 0x00, 0x00}}},
302 /* /88 */
303 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
304 0x00, 0x00, 0x00, 0x00, 0x00}}},
305 /* /89 */
306 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
307 0x80, 0x00, 0x00, 0x00, 0x00}}},
308 /* /90 */
309 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
310 0xc0, 0x00, 0x00, 0x00, 0x00}}},
311 /* /91 */
312 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
313 0xe0, 0x00, 0x00, 0x00, 0x00}}},
314 /* /92 */
315 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
316 0xf0, 0x00, 0x00, 0x00, 0x00}}},
317 /* /93 */
318 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
319 0xf8, 0x00, 0x00, 0x00, 0x00}}},
320 /* /94 */
321 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
322 0xfc, 0x00, 0x00, 0x00, 0x00}}},
323 /* /95 */
324 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
325 0xfe, 0x00, 0x00, 0x00, 0x00}}},
326 /* /96 */
327 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
328 0xff, 0x00, 0x00, 0x00, 0x00}}},
329 /* /97 */
330 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
331 0xff, 0x80, 0x00, 0x00, 0x00}}},
332 /* /98 */
333 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
334 0xff, 0xc0, 0x00, 0x00, 0x00}}},
335 /* /99 */
336 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
337 0xff, 0xe0, 0x00, 0x00, 0x00}}},
338 /* /100 */
339 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
340 0xff, 0xf0, 0x00, 0x00, 0x00}}},
341 /* /101 */
342 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
343 0xff, 0xf8, 0x00, 0x00, 0x00}}},
344 /* /102 */
345 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
346 0xff, 0xfc, 0x00, 0x00, 0x00}}},
347 /* /103 */
348 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
349 0xff, 0xfe, 0x00, 0x00, 0x00}}},
350 /* /104 */
351 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
352 0xff, 0xff, 0x00, 0x00, 0x00}}},
353 /* /105 */
354 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
355 0xff, 0xff, 0x80, 0x00, 0x00}}},
356 /* /106 */
357 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
358 0xff, 0xff, 0xc0, 0x00, 0x00}}},
359 /* /107 */
360 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
361 0xff, 0xff, 0xe0, 0x00, 0x00}}},
362 /* /108 */
363 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
364 0xff, 0xff, 0xf0, 0x00, 0x00}}},
365 /* /109 */
366 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
367 0xff, 0xff, 0xf8, 0x00, 0x00}}},
368 /* /110 */
369 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
370 0xff, 0xff, 0xfc, 0x00, 0x00}}},
371 /* /111 */
372 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
373 0xff, 0xff, 0xfe, 0x00, 0x00}}},
374 /* /112 */
375 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
376 0xff, 0xff, 0xff, 0x00, 0x00}}},
377 /* /113 */
378 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
379 0xff, 0xff, 0xff, 0x80, 0x00}}},
380 /* /114 */
381 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
382 0xff, 0xff, 0xff, 0xc0, 0x00}}},
383 /* /115 */
384 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
385 0xff, 0xff, 0xff, 0xe0, 0x00}}},
386 /* /116 */
387 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
388 0xff, 0xff, 0xff, 0xf0, 0x00}}},
389 /* /117 */
390 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
391 0xff, 0xff, 0xff, 0xf8, 0x00}}},
392 /* /118 */
393 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
394 0xff, 0xff, 0xff, 0xfc, 0x00}}},
395 /* /119 */
396 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
397 0xff, 0xff, 0xff, 0xfe, 0x00}}},
398 /* /120 */
399 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
400 0xff, 0xff, 0xff, 0xff, 0x00}}},
401 /* /121 */
402 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
403 0xff, 0xff, 0xff, 0xff, 0x80}}},
404 /* /122 */
405 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
406 0xff, 0xff, 0xff, 0xff, 0xc0}}},
407 /* /123 */
408 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
409 0xff, 0xff, 0xff, 0xff, 0xe0}}},
410 /* /124 */
411 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
412 0xff, 0xff, 0xff, 0xff, 0xf0}}},
413 /* /125 */
414 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
415 0xff, 0xff, 0xff, 0xff, 0xf8}}},
416 /* /126 */
417 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
418 0xff, 0xff, 0xff, 0xff, 0xfc}}},
419 /* /127 */
420 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
421 0xff, 0xff, 0xff, 0xff, 0xfe}}},
422 /* /128 */
423 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
424 0xff, 0xff, 0xff, 0xff, 0xff}}}};
425
426 /* Number of bits in prefix type. */
427 #ifndef PNBBY
428 #define PNBBY 8
429 #endif /* PNBBY */
430
431 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
432
433 void prefix_hexdump(const struct prefix *p)
434 {
435 char buf[PREFIX_STRLEN];
436
437 zlog_debug("prefix: %s",
438 prefix2str(p, buf, sizeof(buf)));
439 zlog_hexdump(p, sizeof(struct prefix));
440 }
441
442 int is_zero_mac(struct ethaddr *mac)
443 {
444 int i = 0;
445
446 for (i = 0; i < ETH_ALEN; i++) {
447 if (mac->octet[i])
448 return 0;
449 }
450
451 return 1;
452 }
453
454 unsigned int prefix_bit(const uint8_t *prefix, const uint16_t prefixlen)
455 {
456 unsigned int offset = prefixlen / 8;
457 unsigned int shift = 7 - (prefixlen % 8);
458
459 return (prefix[offset] >> shift) & 1;
460 }
461
462 unsigned int prefix6_bit(const struct in6_addr *prefix, const uint16_t prefixlen)
463 {
464 return prefix_bit((const uint8_t *)&prefix->s6_addr, prefixlen);
465 }
466
467 int str2family(const char *string)
468 {
469 if (!strcmp("ipv4", string))
470 return AF_INET;
471 else if (!strcmp("ipv6", string))
472 return AF_INET6;
473 else if (!strcmp("ethernet", string))
474 return AF_ETHERNET;
475 else if (!strcmp("evpn", string))
476 return AF_EVPN;
477 return -1;
478 }
479
480 const char *family2str(int family)
481 {
482 switch (family) {
483 case AF_INET:
484 return "IPv4";
485 case AF_INET6:
486 return "IPv6";
487 case AF_ETHERNET:
488 return "Ethernet";
489 case AF_EVPN:
490 return "Evpn";
491 }
492 return "?";
493 }
494
495 /* Address Famiy Identifier to Address Family converter. */
496 int afi2family(afi_t afi)
497 {
498 if (afi == AFI_IP)
499 return AF_INET;
500 else if (afi == AFI_IP6)
501 return AF_INET6;
502 else if (afi == AFI_L2VPN)
503 return AF_ETHERNET;
504 /* NOTE: EVPN code should NOT use this interface. */
505 return 0;
506 }
507
508 afi_t family2afi(int family)
509 {
510 if (family == AF_INET)
511 return AFI_IP;
512 else if (family == AF_INET6)
513 return AFI_IP6;
514 else if (family == AF_ETHERNET || family == AF_EVPN)
515 return AFI_L2VPN;
516 return 0;
517 }
518
519 const char *afi2str(afi_t afi)
520 {
521 switch (afi) {
522 case AFI_IP:
523 return "IPv4";
524 case AFI_IP6:
525 return "IPv6";
526 case AFI_L2VPN:
527 return "l2vpn";
528 case AFI_MAX:
529 return "bad-value";
530 default:
531 break;
532 }
533 return NULL;
534 }
535
536 const char *safi2str(safi_t safi)
537 {
538 switch (safi) {
539 case SAFI_UNICAST:
540 return "unicast";
541 case SAFI_MULTICAST:
542 return "multicast";
543 case SAFI_MPLS_VPN:
544 return "vpn";
545 case SAFI_ENCAP:
546 return "encap";
547 case SAFI_EVPN:
548 return "evpn";
549 case SAFI_LABELED_UNICAST:
550 return "labeled-unicast";
551 case SAFI_FLOWSPEC:
552 return "flowspec";
553 default:
554 return "unknown";
555 }
556 }
557
558 /* If n includes p prefix then return 1 else return 0. */
559 int prefix_match(const struct prefix *n, const struct prefix *p)
560 {
561 int offset;
562 int shift;
563 const uint8_t *np, *pp;
564
565 /* If n's prefix is longer than p's one return 0. */
566 if (n->prefixlen > p->prefixlen)
567 return 0;
568
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)
573 return 0;
574
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;
578
579 offset = n->u.prefix_flowspec.prefixlen;
580
581 while (offset--)
582 if (np[offset] != pp[offset])
583 return 0;
584 return 1;
585 }
586
587 /* Set both prefix's head pointer. */
588 np = n->u.val;
589 pp = p->u.val;
590
591 offset = n->prefixlen / PNBBY;
592 shift = n->prefixlen % PNBBY;
593
594 if (shift)
595 if (maskbit[shift] & (np[offset] ^ pp[offset]))
596 return 0;
597
598 while (offset--)
599 if (np[offset] != pp[offset])
600 return 0;
601 return 1;
602 }
603
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)
607 {
608 int offset;
609 int shift;
610 const uint8_t *np, *pp;
611
612 /* Set both prefix's head pointer. */
613 np = n->u.val;
614 pp = p->u.val;
615
616 offset = n->prefixlen / PNBBY;
617 shift = n->prefixlen % PNBBY;
618
619 if (shift)
620 if (maskbit[shift] & (np[offset] ^ pp[offset]))
621 return 0;
622
623 while (offset--)
624 if (np[offset] != pp[offset])
625 return 0;
626 return 1;
627 }
628
629 void prefix_copy(struct prefix *dest, const struct prefix *src)
630 {
631 dest->family = src->family;
632 dest->prefixlen = src->prefixlen;
633
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) {
648 void *temp;
649 int len;
650
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);
659 } else {
660 flog_err(EC_LIB_DEVELOPMENT,
661 "prefix_copy(): Unknown address family %d",
662 src->family);
663 assert(0);
664 }
665 }
666
667 /*
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).
674 */
675 int prefix_same(const struct prefix *p1, const struct prefix *p2)
676 {
677 if ((p1 && !p2) || (!p1 && p2))
678 return 0;
679
680 if (!p1 && !p2)
681 return 1;
682
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))
686 return 1;
687 if (p1->family == AF_INET6)
688 if (IPV6_ADDR_SAME(&p1->u.prefix6.s6_addr,
689 &p2->u.prefix6.s6_addr))
690 return 1;
691 if (p1->family == AF_ETHERNET)
692 if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth,
693 sizeof(struct ethaddr)))
694 return 1;
695 if (p1->family == AF_EVPN)
696 if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn,
697 sizeof(struct evpn_addr)))
698 return 1;
699 if (p1->family == AF_FLOWSPEC) {
700 if (p1->u.prefix_flowspec.prefixlen !=
701 p2->u.prefix_flowspec.prefixlen)
702 return 0;
703 if (!memcmp(&p1->u.prefix_flowspec.ptr,
704 &p2->u.prefix_flowspec.ptr,
705 p2->u.prefix_flowspec.prefixlen))
706 return 1;
707 }
708 }
709 return 0;
710 }
711
712 /*
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
720 * from prefix_same).
721 */
722 int prefix_cmp(const struct prefix *p1, const struct prefix *p2)
723 {
724 int offset;
725 int shift;
726
727 /* Set both prefix's head pointer. */
728 const uint8_t *pp1;
729 const uint8_t *pp2;
730
731 if (p1->family != p2->family)
732 return 1;
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;
736
737 if (p1->u.prefix_flowspec.prefixlen !=
738 p2->u.prefix_flowspec.prefixlen)
739 return 1;
740
741 offset = p1->u.prefix_flowspec.prefixlen;
742 while (offset--)
743 if (pp1[offset] != pp2[offset])
744 return 1;
745 return 0;
746 }
747 pp1 = p1->u.val;
748 pp2 = p2->u.val;
749
750 if (p1->prefixlen != p2->prefixlen)
751 return 1;
752 offset = p1->prefixlen / PNBBY;
753 shift = p1->prefixlen % PNBBY;
754
755 if (shift)
756 if (maskbit[shift] & (pp1[offset] ^ pp2[offset]))
757 return 1;
758
759 while (offset--)
760 if (pp1[offset] != pp2[offset])
761 return 1;
762
763 return 0;
764 }
765
766 /*
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.
771 */
772 int prefix_common_bits(const struct prefix *p1, const struct prefix *p2)
773 {
774 int pos, bit;
775 int length = 0;
776 uint8_t xor ;
777
778 /* Set both prefix's head pointer. */
779 const uint8_t *pp1 = p1->u.val;
780 const uint8_t *pp2 = p2->u.val;
781
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)
787 length = ETH_ALEN;
788 if (p1->family == AF_EVPN)
789 length = 8 * sizeof(struct evpn_addr);
790
791 if (p1->family != p2->family || !length)
792 return -1;
793
794 for (pos = 0; pos < length; pos++)
795 if (pp1[pos] != pp2[pos])
796 break;
797 if (pos == length)
798 return pos * 8;
799
800 xor = pp1[pos] ^ pp2[pos];
801 for (bit = 0; bit < 8; bit++)
802 if (xor&(1 << (7 - bit)))
803 break;
804
805 return pos * 8 + bit;
806 }
807
808 /* Return prefix family type string. */
809 const char *prefix_family_str(const struct prefix *p)
810 {
811 if (p->family == AF_INET)
812 return "inet";
813 if (p->family == AF_INET6)
814 return "inet6";
815 if (p->family == AF_ETHERNET)
816 return "ether";
817 if (p->family == AF_EVPN)
818 return "evpn";
819 return "unspec";
820 }
821
822 /* Allocate new prefix_ipv4 structure. */
823 struct prefix_ipv4 *prefix_ipv4_new()
824 {
825 struct prefix_ipv4 *p;
826
827 /* Call prefix_new to allocate a full-size struct prefix to avoid
828 problems
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();
832 p->family = AF_INET;
833 return p;
834 }
835
836 /* Free prefix_ipv4 structure. */
837 void prefix_ipv4_free(struct prefix_ipv4 *p)
838 {
839 prefix_free((struct prefix *)p);
840 }
841
842 /* When string format is invalid return 0. */
843 int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p)
844 {
845 int ret;
846 int plen;
847 char *pnt;
848 char *cp;
849
850 /* Find slash inside string. */
851 pnt = strchr(str, '/');
852
853 /* String doesn't contail slash. */
854 if (pnt == NULL) {
855 /* Convert string to prefix. */
856 ret = inet_pton(AF_INET, str, &p->prefix);
857 if (ret == 0)
858 return 0;
859
860 /* If address doesn't contain slash we assume it host address.
861 */
862 p->family = AF_INET;
863 p->prefixlen = IPV4_MAX_BITLEN;
864
865 return ret;
866 } else {
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);
872
873 /* Get prefix length. */
874 plen = (uint8_t)atoi(++pnt);
875 if (plen > IPV4_MAX_PREFIXLEN)
876 return 0;
877
878 p->family = AF_INET;
879 p->prefixlen = plen;
880 }
881
882 return ret;
883 }
884
885 /* When string format is invalid return 0. */
886 int str2prefix_eth(const char *str, struct prefix_eth *p)
887 {
888 int ret = 0;
889 int plen = 48;
890 char *pnt;
891 char *cp = NULL;
892 const char *str_addr = str;
893 unsigned int a[6];
894 int i;
895 bool slash = false;
896
897 if (!strcmp(str, "any")) {
898 memset(p, 0, sizeof(*p));
899 p->family = AF_ETHERNET;
900 return 1;
901 }
902
903 /* Find slash inside string. */
904 pnt = strchr(str, '/');
905
906 if (pnt) {
907 /* Get prefix length. */
908 plen = (uint8_t)atoi(++pnt);
909 if (plen > 48) {
910 ret = 0;
911 goto done;
912 }
913
914 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
915 strncpy(cp, str, pnt - str);
916 *(cp + (pnt - str)) = '\0';
917
918 str_addr = cp;
919 slash = true;
920 }
921
922 /* Convert string to prefix. */
923 if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2,
924 a + 3, a + 4, a + 5)
925 != 6) {
926 ret = 0;
927 goto done;
928 }
929 for (i = 0; i < 6; ++i) {
930 p->eth_addr.octet[i] = a[i] & 0xff;
931 }
932 p->prefixlen = plen;
933 p->family = AF_ETHERNET;
934
935 /*
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
939 */
940 if (!slash && is_zero_mac(&(p->eth_addr)))
941 p->prefixlen = 0;
942
943 ret = 1;
944
945 done:
946 if (cp)
947 XFREE(MTYPE_TMP, cp);
948
949 return ret;
950 }
951
952 /* Convert masklen into IP address's netmask (network byte order). */
953 void masklen2ip(const int masklen, struct in_addr *netmask)
954 {
955 assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
956
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) */
960
961 if (sizeof(unsigned long long) > 4)
962 netmask->s_addr = htonl(0xffffffffULL << (32 - masklen));
963 else
964 netmask->s_addr =
965 htonl(masklen ? 0xffffffffU << (32 - masklen) : 0);
966 }
967
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)
971 {
972 uint32_t tmp = ~ntohl(netmask.s_addr);
973
974 /*
975 * clz: count leading zeroes. sadly, the behaviour of this builtin is
976 * undefined for a 0 argument, even though most CPUs give 32
977 */
978 return tmp ? __builtin_clz(tmp) : 32;
979 }
980
981 /* Apply mask to IPv4 prefix (network byte order). */
982 void apply_mask_ipv4(struct prefix_ipv4 *p)
983 {
984 struct in_addr mask;
985 masklen2ip(p->prefixlen, &mask);
986 p->prefix.s_addr &= mask.s_addr;
987 }
988
989 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
990 int prefix_ipv4_any(const struct prefix_ipv4 *p)
991 {
992 return (p->prefix.s_addr == 0 && p->prefixlen == 0);
993 }
994
995 /* Allocate a new ip version 6 route */
996 struct prefix_ipv6 *prefix_ipv6_new(void)
997 {
998 struct prefix_ipv6 *p;
999
1000 /* Allocate a full-size struct prefix to avoid problems with structure
1001 size mismatches. */
1002 p = (struct prefix_ipv6 *)prefix_new();
1003 p->family = AF_INET6;
1004 return p;
1005 }
1006
1007 /* Free prefix for IPv6. */
1008 void prefix_ipv6_free(struct prefix_ipv6 *p)
1009 {
1010 prefix_free((struct prefix *)p);
1011 }
1012
1013 /* If given string is valid return pin6 else return NULL */
1014 int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p)
1015 {
1016 char *pnt;
1017 char *cp;
1018 int ret;
1019
1020 pnt = strchr(str, '/');
1021
1022 /* If string doesn't contain `/' treat it as host route. */
1023 if (pnt == NULL) {
1024 ret = inet_pton(AF_INET6, str, &p->prefix);
1025 if (ret == 0)
1026 return 0;
1027 p->prefixlen = IPV6_MAX_BITLEN;
1028 } else {
1029 int plen;
1030
1031 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
1032 strncpy(cp, str, pnt - str);
1033 *(cp + (pnt - str)) = '\0';
1034 ret = inet_pton(AF_INET6, cp, &p->prefix);
1035 XFREE(MTYPE_TMP, cp);
1036 if (ret == 0)
1037 return 0;
1038 plen = (uint8_t)atoi(++pnt);
1039 if (plen > IPV6_MAX_BITLEN)
1040 return 0;
1041 p->prefixlen = plen;
1042 }
1043 p->family = AF_INET6;
1044
1045 return ret;
1046 }
1047
1048 /* Convert struct in6_addr netmask into integer.
1049 * FIXME return uint8_t as ip_maskleni() does. */
1050 int ip6_masklen(struct in6_addr netmask)
1051 {
1052 int len = 0;
1053 unsigned char val;
1054 unsigned char *pnt;
1055
1056 pnt = (unsigned char *)&netmask;
1057
1058 while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN) {
1059 len += 8;
1060 pnt++;
1061 }
1062
1063 if (len < IPV6_MAX_BITLEN) {
1064 val = *pnt;
1065 while (val) {
1066 len++;
1067 val <<= 1;
1068 }
1069 }
1070 return len;
1071 }
1072
1073 void masklen2ip6(const int masklen, struct in6_addr *netmask)
1074 {
1075 assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
1076 memcpy(netmask, maskbytes6 + masklen, sizeof(struct in6_addr));
1077 }
1078
1079 void apply_mask_ipv6(struct prefix_ipv6 *p)
1080 {
1081 uint8_t *pnt;
1082 int index;
1083 int offset;
1084
1085 index = p->prefixlen / 8;
1086
1087 if (index < 16) {
1088 pnt = (uint8_t *)&p->prefix;
1089 offset = p->prefixlen % 8;
1090
1091 pnt[index] &= maskbit[offset];
1092 index++;
1093
1094 while (index < 16)
1095 pnt[index++] = 0;
1096 }
1097 }
1098
1099 void apply_mask(struct prefix *p)
1100 {
1101 switch (p->family) {
1102 case AF_INET:
1103 apply_mask_ipv4((struct prefix_ipv4 *)p);
1104 break;
1105 case AF_INET6:
1106 apply_mask_ipv6((struct prefix_ipv6 *)p);
1107 break;
1108 default:
1109 break;
1110 }
1111 return;
1112 }
1113
1114 /* Utility function of convert between struct prefix <=> union sockunion.
1115 * FIXME This function isn't used anywhere. */
1116 struct prefix *sockunion2prefix(const union sockunion *dest,
1117 const union sockunion *mask)
1118 {
1119 if (dest->sa.sa_family == AF_INET) {
1120 struct prefix_ipv4 *p;
1121
1122 p = prefix_ipv4_new();
1123 p->family = AF_INET;
1124 p->prefix = dest->sin.sin_addr;
1125 p->prefixlen = ip_masklen(mask->sin.sin_addr);
1126 return (struct prefix *)p;
1127 }
1128 if (dest->sa.sa_family == AF_INET6) {
1129 struct prefix_ipv6 *p;
1130
1131 p = prefix_ipv6_new();
1132 p->family = AF_INET6;
1133 p->prefixlen = ip6_masklen(mask->sin6.sin6_addr);
1134 memcpy(&p->prefix, &dest->sin6.sin6_addr,
1135 sizeof(struct in6_addr));
1136 return (struct prefix *)p;
1137 }
1138 return NULL;
1139 }
1140
1141 /* Utility function of convert between struct prefix <=> union sockunion. */
1142 struct prefix *sockunion2hostprefix(const union sockunion *su,
1143 struct prefix *prefix)
1144 {
1145 if (su->sa.sa_family == AF_INET) {
1146 struct prefix_ipv4 *p;
1147
1148 p = prefix ? (struct prefix_ipv4 *)prefix : prefix_ipv4_new();
1149 p->family = AF_INET;
1150 p->prefix = su->sin.sin_addr;
1151 p->prefixlen = IPV4_MAX_BITLEN;
1152 return (struct prefix *)p;
1153 }
1154 if (su->sa.sa_family == AF_INET6) {
1155 struct prefix_ipv6 *p;
1156
1157 p = prefix ? (struct prefix_ipv6 *)prefix : prefix_ipv6_new();
1158 p->family = AF_INET6;
1159 p->prefixlen = IPV6_MAX_BITLEN;
1160 memcpy(&p->prefix, &su->sin6.sin6_addr,
1161 sizeof(struct in6_addr));
1162 return (struct prefix *)p;
1163 }
1164 return NULL;
1165 }
1166
1167 void prefix2sockunion(const struct prefix *p, union sockunion *su)
1168 {
1169 memset(su, 0, sizeof(*su));
1170
1171 su->sa.sa_family = p->family;
1172 if (p->family == AF_INET)
1173 su->sin.sin_addr = p->u.prefix4;
1174 if (p->family == AF_INET6)
1175 memcpy(&su->sin6.sin6_addr, &p->u.prefix6,
1176 sizeof(struct in6_addr));
1177 }
1178
1179 int prefix_blen(const struct prefix *p)
1180 {
1181 switch (p->family) {
1182 case AF_INET:
1183 return IPV4_MAX_BYTELEN;
1184 break;
1185 case AF_INET6:
1186 return IPV6_MAX_BYTELEN;
1187 break;
1188 case AF_ETHERNET:
1189 return ETH_ALEN;
1190 break;
1191 }
1192 return 0;
1193 }
1194
1195 /* Generic function for conversion string to struct prefix. */
1196 int str2prefix(const char *str, struct prefix *p)
1197 {
1198 int ret;
1199
1200 if (!str || !p)
1201 return 0;
1202
1203 /* First we try to convert string to struct prefix_ipv4. */
1204 ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p);
1205 if (ret)
1206 return ret;
1207
1208 /* Next we try to convert string to struct prefix_ipv6. */
1209 ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p);
1210 if (ret)
1211 return ret;
1212
1213 /* Next we try to convert string to struct prefix_eth. */
1214 ret = str2prefix_eth(str, (struct prefix_eth *)p);
1215 if (ret)
1216 return ret;
1217
1218 return 0;
1219 }
1220
1221 static const char *prefixevpn_ead2str(const struct prefix_evpn *p, char *str,
1222 int size)
1223 {
1224 snprintf(str, size, "Unsupported EVPN prefix");
1225 return str;
1226 }
1227
1228 static const char *prefixevpn_macip2str(const struct prefix_evpn *p, char *str,
1229 int size)
1230 {
1231 uint8_t family;
1232 char buf[PREFIX2STR_BUFFER];
1233 char buf2[ETHER_ADDR_STRLEN];
1234
1235 if (is_evpn_prefix_ipaddr_none(p))
1236 snprintf(str, size, "[%d]:[%s]/%d",
1237 p->prefix.route_type,
1238 prefix_mac2str(&p->prefix.macip_addr.mac,
1239 buf2, sizeof(buf2)),
1240 p->prefixlen);
1241 else {
1242 family = is_evpn_prefix_ipaddr_v4(p)
1243 ? AF_INET
1244 : AF_INET6;
1245 snprintf(str, size, "[%d]:[%s]:[%s]/%d",
1246 p->prefix.route_type,
1247 prefix_mac2str(&p->prefix.macip_addr.mac,
1248 buf2, sizeof(buf2)),
1249 inet_ntop(family,
1250 &p->prefix.macip_addr.ip.ip.addr,
1251 buf, PREFIX2STR_BUFFER),
1252 p->prefixlen);
1253 }
1254 return str;
1255 }
1256
1257 static const char *prefixevpn_imet2str(const struct prefix_evpn *p, char *str,
1258 int size)
1259 {
1260 uint8_t family;
1261 char buf[PREFIX2STR_BUFFER];
1262
1263 family = is_evpn_prefix_ipaddr_v4(p)
1264 ? AF_INET
1265 : AF_INET6;
1266 snprintf(str, size, "[%d]:[%s]/%d", p->prefix.route_type,
1267 inet_ntop(family,
1268 &p->prefix.imet_addr.ip.ip.addr, buf,
1269 PREFIX2STR_BUFFER),
1270 p->prefixlen);
1271 return str;
1272 }
1273
1274 static const char *prefixevpn_es2str(const struct prefix_evpn *p, char *str,
1275 int size)
1276 {
1277 char buf[ESI_STR_LEN];
1278
1279 snprintf(str, size, "[%d]:[%s]:[%s]/%d", p->prefix.route_type,
1280 esi_to_str(&p->prefix.es_addr.esi, buf, sizeof(buf)),
1281 inet_ntoa(p->prefix.es_addr.ip.ipaddr_v4),
1282 p->prefixlen);
1283 return str;
1284 }
1285
1286 static const char *prefixevpn_prefix2str(const struct prefix_evpn *p, char *str,
1287 int size)
1288 {
1289 uint8_t family;
1290 char buf[PREFIX2STR_BUFFER];
1291
1292 family = is_evpn_prefix_ipaddr_v4(p)
1293 ? AF_INET
1294 : AF_INET6;
1295 snprintf(str, size, "[%d]:[%u][%s/%d]/%d",
1296 p->prefix.route_type,
1297 p->prefix.prefix_addr.eth_tag,
1298 inet_ntop(family,
1299 &p->prefix.prefix_addr.ip.ip.addr, buf,
1300 PREFIX2STR_BUFFER),
1301 p->prefix.prefix_addr.ip_prefix_length,
1302 p->prefixlen);
1303 return str;
1304 }
1305
1306 static const char *prefixevpn2str(const struct prefix_evpn *p, char *str,
1307 int size)
1308 {
1309 switch (p->prefix.route_type) {
1310 case 1:
1311 return prefixevpn_ead2str(p, str, size);
1312 case 2:
1313 return prefixevpn_macip2str(p, str, size);
1314 case 3:
1315 return prefixevpn_imet2str(p, str, size);
1316 case 4:
1317 return prefixevpn_es2str(p, str, size);
1318 case 5:
1319 return prefixevpn_prefix2str(p, str, size);
1320 default:
1321 snprintf(str, size, "Unsupported EVPN prefix");
1322 break;
1323 }
1324 return str;
1325 }
1326
1327 const char *prefix2str(union prefixconstptr pu, char *str, int size)
1328 {
1329 const struct prefix *p = pu.p;
1330 char buf[PREFIX2STR_BUFFER];
1331
1332 switch (p->family) {
1333 case AF_INET:
1334 case AF_INET6:
1335 snprintf(str, size, "%s/%d", inet_ntop(p->family, &p->u.prefix,
1336 buf, PREFIX2STR_BUFFER),
1337 p->prefixlen);
1338 break;
1339
1340 case AF_ETHERNET:
1341 snprintf(str, size, "%s/%d",
1342 prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)),
1343 p->prefixlen);
1344 break;
1345
1346 case AF_EVPN:
1347 prefixevpn2str((const struct prefix_evpn *)p, str, size);
1348 break;
1349
1350 case AF_FLOWSPEC:
1351 sprintf(str, "FS prefix");
1352 break;
1353
1354 default:
1355 sprintf(str, "UNK prefix");
1356 break;
1357 }
1358
1359 return str;
1360 }
1361
1362 struct prefix *prefix_new()
1363 {
1364 struct prefix *p;
1365
1366 p = XCALLOC(MTYPE_PREFIX, sizeof *p);
1367 return p;
1368 }
1369
1370 /* Free prefix structure. */
1371 void prefix_free(struct prefix *p)
1372 {
1373 XFREE(MTYPE_PREFIX, p);
1374 }
1375
1376 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1377 void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
1378 {
1379
1380 uint32_t destination;
1381
1382 destination = ntohl(p->prefix.s_addr);
1383
1384 if (p->prefixlen == IPV4_MAX_PREFIXLEN)
1385 ;
1386 /* do nothing for host routes */
1387 else if (IN_CLASSC(destination)) {
1388 p->prefixlen = 24;
1389 apply_mask_ipv4(p);
1390 } else if (IN_CLASSB(destination)) {
1391 p->prefixlen = 16;
1392 apply_mask_ipv4(p);
1393 } else {
1394 p->prefixlen = 8;
1395 apply_mask_ipv4(p);
1396 }
1397 }
1398
1399 in_addr_t ipv4_network_addr(in_addr_t hostaddr, int masklen)
1400 {
1401 struct in_addr mask;
1402
1403 masklen2ip(masklen, &mask);
1404 return hostaddr & mask.s_addr;
1405 }
1406
1407 in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
1408 {
1409 struct in_addr mask;
1410
1411 masklen2ip(masklen, &mask);
1412 return (masklen != IPV4_MAX_PREFIXLEN - 1) ?
1413 /* normal case */
1414 (hostaddr | ~mask.s_addr)
1415 :
1416 /* special case for /31 */
1417 (hostaddr ^ ~mask.s_addr);
1418 }
1419
1420 /* Utility function to convert ipv4 netmask to prefixes
1421 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1422 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1423 int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1424 char *prefix_str)
1425 {
1426 struct in_addr network;
1427 struct in_addr mask;
1428 uint8_t prefixlen;
1429 uint32_t destination;
1430 int ret;
1431
1432 ret = inet_aton(net_str, &network);
1433 if (!ret)
1434 return 0;
1435
1436 if (mask_str) {
1437 ret = inet_aton(mask_str, &mask);
1438 if (!ret)
1439 return 0;
1440
1441 prefixlen = ip_masklen(mask);
1442 } else {
1443 destination = ntohl(network.s_addr);
1444
1445 if (network.s_addr == 0)
1446 prefixlen = 0;
1447 else if (IN_CLASSC(destination))
1448 prefixlen = 24;
1449 else if (IN_CLASSB(destination))
1450 prefixlen = 16;
1451 else if (IN_CLASSA(destination))
1452 prefixlen = 8;
1453 else
1454 return 0;
1455 }
1456
1457 sprintf(prefix_str, "%s/%d", net_str, prefixlen);
1458
1459 return 1;
1460 }
1461
1462 /* Utility function for making IPv6 address string. */
1463 const char *inet6_ntoa(struct in6_addr addr)
1464 {
1465 static char buf[INET6_ADDRSTRLEN];
1466
1467 inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1468 return buf;
1469 }
1470
1471 /* converts to internal representation of mac address
1472 * returns 1 on success, 0 otherwise
1473 * format accepted: AA:BB:CC:DD:EE:FF
1474 * if mac parameter is null, then check only
1475 */
1476 int prefix_str2mac(const char *str, struct ethaddr *mac)
1477 {
1478 unsigned int a[6];
1479 int i;
1480
1481 if (!str)
1482 return 0;
1483
1484 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3,
1485 a + 4, a + 5)
1486 != 6) {
1487 /* error in incoming str length */
1488 return 0;
1489 }
1490 /* valid mac address */
1491 if (!mac)
1492 return 1;
1493 for (i = 0; i < 6; ++i)
1494 mac->octet[i] = a[i] & 0xff;
1495 return 1;
1496 }
1497
1498 char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
1499 {
1500 char *ptr;
1501
1502 if (!mac)
1503 return NULL;
1504 if (!buf)
1505 ptr = (char *)XMALLOC(MTYPE_TMP,
1506 ETHER_ADDR_STRLEN * sizeof(char));
1507 else {
1508 assert(size >= ETHER_ADDR_STRLEN);
1509 ptr = buf;
1510 }
1511 snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x",
1512 (uint8_t)mac->octet[0], (uint8_t)mac->octet[1],
1513 (uint8_t)mac->octet[2], (uint8_t)mac->octet[3],
1514 (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]);
1515 return ptr;
1516 }
1517
1518 unsigned prefix_hash_key(void *pp)
1519 {
1520 struct prefix copy;
1521
1522 if (((struct prefix *)pp)->family == AF_FLOWSPEC) {
1523 uint32_t len;
1524 void *temp;
1525
1526 /* make sure *all* unused bits are zero,
1527 * particularly including alignment /
1528 * padding and unused prefix bytes.
1529 */
1530 memset(&copy, 0, sizeof(copy));
1531 prefix_copy(&copy, (struct prefix *)pp);
1532 len = jhash((void *)copy.u.prefix_flowspec.ptr,
1533 copy.u.prefix_flowspec.prefixlen,
1534 0x55aa5a5a);
1535 temp = (void *)copy.u.prefix_flowspec.ptr;
1536 XFREE(MTYPE_PREFIX_FLOWSPEC, temp);
1537 copy.u.prefix_flowspec.ptr = (uintptr_t)NULL;
1538 return len;
1539 }
1540 /* make sure *all* unused bits are zero, particularly including
1541 * alignment /
1542 * padding and unused prefix bytes. */
1543 memset(&copy, 0, sizeof(copy));
1544 prefix_copy(&copy, (struct prefix *)pp);
1545 return jhash(&copy,
1546 offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen),
1547 0x55aa5a5a);
1548 }
1549
1550 /* converts to internal representation of esi
1551 * returns 1 on success, 0 otherwise
1552 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1553 * if esi parameter is null, then check only
1554 */
1555 int str_to_esi(const char *str, esi_t *esi)
1556 {
1557 int i;
1558 unsigned int a[ESI_BYTES];
1559
1560 if (!str)
1561 return 0;
1562
1563 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1564 a + 0, a + 1, a + 2, a + 3,
1565 a + 4, a + 5, a + 6, a + 7,
1566 a + 8, a + 9)
1567 != ESI_BYTES) {
1568 /* error in incoming str length */
1569 return 0;
1570 }
1571
1572 /* valid ESI */
1573 if (!esi)
1574 return 1;
1575 for (i = 0; i < ESI_BYTES; ++i)
1576 esi->val[i] = a[i] & 0xff;
1577 return 1;
1578 }
1579
1580 char *esi_to_str(const esi_t *esi, char *buf, int size)
1581 {
1582 char *ptr;
1583
1584 if (!esi)
1585 return NULL;
1586 if (!buf)
1587 ptr = (char *)XMALLOC(MTYPE_TMP,
1588 ESI_STR_LEN * sizeof(char));
1589 else {
1590 assert(size >= ESI_STR_LEN);
1591 ptr = buf;
1592 }
1593
1594 snprintf(ptr, ESI_STR_LEN,
1595 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1596 esi->val[0], esi->val[1], esi->val[2],
1597 esi->val[3], esi->val[4], esi->val[5],
1598 esi->val[6], esi->val[7], esi->val[8],
1599 esi->val[9]);
1600 return ptr;
1601 }