net.h 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *	BIRD Internet Routing Daemon -- Network addresses
 *
 *	(c) 2015 Ondrej Zajicek <santiago@crfreenet.org>
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#ifndef _BIRD_NET_H_
#define _BIRD_NET_H_

#include "lib/ip.h"


#define NET_IP4		1
#define NET_IP6		2
#define NET_VPN4	3
#define NET_VPN6	4
Pavel Tvrdík's avatar
Pavel Tvrdík committed
20 21 22
#define NET_ROA4	5
#define NET_ROA6	6
#define NET_MAX		7
23

24 25 26 27
#define NB_IP4		(1 << NET_IP4)
#define NB_IP6		(1 << NET_IP6)
#define NB_VPN4		(1 << NET_VPN4)
#define NB_VPN6		(1 << NET_VPN6)
Ondřej Zajíček's avatar
Ondřej Zajíček committed
28 29
#define NB_ROA4		(1 << NET_ROA4)
#define NB_ROA6		(1 << NET_ROA6)
30 31 32 33 34

#define NB_IP		(NB_IP4 | NB_IP6)
#define NB_ANY		0xffffffff


35 36 37 38
typedef struct net_addr {
  u8 type;
  u8 pxlen;
  u16 length;
39
  u8 data[16];
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  u64 align[0];
} net_addr;

typedef struct net_addr_ip4 {
  u8 type;
  u8 pxlen;
  u16 length;
  ip4_addr prefix;
} net_addr_ip4;

typedef struct net_addr_ip6 {
  u8 type;
  u8 pxlen;
  u16 length;
  ip6_addr prefix;
} net_addr_ip6;

typedef struct net_addr_vpn4 {
  u8 type;
  u8 pxlen;
  u16 length;
  ip4_addr prefix;
  u64 rd;
} net_addr_vpn4;

typedef struct net_addr_vpn6 {
  u8 type;
  u8 pxlen;
  u16 length;
  ip6_addr prefix;
  u64 rd;
} net_addr_vpn6;

Pavel Tvrdík's avatar
Pavel Tvrdík committed
73 74 75 76 77
typedef struct net_addr_roa4 {
  u8 type;
  u8 pxlen;
  u16 length;
  ip4_addr prefix;
78
  u32 max_pxlen;
Pavel Tvrdík's avatar
Pavel Tvrdík committed
79 80 81 82 83 84 85 86
  u32 asn;
} net_addr_roa4;

typedef struct net_addr_roa6 {
  u8 type;
  u8 pxlen;
  u16 length;
  ip6_addr prefix;
87
  u32 max_pxlen;
Pavel Tvrdík's avatar
Pavel Tvrdík committed
88 89
  u32 asn;
} net_addr_roa6;
90 91 92 93 94 95 96

typedef union net_addr_union {
  net_addr n;
  net_addr_ip4 ip4;
  net_addr_ip6 ip6;
  net_addr_vpn4 vpn4;
  net_addr_vpn6 vpn6;
Pavel Tvrdík's avatar
Pavel Tvrdík committed
97 98
  net_addr_roa4 roa4;
  net_addr_roa6 roa6;
99 100 101
} net_addr_union;


102
extern const char * const net_label[];
103
extern const u16 net_addr_length[];
104 105 106 107 108
extern const u8  net_max_prefix_length[];
extern const u16 net_max_text_length[];

#define NET_MAX_TEXT_LENGTH	65

109 110 111 112 113 114 115 116 117 118 119 120 121

#define NET_ADDR_IP4(prefix,pxlen) \
  ((net_addr_ip4) { NET_IP4, pxlen, sizeof(net_addr_ip4), prefix })

#define NET_ADDR_IP6(prefix,pxlen) \
  ((net_addr_ip6) { NET_IP6, pxlen, sizeof(net_addr_ip6), prefix })

#define NET_ADDR_VPN4(prefix,pxlen,rd) \
  ((net_addr_vpn4) { NET_VPN4, pxlen, sizeof(net_addr_vpn4), prefix, rd })

#define NET_ADDR_VPN6(prefix,pxlen,rd) \
  ((net_addr_vpn6) { NET_VPN6, pxlen, sizeof(net_addr_vpn6), prefix, rd })

Pavel Tvrdík's avatar
Pavel Tvrdík committed
122 123 124 125 126 127 128
#define NET_ADDR_ROA4(prefix,pxlen,max_pxlen,asn) \
  ((net_addr_roa4) { NET_ROA4, pxlen, sizeof(net_addr_roa4), prefix, max_pxlen, asn })

#define NET_ADDR_ROA6(prefix,pxlen,max_pxlen,asn) \
  ((net_addr_roa6) { NET_ROA6, pxlen, sizeof(net_addr_roa6), prefix, max_pxlen, asn })


129 130 131 132 133 134 135 136 137 138 139 140 141

static inline void net_fill_ip4(net_addr *a, ip4_addr prefix, uint pxlen)
{ *(net_addr_ip4 *)a = NET_ADDR_IP4(prefix, pxlen); }

static inline void net_fill_ip6(net_addr *a, ip6_addr prefix, uint pxlen)
{ *(net_addr_ip6 *)a = NET_ADDR_IP6(prefix, pxlen); }

static inline void net_fill_vpn4(net_addr *a, ip4_addr prefix, uint pxlen, u64 rd)
{ *(net_addr_vpn4 *)a = NET_ADDR_VPN4(prefix, pxlen, rd); }

static inline void net_fill_vpn6(net_addr *a, ip6_addr prefix, uint pxlen, u64 rd)
{ *(net_addr_vpn6 *)a = NET_ADDR_VPN6(prefix, pxlen, rd); }

Pavel Tvrdík's avatar
Pavel Tvrdík committed
142 143 144 145 146 147
static inline void net_fill_roa4(net_addr *a, ip4_addr prefix, uint pxlen, uint max_pxlen, u32 asn)
{ *(net_addr_roa4 *)a = NET_ADDR_ROA4(prefix, pxlen, max_pxlen, asn); }

static inline void net_fill_roa6(net_addr *a, ip6_addr prefix, uint pxlen, uint max_pxlen, u32 asn)
{ *(net_addr_roa6 *)a = NET_ADDR_ROA6(prefix, pxlen, max_pxlen, asn); }

148 149 150 151 152 153 154 155
static inline void net_fill_ipa(net_addr *a, ip_addr prefix, uint pxlen)
{
  if (ipa_is_ip4(prefix))
    net_fill_ip4(a, ipa_to_ip4(prefix), pxlen);
  else
    net_fill_ip6(a, ipa_to_ip6(prefix), pxlen);
}

156 157 158 159 160 161 162 163
static inline void net_fill_ip_host(net_addr *a, ip_addr prefix)
{
  if (ipa_is_ip4(prefix))
    net_fill_ip4(a, ipa_to_ip4(prefix), IP4_MAX_PREFIX_LENGTH);
  else
    net_fill_ip6(a, ipa_to_ip6(prefix), IP6_MAX_PREFIX_LENGTH);
}

164 165 166 167 168 169 170

static inline int net_val_match(u8 type, u32 mask)
{ return !!((1 << type) & mask); }

static inline int net_type_match(const net_addr *a, u32 mask)
{ return net_val_match(a->type, mask); }

171 172 173
static inline int net_is_ip(const net_addr *a)
{ return (a->type == NET_IP4) || (a->type == NET_IP6); }

174 175 176
static inline int net_is_roa(const net_addr *a)
{ return (a->type == NET_ROA4) || (a->type == NET_ROA6); }

177

178 179 180 181 182 183 184 185 186 187 188
static inline ip4_addr net4_prefix(const net_addr *a)
{ return ((net_addr_ip4 *) a)->prefix; }

static inline ip6_addr net6_prefix(const net_addr *a)
{ return ((net_addr_ip6 *) a)->prefix; }

static inline ip_addr net_prefix(const net_addr *a)
{
  switch (a->type)
  {
  case NET_IP4:
Pavel Tvrdík's avatar
Pavel Tvrdík committed
189 190 191 192
  case NET_VPN4:
  case NET_ROA4:
    return ipa_from_ip4(net4_prefix(a));

193
  case NET_IP6:
Pavel Tvrdík's avatar
Pavel Tvrdík committed
194 195 196 197 198 199
  case NET_VPN6:
  case NET_ROA6:
    return ipa_from_ip6(net6_prefix(a));

  default:
    return IPA_NONE;
200 201 202 203 204 205 206 207 208 209 210 211
  }
}

static inline uint net4_pxlen(const net_addr *a)
{ return a->pxlen; }

static inline uint net6_pxlen(const net_addr *a)
{ return a->pxlen; }

static inline uint net_pxlen(const net_addr *a)
{ return a->pxlen; }

212 213
ip_addr net_pxmask(const net_addr *a);

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

static inline int net_equal(const net_addr *a, const net_addr *b)
{ return (a->length == b->length) && !memcmp(a, b, a->length); }

static inline int net_equal_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
{ return !memcmp(a, b, sizeof(net_addr_ip4)); }

static inline int net_equal_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
{ return !memcmp(a, b, sizeof(net_addr_ip6)); }

static inline int net_equal_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b)
{ return !memcmp(a, b, sizeof(net_addr_vpn4)); }

static inline int net_equal_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
{ return !memcmp(a, b, sizeof(net_addr_vpn6)); }

Pavel Tvrdík's avatar
Pavel Tvrdík committed
230 231 232 233 234 235
static inline int net_equal_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
{ return !memcmp(a, b, sizeof(net_addr_roa4)); }

static inline int net_equal_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
{ return !memcmp(a, b, sizeof(net_addr_roa6)); }

Ondřej Zajíček's avatar
Ondřej Zajíček committed
236 237 238 239 240 241
static inline int net_equal_prefix_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
{ return ip4_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); }

static inline int net_equal_prefix_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
{ return ip6_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); }

242 243 244 245 246 247 248 249 250 251 252 253 254

static inline int net_zero_ip4(const net_addr_ip4 *a)
{ return !a->pxlen && ip4_zero(a->prefix); }

static inline int net_zero_ip6(const net_addr_ip6 *a)
{ return !a->pxlen && ip6_zero(a->prefix); }

static inline int net_zero_vpn4(const net_addr_vpn4 *a)
{ return !a->pxlen && ip4_zero(a->prefix) && !a->rd; }

static inline int net_zero_vpn6(const net_addr_vpn6 *a)
{ return !a->pxlen && ip6_zero(a->prefix) && !a->rd; }

Pavel Tvrdík's avatar
Pavel Tvrdík committed
255
static inline int net_zero_roa4(const net_addr_roa4 *a)
Pavel Tvrdík's avatar
Pavel Tvrdík committed
256
{ return !a->pxlen && ip4_zero(a->prefix) && !a->max_pxlen && !a->asn; }
Pavel Tvrdík's avatar
Pavel Tvrdík committed
257 258

static inline int net_zero_roa6(const net_addr_roa6 *a)
Pavel Tvrdík's avatar
Pavel Tvrdík committed
259
{ return !a->pxlen && ip6_zero(a->prefix) && !a->max_pxlen && !a->asn; }
Pavel Tvrdík's avatar
Pavel Tvrdík committed
260

261

262 263 264 265 266 267 268 269 270 271 272 273
static inline int net_compare_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }

static inline int net_compare_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }

static inline int net_compare_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b)
{ return u64_cmp(a->rd, b->rd) ?: ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }

static inline int net_compare_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
{ return u64_cmp(a->rd, b->rd) ?: ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }

Pavel Tvrdík's avatar
Pavel Tvrdík committed
274
static inline int net_compare_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
Pavel Tvrdík's avatar
Pavel Tvrdík committed
275
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->max_pxlen, b->max_pxlen) ?: uint_cmp(a->asn, b->asn); }
Pavel Tvrdík's avatar
Pavel Tvrdík committed
276 277

static inline int net_compare_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
Pavel Tvrdík's avatar
Pavel Tvrdík committed
278
{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->max_pxlen, b->max_pxlen) ?: uint_cmp(a->asn, b->asn); }
Pavel Tvrdík's avatar
Pavel Tvrdík committed
279

280 281 282
int net_compare(const net_addr *a, const net_addr *b);


283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
static inline void net_copy(net_addr *dst, const net_addr *src)
{ memcpy(dst, src, src->length); }

static inline void net_copy_ip4(net_addr_ip4 *dst, const net_addr_ip4 *src)
{ memcpy(dst, src, sizeof(net_addr_ip4)); }

static inline void net_copy_ip6(net_addr_ip6 *dst, const net_addr_ip6 *src)
{ memcpy(dst, src, sizeof(net_addr_ip6)); }

static inline void net_copy_vpn4(net_addr_vpn4 *dst, const net_addr_vpn4 *src)
{ memcpy(dst, src, sizeof(net_addr_vpn4)); }

static inline void net_copy_vpn6(net_addr_vpn6 *dst, const net_addr_vpn6 *src)
{ memcpy(dst, src, sizeof(net_addr_vpn6)); }

Pavel Tvrdík's avatar
Pavel Tvrdík committed
298 299 300 301 302 303
static inline void net_copy_roa4(net_addr_roa4 *dst, const net_addr_roa4 *src)
{ memcpy(dst, src, sizeof(net_addr_roa4)); }

static inline void net_copy_roa6(net_addr_roa6 *dst, const net_addr_roa6 *src)
{ memcpy(dst, src, sizeof(net_addr_roa6)); }

304 305

static inline u32 net_hash_ip4(const net_addr_ip4 *n)
306
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
307 308

static inline u32 net_hash_ip6(const net_addr_ip6 *n)
309
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
310 311

/* XXXX */
312
static inline u32 u64_hash(u64 a)
313 314 315
{ return u32_hash(a); }

static inline u32 net_hash_vpn4(const net_addr_vpn4 *n)
316
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
317 318

static inline u32 net_hash_vpn6(const net_addr_vpn6 *n)
319
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
320

Pavel Tvrdík's avatar
Pavel Tvrdík committed
321
static inline u32 net_hash_roa4(const net_addr_roa4 *n)
Pavel Tvrdík's avatar
Pavel Tvrdík committed
322
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
Pavel Tvrdík's avatar
Pavel Tvrdík committed
323 324

static inline u32 net_hash_roa6(const net_addr_roa6 *n)
Pavel Tvrdík's avatar
Pavel Tvrdík committed
325
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
Pavel Tvrdík's avatar
Pavel Tvrdík committed
326

327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
static inline int net_validate_ip4(const net_addr_ip4 *n)
{
  return (n->pxlen <= IP4_MAX_PREFIX_LENGTH) &&
    ip4_zero(ip4_and(n->prefix, ip4_not(ip4_mkmask(n->pxlen))));
}

static inline int net_validate_ip6(const net_addr_ip6 *n)
{
  return (n->pxlen <= IP6_MAX_PREFIX_LENGTH) &&
    ip6_zero(ip6_and(n->prefix, ip6_not(ip6_mkmask(n->pxlen))));
}

int net_validate(const net_addr *N);


343 344 345 346 347 348 349 350
static inline void net_normalize_ip4(net_addr_ip4 *n)
{ n->prefix = ip4_and(n->prefix, ip4_mkmask(n->pxlen)); }

static inline void net_normalize_ip6(net_addr_ip6 *n)
{ n->prefix = ip6_and(n->prefix, ip6_mkmask(n->pxlen)); }

void net_normalize(net_addr *N);

351

352
int net_classify(const net_addr *N);
353
int net_format(const net_addr *N, char *buf, int buflen);
354 355


356 357 358
int ipa_in_netX(const ip_addr A, const net_addr *N);
int net_in_netX(const net_addr *A, const net_addr *N);

359 360

#endif