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