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