Statistics
| Branch: | Revision:

iof-bird-daemon / lib / net.c @ f4a60a9b

History | View | Annotate | Download (4.67 KB)

1

    
2
#include "nest/bird.h"
3
#include "lib/ip.h"
4
#include "lib/net.h"
5

    
6

    
7
const char * const net_label[] = {
8
  [NET_IP4] = "ipv4",
9
  [NET_IP6] = "ipv6",
10
  [NET_VPN4] = "vpn4",
11
  [NET_VPN6] = "vpn6"
12
};
13

    
14
const u16 net_addr_length[] = {
15
  [NET_IP4] = sizeof(net_addr_ip4),
16
  [NET_IP6] = sizeof(net_addr_ip6),
17
  [NET_VPN4] = sizeof(net_addr_vpn4),
18
  [NET_VPN6] = sizeof(net_addr_vpn6),
19
  [NET_ROA4] = sizeof(net_addr_roa4),
20
  [NET_ROA6] = sizeof(net_addr_roa6)
21
};
22

    
23
const u8 net_max_prefix_length[] = {
24
  [NET_IP4] = IP4_MAX_PREFIX_LENGTH,
25
  [NET_IP6] = IP6_MAX_PREFIX_LENGTH,
26
  [NET_VPN4] = IP4_MAX_PREFIX_LENGTH,
27
  [NET_VPN6] = IP6_MAX_PREFIX_LENGTH,
28
  [NET_ROA4] = IP4_MAX_PREFIX_LENGTH,
29
  [NET_ROA6] = IP6_MAX_PREFIX_LENGTH
30
};
31

    
32
const u16 net_max_text_length[] = {
33
  [NET_IP4] = 18,        /* "255.255.255.255/32" */
34
  [NET_IP6] = 43,        /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
35
  [NET_VPN4] = 40,        /* "4294967296:4294967296 255.255.255.255/32" */
36
  [NET_VPN6] = 65,        /* "4294967296:4294967296 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
37
  [NET_ROA4] = 30,      /* "255.255.255.255/32 AS4294967295" */
38
  [NET_ROA6] = 56,      /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128 AS4294967295" */
39
};
40

    
41

    
42
int
43
net_format(const net_addr *N, char *buf, int buflen)
44
{
45
  net_addr_union *n = (void *) N;
46

    
47
  switch (n->n.type)
48
  {
49
  case NET_IP4:
50
    return bsnprintf(buf, buflen, "%I4/%d", n->ip4.prefix, n->ip4.pxlen);
51
  case NET_IP6:
52
    return bsnprintf(buf, buflen, "%I6/%d", n->ip6.prefix, n->ip6.pxlen);
53
  case NET_VPN4:
54
    return bsnprintf(buf, buflen, "%u:%u %I4/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
55
  case NET_VPN6:
56
    return bsnprintf(buf, buflen, "%u:%u %I6/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
57
  case NET_ROA4:
58
    return bsnprintf(buf, buflen, "%I4/%u-%u AS%u",  n->roa4.prefix, n->roa4.pxlen, n->roa4.max_pxlen, n->roa4.asn);
59
  case NET_ROA6:
60
    return bsnprintf(buf, buflen, "%I6/%u-%u AS%u",  n->roa6.prefix, n->roa6.pxlen, n->roa6.max_pxlen, n->roa6.asn);
61
  }
62

    
63
  return 0;
64
}
65

    
66
ip_addr
67
net_pxmask(const net_addr *a)
68
{
69
  switch (a->type)
70
  {
71
  case NET_IP4:
72
  case NET_VPN4:
73
  case NET_ROA4:
74
    return ipa_from_ip4(ip4_mkmask(net4_pxlen(a)));
75

    
76
  case NET_IP6:
77
  case NET_VPN6:
78
  case NET_ROA6:
79
    return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
80

    
81
  default:
82
    return IPA_NONE;
83
  }
84
}
85

    
86
int
87
net_compare(const net_addr *a, const net_addr *b)
88
{
89
  if (a->type != b->type)
90
    return uint_cmp(a->type, b->type);
91

    
92
  switch (a->type)
93
  {
94
  case NET_IP4:
95
    return net_compare_ip4((const net_addr_ip4 *) a, (const net_addr_ip4 *) b);
96
  case NET_IP6:
97
    return net_compare_ip6((const net_addr_ip6 *) a, (const net_addr_ip6 *) b);
98
  case NET_VPN4:
99
    return net_compare_vpn4((const net_addr_vpn4 *) a, (const net_addr_vpn4 *) b);
100
  case NET_VPN6:
101
    return net_compare_vpn6((const net_addr_vpn6 *) a, (const net_addr_vpn6 *) b);
102
  case NET_ROA4:
103
    return net_compare_roa4((const net_addr_roa4 *) a, (const net_addr_roa4 *) b);
104
  case NET_ROA6:
105
    return net_compare_roa6((const net_addr_roa6 *) a, (const net_addr_roa6 *) b);
106
  }
107
  return 0;
108
}
109

    
110
int
111
net_validate(const net_addr *N)
112
{
113
  switch (N->type)
114
  {
115
  case NET_IP4:
116
  case NET_VPN4:
117
  case NET_ROA4:
118
    return net_validate_ip4((net_addr_ip4 *) N);
119

    
120
  case NET_IP6:
121
  case NET_VPN6:
122
  case NET_ROA6:
123
    return net_validate_ip6((net_addr_ip6 *) N);
124

    
125
  default:
126
    return 0;
127
  }
128
}
129

    
130
void
131
net_normalize(net_addr *N)
132
{
133
  net_addr_union *n = (void *) N;
134

    
135
  switch (n->n.type)
136
  {
137
  case NET_IP4:
138
  case NET_VPN4:
139
  case NET_ROA4:
140
    return net_normalize_ip4(&n->ip4);
141

    
142
  case NET_IP6:
143
  case NET_VPN6:
144
  case NET_ROA6:
145
    return net_normalize_ip6(&n->ip6);
146
  }
147
}
148

    
149
int
150
net_classify(const net_addr *N)
151
{
152
  net_addr_union *n = (void *) N;
153

    
154
  switch (n->n.type)
155
  {
156
  case NET_IP4:
157
  case NET_VPN4:
158
  case NET_ROA4:
159
    return ip4_zero(n->ip4.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip4_classify(n->ip4.prefix);
160

    
161
  case NET_IP6:
162
  case NET_VPN6:
163
  case NET_ROA6:
164
    return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);
165
  }
166

    
167
  return IADDR_INVALID;
168
}
169

    
170
int
171
ipa_in_netX(const ip_addr a, const net_addr *n)
172
{
173
  switch (n->type)
174
  {
175
  case NET_IP4:
176
  case NET_VPN4:
177
  case NET_ROA4:
178
    if (!ipa_is_ip4(a)) return 0;
179
    return ip4_zero(ip4_and(ip4_xor(ipa_to_ip4(a), net4_prefix(n)),
180
                            ip4_mkmask(net4_pxlen(n))));
181

    
182
  case NET_IP6:
183
  case NET_VPN6:
184
  case NET_ROA6:
185
    if (ipa_is_ip4(a)) return 0;
186
    return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)),
187
                            ip6_mkmask(net6_pxlen(n))));
188

    
189
  default:
190
    return 0;
191
  }
192
}
193

    
194
int
195
net_in_netX(const net_addr *a, const net_addr *n)
196
{
197
  if (a->type != n->type)
198
    return 0;
199

    
200
  return (net_pxlen(n) <= net_pxlen(a)) && ipa_in_netX(net_prefix(a), n);
201
}