summaryrefslogtreecommitdiff
path: root/lib/net.c
blob: 91a4474ae3e04f81ccda3c1dd5cb77c6fe29fbce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

#include "nest/bird.h"
#include "lib/ip.h"
#include "lib/net.h"
#include "lib/flowspec.h"


const char * const net_label[] = {
  [NET_IP4] 	= "ipv4",
  [NET_IP6] 	= "ipv6",
  [NET_VPN4] 	= "vpn4",
  [NET_VPN6] 	= "vpn6",
  [NET_ROA4] 	= "roa4",
  [NET_ROA6] 	= "roa6",
  [NET_FLOW4] 	= "flow4",
  [NET_FLOW6] 	= "flow6",
  [NET_MPLS]	= "mpls",
};

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),
  [NET_ROA4] 	= sizeof(net_addr_roa4),
  [NET_ROA6] 	= sizeof(net_addr_roa6),
  [NET_FLOW4] 	= 0,
  [NET_FLOW6] 	= 0,
  [NET_MPLS]	= sizeof(net_addr_mpls),
};

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] 	= IP6_MAX_PREFIX_LENGTH,
  [NET_ROA4] 	= IP4_MAX_PREFIX_LENGTH,
  [NET_ROA6] 	= IP6_MAX_PREFIX_LENGTH,
  [NET_FLOW4] 	= IP4_MAX_PREFIX_LENGTH,
  [NET_FLOW6] 	= IP6_MAX_PREFIX_LENGTH,
  [NET_MPLS]	= 0,
};

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" */
  [NET_ROA4] 	= 34,	/* "255.255.255.255/32-32 AS4294967295" */
  [NET_ROA6] 	= 60,	/* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128-128 AS4294967295" */
  [NET_FLOW4] 	= 0,	/* "flow4 { ... }" */
  [NET_FLOW6] 	= 0,	/* "flow6 { ... }" */
  [NET_MPLS]	= 7,	/* "1048575" */
};


int
net_format(const net_addr *N, char *buf, int buflen)
{
  net_addr_union *n = (void *) N;

  switch (n->n.type)
  {
  case NET_IP4:
    return bsnprintf(buf, buflen, "%I4/%d", n->ip4.prefix, n->ip4.pxlen);
  case NET_IP6:
    return bsnprintf(buf, buflen, "%I6/%d", n->ip6.prefix, n->ip6.pxlen);
  case NET_VPN4:
    return bsnprintf(buf, buflen, "%u:%u %I4/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
  case NET_VPN6:
    return bsnprintf(buf, buflen, "%u:%u %I6/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
  case NET_ROA4:
    return bsnprintf(buf, buflen, "%I4/%u-%u AS%u",  n->roa4.prefix, n->roa4.pxlen, n->roa4.max_pxlen, n->roa4.asn);
  case NET_ROA6:
    return bsnprintf(buf, buflen, "%I6/%u-%u AS%u",  n->roa6.prefix, n->roa6.pxlen, n->roa6.max_pxlen, n->roa6.asn);
  case NET_FLOW4:
    return flow4_net_format(buf, buflen, &n->flow4);
  case NET_FLOW6:
    return flow6_net_format(buf, buflen, &n->flow6);
  case NET_MPLS:
    return bsnprintf(buf, buflen, "%u", n->mpls.label);
  }

  return 0;
}

ip_addr
net_pxmask(const net_addr *a)
{
  switch (a->type)
  {
  case NET_IP4:
  case NET_VPN4:
  case NET_ROA4:
  case NET_FLOW4:
    return ipa_from_ip4(ip4_mkmask(net4_pxlen(a)));

  case NET_IP6:
  case NET_VPN6:
  case NET_ROA6:
  case NET_FLOW6:
    return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));

  case NET_MPLS:
  default:
    return IPA_NONE;
  }
}

int
net_compare(const net_addr *a, const net_addr *b)
{
  if (a->type != b->type)
    return uint_cmp(a->type, b->type);

  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);
  case NET_ROA4:
    return net_compare_roa4((const net_addr_roa4 *) a, (const net_addr_roa4 *) b);
  case NET_ROA6:
    return net_compare_roa6((const net_addr_roa6 *) a, (const net_addr_roa6 *) b);
  case NET_FLOW4:
    return net_compare_flow4((const net_addr_flow4 *) a, (const net_addr_flow4 *) b);
  case NET_FLOW6:
    return net_compare_flow6((const net_addr_flow6 *) a, (const net_addr_flow6 *) b);
  case NET_MPLS:
    return net_compare_mpls((const net_addr_mpls *) a, (const net_addr_mpls *) b);
  }
  return 0;
}

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

u32
net_hash(const net_addr *n)
{
  switch (n->type)
  {
  case NET_IP4: return NET_HASH(n, ip4);
  case NET_IP6: return NET_HASH(n, ip6);
  case NET_VPN4: return NET_HASH(n, vpn4);
  case NET_VPN6: return NET_HASH(n, vpn6);
  case NET_ROA4: return NET_HASH(n, roa4);
  case NET_ROA6: return NET_HASH(n, roa6);
  case NET_FLOW4: return NET_HASH(n, flow4);
  case NET_FLOW6: return NET_HASH(n, flow6);
  default: bug("invalid type");
  }
}


int
net_validate(const net_addr *N)
{
  switch (N->type)
  {
  case NET_IP4:
  case NET_VPN4:
  case NET_ROA4:
  case NET_FLOW4:
    return net_validate_ip4((net_addr_ip4 *) N);

  case NET_IP6:
  case NET_VPN6:
  case NET_ROA6:
  case NET_FLOW6:
    return net_validate_ip6((net_addr_ip6 *) N);

  case NET_MPLS:
    return net_validate_mpls((net_addr_mpls *) N);

  default:
    return 0;
  }
}

void
net_normalize(net_addr *N)
{
  net_addr_union *n = (void *) N;

  switch (n->n.type)
  {
  case NET_IP4:
  case NET_VPN4:
  case NET_ROA4:
  case NET_FLOW4:
    return net_normalize_ip4(&n->ip4);

  case NET_IP6:
  case NET_VPN6:
  case NET_ROA6:
  case NET_FLOW6:
    return net_normalize_ip6(&n->ip6);

  case NET_MPLS:
    return;
  }
}

int
net_classify(const net_addr *N)
{
  net_addr_union *n = (void *) N;

  switch (n->n.type)
  {
  case NET_IP4:
  case NET_VPN4:
  case NET_ROA4:
  case NET_FLOW4:
    return ip4_zero(n->ip4.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip4_classify(n->ip4.prefix);

  case NET_IP6:
  case NET_VPN6:
  case NET_ROA6:
  case NET_FLOW6:
    return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);

  /* classify probably not needed for NET_MPLS */
  }

  return IADDR_INVALID;
}

int
ipa_in_netX(const ip_addr a, const net_addr *n)
{
  switch (n->type)
  {
  case NET_IP4:
  case NET_VPN4:
  case NET_ROA4:
  case NET_FLOW4:
    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))));

  case NET_IP6:
  case NET_VPN6:
  case NET_ROA6:
  case NET_FLOW6:
    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))));

  case NET_MPLS:
  default:
    return 0;
  }
}

int
net_in_netX(const net_addr *a, const net_addr *n)
{
  if (a->type != n->type)
    return 0;

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