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