net.c 8.92 KB
Newer Older
1 2 3 4

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

7

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

21
const u16 net_addr_length[] = {
22 23 24 25 26 27 28 29
  [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,
30
  [NET_IP6_SADR]= sizeof(net_addr_ip6_sadr),
31
  [NET_MPLS]	= sizeof(net_addr_mpls),
32
};
33

34
const u8 net_max_prefix_length[] = {
35 36 37 38 39 40 41 42
  [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,
43
  [NET_IP6_SADR]= IP6_MAX_PREFIX_LENGTH,
44
  [NET_MPLS]	= 0,
45 46
};

47
const u16 net_max_text_length[] = {
48 49 50 51 52 53 54 55
  [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 { ... }" */
56
  [NET_IP6_SADR]= 92,	/* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128 from ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
57
  [NET_MPLS]	= 7,	/* "1048575" */
58 59
};

60

61 62 63 64 65
int
rd_format(const u64 rd, char *buf, int buflen)
{
  switch (rd >> 48)
  {
66 67 68 69 70 71
    case 0: return bsnprintf(buf, buflen, "%u:%u", (u32) (rd >> 32), (u32) rd);
    case 1: return bsnprintf(buf, buflen, "%I4:%u", ip4_from_u32(rd >> 16), (u32) (rd & 0xffff));
    case 2: if (((u32) (rd >> 16)) >> 16)
	      return bsnprintf(buf, buflen, "%u:%u", (u32) (rd >> 16), (u32) (rd & 0xffff));
	    else
	      return bsnprintf(buf, buflen, "2:%u:%u", (u32) (rd >> 16), (u32) (rd & 0xffff));
72 73 74 75
    default: return bsnprintf(buf, buflen, "X:%08x:%08x", (u32) (rd >> 32), (u32) rd);
  }
}

76
int
77 78 79
net_format(const net_addr *N, char *buf, int buflen)
{
  net_addr_union *n = (void *) N;
80
  buf[0] = 0;
81 82 83 84

  switch (n->n.type)
  {
  case NET_IP4:
85
    return bsnprintf(buf, buflen, "%I4/%d", n->ip4.prefix, n->ip4.pxlen);
86
  case NET_IP6:
87
    return bsnprintf(buf, buflen, "%I6/%d", n->ip6.prefix, n->ip6.pxlen);
88
  case NET_VPN4:
89
    {
90
    int c = rd_format(n->vpn4.rd, buf, buflen);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
91
    ADVANCE(buf, buflen, c);
92
    return bsnprintf(buf, buflen, " %I4/%d", n->vpn4.prefix, n->vpn4.pxlen);
93
    }
94
  case NET_VPN6:
95
    {
96 97
    /* XXX: RD format is specified for VPN4; not found any for VPN6, reusing the same as for VPN4 */
    int c = rd_format(n->vpn6.rd, buf, buflen);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
98
    ADVANCE(buf, buflen, c);
99
    return bsnprintf(buf, buflen, " %I6/%d", n->vpn6.prefix, n->vpn6.pxlen);
100
    }
101
  case NET_ROA4:
102
    return bsnprintf(buf, buflen, "%I4/%u-%u AS%u",  n->roa4.prefix, n->roa4.pxlen, n->roa4.max_pxlen, n->roa4.asn);
103
  case NET_ROA6:
104
    return bsnprintf(buf, buflen, "%I6/%u-%u AS%u",  n->roa6.prefix, n->roa6.pxlen, n->roa6.max_pxlen, n->roa6.asn);
105 106 107 108
  case NET_FLOW4:
    return flow4_net_format(buf, buflen, &n->flow4);
  case NET_FLOW6:
    return flow6_net_format(buf, buflen, &n->flow6);
109 110
  case NET_IP6_SADR:
    return bsnprintf(buf, buflen, "%I6/%d from %I6/%d", n->ip6_sadr.dst_prefix, n->ip6_sadr.dst_pxlen, n->ip6_sadr.src_prefix, n->ip6_sadr.src_pxlen);
111 112
  case NET_MPLS:
    return bsnprintf(buf, buflen, "%u", n->mpls.label);
113
  }
114

115
  bug("unknown network type");
116 117 118 119 120 121 122 123 124
}

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

  case NET_IP6:
  case NET_VPN6:
131
  case NET_ROA6:
132
  case NET_FLOW6:
133
  case NET_IP6_SADR:
134 135
    return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));

136
  case NET_MPLS:
137 138 139 140 141
  default:
    return IPA_NONE;
  }
}

142 143
int
net_compare(const net_addr *a, const net_addr *b)
144
{
145 146
  if (a->type != b->type)
    return uint_cmp(a->type, b->type);
147

148 149 150 151 152 153 154 155 156 157
  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);
158 159 160 161
  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);
162 163 164 165
  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);
166 167
  case NET_IP6_SADR:
    return net_compare_ip6_sadr((const net_addr_ip6_sadr *) a, (const net_addr_ip6_sadr *) b);
168 169
  case NET_MPLS:
    return net_compare_mpls((const net_addr_mpls *) a, (const net_addr_mpls *) b);
170 171
  }
  return 0;
172 173
}

Ondřej Zajíček's avatar
Ondřej Zajíček committed
174 175 176 177 178 179 180 181 182 183 184 185 186
#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);
187 188
  case NET_FLOW4: return NET_HASH(n, flow4);
  case NET_FLOW6: return NET_HASH(n, flow6);
189
  case NET_IP6_SADR: return NET_HASH(n, ip6_sadr);
190
  case NET_MPLS: return NET_HASH(n, mpls);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
191 192 193 194 195
  default: bug("invalid type");
  }
}


196 197
#define NET_VALIDATE(a,t) net_validate_##t((const net_addr_##t *) a)

198
int
199
net_validate(const net_addr *n)
200
{
201
  switch (n->type)
202
  {
203 204 205 206 207 208 209 210
  case NET_IP4: return NET_VALIDATE(n, ip4);
  case NET_IP6: return NET_VALIDATE(n, ip6);
  case NET_VPN4: return NET_VALIDATE(n, vpn4);
  case NET_VPN6: return NET_VALIDATE(n, vpn6);
  case NET_ROA4: return NET_VALIDATE(n, roa4);
  case NET_ROA6: return NET_VALIDATE(n, roa6);
  case NET_FLOW4: return NET_VALIDATE(n, flow4);
  case NET_FLOW6: return NET_VALIDATE(n, flow6);
211
  case NET_IP6_SADR: return NET_VALIDATE(n, ip6_sadr);
212 213
  case NET_MPLS: return NET_VALIDATE(n, mpls);
  default: return 0;
214
  }
215 216
}

217 218 219 220 221 222 223 224 225
void
net_normalize(net_addr *N)
{
  net_addr_union *n = (void *) N;

  switch (n->n.type)
  {
  case NET_IP4:
  case NET_VPN4:
226
  case NET_ROA4:
227
  case NET_FLOW4:
228 229 230 231
    return net_normalize_ip4(&n->ip4);

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

236 237 238
  case NET_IP6_SADR:
    return net_normalize_ip6_sadr(&n->ip6_sadr);

239 240
  case NET_MPLS:
    return;
241 242 243
  }
}

244 245 246 247 248 249 250 251 252
int
net_classify(const net_addr *N)
{
  net_addr_union *n = (void *) N;

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

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

263 264 265
  case NET_IP6_SADR:
    return ip6_zero(n->ip6_sadr.dst_prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6_sadr.dst_prefix);

266 267
  case NET_MPLS:
    return IADDR_HOST | SCOPE_UNIVERSE;
268
  }
269

270
  return IADDR_INVALID;
271
}
272 273

int
274
ipa_in_netX(const ip_addr a, const net_addr *n)
275
{
276
  switch (n->type)
277 278 279
  {
  case NET_IP4:
  case NET_VPN4:
280
  case NET_ROA4:
281
  case NET_FLOW4:
282 283 284
    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))));
285 286 287

  case NET_IP6:
  case NET_VPN6:
288
  case NET_ROA6:
289
  case NET_FLOW6:
290 291 292
    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))));
293

294 295 296 297 298
  case NET_IP6_SADR:
    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))));

299
  case NET_MPLS:
300 301 302
  default:
    return 0;
  }
303 304 305
}

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

311
  return (net_pxlen(n) <= net_pxlen(a)) && ipa_in_netX(net_prefix(a), n);
312
}
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

#define CHECK_NET(T,S) \
  ({ if (sizeof(T) != S) die("sizeof %s is %d/%d", #T, (int) sizeof(T), S); })

void
net_init(void)
{
  CHECK_NET(net_addr,		24);
  CHECK_NET(net_addr_ip4,	 8);
  CHECK_NET(net_addr_ip6,	20);
  CHECK_NET(net_addr_vpn4,	16);
  CHECK_NET(net_addr_vpn6,	32);
  CHECK_NET(net_addr_roa4,	16);
  CHECK_NET(net_addr_roa6,	28);
  CHECK_NET(net_addr_flow4,	 8);
  CHECK_NET(net_addr_flow6,	20);
329
  CHECK_NET(net_addr_ip6_sadr,	40);
330 331
  CHECK_NET(net_addr_mpls,	 8);
}