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