2015-11-05 19:48:52 +08:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/ip.h"
|
|
|
|
#include "lib/net.h"
|
|
|
|
|
2015-12-11 22:35:37 +08:00
|
|
|
|
2015-11-05 19:48:52 +08:00
|
|
|
const u16 net_addr_length[] = {
|
|
|
|
[NET_IP4] = sizeof(net_addr_ip4),
|
|
|
|
[NET_IP6] = sizeof(net_addr_ip6),
|
|
|
|
[NET_VPN4] = sizeof(net_addr_vpn4),
|
|
|
|
[NET_VPN6] = sizeof(net_addr_vpn6)
|
2015-12-07 23:24:18 +08:00
|
|
|
};
|
2015-11-05 19:48:52 +08:00
|
|
|
|
2015-12-11 22:35:37 +08:00
|
|
|
const u8 net_max_prefix_length[] = {
|
|
|
|
[NET_IP4] = IP4_MAX_PREFIX_LENGTH,
|
|
|
|
[NET_IP6] = IP6_MAX_PREFIX_LENGTH,
|
|
|
|
[NET_VPN4] = IP4_MAX_PREFIX_LENGTH,
|
|
|
|
[NET_VPN6] = IP4_MAX_PREFIX_LENGTH
|
|
|
|
};
|
|
|
|
|
2015-12-18 18:57:38 +08:00
|
|
|
const u16 net_max_text_length[] = {
|
|
|
|
[NET_IP4] = 18, /* "255.255.255.255/32" */
|
|
|
|
[NET_IP6] = 43, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
|
|
|
|
[NET_VPN4] = 40, /* "4294967296:4294967296 255.255.255.255/32" */
|
|
|
|
[NET_VPN6] = 65, /* "4294967296:4294967296 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
|
|
|
|
};
|
|
|
|
|
2015-12-11 22:35:37 +08:00
|
|
|
|
2015-12-07 23:24:18 +08:00
|
|
|
int
|
2015-11-05 19:48:52 +08:00
|
|
|
net_format(const net_addr *N, char *buf, int buflen)
|
|
|
|
{
|
|
|
|
net_addr_union *n = (void *) N;
|
|
|
|
|
2015-12-21 10:27:41 +08:00
|
|
|
/* XXXX fix %I vs %R */
|
2015-11-05 19:48:52 +08:00
|
|
|
switch (n->n.type)
|
|
|
|
{
|
|
|
|
case NET_IP4:
|
2015-12-21 10:27:41 +08:00
|
|
|
return bsnprintf(buf, buflen, "%R/%d", n->ip4.prefix, n->ip4.pxlen);
|
2015-11-05 19:48:52 +08:00
|
|
|
case NET_IP6:
|
|
|
|
return bsnprintf(buf, buflen, "%I/%d", n->ip6.prefix, n->ip6.pxlen);
|
|
|
|
case NET_VPN4:
|
2015-12-21 10:27:41 +08:00
|
|
|
return bsnprintf(buf, buflen, "%u:%u %R/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
|
2015-11-05 19:48:52 +08:00
|
|
|
case NET_VPN6:
|
|
|
|
return bsnprintf(buf, buflen, "%u:%u %I/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
|
|
|
|
}
|
2015-12-07 23:24:18 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ip_addr
|
|
|
|
net_pxmask(const net_addr *a)
|
|
|
|
{
|
|
|
|
switch (a->type)
|
|
|
|
{
|
|
|
|
case NET_IP4:
|
|
|
|
case NET_VPN4:
|
|
|
|
return ipa_from_ip4(ip4_mkmask(net4_pxlen(a)));
|
|
|
|
|
|
|
|
case NET_IP6:
|
|
|
|
case NET_VPN6:
|
|
|
|
return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
|
|
|
|
|
|
|
|
default:
|
|
|
|
return IPA_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:25:12 +08:00
|
|
|
int
|
|
|
|
net_compare(const net_addr *a, const net_addr *b)
|
2015-12-07 23:24:18 +08:00
|
|
|
{
|
2015-12-16 17:25:12 +08:00
|
|
|
if (a->type != b->type)
|
|
|
|
return uint_cmp(a->type, b->type);
|
2015-12-07 23:24:18 +08:00
|
|
|
|
2015-12-16 17:25:12 +08:00
|
|
|
switch (a->type)
|
|
|
|
{
|
|
|
|
case NET_IP4:
|
|
|
|
return net_compare_ip4((const net_addr_ip4 *) a, (const net_addr_ip4 *) b);
|
|
|
|
case NET_IP6:
|
|
|
|
return net_compare_ip6((const net_addr_ip6 *) a, (const net_addr_ip6 *) b);
|
|
|
|
case NET_VPN4:
|
|
|
|
return net_compare_vpn4((const net_addr_vpn4 *) a, (const net_addr_vpn4 *) b);
|
|
|
|
case NET_VPN6:
|
|
|
|
return net_compare_vpn6((const net_addr_vpn6 *) a, (const net_addr_vpn6 *) b);
|
|
|
|
}
|
|
|
|
return 0;
|
2015-12-07 23:24:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
net_validate(const net_addr *N)
|
|
|
|
{
|
2015-12-16 17:25:12 +08:00
|
|
|
switch (N->type)
|
2015-12-07 23:24:18 +08:00
|
|
|
{
|
|
|
|
case NET_IP4:
|
|
|
|
case NET_VPN4:
|
|
|
|
return net_validate_ip4((net_addr_ip4 *) N);
|
|
|
|
|
|
|
|
case NET_IP6:
|
|
|
|
case NET_VPN6:
|
|
|
|
return net_validate_ip6((net_addr_ip6 *) N);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2015-11-05 19:48:52 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 17:43:58 +08:00
|
|
|
void
|
|
|
|
net_normalize(net_addr *N)
|
|
|
|
{
|
|
|
|
net_addr_union *n = (void *) N;
|
|
|
|
|
|
|
|
switch (n->n.type)
|
|
|
|
{
|
|
|
|
case NET_IP4:
|
|
|
|
case NET_VPN4:
|
|
|
|
return net_normalize_ip4(&n->ip4);
|
|
|
|
|
|
|
|
case NET_IP6:
|
|
|
|
case NET_VPN6:
|
|
|
|
return net_normalize_ip6(&n->ip6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 19:48:52 +08:00
|
|
|
int
|
|
|
|
net_classify(const net_addr *N)
|
|
|
|
{
|
|
|
|
net_addr_union *n = (void *) N;
|
|
|
|
|
|
|
|
switch (n->n.type)
|
|
|
|
{
|
|
|
|
case NET_IP4:
|
|
|
|
case NET_VPN4:
|
|
|
|
return ip4_zero(n->ip4.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip4_classify(n->ip4.prefix);
|
|
|
|
|
|
|
|
case NET_IP6:
|
|
|
|
case NET_VPN6:
|
2015-12-07 23:24:18 +08:00
|
|
|
return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);
|
2015-11-05 19:48:52 +08:00
|
|
|
}
|
2015-12-07 23:24:18 +08:00
|
|
|
|
2015-12-22 00:17:21 +08:00
|
|
|
return IADDR_INVALID;
|
2015-11-05 19:48:52 +08:00
|
|
|
}
|
2015-12-16 17:43:58 +08:00
|
|
|
|
|
|
|
int
|
2015-12-22 00:17:21 +08:00
|
|
|
ipa_in_netX(const ip_addr a, const net_addr *n)
|
2015-12-16 17:43:58 +08:00
|
|
|
{
|
2015-12-22 00:17:21 +08:00
|
|
|
switch (n->type)
|
2015-12-16 17:43:58 +08:00
|
|
|
{
|
|
|
|
case NET_IP4:
|
|
|
|
case NET_VPN4:
|
2015-12-22 00:17:21 +08:00
|
|
|
if (!ipa_is_ip4(a)) return 0;
|
|
|
|
return ip4_zero(ip4_and(ip4_xor(ipa_to_ip4(a), net4_prefix(n)),
|
|
|
|
ip4_mkmask(net4_pxlen(n))));
|
2015-12-16 17:43:58 +08:00
|
|
|
|
|
|
|
case NET_IP6:
|
|
|
|
case NET_VPN6:
|
2015-12-22 00:17:21 +08:00
|
|
|
if (ipa_is_ip4(a)) return 0;
|
|
|
|
return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)),
|
|
|
|
ip6_mkmask(net6_pxlen(n))));
|
2015-12-16 17:43:58 +08:00
|
|
|
|
2015-12-22 00:17:21 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-16 17:43:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-12-22 00:17:21 +08:00
|
|
|
net_in_netX(const net_addr *a, const net_addr *n)
|
2015-12-16 17:43:58 +08:00
|
|
|
{
|
2015-12-22 00:17:21 +08:00
|
|
|
if (a->type != n->type)
|
2015-12-16 17:43:58 +08:00
|
|
|
return 0;
|
|
|
|
|
2015-12-22 00:17:21 +08:00
|
|
|
return (net_pxlen(n) <= net_pxlen(a)) && ipa_in_netX(net_prefix(a), n);
|
2015-12-16 17:43:58 +08:00
|
|
|
}
|