]> git.proxmox.com Git - mirror_frr.git/blame - lib/prefix.h
*: make consistent & update GPLv2 file headers
[mirror_frr.git] / lib / prefix.h
CommitLineData
718e3744 1/*
2 * Prefix structure.
3 * Copyright (C) 1998 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#ifndef _ZEBRA_PREFIX_H
23#define _ZEBRA_PREFIX_H
24
32ac65d9
LB
25#ifdef SUNOS_5
26# include <sys/ethernet.h>
27#else
28# ifdef GNU_LINUX
29# include <net/ethernet.h>
30# else
31# include <netinet/if_ether.h>
32# endif
33#endif
8cc4198f 34#include "sockunion.h"
35
32ac65d9 36#ifndef ETHER_ADDR_LEN
c215ecaf 37#ifdef ETHERADDRL
32ac65d9 38#define ETHER_ADDR_LEN ETHERADDRL
c215ecaf
PG
39#else
40#define ETHER_ADDR_LEN 6
41#endif
32ac65d9
LB
42#endif
43
c215ecaf 44#define ETHER_ADDR_STRLEN (3*ETHER_ADDR_LEN)
32ac65d9
LB
45/*
46 * there isn't a portable ethernet address type. We define our
47 * own to simplify internal handling
48 */
49struct ethaddr {
50 u_char octet[ETHER_ADDR_LEN];
79c0c959 51} __attribute__ ((packed));
32ac65d9
LB
52
53
a440846e 54/* length is the number of valuable bits of prefix structure
55* 18 bytes is current length in structure, if address is ipv4
56* 30 bytes is in case of ipv6
57*/
58#define PREFIX_LEN_ROUTE_TYPE_5_IPV4 (18*8)
59#define PREFIX_LEN_ROUTE_TYPE_5_IPV6 (30*8)
60
61/* EVPN address (RFC 7432) */
62struct evpn_addr
63{
64 u_char route_type;
65 u_char flags;
66#define IP_ADDR_NONE 0x0
67#define IP_ADDR_V4 0x1
68#define IP_ADDR_V6 0x2
4470c22c
PG
69#define IP_PREFIX_V4 0x4
70#define IP_PREFIX_V6 0x8
a440846e 71 struct ethaddr mac;
72 uint32_t eth_tag;
73 u_char ip_prefix_length;
74 union
75 {
78b81eaa 76 u_char addr;
a440846e 77 struct in_addr v4_addr;
78 struct in6_addr v6_addr;
79 } ip;
80};
81
82/* EVPN prefix structure. */
83struct prefix_evpn
84{
85 u_char family;
86 u_char prefixlen;
87 struct evpn_addr prefix __attribute__ ((aligned (8)));
88};
89
9d24baaa 90/*
91 * A struct prefix contains an address family, a prefix length, and an
92 * address. This can represent either a 'network prefix' as defined
93 * by CIDR, where the 'host bits' of the prefix are 0
94 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
95 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
96 * interface.
97 */
98
32ac65d9
LB
99/* different OSes use different names */
100#if defined(AF_PACKET)
101#define AF_ETHERNET AF_PACKET
102#else
103#if defined(AF_LINK)
104#define AF_ETHERNET AF_LINK
105#endif
106#endif
107
718e3744 108/* IPv4 and IPv6 unified prefix structure. */
109struct prefix
110{
111 u_char family;
112 u_char prefixlen;
113 union
114 {
115 u_char prefix;
116 struct in_addr prefix4;
718e3744 117 struct in6_addr prefix6;
718e3744 118 struct
119 {
120 struct in_addr id;
121 struct in_addr adv_router;
122 } lp;
32ac65d9 123 struct ethaddr prefix_eth; /* AF_ETHERNET */
718e3744 124 u_char val[8];
b4b359a2 125 uintptr_t ptr;
a440846e 126 struct evpn_addr prefix_evpn;
718e3744 127 } u __attribute__ ((aligned (8)));
128};
129
130/* IPv4 prefix structure. */
131struct prefix_ipv4
132{
133 u_char family;
134 u_char prefixlen;
135 struct in_addr prefix __attribute__ ((aligned (8)));
136};
137
138/* IPv6 prefix structure. */
718e3744 139struct prefix_ipv6
140{
141 u_char family;
142 u_char prefixlen;
143 struct in6_addr prefix __attribute__ ((aligned (8)));
144};
718e3744 145
146struct prefix_ls
147{
148 u_char family;
149 u_char prefixlen;
150 struct in_addr id __attribute__ ((aligned (8)));
151 struct in_addr adv_router;
152};
153
154/* Prefix for routing distinguisher. */
155struct prefix_rd
156{
157 u_char family;
158 u_char prefixlen;
159 u_char val[8] __attribute__ ((aligned (8)));
160};
161
32ac65d9
LB
162/* Prefix for ethernet. */
163struct prefix_eth
164{
165 u_char family;
166 u_char prefixlen;
167 struct ethaddr eth_addr __attribute__ ((aligned (8))); /* AF_ETHERNET */
168};
169
b4b359a2
PM
170/* Prefix for a generic pointer */
171struct prefix_ptr
172{
173 u_char family;
174 u_char prefixlen;
175 uintptr_t prefix __attribute__ ((aligned (8)));
176};
177
e9219290
DS
178struct prefix_sg
179{
180 u_char family;
181 u_char prefixlen;
182 struct in_addr src __attribute ((aligned (8)));
183 struct in_addr grp;
184};
185
f7bf4153
DL
186/* helper to get type safety/avoid casts on calls
187 * (w/o this, functions accepting all prefix types need casts on the caller
188 * side, which strips type safety since the cast will accept any pointer
189 * type.)
190 */
78b81eaa 191union prefixptr
f7bf4153
DL
192{
193 struct prefix *p;
194 struct prefix_ipv4 *p4;
195 struct prefix_ipv6 *p6;
78b81eaa 196 struct prefix_evpn *evp;
f7bf4153
DL
197} __attribute__ ((transparent_union));
198
78b81eaa 199union prefixconstptr
f7bf4153
DL
200{
201 const struct prefix *p;
202 const struct prefix_ipv4 *p4;
203 const struct prefix_ipv6 *p6;
78b81eaa 204 const struct prefix_evpn *evp;
f7bf4153
DL
205} __attribute__ ((transparent_union));
206
718e3744 207#ifndef INET_ADDRSTRLEN
208#define INET_ADDRSTRLEN 16
209#endif /* INET_ADDRSTRLEN */
210
211#ifndef INET6_ADDRSTRLEN
212#define INET6_ADDRSTRLEN 46
213#endif /* INET6_ADDRSTRLEN */
214
215#ifndef INET6_BUFSIZ
216#define INET6_BUFSIZ 51
217#endif /* INET6_BUFSIZ */
218
855110bb
TT
219/* Maximum prefix string length (IPv6) */
220#define PREFIX_STRLEN 51
221
718e3744 222/* Max bit/byte length of IPv4 address. */
223#define IPV4_MAX_BYTELEN 4
224#define IPV4_MAX_BITLEN 32
225#define IPV4_MAX_PREFIXLEN 32
226#define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
227#define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0)
228#define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN)
229
230#define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000)
231#define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000)
6dc686a2 232#define IPV4_LINKLOCAL(a) ((((u_int32_t) (a)) & 0xffff0000) == 0xa9fe0000)
733cd9e5 233#define IPV4_CLASS_DE(a) ((((u_int32_t) (a)) & 0xe0000000) == 0xe0000000)
718e3744 234
235/* Max bit/byte length of IPv6 address. */
236#define IPV6_MAX_BYTELEN 16
237#define IPV6_MAX_BITLEN 128
238#define IPV6_MAX_PREFIXLEN 128
239#define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
240#define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
241#define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
242
243/* Count prefix size from mask length */
244#define PSIZE(a) (((a) + 7) / (8))
245
cd1964ff
DS
246#define BSIZE(a) ((a) * (8))
247
718e3744 248/* Prefix's family member. */
249#define PREFIX_FAMILY(p) ((p)->family)
250
5c6ed111
DL
251/* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
252#ifndef s6_addr32
253#if defined(SUNOS_5)
254/* Some SunOS define s6_addr32 only to kernel */
255#define s6_addr32 _S6_un._S6_u32
256#else
257#define s6_addr32 __u6_addr.__u6_addr32
258#endif /* SUNOS_5 */
259#endif /*s6_addr32*/
260
718e3744 261/* Prototypes. */
f3ccedaa 262extern int str2family(const char *);
4c9641ba
ML
263extern int afi2family (afi_t);
264extern afi_t family2afi (int);
1ec23d90 265extern const char *safi2str(safi_t safi);
32ac65d9 266extern const char *afi2str(afi_t afi);
8cc4198f 267
f63f06da
PJ
268/* Check bit of the prefix. */
269extern unsigned int prefix_bit (const u_char *prefix, const u_char prefixlen);
270extern unsigned int prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen);
271
8cc4198f 272extern struct prefix *prefix_new (void);
273extern void prefix_free (struct prefix *);
274extern const char *prefix_family_str (const struct prefix *);
275extern int prefix_blen (const struct prefix *);
276extern int str2prefix (const char *, struct prefix *);
4690c7d7 277
a9ea959f 278#define PREFIX2STR_BUFFER PREFIX_STRLEN
279
78b81eaa 280extern const char *prefix2str (union prefixconstptr, char *, int);
8cc4198f 281extern int prefix_match (const struct prefix *, const struct prefix *);
282extern int prefix_same (const struct prefix *, const struct prefix *);
283extern int prefix_cmp (const struct prefix *, const struct prefix *);
17e52061 284extern int prefix_common_bits (const struct prefix *, const struct prefix *);
8cc4198f 285extern void prefix_copy (struct prefix *dest, const struct prefix *src);
286extern void apply_mask (struct prefix *);
287
288extern struct prefix *sockunion2prefix (const union sockunion *dest,
289 const union sockunion *mask);
40ee54a7 290extern struct prefix *sockunion2hostprefix (const union sockunion *, struct prefix *p);
17e52061 291extern void prefix2sockunion (const struct prefix *, union sockunion *);
8cc4198f 292
32ac65d9
LB
293extern int str2prefix_eth (const char *, struct prefix_eth *);
294
8cc4198f 295extern struct prefix_ipv4 *prefix_ipv4_new (void);
296extern void prefix_ipv4_free (struct prefix_ipv4 *);
297extern int str2prefix_ipv4 (const char *, struct prefix_ipv4 *);
298extern void apply_mask_ipv4 (struct prefix_ipv4 *);
299
e4529636
AS
300#define PREFIX_COPY_IPV4(DST, SRC) \
301 *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC));
302
8cc4198f 303extern int prefix_ipv4_any (const struct prefix_ipv4 *);
304extern void apply_classful_mask_ipv4 (struct prefix_ipv4 *);
305
306extern u_char ip_masklen (struct in_addr);
9663386f 307extern void masklen2ip (const int, struct in_addr *);
3fb9cd6e 308/* returns the network portion of the host address */
8cc4198f 309extern in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen);
3fb9cd6e 310/* given the address of a host on a network and the network mask length,
311 * calculate the broadcast address for that network;
312 * special treatment for /31: returns the address of the other host
313 * on the network by flipping the host bit */
8cc4198f 314extern in_addr_t ipv4_broadcast_addr (in_addr_t hostaddr, int masklen);
3fb9cd6e 315
8cc4198f 316extern int netmask_str2prefix_str (const char *, const char *, char *);
718e3744 317
8cc4198f 318extern struct prefix_ipv6 *prefix_ipv6_new (void);
319extern void prefix_ipv6_free (struct prefix_ipv6 *);
320extern int str2prefix_ipv6 (const char *, struct prefix_ipv6 *);
321extern void apply_mask_ipv6 (struct prefix_ipv6 *);
718e3744 322
e4529636
AS
323#define PREFIX_COPY_IPV6(DST, SRC) \
324 *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
325
8cc4198f 326extern int ip6_masklen (struct in6_addr);
21f569e3 327extern void masklen2ip6 (const int, struct in6_addr *);
b04c699e 328
8cc4198f 329extern const char *inet6_ntoa (struct in6_addr);
5920990f 330
db42a173
PG
331extern int prefix_str2mac(const char *str, struct ethaddr *mac);
332extern char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size);
c215ecaf 333
d914d5ff
DS
334static inline int ipv6_martian (struct in6_addr *addr)
335{
336 struct in6_addr localhost_addr;
337
338 inet_pton (AF_INET6, "::1", &localhost_addr);
339
340 if (IPV6_ADDR_SAME(&localhost_addr, addr))
341 return 1;
342
343 return 0;
344}
345
8cc4198f 346extern int all_digit (const char *);
718e3744 347
d914d5ff 348/* NOTE: This routine expects the address argument in network byte order. */
6ee06fa9
PM
349static inline int ipv4_martian (struct in_addr *addr)
350{
d914d5ff 351 in_addr_t ip = ntohl(addr->s_addr);
6ee06fa9
PM
352
353 if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) {
354 return 1;
355 }
356 return 0;
357}
358
18ff3edd
DS
359static inline int
360is_default_prefix (struct prefix *p)
361{
362 if (!p)
363 return 0;
364
365 if (((p->family == AF_INET) && (p->u.prefix4.s_addr == INADDR_ANY))
366 || ((p->family == AF_INET6) &&
367 !memcmp(&p->u.prefix6, &in6addr_any, sizeof (struct in6_addr))))
368 return 1;
369
370 return 0;
371}
372
718e3744 373#endif /* _ZEBRA_PREFIX_H */