Statistics
| Branch: | Revision:

iof-bird-daemon / lib / net.c @ 62e64905

History | View | Annotate | Download (7.59 KB)

1

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

    
7

    
8
const char * const net_label[] = {
9
  [NET_IP4]         = "ipv4",
10
  [NET_IP6]         = "ipv6",
11
  [NET_VPN4]         = "vpn4",
12
  [NET_VPN6]         = "vpn6",
13
  [NET_ROA4]         = "roa4",
14
  [NET_ROA6]         = "roa6",
15
  [NET_FLOW4]         = "flow4",
16
  [NET_FLOW6]         = "flow6",
17
  [NET_MPLS]        = "mpls",
18
};
19

    
20
const u16 net_addr_length[] = {
21
  [NET_IP4]         = sizeof(net_addr_ip4),
22
  [NET_IP6]         = sizeof(net_addr_ip6),
23
  [NET_VPN4]         = sizeof(net_addr_vpn4),
24
  [NET_VPN6]         = sizeof(net_addr_vpn6),
25
  [NET_ROA4]         = sizeof(net_addr_roa4),
26
  [NET_ROA6]         = sizeof(net_addr_roa6),
27
  [NET_FLOW4]         = 0,
28
  [NET_FLOW6]         = 0,
29
  [NET_MPLS]        = sizeof(net_addr_mpls),
30
};
31

    
32
const u8 net_max_prefix_length[] = {
33
  [NET_IP4]         = IP4_MAX_PREFIX_LENGTH,
34
  [NET_IP6]         = IP6_MAX_PREFIX_LENGTH,
35
  [NET_VPN4]         = IP4_MAX_PREFIX_LENGTH,
36
  [NET_VPN6]         = IP6_MAX_PREFIX_LENGTH,
37
  [NET_ROA4]         = IP4_MAX_PREFIX_LENGTH,
38
  [NET_ROA6]         = IP6_MAX_PREFIX_LENGTH,
39
  [NET_FLOW4]         = IP4_MAX_PREFIX_LENGTH,
40
  [NET_FLOW6]         = IP6_MAX_PREFIX_LENGTH,
41
  [NET_MPLS]        = 0,
42
};
43

    
44
const u16 net_max_text_length[] = {
45
  [NET_IP4]         = 18,        /* "255.255.255.255/32" */
46
  [NET_IP6]         = 43,        /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
47
  [NET_VPN4]         = 40,        /* "4294967296:4294967296 255.255.255.255/32" */
48
  [NET_VPN6]         = 65,        /* "4294967296:4294967296 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
49
  [NET_ROA4]         = 34,        /* "255.255.255.255/32-32 AS4294967295" */
50
  [NET_ROA6]         = 60,        /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128-128 AS4294967295" */
51
  [NET_FLOW4]         = 0,        /* "flow4 { ... }" */
52
  [NET_FLOW6]         = 0,        /* "flow6 { ... }" */
53
  [NET_MPLS]        = 7,        /* "1048575" */
54
};
55

    
56

    
57
int
58
net_format(const net_addr *N, char *buf, int buflen)
59
{
60
  net_addr_union *n = (void *) N;
61
  buf[0] = 0;
62

    
63
  switch (n->n.type)
64
  {
65
  case NET_IP4:
66
    return bsnprintf(buf, buflen, "%I4/%d", n->ip4.prefix, n->ip4.pxlen);
67
  case NET_IP6:
68
    return bsnprintf(buf, buflen, "%I6/%d", n->ip6.prefix, n->ip6.pxlen);
69
  case NET_VPN4:
70
    switch (n->vpn4.rd >> 48)
71
    {
72
    case 0: return bsnprintf(buf, buflen, "0:%u:%u %I4/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
73
    case 1: return bsnprintf(buf, buflen, "1:%I4:%u %I4/%d", ip4_from_u32(n->vpn4.rd >> 16), (u32) (n->vpn4.rd & 0xffff), n->vpn4.prefix, n->vpn4.pxlen);
74
    case 2: return bsnprintf(buf, buflen, "2:%u:%u %I4/%d", (u32) (n->vpn4.rd >> 16), (u32) (n->vpn4.rd & 0xffff), n->vpn4.prefix, n->vpn4.pxlen);
75
    default: return bsnprintf(buf, buflen, "X:%08x:%08x %I4/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
76
    }
77
  case NET_VPN6:
78
    /* XXX: RD format is specified for VPN4; not found any for VPN6, reusing the same as for VPN4 */
79
    switch (n->vpn6.rd >> 48)
80
    {
81
    case 0: return bsnprintf(buf, buflen, "0:%u:%u %I6/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
82
    case 1: return bsnprintf(buf, buflen, "1:%I4:%u %I6/%d", ip4_from_u32(n->vpn6.rd >> 16), (u32) (n->vpn6.rd & 0xffff), n->vpn6.prefix, n->vpn6.pxlen);
83
    case 2: return bsnprintf(buf, buflen, "2:%u:%u %I6/%d", (u32) (n->vpn6.rd >> 16), (u32) (n->vpn6.rd & 0xffff), n->vpn6.prefix, n->vpn6.pxlen);
84
    default: return bsnprintf(buf, buflen, "X:%08x:%08x %I6/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
85
    }
86
  case NET_ROA4:
87
    return bsnprintf(buf, buflen, "%I4/%u-%u AS%u",  n->roa4.prefix, n->roa4.pxlen, n->roa4.max_pxlen, n->roa4.asn);
88
  case NET_ROA6:
89
    return bsnprintf(buf, buflen, "%I6/%u-%u AS%u",  n->roa6.prefix, n->roa6.pxlen, n->roa6.max_pxlen, n->roa6.asn);
90
  case NET_FLOW4:
91
    return flow4_net_format(buf, buflen, &n->flow4);
92
  case NET_FLOW6:
93
    return flow6_net_format(buf, buflen, &n->flow6);
94
  case NET_MPLS:
95
    return bsnprintf(buf, buflen, "%u", n->mpls.label);
96
  }
97

    
98
  bug("unknown network type");
99
}
100

    
101
ip_addr
102
net_pxmask(const net_addr *a)
103
{
104
  switch (a->type)
105
  {
106
  case NET_IP4:
107
  case NET_VPN4:
108
  case NET_ROA4:
109
  case NET_FLOW4:
110
    return ipa_from_ip4(ip4_mkmask(net4_pxlen(a)));
111

    
112
  case NET_IP6:
113
  case NET_VPN6:
114
  case NET_ROA6:
115
  case NET_FLOW6:
116
    return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
117

    
118
  case NET_MPLS:
119
  default:
120
    return IPA_NONE;
121
  }
122
}
123

    
124
int
125
net_compare(const net_addr *a, const net_addr *b)
126
{
127
  if (a->type != b->type)
128
    return uint_cmp(a->type, b->type);
129

    
130
  switch (a->type)
131
  {
132
  case NET_IP4:
133
    return net_compare_ip4((const net_addr_ip4 *) a, (const net_addr_ip4 *) b);
134
  case NET_IP6:
135
    return net_compare_ip6((const net_addr_ip6 *) a, (const net_addr_ip6 *) b);
136
  case NET_VPN4:
137
    return net_compare_vpn4((const net_addr_vpn4 *) a, (const net_addr_vpn4 *) b);
138
  case NET_VPN6:
139
    return net_compare_vpn6((const net_addr_vpn6 *) a, (const net_addr_vpn6 *) b);
140
  case NET_ROA4:
141
    return net_compare_roa4((const net_addr_roa4 *) a, (const net_addr_roa4 *) b);
142
  case NET_ROA6:
143
    return net_compare_roa6((const net_addr_roa6 *) a, (const net_addr_roa6 *) b);
144
  case NET_FLOW4:
145
    return net_compare_flow4((const net_addr_flow4 *) a, (const net_addr_flow4 *) b);
146
  case NET_FLOW6:
147
    return net_compare_flow6((const net_addr_flow6 *) a, (const net_addr_flow6 *) b);
148
  case NET_MPLS:
149
    return net_compare_mpls((const net_addr_mpls *) a, (const net_addr_mpls *) b);
150
  }
151
  return 0;
152
}
153

    
154
#define NET_HASH(a,t) net_hash_##t((const net_addr_##t *) a)
155

    
156
u32
157
net_hash(const net_addr *n)
158
{
159
  switch (n->type)
160
  {
161
  case NET_IP4: return NET_HASH(n, ip4);
162
  case NET_IP6: return NET_HASH(n, ip6);
163
  case NET_VPN4: return NET_HASH(n, vpn4);
164
  case NET_VPN6: return NET_HASH(n, vpn6);
165
  case NET_ROA4: return NET_HASH(n, roa4);
166
  case NET_ROA6: return NET_HASH(n, roa6);
167
  case NET_FLOW4: return NET_HASH(n, flow4);
168
  case NET_FLOW6: return NET_HASH(n, flow6);
169
  default: bug("invalid type");
170
  }
171
}
172

    
173

    
174
int
175
net_validate(const net_addr *N)
176
{
177
  switch (N->type)
178
  {
179
  case NET_IP4:
180
  case NET_VPN4:
181
  case NET_ROA4:
182
  case NET_FLOW4:
183
    return net_validate_ip4((net_addr_ip4 *) N);
184

    
185
  case NET_IP6:
186
  case NET_VPN6:
187
  case NET_ROA6:
188
  case NET_FLOW6:
189
    return net_validate_ip6((net_addr_ip6 *) N);
190

    
191
  case NET_MPLS:
192
    return net_validate_mpls((net_addr_mpls *) N);
193

    
194
  default:
195
    return 0;
196
  }
197
}
198

    
199
void
200
net_normalize(net_addr *N)
201
{
202
  net_addr_union *n = (void *) N;
203

    
204
  switch (n->n.type)
205
  {
206
  case NET_IP4:
207
  case NET_VPN4:
208
  case NET_ROA4:
209
  case NET_FLOW4:
210
    return net_normalize_ip4(&n->ip4);
211

    
212
  case NET_IP6:
213
  case NET_VPN6:
214
  case NET_ROA6:
215
  case NET_FLOW6:
216
    return net_normalize_ip6(&n->ip6);
217

    
218
  case NET_MPLS:
219
    return;
220
  }
221
}
222

    
223
int
224
net_classify(const net_addr *N)
225
{
226
  net_addr_union *n = (void *) N;
227

    
228
  switch (n->n.type)
229
  {
230
  case NET_IP4:
231
  case NET_VPN4:
232
  case NET_ROA4:
233
  case NET_FLOW4:
234
    return ip4_zero(n->ip4.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip4_classify(n->ip4.prefix);
235

    
236
  case NET_IP6:
237
  case NET_VPN6:
238
  case NET_ROA6:
239
  case NET_FLOW6:
240
    return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);
241

    
242
  case NET_MPLS:
243
    return IADDR_HOST | SCOPE_UNIVERSE;
244
  }
245

    
246
  return IADDR_INVALID;
247
}
248

    
249
int
250
ipa_in_netX(const ip_addr a, const net_addr *n)
251
{
252
  switch (n->type)
253
  {
254
  case NET_IP4:
255
  case NET_VPN4:
256
  case NET_ROA4:
257
  case NET_FLOW4:
258
    if (!ipa_is_ip4(a)) return 0;
259
    return ip4_zero(ip4_and(ip4_xor(ipa_to_ip4(a), net4_prefix(n)),
260
                            ip4_mkmask(net4_pxlen(n))));
261

    
262
  case NET_IP6:
263
  case NET_VPN6:
264
  case NET_ROA6:
265
  case NET_FLOW6:
266
    if (ipa_is_ip4(a)) return 0;
267
    return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)),
268
                            ip6_mkmask(net6_pxlen(n))));
269

    
270
  case NET_MPLS:
271
  default:
272
    return 0;
273
  }
274
}
275

    
276
int
277
net_in_netX(const net_addr *a, const net_addr *n)
278
{
279
  if (a->type != n->type)
280
    return 0;
281

    
282
  return (net_pxlen(n) <= net_pxlen(a)) && ipa_in_netX(net_prefix(a), n);
283
}