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