]> git.proxmox.com Git - mirror_frr.git/blame - lib/prefix.c
*: conform with COMMUNITY.md formatting rules, via 'make indent'
[mirror_frr.git] / lib / prefix.c
CommitLineData
718e3744 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 *
896014f4
DL
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
718e3744 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"
7a7761d2 29#include "jhash.h"
6b0655a2 30
4a1ab8e4
DL
31DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix")
32
718e3744 33/* Maskbit. */
2d362d10 34static const u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
d62a17ae 35 0xf8, 0xfc, 0xfe, 0xff};
36
37static const struct in6_addr maskbytes6[] = {
38 /* /0 */ {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}},
996c9314
LB
40 /* /1 */
41 {{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00}}},
43 /* /2 */
44 {{{0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00}}},
46 /* /3 */
47 {{{0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00}}},
49 /* /4 */
50 {{{0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00}}},
52 /* /5 */
53 {{{0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00}}},
55 /* /6 */
56 {{{0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00}}},
58 /* /7 */
59 {{{0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00}}},
61 /* /8 */
62 {{{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00}}},
64 /* /9 */
65 {{{0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00}}},
67 /* /10 */
68 {{{0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00}}},
70 /* /11 */
71 {{{0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00}}},
73 /* /12 */
74 {{{0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00}}},
76 /* /13 */
77 {{{0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00}}},
79 /* /14 */
80 {{{0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00}}},
82 /* /15 */
83 {{{0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0x00}}},
85 /* /16 */
86 {{{0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00}}},
88 /* /17 */
89 {{{0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00}}},
91 /* /18 */
92 {{{0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00}}},
94 /* /19 */
95 {{{0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00, 0x00}}},
97 /* /20 */
98 {{{0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00, 0x00}}},
100 /* /21 */
101 {{{0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102 0x00, 0x00, 0x00, 0x00, 0x00}}},
103 /* /22 */
104 {{{0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x00}}},
106 /* /23 */
107 {{{0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 0x00, 0x00, 0x00, 0x00, 0x00}}},
109 /* /24 */
110 {{{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x00}}},
112 /* /25 */
113 {{{0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00}}},
115 /* /26 */
116 {{{0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 0x00, 0x00, 0x00, 0x00, 0x00}}},
118 /* /27 */
119 {{{0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
120 0x00, 0x00, 0x00, 0x00, 0x00}}},
121 /* /28 */
122 {{{0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 0x00, 0x00, 0x00, 0x00, 0x00}}},
124 /* /29 */
125 {{{0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00}}},
127 /* /30 */
128 {{{0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00}}},
130 /* /31 */
131 {{{0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 0x00, 0x00, 0x00, 0x00, 0x00}}},
133 /* /32 */
134 {{{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00, 0x00}}},
136 /* /33 */
137 {{{0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 0x00, 0x00, 0x00, 0x00, 0x00}}},
139 /* /34 */
140 {{{0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
141 0x00, 0x00, 0x00, 0x00, 0x00}}},
142 /* /35 */
143 {{{0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x00, 0x00}}},
145 /* /36 */
146 {{{0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147 0x00, 0x00, 0x00, 0x00, 0x00}}},
148 /* /37 */
149 {{{0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150 0x00, 0x00, 0x00, 0x00, 0x00}}},
151 /* /38 */
152 {{{0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00}}},
154 /* /39 */
155 {{{0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x00}}},
157 /* /40 */
158 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0x00, 0x00}}},
160 /* /41 */
161 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
162 0x00, 0x00, 0x00, 0x00, 0x00}}},
163 /* /42 */
164 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
165 0x00, 0x00, 0x00, 0x00, 0x00}}},
166 /* /43 */
167 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
168 0x00, 0x00, 0x00, 0x00, 0x00}}},
169 /* /44 */
170 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
171 0x00, 0x00, 0x00, 0x00, 0x00}}},
172 /* /45 */
173 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
174 0x00, 0x00, 0x00, 0x00, 0x00}}},
175 /* /46 */
176 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
177 0x00, 0x00, 0x00, 0x00, 0x00}}},
178 /* /47 */
179 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
180 0x00, 0x00, 0x00, 0x00, 0x00}}},
181 /* /48 */
182 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
183 0x00, 0x00, 0x00, 0x00, 0x00}}},
184 /* /49 */
185 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00,
186 0x00, 0x00, 0x00, 0x00, 0x00}}},
187 /* /50 */
188 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00,
189 0x00, 0x00, 0x00, 0x00, 0x00}}},
190 /* /51 */
191 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00, 0x00}}},
193 /* /52 */
194 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00,
195 0x00, 0x00, 0x00, 0x00, 0x00}}},
196 /* /53 */
197 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00}}},
199 /* /54 */
200 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
201 0x00, 0x00, 0x00, 0x00, 0x00}}},
202 /* /55 */
203 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
204 0x00, 0x00, 0x00, 0x00, 0x00}}},
205 /* /56 */
206 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
207 0x00, 0x00, 0x00, 0x00, 0x00}}},
208 /* /57 */
209 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
210 0x00, 0x00, 0x00, 0x00, 0x00}}},
211 /* /58 */
212 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00,
213 0x00, 0x00, 0x00, 0x00, 0x00}}},
214 /* /59 */
215 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00,
216 0x00, 0x00, 0x00, 0x00, 0x00}}},
217 /* /60 */
218 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00,
219 0x00, 0x00, 0x00, 0x00, 0x00}}},
220 /* /61 */
221 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00,
222 0x00, 0x00, 0x00, 0x00, 0x00}}},
223 /* /62 */
224 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00,
225 0x00, 0x00, 0x00, 0x00, 0x00}}},
226 /* /63 */
227 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x00}}},
229 /* /64 */
230 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
231 0x00, 0x00, 0x00, 0x00, 0x00}}},
232 /* /65 */
233 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00,
234 0x00, 0x00, 0x00, 0x00, 0x00}}},
235 /* /66 */
236 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00}}},
238 /* /67 */
239 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00,
240 0x00, 0x00, 0x00, 0x00, 0x00}}},
241 /* /68 */
242 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00,
243 0x00, 0x00, 0x00, 0x00, 0x00}}},
244 /* /69 */
245 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00,
246 0x00, 0x00, 0x00, 0x00, 0x00}}},
247 /* /70 */
248 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00,
249 0x00, 0x00, 0x00, 0x00, 0x00}}},
250 /* /71 */
251 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00,
252 0x00, 0x00, 0x00, 0x00, 0x00}}},
253 /* /72 */
254 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
255 0x00, 0x00, 0x00, 0x00, 0x00}}},
256 /* /73 */
257 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
258 0x00, 0x00, 0x00, 0x00, 0x00}}},
259 /* /74 */
260 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00,
261 0x00, 0x00, 0x00, 0x00, 0x00}}},
262 /* /75 */
263 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00,
264 0x00, 0x00, 0x00, 0x00, 0x00}}},
265 /* /76 */
266 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00,
267 0x00, 0x00, 0x00, 0x00, 0x00}}},
268 /* /77 */
269 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00,
270 0x00, 0x00, 0x00, 0x00, 0x00}}},
271 /* /78 */
272 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00,
273 0x00, 0x00, 0x00, 0x00, 0x00}}},
274 /* /79 */
275 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00,
276 0x00, 0x00, 0x00, 0x00, 0x00}}},
277 /* /80 */
278 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
279 0x00, 0x00, 0x00, 0x00, 0x00}}},
280 /* /81 */
281 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
282 0x00, 0x00, 0x00, 0x00, 0x00}}},
283 /* /82 */
284 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
285 0x00, 0x00, 0x00, 0x00, 0x00}}},
286 /* /83 */
287 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
288 0x00, 0x00, 0x00, 0x00, 0x00}}},
289 /* /84 */
290 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
291 0x00, 0x00, 0x00, 0x00, 0x00}}},
292 /* /85 */
293 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8,
294 0x00, 0x00, 0x00, 0x00, 0x00}}},
295 /* /86 */
296 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
297 0x00, 0x00, 0x00, 0x00, 0x00}}},
298 /* /87 */
299 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
300 0x00, 0x00, 0x00, 0x00, 0x00}}},
301 /* /88 */
302 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
303 0x00, 0x00, 0x00, 0x00, 0x00}}},
304 /* /89 */
305 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
306 0x80, 0x00, 0x00, 0x00, 0x00}}},
307 /* /90 */
308 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
309 0xc0, 0x00, 0x00, 0x00, 0x00}}},
310 /* /91 */
311 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
312 0xe0, 0x00, 0x00, 0x00, 0x00}}},
313 /* /92 */
314 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
315 0xf0, 0x00, 0x00, 0x00, 0x00}}},
316 /* /93 */
317 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
318 0xf8, 0x00, 0x00, 0x00, 0x00}}},
319 /* /94 */
320 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
321 0xfc, 0x00, 0x00, 0x00, 0x00}}},
322 /* /95 */
323 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
324 0xfe, 0x00, 0x00, 0x00, 0x00}}},
325 /* /96 */
326 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
327 0xff, 0x00, 0x00, 0x00, 0x00}}},
328 /* /97 */
329 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
330 0xff, 0x80, 0x00, 0x00, 0x00}}},
331 /* /98 */
332 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
333 0xff, 0xc0, 0x00, 0x00, 0x00}}},
334 /* /99 */
335 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
336 0xff, 0xe0, 0x00, 0x00, 0x00}}},
337 /* /100 */
338 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
339 0xff, 0xf0, 0x00, 0x00, 0x00}}},
340 /* /101 */
341 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
342 0xff, 0xf8, 0x00, 0x00, 0x00}}},
343 /* /102 */
344 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
345 0xff, 0xfc, 0x00, 0x00, 0x00}}},
346 /* /103 */
347 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
348 0xff, 0xfe, 0x00, 0x00, 0x00}}},
349 /* /104 */
350 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
351 0xff, 0xff, 0x00, 0x00, 0x00}}},
352 /* /105 */
353 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
354 0xff, 0xff, 0x80, 0x00, 0x00}}},
355 /* /106 */
356 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
357 0xff, 0xff, 0xc0, 0x00, 0x00}}},
358 /* /107 */
359 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
360 0xff, 0xff, 0xe0, 0x00, 0x00}}},
361 /* /108 */
362 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
363 0xff, 0xff, 0xf0, 0x00, 0x00}}},
364 /* /109 */
365 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
366 0xff, 0xff, 0xf8, 0x00, 0x00}}},
367 /* /110 */
368 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
369 0xff, 0xff, 0xfc, 0x00, 0x00}}},
370 /* /111 */
371 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
372 0xff, 0xff, 0xfe, 0x00, 0x00}}},
373 /* /112 */
374 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
375 0xff, 0xff, 0xff, 0x00, 0x00}}},
376 /* /113 */
377 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
378 0xff, 0xff, 0xff, 0x80, 0x00}}},
379 /* /114 */
380 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
381 0xff, 0xff, 0xff, 0xc0, 0x00}}},
382 /* /115 */
383 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
384 0xff, 0xff, 0xff, 0xe0, 0x00}}},
385 /* /116 */
386 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
387 0xff, 0xff, 0xff, 0xf0, 0x00}}},
388 /* /117 */
389 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
390 0xff, 0xff, 0xff, 0xf8, 0x00}}},
391 /* /118 */
392 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
393 0xff, 0xff, 0xff, 0xfc, 0x00}}},
394 /* /119 */
395 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
396 0xff, 0xff, 0xff, 0xfe, 0x00}}},
397 /* /120 */
398 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
399 0xff, 0xff, 0xff, 0xff, 0x00}}},
400 /* /121 */
401 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
402 0xff, 0xff, 0xff, 0xff, 0x80}}},
403 /* /122 */
404 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
405 0xff, 0xff, 0xff, 0xff, 0xc0}}},
406 /* /123 */
407 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
408 0xff, 0xff, 0xff, 0xff, 0xe0}}},
409 /* /124 */
410 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
411 0xff, 0xff, 0xff, 0xff, 0xf0}}},
412 /* /125 */
413 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
414 0xff, 0xff, 0xff, 0xff, 0xf8}}},
415 /* /126 */
416 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
417 0xff, 0xff, 0xff, 0xff, 0xfc}}},
418 /* /127 */
419 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
420 0xff, 0xff, 0xff, 0xff, 0xfe}}},
421 /* /128 */
422 {{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
423 0xff, 0xff, 0xff, 0xff, 0xff}}}};
718e3744 424
425/* Number of bits in prefix type. */
426#ifndef PNBBY
427#define PNBBY 8
428#endif /* PNBBY */
429
430#define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
431
b7cfce93 432int is_zero_mac(struct ethaddr *mac)
69b61704
MK
433{
434 int i = 0;
435
436 for (i = 0; i < ETH_ALEN; i++) {
437 if (mac->octet[i])
438 return 0;
439 }
440
441 return 1;
442}
443
d62a17ae 444unsigned int prefix_bit(const u_char *prefix, const u_char prefixlen)
f63f06da 445{
d62a17ae 446 unsigned int offset = prefixlen / 8;
447 unsigned int shift = 7 - (prefixlen % 8);
448
449 return (prefix[offset] >> shift) & 1;
f63f06da
PJ
450}
451
d62a17ae 452unsigned int prefix6_bit(const struct in6_addr *prefix, const u_char prefixlen)
f63f06da 453{
d62a17ae 454 return prefix_bit((const u_char *)&prefix->s6_addr, prefixlen);
f63f06da 455}
6b0655a2 456
d62a17ae 457int str2family(const char *string)
f3ccedaa 458{
d62a17ae 459 if (!strcmp("ipv4", string))
460 return AF_INET;
461 else if (!strcmp("ipv6", string))
462 return AF_INET6;
463 else if (!strcmp("ethernet", string))
464 return AF_ETHERNET;
b03b8898
DS
465 else if (!strcmp("evpn", string))
466 return AF_EVPN;
d62a17ae 467 return -1;
f3ccedaa
CF
468}
469
718e3744 470/* Address Famiy Identifier to Address Family converter. */
d62a17ae 471int afi2family(afi_t afi)
718e3744 472{
d62a17ae 473 if (afi == AFI_IP)
474 return AF_INET;
475 else if (afi == AFI_IP6)
476 return AF_INET6;
477 else if (afi == AFI_L2VPN)
478 return AF_ETHERNET;
b03b8898 479 /* NOTE: EVPN code should NOT use this interface. */
d62a17ae 480 return 0;
718e3744 481}
482
d62a17ae 483afi_t family2afi(int family)
718e3744 484{
d62a17ae 485 if (family == AF_INET)
486 return AFI_IP;
487 else if (family == AF_INET6)
488 return AFI_IP6;
b03b8898 489 else if (family == AF_ETHERNET || family == AF_EVPN)
d62a17ae 490 return AFI_L2VPN;
491 return 0;
718e3744 492}
493
d62a17ae 494const char *afi2str(afi_t afi)
32ac65d9 495{
d62a17ae 496 switch (afi) {
497 case AFI_IP:
498 return "IPv4";
499 case AFI_IP6:
500 return "IPv6";
501 case AFI_L2VPN:
502 return "l2vpn";
503 case AFI_MAX:
504 return "bad-value";
505 default:
506 break;
507 }
508 return NULL;
32ac65d9
LB
509}
510
d62a17ae 511const char *safi2str(safi_t safi)
1ec23d90 512{
d62a17ae 513 switch (safi) {
514 case SAFI_UNICAST:
515 return "unicast";
516 case SAFI_MULTICAST:
517 return "multicast";
518 case SAFI_MPLS_VPN:
519 return "vpn";
520 case SAFI_ENCAP:
521 return "encap";
522 case SAFI_EVPN:
523 return "evpn";
524 case SAFI_LABELED_UNICAST:
525 return "labeled-unicast";
5c525538
RW
526 default:
527 return "unknown";
d62a17ae 528 }
1ec23d90
LB
529}
530
718e3744 531/* If n includes p prefix then return 1 else return 0. */
d62a17ae 532int prefix_match(const struct prefix *n, const struct prefix *p)
718e3744 533{
d62a17ae 534 int offset;
535 int shift;
536 const u_char *np, *pp;
537
538 /* If n's prefix is longer than p's one return 0. */
539 if (n->prefixlen > p->prefixlen)
540 return 0;
541
542 /* Set both prefix's head pointer. */
543 np = (const u_char *)&n->u.prefix;
544 pp = (const u_char *)&p->u.prefix;
545
546 offset = n->prefixlen / PNBBY;
547 shift = n->prefixlen % PNBBY;
548
549 if (shift)
550 if (maskbit[shift] & (np[offset] ^ pp[offset]))
551 return 0;
552
553 while (offset--)
554 if (np[offset] != pp[offset])
555 return 0;
556 return 1;
3bec29ac
DS
557}
558
559/* If n includes p then return 1 else return 0. Prefix mask is not considered */
d62a17ae 560int prefix_match_network_statement(const struct prefix *n,
561 const struct prefix *p)
3bec29ac 562{
d62a17ae 563 int offset;
564 int shift;
565 const u_char *np, *pp;
3bec29ac 566
d62a17ae 567 /* Set both prefix's head pointer. */
568 np = (const u_char *)&n->u.prefix;
569 pp = (const u_char *)&p->u.prefix;
3bec29ac 570
d62a17ae 571 offset = n->prefixlen / PNBBY;
572 shift = n->prefixlen % PNBBY;
3bec29ac 573
d62a17ae 574 if (shift)
575 if (maskbit[shift] & (np[offset] ^ pp[offset]))
576 return 0;
3bec29ac 577
d62a17ae 578 while (offset--)
579 if (np[offset] != pp[offset])
580 return 0;
581 return 1;
718e3744 582}
583
584/* Copy prefix from src to dest. */
d62a17ae 585void prefix_copy(struct prefix *dest, const struct prefix *src)
718e3744 586{
d62a17ae 587 dest->family = src->family;
588 dest->prefixlen = src->prefixlen;
589
590 if (src->family == AF_INET)
591 dest->u.prefix4 = src->u.prefix4;
592 else if (src->family == AF_INET6)
593 dest->u.prefix6 = src->u.prefix6;
594 else if (src->family == AF_ETHERNET) {
b03b8898
DS
595 memcpy(&dest->u.prefix_eth, &src->u.prefix_eth,
596 sizeof(struct ethaddr));
597 } else if (src->family == AF_EVPN) {
d62a17ae 598 memcpy(&dest->u.prefix_evpn, &src->u.prefix_evpn,
599 sizeof(struct evpn_addr));
600 } else if (src->family == AF_UNSPEC) {
601 dest->u.lp.id = src->u.lp.id;
602 dest->u.lp.adv_router = src->u.lp.adv_router;
603 } else {
604 zlog_err("prefix_copy(): Unknown address family %d",
605 src->family);
606 assert(0);
607 }
718e3744 608}
609
d62a17ae 610/*
9d24baaa 611 * Return 1 if the address/netmask contained in the prefix structure
612 * is the same, and else return 0. For this routine, 'same' requires
613 * that not only the prefix length and the network part be the same,
614 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
615 * the same. Note that this routine has the same return value sense
616 * as '==' (which is different from prefix_cmp).
617 */
d62a17ae 618int prefix_same(const struct prefix *p1, const struct prefix *p2)
718e3744 619{
d62a17ae 620 if ((p1 && !p2) || (!p1 && p2))
621 return 0;
622
623 if (!p1 && !p2)
624 return 1;
625
626 if (p1->family == p2->family && p1->prefixlen == p2->prefixlen) {
627 if (p1->family == AF_INET)
19aad877 628 if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
d62a17ae 629 return 1;
630 if (p1->family == AF_INET6)
631 if (IPV6_ADDR_SAME(&p1->u.prefix6.s6_addr,
632 &p2->u.prefix6.s6_addr))
633 return 1;
634 if (p1->family == AF_ETHERNET)
b03b8898
DS
635 if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth,
636 sizeof(struct ethaddr)))
637 return 1;
638 if (p1->family == AF_EVPN)
d62a17ae 639 if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn,
640 sizeof(struct evpn_addr)))
641 return 1;
642 }
643 return 0;
718e3744 644}
645
9d24baaa 646/*
647 * Return 0 if the network prefixes represented by the struct prefix
648 * arguments are the same prefix, and 1 otherwise. Network prefixes
649 * are considered the same if the prefix lengths are equal and the
650 * network parts are the same. Host bits (which are considered masked
651 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
652 * 10.0.0.2/8 are considered equivalent by this routine. Note that
653 * this routine has the same return sense as strcmp (which is different
654 * from prefix_same).
655 */
d62a17ae 656int prefix_cmp(const struct prefix *p1, const struct prefix *p2)
718e3744 657{
d62a17ae 658 int offset;
659 int shift;
718e3744 660
d62a17ae 661 /* Set both prefix's head pointer. */
662 const u_char *pp1 = (const u_char *)&p1->u.prefix;
663 const u_char *pp2 = (const u_char *)&p2->u.prefix;
718e3744 664
d62a17ae 665 if (p1->family != p2->family || p1->prefixlen != p2->prefixlen)
666 return 1;
718e3744 667
d62a17ae 668 offset = p1->prefixlen / PNBBY;
669 shift = p1->prefixlen % PNBBY;
718e3744 670
d62a17ae 671 if (shift)
672 if (maskbit[shift] & (pp1[offset] ^ pp2[offset]))
673 return 1;
718e3744 674
d62a17ae 675 while (offset--)
676 if (pp1[offset] != pp2[offset])
677 return 1;
718e3744 678
d62a17ae 679 return 0;
718e3744 680}
681
17e52061
DL
682/*
683 * Count the number of common bits in 2 prefixes. The prefix length is
684 * ignored for this function; the whole prefix is compared. If the prefix
685 * address families don't match, return -1; otherwise the return value is
686 * in range 0 ... maximum prefix length for the address family.
687 */
d62a17ae 688int prefix_common_bits(const struct prefix *p1, const struct prefix *p2)
17e52061 689{
d62a17ae 690 int pos, bit;
691 int length = 0;
692 u_char xor ;
693
694 /* Set both prefix's head pointer. */
695 const u_char *pp1 = (const u_char *)&p1->u.prefix;
696 const u_char *pp2 = (const u_char *)&p2->u.prefix;
697
698 if (p1->family == AF_INET)
699 length = IPV4_MAX_BYTELEN;
700 if (p1->family == AF_INET6)
701 length = IPV6_MAX_BYTELEN;
702 if (p1->family == AF_ETHERNET)
b03b8898
DS
703 length = ETH_ALEN;
704 if (p1->family == AF_EVPN)
d62a17ae 705 length = 8 * sizeof(struct evpn_addr);
706
707 if (p1->family != p2->family || !length)
708 return -1;
709
710 for (pos = 0; pos < length; pos++)
711 if (pp1[pos] != pp2[pos])
712 break;
713 if (pos == length)
714 return pos * 8;
715
716 xor = pp1[pos] ^ pp2[pos];
717 for (bit = 0; bit < 8; bit++)
718 if (xor&(1 << (7 - bit)))
719 break;
720
721 return pos * 8 + bit;
17e52061
DL
722}
723
718e3744 724/* Return prefix family type string. */
d62a17ae 725const char *prefix_family_str(const struct prefix *p)
718e3744 726{
d62a17ae 727 if (p->family == AF_INET)
728 return "inet";
729 if (p->family == AF_INET6)
730 return "inet6";
731 if (p->family == AF_ETHERNET)
732 return "ether";
b03b8898
DS
733 if (p->family == AF_EVPN)
734 return "evpn";
d62a17ae 735 return "unspec";
718e3744 736}
737
738/* Allocate new prefix_ipv4 structure. */
d62a17ae 739struct prefix_ipv4 *prefix_ipv4_new()
718e3744 740{
d62a17ae 741 struct prefix_ipv4 *p;
742
743 /* Call prefix_new to allocate a full-size struct prefix to avoid
744 problems
745 where the struct prefix_ipv4 is cast to struct prefix and unallocated
746 bytes were being referenced (e.g. in structure assignments). */
747 p = (struct prefix_ipv4 *)prefix_new();
748 p->family = AF_INET;
749 return p;
718e3744 750}
751
752/* Free prefix_ipv4 structure. */
d62a17ae 753void prefix_ipv4_free(struct prefix_ipv4 *p)
718e3744 754{
d62a17ae 755 prefix_free((struct prefix *)p);
718e3744 756}
757
758/* When string format is invalid return 0. */
d62a17ae 759int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p)
718e3744 760{
d62a17ae 761 int ret;
762 int plen;
763 char *pnt;
764 char *cp;
765
766 /* Find slash inside string. */
767 pnt = strchr(str, '/');
768
769 /* String doesn't contail slash. */
770 if (pnt == NULL) {
771 /* Convert string to prefix. */
772 ret = inet_aton(str, &p->prefix);
773 if (ret == 0)
774 return 0;
775
776 /* If address doesn't contain slash we assume it host address.
777 */
778 p->family = AF_INET;
779 p->prefixlen = IPV4_MAX_BITLEN;
780
781 return ret;
782 } else {
783 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
784 strncpy(cp, str, pnt - str);
785 *(cp + (pnt - str)) = '\0';
786 ret = inet_aton(cp, &p->prefix);
787 XFREE(MTYPE_TMP, cp);
788
789 /* Get prefix length. */
790 plen = (u_char)atoi(++pnt);
791 if (plen > IPV4_MAX_PREFIXLEN)
792 return 0;
793
794 p->family = AF_INET;
795 p->prefixlen = plen;
796 }
718e3744 797
d62a17ae 798 return ret;
718e3744 799}
800
32ac65d9 801/* When string format is invalid return 0. */
d62a17ae 802int str2prefix_eth(const char *str, struct prefix_eth *p)
32ac65d9 803{
d62a17ae 804 int ret = 0;
805 int plen = 48;
806 char *pnt;
807 char *cp = NULL;
808 const char *str_addr = str;
809 unsigned int a[6];
810 int i;
0f6476cc 811 bool slash = false;
d62a17ae 812
3b0f6068
DL
813 if (!strcmp(str, "any")) {
814 memset(p, 0, sizeof(*p));
815 p->family = AF_ETHERNET;
816 return 1;
817 }
818
d62a17ae 819 /* Find slash inside string. */
820 pnt = strchr(str, '/');
821
822 if (pnt) {
823 /* Get prefix length. */
824 plen = (u_char)atoi(++pnt);
825 if (plen > 48) {
826 ret = 0;
827 goto done;
828 }
829
830 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
831 strncpy(cp, str, pnt - str);
832 *(cp + (pnt - str)) = '\0';
833
834 str_addr = cp;
0f6476cc 835 slash = true;
32ac65d9
LB
836 }
837
d62a17ae 838 /* Convert string to prefix. */
839 if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2,
840 a + 3, a + 4, a + 5)
841 != 6) {
842 ret = 0;
843 goto done;
844 }
845 for (i = 0; i < 6; ++i) {
846 p->eth_addr.octet[i] = a[i] & 0xff;
847 }
848 p->prefixlen = plen;
849 p->family = AF_ETHERNET;
0f6476cc
DS
850
851 /*
852 * special case to allow old configurations to work
853 * Since all zero's is implicitly meant to allow
854 * a comparison to zero, let's assume
855 */
856 if (!slash && is_zero_mac(&(p->eth_addr)))
996c9314 857 p->prefixlen = 0;
0f6476cc 858
d62a17ae 859 ret = 1;
32ac65d9
LB
860
861done:
d62a17ae 862 if (cp)
863 XFREE(MTYPE_TMP, cp);
32ac65d9 864
d62a17ae 865 return ret;
32ac65d9
LB
866}
867
051954f5 868/* Convert masklen into IP address's netmask (network byte order). */
d62a17ae 869void masklen2ip(const int masklen, struct in_addr *netmask)
718e3744 870{
d62a17ae 871 assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
e96b3121 872
d62a17ae 873 /* left shift is only defined for less than the size of the type.
874 * we unconditionally use long long in case the target platform
875 * has defined behaviour for << 32 (or has a 64-bit left shift) */
e96b3121 876
d62a17ae 877 if (sizeof(unsigned long long) > 4)
878 netmask->s_addr = htonl(0xffffffffULL << (32 - masklen));
879 else
880 netmask->s_addr =
881 htonl(masklen ? 0xffffffffU << (32 - masklen) : 0);
718e3744 882}
883
884/* Convert IP address's netmask into integer. We assume netmask is
885 sequential one. Argument netmask should be network byte order. */
d62a17ae 886u_char ip_masklen(struct in_addr netmask)
718e3744 887{
d62a17ae 888 uint32_t tmp = ~ntohl(netmask.s_addr);
889 if (tmp)
890 /* clz: count leading zeroes. sadly, the behaviour of this
891 * builtin
892 * is undefined for a 0 argument, even though most CPUs give 32
893 */
894 return __builtin_clz(tmp);
895 else
896 return 32;
718e3744 897}
898
caff7905 899/* Apply mask to IPv4 prefix (network byte order). */
d62a17ae 900void apply_mask_ipv4(struct prefix_ipv4 *p)
718e3744 901{
d62a17ae 902 struct in_addr mask;
903 masklen2ip(p->prefixlen, &mask);
904 p->prefix.s_addr &= mask.s_addr;
718e3744 905}
906
907/* If prefix is 0.0.0.0/0 then return 1 else return 0. */
d62a17ae 908int prefix_ipv4_any(const struct prefix_ipv4 *p)
718e3744 909{
d62a17ae 910 return (p->prefix.s_addr == 0 && p->prefixlen == 0);
718e3744 911}
6b0655a2 912
718e3744 913/* Allocate a new ip version 6 route */
d62a17ae 914struct prefix_ipv6 *prefix_ipv6_new(void)
718e3744 915{
d62a17ae 916 struct prefix_ipv6 *p;
718e3744 917
d62a17ae 918 /* Allocate a full-size struct prefix to avoid problems with structure
919 size mismatches. */
920 p = (struct prefix_ipv6 *)prefix_new();
921 p->family = AF_INET6;
922 return p;
718e3744 923}
924
925/* Free prefix for IPv6. */
d62a17ae 926void prefix_ipv6_free(struct prefix_ipv6 *p)
718e3744 927{
d62a17ae 928 prefix_free((struct prefix *)p);
718e3744 929}
930
931/* If given string is valid return pin6 else return NULL */
d62a17ae 932int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p)
718e3744 933{
d62a17ae 934 char *pnt;
935 char *cp;
936 int ret;
937
938 pnt = strchr(str, '/');
939
940 /* If string doesn't contain `/' treat it as host route. */
941 if (pnt == NULL) {
942 ret = inet_pton(AF_INET6, str, &p->prefix);
943 if (ret == 0)
944 return 0;
945 p->prefixlen = IPV6_MAX_BITLEN;
946 } else {
947 int plen;
948
949 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
950 strncpy(cp, str, pnt - str);
951 *(cp + (pnt - str)) = '\0';
952 ret = inet_pton(AF_INET6, cp, &p->prefix);
953 XFREE(MTYPE_TMP, cp);
954 if (ret == 0)
955 return 0;
956 plen = (u_char)atoi(++pnt);
957 if (plen > IPV6_MAX_BITLEN)
958 return 0;
959 p->prefixlen = plen;
960 }
961 p->family = AF_INET6;
718e3744 962
d62a17ae 963 return ret;
718e3744 964}
965
b04c699e 966/* Convert struct in6_addr netmask into integer.
967 * FIXME return u_char as ip_maskleni() does. */
d62a17ae 968int ip6_masklen(struct in6_addr netmask)
718e3744 969{
d62a17ae 970 int len = 0;
971 unsigned char val;
972 unsigned char *pnt;
973
974 pnt = (unsigned char *)&netmask;
975
976 while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN) {
977 len += 8;
978 pnt++;
718e3744 979 }
d62a17ae 980
981 if (len < IPV6_MAX_BITLEN) {
982 val = *pnt;
983 while (val) {
984 len++;
985 val <<= 1;
986 }
987 }
988 return len;
718e3744 989}
990
d62a17ae 991void masklen2ip6(const int masklen, struct in6_addr *netmask)
718e3744 992{
d62a17ae 993 assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
994 memcpy(netmask, maskbytes6 + masklen, sizeof(struct in6_addr));
718e3744 995}
996
d62a17ae 997void apply_mask_ipv6(struct prefix_ipv6 *p)
718e3744 998{
d62a17ae 999 u_char *pnt;
1000 int index;
1001 int offset;
8c7f49d2 1002
d62a17ae 1003 index = p->prefixlen / 8;
8c7f49d2 1004
d62a17ae 1005 if (index < 16) {
1006 pnt = (u_char *)&p->prefix;
1007 offset = p->prefixlen % 8;
8c7f49d2 1008
d62a17ae 1009 pnt[index] &= maskbit[offset];
1010 index++;
8c7f49d2 1011
d62a17ae 1012 while (index < 16)
1013 pnt[index++] = 0;
1014 }
718e3744 1015}
1016
d62a17ae 1017void apply_mask(struct prefix *p)
718e3744 1018{
d62a17ae 1019 switch (p->family) {
1020 case AF_INET:
1021 apply_mask_ipv4((struct prefix_ipv4 *)p);
1022 break;
1023 case AF_INET6:
1024 apply_mask_ipv6((struct prefix_ipv6 *)p);
1025 break;
1026 default:
1027 break;
1028 }
1029 return;
718e3744 1030}
1031
b04c699e 1032/* Utility function of convert between struct prefix <=> union sockunion.
1033 * FIXME This function isn't used anywhere. */
d62a17ae 1034struct prefix *sockunion2prefix(const union sockunion *dest,
1035 const union sockunion *mask)
718e3744 1036{
d62a17ae 1037 if (dest->sa.sa_family == AF_INET) {
1038 struct prefix_ipv4 *p;
1039
1040 p = prefix_ipv4_new();
1041 p->family = AF_INET;
1042 p->prefix = dest->sin.sin_addr;
1043 p->prefixlen = ip_masklen(mask->sin.sin_addr);
1044 return (struct prefix *)p;
1045 }
1046 if (dest->sa.sa_family == AF_INET6) {
1047 struct prefix_ipv6 *p;
1048
1049 p = prefix_ipv6_new();
1050 p->family = AF_INET6;
1051 p->prefixlen = ip6_masklen(mask->sin6.sin6_addr);
1052 memcpy(&p->prefix, &dest->sin6.sin6_addr,
1053 sizeof(struct in6_addr));
1054 return (struct prefix *)p;
1055 }
1056 return NULL;
718e3744 1057}
1058
b04c699e 1059/* Utility function of convert between struct prefix <=> union sockunion. */
d62a17ae 1060struct prefix *sockunion2hostprefix(const union sockunion *su,
1061 struct prefix *prefix)
718e3744 1062{
d62a17ae 1063 if (su->sa.sa_family == AF_INET) {
1064 struct prefix_ipv4 *p;
1065
1066 p = prefix ? (struct prefix_ipv4 *)prefix : prefix_ipv4_new();
1067 p->family = AF_INET;
1068 p->prefix = su->sin.sin_addr;
1069 p->prefixlen = IPV4_MAX_BITLEN;
1070 return (struct prefix *)p;
1071 }
1072 if (su->sa.sa_family == AF_INET6) {
1073 struct prefix_ipv6 *p;
1074
1075 p = prefix ? (struct prefix_ipv6 *)prefix : prefix_ipv6_new();
1076 p->family = AF_INET6;
1077 p->prefixlen = IPV6_MAX_BITLEN;
1078 memcpy(&p->prefix, &su->sin6.sin6_addr,
1079 sizeof(struct in6_addr));
1080 return (struct prefix *)p;
1081 }
1082 return NULL;
718e3744 1083}
1084
d62a17ae 1085void prefix2sockunion(const struct prefix *p, union sockunion *su)
17e52061 1086{
d62a17ae 1087 memset(su, 0, sizeof(*su));
1088
1089 su->sa.sa_family = p->family;
1090 if (p->family == AF_INET)
1091 su->sin.sin_addr = p->u.prefix4;
1092 if (p->family == AF_INET6)
1093 memcpy(&su->sin6.sin6_addr, &p->u.prefix6,
1094 sizeof(struct in6_addr));
17e52061
DL
1095}
1096
d62a17ae 1097int prefix_blen(const struct prefix *p)
718e3744 1098{
d62a17ae 1099 switch (p->family) {
1100 case AF_INET:
1101 return IPV4_MAX_BYTELEN;
1102 break;
1103 case AF_INET6:
1104 return IPV6_MAX_BYTELEN;
1105 break;
1106 case AF_ETHERNET:
7628d862 1107 return ETH_ALEN;
b03b8898 1108 break;
d62a17ae 1109 }
1110 return 0;
718e3744 1111}
1112
1113/* Generic function for conversion string to struct prefix. */
d62a17ae 1114int str2prefix(const char *str, struct prefix *p)
718e3744 1115{
d62a17ae 1116 int ret;
718e3744 1117
d62a17ae 1118 /* First we try to convert string to struct prefix_ipv4. */
1119 ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p);
1120 if (ret)
1121 return ret;
718e3744 1122
d62a17ae 1123 /* Next we try to convert string to struct prefix_ipv6. */
1124 ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p);
1125 if (ret)
1126 return ret;
718e3744 1127
d62a17ae 1128 /* Next we try to convert string to struct prefix_eth. */
1129 ret = str2prefix_eth(str, (struct prefix_eth *)p);
1130 if (ret)
1131 return ret;
32ac65d9 1132
d62a17ae 1133 return 0;
718e3744 1134}
1135
b03b8898 1136static const char *prefixevpn2str(const struct prefix *p, char *str, int size)
86f1ef44 1137{
d62a17ae 1138 u_char family;
1139 char buf[PREFIX2STR_BUFFER];
1140 char buf2[ETHER_ADDR_STRLEN];
1141
1142 if (p->u.prefix_evpn.route_type == 2) {
1143 if (IS_EVPN_PREFIX_IPADDR_NONE((struct prefix_evpn *)p))
1144 snprintf(str, size, "[%d]:[%s]/%d",
1145 p->u.prefix_evpn.route_type,
1146 prefix_mac2str(&p->u.prefix_evpn.mac, buf2,
1147 sizeof(buf2)),
1148 p->prefixlen);
1149 else {
1150 family = IS_EVPN_PREFIX_IPADDR_V4(
1151 (struct prefix_evpn *)p)
1152 ? AF_INET
1153 : AF_INET6;
1154 snprintf(str, size, "[%d]:[%s]:[%s]/%d",
1155 p->u.prefix_evpn.route_type,
1156 prefix_mac2str(&p->u.prefix_evpn.mac, buf2,
1157 sizeof(buf2)),
1158 inet_ntop(family, &p->u.prefix_evpn.ip.ip.addr,
1159 buf, PREFIX2STR_BUFFER),
1160 p->prefixlen);
1161 }
1162 } else if (p->u.prefix_evpn.route_type == 3) {
1163 family = IS_EVPN_PREFIX_IPADDR_V4((struct prefix_evpn *)p)
1164 ? AF_INET
1165 : AF_INET6;
1166 snprintf(str, size, "[%d]:[%s]/%d", p->u.prefix_evpn.route_type,
1167 inet_ntop(family, &p->u.prefix_evpn.ip.ip.addr, buf,
1168 PREFIX2STR_BUFFER),
1169 p->prefixlen);
1170 } else if (p->u.prefix_evpn.route_type == 5) {
1171 family = IS_EVPN_PREFIX_IPADDR_V4((struct prefix_evpn *)p)
1172 ? AF_INET
1173 : AF_INET6;
342dd0c6 1174 snprintf(str, size, "[%d]:[%u][%s/%d]/%d",
d62a17ae 1175 p->u.prefix_evpn.route_type, p->u.prefix_evpn.eth_tag,
1176 inet_ntop(family, &p->u.prefix_evpn.ip.ip.addr, buf,
1177 PREFIX2STR_BUFFER),
996c9314 1178 p->u.prefix_evpn.ip_prefix_length, p->prefixlen);
d62a17ae 1179 } else {
b03b8898 1180 sprintf(str, "Unsupported EVPN route type %d",
996c9314 1181 p->u.prefix_evpn.route_type);
d62a17ae 1182 }
1183
1184 return str;
86f1ef44 1185}
1186
d62a17ae 1187const char *prefix2str(union prefixconstptr pu, char *str, int size)
718e3744 1188{
d62a17ae 1189 const struct prefix *p = pu.p;
1190 char buf[PREFIX2STR_BUFFER];
1191
1192 switch (p->family) {
1193 case AF_INET:
1194 case AF_INET6:
9d303b37
DL
1195 snprintf(str, size, "%s/%d", inet_ntop(p->family, &p->u.prefix,
1196 buf, PREFIX2STR_BUFFER),
d62a17ae 1197 p->prefixlen);
1198 break;
1199
1200 case AF_ETHERNET:
b03b8898
DS
1201 snprintf(str, size, "%s/%d",
1202 prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)),
1203 p->prefixlen);
1204 break;
1205
1206 case AF_EVPN:
1207 prefixevpn2str(p, str, size);
d62a17ae 1208 break;
1209
1210 default:
1211 sprintf(str, "UNK prefix");
1212 break;
1213 }
1214
1215 return str;
718e3744 1216}
1217
d62a17ae 1218struct prefix *prefix_new()
718e3744 1219{
d62a17ae 1220 struct prefix *p;
718e3744 1221
d62a17ae 1222 p = XCALLOC(MTYPE_PREFIX, sizeof *p);
1223 return p;
718e3744 1224}
1225
1226/* Free prefix structure. */
d62a17ae 1227void prefix_free(struct prefix *p)
718e3744 1228{
d62a17ae 1229 XFREE(MTYPE_PREFIX, p);
718e3744 1230}
1231
1232/* Utility function. Check the string only contains digit
b04c699e 1233 * character.
1234 * FIXME str.[c|h] would be better place for this function. */
d62a17ae 1235int all_digit(const char *str)
718e3744 1236{
d62a17ae 1237 for (; *str != '\0'; str++)
1238 if (!isdigit((int)*str))
1239 return 0;
1240 return 1;
718e3744 1241}
1242
1243/* Utility function to convert ipv4 prefixes to Classful prefixes */
d62a17ae 1244void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
718e3744 1245{
1246
d62a17ae 1247 u_int32_t destination;
1248
1249 destination = ntohl(p->prefix.s_addr);
1250
1251 if (p->prefixlen == IPV4_MAX_PREFIXLEN)
1252 ;
1253 /* do nothing for host routes */
1254 else if (IN_CLASSC(destination)) {
1255 p->prefixlen = 24;
1256 apply_mask_ipv4(p);
1257 } else if (IN_CLASSB(destination)) {
1258 p->prefixlen = 16;
1259 apply_mask_ipv4(p);
1260 } else {
1261 p->prefixlen = 8;
1262 apply_mask_ipv4(p);
1263 }
718e3744 1264}
1265
d62a17ae 1266in_addr_t ipv4_network_addr(in_addr_t hostaddr, int masklen)
3fb9cd6e 1267{
d62a17ae 1268 struct in_addr mask;
3fb9cd6e 1269
d62a17ae 1270 masklen2ip(masklen, &mask);
1271 return hostaddr & mask.s_addr;
3fb9cd6e 1272}
1273
d62a17ae 1274in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
3fb9cd6e 1275{
d62a17ae 1276 struct in_addr mask;
1277
1278 masklen2ip(masklen, &mask);
1279 return (masklen != IPV4_MAX_PREFIXLEN - 1) ?
1280 /* normal case */
1281 (hostaddr | ~mask.s_addr)
1282 :
1283 /* special case for /31 */
1284 (hostaddr ^ ~mask.s_addr);
3fb9cd6e 1285}
1286
d62a17ae 1287/* Utility function to convert ipv4 netmask to prefixes
718e3744 1288 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1289 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
d62a17ae 1290int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1291 char *prefix_str)
718e3744 1292{
d62a17ae 1293 struct in_addr network;
1294 struct in_addr mask;
1295 u_char prefixlen;
1296 u_int32_t destination;
1297 int ret;
1298
1299 ret = inet_aton(net_str, &network);
1300 if (!ret)
1301 return 0;
1302
1303 if (mask_str) {
1304 ret = inet_aton(mask_str, &mask);
1305 if (!ret)
1306 return 0;
1307
1308 prefixlen = ip_masklen(mask);
1309 } else {
1310 destination = ntohl(network.s_addr);
1311
1312 if (network.s_addr == 0)
1313 prefixlen = 0;
1314 else if (IN_CLASSC(destination))
1315 prefixlen = 24;
1316 else if (IN_CLASSB(destination))
1317 prefixlen = 16;
1318 else if (IN_CLASSA(destination))
1319 prefixlen = 8;
1320 else
1321 return 0;
1322 }
718e3744 1323
d62a17ae 1324 sprintf(prefix_str, "%s/%d", net_str, prefixlen);
718e3744 1325
d62a17ae 1326 return 1;
718e3744 1327}
1328
5920990f 1329/* Utility function for making IPv6 address string. */
d62a17ae 1330const char *inet6_ntoa(struct in6_addr addr)
5920990f 1331{
d62a17ae 1332 static char buf[INET6_ADDRSTRLEN];
5920990f 1333
d62a17ae 1334 inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1335 return buf;
5920990f 1336}
c215ecaf 1337
c215ecaf 1338/* converts to internal representation of mac address
d62a17ae 1339 * returns 1 on success, 0 otherwise
c215ecaf
PG
1340 * format accepted: AA:BB:CC:DD:EE:FF
1341 * if mac parameter is null, then check only
1342 */
db42a173 1343int prefix_str2mac(const char *str, struct ethaddr *mac)
c215ecaf 1344{
d62a17ae 1345 unsigned int a[6];
1346 int i;
1347
1348 if (!str)
1349 return 0;
1350
1351 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3,
1352 a + 4, a + 5)
1353 != 6) {
1354 /* error in incoming str length */
1355 return 0;
1356 }
1357 /* valid mac address */
1358 if (!mac)
1359 return 1;
1360 for (i = 0; i < 6; ++i)
1361 mac->octet[i] = a[i] & 0xff;
1362 return 1;
c215ecaf
PG
1363}
1364
db42a173 1365char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
c215ecaf 1366{
d62a17ae 1367 char *ptr;
1368
1369 if (!mac)
1370 return NULL;
1371 if (!buf)
1372 ptr = (char *)XMALLOC(MTYPE_TMP,
1373 ETHER_ADDR_STRLEN * sizeof(char));
1374 else {
1375 assert(size >= ETHER_ADDR_STRLEN);
1376 ptr = buf;
1377 }
1378 snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x",
1379 (uint8_t)mac->octet[0], (uint8_t)mac->octet[1],
1380 (uint8_t)mac->octet[2], (uint8_t)mac->octet[3],
1381 (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]);
1382 return ptr;
c215ecaf 1383}
7a7761d2
CF
1384
1385unsigned prefix_hash_key(void *pp)
1386{
1387 struct prefix copy;
1388
1389 /* make sure *all* unused bits are zero, particularly including
1390 * alignment /
1391 * padding and unused prefix bytes. */
1392 memset(&copy, 0, sizeof(copy));
1393 prefix_copy(&copy, (struct prefix *)pp);
996c9314
LB
1394 return jhash(&copy,
1395 offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen),
1396 0x55aa5a5a);
7a7761d2 1397}