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