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