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