]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
Merge pull request #2034 from vincentbernat/fix/rfc8365-auto-rt
[mirror_frr.git] / lib / prefix.c
1 /*
2 * Prefix related functions.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "prefix.h"
25 #include "vty.h"
26 #include "sockunion.h"
27 #include "memory.h"
28 #include "log.h"
29 #include "jhash.h"
30
31 DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix")
32
33 /* Maskbit. */
34 static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
35 0xf8, 0xfc, 0xfe, 0xff};
36
37 static const struct in6_addr maskbytes6[] = {
38 /* /0 */ {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}},
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}}}};
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
432 int is_zero_mac(struct ethaddr *mac)
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
444 unsigned int prefix_bit(const uint8_t *prefix, const uint8_t prefixlen)
445 {
446 unsigned int offset = prefixlen / 8;
447 unsigned int shift = 7 - (prefixlen % 8);
448
449 return (prefix[offset] >> shift) & 1;
450 }
451
452 unsigned int prefix6_bit(const struct in6_addr *prefix, const uint8_t prefixlen)
453 {
454 return prefix_bit((const uint8_t *)&prefix->s6_addr, prefixlen);
455 }
456
457 int str2family(const char *string)
458 {
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;
465 else if (!strcmp("evpn", string))
466 return AF_EVPN;
467 return -1;
468 }
469
470 const char *family2str(int family)
471 {
472 switch (family) {
473 case AF_INET:
474 return "IPv4";
475 case AF_INET6:
476 return "IPv6";
477 case AF_ETHERNET:
478 return "Ethernet";
479 case AF_EVPN:
480 return "Evpn";
481 }
482 return "?";
483 }
484
485 /* Address Famiy Identifier to Address Family converter. */
486 int afi2family(afi_t afi)
487 {
488 if (afi == AFI_IP)
489 return AF_INET;
490 else if (afi == AFI_IP6)
491 return AF_INET6;
492 else if (afi == AFI_L2VPN)
493 return AF_ETHERNET;
494 /* NOTE: EVPN code should NOT use this interface. */
495 return 0;
496 }
497
498 afi_t family2afi(int family)
499 {
500 if (family == AF_INET)
501 return AFI_IP;
502 else if (family == AF_INET6)
503 return AFI_IP6;
504 else if (family == AF_ETHERNET || family == AF_EVPN)
505 return AFI_L2VPN;
506 return 0;
507 }
508
509 const char *afi2str(afi_t afi)
510 {
511 switch (afi) {
512 case AFI_IP:
513 return "IPv4";
514 case AFI_IP6:
515 return "IPv6";
516 case AFI_L2VPN:
517 return "l2vpn";
518 case AFI_MAX:
519 return "bad-value";
520 default:
521 break;
522 }
523 return NULL;
524 }
525
526 const char *safi2str(safi_t safi)
527 {
528 switch (safi) {
529 case SAFI_UNICAST:
530 return "unicast";
531 case SAFI_MULTICAST:
532 return "multicast";
533 case SAFI_MPLS_VPN:
534 return "vpn";
535 case SAFI_ENCAP:
536 return "encap";
537 case SAFI_EVPN:
538 return "evpn";
539 case SAFI_LABELED_UNICAST:
540 return "labeled-unicast";
541 case SAFI_FLOWSPEC:
542 return "flowspec";
543 default:
544 return "unknown";
545 }
546 }
547
548 /* If n includes p prefix then return 1 else return 0. */
549 int prefix_match(const struct prefix *n, const struct prefix *p)
550 {
551 int offset;
552 int shift;
553 const uint8_t *np, *pp;
554
555 /* If n's prefix is longer than p's one return 0. */
556 if (n->prefixlen > p->prefixlen)
557 return 0;
558
559 if (n->family == AF_FLOWSPEC) {
560 /* prefixlen is unused. look at fs prefix len */
561 if (n->u.prefix_flowspec.prefixlen >
562 p->u.prefix_flowspec.prefixlen)
563 return 0;
564
565 /* Set both prefix's head pointer. */
566 np = (const uint8_t *)&n->u.prefix_flowspec.ptr;
567 pp = (const uint8_t *)&p->u.prefix_flowspec.ptr;
568
569 offset = n->u.prefix_flowspec.prefixlen;
570
571 while (offset--)
572 if (np[offset] != pp[offset])
573 return 0;
574 return 1;
575 }
576
577 /* Set both prefix's head pointer. */
578 np = (const uint8_t *)&n->u.prefix;
579 pp = (const uint8_t *)&p->u.prefix;
580
581 offset = n->prefixlen / PNBBY;
582 shift = n->prefixlen % PNBBY;
583
584 if (shift)
585 if (maskbit[shift] & (np[offset] ^ pp[offset]))
586 return 0;
587
588 while (offset--)
589 if (np[offset] != pp[offset])
590 return 0;
591 return 1;
592 }
593
594 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
595 int prefix_match_network_statement(const struct prefix *n,
596 const struct prefix *p)
597 {
598 int offset;
599 int shift;
600 const uint8_t *np, *pp;
601
602 /* Set both prefix's head pointer. */
603 np = (const uint8_t *)&n->u.prefix;
604 pp = (const uint8_t *)&p->u.prefix;
605
606 offset = n->prefixlen / PNBBY;
607 shift = n->prefixlen % PNBBY;
608
609 if (shift)
610 if (maskbit[shift] & (np[offset] ^ pp[offset]))
611 return 0;
612
613 while (offset--)
614 if (np[offset] != pp[offset])
615 return 0;
616 return 1;
617 }
618
619 void prefix_copy(struct prefix *dest, const struct prefix *src)
620 {
621 dest->family = src->family;
622 dest->prefixlen = src->prefixlen;
623
624 if (src->family == AF_INET)
625 dest->u.prefix4 = src->u.prefix4;
626 else if (src->family == AF_INET6)
627 dest->u.prefix6 = src->u.prefix6;
628 else if (src->family == AF_ETHERNET) {
629 memcpy(&dest->u.prefix_eth, &src->u.prefix_eth,
630 sizeof(struct ethaddr));
631 } else if (src->family == AF_EVPN) {
632 memcpy(&dest->u.prefix_evpn, &src->u.prefix_evpn,
633 sizeof(struct evpn_addr));
634 } else if (src->family == AF_UNSPEC) {
635 dest->u.lp.id = src->u.lp.id;
636 dest->u.lp.adv_router = src->u.lp.adv_router;
637 } else if (src->family == AF_FLOWSPEC) {
638 void *temp;
639 int len;
640
641 len = src->u.prefix_flowspec.prefixlen;
642 dest->u.prefix_flowspec.prefixlen =
643 src->u.prefix_flowspec.prefixlen;
644 dest->family = src->family;
645 temp = XCALLOC(MTYPE_PREFIX_FLOWSPEC, len);
646 dest->u.prefix_flowspec.ptr = (uintptr_t)temp;
647 memcpy((void *)dest->u.prefix_flowspec.ptr,
648 (void *)src->u.prefix_flowspec.ptr, len);
649 } else {
650 zlog_err("prefix_copy(): Unknown address family %d",
651 src->family);
652 assert(0);
653 }
654 }
655
656 /*
657 * Return 1 if the address/netmask contained in the prefix structure
658 * is the same, and else return 0. For this routine, 'same' requires
659 * that not only the prefix length and the network part be the same,
660 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
661 * the same. Note that this routine has the same return value sense
662 * as '==' (which is different from prefix_cmp).
663 */
664 int prefix_same(const struct prefix *p1, const struct prefix *p2)
665 {
666 if ((p1 && !p2) || (!p1 && p2))
667 return 0;
668
669 if (!p1 && !p2)
670 return 1;
671
672 if (p1->family == p2->family && p1->prefixlen == p2->prefixlen) {
673 if (p1->family == AF_INET)
674 if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
675 return 1;
676 if (p1->family == AF_INET6)
677 if (IPV6_ADDR_SAME(&p1->u.prefix6.s6_addr,
678 &p2->u.prefix6.s6_addr))
679 return 1;
680 if (p1->family == AF_ETHERNET)
681 if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth,
682 sizeof(struct ethaddr)))
683 return 1;
684 if (p1->family == AF_EVPN)
685 if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn,
686 sizeof(struct evpn_addr)))
687 return 1;
688 if (p1->family == AF_FLOWSPEC) {
689 if (p1->u.prefix_flowspec.prefixlen !=
690 p2->u.prefix_flowspec.prefixlen)
691 return 0;
692 if (!memcmp(&p1->u.prefix_flowspec.ptr,
693 &p2->u.prefix_flowspec.ptr,
694 p2->u.prefix_flowspec.prefixlen))
695 return 1;
696 }
697 }
698 return 0;
699 }
700
701 /*
702 * Return 0 if the network prefixes represented by the struct prefix
703 * arguments are the same prefix, and 1 otherwise. Network prefixes
704 * are considered the same if the prefix lengths are equal and the
705 * network parts are the same. Host bits (which are considered masked
706 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
707 * 10.0.0.2/8 are considered equivalent by this routine. Note that
708 * this routine has the same return sense as strcmp (which is different
709 * from prefix_same).
710 */
711 int prefix_cmp(const struct prefix *p1, const struct prefix *p2)
712 {
713 int offset;
714 int shift;
715
716 /* Set both prefix's head pointer. */
717 const uint8_t *pp1;
718 const uint8_t *pp2;
719
720 if (p1->family != p2->family)
721 return 1;
722 if (p1->family == AF_FLOWSPEC) {
723 pp1 = (const uint8_t *)p1->u.prefix_flowspec.ptr;
724 pp2 = (const uint8_t *)p2->u.prefix_flowspec.ptr;
725
726 if (p1->u.prefix_flowspec.prefixlen !=
727 p2->u.prefix_flowspec.prefixlen)
728 return 1;
729
730 offset = p1->u.prefix_flowspec.prefixlen;
731 while (offset--)
732 if (pp1[offset] != pp2[offset])
733 return 1;
734 return 0;
735 }
736 pp1 = (const uint8_t *)&p1->u.prefix;
737 pp2 = (const uint8_t *)&p2->u.prefix;
738
739 if (p1->prefixlen != p2->prefixlen)
740 return 1;
741 offset = p1->prefixlen / PNBBY;
742 shift = p1->prefixlen % PNBBY;
743
744 if (shift)
745 if (maskbit[shift] & (pp1[offset] ^ pp2[offset]))
746 return 1;
747
748 while (offset--)
749 if (pp1[offset] != pp2[offset])
750 return 1;
751
752 return 0;
753 }
754
755 /*
756 * Count the number of common bits in 2 prefixes. The prefix length is
757 * ignored for this function; the whole prefix is compared. If the prefix
758 * address families don't match, return -1; otherwise the return value is
759 * in range 0 ... maximum prefix length for the address family.
760 */
761 int prefix_common_bits(const struct prefix *p1, const struct prefix *p2)
762 {
763 int pos, bit;
764 int length = 0;
765 uint8_t xor ;
766
767 /* Set both prefix's head pointer. */
768 const uint8_t *pp1 = (const uint8_t *)&p1->u.prefix;
769 const uint8_t *pp2 = (const uint8_t *)&p2->u.prefix;
770
771 if (p1->family == AF_INET)
772 length = IPV4_MAX_BYTELEN;
773 if (p1->family == AF_INET6)
774 length = IPV6_MAX_BYTELEN;
775 if (p1->family == AF_ETHERNET)
776 length = ETH_ALEN;
777 if (p1->family == AF_EVPN)
778 length = 8 * sizeof(struct evpn_addr);
779
780 if (p1->family != p2->family || !length)
781 return -1;
782
783 for (pos = 0; pos < length; pos++)
784 if (pp1[pos] != pp2[pos])
785 break;
786 if (pos == length)
787 return pos * 8;
788
789 xor = pp1[pos] ^ pp2[pos];
790 for (bit = 0; bit < 8; bit++)
791 if (xor&(1 << (7 - bit)))
792 break;
793
794 return pos * 8 + bit;
795 }
796
797 /* Return prefix family type string. */
798 const char *prefix_family_str(const struct prefix *p)
799 {
800 if (p->family == AF_INET)
801 return "inet";
802 if (p->family == AF_INET6)
803 return "inet6";
804 if (p->family == AF_ETHERNET)
805 return "ether";
806 if (p->family == AF_EVPN)
807 return "evpn";
808 return "unspec";
809 }
810
811 /* Allocate new prefix_ipv4 structure. */
812 struct prefix_ipv4 *prefix_ipv4_new()
813 {
814 struct prefix_ipv4 *p;
815
816 /* Call prefix_new to allocate a full-size struct prefix to avoid
817 problems
818 where the struct prefix_ipv4 is cast to struct prefix and unallocated
819 bytes were being referenced (e.g. in structure assignments). */
820 p = (struct prefix_ipv4 *)prefix_new();
821 p->family = AF_INET;
822 return p;
823 }
824
825 /* Free prefix_ipv4 structure. */
826 void prefix_ipv4_free(struct prefix_ipv4 *p)
827 {
828 prefix_free((struct prefix *)p);
829 }
830
831 /* When string format is invalid return 0. */
832 int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p)
833 {
834 int ret;
835 int plen;
836 char *pnt;
837 char *cp;
838
839 /* Find slash inside string. */
840 pnt = strchr(str, '/');
841
842 /* String doesn't contail slash. */
843 if (pnt == NULL) {
844 /* Convert string to prefix. */
845 ret = inet_aton(str, &p->prefix);
846 if (ret == 0)
847 return 0;
848
849 /* If address doesn't contain slash we assume it host address.
850 */
851 p->family = AF_INET;
852 p->prefixlen = IPV4_MAX_BITLEN;
853
854 return ret;
855 } else {
856 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
857 strncpy(cp, str, pnt - str);
858 *(cp + (pnt - str)) = '\0';
859 ret = inet_aton(cp, &p->prefix);
860 XFREE(MTYPE_TMP, cp);
861
862 /* Get prefix length. */
863 plen = (uint8_t)atoi(++pnt);
864 if (plen > IPV4_MAX_PREFIXLEN)
865 return 0;
866
867 p->family = AF_INET;
868 p->prefixlen = plen;
869 }
870
871 return ret;
872 }
873
874 /* When string format is invalid return 0. */
875 int str2prefix_eth(const char *str, struct prefix_eth *p)
876 {
877 int ret = 0;
878 int plen = 48;
879 char *pnt;
880 char *cp = NULL;
881 const char *str_addr = str;
882 unsigned int a[6];
883 int i;
884 bool slash = false;
885
886 if (!strcmp(str, "any")) {
887 memset(p, 0, sizeof(*p));
888 p->family = AF_ETHERNET;
889 return 1;
890 }
891
892 /* Find slash inside string. */
893 pnt = strchr(str, '/');
894
895 if (pnt) {
896 /* Get prefix length. */
897 plen = (uint8_t)atoi(++pnt);
898 if (plen > 48) {
899 ret = 0;
900 goto done;
901 }
902
903 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
904 strncpy(cp, str, pnt - str);
905 *(cp + (pnt - str)) = '\0';
906
907 str_addr = cp;
908 slash = true;
909 }
910
911 /* Convert string to prefix. */
912 if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2,
913 a + 3, a + 4, a + 5)
914 != 6) {
915 ret = 0;
916 goto done;
917 }
918 for (i = 0; i < 6; ++i) {
919 p->eth_addr.octet[i] = a[i] & 0xff;
920 }
921 p->prefixlen = plen;
922 p->family = AF_ETHERNET;
923
924 /*
925 * special case to allow old configurations to work
926 * Since all zero's is implicitly meant to allow
927 * a comparison to zero, let's assume
928 */
929 if (!slash && is_zero_mac(&(p->eth_addr)))
930 p->prefixlen = 0;
931
932 ret = 1;
933
934 done:
935 if (cp)
936 XFREE(MTYPE_TMP, cp);
937
938 return ret;
939 }
940
941 /* Convert masklen into IP address's netmask (network byte order). */
942 void masklen2ip(const int masklen, struct in_addr *netmask)
943 {
944 assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
945
946 /* left shift is only defined for less than the size of the type.
947 * we unconditionally use long long in case the target platform
948 * has defined behaviour for << 32 (or has a 64-bit left shift) */
949
950 if (sizeof(unsigned long long) > 4)
951 netmask->s_addr = htonl(0xffffffffULL << (32 - masklen));
952 else
953 netmask->s_addr =
954 htonl(masklen ? 0xffffffffU << (32 - masklen) : 0);
955 }
956
957 /* Convert IP address's netmask into integer. We assume netmask is
958 sequential one. Argument netmask should be network byte order. */
959 uint8_t ip_masklen(struct in_addr netmask)
960 {
961 uint32_t tmp = ~ntohl(netmask.s_addr);
962 if (tmp)
963 /* clz: count leading zeroes. sadly, the behaviour of this
964 * builtin
965 * is undefined for a 0 argument, even though most CPUs give 32
966 */
967 return __builtin_clz(tmp);
968 else
969 return 32;
970 }
971
972 /* Apply mask to IPv4 prefix (network byte order). */
973 void apply_mask_ipv4(struct prefix_ipv4 *p)
974 {
975 struct in_addr mask;
976 masklen2ip(p->prefixlen, &mask);
977 p->prefix.s_addr &= mask.s_addr;
978 }
979
980 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
981 int prefix_ipv4_any(const struct prefix_ipv4 *p)
982 {
983 return (p->prefix.s_addr == 0 && p->prefixlen == 0);
984 }
985
986 /* Allocate a new ip version 6 route */
987 struct prefix_ipv6 *prefix_ipv6_new(void)
988 {
989 struct prefix_ipv6 *p;
990
991 /* Allocate a full-size struct prefix to avoid problems with structure
992 size mismatches. */
993 p = (struct prefix_ipv6 *)prefix_new();
994 p->family = AF_INET6;
995 return p;
996 }
997
998 /* Free prefix for IPv6. */
999 void prefix_ipv6_free(struct prefix_ipv6 *p)
1000 {
1001 prefix_free((struct prefix *)p);
1002 }
1003
1004 /* If given string is valid return pin6 else return NULL */
1005 int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p)
1006 {
1007 char *pnt;
1008 char *cp;
1009 int ret;
1010
1011 pnt = strchr(str, '/');
1012
1013 /* If string doesn't contain `/' treat it as host route. */
1014 if (pnt == NULL) {
1015 ret = inet_pton(AF_INET6, str, &p->prefix);
1016 if (ret == 0)
1017 return 0;
1018 p->prefixlen = IPV6_MAX_BITLEN;
1019 } else {
1020 int plen;
1021
1022 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
1023 strncpy(cp, str, pnt - str);
1024 *(cp + (pnt - str)) = '\0';
1025 ret = inet_pton(AF_INET6, cp, &p->prefix);
1026 XFREE(MTYPE_TMP, cp);
1027 if (ret == 0)
1028 return 0;
1029 plen = (uint8_t)atoi(++pnt);
1030 if (plen > IPV6_MAX_BITLEN)
1031 return 0;
1032 p->prefixlen = plen;
1033 }
1034 p->family = AF_INET6;
1035
1036 return ret;
1037 }
1038
1039 /* Convert struct in6_addr netmask into integer.
1040 * FIXME return uint8_t as ip_maskleni() does. */
1041 int ip6_masklen(struct in6_addr netmask)
1042 {
1043 int len = 0;
1044 unsigned char val;
1045 unsigned char *pnt;
1046
1047 pnt = (unsigned char *)&netmask;
1048
1049 while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN) {
1050 len += 8;
1051 pnt++;
1052 }
1053
1054 if (len < IPV6_MAX_BITLEN) {
1055 val = *pnt;
1056 while (val) {
1057 len++;
1058 val <<= 1;
1059 }
1060 }
1061 return len;
1062 }
1063
1064 void masklen2ip6(const int masklen, struct in6_addr *netmask)
1065 {
1066 assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
1067 memcpy(netmask, maskbytes6 + masklen, sizeof(struct in6_addr));
1068 }
1069
1070 void apply_mask_ipv6(struct prefix_ipv6 *p)
1071 {
1072 uint8_t *pnt;
1073 int index;
1074 int offset;
1075
1076 index = p->prefixlen / 8;
1077
1078 if (index < 16) {
1079 pnt = (uint8_t *)&p->prefix;
1080 offset = p->prefixlen % 8;
1081
1082 pnt[index] &= maskbit[offset];
1083 index++;
1084
1085 while (index < 16)
1086 pnt[index++] = 0;
1087 }
1088 }
1089
1090 void apply_mask(struct prefix *p)
1091 {
1092 switch (p->family) {
1093 case AF_INET:
1094 apply_mask_ipv4((struct prefix_ipv4 *)p);
1095 break;
1096 case AF_INET6:
1097 apply_mask_ipv6((struct prefix_ipv6 *)p);
1098 break;
1099 default:
1100 break;
1101 }
1102 return;
1103 }
1104
1105 /* Utility function of convert between struct prefix <=> union sockunion.
1106 * FIXME This function isn't used anywhere. */
1107 struct prefix *sockunion2prefix(const union sockunion *dest,
1108 const union sockunion *mask)
1109 {
1110 if (dest->sa.sa_family == AF_INET) {
1111 struct prefix_ipv4 *p;
1112
1113 p = prefix_ipv4_new();
1114 p->family = AF_INET;
1115 p->prefix = dest->sin.sin_addr;
1116 p->prefixlen = ip_masklen(mask->sin.sin_addr);
1117 return (struct prefix *)p;
1118 }
1119 if (dest->sa.sa_family == AF_INET6) {
1120 struct prefix_ipv6 *p;
1121
1122 p = prefix_ipv6_new();
1123 p->family = AF_INET6;
1124 p->prefixlen = ip6_masklen(mask->sin6.sin6_addr);
1125 memcpy(&p->prefix, &dest->sin6.sin6_addr,
1126 sizeof(struct in6_addr));
1127 return (struct prefix *)p;
1128 }
1129 return NULL;
1130 }
1131
1132 /* Utility function of convert between struct prefix <=> union sockunion. */
1133 struct prefix *sockunion2hostprefix(const union sockunion *su,
1134 struct prefix *prefix)
1135 {
1136 if (su->sa.sa_family == AF_INET) {
1137 struct prefix_ipv4 *p;
1138
1139 p = prefix ? (struct prefix_ipv4 *)prefix : prefix_ipv4_new();
1140 p->family = AF_INET;
1141 p->prefix = su->sin.sin_addr;
1142 p->prefixlen = IPV4_MAX_BITLEN;
1143 return (struct prefix *)p;
1144 }
1145 if (su->sa.sa_family == AF_INET6) {
1146 struct prefix_ipv6 *p;
1147
1148 p = prefix ? (struct prefix_ipv6 *)prefix : prefix_ipv6_new();
1149 p->family = AF_INET6;
1150 p->prefixlen = IPV6_MAX_BITLEN;
1151 memcpy(&p->prefix, &su->sin6.sin6_addr,
1152 sizeof(struct in6_addr));
1153 return (struct prefix *)p;
1154 }
1155 return NULL;
1156 }
1157
1158 void prefix2sockunion(const struct prefix *p, union sockunion *su)
1159 {
1160 memset(su, 0, sizeof(*su));
1161
1162 su->sa.sa_family = p->family;
1163 if (p->family == AF_INET)
1164 su->sin.sin_addr = p->u.prefix4;
1165 if (p->family == AF_INET6)
1166 memcpy(&su->sin6.sin6_addr, &p->u.prefix6,
1167 sizeof(struct in6_addr));
1168 }
1169
1170 int prefix_blen(const struct prefix *p)
1171 {
1172 switch (p->family) {
1173 case AF_INET:
1174 return IPV4_MAX_BYTELEN;
1175 break;
1176 case AF_INET6:
1177 return IPV6_MAX_BYTELEN;
1178 break;
1179 case AF_ETHERNET:
1180 return ETH_ALEN;
1181 break;
1182 }
1183 return 0;
1184 }
1185
1186 /* Generic function for conversion string to struct prefix. */
1187 int str2prefix(const char *str, struct prefix *p)
1188 {
1189 int ret;
1190
1191 /* First we try to convert string to struct prefix_ipv4. */
1192 ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p);
1193 if (ret)
1194 return ret;
1195
1196 /* Next we try to convert string to struct prefix_ipv6. */
1197 ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p);
1198 if (ret)
1199 return ret;
1200
1201 /* Next we try to convert string to struct prefix_eth. */
1202 ret = str2prefix_eth(str, (struct prefix_eth *)p);
1203 if (ret)
1204 return ret;
1205
1206 return 0;
1207 }
1208
1209 static const char *prefixevpn_ead2str(const struct prefix_evpn *p, char *str,
1210 int size)
1211 {
1212 snprintf(str, size, "Unsupported EVPN prefix");
1213 return str;
1214 }
1215
1216 static const char *prefixevpn_macip2str(const struct prefix_evpn *p, char *str,
1217 int size)
1218 {
1219 uint8_t family;
1220 char buf[PREFIX2STR_BUFFER];
1221 char buf2[ETHER_ADDR_STRLEN];
1222
1223 if (is_evpn_prefix_ipaddr_none(p))
1224 snprintf(str, size, "[%d]:[%s]/%d",
1225 p->prefix.route_type,
1226 prefix_mac2str(&p->prefix.macip_addr.mac,
1227 buf2, sizeof(buf2)),
1228 p->prefixlen);
1229 else {
1230 family = is_evpn_prefix_ipaddr_v4(p)
1231 ? AF_INET
1232 : AF_INET6;
1233 snprintf(str, size, "[%d]:[%s]:[%s]/%d",
1234 p->prefix.route_type,
1235 prefix_mac2str(&p->prefix.macip_addr.mac,
1236 buf2, sizeof(buf2)),
1237 inet_ntop(family,
1238 &p->prefix.macip_addr.ip.ip.addr,
1239 buf, PREFIX2STR_BUFFER),
1240 p->prefixlen);
1241 }
1242 return str;
1243 }
1244
1245 static const char *prefixevpn_imet2str(const struct prefix_evpn *p, char *str,
1246 int size)
1247 {
1248 uint8_t family;
1249 char buf[PREFIX2STR_BUFFER];
1250
1251 family = is_evpn_prefix_ipaddr_v4(p)
1252 ? AF_INET
1253 : AF_INET6;
1254 snprintf(str, size, "[%d]:[%s]/%d", p->prefix.route_type,
1255 inet_ntop(family,
1256 &p->prefix.imet_addr.ip.ip.addr, buf,
1257 PREFIX2STR_BUFFER),
1258 p->prefixlen);
1259 return str;
1260 }
1261
1262 static const char *prefixevpn_es2str(const struct prefix_evpn *p, char *str,
1263 int size)
1264 {
1265 snprintf(str, size, "Unsupported EVPN prefix");
1266 return str;
1267 }
1268
1269 static const char *prefixevpn_prefix2str(const struct prefix_evpn *p, char *str,
1270 int size)
1271 {
1272 uint8_t family;
1273 char buf[PREFIX2STR_BUFFER];
1274
1275 family = is_evpn_prefix_ipaddr_v4(p)
1276 ? AF_INET
1277 : AF_INET6;
1278 snprintf(str, size, "[%d]:[%u][%s/%d]/%d",
1279 p->prefix.route_type,
1280 p->prefix.prefix_addr.eth_tag,
1281 inet_ntop(family,
1282 &p->prefix.prefix_addr.ip.ip.addr, buf,
1283 PREFIX2STR_BUFFER),
1284 p->prefix.prefix_addr.ip_prefix_length,
1285 p->prefixlen);
1286 return str;
1287 }
1288
1289 static const char *prefixevpn2str(const struct prefix_evpn *p, char *str,
1290 int size)
1291 {
1292 switch (p->prefix.route_type) {
1293 case 1:
1294 return prefixevpn_ead2str(p, str, size);
1295 case 2:
1296 return prefixevpn_macip2str(p, str, size);
1297 case 3:
1298 return prefixevpn_imet2str(p, str, size);
1299 case 4:
1300 return prefixevpn_es2str(p, str, size);
1301 case 5:
1302 return prefixevpn_prefix2str(p, str, size);
1303 default:
1304 snprintf(str, size, "Unsupported EVPN prefix");
1305 break;
1306 }
1307 return str;
1308 }
1309
1310 const char *prefix2str(union prefixconstptr pu, char *str, int size)
1311 {
1312 const struct prefix *p = pu.p;
1313 char buf[PREFIX2STR_BUFFER];
1314
1315 switch (p->family) {
1316 case AF_INET:
1317 case AF_INET6:
1318 snprintf(str, size, "%s/%d", inet_ntop(p->family, &p->u.prefix,
1319 buf, PREFIX2STR_BUFFER),
1320 p->prefixlen);
1321 break;
1322
1323 case AF_ETHERNET:
1324 snprintf(str, size, "%s/%d",
1325 prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)),
1326 p->prefixlen);
1327 break;
1328
1329 case AF_EVPN:
1330 prefixevpn2str((const struct prefix_evpn *)p, str, size);
1331 break;
1332
1333 case AF_FLOWSPEC:
1334 sprintf(str, "FS prefix");
1335 break;
1336
1337 default:
1338 sprintf(str, "UNK prefix");
1339 break;
1340 }
1341
1342 return str;
1343 }
1344
1345 struct prefix *prefix_new()
1346 {
1347 struct prefix *p;
1348
1349 p = XCALLOC(MTYPE_PREFIX, sizeof *p);
1350 return p;
1351 }
1352
1353 /* Free prefix structure. */
1354 void prefix_free(struct prefix *p)
1355 {
1356 XFREE(MTYPE_PREFIX, p);
1357 }
1358
1359 /* Utility function. Check the string only contains digit
1360 * character.
1361 * FIXME str.[c|h] would be better place for this function. */
1362 int all_digit(const char *str)
1363 {
1364 for (; *str != '\0'; str++)
1365 if (!isdigit((int)*str))
1366 return 0;
1367 return 1;
1368 }
1369
1370 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1371 void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
1372 {
1373
1374 uint32_t destination;
1375
1376 destination = ntohl(p->prefix.s_addr);
1377
1378 if (p->prefixlen == IPV4_MAX_PREFIXLEN)
1379 ;
1380 /* do nothing for host routes */
1381 else if (IN_CLASSC(destination)) {
1382 p->prefixlen = 24;
1383 apply_mask_ipv4(p);
1384 } else if (IN_CLASSB(destination)) {
1385 p->prefixlen = 16;
1386 apply_mask_ipv4(p);
1387 } else {
1388 p->prefixlen = 8;
1389 apply_mask_ipv4(p);
1390 }
1391 }
1392
1393 in_addr_t ipv4_network_addr(in_addr_t hostaddr, int masklen)
1394 {
1395 struct in_addr mask;
1396
1397 masklen2ip(masklen, &mask);
1398 return hostaddr & mask.s_addr;
1399 }
1400
1401 in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
1402 {
1403 struct in_addr mask;
1404
1405 masklen2ip(masklen, &mask);
1406 return (masklen != IPV4_MAX_PREFIXLEN - 1) ?
1407 /* normal case */
1408 (hostaddr | ~mask.s_addr)
1409 :
1410 /* special case for /31 */
1411 (hostaddr ^ ~mask.s_addr);
1412 }
1413
1414 /* Utility function to convert ipv4 netmask to prefixes
1415 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1416 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1417 int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1418 char *prefix_str)
1419 {
1420 struct in_addr network;
1421 struct in_addr mask;
1422 uint8_t prefixlen;
1423 uint32_t destination;
1424 int ret;
1425
1426 ret = inet_aton(net_str, &network);
1427 if (!ret)
1428 return 0;
1429
1430 if (mask_str) {
1431 ret = inet_aton(mask_str, &mask);
1432 if (!ret)
1433 return 0;
1434
1435 prefixlen = ip_masklen(mask);
1436 } else {
1437 destination = ntohl(network.s_addr);
1438
1439 if (network.s_addr == 0)
1440 prefixlen = 0;
1441 else if (IN_CLASSC(destination))
1442 prefixlen = 24;
1443 else if (IN_CLASSB(destination))
1444 prefixlen = 16;
1445 else if (IN_CLASSA(destination))
1446 prefixlen = 8;
1447 else
1448 return 0;
1449 }
1450
1451 sprintf(prefix_str, "%s/%d", net_str, prefixlen);
1452
1453 return 1;
1454 }
1455
1456 /* Utility function for making IPv6 address string. */
1457 const char *inet6_ntoa(struct in6_addr addr)
1458 {
1459 static char buf[INET6_ADDRSTRLEN];
1460
1461 inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1462 return buf;
1463 }
1464
1465 /* converts to internal representation of mac address
1466 * returns 1 on success, 0 otherwise
1467 * format accepted: AA:BB:CC:DD:EE:FF
1468 * if mac parameter is null, then check only
1469 */
1470 int prefix_str2mac(const char *str, struct ethaddr *mac)
1471 {
1472 unsigned int a[6];
1473 int i;
1474
1475 if (!str)
1476 return 0;
1477
1478 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3,
1479 a + 4, a + 5)
1480 != 6) {
1481 /* error in incoming str length */
1482 return 0;
1483 }
1484 /* valid mac address */
1485 if (!mac)
1486 return 1;
1487 for (i = 0; i < 6; ++i)
1488 mac->octet[i] = a[i] & 0xff;
1489 return 1;
1490 }
1491
1492 char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
1493 {
1494 char *ptr;
1495
1496 if (!mac)
1497 return NULL;
1498 if (!buf)
1499 ptr = (char *)XMALLOC(MTYPE_TMP,
1500 ETHER_ADDR_STRLEN * sizeof(char));
1501 else {
1502 assert(size >= ETHER_ADDR_STRLEN);
1503 ptr = buf;
1504 }
1505 snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x",
1506 (uint8_t)mac->octet[0], (uint8_t)mac->octet[1],
1507 (uint8_t)mac->octet[2], (uint8_t)mac->octet[3],
1508 (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]);
1509 return ptr;
1510 }
1511
1512 unsigned prefix_hash_key(void *pp)
1513 {
1514 struct prefix copy;
1515
1516 if (((struct prefix *)pp)->family == AF_FLOWSPEC) {
1517 uint32_t len;
1518 void *temp;
1519
1520 /* make sure *all* unused bits are zero,
1521 * particularly including alignment /
1522 * padding and unused prefix bytes.
1523 */
1524 memset(&copy, 0, sizeof(copy));
1525 prefix_copy(&copy, (struct prefix *)pp);
1526 len = jhash((void *)copy.u.prefix_flowspec.ptr,
1527 copy.u.prefix_flowspec.prefixlen,
1528 0x55aa5a5a);
1529 temp = (void *)copy.u.prefix_flowspec.ptr;
1530 XFREE(MTYPE_PREFIX_FLOWSPEC, temp);
1531 copy.u.prefix_flowspec.ptr = (uintptr_t)NULL;
1532 return len;
1533 }
1534 /* make sure *all* unused bits are zero, particularly including
1535 * alignment /
1536 * padding and unused prefix bytes. */
1537 memset(&copy, 0, sizeof(copy));
1538 prefix_copy(&copy, (struct prefix *)pp);
1539 return jhash(&copy,
1540 offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen),
1541 0x55aa5a5a);
1542 }