]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
Merge pull request #5128 from chiragshah6/yang_dev
[mirror_frr.git] / lib / prefix.c
1 /*
2 * Prefix related functions.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "prefix.h"
25 #include "vty.h"
26 #include "sockunion.h"
27 #include "memory.h"
28 #include "log.h"
29 #include "jhash.h"
30 #include "lib_errors.h"
31 #include "printfrr.h"
32
33 DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix")
34 DEFINE_MTYPE_STATIC(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec")
35
36 /* Maskbit. */
37 static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
38 0xf8, 0xfc, 0xfe, 0xff};
39
40 static const struct in6_addr maskbytes6[] = {
41 /* /0 */ {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}},
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}}}};
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
435 void prefix_hexdump(const struct prefix *p)
436 {
437 char buf[PREFIX_STRLEN];
438
439 zlog_debug("prefix: %s",
440 prefix2str(p, buf, sizeof(buf)));
441 zlog_hexdump(p, sizeof(struct prefix));
442 }
443
444 int is_zero_mac(const struct ethaddr *mac)
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
456 unsigned int prefix_bit(const uint8_t *prefix, const uint16_t prefixlen)
457 {
458 unsigned int offset = prefixlen / 8;
459 unsigned int shift = 7 - (prefixlen % 8);
460
461 return (prefix[offset] >> shift) & 1;
462 }
463
464 unsigned int prefix6_bit(const struct in6_addr *prefix, const uint16_t prefixlen)
465 {
466 return prefix_bit((const uint8_t *)&prefix->s6_addr, prefixlen);
467 }
468
469 int str2family(const char *string)
470 {
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;
477 else if (!strcmp("evpn", string))
478 return AF_EVPN;
479 return -1;
480 }
481
482 const 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
497 /* Address Famiy Identifier to Address Family converter. */
498 int afi2family(afi_t afi)
499 {
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;
506 /* NOTE: EVPN code should NOT use this interface. */
507 return 0;
508 }
509
510 afi_t family2afi(int family)
511 {
512 if (family == AF_INET)
513 return AFI_IP;
514 else if (family == AF_INET6)
515 return AFI_IP6;
516 else if (family == AF_ETHERNET || family == AF_EVPN)
517 return AFI_L2VPN;
518 return 0;
519 }
520
521 const char *afi2str(afi_t afi)
522 {
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;
536 }
537
538 const char *safi2str(safi_t safi)
539 {
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";
553 case SAFI_FLOWSPEC:
554 return "flowspec";
555 default:
556 return "unknown";
557 }
558 }
559
560 /* If n includes p prefix then return 1 else return 0. */
561 int prefix_match(const struct prefix *n, const struct prefix *p)
562 {
563 int offset;
564 int shift;
565 const uint8_t *np, *pp;
566
567 /* If n's prefix is longer than p's one return 0. */
568 if (n->prefixlen > p->prefixlen)
569 return 0;
570
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
589 /* Set both prefix's head pointer. */
590 np = n->u.val;
591 pp = p->u.val;
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;
604 }
605
606 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
607 int prefix_match_network_statement(const struct prefix *n,
608 const struct prefix *p)
609 {
610 int offset;
611 int shift;
612 const uint8_t *np, *pp;
613
614 /* Set both prefix's head pointer. */
615 np = n->u.val;
616 pp = p->u.val;
617
618 offset = n->prefixlen / PNBBY;
619 shift = n->prefixlen % PNBBY;
620
621 if (shift)
622 if (maskbit[shift] & (np[offset] ^ pp[offset]))
623 return 0;
624
625 while (offset--)
626 if (np[offset] != pp[offset])
627 return 0;
628 return 1;
629 }
630
631 #ifdef __clang_analyzer__
632 #undef prefix_copy /* cf. prefix.h */
633 #endif
634
635 void prefix_copy(union prefixptr udest, union prefixconstptr usrc)
636 {
637 struct prefix *dest = udest.p;
638 const struct prefix *src = usrc.p;
639
640 dest->family = src->family;
641 dest->prefixlen = src->prefixlen;
642
643 if (src->family == AF_INET)
644 dest->u.prefix4 = src->u.prefix4;
645 else if (src->family == AF_INET6)
646 dest->u.prefix6 = src->u.prefix6;
647 else if (src->family == AF_ETHERNET) {
648 memcpy(&dest->u.prefix_eth, &src->u.prefix_eth,
649 sizeof(struct ethaddr));
650 } else if (src->family == AF_EVPN) {
651 memcpy(&dest->u.prefix_evpn, &src->u.prefix_evpn,
652 sizeof(struct evpn_addr));
653 } else if (src->family == AF_UNSPEC) {
654 dest->u.lp.id = src->u.lp.id;
655 dest->u.lp.adv_router = src->u.lp.adv_router;
656 } else if (src->family == AF_FLOWSPEC) {
657 void *temp;
658 int len;
659
660 len = src->u.prefix_flowspec.prefixlen;
661 dest->u.prefix_flowspec.prefixlen =
662 src->u.prefix_flowspec.prefixlen;
663 dest->family = src->family;
664 temp = XCALLOC(MTYPE_PREFIX_FLOWSPEC, len);
665 dest->u.prefix_flowspec.ptr = (uintptr_t)temp;
666 memcpy((void *)dest->u.prefix_flowspec.ptr,
667 (void *)src->u.prefix_flowspec.ptr, len);
668 } else {
669 flog_err(EC_LIB_DEVELOPMENT,
670 "prefix_copy(): Unknown address family %d",
671 src->family);
672 assert(0);
673 }
674 }
675
676 /*
677 * Return 1 if the address/netmask contained in the prefix structure
678 * is the same, and else return 0. For this routine, 'same' requires
679 * that not only the prefix length and the network part be the same,
680 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
681 * the same. Note that this routine has the same return value sense
682 * as '==' (which is different from prefix_cmp).
683 */
684 int prefix_same(union prefixconstptr up1, union prefixconstptr up2)
685 {
686 const struct prefix *p1 = up1.p;
687 const struct prefix *p2 = up2.p;
688
689 if ((p1 && !p2) || (!p1 && p2))
690 return 0;
691
692 if (!p1 && !p2)
693 return 1;
694
695 if (p1->family == p2->family && p1->prefixlen == p2->prefixlen) {
696 if (p1->family == AF_INET)
697 if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
698 return 1;
699 if (p1->family == AF_INET6)
700 if (IPV6_ADDR_SAME(&p1->u.prefix6.s6_addr,
701 &p2->u.prefix6.s6_addr))
702 return 1;
703 if (p1->family == AF_ETHERNET)
704 if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth,
705 sizeof(struct ethaddr)))
706 return 1;
707 if (p1->family == AF_EVPN)
708 if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn,
709 sizeof(struct evpn_addr)))
710 return 1;
711 if (p1->family == AF_FLOWSPEC) {
712 if (p1->u.prefix_flowspec.prefixlen !=
713 p2->u.prefix_flowspec.prefixlen)
714 return 0;
715 if (!memcmp(&p1->u.prefix_flowspec.ptr,
716 &p2->u.prefix_flowspec.ptr,
717 p2->u.prefix_flowspec.prefixlen))
718 return 1;
719 }
720 }
721 return 0;
722 }
723
724 /*
725 * Return -1/0/1 comparing the prefixes in a way that gives a full/linear
726 * order.
727 *
728 * Network prefixes are considered the same if the prefix lengths are equal
729 * and the network parts are the same. Host bits (which are considered masked
730 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
731 * 10.0.0.2/8 are considered equivalent by this routine. Note that
732 * this routine has the same return sense as strcmp (which is different
733 * from prefix_same).
734 */
735 int prefix_cmp(union prefixconstptr up1, union prefixconstptr up2)
736 {
737 const struct prefix *p1 = up1.p;
738 const struct prefix *p2 = up2.p;
739 int offset;
740 int shift;
741 int i;
742
743 /* Set both prefix's head pointer. */
744 const uint8_t *pp1;
745 const uint8_t *pp2;
746
747 if (p1->family != p2->family)
748 return numcmp(p1->family, p2->family);
749 if (p1->family == AF_FLOWSPEC) {
750 pp1 = (const uint8_t *)p1->u.prefix_flowspec.ptr;
751 pp2 = (const uint8_t *)p2->u.prefix_flowspec.ptr;
752
753 if (p1->u.prefix_flowspec.prefixlen !=
754 p2->u.prefix_flowspec.prefixlen)
755 return numcmp(p1->u.prefix_flowspec.prefixlen,
756 p2->u.prefix_flowspec.prefixlen);
757
758 offset = p1->u.prefix_flowspec.prefixlen;
759 while (offset--)
760 if (pp1[offset] != pp2[offset])
761 return numcmp(pp1[offset], pp2[offset]);
762 return 0;
763 }
764 pp1 = p1->u.val;
765 pp2 = p2->u.val;
766
767 if (p1->prefixlen != p2->prefixlen)
768 return numcmp(p1->prefixlen, p2->prefixlen);
769 offset = p1->prefixlen / PNBBY;
770 shift = p1->prefixlen % PNBBY;
771
772 i = memcmp(pp1, pp2, offset);
773 if (i)
774 return i;
775
776 /*
777 * At this point offset was the same, if we have shift
778 * that means we still have data to compare, if shift is
779 * 0 then we are at the end of the data structure
780 * and should just return, as that we will be accessing
781 * memory beyond the end of the party zone
782 */
783 if (shift)
784 return numcmp(pp1[offset] & maskbit[shift],
785 pp2[offset] & maskbit[shift]);
786
787 return 0;
788 }
789
790 /*
791 * Count the number of common bits in 2 prefixes. The prefix length is
792 * ignored for this function; the whole prefix is compared. If the prefix
793 * address families don't match, return -1; otherwise the return value is
794 * in range 0 ... maximum prefix length for the address family.
795 */
796 int prefix_common_bits(const struct prefix *p1, const struct prefix *p2)
797 {
798 int pos, bit;
799 int length = 0;
800 uint8_t xor ;
801
802 /* Set both prefix's head pointer. */
803 const uint8_t *pp1 = p1->u.val;
804 const uint8_t *pp2 = p2->u.val;
805
806 if (p1->family == AF_INET)
807 length = IPV4_MAX_BYTELEN;
808 if (p1->family == AF_INET6)
809 length = IPV6_MAX_BYTELEN;
810 if (p1->family == AF_ETHERNET)
811 length = ETH_ALEN;
812 if (p1->family == AF_EVPN)
813 length = 8 * sizeof(struct evpn_addr);
814
815 if (p1->family != p2->family || !length)
816 return -1;
817
818 for (pos = 0; pos < length; pos++)
819 if (pp1[pos] != pp2[pos])
820 break;
821 if (pos == length)
822 return pos * 8;
823
824 xor = pp1[pos] ^ pp2[pos];
825 for (bit = 0; bit < 8; bit++)
826 if (xor&(1 << (7 - bit)))
827 break;
828
829 return pos * 8 + bit;
830 }
831
832 /* Return prefix family type string. */
833 const char *prefix_family_str(const struct prefix *p)
834 {
835 if (p->family == AF_INET)
836 return "inet";
837 if (p->family == AF_INET6)
838 return "inet6";
839 if (p->family == AF_ETHERNET)
840 return "ether";
841 if (p->family == AF_EVPN)
842 return "evpn";
843 return "unspec";
844 }
845
846 /* Allocate new prefix_ipv4 structure. */
847 struct prefix_ipv4 *prefix_ipv4_new(void)
848 {
849 struct prefix_ipv4 *p;
850
851 /* Call prefix_new to allocate a full-size struct prefix to avoid
852 problems
853 where the struct prefix_ipv4 is cast to struct prefix and unallocated
854 bytes were being referenced (e.g. in structure assignments). */
855 p = (struct prefix_ipv4 *)prefix_new();
856 p->family = AF_INET;
857 return p;
858 }
859
860 /* Free prefix_ipv4 structure. */
861 void prefix_ipv4_free(struct prefix_ipv4 *p)
862 {
863 prefix_free((struct prefix *)p);
864 }
865
866 /* If given string is valid return 1 else return 0 */
867 int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p)
868 {
869 int ret;
870 int plen;
871 char *pnt;
872 char *cp;
873
874 /* Find slash inside string. */
875 pnt = strchr(str, '/');
876
877 /* String doesn't contail slash. */
878 if (pnt == NULL) {
879 /* Convert string to prefix. */
880 ret = inet_pton(AF_INET, str, &p->prefix);
881 if (ret == 0)
882 return 0;
883
884 /* If address doesn't contain slash we assume it host address.
885 */
886 p->family = AF_INET;
887 p->prefixlen = IPV4_MAX_BITLEN;
888
889 return ret;
890 } else {
891 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
892 memcpy(cp, str, pnt - str);
893 *(cp + (pnt - str)) = '\0';
894 ret = inet_pton(AF_INET, cp, &p->prefix);
895 XFREE(MTYPE_TMP, cp);
896 if (ret == 0)
897 return 0;
898
899 /* Get prefix length. */
900 plen = (uint8_t)atoi(++pnt);
901 if (plen > IPV4_MAX_PREFIXLEN)
902 return 0;
903
904 p->family = AF_INET;
905 p->prefixlen = plen;
906 }
907
908 return ret;
909 }
910
911 /* When string format is invalid return 0. */
912 int str2prefix_eth(const char *str, struct prefix_eth *p)
913 {
914 int ret = 0;
915 int plen = 48;
916 char *pnt;
917 char *cp = NULL;
918 const char *str_addr = str;
919 unsigned int a[6];
920 int i;
921 bool slash = false;
922
923 if (!strcmp(str, "any")) {
924 memset(p, 0, sizeof(*p));
925 p->family = AF_ETHERNET;
926 return 1;
927 }
928
929 /* Find slash inside string. */
930 pnt = strchr(str, '/');
931
932 if (pnt) {
933 /* Get prefix length. */
934 plen = (uint8_t)atoi(++pnt);
935 if (plen > 48) {
936 ret = 0;
937 goto done;
938 }
939
940 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
941 memcpy(cp, str, pnt - str);
942 *(cp + (pnt - str)) = '\0';
943
944 str_addr = cp;
945 slash = true;
946 }
947
948 /* Convert string to prefix. */
949 if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2,
950 a + 3, a + 4, a + 5)
951 != 6) {
952 ret = 0;
953 goto done;
954 }
955 for (i = 0; i < 6; ++i) {
956 p->eth_addr.octet[i] = a[i] & 0xff;
957 }
958 p->prefixlen = plen;
959 p->family = AF_ETHERNET;
960
961 /*
962 * special case to allow old configurations to work
963 * Since all zero's is implicitly meant to allow
964 * a comparison to zero, let's assume
965 */
966 if (!slash && is_zero_mac(&(p->eth_addr)))
967 p->prefixlen = 0;
968
969 ret = 1;
970
971 done:
972 XFREE(MTYPE_TMP, cp);
973
974 return ret;
975 }
976
977 /* Convert masklen into IP address's netmask (network byte order). */
978 void masklen2ip(const int masklen, struct in_addr *netmask)
979 {
980 assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
981
982 /* left shift is only defined for less than the size of the type.
983 * we unconditionally use long long in case the target platform
984 * has defined behaviour for << 32 (or has a 64-bit left shift) */
985
986 if (sizeof(unsigned long long) > 4)
987 netmask->s_addr = htonl(0xffffffffULL << (32 - masklen));
988 else
989 netmask->s_addr =
990 htonl(masklen ? 0xffffffffU << (32 - masklen) : 0);
991 }
992
993 /* Convert IP address's netmask into integer. We assume netmask is
994 * sequential one. Argument netmask should be network byte order. */
995 uint8_t ip_masklen(struct in_addr netmask)
996 {
997 uint32_t tmp = ~ntohl(netmask.s_addr);
998
999 /*
1000 * clz: count leading zeroes. sadly, the behaviour of this builtin is
1001 * undefined for a 0 argument, even though most CPUs give 32
1002 */
1003 return tmp ? __builtin_clz(tmp) : 32;
1004 }
1005
1006 /* Apply mask to IPv4 prefix (network byte order). */
1007 void apply_mask_ipv4(struct prefix_ipv4 *p)
1008 {
1009 struct in_addr mask;
1010 masklen2ip(p->prefixlen, &mask);
1011 p->prefix.s_addr &= mask.s_addr;
1012 }
1013
1014 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
1015 int prefix_ipv4_any(const struct prefix_ipv4 *p)
1016 {
1017 return (p->prefix.s_addr == 0 && p->prefixlen == 0);
1018 }
1019
1020 /* Allocate a new ip version 6 route */
1021 struct prefix_ipv6 *prefix_ipv6_new(void)
1022 {
1023 struct prefix_ipv6 *p;
1024
1025 /* Allocate a full-size struct prefix to avoid problems with structure
1026 size mismatches. */
1027 p = (struct prefix_ipv6 *)prefix_new();
1028 p->family = AF_INET6;
1029 return p;
1030 }
1031
1032 /* Free prefix for IPv6. */
1033 void prefix_ipv6_free(struct prefix_ipv6 *p)
1034 {
1035 prefix_free((struct prefix *)p);
1036 }
1037
1038 /* If given string is valid return 1 else return 0 */
1039 int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p)
1040 {
1041 char *pnt;
1042 char *cp;
1043 int ret;
1044
1045 pnt = strchr(str, '/');
1046
1047 /* If string doesn't contain `/' treat it as host route. */
1048 if (pnt == NULL) {
1049 ret = inet_pton(AF_INET6, str, &p->prefix);
1050 if (ret == 0)
1051 return 0;
1052 p->prefixlen = IPV6_MAX_BITLEN;
1053 } else {
1054 int plen;
1055
1056 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
1057 memcpy(cp, str, pnt - str);
1058 *(cp + (pnt - str)) = '\0';
1059 ret = inet_pton(AF_INET6, cp, &p->prefix);
1060 XFREE(MTYPE_TMP, cp);
1061 if (ret == 0)
1062 return 0;
1063 plen = (uint8_t)atoi(++pnt);
1064 if (plen > IPV6_MAX_BITLEN)
1065 return 0;
1066 p->prefixlen = plen;
1067 }
1068 p->family = AF_INET6;
1069
1070 return ret;
1071 }
1072
1073 /* Convert struct in6_addr netmask into integer.
1074 * FIXME return uint8_t as ip_maskleni() does. */
1075 int ip6_masklen(struct in6_addr netmask)
1076 {
1077 int len = 0;
1078 unsigned char val;
1079 unsigned char *pnt;
1080
1081 pnt = (unsigned char *)&netmask;
1082
1083 while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN) {
1084 len += 8;
1085 pnt++;
1086 }
1087
1088 if (len < IPV6_MAX_BITLEN) {
1089 val = *pnt;
1090 while (val) {
1091 len++;
1092 val <<= 1;
1093 }
1094 }
1095 return len;
1096 }
1097
1098 void masklen2ip6(const int masklen, struct in6_addr *netmask)
1099 {
1100 assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
1101 memcpy(netmask, maskbytes6 + masklen, sizeof(struct in6_addr));
1102 }
1103
1104 void apply_mask_ipv6(struct prefix_ipv6 *p)
1105 {
1106 uint8_t *pnt;
1107 int index;
1108 int offset;
1109
1110 index = p->prefixlen / 8;
1111
1112 if (index < 16) {
1113 pnt = (uint8_t *)&p->prefix;
1114 offset = p->prefixlen % 8;
1115
1116 pnt[index] &= maskbit[offset];
1117 index++;
1118
1119 while (index < 16)
1120 pnt[index++] = 0;
1121 }
1122 }
1123
1124 void apply_mask(struct prefix *p)
1125 {
1126 switch (p->family) {
1127 case AF_INET:
1128 apply_mask_ipv4((struct prefix_ipv4 *)p);
1129 break;
1130 case AF_INET6:
1131 apply_mask_ipv6((struct prefix_ipv6 *)p);
1132 break;
1133 default:
1134 break;
1135 }
1136 return;
1137 }
1138
1139 /* Utility function of convert between struct prefix <=> union sockunion.
1140 * FIXME This function isn't used anywhere. */
1141 struct prefix *sockunion2prefix(const union sockunion *dest,
1142 const union sockunion *mask)
1143 {
1144 if (dest->sa.sa_family == AF_INET) {
1145 struct prefix_ipv4 *p;
1146
1147 p = prefix_ipv4_new();
1148 p->family = AF_INET;
1149 p->prefix = dest->sin.sin_addr;
1150 p->prefixlen = ip_masklen(mask->sin.sin_addr);
1151 return (struct prefix *)p;
1152 }
1153 if (dest->sa.sa_family == AF_INET6) {
1154 struct prefix_ipv6 *p;
1155
1156 p = prefix_ipv6_new();
1157 p->family = AF_INET6;
1158 p->prefixlen = ip6_masklen(mask->sin6.sin6_addr);
1159 memcpy(&p->prefix, &dest->sin6.sin6_addr,
1160 sizeof(struct in6_addr));
1161 return (struct prefix *)p;
1162 }
1163 return NULL;
1164 }
1165
1166 /* Utility function of convert between struct prefix <=> union sockunion. */
1167 struct prefix *sockunion2hostprefix(const union sockunion *su,
1168 struct prefix *prefix)
1169 {
1170 if (su->sa.sa_family == AF_INET) {
1171 struct prefix_ipv4 *p;
1172
1173 p = prefix ? (struct prefix_ipv4 *)prefix : prefix_ipv4_new();
1174 p->family = AF_INET;
1175 p->prefix = su->sin.sin_addr;
1176 p->prefixlen = IPV4_MAX_BITLEN;
1177 return (struct prefix *)p;
1178 }
1179 if (su->sa.sa_family == AF_INET6) {
1180 struct prefix_ipv6 *p;
1181
1182 p = prefix ? (struct prefix_ipv6 *)prefix : prefix_ipv6_new();
1183 p->family = AF_INET6;
1184 p->prefixlen = IPV6_MAX_BITLEN;
1185 memcpy(&p->prefix, &su->sin6.sin6_addr,
1186 sizeof(struct in6_addr));
1187 return (struct prefix *)p;
1188 }
1189 return NULL;
1190 }
1191
1192 void prefix2sockunion(const struct prefix *p, union sockunion *su)
1193 {
1194 memset(su, 0, sizeof(*su));
1195
1196 su->sa.sa_family = p->family;
1197 if (p->family == AF_INET)
1198 su->sin.sin_addr = p->u.prefix4;
1199 if (p->family == AF_INET6)
1200 memcpy(&su->sin6.sin6_addr, &p->u.prefix6,
1201 sizeof(struct in6_addr));
1202 }
1203
1204 int prefix_blen(const struct prefix *p)
1205 {
1206 switch (p->family) {
1207 case AF_INET:
1208 return IPV4_MAX_BYTELEN;
1209 break;
1210 case AF_INET6:
1211 return IPV6_MAX_BYTELEN;
1212 break;
1213 case AF_ETHERNET:
1214 return ETH_ALEN;
1215 break;
1216 }
1217 return 0;
1218 }
1219
1220 /* Generic function for conversion string to struct prefix. */
1221 int str2prefix(const char *str, struct prefix *p)
1222 {
1223 int ret;
1224
1225 if (!str || !p)
1226 return 0;
1227
1228 /* First we try to convert string to struct prefix_ipv4. */
1229 ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p);
1230 if (ret)
1231 return ret;
1232
1233 /* Next we try to convert string to struct prefix_ipv6. */
1234 ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p);
1235 if (ret)
1236 return ret;
1237
1238 /* Next we try to convert string to struct prefix_eth. */
1239 ret = str2prefix_eth(str, (struct prefix_eth *)p);
1240 if (ret)
1241 return ret;
1242
1243 return 0;
1244 }
1245
1246 static const char *prefixevpn_ead2str(const struct prefix_evpn *p, char *str,
1247 int size)
1248 {
1249 snprintf(str, size, "Unsupported EVPN prefix");
1250 return str;
1251 }
1252
1253 static const char *prefixevpn_macip2str(const struct prefix_evpn *p, char *str,
1254 int size)
1255 {
1256 uint8_t family;
1257 char buf[PREFIX2STR_BUFFER];
1258 char buf2[ETHER_ADDR_STRLEN];
1259
1260 if (is_evpn_prefix_ipaddr_none(p))
1261 snprintf(str, size, "[%d]:[%s]/%d",
1262 p->prefix.route_type,
1263 prefix_mac2str(&p->prefix.macip_addr.mac,
1264 buf2, sizeof(buf2)),
1265 p->prefixlen);
1266 else {
1267 family = is_evpn_prefix_ipaddr_v4(p)
1268 ? AF_INET
1269 : AF_INET6;
1270 snprintf(str, size, "[%d]:[%s]:[%s]/%d",
1271 p->prefix.route_type,
1272 prefix_mac2str(&p->prefix.macip_addr.mac,
1273 buf2, sizeof(buf2)),
1274 inet_ntop(family,
1275 &p->prefix.macip_addr.ip.ip.addr,
1276 buf, PREFIX2STR_BUFFER),
1277 p->prefixlen);
1278 }
1279 return str;
1280 }
1281
1282 static const char *prefixevpn_imet2str(const struct prefix_evpn *p, char *str,
1283 int size)
1284 {
1285 uint8_t family;
1286 char buf[PREFIX2STR_BUFFER];
1287
1288 family = is_evpn_prefix_ipaddr_v4(p)
1289 ? AF_INET
1290 : AF_INET6;
1291 snprintf(str, size, "[%d]:[%s]/%d", p->prefix.route_type,
1292 inet_ntop(family,
1293 &p->prefix.imet_addr.ip.ip.addr, buf,
1294 PREFIX2STR_BUFFER),
1295 p->prefixlen);
1296 return str;
1297 }
1298
1299 static const char *prefixevpn_es2str(const struct prefix_evpn *p, char *str,
1300 int size)
1301 {
1302 char buf[ESI_STR_LEN];
1303
1304 snprintf(str, size, "[%d]:[%s]:[%s]/%d", p->prefix.route_type,
1305 esi_to_str(&p->prefix.es_addr.esi, buf, sizeof(buf)),
1306 inet_ntoa(p->prefix.es_addr.ip.ipaddr_v4),
1307 p->prefixlen);
1308 return str;
1309 }
1310
1311 static const char *prefixevpn_prefix2str(const struct prefix_evpn *p, char *str,
1312 int size)
1313 {
1314 uint8_t family;
1315 char buf[PREFIX2STR_BUFFER];
1316
1317 family = is_evpn_prefix_ipaddr_v4(p)
1318 ? AF_INET
1319 : AF_INET6;
1320 snprintf(str, size, "[%d]:[%u][%s/%d]/%d",
1321 p->prefix.route_type,
1322 p->prefix.prefix_addr.eth_tag,
1323 inet_ntop(family,
1324 &p->prefix.prefix_addr.ip.ip.addr, buf,
1325 PREFIX2STR_BUFFER),
1326 p->prefix.prefix_addr.ip_prefix_length,
1327 p->prefixlen);
1328 return str;
1329 }
1330
1331 static const char *prefixevpn2str(const struct prefix_evpn *p, char *str,
1332 int size)
1333 {
1334 switch (p->prefix.route_type) {
1335 case 1:
1336 return prefixevpn_ead2str(p, str, size);
1337 case 2:
1338 return prefixevpn_macip2str(p, str, size);
1339 case 3:
1340 return prefixevpn_imet2str(p, str, size);
1341 case 4:
1342 return prefixevpn_es2str(p, str, size);
1343 case 5:
1344 return prefixevpn_prefix2str(p, str, size);
1345 default:
1346 snprintf(str, size, "Unsupported EVPN prefix");
1347 break;
1348 }
1349 return str;
1350 }
1351
1352 const char *prefix2str(union prefixconstptr pu, char *str, int size)
1353 {
1354 const struct prefix *p = pu.p;
1355 char buf[PREFIX2STR_BUFFER];
1356 int byte, tmp, a, b;
1357 bool z = false;
1358 size_t l;
1359
1360 switch (p->family) {
1361 case AF_INET:
1362 case AF_INET6:
1363 inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf));
1364 l = strlen(buf);
1365 buf[l++] = '/';
1366 byte = p->prefixlen;
1367 if ((tmp = p->prefixlen - 100) >= 0) {
1368 buf[l++] = '1';
1369 z = true;
1370 byte = tmp;
1371 }
1372 b = byte % 10;
1373 a = byte / 10;
1374 if (a || z)
1375 buf[l++] = '0' + a;
1376 buf[l++] = '0' + b;
1377 buf[l] = '\0';
1378 strlcpy(str, buf, size);
1379 break;
1380
1381 case AF_ETHERNET:
1382 snprintf(str, size, "%s/%d",
1383 prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)),
1384 p->prefixlen);
1385 break;
1386
1387 case AF_EVPN:
1388 prefixevpn2str((const struct prefix_evpn *)p, str, size);
1389 break;
1390
1391 case AF_FLOWSPEC:
1392 strlcpy(str, "FS prefix", size);
1393 break;
1394
1395 default:
1396 strlcpy(str, "UNK prefix", size);
1397 break;
1398 }
1399
1400 return str;
1401 }
1402
1403 void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr,
1404 char *buf, int buf_size)
1405 {
1406 int save_errno = errno;
1407
1408 if (addr.s_addr == INADDR_ANY)
1409 strlcpy(buf, "*", buf_size);
1410 else {
1411 if (!inet_ntop(AF_INET, &addr, buf, buf_size)) {
1412 if (onfail)
1413 snprintf(buf, buf_size, "%s", onfail);
1414 }
1415 }
1416
1417 errno = save_errno;
1418 }
1419
1420 const char *prefix_sg2str(const struct prefix_sg *sg, char *sg_str)
1421 {
1422 char src_str[INET_ADDRSTRLEN];
1423 char grp_str[INET_ADDRSTRLEN];
1424
1425 prefix_mcast_inet4_dump("<src?>", sg->src, src_str, sizeof(src_str));
1426 prefix_mcast_inet4_dump("<grp?>", sg->grp, grp_str, sizeof(grp_str));
1427 snprintf(sg_str, PREFIX_SG_STR_LEN, "(%s,%s)", src_str, grp_str);
1428
1429 return sg_str;
1430 }
1431
1432 struct prefix *prefix_new(void)
1433 {
1434 struct prefix *p;
1435
1436 p = XCALLOC(MTYPE_PREFIX, sizeof *p);
1437 return p;
1438 }
1439
1440 /* Free prefix structure. */
1441 void prefix_free(struct prefix *p)
1442 {
1443 XFREE(MTYPE_PREFIX, p);
1444 }
1445
1446 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1447 void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
1448 {
1449
1450 uint32_t destination;
1451
1452 destination = ntohl(p->prefix.s_addr);
1453
1454 if (p->prefixlen == IPV4_MAX_PREFIXLEN)
1455 ;
1456 /* do nothing for host routes */
1457 else if (IN_CLASSC(destination)) {
1458 p->prefixlen = 24;
1459 apply_mask_ipv4(p);
1460 } else if (IN_CLASSB(destination)) {
1461 p->prefixlen = 16;
1462 apply_mask_ipv4(p);
1463 } else {
1464 p->prefixlen = 8;
1465 apply_mask_ipv4(p);
1466 }
1467 }
1468
1469 in_addr_t ipv4_network_addr(in_addr_t hostaddr, int masklen)
1470 {
1471 struct in_addr mask;
1472
1473 masklen2ip(masklen, &mask);
1474 return hostaddr & mask.s_addr;
1475 }
1476
1477 in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
1478 {
1479 struct in_addr mask;
1480
1481 masklen2ip(masklen, &mask);
1482 return (masklen != IPV4_MAX_PREFIXLEN - 1) ?
1483 /* normal case */
1484 (hostaddr | ~mask.s_addr)
1485 :
1486 /* special case for /31 */
1487 (hostaddr ^ ~mask.s_addr);
1488 }
1489
1490 /* Utility function to convert ipv4 netmask to prefixes
1491 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1492 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1493 int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1494 char *prefix_str)
1495 {
1496 struct in_addr network;
1497 struct in_addr mask;
1498 uint8_t prefixlen;
1499 uint32_t destination;
1500 int ret;
1501
1502 ret = inet_aton(net_str, &network);
1503 if (!ret)
1504 return 0;
1505
1506 if (mask_str) {
1507 ret = inet_aton(mask_str, &mask);
1508 if (!ret)
1509 return 0;
1510
1511 prefixlen = ip_masklen(mask);
1512 } else {
1513 destination = ntohl(network.s_addr);
1514
1515 if (network.s_addr == 0)
1516 prefixlen = 0;
1517 else if (IN_CLASSC(destination))
1518 prefixlen = 24;
1519 else if (IN_CLASSB(destination))
1520 prefixlen = 16;
1521 else if (IN_CLASSA(destination))
1522 prefixlen = 8;
1523 else
1524 return 0;
1525 }
1526
1527 sprintf(prefix_str, "%s/%d", net_str, prefixlen);
1528
1529 return 1;
1530 }
1531
1532 /* Utility function for making IPv6 address string. */
1533 const char *inet6_ntoa(struct in6_addr addr)
1534 {
1535 static char buf[INET6_ADDRSTRLEN];
1536
1537 inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1538 return buf;
1539 }
1540
1541 /* converts to internal representation of mac address
1542 * returns 1 on success, 0 otherwise
1543 * format accepted: AA:BB:CC:DD:EE:FF
1544 * if mac parameter is null, then check only
1545 */
1546 int prefix_str2mac(const char *str, struct ethaddr *mac)
1547 {
1548 unsigned int a[6];
1549 int i;
1550
1551 if (!str)
1552 return 0;
1553
1554 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3,
1555 a + 4, a + 5)
1556 != 6) {
1557 /* error in incoming str length */
1558 return 0;
1559 }
1560 /* valid mac address */
1561 if (!mac)
1562 return 1;
1563 for (i = 0; i < 6; ++i)
1564 mac->octet[i] = a[i] & 0xff;
1565 return 1;
1566 }
1567
1568 char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
1569 {
1570 char *ptr;
1571
1572 if (!mac)
1573 return NULL;
1574 if (!buf)
1575 ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char));
1576 else {
1577 assert(size >= ETHER_ADDR_STRLEN);
1578 ptr = buf;
1579 }
1580 snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x",
1581 (uint8_t)mac->octet[0], (uint8_t)mac->octet[1],
1582 (uint8_t)mac->octet[2], (uint8_t)mac->octet[3],
1583 (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]);
1584 return ptr;
1585 }
1586
1587 unsigned prefix_hash_key(const void *pp)
1588 {
1589 struct prefix copy;
1590
1591 if (((struct prefix *)pp)->family == AF_FLOWSPEC) {
1592 uint32_t len;
1593 void *temp;
1594
1595 /* make sure *all* unused bits are zero,
1596 * particularly including alignment /
1597 * padding and unused prefix bytes.
1598 */
1599 memset(&copy, 0, sizeof(copy));
1600 prefix_copy(&copy, (struct prefix *)pp);
1601 len = jhash((void *)copy.u.prefix_flowspec.ptr,
1602 copy.u.prefix_flowspec.prefixlen,
1603 0x55aa5a5a);
1604 temp = (void *)copy.u.prefix_flowspec.ptr;
1605 XFREE(MTYPE_PREFIX_FLOWSPEC, temp);
1606 copy.u.prefix_flowspec.ptr = (uintptr_t)NULL;
1607 return len;
1608 }
1609 /* make sure *all* unused bits are zero, particularly including
1610 * alignment /
1611 * padding and unused prefix bytes. */
1612 memset(&copy, 0, sizeof(copy));
1613 prefix_copy(&copy, (struct prefix *)pp);
1614 return jhash(&copy,
1615 offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen),
1616 0x55aa5a5a);
1617 }
1618
1619 /* converts to internal representation of esi
1620 * returns 1 on success, 0 otherwise
1621 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1622 * if esi parameter is null, then check only
1623 */
1624 int str_to_esi(const char *str, esi_t *esi)
1625 {
1626 int i;
1627 unsigned int a[ESI_BYTES];
1628
1629 if (!str)
1630 return 0;
1631
1632 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1633 a + 0, a + 1, a + 2, a + 3,
1634 a + 4, a + 5, a + 6, a + 7,
1635 a + 8, a + 9)
1636 != ESI_BYTES) {
1637 /* error in incoming str length */
1638 return 0;
1639 }
1640
1641 /* valid ESI */
1642 if (!esi)
1643 return 1;
1644 for (i = 0; i < ESI_BYTES; ++i)
1645 esi->val[i] = a[i] & 0xff;
1646 return 1;
1647 }
1648
1649 char *esi_to_str(const esi_t *esi, char *buf, int size)
1650 {
1651 char *ptr;
1652
1653 if (!esi)
1654 return NULL;
1655 if (!buf)
1656 ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char));
1657 else {
1658 assert(size >= ESI_STR_LEN);
1659 ptr = buf;
1660 }
1661
1662 snprintf(ptr, ESI_STR_LEN,
1663 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1664 esi->val[0], esi->val[1], esi->val[2],
1665 esi->val[3], esi->val[4], esi->val[5],
1666 esi->val[6], esi->val[7], esi->val[8],
1667 esi->val[9]);
1668 return ptr;
1669 }
1670
1671 printfrr_ext_autoreg_p("I4", printfrr_i4)
1672 static ssize_t printfrr_i4(char *buf, size_t bsz, const char *fmt,
1673 int prec, const void *ptr)
1674 {
1675 inet_ntop(AF_INET, ptr, buf, bsz);
1676 return 2;
1677 }
1678
1679 printfrr_ext_autoreg_p("I6", printfrr_i6)
1680 static ssize_t printfrr_i6(char *buf, size_t bsz, const char *fmt,
1681 int prec, const void *ptr)
1682 {
1683 inet_ntop(AF_INET6, ptr, buf, bsz);
1684 return 2;
1685 }
1686
1687 printfrr_ext_autoreg_p("FX", printfrr_pfx)
1688 static ssize_t printfrr_pfx(char *buf, size_t bsz, const char *fmt,
1689 int prec, const void *ptr)
1690 {
1691 prefix2str(ptr, buf, bsz);
1692 return 2;
1693 }
1694
1695 printfrr_ext_autoreg_p("SG4", printfrr_psg)
1696 static ssize_t printfrr_psg(char *buf, size_t bsz, const char *fmt,
1697 int prec, const void *ptr)
1698 {
1699 const struct prefix_sg *sg = ptr;
1700 struct fbuf fb = { .buf = buf, .pos = buf, .len = bsz - 1 };
1701
1702 if (sg->src.s_addr == INADDR_ANY)
1703 bprintfrr(&fb, "(*,");
1704 else
1705 bprintfrr(&fb, "(%pI4,", &sg->src);
1706
1707 if (sg->grp.s_addr == INADDR_ANY)
1708 bprintfrr(&fb, "*)");
1709 else
1710 bprintfrr(&fb, "%pI4)", &sg->grp);
1711
1712 fb.pos[0] = '\0';
1713 return 3;
1714 }