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