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