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