]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
debianpkg: Remove -werror from Ubuntu 14.04 and 12.04 build to skip warnings from...
[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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24
25 #include "prefix.h"
26 #include "vty.h"
27 #include "sockunion.h"
28 #include "memory.h"
29 #include "log.h"
30
31 DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix")
32
33 /* Maskbit. */
34 static const u_char 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 unsigned int prefix_bit(const u_char *prefix, const u_char prefixlen)
433 {
434 unsigned int offset = prefixlen / 8;
435 unsigned int shift = 7 - (prefixlen % 8);
436
437 return (prefix[offset] >> shift) & 1;
438 }
439
440 unsigned int prefix6_bit(const struct in6_addr *prefix, const u_char prefixlen)
441 {
442 return prefix_bit((const u_char *)&prefix->s6_addr, prefixlen);
443 }
444
445 int str2family(const char *string)
446 {
447 if (!strcmp("ipv4", string))
448 return AF_INET;
449 else if (!strcmp("ipv6", string))
450 return AF_INET6;
451 else if (!strcmp("ethernet", string))
452 return AF_ETHERNET;
453 return -1;
454 }
455
456 /* Address Famiy Identifier to Address Family converter. */
457 int afi2family(afi_t afi)
458 {
459 if (afi == AFI_IP)
460 return AF_INET;
461 else if (afi == AFI_IP6)
462 return AF_INET6;
463 else if (afi == AFI_L2VPN)
464 return AF_ETHERNET;
465 return 0;
466 }
467
468 afi_t family2afi(int family)
469 {
470 if (family == AF_INET)
471 return AFI_IP;
472 else if (family == AF_INET6)
473 return AFI_IP6;
474 else if (family == AF_ETHERNET)
475 return AFI_L2VPN;
476 return 0;
477 }
478
479 const char *afi2str(afi_t afi)
480 {
481 switch (afi) {
482 case AFI_IP:
483 return "IPv4";
484 case AFI_IP6:
485 return "IPv6";
486 case AFI_L2VPN:
487 return "l2vpn";
488 case AFI_MAX:
489 return "bad-value";
490 default:
491 break;
492 }
493 return NULL;
494 }
495
496 const char *safi2str(safi_t safi)
497 {
498 switch (safi) {
499 case SAFI_UNICAST:
500 return "unicast";
501 case SAFI_MULTICAST:
502 return "multicast";
503 case SAFI_ENCAP:
504 return "encap";
505 case SAFI_MPLS_VPN:
506 return "vpn";
507 case SAFI_EVPN:
508 return "evpn";
509 }
510 return NULL;
511 }
512
513 /* If n includes p prefix then return 1 else return 0. */
514 int prefix_match(const struct prefix *n, const struct prefix *p)
515 {
516 int offset;
517 int shift;
518 const u_char *np, *pp;
519
520 /* If n's prefix is longer than p's one return 0. */
521 if (n->prefixlen > p->prefixlen)
522 return 0;
523
524 /* Set both prefix's head pointer. */
525 np = (const u_char *)&n->u.prefix;
526 pp = (const u_char *)&p->u.prefix;
527
528 offset = n->prefixlen / PNBBY;
529 shift = n->prefixlen % PNBBY;
530
531 if (shift)
532 if (maskbit[shift] & (np[offset] ^ pp[offset]))
533 return 0;
534
535 while (offset--)
536 if (np[offset] != pp[offset])
537 return 0;
538 return 1;
539 }
540
541 /* Copy prefix from src to dest. */
542 void prefix_copy(struct prefix *dest, const struct prefix *src)
543 {
544 dest->family = src->family;
545 dest->prefixlen = src->prefixlen;
546
547 if (src->family == AF_INET)
548 dest->u.prefix4 = src->u.prefix4;
549 else if (src->family == AF_INET6)
550 dest->u.prefix6 = src->u.prefix6;
551 else if (src->family == AF_ETHERNET) {
552 memcpy(&dest->u.prefix_evpn, &src->u.prefix_evpn,
553 sizeof(struct evpn_addr));
554 } else if (src->family == AF_UNSPEC) {
555 dest->u.lp.id = src->u.lp.id;
556 dest->u.lp.adv_router = src->u.lp.adv_router;
557 } else {
558 zlog_err("prefix_copy(): Unknown address family %d",
559 src->family);
560 assert(0);
561 }
562 }
563
564 /*
565 * Return 1 if the address/netmask contained in the prefix structure
566 * is the same, and else return 0. For this routine, 'same' requires
567 * that not only the prefix length and the network part be the same,
568 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
569 * the same. Note that this routine has the same return value sense
570 * as '==' (which is different from prefix_cmp).
571 */
572 int prefix_same(const struct prefix *p1, const struct prefix *p2)
573 {
574 if ((p1 && !p2) || (!p1 && p2))
575 return 0;
576
577 if (!p1 && !p2)
578 return 1;
579
580 if (p1->family == p2->family && p1->prefixlen == p2->prefixlen) {
581 if (p1->family == AF_INET)
582 if (IPV4_ADDR_SAME(&p1->u.prefix4.s_addr,
583 &p2->u.prefix4.s_addr))
584 return 1;
585 if (p1->family == AF_INET6)
586 if (IPV6_ADDR_SAME(&p1->u.prefix6.s6_addr,
587 &p2->u.prefix6.s6_addr))
588 return 1;
589 if (p1->family == AF_ETHERNET)
590 if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn,
591 sizeof(struct evpn_addr)))
592 return 1;
593 }
594 return 0;
595 }
596
597 /*
598 * Return 0 if the network prefixes represented by the struct prefix
599 * arguments are the same prefix, and 1 otherwise. Network prefixes
600 * are considered the same if the prefix lengths are equal and the
601 * network parts are the same. Host bits (which are considered masked
602 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
603 * 10.0.0.2/8 are considered equivalent by this routine. Note that
604 * this routine has the same return sense as strcmp (which is different
605 * from prefix_same).
606 */
607 int prefix_cmp(const struct prefix *p1, const struct prefix *p2)
608 {
609 int offset;
610 int shift;
611
612 /* Set both prefix's head pointer. */
613 const u_char *pp1 = (const u_char *)&p1->u.prefix;
614 const u_char *pp2 = (const u_char *)&p2->u.prefix;
615
616 if (p1->family != p2->family || p1->prefixlen != p2->prefixlen)
617 return 1;
618
619 offset = p1->prefixlen / PNBBY;
620 shift = p1->prefixlen % PNBBY;
621
622 if (shift)
623 if (maskbit[shift] & (pp1[offset] ^ pp2[offset]))
624 return 1;
625
626 while (offset--)
627 if (pp1[offset] != pp2[offset])
628 return 1;
629
630 return 0;
631 }
632
633 /*
634 * Count the number of common bits in 2 prefixes. The prefix length is
635 * ignored for this function; the whole prefix is compared. If the prefix
636 * address families don't match, return -1; otherwise the return value is
637 * in range 0 ... maximum prefix length for the address family.
638 */
639 int prefix_common_bits(const struct prefix *p1, const struct prefix *p2)
640 {
641 int pos, bit;
642 int length = 0;
643 u_char xor ;
644
645 /* Set both prefix's head pointer. */
646 const u_char *pp1 = (const u_char *)&p1->u.prefix;
647 const u_char *pp2 = (const u_char *)&p2->u.prefix;
648
649 if (p1->family == AF_INET)
650 length = IPV4_MAX_BYTELEN;
651 if (p1->family == AF_INET6)
652 length = IPV6_MAX_BYTELEN;
653 if (p1->family == AF_ETHERNET)
654 length = 8 * sizeof(struct evpn_addr);
655
656 if (p1->family != p2->family || !length)
657 return -1;
658
659 for (pos = 0; pos < length; pos++)
660 if (pp1[pos] != pp2[pos])
661 break;
662 if (pos == length)
663 return pos * 8;
664
665 xor = pp1[pos] ^ pp2[pos];
666 for (bit = 0; bit < 8; bit++)
667 if (xor&(1 << (7 - bit)))
668 break;
669
670 return pos * 8 + bit;
671 }
672
673 /* Return prefix family type string. */
674 const char *prefix_family_str(const struct prefix *p)
675 {
676 if (p->family == AF_INET)
677 return "inet";
678 if (p->family == AF_INET6)
679 return "inet6";
680 if (p->family == AF_ETHERNET)
681 return "ether";
682 return "unspec";
683 }
684
685 /* Allocate new prefix_ipv4 structure. */
686 struct prefix_ipv4 *prefix_ipv4_new()
687 {
688 struct prefix_ipv4 *p;
689
690 /* Call prefix_new to allocate a full-size struct prefix to avoid
691 problems
692 where the struct prefix_ipv4 is cast to struct prefix and unallocated
693 bytes were being referenced (e.g. in structure assignments). */
694 p = (struct prefix_ipv4 *)prefix_new();
695 p->family = AF_INET;
696 return p;
697 }
698
699 /* Free prefix_ipv4 structure. */
700 void prefix_ipv4_free(struct prefix_ipv4 *p)
701 {
702 prefix_free((struct prefix *)p);
703 }
704
705 /* When string format is invalid return 0. */
706 int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p)
707 {
708 int ret;
709 int plen;
710 char *pnt;
711 char *cp;
712
713 /* Find slash inside string. */
714 pnt = strchr(str, '/');
715
716 /* String doesn't contail slash. */
717 if (pnt == NULL) {
718 /* Convert string to prefix. */
719 ret = inet_aton(str, &p->prefix);
720 if (ret == 0)
721 return 0;
722
723 /* If address doesn't contain slash we assume it host address.
724 */
725 p->family = AF_INET;
726 p->prefixlen = IPV4_MAX_BITLEN;
727
728 return ret;
729 } else {
730 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
731 strncpy(cp, str, pnt - str);
732 *(cp + (pnt - str)) = '\0';
733 ret = inet_aton(cp, &p->prefix);
734 XFREE(MTYPE_TMP, cp);
735
736 /* Get prefix length. */
737 plen = (u_char)atoi(++pnt);
738 if (plen > IPV4_MAX_PREFIXLEN)
739 return 0;
740
741 p->family = AF_INET;
742 p->prefixlen = plen;
743 }
744
745 return ret;
746 }
747
748 /* When string format is invalid return 0. */
749 int str2prefix_eth(const char *str, struct prefix_eth *p)
750 {
751 int ret = 0;
752 int plen = 48;
753 char *pnt;
754 char *cp = NULL;
755 const char *str_addr = str;
756 unsigned int a[6];
757 int i;
758
759 /* Find slash inside string. */
760 pnt = strchr(str, '/');
761
762 if (pnt) {
763 /* Get prefix length. */
764 plen = (u_char)atoi(++pnt);
765 if (plen > 48) {
766 ret = 0;
767 goto done;
768 }
769
770 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
771 strncpy(cp, str, pnt - str);
772 *(cp + (pnt - str)) = '\0';
773
774 str_addr = cp;
775 }
776
777 /* Convert string to prefix. */
778 if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2,
779 a + 3, a + 4, a + 5)
780 != 6) {
781 ret = 0;
782 goto done;
783 }
784 for (i = 0; i < 6; ++i) {
785 p->eth_addr.octet[i] = a[i] & 0xff;
786 }
787 p->prefixlen = plen;
788 p->family = AF_ETHERNET;
789 ret = 1;
790
791 done:
792 if (cp)
793 XFREE(MTYPE_TMP, cp);
794
795 return ret;
796 }
797
798 /* Convert masklen into IP address's netmask (network byte order). */
799 void masklen2ip(const int masklen, struct in_addr *netmask)
800 {
801 assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
802
803 /* left shift is only defined for less than the size of the type.
804 * we unconditionally use long long in case the target platform
805 * has defined behaviour for << 32 (or has a 64-bit left shift) */
806
807 if (sizeof(unsigned long long) > 4)
808 netmask->s_addr = htonl(0xffffffffULL << (32 - masklen));
809 else
810 netmask->s_addr =
811 htonl(masklen ? 0xffffffffU << (32 - masklen) : 0);
812 }
813
814 /* Convert IP address's netmask into integer. We assume netmask is
815 sequential one. Argument netmask should be network byte order. */
816 u_char ip_masklen(struct in_addr netmask)
817 {
818 uint32_t tmp = ~ntohl(netmask.s_addr);
819 if (tmp)
820 /* clz: count leading zeroes. sadly, the behaviour of this
821 * builtin
822 * is undefined for a 0 argument, even though most CPUs give 32
823 */
824 return __builtin_clz(tmp);
825 else
826 return 32;
827 }
828
829 /* Apply mask to IPv4 prefix (network byte order). */
830 void apply_mask_ipv4(struct prefix_ipv4 *p)
831 {
832 struct in_addr mask;
833 masklen2ip(p->prefixlen, &mask);
834 p->prefix.s_addr &= mask.s_addr;
835 }
836
837 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
838 int prefix_ipv4_any(const struct prefix_ipv4 *p)
839 {
840 return (p->prefix.s_addr == 0 && p->prefixlen == 0);
841 }
842
843 /* Allocate a new ip version 6 route */
844 struct prefix_ipv6 *prefix_ipv6_new(void)
845 {
846 struct prefix_ipv6 *p;
847
848 /* Allocate a full-size struct prefix to avoid problems with structure
849 size mismatches. */
850 p = (struct prefix_ipv6 *)prefix_new();
851 p->family = AF_INET6;
852 return p;
853 }
854
855 /* Free prefix for IPv6. */
856 void prefix_ipv6_free(struct prefix_ipv6 *p)
857 {
858 prefix_free((struct prefix *)p);
859 }
860
861 /* If given string is valid return pin6 else return NULL */
862 int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p)
863 {
864 char *pnt;
865 char *cp;
866 int ret;
867
868 pnt = strchr(str, '/');
869
870 /* If string doesn't contain `/' treat it as host route. */
871 if (pnt == NULL) {
872 ret = inet_pton(AF_INET6, str, &p->prefix);
873 if (ret == 0)
874 return 0;
875 p->prefixlen = IPV6_MAX_BITLEN;
876 } else {
877 int plen;
878
879 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
880 strncpy(cp, str, pnt - str);
881 *(cp + (pnt - str)) = '\0';
882 ret = inet_pton(AF_INET6, cp, &p->prefix);
883 XFREE(MTYPE_TMP, cp);
884 if (ret == 0)
885 return 0;
886 plen = (u_char)atoi(++pnt);
887 if (plen > IPV6_MAX_BITLEN)
888 return 0;
889 p->prefixlen = plen;
890 }
891 p->family = AF_INET6;
892
893 return ret;
894 }
895
896 /* Convert struct in6_addr netmask into integer.
897 * FIXME return u_char as ip_maskleni() does. */
898 int ip6_masklen(struct in6_addr netmask)
899 {
900 int len = 0;
901 unsigned char val;
902 unsigned char *pnt;
903
904 pnt = (unsigned char *)&netmask;
905
906 while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN) {
907 len += 8;
908 pnt++;
909 }
910
911 if (len < IPV6_MAX_BITLEN) {
912 val = *pnt;
913 while (val) {
914 len++;
915 val <<= 1;
916 }
917 }
918 return len;
919 }
920
921 void masklen2ip6(const int masklen, struct in6_addr *netmask)
922 {
923 assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
924 memcpy(netmask, maskbytes6 + masklen, sizeof(struct in6_addr));
925 }
926
927 void apply_mask_ipv6(struct prefix_ipv6 *p)
928 {
929 u_char *pnt;
930 int index;
931 int offset;
932
933 index = p->prefixlen / 8;
934
935 if (index < 16) {
936 pnt = (u_char *)&p->prefix;
937 offset = p->prefixlen % 8;
938
939 pnt[index] &= maskbit[offset];
940 index++;
941
942 while (index < 16)
943 pnt[index++] = 0;
944 }
945 }
946
947 void apply_mask(struct prefix *p)
948 {
949 switch (p->family) {
950 case AF_INET:
951 apply_mask_ipv4((struct prefix_ipv4 *)p);
952 break;
953 case AF_INET6:
954 apply_mask_ipv6((struct prefix_ipv6 *)p);
955 break;
956 default:
957 break;
958 }
959 return;
960 }
961
962 /* Utility function of convert between struct prefix <=> union sockunion.
963 * FIXME This function isn't used anywhere. */
964 struct prefix *sockunion2prefix(const union sockunion *dest,
965 const union sockunion *mask)
966 {
967 if (dest->sa.sa_family == AF_INET) {
968 struct prefix_ipv4 *p;
969
970 p = prefix_ipv4_new();
971 p->family = AF_INET;
972 p->prefix = dest->sin.sin_addr;
973 p->prefixlen = ip_masklen(mask->sin.sin_addr);
974 return (struct prefix *)p;
975 }
976 if (dest->sa.sa_family == AF_INET6) {
977 struct prefix_ipv6 *p;
978
979 p = prefix_ipv6_new();
980 p->family = AF_INET6;
981 p->prefixlen = ip6_masklen(mask->sin6.sin6_addr);
982 memcpy(&p->prefix, &dest->sin6.sin6_addr,
983 sizeof(struct in6_addr));
984 return (struct prefix *)p;
985 }
986 return NULL;
987 }
988
989 /* Utility function of convert between struct prefix <=> union sockunion. */
990 struct prefix *sockunion2hostprefix(const union sockunion *su,
991 struct prefix *prefix)
992 {
993 if (su->sa.sa_family == AF_INET) {
994 struct prefix_ipv4 *p;
995
996 p = prefix ? (struct prefix_ipv4 *)prefix : prefix_ipv4_new();
997 p->family = AF_INET;
998 p->prefix = su->sin.sin_addr;
999 p->prefixlen = IPV4_MAX_BITLEN;
1000 return (struct prefix *)p;
1001 }
1002 if (su->sa.sa_family == AF_INET6) {
1003 struct prefix_ipv6 *p;
1004
1005 p = prefix ? (struct prefix_ipv6 *)prefix : prefix_ipv6_new();
1006 p->family = AF_INET6;
1007 p->prefixlen = IPV6_MAX_BITLEN;
1008 memcpy(&p->prefix, &su->sin6.sin6_addr,
1009 sizeof(struct in6_addr));
1010 return (struct prefix *)p;
1011 }
1012 return NULL;
1013 }
1014
1015 void prefix2sockunion(const struct prefix *p, union sockunion *su)
1016 {
1017 memset(su, 0, sizeof(*su));
1018
1019 su->sa.sa_family = p->family;
1020 if (p->family == AF_INET)
1021 su->sin.sin_addr = p->u.prefix4;
1022 if (p->family == AF_INET6)
1023 memcpy(&su->sin6.sin6_addr, &p->u.prefix6,
1024 sizeof(struct in6_addr));
1025 }
1026
1027 int prefix_blen(const struct prefix *p)
1028 {
1029 switch (p->family) {
1030 case AF_INET:
1031 return IPV4_MAX_BYTELEN;
1032 break;
1033 case AF_INET6:
1034 return IPV6_MAX_BYTELEN;
1035 break;
1036 case AF_ETHERNET:
1037 return ETHER_ADDR_LEN;
1038 }
1039 return 0;
1040 }
1041
1042 /* Generic function for conversion string to struct prefix. */
1043 int str2prefix(const char *str, struct prefix *p)
1044 {
1045 int ret;
1046
1047 /* First we try to convert string to struct prefix_ipv4. */
1048 ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p);
1049 if (ret)
1050 return ret;
1051
1052 /* Next we try to convert string to struct prefix_ipv6. */
1053 ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p);
1054 if (ret)
1055 return ret;
1056
1057 /* Next we try to convert string to struct prefix_eth. */
1058 ret = str2prefix_eth(str, (struct prefix_eth *)p);
1059 if (ret)
1060 return ret;
1061
1062 return 0;
1063 }
1064
1065 const char *prefix2str(union prefixconstptr pu, char *str, int size)
1066 {
1067 const struct prefix *p = pu.p;
1068 char buf[PREFIX2STR_BUFFER];
1069
1070 switch (p->family) {
1071 case AF_INET:
1072 case AF_INET6:
1073 snprintf(str, size, "%s/%d", inet_ntop(p->family, &p->u.prefix,
1074 buf, PREFIX2STR_BUFFER),
1075 p->prefixlen);
1076 break;
1077
1078 case AF_ETHERNET:
1079 if (p->u.prefix_evpn.route_type == 5) {
1080 u_char family;
1081 family = (p->u.prefix_evpn.flags
1082 & (IP_ADDR_V4 | IP_PREFIX_V4))
1083 ? AF_INET
1084 : AF_INET6;
1085 snprintf(str, size, "[%d]:[%u][%s]/%d",
1086 p->u.prefix_evpn.route_type,
1087 p->u.prefix_evpn.eth_tag,
1088 inet_ntop(family, &p->u.prefix_evpn.ip.addr,
1089 buf, PREFIX2STR_BUFFER),
1090 p->prefixlen);
1091 } else {
1092 sprintf(str, "UNK AF_ETHER prefix");
1093 snprintf(str, size, "%02x:%02x:%02x:%02x:%02x:%02x/%d",
1094 p->u.prefix_eth.octet[0],
1095 p->u.prefix_eth.octet[1],
1096 p->u.prefix_eth.octet[2],
1097 p->u.prefix_eth.octet[3],
1098 p->u.prefix_eth.octet[4],
1099 p->u.prefix_eth.octet[5], p->prefixlen);
1100 }
1101 break;
1102 default:
1103 sprintf(str, "UNK prefix");
1104 break;
1105 }
1106
1107 return str;
1108 }
1109
1110 struct prefix *prefix_new()
1111 {
1112 struct prefix *p;
1113
1114 p = XCALLOC(MTYPE_PREFIX, sizeof *p);
1115 return p;
1116 }
1117
1118 /* Free prefix structure. */
1119 void prefix_free(struct prefix *p)
1120 {
1121 XFREE(MTYPE_PREFIX, p);
1122 }
1123
1124 /* Utility function. Check the string only contains digit
1125 * character.
1126 * FIXME str.[c|h] would be better place for this function. */
1127 int all_digit(const char *str)
1128 {
1129 for (; *str != '\0'; str++)
1130 if (!isdigit((int)*str))
1131 return 0;
1132 return 1;
1133 }
1134
1135 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1136 void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
1137 {
1138
1139 u_int32_t destination;
1140
1141 destination = ntohl(p->prefix.s_addr);
1142
1143 if (p->prefixlen == IPV4_MAX_PREFIXLEN)
1144 ;
1145 /* do nothing for host routes */
1146 else if (IN_CLASSC(destination)) {
1147 p->prefixlen = 24;
1148 apply_mask_ipv4(p);
1149 } else if (IN_CLASSB(destination)) {
1150 p->prefixlen = 16;
1151 apply_mask_ipv4(p);
1152 } else {
1153 p->prefixlen = 8;
1154 apply_mask_ipv4(p);
1155 }
1156 }
1157
1158 in_addr_t ipv4_network_addr(in_addr_t hostaddr, int masklen)
1159 {
1160 struct in_addr mask;
1161
1162 masklen2ip(masklen, &mask);
1163 return hostaddr & mask.s_addr;
1164 }
1165
1166 in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
1167 {
1168 struct in_addr mask;
1169
1170 masklen2ip(masklen, &mask);
1171 return (masklen != IPV4_MAX_PREFIXLEN - 1) ?
1172 /* normal case */
1173 (hostaddr | ~mask.s_addr)
1174 :
1175 /* special case for /31 */
1176 (hostaddr ^ ~mask.s_addr);
1177 }
1178
1179 /* Utility function to convert ipv4 netmask to prefixes
1180 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1181 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1182 int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1183 char *prefix_str)
1184 {
1185 struct in_addr network;
1186 struct in_addr mask;
1187 u_char prefixlen;
1188 u_int32_t destination;
1189 int ret;
1190
1191 ret = inet_aton(net_str, &network);
1192 if (!ret)
1193 return 0;
1194
1195 if (mask_str) {
1196 ret = inet_aton(mask_str, &mask);
1197 if (!ret)
1198 return 0;
1199
1200 prefixlen = ip_masklen(mask);
1201 } else {
1202 destination = ntohl(network.s_addr);
1203
1204 if (network.s_addr == 0)
1205 prefixlen = 0;
1206 else if (IN_CLASSC(destination))
1207 prefixlen = 24;
1208 else if (IN_CLASSB(destination))
1209 prefixlen = 16;
1210 else if (IN_CLASSA(destination))
1211 prefixlen = 8;
1212 else
1213 return 0;
1214 }
1215
1216 sprintf(prefix_str, "%s/%d", net_str, prefixlen);
1217
1218 return 1;
1219 }
1220
1221 /* Utility function for making IPv6 address string. */
1222 const char *inet6_ntoa(struct in6_addr addr)
1223 {
1224 static char buf[INET6_ADDRSTRLEN];
1225
1226 inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1227 return buf;
1228 }
1229
1230 /* converts to internal representation of mac address
1231 * returns 1 on success, 0 otherwise
1232 * format accepted: AA:BB:CC:DD:EE:FF
1233 * if mac parameter is null, then check only
1234 */
1235 int prefix_str2mac(const char *str, struct ethaddr *mac)
1236 {
1237 unsigned int a[6];
1238 int i;
1239
1240 if (!str)
1241 return 0;
1242
1243 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3,
1244 a + 4, a + 5)
1245 != 6) {
1246 /* error in incoming str length */
1247 return 0;
1248 }
1249 /* valid mac address */
1250 if (!mac)
1251 return 1;
1252 for (i = 0; i < 6; ++i)
1253 mac->octet[i] = a[i] & 0xff;
1254 return 1;
1255 }
1256
1257 char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
1258 {
1259 char *ptr;
1260
1261 if (!mac)
1262 return NULL;
1263 if (!buf)
1264 ptr = (char *)XMALLOC(MTYPE_TMP,
1265 ETHER_ADDR_STRLEN * sizeof(char));
1266 else {
1267 assert(size >= ETHER_ADDR_STRLEN);
1268 ptr = buf;
1269 }
1270 snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x",
1271 (uint8_t)mac->octet[0], (uint8_t)mac->octet[1],
1272 (uint8_t)mac->octet[2], (uint8_t)mac->octet[3],
1273 (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]);
1274 return ptr;
1275 }