]>
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 | ||
d62a17ae | 631 | void prefix_copy(struct prefix *dest, const struct prefix *src) |
718e3744 | 632 | { |
d62a17ae | 633 | dest->family = src->family; |
634 | dest->prefixlen = src->prefixlen; | |
635 | ||
636 | if (src->family == AF_INET) | |
637 | dest->u.prefix4 = src->u.prefix4; | |
638 | else if (src->family == AF_INET6) | |
639 | dest->u.prefix6 = src->u.prefix6; | |
640 | else if (src->family == AF_ETHERNET) { | |
b03b8898 DS |
641 | memcpy(&dest->u.prefix_eth, &src->u.prefix_eth, |
642 | sizeof(struct ethaddr)); | |
643 | } else if (src->family == AF_EVPN) { | |
d62a17ae | 644 | memcpy(&dest->u.prefix_evpn, &src->u.prefix_evpn, |
645 | sizeof(struct evpn_addr)); | |
646 | } else if (src->family == AF_UNSPEC) { | |
647 | dest->u.lp.id = src->u.lp.id; | |
648 | dest->u.lp.adv_router = src->u.lp.adv_router; | |
9a14899b PG |
649 | } else if (src->family == AF_FLOWSPEC) { |
650 | void *temp; | |
651 | int len; | |
652 | ||
653 | len = src->u.prefix_flowspec.prefixlen; | |
654 | dest->u.prefix_flowspec.prefixlen = | |
655 | src->u.prefix_flowspec.prefixlen; | |
656 | dest->family = src->family; | |
657 | temp = XCALLOC(MTYPE_PREFIX_FLOWSPEC, len); | |
658 | dest->u.prefix_flowspec.ptr = (uintptr_t)temp; | |
659 | memcpy((void *)dest->u.prefix_flowspec.ptr, | |
660 | (void *)src->u.prefix_flowspec.ptr, len); | |
d62a17ae | 661 | } else { |
450971aa | 662 | flog_err(EC_LIB_DEVELOPMENT, |
1c50c1c0 QY |
663 | "prefix_copy(): Unknown address family %d", |
664 | src->family); | |
d62a17ae | 665 | assert(0); |
666 | } | |
718e3744 | 667 | } |
668 | ||
d62a17ae | 669 | /* |
9d24baaa | 670 | * Return 1 if the address/netmask contained in the prefix structure |
671 | * is the same, and else return 0. For this routine, 'same' requires | |
672 | * that not only the prefix length and the network part be the same, | |
673 | * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not | |
674 | * the same. Note that this routine has the same return value sense | |
675 | * as '==' (which is different from prefix_cmp). | |
676 | */ | |
d62a17ae | 677 | int prefix_same(const struct prefix *p1, const struct prefix *p2) |
718e3744 | 678 | { |
d62a17ae | 679 | if ((p1 && !p2) || (!p1 && p2)) |
680 | return 0; | |
681 | ||
682 | if (!p1 && !p2) | |
683 | return 1; | |
684 | ||
685 | if (p1->family == p2->family && p1->prefixlen == p2->prefixlen) { | |
686 | if (p1->family == AF_INET) | |
19aad877 | 687 | if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4)) |
d62a17ae | 688 | return 1; |
689 | if (p1->family == AF_INET6) | |
690 | if (IPV6_ADDR_SAME(&p1->u.prefix6.s6_addr, | |
691 | &p2->u.prefix6.s6_addr)) | |
692 | return 1; | |
693 | if (p1->family == AF_ETHERNET) | |
b03b8898 DS |
694 | if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth, |
695 | sizeof(struct ethaddr))) | |
696 | return 1; | |
697 | if (p1->family == AF_EVPN) | |
d62a17ae | 698 | if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn, |
699 | sizeof(struct evpn_addr))) | |
700 | return 1; | |
9a14899b PG |
701 | if (p1->family == AF_FLOWSPEC) { |
702 | if (p1->u.prefix_flowspec.prefixlen != | |
703 | p2->u.prefix_flowspec.prefixlen) | |
704 | return 0; | |
705 | if (!memcmp(&p1->u.prefix_flowspec.ptr, | |
706 | &p2->u.prefix_flowspec.ptr, | |
707 | p2->u.prefix_flowspec.prefixlen)) | |
708 | return 1; | |
709 | } | |
d62a17ae | 710 | } |
711 | return 0; | |
718e3744 | 712 | } |
713 | ||
9d24baaa | 714 | /* |
1315d74d DL |
715 | * Return -1/0/1 comparing the prefixes in a way that gives a full/linear |
716 | * order. | |
717 | * | |
718 | * Network prefixes are considered the same if the prefix lengths are equal | |
719 | * and the network parts are the same. Host bits (which are considered masked | |
9d24baaa | 720 | * by the prefix length) are not significant. Thus, 10.0.0.1/8 and |
721 | * 10.0.0.2/8 are considered equivalent by this routine. Note that | |
722 | * this routine has the same return sense as strcmp (which is different | |
723 | * from prefix_same). | |
724 | */ | |
d62a17ae | 725 | int prefix_cmp(const struct prefix *p1, const struct prefix *p2) |
718e3744 | 726 | { |
d62a17ae | 727 | int offset; |
728 | int shift; | |
1315d74d | 729 | int i; |
718e3744 | 730 | |
d62a17ae | 731 | /* Set both prefix's head pointer. */ |
9a14899b PG |
732 | const uint8_t *pp1; |
733 | const uint8_t *pp2; | |
718e3744 | 734 | |
9a14899b | 735 | if (p1->family != p2->family) |
1315d74d | 736 | return numcmp(p1->family, p2->family); |
9a14899b PG |
737 | if (p1->family == AF_FLOWSPEC) { |
738 | pp1 = (const uint8_t *)p1->u.prefix_flowspec.ptr; | |
739 | pp2 = (const uint8_t *)p2->u.prefix_flowspec.ptr; | |
718e3744 | 740 | |
9a14899b PG |
741 | if (p1->u.prefix_flowspec.prefixlen != |
742 | p2->u.prefix_flowspec.prefixlen) | |
1315d74d DL |
743 | return numcmp(p1->u.prefix_flowspec.prefixlen, |
744 | p2->u.prefix_flowspec.prefixlen); | |
9a14899b PG |
745 | |
746 | offset = p1->u.prefix_flowspec.prefixlen; | |
747 | while (offset--) | |
748 | if (pp1[offset] != pp2[offset]) | |
1315d74d | 749 | return numcmp(pp1[offset], pp2[offset]); |
9a14899b PG |
750 | return 0; |
751 | } | |
f0ed6bea | 752 | pp1 = p1->u.val; |
753 | pp2 = p2->u.val; | |
9a14899b PG |
754 | |
755 | if (p1->prefixlen != p2->prefixlen) | |
1315d74d | 756 | return numcmp(p1->prefixlen, p2->prefixlen); |
d62a17ae | 757 | offset = p1->prefixlen / PNBBY; |
758 | shift = p1->prefixlen % PNBBY; | |
718e3744 | 759 | |
1315d74d DL |
760 | i = memcmp(pp1, pp2, offset); |
761 | if (i) | |
762 | return i; | |
718e3744 | 763 | |
1315d74d DL |
764 | return numcmp(pp1[offset] & maskbit[shift], |
765 | pp2[offset] & maskbit[shift]); | |
718e3744 | 766 | } |
767 | ||
17e52061 DL |
768 | /* |
769 | * Count the number of common bits in 2 prefixes. The prefix length is | |
770 | * ignored for this function; the whole prefix is compared. If the prefix | |
771 | * address families don't match, return -1; otherwise the return value is | |
772 | * in range 0 ... maximum prefix length for the address family. | |
773 | */ | |
d62a17ae | 774 | int prefix_common_bits(const struct prefix *p1, const struct prefix *p2) |
17e52061 | 775 | { |
d62a17ae | 776 | int pos, bit; |
777 | int length = 0; | |
d7c0a89a | 778 | uint8_t xor ; |
d62a17ae | 779 | |
780 | /* Set both prefix's head pointer. */ | |
f0ed6bea | 781 | const uint8_t *pp1 = p1->u.val; |
782 | const uint8_t *pp2 = p2->u.val; | |
d62a17ae | 783 | |
784 | if (p1->family == AF_INET) | |
785 | length = IPV4_MAX_BYTELEN; | |
786 | if (p1->family == AF_INET6) | |
787 | length = IPV6_MAX_BYTELEN; | |
788 | if (p1->family == AF_ETHERNET) | |
b03b8898 DS |
789 | length = ETH_ALEN; |
790 | if (p1->family == AF_EVPN) | |
d62a17ae | 791 | length = 8 * sizeof(struct evpn_addr); |
792 | ||
793 | if (p1->family != p2->family || !length) | |
794 | return -1; | |
795 | ||
796 | for (pos = 0; pos < length; pos++) | |
797 | if (pp1[pos] != pp2[pos]) | |
798 | break; | |
799 | if (pos == length) | |
800 | return pos * 8; | |
801 | ||
802 | xor = pp1[pos] ^ pp2[pos]; | |
803 | for (bit = 0; bit < 8; bit++) | |
804 | if (xor&(1 << (7 - bit))) | |
805 | break; | |
806 | ||
807 | return pos * 8 + bit; | |
17e52061 DL |
808 | } |
809 | ||
718e3744 | 810 | /* Return prefix family type string. */ |
d62a17ae | 811 | const char *prefix_family_str(const struct prefix *p) |
718e3744 | 812 | { |
d62a17ae | 813 | if (p->family == AF_INET) |
814 | return "inet"; | |
815 | if (p->family == AF_INET6) | |
816 | return "inet6"; | |
817 | if (p->family == AF_ETHERNET) | |
818 | return "ether"; | |
b03b8898 DS |
819 | if (p->family == AF_EVPN) |
820 | return "evpn"; | |
d62a17ae | 821 | return "unspec"; |
718e3744 | 822 | } |
823 | ||
824 | /* Allocate new prefix_ipv4 structure. */ | |
4d762f26 | 825 | struct prefix_ipv4 *prefix_ipv4_new(void) |
718e3744 | 826 | { |
d62a17ae | 827 | struct prefix_ipv4 *p; |
828 | ||
829 | /* Call prefix_new to allocate a full-size struct prefix to avoid | |
830 | problems | |
831 | where the struct prefix_ipv4 is cast to struct prefix and unallocated | |
832 | bytes were being referenced (e.g. in structure assignments). */ | |
833 | p = (struct prefix_ipv4 *)prefix_new(); | |
834 | p->family = AF_INET; | |
835 | return p; | |
718e3744 | 836 | } |
837 | ||
838 | /* Free prefix_ipv4 structure. */ | |
d62a17ae | 839 | void prefix_ipv4_free(struct prefix_ipv4 *p) |
718e3744 | 840 | { |
d62a17ae | 841 | prefix_free((struct prefix *)p); |
718e3744 | 842 | } |
843 | ||
844 | /* When string format is invalid return 0. */ | |
d62a17ae | 845 | int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p) |
718e3744 | 846 | { |
d62a17ae | 847 | int ret; |
848 | int plen; | |
849 | char *pnt; | |
850 | char *cp; | |
851 | ||
852 | /* Find slash inside string. */ | |
853 | pnt = strchr(str, '/'); | |
854 | ||
855 | /* String doesn't contail slash. */ | |
856 | if (pnt == NULL) { | |
857 | /* Convert string to prefix. */ | |
8d920049 | 858 | ret = inet_pton(AF_INET, str, &p->prefix); |
d62a17ae | 859 | if (ret == 0) |
860 | return 0; | |
861 | ||
862 | /* If address doesn't contain slash we assume it host address. | |
863 | */ | |
864 | p->family = AF_INET; | |
865 | p->prefixlen = IPV4_MAX_BITLEN; | |
866 | ||
867 | return ret; | |
868 | } else { | |
869 | cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1); | |
aab9a0a0 | 870 | memcpy(cp, str, pnt - str); |
d62a17ae | 871 | *(cp + (pnt - str)) = '\0'; |
872 | ret = inet_aton(cp, &p->prefix); | |
873 | XFREE(MTYPE_TMP, cp); | |
874 | ||
875 | /* Get prefix length. */ | |
d7c0a89a | 876 | plen = (uint8_t)atoi(++pnt); |
d62a17ae | 877 | if (plen > IPV4_MAX_PREFIXLEN) |
878 | return 0; | |
879 | ||
880 | p->family = AF_INET; | |
881 | p->prefixlen = plen; | |
882 | } | |
718e3744 | 883 | |
d62a17ae | 884 | return ret; |
718e3744 | 885 | } |
886 | ||
32ac65d9 | 887 | /* When string format is invalid return 0. */ |
d62a17ae | 888 | int str2prefix_eth(const char *str, struct prefix_eth *p) |
32ac65d9 | 889 | { |
d62a17ae | 890 | int ret = 0; |
891 | int plen = 48; | |
892 | char *pnt; | |
893 | char *cp = NULL; | |
894 | const char *str_addr = str; | |
895 | unsigned int a[6]; | |
896 | int i; | |
0f6476cc | 897 | bool slash = false; |
d62a17ae | 898 | |
3b0f6068 DL |
899 | if (!strcmp(str, "any")) { |
900 | memset(p, 0, sizeof(*p)); | |
901 | p->family = AF_ETHERNET; | |
902 | return 1; | |
903 | } | |
904 | ||
d62a17ae | 905 | /* Find slash inside string. */ |
906 | pnt = strchr(str, '/'); | |
907 | ||
908 | if (pnt) { | |
909 | /* Get prefix length. */ | |
d7c0a89a | 910 | plen = (uint8_t)atoi(++pnt); |
d62a17ae | 911 | if (plen > 48) { |
912 | ret = 0; | |
913 | goto done; | |
914 | } | |
915 | ||
916 | cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1); | |
aab9a0a0 | 917 | memcpy(cp, str, pnt - str); |
d62a17ae | 918 | *(cp + (pnt - str)) = '\0'; |
919 | ||
920 | str_addr = cp; | |
0f6476cc | 921 | slash = true; |
32ac65d9 LB |
922 | } |
923 | ||
d62a17ae | 924 | /* Convert string to prefix. */ |
925 | if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, | |
926 | a + 3, a + 4, a + 5) | |
927 | != 6) { | |
928 | ret = 0; | |
929 | goto done; | |
930 | } | |
931 | for (i = 0; i < 6; ++i) { | |
932 | p->eth_addr.octet[i] = a[i] & 0xff; | |
933 | } | |
934 | p->prefixlen = plen; | |
935 | p->family = AF_ETHERNET; | |
0f6476cc DS |
936 | |
937 | /* | |
938 | * special case to allow old configurations to work | |
939 | * Since all zero's is implicitly meant to allow | |
940 | * a comparison to zero, let's assume | |
941 | */ | |
942 | if (!slash && is_zero_mac(&(p->eth_addr))) | |
996c9314 | 943 | p->prefixlen = 0; |
0f6476cc | 944 | |
d62a17ae | 945 | ret = 1; |
32ac65d9 LB |
946 | |
947 | done: | |
0a22ddfb | 948 | XFREE(MTYPE_TMP, cp); |
32ac65d9 | 949 | |
d62a17ae | 950 | return ret; |
32ac65d9 LB |
951 | } |
952 | ||
051954f5 | 953 | /* Convert masklen into IP address's netmask (network byte order). */ |
d62a17ae | 954 | void masklen2ip(const int masklen, struct in_addr *netmask) |
718e3744 | 955 | { |
d62a17ae | 956 | assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN); |
e96b3121 | 957 | |
d62a17ae | 958 | /* left shift is only defined for less than the size of the type. |
959 | * we unconditionally use long long in case the target platform | |
960 | * has defined behaviour for << 32 (or has a 64-bit left shift) */ | |
e96b3121 | 961 | |
d62a17ae | 962 | if (sizeof(unsigned long long) > 4) |
963 | netmask->s_addr = htonl(0xffffffffULL << (32 - masklen)); | |
964 | else | |
965 | netmask->s_addr = | |
966 | htonl(masklen ? 0xffffffffU << (32 - masklen) : 0); | |
718e3744 | 967 | } |
968 | ||
969 | /* Convert IP address's netmask into integer. We assume netmask is | |
f93eee44 | 970 | * sequential one. Argument netmask should be network byte order. */ |
d7c0a89a | 971 | uint8_t ip_masklen(struct in_addr netmask) |
718e3744 | 972 | { |
d62a17ae | 973 | uint32_t tmp = ~ntohl(netmask.s_addr); |
f93eee44 | 974 | |
61be6e94 QY |
975 | /* |
976 | * clz: count leading zeroes. sadly, the behaviour of this builtin is | |
977 | * undefined for a 0 argument, even though most CPUs give 32 | |
978 | */ | |
f93eee44 | 979 | return tmp ? __builtin_clz(tmp) : 32; |
718e3744 | 980 | } |
981 | ||
caff7905 | 982 | /* Apply mask to IPv4 prefix (network byte order). */ |
d62a17ae | 983 | void apply_mask_ipv4(struct prefix_ipv4 *p) |
718e3744 | 984 | { |
d62a17ae | 985 | struct in_addr mask; |
986 | masklen2ip(p->prefixlen, &mask); | |
987 | p->prefix.s_addr &= mask.s_addr; | |
718e3744 | 988 | } |
989 | ||
990 | /* If prefix is 0.0.0.0/0 then return 1 else return 0. */ | |
d62a17ae | 991 | int prefix_ipv4_any(const struct prefix_ipv4 *p) |
718e3744 | 992 | { |
d62a17ae | 993 | return (p->prefix.s_addr == 0 && p->prefixlen == 0); |
718e3744 | 994 | } |
6b0655a2 | 995 | |
718e3744 | 996 | /* Allocate a new ip version 6 route */ |
d62a17ae | 997 | struct prefix_ipv6 *prefix_ipv6_new(void) |
718e3744 | 998 | { |
d62a17ae | 999 | struct prefix_ipv6 *p; |
718e3744 | 1000 | |
d62a17ae | 1001 | /* Allocate a full-size struct prefix to avoid problems with structure |
1002 | size mismatches. */ | |
1003 | p = (struct prefix_ipv6 *)prefix_new(); | |
1004 | p->family = AF_INET6; | |
1005 | return p; | |
718e3744 | 1006 | } |
1007 | ||
1008 | /* Free prefix for IPv6. */ | |
d62a17ae | 1009 | void prefix_ipv6_free(struct prefix_ipv6 *p) |
718e3744 | 1010 | { |
d62a17ae | 1011 | prefix_free((struct prefix *)p); |
718e3744 | 1012 | } |
1013 | ||
1014 | /* If given string is valid return pin6 else return NULL */ | |
d62a17ae | 1015 | int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p) |
718e3744 | 1016 | { |
d62a17ae | 1017 | char *pnt; |
1018 | char *cp; | |
1019 | int ret; | |
1020 | ||
1021 | pnt = strchr(str, '/'); | |
1022 | ||
1023 | /* If string doesn't contain `/' treat it as host route. */ | |
1024 | if (pnt == NULL) { | |
1025 | ret = inet_pton(AF_INET6, str, &p->prefix); | |
1026 | if (ret == 0) | |
1027 | return 0; | |
1028 | p->prefixlen = IPV6_MAX_BITLEN; | |
1029 | } else { | |
1030 | int plen; | |
1031 | ||
1032 | cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1); | |
aab9a0a0 | 1033 | memcpy(cp, str, pnt - str); |
d62a17ae | 1034 | *(cp + (pnt - str)) = '\0'; |
1035 | ret = inet_pton(AF_INET6, cp, &p->prefix); | |
1036 | XFREE(MTYPE_TMP, cp); | |
1037 | if (ret == 0) | |
1038 | return 0; | |
d7c0a89a | 1039 | plen = (uint8_t)atoi(++pnt); |
d62a17ae | 1040 | if (plen > IPV6_MAX_BITLEN) |
1041 | return 0; | |
1042 | p->prefixlen = plen; | |
1043 | } | |
1044 | p->family = AF_INET6; | |
718e3744 | 1045 | |
d62a17ae | 1046 | return ret; |
718e3744 | 1047 | } |
1048 | ||
b04c699e | 1049 | /* Convert struct in6_addr netmask into integer. |
d7c0a89a | 1050 | * FIXME return uint8_t as ip_maskleni() does. */ |
d62a17ae | 1051 | int ip6_masklen(struct in6_addr netmask) |
718e3744 | 1052 | { |
d62a17ae | 1053 | int len = 0; |
1054 | unsigned char val; | |
1055 | unsigned char *pnt; | |
1056 | ||
1057 | pnt = (unsigned char *)&netmask; | |
1058 | ||
1059 | while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN) { | |
1060 | len += 8; | |
1061 | pnt++; | |
718e3744 | 1062 | } |
d62a17ae | 1063 | |
1064 | if (len < IPV6_MAX_BITLEN) { | |
1065 | val = *pnt; | |
1066 | while (val) { | |
1067 | len++; | |
1068 | val <<= 1; | |
1069 | } | |
1070 | } | |
1071 | return len; | |
718e3744 | 1072 | } |
1073 | ||
d62a17ae | 1074 | void masklen2ip6(const int masklen, struct in6_addr *netmask) |
718e3744 | 1075 | { |
d62a17ae | 1076 | assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN); |
1077 | memcpy(netmask, maskbytes6 + masklen, sizeof(struct in6_addr)); | |
718e3744 | 1078 | } |
1079 | ||
d62a17ae | 1080 | void apply_mask_ipv6(struct prefix_ipv6 *p) |
718e3744 | 1081 | { |
d7c0a89a | 1082 | uint8_t *pnt; |
d62a17ae | 1083 | int index; |
1084 | int offset; | |
8c7f49d2 | 1085 | |
d62a17ae | 1086 | index = p->prefixlen / 8; |
8c7f49d2 | 1087 | |
d62a17ae | 1088 | if (index < 16) { |
d7c0a89a | 1089 | pnt = (uint8_t *)&p->prefix; |
d62a17ae | 1090 | offset = p->prefixlen % 8; |
8c7f49d2 | 1091 | |
d62a17ae | 1092 | pnt[index] &= maskbit[offset]; |
1093 | index++; | |
8c7f49d2 | 1094 | |
d62a17ae | 1095 | while (index < 16) |
1096 | pnt[index++] = 0; | |
1097 | } | |
718e3744 | 1098 | } |
1099 | ||
d62a17ae | 1100 | void apply_mask(struct prefix *p) |
718e3744 | 1101 | { |
d62a17ae | 1102 | switch (p->family) { |
1103 | case AF_INET: | |
1104 | apply_mask_ipv4((struct prefix_ipv4 *)p); | |
1105 | break; | |
1106 | case AF_INET6: | |
1107 | apply_mask_ipv6((struct prefix_ipv6 *)p); | |
1108 | break; | |
1109 | default: | |
1110 | break; | |
1111 | } | |
1112 | return; | |
718e3744 | 1113 | } |
1114 | ||
b04c699e | 1115 | /* Utility function of convert between struct prefix <=> union sockunion. |
1116 | * FIXME This function isn't used anywhere. */ | |
d62a17ae | 1117 | struct prefix *sockunion2prefix(const union sockunion *dest, |
1118 | const union sockunion *mask) | |
718e3744 | 1119 | { |
d62a17ae | 1120 | if (dest->sa.sa_family == AF_INET) { |
1121 | struct prefix_ipv4 *p; | |
1122 | ||
1123 | p = prefix_ipv4_new(); | |
1124 | p->family = AF_INET; | |
1125 | p->prefix = dest->sin.sin_addr; | |
1126 | p->prefixlen = ip_masklen(mask->sin.sin_addr); | |
1127 | return (struct prefix *)p; | |
1128 | } | |
1129 | if (dest->sa.sa_family == AF_INET6) { | |
1130 | struct prefix_ipv6 *p; | |
1131 | ||
1132 | p = prefix_ipv6_new(); | |
1133 | p->family = AF_INET6; | |
1134 | p->prefixlen = ip6_masklen(mask->sin6.sin6_addr); | |
1135 | memcpy(&p->prefix, &dest->sin6.sin6_addr, | |
1136 | sizeof(struct in6_addr)); | |
1137 | return (struct prefix *)p; | |
1138 | } | |
1139 | return NULL; | |
718e3744 | 1140 | } |
1141 | ||
b04c699e | 1142 | /* Utility function of convert between struct prefix <=> union sockunion. */ |
d62a17ae | 1143 | struct prefix *sockunion2hostprefix(const union sockunion *su, |
1144 | struct prefix *prefix) | |
718e3744 | 1145 | { |
d62a17ae | 1146 | if (su->sa.sa_family == AF_INET) { |
1147 | struct prefix_ipv4 *p; | |
1148 | ||
1149 | p = prefix ? (struct prefix_ipv4 *)prefix : prefix_ipv4_new(); | |
1150 | p->family = AF_INET; | |
1151 | p->prefix = su->sin.sin_addr; | |
1152 | p->prefixlen = IPV4_MAX_BITLEN; | |
1153 | return (struct prefix *)p; | |
1154 | } | |
1155 | if (su->sa.sa_family == AF_INET6) { | |
1156 | struct prefix_ipv6 *p; | |
1157 | ||
1158 | p = prefix ? (struct prefix_ipv6 *)prefix : prefix_ipv6_new(); | |
1159 | p->family = AF_INET6; | |
1160 | p->prefixlen = IPV6_MAX_BITLEN; | |
1161 | memcpy(&p->prefix, &su->sin6.sin6_addr, | |
1162 | sizeof(struct in6_addr)); | |
1163 | return (struct prefix *)p; | |
1164 | } | |
1165 | return NULL; | |
718e3744 | 1166 | } |
1167 | ||
d62a17ae | 1168 | void prefix2sockunion(const struct prefix *p, union sockunion *su) |
17e52061 | 1169 | { |
d62a17ae | 1170 | memset(su, 0, sizeof(*su)); |
1171 | ||
1172 | su->sa.sa_family = p->family; | |
1173 | if (p->family == AF_INET) | |
1174 | su->sin.sin_addr = p->u.prefix4; | |
1175 | if (p->family == AF_INET6) | |
1176 | memcpy(&su->sin6.sin6_addr, &p->u.prefix6, | |
1177 | sizeof(struct in6_addr)); | |
17e52061 DL |
1178 | } |
1179 | ||
d62a17ae | 1180 | int prefix_blen(const struct prefix *p) |
718e3744 | 1181 | { |
d62a17ae | 1182 | switch (p->family) { |
1183 | case AF_INET: | |
1184 | return IPV4_MAX_BYTELEN; | |
1185 | break; | |
1186 | case AF_INET6: | |
1187 | return IPV6_MAX_BYTELEN; | |
1188 | break; | |
1189 | case AF_ETHERNET: | |
7628d862 | 1190 | return ETH_ALEN; |
b03b8898 | 1191 | break; |
d62a17ae | 1192 | } |
1193 | return 0; | |
718e3744 | 1194 | } |
1195 | ||
1196 | /* Generic function for conversion string to struct prefix. */ | |
d62a17ae | 1197 | int str2prefix(const char *str, struct prefix *p) |
718e3744 | 1198 | { |
d62a17ae | 1199 | int ret; |
718e3744 | 1200 | |
c37a11ad | 1201 | if (!str || !p) |
1202 | return 0; | |
1203 | ||
d62a17ae | 1204 | /* First we try to convert string to struct prefix_ipv4. */ |
1205 | ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p); | |
1206 | if (ret) | |
1207 | return ret; | |
718e3744 | 1208 | |
d62a17ae | 1209 | /* Next we try to convert string to struct prefix_ipv6. */ |
1210 | ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p); | |
1211 | if (ret) | |
1212 | return ret; | |
718e3744 | 1213 | |
d62a17ae | 1214 | /* Next we try to convert string to struct prefix_eth. */ |
1215 | ret = str2prefix_eth(str, (struct prefix_eth *)p); | |
1216 | if (ret) | |
1217 | return ret; | |
32ac65d9 | 1218 | |
d62a17ae | 1219 | return 0; |
718e3744 | 1220 | } |
1221 | ||
3714a385 | 1222 | static const char *prefixevpn_ead2str(const struct prefix_evpn *p, char *str, |
1223 | int size) | |
1224 | { | |
1225 | snprintf(str, size, "Unsupported EVPN prefix"); | |
1226 | return str; | |
1227 | } | |
1228 | ||
1229 | static const char *prefixevpn_macip2str(const struct prefix_evpn *p, char *str, | |
1230 | int size) | |
86f1ef44 | 1231 | { |
d7c0a89a | 1232 | uint8_t family; |
d62a17ae | 1233 | char buf[PREFIX2STR_BUFFER]; |
1234 | char buf2[ETHER_ADDR_STRLEN]; | |
1235 | ||
3714a385 | 1236 | if (is_evpn_prefix_ipaddr_none(p)) |
1237 | snprintf(str, size, "[%d]:[%s]/%d", | |
1238 | p->prefix.route_type, | |
1239 | prefix_mac2str(&p->prefix.macip_addr.mac, | |
1240 | buf2, sizeof(buf2)), | |
d62a17ae | 1241 | p->prefixlen); |
3714a385 | 1242 | else { |
1243 | family = is_evpn_prefix_ipaddr_v4(p) | |
d62a17ae | 1244 | ? AF_INET |
1245 | : AF_INET6; | |
3714a385 | 1246 | snprintf(str, size, "[%d]:[%s]:[%s]/%d", |
1247 | p->prefix.route_type, | |
1248 | prefix_mac2str(&p->prefix.macip_addr.mac, | |
1249 | buf2, sizeof(buf2)), | |
1250 | inet_ntop(family, | |
1251 | &p->prefix.macip_addr.ip.ip.addr, | |
1252 | buf, PREFIX2STR_BUFFER), | |
1253 | p->prefixlen); | |
d62a17ae | 1254 | } |
3714a385 | 1255 | return str; |
1256 | } | |
1257 | ||
1258 | static const char *prefixevpn_imet2str(const struct prefix_evpn *p, char *str, | |
1259 | int size) | |
1260 | { | |
1261 | uint8_t family; | |
1262 | char buf[PREFIX2STR_BUFFER]; | |
d62a17ae | 1263 | |
3714a385 | 1264 | family = is_evpn_prefix_ipaddr_v4(p) |
1265 | ? AF_INET | |
1266 | : AF_INET6; | |
1267 | snprintf(str, size, "[%d]:[%s]/%d", p->prefix.route_type, | |
1268 | inet_ntop(family, | |
1269 | &p->prefix.imet_addr.ip.ip.addr, buf, | |
1270 | PREFIX2STR_BUFFER), | |
1271 | p->prefixlen); | |
1272 | return str; | |
1273 | } | |
1274 | ||
1275 | static const char *prefixevpn_es2str(const struct prefix_evpn *p, char *str, | |
1276 | int size) | |
1277 | { | |
50f74cf1 | 1278 | char buf[ESI_STR_LEN]; |
1279 | ||
1280 | snprintf(str, size, "[%d]:[%s]:[%s]/%d", p->prefix.route_type, | |
1281 | esi_to_str(&p->prefix.es_addr.esi, buf, sizeof(buf)), | |
1282 | inet_ntoa(p->prefix.es_addr.ip.ipaddr_v4), | |
1283 | p->prefixlen); | |
3714a385 | 1284 | return str; |
1285 | } | |
1286 | ||
1287 | static const char *prefixevpn_prefix2str(const struct prefix_evpn *p, char *str, | |
1288 | int size) | |
1289 | { | |
1290 | uint8_t family; | |
1291 | char buf[PREFIX2STR_BUFFER]; | |
1292 | ||
1293 | family = is_evpn_prefix_ipaddr_v4(p) | |
1294 | ? AF_INET | |
1295 | : AF_INET6; | |
1296 | snprintf(str, size, "[%d]:[%u][%s/%d]/%d", | |
1297 | p->prefix.route_type, | |
1298 | p->prefix.prefix_addr.eth_tag, | |
1299 | inet_ntop(family, | |
1300 | &p->prefix.prefix_addr.ip.ip.addr, buf, | |
1301 | PREFIX2STR_BUFFER), | |
1302 | p->prefix.prefix_addr.ip_prefix_length, | |
1303 | p->prefixlen); | |
1304 | return str; | |
1305 | } | |
1306 | ||
1307 | static const char *prefixevpn2str(const struct prefix_evpn *p, char *str, | |
1308 | int size) | |
1309 | { | |
1310 | switch (p->prefix.route_type) { | |
1311 | case 1: | |
1312 | return prefixevpn_ead2str(p, str, size); | |
1313 | case 2: | |
1314 | return prefixevpn_macip2str(p, str, size); | |
1315 | case 3: | |
1316 | return prefixevpn_imet2str(p, str, size); | |
1317 | case 4: | |
1318 | return prefixevpn_es2str(p, str, size); | |
1319 | case 5: | |
1320 | return prefixevpn_prefix2str(p, str, size); | |
1321 | default: | |
1322 | snprintf(str, size, "Unsupported EVPN prefix"); | |
1323 | break; | |
1324 | } | |
d62a17ae | 1325 | return str; |
86f1ef44 | 1326 | } |
1327 | ||
d62a17ae | 1328 | const char *prefix2str(union prefixconstptr pu, char *str, int size) |
718e3744 | 1329 | { |
d62a17ae | 1330 | const struct prefix *p = pu.p; |
1331 | char buf[PREFIX2STR_BUFFER]; | |
ec466f65 QY |
1332 | int byte, tmp, a, b; |
1333 | bool z = false; | |
1334 | size_t l; | |
d62a17ae | 1335 | |
1336 | switch (p->family) { | |
1337 | case AF_INET: | |
1338 | case AF_INET6: | |
ec466f65 QY |
1339 | inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf)); |
1340 | l = strlen(buf); | |
1341 | buf[l++] = '/'; | |
1342 | byte = p->prefixlen; | |
1343 | if ((tmp = p->prefixlen - 100) >= 0) { | |
1344 | buf[l++] = '1'; | |
1345 | z = true; | |
1346 | byte = tmp; | |
1347 | } | |
1348 | b = byte % 10; | |
1349 | a = byte / 10; | |
1350 | if (a || z) | |
1351 | buf[l++] = '0' + a; | |
1352 | buf[l++] = '0' + b; | |
1353 | buf[l] = '\0'; | |
1354 | strlcpy(str, buf, size); | |
d62a17ae | 1355 | break; |
1356 | ||
1357 | case AF_ETHERNET: | |
b03b8898 DS |
1358 | snprintf(str, size, "%s/%d", |
1359 | prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)), | |
1360 | p->prefixlen); | |
1361 | break; | |
1362 | ||
1363 | case AF_EVPN: | |
3714a385 | 1364 | prefixevpn2str((const struct prefix_evpn *)p, str, size); |
d62a17ae | 1365 | break; |
1366 | ||
9a14899b | 1367 | case AF_FLOWSPEC: |
ec466f65 | 1368 | strlcpy(str, "FS prefix", size); |
9a14899b PG |
1369 | break; |
1370 | ||
d62a17ae | 1371 | default: |
ec466f65 | 1372 | strlcpy(str, "UNK prefix", size); |
d62a17ae | 1373 | break; |
1374 | } | |
1375 | ||
1376 | return str; | |
718e3744 | 1377 | } |
1378 | ||
c6b6b53b AK |
1379 | void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr, |
1380 | char *buf, int buf_size) | |
1381 | { | |
1382 | int save_errno = errno; | |
1383 | ||
1384 | if (addr.s_addr == INADDR_ANY) | |
9f73d2c9 | 1385 | strlcpy(buf, "*", buf_size); |
c6b6b53b AK |
1386 | else { |
1387 | if (!inet_ntop(AF_INET, &addr, buf, buf_size)) { | |
1388 | if (onfail) | |
1389 | snprintf(buf, buf_size, "%s", onfail); | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | errno = save_errno; | |
1394 | } | |
1395 | ||
1396 | const char *prefix_sg2str(const struct prefix_sg *sg, char *sg_str) | |
1397 | { | |
1398 | char src_str[INET_ADDRSTRLEN]; | |
1399 | char grp_str[INET_ADDRSTRLEN]; | |
1400 | ||
1401 | prefix_mcast_inet4_dump("<src?>", sg->src, src_str, sizeof(src_str)); | |
1402 | prefix_mcast_inet4_dump("<grp?>", sg->grp, grp_str, sizeof(grp_str)); | |
1403 | snprintf(sg_str, PREFIX_SG_STR_LEN, "(%s,%s)", src_str, grp_str); | |
1404 | ||
1405 | return sg_str; | |
1406 | } | |
1407 | ||
4d762f26 | 1408 | struct prefix *prefix_new(void) |
718e3744 | 1409 | { |
d62a17ae | 1410 | struct prefix *p; |
718e3744 | 1411 | |
d62a17ae | 1412 | p = XCALLOC(MTYPE_PREFIX, sizeof *p); |
1413 | return p; | |
718e3744 | 1414 | } |
1415 | ||
1416 | /* Free prefix structure. */ | |
d62a17ae | 1417 | void prefix_free(struct prefix *p) |
718e3744 | 1418 | { |
d62a17ae | 1419 | XFREE(MTYPE_PREFIX, p); |
718e3744 | 1420 | } |
1421 | ||
718e3744 | 1422 | /* Utility function to convert ipv4 prefixes to Classful prefixes */ |
d62a17ae | 1423 | void apply_classful_mask_ipv4(struct prefix_ipv4 *p) |
718e3744 | 1424 | { |
1425 | ||
d7c0a89a | 1426 | uint32_t destination; |
d62a17ae | 1427 | |
1428 | destination = ntohl(p->prefix.s_addr); | |
1429 | ||
1430 | if (p->prefixlen == IPV4_MAX_PREFIXLEN) | |
1431 | ; | |
1432 | /* do nothing for host routes */ | |
1433 | else if (IN_CLASSC(destination)) { | |
1434 | p->prefixlen = 24; | |
1435 | apply_mask_ipv4(p); | |
1436 | } else if (IN_CLASSB(destination)) { | |
1437 | p->prefixlen = 16; | |
1438 | apply_mask_ipv4(p); | |
1439 | } else { | |
1440 | p->prefixlen = 8; | |
1441 | apply_mask_ipv4(p); | |
1442 | } | |
718e3744 | 1443 | } |
1444 | ||
d62a17ae | 1445 | in_addr_t ipv4_network_addr(in_addr_t hostaddr, int masklen) |
3fb9cd6e | 1446 | { |
d62a17ae | 1447 | struct in_addr mask; |
3fb9cd6e | 1448 | |
d62a17ae | 1449 | masklen2ip(masklen, &mask); |
1450 | return hostaddr & mask.s_addr; | |
3fb9cd6e | 1451 | } |
1452 | ||
d62a17ae | 1453 | in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen) |
3fb9cd6e | 1454 | { |
d62a17ae | 1455 | struct in_addr mask; |
1456 | ||
1457 | masklen2ip(masklen, &mask); | |
1458 | return (masklen != IPV4_MAX_PREFIXLEN - 1) ? | |
1459 | /* normal case */ | |
1460 | (hostaddr | ~mask.s_addr) | |
1461 | : | |
1462 | /* special case for /31 */ | |
1463 | (hostaddr ^ ~mask.s_addr); | |
3fb9cd6e | 1464 | } |
1465 | ||
d62a17ae | 1466 | /* Utility function to convert ipv4 netmask to prefixes |
718e3744 | 1467 | ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16" |
1468 | ex.) "1.0.0.0" NULL => "1.0.0.0/8" */ | |
d62a17ae | 1469 | int netmask_str2prefix_str(const char *net_str, const char *mask_str, |
1470 | char *prefix_str) | |
718e3744 | 1471 | { |
d62a17ae | 1472 | struct in_addr network; |
1473 | struct in_addr mask; | |
d7c0a89a QY |
1474 | uint8_t prefixlen; |
1475 | uint32_t destination; | |
d62a17ae | 1476 | int ret; |
1477 | ||
1478 | ret = inet_aton(net_str, &network); | |
1479 | if (!ret) | |
1480 | return 0; | |
1481 | ||
1482 | if (mask_str) { | |
1483 | ret = inet_aton(mask_str, &mask); | |
1484 | if (!ret) | |
1485 | return 0; | |
1486 | ||
1487 | prefixlen = ip_masklen(mask); | |
1488 | } else { | |
1489 | destination = ntohl(network.s_addr); | |
1490 | ||
1491 | if (network.s_addr == 0) | |
1492 | prefixlen = 0; | |
1493 | else if (IN_CLASSC(destination)) | |
1494 | prefixlen = 24; | |
1495 | else if (IN_CLASSB(destination)) | |
1496 | prefixlen = 16; | |
1497 | else if (IN_CLASSA(destination)) | |
1498 | prefixlen = 8; | |
1499 | else | |
1500 | return 0; | |
1501 | } | |
718e3744 | 1502 | |
d62a17ae | 1503 | sprintf(prefix_str, "%s/%d", net_str, prefixlen); |
718e3744 | 1504 | |
d62a17ae | 1505 | return 1; |
718e3744 | 1506 | } |
1507 | ||
5920990f | 1508 | /* Utility function for making IPv6 address string. */ |
d62a17ae | 1509 | const char *inet6_ntoa(struct in6_addr addr) |
5920990f | 1510 | { |
d62a17ae | 1511 | static char buf[INET6_ADDRSTRLEN]; |
5920990f | 1512 | |
d62a17ae | 1513 | inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN); |
1514 | return buf; | |
5920990f | 1515 | } |
c215ecaf | 1516 | |
c215ecaf | 1517 | /* converts to internal representation of mac address |
d62a17ae | 1518 | * returns 1 on success, 0 otherwise |
c215ecaf PG |
1519 | * format accepted: AA:BB:CC:DD:EE:FF |
1520 | * if mac parameter is null, then check only | |
1521 | */ | |
db42a173 | 1522 | int prefix_str2mac(const char *str, struct ethaddr *mac) |
c215ecaf | 1523 | { |
d62a17ae | 1524 | unsigned int a[6]; |
1525 | int i; | |
1526 | ||
1527 | if (!str) | |
1528 | return 0; | |
1529 | ||
1530 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3, | |
1531 | a + 4, a + 5) | |
1532 | != 6) { | |
1533 | /* error in incoming str length */ | |
1534 | return 0; | |
1535 | } | |
1536 | /* valid mac address */ | |
1537 | if (!mac) | |
1538 | return 1; | |
1539 | for (i = 0; i < 6; ++i) | |
1540 | mac->octet[i] = a[i] & 0xff; | |
1541 | return 1; | |
c215ecaf PG |
1542 | } |
1543 | ||
db42a173 | 1544 | char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size) |
c215ecaf | 1545 | { |
d62a17ae | 1546 | char *ptr; |
1547 | ||
1548 | if (!mac) | |
1549 | return NULL; | |
1550 | if (!buf) | |
9f5dc319 | 1551 | ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char)); |
d62a17ae | 1552 | else { |
1553 | assert(size >= ETHER_ADDR_STRLEN); | |
1554 | ptr = buf; | |
1555 | } | |
1556 | snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x", | |
1557 | (uint8_t)mac->octet[0], (uint8_t)mac->octet[1], | |
1558 | (uint8_t)mac->octet[2], (uint8_t)mac->octet[3], | |
1559 | (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]); | |
1560 | return ptr; | |
c215ecaf | 1561 | } |
7a7761d2 | 1562 | |
62b4b3b6 | 1563 | unsigned prefix_hash_key(const void *pp) |
7a7761d2 CF |
1564 | { |
1565 | struct prefix copy; | |
1566 | ||
9a14899b PG |
1567 | if (((struct prefix *)pp)->family == AF_FLOWSPEC) { |
1568 | uint32_t len; | |
1569 | void *temp; | |
1570 | ||
1571 | /* make sure *all* unused bits are zero, | |
1572 | * particularly including alignment / | |
1573 | * padding and unused prefix bytes. | |
1574 | */ | |
1575 | memset(©, 0, sizeof(copy)); | |
1576 | prefix_copy(©, (struct prefix *)pp); | |
1577 | len = jhash((void *)copy.u.prefix_flowspec.ptr, | |
1578 | copy.u.prefix_flowspec.prefixlen, | |
1579 | 0x55aa5a5a); | |
1580 | temp = (void *)copy.u.prefix_flowspec.ptr; | |
1581 | XFREE(MTYPE_PREFIX_FLOWSPEC, temp); | |
1582 | copy.u.prefix_flowspec.ptr = (uintptr_t)NULL; | |
1583 | return len; | |
1584 | } | |
7a7761d2 CF |
1585 | /* make sure *all* unused bits are zero, particularly including |
1586 | * alignment / | |
1587 | * padding and unused prefix bytes. */ | |
1588 | memset(©, 0, sizeof(copy)); | |
1589 | prefix_copy(©, (struct prefix *)pp); | |
996c9314 LB |
1590 | return jhash(©, |
1591 | offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen), | |
1592 | 0x55aa5a5a); | |
7a7761d2 | 1593 | } |
50f74cf1 | 1594 | |
1595 | /* converts to internal representation of esi | |
1596 | * returns 1 on success, 0 otherwise | |
1597 | * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa | |
1598 | * if esi parameter is null, then check only | |
1599 | */ | |
1600 | int str_to_esi(const char *str, esi_t *esi) | |
1601 | { | |
1602 | int i; | |
1603 | unsigned int a[ESI_BYTES]; | |
1604 | ||
1605 | if (!str) | |
1606 | return 0; | |
1607 | ||
1608 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x", | |
1609 | a + 0, a + 1, a + 2, a + 3, | |
1610 | a + 4, a + 5, a + 6, a + 7, | |
1611 | a + 8, a + 9) | |
1612 | != ESI_BYTES) { | |
1613 | /* error in incoming str length */ | |
1614 | return 0; | |
1615 | } | |
1616 | ||
1617 | /* valid ESI */ | |
1618 | if (!esi) | |
1619 | return 1; | |
1620 | for (i = 0; i < ESI_BYTES; ++i) | |
1621 | esi->val[i] = a[i] & 0xff; | |
1622 | return 1; | |
1623 | } | |
1624 | ||
1625 | char *esi_to_str(const esi_t *esi, char *buf, int size) | |
1626 | { | |
1627 | char *ptr; | |
1628 | ||
1629 | if (!esi) | |
1630 | return NULL; | |
1631 | if (!buf) | |
9f5dc319 | 1632 | ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char)); |
50f74cf1 | 1633 | else { |
1634 | assert(size >= ESI_STR_LEN); | |
1635 | ptr = buf; | |
1636 | } | |
1637 | ||
1638 | snprintf(ptr, ESI_STR_LEN, | |
1639 | "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", | |
1640 | esi->val[0], esi->val[1], esi->val[2], | |
1641 | esi->val[3], esi->val[4], esi->val[5], | |
1642 | esi->val[6], esi->val[7], esi->val[8], | |
1643 | esi->val[9]); | |
1644 | return ptr; | |
1645 | } | |
d52ec572 DL |
1646 | |
1647 | printfrr_ext_autoreg_p("I4", printfrr_i4) | |
1648 | static ssize_t printfrr_i4(char *buf, size_t bsz, const char *fmt, | |
1649 | int prec, const void *ptr) | |
1650 | { | |
1651 | inet_ntop(AF_INET, ptr, buf, bsz); | |
1652 | return 2; | |
1653 | } | |
1654 | ||
1655 | printfrr_ext_autoreg_p("I6", printfrr_i6) | |
1656 | static ssize_t printfrr_i6(char *buf, size_t bsz, const char *fmt, | |
1657 | int prec, const void *ptr) | |
1658 | { | |
1659 | inet_ntop(AF_INET6, ptr, buf, bsz); | |
1660 | return 2; | |
1661 | } | |
1662 | ||
1663 | printfrr_ext_autoreg_p("FX", printfrr_pfx) | |
1664 | static ssize_t printfrr_pfx(char *buf, size_t bsz, const char *fmt, | |
1665 | int prec, const void *ptr) | |
1666 | { | |
1667 | prefix2str(ptr, buf, bsz); | |
1668 | return 2; | |
1669 | } | |
1670 | ||
1671 | printfrr_ext_autoreg_p("SG4", printfrr_psg) | |
1672 | static ssize_t printfrr_psg(char *buf, size_t bsz, const char *fmt, | |
1673 | int prec, const void *ptr) | |
1674 | { | |
1675 | const struct prefix_sg *sg = ptr; | |
1676 | struct fbuf fb = { .buf = buf, .pos = buf, .len = bsz - 1 }; | |
1677 | ||
1678 | if (sg->src.s_addr == INADDR_ANY) | |
1679 | bprintfrr(&fb, "(*,"); | |
1680 | else | |
1681 | bprintfrr(&fb, "(%pI4,", &sg->src); | |
1682 | ||
1683 | if (sg->grp.s_addr == INADDR_ANY) | |
1684 | bprintfrr(&fb, "*)"); | |
1685 | else | |
1686 | bprintfrr(&fb, "%pI4)", &sg->grp); | |
1687 | ||
1688 | fb.pos[0] = '\0'; | |
1689 | return 3; | |
1690 | } |